##// END OF EJS Templates
last_update primeras correciones rhi
avaldezp -
r1418:2ccc6450afd4
parent child
Show More
@@ -0,0 +1,64
1 import numpy as np
2 import matplotlib.pyplot as pl
3 import warnings
4 #export WRADLIB_DATA=/path/to/wradlib-data
5 warnings.filterwarnings('ignore')
6
7 from wradlib.io import read_generic_netcdf
8 from wradlib.util import get_wradlib_data_file
9 import os
10
11
12
13 # A little helper function for repeated tasks
14 def read_and_overview(filename):
15 """Read NetCDF using read_generic_netcdf and print upper level dictionary keys
16 """
17 test = read_generic_netcdf(filename)
18 print("\nPrint keys for file %s" % os.path.basename(filename))
19 for key in test.keys():
20 print("\t%s" % key)
21 return test
22
23
24 filename = '/home/soporte/Downloads/PX1000/PX-20180220-174014-E0.0-Z.nc'
25 filename = get_wradlib_data_file(filename)
26 test= read_and_overview(filename)
27 print("Height",test['Height'])
28 print("Azimuth",test['Azimuth'])
29 print("Elevation",test['Elevation'])
30 print("CalibH-value",test['CalibH-value'])
31 print("attributes",test['attributes'])
32 print("-------------------------------------------------------------------------------------")
33 for key in test.keys():
34 print(key,test[str(key)])
35
36 '''
37 try:
38 get_ipython().magic('matplotlib inline')
39 except:
40 pl.ion()
41 img, meta = wradlib.io.read_dx(filename)
42 print("Shape of polar array: %r\n" % (img.shape,))
43 print("Some meta data of the DX file:")
44 #print("\tdatetime: %r" % (meta["datetime"],))
45 #print("\tRadar ID: %s" % (meta["radarid"],))
46
47 img[200:250,:]= np.ones([50,img.shape[1]])*np.nan
48
49 img[300:360,:]= np.ones([60,img.shape[1]])*np.nan
50
51 cgax, pm= wradlib.vis.plot_ppi(img)
52 txt = pl.title('Simple PPI')
53 print("coordenada angular",img[:,0],len(img[:,0]))
54 print("COORDENADA 0",img[0],len(img[0]))
55 cbar = pl.gcf().colorbar(pm, pad=0.075)
56
57 #r = np.arange(40, 80)
58 #az = np.arange(200, 250)
59 #ax, pm = wradlib.vis.plot_ppi(img[200:250, 40:80], r, az, autoext=False)
60 #ax, pm = wradlib.vis.plot_ppi(img[200:250, 40:80], r, az)
61
62 #txt = pl.title('Sector PPI')
63 pl.show()
64 '''
@@ -1,707 +1,898
1 1 import os
2 2 import datetime
3 3 import numpy
4 4
5 5 from schainpy.model.graphics.jroplot_base import Plot, plt
6 6 from schainpy.model.graphics.jroplot_spectra import SpectraPlot, RTIPlot, CoherencePlot, SpectraCutPlot
7 7 from schainpy.utils import log
8 8 # libreria wradlib
9 9 import wradlib as wrl
10 10
11 11 EARTH_RADIUS = 6.3710e3
12 12
13 13
14 14 def ll2xy(lat1, lon1, lat2, lon2):
15 15
16 16 p = 0.017453292519943295
17 17 a = 0.5 - numpy.cos((lat2 - lat1) * p)/2 + numpy.cos(lat1 * p) * \
18 18 numpy.cos(lat2 * p) * (1 - numpy.cos((lon2 - lon1) * p)) / 2
19 19 r = 12742 * numpy.arcsin(numpy.sqrt(a))
20 20 theta = numpy.arctan2(numpy.sin((lon2-lon1)*p)*numpy.cos(lat2*p), numpy.cos(lat1*p)
21 21 * numpy.sin(lat2*p)-numpy.sin(lat1*p)*numpy.cos(lat2*p)*numpy.cos((lon2-lon1)*p))
22 22 theta = -theta + numpy.pi/2
23 23 return r*numpy.cos(theta), r*numpy.sin(theta)
24 24
25 25
26 26 def km2deg(km):
27 27 '''
28 28 Convert distance in km to degrees
29 29 '''
30 30
31 31 return numpy.rad2deg(km/EARTH_RADIUS)
32 32
33 33
34 34
35 35 class SpectralMomentsPlot(SpectraPlot):
36 36 '''
37 37 Plot for Spectral Moments
38 38 '''
39 39 CODE = 'spc_moments'
40 40 # colormap = 'jet'
41 41 # plot_type = 'pcolor'
42 42
43 43 class DobleGaussianPlot(SpectraPlot):
44 44 '''
45 45 Plot for Double Gaussian Plot
46 46 '''
47 47 CODE = 'gaussian_fit'
48 48 # colormap = 'jet'
49 49 # plot_type = 'pcolor'
50 50
51 51 class DoubleGaussianSpectraCutPlot(SpectraCutPlot):
52 52 '''
53 53 Plot SpectraCut with Double Gaussian Fit
54 54 '''
55 55 CODE = 'cut_gaussian_fit'
56 56
57 57 class SnrPlot(RTIPlot):
58 58 '''
59 59 Plot for SNR Data
60 60 '''
61 61
62 62 CODE = 'snr'
63 63 colormap = 'jet'
64 64
65 65 def update(self, dataOut):
66 66
67 67 data = {
68 68 'snr': 10*numpy.log10(dataOut.data_snr)
69 69 }
70 70
71 71 return data, {}
72 72
73 73 class DopplerPlot(RTIPlot):
74 74 '''
75 75 Plot for DOPPLER Data (1st moment)
76 76 '''
77 77
78 78 CODE = 'dop'
79 79 colormap = 'jet'
80 80
81 81 def update(self, dataOut):
82 82
83 83 data = {
84 84 'dop': 10*numpy.log10(dataOut.data_dop)
85 85 }
86 86
87 87 return data, {}
88 88
89 89 class PowerPlot(RTIPlot):
90 90 '''
91 91 Plot for Power Data (0 moment)
92 92 '''
93 93
94 94 CODE = 'pow'
95 95 colormap = 'jet'
96 96
97 97 def update(self, dataOut):
98 98 data = {
99 99 'pow': 10*numpy.log10(dataOut.data_pow/dataOut.normFactor)
100 100 }
101 101 return data, {}
102 102
103 103 class SpectralWidthPlot(RTIPlot):
104 104 '''
105 105 Plot for Spectral Width Data (2nd moment)
106 106 '''
107 107
108 108 CODE = 'width'
109 109 colormap = 'jet'
110 110
111 111 def update(self, dataOut):
112 112
113 113 data = {
114 114 'width': dataOut.data_width
115 115 }
116 116
117 117 return data, {}
118 118
119 119 class SkyMapPlot(Plot):
120 120 '''
121 121 Plot for meteors detection data
122 122 '''
123 123
124 124 CODE = 'param'
125 125
126 126 def setup(self):
127 127
128 128 self.ncols = 1
129 129 self.nrows = 1
130 130 self.width = 7.2
131 131 self.height = 7.2
132 132 self.nplots = 1
133 133 self.xlabel = 'Zonal Zenith Angle (deg)'
134 134 self.ylabel = 'Meridional Zenith Angle (deg)'
135 135 self.polar = True
136 136 self.ymin = -180
137 137 self.ymax = 180
138 138 self.colorbar = False
139 139
140 140 def plot(self):
141 141
142 142 arrayParameters = numpy.concatenate(self.data['param'])
143 143 error = arrayParameters[:, -1]
144 144 indValid = numpy.where(error == 0)[0]
145 145 finalMeteor = arrayParameters[indValid, :]
146 146 finalAzimuth = finalMeteor[:, 3]
147 147 finalZenith = finalMeteor[:, 4]
148 148
149 149 x = finalAzimuth * numpy.pi / 180
150 150 y = finalZenith
151 151
152 152 ax = self.axes[0]
153 153
154 154 if ax.firsttime:
155 155 ax.plot = ax.plot(x, y, 'bo', markersize=5)[0]
156 156 else:
157 157 ax.plot.set_data(x, y)
158 158
159 159 dt1 = self.getDateTime(self.data.min_time).strftime('%y/%m/%d %H:%M:%S')
160 160 dt2 = self.getDateTime(self.data.max_time).strftime('%y/%m/%d %H:%M:%S')
161 161 title = 'Meteor Detection Sky Map\n %s - %s \n Number of events: %5.0f\n' % (dt1,
162 162 dt2,
163 163 len(x))
164 164 self.titles[0] = title
165 165
166 166
167 167 class GenericRTIPlot(Plot):
168 168 '''
169 169 Plot for data_xxxx object
170 170 '''
171 171
172 172 CODE = 'param'
173 173 colormap = 'viridis'
174 174 plot_type = 'pcolorbuffer'
175 175
176 176 def setup(self):
177 177 self.xaxis = 'time'
178 178 self.ncols = 1
179 179 self.nrows = self.data.shape('param')[0]
180 180 self.nplots = self.nrows
181 181 self.plots_adjust.update({'hspace':0.8, 'left': 0.1, 'bottom': 0.08, 'right':0.95, 'top': 0.95})
182 182
183 183 if not self.xlabel:
184 184 self.xlabel = 'Time'
185 185
186 186 self.ylabel = 'Range [km]'
187 187 if not self.titles:
188 188 self.titles = ['Param {}'.format(x) for x in range(self.nrows)]
189 189
190 190 def update(self, dataOut):
191 191
192 192 data = {
193 193 'param' : numpy.concatenate([getattr(dataOut, attr) for attr in self.attr_data], axis=0)
194 194 }
195 195
196 196 meta = {}
197 197
198 198 return data, meta
199 199
200 200 def plot(self):
201 201 # self.data.normalize_heights()
202 202 self.x = self.data.times
203 203 self.y = self.data.yrange
204 204 self.z = self.data['param']
205 205 self.z = 10*numpy.log10(self.z)
206 206 self.z = numpy.ma.masked_invalid(self.z)
207 207
208 208 if self.decimation is None:
209 209 x, y, z = self.fill_gaps(self.x, self.y, self.z)
210 210 else:
211 211 x, y, z = self.fill_gaps(*self.decimate())
212 212
213 213 for n, ax in enumerate(self.axes):
214 214
215 215 self.zmax = self.zmax if self.zmax is not None else numpy.max(
216 216 self.z[n])
217 217 self.zmin = self.zmin if self.zmin is not None else numpy.min(
218 218 self.z[n])
219 219
220 220 if ax.firsttime:
221 221 if self.zlimits is not None:
222 222 self.zmin, self.zmax = self.zlimits[n]
223 223
224 224 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
225 225 vmin=self.zmin,
226 226 vmax=self.zmax,
227 227 cmap=self.cmaps[n]
228 228 )
229 229 else:
230 230 if self.zlimits is not None:
231 231 self.zmin, self.zmax = self.zlimits[n]
232 232 ax.collections.remove(ax.collections[0])
233 233 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
234 234 vmin=self.zmin,
235 235 vmax=self.zmax,
236 236 cmap=self.cmaps[n]
237 237 )
238 238
239 239
240 240 class PolarMapPlot(Plot):
241 241 '''
242 242 Plot for weather radar
243 243 '''
244 244
245 245 CODE = 'param'
246 246 colormap = 'seismic'
247 247
248 248 def setup(self):
249 249 self.ncols = 1
250 250 self.nrows = 1
251 251 self.width = 9
252 252 self.height = 8
253 253 self.mode = self.data.meta['mode']
254 254 if self.channels is not None:
255 255 self.nplots = len(self.channels)
256 256 self.nrows = len(self.channels)
257 257 else:
258 258 self.nplots = self.data.shape(self.CODE)[0]
259 259 self.nrows = self.nplots
260 260 self.channels = list(range(self.nplots))
261 261 if self.mode == 'E':
262 262 self.xlabel = 'Longitude'
263 263 self.ylabel = 'Latitude'
264 264 else:
265 265 self.xlabel = 'Range (km)'
266 266 self.ylabel = 'Height (km)'
267 267 self.bgcolor = 'white'
268 268 self.cb_labels = self.data.meta['units']
269 269 self.lat = self.data.meta['latitude']
270 270 self.lon = self.data.meta['longitude']
271 271 self.xmin, self.xmax = float(
272 272 km2deg(self.xmin) + self.lon), float(km2deg(self.xmax) + self.lon)
273 273 self.ymin, self.ymax = float(
274 274 km2deg(self.ymin) + self.lat), float(km2deg(self.ymax) + self.lat)
275 275 # self.polar = True
276 276
277 277 def plot(self):
278 278
279 279 for n, ax in enumerate(self.axes):
280 280 data = self.data['param'][self.channels[n]]
281 281
282 282 zeniths = numpy.linspace(
283 283 0, self.data.meta['max_range'], data.shape[1])
284 284 if self.mode == 'E':
285 285 azimuths = -numpy.radians(self.data.yrange)+numpy.pi/2
286 286 r, theta = numpy.meshgrid(zeniths, azimuths)
287 287 x, y = r*numpy.cos(theta)*numpy.cos(numpy.radians(self.data.meta['elevation'])), r*numpy.sin(
288 288 theta)*numpy.cos(numpy.radians(self.data.meta['elevation']))
289 289 x = km2deg(x) + self.lon
290 290 y = km2deg(y) + self.lat
291 291 else:
292 292 azimuths = numpy.radians(self.data.yrange)
293 293 r, theta = numpy.meshgrid(zeniths, azimuths)
294 294 x, y = r*numpy.cos(theta), r*numpy.sin(theta)
295 295 self.y = zeniths
296 296
297 297 if ax.firsttime:
298 298 if self.zlimits is not None:
299 299 self.zmin, self.zmax = self.zlimits[n]
300 300 ax.plt = ax.pcolormesh( # r, theta, numpy.ma.array(data, mask=numpy.isnan(data)),
301 301 x, y, numpy.ma.array(data, mask=numpy.isnan(data)),
302 302 vmin=self.zmin,
303 303 vmax=self.zmax,
304 304 cmap=self.cmaps[n])
305 305 else:
306 306 if self.zlimits is not None:
307 307 self.zmin, self.zmax = self.zlimits[n]
308 308 ax.collections.remove(ax.collections[0])
309 309 ax.plt = ax.pcolormesh( # r, theta, numpy.ma.array(data, mask=numpy.isnan(data)),
310 310 x, y, numpy.ma.array(data, mask=numpy.isnan(data)),
311 311 vmin=self.zmin,
312 312 vmax=self.zmax,
313 313 cmap=self.cmaps[n])
314 314
315 315 if self.mode == 'A':
316 316 continue
317 317
318 318 # plot district names
319 319 f = open('/data/workspace/schain_scripts/distrito.csv')
320 320 for line in f:
321 321 label, lon, lat = [s.strip() for s in line.split(',') if s]
322 322 lat = float(lat)
323 323 lon = float(lon)
324 324 # ax.plot(lon, lat, '.b', ms=2)
325 325 ax.text(lon, lat, label.decode('utf8'), ha='center',
326 326 va='bottom', size='8', color='black')
327 327
328 328 # plot limites
329 329 limites = []
330 330 tmp = []
331 331 for line in open('/data/workspace/schain_scripts/lima.csv'):
332 332 if '#' in line:
333 333 if tmp:
334 334 limites.append(tmp)
335 335 tmp = []
336 336 continue
337 337 values = line.strip().split(',')
338 338 tmp.append((float(values[0]), float(values[1])))
339 339 for points in limites:
340 340 ax.add_patch(
341 341 Polygon(points, ec='k', fc='none', ls='--', lw=0.5))
342 342
343 343 # plot Cuencas
344 344 for cuenca in ('rimac', 'lurin', 'mala', 'chillon', 'chilca', 'chancay-huaral'):
345 345 f = open('/data/workspace/schain_scripts/{}.csv'.format(cuenca))
346 346 values = [line.strip().split(',') for line in f]
347 347 points = [(float(s[0]), float(s[1])) for s in values]
348 348 ax.add_patch(Polygon(points, ec='b', fc='none'))
349 349
350 350 # plot grid
351 351 for r in (15, 30, 45, 60):
352 352 ax.add_artist(plt.Circle((self.lon, self.lat),
353 353 km2deg(r), color='0.6', fill=False, lw=0.2))
354 354 ax.text(
355 355 self.lon + (km2deg(r))*numpy.cos(60*numpy.pi/180),
356 356 self.lat + (km2deg(r))*numpy.sin(60*numpy.pi/180),
357 357 '{}km'.format(r),
358 358 ha='center', va='bottom', size='8', color='0.6', weight='heavy')
359 359
360 360 if self.mode == 'E':
361 361 title = 'El={}$^\circ$'.format(self.data.meta['elevation'])
362 362 label = 'E{:02d}'.format(int(self.data.meta['elevation']))
363 363 else:
364 364 title = 'Az={}$^\circ$'.format(self.data.meta['azimuth'])
365 365 label = 'A{:02d}'.format(int(self.data.meta['azimuth']))
366 366
367 367 self.save_labels = ['{}-{}'.format(lbl, label) for lbl in self.labels]
368 368 self.titles = ['{} {}'.format(
369 369 self.data.parameters[x], title) for x in self.channels]
370 370
371 371 class WeatherPlot(Plot):
372 372 CODE = 'weather'
373 373 plot_name = 'weather'
374 374 plot_type = 'ppistyle'
375 375 buffering = False
376 376
377 377 def setup(self):
378 378 self.ncols = 1
379 379 self.nrows = 1
380 380 self.nplots= 1
381 381 self.ylabel= 'Range [Km]'
382 382 self.titles= ['Weather']
383 383 self.colorbar=False
384 384 self.width =8
385 385 self.height =8
386 386 self.ini =0
387 387 self.len_azi =0
388 388 self.buffer_ini = None
389 389 self.buffer_azi = None
390 390 self.plots_adjust.update({'wspace': 0.4, 'hspace':0.4, 'left': 0.1, 'right': 0.9, 'bottom': 0.08})
391 391 self.flag =0
392 392 self.indicador= 0
393 393 self.last_data_azi = None
394 394 self.val_mean = None
395 395
396 396 def update(self, dataOut):
397 397
398 398 data = {}
399 399 meta = {}
400 400 if hasattr(dataOut, 'dataPP_POWER'):
401 401 factor = 1
402 402 if hasattr(dataOut, 'nFFTPoints'):
403 403 factor = dataOut.normFactor
404 404 data['weather'] = 10*numpy.log10(dataOut.data_360[1]/(factor))
405 405 data['azi'] = dataOut.data_azi
406 406 data['ele'] = dataOut.data_ele
407 407 return data, meta
408 408
409 409 def get2List(self,angulos):
410 410 list1=[]
411 411 list2=[]
412 412 for i in reversed(range(len(angulos))):
413 413 diff_ = angulos[i]-angulos[i-1]
414 414 if diff_ >1.5:
415 415 list1.append(i-1)
416 416 list2.append(diff_)
417 417 return list(reversed(list1)),list(reversed(list2))
418 418
419 419 def fixData360(self,list_,ang_):
420 420 if list_[0]==-1:
421 421 vec = numpy.where(ang_<ang_[0])
422 422 ang_[vec] = ang_[vec]+360
423 423 return ang_
424 424 return ang_
425 425
426 426 def fixData360HL(self,angulos):
427 427 vec = numpy.where(angulos>=360)
428 428 angulos[vec]=angulos[vec]-360
429 429 return angulos
430 430
431 431 def search_pos(self,pos,list_):
432 432 for i in range(len(list_)):
433 433 if pos == list_[i]:
434 434 return True,i
435 435 i=None
436 436 return False,i
437 437
438 438 def fixDataComp(self,ang_,list1_,list2_):
439 439 size = len(ang_)
440 440 size2 = 0
441 441 for i in range(len(list2_)):
442 442 size2=size2+round(list2_[i])-1
443 443 new_size= size+size2
444 444 ang_new = numpy.zeros(new_size)
445 445 ang_new2 = numpy.zeros(new_size)
446 446
447 447 tmp = 0
448 448 c = 0
449 449 for i in range(len(ang_)):
450 450 ang_new[tmp +c] = ang_[i]
451 451 ang_new2[tmp+c] = ang_[i]
452 452 condition , value = self.search_pos(i,list1_)
453 453 if condition:
454 454 pos = tmp + c + 1
455 455 for k in range(round(list2_[value])-1):
456 456 ang_new[pos+k] = ang_new[pos+k-1]+1
457 457 ang_new2[pos+k] = numpy.nan
458 458 tmp = pos +k
459 459 c = 0
460 460 c=c+1
461 461 return ang_new,ang_new2
462 462
463 463 def globalCheckPED(self,angulos):
464 464 l1,l2 = self.get2List(angulos)
465 465 if len(l1)>0:
466 466 angulos2 = self.fixData360(list_=l1,ang_=angulos)
467 467 l1,l2 = self.get2List(angulos2)
468 468
469 469 ang1_,ang2_ = self.fixDataComp(ang_=angulos2,list1_=l1,list2_=l2)
470 470 ang1_ = self.fixData360HL(ang1_)
471 471 ang2_ = self.fixData360HL(ang2_)
472 472 else:
473 473 ang1_= angulos
474 474 ang2_= angulos
475 475 return ang1_,ang2_
476 476
477 477 def analizeDATA(self,data_azi):
478 478 list1 = []
479 479 list2 = []
480 480 dat = data_azi
481 481 for i in reversed(range(1,len(dat))):
482 482 if dat[i]>dat[i-1]:
483 483 diff = int(dat[i])-int(dat[i-1])
484 484 else:
485 485 diff = 360+int(dat[i])-int(dat[i-1])
486 486 if diff > 1:
487 487 list1.append(i-1)
488 488 list2.append(diff-1)
489 489 return list1,list2
490 490
491 491 def fixDATANEW(self,data_azi,data_weather):
492 492 list1,list2 = self.analizeDATA(data_azi)
493 493 if len(list1)== 0:
494 494 return data_azi,data_weather
495 495 else:
496 496 resize = 0
497 497 for i in range(len(list2)):
498 498 resize= resize + list2[i]
499 499 new_data_azi = numpy.resize(data_azi,resize)
500 500 new_data_weather= numpy.resize(date_weather,resize)
501 501
502 502 for i in range(len(list2)):
503 503 j=0
504 504 position=list1[i]+1
505 505 for j in range(list2[i]):
506 506 new_data_azi[position+j]=new_data_azi[position+j-1]+1
507 507 return new_data_azi
508 508
509 509 def fixDATA(self,data_azi):
510 510 data=data_azi
511 511 for i in range(len(data)):
512 512 if numpy.isnan(data[i]):
513 513 data[i]=data[i-1]+1
514 514 return data
515 515
516 516 def replaceNAN(self,data_weather,data_azi,val):
517 517 data= data_azi
518 518 data_T= data_weather
519 519 if data.shape[0]> data_T.shape[0]:
520 520 data_N = numpy.ones( [data.shape[0],data_T.shape[1]])
521 521 c = 0
522 522 for i in range(len(data)):
523 523 if numpy.isnan(data[i]):
524 524 data_N[i,:]=numpy.ones(data_T.shape[1])*numpy.nan
525 525 else:
526 526 data_N[i,:]=data_T[c,:]
527 527 sc=c+1
528 528 else:
529 529 for i in range(len(data)):
530 530 if numpy.isnan(data[i]):
531 531 data_T[i,:]=numpy.ones(data_T.shape[1])*numpy.nan
532 532 return data_T
533 533
534 534 def const_ploteo(self,data_weather,data_azi,step,res):
535 535 if self.ini==0:
536 536 #-------
537 537 n = (360/res)-len(data_azi)
538 538 #--------------------- new -------------------------
539 539 data_azi_new ,data_azi_old= self.globalCheckPED(data_azi)
540 540 #------------------------
541 541 start = data_azi_new[-1] + res
542 542 end = data_azi_new[0] - res
543 543 #------ new
544 544 self.last_data_azi = end
545 545 if start>end:
546 546 end = end + 360
547 547 azi_vacia = numpy.linspace(start,end,int(n))
548 548 azi_vacia = numpy.where(azi_vacia>360,azi_vacia-360,azi_vacia)
549 549 data_azi = numpy.hstack((data_azi_new,azi_vacia))
550 550 # RADAR
551 551 val_mean = numpy.mean(data_weather[:,-1])
552 552 self.val_mean = val_mean
553 553 data_weather_cmp = numpy.ones([(360-data_weather.shape[0]),data_weather.shape[1]])*val_mean
554 554 data_weather = self.replaceNAN(data_weather=data_weather,data_azi=data_azi_old,val=self.val_mean)
555 555 data_weather = numpy.vstack((data_weather,data_weather_cmp))
556 556 else:
557 557 # azimuth
558 558 flag=0
559 559 start_azi = self.res_azi[0]
560 560 #-----------new------------
561 561 data_azi ,data_azi_old= self.globalCheckPED(data_azi)
562 562 data_weather = self.replaceNAN(data_weather=data_weather,data_azi=data_azi_old,val=self.val_mean)
563 563 #--------------------------
564 564 start = data_azi[0]
565 565 end = data_azi[-1]
566 566 self.last_data_azi= end
567 567 if start< start_azi:
568 568 start = start +360
569 569 if end <start_azi:
570 570 end = end +360
571 571
572 572 pos_ini = int((start-start_azi)/res)
573 573 len_azi = len(data_azi)
574 574 if (360-pos_ini)<len_azi:
575 575 if pos_ini+1==360:
576 576 pos_ini=0
577 577 else:
578 578 flag=1
579 579 dif= 360-pos_ini
580 580 comp= len_azi-dif
581 581 #-----------------
582 582 if flag==0:
583 583 # AZIMUTH
584 584 self.res_azi[pos_ini:pos_ini+len_azi] = data_azi
585 585 # RADAR
586 586 self.res_weather[pos_ini:pos_ini+len_azi,:] = data_weather
587 587 else:
588 588 # AZIMUTH
589 589 self.res_azi[pos_ini:pos_ini+dif] = data_azi[0:dif]
590 590 self.res_azi[0:comp] = data_azi[dif:]
591 591 # RADAR
592 592 self.res_weather[pos_ini:pos_ini+dif,:] = data_weather[0:dif,:]
593 593 self.res_weather[0:comp,:] = data_weather[dif:,:]
594 594 flag=0
595 595 data_azi = self.res_azi
596 596 data_weather = self.res_weather
597 597
598 598 return data_weather,data_azi
599 599
600 600 def plot(self):
601 601 thisDatetime = datetime.datetime.utcfromtimestamp(self.data.times[-1]).strftime('%Y-%m-%d %H:%M:%S')
602 602 data = self.data[-1]
603 603 r = self.data.yrange
604 604 delta_height = r[1]-r[0]
605 605 r_mask = numpy.where(r>=0)[0]
606 606 r = numpy.arange(len(r_mask))*delta_height
607 607 self.y = 2*r
608 608 # RADAR
609 609 #data_weather = data['weather']
610 610 # PEDESTAL
611 611 #data_azi = data['azi']
612 612 res = 1
613 613 # STEP
614 614 step = (360/(res*data['weather'].shape[0]))
615 615
616 616 self.res_weather, self.res_azi = self.const_ploteo(data_weather=data['weather'][:,r_mask],data_azi=data['azi'],step=step,res=res)
617 617 self.res_ele = numpy.mean(data['ele'])
618 618 ################# PLOTEO ###################
619 619
620 620 for i,ax in enumerate(self.axes):
621 621 if ax.firsttime:
622 622 plt.clf()
623 623 cgax, pm = wrl.vis.plot_ppi(self.res_weather,r=r,az=self.res_azi,fig=self.figures[0], proj='cg', vmin=8, vmax=35)
624 624 else:
625 625 plt.clf()
626 626 cgax, pm = wrl.vis.plot_ppi(self.res_weather,r=r,az=self.res_azi,fig=self.figures[0], proj='cg', vmin=8, vmax=35)
627 627 caax = cgax.parasites[0]
628 628 paax = cgax.parasites[1]
629 629 cbar = plt.gcf().colorbar(pm, pad=0.075)
630 630 caax.set_xlabel('x_range [km]')
631 631 caax.set_ylabel('y_range [km]')
632 632 plt.text(1.0, 1.05, 'Azimuth '+str(thisDatetime)+" Step "+str(self.ini)+ " Elev: "+str(round(self.res_ele,2)), transform=caax.transAxes, va='bottom',ha='right')
633 633
634 634 self.ini= self.ini+1
635 635
636 636
637 637 class WeatherRHIPlot(Plot):
638 638 CODE = 'weather'
639 639 plot_name = 'weather'
640 640 plot_type = 'rhistyle'
641 641 buffering = False
642 642
643 643 def setup(self):
644 644 self.ncols = 1
645 645 self.nrows = 1
646 646 self.nplots= 1
647 647 self.ylabel= 'Range [Km]'
648 648 self.titles= ['Weather']
649 649 self.colorbar=False
650 650 self.width =8
651 651 self.height =8
652 652 self.ini =0
653 653 self.len_azi =0
654 654 self.buffer_ini = None
655 self.buffer_azi = None
655 self.buffer_ele = None
656 656 self.plots_adjust.update({'wspace': 0.4, 'hspace':0.4, 'left': 0.1, 'right': 0.9, 'bottom': 0.08})
657 657 self.flag =0
658 658 self.indicador= 0
659 self.last_data_azi = None
659 self.last_data_ele = None
660 660 self.val_mean = None
661 661
662 662 def update(self, dataOut):
663 663
664 664 data = {}
665 665 meta = {}
666 666 if hasattr(dataOut, 'dataPP_POWER'):
667 667 factor = 1
668 668 if hasattr(dataOut, 'nFFTPoints'):
669 669 factor = dataOut.normFactor
670 670 data['weather'] = 10*numpy.log10(dataOut.data_360[1]/(factor))
671 671 data['azi'] = dataOut.data_azi
672 672 data['ele'] = dataOut.data_ele
673 673 return data, meta
674 674
675 def get2List(self,angulos):
676 list1=[]
677 list2=[]
678 for i in reversed(range(len(angulos))):
679 diff_ = angulos[i]-angulos[i-1]
680 if diff_ >1.5:
681 list1.append(i-1)
682 list2.append(diff_)
683 return list(reversed(list1)),list(reversed(list2))
684
685 def fixData180(self,list_,ang_):
686 if list_[0]==-1:
687 vec = numpy.where(ang_<ang_[0])
688 ang_[vec] = ang_[vec]+180
689 return ang_
690 return ang_
691
692 def fixData180HL(self,angulos):
693 vec = numpy.where(angulos>=180)
694 angulos[vec]=angulos[vec]-180
695 return angulos
696
697
698 def search_pos(self,pos,list_):
699 for i in range(len(list_)):
700 if pos == list_[i]:
701 return True,i
702 i=None
703 return False,i
704
705 def fixDataComp(self,ang_,list1_,list2_):
706 size = len(ang_)
707 size2 = 0
708 for i in range(len(list2_)):
709 size2=size2+round(list2_[i])-1
710 new_size= size+size2
711 ang_new = numpy.zeros(new_size)
712 ang_new2 = numpy.zeros(new_size)
713
714 tmp = 0
715 c = 0
716 for i in range(len(ang_)):
717 ang_new[tmp +c] = ang_[i]
718 ang_new2[tmp+c] = ang_[i]
719 condition , value = self.search_pos(i,list1_)
720 if condition:
721 pos = tmp + c + 1
722 for k in range(round(list2_[value])-1):
723 ang_new[pos+k] = ang_new[pos+k-1]+1
724 ang_new2[pos+k] = numpy.nan
725 tmp = pos +k
726 c = 0
727 c=c+1
728 return ang_new,ang_new2
729
730 def globalCheckPED(self,angulos):
731 l1,l2 = self.get2List(angulos)
732 if len(l1)>0:
733 angulos2 = self.fixData180(list_=l1,ang_=angulos)
734 l1,l2 = self.get2List(angulos2)
735
736 ang1_,ang2_ = self.fixDataComp(ang_=angulos2,list1_=l1,list2_=l2)
737 ang1_ = self.fixData180HL(ang1_)
738 ang2_ = self.fixData180HL(ang2_)
739 else:
740 ang1_= angulos
741 ang2_= angulos
742 return ang1_,ang2_
743
744
745 def replaceNAN(self,data_weather,data_ele,val):
746 data= data_ele
747 data_T= data_weather
748 if data.shape[0]> data_T.shape[0]:
749 data_N = numpy.ones( [data.shape[0],data_T.shape[1]])
750 c = 0
751 for i in range(len(data)):
752 if numpy.isnan(data[i]):
753 data_N[i,:]=numpy.ones(data_T.shape[1])*numpy.nan
754 else:
755 data_N[i,:]=data_T[c,:]
756 sc=c+1
757 else:
758 for i in range(len(data)):
759 if numpy.isnan(data[i]):
760 data_T[i,:]=numpy.ones(data_T.shape[1])*numpy.nan
761 return data_T
762
763 def const_ploteo(self,data_weather,data_ele,step,res):
764 if self.ini==0:
765 #-------
766 n = (180/res)-len(data_ele)
767 #--------------------- new -------------------------
768 data_ele_new ,data_ele_old= self.globalCheckPED(data_ele)
769 #------------------------
770 start = data_ele_new[-1] + res
771 end = data_ele_new[0] - res
772 #------ new
773 self.last_data_ele = end
774 if start>end:
775 end = end + 180
776 ele_vacia = numpy.linspace(start,end,int(n))
777 ele_vacia = numpy.where(ele_vacia>180,ele_vacia-180,ele_vacia)
778 data_ele = numpy.hstack((data_ele_new,ele_vacia))
779 # RADAR
780 val_mean = numpy.mean(data_weather[:,-1])
781 self.val_mean = val_mean
782 data_weather_cmp = numpy.ones([(180-data_weather.shape[0]),data_weather.shape[1]])*val_mean
783 data_weather = self.replaceNAN(data_weather=data_weather,data_ele=data_ele_old,val=self.val_mean)
784 data_weather = numpy.vstack((data_weather,data_weather_cmp))
785 else:
786 # azimuth
787 flag=0
788 start_ele = self.res_ele[0]
789 #-----------new------------
790 data_ele ,data_ele_old= self.globalCheckPED(data_ele)
791 data_weather = self.replaceNAN(data_weather=data_weather,data_ele=data_ele_old,val=self.val_mean)
792 #--------------------------
793 start = data_ele[0]
794 end = data_ele[-1]
795 self.last_data_ele= end
796 if start< start_ele:
797 start = start +180
798 if end <start_ele:
799 end = end +180
800
801 pos_ini = int((start-start_ele)/res)
802 len_ele = len(data_ele)
803 if (180-pos_ini)<len_ele:
804 if pos_ini+1==180:
805 pos_ini=0
806 else:
807 flag=1
808 dif= 180-pos_ini
809 comp= len_ele-dif
810 #-----------------
811 if flag==0:
812 # elevacion
813 self.res_ele[pos_ini:pos_ini+len_ele] = data_ele
814 # RADAR
815 self.res_weather[pos_ini:pos_ini+len_ele,:] = data_weather
816 else:
817 # elevacion
818 self.res_ele[pos_ini:pos_ini+dif] = data_ele[0:dif]
819 self.res_ele[0:comp] = data_ele[dif:]
820 # RADAR
821 self.res_weather[pos_ini:pos_ini+dif,:] = data_weather[0:dif,:]
822 self.res_weather[0:comp,:] = data_weather[dif:,:]
823 flag=0
824 data_ele = self.res_ele
825 data_weather = self.res_weather
826
827 return data_weather,data_ele
828
829
675 830 def plot(self):
676 831 thisDatetime = datetime.datetime.utcfromtimestamp(self.data.times[-1]).strftime('%Y-%m-%d %H:%M:%S')
677 832 data = self.data[-1]
678 833 r = self.data.yrange
679 834 delta_height = r[1]-r[0]
680 835 r_mask = numpy.where(r>=0)[0]
681 836 r = numpy.arange(len(r_mask))*delta_height
682 837 self.y = 2*r
838 '''
839 #-------------------------------------------------------------
840 # RADAR
841 #data_weather = data['weather']
842 # PEDESTAL
843 #data_azi = data['azi']
844 res = 1
845 # STEP
846 step = (180/(res*data['weather'].shape[0]))
847
848
849 self.res_weather, self.res_ele = self.const_ploteo(data_weather=data['weather'][:,r_mask],data_ele=data['ele'],step=step,res=res)
850 self.res_azi = numpy.mean(data['azi'])
851 print("self.res_ele------------------------------:",self.res_ele)
852 ################# PLOTEO ###################
853
854 for i,ax in enumerate(self.axes):
855 if ax.firsttime:
856 plt.clf()
857 cgax, pm = wrl.vis.plot_rhi(self.res_weather,r=r,th=self.res_ele,fig=self.figures[0], proj='cg', vmin=8, vmax=35)
858 else:
859 plt.clf()
860 cgax, pm = wrl.vis.plot_rhi(self.res_weather,r=r,th=self.res_ele,fig=self.figures[0], proj='cg', vmin=8, vmax=35)
861 caax = cgax.parasites[0]
862 paax = cgax.parasites[1]
863 cbar = plt.gcf().colorbar(pm, pad=0.075)
864 caax.set_xlabel('x_range [km]')
865 caax.set_ylabel('y_range [km]')
866 plt.text(1.0, 1.05, 'Elevacion '+str(thisDatetime)+" Step "+str(self.ini)+ " Azi: "+str(round(self.res_azi,2)), transform=caax.transAxes, va='bottom',ha='right')
867
868 self.ini= self.ini+1
869
870
871 '''
872 #--------------------------------------------------------
873
683 874 ###self.res_weather, self.res_ele = self.const_ploteo(data_weather=data['weather'][:,r_mask],data_azi=data['ele'],step=step,res=res)
684 875 ###self.res_azi = numpy.mean(data['azi'])
685 876 #-------------
686 877 # 90 angulos en el axis 0
687 878 # 1000 step en el axis 1
688 self.res_weather = numpy.ones([90,1000])
879 self.res_weather = numpy.ones([120,1000])
689 880 r = numpy.linspace(0,1999,1000)
690 self.res_ele = numpy.arange(0,90)
881 self.res_ele = numpy.arange(0,120)
691 882 self.res_azi = 240
692 883 #-------------
693 884 for i,ax in enumerate(self.axes):
694 885 if ax.firsttime:
695 886 plt.clf()
696 887 cgax, pm = wrl.vis.plot_rhi(self.res_weather,r=r,th=self.res_ele,fig=self.figures[0], proj='cg')
697 888 else:
698 889 plt.clf()
699 890 cgax, pm = wrl.vis.plot_rhi(self.res_weather,r=r,th=self.res_ele,fig=self.figures[0], proj='cg')
700 891 caax = cgax.parasites[0]
701 892 paax = cgax.parasites[1]
702 893 cbar = plt.gcf().colorbar(pm, pad=0.075)
703 894 caax.set_xlabel('x_range [km]')
704 895 caax.set_ylabel('y_range [km]')
705 896 plt.text(1.0, 1.05, 'Elevacion '+str(thisDatetime)+" Step "+str(self.ini)+ " Azi: "+str(round(self.res_azi,2)), transform=caax.transAxes, va='bottom',ha='right')
706 897
707 898 self.ini= self.ini+1
@@ -1,4368 +1,4386
1 1 import numpy,os,h5py
2 2 import math
3 3 from scipy import optimize, interpolate, signal, stats, ndimage
4 4 import scipy
5 5 import re
6 6 import datetime
7 7 import copy
8 8 import sys
9 9 import importlib
10 10 import itertools
11 11 from multiprocessing import Pool, TimeoutError
12 12 from multiprocessing.pool import ThreadPool
13 13 import time
14 14
15 15 from scipy.optimize import fmin_l_bfgs_b #optimize with bounds on state papameters
16 16 from .jroproc_base import ProcessingUnit, Operation, MPDecorator
17 17 from schainpy.model.data.jrodata import Parameters, hildebrand_sekhon
18 18 from scipy import asarray as ar,exp
19 19 from scipy.optimize import curve_fit
20 20 from schainpy.utils import log
21 21 import warnings
22 22 from numpy import NaN
23 23 from scipy.optimize.optimize import OptimizeWarning
24 24 warnings.filterwarnings('ignore')
25 25
26 26 from time import sleep
27 27
28 28 import matplotlib.pyplot as plt
29 29
30 30 SPEED_OF_LIGHT = 299792458
31 31
32 32 '''solving pickling issue'''
33 33
34 34 def _pickle_method(method):
35 35 func_name = method.__func__.__name__
36 36 obj = method.__self__
37 37 cls = method.__self__.__class__
38 38 return _unpickle_method, (func_name, obj, cls)
39 39
40 40 def _unpickle_method(func_name, obj, cls):
41 41 for cls in cls.mro():
42 42 try:
43 43 func = cls.__dict__[func_name]
44 44 except KeyError:
45 45 pass
46 46 else:
47 47 break
48 48 return func.__get__(obj, cls)
49 49
50 50 def isNumber(str):
51 51 try:
52 52 float(str)
53 53 return True
54 54 except:
55 55 return False
56 56
57 57 class ParametersProc(ProcessingUnit):
58 58
59 59 METHODS = {}
60 60 nSeconds = None
61 61
62 62 def __init__(self):
63 63 ProcessingUnit.__init__(self)
64 64
65 65 # self.objectDict = {}
66 66 self.buffer = None
67 67 self.firstdatatime = None
68 68 self.profIndex = 0
69 69 self.dataOut = Parameters()
70 70 self.setupReq = False #Agregar a todas las unidades de proc
71 71
72 72 def __updateObjFromInput(self):
73 73
74 74 self.dataOut.inputUnit = self.dataIn.type
75 75
76 76 self.dataOut.timeZone = self.dataIn.timeZone
77 77 self.dataOut.dstFlag = self.dataIn.dstFlag
78 78 self.dataOut.errorCount = self.dataIn.errorCount
79 79 self.dataOut.useLocalTime = self.dataIn.useLocalTime
80 80
81 81 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
82 82 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
83 83 self.dataOut.channelList = self.dataIn.channelList
84 84 self.dataOut.heightList = self.dataIn.heightList
85 85 self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')])
86 86 # self.dataOut.nHeights = self.dataIn.nHeights
87 87 # self.dataOut.nChannels = self.dataIn.nChannels
88 88 # self.dataOut.nBaud = self.dataIn.nBaud
89 89 # self.dataOut.nCode = self.dataIn.nCode
90 90 # self.dataOut.code = self.dataIn.code
91 91 # self.dataOut.nProfiles = self.dataOut.nFFTPoints
92 92 self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock
93 93 # self.dataOut.utctime = self.firstdatatime
94 94 self.dataOut.utctime = self.dataIn.utctime
95 95 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada
96 96 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip
97 97 self.dataOut.nCohInt = self.dataIn.nCohInt
98 98 # self.dataOut.nIncohInt = 1
99 99 # self.dataOut.ippSeconds = self.dataIn.ippSeconds
100 100 # self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
101 101 self.dataOut.timeInterval1 = self.dataIn.timeInterval
102 102 self.dataOut.heightList = self.dataIn.heightList
103 103 self.dataOut.frequency = self.dataIn.frequency
104 104 # self.dataOut.noise = self.dataIn.noise
105 105
106 106 def run(self):
107 107
108 108
109 109 #print("HOLA MUNDO SOY YO")
110 110 #---------------------- Voltage Data ---------------------------
111 111
112 112 if self.dataIn.type == "Voltage":
113 113
114 114 self.__updateObjFromInput()
115 115 self.dataOut.data_pre = self.dataIn.data.copy()
116 116 self.dataOut.flagNoData = False
117 117 self.dataOut.utctimeInit = self.dataIn.utctime
118 118 self.dataOut.paramInterval = self.dataIn.nProfiles*self.dataIn.nCohInt*self.dataIn.ippSeconds
119 119
120 120 if hasattr(self.dataIn, 'flagDataAsBlock'):
121 121 self.dataOut.flagDataAsBlock = self.dataIn.flagDataAsBlock
122 122
123 123 if hasattr(self.dataIn, 'profileIndex'):
124 124 self.dataOut.profileIndex = self.dataIn.profileIndex
125 125
126 126 if hasattr(self.dataIn, 'dataPP_POW'):
127 127 self.dataOut.dataPP_POW = self.dataIn.dataPP_POW
128 128
129 129 if hasattr(self.dataIn, 'dataPP_POWER'):
130 130 self.dataOut.dataPP_POWER = self.dataIn.dataPP_POWER
131 131
132 132 if hasattr(self.dataIn, 'dataPP_DOP'):
133 133 self.dataOut.dataPP_DOP = self.dataIn.dataPP_DOP
134 134
135 135 if hasattr(self.dataIn, 'dataPP_SNR'):
136 136 self.dataOut.dataPP_SNR = self.dataIn.dataPP_SNR
137 137
138 138 if hasattr(self.dataIn, 'dataPP_WIDTH'):
139 139 self.dataOut.dataPP_WIDTH = self.dataIn.dataPP_WIDTH
140 140 return
141 141
142 142 #---------------------- Spectra Data ---------------------------
143 143
144 144 if self.dataIn.type == "Spectra":
145 145 #print("que paso en spectra")
146 146 self.dataOut.data_pre = [self.dataIn.data_spc, self.dataIn.data_cspc]
147 147 self.dataOut.data_spc = self.dataIn.data_spc
148 148 self.dataOut.data_cspc = self.dataIn.data_cspc
149 149 self.dataOut.nProfiles = self.dataIn.nProfiles
150 150 self.dataOut.nIncohInt = self.dataIn.nIncohInt
151 151 self.dataOut.nFFTPoints = self.dataIn.nFFTPoints
152 152 self.dataOut.ippFactor = self.dataIn.ippFactor
153 153 self.dataOut.abscissaList = self.dataIn.getVelRange(1)
154 154 self.dataOut.spc_noise = self.dataIn.getNoise()
155 155 self.dataOut.spc_range = (self.dataIn.getFreqRange(1) , self.dataIn.getAcfRange(1) , self.dataIn.getVelRange(1))
156 156 # self.dataOut.normFactor = self.dataIn.normFactor
157 157 self.dataOut.pairsList = self.dataIn.pairsList
158 158 self.dataOut.groupList = self.dataIn.pairsList
159 159 self.dataOut.flagNoData = False
160 160
161 161 if hasattr(self.dataIn, 'flagDataAsBlock'):
162 162 self.dataOut.flagDataAsBlock = self.dataIn.flagDataAsBlock
163 163
164 164 if hasattr(self.dataIn, 'ChanDist'): #Distances of receiver channels
165 165 self.dataOut.ChanDist = self.dataIn.ChanDist
166 166 else: self.dataOut.ChanDist = None
167 167
168 168 #if hasattr(self.dataIn, 'VelRange'): #Velocities range
169 169 # self.dataOut.VelRange = self.dataIn.VelRange
170 170 #else: self.dataOut.VelRange = None
171 171
172 172 if hasattr(self.dataIn, 'RadarConst'): #Radar Constant
173 173 self.dataOut.RadarConst = self.dataIn.RadarConst
174 174
175 175 if hasattr(self.dataIn, 'NPW'): #NPW
176 176 self.dataOut.NPW = self.dataIn.NPW
177 177
178 178 if hasattr(self.dataIn, 'COFA'): #COFA
179 179 self.dataOut.COFA = self.dataIn.COFA
180 180
181 181
182 182
183 183 #---------------------- Correlation Data ---------------------------
184 184
185 185 if self.dataIn.type == "Correlation":
186 186 acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf = self.dataIn.splitFunctions()
187 187
188 188 self.dataOut.data_pre = (self.dataIn.data_cf[acf_ind,:], self.dataIn.data_cf[ccf_ind,:,:])
189 189 self.dataOut.normFactor = (self.dataIn.normFactor[acf_ind,:], self.dataIn.normFactor[ccf_ind,:])
190 190 self.dataOut.groupList = (acf_pairs, ccf_pairs)
191 191
192 192 self.dataOut.abscissaList = self.dataIn.lagRange
193 193 self.dataOut.noise = self.dataIn.noise
194 194 self.dataOut.data_snr = self.dataIn.SNR
195 195 self.dataOut.flagNoData = False
196 196 self.dataOut.nAvg = self.dataIn.nAvg
197 197
198 198 #---------------------- Parameters Data ---------------------------
199 199
200 200 if self.dataIn.type == "Parameters":
201 201 self.dataOut.copy(self.dataIn)
202 202 self.dataOut.flagNoData = False
203 203 #print("yo si entre")
204 204
205 205 return True
206 206
207 207 self.__updateObjFromInput()
208 208 #print("yo si entre2")
209 209
210 210 self.dataOut.utctimeInit = self.dataIn.utctime
211 211 self.dataOut.paramInterval = self.dataIn.timeInterval
212 212 #print("soy spectra ",self.dataOut.utctimeInit)
213 213 return
214 214
215 215
216 216 def target(tups):
217 217
218 218 obj, args = tups
219 219
220 220 return obj.FitGau(args)
221 221
222 222 class RemoveWideGC(Operation):
223 223 ''' This class remove the wide clutter and replace it with a simple interpolation points
224 224 This mainly applies to CLAIRE radar
225 225
226 226 ClutterWidth : Width to look for the clutter peak
227 227
228 228 Input:
229 229
230 230 self.dataOut.data_pre : SPC and CSPC
231 231 self.dataOut.spc_range : To select wind and rainfall velocities
232 232
233 233 Affected:
234 234
235 235 self.dataOut.data_pre : It is used for the new SPC and CSPC ranges of wind
236 236
237 237 Written by D. ScipiΓ³n 25.02.2021
238 238 '''
239 239 def __init__(self):
240 240 Operation.__init__(self)
241 241 self.i = 0
242 242 self.ich = 0
243 243 self.ir = 0
244 244
245 245 def run(self, dataOut, ClutterWidth=2.5):
246 246 # print ('Entering RemoveWideGC ... ')
247 247
248 248 self.spc = dataOut.data_pre[0].copy()
249 249 self.spc_out = dataOut.data_pre[0].copy()
250 250 self.Num_Chn = self.spc.shape[0]
251 251 self.Num_Hei = self.spc.shape[2]
252 252 VelRange = dataOut.spc_range[2][:-1]
253 253 dv = VelRange[1]-VelRange[0]
254 254
255 255 # Find the velocities that corresponds to zero
256 256 gc_values = numpy.squeeze(numpy.where(numpy.abs(VelRange) <= ClutterWidth))
257 257
258 258 # Removing novalid data from the spectra
259 259 for ich in range(self.Num_Chn) :
260 260 for ir in range(self.Num_Hei) :
261 261 # Estimate the noise at each range
262 262 HSn = hildebrand_sekhon(self.spc[ich,:,ir],dataOut.nIncohInt)
263 263
264 264 # Removing the noise floor at each range
265 265 novalid = numpy.where(self.spc[ich,:,ir] < HSn)
266 266 self.spc[ich,novalid,ir] = HSn
267 267
268 268 junk = numpy.append(numpy.insert(numpy.squeeze(self.spc[ich,gc_values,ir]),0,HSn),HSn)
269 269 j1index = numpy.squeeze(numpy.where(numpy.diff(junk)>0))
270 270 j2index = numpy.squeeze(numpy.where(numpy.diff(junk)<0))
271 271 if ((numpy.size(j1index)<=1) | (numpy.size(j2index)<=1)) :
272 272 continue
273 273 junk3 = numpy.squeeze(numpy.diff(j1index))
274 274 junk4 = numpy.squeeze(numpy.diff(j2index))
275 275
276 276 valleyindex = j2index[numpy.where(junk4>1)]
277 277 peakindex = j1index[numpy.where(junk3>1)]
278 278
279 279 isvalid = numpy.squeeze(numpy.where(numpy.abs(VelRange[gc_values[peakindex]]) <= 2.5*dv))
280 280 if numpy.size(isvalid) == 0 :
281 281 continue
282 282 if numpy.size(isvalid) >1 :
283 283 vindex = numpy.argmax(self.spc[ich,gc_values[peakindex[isvalid]],ir])
284 284 isvalid = isvalid[vindex]
285 285
286 286 # clutter peak
287 287 gcpeak = peakindex[isvalid]
288 288 vl = numpy.where(valleyindex < gcpeak)
289 289 if numpy.size(vl) == 0:
290 290 continue
291 291 gcvl = valleyindex[vl[0][-1]]
292 292 vr = numpy.where(valleyindex > gcpeak)
293 293 if numpy.size(vr) == 0:
294 294 continue
295 295 gcvr = valleyindex[vr[0][0]]
296 296
297 297 # Removing the clutter
298 298 interpindex = numpy.array([gc_values[gcvl], gc_values[gcvr]])
299 299 gcindex = gc_values[gcvl+1:gcvr-1]
300 300 self.spc_out[ich,gcindex,ir] = numpy.interp(VelRange[gcindex],VelRange[interpindex],self.spc[ich,interpindex,ir])
301 301
302 302 dataOut.data_pre[0] = self.spc_out
303 303 #print ('Leaving RemoveWideGC ... ')
304 304 return dataOut
305 305
306 306 class SpectralFilters(Operation):
307 307 ''' This class allows to replace the novalid values with noise for each channel
308 308 This applies to CLAIRE RADAR
309 309
310 310 PositiveLimit : RightLimit of novalid data
311 311 NegativeLimit : LeftLimit of novalid data
312 312
313 313 Input:
314 314
315 315 self.dataOut.data_pre : SPC and CSPC
316 316 self.dataOut.spc_range : To select wind and rainfall velocities
317 317
318 318 Affected:
319 319
320 320 self.dataOut.data_pre : It is used for the new SPC and CSPC ranges of wind
321 321
322 322 Written by D. ScipiΓ³n 29.01.2021
323 323 '''
324 324 def __init__(self):
325 325 Operation.__init__(self)
326 326 self.i = 0
327 327
328 328 def run(self, dataOut, ):
329 329
330 330 self.spc = dataOut.data_pre[0].copy()
331 331 self.Num_Chn = self.spc.shape[0]
332 332 VelRange = dataOut.spc_range[2]
333 333
334 334 # novalid corresponds to data within the Negative and PositiveLimit
335 335
336 336
337 337 # Removing novalid data from the spectra
338 338 for i in range(self.Num_Chn):
339 339 self.spc[i,novalid,:] = dataOut.noise[i]
340 340 dataOut.data_pre[0] = self.spc
341 341 return dataOut
342 342
343 343 class GaussianFit(Operation):
344 344
345 345 '''
346 346 Function that fit of one and two generalized gaussians (gg) based
347 347 on the PSD shape across an "power band" identified from a cumsum of
348 348 the measured spectrum - noise.
349 349
350 350 Input:
351 351 self.dataOut.data_pre : SelfSpectra
352 352
353 353 Output:
354 354 self.dataOut.SPCparam : SPC_ch1, SPC_ch2
355 355
356 356 '''
357 357 def __init__(self):
358 358 Operation.__init__(self)
359 359 self.i=0
360 360
361 361
362 362 # def run(self, dataOut, num_intg=7, pnoise=1., SNRlimit=-9): #num_intg: Incoherent integrations, pnoise: Noise, vel_arr: range of velocities, similar to the ftt points
363 363 def run(self, dataOut, SNRdBlimit=-9, method='generalized'):
364 364 """This routine will find a couple of generalized Gaussians to a power spectrum
365 365 methods: generalized, squared
366 366 input: spc
367 367 output:
368 368 noise, amplitude0,shift0,width0,p0,Amplitude1,shift1,width1,p1
369 369 """
370 370 print ('Entering ',method,' double Gaussian fit')
371 371 self.spc = dataOut.data_pre[0].copy()
372 372 self.Num_Hei = self.spc.shape[2]
373 373 self.Num_Bin = self.spc.shape[1]
374 374 self.Num_Chn = self.spc.shape[0]
375 375
376 376 start_time = time.time()
377 377
378 378 pool = Pool(processes=self.Num_Chn)
379 379 args = [(dataOut.spc_range[2], ich, dataOut.spc_noise[ich], dataOut.nIncohInt, SNRdBlimit) for ich in range(self.Num_Chn)]
380 380 objs = [self for __ in range(self.Num_Chn)]
381 381 attrs = list(zip(objs, args))
382 382 DGauFitParam = pool.map(target, attrs)
383 383 # Parameters:
384 384 # 0. Noise, 1. Amplitude, 2. Shift, 3. Width 4. Power
385 385 dataOut.DGauFitParams = numpy.asarray(DGauFitParam)
386 386
387 387 # Double Gaussian Curves
388 388 gau0 = numpy.zeros([self.Num_Chn,self.Num_Bin,self.Num_Hei])
389 389 gau0[:] = numpy.NaN
390 390 gau1 = numpy.zeros([self.Num_Chn,self.Num_Bin,self.Num_Hei])
391 391 gau1[:] = numpy.NaN
392 392 x_mtr = numpy.transpose(numpy.tile(dataOut.getVelRange(1)[:-1], (self.Num_Hei,1)))
393 393 for iCh in range(self.Num_Chn):
394 394 N0 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][0,:,0]] * self.Num_Bin))
395 395 N1 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][0,:,1]] * self.Num_Bin))
396 396 A0 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][1,:,0]] * self.Num_Bin))
397 397 A1 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][1,:,1]] * self.Num_Bin))
398 398 v0 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][2,:,0]] * self.Num_Bin))
399 399 v1 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][2,:,1]] * self.Num_Bin))
400 400 s0 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][3,:,0]] * self.Num_Bin))
401 401 s1 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][3,:,1]] * self.Num_Bin))
402 402 if method == 'genealized':
403 403 p0 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][4,:,0]] * self.Num_Bin))
404 404 p1 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][4,:,1]] * self.Num_Bin))
405 405 elif method == 'squared':
406 406 p0 = 2.
407 407 p1 = 2.
408 408 gau0[iCh] = A0*numpy.exp(-0.5*numpy.abs((x_mtr-v0)/s0)**p0)+N0
409 409 gau1[iCh] = A1*numpy.exp(-0.5*numpy.abs((x_mtr-v1)/s1)**p1)+N1
410 410 dataOut.GaussFit0 = gau0
411 411 dataOut.GaussFit1 = gau1
412 412
413 413 print('Leaving ',method ,' double Gaussian fit')
414 414 return dataOut
415 415
416 416 def FitGau(self, X):
417 417 # print('Entering FitGau')
418 418 # Assigning the variables
419 419 Vrange, ch, wnoise, num_intg, SNRlimit = X
420 420 # Noise Limits
421 421 noisebl = wnoise * 0.9
422 422 noisebh = wnoise * 1.1
423 423 # Radar Velocity
424 424 Va = max(Vrange)
425 425 deltav = Vrange[1] - Vrange[0]
426 426 x = numpy.arange(self.Num_Bin)
427 427
428 428 # print ('stop 0')
429 429
430 430 # 5 parameters, 2 Gaussians
431 431 DGauFitParam = numpy.zeros([5, self.Num_Hei,2])
432 432 DGauFitParam[:] = numpy.NaN
433 433
434 434 # SPCparam = []
435 435 # SPC_ch1 = numpy.zeros([self.Num_Bin,self.Num_Hei])
436 436 # SPC_ch2 = numpy.zeros([self.Num_Bin,self.Num_Hei])
437 437 # SPC_ch1[:] = 0 #numpy.NaN
438 438 # SPC_ch2[:] = 0 #numpy.NaN
439 439 # print ('stop 1')
440 440 for ht in range(self.Num_Hei):
441 441 # print (ht)
442 442 # print ('stop 2')
443 443 # Spectra at each range
444 444 spc = numpy.asarray(self.spc)[ch,:,ht]
445 445 snr = ( spc.mean() - wnoise ) / wnoise
446 446 snrdB = 10.*numpy.log10(snr)
447 447
448 448 #print ('stop 3')
449 449 if snrdB < SNRlimit :
450 450 # snr = numpy.NaN
451 451 # SPC_ch1[:,ht] = 0#numpy.NaN
452 452 # SPC_ch1[:,ht] = 0#numpy.NaN
453 453 # SPCparam = (SPC_ch1,SPC_ch2)
454 454 # print ('SNR less than SNRth')
455 455 continue
456 456 # wnoise = hildebrand_sekhon(spc,num_intg)
457 457 # print ('stop 2.01')
458 458 #############################################
459 459 # normalizing spc and noise
460 460 # This part differs from gg1
461 461 # spc_norm_max = max(spc) #commented by D. ScipiΓ³n 19.03.2021
462 462 #spc = spc / spc_norm_max
463 463 # pnoise = pnoise #/ spc_norm_max #commented by D. ScipiΓ³n 19.03.2021
464 464 #############################################
465 465
466 466 # print ('stop 2.1')
467 467 fatspectra=1.0
468 468 # noise per channel.... we might want to use the noise at each range
469 469
470 470 # wnoise = noise_ #/ spc_norm_max #commented by D. ScipiΓ³n 19.03.2021
471 471 #wnoise,stdv,i_max,index =enoise(spc,num_intg) #noise estimate using Hildebrand Sekhon, only wnoise is used
472 472 #if wnoise>1.1*pnoise: # to be tested later
473 473 # wnoise=pnoise
474 474 # noisebl = wnoise*0.9
475 475 # noisebh = wnoise*1.1
476 476 spc = spc - wnoise # signal
477 477
478 478 # print ('stop 2.2')
479 479 minx = numpy.argmin(spc)
480 480 #spcs=spc.copy()
481 481 spcs = numpy.roll(spc,-minx)
482 482 cum = numpy.cumsum(spcs)
483 483 # tot_noise = wnoise * self.Num_Bin #64;
484 484
485 485 # print ('stop 2.3')
486 486 # snr = sum(spcs) / tot_noise
487 487 # snrdB = 10.*numpy.log10(snr)
488 488 #print ('stop 3')
489 489 # if snrdB < SNRlimit :
490 490 # snr = numpy.NaN
491 491 # SPC_ch1[:,ht] = 0#numpy.NaN
492 492 # SPC_ch1[:,ht] = 0#numpy.NaN
493 493 # SPCparam = (SPC_ch1,SPC_ch2)
494 494 # print ('SNR less than SNRth')
495 495 # continue
496 496
497 497
498 498 #if snrdB<-18 or numpy.isnan(snrdB) or num_intg<4:
499 499 # return [None,]*4,[None,]*4,None,snrdB,None,None,[None,]*5,[None,]*9,None
500 500 # print ('stop 4')
501 501 cummax = max(cum)
502 502 epsi = 0.08 * fatspectra # cumsum to narrow down the energy region
503 503 cumlo = cummax * epsi
504 504 cumhi = cummax * (1-epsi)
505 505 powerindex = numpy.array(numpy.where(numpy.logical_and(cum>cumlo, cum<cumhi))[0])
506 506
507 507 # print ('stop 5')
508 508 if len(powerindex) < 1:# case for powerindex 0
509 509 # print ('powerindex < 1')
510 510 continue
511 511 powerlo = powerindex[0]
512 512 powerhi = powerindex[-1]
513 513 powerwidth = powerhi-powerlo
514 514 if powerwidth <= 1:
515 515 # print('powerwidth <= 1')
516 516 continue
517 517
518 518 # print ('stop 6')
519 519 firstpeak = powerlo + powerwidth/10.# first gaussian energy location
520 520 secondpeak = powerhi - powerwidth/10. #second gaussian energy location
521 521 midpeak = (firstpeak + secondpeak)/2.
522 522 firstamp = spcs[int(firstpeak)]
523 523 secondamp = spcs[int(secondpeak)]
524 524 midamp = spcs[int(midpeak)]
525 525
526 526 y_data = spc + wnoise
527 527
528 528 ''' single Gaussian '''
529 529 shift0 = numpy.mod(midpeak+minx, self.Num_Bin )
530 530 width0 = powerwidth/4.#Initialization entire power of spectrum divided by 4
531 531 power0 = 2.
532 532 amplitude0 = midamp
533 533 state0 = [shift0,width0,amplitude0,power0,wnoise]
534 534 bnds = ((0,self.Num_Bin-1),(1,powerwidth),(0,None),(0.5,3.),(noisebl,noisebh))
535 535 lsq1 = fmin_l_bfgs_b(self.misfit1, state0, args=(y_data,x,num_intg), bounds=bnds, approx_grad=True)
536 536 # print ('stop 7.1')
537 537 # print (bnds)
538 538
539 539 chiSq1=lsq1[1]
540 540
541 541 # print ('stop 8')
542 542 if fatspectra<1.0 and powerwidth<4:
543 543 choice=0
544 544 Amplitude0=lsq1[0][2]
545 545 shift0=lsq1[0][0]
546 546 width0=lsq1[0][1]
547 547 p0=lsq1[0][3]
548 548 Amplitude1=0.
549 549 shift1=0.
550 550 width1=0.
551 551 p1=0.
552 552 noise=lsq1[0][4]
553 553 #return (numpy.array([shift0,width0,Amplitude0,p0]),
554 554 # numpy.array([shift1,width1,Amplitude1,p1]),noise,snrdB,chiSq1,6.,sigmas1,[None,]*9,choice)
555 555
556 556 # print ('stop 9')
557 557 ''' two Gaussians '''
558 558 #shift0=numpy.mod(firstpeak+minx,64); shift1=numpy.mod(secondpeak+minx,64)
559 559 shift0 = numpy.mod(firstpeak+minx, self.Num_Bin )
560 560 shift1 = numpy.mod(secondpeak+minx, self.Num_Bin )
561 561 width0 = powerwidth/6.
562 562 width1 = width0
563 563 power0 = 2.
564 564 power1 = power0
565 565 amplitude0 = firstamp
566 566 amplitude1 = secondamp
567 567 state0 = [shift0,width0,amplitude0,power0,shift1,width1,amplitude1,power1,wnoise]
568 568 #bnds=((0,63),(1,powerwidth/2.),(0,None),(0.5,3.),(0,63),(1,powerwidth/2.),(0,None),(0.5,3.),(noisebl,noisebh))
569 569 bnds=((0,self.Num_Bin-1),(1,powerwidth/2.),(0,None),(0.5,3.),(0,self.Num_Bin-1),(1,powerwidth/2.),(0,None),(0.5,3.),(noisebl,noisebh))
570 570 #bnds=(( 0,(self.Num_Bin-1) ),(1,powerwidth/2.),(0,None),(0.5,3.),( 0,(self.Num_Bin-1)),(1,powerwidth/2.),(0,None),(0.5,3.),(0.1,0.5))
571 571
572 572 # print ('stop 10')
573 573 lsq2 = fmin_l_bfgs_b( self.misfit2 , state0 , args=(y_data,x,num_intg) , bounds=bnds , approx_grad=True )
574 574
575 575 # print ('stop 11')
576 576 chiSq2 = lsq2[1]
577 577
578 578 # print ('stop 12')
579 579
580 580 oneG = (chiSq1<5 and chiSq1/chiSq2<2.0) and (abs(lsq2[0][0]-lsq2[0][4])<(lsq2[0][1]+lsq2[0][5])/3. or abs(lsq2[0][0]-lsq2[0][4])<10)
581 581
582 582 # print ('stop 13')
583 583 if snrdB>-12: # when SNR is strong pick the peak with least shift (LOS velocity) error
584 584 if oneG:
585 585 choice = 0
586 586 else:
587 587 w1 = lsq2[0][1]; w2 = lsq2[0][5]
588 588 a1 = lsq2[0][2]; a2 = lsq2[0][6]
589 589 p1 = lsq2[0][3]; p2 = lsq2[0][7]
590 590 s1 = (2**(1+1./p1))*scipy.special.gamma(1./p1)/p1
591 591 s2 = (2**(1+1./p2))*scipy.special.gamma(1./p2)/p2
592 592 gp1 = a1*w1*s1; gp2 = a2*w2*s2 # power content of each ggaussian with proper p scaling
593 593
594 594 if gp1>gp2:
595 595 if a1>0.7*a2:
596 596 choice = 1
597 597 else:
598 598 choice = 2
599 599 elif gp2>gp1:
600 600 if a2>0.7*a1:
601 601 choice = 2
602 602 else:
603 603 choice = 1
604 604 else:
605 605 choice = numpy.argmax([a1,a2])+1
606 606 #else:
607 607 #choice=argmin([std2a,std2b])+1
608 608
609 609 else: # with low SNR go to the most energetic peak
610 610 choice = numpy.argmax([lsq1[0][2]*lsq1[0][1],lsq2[0][2]*lsq2[0][1],lsq2[0][6]*lsq2[0][5]])
611 611
612 612 # print ('stop 14')
613 613 shift0 = lsq2[0][0]
614 614 vel0 = Vrange[0] + shift0 * deltav
615 615 shift1 = lsq2[0][4]
616 616 # vel1=Vrange[0] + shift1 * deltav
617 617
618 618 # max_vel = 1.0
619 619 # Va = max(Vrange)
620 620 # deltav = Vrange[1]-Vrange[0]
621 621 # print ('stop 15')
622 622 #first peak will be 0, second peak will be 1
623 623 # if vel0 > -1.0 and vel0 < max_vel : #first peak is in the correct range # Commented by D.ScipiΓ³n 19.03.2021
624 624 if vel0 > -Va and vel0 < Va : #first peak is in the correct range
625 625 shift0 = lsq2[0][0]
626 626 width0 = lsq2[0][1]
627 627 Amplitude0 = lsq2[0][2]
628 628 p0 = lsq2[0][3]
629 629
630 630 shift1 = lsq2[0][4]
631 631 width1 = lsq2[0][5]
632 632 Amplitude1 = lsq2[0][6]
633 633 p1 = lsq2[0][7]
634 634 noise = lsq2[0][8]
635 635 else:
636 636 shift1 = lsq2[0][0]
637 637 width1 = lsq2[0][1]
638 638 Amplitude1 = lsq2[0][2]
639 639 p1 = lsq2[0][3]
640 640
641 641 shift0 = lsq2[0][4]
642 642 width0 = lsq2[0][5]
643 643 Amplitude0 = lsq2[0][6]
644 644 p0 = lsq2[0][7]
645 645 noise = lsq2[0][8]
646 646
647 647 if Amplitude0<0.05: # in case the peak is noise
648 648 shift0,width0,Amplitude0,p0 = 4*[numpy.NaN]
649 649 if Amplitude1<0.05:
650 650 shift1,width1,Amplitude1,p1 = 4*[numpy.NaN]
651 651
652 652 # print ('stop 16 ')
653 653 # SPC_ch1[:,ht] = noise + Amplitude0*numpy.exp(-0.5*(abs(x-shift0)/width0)**p0)
654 654 # SPC_ch2[:,ht] = noise + Amplitude1*numpy.exp(-0.5*(abs(x-shift1)/width1)**p1)
655 655 # SPCparam = (SPC_ch1,SPC_ch2)
656 656
657 657 DGauFitParam[0,ht,0] = noise
658 658 DGauFitParam[0,ht,1] = noise
659 659 DGauFitParam[1,ht,0] = Amplitude0
660 660 DGauFitParam[1,ht,1] = Amplitude1
661 661 DGauFitParam[2,ht,0] = Vrange[0] + shift0 * deltav
662 662 DGauFitParam[2,ht,1] = Vrange[0] + shift1 * deltav
663 663 DGauFitParam[3,ht,0] = width0 * deltav
664 664 DGauFitParam[3,ht,1] = width1 * deltav
665 665 DGauFitParam[4,ht,0] = p0
666 666 DGauFitParam[4,ht,1] = p1
667 667
668 668 # print (DGauFitParam.shape)
669 669 # print ('Leaving FitGau')
670 670 return DGauFitParam
671 671 # return SPCparam
672 672 # return GauSPC
673 673
674 674 def y_model1(self,x,state):
675 675 shift0, width0, amplitude0, power0, noise = state
676 676 model0 = amplitude0*numpy.exp(-0.5*abs((x - shift0)/width0)**power0)
677 677 model0u = amplitude0*numpy.exp(-0.5*abs((x - shift0 - self.Num_Bin)/width0)**power0)
678 678 model0d = amplitude0*numpy.exp(-0.5*abs((x - shift0 + self.Num_Bin)/width0)**power0)
679 679 return model0 + model0u + model0d + noise
680 680
681 681 def y_model2(self,x,state): #Equation for two generalized Gaussians with Nyquist
682 682 shift0, width0, amplitude0, power0, shift1, width1, amplitude1, power1, noise = state
683 683 model0 = amplitude0*numpy.exp(-0.5*abs((x-shift0)/width0)**power0)
684 684 model0u = amplitude0*numpy.exp(-0.5*abs((x - shift0 - self.Num_Bin)/width0)**power0)
685 685 model0d = amplitude0*numpy.exp(-0.5*abs((x - shift0 + self.Num_Bin)/width0)**power0)
686 686
687 687 model1 = amplitude1*numpy.exp(-0.5*abs((x - shift1)/width1)**power1)
688 688 model1u = amplitude1*numpy.exp(-0.5*abs((x - shift1 - self.Num_Bin)/width1)**power1)
689 689 model1d = amplitude1*numpy.exp(-0.5*abs((x - shift1 + self.Num_Bin)/width1)**power1)
690 690 return model0 + model0u + model0d + model1 + model1u + model1d + noise
691 691
692 692 def misfit1(self,state,y_data,x,num_intg): # This function compares how close real data is with the model data, the close it is, the better it is.
693 693
694 694 return num_intg*sum((numpy.log(y_data)-numpy.log(self.y_model1(x,state)))**2)#/(64-5.) # /(64-5.) can be commented
695 695
696 696 def misfit2(self,state,y_data,x,num_intg):
697 697 return num_intg*sum((numpy.log(y_data)-numpy.log(self.y_model2(x,state)))**2)#/(64-9.)
698 698
699 699
700 700
701 701 class PrecipitationProc(Operation):
702 702
703 703 '''
704 704 Operator that estimates Reflectivity factor (Z), and estimates rainfall Rate (R)
705 705
706 706 Input:
707 707 self.dataOut.data_pre : SelfSpectra
708 708
709 709 Output:
710 710
711 711 self.dataOut.data_output : Reflectivity factor, rainfall Rate
712 712
713 713
714 714 Parameters affected:
715 715 '''
716 716
717 717 def __init__(self):
718 718 Operation.__init__(self)
719 719 self.i=0
720 720
721 721 def run(self, dataOut, radar=None, Pt=5000, Gt=295.1209, Gr=70.7945, Lambda=0.6741, aL=2.5118,
722 722 tauW=4e-06, ThetaT=0.1656317, ThetaR=0.36774087, Km2 = 0.93, Altitude=3350,SNRdBlimit=-30):
723 723
724 724 # print ('Entering PrecepitationProc ... ')
725 725
726 726 if radar == "MIRA35C" :
727 727
728 728 self.spc = dataOut.data_pre[0].copy()
729 729 self.Num_Hei = self.spc.shape[2]
730 730 self.Num_Bin = self.spc.shape[1]
731 731 self.Num_Chn = self.spc.shape[0]
732 732 Ze = self.dBZeMODE2(dataOut)
733 733
734 734 else:
735 735
736 736 self.spc = dataOut.data_pre[0].copy()
737 737
738 738 #NOTA SE DEBE REMOVER EL RANGO DEL PULSO TX
739 739 self.spc[:,:,0:7]= numpy.NaN
740 740
741 741 self.Num_Hei = self.spc.shape[2]
742 742 self.Num_Bin = self.spc.shape[1]
743 743 self.Num_Chn = self.spc.shape[0]
744 744
745 745 VelRange = dataOut.spc_range[2]
746 746
747 747 ''' Se obtiene la constante del RADAR '''
748 748
749 749 self.Pt = Pt
750 750 self.Gt = Gt
751 751 self.Gr = Gr
752 752 self.Lambda = Lambda
753 753 self.aL = aL
754 754 self.tauW = tauW
755 755 self.ThetaT = ThetaT
756 756 self.ThetaR = ThetaR
757 757 self.GSys = 10**(36.63/10) # Ganancia de los LNA 36.63 dB
758 758 self.lt = 10**(1.67/10) # Perdida en cables Tx 1.67 dB
759 759 self.lr = 10**(5.73/10) # Perdida en cables Rx 5.73 dB
760 760
761 761 Numerator = ( (4*numpy.pi)**3 * aL**2 * 16 * numpy.log(2) )
762 762 Denominator = ( Pt * Gt * Gr * Lambda**2 * SPEED_OF_LIGHT * tauW * numpy.pi * ThetaT * ThetaR)
763 763 RadarConstant = 10e-26 * Numerator / Denominator #
764 764 ExpConstant = 10**(40/10) #Constante Experimental
765 765
766 766 SignalPower = numpy.zeros([self.Num_Chn,self.Num_Bin,self.Num_Hei])
767 767 for i in range(self.Num_Chn):
768 768 SignalPower[i,:,:] = self.spc[i,:,:] - dataOut.noise[i]
769 769 SignalPower[numpy.where(SignalPower < 0)] = 1e-20
770 770
771 771 SPCmean = numpy.mean(SignalPower, 0)
772 772 Pr = SPCmean[:,:]/dataOut.normFactor
773 773
774 774 # Declaring auxiliary variables
775 775 Range = dataOut.heightList*1000. #Range in m
776 776 # replicate the heightlist to obtain a matrix [Num_Bin,Num_Hei]
777 777 rMtrx = numpy.transpose(numpy.transpose([dataOut.heightList*1000.] * self.Num_Bin))
778 778 zMtrx = rMtrx+Altitude
779 779 # replicate the VelRange to obtain a matrix [Num_Bin,Num_Hei]
780 780 VelMtrx = numpy.transpose(numpy.tile(VelRange[:-1], (self.Num_Hei,1)))
781 781
782 782 # height dependence to air density Foote and Du Toit (1969)
783 783 delv_z = 1 + 3.68e-5 * zMtrx + 1.71e-9 * zMtrx**2
784 784 VMtrx = VelMtrx / delv_z #Normalized velocity
785 785 VMtrx[numpy.where(VMtrx> 9.6)] = numpy.NaN
786 786 # Diameter is related to the fall speed of falling drops
787 787 D_Vz = -1.667 * numpy.log( 0.9369 - 0.097087 * VMtrx ) # D in [mm]
788 788 # Only valid for D>= 0.16 mm
789 789 D_Vz[numpy.where(D_Vz < 0.16)] = numpy.NaN
790 790
791 791 #Calculate Radar Reflectivity ETAn
792 792 ETAn = (RadarConstant *ExpConstant) * Pr * rMtrx**2 #Reflectivity (ETA)
793 793 ETAd = ETAn * 6.18 * exp( -0.6 * D_Vz ) * delv_z
794 794 # Radar Cross Section
795 795 sigmaD = Km2 * (D_Vz * 1e-3 )**6 * numpy.pi**5 / Lambda**4
796 796 # Drop Size Distribution
797 797 DSD = ETAn / sigmaD
798 798 # Equivalente Reflectivy
799 799 Ze_eqn = numpy.nansum( DSD * D_Vz**6 ,axis=0)
800 800 Ze_org = numpy.nansum(ETAn * Lambda**4, axis=0) / (1e-18*numpy.pi**5 * Km2) # [mm^6 /m^3]
801 801 # RainFall Rate
802 802 RR = 0.0006*numpy.pi * numpy.nansum( D_Vz**3 * DSD * VelMtrx ,0) #mm/hr
803 803
804 804 # Censoring the data
805 805 # Removing data with SNRth < 0dB se debe considerar el SNR por canal
806 806 SNRth = 10**(SNRdBlimit/10) #-30dB
807 807 novalid = numpy.where((dataOut.data_snr[0,:] <SNRth) | (dataOut.data_snr[1,:] <SNRth) | (dataOut.data_snr[2,:] <SNRth)) # AND condition. Maybe OR condition better
808 808 W = numpy.nanmean(dataOut.data_dop,0)
809 809 W[novalid] = numpy.NaN
810 810 Ze_org[novalid] = numpy.NaN
811 811 RR[novalid] = numpy.NaN
812 812
813 813 dataOut.data_output = RR[8]
814 814 dataOut.data_param = numpy.ones([3,self.Num_Hei])
815 815 dataOut.channelList = [0,1,2]
816 816
817 817 dataOut.data_param[0]=10*numpy.log10(Ze_org)
818 818 dataOut.data_param[1]=-W
819 819 dataOut.data_param[2]=RR
820 820
821 821 # print ('Leaving PrecepitationProc ... ')
822 822 return dataOut
823 823
824 824 def dBZeMODE2(self, dataOut): # Processing for MIRA35C
825 825
826 826 NPW = dataOut.NPW
827 827 COFA = dataOut.COFA
828 828
829 829 SNR = numpy.array([self.spc[0,:,:] / NPW[0]]) #, self.spc[1,:,:] / NPW[1]])
830 830 RadarConst = dataOut.RadarConst
831 831 #frequency = 34.85*10**9
832 832
833 833 ETA = numpy.zeros(([self.Num_Chn ,self.Num_Hei]))
834 834 data_output = numpy.ones([self.Num_Chn , self.Num_Hei])*numpy.NaN
835 835
836 836 ETA = numpy.sum(SNR,1)
837 837
838 838 ETA = numpy.where(ETA != 0. , ETA, numpy.NaN)
839 839
840 840 Ze = numpy.ones([self.Num_Chn, self.Num_Hei] )
841 841
842 842 for r in range(self.Num_Hei):
843 843
844 844 Ze[0,r] = ( ETA[0,r] ) * COFA[0,r][0] * RadarConst * ((r/5000.)**2)
845 845 #Ze[1,r] = ( ETA[1,r] ) * COFA[1,r][0] * RadarConst * ((r/5000.)**2)
846 846
847 847 return Ze
848 848
849 849 # def GetRadarConstant(self):
850 850 #
851 851 # """
852 852 # Constants:
853 853 #
854 854 # Pt: Transmission Power dB 5kW 5000
855 855 # Gt: Transmission Gain dB 24.7 dB 295.1209
856 856 # Gr: Reception Gain dB 18.5 dB 70.7945
857 857 # Lambda: Wavelenght m 0.6741 m 0.6741
858 858 # aL: Attenuation loses dB 4dB 2.5118
859 859 # tauW: Width of transmission pulse s 4us 4e-6
860 860 # ThetaT: Transmission antenna bean angle rad 0.1656317 rad 0.1656317
861 861 # ThetaR: Reception antenna beam angle rad 0.36774087 rad 0.36774087
862 862 #
863 863 # """
864 864 #
865 865 # Numerator = ( (4*numpy.pi)**3 * aL**2 * 16 * numpy.log(2) )
866 866 # Denominator = ( Pt * Gt * Gr * Lambda**2 * SPEED_OF_LIGHT * TauW * numpy.pi * ThetaT * TheraR)
867 867 # RadarConstant = Numerator / Denominator
868 868 #
869 869 # return RadarConstant
870 870
871 871
872 872
873 873 class FullSpectralAnalysis(Operation):
874 874
875 875 """
876 876 Function that implements Full Spectral Analysis technique.
877 877
878 878 Input:
879 879 self.dataOut.data_pre : SelfSpectra and CrossSpectra data
880 880 self.dataOut.groupList : Pairlist of channels
881 881 self.dataOut.ChanDist : Physical distance between receivers
882 882
883 883
884 884 Output:
885 885
886 886 self.dataOut.data_output : Zonal wind, Meridional wind, and Vertical wind
887 887
888 888
889 889 Parameters affected: Winds, height range, SNR
890 890
891 891 """
892 892 def run(self, dataOut, Xi01=None, Xi02=None, Xi12=None, Eta01=None, Eta02=None, Eta12=None, SNRdBlimit=-30,
893 893 minheight=None, maxheight=None, NegativeLimit=None, PositiveLimit=None):
894 894
895 895 spc = dataOut.data_pre[0].copy()
896 896 cspc = dataOut.data_pre[1]
897 897 nHeights = spc.shape[2]
898 898
899 899 # first_height = 0.75 #km (ref: data header 20170822)
900 900 # resolution_height = 0.075 #km
901 901 '''
902 902 finding height range. check this when radar parameters are changed!
903 903 '''
904 904 if maxheight is not None:
905 905 # range_max = math.ceil((maxheight - first_height) / resolution_height) # theoretical
906 906 range_max = math.ceil(13.26 * maxheight - 3) # empirical, works better
907 907 else:
908 908 range_max = nHeights
909 909 if minheight is not None:
910 910 # range_min = int((minheight - first_height) / resolution_height) # theoretical
911 911 range_min = int(13.26 * minheight - 5) # empirical, works better
912 912 if range_min < 0:
913 913 range_min = 0
914 914 else:
915 915 range_min = 0
916 916
917 917 pairsList = dataOut.groupList
918 918 if dataOut.ChanDist is not None :
919 919 ChanDist = dataOut.ChanDist
920 920 else:
921 921 ChanDist = numpy.array([[Xi01, Eta01],[Xi02,Eta02],[Xi12,Eta12]])
922 922
923 923 # 4 variables: zonal, meridional, vertical, and average SNR
924 924 data_param = numpy.zeros([4,nHeights]) * numpy.NaN
925 925 velocityX = numpy.zeros([nHeights]) * numpy.NaN
926 926 velocityY = numpy.zeros([nHeights]) * numpy.NaN
927 927 velocityZ = numpy.zeros([nHeights]) * numpy.NaN
928 928
929 929 dbSNR = 10*numpy.log10(numpy.average(dataOut.data_snr,0))
930 930
931 931 '''***********************************************WIND ESTIMATION**************************************'''
932 932 for Height in range(nHeights):
933 933
934 934 if Height >= range_min and Height < range_max:
935 935 # error_code will be useful in future analysis
936 936 [Vzon,Vmer,Vver, error_code] = self.WindEstimation(spc[:,:,Height], cspc[:,:,Height], pairsList,
937 937 ChanDist, Height, dataOut.noise, dataOut.spc_range, dbSNR[Height], SNRdBlimit, NegativeLimit, PositiveLimit,dataOut.frequency)
938 938
939 939 if abs(Vzon) < 100. and abs(Vmer) < 100.:
940 940 velocityX[Height] = Vzon
941 941 velocityY[Height] = -Vmer
942 942 velocityZ[Height] = Vver
943 943
944 944 # Censoring data with SNR threshold
945 945 dbSNR [dbSNR < SNRdBlimit] = numpy.NaN
946 946
947 947 data_param[0] = velocityX
948 948 data_param[1] = velocityY
949 949 data_param[2] = velocityZ
950 950 data_param[3] = dbSNR
951 951 dataOut.data_param = data_param
952 952 return dataOut
953 953
954 954 def moving_average(self,x, N=2):
955 955 """ convolution for smoothenig data. note that last N-1 values are convolution with zeroes """
956 956 return numpy.convolve(x, numpy.ones((N,))/N)[(N-1):]
957 957
958 958 def gaus(self,xSamples,Amp,Mu,Sigma):
959 959 return Amp * numpy.exp(-0.5*((xSamples - Mu)/Sigma)**2)
960 960
961 961 def Moments(self, ySamples, xSamples):
962 962 Power = numpy.nanmean(ySamples) # Power, 0th Moment
963 963 yNorm = ySamples / numpy.nansum(ySamples)
964 964 RadVel = numpy.nansum(xSamples * yNorm) # Radial Velocity, 1st Moment
965 965 Sigma2 = numpy.nansum(yNorm * (xSamples - RadVel)**2) # Spectral Width, 2nd Moment
966 966 StdDev = numpy.sqrt(numpy.abs(Sigma2)) # Desv. Estandar, Ancho espectral
967 967 return numpy.array([Power,RadVel,StdDev])
968 968
969 969 def StopWindEstimation(self, error_code):
970 970 Vzon = numpy.NaN
971 971 Vmer = numpy.NaN
972 972 Vver = numpy.NaN
973 973 return Vzon, Vmer, Vver, error_code
974 974
975 975 def AntiAliasing(self, interval, maxstep):
976 976 """
977 977 function to prevent errors from aliased values when computing phaseslope
978 978 """
979 979 antialiased = numpy.zeros(len(interval))
980 980 copyinterval = interval.copy()
981 981
982 982 antialiased[0] = copyinterval[0]
983 983
984 984 for i in range(1,len(antialiased)):
985 985 step = interval[i] - interval[i-1]
986 986 if step > maxstep:
987 987 copyinterval -= 2*numpy.pi
988 988 antialiased[i] = copyinterval[i]
989 989 elif step < maxstep*(-1):
990 990 copyinterval += 2*numpy.pi
991 991 antialiased[i] = copyinterval[i]
992 992 else:
993 993 antialiased[i] = copyinterval[i].copy()
994 994
995 995 return antialiased
996 996
997 997 def WindEstimation(self, spc, cspc, pairsList, ChanDist, Height, noise, AbbsisaRange, dbSNR, SNRlimit, NegativeLimit, PositiveLimit, radfreq):
998 998 """
999 999 Function that Calculates Zonal, Meridional and Vertical wind velocities.
1000 1000 Initial Version by E. Bocanegra updated by J. Zibell until Nov. 2019.
1001 1001
1002 1002 Input:
1003 1003 spc, cspc : self spectra and cross spectra data. In Briggs notation something like S_i*(S_i)_conj, (S_j)_conj respectively.
1004 1004 pairsList : Pairlist of channels
1005 1005 ChanDist : array of xi_ij and eta_ij
1006 1006 Height : height at which data is processed
1007 1007 noise : noise in [channels] format for specific height
1008 1008 Abbsisarange : range of the frequencies or velocities
1009 1009 dbSNR, SNRlimit : signal to noise ratio in db, lower limit
1010 1010
1011 1011 Output:
1012 1012 Vzon, Vmer, Vver : wind velocities
1013 1013 error_code : int that states where code is terminated
1014 1014
1015 1015 0 : no error detected
1016 1016 1 : Gaussian of mean spc exceeds widthlimit
1017 1017 2 : no Gaussian of mean spc found
1018 1018 3 : SNR to low or velocity to high -> prec. e.g.
1019 1019 4 : at least one Gaussian of cspc exceeds widthlimit
1020 1020 5 : zero out of three cspc Gaussian fits converged
1021 1021 6 : phase slope fit could not be found
1022 1022 7 : arrays used to fit phase have different length
1023 1023 8 : frequency range is either too short (len <= 5) or very long (> 30% of cspc)
1024 1024
1025 1025 """
1026 1026
1027 1027 error_code = 0
1028 1028
1029 1029 nChan = spc.shape[0]
1030 1030 nProf = spc.shape[1]
1031 1031 nPair = cspc.shape[0]
1032 1032
1033 1033 SPC_Samples = numpy.zeros([nChan, nProf]) # for normalized spc values for one height
1034 1034 CSPC_Samples = numpy.zeros([nPair, nProf], dtype=numpy.complex_) # for normalized cspc values
1035 1035 phase = numpy.zeros([nPair, nProf]) # phase between channels
1036 1036 PhaseSlope = numpy.zeros(nPair) # slope of the phases, channelwise
1037 1037 PhaseInter = numpy.zeros(nPair) # intercept to the slope of the phases, channelwise
1038 1038 xFrec = AbbsisaRange[0][:-1] # frequency range
1039 1039 xVel = AbbsisaRange[2][:-1] # velocity range
1040 1040 xSamples = xFrec # the frequency range is taken
1041 1041 delta_x = xSamples[1] - xSamples[0] # delta_f or delta_x
1042 1042
1043 1043 # only consider velocities with in NegativeLimit and PositiveLimit
1044 1044 if (NegativeLimit is None):
1045 1045 NegativeLimit = numpy.min(xVel)
1046 1046 if (PositiveLimit is None):
1047 1047 PositiveLimit = numpy.max(xVel)
1048 1048 xvalid = numpy.where((xVel > NegativeLimit) & (xVel < PositiveLimit))
1049 1049 xSamples_zoom = xSamples[xvalid]
1050 1050
1051 1051 '''Getting Eij and Nij'''
1052 1052 Xi01, Xi02, Xi12 = ChanDist[:,0]
1053 1053 Eta01, Eta02, Eta12 = ChanDist[:,1]
1054 1054
1055 1055 # spwd limit - updated by D. ScipiΓ³n 30.03.2021
1056 1056 widthlimit = 10
1057 1057 '''************************* SPC is normalized ********************************'''
1058 1058 spc_norm = spc.copy()
1059 1059 # For each channel
1060 1060 for i in range(nChan):
1061 1061 spc_sub = spc_norm[i,:] - noise[i] # only the signal power
1062 1062 SPC_Samples[i] = spc_sub / (numpy.nansum(spc_sub) * delta_x)
1063 1063
1064 1064 '''********************** FITTING MEAN SPC GAUSSIAN **********************'''
1065 1065
1066 1066 """ the gaussian of the mean: first subtract noise, then normalize. this is legal because
1067 1067 you only fit the curve and don't need the absolute value of height for calculation,
1068 1068 only for estimation of width. for normalization of cross spectra, you need initial,
1069 1069 unnormalized self-spectra With noise.
1070 1070
1071 1071 Technically, you don't even need to normalize the self-spectra, as you only need the
1072 1072 width of the peak. However, it was left this way. Note that the normalization has a flaw:
1073 1073 due to subtraction of the noise, some values are below zero. Raw "spc" values should be
1074 1074 >= 0, as it is the modulus squared of the signals (complex * it's conjugate)
1075 1075 """
1076 1076 # initial conditions
1077 1077 popt = [1e-10,0,1e-10]
1078 1078 # Spectra average
1079 1079 SPCMean = numpy.average(SPC_Samples,0)
1080 1080 # Moments in frequency
1081 1081 SPCMoments = self.Moments(SPCMean[xvalid], xSamples_zoom)
1082 1082
1083 1083 # Gauss Fit SPC in frequency domain
1084 1084 if dbSNR > SNRlimit: # only if SNR > SNRth
1085 1085 try:
1086 1086 popt,pcov = curve_fit(self.gaus,xSamples_zoom,SPCMean[xvalid],p0=SPCMoments)
1087 1087 if popt[2] <= 0 or popt[2] > widthlimit: # CONDITION
1088 1088 return self.StopWindEstimation(error_code = 1)
1089 1089 FitGauss = self.gaus(xSamples_zoom,*popt)
1090 1090 except :#RuntimeError:
1091 1091 return self.StopWindEstimation(error_code = 2)
1092 1092 else:
1093 1093 return self.StopWindEstimation(error_code = 3)
1094 1094
1095 1095 '''***************************** CSPC Normalization *************************
1096 1096 The Spc spectra are used to normalize the crossspectra. Peaks from precipitation
1097 1097 influence the norm which is not desired. First, a range is identified where the
1098 1098 wind peak is estimated -> sum_wind is sum of those frequencies. Next, the area
1099 1099 around it gets cut off and values replaced by mean determined by the boundary
1100 1100 data -> sum_noise (spc is not normalized here, thats why the noise is important)
1101 1101
1102 1102 The sums are then added and multiplied by range/datapoints, because you need
1103 1103 an integral and not a sum for normalization.
1104 1104
1105 1105 A norm is found according to Briggs 92.
1106 1106 '''
1107 1107 # for each pair
1108 1108 for i in range(nPair):
1109 1109 cspc_norm = cspc[i,:].copy()
1110 1110 chan_index0 = pairsList[i][0]
1111 1111 chan_index1 = pairsList[i][1]
1112 1112 CSPC_Samples[i] = cspc_norm / (numpy.sqrt(numpy.nansum(spc_norm[chan_index0])*numpy.nansum(spc_norm[chan_index1])) * delta_x)
1113 1113 phase[i] = numpy.arctan2(CSPC_Samples[i].imag, CSPC_Samples[i].real)
1114 1114
1115 1115 CSPCmoments = numpy.vstack([self.Moments(numpy.abs(CSPC_Samples[0,xvalid]), xSamples_zoom),
1116 1116 self.Moments(numpy.abs(CSPC_Samples[1,xvalid]), xSamples_zoom),
1117 1117 self.Moments(numpy.abs(CSPC_Samples[2,xvalid]), xSamples_zoom)])
1118 1118
1119 1119 popt01, popt02, popt12 = [1e-10,0,1e-10], [1e-10,0,1e-10] ,[1e-10,0,1e-10]
1120 1120 FitGauss01, FitGauss02, FitGauss12 = numpy.zeros(len(xSamples)), numpy.zeros(len(xSamples)), numpy.zeros(len(xSamples))
1121 1121
1122 1122 '''*******************************FIT GAUSS CSPC************************************'''
1123 1123 try:
1124 1124 popt01,pcov = curve_fit(self.gaus,xSamples_zoom,numpy.abs(CSPC_Samples[0][xvalid]),p0=CSPCmoments[0])
1125 1125 if popt01[2] > widthlimit: # CONDITION
1126 1126 return self.StopWindEstimation(error_code = 4)
1127 1127 popt02,pcov = curve_fit(self.gaus,xSamples_zoom,numpy.abs(CSPC_Samples[1][xvalid]),p0=CSPCmoments[1])
1128 1128 if popt02[2] > widthlimit: # CONDITION
1129 1129 return self.StopWindEstimation(error_code = 4)
1130 1130 popt12,pcov = curve_fit(self.gaus,xSamples_zoom,numpy.abs(CSPC_Samples[2][xvalid]),p0=CSPCmoments[2])
1131 1131 if popt12[2] > widthlimit: # CONDITION
1132 1132 return self.StopWindEstimation(error_code = 4)
1133 1133
1134 1134 FitGauss01 = self.gaus(xSamples_zoom, *popt01)
1135 1135 FitGauss02 = self.gaus(xSamples_zoom, *popt02)
1136 1136 FitGauss12 = self.gaus(xSamples_zoom, *popt12)
1137 1137 except:
1138 1138 return self.StopWindEstimation(error_code = 5)
1139 1139
1140 1140
1141 1141 '''************* Getting Fij ***************'''
1142 1142 # x-axis point of the gaussian where the center is located from GaussFit of spectra
1143 1143 GaussCenter = popt[1]
1144 1144 ClosestCenter = xSamples_zoom[numpy.abs(xSamples_zoom-GaussCenter).argmin()]
1145 1145 PointGauCenter = numpy.where(xSamples_zoom==ClosestCenter)[0][0]
1146 1146
1147 1147 # Point where e^-1 is located in the gaussian
1148 1148 PeMinus1 = numpy.max(FitGauss) * numpy.exp(-1)
1149 1149 FijClosest = FitGauss[numpy.abs(FitGauss-PeMinus1).argmin()] # The closest point to"Peminus1" in "FitGauss"
1150 1150 PointFij = numpy.where(FitGauss==FijClosest)[0][0]
1151 1151 Fij = numpy.abs(xSamples_zoom[PointFij] - xSamples_zoom[PointGauCenter])
1152 1152
1153 1153 '''********** Taking frequency ranges from mean SPCs **********'''
1154 1154 GauWidth = popt[2] * 3/2 # Bandwidth of Gau01
1155 1155 Range = numpy.empty(2)
1156 1156 Range[0] = GaussCenter - GauWidth
1157 1157 Range[1] = GaussCenter + GauWidth
1158 1158 # Point in x-axis where the bandwidth is located (min:max)
1159 1159 ClosRangeMin = xSamples_zoom[numpy.abs(xSamples_zoom-Range[0]).argmin()]
1160 1160 ClosRangeMax = xSamples_zoom[numpy.abs(xSamples_zoom-Range[1]).argmin()]
1161 1161 PointRangeMin = numpy.where(xSamples_zoom==ClosRangeMin)[0][0]
1162 1162 PointRangeMax = numpy.where(xSamples_zoom==ClosRangeMax)[0][0]
1163 1163 Range = numpy.array([ PointRangeMin, PointRangeMax ])
1164 1164 FrecRange = xSamples_zoom[ Range[0] : Range[1] ]
1165 1165
1166 1166 '''************************** Getting Phase Slope ***************************'''
1167 1167 for i in range(nPair):
1168 1168 if len(FrecRange) > 5:
1169 1169 PhaseRange = phase[i, xvalid[0][Range[0]:Range[1]]].copy()
1170 1170 mask = ~numpy.isnan(FrecRange) & ~numpy.isnan(PhaseRange)
1171 1171 if len(FrecRange) == len(PhaseRange):
1172 1172 try:
1173 1173 slope, intercept, _, _, _ = stats.linregress(FrecRange[mask], self.AntiAliasing(PhaseRange[mask], 4.5))
1174 1174 PhaseSlope[i] = slope
1175 1175 PhaseInter[i] = intercept
1176 1176 except:
1177 1177 return self.StopWindEstimation(error_code = 6)
1178 1178 else:
1179 1179 return self.StopWindEstimation(error_code = 7)
1180 1180 else:
1181 1181 return self.StopWindEstimation(error_code = 8)
1182 1182
1183 1183 '''*** Constants A-H correspond to the convention as in Briggs and Vincent 1992 ***'''
1184 1184
1185 1185 '''Getting constant C'''
1186 1186 cC=(Fij*numpy.pi)**2
1187 1187
1188 1188 '''****** Getting constants F and G ******'''
1189 1189 MijEijNij = numpy.array([[Xi02,Eta02], [Xi12,Eta12]])
1190 1190 # MijEijNij = numpy.array([[Xi01,Eta01], [Xi02,Eta02], [Xi12,Eta12]])
1191 1191 # MijResult0 = (-PhaseSlope[0] * cC) / (2*numpy.pi)
1192 1192 MijResult1 = (-PhaseSlope[1] * cC) / (2*numpy.pi)
1193 1193 MijResult2 = (-PhaseSlope[2] * cC) / (2*numpy.pi)
1194 1194 # MijResults = numpy.array([MijResult0, MijResult1, MijResult2])
1195 1195 MijResults = numpy.array([MijResult1, MijResult2])
1196 1196 (cF,cG) = numpy.linalg.solve(MijEijNij, MijResults)
1197 1197
1198 1198 '''****** Getting constants A, B and H ******'''
1199 1199 W01 = numpy.nanmax( FitGauss01 )
1200 1200 W02 = numpy.nanmax( FitGauss02 )
1201 1201 W12 = numpy.nanmax( FitGauss12 )
1202 1202
1203 1203 WijResult01 = ((cF * Xi01 + cG * Eta01)**2)/cC - numpy.log(W01 / numpy.sqrt(numpy.pi / cC))
1204 1204 WijResult02 = ((cF * Xi02 + cG * Eta02)**2)/cC - numpy.log(W02 / numpy.sqrt(numpy.pi / cC))
1205 1205 WijResult12 = ((cF * Xi12 + cG * Eta12)**2)/cC - numpy.log(W12 / numpy.sqrt(numpy.pi / cC))
1206 1206 WijResults = numpy.array([WijResult01, WijResult02, WijResult12])
1207 1207
1208 1208 WijEijNij = numpy.array([ [Xi01**2, Eta01**2, 2*Xi01*Eta01] , [Xi02**2, Eta02**2, 2*Xi02*Eta02] , [Xi12**2, Eta12**2, 2*Xi12*Eta12] ])
1209 1209 (cA,cB,cH) = numpy.linalg.solve(WijEijNij, WijResults)
1210 1210
1211 1211 VxVy = numpy.array([[cA,cH],[cH,cB]])
1212 1212 VxVyResults = numpy.array([-cF,-cG])
1213 1213 (Vmer,Vzon) = numpy.linalg.solve(VxVy, VxVyResults)
1214 1214 Vver = -SPCMoments[1]*SPEED_OF_LIGHT/(2*radfreq)
1215 1215 error_code = 0
1216 1216
1217 1217 return Vzon, Vmer, Vver, error_code
1218 1218
1219 1219 class SpectralMoments(Operation):
1220 1220
1221 1221 '''
1222 1222 Function SpectralMoments()
1223 1223
1224 1224 Calculates moments (power, mean, standard deviation) and SNR of the signal
1225 1225
1226 1226 Type of dataIn: Spectra
1227 1227
1228 1228 Configuration Parameters:
1229 1229
1230 1230 dirCosx : Cosine director in X axis
1231 1231 dirCosy : Cosine director in Y axis
1232 1232
1233 1233 elevation :
1234 1234 azimuth :
1235 1235
1236 1236 Input:
1237 1237 channelList : simple channel list to select e.g. [2,3,7]
1238 1238 self.dataOut.data_pre : Spectral data
1239 1239 self.dataOut.abscissaList : List of frequencies
1240 1240 self.dataOut.noise : Noise level per channel
1241 1241
1242 1242 Affected:
1243 1243 self.dataOut.moments : Parameters per channel
1244 1244 self.dataOut.data_snr : SNR per channel
1245 1245
1246 1246 '''
1247 1247
1248 1248 def run(self, dataOut):
1249 1249
1250 1250 data = dataOut.data_pre[0]
1251 1251 absc = dataOut.abscissaList[:-1]
1252 1252 noise = dataOut.noise
1253 1253 nChannel = data.shape[0]
1254 1254 data_param = numpy.zeros((nChannel, 4, data.shape[2]))
1255 1255
1256 1256 for ind in range(nChannel):
1257 1257 data_param[ind,:,:] = self.__calculateMoments( data[ind,:,:] , absc , noise[ind] )
1258 1258
1259 1259 dataOut.moments = data_param[:,1:,:]
1260 1260 dataOut.data_snr = data_param[:,0]
1261 1261 dataOut.data_pow = data_param[:,1]
1262 1262 dataOut.data_dop = data_param[:,2]
1263 1263 dataOut.data_width = data_param[:,3]
1264 1264 return dataOut
1265 1265
1266 1266 def __calculateMoments(self, oldspec, oldfreq, n0,
1267 1267 nicoh = None, graph = None, smooth = None, type1 = None, fwindow = None, snrth = None, dc = None, aliasing = None, oldfd = None, wwauto = None):
1268 1268
1269 1269 if (nicoh is None): nicoh = 1
1270 1270 if (graph is None): graph = 0
1271 1271 if (smooth is None): smooth = 0
1272 1272 elif (self.smooth < 3): smooth = 0
1273 1273
1274 1274 if (type1 is None): type1 = 0
1275 1275 if (fwindow is None): fwindow = numpy.zeros(oldfreq.size) + 1
1276 1276 if (snrth is None): snrth = -3
1277 1277 if (dc is None): dc = 0
1278 1278 if (aliasing is None): aliasing = 0
1279 1279 if (oldfd is None): oldfd = 0
1280 1280 if (wwauto is None): wwauto = 0
1281 1281
1282 1282 if (n0 < 1.e-20): n0 = 1.e-20
1283 1283
1284 1284 freq = oldfreq
1285 1285 vec_power = numpy.zeros(oldspec.shape[1])
1286 1286 vec_fd = numpy.zeros(oldspec.shape[1])
1287 1287 vec_w = numpy.zeros(oldspec.shape[1])
1288 1288 vec_snr = numpy.zeros(oldspec.shape[1])
1289 1289
1290 1290 # oldspec = numpy.ma.masked_invalid(oldspec)
1291 1291 for ind in range(oldspec.shape[1]):
1292 1292
1293 1293 spec = oldspec[:,ind]
1294 1294 aux = spec*fwindow
1295 1295 max_spec = aux.max()
1296 1296 m = aux.tolist().index(max_spec)
1297 1297
1298 1298 # Smooth
1299 1299 if (smooth == 0):
1300 1300 spec2 = spec
1301 1301 else:
1302 1302 spec2 = scipy.ndimage.filters.uniform_filter1d(spec,size=smooth)
1303 1303
1304 1304 # Moments Estimation
1305 1305 bb = spec2[numpy.arange(m,spec2.size)]
1306 1306 bb = (bb<n0).nonzero()
1307 1307 bb = bb[0]
1308 1308
1309 1309 ss = spec2[numpy.arange(0,m + 1)]
1310 1310 ss = (ss<n0).nonzero()
1311 1311 ss = ss[0]
1312 1312
1313 1313 if (bb.size == 0):
1314 1314 bb0 = spec.size - 1 - m
1315 1315 else:
1316 1316 bb0 = bb[0] - 1
1317 1317 if (bb0 < 0):
1318 1318 bb0 = 0
1319 1319
1320 1320 if (ss.size == 0):
1321 1321 ss1 = 1
1322 1322 else:
1323 1323 ss1 = max(ss) + 1
1324 1324
1325 1325 if (ss1 > m):
1326 1326 ss1 = m
1327 1327
1328 1328 #valid = numpy.arange(int(m + bb0 - ss1 + 1)) + ss1
1329 1329 valid = numpy.arange(1,oldspec.shape[0])# valid perfil completo igual pulsepair
1330 1330 signal_power = ((spec2[valid] - n0) * fwindow[valid]).mean() # D. ScipiΓ³n added with correct definition
1331 1331 total_power = (spec2[valid] * fwindow[valid]).mean() # D. ScipiΓ³n added with correct definition
1332 1332 power = ((spec2[valid] - n0) * fwindow[valid]).sum()
1333 1333 fd = ((spec2[valid]- n0)*freq[valid] * fwindow[valid]).sum() / power
1334 1334 w = numpy.sqrt(((spec2[valid] - n0)*fwindow[valid]*(freq[valid]- fd)**2).sum() / power)
1335 1335 snr = (spec2.mean()-n0)/n0
1336 1336 if (snr < 1.e-20) :
1337 1337 snr = 1.e-20
1338 1338
1339 1339 # vec_power[ind] = power #D. ScipiΓ³n replaced with the line below
1340 1340 vec_power[ind] = total_power
1341 1341 vec_fd[ind] = fd
1342 1342 vec_w[ind] = w
1343 1343 vec_snr[ind] = snr
1344 1344
1345 1345 return numpy.vstack((vec_snr, vec_power, vec_fd, vec_w))
1346 1346
1347 1347 #------------------ Get SA Parameters --------------------------
1348 1348
1349 1349 def GetSAParameters(self):
1350 1350 #SA en frecuencia
1351 1351 pairslist = self.dataOut.groupList
1352 1352 num_pairs = len(pairslist)
1353 1353
1354 1354 vel = self.dataOut.abscissaList
1355 1355 spectra = self.dataOut.data_pre
1356 1356 cspectra = self.dataIn.data_cspc
1357 1357 delta_v = vel[1] - vel[0]
1358 1358
1359 1359 #Calculating the power spectrum
1360 1360 spc_pow = numpy.sum(spectra, 3)*delta_v
1361 1361 #Normalizing Spectra
1362 1362 norm_spectra = spectra/spc_pow
1363 1363 #Calculating the norm_spectra at peak
1364 1364 max_spectra = numpy.max(norm_spectra, 3)
1365 1365
1366 1366 #Normalizing Cross Spectra
1367 1367 norm_cspectra = numpy.zeros(cspectra.shape)
1368 1368
1369 1369 for i in range(num_chan):
1370 1370 norm_cspectra[i,:,:] = cspectra[i,:,:]/numpy.sqrt(spc_pow[pairslist[i][0],:]*spc_pow[pairslist[i][1],:])
1371 1371
1372 1372 max_cspectra = numpy.max(norm_cspectra,2)
1373 1373 max_cspectra_index = numpy.argmax(norm_cspectra, 2)
1374 1374
1375 1375 for i in range(num_pairs):
1376 1376 cspc_par[i,:,:] = __calculateMoments(norm_cspectra)
1377 1377 #------------------- Get Lags ----------------------------------
1378 1378
1379 1379 class SALags(Operation):
1380 1380 '''
1381 1381 Function GetMoments()
1382 1382
1383 1383 Input:
1384 1384 self.dataOut.data_pre
1385 1385 self.dataOut.abscissaList
1386 1386 self.dataOut.noise
1387 1387 self.dataOut.normFactor
1388 1388 self.dataOut.data_snr
1389 1389 self.dataOut.groupList
1390 1390 self.dataOut.nChannels
1391 1391
1392 1392 Affected:
1393 1393 self.dataOut.data_param
1394 1394
1395 1395 '''
1396 1396 def run(self, dataOut):
1397 1397 data_acf = dataOut.data_pre[0]
1398 1398 data_ccf = dataOut.data_pre[1]
1399 1399 normFactor_acf = dataOut.normFactor[0]
1400 1400 normFactor_ccf = dataOut.normFactor[1]
1401 1401 pairs_acf = dataOut.groupList[0]
1402 1402 pairs_ccf = dataOut.groupList[1]
1403 1403
1404 1404 nHeights = dataOut.nHeights
1405 1405 absc = dataOut.abscissaList
1406 1406 noise = dataOut.noise
1407 1407 SNR = dataOut.data_snr
1408 1408 nChannels = dataOut.nChannels
1409 1409 # pairsList = dataOut.groupList
1410 1410 # pairsAutoCorr, pairsCrossCorr = self.__getPairsAutoCorr(pairsList, nChannels)
1411 1411
1412 1412 for l in range(len(pairs_acf)):
1413 1413 data_acf[l,:,:] = data_acf[l,:,:]/normFactor_acf[l,:]
1414 1414
1415 1415 for l in range(len(pairs_ccf)):
1416 1416 data_ccf[l,:,:] = data_ccf[l,:,:]/normFactor_ccf[l,:]
1417 1417
1418 1418 dataOut.data_param = numpy.zeros((len(pairs_ccf)*2 + 1, nHeights))
1419 1419 dataOut.data_param[:-1,:] = self.__calculateTaus(data_acf, data_ccf, absc)
1420 1420 dataOut.data_param[-1,:] = self.__calculateLag1Phase(data_acf, absc)
1421 1421 return
1422 1422
1423 1423 # def __getPairsAutoCorr(self, pairsList, nChannels):
1424 1424 #
1425 1425 # pairsAutoCorr = numpy.zeros(nChannels, dtype = 'int')*numpy.nan
1426 1426 #
1427 1427 # for l in range(len(pairsList)):
1428 1428 # firstChannel = pairsList[l][0]
1429 1429 # secondChannel = pairsList[l][1]
1430 1430 #
1431 1431 # #Obteniendo pares de Autocorrelacion
1432 1432 # if firstChannel == secondChannel:
1433 1433 # pairsAutoCorr[firstChannel] = int(l)
1434 1434 #
1435 1435 # pairsAutoCorr = pairsAutoCorr.astype(int)
1436 1436 #
1437 1437 # pairsCrossCorr = range(len(pairsList))
1438 1438 # pairsCrossCorr = numpy.delete(pairsCrossCorr,pairsAutoCorr)
1439 1439 #
1440 1440 # return pairsAutoCorr, pairsCrossCorr
1441 1441
1442 1442 def __calculateTaus(self, data_acf, data_ccf, lagRange):
1443 1443
1444 1444 lag0 = data_acf.shape[1]/2
1445 1445 #Funcion de Autocorrelacion
1446 1446 mean_acf = stats.nanmean(data_acf, axis = 0)
1447 1447
1448 1448 #Obtencion Indice de TauCross
1449 1449 ind_ccf = data_ccf.argmax(axis = 1)
1450 1450 #Obtencion Indice de TauAuto
1451 1451 ind_acf = numpy.zeros(ind_ccf.shape,dtype = 'int')
1452 1452 ccf_lag0 = data_ccf[:,lag0,:]
1453 1453
1454 1454 for i in range(ccf_lag0.shape[0]):
1455 1455 ind_acf[i,:] = numpy.abs(mean_acf - ccf_lag0[i,:]).argmin(axis = 0)
1456 1456
1457 1457 #Obtencion de TauCross y TauAuto
1458 1458 tau_ccf = lagRange[ind_ccf]
1459 1459 tau_acf = lagRange[ind_acf]
1460 1460
1461 1461 Nan1, Nan2 = numpy.where(tau_ccf == lagRange[0])
1462 1462
1463 1463 tau_ccf[Nan1,Nan2] = numpy.nan
1464 1464 tau_acf[Nan1,Nan2] = numpy.nan
1465 1465 tau = numpy.vstack((tau_ccf,tau_acf))
1466 1466
1467 1467 return tau
1468 1468
1469 1469 def __calculateLag1Phase(self, data, lagTRange):
1470 1470 data1 = stats.nanmean(data, axis = 0)
1471 1471 lag1 = numpy.where(lagTRange == 0)[0][0] + 1
1472 1472
1473 1473 phase = numpy.angle(data1[lag1,:])
1474 1474
1475 1475 return phase
1476 1476
1477 1477 class SpectralFitting(Operation):
1478 1478 '''
1479 1479 Function GetMoments()
1480 1480
1481 1481 Input:
1482 1482 Output:
1483 1483 Variables modified:
1484 1484 '''
1485 1485
1486 1486 def run(self, dataOut, getSNR = True, path=None, file=None, groupList=None):
1487 1487
1488 1488
1489 1489 if path != None:
1490 1490 sys.path.append(path)
1491 1491 self.dataOut.library = importlib.import_module(file)
1492 1492
1493 1493 #To be inserted as a parameter
1494 1494 groupArray = numpy.array(groupList)
1495 1495 # groupArray = numpy.array([[0,1],[2,3]])
1496 1496 self.dataOut.groupList = groupArray
1497 1497
1498 1498 nGroups = groupArray.shape[0]
1499 1499 nChannels = self.dataIn.nChannels
1500 1500 nHeights=self.dataIn.heightList.size
1501 1501
1502 1502 #Parameters Array
1503 1503 self.dataOut.data_param = None
1504 1504
1505 1505 #Set constants
1506 1506 constants = self.dataOut.library.setConstants(self.dataIn)
1507 1507 self.dataOut.constants = constants
1508 1508 M = self.dataIn.normFactor
1509 1509 N = self.dataIn.nFFTPoints
1510 1510 ippSeconds = self.dataIn.ippSeconds
1511 1511 K = self.dataIn.nIncohInt
1512 1512 pairsArray = numpy.array(self.dataIn.pairsList)
1513 1513
1514 1514 #List of possible combinations
1515 1515 listComb = itertools.combinations(numpy.arange(groupArray.shape[1]),2)
1516 1516 indCross = numpy.zeros(len(list(listComb)), dtype = 'int')
1517 1517
1518 1518 if getSNR:
1519 1519 listChannels = groupArray.reshape((groupArray.size))
1520 1520 listChannels.sort()
1521 1521 noise = self.dataIn.getNoise()
1522 1522 self.dataOut.data_snr = self.__getSNR(self.dataIn.data_spc[listChannels,:,:], noise[listChannels])
1523 1523
1524 1524 for i in range(nGroups):
1525 1525 coord = groupArray[i,:]
1526 1526
1527 1527 #Input data array
1528 1528 data = self.dataIn.data_spc[coord,:,:]/(M*N)
1529 1529 data = data.reshape((data.shape[0]*data.shape[1],data.shape[2]))
1530 1530
1531 1531 #Cross Spectra data array for Covariance Matrixes
1532 1532 ind = 0
1533 1533 for pairs in listComb:
1534 1534 pairsSel = numpy.array([coord[x],coord[y]])
1535 1535 indCross[ind] = int(numpy.where(numpy.all(pairsArray == pairsSel, axis = 1))[0][0])
1536 1536 ind += 1
1537 1537 dataCross = self.dataIn.data_cspc[indCross,:,:]/(M*N)
1538 1538 dataCross = dataCross**2/K
1539 1539
1540 1540 for h in range(nHeights):
1541 1541
1542 1542 #Input
1543 1543 d = data[:,h]
1544 1544
1545 1545 #Covariance Matrix
1546 1546 D = numpy.diag(d**2/K)
1547 1547 ind = 0
1548 1548 for pairs in listComb:
1549 1549 #Coordinates in Covariance Matrix
1550 1550 x = pairs[0]
1551 1551 y = pairs[1]
1552 1552 #Channel Index
1553 1553 S12 = dataCross[ind,:,h]
1554 1554 D12 = numpy.diag(S12)
1555 1555 #Completing Covariance Matrix with Cross Spectras
1556 1556 D[x*N:(x+1)*N,y*N:(y+1)*N] = D12
1557 1557 D[y*N:(y+1)*N,x*N:(x+1)*N] = D12
1558 1558 ind += 1
1559 1559 Dinv=numpy.linalg.inv(D)
1560 1560 L=numpy.linalg.cholesky(Dinv)
1561 1561 LT=L.T
1562 1562
1563 1563 dp = numpy.dot(LT,d)
1564 1564
1565 1565 #Initial values
1566 1566 data_spc = self.dataIn.data_spc[coord,:,h]
1567 1567
1568 1568 if (h>0)and(error1[3]<5):
1569 1569 p0 = self.dataOut.data_param[i,:,h-1]
1570 1570 else:
1571 1571 p0 = numpy.array(self.dataOut.library.initialValuesFunction(data_spc, constants, i))
1572 1572
1573 1573 try:
1574 1574 #Least Squares
1575 1575 minp,covp,infodict,mesg,ier = optimize.leastsq(self.__residFunction,p0,args=(dp,LT,constants),full_output=True)
1576 1576 # minp,covp = optimize.leastsq(self.__residFunction,p0,args=(dp,LT,constants))
1577 1577 #Chi square error
1578 1578 error0 = numpy.sum(infodict['fvec']**2)/(2*N)
1579 1579 #Error with Jacobian
1580 1580 error1 = self.dataOut.library.errorFunction(minp,constants,LT)
1581 1581 except:
1582 1582 minp = p0*numpy.nan
1583 1583 error0 = numpy.nan
1584 1584 error1 = p0*numpy.nan
1585 1585
1586 1586 #Save
1587 1587 if self.dataOut.data_param is None:
1588 1588 self.dataOut.data_param = numpy.zeros((nGroups, p0.size, nHeights))*numpy.nan
1589 1589 self.dataOut.data_error = numpy.zeros((nGroups, p0.size + 1, nHeights))*numpy.nan
1590 1590
1591 1591 self.dataOut.data_error[i,:,h] = numpy.hstack((error0,error1))
1592 1592 self.dataOut.data_param[i,:,h] = minp
1593 1593 return
1594 1594
1595 1595 def __residFunction(self, p, dp, LT, constants):
1596 1596
1597 1597 fm = self.dataOut.library.modelFunction(p, constants)
1598 1598 fmp=numpy.dot(LT,fm)
1599 1599
1600 1600 return dp-fmp
1601 1601
1602 1602 def __getSNR(self, z, noise):
1603 1603
1604 1604 avg = numpy.average(z, axis=1)
1605 1605 SNR = (avg.T-noise)/noise
1606 1606 SNR = SNR.T
1607 1607 return SNR
1608 1608
1609 1609 def __chisq(p,chindex,hindex):
1610 1610 #similar to Resid but calculates CHI**2
1611 1611 [LT,d,fm]=setupLTdfm(p,chindex,hindex)
1612 1612 dp=numpy.dot(LT,d)
1613 1613 fmp=numpy.dot(LT,fm)
1614 1614 chisq=numpy.dot((dp-fmp).T,(dp-fmp))
1615 1615 return chisq
1616 1616
1617 1617 class WindProfiler(Operation):
1618 1618
1619 1619 __isConfig = False
1620 1620
1621 1621 __initime = None
1622 1622 __lastdatatime = None
1623 1623 __integrationtime = None
1624 1624
1625 1625 __buffer = None
1626 1626
1627 1627 __dataReady = False
1628 1628
1629 1629 __firstdata = None
1630 1630
1631 1631 n = None
1632 1632
1633 1633 def __init__(self):
1634 1634 Operation.__init__(self)
1635 1635
1636 1636 def __calculateCosDir(self, elev, azim):
1637 1637 zen = (90 - elev)*numpy.pi/180
1638 1638 azim = azim*numpy.pi/180
1639 1639 cosDirX = numpy.sqrt((1-numpy.cos(zen)**2)/((1+numpy.tan(azim)**2)))
1640 1640 cosDirY = numpy.sqrt(1-numpy.cos(zen)**2-cosDirX**2)
1641 1641
1642 1642 signX = numpy.sign(numpy.cos(azim))
1643 1643 signY = numpy.sign(numpy.sin(azim))
1644 1644
1645 1645 cosDirX = numpy.copysign(cosDirX, signX)
1646 1646 cosDirY = numpy.copysign(cosDirY, signY)
1647 1647 return cosDirX, cosDirY
1648 1648
1649 1649 def __calculateAngles(self, theta_x, theta_y, azimuth):
1650 1650
1651 1651 dir_cosw = numpy.sqrt(1-theta_x**2-theta_y**2)
1652 1652 zenith_arr = numpy.arccos(dir_cosw)
1653 1653 azimuth_arr = numpy.arctan2(theta_x,theta_y) + azimuth*math.pi/180
1654 1654
1655 1655 dir_cosu = numpy.sin(azimuth_arr)*numpy.sin(zenith_arr)
1656 1656 dir_cosv = numpy.cos(azimuth_arr)*numpy.sin(zenith_arr)
1657 1657
1658 1658 return azimuth_arr, zenith_arr, dir_cosu, dir_cosv, dir_cosw
1659 1659
1660 1660 def __calculateMatA(self, dir_cosu, dir_cosv, dir_cosw, horOnly):
1661 1661
1662 1662 #
1663 1663 if horOnly:
1664 1664 A = numpy.c_[dir_cosu,dir_cosv]
1665 1665 else:
1666 1666 A = numpy.c_[dir_cosu,dir_cosv,dir_cosw]
1667 1667 A = numpy.asmatrix(A)
1668 1668 A1 = numpy.linalg.inv(A.transpose()*A)*A.transpose()
1669 1669
1670 1670 return A1
1671 1671
1672 1672 def __correctValues(self, heiRang, phi, velRadial, SNR):
1673 1673 listPhi = phi.tolist()
1674 1674 maxid = listPhi.index(max(listPhi))
1675 1675 minid = listPhi.index(min(listPhi))
1676 1676
1677 1677 rango = list(range(len(phi)))
1678 1678 # rango = numpy.delete(rango,maxid)
1679 1679
1680 1680 heiRang1 = heiRang*math.cos(phi[maxid])
1681 1681 heiRangAux = heiRang*math.cos(phi[minid])
1682 1682 indOut = (heiRang1 < heiRangAux[0]).nonzero()
1683 1683 heiRang1 = numpy.delete(heiRang1,indOut)
1684 1684
1685 1685 velRadial1 = numpy.zeros([len(phi),len(heiRang1)])
1686 1686 SNR1 = numpy.zeros([len(phi),len(heiRang1)])
1687 1687
1688 1688 for i in rango:
1689 1689 x = heiRang*math.cos(phi[i])
1690 1690 y1 = velRadial[i,:]
1691 1691 f1 = interpolate.interp1d(x,y1,kind = 'cubic')
1692 1692
1693 1693 x1 = heiRang1
1694 1694 y11 = f1(x1)
1695 1695
1696 1696 y2 = SNR[i,:]
1697 1697 f2 = interpolate.interp1d(x,y2,kind = 'cubic')
1698 1698 y21 = f2(x1)
1699 1699
1700 1700 velRadial1[i,:] = y11
1701 1701 SNR1[i,:] = y21
1702 1702
1703 1703 return heiRang1, velRadial1, SNR1
1704 1704
1705 1705 def __calculateVelUVW(self, A, velRadial):
1706 1706
1707 1707 #Operacion Matricial
1708 1708 # velUVW = numpy.zeros((velRadial.shape[1],3))
1709 1709 # for ind in range(velRadial.shape[1]):
1710 1710 # velUVW[ind,:] = numpy.dot(A,velRadial[:,ind])
1711 1711 # velUVW = velUVW.transpose()
1712 1712 velUVW = numpy.zeros((A.shape[0],velRadial.shape[1]))
1713 1713 velUVW[:,:] = numpy.dot(A,velRadial)
1714 1714
1715 1715
1716 1716 return velUVW
1717 1717
1718 1718 # def techniqueDBS(self, velRadial0, dirCosx, disrCosy, azimuth, correct, horizontalOnly, heiRang, SNR0):
1719 1719
1720 1720 def techniqueDBS(self, kwargs):
1721 1721 """
1722 1722 Function that implements Doppler Beam Swinging (DBS) technique.
1723 1723
1724 1724 Input: Radial velocities, Direction cosines (x and y) of the Beam, Antenna azimuth,
1725 1725 Direction correction (if necessary), Ranges and SNR
1726 1726
1727 1727 Output: Winds estimation (Zonal, Meridional and Vertical)
1728 1728
1729 1729 Parameters affected: Winds, height range, SNR
1730 1730 """
1731 1731 velRadial0 = kwargs['velRadial']
1732 1732 heiRang = kwargs['heightList']
1733 1733 SNR0 = kwargs['SNR']
1734 1734
1735 1735 if 'dirCosx' in kwargs and 'dirCosy' in kwargs:
1736 1736 theta_x = numpy.array(kwargs['dirCosx'])
1737 1737 theta_y = numpy.array(kwargs['dirCosy'])
1738 1738 else:
1739 1739 elev = numpy.array(kwargs['elevation'])
1740 1740 azim = numpy.array(kwargs['azimuth'])
1741 1741 theta_x, theta_y = self.__calculateCosDir(elev, azim)
1742 1742 azimuth = kwargs['correctAzimuth']
1743 1743 if 'horizontalOnly' in kwargs:
1744 1744 horizontalOnly = kwargs['horizontalOnly']
1745 1745 else: horizontalOnly = False
1746 1746 if 'correctFactor' in kwargs:
1747 1747 correctFactor = kwargs['correctFactor']
1748 1748 else: correctFactor = 1
1749 1749 if 'channelList' in kwargs:
1750 1750 channelList = kwargs['channelList']
1751 1751 if len(channelList) == 2:
1752 1752 horizontalOnly = True
1753 1753 arrayChannel = numpy.array(channelList)
1754 1754 param = param[arrayChannel,:,:]
1755 1755 theta_x = theta_x[arrayChannel]
1756 1756 theta_y = theta_y[arrayChannel]
1757 1757
1758 1758 azimuth_arr, zenith_arr, dir_cosu, dir_cosv, dir_cosw = self.__calculateAngles(theta_x, theta_y, azimuth)
1759 1759 heiRang1, velRadial1, SNR1 = self.__correctValues(heiRang, zenith_arr, correctFactor*velRadial0, SNR0)
1760 1760 A = self.__calculateMatA(dir_cosu, dir_cosv, dir_cosw, horizontalOnly)
1761 1761
1762 1762 #Calculo de Componentes de la velocidad con DBS
1763 1763 winds = self.__calculateVelUVW(A,velRadial1)
1764 1764
1765 1765 return winds, heiRang1, SNR1
1766 1766
1767 1767 def __calculateDistance(self, posx, posy, pairs_ccf, azimuth = None):
1768 1768
1769 1769 nPairs = len(pairs_ccf)
1770 1770 posx = numpy.asarray(posx)
1771 1771 posy = numpy.asarray(posy)
1772 1772
1773 1773 #Rotacion Inversa para alinear con el azimuth
1774 1774 if azimuth!= None:
1775 1775 azimuth = azimuth*math.pi/180
1776 1776 posx1 = posx*math.cos(azimuth) + posy*math.sin(azimuth)
1777 1777 posy1 = -posx*math.sin(azimuth) + posy*math.cos(azimuth)
1778 1778 else:
1779 1779 posx1 = posx
1780 1780 posy1 = posy
1781 1781
1782 1782 #Calculo de Distancias
1783 1783 distx = numpy.zeros(nPairs)
1784 1784 disty = numpy.zeros(nPairs)
1785 1785 dist = numpy.zeros(nPairs)
1786 1786 ang = numpy.zeros(nPairs)
1787 1787
1788 1788 for i in range(nPairs):
1789 1789 distx[i] = posx1[pairs_ccf[i][1]] - posx1[pairs_ccf[i][0]]
1790 1790 disty[i] = posy1[pairs_ccf[i][1]] - posy1[pairs_ccf[i][0]]
1791 1791 dist[i] = numpy.sqrt(distx[i]**2 + disty[i]**2)
1792 1792 ang[i] = numpy.arctan2(disty[i],distx[i])
1793 1793
1794 1794 return distx, disty, dist, ang
1795 1795 #Calculo de Matrices
1796 1796 # nPairs = len(pairs)
1797 1797 # ang1 = numpy.zeros((nPairs, 2, 1))
1798 1798 # dist1 = numpy.zeros((nPairs, 2, 1))
1799 1799 #
1800 1800 # for j in range(nPairs):
1801 1801 # dist1[j,0,0] = dist[pairs[j][0]]
1802 1802 # dist1[j,1,0] = dist[pairs[j][1]]
1803 1803 # ang1[j,0,0] = ang[pairs[j][0]]
1804 1804 # ang1[j,1,0] = ang[pairs[j][1]]
1805 1805 #
1806 1806 # return distx,disty, dist1,ang1
1807 1807
1808 1808
1809 1809 def __calculateVelVer(self, phase, lagTRange, _lambda):
1810 1810
1811 1811 Ts = lagTRange[1] - lagTRange[0]
1812 1812 velW = -_lambda*phase/(4*math.pi*Ts)
1813 1813
1814 1814 return velW
1815 1815
1816 1816 def __calculateVelHorDir(self, dist, tau1, tau2, ang):
1817 1817 nPairs = tau1.shape[0]
1818 1818 nHeights = tau1.shape[1]
1819 1819 vel = numpy.zeros((nPairs,3,nHeights))
1820 1820 dist1 = numpy.reshape(dist, (dist.size,1))
1821 1821
1822 1822 angCos = numpy.cos(ang)
1823 1823 angSin = numpy.sin(ang)
1824 1824
1825 1825 vel0 = dist1*tau1/(2*tau2**2)
1826 1826 vel[:,0,:] = (vel0*angCos).sum(axis = 1)
1827 1827 vel[:,1,:] = (vel0*angSin).sum(axis = 1)
1828 1828
1829 1829 ind = numpy.where(numpy.isinf(vel))
1830 1830 vel[ind] = numpy.nan
1831 1831
1832 1832 return vel
1833 1833
1834 1834 # def __getPairsAutoCorr(self, pairsList, nChannels):
1835 1835 #
1836 1836 # pairsAutoCorr = numpy.zeros(nChannels, dtype = 'int')*numpy.nan
1837 1837 #
1838 1838 # for l in range(len(pairsList)):
1839 1839 # firstChannel = pairsList[l][0]
1840 1840 # secondChannel = pairsList[l][1]
1841 1841 #
1842 1842 # #Obteniendo pares de Autocorrelacion
1843 1843 # if firstChannel == secondChannel:
1844 1844 # pairsAutoCorr[firstChannel] = int(l)
1845 1845 #
1846 1846 # pairsAutoCorr = pairsAutoCorr.astype(int)
1847 1847 #
1848 1848 # pairsCrossCorr = range(len(pairsList))
1849 1849 # pairsCrossCorr = numpy.delete(pairsCrossCorr,pairsAutoCorr)
1850 1850 #
1851 1851 # return pairsAutoCorr, pairsCrossCorr
1852 1852
1853 1853 # def techniqueSA(self, pairsSelected, pairsList, nChannels, tau, azimuth, _lambda, position_x, position_y, lagTRange, correctFactor):
1854 1854 def techniqueSA(self, kwargs):
1855 1855
1856 1856 """
1857 1857 Function that implements Spaced Antenna (SA) technique.
1858 1858
1859 1859 Input: Radial velocities, Direction cosines (x and y) of the Beam, Antenna azimuth,
1860 1860 Direction correction (if necessary), Ranges and SNR
1861 1861
1862 1862 Output: Winds estimation (Zonal, Meridional and Vertical)
1863 1863
1864 1864 Parameters affected: Winds
1865 1865 """
1866 1866 position_x = kwargs['positionX']
1867 1867 position_y = kwargs['positionY']
1868 1868 azimuth = kwargs['azimuth']
1869 1869
1870 1870 if 'correctFactor' in kwargs:
1871 1871 correctFactor = kwargs['correctFactor']
1872 1872 else:
1873 1873 correctFactor = 1
1874 1874
1875 1875 groupList = kwargs['groupList']
1876 1876 pairs_ccf = groupList[1]
1877 1877 tau = kwargs['tau']
1878 1878 _lambda = kwargs['_lambda']
1879 1879
1880 1880 #Cross Correlation pairs obtained
1881 1881 # pairsAutoCorr, pairsCrossCorr = self.__getPairsAutoCorr(pairssList, nChannels)
1882 1882 # pairsArray = numpy.array(pairsList)[pairsCrossCorr]
1883 1883 # pairsSelArray = numpy.array(pairsSelected)
1884 1884 # pairs = []
1885 1885 #
1886 1886 # #Wind estimation pairs obtained
1887 1887 # for i in range(pairsSelArray.shape[0]/2):
1888 1888 # ind1 = numpy.where(numpy.all(pairsArray == pairsSelArray[2*i], axis = 1))[0][0]
1889 1889 # ind2 = numpy.where(numpy.all(pairsArray == pairsSelArray[2*i + 1], axis = 1))[0][0]
1890 1890 # pairs.append((ind1,ind2))
1891 1891
1892 1892 indtau = tau.shape[0]/2
1893 1893 tau1 = tau[:indtau,:]
1894 1894 tau2 = tau[indtau:-1,:]
1895 1895 # tau1 = tau1[pairs,:]
1896 1896 # tau2 = tau2[pairs,:]
1897 1897 phase1 = tau[-1,:]
1898 1898
1899 1899 #---------------------------------------------------------------------
1900 1900 #Metodo Directo
1901 1901 distx, disty, dist, ang = self.__calculateDistance(position_x, position_y, pairs_ccf,azimuth)
1902 1902 winds = self.__calculateVelHorDir(dist, tau1, tau2, ang)
1903 1903 winds = stats.nanmean(winds, axis=0)
1904 1904 #---------------------------------------------------------------------
1905 1905 #Metodo General
1906 1906 # distx, disty, dist = self.calculateDistance(position_x,position_y,pairsCrossCorr, pairsList, azimuth)
1907 1907 # #Calculo Coeficientes de Funcion de Correlacion
1908 1908 # F,G,A,B,H = self.calculateCoef(tau1,tau2,distx,disty,n)
1909 1909 # #Calculo de Velocidades
1910 1910 # winds = self.calculateVelUV(F,G,A,B,H)
1911 1911
1912 1912 #---------------------------------------------------------------------
1913 1913 winds[2,:] = self.__calculateVelVer(phase1, lagTRange, _lambda)
1914 1914 winds = correctFactor*winds
1915 1915 return winds
1916 1916
1917 1917 def __checkTime(self, currentTime, paramInterval, outputInterval):
1918 1918
1919 1919 dataTime = currentTime + paramInterval
1920 1920 deltaTime = dataTime - self.__initime
1921 1921
1922 1922 if deltaTime >= outputInterval or deltaTime < 0:
1923 1923 self.__dataReady = True
1924 1924 return
1925 1925
1926 1926 def techniqueMeteors(self, arrayMeteor, meteorThresh, heightMin, heightMax):
1927 1927 '''
1928 1928 Function that implements winds estimation technique with detected meteors.
1929 1929
1930 1930 Input: Detected meteors, Minimum meteor quantity to wind estimation
1931 1931
1932 1932 Output: Winds estimation (Zonal and Meridional)
1933 1933
1934 1934 Parameters affected: Winds
1935 1935 '''
1936 1936 #Settings
1937 1937 nInt = (heightMax - heightMin)/2
1938 1938 nInt = int(nInt)
1939 1939 winds = numpy.zeros((2,nInt))*numpy.nan
1940 1940
1941 1941 #Filter errors
1942 1942 error = numpy.where(arrayMeteor[:,-1] == 0)[0]
1943 1943 finalMeteor = arrayMeteor[error,:]
1944 1944
1945 1945 #Meteor Histogram
1946 1946 finalHeights = finalMeteor[:,2]
1947 1947 hist = numpy.histogram(finalHeights, bins = nInt, range = (heightMin,heightMax))
1948 1948 nMeteorsPerI = hist[0]
1949 1949 heightPerI = hist[1]
1950 1950
1951 1951 #Sort of meteors
1952 1952 indSort = finalHeights.argsort()
1953 1953 finalMeteor2 = finalMeteor[indSort,:]
1954 1954
1955 1955 # Calculating winds
1956 1956 ind1 = 0
1957 1957 ind2 = 0
1958 1958
1959 1959 for i in range(nInt):
1960 1960 nMet = nMeteorsPerI[i]
1961 1961 ind1 = ind2
1962 1962 ind2 = ind1 + nMet
1963 1963
1964 1964 meteorAux = finalMeteor2[ind1:ind2,:]
1965 1965
1966 1966 if meteorAux.shape[0] >= meteorThresh:
1967 1967 vel = meteorAux[:, 6]
1968 1968 zen = meteorAux[:, 4]*numpy.pi/180
1969 1969 azim = meteorAux[:, 3]*numpy.pi/180
1970 1970
1971 1971 n = numpy.cos(zen)
1972 1972 # m = (1 - n**2)/(1 - numpy.tan(azim)**2)
1973 1973 # l = m*numpy.tan(azim)
1974 1974 l = numpy.sin(zen)*numpy.sin(azim)
1975 1975 m = numpy.sin(zen)*numpy.cos(azim)
1976 1976
1977 1977 A = numpy.vstack((l, m)).transpose()
1978 1978 A1 = numpy.dot(numpy.linalg.inv( numpy.dot(A.transpose(),A) ),A.transpose())
1979 1979 windsAux = numpy.dot(A1, vel)
1980 1980
1981 1981 winds[0,i] = windsAux[0]
1982 1982 winds[1,i] = windsAux[1]
1983 1983
1984 1984 return winds, heightPerI[:-1]
1985 1985
1986 1986 def techniqueNSM_SA(self, **kwargs):
1987 1987 metArray = kwargs['metArray']
1988 1988 heightList = kwargs['heightList']
1989 1989 timeList = kwargs['timeList']
1990 1990
1991 1991 rx_location = kwargs['rx_location']
1992 1992 groupList = kwargs['groupList']
1993 1993 azimuth = kwargs['azimuth']
1994 1994 dfactor = kwargs['dfactor']
1995 1995 k = kwargs['k']
1996 1996
1997 1997 azimuth1, dist = self.__calculateAzimuth1(rx_location, groupList, azimuth)
1998 1998 d = dist*dfactor
1999 1999 #Phase calculation
2000 2000 metArray1 = self.__getPhaseSlope(metArray, heightList, timeList)
2001 2001
2002 2002 metArray1[:,-2] = metArray1[:,-2]*metArray1[:,2]*1000/(k*d[metArray1[:,1].astype(int)]) #angles into velocities
2003 2003
2004 2004 velEst = numpy.zeros((heightList.size,2))*numpy.nan
2005 2005 azimuth1 = azimuth1*numpy.pi/180
2006 2006
2007 2007 for i in range(heightList.size):
2008 2008 h = heightList[i]
2009 2009 indH = numpy.where((metArray1[:,2] == h)&(numpy.abs(metArray1[:,-2]) < 100))[0]
2010 2010 metHeight = metArray1[indH,:]
2011 2011 if metHeight.shape[0] >= 2:
2012 2012 velAux = numpy.asmatrix(metHeight[:,-2]).T #Radial Velocities
2013 2013 iazim = metHeight[:,1].astype(int)
2014 2014 azimAux = numpy.asmatrix(azimuth1[iazim]).T #Azimuths
2015 2015 A = numpy.hstack((numpy.cos(azimAux),numpy.sin(azimAux)))
2016 2016 A = numpy.asmatrix(A)
2017 2017 A1 = numpy.linalg.pinv(A.transpose()*A)*A.transpose()
2018 2018 velHor = numpy.dot(A1,velAux)
2019 2019
2020 2020 velEst[i,:] = numpy.squeeze(velHor)
2021 2021 return velEst
2022 2022
2023 2023 def __getPhaseSlope(self, metArray, heightList, timeList):
2024 2024 meteorList = []
2025 2025 #utctime sec1 height SNR velRad ph0 ph1 ph2 coh0 coh1 coh2
2026 2026 #Putting back together the meteor matrix
2027 2027 utctime = metArray[:,0]
2028 2028 uniqueTime = numpy.unique(utctime)
2029 2029
2030 2030 phaseDerThresh = 0.5
2031 2031 ippSeconds = timeList[1] - timeList[0]
2032 2032 sec = numpy.where(timeList>1)[0][0]
2033 2033 nPairs = metArray.shape[1] - 6
2034 2034 nHeights = len(heightList)
2035 2035
2036 2036 for t in uniqueTime:
2037 2037 metArray1 = metArray[utctime==t,:]
2038 2038 # phaseDerThresh = numpy.pi/4 #reducir Phase thresh
2039 2039 tmet = metArray1[:,1].astype(int)
2040 2040 hmet = metArray1[:,2].astype(int)
2041 2041
2042 2042 metPhase = numpy.zeros((nPairs, heightList.size, timeList.size - 1))
2043 2043 metPhase[:,:] = numpy.nan
2044 2044 metPhase[:,hmet,tmet] = metArray1[:,6:].T
2045 2045
2046 2046 #Delete short trails
2047 2047 metBool = ~numpy.isnan(metPhase[0,:,:])
2048 2048 heightVect = numpy.sum(metBool, axis = 1)
2049 2049 metBool[heightVect<sec,:] = False
2050 2050 metPhase[:,heightVect<sec,:] = numpy.nan
2051 2051
2052 2052 #Derivative
2053 2053 metDer = numpy.abs(metPhase[:,:,1:] - metPhase[:,:,:-1])
2054 2054 phDerAux = numpy.dstack((numpy.full((nPairs,nHeights,1), False, dtype=bool),metDer > phaseDerThresh))
2055 2055 metPhase[phDerAux] = numpy.nan
2056 2056
2057 2057 #--------------------------METEOR DETECTION -----------------------------------------
2058 2058 indMet = numpy.where(numpy.any(metBool,axis=1))[0]
2059 2059
2060 2060 for p in numpy.arange(nPairs):
2061 2061 phase = metPhase[p,:,:]
2062 2062 phDer = metDer[p,:,:]
2063 2063
2064 2064 for h in indMet:
2065 2065 height = heightList[h]
2066 2066 phase1 = phase[h,:] #82
2067 2067 phDer1 = phDer[h,:]
2068 2068
2069 2069 phase1[~numpy.isnan(phase1)] = numpy.unwrap(phase1[~numpy.isnan(phase1)]) #Unwrap
2070 2070
2071 2071 indValid = numpy.where(~numpy.isnan(phase1))[0]
2072 2072 initMet = indValid[0]
2073 2073 endMet = 0
2074 2074
2075 2075 for i in range(len(indValid)-1):
2076 2076
2077 2077 #Time difference
2078 2078 inow = indValid[i]
2079 2079 inext = indValid[i+1]
2080 2080 idiff = inext - inow
2081 2081 #Phase difference
2082 2082 phDiff = numpy.abs(phase1[inext] - phase1[inow])
2083 2083
2084 2084 if idiff>sec or phDiff>numpy.pi/4 or inext==indValid[-1]: #End of Meteor
2085 2085 sizeTrail = inow - initMet + 1
2086 2086 if sizeTrail>3*sec: #Too short meteors
2087 2087 x = numpy.arange(initMet,inow+1)*ippSeconds
2088 2088 y = phase1[initMet:inow+1]
2089 2089 ynnan = ~numpy.isnan(y)
2090 2090 x = x[ynnan]
2091 2091 y = y[ynnan]
2092 2092 slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
2093 2093 ylin = x*slope + intercept
2094 2094 rsq = r_value**2
2095 2095 if rsq > 0.5:
2096 2096 vel = slope#*height*1000/(k*d)
2097 2097 estAux = numpy.array([utctime,p,height, vel, rsq])
2098 2098 meteorList.append(estAux)
2099 2099 initMet = inext
2100 2100 metArray2 = numpy.array(meteorList)
2101 2101
2102 2102 return metArray2
2103 2103
2104 2104 def __calculateAzimuth1(self, rx_location, pairslist, azimuth0):
2105 2105
2106 2106 azimuth1 = numpy.zeros(len(pairslist))
2107 2107 dist = numpy.zeros(len(pairslist))
2108 2108
2109 2109 for i in range(len(rx_location)):
2110 2110 ch0 = pairslist[i][0]
2111 2111 ch1 = pairslist[i][1]
2112 2112
2113 2113 diffX = rx_location[ch0][0] - rx_location[ch1][0]
2114 2114 diffY = rx_location[ch0][1] - rx_location[ch1][1]
2115 2115 azimuth1[i] = numpy.arctan2(diffY,diffX)*180/numpy.pi
2116 2116 dist[i] = numpy.sqrt(diffX**2 + diffY**2)
2117 2117
2118 2118 azimuth1 -= azimuth0
2119 2119 return azimuth1, dist
2120 2120
2121 2121 def techniqueNSM_DBS(self, **kwargs):
2122 2122 metArray = kwargs['metArray']
2123 2123 heightList = kwargs['heightList']
2124 2124 timeList = kwargs['timeList']
2125 2125 azimuth = kwargs['azimuth']
2126 2126 theta_x = numpy.array(kwargs['theta_x'])
2127 2127 theta_y = numpy.array(kwargs['theta_y'])
2128 2128
2129 2129 utctime = metArray[:,0]
2130 2130 cmet = metArray[:,1].astype(int)
2131 2131 hmet = metArray[:,3].astype(int)
2132 2132 SNRmet = metArray[:,4]
2133 2133 vmet = metArray[:,5]
2134 2134 spcmet = metArray[:,6]
2135 2135
2136 2136 nChan = numpy.max(cmet) + 1
2137 2137 nHeights = len(heightList)
2138 2138
2139 2139 azimuth_arr, zenith_arr, dir_cosu, dir_cosv, dir_cosw = self.__calculateAngles(theta_x, theta_y, azimuth)
2140 2140 hmet = heightList[hmet]
2141 2141 h1met = hmet*numpy.cos(zenith_arr[cmet]) #Corrected heights
2142 2142
2143 2143 velEst = numpy.zeros((heightList.size,2))*numpy.nan
2144 2144
2145 2145 for i in range(nHeights - 1):
2146 2146 hmin = heightList[i]
2147 2147 hmax = heightList[i + 1]
2148 2148
2149 2149 thisH = (h1met>=hmin) & (h1met<hmax) & (cmet!=2) & (SNRmet>8) & (vmet<50) & (spcmet<10)
2150 2150 indthisH = numpy.where(thisH)
2151 2151
2152 2152 if numpy.size(indthisH) > 3:
2153 2153
2154 2154 vel_aux = vmet[thisH]
2155 2155 chan_aux = cmet[thisH]
2156 2156 cosu_aux = dir_cosu[chan_aux]
2157 2157 cosv_aux = dir_cosv[chan_aux]
2158 2158 cosw_aux = dir_cosw[chan_aux]
2159 2159
2160 2160 nch = numpy.size(numpy.unique(chan_aux))
2161 2161 if nch > 1:
2162 2162 A = self.__calculateMatA(cosu_aux, cosv_aux, cosw_aux, True)
2163 2163 velEst[i,:] = numpy.dot(A,vel_aux)
2164 2164
2165 2165 return velEst
2166 2166
2167 2167 def run(self, dataOut, technique, nHours=1, hmin=70, hmax=110, **kwargs):
2168 2168
2169 2169 param = dataOut.data_param
2170 2170 if dataOut.abscissaList != None:
2171 2171 absc = dataOut.abscissaList[:-1]
2172 2172 # noise = dataOut.noise
2173 2173 heightList = dataOut.heightList
2174 2174 SNR = dataOut.data_snr
2175 2175
2176 2176 if technique == 'DBS':
2177 2177
2178 2178 kwargs['velRadial'] = param[:,1,:] #Radial velocity
2179 2179 kwargs['heightList'] = heightList
2180 2180 kwargs['SNR'] = SNR
2181 2181
2182 2182 dataOut.data_output, dataOut.heightList, dataOut.data_snr = self.techniqueDBS(kwargs) #DBS Function
2183 2183 dataOut.utctimeInit = dataOut.utctime
2184 2184 dataOut.outputInterval = dataOut.paramInterval
2185 2185
2186 2186 elif technique == 'SA':
2187 2187
2188 2188 #Parameters
2189 2189 # position_x = kwargs['positionX']
2190 2190 # position_y = kwargs['positionY']
2191 2191 # azimuth = kwargs['azimuth']
2192 2192 #
2193 2193 # if kwargs.has_key('crosspairsList'):
2194 2194 # pairs = kwargs['crosspairsList']
2195 2195 # else:
2196 2196 # pairs = None
2197 2197 #
2198 2198 # if kwargs.has_key('correctFactor'):
2199 2199 # correctFactor = kwargs['correctFactor']
2200 2200 # else:
2201 2201 # correctFactor = 1
2202 2202
2203 2203 # tau = dataOut.data_param
2204 2204 # _lambda = dataOut.C/dataOut.frequency
2205 2205 # pairsList = dataOut.groupList
2206 2206 # nChannels = dataOut.nChannels
2207 2207
2208 2208 kwargs['groupList'] = dataOut.groupList
2209 2209 kwargs['tau'] = dataOut.data_param
2210 2210 kwargs['_lambda'] = dataOut.C/dataOut.frequency
2211 2211 # dataOut.data_output = self.techniqueSA(pairs, pairsList, nChannels, tau, azimuth, _lambda, position_x, position_y, absc, correctFactor)
2212 2212 dataOut.data_output = self.techniqueSA(kwargs)
2213 2213 dataOut.utctimeInit = dataOut.utctime
2214 2214 dataOut.outputInterval = dataOut.timeInterval
2215 2215
2216 2216 elif technique == 'Meteors':
2217 2217 dataOut.flagNoData = True
2218 2218 self.__dataReady = False
2219 2219
2220 2220 if 'nHours' in kwargs:
2221 2221 nHours = kwargs['nHours']
2222 2222 else:
2223 2223 nHours = 1
2224 2224
2225 2225 if 'meteorsPerBin' in kwargs:
2226 2226 meteorThresh = kwargs['meteorsPerBin']
2227 2227 else:
2228 2228 meteorThresh = 6
2229 2229
2230 2230 if 'hmin' in kwargs:
2231 2231 hmin = kwargs['hmin']
2232 2232 else: hmin = 70
2233 2233 if 'hmax' in kwargs:
2234 2234 hmax = kwargs['hmax']
2235 2235 else: hmax = 110
2236 2236
2237 2237 dataOut.outputInterval = nHours*3600
2238 2238
2239 2239 if self.__isConfig == False:
2240 2240 # self.__initime = dataOut.datatime.replace(minute = 0, second = 0, microsecond = 03)
2241 2241 #Get Initial LTC time
2242 2242 self.__initime = datetime.datetime.utcfromtimestamp(dataOut.utctime)
2243 2243 self.__initime = (self.__initime.replace(minute = 0, second = 0, microsecond = 0) - datetime.datetime(1970, 1, 1)).total_seconds()
2244 2244
2245 2245 self.__isConfig = True
2246 2246
2247 2247 if self.__buffer is None:
2248 2248 self.__buffer = dataOut.data_param
2249 2249 self.__firstdata = copy.copy(dataOut)
2250 2250
2251 2251 else:
2252 2252 self.__buffer = numpy.vstack((self.__buffer, dataOut.data_param))
2253 2253
2254 2254 self.__checkTime(dataOut.utctime, dataOut.paramInterval, dataOut.outputInterval) #Check if the buffer is ready
2255 2255
2256 2256 if self.__dataReady:
2257 2257 dataOut.utctimeInit = self.__initime
2258 2258
2259 2259 self.__initime += dataOut.outputInterval #to erase time offset
2260 2260
2261 2261 dataOut.data_output, dataOut.heightList = self.techniqueMeteors(self.__buffer, meteorThresh, hmin, hmax)
2262 2262 dataOut.flagNoData = False
2263 2263 self.__buffer = None
2264 2264
2265 2265 elif technique == 'Meteors1':
2266 2266 dataOut.flagNoData = True
2267 2267 self.__dataReady = False
2268 2268
2269 2269 if 'nMins' in kwargs:
2270 2270 nMins = kwargs['nMins']
2271 2271 else: nMins = 20
2272 2272 if 'rx_location' in kwargs:
2273 2273 rx_location = kwargs['rx_location']
2274 2274 else: rx_location = [(0,1),(1,1),(1,0)]
2275 2275 if 'azimuth' in kwargs:
2276 2276 azimuth = kwargs['azimuth']
2277 2277 else: azimuth = 51.06
2278 2278 if 'dfactor' in kwargs:
2279 2279 dfactor = kwargs['dfactor']
2280 2280 if 'mode' in kwargs:
2281 2281 mode = kwargs['mode']
2282 2282 if 'theta_x' in kwargs:
2283 2283 theta_x = kwargs['theta_x']
2284 2284 if 'theta_y' in kwargs:
2285 2285 theta_y = kwargs['theta_y']
2286 2286 else: mode = 'SA'
2287 2287
2288 2288 #Borrar luego esto
2289 2289 if dataOut.groupList is None:
2290 2290 dataOut.groupList = [(0,1),(0,2),(1,2)]
2291 2291 groupList = dataOut.groupList
2292 2292 C = 3e8
2293 2293 freq = 50e6
2294 2294 lamb = C/freq
2295 2295 k = 2*numpy.pi/lamb
2296 2296
2297 2297 timeList = dataOut.abscissaList
2298 2298 heightList = dataOut.heightList
2299 2299
2300 2300 if self.__isConfig == False:
2301 2301 dataOut.outputInterval = nMins*60
2302 2302 # self.__initime = dataOut.datatime.replace(minute = 0, second = 0, microsecond = 03)
2303 2303 #Get Initial LTC time
2304 2304 initime = datetime.datetime.utcfromtimestamp(dataOut.utctime)
2305 2305 minuteAux = initime.minute
2306 2306 minuteNew = int(numpy.floor(minuteAux/nMins)*nMins)
2307 2307 self.__initime = (initime.replace(minute = minuteNew, second = 0, microsecond = 0) - datetime.datetime(1970, 1, 1)).total_seconds()
2308 2308
2309 2309 self.__isConfig = True
2310 2310
2311 2311 if self.__buffer is None:
2312 2312 self.__buffer = dataOut.data_param
2313 2313 self.__firstdata = copy.copy(dataOut)
2314 2314
2315 2315 else:
2316 2316 self.__buffer = numpy.vstack((self.__buffer, dataOut.data_param))
2317 2317
2318 2318 self.__checkTime(dataOut.utctime, dataOut.paramInterval, dataOut.outputInterval) #Check if the buffer is ready
2319 2319
2320 2320 if self.__dataReady:
2321 2321 dataOut.utctimeInit = self.__initime
2322 2322 self.__initime += dataOut.outputInterval #to erase time offset
2323 2323
2324 2324 metArray = self.__buffer
2325 2325 if mode == 'SA':
2326 2326 dataOut.data_output = self.techniqueNSM_SA(rx_location=rx_location, groupList=groupList, azimuth=azimuth, dfactor=dfactor, k=k,metArray=metArray, heightList=heightList,timeList=timeList)
2327 2327 elif mode == 'DBS':
2328 2328 dataOut.data_output = self.techniqueNSM_DBS(metArray=metArray,heightList=heightList,timeList=timeList, azimuth=azimuth, theta_x=theta_x, theta_y=theta_y)
2329 2329 dataOut.data_output = dataOut.data_output.T
2330 2330 dataOut.flagNoData = False
2331 2331 self.__buffer = None
2332 2332
2333 2333 return
2334 2334
2335 2335 class EWDriftsEstimation(Operation):
2336 2336
2337 2337 def __init__(self):
2338 2338 Operation.__init__(self)
2339 2339
2340 2340 def __correctValues(self, heiRang, phi, velRadial, SNR):
2341 2341 listPhi = phi.tolist()
2342 2342 maxid = listPhi.index(max(listPhi))
2343 2343 minid = listPhi.index(min(listPhi))
2344 2344
2345 2345 rango = list(range(len(phi)))
2346 2346 # rango = numpy.delete(rango,maxid)
2347 2347
2348 2348 heiRang1 = heiRang*math.cos(phi[maxid])
2349 2349 heiRangAux = heiRang*math.cos(phi[minid])
2350 2350 indOut = (heiRang1 < heiRangAux[0]).nonzero()
2351 2351 heiRang1 = numpy.delete(heiRang1,indOut)
2352 2352
2353 2353 velRadial1 = numpy.zeros([len(phi),len(heiRang1)])
2354 2354 SNR1 = numpy.zeros([len(phi),len(heiRang1)])
2355 2355
2356 2356 for i in rango:
2357 2357 x = heiRang*math.cos(phi[i])
2358 2358 y1 = velRadial[i,:]
2359 2359 f1 = interpolate.interp1d(x,y1,kind = 'cubic')
2360 2360
2361 2361 x1 = heiRang1
2362 2362 y11 = f1(x1)
2363 2363
2364 2364 y2 = SNR[i,:]
2365 2365 f2 = interpolate.interp1d(x,y2,kind = 'cubic')
2366 2366 y21 = f2(x1)
2367 2367
2368 2368 velRadial1[i,:] = y11
2369 2369 SNR1[i,:] = y21
2370 2370
2371 2371 return heiRang1, velRadial1, SNR1
2372 2372
2373 2373 def run(self, dataOut, zenith, zenithCorrection):
2374 2374 heiRang = dataOut.heightList
2375 2375 velRadial = dataOut.data_param[:,3,:]
2376 2376 SNR = dataOut.data_snr
2377 2377
2378 2378 zenith = numpy.array(zenith)
2379 2379 zenith -= zenithCorrection
2380 2380 zenith *= numpy.pi/180
2381 2381
2382 2382 heiRang1, velRadial1, SNR1 = self.__correctValues(heiRang, numpy.abs(zenith), velRadial, SNR)
2383 2383
2384 2384 alp = zenith[0]
2385 2385 bet = zenith[1]
2386 2386
2387 2387 w_w = velRadial1[0,:]
2388 2388 w_e = velRadial1[1,:]
2389 2389
2390 2390 w = (w_w*numpy.sin(bet) - w_e*numpy.sin(alp))/(numpy.cos(alp)*numpy.sin(bet) - numpy.cos(bet)*numpy.sin(alp))
2391 2391 u = (w_w*numpy.cos(bet) - w_e*numpy.cos(alp))/(numpy.sin(alp)*numpy.cos(bet) - numpy.sin(bet)*numpy.cos(alp))
2392 2392
2393 2393 winds = numpy.vstack((u,w))
2394 2394
2395 2395 dataOut.heightList = heiRang1
2396 2396 dataOut.data_output = winds
2397 2397 dataOut.data_snr = SNR1
2398 2398
2399 2399 dataOut.utctimeInit = dataOut.utctime
2400 2400 dataOut.outputInterval = dataOut.timeInterval
2401 2401 return
2402 2402
2403 2403 #--------------- Non Specular Meteor ----------------
2404 2404
2405 2405 class NonSpecularMeteorDetection(Operation):
2406 2406
2407 2407 def run(self, dataOut, mode, SNRthresh=8, phaseDerThresh=0.5, cohThresh=0.8, allData = False):
2408 2408 data_acf = dataOut.data_pre[0]
2409 2409 data_ccf = dataOut.data_pre[1]
2410 2410 pairsList = dataOut.groupList[1]
2411 2411
2412 2412 lamb = dataOut.C/dataOut.frequency
2413 2413 tSamp = dataOut.ippSeconds*dataOut.nCohInt
2414 2414 paramInterval = dataOut.paramInterval
2415 2415
2416 2416 nChannels = data_acf.shape[0]
2417 2417 nLags = data_acf.shape[1]
2418 2418 nProfiles = data_acf.shape[2]
2419 2419 nHeights = dataOut.nHeights
2420 2420 nCohInt = dataOut.nCohInt
2421 2421 sec = numpy.round(nProfiles/dataOut.paramInterval)
2422 2422 heightList = dataOut.heightList
2423 2423 ippSeconds = dataOut.ippSeconds*dataOut.nCohInt*dataOut.nAvg
2424 2424 utctime = dataOut.utctime
2425 2425
2426 2426 dataOut.abscissaList = numpy.arange(0,paramInterval+ippSeconds,ippSeconds)
2427 2427
2428 2428 #------------------------ SNR --------------------------------------
2429 2429 power = data_acf[:,0,:,:].real
2430 2430 noise = numpy.zeros(nChannels)
2431 2431 SNR = numpy.zeros(power.shape)
2432 2432 for i in range(nChannels):
2433 2433 noise[i] = hildebrand_sekhon(power[i,:], nCohInt)
2434 2434 SNR[i] = (power[i]-noise[i])/noise[i]
2435 2435 SNRm = numpy.nanmean(SNR, axis = 0)
2436 2436 SNRdB = 10*numpy.log10(SNR)
2437 2437
2438 2438 if mode == 'SA':
2439 2439 dataOut.groupList = dataOut.groupList[1]
2440 2440 nPairs = data_ccf.shape[0]
2441 2441 #---------------------- Coherence and Phase --------------------------
2442 2442 phase = numpy.zeros(data_ccf[:,0,:,:].shape)
2443 2443 # phase1 = numpy.copy(phase)
2444 2444 coh1 = numpy.zeros(data_ccf[:,0,:,:].shape)
2445 2445
2446 2446 for p in range(nPairs):
2447 2447 ch0 = pairsList[p][0]
2448 2448 ch1 = pairsList[p][1]
2449 2449 ccf = data_ccf[p,0,:,:]/numpy.sqrt(data_acf[ch0,0,:,:]*data_acf[ch1,0,:,:])
2450 2450 phase[p,:,:] = ndimage.median_filter(numpy.angle(ccf), size = (5,1)) #median filter
2451 2451 # phase1[p,:,:] = numpy.angle(ccf) #median filter
2452 2452 coh1[p,:,:] = ndimage.median_filter(numpy.abs(ccf), 5) #median filter
2453 2453 # coh1[p,:,:] = numpy.abs(ccf) #median filter
2454 2454 coh = numpy.nanmax(coh1, axis = 0)
2455 2455 # struc = numpy.ones((5,1))
2456 2456 # coh = ndimage.morphology.grey_dilation(coh, size=(10,1))
2457 2457 #---------------------- Radial Velocity ----------------------------
2458 2458 phaseAux = numpy.mean(numpy.angle(data_acf[:,1,:,:]), axis = 0)
2459 2459 velRad = phaseAux*lamb/(4*numpy.pi*tSamp)
2460 2460
2461 2461 if allData:
2462 2462 boolMetFin = ~numpy.isnan(SNRm)
2463 2463 # coh[:-1,:] = numpy.nanmean(numpy.abs(phase[:,1:,:] - phase[:,:-1,:]),axis=0)
2464 2464 else:
2465 2465 #------------------------ Meteor mask ---------------------------------
2466 2466 # #SNR mask
2467 2467 # boolMet = (SNRdB>SNRthresh)#|(~numpy.isnan(SNRdB))
2468 2468 #
2469 2469 # #Erase small objects
2470 2470 # boolMet1 = self.__erase_small(boolMet, 2*sec, 5)
2471 2471 #
2472 2472 # auxEEJ = numpy.sum(boolMet1,axis=0)
2473 2473 # indOver = auxEEJ>nProfiles*0.8 #Use this later
2474 2474 # indEEJ = numpy.where(indOver)[0]
2475 2475 # indNEEJ = numpy.where(~indOver)[0]
2476 2476 #
2477 2477 # boolMetFin = boolMet1
2478 2478 #
2479 2479 # if indEEJ.size > 0:
2480 2480 # boolMet1[:,indEEJ] = False #Erase heights with EEJ
2481 2481 #
2482 2482 # boolMet2 = coh > cohThresh
2483 2483 # boolMet2 = self.__erase_small(boolMet2, 2*sec,5)
2484 2484 #
2485 2485 # #Final Meteor mask
2486 2486 # boolMetFin = boolMet1|boolMet2
2487 2487
2488 2488 #Coherence mask
2489 2489 boolMet1 = coh > 0.75
2490 2490 struc = numpy.ones((30,1))
2491 2491 boolMet1 = ndimage.morphology.binary_dilation(boolMet1, structure=struc)
2492 2492
2493 2493 #Derivative mask
2494 2494 derPhase = numpy.nanmean(numpy.abs(phase[:,1:,:] - phase[:,:-1,:]),axis=0)
2495 2495 boolMet2 = derPhase < 0.2
2496 2496 # boolMet2 = ndimage.morphology.binary_opening(boolMet2)
2497 2497 # boolMet2 = ndimage.morphology.binary_closing(boolMet2, structure = numpy.ones((10,1)))
2498 2498 boolMet2 = ndimage.median_filter(boolMet2,size=5)
2499 2499 boolMet2 = numpy.vstack((boolMet2,numpy.full((1,nHeights), True, dtype=bool)))
2500 2500 # #Final mask
2501 2501 # boolMetFin = boolMet2
2502 2502 boolMetFin = boolMet1&boolMet2
2503 2503 # boolMetFin = ndimage.morphology.binary_dilation(boolMetFin)
2504 2504 #Creating data_param
2505 2505 coordMet = numpy.where(boolMetFin)
2506 2506
2507 2507 tmet = coordMet[0]
2508 2508 hmet = coordMet[1]
2509 2509
2510 2510 data_param = numpy.zeros((tmet.size, 6 + nPairs))
2511 2511 data_param[:,0] = utctime
2512 2512 data_param[:,1] = tmet
2513 2513 data_param[:,2] = hmet
2514 2514 data_param[:,3] = SNRm[tmet,hmet]
2515 2515 data_param[:,4] = velRad[tmet,hmet]
2516 2516 data_param[:,5] = coh[tmet,hmet]
2517 2517 data_param[:,6:] = phase[:,tmet,hmet].T
2518 2518
2519 2519 elif mode == 'DBS':
2520 2520 dataOut.groupList = numpy.arange(nChannels)
2521 2521
2522 2522 #Radial Velocities
2523 2523 phase = numpy.angle(data_acf[:,1,:,:])
2524 2524 # phase = ndimage.median_filter(numpy.angle(data_acf[:,1,:,:]), size = (1,5,1))
2525 2525 velRad = phase*lamb/(4*numpy.pi*tSamp)
2526 2526
2527 2527 #Spectral width
2528 2528 # acf1 = ndimage.median_filter(numpy.abs(data_acf[:,1,:,:]), size = (1,5,1))
2529 2529 # acf2 = ndimage.median_filter(numpy.abs(data_acf[:,2,:,:]), size = (1,5,1))
2530 2530 acf1 = data_acf[:,1,:,:]
2531 2531 acf2 = data_acf[:,2,:,:]
2532 2532
2533 2533 spcWidth = (lamb/(2*numpy.sqrt(6)*numpy.pi*tSamp))*numpy.sqrt(numpy.log(acf1/acf2))
2534 2534 # velRad = ndimage.median_filter(velRad, size = (1,5,1))
2535 2535 if allData:
2536 2536 boolMetFin = ~numpy.isnan(SNRdB)
2537 2537 else:
2538 2538 #SNR
2539 2539 boolMet1 = (SNRdB>SNRthresh) #SNR mask
2540 2540 boolMet1 = ndimage.median_filter(boolMet1, size=(1,5,5))
2541 2541
2542 2542 #Radial velocity
2543 2543 boolMet2 = numpy.abs(velRad) < 20
2544 2544 boolMet2 = ndimage.median_filter(boolMet2, (1,5,5))
2545 2545
2546 2546 #Spectral Width
2547 2547 boolMet3 = spcWidth < 30
2548 2548 boolMet3 = ndimage.median_filter(boolMet3, (1,5,5))
2549 2549 # boolMetFin = self.__erase_small(boolMet1, 10,5)
2550 2550 boolMetFin = boolMet1&boolMet2&boolMet3
2551 2551
2552 2552 #Creating data_param
2553 2553 coordMet = numpy.where(boolMetFin)
2554 2554
2555 2555 cmet = coordMet[0]
2556 2556 tmet = coordMet[1]
2557 2557 hmet = coordMet[2]
2558 2558
2559 2559 data_param = numpy.zeros((tmet.size, 7))
2560 2560 data_param[:,0] = utctime
2561 2561 data_param[:,1] = cmet
2562 2562 data_param[:,2] = tmet
2563 2563 data_param[:,3] = hmet
2564 2564 data_param[:,4] = SNR[cmet,tmet,hmet].T
2565 2565 data_param[:,5] = velRad[cmet,tmet,hmet].T
2566 2566 data_param[:,6] = spcWidth[cmet,tmet,hmet].T
2567 2567
2568 2568 # self.dataOut.data_param = data_int
2569 2569 if len(data_param) == 0:
2570 2570 dataOut.flagNoData = True
2571 2571 else:
2572 2572 dataOut.data_param = data_param
2573 2573
2574 2574 def __erase_small(self, binArray, threshX, threshY):
2575 2575 labarray, numfeat = ndimage.measurements.label(binArray)
2576 2576 binArray1 = numpy.copy(binArray)
2577 2577
2578 2578 for i in range(1,numfeat + 1):
2579 2579 auxBin = (labarray==i)
2580 2580 auxSize = auxBin.sum()
2581 2581
2582 2582 x,y = numpy.where(auxBin)
2583 2583 widthX = x.max() - x.min()
2584 2584 widthY = y.max() - y.min()
2585 2585
2586 2586 #width X: 3 seg -> 12.5*3
2587 2587 #width Y:
2588 2588
2589 2589 if (auxSize < 50) or (widthX < threshX) or (widthY < threshY):
2590 2590 binArray1[auxBin] = False
2591 2591
2592 2592 return binArray1
2593 2593
2594 2594 #--------------- Specular Meteor ----------------
2595 2595
2596 2596 class SMDetection(Operation):
2597 2597 '''
2598 2598 Function DetectMeteors()
2599 2599 Project developed with paper:
2600 2600 HOLDSWORTH ET AL. 2004
2601 2601
2602 2602 Input:
2603 2603 self.dataOut.data_pre
2604 2604
2605 2605 centerReceiverIndex: From the channels, which is the center receiver
2606 2606
2607 2607 hei_ref: Height reference for the Beacon signal extraction
2608 2608 tauindex:
2609 2609 predefinedPhaseShifts: Predefined phase offset for the voltge signals
2610 2610
2611 2611 cohDetection: Whether to user Coherent detection or not
2612 2612 cohDet_timeStep: Coherent Detection calculation time step
2613 2613 cohDet_thresh: Coherent Detection phase threshold to correct phases
2614 2614
2615 2615 noise_timeStep: Noise calculation time step
2616 2616 noise_multiple: Noise multiple to define signal threshold
2617 2617
2618 2618 multDet_timeLimit: Multiple Detection Removal time limit in seconds
2619 2619 multDet_rangeLimit: Multiple Detection Removal range limit in km
2620 2620
2621 2621 phaseThresh: Maximum phase difference between receiver to be consider a meteor
2622 2622 SNRThresh: Minimum SNR threshold of the meteor signal to be consider a meteor
2623 2623
2624 2624 hmin: Minimum Height of the meteor to use it in the further wind estimations
2625 2625 hmax: Maximum Height of the meteor to use it in the further wind estimations
2626 2626 azimuth: Azimuth angle correction
2627 2627
2628 2628 Affected:
2629 2629 self.dataOut.data_param
2630 2630
2631 2631 Rejection Criteria (Errors):
2632 2632 0: No error; analysis OK
2633 2633 1: SNR < SNR threshold
2634 2634 2: angle of arrival (AOA) ambiguously determined
2635 2635 3: AOA estimate not feasible
2636 2636 4: Large difference in AOAs obtained from different antenna baselines
2637 2637 5: echo at start or end of time series
2638 2638 6: echo less than 5 examples long; too short for analysis
2639 2639 7: echo rise exceeds 0.3s
2640 2640 8: echo decay time less than twice rise time
2641 2641 9: large power level before echo
2642 2642 10: large power level after echo
2643 2643 11: poor fit to amplitude for estimation of decay time
2644 2644 12: poor fit to CCF phase variation for estimation of radial drift velocity
2645 2645 13: height unresolvable echo: not valid height within 70 to 110 km
2646 2646 14: height ambiguous echo: more then one possible height within 70 to 110 km
2647 2647 15: radial drift velocity or projected horizontal velocity exceeds 200 m/s
2648 2648 16: oscilatory echo, indicating event most likely not an underdense echo
2649 2649
2650 2650 17: phase difference in meteor Reestimation
2651 2651
2652 2652 Data Storage:
2653 2653 Meteors for Wind Estimation (8):
2654 2654 Utc Time | Range Height
2655 2655 Azimuth Zenith errorCosDir
2656 2656 VelRad errorVelRad
2657 2657 Phase0 Phase1 Phase2 Phase3
2658 2658 TypeError
2659 2659
2660 2660 '''
2661 2661
2662 2662 def run(self, dataOut, hei_ref = None, tauindex = 0,
2663 2663 phaseOffsets = None,
2664 2664 cohDetection = False, cohDet_timeStep = 1, cohDet_thresh = 25,
2665 2665 noise_timeStep = 4, noise_multiple = 4,
2666 2666 multDet_timeLimit = 1, multDet_rangeLimit = 3,
2667 2667 phaseThresh = 20, SNRThresh = 5,
2668 2668 hmin = 50, hmax=150, azimuth = 0,
2669 2669 channelPositions = None) :
2670 2670
2671 2671
2672 2672 #Getting Pairslist
2673 2673 if channelPositions is None:
2674 2674 # channelPositions = [(2.5,0), (0,2.5), (0,0), (0,4.5), (-2,0)] #T
2675 2675 channelPositions = [(4.5,2), (2,4.5), (2,2), (2,0), (0,2)] #Estrella
2676 2676 meteorOps = SMOperations()
2677 2677 pairslist0, distances = meteorOps.getPhasePairs(channelPositions)
2678 2678 heiRang = dataOut.heightList
2679 2679 #Get Beacon signal - No Beacon signal anymore
2680 2680 # newheis = numpy.where(self.dataOut.heightList>self.dataOut.radarControllerHeaderObj.Taus[tauindex])
2681 2681 #
2682 2682 # if hei_ref != None:
2683 2683 # newheis = numpy.where(self.dataOut.heightList>hei_ref)
2684 2684 #
2685 2685
2686 2686
2687 2687 #****************REMOVING HARDWARE PHASE DIFFERENCES***************
2688 2688 # see if the user put in pre defined phase shifts
2689 2689 voltsPShift = dataOut.data_pre.copy()
2690 2690
2691 2691 # if predefinedPhaseShifts != None:
2692 2692 # hardwarePhaseShifts = numpy.array(predefinedPhaseShifts)*numpy.pi/180
2693 2693 #
2694 2694 # # elif beaconPhaseShifts:
2695 2695 # # #get hardware phase shifts using beacon signal
2696 2696 # # hardwarePhaseShifts = self.__getHardwarePhaseDiff(self.dataOut.data_pre, pairslist, newheis, 10)
2697 2697 # # hardwarePhaseShifts = numpy.insert(hardwarePhaseShifts,centerReceiverIndex,0)
2698 2698 #
2699 2699 # else:
2700 2700 # hardwarePhaseShifts = numpy.zeros(5)
2701 2701 #
2702 2702 # voltsPShift = numpy.zeros((self.dataOut.data_pre.shape[0],self.dataOut.data_pre.shape[1],self.dataOut.data_pre.shape[2]), dtype = 'complex')
2703 2703 # for i in range(self.dataOut.data_pre.shape[0]):
2704 2704 # voltsPShift[i,:,:] = self.__shiftPhase(self.dataOut.data_pre[i,:,:], hardwarePhaseShifts[i])
2705 2705
2706 2706 #******************END OF REMOVING HARDWARE PHASE DIFFERENCES*********
2707 2707
2708 2708 #Remove DC
2709 2709 voltsDC = numpy.mean(voltsPShift,1)
2710 2710 voltsDC = numpy.mean(voltsDC,1)
2711 2711 for i in range(voltsDC.shape[0]):
2712 2712 voltsPShift[i] = voltsPShift[i] - voltsDC[i]
2713 2713
2714 2714 #Don't considerate last heights, theyre used to calculate Hardware Phase Shift
2715 2715 # voltsPShift = voltsPShift[:,:,:newheis[0][0]]
2716 2716
2717 2717 #************ FIND POWER OF DATA W/COH OR NON COH DETECTION (3.4) **********
2718 2718 #Coherent Detection
2719 2719 if cohDetection:
2720 2720 #use coherent detection to get the net power
2721 2721 cohDet_thresh = cohDet_thresh*numpy.pi/180
2722 2722 voltsPShift = self.__coherentDetection(voltsPShift, cohDet_timeStep, dataOut.timeInterval, pairslist0, cohDet_thresh)
2723 2723
2724 2724 #Non-coherent detection!
2725 2725 powerNet = numpy.nansum(numpy.abs(voltsPShift[:,:,:])**2,0)
2726 2726 #********** END OF COH/NON-COH POWER CALCULATION**********************
2727 2727
2728 2728 #********** FIND THE NOISE LEVEL AND POSSIBLE METEORS ****************
2729 2729 #Get noise
2730 2730 noise, noise1 = self.__getNoise(powerNet, noise_timeStep, dataOut.timeInterval)
2731 2731 # noise = self.getNoise1(powerNet, noise_timeStep, self.dataOut.timeInterval)
2732 2732 #Get signal threshold
2733 2733 signalThresh = noise_multiple*noise
2734 2734 #Meteor echoes detection
2735 2735 listMeteors = self.__findMeteors(powerNet, signalThresh)
2736 2736 #******* END OF NOISE LEVEL AND POSSIBLE METEORS CACULATION **********
2737 2737
2738 2738 #************** REMOVE MULTIPLE DETECTIONS (3.5) ***************************
2739 2739 #Parameters
2740 2740 heiRange = dataOut.heightList
2741 2741 rangeInterval = heiRange[1] - heiRange[0]
2742 2742 rangeLimit = multDet_rangeLimit/rangeInterval
2743 2743 timeLimit = multDet_timeLimit/dataOut.timeInterval
2744 2744 #Multiple detection removals
2745 2745 listMeteors1 = self.__removeMultipleDetections(listMeteors, rangeLimit, timeLimit)
2746 2746 #************ END OF REMOVE MULTIPLE DETECTIONS **********************
2747 2747
2748 2748 #********************* METEOR REESTIMATION (3.7, 3.8, 3.9, 3.10) ********************
2749 2749 #Parameters
2750 2750 phaseThresh = phaseThresh*numpy.pi/180
2751 2751 thresh = [phaseThresh, noise_multiple, SNRThresh]
2752 2752 #Meteor reestimation (Errors N 1, 6, 12, 17)
2753 2753 listMeteors2, listMeteorsPower, listMeteorsVolts = self.__meteorReestimation(listMeteors1, voltsPShift, pairslist0, thresh, noise, dataOut.timeInterval, dataOut.frequency)
2754 2754 # listMeteors2, listMeteorsPower, listMeteorsVolts = self.meteorReestimation3(listMeteors2, listMeteorsPower, listMeteorsVolts, voltsPShift, pairslist, thresh, noise)
2755 2755 #Estimation of decay times (Errors N 7, 8, 11)
2756 2756 listMeteors3 = self.__estimateDecayTime(listMeteors2, listMeteorsPower, dataOut.timeInterval, dataOut.frequency)
2757 2757 #******************* END OF METEOR REESTIMATION *******************
2758 2758
2759 2759 #********************* METEOR PARAMETERS CALCULATION (3.11, 3.12, 3.13) **************************
2760 2760 #Calculating Radial Velocity (Error N 15)
2761 2761 radialStdThresh = 10
2762 2762 listMeteors4 = self.__getRadialVelocity(listMeteors3, listMeteorsVolts, radialStdThresh, pairslist0, dataOut.timeInterval)
2763 2763
2764 2764 if len(listMeteors4) > 0:
2765 2765 #Setting New Array
2766 2766 date = dataOut.utctime
2767 2767 arrayParameters = self.__setNewArrays(listMeteors4, date, heiRang)
2768 2768
2769 2769 #Correcting phase offset
2770 2770 if phaseOffsets != None:
2771 2771 phaseOffsets = numpy.array(phaseOffsets)*numpy.pi/180
2772 2772 arrayParameters[:,8:12] = numpy.unwrap(arrayParameters[:,8:12] + phaseOffsets)
2773 2773
2774 2774 #Second Pairslist
2775 2775 pairsList = []
2776 2776 pairx = (0,1)
2777 2777 pairy = (2,3)
2778 2778 pairsList.append(pairx)
2779 2779 pairsList.append(pairy)
2780 2780
2781 2781 jph = numpy.array([0,0,0,0])
2782 2782 h = (hmin,hmax)
2783 2783 arrayParameters = meteorOps.getMeteorParams(arrayParameters, azimuth, h, pairsList, distances, jph)
2784 2784
2785 2785 # #Calculate AOA (Error N 3, 4)
2786 2786 # #JONES ET AL. 1998
2787 2787 # error = arrayParameters[:,-1]
2788 2788 # AOAthresh = numpy.pi/8
2789 2789 # phases = -arrayParameters[:,9:13]
2790 2790 # arrayParameters[:,4:7], arrayParameters[:,-1] = meteorOps.getAOA(phases, pairsList, error, AOAthresh, azimuth)
2791 2791 #
2792 2792 # #Calculate Heights (Error N 13 and 14)
2793 2793 # error = arrayParameters[:,-1]
2794 2794 # Ranges = arrayParameters[:,2]
2795 2795 # zenith = arrayParameters[:,5]
2796 2796 # arrayParameters[:,3], arrayParameters[:,-1] = meteorOps.getHeights(Ranges, zenith, error, hmin, hmax)
2797 2797 # error = arrayParameters[:,-1]
2798 2798 #********************* END OF PARAMETERS CALCULATION **************************
2799 2799
2800 2800 #***************************+ PASS DATA TO NEXT STEP **********************
2801 2801 # arrayFinal = arrayParameters.reshape((1,arrayParameters.shape[0],arrayParameters.shape[1]))
2802 2802 dataOut.data_param = arrayParameters
2803 2803
2804 2804 if arrayParameters is None:
2805 2805 dataOut.flagNoData = True
2806 2806 else:
2807 2807 dataOut.flagNoData = True
2808 2808
2809 2809 return
2810 2810
2811 2811 def __getHardwarePhaseDiff(self, voltage0, pairslist, newheis, n):
2812 2812
2813 2813 minIndex = min(newheis[0])
2814 2814 maxIndex = max(newheis[0])
2815 2815
2816 2816 voltage = voltage0[:,:,minIndex:maxIndex+1]
2817 2817 nLength = voltage.shape[1]/n
2818 2818 nMin = 0
2819 2819 nMax = 0
2820 2820 phaseOffset = numpy.zeros((len(pairslist),n))
2821 2821
2822 2822 for i in range(n):
2823 2823 nMax += nLength
2824 2824 phaseCCF = -numpy.angle(self.__calculateCCF(voltage[:,nMin:nMax,:], pairslist, [0]))
2825 2825 phaseCCF = numpy.mean(phaseCCF, axis = 2)
2826 2826 phaseOffset[:,i] = phaseCCF.transpose()
2827 2827 nMin = nMax
2828 2828 # phaseDiff, phaseArrival = self.estimatePhaseDifference(voltage, pairslist)
2829 2829
2830 2830 #Remove Outliers
2831 2831 factor = 2
2832 2832 wt = phaseOffset - signal.medfilt(phaseOffset,(1,5))
2833 2833 dw = numpy.std(wt,axis = 1)
2834 2834 dw = dw.reshape((dw.size,1))
2835 2835 ind = numpy.where(numpy.logical_or(wt>dw*factor,wt<-dw*factor))
2836 2836 phaseOffset[ind] = numpy.nan
2837 2837 phaseOffset = stats.nanmean(phaseOffset, axis=1)
2838 2838
2839 2839 return phaseOffset
2840 2840
2841 2841 def __shiftPhase(self, data, phaseShift):
2842 2842 #this will shift the phase of a complex number
2843 2843 dataShifted = numpy.abs(data) * numpy.exp((numpy.angle(data)+phaseShift)*1j)
2844 2844 return dataShifted
2845 2845
2846 2846 def __estimatePhaseDifference(self, array, pairslist):
2847 2847 nChannel = array.shape[0]
2848 2848 nHeights = array.shape[2]
2849 2849 numPairs = len(pairslist)
2850 2850 # phaseCCF = numpy.zeros((nChannel, 5, nHeights))
2851 2851 phaseCCF = numpy.angle(self.__calculateCCF(array, pairslist, [-2,-1,0,1,2]))
2852 2852
2853 2853 #Correct phases
2854 2854 derPhaseCCF = phaseCCF[:,1:,:] - phaseCCF[:,0:-1,:]
2855 2855 indDer = numpy.where(numpy.abs(derPhaseCCF) > numpy.pi)
2856 2856
2857 2857 if indDer[0].shape[0] > 0:
2858 2858 for i in range(indDer[0].shape[0]):
2859 2859 signo = -numpy.sign(derPhaseCCF[indDer[0][i],indDer[1][i],indDer[2][i]])
2860 2860 phaseCCF[indDer[0][i],indDer[1][i]+1:,:] += signo*2*numpy.pi
2861 2861
2862 2862 # for j in range(numSides):
2863 2863 # phaseCCFAux = self.calculateCCF(arrayCenter, arraySides[j,:,:], [-2,1,0,1,2])
2864 2864 # phaseCCF[j,:,:] = numpy.angle(phaseCCFAux)
2865 2865 #
2866 2866 #Linear
2867 2867 phaseInt = numpy.zeros((numPairs,1))
2868 2868 angAllCCF = phaseCCF[:,[0,1,3,4],0]
2869 2869 for j in range(numPairs):
2870 2870 fit = stats.linregress([-2,-1,1,2],angAllCCF[j,:])
2871 2871 phaseInt[j] = fit[1]
2872 2872 #Phase Differences
2873 2873 phaseDiff = phaseInt - phaseCCF[:,2,:]
2874 2874 phaseArrival = phaseInt.reshape(phaseInt.size)
2875 2875
2876 2876 #Dealias
2877 2877 phaseArrival = numpy.angle(numpy.exp(1j*phaseArrival))
2878 2878 # indAlias = numpy.where(phaseArrival > numpy.pi)
2879 2879 # phaseArrival[indAlias] -= 2*numpy.pi
2880 2880 # indAlias = numpy.where(phaseArrival < -numpy.pi)
2881 2881 # phaseArrival[indAlias] += 2*numpy.pi
2882 2882
2883 2883 return phaseDiff, phaseArrival
2884 2884
2885 2885 def __coherentDetection(self, volts, timeSegment, timeInterval, pairslist, thresh):
2886 2886 #this function will run the coherent detection used in Holdworth et al. 2004 and return the net power
2887 2887 #find the phase shifts of each channel over 1 second intervals
2888 2888 #only look at ranges below the beacon signal
2889 2889 numProfPerBlock = numpy.ceil(timeSegment/timeInterval)
2890 2890 numBlocks = int(volts.shape[1]/numProfPerBlock)
2891 2891 numHeights = volts.shape[2]
2892 2892 nChannel = volts.shape[0]
2893 2893 voltsCohDet = volts.copy()
2894 2894
2895 2895 pairsarray = numpy.array(pairslist)
2896 2896 indSides = pairsarray[:,1]
2897 2897 # indSides = numpy.array(range(nChannel))
2898 2898 # indSides = numpy.delete(indSides, indCenter)
2899 2899 #
2900 2900 # listCenter = numpy.array_split(volts[indCenter,:,:], numBlocks, 0)
2901 2901 listBlocks = numpy.array_split(volts, numBlocks, 1)
2902 2902
2903 2903 startInd = 0
2904 2904 endInd = 0
2905 2905
2906 2906 for i in range(numBlocks):
2907 2907 startInd = endInd
2908 2908 endInd = endInd + listBlocks[i].shape[1]
2909 2909
2910 2910 arrayBlock = listBlocks[i]
2911 2911 # arrayBlockCenter = listCenter[i]
2912 2912
2913 2913 #Estimate the Phase Difference
2914 2914 phaseDiff, aux = self.__estimatePhaseDifference(arrayBlock, pairslist)
2915 2915 #Phase Difference RMS
2916 2916 arrayPhaseRMS = numpy.abs(phaseDiff)
2917 2917 phaseRMSaux = numpy.sum(arrayPhaseRMS < thresh,0)
2918 2918 indPhase = numpy.where(phaseRMSaux==4)
2919 2919 #Shifting
2920 2920 if indPhase[0].shape[0] > 0:
2921 2921 for j in range(indSides.size):
2922 2922 arrayBlock[indSides[j],:,indPhase] = self.__shiftPhase(arrayBlock[indSides[j],:,indPhase], phaseDiff[j,indPhase].transpose())
2923 2923 voltsCohDet[:,startInd:endInd,:] = arrayBlock
2924 2924
2925 2925 return voltsCohDet
2926 2926
2927 2927 def __calculateCCF(self, volts, pairslist ,laglist):
2928 2928
2929 2929 nHeights = volts.shape[2]
2930 2930 nPoints = volts.shape[1]
2931 2931 voltsCCF = numpy.zeros((len(pairslist), len(laglist), nHeights),dtype = 'complex')
2932 2932
2933 2933 for i in range(len(pairslist)):
2934 2934 volts1 = volts[pairslist[i][0]]
2935 2935 volts2 = volts[pairslist[i][1]]
2936 2936
2937 2937 for t in range(len(laglist)):
2938 2938 idxT = laglist[t]
2939 2939 if idxT >= 0:
2940 2940 vStacked = numpy.vstack((volts2[idxT:,:],
2941 2941 numpy.zeros((idxT, nHeights),dtype='complex')))
2942 2942 else:
2943 2943 vStacked = numpy.vstack((numpy.zeros((-idxT, nHeights),dtype='complex'),
2944 2944 volts2[:(nPoints + idxT),:]))
2945 2945 voltsCCF[i,t,:] = numpy.sum((numpy.conjugate(volts1)*vStacked),axis=0)
2946 2946
2947 2947 vStacked = None
2948 2948 return voltsCCF
2949 2949
2950 2950 def __getNoise(self, power, timeSegment, timeInterval):
2951 2951 numProfPerBlock = numpy.ceil(timeSegment/timeInterval)
2952 2952 numBlocks = int(power.shape[0]/numProfPerBlock)
2953 2953 numHeights = power.shape[1]
2954 2954
2955 2955 listPower = numpy.array_split(power, numBlocks, 0)
2956 2956 noise = numpy.zeros((power.shape[0], power.shape[1]))
2957 2957 noise1 = numpy.zeros((power.shape[0], power.shape[1]))
2958 2958
2959 2959 startInd = 0
2960 2960 endInd = 0
2961 2961
2962 2962 for i in range(numBlocks): #split por canal
2963 2963 startInd = endInd
2964 2964 endInd = endInd + listPower[i].shape[0]
2965 2965
2966 2966 arrayBlock = listPower[i]
2967 2967 noiseAux = numpy.mean(arrayBlock, 0)
2968 2968 # noiseAux = numpy.median(noiseAux)
2969 2969 # noiseAux = numpy.mean(arrayBlock)
2970 2970 noise[startInd:endInd,:] = noise[startInd:endInd,:] + noiseAux
2971 2971
2972 2972 noiseAux1 = numpy.mean(arrayBlock)
2973 2973 noise1[startInd:endInd,:] = noise1[startInd:endInd,:] + noiseAux1
2974 2974
2975 2975 return noise, noise1
2976 2976
2977 2977 def __findMeteors(self, power, thresh):
2978 2978 nProf = power.shape[0]
2979 2979 nHeights = power.shape[1]
2980 2980 listMeteors = []
2981 2981
2982 2982 for i in range(nHeights):
2983 2983 powerAux = power[:,i]
2984 2984 threshAux = thresh[:,i]
2985 2985
2986 2986 indUPthresh = numpy.where(powerAux > threshAux)[0]
2987 2987 indDNthresh = numpy.where(powerAux <= threshAux)[0]
2988 2988
2989 2989 j = 0
2990 2990
2991 2991 while (j < indUPthresh.size - 2):
2992 2992 if (indUPthresh[j + 2] == indUPthresh[j] + 2):
2993 2993 indDNAux = numpy.where(indDNthresh > indUPthresh[j])
2994 2994 indDNthresh = indDNthresh[indDNAux]
2995 2995
2996 2996 if (indDNthresh.size > 0):
2997 2997 indEnd = indDNthresh[0] - 1
2998 2998 indInit = indUPthresh[j]
2999 2999
3000 3000 meteor = powerAux[indInit:indEnd + 1]
3001 3001 indPeak = meteor.argmax() + indInit
3002 3002 FLA = sum(numpy.conj(meteor)*numpy.hstack((meteor[1:],0)))
3003 3003
3004 3004 listMeteors.append(numpy.array([i,indInit,indPeak,indEnd,FLA])) #CHEQUEAR!!!!!
3005 3005 j = numpy.where(indUPthresh == indEnd)[0] + 1
3006 3006 else: j+=1
3007 3007 else: j+=1
3008 3008
3009 3009 return listMeteors
3010 3010
3011 3011 def __removeMultipleDetections(self,listMeteors, rangeLimit, timeLimit):
3012 3012
3013 3013 arrayMeteors = numpy.asarray(listMeteors)
3014 3014 listMeteors1 = []
3015 3015
3016 3016 while arrayMeteors.shape[0] > 0:
3017 3017 FLAs = arrayMeteors[:,4]
3018 3018 maxFLA = FLAs.argmax()
3019 3019 listMeteors1.append(arrayMeteors[maxFLA,:])
3020 3020
3021 3021 MeteorInitTime = arrayMeteors[maxFLA,1]
3022 3022 MeteorEndTime = arrayMeteors[maxFLA,3]
3023 3023 MeteorHeight = arrayMeteors[maxFLA,0]
3024 3024
3025 3025 #Check neighborhood
3026 3026 maxHeightIndex = MeteorHeight + rangeLimit
3027 3027 minHeightIndex = MeteorHeight - rangeLimit
3028 3028 minTimeIndex = MeteorInitTime - timeLimit
3029 3029 maxTimeIndex = MeteorEndTime + timeLimit
3030 3030
3031 3031 #Check Heights
3032 3032 indHeight = numpy.logical_and(arrayMeteors[:,0] >= minHeightIndex, arrayMeteors[:,0] <= maxHeightIndex)
3033 3033 indTime = numpy.logical_and(arrayMeteors[:,3] >= minTimeIndex, arrayMeteors[:,1] <= maxTimeIndex)
3034 3034 indBoth = numpy.where(numpy.logical_and(indTime,indHeight))
3035 3035
3036 3036 arrayMeteors = numpy.delete(arrayMeteors, indBoth, axis = 0)
3037 3037
3038 3038 return listMeteors1
3039 3039
3040 3040 def __meteorReestimation(self, listMeteors, volts, pairslist, thresh, noise, timeInterval,frequency):
3041 3041 numHeights = volts.shape[2]
3042 3042 nChannel = volts.shape[0]
3043 3043
3044 3044 thresholdPhase = thresh[0]
3045 3045 thresholdNoise = thresh[1]
3046 3046 thresholdDB = float(thresh[2])
3047 3047
3048 3048 thresholdDB1 = 10**(thresholdDB/10)
3049 3049 pairsarray = numpy.array(pairslist)
3050 3050 indSides = pairsarray[:,1]
3051 3051
3052 3052 pairslist1 = list(pairslist)
3053 3053 pairslist1.append((0,1))
3054 3054 pairslist1.append((3,4))
3055 3055
3056 3056 listMeteors1 = []
3057 3057 listPowerSeries = []
3058 3058 listVoltageSeries = []
3059 3059 #volts has the war data
3060 3060
3061 3061 if frequency == 30e6:
3062 3062 timeLag = 45*10**-3
3063 3063 else:
3064 3064 timeLag = 15*10**-3
3065 3065 lag = numpy.ceil(timeLag/timeInterval)
3066 3066
3067 3067 for i in range(len(listMeteors)):
3068 3068
3069 3069 ###################### 3.6 - 3.7 PARAMETERS REESTIMATION #########################
3070 3070 meteorAux = numpy.zeros(16)
3071 3071
3072 3072 #Loading meteor Data (mHeight, mStart, mPeak, mEnd)
3073 3073 mHeight = listMeteors[i][0]
3074 3074 mStart = listMeteors[i][1]
3075 3075 mPeak = listMeteors[i][2]
3076 3076 mEnd = listMeteors[i][3]
3077 3077
3078 3078 #get the volt data between the start and end times of the meteor
3079 3079 meteorVolts = volts[:,mStart:mEnd+1,mHeight]
3080 3080 meteorVolts = meteorVolts.reshape(meteorVolts.shape[0], meteorVolts.shape[1], 1)
3081 3081
3082 3082 #3.6. Phase Difference estimation
3083 3083 phaseDiff, aux = self.__estimatePhaseDifference(meteorVolts, pairslist)
3084 3084
3085 3085 #3.7. Phase difference removal & meteor start, peak and end times reestimated
3086 3086 #meteorVolts0.- all Channels, all Profiles
3087 3087 meteorVolts0 = volts[:,:,mHeight]
3088 3088 meteorThresh = noise[:,mHeight]*thresholdNoise
3089 3089 meteorNoise = noise[:,mHeight]
3090 3090 meteorVolts0[indSides,:] = self.__shiftPhase(meteorVolts0[indSides,:], phaseDiff) #Phase Shifting
3091 3091 powerNet0 = numpy.nansum(numpy.abs(meteorVolts0)**2, axis = 0) #Power
3092 3092
3093 3093 #Times reestimation
3094 3094 mStart1 = numpy.where(powerNet0[:mPeak] < meteorThresh[:mPeak])[0]
3095 3095 if mStart1.size > 0:
3096 3096 mStart1 = mStart1[-1] + 1
3097 3097
3098 3098 else:
3099 3099 mStart1 = mPeak
3100 3100
3101 3101 mEnd1 = numpy.where(powerNet0[mPeak:] < meteorThresh[mPeak:])[0][0] + mPeak - 1
3102 3102 mEndDecayTime1 = numpy.where(powerNet0[mPeak:] < meteorNoise[mPeak:])[0]
3103 3103 if mEndDecayTime1.size == 0:
3104 3104 mEndDecayTime1 = powerNet0.size
3105 3105 else:
3106 3106 mEndDecayTime1 = mEndDecayTime1[0] + mPeak - 1
3107 3107 # mPeak1 = meteorVolts0[mStart1:mEnd1 + 1].argmax()
3108 3108
3109 3109 #meteorVolts1.- all Channels, from start to end
3110 3110 meteorVolts1 = meteorVolts0[:,mStart1:mEnd1 + 1]
3111 3111 meteorVolts2 = meteorVolts0[:,mPeak + lag:mEnd1 + 1]
3112 3112 if meteorVolts2.shape[1] == 0:
3113 3113 meteorVolts2 = meteorVolts0[:,mPeak:mEnd1 + 1]
3114 3114 meteorVolts1 = meteorVolts1.reshape(meteorVolts1.shape[0], meteorVolts1.shape[1], 1)
3115 3115 meteorVolts2 = meteorVolts2.reshape(meteorVolts2.shape[0], meteorVolts2.shape[1], 1)
3116 3116 ##################### END PARAMETERS REESTIMATION #########################
3117 3117
3118 3118 ##################### 3.8 PHASE DIFFERENCE REESTIMATION ########################
3119 3119 # if mEnd1 - mStart1 > 4: #Error Number 6: echo less than 5 samples long; too short for analysis
3120 3120 if meteorVolts2.shape[1] > 0:
3121 3121 #Phase Difference re-estimation
3122 3122 phaseDiff1, phaseDiffint = self.__estimatePhaseDifference(meteorVolts2, pairslist1) #Phase Difference Estimation
3123 3123 # phaseDiff1, phaseDiffint = self.estimatePhaseDifference(meteorVolts2, pairslist)
3124 3124 meteorVolts2 = meteorVolts2.reshape(meteorVolts2.shape[0], meteorVolts2.shape[1])
3125 3125 phaseDiff11 = numpy.reshape(phaseDiff1, (phaseDiff1.shape[0],1))
3126 3126 meteorVolts2[indSides,:] = self.__shiftPhase(meteorVolts2[indSides,:], phaseDiff11[0:4]) #Phase Shifting
3127 3127
3128 3128 #Phase Difference RMS
3129 3129 phaseRMS1 = numpy.sqrt(numpy.mean(numpy.square(phaseDiff1)))
3130 3130 powerNet1 = numpy.nansum(numpy.abs(meteorVolts1[:,:])**2,0)
3131 3131 #Data from Meteor
3132 3132 mPeak1 = powerNet1.argmax() + mStart1
3133 3133 mPeakPower1 = powerNet1.max()
3134 3134 noiseAux = sum(noise[mStart1:mEnd1 + 1,mHeight])
3135 3135 mSNR1 = (sum(powerNet1)-noiseAux)/noiseAux
3136 3136 Meteor1 = numpy.array([mHeight, mStart1, mPeak1, mEnd1, mPeakPower1, mSNR1, phaseRMS1])
3137 3137 Meteor1 = numpy.hstack((Meteor1,phaseDiffint))
3138 3138 PowerSeries = powerNet0[mStart1:mEndDecayTime1 + 1]
3139 3139 #Vectorize
3140 3140 meteorAux[0:7] = [mHeight, mStart1, mPeak1, mEnd1, mPeakPower1, mSNR1, phaseRMS1]
3141 3141 meteorAux[7:11] = phaseDiffint[0:4]
3142 3142
3143 3143 #Rejection Criterions
3144 3144 if phaseRMS1 > thresholdPhase: #Error Number 17: Phase variation
3145 3145 meteorAux[-1] = 17
3146 3146 elif mSNR1 < thresholdDB1: #Error Number 1: SNR < threshold dB
3147 3147 meteorAux[-1] = 1
3148 3148
3149 3149
3150 3150 else:
3151 3151 meteorAux[0:4] = [mHeight, mStart, mPeak, mEnd]
3152 3152 meteorAux[-1] = 6 #Error Number 6: echo less than 5 samples long; too short for analysis
3153 3153 PowerSeries = 0
3154 3154
3155 3155 listMeteors1.append(meteorAux)
3156 3156 listPowerSeries.append(PowerSeries)
3157 3157 listVoltageSeries.append(meteorVolts1)
3158 3158
3159 3159 return listMeteors1, listPowerSeries, listVoltageSeries
3160 3160
3161 3161 def __estimateDecayTime(self, listMeteors, listPower, timeInterval, frequency):
3162 3162
3163 3163 threshError = 10
3164 3164 #Depending if it is 30 or 50 MHz
3165 3165 if frequency == 30e6:
3166 3166 timeLag = 45*10**-3
3167 3167 else:
3168 3168 timeLag = 15*10**-3
3169 3169 lag = numpy.ceil(timeLag/timeInterval)
3170 3170
3171 3171 listMeteors1 = []
3172 3172
3173 3173 for i in range(len(listMeteors)):
3174 3174 meteorPower = listPower[i]
3175 3175 meteorAux = listMeteors[i]
3176 3176
3177 3177 if meteorAux[-1] == 0:
3178 3178
3179 3179 try:
3180 3180 indmax = meteorPower.argmax()
3181 3181 indlag = indmax + lag
3182 3182
3183 3183 y = meteorPower[indlag:]
3184 3184 x = numpy.arange(0, y.size)*timeLag
3185 3185
3186 3186 #first guess
3187 3187 a = y[0]
3188 3188 tau = timeLag
3189 3189 #exponential fit
3190 3190 popt, pcov = optimize.curve_fit(self.__exponential_function, x, y, p0 = [a, tau])
3191 3191 y1 = self.__exponential_function(x, *popt)
3192 3192 #error estimation
3193 3193 error = sum((y - y1)**2)/(numpy.var(y)*(y.size - popt.size))
3194 3194
3195 3195 decayTime = popt[1]
3196 3196 riseTime = indmax*timeInterval
3197 3197 meteorAux[11:13] = [decayTime, error]
3198 3198
3199 3199 #Table items 7, 8 and 11
3200 3200 if (riseTime > 0.3): #Number 7: Echo rise exceeds 0.3s
3201 3201 meteorAux[-1] = 7
3202 3202 elif (decayTime < 2*riseTime) : #Number 8: Echo decay time less than than twice rise time
3203 3203 meteorAux[-1] = 8
3204 3204 if (error > threshError): #Number 11: Poor fit to amplitude for estimation of decay time
3205 3205 meteorAux[-1] = 11
3206 3206
3207 3207
3208 3208 except:
3209 3209 meteorAux[-1] = 11
3210 3210
3211 3211
3212 3212 listMeteors1.append(meteorAux)
3213 3213
3214 3214 return listMeteors1
3215 3215
3216 3216 #Exponential Function
3217 3217
3218 3218 def __exponential_function(self, x, a, tau):
3219 3219 y = a*numpy.exp(-x/tau)
3220 3220 return y
3221 3221
3222 3222 def __getRadialVelocity(self, listMeteors, listVolts, radialStdThresh, pairslist, timeInterval):
3223 3223
3224 3224 pairslist1 = list(pairslist)
3225 3225 pairslist1.append((0,1))
3226 3226 pairslist1.append((3,4))
3227 3227 numPairs = len(pairslist1)
3228 3228 #Time Lag
3229 3229 timeLag = 45*10**-3
3230 3230 c = 3e8
3231 3231 lag = numpy.ceil(timeLag/timeInterval)
3232 3232 freq = 30e6
3233 3233
3234 3234 listMeteors1 = []
3235 3235
3236 3236 for i in range(len(listMeteors)):
3237 3237 meteorAux = listMeteors[i]
3238 3238 if meteorAux[-1] == 0:
3239 3239 mStart = listMeteors[i][1]
3240 3240 mPeak = listMeteors[i][2]
3241 3241 mLag = mPeak - mStart + lag
3242 3242
3243 3243 #get the volt data between the start and end times of the meteor
3244 3244 meteorVolts = listVolts[i]
3245 3245 meteorVolts = meteorVolts.reshape(meteorVolts.shape[0], meteorVolts.shape[1], 1)
3246 3246
3247 3247 #Get CCF
3248 3248 allCCFs = self.__calculateCCF(meteorVolts, pairslist1, [-2,-1,0,1,2])
3249 3249
3250 3250 #Method 2
3251 3251 slopes = numpy.zeros(numPairs)
3252 3252 time = numpy.array([-2,-1,1,2])*timeInterval
3253 3253 angAllCCF = numpy.angle(allCCFs[:,[0,1,3,4],0])
3254 3254
3255 3255 #Correct phases
3256 3256 derPhaseCCF = angAllCCF[:,1:] - angAllCCF[:,0:-1]
3257 3257 indDer = numpy.where(numpy.abs(derPhaseCCF) > numpy.pi)
3258 3258
3259 3259 if indDer[0].shape[0] > 0:
3260 3260 for i in range(indDer[0].shape[0]):
3261 3261 signo = -numpy.sign(derPhaseCCF[indDer[0][i],indDer[1][i]])
3262 3262 angAllCCF[indDer[0][i],indDer[1][i]+1:] += signo*2*numpy.pi
3263 3263
3264 3264 # fit = scipy.stats.linregress(numpy.array([-2,-1,1,2])*timeInterval, numpy.array([phaseLagN2s[i],phaseLagN1s[i],phaseLag1s[i],phaseLag2s[i]]))
3265 3265 for j in range(numPairs):
3266 3266 fit = stats.linregress(time, angAllCCF[j,:])
3267 3267 slopes[j] = fit[0]
3268 3268
3269 3269 #Remove Outlier
3270 3270 # indOut = numpy.argmax(numpy.abs(slopes - numpy.mean(slopes)))
3271 3271 # slopes = numpy.delete(slopes,indOut)
3272 3272 # indOut = numpy.argmax(numpy.abs(slopes - numpy.mean(slopes)))
3273 3273 # slopes = numpy.delete(slopes,indOut)
3274 3274
3275 3275 radialVelocity = -numpy.mean(slopes)*(0.25/numpy.pi)*(c/freq)
3276 3276 radialError = numpy.std(slopes)*(0.25/numpy.pi)*(c/freq)
3277 3277 meteorAux[-2] = radialError
3278 3278 meteorAux[-3] = radialVelocity
3279 3279
3280 3280 #Setting Error
3281 3281 #Number 15: Radial Drift velocity or projected horizontal velocity exceeds 200 m/s
3282 3282 if numpy.abs(radialVelocity) > 200:
3283 3283 meteorAux[-1] = 15
3284 3284 #Number 12: Poor fit to CCF variation for estimation of radial drift velocity
3285 3285 elif radialError > radialStdThresh:
3286 3286 meteorAux[-1] = 12
3287 3287
3288 3288 listMeteors1.append(meteorAux)
3289 3289 return listMeteors1
3290 3290
3291 3291 def __setNewArrays(self, listMeteors, date, heiRang):
3292 3292
3293 3293 #New arrays
3294 3294 arrayMeteors = numpy.array(listMeteors)
3295 3295 arrayParameters = numpy.zeros((len(listMeteors), 13))
3296 3296
3297 3297 #Date inclusion
3298 3298 # date = re.findall(r'\((.*?)\)', date)
3299 3299 # date = date[0].split(',')
3300 3300 # date = map(int, date)
3301 3301 #
3302 3302 # if len(date)<6:
3303 3303 # date.append(0)
3304 3304 #
3305 3305 # date = [date[0]*10000 + date[1]*100 + date[2], date[3]*10000 + date[4]*100 + date[5]]
3306 3306 # arrayDate = numpy.tile(date, (len(listMeteors), 1))
3307 3307 arrayDate = numpy.tile(date, (len(listMeteors)))
3308 3308
3309 3309 #Meteor array
3310 3310 # arrayMeteors[:,0] = heiRang[arrayMeteors[:,0].astype(int)]
3311 3311 # arrayMeteors = numpy.hstack((arrayDate, arrayMeteors))
3312 3312
3313 3313 #Parameters Array
3314 3314 arrayParameters[:,0] = arrayDate #Date
3315 3315 arrayParameters[:,1] = heiRang[arrayMeteors[:,0].astype(int)] #Range
3316 3316 arrayParameters[:,6:8] = arrayMeteors[:,-3:-1] #Radial velocity and its error
3317 3317 arrayParameters[:,8:12] = arrayMeteors[:,7:11] #Phases
3318 3318 arrayParameters[:,-1] = arrayMeteors[:,-1] #Error
3319 3319
3320 3320
3321 3321 return arrayParameters
3322 3322
3323 3323 class CorrectSMPhases(Operation):
3324 3324
3325 3325 def run(self, dataOut, phaseOffsets, hmin = 50, hmax = 150, azimuth = 45, channelPositions = None):
3326 3326
3327 3327 arrayParameters = dataOut.data_param
3328 3328 pairsList = []
3329 3329 pairx = (0,1)
3330 3330 pairy = (2,3)
3331 3331 pairsList.append(pairx)
3332 3332 pairsList.append(pairy)
3333 3333 jph = numpy.zeros(4)
3334 3334
3335 3335 phaseOffsets = numpy.array(phaseOffsets)*numpy.pi/180
3336 3336 # arrayParameters[:,8:12] = numpy.unwrap(arrayParameters[:,8:12] + phaseOffsets)
3337 3337 arrayParameters[:,8:12] = numpy.angle(numpy.exp(1j*(arrayParameters[:,8:12] + phaseOffsets)))
3338 3338
3339 3339 meteorOps = SMOperations()
3340 3340 if channelPositions is None:
3341 3341 # channelPositions = [(2.5,0), (0,2.5), (0,0), (0,4.5), (-2,0)] #T
3342 3342 channelPositions = [(4.5,2), (2,4.5), (2,2), (2,0), (0,2)] #Estrella
3343 3343
3344 3344 pairslist0, distances = meteorOps.getPhasePairs(channelPositions)
3345 3345 h = (hmin,hmax)
3346 3346
3347 3347 arrayParameters = meteorOps.getMeteorParams(arrayParameters, azimuth, h, pairsList, distances, jph)
3348 3348
3349 3349 dataOut.data_param = arrayParameters
3350 3350 return
3351 3351
3352 3352 class SMPhaseCalibration(Operation):
3353 3353
3354 3354 __buffer = None
3355 3355
3356 3356 __initime = None
3357 3357
3358 3358 __dataReady = False
3359 3359
3360 3360 __isConfig = False
3361 3361
3362 3362 def __checkTime(self, currentTime, initTime, paramInterval, outputInterval):
3363 3363
3364 3364 dataTime = currentTime + paramInterval
3365 3365 deltaTime = dataTime - initTime
3366 3366
3367 3367 if deltaTime >= outputInterval or deltaTime < 0:
3368 3368 return True
3369 3369
3370 3370 return False
3371 3371
3372 3372 def __getGammas(self, pairs, d, phases):
3373 3373 gammas = numpy.zeros(2)
3374 3374
3375 3375 for i in range(len(pairs)):
3376 3376
3377 3377 pairi = pairs[i]
3378 3378
3379 3379 phip3 = phases[:,pairi[0]]
3380 3380 d3 = d[pairi[0]]
3381 3381 phip2 = phases[:,pairi[1]]
3382 3382 d2 = d[pairi[1]]
3383 3383 #Calculating gamma
3384 3384 # jdcos = alp1/(k*d1)
3385 3385 # jgamma = numpy.angle(numpy.exp(1j*(d0*alp1/d1 - alp0)))
3386 3386 jgamma = -phip2*d3/d2 - phip3
3387 3387 jgamma = numpy.angle(numpy.exp(1j*jgamma))
3388 3388 # jgamma[jgamma>numpy.pi] -= 2*numpy.pi
3389 3389 # jgamma[jgamma<-numpy.pi] += 2*numpy.pi
3390 3390
3391 3391 #Revised distribution
3392 3392 jgammaArray = numpy.hstack((jgamma,jgamma+0.5*numpy.pi,jgamma-0.5*numpy.pi))
3393 3393
3394 3394 #Histogram
3395 3395 nBins = 64
3396 3396 rmin = -0.5*numpy.pi
3397 3397 rmax = 0.5*numpy.pi
3398 3398 phaseHisto = numpy.histogram(jgammaArray, bins=nBins, range=(rmin,rmax))
3399 3399
3400 3400 meteorsY = phaseHisto[0]
3401 3401 phasesX = phaseHisto[1][:-1]
3402 3402 width = phasesX[1] - phasesX[0]
3403 3403 phasesX += width/2
3404 3404
3405 3405 #Gaussian aproximation
3406 3406 bpeak = meteorsY.argmax()
3407 3407 peak = meteorsY.max()
3408 3408 jmin = bpeak - 5
3409 3409 jmax = bpeak + 5 + 1
3410 3410
3411 3411 if jmin<0:
3412 3412 jmin = 0
3413 3413 jmax = 6
3414 3414 elif jmax > meteorsY.size:
3415 3415 jmin = meteorsY.size - 6
3416 3416 jmax = meteorsY.size
3417 3417
3418 3418 x0 = numpy.array([peak,bpeak,50])
3419 3419 coeff = optimize.leastsq(self.__residualFunction, x0, args=(meteorsY[jmin:jmax], phasesX[jmin:jmax]))
3420 3420
3421 3421 #Gammas
3422 3422 gammas[i] = coeff[0][1]
3423 3423
3424 3424 return gammas
3425 3425
3426 3426 def __residualFunction(self, coeffs, y, t):
3427 3427
3428 3428 return y - self.__gauss_function(t, coeffs)
3429 3429
3430 3430 def __gauss_function(self, t, coeffs):
3431 3431
3432 3432 return coeffs[0]*numpy.exp(-0.5*((t - coeffs[1]) / coeffs[2])**2)
3433 3433
3434 3434 def __getPhases(self, azimuth, h, pairsList, d, gammas, meteorsArray):
3435 3435 meteorOps = SMOperations()
3436 3436 nchan = 4
3437 3437 pairx = pairsList[0] #x es 0
3438 3438 pairy = pairsList[1] #y es 1
3439 3439 center_xangle = 0
3440 3440 center_yangle = 0
3441 3441 range_angle = numpy.array([10*numpy.pi,numpy.pi,numpy.pi/2,numpy.pi/4])
3442 3442 ntimes = len(range_angle)
3443 3443
3444 3444 nstepsx = 20
3445 3445 nstepsy = 20
3446 3446
3447 3447 for iz in range(ntimes):
3448 3448 min_xangle = -range_angle[iz]/2 + center_xangle
3449 3449 max_xangle = range_angle[iz]/2 + center_xangle
3450 3450 min_yangle = -range_angle[iz]/2 + center_yangle
3451 3451 max_yangle = range_angle[iz]/2 + center_yangle
3452 3452
3453 3453 inc_x = (max_xangle-min_xangle)/nstepsx
3454 3454 inc_y = (max_yangle-min_yangle)/nstepsy
3455 3455
3456 3456 alpha_y = numpy.arange(nstepsy)*inc_y + min_yangle
3457 3457 alpha_x = numpy.arange(nstepsx)*inc_x + min_xangle
3458 3458 penalty = numpy.zeros((nstepsx,nstepsy))
3459 3459 jph_array = numpy.zeros((nchan,nstepsx,nstepsy))
3460 3460 jph = numpy.zeros(nchan)
3461 3461
3462 3462 # Iterations looking for the offset
3463 3463 for iy in range(int(nstepsy)):
3464 3464 for ix in range(int(nstepsx)):
3465 3465 d3 = d[pairsList[1][0]]
3466 3466 d2 = d[pairsList[1][1]]
3467 3467 d5 = d[pairsList[0][0]]
3468 3468 d4 = d[pairsList[0][1]]
3469 3469
3470 3470 alp2 = alpha_y[iy] #gamma 1
3471 3471 alp4 = alpha_x[ix] #gamma 0
3472 3472
3473 3473 alp3 = -alp2*d3/d2 - gammas[1]
3474 3474 alp5 = -alp4*d5/d4 - gammas[0]
3475 3475 # jph[pairy[1]] = alpha_y[iy]
3476 3476 # jph[pairy[0]] = -gammas[1] - alpha_y[iy]*d[pairy[1]]/d[pairy[0]]
3477 3477
3478 3478 # jph[pairx[1]] = alpha_x[ix]
3479 3479 # jph[pairx[0]] = -gammas[0] - alpha_x[ix]*d[pairx[1]]/d[pairx[0]]
3480 3480 jph[pairsList[0][1]] = alp4
3481 3481 jph[pairsList[0][0]] = alp5
3482 3482 jph[pairsList[1][0]] = alp3
3483 3483 jph[pairsList[1][1]] = alp2
3484 3484 jph_array[:,ix,iy] = jph
3485 3485 # d = [2.0,2.5,2.5,2.0]
3486 3486 #falta chequear si va a leer bien los meteoros
3487 3487 meteorsArray1 = meteorOps.getMeteorParams(meteorsArray, azimuth, h, pairsList, d, jph)
3488 3488 error = meteorsArray1[:,-1]
3489 3489 ind1 = numpy.where(error==0)[0]
3490 3490 penalty[ix,iy] = ind1.size
3491 3491
3492 3492 i,j = numpy.unravel_index(penalty.argmax(), penalty.shape)
3493 3493 phOffset = jph_array[:,i,j]
3494 3494
3495 3495 center_xangle = phOffset[pairx[1]]
3496 3496 center_yangle = phOffset[pairy[1]]
3497 3497
3498 3498 phOffset = numpy.angle(numpy.exp(1j*jph_array[:,i,j]))
3499 3499 phOffset = phOffset*180/numpy.pi
3500 3500 return phOffset
3501 3501
3502 3502
3503 3503 def run(self, dataOut, hmin, hmax, channelPositions=None, nHours = 1):
3504 3504
3505 3505 dataOut.flagNoData = True
3506 3506 self.__dataReady = False
3507 3507 dataOut.outputInterval = nHours*3600
3508 3508
3509 3509 if self.__isConfig == False:
3510 3510 # self.__initime = dataOut.datatime.replace(minute = 0, second = 0, microsecond = 03)
3511 3511 #Get Initial LTC time
3512 3512 self.__initime = datetime.datetime.utcfromtimestamp(dataOut.utctime)
3513 3513 self.__initime = (self.__initime.replace(minute = 0, second = 0, microsecond = 0) - datetime.datetime(1970, 1, 1)).total_seconds()
3514 3514
3515 3515 self.__isConfig = True
3516 3516
3517 3517 if self.__buffer is None:
3518 3518 self.__buffer = dataOut.data_param.copy()
3519 3519
3520 3520 else:
3521 3521 self.__buffer = numpy.vstack((self.__buffer, dataOut.data_param))
3522 3522
3523 3523 self.__dataReady = self.__checkTime(dataOut.utctime, self.__initime, dataOut.paramInterval, dataOut.outputInterval) #Check if the buffer is ready
3524 3524
3525 3525 if self.__dataReady:
3526 3526 dataOut.utctimeInit = self.__initime
3527 3527 self.__initime += dataOut.outputInterval #to erase time offset
3528 3528
3529 3529 freq = dataOut.frequency
3530 3530 c = dataOut.C #m/s
3531 3531 lamb = c/freq
3532 3532 k = 2*numpy.pi/lamb
3533 3533 azimuth = 0
3534 3534 h = (hmin, hmax)
3535 3535 # pairs = ((0,1),(2,3)) #Estrella
3536 3536 # pairs = ((1,0),(2,3)) #T
3537 3537
3538 3538 if channelPositions is None:
3539 3539 # channelPositions = [(2.5,0), (0,2.5), (0,0), (0,4.5), (-2,0)] #T
3540 3540 channelPositions = [(4.5,2), (2,4.5), (2,2), (2,0), (0,2)] #Estrella
3541 3541 meteorOps = SMOperations()
3542 3542 pairslist0, distances = meteorOps.getPhasePairs(channelPositions)
3543 3543
3544 3544 #Checking correct order of pairs
3545 3545 pairs = []
3546 3546 if distances[1] > distances[0]:
3547 3547 pairs.append((1,0))
3548 3548 else:
3549 3549 pairs.append((0,1))
3550 3550
3551 3551 if distances[3] > distances[2]:
3552 3552 pairs.append((3,2))
3553 3553 else:
3554 3554 pairs.append((2,3))
3555 3555 # distances1 = [-distances[0]*lamb, distances[1]*lamb, -distances[2]*lamb, distances[3]*lamb]
3556 3556
3557 3557 meteorsArray = self.__buffer
3558 3558 error = meteorsArray[:,-1]
3559 3559 boolError = (error==0)|(error==3)|(error==4)|(error==13)|(error==14)
3560 3560 ind1 = numpy.where(boolError)[0]
3561 3561 meteorsArray = meteorsArray[ind1,:]
3562 3562 meteorsArray[:,-1] = 0
3563 3563 phases = meteorsArray[:,8:12]
3564 3564
3565 3565 #Calculate Gammas
3566 3566 gammas = self.__getGammas(pairs, distances, phases)
3567 3567 # gammas = numpy.array([-21.70409463,45.76935864])*numpy.pi/180
3568 3568 #Calculate Phases
3569 3569 phasesOff = self.__getPhases(azimuth, h, pairs, distances, gammas, meteorsArray)
3570 3570 phasesOff = phasesOff.reshape((1,phasesOff.size))
3571 3571 dataOut.data_output = -phasesOff
3572 3572 dataOut.flagNoData = False
3573 3573 self.__buffer = None
3574 3574
3575 3575
3576 3576 return
3577 3577
3578 3578 class SMOperations():
3579 3579
3580 3580 def __init__(self):
3581 3581
3582 3582 return
3583 3583
3584 3584 def getMeteorParams(self, arrayParameters0, azimuth, h, pairsList, distances, jph):
3585 3585
3586 3586 arrayParameters = arrayParameters0.copy()
3587 3587 hmin = h[0]
3588 3588 hmax = h[1]
3589 3589
3590 3590 #Calculate AOA (Error N 3, 4)
3591 3591 #JONES ET AL. 1998
3592 3592 AOAthresh = numpy.pi/8
3593 3593 error = arrayParameters[:,-1]
3594 3594 phases = -arrayParameters[:,8:12] + jph
3595 3595 # phases = numpy.unwrap(phases)
3596 3596 arrayParameters[:,3:6], arrayParameters[:,-1] = self.__getAOA(phases, pairsList, distances, error, AOAthresh, azimuth)
3597 3597
3598 3598 #Calculate Heights (Error N 13 and 14)
3599 3599 error = arrayParameters[:,-1]
3600 3600 Ranges = arrayParameters[:,1]
3601 3601 zenith = arrayParameters[:,4]
3602 3602 arrayParameters[:,2], arrayParameters[:,-1] = self.__getHeights(Ranges, zenith, error, hmin, hmax)
3603 3603
3604 3604 #----------------------- Get Final data ------------------------------------
3605 3605 # error = arrayParameters[:,-1]
3606 3606 # ind1 = numpy.where(error==0)[0]
3607 3607 # arrayParameters = arrayParameters[ind1,:]
3608 3608
3609 3609 return arrayParameters
3610 3610
3611 3611 def __getAOA(self, phases, pairsList, directions, error, AOAthresh, azimuth):
3612 3612
3613 3613 arrayAOA = numpy.zeros((phases.shape[0],3))
3614 3614 cosdir0, cosdir = self.__getDirectionCosines(phases, pairsList,directions)
3615 3615
3616 3616 arrayAOA[:,:2] = self.__calculateAOA(cosdir, azimuth)
3617 3617 cosDirError = numpy.sum(numpy.abs(cosdir0 - cosdir), axis = 1)
3618 3618 arrayAOA[:,2] = cosDirError
3619 3619
3620 3620 azimuthAngle = arrayAOA[:,0]
3621 3621 zenithAngle = arrayAOA[:,1]
3622 3622
3623 3623 #Setting Error
3624 3624 indError = numpy.where(numpy.logical_or(error == 3, error == 4))[0]
3625 3625 error[indError] = 0
3626 3626 #Number 3: AOA not fesible
3627 3627 indInvalid = numpy.where(numpy.logical_and((numpy.logical_or(numpy.isnan(zenithAngle), numpy.isnan(azimuthAngle))),error == 0))[0]
3628 3628 error[indInvalid] = 3
3629 3629 #Number 4: Large difference in AOAs obtained from different antenna baselines
3630 3630 indInvalid = numpy.where(numpy.logical_and(cosDirError > AOAthresh,error == 0))[0]
3631 3631 error[indInvalid] = 4
3632 3632 return arrayAOA, error
3633 3633
3634 3634 def __getDirectionCosines(self, arrayPhase, pairsList, distances):
3635 3635
3636 3636 #Initializing some variables
3637 3637 ang_aux = numpy.array([-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8])*2*numpy.pi
3638 3638 ang_aux = ang_aux.reshape(1,ang_aux.size)
3639 3639
3640 3640 cosdir = numpy.zeros((arrayPhase.shape[0],2))
3641 3641 cosdir0 = numpy.zeros((arrayPhase.shape[0],2))
3642 3642
3643 3643
3644 3644 for i in range(2):
3645 3645 ph0 = arrayPhase[:,pairsList[i][0]]
3646 3646 ph1 = arrayPhase[:,pairsList[i][1]]
3647 3647 d0 = distances[pairsList[i][0]]
3648 3648 d1 = distances[pairsList[i][1]]
3649 3649
3650 3650 ph0_aux = ph0 + ph1
3651 3651 ph0_aux = numpy.angle(numpy.exp(1j*ph0_aux))
3652 3652 # ph0_aux[ph0_aux > numpy.pi] -= 2*numpy.pi
3653 3653 # ph0_aux[ph0_aux < -numpy.pi] += 2*numpy.pi
3654 3654 #First Estimation
3655 3655 cosdir0[:,i] = (ph0_aux)/(2*numpy.pi*(d0 - d1))
3656 3656
3657 3657 #Most-Accurate Second Estimation
3658 3658 phi1_aux = ph0 - ph1
3659 3659 phi1_aux = phi1_aux.reshape(phi1_aux.size,1)
3660 3660 #Direction Cosine 1
3661 3661 cosdir1 = (phi1_aux + ang_aux)/(2*numpy.pi*(d0 + d1))
3662 3662
3663 3663 #Searching the correct Direction Cosine
3664 3664 cosdir0_aux = cosdir0[:,i]
3665 3665 cosdir0_aux = cosdir0_aux.reshape(cosdir0_aux.size,1)
3666 3666 #Minimum Distance
3667 3667 cosDiff = (cosdir1 - cosdir0_aux)**2
3668 3668 indcos = cosDiff.argmin(axis = 1)
3669 3669 #Saving Value obtained
3670 3670 cosdir[:,i] = cosdir1[numpy.arange(len(indcos)),indcos]
3671 3671
3672 3672 return cosdir0, cosdir
3673 3673
3674 3674 def __calculateAOA(self, cosdir, azimuth):
3675 3675 cosdirX = cosdir[:,0]
3676 3676 cosdirY = cosdir[:,1]
3677 3677
3678 3678 zenithAngle = numpy.arccos(numpy.sqrt(1 - cosdirX**2 - cosdirY**2))*180/numpy.pi
3679 3679 azimuthAngle = numpy.arctan2(cosdirX,cosdirY)*180/numpy.pi + azimuth#0 deg north, 90 deg east
3680 3680 angles = numpy.vstack((azimuthAngle, zenithAngle)).transpose()
3681 3681
3682 3682 return angles
3683 3683
3684 3684 def __getHeights(self, Ranges, zenith, error, minHeight, maxHeight):
3685 3685
3686 3686 Ramb = 375 #Ramb = c/(2*PRF)
3687 3687 Re = 6371 #Earth Radius
3688 3688 heights = numpy.zeros(Ranges.shape)
3689 3689
3690 3690 R_aux = numpy.array([0,1,2])*Ramb
3691 3691 R_aux = R_aux.reshape(1,R_aux.size)
3692 3692
3693 3693 Ranges = Ranges.reshape(Ranges.size,1)
3694 3694
3695 3695 Ri = Ranges + R_aux
3696 3696 hi = numpy.sqrt(Re**2 + Ri**2 + (2*Re*numpy.cos(zenith*numpy.pi/180)*Ri.transpose()).transpose()) - Re
3697 3697
3698 3698 #Check if there is a height between 70 and 110 km
3699 3699 h_bool = numpy.sum(numpy.logical_and(hi > minHeight, hi < maxHeight), axis = 1)
3700 3700 ind_h = numpy.where(h_bool == 1)[0]
3701 3701
3702 3702 hCorr = hi[ind_h, :]
3703 3703 ind_hCorr = numpy.where(numpy.logical_and(hi > minHeight, hi < maxHeight))
3704 3704
3705 3705 hCorr = hi[ind_hCorr][:len(ind_h)]
3706 3706 heights[ind_h] = hCorr
3707 3707
3708 3708 #Setting Error
3709 3709 #Number 13: Height unresolvable echo: not valid height within 70 to 110 km
3710 3710 #Number 14: Height ambiguous echo: more than one possible height within 70 to 110 km
3711 3711 indError = numpy.where(numpy.logical_or(error == 13, error == 14))[0]
3712 3712 error[indError] = 0
3713 3713 indInvalid2 = numpy.where(numpy.logical_and(h_bool > 1, error == 0))[0]
3714 3714 error[indInvalid2] = 14
3715 3715 indInvalid1 = numpy.where(numpy.logical_and(h_bool == 0, error == 0))[0]
3716 3716 error[indInvalid1] = 13
3717 3717
3718 3718 return heights, error
3719 3719
3720 3720 def getPhasePairs(self, channelPositions):
3721 3721 chanPos = numpy.array(channelPositions)
3722 3722 listOper = list(itertools.combinations(list(range(5)),2))
3723 3723
3724 3724 distances = numpy.zeros(4)
3725 3725 axisX = []
3726 3726 axisY = []
3727 3727 distX = numpy.zeros(3)
3728 3728 distY = numpy.zeros(3)
3729 3729 ix = 0
3730 3730 iy = 0
3731 3731
3732 3732 pairX = numpy.zeros((2,2))
3733 3733 pairY = numpy.zeros((2,2))
3734 3734
3735 3735 for i in range(len(listOper)):
3736 3736 pairi = listOper[i]
3737 3737
3738 3738 posDif = numpy.abs(chanPos[pairi[0],:] - chanPos[pairi[1],:])
3739 3739
3740 3740 if posDif[0] == 0:
3741 3741 axisY.append(pairi)
3742 3742 distY[iy] = posDif[1]
3743 3743 iy += 1
3744 3744 elif posDif[1] == 0:
3745 3745 axisX.append(pairi)
3746 3746 distX[ix] = posDif[0]
3747 3747 ix += 1
3748 3748
3749 3749 for i in range(2):
3750 3750 if i==0:
3751 3751 dist0 = distX
3752 3752 axis0 = axisX
3753 3753 else:
3754 3754 dist0 = distY
3755 3755 axis0 = axisY
3756 3756
3757 3757 side = numpy.argsort(dist0)[:-1]
3758 3758 axis0 = numpy.array(axis0)[side,:]
3759 3759 chanC = int(numpy.intersect1d(axis0[0,:], axis0[1,:])[0])
3760 3760 axis1 = numpy.unique(numpy.reshape(axis0,4))
3761 3761 side = axis1[axis1 != chanC]
3762 3762 diff1 = chanPos[chanC,i] - chanPos[side[0],i]
3763 3763 diff2 = chanPos[chanC,i] - chanPos[side[1],i]
3764 3764 if diff1<0:
3765 3765 chan2 = side[0]
3766 3766 d2 = numpy.abs(diff1)
3767 3767 chan1 = side[1]
3768 3768 d1 = numpy.abs(diff2)
3769 3769 else:
3770 3770 chan2 = side[1]
3771 3771 d2 = numpy.abs(diff2)
3772 3772 chan1 = side[0]
3773 3773 d1 = numpy.abs(diff1)
3774 3774
3775 3775 if i==0:
3776 3776 chanCX = chanC
3777 3777 chan1X = chan1
3778 3778 chan2X = chan2
3779 3779 distances[0:2] = numpy.array([d1,d2])
3780 3780 else:
3781 3781 chanCY = chanC
3782 3782 chan1Y = chan1
3783 3783 chan2Y = chan2
3784 3784 distances[2:4] = numpy.array([d1,d2])
3785 3785 # axisXsides = numpy.reshape(axisX[ix,:],4)
3786 3786 #
3787 3787 # channelCentX = int(numpy.intersect1d(pairX[0,:], pairX[1,:])[0])
3788 3788 # channelCentY = int(numpy.intersect1d(pairY[0,:], pairY[1,:])[0])
3789 3789 #
3790 3790 # ind25X = numpy.where(pairX[0,:] != channelCentX)[0][0]
3791 3791 # ind20X = numpy.where(pairX[1,:] != channelCentX)[0][0]
3792 3792 # channel25X = int(pairX[0,ind25X])
3793 3793 # channel20X = int(pairX[1,ind20X])
3794 3794 # ind25Y = numpy.where(pairY[0,:] != channelCentY)[0][0]
3795 3795 # ind20Y = numpy.where(pairY[1,:] != channelCentY)[0][0]
3796 3796 # channel25Y = int(pairY[0,ind25Y])
3797 3797 # channel20Y = int(pairY[1,ind20Y])
3798 3798
3799 3799 # pairslist = [(channelCentX, channel25X),(channelCentX, channel20X),(channelCentY,channel25Y),(channelCentY, channel20Y)]
3800 3800 pairslist = [(chanCX, chan1X),(chanCX, chan2X),(chanCY,chan1Y),(chanCY, chan2Y)]
3801 3801
3802 3802 return pairslist, distances
3803 3803 # def __getAOA(self, phases, pairsList, error, AOAthresh, azimuth):
3804 3804 #
3805 3805 # arrayAOA = numpy.zeros((phases.shape[0],3))
3806 3806 # cosdir0, cosdir = self.__getDirectionCosines(phases, pairsList)
3807 3807 #
3808 3808 # arrayAOA[:,:2] = self.__calculateAOA(cosdir, azimuth)
3809 3809 # cosDirError = numpy.sum(numpy.abs(cosdir0 - cosdir), axis = 1)
3810 3810 # arrayAOA[:,2] = cosDirError
3811 3811 #
3812 3812 # azimuthAngle = arrayAOA[:,0]
3813 3813 # zenithAngle = arrayAOA[:,1]
3814 3814 #
3815 3815 # #Setting Error
3816 3816 # #Number 3: AOA not fesible
3817 3817 # indInvalid = numpy.where(numpy.logical_and((numpy.logical_or(numpy.isnan(zenithAngle), numpy.isnan(azimuthAngle))),error == 0))[0]
3818 3818 # error[indInvalid] = 3
3819 3819 # #Number 4: Large difference in AOAs obtained from different antenna baselines
3820 3820 # indInvalid = numpy.where(numpy.logical_and(cosDirError > AOAthresh,error == 0))[0]
3821 3821 # error[indInvalid] = 4
3822 3822 # return arrayAOA, error
3823 3823 #
3824 3824 # def __getDirectionCosines(self, arrayPhase, pairsList):
3825 3825 #
3826 3826 # #Initializing some variables
3827 3827 # ang_aux = numpy.array([-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8])*2*numpy.pi
3828 3828 # ang_aux = ang_aux.reshape(1,ang_aux.size)
3829 3829 #
3830 3830 # cosdir = numpy.zeros((arrayPhase.shape[0],2))
3831 3831 # cosdir0 = numpy.zeros((arrayPhase.shape[0],2))
3832 3832 #
3833 3833 #
3834 3834 # for i in range(2):
3835 3835 # #First Estimation
3836 3836 # phi0_aux = arrayPhase[:,pairsList[i][0]] + arrayPhase[:,pairsList[i][1]]
3837 3837 # #Dealias
3838 3838 # indcsi = numpy.where(phi0_aux > numpy.pi)
3839 3839 # phi0_aux[indcsi] -= 2*numpy.pi
3840 3840 # indcsi = numpy.where(phi0_aux < -numpy.pi)
3841 3841 # phi0_aux[indcsi] += 2*numpy.pi
3842 3842 # #Direction Cosine 0
3843 3843 # cosdir0[:,i] = -(phi0_aux)/(2*numpy.pi*0.5)
3844 3844 #
3845 3845 # #Most-Accurate Second Estimation
3846 3846 # phi1_aux = arrayPhase[:,pairsList[i][0]] - arrayPhase[:,pairsList[i][1]]
3847 3847 # phi1_aux = phi1_aux.reshape(phi1_aux.size,1)
3848 3848 # #Direction Cosine 1
3849 3849 # cosdir1 = -(phi1_aux + ang_aux)/(2*numpy.pi*4.5)
3850 3850 #
3851 3851 # #Searching the correct Direction Cosine
3852 3852 # cosdir0_aux = cosdir0[:,i]
3853 3853 # cosdir0_aux = cosdir0_aux.reshape(cosdir0_aux.size,1)
3854 3854 # #Minimum Distance
3855 3855 # cosDiff = (cosdir1 - cosdir0_aux)**2
3856 3856 # indcos = cosDiff.argmin(axis = 1)
3857 3857 # #Saving Value obtained
3858 3858 # cosdir[:,i] = cosdir1[numpy.arange(len(indcos)),indcos]
3859 3859 #
3860 3860 # return cosdir0, cosdir
3861 3861 #
3862 3862 # def __calculateAOA(self, cosdir, azimuth):
3863 3863 # cosdirX = cosdir[:,0]
3864 3864 # cosdirY = cosdir[:,1]
3865 3865 #
3866 3866 # zenithAngle = numpy.arccos(numpy.sqrt(1 - cosdirX**2 - cosdirY**2))*180/numpy.pi
3867 3867 # azimuthAngle = numpy.arctan2(cosdirX,cosdirY)*180/numpy.pi + azimuth #0 deg north, 90 deg east
3868 3868 # angles = numpy.vstack((azimuthAngle, zenithAngle)).transpose()
3869 3869 #
3870 3870 # return angles
3871 3871 #
3872 3872 # def __getHeights(self, Ranges, zenith, error, minHeight, maxHeight):
3873 3873 #
3874 3874 # Ramb = 375 #Ramb = c/(2*PRF)
3875 3875 # Re = 6371 #Earth Radius
3876 3876 # heights = numpy.zeros(Ranges.shape)
3877 3877 #
3878 3878 # R_aux = numpy.array([0,1,2])*Ramb
3879 3879 # R_aux = R_aux.reshape(1,R_aux.size)
3880 3880 #
3881 3881 # Ranges = Ranges.reshape(Ranges.size,1)
3882 3882 #
3883 3883 # Ri = Ranges + R_aux
3884 3884 # hi = numpy.sqrt(Re**2 + Ri**2 + (2*Re*numpy.cos(zenith*numpy.pi/180)*Ri.transpose()).transpose()) - Re
3885 3885 #
3886 3886 # #Check if there is a height between 70 and 110 km
3887 3887 # h_bool = numpy.sum(numpy.logical_and(hi > minHeight, hi < maxHeight), axis = 1)
3888 3888 # ind_h = numpy.where(h_bool == 1)[0]
3889 3889 #
3890 3890 # hCorr = hi[ind_h, :]
3891 3891 # ind_hCorr = numpy.where(numpy.logical_and(hi > minHeight, hi < maxHeight))
3892 3892 #
3893 3893 # hCorr = hi[ind_hCorr]
3894 3894 # heights[ind_h] = hCorr
3895 3895 #
3896 3896 # #Setting Error
3897 3897 # #Number 13: Height unresolvable echo: not valid height within 70 to 110 km
3898 3898 # #Number 14: Height ambiguous echo: more than one possible height within 70 to 110 km
3899 3899 #
3900 3900 # indInvalid2 = numpy.where(numpy.logical_and(h_bool > 1, error == 0))[0]
3901 3901 # error[indInvalid2] = 14
3902 3902 # indInvalid1 = numpy.where(numpy.logical_and(h_bool == 0, error == 0))[0]
3903 3903 # error[indInvalid1] = 13
3904 3904 #
3905 3905 # return heights, error
3906 3906
3907 3907
3908 3908 class WeatherRadar(Operation):
3909 3909 '''
3910 3910 Function tat implements Weather Radar operations-
3911 3911 Input:
3912 3912 Output:
3913 3913 Parameters affected:
3914 3914 '''
3915 3915 isConfig = False
3916 3916 variableList = None
3917 3917
3918 3918 def __init__(self):
3919 3919 Operation.__init__(self)
3920 3920
3921 3921 def setup(self,dataOut,variableList= None,Pt=0,Gt=0,Gr=0,lambda_=0, aL=0,
3922 3922 tauW= 0,thetaT=0,thetaR=0,Km =0):
3923 3923 self.nCh = dataOut.nChannels
3924 3924 self.nHeis = dataOut.nHeights
3925 3925 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
3926 3926 self.Range = numpy.arange(dataOut.nHeights)*deltaHeight + dataOut.heightList[0]
3927 3927 self.Range = self.Range.reshape(1,self.nHeis)
3928 3928 self.Range = numpy.tile(self.Range,[self.nCh,1])
3929 3929 '''-----------1 Constante del Radar----------'''
3930 3930 self.Pt = Pt
3931 3931 self.Gt = Gt
3932 3932 self.Gr = Gr
3933 3933 self.lambda_ = lambda_
3934 3934 self.aL = aL
3935 3935 self.tauW = tauW
3936 3936 self.thetaT = thetaT
3937 3937 self.thetaR = thetaR
3938 3938 self.Km = Km
3939 3939 Numerator = ((4*numpy.pi)**3 * aL**2 * 16 *numpy.log(2))
3940 3940 Denominator = (Pt * Gt * Gr * lambda_**2 * SPEED_OF_LIGHT * tauW * numpy.pi*thetaT*thetaR)
3941 3941 self.RadarConstant = Numerator/Denominator
3942 3942 self.variableList= variableList
3943 3943
3944 3944 def setMoments(self,dataOut,i):
3945 3945
3946 3946 type = dataOut.inputUnit
3947 3947 nCh = dataOut.nChannels
3948 3948 nHeis = dataOut.nHeights
3949 3949 data_param = numpy.zeros((nCh,4,nHeis))
3950 3950 if type == "Voltage":
3951 3951 factor = dataOut.normFactor
3952 3952 data_param[:,0,:] = dataOut.dataPP_POW/(factor)
3953 3953 data_param[:,1,:] = dataOut.dataPP_DOP
3954 3954 data_param[:,2,:] = dataOut.dataPP_WIDTH
3955 3955 data_param[:,3,:] = dataOut.dataPP_SNR
3956 3956 if type == "Spectra":
3957 3957 data_param[:,0,:] = dataOut.data_POW
3958 3958 data_param[:,1,:] = dataOut.data_DOP
3959 3959 data_param[:,2,:] = dataOut.data_WIDTH
3960 3960 data_param[:,3,:] = dataOut.data_SNR
3961 3961
3962 3962 return data_param[:,i,:]
3963 3963
3964 def getCoeficienteCorrelacionROhv_R(self.dataOut):
3964 def getCoeficienteCorrelacionROhv_R(self,dataOut):
3965 3965 type = dataOut.inputUnit
3966 3966 nHeis = dataOut.nHeights
3967 3967 data_RhoHV_R = numpy.zeros((nHeis))
3968 3968 if type == "Voltage":
3969 3969 powa = dataOut.dataPP_POWER[0]
3970 3970 powb = dataOut.dataPP_POWER[1]
3971 3971 ccf = dataOut.dataPP_CCF
3972 3972 avgcoherenceComplex = ccf / numpy.sqrt(powa * powb)
3973 3973 data_RhoHV_R = numpy.abs(avgcoherenceComplex)
3974 3974 if type == "Spectra":
3975 3975 data_RhoHV_R = dataOut.getCoherence()
3976 3976
3977 3977 return data_RhoHV_R
3978 3978
3979 def getFasediferencialPhiD_P(self.dataOut,phase= True):
3979 def getFasediferencialPhiD_P(self,dataOut,phase= True):
3980 3980 type = dataOut.inputUnit
3981 3981 nHeis = dataOut.nHeights
3982 3982 data_PhiD_P = numpy.zeros((nHeis))
3983 3983 if type == "Voltage":
3984 3984 powa = dataOut.dataPP_POWER[0]
3985 3985 powb = dataOut.dataPP_POWER[1]
3986 3986 ccf = dataOut.dataPP_CCF
3987 3987 avgcoherenceComplex = ccf / numpy.sqrt(powa * powb)
3988 3988 if phase:
3989 3989 data_PhiD_P = numpy.arctan2(avgcoherenceComplex.imag,
3990 3990 avgcoherenceComplex.real) * 180 / numpy.pi
3991 3991 if type == "Spectra":
3992 3992 data_PhiD_P = dataOut.getCoherence(phase = phase)
3993 3993
3994 3994 return data_PhiD_P
3995 3995
3996 3996 def getReflectividad_D(self,dataOut):
3997 3997 '''-----------------------------Potencia de Radar -Signal S-----------------------------'''
3998 3998
3999 3999 Pr = self.setMoments(dataOut,0)
4000 4000
4001 4001 '''-----------2 Reflectividad del Radar y Factor de Reflectividad------'''
4002 4002 self.n_radar = numpy.zeros((self.nCh,self.nHeis))
4003 4003 self.Z_radar = numpy.zeros((self.nCh,self.nHeis))
4004 4004 for R in range(self.nHeis):
4005 4005 self.n_radar[:,R] = self.RadarConstant*Pr[:,R]* (self.Range[:,R])**2
4006 4006
4007 4007 self.Z_radar[:,R] = self.n_radar[:,R]* self.lambda_**4/( numpy.pi**5 * self.Km**2)
4008 4008
4009 4009 '''----------- Factor de Reflectividad Equivalente lamda_ < 10 cm , lamda_= 3.2cm-------'''
4010 4010 Zeh = self.Z_radar
4011 4011 dBZeh = 10*numpy.log10(Zeh)
4012 4012 Zdb_D = dBZeh[0] - dBZeh[1]
4013 4013 return Zdb_D
4014 4014
4015 4015 def getRadialVelocity_V(self,dataOut):
4016 4016 velRadial_V = self.setMoments(dataOut,1)
4017 4017 return velRadial_V
4018 4018
4019 4019 def getAnchoEspectral_W(self,dataOut):
4020 4020 Sigmav_W = self.setMoments(dataOut,2)
4021 4021 return Sigmav_W
4022 4022
4023 4023
4024 4024 def run(self,dataOut,variableList=None,Pt=25,Gt=200.0,Gr=50.0,lambda_=0.32, aL=2.5118,
4025 4025 tauW= 4.0e-6,thetaT=0.165,thetaR=0.367,Km =0.93):
4026 4026
4027 4027 if not self.isConfig:
4028 4028 self.setup(dataOut= dataOut,variableList=None,Pt=25,Gt=200.0,Gr=50.0,lambda_=0.32, aL=2.5118,
4029 4029 tauW= 4.0e-6,thetaT=0.165,thetaR=0.367,Km =0.93)
4030 4030 self.isConfig = True
4031 4031
4032 4032 for i in range(len(self.variableList)):
4033 4033 if self.variableList[i]=='ReflectividadDiferencial':
4034 4034 dataOut.Zdb_D =self.getReflectividad_D(dataOut=dataOut)
4035 4035 if self.variableList[i]=='FaseDiferencial':
4036 4036 dataOut.PhiD_P =self.getFasediferencialPhiD_P(dataOut=dataOut, phase=True)
4037 4037 if self.variableList[i] == "CoeficienteCorrelacion":
4038 4038 dataOut.RhoHV_R = self.getCoeficienteCorrelacionROhv_R(dataOut)
4039 4039 if self.variableList[i] =="VelocidadRadial":
4040 4040 dataOut.velRadial_V = self.getRadialVelocity_V(dataOut)
4041 4041 if self.variableList[i] =="AnchoEspectral":
4042 4042 dataOut.Sigmav_W = self.getAnchoEspectral_W(dataOut)
4043 4043 return dataOut
4044 4044
4045 4045 class PedestalInformation(Operation):
4046 4046 path_ped = None
4047 4047 path_adq = None
4048 4048 t_Interval_p = None
4049 4049 n_Muestras_p = None
4050 4050 isConfig = False
4051 4051 blocksPerfile= None
4052 4052 f_a_p = None
4053 4053 online = None
4054 4054 angulo_adq = None
4055 4055 nro_file = None
4056 4056 nro_key_p = None
4057 4057 tmp = None
4058 4058
4059 4059
4060 4060 def __init__(self):
4061 4061 Operation.__init__(self)
4062 4062
4063 4063
4064 def getAnguloProfile(self,utc_adq,list_pedestal):
4064 def getAnguloProfile(self,utc_adq,utc_ped_list):
4065 4065 utc_adq = utc_adq
4066 list_pedestal = list_pedestal
4067 utc_ped_list = []
4068 for i in range(len(list_pedestal)):
4069 #print(i)# OJO IDENTIFICADOR DE SINCRONISMO
4070 utc_ped_list.append(self.gettimeutcfromDirFilename(path=self.path_ped,file=list_pedestal[i]))
4071
4066 ##list_pedestal = list_pedestal
4067 utc_ped_list = utc_ped_list
4068 #for i in range(len(list_pedestal)):
4069 # #print(i)# OJO IDENTIFICADOR DE SINCRONISMO
4070 # utc_ped_list.append(self.gettimeutcfromDirFilename(path=self.path_ped,file=list_pedestal[i]))
4072 4071 nro_file,utc_ped,utc_ped_1 =self.getNROFile(utc_adq,utc_ped_list)
4073 ###print("NROFILE************************************", nro_file)
4072 #print("NROFILE************************************", nro_file,utc_ped)
4074 4073 if nro_file < 0:
4075 4074 return numpy.NaN,numpy.NaN
4076 4075 else:
4077 4076 nro_key_p = int((utc_adq-utc_ped)/self.t_Interval_p)-1 # ojito al -1 estimado alex
4078 ff_pedestal = list_pedestal[nro_file]
4077 #print("nro_key_p",nro_key_p)
4078 ff_pedestal = self.list_pedestal[nro_file]
4079 4079 #angulo = self.getDatavaluefromDirFilename(path=self.path_ped,file=ff_pedestal,value="azimuth")
4080 4080 angulo = self.getDatavaluefromDirFilename(path=self.path_ped,file=ff_pedestal,value="azi_pos")
4081 4081 angulo_ele = self.getDatavaluefromDirFilename(path=self.path_ped,file=ff_pedestal,value="ele_pos")
4082
4082 #-----Adicion de filtro........................
4083 vel_ele = self.getDatavaluefromDirFilename(path=self.path_ped,file=ff_pedestal,value="ele_vel")
4084 '''
4085 vel_mean = numpy.mean(vel_ele)
4086 print("#############################################################")
4087 print("VEL MEAN----------------:",vel_mean)
4088 f vel_mean<7.7 or vel_mean>8.3:
4089 return numpy.NaN,numpy.NaN
4090 #------------------------------------------------------------------------------------------------------
4091 '''
4083 4092 if 99>=nro_key_p>0:
4084 4093 ##print("angulo_array :",angulo[nro_key_p])
4085 4094 return angulo[nro_key_p],angulo_ele[nro_key_p]
4086 4095 else:
4087 4096 #print("-----------------------------------------------------------------")
4088 4097 return numpy.NaN,numpy.NaN
4089 4098
4090 4099
4091 4100 def getfirstFilefromPath(self,path,meta,ext):
4092 4101 validFilelist = []
4093 4102 #print("SEARH",path)
4094 4103 try:
4095 4104 fileList = os.listdir(path)
4096 4105 except:
4097 4106 print("check path - fileList")
4098 4107 if len(fileList)<1:
4099 4108 return None
4100 4109 # meta 1234 567 8-18 BCDE
4101 4110 # H,D,PE YYYY DDD EPOC .ext
4102 4111
4103 4112 for thisFile in fileList:
4104 4113 #print("HI",thisFile)
4105 4114 if meta =="PE":
4106 4115 try:
4107 4116 number= int(thisFile[len(meta)+7:len(meta)+17])
4108 4117 except:
4109 4118 print("There is a file or folder with different format")
4110 4119 if meta == "D":
4111 4120 try:
4112 4121 number= int(thisFile[8:11])
4113 4122 except:
4114 4123 print("There is a file or folder with different format")
4115 4124
4116 4125 if not isNumber(str=number):
4117 4126 continue
4118 4127 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
4119 4128 continue
4120 4129 validFilelist.sort()
4121 4130 validFilelist.append(thisFile)
4122 4131 if len(validFilelist)>0:
4123 4132 validFilelist = sorted(validFilelist,key=str.lower)
4124 4133 return validFilelist
4125 4134 return None
4126 4135
4127 4136 def gettimeutcfromDirFilename(self,path,file):
4128 4137 dir_file= path+"/"+file
4129 4138 fp = h5py.File(dir_file,'r')
4130 4139 #epoc = fp['Metadata'].get('utctimeInit')[()]
4131 4140 epoc = fp['Data'].get('utc')[()]
4132 4141 fp.close()
4133 4142 return epoc
4134 4143
4135 4144 def gettimeutcadqfromDirFilename(self,path,file):
4136 4145 pass
4137 4146
4138 4147 def getDatavaluefromDirFilename(self,path,file,value):
4139 4148 dir_file= path+"/"+file
4140 4149 fp = h5py.File(dir_file,'r')
4141 4150 array = fp['Data'].get(value)[()]
4142 4151 fp.close()
4143 4152 return array
4144 4153
4145 4154
4146 4155 def getNROFile(self,utc_adq,utc_ped_list):
4147 4156 c=0
4148 4157 #print(utc_adq)
4149 4158 #print(len(utc_ped_list))
4150 4159 ###print(utc_ped_list)
4151 for i in range(len(utc_ped_list)):
4152 if utc_adq>utc_ped_list[i]:
4153 #print("mayor")
4154 #print("utc_ped_list",utc_ped_list[i])
4155 c +=1
4160 if utc_adq<utc_ped_list[0]:
4161 pass
4162 else:
4163 for i in range(len(utc_ped_list)):
4164 if utc_adq>utc_ped_list[i]:
4165 #print("mayor")
4166 #print("utc_ped_list",utc_ped_list[i])
4167 c +=1
4156 4168
4157 4169 return c-1,utc_ped_list[c-1],utc_ped_list[c]
4158 4170
4159 4171 def verificarNROFILE(self,dataOut,utc_ped,f_a_p,n_Muestras_p):
4160 4172 pass
4161 4173
4162 4174 def setup_offline(self,dataOut,list_pedestal):
4163 4175 pass
4164 4176
4165 4177 def setup_online(self,dataOut):
4166 4178 pass
4167 4179
4168 4180 #def setup(self,dataOut,path_ped,path_adq,t_Interval_p,n_Muestras_p,blocksPerfile,f_a_p,online):
4169 4181 def setup(self,dataOut,path_ped,t_Interval_p,wr_exp):
4170 4182 self.__dataReady = False
4171 4183 self.path_ped = path_ped
4172 4184 self.t_Interval_p = t_Interval_p
4173 4185 self.list_pedestal = self.getfirstFilefromPath(path=self.path_ped,meta="PE",ext=".hdf5")
4186 self.utc_ped_list= []
4187 for i in range(len(self.list_pedestal)):
4188 #print(i)# OJO IDENTIFICADOR DE SINCRONISMO
4189 self.utc_ped_list.append(self.gettimeutcfromDirFilename(path=self.path_ped,file=self.list_pedestal[i]))
4174 4190 dataOut.wr_exp = wr_exp
4191 print("SETUP READY")
4175 4192
4176 4193
4177 4194 def setNextFileP(self,dataOut):
4178 4195 pass
4179 4196
4180 4197 def checkPedFile(self,path,nro_file):
4181 4198 pass
4182 4199
4183 4200 def setNextFileoffline(self,dataOut):
4184 4201 pass
4185 4202
4186 4203 def setNextFileonline(self):
4187 4204 pass
4188 4205
4189 4206 def run(self, dataOut,path_ped,t_Interval_p,wr_exp):
4190 4207 ###print("INTEGRATION -----")
4191 4208 if not self.isConfig:
4192 4209 self.setup(dataOut, path_ped,t_Interval_p,wr_exp)
4193 4210 self.__dataReady = True
4194 4211 self.isConfig = True
4195 4212 #print("config TRUE")
4196 4213 utc_adq = dataOut.utctime
4197 ###print("utc_adq---------------",utc_adq)
4214 #print("utc_adq---------------",utc_adq)
4198 4215
4199 4216 list_pedestal = self.list_pedestal
4200 #print("list_pedestal",list_pedestal)
4201 angulo,angulo_ele = self.getAnguloProfile(utc_adq=utc_adq,list_pedestal=list_pedestal)
4202 ###print("angulo**********",angulo)
4217 #print("list_pedestal",list_pedestal[:20])
4218 angulo,angulo_ele = self.getAnguloProfile(utc_adq=utc_adq,utc_ped_list=self.utc_ped_list)
4219 #print("angulo**********",angulo)
4203 4220 dataOut.flagNoData = False
4204 4221 if numpy.isnan(angulo) or numpy.isnan(angulo_ele) :
4205 4222 dataOut.flagNoData = True
4206 4223 return dataOut
4207 4224 dataOut.azimuth = angulo
4208 4225 dataOut.elevation = angulo_ele
4209 4226 return dataOut
4210 4227
4211 4228 class Block360(Operation):
4212 4229 '''
4213 4230 '''
4214 4231 isConfig = False
4215 4232 __profIndex = 0
4216 4233 __initime = None
4217 4234 __lastdatatime = None
4218 4235 __buffer = None
4219 4236 __dataReady = False
4220 4237 n = None
4221 4238 __nch = 0
4222 4239 __nHeis = 0
4223 4240 index = 0
4224 4241 mode = 0
4225 4242
4226 4243 def __init__(self,**kwargs):
4227 4244 Operation.__init__(self,**kwargs)
4228 4245
4229 4246 def setup(self, dataOut, n = None, mode = None):
4230 4247 '''
4231 4248 n= Numero de PRF's de entrada
4232 4249 '''
4233 4250 self.__initime = None
4234 4251 self.__lastdatatime = 0
4235 4252 self.__dataReady = False
4236 4253 self.__buffer = 0
4237 4254 self.__buffer_1D = 0
4238 4255 self.__profIndex = 0
4239 4256 self.index = 0
4240 4257 self.__nch = dataOut.nChannels
4241 4258 self.__nHeis = dataOut.nHeights
4242 4259 ##print("ELVALOR DE n es:", n)
4243 4260 if n == None:
4244 4261 raise ValueError("n should be specified.")
4245 4262
4246 4263 if mode == None:
4247 4264 raise ValueError("mode should be specified.")
4248 4265
4249 4266 if n != None:
4250 4267 if n<1:
4251 4268 print("n should be greater than 2")
4252 4269 raise ValueError("n should be greater than 2")
4253 4270
4254 4271 self.n = n
4255 4272 self.mode = mode
4256 4273 #print("self.mode",self.mode)
4257 4274 #print("nHeights")
4258 4275 self.__buffer = numpy.zeros(( dataOut.nChannels,n, dataOut.nHeights))
4259 4276 self.__buffer2 = numpy.zeros(n)
4260 4277 self.__buffer3 = numpy.zeros(n)
4261 4278
4262 4279
4263 4280
4264 4281
4265 4282 def putData(self,data,mode):
4266 4283 '''
4267 4284 Add a profile to he __buffer and increase in one the __profiel Index
4268 4285 '''
4269 4286 #print("line 4049",data.dataPP_POW.shape,data.dataPP_POW[:10])
4270 4287 #print("line 4049",data.azimuth.shape,data.azimuth)
4271 4288 if self.mode==0:
4272 4289 self.__buffer[:,self.__profIndex,:]= data.dataPP_POWER# PRIMER MOMENTO
4273 4290 if self.mode==1:
4274 4291 self.__buffer[:,self.__profIndex,:]= data.data_pow
4275 4292 #print("me casi",self.index,data.azimuth[self.index])
4276 4293 #print(self.__profIndex, self.index , data.azimuth[self.index] )
4277 4294 #print("magic",data.profileIndex)
4278 4295 #print(data.azimuth[self.index])
4279 4296 #print("index",self.index)
4280 4297
4281 4298 #####self.__buffer2[self.__profIndex] = data.azimuth[self.index]
4282 4299 self.__buffer2[self.__profIndex] = data.azimuth
4283 4300 self.__buffer3[self.__profIndex] = data.elevation
4284 4301 #print("q pasa")
4285 4302 #####self.index+=1
4286 4303 #print("index",self.index,data.azimuth[:10])
4287 4304 self.__profIndex += 1
4288 4305 return #Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β· Remove DCΒ·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·
4289 4306
4290 4307 def pushData(self,data):
4291 4308 '''
4292 4309 Return the PULSEPAIR and the profiles used in the operation
4293 4310 Affected : self.__profileIndex
4294 4311 '''
4295 4312 #print("pushData")
4296 4313
4297 4314 data_360 = self.__buffer
4298 4315 data_p = self.__buffer2
4299 4316 data_e = self.__buffer3
4300 4317 n = self.__profIndex
4301 4318
4302 4319 self.__buffer = numpy.zeros((self.__nch, self.n,self.__nHeis))
4303 4320 self.__buffer2 = numpy.zeros(self.n)
4304 4321 self.__buffer3 = numpy.zeros(self.n)
4305 4322 self.__profIndex = 0
4306 4323 #print("pushData")
4307 4324 return data_360,n,data_p,data_e
4308 4325
4309 4326
4310 4327 def byProfiles(self,dataOut):
4311 4328
4312 4329 self.__dataReady = False
4313 4330 data_360 = None
4314 4331 data_p = None
4315 4332 data_e = None
4316 4333 #print("dataOu",dataOut.dataPP_POW)
4317 4334 self.putData(data=dataOut,mode = self.mode)
4318 4335 ##### print("profIndex",self.__profIndex)
4319 4336 if self.__profIndex == self.n:
4320 4337 data_360,n,data_p,data_e = self.pushData(data=dataOut)
4321 4338 self.__dataReady = True
4322 4339
4323 4340 return data_360,data_p,data_e
4324 4341
4325 4342
4326 4343 def blockOp(self, dataOut, datatime= None):
4327 4344 if self.__initime == None:
4328 4345 self.__initime = datatime
4329 4346 data_360,data_p,data_e = self.byProfiles(dataOut)
4330 4347 self.__lastdatatime = datatime
4331 4348
4332 4349 if data_360 is None:
4333 4350 return None, None,None,None
4334 4351
4335 4352
4336 4353 avgdatatime = self.__initime
4337 4354 if self.n==1:
4338 4355 avgdatatime = datatime
4339 4356 deltatime = datatime - self.__lastdatatime
4340 4357 self.__initime = datatime
4341 4358 #print(data_360.shape,avgdatatime,data_p.shape)
4342 4359 return data_360,avgdatatime,data_p,data_e
4343 4360
4344 4361 def run(self, dataOut,n = None,mode=None,**kwargs):
4345 4362 #print("BLOCK 360 HERE WE GO MOMENTOS")
4346 4363 if not self.isConfig:
4347 4364 self.setup(dataOut = dataOut, n = n ,mode= mode ,**kwargs)
4348 4365 ####self.index = 0
4349 4366 #print("comova",self.isConfig)
4350 4367 self.isConfig = True
4351 4368 ####if self.index==dataOut.azimuth.shape[0]:
4352 4369 #### self.index=0
4353 4370 data_360, avgdatatime,data_p,data_e = self.blockOp(dataOut, dataOut.utctime)
4354 4371 dataOut.flagNoData = True
4355 4372
4356 4373 if self.__dataReady:
4357 4374 dataOut.data_360 = data_360 # S
4358 4375 ##print("---------------------------------------------------------------------------------")
4359 #####print("---------------------------DATAREADY---------------------------------------------")
4376 print("---------------------------DATAREADY---------------------------------------------")
4360 4377 ##print("---------------------------------------------------------------------------------")
4361 4378 ##print("data_360",dataOut.data_360.shape)
4362 4379 dataOut.data_azi = data_p
4363 4380 dataOut.data_ele = data_e
4364 4381 #####print("azi: ",dataOut.data_azi)
4382 print("ele: ",dataOut.data_ele)
4365 4383 #print("jroproc_parameters",data_p[0],data_p[-1])#,data_360.shape,avgdatatime)
4366 4384 dataOut.utctime = avgdatatime
4367 4385 dataOut.flagNoData = False
4368 4386 return dataOut
@@ -1,1633 +1,1633
1 1 import sys
2 2 import numpy,math
3 3 from scipy import interpolate
4 4 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
5 5 from schainpy.model.data.jrodata import Voltage,hildebrand_sekhon
6 6 from schainpy.utils import log
7 7 from time import time
8 8
9 9
10 10
11 11 class VoltageProc(ProcessingUnit):
12 12
13 13 def __init__(self):
14 14
15 15 ProcessingUnit.__init__(self)
16 16
17 17 self.dataOut = Voltage()
18 18 self.flip = 1
19 19 self.setupReq = False
20 20
21 21 def run(self):
22 22
23 23 if self.dataIn.type == 'AMISR':
24 24 self.__updateObjFromAmisrInput()
25 25
26 26 if self.dataIn.type == 'Voltage':
27 27 self.dataOut.copy(self.dataIn)
28 28
29 29 def __updateObjFromAmisrInput(self):
30 30
31 31 self.dataOut.timeZone = self.dataIn.timeZone
32 32 self.dataOut.dstFlag = self.dataIn.dstFlag
33 33 self.dataOut.errorCount = self.dataIn.errorCount
34 34 self.dataOut.useLocalTime = self.dataIn.useLocalTime
35 35
36 36 self.dataOut.flagNoData = self.dataIn.flagNoData
37 37 self.dataOut.data = self.dataIn.data
38 38 self.dataOut.utctime = self.dataIn.utctime
39 39 self.dataOut.channelList = self.dataIn.channelList
40 40 #self.dataOut.timeInterval = self.dataIn.timeInterval
41 41 self.dataOut.heightList = self.dataIn.heightList
42 42 self.dataOut.nProfiles = self.dataIn.nProfiles
43 43
44 44 self.dataOut.nCohInt = self.dataIn.nCohInt
45 45 self.dataOut.ippSeconds = self.dataIn.ippSeconds
46 46 self.dataOut.frequency = self.dataIn.frequency
47 47
48 48 self.dataOut.azimuth = self.dataIn.azimuth
49 49 self.dataOut.zenith = self.dataIn.zenith
50 50
51 51 self.dataOut.beam.codeList = self.dataIn.beam.codeList
52 52 self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList
53 53 self.dataOut.beam.zenithList = self.dataIn.beam.zenithList
54 54
55 55
56 56 class selectChannels(Operation):
57 57
58 58 def run(self, dataOut, channelList):
59 59
60 60 channelIndexList = []
61 61 self.dataOut = dataOut
62 62 for channel in channelList:
63 63 if channel not in self.dataOut.channelList:
64 64 raise ValueError("Channel %d is not in %s" %(channel, str(self.dataOut.channelList)))
65 65
66 66 index = self.dataOut.channelList.index(channel)
67 67 channelIndexList.append(index)
68 68 self.selectChannelsByIndex(channelIndexList)
69 69 return self.dataOut
70 70
71 71 def selectChannelsByIndex(self, channelIndexList):
72 72 """
73 73 Selecciona un bloque de datos en base a canales segun el channelIndexList
74 74
75 75 Input:
76 76 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
77 77
78 78 Affected:
79 79 self.dataOut.data
80 80 self.dataOut.channelIndexList
81 81 self.dataOut.nChannels
82 82 self.dataOut.m_ProcessingHeader.totalSpectra
83 83 self.dataOut.systemHeaderObj.numChannels
84 84 self.dataOut.m_ProcessingHeader.blockSize
85 85
86 86 Return:
87 87 None
88 88 """
89 89
90 90 for channelIndex in channelIndexList:
91 91 if channelIndex not in self.dataOut.channelIndexList:
92 92 raise ValueError("The value %d in channelIndexList is not valid" %channelIndex)
93 93
94 94 if self.dataOut.type == 'Voltage':
95 95 if self.dataOut.flagDataAsBlock:
96 96 """
97 97 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
98 98 """
99 99 data = self.dataOut.data[channelIndexList,:,:]
100 100 else:
101 101 data = self.dataOut.data[channelIndexList,:]
102 102
103 103 self.dataOut.data = data
104 104 # self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
105 105 self.dataOut.channelList = range(len(channelIndexList))
106 106
107 107 elif self.dataOut.type == 'Spectra':
108 108 data_spc = self.dataOut.data_spc[channelIndexList, :]
109 109 data_dc = self.dataOut.data_dc[channelIndexList, :]
110 110
111 111 self.dataOut.data_spc = data_spc
112 112 self.dataOut.data_dc = data_dc
113 113
114 114 # self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
115 115 self.dataOut.channelList = range(len(channelIndexList))
116 116 self.__selectPairsByChannel(channelIndexList)
117 117
118 118 return 1
119 119
120 120 def __selectPairsByChannel(self, channelList=None):
121 121
122 122 if channelList == None:
123 123 return
124 124
125 125 pairsIndexListSelected = []
126 126 for pairIndex in self.dataOut.pairsIndexList:
127 127 # First pair
128 128 if self.dataOut.pairsList[pairIndex][0] not in channelList:
129 129 continue
130 130 # Second pair
131 131 if self.dataOut.pairsList[pairIndex][1] not in channelList:
132 132 continue
133 133
134 134 pairsIndexListSelected.append(pairIndex)
135 135
136 136 if not pairsIndexListSelected:
137 137 self.dataOut.data_cspc = None
138 138 self.dataOut.pairsList = []
139 139 return
140 140
141 141 self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndexListSelected]
142 142 self.dataOut.pairsList = [self.dataOut.pairsList[i]
143 143 for i in pairsIndexListSelected]
144 144
145 145 return
146 146
147 147 class selectHeights(Operation):
148 148
149 149 def run(self, dataOut, minHei=None, maxHei=None, minIndex=None, maxIndex=None):
150 150 """
151 151 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
152 152 minHei <= height <= maxHei
153 153
154 154 Input:
155 155 minHei : valor minimo de altura a considerar
156 156 maxHei : valor maximo de altura a considerar
157 157
158 158 Affected:
159 159 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
160 160
161 161 Return:
162 162 1 si el metodo se ejecuto con exito caso contrario devuelve 0
163 163 """
164 164
165 165 self.dataOut = dataOut
166 166
167 167 if minHei and maxHei:
168 168
169 169 if (minHei < self.dataOut.heightList[0]):
170 170 minHei = self.dataOut.heightList[0]
171 171
172 172 if (maxHei > self.dataOut.heightList[-1]):
173 173 maxHei = self.dataOut.heightList[-1]
174 174
175 175 minIndex = 0
176 176 maxIndex = 0
177 177 heights = self.dataOut.heightList
178 178
179 179 inda = numpy.where(heights >= minHei)
180 180 indb = numpy.where(heights <= maxHei)
181 181
182 182 try:
183 183 minIndex = inda[0][0]
184 184 except:
185 185 minIndex = 0
186 186
187 187 try:
188 188 maxIndex = indb[0][-1]
189 189 except:
190 190 maxIndex = len(heights)
191 191
192 192 self.selectHeightsByIndex(minIndex, maxIndex)
193 193
194 194 return self.dataOut
195 195
196 196 def selectHeightsByIndex(self, minIndex, maxIndex):
197 197 """
198 198 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
199 199 minIndex <= index <= maxIndex
200 200
201 201 Input:
202 202 minIndex : valor de indice minimo de altura a considerar
203 203 maxIndex : valor de indice maximo de altura a considerar
204 204
205 205 Affected:
206 206 self.dataOut.data
207 207 self.dataOut.heightList
208 208
209 209 Return:
210 210 1 si el metodo se ejecuto con exito caso contrario devuelve 0
211 211 """
212 212
213 213 if self.dataOut.type == 'Voltage':
214 214 if (minIndex < 0) or (minIndex > maxIndex):
215 215 raise ValueError("Height index range (%d,%d) is not valid" % (minIndex, maxIndex))
216 216
217 217 if (maxIndex >= self.dataOut.nHeights):
218 218 maxIndex = self.dataOut.nHeights
219 219
220 220 #voltage
221 221 if self.dataOut.flagDataAsBlock:
222 222 """
223 223 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
224 224 """
225 225 data = self.dataOut.data[:,:, minIndex:maxIndex]
226 226 else:
227 227 data = self.dataOut.data[:, minIndex:maxIndex]
228 228
229 229 # firstHeight = self.dataOut.heightList[minIndex]
230 230
231 231 self.dataOut.data = data
232 232 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex]
233 233
234 234 if self.dataOut.nHeights <= 1:
235 235 raise ValueError("selectHeights: Too few heights. Current number of heights is %d" %(self.dataOut.nHeights))
236 236 elif self.dataOut.type == 'Spectra':
237 237 if (minIndex < 0) or (minIndex > maxIndex):
238 238 raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % (
239 239 minIndex, maxIndex))
240 240
241 241 if (maxIndex >= self.dataOut.nHeights):
242 242 maxIndex = self.dataOut.nHeights - 1
243 243
244 244 # Spectra
245 245 data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1]
246 246
247 247 data_cspc = None
248 248 if self.dataOut.data_cspc is not None:
249 249 data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1]
250 250
251 251 data_dc = None
252 252 if self.dataOut.data_dc is not None:
253 253 data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1]
254 254
255 255 self.dataOut.data_spc = data_spc
256 256 self.dataOut.data_cspc = data_cspc
257 257 self.dataOut.data_dc = data_dc
258 258
259 259 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex + 1]
260 260
261 261 return 1
262 262
263 263
264 264 class filterByHeights(Operation):
265 265
266 266 def run(self, dataOut, window):
267 267
268 268 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
269 269
270 270 if window == None:
271 271 window = (dataOut.radarControllerHeaderObj.txA/dataOut.radarControllerHeaderObj.nBaud) / deltaHeight
272 272
273 273 newdelta = deltaHeight * window
274 274 r = dataOut.nHeights % window
275 275 newheights = (dataOut.nHeights-r)/window
276 276
277 277 if newheights <= 1:
278 278 raise ValueError("filterByHeights: Too few heights. Current number of heights is %d and window is %d" %(dataOut.nHeights, window))
279 279
280 280 if dataOut.flagDataAsBlock:
281 281 """
282 282 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
283 283 """
284 284 buffer = dataOut.data[:, :, 0:int(dataOut.nHeights-r)]
285 285 buffer = buffer.reshape(dataOut.nChannels, dataOut.nProfiles, int(dataOut.nHeights/window), window)
286 286 buffer = numpy.sum(buffer,3)
287 287
288 288 else:
289 289 buffer = dataOut.data[:,0:int(dataOut.nHeights-r)]
290 290 buffer = buffer.reshape(dataOut.nChannels,int(dataOut.nHeights/window),int(window))
291 291 buffer = numpy.sum(buffer,2)
292 292
293 293 dataOut.data = buffer
294 294 dataOut.heightList = dataOut.heightList[0] + numpy.arange( newheights )*newdelta
295 295 dataOut.windowOfFilter = window
296 296
297 297 return dataOut
298 298
299 299
300 300 class setH0(Operation):
301 301
302 302 def run(self, dataOut, h0, deltaHeight = None):
303 303
304 304 if not deltaHeight:
305 305 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
306 306
307 307 nHeights = dataOut.nHeights
308 308
309 309 newHeiRange = h0 + numpy.arange(nHeights)*deltaHeight
310 310
311 311 dataOut.heightList = newHeiRange
312 312
313 313 return dataOut
314 314
315 315
316 316 class deFlip(Operation):
317 317
318 318 def run(self, dataOut, channelList = []):
319 319
320 320 data = dataOut.data.copy()
321 321
322 322 if dataOut.flagDataAsBlock:
323 323 flip = self.flip
324 324 profileList = list(range(dataOut.nProfiles))
325 325
326 326 if not channelList:
327 327 for thisProfile in profileList:
328 328 data[:,thisProfile,:] = data[:,thisProfile,:]*flip
329 329 flip *= -1.0
330 330 else:
331 331 for thisChannel in channelList:
332 332 if thisChannel not in dataOut.channelList:
333 333 continue
334 334
335 335 for thisProfile in profileList:
336 336 data[thisChannel,thisProfile,:] = data[thisChannel,thisProfile,:]*flip
337 337 flip *= -1.0
338 338
339 339 self.flip = flip
340 340
341 341 else:
342 342 if not channelList:
343 343 data[:,:] = data[:,:]*self.flip
344 344 else:
345 345 for thisChannel in channelList:
346 346 if thisChannel not in dataOut.channelList:
347 347 continue
348 348
349 349 data[thisChannel,:] = data[thisChannel,:]*self.flip
350 350
351 351 self.flip *= -1.
352 352
353 353 dataOut.data = data
354 354
355 355 return dataOut
356 356
357 357
358 358 class setAttribute(Operation):
359 359 '''
360 360 Set an arbitrary attribute(s) to dataOut
361 361 '''
362 362
363 363 def __init__(self):
364 364
365 365 Operation.__init__(self)
366 366 self._ready = False
367 367
368 368 def run(self, dataOut, **kwargs):
369 369
370 370 for key, value in kwargs.items():
371 371 setattr(dataOut, key, value)
372 372
373 373 return dataOut
374 374
375 375
376 376 @MPDecorator
377 377 class printAttribute(Operation):
378 378 '''
379 379 Print an arbitrary attribute of dataOut
380 380 '''
381 381
382 382 def __init__(self):
383 383
384 384 Operation.__init__(self)
385 385
386 386 def run(self, dataOut, attributes):
387 387
388 388 if isinstance(attributes, str):
389 389 attributes = [attributes]
390 390 for attr in attributes:
391 391 if hasattr(dataOut, attr):
392 392 log.log(getattr(dataOut, attr), attr)
393 393
394 394
395 395 class interpolateHeights(Operation):
396 396
397 397 def run(self, dataOut, topLim, botLim):
398 398 #69 al 72 para julia
399 399 #82-84 para meteoros
400 400 if len(numpy.shape(dataOut.data))==2:
401 401 sampInterp = (dataOut.data[:,botLim-1] + dataOut.data[:,topLim+1])/2
402 402 sampInterp = numpy.transpose(numpy.tile(sampInterp,(topLim-botLim + 1,1)))
403 403 #dataOut.data[:,botLim:limSup+1] = sampInterp
404 404 dataOut.data[:,botLim:topLim+1] = sampInterp
405 405 else:
406 406 nHeights = dataOut.data.shape[2]
407 407 x = numpy.hstack((numpy.arange(botLim),numpy.arange(topLim+1,nHeights)))
408 408 y = dataOut.data[:,:,list(range(botLim))+list(range(topLim+1,nHeights))]
409 409 f = interpolate.interp1d(x, y, axis = 2)
410 410 xnew = numpy.arange(botLim,topLim+1)
411 411 ynew = f(xnew)
412 412 dataOut.data[:,:,botLim:topLim+1] = ynew
413 413
414 414 return dataOut
415 415
416 416
417 417 class CohInt(Operation):
418 418
419 419 isConfig = False
420 420 __profIndex = 0
421 421 __byTime = False
422 422 __initime = None
423 423 __lastdatatime = None
424 424 __integrationtime = None
425 425 __buffer = None
426 426 __bufferStride = []
427 427 __dataReady = False
428 428 __profIndexStride = 0
429 429 __dataToPutStride = False
430 430 n = None
431 431
432 432 def __init__(self, **kwargs):
433 433
434 434 Operation.__init__(self, **kwargs)
435 435
436 436 def setup(self, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False):
437 437 """
438 438 Set the parameters of the integration class.
439 439
440 440 Inputs:
441 441
442 442 n : Number of coherent integrations
443 443 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
444 444 overlapping :
445 445 """
446 446
447 447 self.__initime = None
448 448 self.__lastdatatime = 0
449 449 self.__buffer = None
450 450 self.__dataReady = False
451 451 self.byblock = byblock
452 452 self.stride = stride
453 453
454 454 if n == None and timeInterval == None:
455 455 raise ValueError("n or timeInterval should be specified ...")
456 456
457 457 if n != None:
458 458 self.n = n
459 459 self.__byTime = False
460 460 else:
461 461 self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line
462 462 self.n = 9999
463 463 self.__byTime = True
464 464
465 465 if overlapping:
466 466 self.__withOverlapping = True
467 467 self.__buffer = None
468 468 else:
469 469 self.__withOverlapping = False
470 470 self.__buffer = 0
471 471
472 472 self.__profIndex = 0
473 473
474 474 def putData(self, data):
475 475
476 476 """
477 477 Add a profile to the __buffer and increase in one the __profileIndex
478 478
479 479 """
480 480
481 481 if not self.__withOverlapping:
482 482 self.__buffer += data.copy()
483 483 self.__profIndex += 1
484 484 return
485 485
486 486 #Overlapping data
487 487 nChannels, nHeis = data.shape
488 488 data = numpy.reshape(data, (1, nChannels, nHeis))
489 489
490 490 #If the buffer is empty then it takes the data value
491 491 if self.__buffer is None:
492 492 self.__buffer = data
493 493 self.__profIndex += 1
494 494 return
495 495
496 496 #If the buffer length is lower than n then stakcing the data value
497 497 if self.__profIndex < self.n:
498 498 self.__buffer = numpy.vstack((self.__buffer, data))
499 499 self.__profIndex += 1
500 500 return
501 501
502 502 #If the buffer length is equal to n then replacing the last buffer value with the data value
503 503 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
504 504 self.__buffer[self.n-1] = data
505 505 self.__profIndex = self.n
506 506 return
507 507
508 508
509 509 def pushData(self):
510 510 """
511 511 Return the sum of the last profiles and the profiles used in the sum.
512 512
513 513 Affected:
514 514
515 515 self.__profileIndex
516 516
517 517 """
518 518
519 519 if not self.__withOverlapping:
520 520 data = self.__buffer
521 521 n = self.__profIndex
522 522
523 523 self.__buffer = 0
524 524 self.__profIndex = 0
525 525
526 526 return data, n
527 527
528 528 #Integration with Overlapping
529 529 data = numpy.sum(self.__buffer, axis=0)
530 530 # print data
531 531 # raise
532 532 n = self.__profIndex
533 533
534 534 return data, n
535 535
536 536 def byProfiles(self, data):
537 537
538 538 self.__dataReady = False
539 539 avgdata = None
540 540 # n = None
541 541 # print data
542 542 # raise
543 543 self.putData(data)
544 544
545 545 if self.__profIndex == self.n:
546 546 avgdata, n = self.pushData()
547 547 self.__dataReady = True
548 548
549 549 return avgdata
550 550
551 551 def byTime(self, data, datatime):
552 552
553 553 self.__dataReady = False
554 554 avgdata = None
555 555 n = None
556 556
557 557 self.putData(data)
558 558
559 559 if (datatime - self.__initime) >= self.__integrationtime:
560 560 avgdata, n = self.pushData()
561 561 self.n = n
562 562 self.__dataReady = True
563 563
564 564 return avgdata
565 565
566 566 def integrateByStride(self, data, datatime):
567 567 # print data
568 568 if self.__profIndex == 0:
569 569 self.__buffer = [[data.copy(), datatime]]
570 570 else:
571 571 self.__buffer.append([data.copy(),datatime])
572 572 self.__profIndex += 1
573 573 self.__dataReady = False
574 574
575 575 if self.__profIndex == self.n * self.stride :
576 576 self.__dataToPutStride = True
577 577 self.__profIndexStride = 0
578 578 self.__profIndex = 0
579 579 self.__bufferStride = []
580 580 for i in range(self.stride):
581 581 current = self.__buffer[i::self.stride]
582 582 data = numpy.sum([t[0] for t in current], axis=0)
583 583 avgdatatime = numpy.average([t[1] for t in current])
584 584 # print data
585 585 self.__bufferStride.append((data, avgdatatime))
586 586
587 587 if self.__dataToPutStride:
588 588 self.__dataReady = True
589 589 self.__profIndexStride += 1
590 590 if self.__profIndexStride == self.stride:
591 591 self.__dataToPutStride = False
592 592 # print self.__bufferStride[self.__profIndexStride - 1]
593 593 # raise
594 594 return self.__bufferStride[self.__profIndexStride - 1]
595 595
596 596
597 597 return None, None
598 598
599 599 def integrate(self, data, datatime=None):
600 600
601 601 if self.__initime == None:
602 602 self.__initime = datatime
603 603
604 604 if self.__byTime:
605 605 avgdata = self.byTime(data, datatime)
606 606 else:
607 607 avgdata = self.byProfiles(data)
608 608
609 609
610 610 self.__lastdatatime = datatime
611 611
612 612 if avgdata is None:
613 613 return None, None
614 614
615 615 avgdatatime = self.__initime
616 616
617 617 deltatime = datatime - self.__lastdatatime
618 618
619 619 if not self.__withOverlapping:
620 620 self.__initime = datatime
621 621 else:
622 622 self.__initime += deltatime
623 623
624 624 return avgdata, avgdatatime
625 625
626 626 def integrateByBlock(self, dataOut):
627 627
628 628 times = int(dataOut.data.shape[1]/self.n)
629 629 avgdata = numpy.zeros((dataOut.nChannels, times, dataOut.nHeights), dtype=numpy.complex)
630 630
631 631 id_min = 0
632 632 id_max = self.n
633 633
634 634 for i in range(times):
635 635 junk = dataOut.data[:,id_min:id_max,:]
636 636 avgdata[:,i,:] = junk.sum(axis=1)
637 637 id_min += self.n
638 638 id_max += self.n
639 639
640 640 timeInterval = dataOut.ippSeconds*self.n
641 641 avgdatatime = (times - 1) * timeInterval + dataOut.utctime
642 642 self.__dataReady = True
643 643 return avgdata, avgdatatime
644 644
645 645 def run(self, dataOut, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False, **kwargs):
646 646
647 647 if not self.isConfig:
648 648 self.setup(n=n, stride=stride, timeInterval=timeInterval, overlapping=overlapping, byblock=byblock, **kwargs)
649 649 self.isConfig = True
650 650
651 651 if dataOut.flagDataAsBlock:
652 652 """
653 653 Si la data es leida por bloques, dimension = [nChannels, nProfiles, nHeis]
654 654 """
655 655 avgdata, avgdatatime = self.integrateByBlock(dataOut)
656 656 dataOut.nProfiles /= self.n
657 657 else:
658 658 if stride is None:
659 659 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
660 660 else:
661 661 avgdata, avgdatatime = self.integrateByStride(dataOut.data, dataOut.utctime)
662 662
663 663
664 664 # dataOut.timeInterval *= n
665 665 dataOut.flagNoData = True
666 666
667 667 if self.__dataReady:
668 668 dataOut.data = avgdata
669 669 if not dataOut.flagCohInt:
670 670 dataOut.nCohInt *= self.n
671 671 dataOut.flagCohInt = True
672 672 dataOut.utctime = avgdatatime
673 673 # print avgdata, avgdatatime
674 674 # raise
675 675 # dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
676 676 dataOut.flagNoData = False
677 677 return dataOut
678 678
679 679 class Decoder(Operation):
680 680
681 681 isConfig = False
682 682 __profIndex = 0
683 683
684 684 code = None
685 685
686 686 nCode = None
687 687 nBaud = None
688 688
689 689 def __init__(self, **kwargs):
690 690
691 691 Operation.__init__(self, **kwargs)
692 692
693 693 self.times = None
694 694 self.osamp = None
695 695 # self.__setValues = False
696 696 self.isConfig = False
697 697 self.setupReq = False
698 698 def setup(self, code, osamp, dataOut):
699 699
700 700 self.__profIndex = 0
701 701
702 702 self.code = code
703 703
704 704 self.nCode = len(code)
705 705 self.nBaud = len(code[0])
706 706
707 707 if (osamp != None) and (osamp >1):
708 708 self.osamp = osamp
709 709 self.code = numpy.repeat(code, repeats=self.osamp, axis=1)
710 710 self.nBaud = self.nBaud*self.osamp
711 711
712 712 self.__nChannels = dataOut.nChannels
713 713 self.__nProfiles = dataOut.nProfiles
714 714 self.__nHeis = dataOut.nHeights
715 715
716 716 if self.__nHeis < self.nBaud:
717 717 raise ValueError('Number of heights (%d) should be greater than number of bauds (%d)' %(self.__nHeis, self.nBaud))
718 718
719 719 #Frequency
720 720 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
721 721
722 722 __codeBuffer[:,0:self.nBaud] = self.code
723 723
724 724 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
725 725
726 726 if dataOut.flagDataAsBlock:
727 727
728 728 self.ndatadec = self.__nHeis #- self.nBaud + 1
729 729
730 730 self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex)
731 731
732 732 else:
733 733
734 734 #Time
735 735 self.ndatadec = self.__nHeis #- self.nBaud + 1
736 736
737 737 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
738 738
739 739 def __convolutionInFreq(self, data):
740 740
741 741 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
742 742
743 743 fft_data = numpy.fft.fft(data, axis=1)
744 744
745 745 conv = fft_data*fft_code
746 746
747 747 data = numpy.fft.ifft(conv,axis=1)
748 748
749 749 return data
750 750
751 751 def __convolutionInFreqOpt(self, data):
752 752
753 753 raise NotImplementedError
754 754
755 755 def __convolutionInTime(self, data):
756 756
757 757 code = self.code[self.__profIndex]
758 758 for i in range(self.__nChannels):
759 759 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='full')[self.nBaud-1:]
760 760
761 761 return self.datadecTime
762 762
763 763 def __convolutionByBlockInTime(self, data):
764 764
765 765 repetitions = int(self.__nProfiles / self.nCode)
766 766 junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize))
767 767 junk = junk.flatten()
768 768 code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud))
769 769 profilesList = range(self.__nProfiles)
770 770
771 771 for i in range(self.__nChannels):
772 772 for j in profilesList:
773 773 self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:]
774 774 return self.datadecTime
775 775
776 776 def __convolutionByBlockInFreq(self, data):
777 777
778 778 raise NotImplementedError("Decoder by frequency fro Blocks not implemented")
779 779
780 780
781 781 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
782 782
783 783 fft_data = numpy.fft.fft(data, axis=2)
784 784
785 785 conv = fft_data*fft_code
786 786
787 787 data = numpy.fft.ifft(conv,axis=2)
788 788
789 789 return data
790 790
791 791
792 792 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None):
793 793
794 794 if dataOut.flagDecodeData:
795 795 print("This data is already decoded, recoding again ...")
796 796
797 797 if not self.isConfig:
798 798
799 799 if code is None:
800 800 if dataOut.code is None:
801 801 raise ValueError("Code could not be read from %s instance. Enter a value in Code parameter" %dataOut.type)
802 802
803 803 code = dataOut.code
804 804 else:
805 805 code = numpy.array(code).reshape(nCode,nBaud)
806 806 self.setup(code, osamp, dataOut)
807 807
808 808 self.isConfig = True
809 809
810 810 if mode == 3:
811 811 sys.stderr.write("Decoder Warning: mode=%d is not valid, using mode=0\n" %mode)
812 812
813 813 if times != None:
814 814 sys.stderr.write("Decoder Warning: Argument 'times' in not used anymore\n")
815 815
816 816 if self.code is None:
817 817 print("Fail decoding: Code is not defined.")
818 818 return
819 819
820 820 self.__nProfiles = dataOut.nProfiles
821 821 datadec = None
822 822
823 823 if mode == 3:
824 824 mode = 0
825 825
826 826 if dataOut.flagDataAsBlock:
827 827 """
828 828 Decoding when data have been read as block,
829 829 """
830 830
831 831 if mode == 0:
832 832 datadec = self.__convolutionByBlockInTime(dataOut.data)
833 833 if mode == 1:
834 834 datadec = self.__convolutionByBlockInFreq(dataOut.data)
835 835 else:
836 836 """
837 837 Decoding when data have been read profile by profile
838 838 """
839 839 if mode == 0:
840 840 datadec = self.__convolutionInTime(dataOut.data)
841 841
842 842 if mode == 1:
843 843 datadec = self.__convolutionInFreq(dataOut.data)
844 844
845 845 if mode == 2:
846 846 datadec = self.__convolutionInFreqOpt(dataOut.data)
847 847
848 848 if datadec is None:
849 849 raise ValueError("Codification mode selected is not valid: mode=%d. Try selecting 0 or 1" %mode)
850 850
851 851 dataOut.code = self.code
852 852 dataOut.nCode = self.nCode
853 853 dataOut.nBaud = self.nBaud
854 854
855 855 dataOut.data = datadec
856 856
857 857 dataOut.heightList = dataOut.heightList[0:datadec.shape[-1]]
858 858
859 859 dataOut.flagDecodeData = True #asumo q la data esta decodificada
860 860
861 861 if self.__profIndex == self.nCode-1:
862 862 self.__profIndex = 0
863 863 return dataOut
864 864
865 865 self.__profIndex += 1
866 866
867 867 return dataOut
868 868 # dataOut.flagDeflipData = True #asumo q la data no esta sin flip
869 869
870 870
871 871 class ProfileConcat(Operation):
872 872
873 873 isConfig = False
874 874 buffer = None
875 875
876 876 def __init__(self, **kwargs):
877 877
878 878 Operation.__init__(self, **kwargs)
879 879 self.profileIndex = 0
880 880
881 881 def reset(self):
882 882 self.buffer = numpy.zeros_like(self.buffer)
883 883 self.start_index = 0
884 884 self.times = 1
885 885
886 886 def setup(self, data, m, n=1):
887 887 self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0]))
888 888 self.nHeights = data.shape[1]#.nHeights
889 889 self.start_index = 0
890 890 self.times = 1
891 891
892 892 def concat(self, data):
893 893
894 894 self.buffer[:,self.start_index:self.nHeights*self.times] = data.copy()
895 895 self.start_index = self.start_index + self.nHeights
896 896
897 897 def run(self, dataOut, m):
898 898 dataOut.flagNoData = True
899 899
900 900 if not self.isConfig:
901 901 self.setup(dataOut.data, m, 1)
902 902 self.isConfig = True
903 903
904 904 if dataOut.flagDataAsBlock:
905 905 raise ValueError("ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False")
906 906
907 907 else:
908 908 self.concat(dataOut.data)
909 909 self.times += 1
910 910 if self.times > m:
911 911 dataOut.data = self.buffer
912 912 self.reset()
913 913 dataOut.flagNoData = False
914 914 # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas
915 915 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
916 916 xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * m
917 917 dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight)
918 918 dataOut.ippSeconds *= m
919 919 return dataOut
920 920
921 921 class ProfileSelector(Operation):
922 922
923 923 profileIndex = None
924 924 # Tamanho total de los perfiles
925 925 nProfiles = None
926 926
927 927 def __init__(self, **kwargs):
928 928
929 929 Operation.__init__(self, **kwargs)
930 930 self.profileIndex = 0
931 931
932 932 def incProfileIndex(self):
933 933
934 934 self.profileIndex += 1
935 935
936 936 if self.profileIndex >= self.nProfiles:
937 937 self.profileIndex = 0
938 938
939 939 def isThisProfileInRange(self, profileIndex, minIndex, maxIndex):
940 940
941 941 if profileIndex < minIndex:
942 942 return False
943 943
944 944 if profileIndex > maxIndex:
945 945 return False
946 946
947 947 return True
948 948
949 949 def isThisProfileInList(self, profileIndex, profileList):
950 950
951 951 if profileIndex not in profileList:
952 952 return False
953 953
954 954 return True
955 955
956 956 def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False, rangeList = None, nProfiles=None):
957 957
958 958 """
959 959 ProfileSelector:
960 960
961 961 Inputs:
962 962 profileList : Index of profiles selected. Example: profileList = (0,1,2,7,8)
963 963
964 964 profileRangeList : Minimum and maximum profile indexes. Example: profileRangeList = (4, 30)
965 965
966 966 rangeList : List of profile ranges. Example: rangeList = ((4, 30), (32, 64), (128, 256))
967 967
968 968 """
969 969
970 970 if rangeList is not None:
971 971 if type(rangeList[0]) not in (tuple, list):
972 972 rangeList = [rangeList]
973 973
974 974 dataOut.flagNoData = True
975 975
976 976 if dataOut.flagDataAsBlock:
977 977 """
978 978 data dimension = [nChannels, nProfiles, nHeis]
979 979 """
980 980 if profileList != None:
981 981 dataOut.data = dataOut.data[:,profileList,:]
982 982
983 983 if profileRangeList != None:
984 984 minIndex = profileRangeList[0]
985 985 maxIndex = profileRangeList[1]
986 986 profileList = list(range(minIndex, maxIndex+1))
987 987
988 988 dataOut.data = dataOut.data[:,minIndex:maxIndex+1,:]
989 989
990 990 if rangeList != None:
991 991
992 992 profileList = []
993 993
994 994 for thisRange in rangeList:
995 995 minIndex = thisRange[0]
996 996 maxIndex = thisRange[1]
997 997
998 998 profileList.extend(list(range(minIndex, maxIndex+1)))
999 999
1000 1000 dataOut.data = dataOut.data[:,profileList,:]
1001 1001
1002 1002 dataOut.nProfiles = len(profileList)
1003 1003 dataOut.profileIndex = dataOut.nProfiles - 1
1004 1004 dataOut.flagNoData = False
1005 1005
1006 1006 return dataOut
1007 1007
1008 1008 """
1009 1009 data dimension = [nChannels, nHeis]
1010 1010 """
1011 1011
1012 1012 if profileList != None:
1013 1013
1014 1014 if self.isThisProfileInList(dataOut.profileIndex, profileList):
1015 1015
1016 1016 self.nProfiles = len(profileList)
1017 1017 dataOut.nProfiles = self.nProfiles
1018 1018 dataOut.profileIndex = self.profileIndex
1019 1019 dataOut.flagNoData = False
1020 1020
1021 1021 self.incProfileIndex()
1022 1022 return dataOut
1023 1023
1024 1024 if profileRangeList != None:
1025 1025
1026 1026 minIndex = profileRangeList[0]
1027 1027 maxIndex = profileRangeList[1]
1028 1028
1029 1029 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
1030 1030
1031 1031 self.nProfiles = maxIndex - minIndex + 1
1032 1032 dataOut.nProfiles = self.nProfiles
1033 1033 dataOut.profileIndex = self.profileIndex
1034 1034 dataOut.flagNoData = False
1035 1035
1036 1036 self.incProfileIndex()
1037 1037 return dataOut
1038 1038
1039 1039 if rangeList != None:
1040 1040
1041 1041 nProfiles = 0
1042 1042
1043 1043 for thisRange in rangeList:
1044 1044 minIndex = thisRange[0]
1045 1045 maxIndex = thisRange[1]
1046 1046
1047 1047 nProfiles += maxIndex - minIndex + 1
1048 1048
1049 1049 for thisRange in rangeList:
1050 1050
1051 1051 minIndex = thisRange[0]
1052 1052 maxIndex = thisRange[1]
1053 1053
1054 1054 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
1055 1055
1056 1056 self.nProfiles = nProfiles
1057 1057 dataOut.nProfiles = self.nProfiles
1058 1058 dataOut.profileIndex = self.profileIndex
1059 1059 dataOut.flagNoData = False
1060 1060
1061 1061 self.incProfileIndex()
1062 1062
1063 1063 break
1064 1064
1065 1065 return dataOut
1066 1066
1067 1067
1068 1068 if beam != None: #beam is only for AMISR data
1069 1069 if self.isThisProfileInList(dataOut.profileIndex, dataOut.beamRangeDict[beam]):
1070 1070 dataOut.flagNoData = False
1071 1071 dataOut.profileIndex = self.profileIndex
1072 1072
1073 1073 self.incProfileIndex()
1074 1074
1075 1075 return dataOut
1076 1076
1077 1077 raise ValueError("ProfileSelector needs profileList, profileRangeList or rangeList parameter")
1078 1078
1079 1079
1080 1080 class Reshaper(Operation):
1081 1081
1082 1082 def __init__(self, **kwargs):
1083 1083
1084 1084 Operation.__init__(self, **kwargs)
1085 1085
1086 1086 self.__buffer = None
1087 1087 self.__nitems = 0
1088 1088
1089 1089 def __appendProfile(self, dataOut, nTxs):
1090 1090
1091 1091 if self.__buffer is None:
1092 1092 shape = (dataOut.nChannels, int(dataOut.nHeights/nTxs) )
1093 1093 self.__buffer = numpy.empty(shape, dtype = dataOut.data.dtype)
1094 1094
1095 1095 ini = dataOut.nHeights * self.__nitems
1096 1096 end = ini + dataOut.nHeights
1097 1097
1098 1098 self.__buffer[:, ini:end] = dataOut.data
1099 1099
1100 1100 self.__nitems += 1
1101 1101
1102 1102 return int(self.__nitems*nTxs)
1103 1103
1104 1104 def __getBuffer(self):
1105 1105
1106 1106 if self.__nitems == int(1./self.__nTxs):
1107 1107
1108 1108 self.__nitems = 0
1109 1109
1110 1110 return self.__buffer.copy()
1111 1111
1112 1112 return None
1113 1113
1114 1114 def __checkInputs(self, dataOut, shape, nTxs):
1115 1115
1116 1116 if shape is None and nTxs is None:
1117 1117 raise ValueError("Reshaper: shape of factor should be defined")
1118 1118
1119 1119 if nTxs:
1120 1120 if nTxs < 0:
1121 1121 raise ValueError("nTxs should be greater than 0")
1122 1122
1123 1123 if nTxs < 1 and dataOut.nProfiles % (1./nTxs) != 0:
1124 1124 raise ValueError("nProfiles= %d is not divisibled by (1./nTxs) = %f" %(dataOut.nProfiles, (1./nTxs)))
1125 1125
1126 1126 shape = [dataOut.nChannels, dataOut.nProfiles*nTxs, dataOut.nHeights/nTxs]
1127 1127
1128 1128 return shape, nTxs
1129 1129
1130 1130 if len(shape) != 2 and len(shape) != 3:
1131 1131 raise ValueError("shape dimension should be equal to 2 or 3. shape = (nProfiles, nHeis) or (nChannels, nProfiles, nHeis). Actually shape = (%d, %d, %d)" %(dataOut.nChannels, dataOut.nProfiles, dataOut.nHeights))
1132 1132
1133 1133 if len(shape) == 2:
1134 1134 shape_tuple = [dataOut.nChannels]
1135 1135 shape_tuple.extend(shape)
1136 1136 else:
1137 1137 shape_tuple = list(shape)
1138 1138
1139 1139 nTxs = 1.0*shape_tuple[1]/dataOut.nProfiles
1140 1140
1141 1141 return shape_tuple, nTxs
1142 1142
1143 1143 def run(self, dataOut, shape=None, nTxs=None):
1144 1144
1145 1145 shape_tuple, self.__nTxs = self.__checkInputs(dataOut, shape, nTxs)
1146 1146
1147 1147 dataOut.flagNoData = True
1148 1148 profileIndex = None
1149 1149
1150 1150 if dataOut.flagDataAsBlock:
1151 1151
1152 1152 dataOut.data = numpy.reshape(dataOut.data, shape_tuple)
1153 1153 dataOut.flagNoData = False
1154 1154
1155 1155 profileIndex = int(dataOut.nProfiles*self.__nTxs) - 1
1156 1156
1157 1157 else:
1158 1158
1159 1159 if self.__nTxs < 1:
1160 1160
1161 1161 self.__appendProfile(dataOut, self.__nTxs)
1162 1162 new_data = self.__getBuffer()
1163 1163
1164 1164 if new_data is not None:
1165 1165 dataOut.data = new_data
1166 1166 dataOut.flagNoData = False
1167 1167
1168 1168 profileIndex = dataOut.profileIndex*nTxs
1169 1169
1170 1170 else:
1171 1171 raise ValueError("nTxs should be greater than 0 and lower than 1, or use VoltageReader(..., getblock=True)")
1172 1172
1173 1173 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1174 1174
1175 1175 dataOut.heightList = numpy.arange(dataOut.nHeights/self.__nTxs) * deltaHeight + dataOut.heightList[0]
1176 1176
1177 1177 dataOut.nProfiles = int(dataOut.nProfiles*self.__nTxs)
1178 1178
1179 1179 dataOut.profileIndex = profileIndex
1180 1180
1181 1181 dataOut.ippSeconds /= self.__nTxs
1182 1182
1183 1183 return dataOut
1184 1184
1185 1185 class SplitProfiles(Operation):
1186 1186
1187 1187 def __init__(self, **kwargs):
1188 1188
1189 1189 Operation.__init__(self, **kwargs)
1190 1190
1191 1191 def run(self, dataOut, n):
1192 1192
1193 1193 dataOut.flagNoData = True
1194 1194 profileIndex = None
1195 1195
1196 1196 if dataOut.flagDataAsBlock:
1197 1197
1198 1198 #nchannels, nprofiles, nsamples
1199 1199 shape = dataOut.data.shape
1200 1200
1201 1201 if shape[2] % n != 0:
1202 1202 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[2]))
1203 1203
1204 1204 new_shape = shape[0], shape[1]*n, int(shape[2]/n)
1205 1205
1206 1206 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1207 1207 dataOut.flagNoData = False
1208 1208
1209 1209 profileIndex = int(dataOut.nProfiles/n) - 1
1210 1210
1211 1211 else:
1212 1212
1213 1213 raise ValueError("Could not split the data when is read Profile by Profile. Use VoltageReader(..., getblock=True)")
1214 1214
1215 1215 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1216 1216
1217 1217 dataOut.heightList = numpy.arange(dataOut.nHeights/n) * deltaHeight + dataOut.heightList[0]
1218 1218
1219 1219 dataOut.nProfiles = int(dataOut.nProfiles*n)
1220 1220
1221 1221 dataOut.profileIndex = profileIndex
1222 1222
1223 1223 dataOut.ippSeconds /= n
1224 1224
1225 1225 return dataOut
1226 1226
1227 1227 class CombineProfiles(Operation):
1228 1228 def __init__(self, **kwargs):
1229 1229
1230 1230 Operation.__init__(self, **kwargs)
1231 1231
1232 1232 self.__remData = None
1233 1233 self.__profileIndex = 0
1234 1234
1235 1235 def run(self, dataOut, n):
1236 1236
1237 1237 dataOut.flagNoData = True
1238 1238 profileIndex = None
1239 1239
1240 1240 if dataOut.flagDataAsBlock:
1241 1241
1242 1242 #nchannels, nprofiles, nsamples
1243 1243 shape = dataOut.data.shape
1244 1244 new_shape = shape[0], shape[1]/n, shape[2]*n
1245 1245
1246 1246 if shape[1] % n != 0:
1247 1247 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[1]))
1248 1248
1249 1249 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1250 1250 dataOut.flagNoData = False
1251 1251
1252 1252 profileIndex = int(dataOut.nProfiles*n) - 1
1253 1253
1254 1254 else:
1255 1255
1256 1256 #nchannels, nsamples
1257 1257 if self.__remData is None:
1258 1258 newData = dataOut.data
1259 1259 else:
1260 1260 newData = numpy.concatenate((self.__remData, dataOut.data), axis=1)
1261 1261
1262 1262 self.__profileIndex += 1
1263 1263
1264 1264 if self.__profileIndex < n:
1265 1265 self.__remData = newData
1266 1266 #continue
1267 1267 return
1268 1268
1269 1269 self.__profileIndex = 0
1270 1270 self.__remData = None
1271 1271
1272 1272 dataOut.data = newData
1273 1273 dataOut.flagNoData = False
1274 1274
1275 1275 profileIndex = dataOut.profileIndex/n
1276 1276
1277 1277
1278 1278 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1279 1279
1280 1280 dataOut.heightList = numpy.arange(dataOut.nHeights*n) * deltaHeight + dataOut.heightList[0]
1281 1281
1282 1282 dataOut.nProfiles = int(dataOut.nProfiles/n)
1283 1283
1284 1284 dataOut.profileIndex = profileIndex
1285 1285
1286 1286 dataOut.ippSeconds *= n
1287 1287
1288 1288 return dataOut
1289 1289
1290 1290 class PulsePair(Operation):
1291 1291 '''
1292 1292 Function PulsePair(Signal Power, Velocity)
1293 1293 The real component of Lag[0] provides Intensity Information
1294 1294 The imag component of Lag[1] Phase provides Velocity Information
1295 1295
1296 1296 Configuration Parameters:
1297 1297 nPRF = Number of Several PRF
1298 1298 theta = Degree Azimuth angel Boundaries
1299 1299
1300 1300 Input:
1301 1301 self.dataOut
1302 1302 lag[N]
1303 1303 Affected:
1304 1304 self.dataOut.spc
1305 1305 '''
1306 1306 isConfig = False
1307 1307 __profIndex = 0
1308 1308 __initime = None
1309 1309 __lastdatatime = None
1310 1310 __buffer = None
1311 1311 noise = None
1312 1312 __dataReady = False
1313 1313 n = None
1314 1314 __nch = 0
1315 1315 __nHeis = 0
1316 1316 removeDC = False
1317 1317 ipp = None
1318 1318 lambda_ = 0
1319 1319
1320 1320 def __init__(self,**kwargs):
1321 1321 Operation.__init__(self,**kwargs)
1322 1322
1323 1323 def setup(self, dataOut, n = None, removeDC=False):
1324 1324 '''
1325 1325 n= Numero de PRF's de entrada
1326 1326 '''
1327 1327 self.__initime = None
1328 1328 ####print("[INICIO]-setup del METODO PULSE PAIR")
1329 1329 self.__lastdatatime = 0
1330 1330 self.__dataReady = False
1331 1331 self.__buffer = 0
1332 1332 self.__profIndex = 0
1333 1333 self.noise = None
1334 1334 self.__nch = dataOut.nChannels
1335 1335 self.__nHeis = dataOut.nHeights
1336 1336 self.removeDC = removeDC
1337 1337 self.lambda_ = 3.0e8/(9345.0e6)
1338 1338 self.ippSec = dataOut.ippSeconds
1339 1339 self.nCohInt = dataOut.nCohInt
1340 1340 ####print("IPPseconds",dataOut.ippSeconds)
1341 1341 ####print("ELVALOR DE n es:", n)
1342 1342 if n == None:
1343 1343 raise ValueError("n should be specified.")
1344 1344
1345 1345 if n != None:
1346 1346 if n<2:
1347 1347 raise ValueError("n should be greater than 2")
1348 1348
1349 1349 self.n = n
1350 1350 self.__nProf = n
1351 1351
1352 1352 self.__buffer = numpy.zeros((dataOut.nChannels,
1353 1353 n,
1354 1354 dataOut.nHeights),
1355 1355 dtype='complex')
1356 1356
1357 1357 def putData(self,data):
1358 1358 '''
1359 1359 Add a profile to he __buffer and increase in one the __profiel Index
1360 1360 '''
1361 1361 self.__buffer[:,self.__profIndex,:]= data
1362 1362 self.__profIndex += 1
1363 1363 return
1364 1364
1365 1365 def pushData(self,dataOut):
1366 1366 '''
1367 1367 Return the PULSEPAIR and the profiles used in the operation
1368 1368 Affected : self.__profileIndex
1369 1369 '''
1370 1370 #----------------- Remove DC-----------------------------------
1371 1371 if self.removeDC==True:
1372 1372 mean = numpy.mean(self.__buffer,1)
1373 1373 tmp = mean.reshape(self.__nch,1,self.__nHeis)
1374 1374 dc= numpy.tile(tmp,[1,self.__nProf,1])
1375 1375 self.__buffer = self.__buffer - dc
1376 1376 #------------------Calculo de Potencia ------------------------
1377 1377 pair0 = self.__buffer*numpy.conj(self.__buffer)
1378 1378 pair0 = pair0.real
1379 1379 lag_0 = numpy.sum(pair0,1)
1380 1380 #-----------------Calculo de Cscp------------------------------ New
1381 cspc_pair01 = self.__buffer[0]*__self.buffer[1]
1381 cspc_pair01 = self.__buffer[0]*self.__buffer[1]
1382 1382 #------------------Calculo de Ruido x canal--------------------
1383 1383 self.noise = numpy.zeros(self.__nch)
1384 1384 for i in range(self.__nch):
1385 1385 daux = numpy.sort(pair0[i,:,:],axis= None)
1386 1386 self.noise[i]=hildebrand_sekhon( daux ,self.nCohInt)
1387 1387
1388 1388 self.noise = self.noise.reshape(self.__nch,1)
1389 1389 self.noise = numpy.tile(self.noise,[1,self.__nHeis])
1390 1390 noise_buffer = self.noise.reshape(self.__nch,1,self.__nHeis)
1391 1391 noise_buffer = numpy.tile(noise_buffer,[1,self.__nProf,1])
1392 1392 #------------------ Potencia recibida= P , Potencia senal = S , Ruido= N--
1393 1393 #------------------ P= S+N ,P=lag_0/N ---------------------------------
1394 1394 #-------------------- Power --------------------------------------------------
1395 1395 data_power = lag_0/(self.n*self.nCohInt)
1396 1396 #--------------------CCF------------------------------------------------------
1397 1397 data_ccf =numpy.sum(cspc_pair01,axis=0)/(self.n*self.nCohInt)
1398 1398 #------------------ Senal --------------------------------------------------
1399 1399 data_intensity = pair0 - noise_buffer
1400 1400 data_intensity = numpy.sum(data_intensity,axis=1)*(self.n*self.nCohInt)#*self.nCohInt)
1401 1401 #data_intensity = (lag_0-self.noise*self.n)*(self.n*self.nCohInt)
1402 1402 for i in range(self.__nch):
1403 1403 for j in range(self.__nHeis):
1404 1404 if data_intensity[i][j] < 0:
1405 1405 data_intensity[i][j] = numpy.min(numpy.absolute(data_intensity[i][j]))
1406 1406
1407 1407 #----------------- Calculo de Frecuencia y Velocidad doppler--------
1408 1408 pair1 = self.__buffer[:,:-1,:]*numpy.conjugate(self.__buffer[:,1:,:])
1409 1409 lag_1 = numpy.sum(pair1,1)
1410 1410 data_freq = (-1/(2.0*math.pi*self.ippSec*self.nCohInt))*numpy.angle(lag_1)
1411 1411 data_velocity = (self.lambda_/2.0)*data_freq
1412 1412
1413 1413 #---------------- Potencia promedio estimada de la Senal-----------
1414 1414 lag_0 = lag_0/self.n
1415 1415 S = lag_0-self.noise
1416 1416
1417 1417 #---------------- Frecuencia Doppler promedio ---------------------
1418 1418 lag_1 = lag_1/(self.n-1)
1419 1419 R1 = numpy.abs(lag_1)
1420 1420
1421 1421 #---------------- Calculo del SNR----------------------------------
1422 1422 data_snrPP = S/self.noise
1423 1423 for i in range(self.__nch):
1424 1424 for j in range(self.__nHeis):
1425 1425 if data_snrPP[i][j] < 1.e-20:
1426 1426 data_snrPP[i][j] = 1.e-20
1427 1427
1428 1428 #----------------- Calculo del ancho espectral ----------------------
1429 1429 L = S/R1
1430 1430 L = numpy.where(L<0,1,L)
1431 1431 L = numpy.log(L)
1432 1432 tmp = numpy.sqrt(numpy.absolute(L))
1433 1433 data_specwidth = (self.lambda_/(2*math.sqrt(2)*math.pi*self.ippSec*self.nCohInt))*tmp*numpy.sign(L)
1434 1434 n = self.__profIndex
1435 1435
1436 1436 self.__buffer = numpy.zeros((self.__nch, self.__nProf,self.__nHeis), dtype='complex')
1437 1437 self.__profIndex = 0
1438 1438 return data_power,data_intensity,data_velocity,data_snrPP,data_specwidth,data_ccf,n
1439 1439
1440 1440
1441 1441 def pulsePairbyProfiles(self,dataOut):
1442 1442
1443 1443 self.__dataReady = False
1444 1444 data_power = None
1445 1445 data_intensity = None
1446 1446 data_velocity = None
1447 1447 data_specwidth = None
1448 1448 data_snrPP = None
1449 1449 data_ccf = None
1450 1450 self.putData(data=dataOut.data)
1451 1451 if self.__profIndex == self.n:
1452 1452 data_power,data_intensity, data_velocity,data_snrPP,data_specwidth,data_ccf, n = self.pushData(dataOut=dataOut)
1453 1453 self.__dataReady = True
1454 1454
1455 1455 return data_power, data_intensity, data_velocity, data_snrPP,data_specwidth,data_ccf
1456 1456
1457 1457
1458 1458 def pulsePairOp(self, dataOut, datatime= None):
1459 1459
1460 1460 if self.__initime == None:
1461 1461 self.__initime = datatime
1462 1462 data_power, data_intensity, data_velocity, data_snrPP,data_specwidth,data_ccf = self.pulsePairbyProfiles(dataOut)
1463 1463 self.__lastdatatime = datatime
1464 1464
1465 1465 if data_power is None:
1466 return None, None, None,None,None,None
1466 return None, None, None,None,None,None,None
1467 1467
1468 1468 avgdatatime = self.__initime
1469 1469 deltatime = datatime - self.__lastdatatime
1470 1470 self.__initime = datatime
1471 1471
1472 1472 return data_power, data_intensity, data_velocity, data_snrPP,data_specwidth,data_ccf, avgdatatime
1473 1473
1474 1474 def run(self, dataOut,n = None,removeDC= False, overlapping= False,**kwargs):
1475 1475
1476 1476 if not self.isConfig:
1477 1477 self.setup(dataOut = dataOut, n = n , removeDC=removeDC , **kwargs)
1478 1478 self.isConfig = True
1479 1479 data_power, data_intensity, data_velocity,data_snrPP,data_specwidth,data_ccf, avgdatatime = self.pulsePairOp(dataOut, dataOut.utctime)
1480 1480 dataOut.flagNoData = True
1481 1481
1482 1482 if self.__dataReady:
1483 1483 ###print("READY ----------------------------------")
1484 1484 dataOut.nCohInt *= self.n
1485 1485 dataOut.dataPP_POW = data_intensity # S
1486 1486 dataOut.dataPP_POWER = data_power # P valor que corresponde a POTENCIA MOMENTO
1487 1487 dataOut.dataPP_DOP = data_velocity
1488 1488 dataOut.dataPP_SNR = data_snrPP
1489 1489 dataOut.dataPP_WIDTH = data_specwidth
1490 1490 dataOut.dataPP_CCF = data_ccf
1491 1491 dataOut.PRFbyAngle = self.n #numero de PRF*cada angulo rotado que equivale a un tiempo.
1492 1492 dataOut.nProfiles = int(dataOut.nProfiles/n)
1493 1493 dataOut.utctime = avgdatatime
1494 1494 dataOut.flagNoData = False
1495 1495 return dataOut
1496 1496
1497 1497
1498 1498
1499 1499 # import collections
1500 1500 # from scipy.stats import mode
1501 1501 #
1502 1502 # class Synchronize(Operation):
1503 1503 #
1504 1504 # isConfig = False
1505 1505 # __profIndex = 0
1506 1506 #
1507 1507 # def __init__(self, **kwargs):
1508 1508 #
1509 1509 # Operation.__init__(self, **kwargs)
1510 1510 # # self.isConfig = False
1511 1511 # self.__powBuffer = None
1512 1512 # self.__startIndex = 0
1513 1513 # self.__pulseFound = False
1514 1514 #
1515 1515 # def __findTxPulse(self, dataOut, channel=0, pulse_with = None):
1516 1516 #
1517 1517 # #Read data
1518 1518 #
1519 1519 # powerdB = dataOut.getPower(channel = channel)
1520 1520 # noisedB = dataOut.getNoise(channel = channel)[0]
1521 1521 #
1522 1522 # self.__powBuffer.extend(powerdB.flatten())
1523 1523 #
1524 1524 # dataArray = numpy.array(self.__powBuffer)
1525 1525 #
1526 1526 # filteredPower = numpy.correlate(dataArray, dataArray[0:self.__nSamples], "same")
1527 1527 #
1528 1528 # maxValue = numpy.nanmax(filteredPower)
1529 1529 #
1530 1530 # if maxValue < noisedB + 10:
1531 1531 # #No se encuentra ningun pulso de transmision
1532 1532 # return None
1533 1533 #
1534 1534 # maxValuesIndex = numpy.where(filteredPower > maxValue - 0.1*abs(maxValue))[0]
1535 1535 #
1536 1536 # if len(maxValuesIndex) < 2:
1537 1537 # #Solo se encontro un solo pulso de transmision de un baudio, esperando por el siguiente TX
1538 1538 # return None
1539 1539 #
1540 1540 # phasedMaxValuesIndex = maxValuesIndex - self.__nSamples
1541 1541 #
1542 1542 # #Seleccionar solo valores con un espaciamiento de nSamples
1543 1543 # pulseIndex = numpy.intersect1d(maxValuesIndex, phasedMaxValuesIndex)
1544 1544 #
1545 1545 # if len(pulseIndex) < 2:
1546 1546 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1547 1547 # return None
1548 1548 #
1549 1549 # spacing = pulseIndex[1:] - pulseIndex[:-1]
1550 1550 #
1551 1551 # #remover senales que se distancien menos de 10 unidades o muestras
1552 1552 # #(No deberian existir IPP menor a 10 unidades)
1553 1553 #
1554 1554 # realIndex = numpy.where(spacing > 10 )[0]
1555 1555 #
1556 1556 # if len(realIndex) < 2:
1557 1557 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1558 1558 # return None
1559 1559 #
1560 1560 # #Eliminar pulsos anchos (deja solo la diferencia entre IPPs)
1561 1561 # realPulseIndex = pulseIndex[realIndex]
1562 1562 #
1563 1563 # period = mode(realPulseIndex[1:] - realPulseIndex[:-1])[0][0]
1564 1564 #
1565 1565 # print "IPP = %d samples" %period
1566 1566 #
1567 1567 # self.__newNSamples = dataOut.nHeights #int(period)
1568 1568 # self.__startIndex = int(realPulseIndex[0])
1569 1569 #
1570 1570 # return 1
1571 1571 #
1572 1572 #
1573 1573 # def setup(self, nSamples, nChannels, buffer_size = 4):
1574 1574 #
1575 1575 # self.__powBuffer = collections.deque(numpy.zeros( buffer_size*nSamples,dtype=numpy.float),
1576 1576 # maxlen = buffer_size*nSamples)
1577 1577 #
1578 1578 # bufferList = []
1579 1579 #
1580 1580 # for i in range(nChannels):
1581 1581 # bufferByChannel = collections.deque(numpy.zeros( buffer_size*nSamples, dtype=numpy.complex) + numpy.NAN,
1582 1582 # maxlen = buffer_size*nSamples)
1583 1583 #
1584 1584 # bufferList.append(bufferByChannel)
1585 1585 #
1586 1586 # self.__nSamples = nSamples
1587 1587 # self.__nChannels = nChannels
1588 1588 # self.__bufferList = bufferList
1589 1589 #
1590 1590 # def run(self, dataOut, channel = 0):
1591 1591 #
1592 1592 # if not self.isConfig:
1593 1593 # nSamples = dataOut.nHeights
1594 1594 # nChannels = dataOut.nChannels
1595 1595 # self.setup(nSamples, nChannels)
1596 1596 # self.isConfig = True
1597 1597 #
1598 1598 # #Append new data to internal buffer
1599 1599 # for thisChannel in range(self.__nChannels):
1600 1600 # bufferByChannel = self.__bufferList[thisChannel]
1601 1601 # bufferByChannel.extend(dataOut.data[thisChannel])
1602 1602 #
1603 1603 # if self.__pulseFound:
1604 1604 # self.__startIndex -= self.__nSamples
1605 1605 #
1606 1606 # #Finding Tx Pulse
1607 1607 # if not self.__pulseFound:
1608 1608 # indexFound = self.__findTxPulse(dataOut, channel)
1609 1609 #
1610 1610 # if indexFound == None:
1611 1611 # dataOut.flagNoData = True
1612 1612 # return
1613 1613 #
1614 1614 # self.__arrayBuffer = numpy.zeros((self.__nChannels, self.__newNSamples), dtype = numpy.complex)
1615 1615 # self.__pulseFound = True
1616 1616 # self.__startIndex = indexFound
1617 1617 #
1618 1618 # #If pulse was found ...
1619 1619 # for thisChannel in range(self.__nChannels):
1620 1620 # bufferByChannel = self.__bufferList[thisChannel]
1621 1621 # #print self.__startIndex
1622 1622 # x = numpy.array(bufferByChannel)
1623 1623 # self.__arrayBuffer[thisChannel] = x[self.__startIndex:self.__startIndex+self.__newNSamples]
1624 1624 #
1625 1625 # deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1626 1626 # dataOut.heightList = numpy.arange(self.__newNSamples)*deltaHeight
1627 1627 # # dataOut.ippSeconds = (self.__newNSamples / deltaHeight)/1e6
1628 1628 #
1629 1629 # dataOut.data = self.__arrayBuffer
1630 1630 #
1631 1631 # self.__startIndex += self.__newNSamples
1632 1632 #
1633 1633 # return
@@ -1,275 +1,303
1 1 #!python
2 2 '''
3 3 '''
4 4
5 5 import os, sys
6 6 import datetime
7 7 import time
8 8
9 9 #path = os.path.dirname(os.getcwd())
10 10 #path = os.path.dirname(path)
11 11 #sys.path.insert(0, path)
12 12
13 13 from schainpy.controller import Project
14 14
15 15 desc = "USRP_test"
16 16 filename = "USRP_processing.xml"
17 17 controllerObj = Project()
18 18 controllerObj.setup(id = '191', name='Test_USRP', description=desc)
19 19
20 20 ############## USED TO PLOT IQ VOLTAGE, POWER AND SPECTRA #############
21 21
22 22 #######################################################################
23 23 ######PATH DE LECTURA, ESCRITURA, GRAFICOS Y ENVIO WEB#################
24 24 #######################################################################
25 25 #path = '/media/data/data/vientos/57.2063km/echoes/NCO_Woodman'
26 26 #path = '/DATA_RM/TEST_INTEGRACION'
27 27 #path = '/DATA_RM/PRUEBA_USRP_RP'
28 path = '/DATA_RM/PRUEBA_USRP_RP'
28 #path = '/DATA_RM/PRUEBA_USRP_RP'
29 29
30 figpath = '/home/soporte/Pictures/TEST_RP_0001'
31 figpath = '/home/soporte/Pictures/TEST_RP_6000'
32 figpath = '/home/soporte/Pictures/USRP'
30 path = '/DATA_RM/TEST_2M'
31 path = '/DATA_RM/TEST_2M_UD'
32 path = '/DATA_RM/2MHZ17022022'
33 path = '/DATA_RM/10MHZTEST/'
34 path = '/DATA_RM/10MHZDRONE/'
35
36 #figpath = '/home/soporte/Pictures/TEST_RP_0001'
37 #figpath = '/home/soporte/Pictures/TEST_RP_6000'
38 figpath = '/home/soporte/Pictures/USRP_TEST_2M'
39 figpath = '/home/soporte/Pictures/USRP_TEST_2M_UD'
40 figpaht = '/home/soporte/Pictures/10MHZDRONE'
33 41 #remotefolder = "/home/wmaster/graficos"
34 42 #######################################################################
35 43 ################# RANGO DE PLOTEO######################################
36 44 #######################################################################
37 dBmin = '-5'
38 dBmax = '20'
45 dBmin = '20'
46 dBmax = '60'
39 47 xmin = '0'
40 48 xmax ='24'
41 49 ymin = '0'
42 50 ymax = '600'
43 51 #######################################################################
44 52 ########################FECHA##########################################
45 53 #######################################################################
46 54 str = datetime.date.today()
47 55 today = str.strftime("%Y/%m/%d")
48 56 str2 = str - datetime.timedelta(days=1)
49 57 yesterday = str2.strftime("%Y/%m/%d")
50 58 #######################################################################
51 59 ######################## UNIDAD DE LECTURA#############################
52 60 #######################################################################
53 61 readUnitConfObj = controllerObj.addReadUnit(datatype='DigitalRFReader',
54 62 path=path,
55 startDate="2021/07/02",#today,
56 endDate="2021/07/02",#today,
57 startTime='14:50:00',# inicio libre
63 startDate="2022/02/18",#today,
64 endDate="2022/02/18",#today,
65 startTime='00:00:00',# inicio libre
58 66 #startTime='00:00:00',
59 endTime='14:55:59',
67 endTime='23:59:59',
60 68 delay=0,
61 69 #set=0,
62 online=0,
70 online=1,
63 71 walk=1,
64 ippKm = 6000)
72 ippKm = 60)
65 73
66 74 opObj11 = readUnitConfObj.addOperation(name='printInfo')
67 75 #opObj11 = readUnitConfObj.addOperation(name='printNumberOfBlock')
68 76 #######################################################################
69 77 ################ OPERACIONES DOMINIO DEL TIEMPO########################
70 78 #######################################################################
71 79
72 80 procUnitConfObjA = controllerObj.addProcUnit(datatype='VoltageProc', inputId=readUnitConfObj.getId())
73 81
82 '''
83 # OJO SCOPE
84 opObj10 = procUnitConfObjA.addOperation(name='ScopePlot', optype='external')
85 opObj10.addParameter(name='id', value='10', format='int')
86 ##opObj10.addParameter(name='xmin', value='0', format='int')
87 ##opObj10.addParameter(name='xmax', value='50', format='int')
88 opObj10.addParameter(name='type', value='iq')
89 opObj10.addParameter(name='ymin', value='-1200', format='int')
90 opObj10.addParameter(name='ymax', value='1200', format='int')
91 opObj10.addParameter(name='save', value=figpath, format='str')
92 opObj10.addParameter(name='save_period', value=10, format='int')
93 '''
94 '''
74 95 opObj11 = procUnitConfObjA.addOperation(name='selectHeights')
75 96 opObj11.addParameter(name='minIndex', value='1', format='int')
76 97 # opObj11.addParameter(name='maxIndex', value='10000', format='int')
77 98 opObj11.addParameter(name='maxIndex', value='39980', format='int')
78
99 '''
79 100 #
80 101 # codigo64='1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,1,1,1,0,1,1,0,1,0,0,0,1,1,1,0,1,1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,0,0,0,1,0,0,1,0,1,1,1,0,0,0,1,0,'+\
81 102 # '1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,1,1,1,0,1,1,0,1,0,0,0,1,1,1,0,1,0,0,0,1,0,0,1,0,0,0,0,1,1,1,0,1,1,1,1,0,1,1,0,1,0,0,0,1,1,1,0,1'
82 103
83 104 #opObj11 = procUnitConfObjA.addOperation(name='setRadarFrequency')
84 105 #opObj11.addParameter(name='frequency', value='49920000')
85 106
86 107 '''
87 108 opObj11 = procUnitConfObjA.addOperation(name='PulsePair', optype='other')
88 109 opObj11.addParameter(name='n', value='625', format='int')#10
89 110 opObj11.addParameter(name='removeDC', value=1, format='int')
90 111 '''
91 112
92 113 # Ploteo TEST
93 114 '''
94 115 opObj11 = procUnitConfObjA.addOperation(name='PulsepairPowerPlot', optype='other')
95 116 opObj11 = procUnitConfObjA.addOperation(name='PulsepairSignalPlot', optype='other')
96 117 opObj11 = procUnitConfObjA.addOperation(name='PulsepairVelocityPlot', optype='other')
97 118 #opObj11.addParameter(name='xmax', value=8)
98 119 opObj11 = procUnitConfObjA.addOperation(name='PulsepairSpecwidthPlot', optype='other')
99 120 '''
100 121 # OJO SCOPE
101 122 #opObj10 = procUnitConfObjA.addOperation(name='ScopePlot', optype='external')
102 123 #opObj10.addParameter(name='id', value='10', format='int')
103 124 ##opObj10.addParameter(name='xmin', value='0', format='int')
104 125 ##opObj10.addParameter(name='xmax', value='50', format='int')
105 126 #opObj10.addParameter(name='type', value='iq')
106 127 ##opObj10.addParameter(name='ymin', value='-5000', format='int')
107 128 ##opObj10.addParameter(name='ymax', value='8500', format='int')
108 129 #opObj11.addParameter(name='save', value=figpath, format='str')
109 130 #opObj11.addParameter(name='save_period', value=10, format='int')
110 131
111 132 #opObj10 = procUnitConfObjA.addOperation(name='setH0')
112 133 #opObj10.addParameter(name='h0', value='-5000', format='float')
113 134
114 135 #opObj11 = procUnitConfObjA.addOperation(name='filterByHeights')
115 136 #opObj11.addParameter(name='window', value='1', format='int')
116 137
117 138 #codigo='1,1,-1,1,1,-1,1,-1,-1,1,-1,-1,-1,1,-1,-1,-1,1,-1,-1,-1,1,1,1,1,-1,-1,-1'
118 139 #opObj11 = procUnitConfObjSousy.addOperation(name='Decoder', optype='other')
119 140 #opObj11.addParameter(name='code', value=codigo, format='floatlist')
120 141 #opObj11.addParameter(name='nCode', value='1', format='int')
121 142 #opObj11.addParameter(name='nBaud', value='28', format='int')
122 143
123 144 #opObj11 = procUnitConfObjA.addOperation(name='CohInt', optype='other')
124 145 #opObj11.addParameter(name='n', value='100', format='int')
125 146
126 147 #######################################################################
127 148 ########## OPERACIONES ParametersProc########################
128 149 #######################################################################
129 150 ###procUnitConfObjB= controllerObj.addProcUnit(datatype='ParametersProc',inputId=procUnitConfObjA.getId())
130 151 '''
131 152
132 153 opObj11 = procUnitConfObjA.addOperation(name='PedestalInformation')
133 154 opObj11.addParameter(name='path_ped', value=path_ped)
134 155 opObj11.addParameter(name='path_adq', value=path_adq)
135 156 opObj11.addParameter(name='t_Interval_p', value='0.01', format='float')
136 157 opObj11.addParameter(name='n_Muestras_p', value='100', format='float')
137 158 opObj11.addParameter(name='blocksPerfile', value='100', format='int')
138 159 opObj11.addParameter(name='f_a_p', value='25', format='int')
139 160 opObj11.addParameter(name='online', value='0', format='int')
140 161
141 162 opObj11 = procUnitConfObjA.addOperation(name='Block360')
142 163 opObj11.addParameter(name='n', value='40', format='int')
143 164
144 165 opObj11= procUnitConfObjA.addOperation(name='WeatherPlot',optype='other')
145 166 opObj11.addParameter(name='save', value=figpath)
146 167 opObj11.addParameter(name='save_period', value=1)
147 168
148 169 8
149 170 '''
150 171
172
173
174 opObj11 = procUnitConfObjA.addOperation(name='CohInt', optype='other')
175 opObj11.addParameter(name='n', value='250', format='int')
176
151 177 #######################################################################
152 178 ########## OPERACIONES DOMINIO DE LA FRECUENCIA########################
153 179 #######################################################################
154 180
155 #procUnitConfObjB = controllerObj.addProcUnit(datatype='SpectraProc', inputId=procUnitConfObjA.getId())
156 #procUnitConfObjB.addParameter(name='nFFTPoints', value='32', format='int')
157 #procUnitConfObjB.addParameter(name='nProfiles', value='32', format='int')
158
181 procUnitConfObjB = controllerObj.addProcUnit(datatype='SpectraProc', inputId=procUnitConfObjA.getId())
182 procUnitConfObjB.addParameter(name='nFFTPoints', value='32', format='int')
183 procUnitConfObjB.addParameter(name='nProfiles', value='32', format='int')
184 '''
159 185 procUnitConfObjC = controllerObj.addProcUnit(datatype='SpectraHeisProc', inputId=procUnitConfObjA.getId())
160 186 #procUnitConfObjB.addParameter(name='nFFTPoints', value='64', format='int')
161 187 #procUnitConfObjB.addParameter(name='nProfiles', value='64', format='int')
162 188 opObj11 = procUnitConfObjC.addOperation(name='IncohInt4SpectraHeis', optype='other')
163 189 #opObj11.addParameter(name='timeInterval', value='4', format='int')
164 190 opObj11.addParameter(name='n', value='100', format='int')
165 191
166 192 #procUnitConfObjB.addParameter(name='pairsList', value='(0,0),(1,1),(0,1)', format='pairsList')
167 193
168 194 #opObj13 = procUnitConfObjB.addOperation(name='removeDC')
169 195 #opObj13.addParameter(name='mode', value='2', format='int')
170 196
171 197 #opObj11 = procUnitConfObjB.addOperation(name='IncohInt', optype='other')
172 198 #opObj11.addParameter(name='n', value='8', format='float')
173 199 #######################################################################
174 200 ########## PLOTEO DOMINIO DE LA FRECUENCIA#############################
175 201 #######################################################################
176 202 #----
177
203 '''
204 '''
178 205 opObj11 = procUnitConfObjC.addOperation(name='SpectraHeisPlot')
179 206 opObj11.addParameter(name='id', value='10', format='int')
180 207 opObj11.addParameter(name='wintitle', value='Spectra_Alturas', format='str')
181 208 #opObj11.addParameter(name='xmin', value=-100000, format='float')
182 209 #opObj11.addParameter(name='xmax', value=100000, format='float')
183 210 opObj11.addParameter(name='oneFigure', value=False,format='bool')
184 211 #opObj11.addParameter(name='zmin', value=-10, format='int')
185 212 #opObj11.addParameter(name='zmax', value=40, format='int')
186 213 opObj11.addParameter(name='ymin', value=10, format='int')
187 214 opObj11.addParameter(name='ymax', value=55, format='int')
188 215 opObj11.addParameter(name='grid', value=True, format='bool')
189 216 #opObj11.addParameter(name='showprofile', value='1', format='int')
190 217 opObj11.addParameter(name='save', value=figpath, format='str')
191 218 #opObj11.addParameter(name='save_period', value=10, format='int')
192
219 '''
193 220 '''
194 221 opObj11 = procUnitConfObjC.addOperation(name='RTIHeisPlot')
195 222 opObj11.addParameter(name='id', value='10', format='int')
196 223 opObj11.addParameter(name='wintitle', value='RTI_Alturas', format='str')
197 224 opObj11.addParameter(name='xmin', value=11.0, format='float')
198 225 opObj11.addParameter(name='xmax', value=18.0, format='float')
199 226 opObj11.addParameter(name='zmin', value=10, format='int')
200 227 opObj11.addParameter(name='zmax', value=30, format='int')
201 228 opObj11.addParameter(name='ymin', value=5, format='int')
202 229 opObj11.addParameter(name='ymax', value=28, format='int')
203 230 opObj11.addParameter(name='showprofile', value='1', format='int')
204 231 opObj11.addParameter(name='save', value=figpath, format='str')
205 232 opObj11.addParameter(name='save_period', value=10, format='int')
206 233 '''
207 '''
234
208 235 #SpectraPlot
209 236
210 237 opObj11 = procUnitConfObjB.addOperation(name='SpectraPlot', optype='external')
211 238 opObj11.addParameter(name='id', value='1', format='int')
212 239 opObj11.addParameter(name='wintitle', value='Spectra', format='str')
213 240 #opObj11.addParameter(name='xmin', value=-0.01, format='float')
214 241 #opObj11.addParameter(name='xmax', value=0.01, format='float')
215 242 opObj11.addParameter(name='zmin', value=dBmin, format='int')
216 243 opObj11.addParameter(name='zmax', value=dBmax, format='int')
217 244 #opObj11.addParameter(name='ymin', value=ymin, format='int')
218 245 #opObj11.addParameter(name='ymax', value=ymax, format='int')
219 246 opObj11.addParameter(name='showprofile', value='1', format='int')
220 247 opObj11.addParameter(name='save', value=figpath, format='str')
221 248 opObj11.addParameter(name='save_period', value=10, format='int')
222 249
250
223 251 #RTIPLOT
224 252
225 253 opObj11 = procUnitConfObjB.addOperation(name='RTIPlot', optype='external')
226 254 opObj11.addParameter(name='id', value='2', format='int')
227 255 opObj11.addParameter(name='wintitle', value='RTIPlot', format='str')
228 256 opObj11.addParameter(name='zmin', value=dBmin, format='int')
229 257 opObj11.addParameter(name='zmax', value=dBmax, format='int')
230 258 #opObj11.addParameter(name='ymin', value=ymin, format='int')
231 259 #opObj11.addParameter(name='ymax', value=ymax, format='int')
232 260 #opObj11.addParameter(name='xmin', value=15, format='int')
233 261 #opObj11.addParameter(name='xmax', value=16, format='int')
234 262
235 263 opObj11.addParameter(name='showprofile', value='1', format='int')
236 264 opObj11.addParameter(name='save', value=figpath, format='str')
237 265 opObj11.addParameter(name='save_period', value=10, format='int')
238 266
239 267 '''
240 268 # opObj11 = procUnitConfObjB.addOperation(name='CrossSpectraPlot', optype='other')
241 269 # opObj11.addParameter(name='id', value='3', format='int')
242 270 # opObj11.addParameter(name='wintitle', value='CrossSpectraPlot', format='str')
243 271 # opObj11.addParameter(name='ymin', value=ymin, format='int')
244 272 # opObj11.addParameter(name='ymax', value=ymax, format='int')
245 273 # opObj11.addParameter(name='phase_cmap', value='jet', format='str')
246 274 # opObj11.addParameter(name='zmin', value=dBmin, format='int')
247 275 # opObj11.addParameter(name='zmax', value=dBmax, format='int')
248 276 # opObj11.addParameter(name='figpath', value=figures_path, format='str')
249 277 # opObj11.addParameter(name='save', value=0, format='bool')
250 278 # opObj11.addParameter(name='pairsList', value='(0,1)', format='pairsList')
251 279 # #
252 280 # opObj11 = procUnitConfObjB.addOperation(name='CoherenceMap', optype='other')
253 281 # opObj11.addParameter(name='id', value='4', format='int')
254 282 # opObj11.addParameter(name='wintitle', value='Coherence', format='str')
255 283 # opObj11.addParameter(name='phase_cmap', value='jet', format='str')
256 284 # opObj11.addParameter(name='xmin', value=xmin, format='float')
257 285 # opObj11.addParameter(name='xmax', value=xmax, format='float')
258 286 # opObj11.addParameter(name='figpath', value=figures_path, format='str')
259 287 # opObj11.addParameter(name='save', value=0, format='bool')
260 288 # opObj11.addParameter(name='pairsList', value='(0,1)', format='pairsList')
261 289 #
262
290 '''
263 291 '''
264 292 #######################################################################
265 293 ############### UNIDAD DE ESCRITURA ###################################
266 294 #######################################################################
267 295 #opObj11 = procUnitConfObjB.addOperation(name='SpectraWriter', optype='other')
268 296 #opObj11.addParameter(name='path', value=wr_path)
269 297 #opObj11.addParameter(name='blocksPerFile', value='50', format='int')
270 298 print ("Escribiendo el archivo XML")
271 299 print ("Leyendo el archivo XML")
272 300 '''
273 301
274 302
275 303 controllerObj.start()
@@ -1,184 +1,185
1 1 # Ing. AVP
2 2 # 04/01/2022
3 3 # ARCHIVO DE LECTURA
4 4 import os, sys
5 5 import datetime
6 6 import time
7 7 import numpy
8 8 import json
9 9 from ext_met import getfirstFilefromPath,getDatavaluefromDirFilename
10 10 from schainpy.controller import Project
11 11 #-----------------------------------------------------------------------------------------
12 12 # path_ped = "/DATA_RM/TEST_PEDESTAL/P20211110-171003"
13 13 ## print("PATH PEDESTAL :",path_ped)
14 14
15 15 print("[SETUP]-RADAR METEOROLOGICO-")
16 16 path_ped = "/DATA_RM/TEST_PEDESTAL/P20211111-173856"
17 17 print("PATH PEDESTAL :",path_ped)
18 18 path_adq = "/DATA_RM/11"
19 path_adq = "/DATA_RM/10MHZDRONE/"
19 20 print("PATH DATA :",path_adq)
20 21
21 22
22 23 figpath_pp_rti = "/home/soporte/Pictures/TEST_PP_RTI"
23 24 print("PATH PP RTI :",figpath_pp_rti)
24 25 figpath_pp_ppi = "/home/soporte/Pictures/TEST_PP_PPI"
25 26 print("PATH PP PPI :",figpath_pp_ppi)
26 27 path_pp_save_int = "/DATA_RM/TEST_NEW_FORMAT"
27 28 print("PATH SAVE PP INT :",path_pp_save_int)
28 29 print(" ")
29 30 #-------------------------------------------------------------------------------------------
30 31 print("SELECCIONAR MODO: PPI (0) O RHI (1)")
31 32 mode_wr = 0
32 33 if mode_wr==0:
33 34 print("[ ON ] MODE PPI")
34 35 list_ped = getfirstFilefromPath(path=path_ped,meta="PE",ext=".hdf5")
35 36 ff_pedestal = list_ped[2]
36 37 azi_vel = getDatavaluefromDirFilename(path=path_ped,file=ff_pedestal,value="azi_vel")
37 38 V = round(azi_vel[0])
38 39 print("VELOCIDAD AZI :", int(numpy.mean(azi_vel)),"Β°/seg")
39 40 else:
40 41 print("[ ON ] MODE RHI")
41 42 list_ped = getfirstFilefromPath(path=path_ped,meta="PE",ext=".hdf5")
42 43 ff_pedestal = list_ped[2]
43 44 V = round(ele_vel[0])
44 45 ele_vel = getDatavaluefromDirFilename(path=path_ped,file=ff_pedestal,value="ele_vel")
45 46 print("VELOCIDAD ELE :", int(numpy.mean(ele_vel)),"Β°/seg")
46 47 print(" ")
47 48 #---------------------------------------------------------------------------------------
48 49 print("SELECCIONAR MODO: PULSE PAIR (0) O FREQUENCY (1)")
49 50 mode_proc = 0
50 51 if mode_proc==0:
51 52 print("[ ON ] MODE PULSEPAIR")
52 53 else:
53 54 print("[ ON ] MODE FREQUENCY")
54 55 ipp = 60.0
55 56 print("IPP(Km.) : %1.2f"%ipp)
56 57 ipp_sec = (ipp*1.0e3/150.0)*1.0e-6
57 58 print("IPP(useg.) : %1.2f"%(ipp_sec*(1.0e6)))
58 59 VEL=V
59 60 n= int(1/(VEL*ipp_sec))
60 61 print("NΒ° Profiles : ", n)
61 62 #---------------------------------------------------------------------------------------
62 63 plot_rti = 0
63 plot_ppi = 0
64 plot_ppi = 1
64 65 integration = 1
65 save = 1
66 save = 0
66 67 #---------------------------RANGO DE PLOTEO----------------------------------
67 68 dBmin = '1'
68 69 dBmax = '85'
69 70 xmin = '14'
70 71 xmax = '16'
71 72 ymin = '0'
72 73 ymax = '600'
73 74 #----------------------------------------------------------------------------
74 75 time.sleep(3)
75 76 #---------------------SIGNAL CHAIN ------------------------------------
76 77 desc_wr= {
77 78 'Data': {
78 79 'dataPP_POW': 'Power',
79 80 'utctime': 'Time',
80 81 'azimuth': 'az',
81 82 'elevation':'el'
82 83 },
83 84 'Metadata': {
84 85 'heightList': 'range',
85 86 'channelList': 'Channels'
86 87 }
87 88 }
88 89
89 90
90 91 desc = "USRP_WEATHER_RADAR"
91 92 filename = "USRP_processing.xml"
92 93 controllerObj = Project()
93 94 controllerObj.setup(id = '191', name='Test_USRP', description=desc)
94 95 #---------------------UNIDAD DE LECTURA--------------------------------
95 96 readUnitConfObj = controllerObj.addReadUnit(datatype='DigitalRFReader',
96 97 path=path_adq,
97 startDate="2021/11/11",#today,
98 endDate="2021/12/30",#today,
99 startTime='17:39:17',
98 startDate="2022/02/19",#today,
99 endDate="2022/02/18",#today,
100 startTime='00:00:00',
100 101 endTime='23:59:59',
101 102 delay=0,
102 103 #set=0,
103 104 online=0,
104 105 walk=1,
105 106 ippKm=ipp)
106 107
107 108 procUnitConfObjA = controllerObj.addProcUnit(datatype='VoltageProc',inputId=readUnitConfObj.getId())
108 109
109 110 opObj11 = procUnitConfObjA.addOperation(name='selectHeights')
110 111 opObj11.addParameter(name='minIndex', value='1', format='int')
111 112 # opObj11.addParameter(name='maxIndex', value='10000', format='int')
112 113 opObj11.addParameter(name='maxIndex', value='400', format='int')
113 114
114 115 if mode_proc==0:
115 116 opObj11 = procUnitConfObjA.addOperation(name='PulsePair', optype='other')
116 117 opObj11.addParameter(name='n', value=int(n), format='int')
117 118 procUnitConfObjB= controllerObj.addProcUnit(datatype='ParametersProc',inputId=procUnitConfObjA.getId())
118 119 # REVISAR EL test_sim00013.py
119 120 if plot_rti==1:
120 121 opObj11 = procUnitConfObjB.addOperation(name='GenericRTIPlot',optype='external')
121 122 opObj11.addParameter(name='attr_data', value='dataPP_POW')
122 123 opObj11.addParameter(name='colormap', value='jet')
123 124 opObj11.addParameter(name='xmin', value=xmin)
124 125 opObj11.addParameter(name='xmax', value=xmax)
125 126 opObj11.addParameter(name='zmin', value=dBmin)
126 127 opObj11.addParameter(name='zmax', value=dBmax)
127 128 opObj11.addParameter(name='save', value=figpath_pp_rti)
128 129 opObj11.addParameter(name='showprofile', value=0)
129 130 opObj11.addParameter(name='save_period', value=50)
130 131 if integration==1:
131 132 opObj11 = procUnitConfObjB.addOperation(name='PedestalInformation')
132 133 opObj11.addParameter(name='path_ped', value=path_ped)
133 134 opObj11.addParameter(name='t_Interval_p', value='0.01', format='float')
134 135 opObj11.addParameter(name='wr_exp', value='PPI')
135 136 #------------------------------------------------------------------------------
136 137 '''
137 138 opObj11.addParameter(name='Datatype', value='RadialSet')
138 139 opObj11.addParameter(name='Scantype', value='PPI')
139 140 opObj11.addParameter(name='Latitude', value='-11.96')
140 141 opObj11.addParameter(name='Longitud', value='-76.54')
141 142 opObj11.addParameter(name='Heading', value='293')
142 143 opObj11.addParameter(name='Height', value='293')
143 144 opObj11.addParameter(name='Waveform', value='OFM')
144 145 opObj11.addParameter(name='PRF', value='2000')
145 146 opObj11.addParameter(name='CreatedBy', value='WeatherRadarJROTeam')
146 147 opObj11.addParameter(name='ContactInformation', value='avaldez@igp.gob.pe')
147 148 '''
148 149 if plot_ppi==1:
149 150 opObj11 = procUnitConfObjB.addOperation(name='Block360')
150 151 opObj11.addParameter(name='n', value='10', format='int')
151 152 opObj11.addParameter(name='mode', value=mode_proc, format='int')
152 153 # este bloque funciona bien con divisores de 360 no olvidar 0 10 20 30 40 60 90 120 180
153 154 opObj11= procUnitConfObjB.addOperation(name='WeatherPlot',optype='other')
154 155 opObj11.addParameter(name='save', value=figpath_pp_ppi)
155 156 opObj11.addParameter(name='save_period', value=1)
156 157
157 158 if save==1:
158 159 opObj10 = procUnitConfObjB.addOperation(name='HDFWriter')
159 160 opObj10.addParameter(name='path',value=path_pp_save_int)
160 161 opObj10.addParameter(name='mode',value="weather")
161 162 opObj10.addParameter(name='type_data',value='F')
162 163 opObj10.addParameter(name='blocksPerFile',value='360',format='int')
163 164 #opObj10.addParameter(name='metadataList',value='utctimeInit,paramInterval,channelList,heightList,flagDataAsBlock',format='list')
164 165 opObj10.addParameter(name='metadataList',value='heightList,channelList,Typename,Datatype,Scantype,Latitude,Longitud,Heading,Height,Waveform,PRF,CreatedBy,ContactInformation',format='list')
165 166 #--------------------
166 167 opObj10.addParameter(name='Typename', value='Differential_Reflectivity')
167 168 opObj10.addParameter(name='Datatype', value='RadialSet')
168 169 opObj10.addParameter(name='Scantype', value='PPI')
169 170 opObj10.addParameter(name='Latitude', value='-11.96')
170 171 opObj10.addParameter(name='Longitud', value='-76.54')
171 172 opObj10.addParameter(name='Heading', value='293')
172 173 opObj10.addParameter(name='Height', value='293')
173 174 opObj10.addParameter(name='Waveform', value='OFM')
174 175 opObj10.addParameter(name='PRF', value='2000')
175 176 opObj10.addParameter(name='CreatedBy', value='WeatherRadarJROTeam')
176 177 opObj10.addParameter(name='ContactInformation', value='avaldez@igp.gob.pe')
177 178 #---------------------------------------------------
178 179 #opObj10.addParameter(name='dataList',value='dataPP_POW,dataPP_DOP,azimuth,elevation,utctime',format='list')#,format='list'
179 180 #opObj10.addParameter(name='metadataList',value='utctimeInit,timeZone,paramInterval,profileIndex,channelList,heightList,flagDataAsBlock',format='list')
180 181
181 182 opObj10.addParameter(name='dataList',value='dataPP_POW,azimuth,elevation,utctime',format='list')#,format='list'
182 183 opObj10.addParameter(name='description',value=json.dumps(desc_wr))
183 184
184 185 controllerObj.start()
@@ -1,122 +1,125
1 1 # Ing. AVP
2 2 # 04/01/2022
3 3 # ARCHIVO DE LECTURA
4 4 #---- DATA RHI --- 23 DE NOVIEMBRE DEL 2021 --- 23/11/2021---
5 5 #---- PEDESTAL ----------------------------------------------
6 6 #------- HORA 143826 /DATA_RM/TEST_PEDESTAL/P20211123-143826 14:38-15:10
7 7 #---- RADAR ----------------------------------------------
8 8 #------- 14:26-15:00
9 9 #------- /DATA_RM/DRONE/2MHZ_5V_ELEVACION/
10 10 #------- /DATA_RM/DRONE/2MHZ_5V_ELEVACION/ch0/2021-11-23T19-00-00
11 11
12 12 import os, sys
13 13 import datetime
14 14 import time
15 15 import numpy
16 16 from ext_met import getfirstFilefromPath,getDatavaluefromDirFilename
17 17 from schainpy.controller import Project
18 18 #-----------------------------------------------------------------------------------------
19 19 print("[SETUP]-RADAR METEOROLOGICO-")
20 20 path_ped = "/DATA_RM/TEST_PEDESTAL/P20211123-143826"
21 path_ped = "/DATA_RM/TEST_PEDESTAL/P20220217-172216"
21 22 print("PATH PEDESTAL :",path_ped)
22 23 path_adq = "/DATA_RM/DRONE/2MHZ_5V_ELEVACION/"
24 path_adq = "/DATA_RM/2MHZTEST/"
23 25 print("PATH DATA :",path_adq)
24 26 figpath_pp_rti = "/home/soporte/Pictures/TEST_PP_RHI"
25 27 print("PATH PP RTI :",figpath_pp_rti)
26 28 figpath_pp_rhi = "/home/soporte/Pictures/TEST_PP_RHI"
27 29 print("PATH PP RHI :",figpath_pp_rhi)
28 30 path_pp_save_int = "/DATA_RM/TEST_SAVE_PP_INT_RHI"
29 31 print("PATH SAVE PP INT :",path_pp_save_int)
30 32 print(" ")
31 33 #-------------------------------------------------------------------------------------------
32 34 print("SELECCIONAR MODO: PPI (0) O RHI (1)")
33 35 mode_wr = 1
34 36 if mode_wr==0:
35 37 print("[ ON ] MODE PPI")
36 38 list_ped = getfirstFilefromPath(path=path_ped,meta="PE",ext=".hdf5")
37 39 ff_pedestal = list_ped[2]
38 40 azi_vel = getDatavaluefromDirFilename(path=path_ped,file=ff_pedestal,value="azi_vel")
39 41 V = round(azi_vel[0])
40 42 print("VELOCIDAD AZI :", int(numpy.mean(azi_vel)),"Β°/seg")
41 43 else:
42 44 print("[ ON ] MODE RHI")
43 45 list_ped = getfirstFilefromPath(path=path_ped,meta="PE",ext=".hdf5")
44 46 ff_pedestal = list_ped[2]
45 47 ele_vel = getDatavaluefromDirFilename(path=path_ped,file=ff_pedestal,value="ele_vel")
46 48 V = round(ele_vel[0])
47 V = 10.0
49 V = 8.0#10.0
48 50 print("VELOCIDAD ELE :", int(numpy.mean(ele_vel)),"Β°/seg")
49 51 print(" ")
50 52 #---------------------------------------------------------------------------------------
51 53 print("SELECCIONAR MODO: PULSE PAIR (0) O FREQUENCY (1)")
52 54 mode_proc = 0
53 55 if mode_proc==0:
54 56 print("[ ON ] MODE PULSEPAIR")
55 57 else:
56 58 print("[ ON ] MODE FREQUENCY")
57 59 ipp = 60.0
58 60 print("IPP(Km.) : %1.2f"%ipp)
59 61 ipp_sec = (ipp*1.0e3/150.0)*1.0e-6
60 62 print("IPP(useg.) : %1.2f"%(ipp_sec*(1.0e6)))
61 63 VEL=V
62 64 n= int(1/(VEL*ipp_sec))
63 65 print("NΒ° Profiles : ", n)
64 66 #--------------------------------------------
65 67 plot_rti = 0
66 68 plot_rhi = 1
67 69 integration = 1
68 70 save = 0
69 71 #---------------------------RANGO DE PLOTEO----------------------------------
70 72 dBmin = '1'
71 73 dBmax = '85'
72 74 xmin = '17'
73 75 xmax = '17.25'
74 76 ymin = '0'
75 77 ymax = '600'
76 78 #----------------------------------------------------------------------------
77 79 time.sleep(3)
78 80 #---------------------SIGNAL CHAIN ------------------------------------
79 81 desc = "USRP_WEATHER_RADAR"
80 82 filename = "USRP_processing.xml"
81 83 controllerObj = Project()
82 84 controllerObj.setup(id = '191', name='Test_USRP', description=desc)
83 85 #---------------------UNIDAD DE LECTURA--------------------------------
84 86 readUnitConfObj = controllerObj.addReadUnit(datatype='DigitalRFReader',
85 87 path=path_adq,
86 startDate="2021/11/23",#today,
87 endDate="2021/12/30",#today,
88 startTime='14:38:23',
88 startDate="2022/02/17",#today,
89 endDate="2022/02/17",#today,
90 startTime='00:00:00',
89 91 endTime='23:59:59',
90 92 delay=0,
91 93 #set=0,
92 94 online=0,
93 95 walk=1,
94 96 ippKm=ipp)
95 97
96 98 procUnitConfObjA = controllerObj.addProcUnit(datatype='VoltageProc',inputId=readUnitConfObj.getId())
97 99
98 100 opObj11 = procUnitConfObjA.addOperation(name='selectHeights')
99 101 opObj11.addParameter(name='minIndex', value='1', format='int')
100 102 # opObj11.addParameter(name='maxIndex', value='10000', format='int')
101 103 opObj11.addParameter(name='maxIndex', value='400', format='int')
102 104
103 105 if mode_proc==0:
104 106 opObj11 = procUnitConfObjA.addOperation(name='PulsePair', optype='other')
105 107 opObj11.addParameter(name='n', value=int(n), format='int')
106 108 procUnitConfObjB= controllerObj.addProcUnit(datatype='ParametersProc',inputId=procUnitConfObjA.getId())
107 109
108 110 if integration==1:
109 111 opObj11 = procUnitConfObjB.addOperation(name='PedestalInformation')
110 112 opObj11.addParameter(name='path_ped', value=path_ped)
111 113 opObj11.addParameter(name='t_Interval_p', value='0.01', format='float')
114 opObj11.addParameter(name='wr_exp', value='RHI')
112 115
113 116 if plot_rhi==1:
114 117 opObj11 = procUnitConfObjB.addOperation(name='Block360')
115 118 opObj11.addParameter(name='n', value='10', format='int')
116 119 opObj11.addParameter(name='mode', value=mode_proc, format='int')
117 120 # este bloque funciona bien con divisores de 360 no olvidar 0 10 20 30 40 60 90 120 180
118 121 opObj11= procUnitConfObjB.addOperation(name='WeatherRHIPlot',optype='other')
119 122 opObj11.addParameter(name='save', value=figpath_pp_rhi)
120 123 opObj11.addParameter(name='save_period', value=1)
121 124
122 125 controllerObj.start()
@@ -1,123 +1,126
1 1 # Ing-AlexanderValdez
2 2 # Monitoreo de Pedestal
3 3
4 4 ############## IMPORTA LIBRERIAS ###################
5 5 import os,numpy,h5py
6 6 import sys,time
7 7 import matplotlib.pyplot as plt
8 8 ####################################################
9 9 #################################################################
10 10 # LA FECHA 21-10-20 CORRESPONDE A LAS PRUEBAS DEL DIA MIERCOLES
11 11 # 1:15:51 pm hasta 3:49:32 pm
12 12 #################################################################
13 13
14 14 #path_ped = '/DATA_RM/TEST_PEDESTAL/P20211012-082745'
15 15 path_ped = '/DATA_RM/TEST_PEDESTAL/P20211020-131248'
16 16 path_ped = '/DATA_RM/TEST_PEDESTAL/P20211110-171003'
17 17 path_ped = '/DATA_RM/TEST_PEDESTAL/P20211111-173856'
18 18 path_ped = '/DATA_RM/TEST_PEDESTAL/P20211123-143826'
19 path_ped = "/DATA_RM/TEST_PEDESTAL/P20220217-172216"
19 20 #path_ped = '/DATA_RM/TEST_PEDESTAL/P20211111-173409'
20 21 # Metodo para verificar numero
21 22 def isNumber(str):
22 23 try:
23 24 float(str)
24 25 return True
25 26 except:
26 27 return False
27 28 # Metodo para extraer el arreglo
28 29 def getDatavaluefromDirFilename(path,file,value):
29 30 dir_file= path+"/"+file
30 31 fp = h5py.File(dir_file,'r')
31 32 array = fp['Data'].get(value)[()]
32 33 fp.close()
33 34 return array
34 35
35 36 # LISTA COMPLETA DE ARCHIVOS HDF5 Pedestal
36 37 LIST= sorted(os.listdir(path_ped))
37 38 m=len(LIST)
38 39 print("TOTAL DE ARCHIVOS DE PEDESTAL:",m)
39 40 # Contadores temporales
40 41 k= 0
41 42 l= 0
42 43 t= 0
43 44 # Marca de tiempo temporal
44 45 time_ = numpy.zeros([m])
45 46 # creacion de
46 47 for i in range(m):
47 48 print("order:",i)
48 49 tmp_azi_pos = getDatavaluefromDirFilename(path=path_ped,file=LIST[i],value="azi_pos")
49 50 tmp_ele_pos = getDatavaluefromDirFilename(path=path_ped,file=LIST[i],value="ele_pos")
50 51 tmp_azi_vel = getDatavaluefromDirFilename(path=path_ped,file=LIST[i],value="azi_vel")
51 tmp_ele_vel = getDatavaluefromDirFilename(path=path_ped,file=LIST[i],value="azi_vel")# nuevo :D
52 tmp_ele_vel = getDatavaluefromDirFilename(path=path_ped,file=LIST[i],value="ele_vel")# nuevo :D
52 53
53 54 time_[i] = getDatavaluefromDirFilename(path=path_ped,file=LIST[i],value="utc")
54 55
55 56 k=k +tmp_azi_pos.shape[0]
56 57 l=l +tmp_ele_pos.shape[0]
57 58 t=t +tmp_azi_vel.shape[0]
58 59
59 60 print("TOTAL DE MUESTRAS, ARCHIVOS X100:",k)
60 61 time.sleep(5)
61 62 ######CREACION DE ARREGLOS CANTIDAD DE VALORES POR MUESTRA#################
62 63 azi_pos = numpy.zeros([k])
63 64 ele_pos = numpy.zeros([l])
64 65 time_azi_pos= numpy.zeros([k])
65 66 # Contadores temporales
66 67 p=0
67 68 r=0
68 69 z=0
69 70 # VARIABLES TMP para almacenar azimuth, elevacion y tiempo
70 71
71 72 #for filename in sorted(os.listdir(path_ped)):
72 73 # CONDICION POR LEER EN TIEMPO REAL NO OFFLINE
73 74
74 75 for filename in LIST:
75 tmp_azi_pos = getDatavaluefromDirFilename(path=path_ped,file=filename,value="azi_pos")
76 tmp_ele_pos = getDatavaluefromDirFilename(path=path_ped,file=filename,value="ele_pos")
76 #tmp_azi_pos = getDatavaluefromDirFilename(path=path_ped,file=filename,value="azi_pos")
77 #tmp_ele_pos = getDatavaluefromDirFilename(path=path_ped,file=filename,value="ele_pos")
78 tmp_azi_pos = getDatavaluefromDirFilename(path=path_ped,file=filename,value="ele_vel")
79 tmp_ele_pos = getDatavaluefromDirFilename(path=path_ped,file=filename,value="azi_vel")
77 80 # CONDICION POR LEER EN TIEMPO REAL NO OFFLINE
78 81
79 82 if z==(m-1):
80 83 tmp_azi_time=numpy.arange(time_[z],time_[z]+1,1/(tmp_azi_pos.shape[0]))
81 84 else:
82 85 tmp_azi_time=numpy.arange(time_[z],time_[z+1],(time_[z+1]-time_[z])/(tmp_azi_pos.shape[0]))
83 86
84 87 print(filename,time_[z])
85 88 print(z,tmp_azi_pos.shape[0])
86 89
87 90 i=0
88 91 for i in range(tmp_azi_pos.shape[0]):
89 92 index=p+i
90 93 azi_pos[index]=tmp_azi_pos[i]
91 94 time_azi_pos[index]=tmp_azi_time[i]
92 95 p=p+tmp_azi_pos.shape[0]
93 96 i=0
94 97 for i in range(tmp_ele_pos.shape[0]):
95 98 index=r+i
96 99 ele_pos[index]=tmp_ele_pos[i]
97 100 r=r+tmp_ele_pos.shape[0]
98 101
99 102
100 103 z+=1
101 104
102 105
103 106 ######## GRAFIQUEMOS Y VEAMOS LOS DATOS DEL Pedestal
104 107 fig, ax = plt.subplots(figsize=(16,8))
105 108 print(time_azi_pos.shape)
106 109 print(azi_pos.shape)
107 110 t=numpy.arange(time_azi_pos.shape[0])*0.01/(60.0)
108 111 plt.plot(t,azi_pos,label='AZIMUTH_POS',color='blue')
109 112
110 113 # AQUI ESTOY ADICIONANDO LA POSICION EN elevaciont=numpy.arange(len(ele_pos))*0.01/60.0
111 114 t=numpy.arange(len(ele_pos))*0.01/60.0
112 115 plt.plot(t,ele_pos,label='ELEVATION_POS',color='red')#*10
113 116
114 117 ax.set_xlim(0, 4)
115 118 ax.set_ylim(-5, 50)
116 119 plt.ylabel("Azimuth Position")
117 120 plt.xlabel("Muestra")
118 121 plt.title('Azimuth Position vs Muestra ', fontsize=20)
119 122 axes = plt.gca()
120 123 axes.yaxis.grid()
121 124 plt.xticks(fontsize=16)
122 125 plt.yticks(fontsize=16)
123 126 plt.show()
General Comments 0
You need to be logged in to leave comments. Login now