-
Notifications
You must be signed in to change notification settings - Fork 5
/
driver.py
192 lines (159 loc) · 6.87 KB
/
driver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import numpy as np
import netCDF4
import matplotlib.pyplot as plt
import datetime as dt
import my_settings
import preProcess
import llMesh
import segment
import basinMetrics
import correspond
import tracks
printTiming = True
from datetime import datetime
def demo():
""" tpvTrack driver. Using my_settings user-input, runs pre-process, segment, metrics, correspondence, and tracks."""
#setup -----------------
info = my_settings.info
filesData = my_settings.filesData
fMesh = my_settings.fMesh
fMetr = my_settings.fMetr
fSeg = my_settings.fSeg
fCorr = my_settings.fCorr
fTrack = my_settings.fTrack
fMetrics = my_settings.fMetrics
rEarth = my_settings.rEarth
dRegion = my_settings.dFilter
latThresh = my_settings.latThresh
#datetime objects
timeStartGlobal = my_settings.timeStart
deltaTGlobal = my_settings.timeDelta
#pre-process ------------------------
if (my_settings.doPreProc):
if (printTiming):
tStart = datetime.now()
if (my_settings.inputType=='eraI'):
mesh, cell0 = preProcess.demo_eraI(fMesh, filesData, fMetr, rEarth, dRegion, latThresh, my_settings.iTimeStart_fData, my_settings.iTimeEnd_fData, info=info)
elif (my_settings.inputType=='mpas'):
mesh, cell0 = preProcess.demo_mpas(fMesh, filesData, fMetr, rEarth, dRegion, latThresh, my_settings.iTimeStart_fData, my_settings.iTimeEnd_fData, info=info)
elif (my_settings.inputType=='wrf_trop'):
mesh, cell0 = preProcess.demo_wrf_trop(fMesh, filesData, fMetr, rEarth, dRegion, latThresh, my_settings.iTimeStart_fData, my_settings.iTimeEnd_fData,
my_settings.fileMap, info=info, pvIndex=3)
else:
print "Unrecognized input type in my_settings: ",my_settings.inputType
if (printTiming):
tEnd = datetime.now()
print "Time doPreProc: ", tEnd-tStart
else:
#if already processed input data
if (my_settings.inputType=='eraI'):
mesh, cell0 = preProcess.demo_eraI(fMesh, [], fMetr, rEarth, dRegion, latThresh, my_settings.iTimeStart_fData, my_settings.iTimeEnd_fData)
elif (my_settings.inputType=='mpas'):
mesh, cell0 = preProcess.demo_mpas(fMesh, [], fMetr, rEarth, dRegion, latThresh, my_settings.iTimeStart_fData, my_settings.iTimeEnd_fData)
elif (my_settings.inputType=='wrf_trop'):
mesh, cell0 = preProcess.demo_wrf_trop(fMesh, [], fMetr, rEarth, dRegion, latThresh, my_settings.iTimeStart_fData, my_settings.iTimeEnd_fData, my_settings.fileMap)
else:
print "Unrecognized input type in my_settings: ",my_settings.inputType
#segment --------------------------
dataMetr = netCDF4.Dataset(fMetr,'r');
nTimes = len(dataMetr.dimensions['time']); #nTimes = 5
if (my_settings.doSeg):
if (printTiming):
tStart = datetime.now()
#segment.run_segment(fSeg, info, dataMetr, cell0.copy(), mesh, nTimes)
segment.run_segment(fSeg, info, dataMetr, cell0.copy(), mesh, nTimes, segRestrictPerc=my_settings.segRestrictPerc)
if (False):
segment.run_plotBasins(my_settings.fDirSave, dataMetr, fSeg, mesh)
if (printTiming):
tEnd = datetime.now()
print "Time doSeg: ", tEnd-tStart
dataMetr.close()
#spatial metrics ------------------------
dataMetr = netCDF4.Dataset(fMetr,'r')
dataSeg = netCDF4.Dataset(fSeg,'r')
if (my_settings.doMetrics):
if (printTiming):
tStart = datetime.now()
basinMetrics.run_metrics(fMetrics, info, mesh, dataMetr, dataSeg, 0, nTimes-1)
if (printTiming):
tEnd = datetime.now()
print "Time doMetrics: ", tEnd-tStart
dataMetr.close()
dataSeg.close()
#basinMetrics.print_metrics(fMetrics)
#time correspondence -----------------
dataMetr = netCDF4.Dataset(fMetr,'r')
dataSeg = netCDF4.Dataset(fSeg,'r')
dataMetrics = netCDF4.Dataset(fMetrics, 'r')
if (my_settings.doCorr):
if (printTiming):
tStart = datetime.now()
correspond.run_correspond(fCorr, dataMetr, dataSeg, mesh, my_settings.deltaT, my_settings.trackMinMaxBoth, my_settings.areaOverlap, 0, nTimes-1, dataMetrics)
if (False):
correspond.plot_correspondences(my_settings.fDirSave, fCorr, nTimes-1, mesh)
if (printTiming):
tEnd = datetime.now()
print "Time doCorr: ", tEnd-tStart
dataMetrics.close()
dataSeg.close()
dataMetr.close()
#time tracks -------------------------
if (my_settings.doTracks):
if (printTiming):
tStart = datetime.now()
#since appending to fTrack over time, wipe file before starting (if it exists)
my_settings.silentremove(fTrack)
tracks.run_tracks_timeInterval(fTrack, fCorr, 0, nTimes-1, timeStartGlobal, deltaTGlobal, fMetrics=fMetrics, trackOnlyMajor=True)
if (False):
tracks.plot_tracks_metrics(fTrack, my_settings.fDirSave+'test_tracks.png')
#tracks.plot_tracks_cells(fTrack, mesh, my_settings.fDirSave)
if (printTiming):
tEnd = datetime.now()
print "Time doTracks: ", tEnd-tStart
#time metrics ----------------------
def demo_algo_plots():
#setup -----------------
info = my_settings.info
filesData = my_settings.filesData
fMesh = my_settings.fMesh
fMetr = my_settings.fMetr
fSeg = my_settings.fSeg
fCorr = my_settings.fCorr
fTrack = my_settings.fTrack
fMetrics = my_settings.fMetrics
rEarth = my_settings.rEarth
dRegion = my_settings.dFilter
latThresh = my_settings.latThresh
#pre-process ------------------------
#if already processed input data
if (my_settings.inputType=='eraI'):
mesh, cell0 = preProcess.demo_eraI(fMesh, [], fMetr, my_settings.rEarth, dRegion, latThresh, my_settings.iTimeStart_fData, my_settings.iTimeEnd_fData)
elif (my_settings.inputType=='mpas'):
mesh, cell0 = preProcess.demo_mpas(fMesh, [], fMetr, my_settings.rEarth, dRegion, latThresh, my_settings.iTimeStart_fData, my_settings.iTimeEnd_fData)
elif (my_settings.inputType=='wrf_trop'):
mesh, cell0 = preProcess.demo_wrf_trop(fMesh, [], fMetr, rEarth, dRegion, latThresh, my_settings.iTimeStart_fData, my_settings.iTimeEnd_fData, my_settings.fileMap)
else:
print "Unrecognized input type in my_settings: ",my_settings.inputType
#segment --------------------------
if (True):
dataMetr = netCDF4.Dataset(fMetr,'r');
segment.run_plotBasins(my_settings.fDirSave, dataMetr, fSeg, mesh)
dataMetr.close()
#time correspondence -----------------
if (True):
dataMetr = netCDF4.Dataset(fMetr,'r')
dataSeg = netCDF4.Dataset(fSeg,'r')
dataMetrics = netCDF4.Dataset(fMetrics, 'r')
nTimes = len(dataSeg.dimensions['time'])
correspond.plot_correspondences(my_settings.fDirSave, fCorr, nTimes-1, mesh)
dataMetrics.close()
dataSeg.close()
dataMetr.close()
#time tracks -------------------------
if (True):
tracks.plot_tracks_metrics(fTrack, my_settings.fDirSave+'test_tracks.png')
#time metrics ----------------------
if __name__=='__main__':
demo()
demo_algo_plots()
#