##// END OF EJS Templates
UPDATE TEST ZDR
avaldezp -
r1586:8b29f13d0a2c
parent child
Show More
@@ -1,356 +1,362
1 1 import numpy,os,time
2 2 import matplotlib
3 3 import argparse
4 4 import matplotlib.pyplot as plt
5 5 from wradlib.io import read_generic_hdf5
6 6 from wradlib.util import get_wradlib_data_file
7 7 from plotting_codes import sophy_cb_tables
8 8 from scipy import stats
9 9
10 10 for name, cb_table in sophy_cb_tables:
11 11 ncmap = matplotlib.colors.ListedColormap(cb_table, name=name)
12 12 matplotlib.pyplot.register_cmap(cmap=ncmap)
13 13 '''
14 14 NOTA:
15 15 - python3.10
16 16 - Conda environment:
17 17 WR_CONDA_JUN14 * /home/soporte/anaconda3/envs/WR_CONDA_JUN14
18 18 export WRADLIB_DATA = "/media/soporte/TOSHIBAEXT/sophy/HYO_CC4_CC64_COMB@2022-12-26T00-00-32/param-EVENTO/"
19 19 - Update de plotting_codes
20 20 '''
21 21 PARAM = {
22 22 'S': {'var': 'power','vmin': -45, 'vmax': -15, 'cmap': 'jet', 'label': 'Power','unit': 'dBm'},
23 23 'V': {'var': 'velocity', 'vmin': -10, 'vmax': 10 , 'cmap': 'sophy_v', 'label': 'Velocity','unit': 'm/s'},
24 24 'Z': {'var': 'reflectivity','vmin': -20, 'vmax': 80 , 'cmap': 'sophy_z','label': 'Reflectivity','unit': 'dBZ'},
25 25 'W': {'var': 'spectral_width', 'vmin': 0 , 'vmax': 12 , 'cmap': 'sophy_w','label': 'Spectral Width','unit': 'm/s'},
26 26 'R': {'var':'rhoHV','vmin': 0.2, 'vmax': 1, 'cmap': 'sophy_r','label': 'RhoHV', 'unit': ' '},
27 27 'D': {'var': 'differential_reflectivity','vmin': -9, 'vmax': 12, 'cmap': 'sophy_d','label': 'ZDR' , 'unit': 'dB'}
28 28 }
29 29
30 30 class Readsophy():
31 31 def __init__(self):
32 32 self.list_file = None
33 33 self.grado = None
34 34 self.variable = None
35 35 self.save = None
36 36 self.range = None
37 37 def setup(self, path_file,mode,type,grado,range,r_min,variable,save):
38 38 self.path_file = path_file
39 39 self.mode = mode
40 40 self.range = range
41 41 self.grado = grado
42 42 self.r_min = r_min
43 43 self.variable = variable
44 44 self.save = save
45 45 self.type_ = type
46 46 self.list_file = self.read_files(path_file=self.path_file,mode=self.mode,grado=self.grado, variable=self.variable)
47 47 print("self.list_file",self.list_file)
48 48
49 49 def read_files(self,path_file,mode=None,grado=None, variable=None):
50 50 if mode =='PPI':
51 51 filter= "_E"+str(grado)+".0_"+variable
52 52 else:
53 53 filter= "_A"+str(grado)+".0_"+variable
54 54 print("Filter :",filter)
55 55 validFilelist = []
56 56 fileList= os.listdir(path_file)
57 57 for thisFile in fileList:
58 58 #print(thisFile)
59 59 if not os.path.splitext(thisFile)[0][-7:] in filter:
60 60 print("s_:",os.path.splitext(thisFile)[0][-7:])
61 61 continue
62 62 validFilelist.append(thisFile)
63 63 validFilelist.sort()
64 64 return validFilelist
65 65
66 66 def readAttributes(self,obj,variable):
67 67 var = PARAM[variable]['var']
68 68 unit = PARAM[variable]['unit']
69 69 cmap = PARAM[variable]['cmap']
70 70 vmin = PARAM[variable]['vmin']
71 71 vmax = PARAM[variable]['vmax']
72 72 label = PARAM[variable]['label']
73 73 var_ = 'Data/'+var+'/H'
74 74 data_arr = numpy.array(obj[var_]['data']) # data
75 75 utc_time = numpy.array(obj['Data/time']['data'])
76 76 data_azi = numpy.array(obj['Metadata/azimuth']['data']) # th
77 77 data_ele = numpy.array(obj["Metadata/elevation"]['data'])
78 78 heightList = numpy.array(obj["Metadata/range"]['data']) # r
79 79 return data_arr, utc_time, data_azi,data_ele, heightList,unit,cmap,vmin,vmax,label
80 80
81 81 def selectHeights(self,heightList,minHei,maxHei):
82 82
83 83 if minHei and maxHei:
84 84 if (minHei < heightList[0]):
85 85 minHei = heightList[0]
86 86 if (maxHei > heightList[-1]):
87 87 maxHei = heightList[-1]
88 88 minIndex = 0
89 89 maxIndex = 0
90 90 heights = heightList
91 91
92 92 inda = numpy.where(heights >= minHei)
93 93 indb = numpy.where(heights <= maxHei)
94 94
95 95 try:
96 96 minIndex = inda[0][0]
97 97 except:
98 98 minIndex = 0
99 99
100 100 try:
101 101 maxIndex = indb[0][-1]
102 102 except:
103 103 maxIndex = len(heights)
104 104
105 105 new_heightList= self.selectHeightsByIndex(heightList=heightList,minIndex=minIndex, maxIndex=maxIndex)
106 106
107 107 return new_heightList, minIndex,maxIndex
108 108
109 109 def selectHeightsByIndex(self,heightList,minIndex, maxIndex):
110 110
111 111 if (minIndex < 0) or (minIndex > maxIndex):
112 112 raise ValueError("Height index range (%d,%d) is not valid" % (minIndex, maxIndex))
113 113
114 114 if (maxIndex >= len(heightList)):
115 115 maxIndex = len(heightList)
116 116
117 117 new_h = heightList[minIndex:maxIndex]
118 118 return new_h
119 119
120 120 def plot_RTI_PPI_RHI(self,count,x,y,z,cmap,my_time,vmin,vmax,label,unit,mode,grado):
121 121 if count==1:
122 122 fig = plt.figure(figsize=(8,6))
123 123 plt.pcolormesh(x,y,z,cmap =cmap, vmin = vmin, vmax = vmax)
124 title = 'Sophy Plot'+label+"-"+ my_time+" "+mode+" "+grado
124 title = 'Sophy Plot '+label+"-"+ my_time+" "+mode+" "+grado+" NΒ° "+str(count)
125 125 t = plt.title(title, fontsize=12,y=1.05)
126 126 cbar = plt.colorbar()
127 127 cbar.set_label(label+'[' + unit + ']')
128 128 else:
129 129 plt.pcolormesh(x,y,z, cmap =cmap, vmin = vmin, vmax = vmax)
130 title = 'Sophy Plot'+label+"-"+ my_time+" "+mode+" "+grado
130 title = 'Sophy Plot '+label+"-"+ my_time+" "+mode+" "+grado+" NΒ° "+str(count)
131 131 t = plt.title(title, fontsize=12,y=1.05)
132 132 cbar = plt.colorbar()
133 133 cbar.set_label(label+'[' + unit + ']')
134 134
135 135 def plot_PROFILE(self,count,z,y,my_time,label,mode,grado):
136 136 if count==1:
137 137 fig = plt.figure(figsize=(8,6))
138 plt.plot(z,y)
139 title = 'Sophy Plot '+label+"-"+ my_time+" "+mode+" "+grado
138 plt.plot(numpy.nanmean(z,1),y)
139 title = 'Sophy Plot '+label+"-"+ my_time+" "+mode+" "+grado+" NΒ° "+str(count)
140 140 t = plt.title(title, fontsize=12,y=1.05)
141 141 plt.ylim(0,self.range+1)
142 142 plt.xlabel(label)
143 143 plt.ylabel('Height(Km)')
144 144 if self.variable=="R":
145 145 plt.xlim(-1,3)
146 146 if self.variable=='D':
147 147 plt.xlim(-10,10)
148 if self.variable=='Z':
149 plt.xlim(-20,80)
148 150 else:
149 plt.plot(z,y)
150 title = 'Sophy Plot '+label+"-"+ my_time+" "+mode+" "+grado
151 plt.plot(numpy.nanmean(z,1),y)
152 title = 'Sophy Plot '+label+"-"+ my_time+" "+mode+" "+grado+" NΒ° "+str(count)
151 153 t = plt.title(title, fontsize=12,y=1.05)
152 154 plt.ylim(0,self.range+1)
153 155 plt.xlabel(label)
154 156 plt.ylabel('Height(Km)')
155 157 if self.variable=="R":
156 158 plt.xlim(-1,3)
157 159 if self.variable=='D':
158 160 plt.xlim(-10,10)
161 if self.variable=='Z':
162 plt.xlim(-20,80)
159 163
160 164 def save_PIC(self,count,time_save):
161 165 if count ==1:
162 166 filename = "SOPHY"+"_"+time_save+"_"+self.mode+"_"+self.grado+"_"+self.variable+str(self.range)+".png"
163 167 dir =self.variable+"_"+self.mode+self.grado+"CH0/"
164 168 filesavepath = os.path.join(self.path_file,dir)
165 169 try:
166 170 os.mkdir(filesavepath)
167 171 except:
168 172 pass
169 173 else:
170 174 dir =self.variable+"_"+self.mode+self.grado+"CH0/"
171 175 filesavepath = os.path.join(self.path_file,dir)
172 176 filename = "SOPHY"+"_"+time_save+"_"+"E."+self.grado+"_"+self.variable+str(self.range)+".png"
173 177 plt.savefig(filesavepath+filename)
174 178
175 def seleccion_roHV_min(self,count,z,arr):
179 def seleccion_roHV_min(self,count,z,y,arr_):
180 ##print("y",y)
176 181 if self.variable=='R':
177 182 len_Z= z.shape[1]
178 183 min_CC=numpy.zeros(len_Z)
179 184 min_index_CC = numpy.zeros(len_Z)
180 185 for i in range(len_Z):
181 186 tmp=numpy.nanmin(z[:,i])
182 187 tmp_index = numpy.nanargmin((z[:,i]))
183
184 if tmp <0.6:
188 if tmp <0.5:
185 189 tmp_index= numpy.nan
186 190 value = numpy.nan
187 191 else:
188 value= new_heightList[tmp_index]
192 value= y[tmp_index]
189 193 min_CC[i] =value
190 194 moda_,count_m_ = stats.mode(min_CC)
191 print(moda_)
195 #print("MODA",moda_)
192 196 for i in range(len_Z):
193 197 if min_CC[i]>moda_[0]+0.15 or min_CC[i]<moda_[0]-0.15:
194 198 min_CC[i]=numpy.nan
195
196 min_index_CC=min_CC/0.06
199 print("MIN_CC",min_CC)
200 print("y[0]",y[0])
201 min_index_CC=((min_CC-y[0])/0.06)
197 202 if count == 0:
198 203 arr_ = min_index_CC
199 204 else:
200 205 arr_ = numpy.append(arr_,min_index_CC)
206 print("arr_",min_index_CC)
201 207 return arr_
202 208 else:
203 print("Operation JUST for roHV ")
209 print("Operation JUST for roHV - EXIT ")
210 exit()
211
212 def pp_BB(self,count,x,y,z,filename,c_max,prom_List):
213 #print("z shape",z.shape)
214 len_Z = z.shape[1]
215 len_X = x.shape[0]
216 try:
217 min_CC = numpy.load(filename)
218 except:
219 print("There is no file")
220 exit()
221
222 #print(min_CC.shape,len_X)
223 #print(min_CC)
224 if count ==1:
225 c_min = 0
226 c_max = c_max+ len_X
227 plt.plot(x,y[0]+min_CC[c_min:c_max]*0.06,'yo')
228 else:
229 c_min = c_max
230 c_max = c_min+len_X
231 try:
232 plt.plot(x,y[0]+min_CC[c_min:c_max]*0.06,'yo')
233 except:
234 print("Check number of file")
235 return 0
236
237 bb = numpy.zeros(len_Z)
238 min_READ_CC = min_CC[c_min:c_max]
239 #print(min_READ_CC[0:50])
240 for i in range(len_Z):
241 if min_READ_CC[i]==numpy.nan:
242 bb[i]=numpy.nan
243 try:
244 bb[i]=z[int(min_READ_CC[i])][i]
245 except:
246 bb[i]=numpy.nan
247 print("bb _ prom_ZDR",numpy.nanmean(bb))
248 prom_List.append(numpy.nanmean(bb))
249 return c_max
250
204 251
205 252 def run(self):
206 253 count = 0
207 254 len_files = len(self.list_file)
208 255 SAVE_PARAM= []
209 256 for thisFile in self.list_file:
210 257 count= count +1
211 258 print("Count :", count)
212 259 fullpathfile = self.path_file + thisFile
213 260 filename = get_wradlib_data_file(fullpathfile)
214 261 test_hdf5 = read_generic_hdf5(filename)
215 262 # LECTURA
216 263 data_arr, utc_time, data_azi,data_ele, heightList,unit,cmap,vmin,vmax,label = self.readAttributes(obj= test_hdf5,variable=self.variable)
217 264 len_X= data_arr.shape[0]
218 265 # SELECCION DE ALTURAS
219 266 if self.range==0:
220 267 self.range == heightList[-1]
221 268 if self.r_min==0:
222 269 self.r_min = 0.01
223 270 new_heightList,minIndex,maxIndex = self.selectHeights(heightList,self.r_min,self.range)
224 271 # TIEMPO
225 272 utc_time[0] = utc_time[0]+60*60*5
226 273 my_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(utc_time[0]))
227 274 time_save = time.strftime('%Y%m%d_%H%M%S',time.localtime(utc_time[0]))
228 275 data_arr= data_arr[:,minIndex:maxIndex].transpose()
229 276 # VARIABLES
230 277 x=numpy.linspace(1,len_X,len_X)
231 278 y= new_heightList
232 279 z=data_arr
233 280 profile = numpy.mean(z,1)
234 # Seleccion del arreglo
235 '''
236 len_Z= z.shape[1]
237 min_CC=numpy.zeros(len_Z)
238 min_index_CC = numpy.zeros(len_Z)
239 for i in range(len_Z):
240 tmp=numpy.nanmin(z[:,i])
241 tmp_index = numpy.nanargmin((z[:,i]))
242
243 if tmp <0.6:
244 tmp_index= numpy.nan
245 value = numpy.nan
246 else:
247 value= new_heightList[tmp_index]
248 min_CC[i] =value
249 moda_,count_m_ = stats.mode(min_CC)
250 print(moda_)
251 for i in range(len_Z):
252 if min_CC[i]>moda_[0]+0.15 or min_CC[i]<moda_[0]-0.15:
253 min_CC[i]=numpy.nan
254
255 min_index_CC=min_CC/0.06
256 #print(min_CC.shape,min_CC)
257 print("LONGITUD",min_index_CC.shape)
258 '''
259 len_Z= z.shape[1]
260 min_CC = numpy.zeros(len_Z)
261 min_CC = numpy.load("index.npy")
262 bb = numpy.zeros(len_Z)
263
264 for i in range(len_Z):
265 if min_CC[i]==numpy.nan:
266 bb[i]=numpy.nan
267 try:
268 bb[i]=z[int(min_CC[i]*0.06)][i]
269 except:
270 bb[i]=numpy.nan
271 #print("bb ",bb)
272 print("bb _ prom_ZDR",numpy.nanmean(bb))
273 SAVE_PARAM.append(numpy.nanmean(bb))
274 281 if count ==1:
275 282 if self.type_ =="PROFILE":
276 283 self.plot_PROFILE(count=count ,z=z,y=y,my_time=my_time,label=label,mode=self.mode,grado=self.grado)
277 284 if self.type_ =="RTI":
278 285 self.plot_RTI_PPI_RHI(count=count,x=x,y=y,z=z,cmap=cmap,my_time=my_time,vmin=vmin,vmax=vmax,label =label,unit=unit, mode=self.mode,grado=self.grado)
279 286 if self.type_ =='roHV_MIN':
280 arr_= self.seleccion_roHV_min(count=count,z=z,arr_= 0)
281 #arr_ = min_index_CC
282 cota_min = 0
283 cota_max= len_X
284 #plt.plot(x,min_CC[0:len_X]*0.06,'yo')
285
287 arr_= self.seleccion_roHV_min(count=count,z=z,y=y,arr_= 0)
288 if self.type_ =='pp_BB':
289 self.plot_RTI_PPI_RHI(count=count,x=x,y=y,z=z,cmap=cmap,my_time=my_time,vmin=vmin,vmax=vmax,label =label,unit=unit, mode=self.mode,grado=self.grado)
290 c_max = self.pp_BB(count=count,x=x,y=y,z=z,filename='index.npy',c_max=0,prom_List=SAVE_PARAM)
286 291 else:
287 #arr_ = numpy.append(arr_,min_index_CC)
288 292 if self.type_=="RTI":
289 293 self.plot_RTI_PPI_RHI(count=count,x=x,y=y,z=z,cmap=cmap,my_time=my_time,vmin=vmin,vmax=vmax,label =label,unit=unit, mode=self.mode,grado=self.grado)
290 294 if self.type_ =="PROFILE":
291 295 self. plot_PROFILE(count=count,z=z,y=y,my_time=my_time,label=label,mode=self.mode,grado=self.grado)
292 296 if self.type_ =='roHV_MIN':
293 arr_= self.seleccion_roHV_min(count=count,z=z,arr_= arr_)
294
297 arr_= self.seleccion_roHV_min(count=count,z=z,y=y,arr_= arr_)
298 if self.type_ =='pp_BB':
299 self.plot_RTI_PPI_RHI(count=count,x=x,y=y,z=z,cmap=cmap,my_time=my_time,vmin=vmin,vmax=vmax,label =label,unit=unit, mode=self.mode,grado=self.grado)
300 c_max = self.pp_BB(count=count,x=x,y=y,z=z,filename='index.npy',c_max=c_max,prom_List=SAVE_PARAM)
301 if c_max ==0:
302 count=len_files
295 303
296 #arr_ = numpy.append(arr_,min_index_CC)
297 cota_min= cota_min+len_X
298 cota_max= cota_min+len_X
299 #plt.plot(x,min_CC[cota_min:cota_max]*0.06,'yo')
300 print("Y",len_X)
301 304 if self.save == 1:
302 305 self.save_PIC(count=count,time_save=time_save)
303 306 plt.pause(1)
304 307 plt.clf()
305 308 if count == len_files:
306 309 if self.type_ =='roHV_MIN':
307 310 numpy.save("/home/soporte/WRJAN2023/schain/schainpy/scripts/index.npy",arr_)
308 numpy.save("/home/soporte/WRJAN2023/schain/schainpy/scripts/index_"+self.variable+"_prom.npy",SAVE_PARAM)
311 if self.type_ =='pp_BB':
312 numpy.save("/home/soporte/WRJAN2023/schain/schainpy/scripts/index_"+self.variable+"_prom.npy",SAVE_PARAM)
313 print("-----ADIOS-------------------")
309 314 plt.close()
315 exit()
310 316 plt.show()
311 317
312 318
313 319 def main(args):
314 320 grado = args.grado
315 321 parameters = args.parameters
316 322 r_min = args.r_min
317 323 save = args.save
318 324 range = args.range
319 325 mode = args.mode
320 326 type = args.type
321 327 obj = Readsophy()
322 328 print("MODE :", mode)
323 329 if not mode =='PPI' and not mode =='RHI':
324 330 print("Error - Choose Mode RHI or PPI")
325 331 return None
326 332 for param in parameters:
327 333 print("Parameters : ", param)
328 334 if mode =='PPI':
329 335 PATH = "/media/soporte/TOSHIBAEXT/sophy/HYO_CC4_CC64_COMB@2022-12-26T00-00-32/param-EVENTO/"+str(param)+"_PPI_EL_"+str(grado)+".0/"
330 336 else:
331 337 PATH = "/media/soporte/TOSHIBAEXT/sophy/HYO_CC4_CC64_COMB@2022-12-26T00-00-32/param-EVENTO/"+str(param)+"_RHI_AZ_"+str(grado)+".0/"
332 338 print("Path : ",PATH)
333 339 obj.setup(path_file =PATH,mode=mode,type=type,grado = grado,range=range,r_min=r_min, variable=param,save=int(save))
334 340 print("SETUP OK")
335 341 obj.run()
336 342
337 343 if __name__ == '__main__':
338 344
339 345 parser = argparse.ArgumentParser(description='Script to process SOPHy data.')
340 346 parser.add_argument('--parameters', nargs='*', default=['S'],
341 347 help='Variables to process: P, Z, V ,W,D')
342 348 parser.add_argument('--grado', default=2,
343 349 help='Angle in Elev to plot')
344 350 parser.add_argument('--mode',default='PPI',
345 351 help='MODE PPI or RHI')
346 352 parser.add_argument('--save', default=0,
347 353 help='Save plot')
348 354 parser.add_argument('--range', default=0, type=float,
349 355 help='Max range to plot')
350 356 parser.add_argument('--r_min', default=0, type=float,
351 357 help='Min range to plot')
352 358 parser.add_argument('--type', default='RTI',
353 359 help='TYPE Profile or RTI')
354 360 args = parser.parse_args()
355 361
356 362 main(args)
General Comments 0
You need to be logged in to leave comments. Login now