##// END OF EJS Templates
Last Commit foreva!
ebocanegra -
r1157:823d4012bd34
parent child
Show More
@@ -1,2161 +1,2393
1 1 import os
2 2 import datetime
3 3 import numpy
4 4 import inspect
5 5 from figure import Figure, isRealtime, isTimeInHourRange
6 6 from plotting_codes import *
7 7
8 class ParamLine(Figure):
9
10 isConfig = None
11
12 def __init__(self):
13
14 self.isConfig = False
15 self.WIDTH = 300
16 self.HEIGHT = 200
17 self.counter_imagwr = 0
18
19 def getSubplots(self):
20
21 nrow = self.nplots
22 ncol = 3
23 return nrow, ncol
24
25 def setup(self, id, nplots, wintitle, show):
26
27 self.nplots = nplots
28
29 self.createFigure(id=id,
30 wintitle=wintitle,
31 show=show)
32
33 nrow,ncol = self.getSubplots()
34 colspan = 3
35 rowspan = 1
36
37 for i in range(nplots):
38 self.addAxes(nrow, ncol, i, 0, colspan, rowspan)
39
40 def plot_iq(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax):
41 yreal = y[channelIndexList,:].real
42 yimag = y[channelIndexList,:].imag
43
44 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
45 xlabel = "Range (Km)"
46 ylabel = "Intensity - IQ"
47
48 if not self.isConfig:
49 nplots = len(channelIndexList)
50
51 self.setup(id=id,
52 nplots=nplots,
53 wintitle='',
54 show=show)
55
56 if xmin == None: xmin = numpy.nanmin(x)
57 if xmax == None: xmax = numpy.nanmax(x)
58 if ymin == None: ymin = min(numpy.nanmin(yreal),numpy.nanmin(yimag))
59 if ymax == None: ymax = max(numpy.nanmax(yreal),numpy.nanmax(yimag))
60
61 self.isConfig = True
62
63 self.setWinTitle(title)
64
65 for i in range(len(self.axesList)):
66 title = "Channel %d" %(i)
67 axes = self.axesList[i]
68
69 axes.pline(x, yreal[i,:],
70 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
71 xlabel=xlabel, ylabel=ylabel, title=title)
72
73 axes.addpline(x, yimag[i,:], idline=1, color="red", linestyle="solid", lw=2)
74
75 def plot_power(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax):
76 y = y[channelIndexList,:] * numpy.conjugate(y[channelIndexList,:])
77 yreal = y.real
78
79 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
80 xlabel = "Range (Km)"
81 ylabel = "Intensity"
82
83 if not self.isConfig:
84 nplots = len(channelIndexList)
85
86 self.setup(id=id,
87 nplots=nplots,
88 wintitle='',
89 show=show)
90
91 if xmin == None: xmin = numpy.nanmin(x)
92 if xmax == None: xmax = numpy.nanmax(x)
93 if ymin == None: ymin = numpy.nanmin(yreal)
94 if ymax == None: ymax = numpy.nanmax(yreal)
95
96 self.isConfig = True
97
98 self.setWinTitle(title)
99
100 for i in range(len(self.axesList)):
101 title = "Channel %d" %(i)
102 axes = self.axesList[i]
103 ychannel = yreal[i,:]
104 axes.pline(x, ychannel,
105 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
106 xlabel=xlabel, ylabel=ylabel, title=title)
107
108
109 def run(self, dataOut, id, wintitle="", channelList=None,
110 xmin=None, xmax=None, ymin=None, ymax=None, save=False,
111 figpath='./', figfile=None, show=True, wr_period=1,
112 ftp=False, server=None, folder=None, username=None, password=None):
113
114 """
115
116 Input:
117 dataOut :
118 id :
119 wintitle :
120 channelList :
121 xmin : None,
122 xmax : None,
123 ymin : None,
124 ymax : None,
125 """
126
127 if channelList == None:
128 channelIndexList = dataOut.channelIndexList
129 else:
130 channelIndexList = []
131 for channel in channelList:
132 if channel not in dataOut.channelList:
133 raise ValueError, "Channel %d is not in dataOut.channelList"
134 channelIndexList.append(dataOut.channelList.index(channel))
135
136 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
137
138 y = dataOut.RR
139
140 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
141 xlabel = "Range (Km)"
142 ylabel = "Intensity"
143
144 if not self.isConfig:
145 nplots = len(channelIndexList)
146
147 self.setup(id=id,
148 nplots=nplots,
149 wintitle='',
150 show=show)
151
152 if xmin == None: xmin = numpy.nanmin(x)
153 if xmax == None: xmax = numpy.nanmax(x)
154 if ymin == None: ymin = numpy.nanmin(y)
155 if ymax == None: ymax = numpy.nanmax(y)
156
157 self.isConfig = True
158
159 self.setWinTitle(title)
160
161 for i in range(len(self.axesList)):
162 title = "Channel %d" %(i)
163 axes = self.axesList[i]
164 ychannel = y[i,:]
165 axes.pline(x, ychannel,
166 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
167 xlabel=xlabel, ylabel=ylabel, title=title)
168
169
170 self.draw()
171
172 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S") + "_" + str(dataOut.profileIndex)
173 figfile = self.getFilename(name = str_datetime)
174
175 self.save(figpath=figpath,
176 figfile=figfile,
177 save=save,
178 ftp=ftp,
179 wr_period=wr_period,
180 thisDatetime=thisDatetime)
181
182
8 183
9 184 class SpcParamPlot(Figure):
10 185
11 186 isConfig = None
12 187 __nsubplots = None
13 188
14 189 WIDTHPROF = None
15 190 HEIGHTPROF = None
16 191 PREFIX = 'SpcParam'
17 192
18 193 def __init__(self, **kwargs):
19 194 Figure.__init__(self, **kwargs)
20 195 self.isConfig = False
21 196 self.__nsubplots = 1
22 197
23 198 self.WIDTH = 250
24 199 self.HEIGHT = 250
25 200 self.WIDTHPROF = 120
26 201 self.HEIGHTPROF = 0
27 202 self.counter_imagwr = 0
28 203
29 204 self.PLOT_CODE = SPEC_CODE
30 205
31 206 self.FTP_WEI = None
32 207 self.EXP_CODE = None
33 208 self.SUB_EXP_CODE = None
34 209 self.PLOT_POS = None
35 210
36 211 self.__xfilter_ena = False
37 212 self.__yfilter_ena = False
38 213
39 214 def getSubplots(self):
40 215
41 216 ncol = int(numpy.sqrt(self.nplots)+0.9)
42 217 nrow = int(self.nplots*1./ncol + 0.9)
43 218
44 219 return nrow, ncol
45 220
46 221 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
47 222
48 223 self.__showprofile = showprofile
49 224 self.nplots = nplots
50 225
51 226 ncolspan = 1
52 227 colspan = 1
53 228 if showprofile:
54 229 ncolspan = 3
55 230 colspan = 2
56 231 self.__nsubplots = 2
57 232
58 233 self.createFigure(id = id,
59 234 wintitle = wintitle,
60 235 widthplot = self.WIDTH + self.WIDTHPROF,
61 236 heightplot = self.HEIGHT + self.HEIGHTPROF,
62 237 show=show)
63 238
64 239 nrow, ncol = self.getSubplots()
65 240
66 241 counter = 0
67 242 for y in range(nrow):
68 243 for x in range(ncol):
69 244
70 245 if counter >= self.nplots:
71 246 break
72 247
73 248 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
74 249
75 250 if showprofile:
76 251 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
77 252
78 253 counter += 1
79 254
80 255 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
81 256 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
82 257 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
83 258 server=None, folder=None, username=None, password=None,
84 259 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
85 260 xaxis="frequency", colormap='jet', normFactor=None , Selector = 0):
86 261
87 262 """
88 263
89 264 Input:
90 265 dataOut :
91 266 id :
92 267 wintitle :
93 268 channelList :
94 269 showProfile :
95 270 xmin : None,
96 271 xmax : None,
97 272 ymin : None,
98 273 ymax : None,
99 274 zmin : None,
100 275 zmax : None
101 276 """
102 277 if realtime:
103 278 if not(isRealtime(utcdatatime = dataOut.utctime)):
104 279 print 'Skipping this plot function'
105 280 return
106 281
107 282 if channelList == None:
108 283 channelIndexList = dataOut.channelIndexList
109 284 else:
110 285 channelIndexList = []
111 286 for channel in channelList:
112 287 if channel not in dataOut.channelList:
113 288 raise ValueError, "Channel %d is not in dataOut.channelList" %channel
114 289 channelIndexList.append(dataOut.channelList.index(channel))
115 290
116 291 # if normFactor is None:
117 292 # factor = dataOut.normFactor
118 293 # else:
119 294 # factor = normFactor
120 295 if xaxis == "frequency":
121 296 x = dataOut.spcparam_range[0]
122 297 xlabel = "Frequency (kHz)"
123 298
124 299 elif xaxis == "time":
125 300 x = dataOut.spcparam_range[1]
126 301 xlabel = "Time (ms)"
127 302
128 303 else:
129 304 x = dataOut.spcparam_range[2]
130 305 xlabel = "Velocity (m/s)"
131 306 print "Vmax=",x[-1]
132 307
133 308 ylabel = "Range (km)"
134 309
135 310 y = dataOut.getHeiRange()
136 311
137 312 z = dataOut.SPCparam[Selector] /1966080.0#/ dataOut.normFactor#GauSelector] #dataOut.data_spc/factor
138 313 #print 'GausSPC', z[0,32,10:40]
139 314 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
140 315 zdB = 10*numpy.log10(z)
141 316
142 317 avg = numpy.average(z, axis=1)
143 318 avgdB = 10*numpy.log10(avg)
144 319
145 320 noise = dataOut.spc_noise
146 321 noisedB = 10*numpy.log10(noise)
147 322
148 323 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
149 324 title = wintitle + " Spectra"
150 325 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
151 326 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
152 327
153 328 if not self.isConfig:
154 329
155 330 nplots = len(channelIndexList)
156 331
157 332 self.setup(id=id,
158 333 nplots=nplots,
159 334 wintitle=wintitle,
160 335 showprofile=showprofile,
161 336 show=show)
162 337
163 338 if xmin == None: xmin = numpy.nanmin(x)
164 339 if xmax == None: xmax = numpy.nanmax(x)
165 340 if ymin == None: ymin = numpy.nanmin(y)
166 341 if ymax == None: ymax = numpy.nanmax(y)
167 342 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
168 343 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
169 344
170 345 self.FTP_WEI = ftp_wei
171 346 self.EXP_CODE = exp_code
172 347 self.SUB_EXP_CODE = sub_exp_code
173 348 self.PLOT_POS = plot_pos
174 349
175 350 self.isConfig = True
176 351
177 352 self.setWinTitle(title)
178 353
179 354 for i in range(self.nplots):
180 355 index = channelIndexList[i]
181 356 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
182 357 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[index], noisedB[index], str_datetime)
183 358 if len(dataOut.beam.codeList) != 0:
184 359 title = "Ch%d:%4.2fdB,%2.2f,%2.2f:%s" %(dataOut.channelList[index], noisedB[index], dataOut.beam.azimuthList[index], dataOut.beam.zenithList[index], str_datetime)
185 360
186 361 axes = self.axesList[i*self.__nsubplots]
187 362 axes.pcolor(x, y, zdB[index,:,:],
188 363 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
189 364 xlabel=xlabel, ylabel=ylabel, title=title, colormap=colormap,
190 365 ticksize=9, cblabel='')
191 366
192 367 if self.__showprofile:
193 368 axes = self.axesList[i*self.__nsubplots +1]
194 369 axes.pline(avgdB[index,:], y,
195 370 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
196 371 xlabel='dB', ylabel='', title='',
197 372 ytick_visible=False,
198 373 grid='x')
199 374
200 375 noiseline = numpy.repeat(noisedB[index], len(y))
201 376 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
202 377
203 378 self.draw()
204 379
205 380 if figfile == None:
206 381 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
207 382 name = str_datetime
208 383 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
209 384 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
210 385 figfile = self.getFilename(name)
211 386
212 387 self.save(figpath=figpath,
213 388 figfile=figfile,
214 389 save=save,
215 390 ftp=ftp,
216 391 wr_period=wr_period,
217 392 thisDatetime=thisDatetime)
218 393
219 394
220 395
221 396 class MomentsPlot(Figure):
222 397
223 398 isConfig = None
224 399 __nsubplots = None
225 400
226 401 WIDTHPROF = None
227 402 HEIGHTPROF = None
228 403 PREFIX = 'prm'
229 404
230 405 def __init__(self, **kwargs):
231 406 Figure.__init__(self, **kwargs)
232 407 self.isConfig = False
233 408 self.__nsubplots = 1
234 409
235 410 self.WIDTH = 280
236 411 self.HEIGHT = 250
237 412 self.WIDTHPROF = 120
238 413 self.HEIGHTPROF = 0
239 414 self.counter_imagwr = 0
240 415
241 416 self.PLOT_CODE = MOMENTS_CODE
242 417
243 418 self.FTP_WEI = None
244 419 self.EXP_CODE = None
245 420 self.SUB_EXP_CODE = None
246 421 self.PLOT_POS = None
247 422
248 423 def getSubplots(self):
249 424
250 425 ncol = int(numpy.sqrt(self.nplots)+0.9)
251 426 nrow = int(self.nplots*1./ncol + 0.9)
252 427
253 428 return nrow, ncol
254 429
255 430 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
256 431
257 432 self.__showprofile = showprofile
258 433 self.nplots = nplots
259 434
260 435 ncolspan = 1
261 436 colspan = 1
262 437 if showprofile:
263 438 ncolspan = 3
264 439 colspan = 2
265 440 self.__nsubplots = 2
266 441
267 442 self.createFigure(id = id,
268 443 wintitle = wintitle,
269 444 widthplot = self.WIDTH + self.WIDTHPROF,
270 445 heightplot = self.HEIGHT + self.HEIGHTPROF,
271 446 show=show)
272 447
273 448 nrow, ncol = self.getSubplots()
274 449
275 450 counter = 0
276 451 for y in range(nrow):
277 452 for x in range(ncol):
278 453
279 454 if counter >= self.nplots:
280 455 break
281 456
282 457 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
283 458
284 459 if showprofile:
285 460 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
286 461
287 462 counter += 1
288 463
289 464 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
290 465 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
291 466 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
292 467 server=None, folder=None, username=None, password=None,
293 468 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False):
294 469
295 470 """
296 471
297 472 Input:
298 473 dataOut :
299 474 id :
300 475 wintitle :
301 476 channelList :
302 477 showProfile :
303 478 xmin : None,
304 479 xmax : None,
305 480 ymin : None,
306 481 ymax : None,
307 482 zmin : None,
308 483 zmax : None
309 484 """
310 485
311 486 if dataOut.flagNoData:
312 487 return None
313 488
314 489 if realtime:
315 490 if not(isRealtime(utcdatatime = dataOut.utctime)):
316 491 print 'Skipping this plot function'
317 492 return
318 493
319 494 if channelList == None:
320 495 channelIndexList = dataOut.channelIndexList
321 496 else:
322 497 channelIndexList = []
323 498 for channel in channelList:
324 499 if channel not in dataOut.channelList:
325 500 raise ValueError, "Channel %d is not in dataOut.channelList"
326 501 channelIndexList.append(dataOut.channelList.index(channel))
327 502
328 503 factor = dataOut.normFactor
329 504 x = dataOut.abscissaList
330 505 y = dataOut.heightList
331 506
332 507 z = dataOut.data_pre[channelIndexList,:,:]/factor
333 508 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
334 509 avg = numpy.average(z, axis=1)
335 510 noise = dataOut.noise/factor
336 511
337 512 zdB = 10*numpy.log10(z)
338 513 avgdB = 10*numpy.log10(avg)
339 514 noisedB = 10*numpy.log10(noise)
340 515
341 516 #thisDatetime = dataOut.datatime
342 517 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
343 518 title = wintitle + " Parameters"
344 519 xlabel = "Velocity (m/s)"
345 520 ylabel = "Range (Km)"
346 521
347 522 update_figfile = False
348 523
349 524 if not self.isConfig:
350 525
351 526 nplots = len(channelIndexList)
352 527
353 528 self.setup(id=id,
354 529 nplots=nplots,
355 530 wintitle=wintitle,
356 531 showprofile=showprofile,
357 532 show=show)
358 533
359 534 if xmin == None: xmin = numpy.nanmin(x)
360 535 if xmax == None: xmax = numpy.nanmax(x)
361 536 if ymin == None: ymin = numpy.nanmin(y)
362 537 if ymax == None: ymax = numpy.nanmax(y)
363 538 if zmin == None: zmin = numpy.nanmin(avgdB)*0.9
364 539 if zmax == None: zmax = numpy.nanmax(avgdB)*0.9
365 540
366 541 self.FTP_WEI = ftp_wei
367 542 self.EXP_CODE = exp_code
368 543 self.SUB_EXP_CODE = sub_exp_code
369 544 self.PLOT_POS = plot_pos
370 545
371 546 self.isConfig = True
372 547 update_figfile = True
373 548
374 549 self.setWinTitle(title)
375 550
376 551 for i in range(self.nplots):
377 552 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
378 553 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[i], noisedB[i], str_datetime)
379 554 axes = self.axesList[i*self.__nsubplots]
380 555 axes.pcolor(x, y, zdB[i,:,:],
381 556 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
382 557 xlabel=xlabel, ylabel=ylabel, title=title,
383 558 ticksize=9, cblabel='')
384 559 #Mean Line
385 560 mean = dataOut.data_param[i, 1, :]
386 561 axes.addpline(mean, y, idline=0, color="black", linestyle="solid", lw=1)
387 562
388 563 if self.__showprofile:
389 564 axes = self.axesList[i*self.__nsubplots +1]
390 565 axes.pline(avgdB[i], y,
391 566 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
392 567 xlabel='dB', ylabel='', title='',
393 568 ytick_visible=False,
394 569 grid='x')
395 570
396 571 noiseline = numpy.repeat(noisedB[i], len(y))
397 572 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
398 573
399 574 self.draw()
400 575
401 576 self.save(figpath=figpath,
402 577 figfile=figfile,
403 578 save=save,
404 579 ftp=ftp,
405 580 wr_period=wr_period,
406 581 thisDatetime=thisDatetime)
407 582
408 583
409 584
410 585 class SkyMapPlot(Figure):
411 586
412 587 __isConfig = None
413 588 __nsubplots = None
414 589
415 590 WIDTHPROF = None
416 591 HEIGHTPROF = None
417 592 PREFIX = 'mmap'
418 593
419 594 def __init__(self, **kwargs):
420 595 Figure.__init__(self, **kwargs)
421 596 self.isConfig = False
422 597 self.__nsubplots = 1
423 598
424 599 # self.WIDTH = 280
425 600 # self.HEIGHT = 250
426 601 self.WIDTH = 600
427 602 self.HEIGHT = 600
428 603 self.WIDTHPROF = 120
429 604 self.HEIGHTPROF = 0
430 605 self.counter_imagwr = 0
431 606
432 607 self.PLOT_CODE = MSKYMAP_CODE
433 608
434 609 self.FTP_WEI = None
435 610 self.EXP_CODE = None
436 611 self.SUB_EXP_CODE = None
437 612 self.PLOT_POS = None
438 613
439 614 def getSubplots(self):
440 615
441 616 ncol = int(numpy.sqrt(self.nplots)+0.9)
442 617 nrow = int(self.nplots*1./ncol + 0.9)
443 618
444 619 return nrow, ncol
445 620
446 621 def setup(self, id, nplots, wintitle, showprofile=False, show=True):
447 622
448 623 self.__showprofile = showprofile
449 624 self.nplots = nplots
450 625
451 626 ncolspan = 1
452 627 colspan = 1
453 628
454 629 self.createFigure(id = id,
455 630 wintitle = wintitle,
456 631 widthplot = self.WIDTH, #+ self.WIDTHPROF,
457 632 heightplot = self.HEIGHT,# + self.HEIGHTPROF,
458 633 show=show)
459 634
460 635 nrow, ncol = 1,1
461 636 counter = 0
462 637 x = 0
463 638 y = 0
464 639 self.addAxes(1, 1, 0, 0, 1, 1, True)
465 640
466 641 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=False,
467 642 tmin=0, tmax=24, timerange=None,
468 643 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
469 644 server=None, folder=None, username=None, password=None,
470 645 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False):
471 646
472 647 """
473 648
474 649 Input:
475 650 dataOut :
476 651 id :
477 652 wintitle :
478 653 channelList :
479 654 showProfile :
480 655 xmin : None,
481 656 xmax : None,
482 657 ymin : None,
483 658 ymax : None,
484 659 zmin : None,
485 660 zmax : None
486 661 """
487 662
488 663 arrayParameters = dataOut.data_param
489 664 error = arrayParameters[:,-1]
490 665 indValid = numpy.where(error == 0)[0]
491 666 finalMeteor = arrayParameters[indValid,:]
492 667 finalAzimuth = finalMeteor[:,3]
493 668 finalZenith = finalMeteor[:,4]
494 669
495 670 x = finalAzimuth*numpy.pi/180
496 671 y = finalZenith
497 672 x1 = [dataOut.ltctime, dataOut.ltctime]
498 673
499 674 #thisDatetime = dataOut.datatime
500 675 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
501 676 title = wintitle + " Parameters"
502 677 xlabel = "Zonal Zenith Angle (deg) "
503 678 ylabel = "Meridional Zenith Angle (deg)"
504 679 update_figfile = False
505 680
506 681 if not self.isConfig:
507 682
508 683 nplots = 1
509 684
510 685 self.setup(id=id,
511 686 nplots=nplots,
512 687 wintitle=wintitle,
513 688 showprofile=showprofile,
514 689 show=show)
515 690
516 691 if self.xmin is None and self.xmax is None:
517 692 self.xmin, self.xmax = self.getTimeLim(x1, tmin, tmax, timerange)
518 693
519 694 if timerange != None:
520 695 self.timerange = timerange
521 696 else:
522 697 self.timerange = self.xmax - self.xmin
523 698
524 699 self.FTP_WEI = ftp_wei
525 700 self.EXP_CODE = exp_code
526 701 self.SUB_EXP_CODE = sub_exp_code
527 702 self.PLOT_POS = plot_pos
528 703 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
529 704 self.firstdate = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
530 705 self.isConfig = True
531 706 update_figfile = True
532 707
533 708 self.setWinTitle(title)
534 709
535 710 i = 0
536 711 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
537 712
538 713 axes = self.axesList[i*self.__nsubplots]
539 714 nevents = axes.x_buffer.shape[0] + x.shape[0]
540 715 title = "Meteor Detection Sky Map\n %s - %s \n Number of events: %5.0f\n" %(self.firstdate,str_datetime,nevents)
541 716 axes.polar(x, y,
542 717 title=title, xlabel=xlabel, ylabel=ylabel,
543 718 ticksize=9, cblabel='')
544 719
545 720 self.draw()
546 721
547 722 self.save(figpath=figpath,
548 723 figfile=figfile,
549 724 save=save,
550 725 ftp=ftp,
551 726 wr_period=wr_period,
552 727 thisDatetime=thisDatetime,
553 728 update_figfile=update_figfile)
554 729
555 730 if dataOut.ltctime >= self.xmax:
556 731 self.isConfigmagwr = wr_period
557 732 self.isConfig = False
558 733 update_figfile = True
559 734 axes.__firsttime = True
560 735 self.xmin += self.timerange
561 736 self.xmax += self.timerange
562 737
563 738
564 739
565 740
566 741 class WindProfilerPlot(Figure):
567 742
568 743 __isConfig = None
569 744 __nsubplots = None
570 745
571 746 WIDTHPROF = None
572 747 HEIGHTPROF = None
573 748 PREFIX = 'wind'
574 749
575 750 def __init__(self, **kwargs):
576 751 Figure.__init__(self, **kwargs)
577 752 self.timerange = None
578 753 self.isConfig = False
579 754 self.__nsubplots = 1
580 755
581 756 self.WIDTH = 800
582 757 self.HEIGHT = 300
583 758 self.WIDTHPROF = 120
584 759 self.HEIGHTPROF = 0
585 760 self.counter_imagwr = 0
586 761
587 762 self.PLOT_CODE = WIND_CODE
588 763
589 764 self.FTP_WEI = None
590 765 self.EXP_CODE = None
591 766 self.SUB_EXP_CODE = None
592 767 self.PLOT_POS = None
593 768 self.tmin = None
594 769 self.tmax = None
595 770
596 771 self.xmin = None
597 772 self.xmax = None
598 773
599 774 self.figfile = None
600 775
601 776 def getSubplots(self):
602 777
603 778 ncol = 1
604 779 nrow = self.nplots
605 780
606 781 return nrow, ncol
607 782
608 783 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
609 784
610 785 self.__showprofile = showprofile
611 786 self.nplots = nplots
612 787
613 788 ncolspan = 1
614 789 colspan = 1
615 790
616 791 self.createFigure(id = id,
617 792 wintitle = wintitle,
618 793 widthplot = self.WIDTH + self.WIDTHPROF,
619 794 heightplot = self.HEIGHT + self.HEIGHTPROF,
620 795 show=show)
621 796
622 797 nrow, ncol = self.getSubplots()
623 798
624 799 counter = 0
625 800 for y in range(nrow):
626 801 if counter >= self.nplots:
627 802 break
628 803
629 804 self.addAxes(nrow, ncol*ncolspan, y, 0, colspan, 1)
630 805 counter += 1
631 806
632 807 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='False',
633 808 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
634 809 zmax_ver = None, zmin_ver = None, SNRmin = None, SNRmax = None,
635 810 timerange=None, SNRthresh = None,
636 811 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
637 812 server=None, folder=None, username=None, password=None,
638 813 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
639 814 """
640 815
641 816 Input:
642 817 dataOut :
643 818 id :
644 819 wintitle :
645 820 channelList :
646 821 showProfile :
647 822 xmin : None,
648 823 xmax : None,
649 824 ymin : None,
650 825 ymax : None,
651 826 zmin : None,
652 827 zmax : None
653 828 """
654 829
655 830 # if timerange is not None:
656 831 # self.timerange = timerange
657 832 #
658 833 # tmin = None
659 834 # tmax = None
660 835
661 836 x = dataOut.getTimeRange1(dataOut.paramInterval)
662 837 y = dataOut.heightList
663 838 z = dataOut.data_output.copy()
664 839 nplots = z.shape[0] #Number of wind dimensions estimated
665 840 nplotsw = nplots
666 841
667 842
668 843 #If there is a SNR function defined
669 844 if dataOut.data_SNR is not None:
670 845 nplots += 1
671 846 SNR = dataOut.data_SNR[0]
672 847 SNRavg = SNR#numpy.average(SNR, axis=0)
673 848
674 849 SNRdB = 10*numpy.log10(SNR)
675 850 SNRavgdB = 10*numpy.log10(SNRavg)
676 851
677 852 if SNRthresh == None:
678 853 SNRthresh = -5.0
679 854 ind = numpy.where(SNRavg < 10**(SNRthresh/10))[0]
680 855
681 856 for i in range(nplotsw):
682 857 z[i,ind] = numpy.nan
683 858
684 859 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
685 860 #thisDatetime = datetime.datetime.now()
686 861 title = wintitle + "Wind"
687 862 xlabel = ""
688 863 ylabel = "Height (km)"
689 864 update_figfile = False
690 865
691 866 if not self.isConfig:
692 867
693 868 self.setup(id=id,
694 869 nplots=nplots,
695 870 wintitle=wintitle,
696 871 showprofile=showprofile,
697 872 show=show)
698 873
699 874 if timerange is not None:
700 875 self.timerange = timerange
701 876
702 877 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
703 878
704 879 if ymin == None: ymin = numpy.nanmin(y)
705 880 if ymax == None: ymax = numpy.nanmax(y)
706 881
707 882 if zmax == None: zmax = numpy.nanmax(abs(z[range(2),:]))
708 883 #if numpy.isnan(zmax): zmax = 50
709 884 if zmin == None: zmin = -zmax
710 885
711 886 if nplotsw == 3:
712 887 if zmax_ver == None: zmax_ver = numpy.nanmax(abs(z[2,:]))
713 888 if zmin_ver == None: zmin_ver = -zmax_ver
714 889
715 890 if dataOut.data_SNR is not None:
716 891 if SNRmin == None: SNRmin = numpy.nanmin(SNRavgdB)
717 892 if SNRmax == None: SNRmax = numpy.nanmax(SNRavgdB)
718 893
719 894
720 895 self.FTP_WEI = ftp_wei
721 896 self.EXP_CODE = exp_code
722 897 self.SUB_EXP_CODE = sub_exp_code
723 898 self.PLOT_POS = plot_pos
724 899
725 900 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
726 901 self.isConfig = True
727 902 self.figfile = figfile
728 903 update_figfile = True
729 904
730 905 self.setWinTitle(title)
731 906
732 907 if ((self.xmax - x[1]) < (x[1]-x[0])):
733 908 x[1] = self.xmax
734 909
735 910 strWind = ['Zonal', 'Meridional', 'Vertical']
736 911 strCb = ['Velocity (m/s)','Velocity (m/s)','Velocity (cm/s)']
737 912 zmaxVector = [zmax, zmax, zmax_ver]
738 913 zminVector = [zmin, zmin, zmin_ver]
739 914 windFactor = [1,1,100]
740 915
741 916 for i in range(nplotsw):
742 917
743 918 title = "%s Wind: %s" %(strWind[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
744 919 axes = self.axesList[i*self.__nsubplots]
745 920
746 921 z1 = z[i,:].reshape((1,-1))*windFactor[i]
747 922
748 923 print 'x', x
749 924 print datetime.datetime.utcfromtimestamp(x[0])
750 925 print datetime.datetime.utcfromtimestamp(x[1])
751 926
752 927 #z1=numpy.ma.masked_where(z1==0.,z1)
753 928
754 929 axes.pcolorbuffer(x, y, z1,
755 930 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zminVector[i], zmax=zmaxVector[i],
756 931 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
757 932 ticksize=9, cblabel=strCb[i], cbsize="1%", colormap="seismic" )
758 933
759 934 if dataOut.data_SNR is not None:
760 935 i += 1
761 936 title = "Signal Noise Ratio (SNR): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
762 937 axes = self.axesList[i*self.__nsubplots]
763 938 SNRavgdB = SNRavgdB.reshape((1,-1))
764 939 axes.pcolorbuffer(x, y, SNRavgdB,
765 940 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
766 941 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
767 942 ticksize=9, cblabel='', cbsize="1%", colormap="jet")
768 943
769 944 self.draw()
770 945
771 946 self.save(figpath=figpath,
772 947 figfile=figfile,
773 948 save=save,
774 949 ftp=ftp,
775 950 wr_period=wr_period,
776 951 thisDatetime=thisDatetime,
777 952 update_figfile=update_figfile)
778 953
779 954 if dataOut.ltctime + dataOut.paramInterval >= self.xmax:
780 955 self.counter_imagwr = wr_period
781 956 self.isConfig = False
782 957 update_figfile = True
783 958
784 959
785 960 class ParametersPlot(Figure):
786 961
787 962 __isConfig = None
788 963 __nsubplots = None
789 964
790 965 WIDTHPROF = None
791 966 HEIGHTPROF = None
792 967 PREFIX = 'param'
793 968
794 969 nplots = None
795 970 nchan = None
796 971
797 972 def __init__(self, **kwargs):
798 973 Figure.__init__(self, **kwargs)
799 974 self.timerange = None
800 975 self.isConfig = False
801 976 self.__nsubplots = 1
802 977
803 self.WIDTH = 800
804 self.HEIGHT = 250
978 self.WIDTH = 300
979 self.HEIGHT = 550
805 980 self.WIDTHPROF = 120
806 981 self.HEIGHTPROF = 0
807 982 self.counter_imagwr = 0
808 983
809 984 self.PLOT_CODE = RTI_CODE
810 985
811 986 self.FTP_WEI = None
812 987 self.EXP_CODE = None
813 988 self.SUB_EXP_CODE = None
814 989 self.PLOT_POS = None
815 990 self.tmin = None
816 991 self.tmax = None
817 992
818 993 self.xmin = None
819 994 self.xmax = None
820 995
821 996 self.figfile = None
822 997
823 998 def getSubplots(self):
824 999
825 1000 ncol = 1
826 1001 nrow = self.nplots
827 1002
828 1003 return nrow, ncol
829 1004
830 1005 def setup(self, id, nplots, wintitle, show=True):
831 1006
832 1007 self.nplots = nplots
833 1008
834 1009 ncolspan = 1
835 1010 colspan = 1
836 1011
837 1012 self.createFigure(id = id,
838 1013 wintitle = wintitle,
839 1014 widthplot = self.WIDTH + self.WIDTHPROF,
840 1015 heightplot = self.HEIGHT + self.HEIGHTPROF,
841 1016 show=show)
842 1017
843 1018 nrow, ncol = self.getSubplots()
844 1019
845 1020 counter = 0
846 1021 for y in range(nrow):
847 1022 for x in range(ncol):
848 1023
849 1024 if counter >= self.nplots:
850 1025 break
851 1026
852 1027 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
853 1028
854 1029 counter += 1
855 1030
856 1031 def run(self, dataOut, id, wintitle="", channelList=None, paramIndex = 0, colormap="jet",
857 1032 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, timerange=None,
858 1033 showSNR=False, SNRthresh = -numpy.inf, SNRmin=None, SNRmax=None,
859 1034 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
860 1035 server=None, folder=None, username=None, password=None,
861 1036 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, HEIGHT=None):
862 1037 """
863 1038
864 1039 Input:
865 1040 dataOut :
866 1041 id :
867 1042 wintitle :
868 1043 channelList :
869 1044 showProfile :
870 1045 xmin : None,
871 1046 xmax : None,
872 1047 ymin : None,
873 1048 ymax : None,
874 1049 zmin : None,
875 1050 zmax : None
876 1051 """
877 1052
878 1053 if HEIGHT is not None:
879 1054 self.HEIGHT = HEIGHT
880 1055
881 1056
882 1057 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
883 1058 return
884 1059
885 1060 if channelList == None:
886 1061 channelIndexList = range(dataOut.data_param.shape[0])
887 1062 else:
888 1063 channelIndexList = []
889 1064 for channel in channelList:
890 1065 if channel not in dataOut.channelList:
891 1066 raise ValueError, "Channel %d is not in dataOut.channelList"
892 1067 channelIndexList.append(dataOut.channelList.index(channel))
893 1068
894 1069 x = dataOut.getTimeRange1(dataOut.paramInterval)
895 1070 y = dataOut.getHeiRange()
896 1071
897 1072 if dataOut.data_param.ndim == 3:
898 1073 z = dataOut.data_param[channelIndexList,paramIndex,:]
899 1074 else:
900 1075 z = dataOut.data_param[channelIndexList,:]
901 1076
902 1077 if showSNR:
903 1078 #SNR data
904 1079 SNRarray = dataOut.data_SNR[channelIndexList,:]
905 1080 SNRdB = 10*numpy.log10(SNRarray)
906 1081 ind = numpy.where(SNRdB < SNRthresh)
907 1082 z[ind] = numpy.nan
908 1083
909 1084 thisDatetime = dataOut.datatime
910 1085 # thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
911 1086 title = wintitle + " Parameters Plot" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
912 1087 xlabel = ""
913 ylabel = "Range (Km)"
1088 ylabel = "Range (km)"
914 1089
915 1090 update_figfile = False
916 1091
917 1092 if not self.isConfig:
918 1093
919 1094 nchan = len(channelIndexList)
920 1095 self.nchan = nchan
921 1096 self.plotFact = 1
922 1097 nplots = nchan
923 1098
924 1099 if showSNR:
925 1100 nplots = nchan*2
926 1101 self.plotFact = 2
927 1102 if SNRmin == None: SNRmin = numpy.nanmin(SNRdB)
928 1103 if SNRmax == None: SNRmax = numpy.nanmax(SNRdB)
929 1104
930 1105 self.setup(id=id,
931 1106 nplots=nplots,
932 1107 wintitle=wintitle,
933 1108 show=show)
934 1109
935 1110 if timerange != None:
936 1111 self.timerange = timerange
937 1112
938 1113 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
939 1114
940 1115 if ymin == None: ymin = numpy.nanmin(y)
941 1116 if ymax == None: ymax = numpy.nanmax(y)
942 1117 if zmin == None: zmin = numpy.nanmin(z)
943 1118 if zmax == None: zmax = numpy.nanmax(z)
944 1119
945 1120 self.FTP_WEI = ftp_wei
946 1121 self.EXP_CODE = exp_code
947 1122 self.SUB_EXP_CODE = sub_exp_code
948 1123 self.PLOT_POS = plot_pos
949 1124
950 1125 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
951 1126 self.isConfig = True
952 1127 self.figfile = figfile
953 1128 update_figfile = True
954 1129
955 1130 self.setWinTitle(title)
956 1131
957 for i in range(self.nchan):
958 index = channelIndexList[i]
959 title = "Channel %d: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
960 axes = self.axesList[i*self.plotFact]
961 z1 = z[i,:].reshape((1,-1))
962 axes.pcolorbuffer(x, y, z1,
963 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
964 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
965 ticksize=9, cblabel='', cbsize="1%",colormap=colormap)
966
967 if showSNR:
968 title = "Channel %d SNR: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
969 axes = self.axesList[i*self.plotFact + 1]
970 SNRdB1 = SNRdB[i,:].reshape((1,-1))
971 axes.pcolorbuffer(x, y, SNRdB1,
972 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
973 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
974 ticksize=9, cblabel='', cbsize="1%",colormap='jet')
1132 # for i in range(self.nchan):
1133 # index = channelIndexList[i]
1134 # title = "Channel %d: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1135 # axes = self.axesList[i*self.plotFact]
1136 # z1 = z[i,:].reshape((1,-1))
1137 # axes.pcolorbuffer(x, y, z1,
1138 # xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
1139 # xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1140 # ticksize=9, cblabel='', cbsize="1%",colormap=colormap)
1141 #
1142 # if showSNR:
1143 # title = "Channel %d SNR: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1144 # axes = self.axesList[i*self.plotFact + 1]
1145 # SNRdB1 = SNRdB[i,:].reshape((1,-1))
1146 # axes.pcolorbuffer(x, y, SNRdB1,
1147 # xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1148 # xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1149 # ticksize=9, cblabel='', cbsize="1%",colormap='jet')
1150
1151 i=0
1152 index = channelIndexList[i]
1153 title = "Factor de reflectividad Z [dBZ]"
1154 axes = self.axesList[i*self.plotFact]
1155 z1 = z[i,:].reshape((1,-1))
1156 axes.pcolorbuffer(x, y, z1,
1157 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
1158 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1159 ticksize=9, cblabel='', cbsize="1%",colormap=colormap)
1160
1161 if showSNR:
1162 title = "Channel %d SNR: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1163 axes = self.axesList[i*self.plotFact + 1]
1164 SNRdB1 = SNRdB[i,:].reshape((1,-1))
1165 axes.pcolorbuffer(x, y, SNRdB1,
1166 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1167 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1168 ticksize=9, cblabel='', cbsize="1%",colormap='jet')
1169
1170 i=1
1171 index = channelIndexList[i]
1172 title = "Velocidad vertical Doppler [m/s]"
1173 axes = self.axesList[i*self.plotFact]
1174 z1 = z[i,:].reshape((1,-1))
1175 axes.pcolorbuffer(x, y, z1,
1176 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=-10, zmax=10,
1177 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1178 ticksize=9, cblabel='', cbsize="1%",colormap='seismic_r')
1179
1180 if showSNR:
1181 title = "Channel %d SNR: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1182 axes = self.axesList[i*self.plotFact + 1]
1183 SNRdB1 = SNRdB[i,:].reshape((1,-1))
1184 axes.pcolorbuffer(x, y, SNRdB1,
1185 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1186 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1187 ticksize=9, cblabel='', cbsize="1%",colormap='jet')
1188
1189 i=2
1190 index = channelIndexList[i]
1191 title = "Intensidad de lluvia [mm/h]"
1192 axes = self.axesList[i*self.plotFact]
1193 z1 = z[i,:].reshape((1,-1))
1194 axes.pcolorbuffer(x, y, z1,
1195 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=0, zmax=40,
1196 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1197 ticksize=9, cblabel='', cbsize="1%",colormap='ocean_r')
1198
1199 if showSNR:
1200 title = "Channel %d SNR: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1201 axes = self.axesList[i*self.plotFact + 1]
1202 SNRdB1 = SNRdB[i,:].reshape((1,-1))
1203 axes.pcolorbuffer(x, y, SNRdB1,
1204 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1205 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1206 ticksize=9, cblabel='', cbsize="1%",colormap='jet')
975 1207
976 1208
977 1209 self.draw()
978 1210
979 1211 if dataOut.ltctime >= self.xmax:
980 1212 self.counter_imagwr = wr_period
981 1213 self.isConfig = False
982 1214 update_figfile = True
983 1215
984 1216 self.save(figpath=figpath,
985 1217 figfile=figfile,
986 1218 save=save,
987 1219 ftp=ftp,
988 1220 wr_period=wr_period,
989 1221 thisDatetime=thisDatetime,
990 1222 update_figfile=update_figfile)
991 1223
992 1224
993 1225
994 1226 class Parameters1Plot(Figure):
995 1227
996 1228 __isConfig = None
997 1229 __nsubplots = None
998 1230
999 1231 WIDTHPROF = None
1000 1232 HEIGHTPROF = None
1001 1233 PREFIX = 'prm'
1002 1234
1003 1235 def __init__(self, **kwargs):
1004 1236 Figure.__init__(self, **kwargs)
1005 1237 self.timerange = 2*60*60
1006 1238 self.isConfig = False
1007 1239 self.__nsubplots = 1
1008 1240
1009 1241 self.WIDTH = 800
1010 1242 self.HEIGHT = 180
1011 1243 self.WIDTHPROF = 120
1012 1244 self.HEIGHTPROF = 0
1013 1245 self.counter_imagwr = 0
1014 1246
1015 1247 self.PLOT_CODE = PARMS_CODE
1016 1248
1017 1249 self.FTP_WEI = None
1018 1250 self.EXP_CODE = None
1019 1251 self.SUB_EXP_CODE = None
1020 1252 self.PLOT_POS = None
1021 1253 self.tmin = None
1022 1254 self.tmax = None
1023 1255
1024 1256 self.xmin = None
1025 1257 self.xmax = None
1026 1258
1027 1259 self.figfile = None
1028 1260
1029 1261 def getSubplots(self):
1030 1262
1031 1263 ncol = 1
1032 1264 nrow = self.nplots
1033 1265
1034 1266 return nrow, ncol
1035 1267
1036 1268 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1037 1269
1038 1270 self.__showprofile = showprofile
1039 1271 self.nplots = nplots
1040 1272
1041 1273 ncolspan = 1
1042 1274 colspan = 1
1043 1275
1044 1276 self.createFigure(id = id,
1045 1277 wintitle = wintitle,
1046 1278 widthplot = self.WIDTH + self.WIDTHPROF,
1047 1279 heightplot = self.HEIGHT + self.HEIGHTPROF,
1048 1280 show=show)
1049 1281
1050 1282 nrow, ncol = self.getSubplots()
1051 1283
1052 1284 counter = 0
1053 1285 for y in range(nrow):
1054 1286 for x in range(ncol):
1055 1287
1056 1288 if counter >= self.nplots:
1057 1289 break
1058 1290
1059 1291 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1060 1292
1061 1293 if showprofile:
1062 1294 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
1063 1295
1064 1296 counter += 1
1065 1297
1066 1298 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=False,
1067 1299 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,timerange=None,
1068 1300 parameterIndex = None, onlyPositive = False,
1069 1301 SNRthresh = -numpy.inf, SNR = True, SNRmin = None, SNRmax = None, onlySNR = False,
1070 1302 DOP = True,
1071 1303 zlabel = "", parameterName = "", parameterObject = "data_param",
1072 1304 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
1073 1305 server=None, folder=None, username=None, password=None,
1074 1306 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1075 1307 #print inspect.getargspec(self.run).args
1076 1308 """
1077 1309
1078 1310 Input:
1079 1311 dataOut :
1080 1312 id :
1081 1313 wintitle :
1082 1314 channelList :
1083 1315 showProfile :
1084 1316 xmin : None,
1085 1317 xmax : None,
1086 1318 ymin : None,
1087 1319 ymax : None,
1088 1320 zmin : None,
1089 1321 zmax : None
1090 1322 """
1091 1323
1092 1324 data_param = getattr(dataOut, parameterObject)
1093 1325
1094 1326 if channelList == None:
1095 1327 channelIndexList = numpy.arange(data_param.shape[0])
1096 1328 else:
1097 1329 channelIndexList = numpy.array(channelList)
1098 1330
1099 1331 nchan = len(channelIndexList) #Number of channels being plotted
1100 1332
1101 1333 if nchan < 1:
1102 1334 return
1103 1335
1104 1336 nGraphsByChannel = 0
1105 1337
1106 1338 if SNR:
1107 1339 nGraphsByChannel += 1
1108 1340 if DOP:
1109 1341 nGraphsByChannel += 1
1110 1342
1111 1343 if nGraphsByChannel < 1:
1112 1344 return
1113 1345
1114 1346 nplots = nGraphsByChannel*nchan
1115 1347
1116 1348 if timerange is not None:
1117 1349 self.timerange = timerange
1118 1350
1119 1351 #tmin = None
1120 1352 #tmax = None
1121 1353 if parameterIndex == None:
1122 1354 parameterIndex = 1
1123 1355
1124 1356 x = dataOut.getTimeRange1(dataOut.paramInterval)
1125 1357 y = dataOut.heightList
1126 1358 z = data_param[channelIndexList,parameterIndex,:].copy()
1127 1359
1128 1360 zRange = dataOut.abscissaList
1129 1361 # nChannels = z.shape[0] #Number of wind dimensions estimated
1130 1362 # thisDatetime = dataOut.datatime
1131 1363
1132 1364 if dataOut.data_SNR is not None:
1133 1365 SNRarray = dataOut.data_SNR[channelIndexList,:]
1134 1366 SNRdB = 10*numpy.log10(SNRarray)
1135 1367 # SNRavgdB = 10*numpy.log10(SNRavg)
1136 1368 ind = numpy.where(SNRdB < 10**(SNRthresh/10))
1137 1369 z[ind] = numpy.nan
1138 1370
1139 1371 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1140 1372 title = wintitle + " Parameters Plot" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
1141 1373 xlabel = ""
1142 1374 ylabel = "Range (Km)"
1143 1375
1144 1376 if (SNR and not onlySNR): nplots = 2*nplots
1145 1377
1146 1378 if onlyPositive:
1147 1379 colormap = "jet"
1148 1380 zmin = 0
1149 1381 else: colormap = "RdBu_r"
1150 1382
1151 1383 if not self.isConfig:
1152 1384
1153 1385 self.setup(id=id,
1154 1386 nplots=nplots,
1155 1387 wintitle=wintitle,
1156 1388 showprofile=showprofile,
1157 1389 show=show)
1158 1390
1159 1391 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1160 1392
1161 1393 if ymin == None: ymin = numpy.nanmin(y)
1162 1394 if ymax == None: ymax = numpy.nanmax(y)
1163 1395 if zmin == None: zmin = numpy.nanmin(zRange)
1164 1396 if zmax == None: zmax = numpy.nanmax(zRange)
1165 1397
1166 1398 if SNR:
1167 1399 if SNRmin == None: SNRmin = numpy.nanmin(SNRdB)
1168 1400 if SNRmax == None: SNRmax = numpy.nanmax(SNRdB)
1169 1401
1170 1402 self.FTP_WEI = ftp_wei
1171 1403 self.EXP_CODE = exp_code
1172 1404 self.SUB_EXP_CODE = sub_exp_code
1173 1405 self.PLOT_POS = plot_pos
1174 1406
1175 1407 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1176 1408 self.isConfig = True
1177 1409 self.figfile = figfile
1178 1410
1179 1411 self.setWinTitle(title)
1180 1412
1181 1413 if ((self.xmax - x[1]) < (x[1]-x[0])):
1182 1414 x[1] = self.xmax
1183 1415
1184 1416 for i in range(nchan):
1185 1417
1186 1418 if (SNR and not onlySNR): j = 2*i
1187 1419 else: j = i
1188 1420
1189 1421 j = nGraphsByChannel*i
1190 1422
1191 1423 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
1192 1424 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
1193 1425
1194 1426 if not onlySNR:
1195 1427 axes = self.axesList[j*self.__nsubplots]
1196 1428 z1 = z[i,:].reshape((1,-1))
1197 1429 axes.pcolorbuffer(x, y, z1,
1198 1430 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
1199 1431 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap=colormap,
1200 1432 ticksize=9, cblabel=zlabel, cbsize="1%")
1201 1433
1202 1434 if DOP:
1203 1435 title = "%s Channel %d: %s" %(parameterName, channelIndexList[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1204 1436
1205 1437 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
1206 1438 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
1207 1439 axes = self.axesList[j]
1208 1440 z1 = z[i,:].reshape((1,-1))
1209 1441 axes.pcolorbuffer(x, y, z1,
1210 1442 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
1211 1443 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap=colormap,
1212 1444 ticksize=9, cblabel=zlabel, cbsize="1%")
1213 1445
1214 1446 if SNR:
1215 1447 title = "Channel %d Signal Noise Ratio (SNR): %s" %(channelIndexList[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1216 1448 axes = self.axesList[(j)*self.__nsubplots]
1217 1449 if not onlySNR:
1218 1450 axes = self.axesList[(j + 1)*self.__nsubplots]
1219 1451
1220 1452 axes = self.axesList[(j + nGraphsByChannel-1)]
1221 1453
1222 1454 z1 = SNRdB[i,:].reshape((1,-1))
1223 1455 axes.pcolorbuffer(x, y, z1,
1224 1456 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1225 1457 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap="jet",
1226 1458 ticksize=9, cblabel=zlabel, cbsize="1%")
1227 1459
1228 1460
1229 1461
1230 1462 self.draw()
1231 1463
1232 1464 if x[1] >= self.axesList[0].xmax:
1233 1465 self.counter_imagwr = wr_period
1234 1466 self.isConfig = False
1235 1467 self.figfile = None
1236 1468
1237 1469 self.save(figpath=figpath,
1238 1470 figfile=figfile,
1239 1471 save=save,
1240 1472 ftp=ftp,
1241 1473 wr_period=wr_period,
1242 1474 thisDatetime=thisDatetime,
1243 1475 update_figfile=False)
1244 1476
1245 1477 class SpectralFittingPlot(Figure):
1246 1478
1247 1479 __isConfig = None
1248 1480 __nsubplots = None
1249 1481
1250 1482 WIDTHPROF = None
1251 1483 HEIGHTPROF = None
1252 1484 PREFIX = 'prm'
1253 1485
1254 1486
1255 1487 N = None
1256 1488 ippSeconds = None
1257 1489
1258 1490 def __init__(self, **kwargs):
1259 1491 Figure.__init__(self, **kwargs)
1260 1492 self.isConfig = False
1261 1493 self.__nsubplots = 1
1262 1494
1263 1495 self.PLOT_CODE = SPECFIT_CODE
1264 1496
1265 1497 self.WIDTH = 450
1266 1498 self.HEIGHT = 250
1267 1499 self.WIDTHPROF = 0
1268 1500 self.HEIGHTPROF = 0
1269 1501
1270 1502 def getSubplots(self):
1271 1503
1272 1504 ncol = int(numpy.sqrt(self.nplots)+0.9)
1273 1505 nrow = int(self.nplots*1./ncol + 0.9)
1274 1506
1275 1507 return nrow, ncol
1276 1508
1277 1509 def setup(self, id, nplots, wintitle, showprofile=False, show=True):
1278 1510
1279 1511 showprofile = False
1280 1512 self.__showprofile = showprofile
1281 1513 self.nplots = nplots
1282 1514
1283 1515 ncolspan = 5
1284 1516 colspan = 4
1285 1517 if showprofile:
1286 1518 ncolspan = 5
1287 1519 colspan = 4
1288 1520 self.__nsubplots = 2
1289 1521
1290 1522 self.createFigure(id = id,
1291 1523 wintitle = wintitle,
1292 1524 widthplot = self.WIDTH + self.WIDTHPROF,
1293 1525 heightplot = self.HEIGHT + self.HEIGHTPROF,
1294 1526 show=show)
1295 1527
1296 1528 nrow, ncol = self.getSubplots()
1297 1529
1298 1530 counter = 0
1299 1531 for y in range(nrow):
1300 1532 for x in range(ncol):
1301 1533
1302 1534 if counter >= self.nplots:
1303 1535 break
1304 1536
1305 1537 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1306 1538
1307 1539 if showprofile:
1308 1540 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
1309 1541
1310 1542 counter += 1
1311 1543
1312 1544 def run(self, dataOut, id, cutHeight=None, fit=False, wintitle="", channelList=None, showprofile=True,
1313 1545 xmin=None, xmax=None, ymin=None, ymax=None,
1314 1546 save=False, figpath='./', figfile=None, show=True):
1315 1547
1316 1548 """
1317 1549
1318 1550 Input:
1319 1551 dataOut :
1320 1552 id :
1321 1553 wintitle :
1322 1554 channelList :
1323 1555 showProfile :
1324 1556 xmin : None,
1325 1557 xmax : None,
1326 1558 zmin : None,
1327 1559 zmax : None
1328 1560 """
1329 1561
1330 1562 if cutHeight==None:
1331 1563 h=270
1332 1564 heightindex = numpy.abs(cutHeight - dataOut.heightList).argmin()
1333 1565 cutHeight = dataOut.heightList[heightindex]
1334 1566
1335 1567 factor = dataOut.normFactor
1336 1568 x = dataOut.abscissaList[:-1]
1337 1569 #y = dataOut.getHeiRange()
1338 1570
1339 1571 z = dataOut.data_pre[:,:,heightindex]/factor
1340 1572 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
1341 1573 avg = numpy.average(z, axis=1)
1342 1574 listChannels = z.shape[0]
1343 1575
1344 1576 #Reconstruct Function
1345 1577 if fit==True:
1346 1578 groupArray = dataOut.groupList
1347 1579 listChannels = groupArray.reshape((groupArray.size))
1348 1580 listChannels.sort()
1349 1581 spcFitLine = numpy.zeros(z.shape)
1350 1582 constants = dataOut.constants
1351 1583
1352 1584 nGroups = groupArray.shape[0]
1353 1585 nChannels = groupArray.shape[1]
1354 1586 nProfiles = z.shape[1]
1355 1587
1356 1588 for f in range(nGroups):
1357 1589 groupChann = groupArray[f,:]
1358 1590 p = dataOut.data_param[f,:,heightindex]
1359 1591 # p = numpy.array([ 89.343967,0.14036615,0.17086219,18.89835291,1.58388365,1.55099167])
1360 1592 fitLineAux = dataOut.library.modelFunction(p, constants)*nProfiles
1361 1593 fitLineAux = fitLineAux.reshape((nChannels,nProfiles))
1362 1594 spcFitLine[groupChann,:] = fitLineAux
1363 1595 # spcFitLine = spcFitLine/factor
1364 1596
1365 1597 z = z[listChannels,:]
1366 1598 spcFitLine = spcFitLine[listChannels,:]
1367 1599 spcFitLinedB = 10*numpy.log10(spcFitLine)
1368 1600
1369 1601 zdB = 10*numpy.log10(z)
1370 1602 #thisDatetime = dataOut.datatime
1371 1603 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1372 1604 title = wintitle + " Doppler Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
1373 1605 xlabel = "Velocity (m/s)"
1374 1606 ylabel = "Spectrum"
1375 1607
1376 1608 if not self.isConfig:
1377 1609
1378 1610 nplots = listChannels.size
1379 1611
1380 1612 self.setup(id=id,
1381 1613 nplots=nplots,
1382 1614 wintitle=wintitle,
1383 1615 showprofile=showprofile,
1384 1616 show=show)
1385 1617
1386 1618 if xmin == None: xmin = numpy.nanmin(x)
1387 1619 if xmax == None: xmax = numpy.nanmax(x)
1388 1620 if ymin == None: ymin = numpy.nanmin(zdB)
1389 1621 if ymax == None: ymax = numpy.nanmax(zdB)+2
1390 1622
1391 1623 self.isConfig = True
1392 1624
1393 1625 self.setWinTitle(title)
1394 1626 for i in range(self.nplots):
1395 1627 # title = "Channel %d: %4.2fdB" %(dataOut.channelList[i]+1, noisedB[i])
1396 1628 title = "Height %4.1f km\nChannel %d:" %(cutHeight, listChannels[i])
1397 1629 axes = self.axesList[i*self.__nsubplots]
1398 1630 if fit == False:
1399 1631 axes.pline(x, zdB[i,:],
1400 1632 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1401 1633 xlabel=xlabel, ylabel=ylabel, title=title
1402 1634 )
1403 1635 if fit == True:
1404 1636 fitline=spcFitLinedB[i,:]
1405 1637 y=numpy.vstack([zdB[i,:],fitline] )
1406 1638 legendlabels=['Data','Fitting']
1407 1639 axes.pmultilineyaxis(x, y,
1408 1640 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1409 1641 xlabel=xlabel, ylabel=ylabel, title=title,
1410 1642 legendlabels=legendlabels, marker=None,
1411 1643 linestyle='solid', grid='both')
1412 1644
1413 1645 self.draw()
1414 1646
1415 1647 self.save(figpath=figpath,
1416 1648 figfile=figfile,
1417 1649 save=save,
1418 1650 ftp=ftp,
1419 1651 wr_period=wr_period,
1420 1652 thisDatetime=thisDatetime)
1421 1653
1422 1654
1423 1655 class EWDriftsPlot(Figure):
1424 1656
1425 1657 __isConfig = None
1426 1658 __nsubplots = None
1427 1659
1428 1660 WIDTHPROF = None
1429 1661 HEIGHTPROF = None
1430 1662 PREFIX = 'drift'
1431 1663
1432 1664 def __init__(self, **kwargs):
1433 1665 Figure.__init__(self, **kwargs)
1434 1666 self.timerange = 2*60*60
1435 1667 self.isConfig = False
1436 1668 self.__nsubplots = 1
1437 1669
1438 1670 self.WIDTH = 800
1439 1671 self.HEIGHT = 150
1440 1672 self.WIDTHPROF = 120
1441 1673 self.HEIGHTPROF = 0
1442 1674 self.counter_imagwr = 0
1443 1675
1444 1676 self.PLOT_CODE = EWDRIFT_CODE
1445 1677
1446 1678 self.FTP_WEI = None
1447 1679 self.EXP_CODE = None
1448 1680 self.SUB_EXP_CODE = None
1449 1681 self.PLOT_POS = None
1450 1682 self.tmin = None
1451 1683 self.tmax = None
1452 1684
1453 1685 self.xmin = None
1454 1686 self.xmax = None
1455 1687
1456 1688 self.figfile = None
1457 1689
1458 1690 def getSubplots(self):
1459 1691
1460 1692 ncol = 1
1461 1693 nrow = self.nplots
1462 1694
1463 1695 return nrow, ncol
1464 1696
1465 1697 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1466 1698
1467 1699 self.__showprofile = showprofile
1468 1700 self.nplots = nplots
1469 1701
1470 1702 ncolspan = 1
1471 1703 colspan = 1
1472 1704
1473 1705 self.createFigure(id = id,
1474 1706 wintitle = wintitle,
1475 1707 widthplot = self.WIDTH + self.WIDTHPROF,
1476 1708 heightplot = self.HEIGHT + self.HEIGHTPROF,
1477 1709 show=show)
1478 1710
1479 1711 nrow, ncol = self.getSubplots()
1480 1712
1481 1713 counter = 0
1482 1714 for y in range(nrow):
1483 1715 if counter >= self.nplots:
1484 1716 break
1485 1717
1486 1718 self.addAxes(nrow, ncol*ncolspan, y, 0, colspan, 1)
1487 1719 counter += 1
1488 1720
1489 1721 def run(self, dataOut, id, wintitle="", channelList=None,
1490 1722 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
1491 1723 zmaxVertical = None, zminVertical = None, zmaxZonal = None, zminZonal = None,
1492 1724 timerange=None, SNRthresh = -numpy.inf, SNRmin = None, SNRmax = None, SNR_1 = False,
1493 1725 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
1494 1726 server=None, folder=None, username=None, password=None,
1495 1727 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1496 1728 """
1497 1729
1498 1730 Input:
1499 1731 dataOut :
1500 1732 id :
1501 1733 wintitle :
1502 1734 channelList :
1503 1735 showProfile :
1504 1736 xmin : None,
1505 1737 xmax : None,
1506 1738 ymin : None,
1507 1739 ymax : None,
1508 1740 zmin : None,
1509 1741 zmax : None
1510 1742 """
1511 1743
1512 1744 if timerange is not None:
1513 1745 self.timerange = timerange
1514 1746
1515 1747 tmin = None
1516 1748 tmax = None
1517 1749
1518 1750 x = dataOut.getTimeRange1(dataOut.outputInterval)
1519 1751 # y = dataOut.heightList
1520 1752 y = dataOut.heightList
1521 1753
1522 1754 z = dataOut.data_output
1523 1755 nplots = z.shape[0] #Number of wind dimensions estimated
1524 1756 nplotsw = nplots
1525 1757
1526 1758 #If there is a SNR function defined
1527 1759 if dataOut.data_SNR is not None:
1528 1760 nplots += 1
1529 1761 SNR = dataOut.data_SNR
1530 1762
1531 1763 if SNR_1:
1532 1764 SNR += 1
1533 1765
1534 1766 SNRavg = numpy.average(SNR, axis=0)
1535 1767
1536 1768 SNRdB = 10*numpy.log10(SNR)
1537 1769 SNRavgdB = 10*numpy.log10(SNRavg)
1538 1770
1539 1771 ind = numpy.where(SNRavg < 10**(SNRthresh/10))[0]
1540 1772
1541 1773 for i in range(nplotsw):
1542 1774 z[i,ind] = numpy.nan
1543 1775
1544 1776
1545 1777 showprofile = False
1546 1778 # thisDatetime = dataOut.datatime
1547 1779 thisDatetime = datetime.datetime.utcfromtimestamp(x[1])
1548 1780 title = wintitle + " EW Drifts"
1549 1781 xlabel = ""
1550 1782 ylabel = "Height (Km)"
1551 1783
1552 1784 if not self.isConfig:
1553 1785
1554 1786 self.setup(id=id,
1555 1787 nplots=nplots,
1556 1788 wintitle=wintitle,
1557 1789 showprofile=showprofile,
1558 1790 show=show)
1559 1791
1560 1792 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1561 1793
1562 1794 if ymin == None: ymin = numpy.nanmin(y)
1563 1795 if ymax == None: ymax = numpy.nanmax(y)
1564 1796
1565 1797 if zmaxZonal == None: zmaxZonal = numpy.nanmax(abs(z[0,:]))
1566 1798 if zminZonal == None: zminZonal = -zmaxZonal
1567 1799 if zmaxVertical == None: zmaxVertical = numpy.nanmax(abs(z[1,:]))
1568 1800 if zminVertical == None: zminVertical = -zmaxVertical
1569 1801
1570 1802 if dataOut.data_SNR is not None:
1571 1803 if SNRmin == None: SNRmin = numpy.nanmin(SNRavgdB)
1572 1804 if SNRmax == None: SNRmax = numpy.nanmax(SNRavgdB)
1573 1805
1574 1806 self.FTP_WEI = ftp_wei
1575 1807 self.EXP_CODE = exp_code
1576 1808 self.SUB_EXP_CODE = sub_exp_code
1577 1809 self.PLOT_POS = plot_pos
1578 1810
1579 1811 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1580 1812 self.isConfig = True
1581 1813
1582 1814
1583 1815 self.setWinTitle(title)
1584 1816
1585 1817 if ((self.xmax - x[1]) < (x[1]-x[0])):
1586 1818 x[1] = self.xmax
1587 1819
1588 1820 strWind = ['Zonal','Vertical']
1589 1821 strCb = 'Velocity (m/s)'
1590 1822 zmaxVector = [zmaxZonal, zmaxVertical]
1591 1823 zminVector = [zminZonal, zminVertical]
1592 1824
1593 1825 for i in range(nplotsw):
1594 1826
1595 1827 title = "%s Drifts: %s" %(strWind[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1596 1828 axes = self.axesList[i*self.__nsubplots]
1597 1829
1598 1830 z1 = z[i,:].reshape((1,-1))
1599 1831
1600 1832 axes.pcolorbuffer(x, y, z1,
1601 1833 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zminVector[i], zmax=zmaxVector[i],
1602 1834 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1603 1835 ticksize=9, cblabel=strCb, cbsize="1%", colormap="RdBu_r")
1604 1836
1605 1837 if dataOut.data_SNR is not None:
1606 1838 i += 1
1607 1839 if SNR_1:
1608 1840 title = "Signal Noise Ratio + 1 (SNR+1): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1609 1841 else:
1610 1842 title = "Signal Noise Ratio (SNR): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1611 1843 axes = self.axesList[i*self.__nsubplots]
1612 1844 SNRavgdB = SNRavgdB.reshape((1,-1))
1613 1845
1614 1846 axes.pcolorbuffer(x, y, SNRavgdB,
1615 1847 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1616 1848 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1617 1849 ticksize=9, cblabel='', cbsize="1%", colormap="jet")
1618 1850
1619 1851 self.draw()
1620 1852
1621 1853 if x[1] >= self.axesList[0].xmax:
1622 1854 self.counter_imagwr = wr_period
1623 1855 self.isConfig = False
1624 1856 self.figfile = None
1625 1857
1626 1858
1627 1859
1628 1860
1629 1861 class PhasePlot(Figure):
1630 1862
1631 1863 __isConfig = None
1632 1864 __nsubplots = None
1633 1865
1634 1866 PREFIX = 'mphase'
1635 1867
1636 1868 def __init__(self, **kwargs):
1637 1869 Figure.__init__(self, **kwargs)
1638 1870 self.timerange = 24*60*60
1639 1871 self.isConfig = False
1640 1872 self.__nsubplots = 1
1641 1873 self.counter_imagwr = 0
1642 1874 self.WIDTH = 600
1643 1875 self.HEIGHT = 300
1644 1876 self.WIDTHPROF = 120
1645 1877 self.HEIGHTPROF = 0
1646 1878 self.xdata = None
1647 1879 self.ydata = None
1648 1880
1649 1881 self.PLOT_CODE = MPHASE_CODE
1650 1882
1651 1883 self.FTP_WEI = None
1652 1884 self.EXP_CODE = None
1653 1885 self.SUB_EXP_CODE = None
1654 1886 self.PLOT_POS = None
1655 1887
1656 1888
1657 1889 self.filename_phase = None
1658 1890
1659 1891 self.figfile = None
1660 1892
1661 1893 def getSubplots(self):
1662 1894
1663 1895 ncol = 1
1664 1896 nrow = 1
1665 1897
1666 1898 return nrow, ncol
1667 1899
1668 1900 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1669 1901
1670 1902 self.__showprofile = showprofile
1671 1903 self.nplots = nplots
1672 1904
1673 1905 ncolspan = 7
1674 1906 colspan = 6
1675 1907 self.__nsubplots = 2
1676 1908
1677 1909 self.createFigure(id = id,
1678 1910 wintitle = wintitle,
1679 1911 widthplot = self.WIDTH+self.WIDTHPROF,
1680 1912 heightplot = self.HEIGHT+self.HEIGHTPROF,
1681 1913 show=show)
1682 1914
1683 1915 nrow, ncol = self.getSubplots()
1684 1916
1685 1917 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1686 1918
1687 1919
1688 1920 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
1689 1921 xmin=None, xmax=None, ymin=None, ymax=None,
1690 1922 timerange=None,
1691 1923 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1692 1924 server=None, folder=None, username=None, password=None,
1693 1925 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1694 1926
1695 1927
1696 1928 tmin = None
1697 1929 tmax = None
1698 1930 x = dataOut.getTimeRange1(dataOut.outputInterval)
1699 1931 y = dataOut.getHeiRange()
1700 1932
1701 1933
1702 1934 #thisDatetime = dataOut.datatime
1703 1935 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
1704 1936 title = wintitle + " Phase of Beacon Signal" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1705 1937 xlabel = "Local Time"
1706 1938 ylabel = "Phase"
1707 1939
1708 1940
1709 1941 #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList)))
1710 1942 phase_beacon = dataOut.data_output
1711 1943 update_figfile = False
1712 1944
1713 1945 if not self.isConfig:
1714 1946
1715 1947 self.nplots = phase_beacon.size
1716 1948
1717 1949 self.setup(id=id,
1718 1950 nplots=self.nplots,
1719 1951 wintitle=wintitle,
1720 1952 showprofile=showprofile,
1721 1953 show=show)
1722 1954
1723 1955 if timerange is not None:
1724 1956 self.timerange = timerange
1725 1957
1726 1958 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1727 1959
1728 1960 if ymin == None: ymin = numpy.nanmin(phase_beacon) - 10.0
1729 1961 if ymax == None: ymax = numpy.nanmax(phase_beacon) + 10.0
1730 1962
1731 1963 self.FTP_WEI = ftp_wei
1732 1964 self.EXP_CODE = exp_code
1733 1965 self.SUB_EXP_CODE = sub_exp_code
1734 1966 self.PLOT_POS = plot_pos
1735 1967
1736 1968 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1737 1969 self.isConfig = True
1738 1970 self.figfile = figfile
1739 1971 self.xdata = numpy.array([])
1740 1972 self.ydata = numpy.array([])
1741 1973
1742 1974 #open file beacon phase
1743 1975 path = '%s%03d' %(self.PREFIX, self.id)
1744 1976 beacon_file = os.path.join(path,'%s.txt'%self.name)
1745 1977 self.filename_phase = os.path.join(figpath,beacon_file)
1746 1978 update_figfile = True
1747 1979
1748 1980
1749 1981 #store data beacon phase
1750 1982 #self.save_data(self.filename_phase, phase_beacon, thisDatetime)
1751 1983
1752 1984 self.setWinTitle(title)
1753 1985
1754 1986
1755 1987 title = "Phase Offset %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1756 1988
1757 1989 legendlabels = ["phase %d"%(chan) for chan in numpy.arange(self.nplots)]
1758 1990
1759 1991 axes = self.axesList[0]
1760 1992
1761 1993 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1762 1994
1763 1995 if len(self.ydata)==0:
1764 1996 self.ydata = phase_beacon.reshape(-1,1)
1765 1997 else:
1766 1998 self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1)))
1767 1999
1768 2000
1769 2001 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1770 2002 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1771 2003 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1772 2004 XAxisAsTime=True, grid='both'
1773 2005 )
1774 2006
1775 2007 self.draw()
1776 2008
1777 2009 self.save(figpath=figpath,
1778 2010 figfile=figfile,
1779 2011 save=save,
1780 2012 ftp=ftp,
1781 2013 wr_period=wr_period,
1782 2014 thisDatetime=thisDatetime,
1783 2015 update_figfile=update_figfile)
1784 2016
1785 2017 if dataOut.ltctime + dataOut.outputInterval >= self.xmax:
1786 2018 self.counter_imagwr = wr_period
1787 2019 self.isConfig = False
1788 2020 update_figfile = True
1789 2021
1790 2022
1791 2023
1792 2024 class NSMeteorDetection1Plot(Figure):
1793 2025
1794 2026 isConfig = None
1795 2027 __nsubplots = None
1796 2028
1797 2029 WIDTHPROF = None
1798 2030 HEIGHTPROF = None
1799 2031 PREFIX = 'nsm'
1800 2032
1801 2033 zminList = None
1802 2034 zmaxList = None
1803 2035 cmapList = None
1804 2036 titleList = None
1805 2037 nPairs = None
1806 2038 nChannels = None
1807 2039 nParam = None
1808 2040
1809 2041 def __init__(self, **kwargs):
1810 2042 Figure.__init__(self, **kwargs)
1811 2043 self.isConfig = False
1812 2044 self.__nsubplots = 1
1813 2045
1814 2046 self.WIDTH = 750
1815 2047 self.HEIGHT = 250
1816 2048 self.WIDTHPROF = 120
1817 2049 self.HEIGHTPROF = 0
1818 2050 self.counter_imagwr = 0
1819 2051
1820 2052 self.PLOT_CODE = SPEC_CODE
1821 2053
1822 2054 self.FTP_WEI = None
1823 2055 self.EXP_CODE = None
1824 2056 self.SUB_EXP_CODE = None
1825 2057 self.PLOT_POS = None
1826 2058
1827 2059 self.__xfilter_ena = False
1828 2060 self.__yfilter_ena = False
1829 2061
1830 2062 def getSubplots(self):
1831 2063
1832 2064 ncol = 3
1833 2065 nrow = int(numpy.ceil(self.nplots/3.0))
1834 2066
1835 2067 return nrow, ncol
1836 2068
1837 2069 def setup(self, id, nplots, wintitle, show=True):
1838 2070
1839 2071 self.nplots = nplots
1840 2072
1841 2073 ncolspan = 1
1842 2074 colspan = 1
1843 2075
1844 2076 self.createFigure(id = id,
1845 2077 wintitle = wintitle,
1846 2078 widthplot = self.WIDTH + self.WIDTHPROF,
1847 2079 heightplot = self.HEIGHT + self.HEIGHTPROF,
1848 2080 show=show)
1849 2081
1850 2082 nrow, ncol = self.getSubplots()
1851 2083
1852 2084 counter = 0
1853 2085 for y in range(nrow):
1854 2086 for x in range(ncol):
1855 2087
1856 2088 if counter >= self.nplots:
1857 2089 break
1858 2090
1859 2091 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1860 2092
1861 2093 counter += 1
1862 2094
1863 2095 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
1864 2096 xmin=None, xmax=None, ymin=None, ymax=None, SNRmin=None, SNRmax=None,
1865 2097 vmin=None, vmax=None, wmin=None, wmax=None, mode = 'SA',
1866 2098 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1867 2099 server=None, folder=None, username=None, password=None,
1868 2100 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
1869 2101 xaxis="frequency"):
1870 2102
1871 2103 """
1872 2104
1873 2105 Input:
1874 2106 dataOut :
1875 2107 id :
1876 2108 wintitle :
1877 2109 channelList :
1878 2110 showProfile :
1879 2111 xmin : None,
1880 2112 xmax : None,
1881 2113 ymin : None,
1882 2114 ymax : None,
1883 2115 zmin : None,
1884 2116 zmax : None
1885 2117 """
1886 2118 #SEPARAR EN DOS PLOTS
1887 2119 nParam = dataOut.data_param.shape[1] - 3
1888 2120
1889 2121 utctime = dataOut.data_param[0,0]
1890 2122 tmet = dataOut.data_param[:,1].astype(int)
1891 2123 hmet = dataOut.data_param[:,2].astype(int)
1892 2124
1893 2125 x = dataOut.abscissaList
1894 2126 y = dataOut.heightList
1895 2127
1896 2128 z = numpy.zeros((nParam, y.size, x.size - 1))
1897 2129 z[:,:] = numpy.nan
1898 2130 z[:,hmet,tmet] = dataOut.data_param[:,3:].T
1899 2131 z[0,:,:] = 10*numpy.log10(z[0,:,:])
1900 2132
1901 2133 xlabel = "Time (s)"
1902 2134 ylabel = "Range (km)"
1903 2135
1904 2136 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
1905 2137
1906 2138 if not self.isConfig:
1907 2139
1908 2140 nplots = nParam
1909 2141
1910 2142 self.setup(id=id,
1911 2143 nplots=nplots,
1912 2144 wintitle=wintitle,
1913 2145 show=show)
1914 2146
1915 2147 if xmin is None: xmin = numpy.nanmin(x)
1916 2148 if xmax is None: xmax = numpy.nanmax(x)
1917 2149 if ymin is None: ymin = numpy.nanmin(y)
1918 2150 if ymax is None: ymax = numpy.nanmax(y)
1919 2151 if SNRmin is None: SNRmin = numpy.nanmin(z[0,:])
1920 2152 if SNRmax is None: SNRmax = numpy.nanmax(z[0,:])
1921 2153 if vmax is None: vmax = numpy.nanmax(numpy.abs(z[1,:]))
1922 2154 if vmin is None: vmin = -vmax
1923 2155 if wmin is None: wmin = 0
1924 2156 if wmax is None: wmax = 50
1925 2157
1926 2158 pairsList = dataOut.groupList
1927 2159 self.nPairs = len(dataOut.groupList)
1928 2160
1929 2161 zminList = [SNRmin, vmin, cmin] + [pmin]*self.nPairs
1930 2162 zmaxList = [SNRmax, vmax, cmax] + [pmax]*self.nPairs
1931 2163 titleList = ["SNR","Radial Velocity","Coherence"]
1932 2164 cmapList = ["jet","RdBu_r","jet"]
1933 2165
1934 2166 for i in range(self.nPairs):
1935 2167 strAux1 = "Phase Difference "+ str(pairsList[i][0]) + str(pairsList[i][1])
1936 2168 titleList = titleList + [strAux1]
1937 2169 cmapList = cmapList + ["RdBu_r"]
1938 2170
1939 2171 self.zminList = zminList
1940 2172 self.zmaxList = zmaxList
1941 2173 self.cmapList = cmapList
1942 2174 self.titleList = titleList
1943 2175
1944 2176 self.FTP_WEI = ftp_wei
1945 2177 self.EXP_CODE = exp_code
1946 2178 self.SUB_EXP_CODE = sub_exp_code
1947 2179 self.PLOT_POS = plot_pos
1948 2180
1949 2181 self.isConfig = True
1950 2182
1951 2183 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
1952 2184
1953 2185 for i in range(nParam):
1954 2186 title = self.titleList[i] + ": " +str_datetime
1955 2187 axes = self.axesList[i]
1956 2188 axes.pcolor(x, y, z[i,:].T,
1957 2189 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=self.zminList[i], zmax=self.zmaxList[i],
1958 2190 xlabel=xlabel, ylabel=ylabel, title=title, colormap=self.cmapList[i],ticksize=9, cblabel='')
1959 2191 self.draw()
1960 2192
1961 2193 if figfile == None:
1962 2194 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
1963 2195 name = str_datetime
1964 2196 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
1965 2197 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
1966 2198 figfile = self.getFilename(name)
1967 2199
1968 2200 self.save(figpath=figpath,
1969 2201 figfile=figfile,
1970 2202 save=save,
1971 2203 ftp=ftp,
1972 2204 wr_period=wr_period,
1973 2205 thisDatetime=thisDatetime)
1974 2206
1975 2207
1976 2208 class NSMeteorDetection2Plot(Figure):
1977 2209
1978 2210 isConfig = None
1979 2211 __nsubplots = None
1980 2212
1981 2213 WIDTHPROF = None
1982 2214 HEIGHTPROF = None
1983 2215 PREFIX = 'nsm'
1984 2216
1985 2217 zminList = None
1986 2218 zmaxList = None
1987 2219 cmapList = None
1988 2220 titleList = None
1989 2221 nPairs = None
1990 2222 nChannels = None
1991 2223 nParam = None
1992 2224
1993 2225 def __init__(self, **kwargs):
1994 2226 Figure.__init__(self, **kwargs)
1995 2227 self.isConfig = False
1996 2228 self.__nsubplots = 1
1997 2229
1998 2230 self.WIDTH = 750
1999 2231 self.HEIGHT = 250
2000 2232 self.WIDTHPROF = 120
2001 2233 self.HEIGHTPROF = 0
2002 2234 self.counter_imagwr = 0
2003 2235
2004 2236 self.PLOT_CODE = SPEC_CODE
2005 2237
2006 2238 self.FTP_WEI = None
2007 2239 self.EXP_CODE = None
2008 2240 self.SUB_EXP_CODE = None
2009 2241 self.PLOT_POS = None
2010 2242
2011 2243 self.__xfilter_ena = False
2012 2244 self.__yfilter_ena = False
2013 2245
2014 2246 def getSubplots(self):
2015 2247
2016 2248 ncol = 3
2017 2249 nrow = int(numpy.ceil(self.nplots/3.0))
2018 2250
2019 2251 return nrow, ncol
2020 2252
2021 2253 def setup(self, id, nplots, wintitle, show=True):
2022 2254
2023 2255 self.nplots = nplots
2024 2256
2025 2257 ncolspan = 1
2026 2258 colspan = 1
2027 2259
2028 2260 self.createFigure(id = id,
2029 2261 wintitle = wintitle,
2030 2262 widthplot = self.WIDTH + self.WIDTHPROF,
2031 2263 heightplot = self.HEIGHT + self.HEIGHTPROF,
2032 2264 show=show)
2033 2265
2034 2266 nrow, ncol = self.getSubplots()
2035 2267
2036 2268 counter = 0
2037 2269 for y in range(nrow):
2038 2270 for x in range(ncol):
2039 2271
2040 2272 if counter >= self.nplots:
2041 2273 break
2042 2274
2043 2275 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
2044 2276
2045 2277 counter += 1
2046 2278
2047 2279 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
2048 2280 xmin=None, xmax=None, ymin=None, ymax=None, SNRmin=None, SNRmax=None,
2049 2281 vmin=None, vmax=None, wmin=None, wmax=None, mode = 'SA',
2050 2282 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
2051 2283 server=None, folder=None, username=None, password=None,
2052 2284 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
2053 2285 xaxis="frequency"):
2054 2286
2055 2287 """
2056 2288
2057 2289 Input:
2058 2290 dataOut :
2059 2291 id :
2060 2292 wintitle :
2061 2293 channelList :
2062 2294 showProfile :
2063 2295 xmin : None,
2064 2296 xmax : None,
2065 2297 ymin : None,
2066 2298 ymax : None,
2067 2299 zmin : None,
2068 2300 zmax : None
2069 2301 """
2070 2302 #Rebuild matrix
2071 2303 utctime = dataOut.data_param[0,0]
2072 2304 cmet = dataOut.data_param[:,1].astype(int)
2073 2305 tmet = dataOut.data_param[:,2].astype(int)
2074 2306 hmet = dataOut.data_param[:,3].astype(int)
2075 2307
2076 2308 nParam = 3
2077 2309 nChan = len(dataOut.groupList)
2078 2310 x = dataOut.abscissaList
2079 2311 y = dataOut.heightList
2080 2312
2081 2313 z = numpy.full((nChan, nParam, y.size, x.size - 1),numpy.nan)
2082 2314 z[cmet,:,hmet,tmet] = dataOut.data_param[:,4:]
2083 2315 z[:,0,:,:] = 10*numpy.log10(z[:,0,:,:]) #logarithmic scale
2084 2316 z = numpy.reshape(z, (nChan*nParam, y.size, x.size-1))
2085 2317
2086 2318 xlabel = "Time (s)"
2087 2319 ylabel = "Range (km)"
2088 2320
2089 2321 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
2090 2322
2091 2323 if not self.isConfig:
2092 2324
2093 2325 nplots = nParam*nChan
2094 2326
2095 2327 self.setup(id=id,
2096 2328 nplots=nplots,
2097 2329 wintitle=wintitle,
2098 2330 show=show)
2099 2331
2100 2332 if xmin is None: xmin = numpy.nanmin(x)
2101 2333 if xmax is None: xmax = numpy.nanmax(x)
2102 2334 if ymin is None: ymin = numpy.nanmin(y)
2103 2335 if ymax is None: ymax = numpy.nanmax(y)
2104 2336 if SNRmin is None: SNRmin = numpy.nanmin(z[0,:])
2105 2337 if SNRmax is None: SNRmax = numpy.nanmax(z[0,:])
2106 2338 if vmax is None: vmax = numpy.nanmax(numpy.abs(z[1,:]))
2107 2339 if vmin is None: vmin = -vmax
2108 2340 if wmin is None: wmin = 0
2109 2341 if wmax is None: wmax = 50
2110 2342
2111 2343 self.nChannels = nChan
2112 2344
2113 2345 zminList = []
2114 2346 zmaxList = []
2115 2347 titleList = []
2116 2348 cmapList = []
2117 2349 for i in range(self.nChannels):
2118 2350 strAux1 = "SNR Channel "+ str(i)
2119 2351 strAux2 = "Radial Velocity Channel "+ str(i)
2120 2352 strAux3 = "Spectral Width Channel "+ str(i)
2121 2353
2122 2354 titleList = titleList + [strAux1,strAux2,strAux3]
2123 2355 cmapList = cmapList + ["jet","RdBu_r","jet"]
2124 2356 zminList = zminList + [SNRmin,vmin,wmin]
2125 2357 zmaxList = zmaxList + [SNRmax,vmax,wmax]
2126 2358
2127 2359 self.zminList = zminList
2128 2360 self.zmaxList = zmaxList
2129 2361 self.cmapList = cmapList
2130 2362 self.titleList = titleList
2131 2363
2132 2364 self.FTP_WEI = ftp_wei
2133 2365 self.EXP_CODE = exp_code
2134 2366 self.SUB_EXP_CODE = sub_exp_code
2135 2367 self.PLOT_POS = plot_pos
2136 2368
2137 2369 self.isConfig = True
2138 2370
2139 2371 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
2140 2372
2141 2373 for i in range(self.nplots):
2142 2374 title = self.titleList[i] + ": " +str_datetime
2143 2375 axes = self.axesList[i]
2144 2376 axes.pcolor(x, y, z[i,:].T,
2145 2377 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=self.zminList[i], zmax=self.zmaxList[i],
2146 2378 xlabel=xlabel, ylabel=ylabel, title=title, colormap=self.cmapList[i],ticksize=9, cblabel='')
2147 2379 self.draw()
2148 2380
2149 2381 if figfile == None:
2150 2382 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
2151 2383 name = str_datetime
2152 2384 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
2153 2385 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
2154 2386 figfile = self.getFilename(name)
2155 2387
2156 2388 self.save(figpath=figpath,
2157 2389 figfile=figfile,
2158 2390 save=save,
2159 2391 ftp=ftp,
2160 2392 wr_period=wr_period,
2161 2393 thisDatetime=thisDatetime)
@@ -1,1155 +1,795
1 1 import os, sys
2 2 import glob
3 3 import fnmatch
4 4 import datetime
5 5 import time
6 6 import re
7 7 import h5py
8 8 import numpy
9 9 import matplotlib.pyplot as plt
10 10
11 11 import pylab as plb
12 12 from scipy.optimize import curve_fit
13 13 from scipy import asarray as ar, exp
14 14 from scipy import stats
15 15
16 16 from numpy.ma.core import getdata
17 17
18 18 SPEED_OF_LIGHT = 299792458
19 19 SPEED_OF_LIGHT = 3e8
20 20
21 21 try:
22 22 from gevent import sleep
23 23 except:
24 24 from time import sleep
25 25
26 26 from schainpy.model.data.jrodata import Spectra
27 27 #from schainpy.model.data.BLTRheaderIO import FileHeader, RecordHeader
28 28 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
29 29 #from schainpy.model.io.jroIO_bltr import BLTRReader
30 30 from numpy import imag, shape, NaN
31 31
32 32 from jroIO_base import JRODataReader
33 33
34 34
35 35 class Header(object):
36 36
37 37 def __init__(self):
38 38 raise NotImplementedError
39 39
40 40
41 41 def read(self):
42 42
43 43 raise NotImplementedError
44 44
45 45 def write(self):
46 46
47 47 raise NotImplementedError
48 48
49 49 def printInfo(self):
50 50
51 51 message = "#"*50 + "\n"
52 52 message += self.__class__.__name__.upper() + "\n"
53 53 message += "#"*50 + "\n"
54 54
55 55 keyList = self.__dict__.keys()
56 56 keyList.sort()
57 57
58 58 for key in keyList:
59 59 message += "%s = %s" %(key, self.__dict__[key]) + "\n"
60 60
61 61 if "size" not in keyList:
62 62 attr = getattr(self, "size")
63 63
64 64 if attr:
65 65 message += "%s = %s" %("size", attr) + "\n"
66 66
67 67 #print message
68 68
69 69
70 70
71 71
72 72
73 73 FILE_STRUCTURE = numpy.dtype([ #HEADER 48bytes
74 74 ('FileMgcNumber','<u4'), #0x23020100
75 75 ('nFDTdataRecors','<u4'), #No Of FDT data records in this file (0 or more)
76 76 ('OffsetStartHeader','<u4'),
77 77 ('RadarUnitId','<u4'),
78 78 ('SiteName',numpy.str_,32), #Null terminated
79 79 ])
80 80
81 81 class FileHeaderBLTR(Header):
82 82
83 83 def __init__(self):
84 84
85 85 self.FileMgcNumber= 0 #0x23020100
86 86 self.nFDTdataRecors=0 #No Of FDT data records in this file (0 or more)
87 87 self.RadarUnitId= 0
88 88 self.OffsetStartHeader=0
89 89 self.SiteName= ""
90 90 self.size = 48
91 91
92 92 def FHread(self, fp):
93 93 #try:
94 94 startFp = open(fp,"rb")
95 95
96 96 header = numpy.fromfile(startFp, FILE_STRUCTURE,1)
97 97
98 98 print ' '
99 99 print 'puntero file header', startFp.tell()
100 100 print ' '
101 101
102 102
103 103 ''' numpy.fromfile(file, dtype, count, sep='')
104 104 file : file or str
105 105 Open file object or filename.
106 106
107 107 dtype : data-type
108 108 Data type of the returned array. For binary files, it is used to determine
109 109 the size and byte-order of the items in the file.
110 110
111 111 count : int
112 112 Number of items to read. -1 means all items (i.e., the complete file).
113 113
114 114 sep : str
115 115 Separator between items if file is a text file. Empty ("") separator means
116 116 the file should be treated as binary. Spaces (" ") in the separator match zero
117 117 or more whitespace characters. A separator consisting only of spaces must match
118 118 at least one whitespace.
119 119
120 120 '''
121 121
122 122
123 123
124 124 self.FileMgcNumber= hex(header['FileMgcNumber'][0])
125 125 self.nFDTdataRecors=int(header['nFDTdataRecors'][0]) #No Of FDT data records in this file (0 or more)
126 126 self.RadarUnitId= int(header['RadarUnitId'][0])
127 127 self.OffsetStartHeader= int(header['OffsetStartHeader'][0])
128 128 self.SiteName= str(header['SiteName'][0])
129 129
130 130 #print 'Numero de bloques', self.nFDTdataRecors
131 131
132 132
133 133 if self.size <48:
134 134 return 0
135 135
136 136 return 1
137 137
138 138
139 139 def write(self, fp):
140 140
141 141 headerTuple = (self.FileMgcNumber,
142 142 self.nFDTdataRecors,
143 143 self.RadarUnitId,
144 144 self.SiteName,
145 145 self.size)
146 146
147 147
148 148 header = numpy.array(headerTuple, FILE_STRUCTURE)
149 149 # numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
150 150 header.tofile(fp)
151 151 ''' ndarray.tofile(fid, sep, format) Write array to a file as text or binary (default).
152 152
153 153 fid : file or str
154 154 An open file object, or a string containing a filename.
155 155
156 156 sep : str
157 157 Separator between array items for text output. If "" (empty), a binary file is written,
158 158 equivalent to file.write(a.tobytes()).
159 159
160 160 format : str
161 161 Format string for text file output. Each entry in the array is formatted to text by
162 162 first converting it to the closest Python type, and then using "format" % item.
163 163
164 164 '''
165 165
166 166 return 1
167 167
168 168
169 169
170 170
171 171
172 172 RECORD_STRUCTURE = numpy.dtype([ #RECORD HEADER 180+20N bytes
173 173 ('RecMgcNumber','<u4'), #0x23030001
174 174 ('RecCounter','<u4'), #Record counter(0,1, ...)
175 175 ('Off2StartNxtRec','<u4'), #Offset to start of next record form start of this record
176 176 ('Off2StartData','<u4'), #Offset to start of data from start of this record
177 177 ('nUtime','<i4'), #Epoch time stamp of start of acquisition (seconds)
178 178 ('nMilisec','<u4'), #Millisecond component of time stamp (0,...,999)
179 179 ('ExpTagName',numpy.str_,32), #Experiment tag name (null terminated)
180 180 ('ExpComment',numpy.str_,32), #Experiment comment (null terminated)
181 181 ('SiteLatDegrees','<f4'), #Site latitude (from GPS) in degrees (positive implies North)
182 182 ('SiteLongDegrees','<f4'), #Site longitude (from GPS) in degrees (positive implies East)
183 183 ('RTCgpsStatus','<u4'), #RTC GPS engine status (0=SEEK, 1=LOCK, 2=NOT FITTED, 3=UNAVAILABLE)
184 184 ('TransmitFrec','<u4'), #Transmit frequency (Hz)
185 185 ('ReceiveFrec','<u4'), #Receive frequency
186 186 ('FirstOsciFrec','<u4'), #First local oscillator frequency (Hz)
187 187 ('Polarisation','<u4'), #(0="O", 1="E", 2="linear 1", 3="linear2")
188 188 ('ReceiverFiltSett','<u4'), #Receiver filter settings (0,1,2,3)
189 189 ('nModesInUse','<u4'), #Number of modes in use (1 or 2)
190 190 ('DualModeIndex','<u4'), #Dual Mode index number for these data (0 or 1)
191 191 ('DualModeRange','<u4'), #Dual Mode range correction for these data (m)
192 192 ('nDigChannels','<u4'), #Number of digital channels acquired (2*N)
193 193 ('SampResolution','<u4'), #Sampling resolution (meters)
194 194 ('nHeights','<u4'), #Number of range gates sampled
195 195 ('StartRangeSamp','<u4'), #Start range of sampling (meters)
196 196 ('PRFhz','<u4'), #PRF (Hz)
197 197 ('nCohInt','<u4'), #Integrations
198 198 ('nProfiles','<u4'), #Number of data points transformed
199 199 ('nChannels','<u4'), #Number of receive beams stored in file (1 or N)
200 200 ('nIncohInt','<u4'), #Number of spectral averages
201 201 ('FFTwindowingInd','<u4'), #FFT windowing index (0 = no window)
202 202 ('BeamAngleAzim','<f4'), #Beam steer angle (azimuth) in degrees (clockwise from true North)
203 203 ('BeamAngleZen','<f4'), #Beam steer angle (zenith) in degrees (0=> vertical)
204 204 ('AntennaCoord0','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
205 205 ('AntennaAngl0','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
206 206 ('AntennaCoord1','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
207 207 ('AntennaAngl1','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
208 208 ('AntennaCoord2','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
209 209 ('AntennaAngl2','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
210 210 ('RecPhaseCalibr0','<f4'), #Receiver phase calibration (degrees) - N values
211 211 ('RecPhaseCalibr1','<f4'), #Receiver phase calibration (degrees) - N values
212 212 ('RecPhaseCalibr2','<f4'), #Receiver phase calibration (degrees) - N values
213 213 ('RecAmpCalibr0','<f4'), #Receiver amplitude calibration (ratio relative to receiver one) - N values
214 214 ('RecAmpCalibr1','<f4'), #Receiver amplitude calibration (ratio relative to receiver one) - N values
215 215 ('RecAmpCalibr2','<f4'), #Receiver amplitude calibration (ratio relative to receiver one) - N values
216 216 ('ReceiverGaindB0','<i4'), #Receiver gains in dB - N values
217 217 ('ReceiverGaindB1','<i4'), #Receiver gains in dB - N values
218 218 ('ReceiverGaindB2','<i4'), #Receiver gains in dB - N values
219 219 ])
220 220
221 221
222 222 class RecordHeaderBLTR(Header):
223 223
224 224 def __init__(self, RecMgcNumber=None, RecCounter= 0, Off2StartNxtRec= 811248,
225 225 nUtime= 0, nMilisec= 0, ExpTagName= None,
226 226 ExpComment=None, SiteLatDegrees=0, SiteLongDegrees= 0,
227 227 RTCgpsStatus= 0, TransmitFrec= 0, ReceiveFrec= 0,
228 228 FirstOsciFrec= 0, Polarisation= 0, ReceiverFiltSett= 0,
229 229 nModesInUse= 0, DualModeIndex= 0, DualModeRange= 0,
230 230 nDigChannels= 0, SampResolution= 0, nHeights= 0,
231 231 StartRangeSamp= 0, PRFhz= 0, nCohInt= 0,
232 232 nProfiles= 0, nChannels= 0, nIncohInt= 0,
233 233 FFTwindowingInd= 0, BeamAngleAzim= 0, BeamAngleZen= 0,
234 234 AntennaCoord0= 0, AntennaCoord1= 0, AntennaCoord2= 0,
235 235 RecPhaseCalibr0= 0, RecPhaseCalibr1= 0, RecPhaseCalibr2= 0,
236 236 RecAmpCalibr0= 0, RecAmpCalibr1= 0, RecAmpCalibr2= 0,
237 237 AntennaAngl0=0, AntennaAngl1=0, AntennaAngl2=0,
238 238 ReceiverGaindB0= 0, ReceiverGaindB1= 0, ReceiverGaindB2= 0, Off2StartData=0, OffsetStartHeader=0):
239 239
240 240 self.RecMgcNumber = RecMgcNumber #0x23030001
241 241 self.RecCounter = RecCounter
242 242 self.Off2StartNxtRec = Off2StartNxtRec
243 243 self.Off2StartData = Off2StartData
244 244 self.nUtime = nUtime
245 245 self.nMilisec = nMilisec
246 246 self.ExpTagName = ExpTagName
247 247 self.ExpComment = ExpComment
248 248 self.SiteLatDegrees = SiteLatDegrees
249 249 self.SiteLongDegrees = SiteLongDegrees
250 250 self.RTCgpsStatus = RTCgpsStatus
251 251 self.TransmitFrec = TransmitFrec
252 252 self.ReceiveFrec = ReceiveFrec
253 253 self.FirstOsciFrec = FirstOsciFrec
254 254 self.Polarisation = Polarisation
255 255 self.ReceiverFiltSett = ReceiverFiltSett
256 256 self.nModesInUse = nModesInUse
257 257 self.DualModeIndex = DualModeIndex
258 258 self.DualModeRange = DualModeRange
259 259 self.nDigChannels = nDigChannels
260 260 self.SampResolution = SampResolution
261 261 self.nHeights = nHeights
262 262 self.StartRangeSamp = StartRangeSamp
263 263 self.PRFhz = PRFhz
264 264 self.nCohInt = nCohInt
265 265 self.nProfiles = nProfiles
266 266 self.nChannels = nChannels
267 267 self.nIncohInt = nIncohInt
268 268 self.FFTwindowingInd = FFTwindowingInd
269 269 self.BeamAngleAzim = BeamAngleAzim
270 270 self.BeamAngleZen = BeamAngleZen
271 271 self.AntennaCoord0 = AntennaCoord0
272 272 self.AntennaAngl0 = AntennaAngl0
273 273 self.AntennaAngl1 = AntennaAngl1
274 274 self.AntennaAngl2 = AntennaAngl2
275 275 self.AntennaCoord1 = AntennaCoord1
276 276 self.AntennaCoord2 = AntennaCoord2
277 277 self.RecPhaseCalibr0 = RecPhaseCalibr0
278 278 self.RecPhaseCalibr1 = RecPhaseCalibr1
279 279 self.RecPhaseCalibr2 = RecPhaseCalibr2
280 280 self.RecAmpCalibr0 = RecAmpCalibr0
281 281 self.RecAmpCalibr1 = RecAmpCalibr1
282 282 self.RecAmpCalibr2 = RecAmpCalibr2
283 283 self.ReceiverGaindB0 = ReceiverGaindB0
284 284 self.ReceiverGaindB1 = ReceiverGaindB1
285 285 self.ReceiverGaindB2 = ReceiverGaindB2
286 286 self.OffsetStartHeader = 48
287 287
288 288
289 289
290 290 def RHread(self, fp):
291 291 #print fp
292 292 #startFp = open('/home/erick/Documents/Data/huancayo.20161019.22.fdt',"rb") #The method tell() returns the current position of the file read/write pointer within the file.
293 293 startFp = open(fp,"rb") #The method tell() returns the current position of the file read/write pointer within the file.
294 294 #RecCounter=0
295 295 #Off2StartNxtRec=811248
296 296 OffRHeader= self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
297 297 print ' '
298 298 print 'puntero Record Header', startFp.tell()
299 299 print ' '
300 300
301 301
302 302 startFp.seek(OffRHeader, os.SEEK_SET)
303 303
304 304 print ' '
305 305 print 'puntero Record Header con seek', startFp.tell()
306 306 print ' '
307 307
308 308 #print 'Posicion del bloque: ',OffRHeader
309 309
310 310 header = numpy.fromfile(startFp,RECORD_STRUCTURE,1)
311 311
312 312 print ' '
313 313 print 'puntero Record Header con seek', startFp.tell()
314 314 print ' '
315 315
316 316 print ' '
317 317 #
318 318 #print 'puntero Record Header despues de seek', header.tell()
319 319 print ' '
320 320
321 321 self.RecMgcNumber = hex(header['RecMgcNumber'][0]) #0x23030001
322 322 self.RecCounter = int(header['RecCounter'][0])
323 323 self.Off2StartNxtRec = int(header['Off2StartNxtRec'][0])
324 324 self.Off2StartData = int(header['Off2StartData'][0])
325 325 self.nUtime = header['nUtime'][0]
326 326 self.nMilisec = header['nMilisec'][0]
327 327 self.ExpTagName = str(header['ExpTagName'][0])
328 328 self.ExpComment = str(header['ExpComment'][0])
329 329 self.SiteLatDegrees = header['SiteLatDegrees'][0]
330 330 self.SiteLongDegrees = header['SiteLongDegrees'][0]
331 331 self.RTCgpsStatus = header['RTCgpsStatus'][0]
332 332 self.TransmitFrec = header['TransmitFrec'][0]
333 333 self.ReceiveFrec = header['ReceiveFrec'][0]
334 334 self.FirstOsciFrec = header['FirstOsciFrec'][0]
335 335 self.Polarisation = header['Polarisation'][0]
336 336 self.ReceiverFiltSett = header['ReceiverFiltSett'][0]
337 337 self.nModesInUse = header['nModesInUse'][0]
338 338 self.DualModeIndex = header['DualModeIndex'][0]
339 339 self.DualModeRange = header['DualModeRange'][0]
340 340 self.nDigChannels = header['nDigChannels'][0]
341 341 self.SampResolution = header['SampResolution'][0]
342 342 self.nHeights = header['nHeights'][0]
343 343 self.StartRangeSamp = header['StartRangeSamp'][0]
344 344 self.PRFhz = header['PRFhz'][0]
345 345 self.nCohInt = header['nCohInt'][0]
346 346 self.nProfiles = header['nProfiles'][0]
347 347 self.nChannels = header['nChannels'][0]
348 348 self.nIncohInt = header['nIncohInt'][0]
349 349 self.FFTwindowingInd = header['FFTwindowingInd'][0]
350 350 self.BeamAngleAzim = header['BeamAngleAzim'][0]
351 351 self.BeamAngleZen = header['BeamAngleZen'][0]
352 352 self.AntennaCoord0 = header['AntennaCoord0'][0]
353 353 self.AntennaAngl0 = header['AntennaAngl0'][0]
354 354 self.AntennaCoord1 = header['AntennaCoord1'][0]
355 355 self.AntennaAngl1 = header['AntennaAngl1'][0]
356 356 self.AntennaCoord2 = header['AntennaCoord2'][0]
357 357 self.AntennaAngl2 = header['AntennaAngl2'][0]
358 358 self.RecPhaseCalibr0 = header['RecPhaseCalibr0'][0]
359 359 self.RecPhaseCalibr1 = header['RecPhaseCalibr1'][0]
360 360 self.RecPhaseCalibr2 = header['RecPhaseCalibr2'][0]
361 361 self.RecAmpCalibr0 = header['RecAmpCalibr0'][0]
362 362 self.RecAmpCalibr1 = header['RecAmpCalibr1'][0]
363 363 self.RecAmpCalibr2 = header['RecAmpCalibr2'][0]
364 364 self.ReceiverGaindB0 = header['ReceiverGaindB0'][0]
365 365 self.ReceiverGaindB1 = header['ReceiverGaindB1'][0]
366 366 self.ReceiverGaindB2 = header['ReceiverGaindB2'][0]
367 367
368 368 self.ipp= 0.5*(SPEED_OF_LIGHT/self.PRFhz)
369 369
370 370 self.RHsize = 180+20*self.nChannels
371 371 self.Datasize= self.nProfiles*self.nChannels*self.nHeights*2*4
372 372 #print 'Datasize',self.Datasize
373 373 endFp = self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
374 374
375 375 print '=============================================='
376 376 print 'RecMgcNumber ',self.RecMgcNumber
377 377 print 'RecCounter ',self.RecCounter
378 378 print 'Off2StartNxtRec ',self.Off2StartNxtRec
379 379 print 'Off2StartData ',self.Off2StartData
380 380 print 'Range Resolution ',self.SampResolution
381 381 print 'First Height ',self.StartRangeSamp
382 382 print 'PRF (Hz) ',self.PRFhz
383 383 print 'Heights (K) ',self.nHeights
384 384 print 'Channels (N) ',self.nChannels
385 385 print 'Profiles (J) ',self.nProfiles
386 386 print 'iCoh ',self.nCohInt
387 387 print 'iInCoh ',self.nIncohInt
388 388 print 'BeamAngleAzim ',self.BeamAngleAzim
389 389 print 'BeamAngleZen ',self.BeamAngleZen
390 390
391 391 #print 'ModoEnUso ',self.DualModeIndex
392 392 #print 'UtcTime ',self.nUtime
393 393 #print 'MiliSec ',self.nMilisec
394 394 #print 'Exp TagName ',self.ExpTagName
395 395 #print 'Exp Comment ',self.ExpComment
396 396 #print 'FFT Window Index ',self.FFTwindowingInd
397 397 #print 'N Dig. Channels ',self.nDigChannels
398 398 print 'Size de bloque ',self.RHsize
399 399 print 'DataSize ',self.Datasize
400 400 print 'BeamAngleAzim ',self.BeamAngleAzim
401 401 #print 'AntennaCoord0 ',self.AntennaCoord0
402 402 #print 'AntennaAngl0 ',self.AntennaAngl0
403 403 #print 'AntennaCoord1 ',self.AntennaCoord1
404 404 #print 'AntennaAngl1 ',self.AntennaAngl1
405 405 #print 'AntennaCoord2 ',self.AntennaCoord2
406 406 #print 'AntennaAngl2 ',self.AntennaAngl2
407 407 print 'RecPhaseCalibr0 ',self.RecPhaseCalibr0
408 408 print 'RecPhaseCalibr1 ',self.RecPhaseCalibr1
409 409 print 'RecPhaseCalibr2 ',self.RecPhaseCalibr2
410 410 print 'RecAmpCalibr0 ',self.RecAmpCalibr0
411 411 print 'RecAmpCalibr1 ',self.RecAmpCalibr1
412 412 print 'RecAmpCalibr2 ',self.RecAmpCalibr2
413 413 print 'ReceiverGaindB0 ',self.ReceiverGaindB0
414 414 print 'ReceiverGaindB1 ',self.ReceiverGaindB1
415 415 print 'ReceiverGaindB2 ',self.ReceiverGaindB2
416 416 print '=============================================='
417 417
418 418 if OffRHeader > endFp:
419 419 sys.stderr.write("Warning %s: Size value read from System Header is lower than it has to be\n" %fp)
420 420 return 0
421 421
422 422 if OffRHeader < endFp:
423 423 sys.stderr.write("Warning %s: Size value read from System Header size is greater than it has to be\n" %fp)
424 424 return 0
425 425
426 426 return 1
427 427
428 428
429 429 class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODataReader):
430 430
431 431 path = None
432 432 startDate = None
433 433 endDate = None
434 434 startTime = None
435 435 endTime = None
436 436 walk = None
437 437 isConfig = False
438 438
439 439
440 440 fileList= None
441 441
442 442 #metadata
443 443 TimeZone= None
444 444 Interval= None
445 445 heightList= None
446 446
447 447 #data
448 448 data= None
449 449 utctime= None
450 450
451 451
452 452
453 453 def __init__(self, **kwargs):
454 454
455 455 #Eliminar de la base la herencia
456 456 ProcessingUnit.__init__(self, **kwargs)
457 457
458 458 #self.isConfig = False
459 459
460 460 #self.pts2read_SelfSpectra = 0
461 461 #self.pts2read_CrossSpectra = 0
462 462 #self.pts2read_DCchannels = 0
463 463 #self.datablock = None
464 464 self.utc = None
465 465 self.ext = ".fdt"
466 466 self.optchar = "P"
467 467 self.fpFile=None
468 468 self.fp = None
469 469 self.BlockCounter=0
470 470 self.dtype = None
471 471 self.fileSizeByHeader = None
472 472 self.filenameList = []
473 473 self.fileSelector = 0
474 474 self.Off2StartNxtRec=0
475 475 self.RecCounter=0
476 476 self.flagNoMoreFiles = 0
477 477 self.data_spc=None
478 478 self.data_cspc=None
479 479 self.data_output=None
480 480 self.path = None
481 481 self.OffsetStartHeader=0
482 482 self.Off2StartData=0
483 483 self.ipp = 0
484 484 self.nFDTdataRecors=0
485 485 self.blocksize = 0
486 486 self.dataOut = Spectra()
487 487 self.profileIndex = 1 #Always
488 488 self.dataOut.flagNoData=False
489 489 self.dataOut.nRdPairs = 0
490 490 self.dataOut.pairsList = []
491 491 self.dataOut.data_spc=None
492 492 self.dataOut.noise=[]
493 493 self.dataOut.velocityX=[]
494 494 self.dataOut.velocityY=[]
495 495 self.dataOut.velocityV=[]
496 496
497 497
498 498
499 499 def Files2Read(self, fp):
500 500 '''
501 501 Function that indicates the number of .fdt files that exist in the folder to be read.
502 502 It also creates an organized list with the names of the files to read.
503 503 '''
504 504 #self.__checkPath()
505 505
506 506 ListaData=os.listdir(fp) #Gets the list of files within the fp address
507 507 ListaData=sorted(ListaData) #Sort the list of files from least to largest by names
508 508 nFiles=0 #File Counter
509 509 FileList=[] #A list is created that will contain the .fdt files
510 510 for IndexFile in ListaData :
511 511 if '.fdt' in IndexFile:
512 512 FileList.append(IndexFile)
513 513 nFiles+=1
514 514
515 515 #print 'Files2Read'
516 516 #print 'Existen '+str(nFiles)+' archivos .fdt'
517 517
518 518 self.filenameList=FileList #List of files from least to largest by names
519 519
520 520
521 521 def run(self, **kwargs):
522 522 '''
523 523 This method will be the one that will initiate the data entry, will be called constantly.
524 524 You should first verify that your Setup () is set up and then continue to acquire
525 525 the data to be processed with getData ().
526 526 '''
527 527 if not self.isConfig:
528 528 self.setup(**kwargs)
529 529 self.isConfig = True
530 530
531 531 self.getData()
532 532 #print 'running'
533 533
534 534
535 535 def setup(self, path=None,
536 536 startDate=None,
537 537 endDate=None,
538 538 startTime=None,
539 539 endTime=None,
540 540 walk=True,
541 541 timezone='utc',
542 542 code = None,
543 543 online=False,
544 544 ReadMode=None,
545 545 **kwargs):
546 546
547 547 self.isConfig = True
548 548
549 549 self.path=path
550 550 self.startDate=startDate
551 551 self.endDate=endDate
552 552 self.startTime=startTime
553 553 self.endTime=endTime
554 554 self.walk=walk
555 555 self.ReadMode=int(ReadMode)
556 556
557 557 pass
558 558
559 559
560 560 def getData(self):
561 561 '''
562 562 Before starting this function, you should check that there is still an unread file,
563 563 If there are still blocks to read or if the data block is empty.
564 564
565 565 You should call the file "read".
566 566
567 567 '''
568 568
569 569 if self.flagNoMoreFiles:
570 570 self.dataOut.flagNoData = True
571 print 'NoData se vuelve true'
571 #print 'NoData se vuelve true'
572 572 return 0
573 573
574 574 self.fp=self.path
575 575 self.Files2Read(self.fp)
576 576 self.readFile(self.fp)
577 577 self.dataOut.data_spc = self.data_spc
578 578 self.dataOut.data_cspc =self.data_cspc
579 579 self.dataOut.data_output=self.data_output
580 580
581 print 'self.dataOut.data_output', shape(self.dataOut.data_output)
581 #print 'self.dataOut.data_output', shape(self.dataOut.data_output)
582 582
583 583 #self.removeDC()
584 584 return self.dataOut.data_spc
585 585
586 586
587 587 def readFile(self,fp):
588 588 '''
589 589 You must indicate if you are reading in Online or Offline mode and load the
590 590 The parameters for this file reading mode.
591 591
592 592 Then you must do 2 actions:
593 593
594 594 1. Get the BLTR FileHeader.
595 595 2. Start reading the first block.
596 596 '''
597 597
598 598 #The address of the folder is generated the name of the .fdt file that will be read
599 print "File: ",self.fileSelector+1
599 #print "File: ",self.fileSelector+1
600 600
601 601 if self.fileSelector < len(self.filenameList):
602 602
603 603 self.fpFile=str(fp)+'/'+str(self.filenameList[self.fileSelector])
604 604 #print self.fpFile
605 605 fheader = FileHeaderBLTR()
606 606 fheader.FHread(self.fpFile) #Bltr FileHeader Reading
607 607 self.nFDTdataRecors=fheader.nFDTdataRecors
608 608
609 609 self.readBlock() #Block reading
610 610 else:
611 print 'readFile FlagNoData becomes true'
611 #print 'readFile FlagNoData becomes true'
612 612 self.flagNoMoreFiles=True
613 613 self.dataOut.flagNoData = True
614 614 return 0
615 615
616 616 def getVelRange(self, extrapoints=0):
617 617 Lambda= SPEED_OF_LIGHT/50000000
618 618 PRF = self.dataOut.PRF#1./(self.dataOut.ippSeconds * self.dataOut.nCohInt)
619 619 Vmax=-Lambda/(4.*(1./PRF)*self.dataOut.nCohInt*2.)
620 620 deltafreq = PRF / (self.nProfiles)
621 621 deltavel = (Vmax*2) / (self.nProfiles)
622 622 freqrange = deltafreq*(numpy.arange(self.nProfiles)-self.nProfiles/2.) - deltafreq/2
623 623 velrange = deltavel*(numpy.arange(self.nProfiles)-self.nProfiles/2.)
624 624 return velrange
625 625
626 626 def readBlock(self):
627 627 '''
628 628 It should be checked if the block has data, if it is not passed to the next file.
629 629
630 630 Then the following is done:
631 631
632 632 1. Read the RecordHeader
633 633 2. Fill the buffer with the current block number.
634 634
635 635 '''
636 636
637 if self.BlockCounter < self.nFDTdataRecors-2:
638 print self.nFDTdataRecors, 'CONDICION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
637 if self.BlockCounter < self.nFDTdataRecors-1:
638 #print self.nFDTdataRecors, 'CONDICION'
639 639 if self.ReadMode==1:
640 640 rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter+1)
641 641 elif self.ReadMode==0:
642 642 rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter)
643 643
644 644 rheader.RHread(self.fpFile) #Bltr FileHeader Reading
645 645
646 646 self.OffsetStartHeader=rheader.OffsetStartHeader
647 647 self.RecCounter=rheader.RecCounter
648 648 self.Off2StartNxtRec=rheader.Off2StartNxtRec
649 649 self.Off2StartData=rheader.Off2StartData
650 650 self.nProfiles=rheader.nProfiles
651 651 self.nChannels=rheader.nChannels
652 652 self.nHeights=rheader.nHeights
653 653 self.frequency=rheader.TransmitFrec
654 654 self.DualModeIndex=rheader.DualModeIndex
655 655
656 656 self.pairsList =[(0,1),(0,2),(1,2)]
657 657 self.dataOut.pairsList = self.pairsList
658 658
659 659 self.nRdPairs=len(self.dataOut.pairsList)
660 660 self.dataOut.nRdPairs = self.nRdPairs
661 661
662 662 self.__firstHeigth=rheader.StartRangeSamp
663 663 self.__deltaHeigth=rheader.SampResolution
664 664 self.dataOut.heightList= self.__firstHeigth + numpy.array(range(self.nHeights))*self.__deltaHeigth
665 665 self.dataOut.channelList = range(self.nChannels)
666 666 self.dataOut.nProfiles=rheader.nProfiles
667 667 self.dataOut.nIncohInt=rheader.nIncohInt
668 668 self.dataOut.nCohInt=rheader.nCohInt
669 669 self.dataOut.ippSeconds= 1/float(rheader.PRFhz)
670 670 self.dataOut.PRF=rheader.PRFhz
671 671 self.dataOut.nFFTPoints=rheader.nProfiles
672 672 self.dataOut.utctime=rheader.nUtime
673 673 self.dataOut.timeZone=0
674 674 self.dataOut.normFactor= self.dataOut.nProfiles*self.dataOut.nIncohInt*self.dataOut.nCohInt
675 675 self.dataOut.outputInterval= self.dataOut.ippSeconds * self.dataOut.nCohInt * self.dataOut.nIncohInt * self.nProfiles
676 676
677 677 self.data_output=numpy.ones([3,rheader.nHeights])*numpy.NaN
678 print 'self.data_output', shape(self.data_output)
678 #print 'self.data_output', shape(self.data_output)
679 679 self.dataOut.velocityX=[]
680 680 self.dataOut.velocityY=[]
681 681 self.dataOut.velocityV=[]
682 682
683 683 '''Block Reading, the Block Data is received and Reshape is used to give it
684 684 shape.
685 685 '''
686 686
687 687 #Procedure to take the pointer to where the date block starts
688 688 startDATA = open(self.fpFile,"rb")
689 689 OffDATA= self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec+self.Off2StartData
690 690 startDATA.seek(OffDATA, os.SEEK_SET)
691 691
692 692 def moving_average(x, N=2):
693 693 return numpy.convolve(x, numpy.ones((N,))/N)[(N-1):]
694 694
695 695 def gaus(xSamples,a,x0,sigma):
696 696 return a*exp(-(xSamples-x0)**2/(2*sigma**2))
697 697
698 698 def Find(x,value):
699 699 for index in range(len(x)):
700 700 if x[index]==value:
701 701 return index
702 702
703 703 def pol2cart(rho, phi):
704 704 x = rho * numpy.cos(phi)
705 705 y = rho * numpy.sin(phi)
706 706 return(x, y)
707 707
708 708
709 709
710 710
711 711 if self.DualModeIndex==self.ReadMode:
712 712
713 713 self.data_fft = numpy.fromfile( startDATA, [('complex','<c8')],self.nProfiles*self.nChannels*self.nHeights )
714 #
715 # if len(self.data_fft) is not 101376:
716 #
717 # self.data_fft = numpy.empty(101376)
714 718
715 719 self.data_fft=self.data_fft.astype(numpy.dtype('complex'))
716 720
721
722
723
724
717 725 self.data_block=numpy.reshape(self.data_fft,(self.nHeights, self.nChannels, self.nProfiles ))
718 726
719 727 self.data_block = numpy.transpose(self.data_block, (1,2,0))
720 728
721 729 copy = self.data_block.copy()
722 730 spc = copy * numpy.conjugate(copy)
723 731
724 732 self.data_spc = numpy.absolute(spc) # valor absoluto o magnitud
725 733
726 734 factor = self.dataOut.normFactor
727 735
728 736
729 737 z = self.data_spc.copy()#/factor
730 738 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
731 739 #zdB = 10*numpy.log10(z)
732 print ' '
733 print 'Z: '
734 print shape(z)
735 print ' '
736 print ' '
740
737 741
738 742 self.dataOut.data_spc=self.data_spc
739 743
740 744 self.noise = self.dataOut.getNoise(ymin_index=80, ymax_index=132)#/factor
741 745 #noisedB = 10*numpy.log10(self.noise)
742 746
743 747
744 748 ySamples=numpy.ones([3,self.nProfiles])
745 749 phase=numpy.ones([3,self.nProfiles])
746 750 CSPCSamples=numpy.ones([3,self.nProfiles],dtype=numpy.complex_)
747 751 coherence=numpy.ones([3,self.nProfiles])
748 752 PhaseSlope=numpy.ones(3)
749 753 PhaseInter=numpy.ones(3)
750 754
751 755 '''****** Getting CrossSpectra ******'''
752 756 cspc=self.data_block.copy()
753 757 self.data_cspc=self.data_block.copy()
754 758
755 759 xFrec=self.getVelRange(1)
756 760 VelRange=self.getVelRange(1)
757 761 self.dataOut.VelRange=VelRange
758 762 #print ' '
759 763 #print ' '
760 764 #print 'xFrec',xFrec
761 765 #print ' '
762 766 #print ' '
763 767 #Height=35
764 768
765 769 for i in range(self.nRdPairs):
766 770
767 771 chan_index0 = self.dataOut.pairsList[i][0]
768 772 chan_index1 = self.dataOut.pairsList[i][1]
769 773
770 774 self.data_cspc[i,:,:]=cspc[chan_index0,:,:] * numpy.conjugate(cspc[chan_index1,:,:])
771 775
772 776
773 777 '''Getting Eij and Nij'''
774 778 (AntennaX0,AntennaY0)=pol2cart(rheader.AntennaCoord0, rheader.AntennaAngl0*numpy.pi/180)
775 779 (AntennaX1,AntennaY1)=pol2cart(rheader.AntennaCoord1, rheader.AntennaAngl1*numpy.pi/180)
776 780 (AntennaX2,AntennaY2)=pol2cart(rheader.AntennaCoord2, rheader.AntennaAngl2*numpy.pi/180)
777 781
778 782 E01=AntennaX0-AntennaX1
779 783 N01=AntennaY0-AntennaY1
780 784
781 785 E02=AntennaX0-AntennaX2
782 786 N02=AntennaY0-AntennaY2
783 787
784 788 E12=AntennaX1-AntennaX2
785 789 N12=AntennaY1-AntennaY2
786 790
787 791 self.ChanDist= numpy.array([[E01, N01],[E02,N02],[E12,N12]])
788 792
789 793 self.dataOut.ChanDist = self.ChanDist
790 794
791 795
792 # for Height in range(self.nHeights):
793 #
794 # for i in range(self.nRdPairs):
795 #
796 # '''****** Line of Data SPC ******'''
797 # zline=z[i,:,Height]
798 #
799 # '''****** DC is removed ******'''
800 # DC=Find(zline,numpy.amax(zline))
801 # zline[DC]=(zline[DC-1]+zline[DC+1])/2
802 #
803 #
804 # '''****** SPC is normalized ******'''
805 # FactNorm= zline.copy() / numpy.sum(zline.copy())
806 # FactNorm= FactNorm/numpy.sum(FactNorm)
807 #
808 # SmoothSPC=moving_average(FactNorm,N=3)
809 #
810 # xSamples = ar(range(len(SmoothSPC)))
811 # ySamples[i] = SmoothSPC-self.noise[i]
812 #
813 # for i in range(self.nRdPairs):
814 #
815 # '''****** Line of Data CSPC ******'''
816 # cspcLine=self.data_cspc[i,:,Height].copy()
817 #
818 #
819 #
820 # '''****** CSPC is normalized ******'''
821 # chan_index0 = self.dataOut.pairsList[i][0]
822 # chan_index1 = self.dataOut.pairsList[i][1]
823 # CSPCFactor= numpy.sum(ySamples[chan_index0]) * numpy.sum(ySamples[chan_index1])
824 #
825 #
826 # CSPCNorm= cspcLine.copy() / numpy.sqrt(CSPCFactor)
827 #
828 #
829 # CSPCSamples[i] = CSPCNorm-self.noise[i]
830 # coherence[i] = numpy.abs(CSPCSamples[i]) / numpy.sqrt(CSPCFactor)
831 #
832 # '''****** DC is removed ******'''
833 # DC=Find(coherence[i],numpy.amax(coherence[i]))
834 # coherence[i][DC]=(coherence[i][DC-1]+coherence[i][DC+1])/2
835 # coherence[i]= moving_average(coherence[i],N=2)
836 #
837 # phase[i] = moving_average( numpy.arctan2(CSPCSamples[i].imag, CSPCSamples[i].real),N=1)#*180/numpy.pi
838 #
839 #
840 # '''****** Getting fij width ******'''
841 #
842 # yMean=[]
843 # yMean2=[]
844 #
845 # for j in range(len(ySamples[1])):
846 # yMean=numpy.append(yMean,numpy.average([ySamples[0,j],ySamples[1,j],ySamples[2,j]]))
847 #
848 # '''******* Getting fitting Gaussian ******'''
849 # meanGauss=sum(xSamples*yMean) / len(xSamples)
850 # sigma=sum(yMean*(xSamples-meanGauss)**2) / len(xSamples)
851 # #print 'Height',Height,'SNR', meanGauss/sigma**2
852 #
853 # if (abs(meanGauss/sigma**2) > 0.0001) :
854 #
855 # try:
856 # popt,pcov = curve_fit(gaus,xSamples,yMean,p0=[1,meanGauss,sigma])
857 #
858 # if numpy.amax(popt)>numpy.amax(yMean)*0.3:
859 # FitGauss=gaus(xSamples,*popt)
860 #
861 # else:
862 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
863 # print 'Verificador: Dentro', Height
864 # except RuntimeError:
865 #
866 # try:
867 # for j in range(len(ySamples[1])):
868 # yMean2=numpy.append(yMean2,numpy.average([ySamples[1,j],ySamples[2,j]]))
869 # popt,pcov = curve_fit(gaus,xSamples,yMean2,p0=[1,meanGauss,sigma])
870 # FitGauss=gaus(xSamples,*popt)
871 # print 'Verificador: Exepcion1', Height
872 # except RuntimeError:
873 #
874 # try:
875 # popt,pcov = curve_fit(gaus,xSamples,ySamples[1],p0=[1,meanGauss,sigma])
876 # FitGauss=gaus(xSamples,*popt)
877 # print 'Verificador: Exepcion2', Height
878 # except RuntimeError:
879 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
880 # print 'Verificador: Exepcion3', Height
881 # else:
882 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
883 # #print 'Verificador: Fuera', Height
884 #
885 #
886 #
887 # Maximun=numpy.amax(yMean)
888 # eMinus1=Maximun*numpy.exp(-1)
889 #
890 # HWpos=Find(FitGauss,min(FitGauss, key=lambda value:abs(value-eMinus1)))
891 # HalfWidth= xFrec[HWpos]
892 # GCpos=Find(FitGauss, numpy.amax(FitGauss))
893 # Vpos=Find(FactNorm, numpy.amax(FactNorm))
894 # #Vpos=numpy.sum(FactNorm)/len(FactNorm)
895 # #Vpos=Find(FactNorm, min(FactNorm, key=lambda value:abs(value- numpy.mean(FactNorm) )))
896 # #print 'GCpos',GCpos, numpy.amax(FitGauss), 'HWpos',HWpos
897 # '''****** Getting Fij ******'''
898 #
899 # GaussCenter=xFrec[GCpos]
900 # if (GaussCenter<0 and HalfWidth>0) or (GaussCenter>0 and HalfWidth<0):
901 # Fij=abs(GaussCenter)+abs(HalfWidth)+0.0000001
902 # else:
903 # Fij=abs(GaussCenter-HalfWidth)+0.0000001
904 #
905 # '''****** Getting Frecuency range of significant data ******'''
906 #
907 # Rangpos=Find(FitGauss,min(FitGauss, key=lambda value:abs(value-Maximun*0.10)))
908 #
909 # if Rangpos<GCpos:
910 # Range=numpy.array([Rangpos,2*GCpos-Rangpos])
911 # else:
912 # Range=numpy.array([2*GCpos-Rangpos,Rangpos])
913 #
914 # FrecRange=xFrec[Range[0]:Range[1]]
915 #
916 # #print 'FrecRange', FrecRange
917 # '''****** Getting SCPC Slope ******'''
918 #
919 # for i in range(self.nRdPairs):
920 #
921 # if len(FrecRange)>5 and len(FrecRange)<self.nProfiles*0.5:
922 # PhaseRange=moving_average(phase[i,Range[0]:Range[1]],N=3)
923 #
924 # slope, intercept, r_value, p_value, std_err = stats.linregress(FrecRange,PhaseRange)
925 # PhaseSlope[i]=slope
926 # PhaseInter[i]=intercept
927 # else:
928 # PhaseSlope[i]=0
929 # PhaseInter[i]=0
930 #
931 # # plt.figure(i+15)
932 # # plt.title('FASE ( CH%s*CH%s )' %(self.dataOut.pairsList[i][0],self.dataOut.pairsList[i][1]))
933 # # plt.xlabel('Frecuencia (KHz)')
934 # # plt.ylabel('Magnitud')
935 # # #plt.subplot(311+i)
936 # # plt.plot(FrecRange,PhaseRange,'b')
937 # # plt.plot(FrecRange,FrecRange*PhaseSlope[i]+PhaseInter[i],'r')
938 #
939 # #plt.axis([-0.6, 0.2, -3.2, 3.2])
940 #
941 #
942 # '''Getting constant C'''
943 # cC=(Fij*numpy.pi)**2
944 #
945 # # '''Getting Eij and Nij'''
946 # # (AntennaX0,AntennaY0)=pol2cart(rheader.AntennaCoord0, rheader.AntennaAngl0*numpy.pi/180)
947 # # (AntennaX1,AntennaY1)=pol2cart(rheader.AntennaCoord1, rheader.AntennaAngl1*numpy.pi/180)
948 # # (AntennaX2,AntennaY2)=pol2cart(rheader.AntennaCoord2, rheader.AntennaAngl2*numpy.pi/180)
949 # #
950 # # E01=AntennaX0-AntennaX1
951 # # N01=AntennaY0-AntennaY1
952 # #
953 # # E02=AntennaX0-AntennaX2
954 # # N02=AntennaY0-AntennaY2
955 # #
956 # # E12=AntennaX1-AntennaX2
957 # # N12=AntennaY1-AntennaY2
958 #
959 # '''****** Getting constants F and G ******'''
960 # MijEijNij=numpy.array([[E02,N02], [E12,N12]])
961 # MijResult0=(-PhaseSlope[1]*cC) / (2*numpy.pi)
962 # MijResult1=(-PhaseSlope[2]*cC) / (2*numpy.pi)
963 # MijResults=numpy.array([MijResult0,MijResult1])
964 # (cF,cG) = numpy.linalg.solve(MijEijNij, MijResults)
965 #
966 # '''****** Getting constants A, B and H ******'''
967 # W01=numpy.amax(coherence[0])
968 # W02=numpy.amax(coherence[1])
969 # W12=numpy.amax(coherence[2])
970 #
971 # WijResult0=((cF*E01+cG*N01)**2)/cC - numpy.log(W01 / numpy.sqrt(numpy.pi/cC))
972 # WijResult1=((cF*E02+cG*N02)**2)/cC - numpy.log(W02 / numpy.sqrt(numpy.pi/cC))
973 # WijResult2=((cF*E12+cG*N12)**2)/cC - numpy.log(W12 / numpy.sqrt(numpy.pi/cC))
974 #
975 # WijResults=numpy.array([WijResult0, WijResult1, WijResult2])
976 #
977 # WijEijNij=numpy.array([ [E01**2, N01**2, 2*E01*N01] , [E02**2, N02**2, 2*E02*N02] , [E12**2, N12**2, 2*E12*N12] ])
978 # (cA,cB,cH) = numpy.linalg.solve(WijEijNij, WijResults)
979 #
980 # VxVy=numpy.array([[cA,cH],[cH,cB]])
981 #
982 # VxVyResults=numpy.array([-cF,-cG])
983 # (Vx,Vy) = numpy.linalg.solve(VxVy, VxVyResults)
984 # Vzon = Vy
985 # Vmer = Vx
986 # Vmag=numpy.sqrt(Vzon**2+Vmer**2)
987 # Vang=numpy.arctan2(Vmer,Vzon)
988 #
989 # if abs(Vy)<100 and abs(Vy)> 0.:
990 # self.dataOut.velocityX=numpy.append(self.dataOut.velocityX, Vzon) #Vmag
991 # #print 'Vmag',Vmag
992 # else:
993 # self.dataOut.velocityX=numpy.append(self.dataOut.velocityX, NaN)
994 #
995 # if abs(Vx)<100 and abs(Vx) > 0.:
996 # self.dataOut.velocityY=numpy.append(self.dataOut.velocityY, Vmer) #Vang
997 # #print 'Vang',Vang
998 # else:
999 # self.dataOut.velocityY=numpy.append(self.dataOut.velocityY, NaN)
1000 #
1001 # if abs(GaussCenter)<2:
1002 # self.dataOut.velocityV=numpy.append(self.dataOut.velocityV, xFrec[Vpos])
1003 #
1004 # else:
1005 # self.dataOut.velocityV=numpy.append(self.dataOut.velocityV, NaN)
1006 #
1007 #
1008 # # print '********************************************'
1009 # # print 'HalfWidth ', HalfWidth
1010 # # print 'Maximun ', Maximun
1011 # # print 'eMinus1 ', eMinus1
1012 # # print 'Rangpos ', Rangpos
1013 # # print 'GaussCenter ',GaussCenter
1014 # # print 'E01 ',E01
1015 # # print 'N01 ',N01
1016 # # print 'E02 ',E02
1017 # # print 'N02 ',N02
1018 # # print 'E12 ',E12
1019 # # print 'N12 ',N12
1020 # #print 'self.dataOut.velocityX ', self.dataOut.velocityX
1021 # # print 'Fij ', Fij
1022 # # print 'cC ', cC
1023 # # print 'cF ', cF
1024 # # print 'cG ', cG
1025 # # print 'cA ', cA
1026 # # print 'cB ', cB
1027 # # print 'cH ', cH
1028 # # print 'Vx ', Vx
1029 # # print 'Vy ', Vy
1030 # # print 'Vmag ', Vmag
1031 # # print 'Vang ', Vang*180/numpy.pi
1032 # # print 'PhaseSlope ',PhaseSlope[0]
1033 # # print 'PhaseSlope ',PhaseSlope[1]
1034 # # print 'PhaseSlope ',PhaseSlope[2]
1035 # # print '********************************************'
1036 # #print 'data_output',shape(self.dataOut.velocityX), shape(self.dataOut.velocityY)
1037 #
1038 # #print 'self.dataOut.velocityX', len(self.dataOut.velocityX)
1039 # #print 'self.dataOut.velocityY', len(self.dataOut.velocityY)
1040 # #print 'self.dataOut.velocityV', self.dataOut.velocityV
1041 #
1042 # self.data_output[0]=numpy.array(self.dataOut.velocityX)
1043 # self.data_output[1]=numpy.array(self.dataOut.velocityY)
1044 # self.data_output[2]=numpy.array(self.dataOut.velocityV)
1045 #
1046 # prin= self.data_output[0][~numpy.isnan(self.data_output[0])]
1047 # print ' '
1048 # print 'VmagAverage',numpy.mean(prin)
1049 # print ' '
1050 # # plt.figure(5)
1051 # # plt.subplot(211)
1052 # # plt.plot(self.dataOut.velocityX,'yo:')
1053 # # plt.subplot(212)
1054 # # plt.plot(self.dataOut.velocityY,'yo:')
1055 #
1056 # # plt.figure(1)
1057 # # # plt.subplot(121)
1058 # # # plt.plot(xFrec,ySamples[0],'k',label='Ch0')
1059 # # # plt.plot(xFrec,ySamples[1],'g',label='Ch1')
1060 # # # plt.plot(xFrec,ySamples[2],'r',label='Ch2')
1061 # # # plt.plot(xFrec,FitGauss,'yo:',label='fit')
1062 # # # plt.legend()
1063 # # plt.title('DATOS A ALTURA DE 2850 METROS')
1064 # #
1065 # # plt.xlabel('Frecuencia (KHz)')
1066 # # plt.ylabel('Magnitud')
1067 # # # plt.subplot(122)
1068 # # # plt.title('Fit for Time Constant')
1069 # # #plt.plot(xFrec,zline)
1070 # # #plt.plot(xFrec,SmoothSPC,'g')
1071 # # plt.plot(xFrec,FactNorm)
1072 # # plt.axis([-4, 4, 0, 0.15])
1073 # # # plt.xlabel('SelfSpectra KHz')
1074 # #
1075 # # plt.figure(10)
1076 # # # plt.subplot(121)
1077 # # plt.plot(xFrec,ySamples[0],'b',label='Ch0')
1078 # # plt.plot(xFrec,ySamples[1],'y',label='Ch1')
1079 # # plt.plot(xFrec,ySamples[2],'r',label='Ch2')
1080 # # # plt.plot(xFrec,FitGauss,'yo:',label='fit')
1081 # # plt.legend()
1082 # # plt.title('SELFSPECTRA EN CANALES')
1083 # #
1084 # # plt.xlabel('Frecuencia (KHz)')
1085 # # plt.ylabel('Magnitud')
1086 # # # plt.subplot(122)
1087 # # # plt.title('Fit for Time Constant')
1088 # # #plt.plot(xFrec,zline)
1089 # # #plt.plot(xFrec,SmoothSPC,'g')
1090 # # # plt.plot(xFrec,FactNorm)
1091 # # # plt.axis([-4, 4, 0, 0.15])
1092 # # # plt.xlabel('SelfSpectra KHz')
1093 # #
1094 # # plt.figure(9)
1095 # #
1096 # #
1097 # # plt.title('DATOS SUAVIZADOS')
1098 # # plt.xlabel('Frecuencia (KHz)')
1099 # # plt.ylabel('Magnitud')
1100 # # plt.plot(xFrec,SmoothSPC,'g')
1101 # #
1102 # # #plt.plot(xFrec,FactNorm)
1103 # # plt.axis([-4, 4, 0, 0.15])
1104 # # # plt.xlabel('SelfSpectra KHz')
1105 # # #
1106 # # plt.figure(2)
1107 # # # #plt.subplot(121)
1108 # # plt.plot(xFrec,yMean,'r',label='Mean SelfSpectra')
1109 # # plt.plot(xFrec,FitGauss,'yo:',label='Ajuste Gaussiano')
1110 # # # plt.plot(xFrec[Rangpos],FitGauss[Find(FitGauss,min(FitGauss, key=lambda value:abs(value-Maximun*0.1)))],'bo')
1111 # # # #plt.plot(xFrec,phase)
1112 # # # plt.xlabel('Suavizado, promediado KHz')
1113 # # plt.title('SELFSPECTRA PROMEDIADO')
1114 # # # #plt.subplot(122)
1115 # # # #plt.plot(xSamples,zline)
1116 # # plt.xlabel('Frecuencia (KHz)')
1117 # # plt.ylabel('Magnitud')
1118 # # plt.legend()
1119 # # #
1120 # # # plt.figure(3)
1121 # # # plt.subplot(311)
1122 # # # #plt.plot(xFrec,phase[0])
1123 # # # plt.plot(xFrec,phase[0],'g')
1124 # # # plt.subplot(312)
1125 # # # plt.plot(xFrec,phase[1],'g')
1126 # # # plt.subplot(313)
1127 # # # plt.plot(xFrec,phase[2],'g')
1128 # # # #plt.plot(xFrec,phase[2])
1129 # # #
1130 # # # plt.figure(4)
1131 # # #
1132 # # # plt.plot(xSamples,coherence[0],'b')
1133 # # # plt.plot(xSamples,coherence[1],'r')
1134 # # # plt.plot(xSamples,coherence[2],'g')
1135 # # plt.show()
1136 # # #
1137 # # # plt.clf()
1138 # # # plt.cla()
1139 # # # plt.close()
1140 #
1141 # print ' '
1142
1143
1144
1145 self.BlockCounter+=2
1146
1147 else:
1148 self.fileSelector+=1
1149 self.BlockCounter=0
1150 print "Next File"
1151
1152
1153
1154
1155
@@ -1,1091 +1,1095
1 1 import numpy
2 2 import time
3 3 import os
4 4 import h5py
5 5 import re
6 6 import datetime
7 7
8 8 from schainpy.model.data.jrodata import *
9 9 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
10 10 # from jroIO_base import *
11 11 from schainpy.model.io.jroIO_base import *
12 12 import schainpy
13 13
14 14
15 15 class ParamReader(ProcessingUnit):
16 16 '''
17 17 Reads HDF5 format files
18 18
19 19 path
20 20
21 21 startDate
22 22
23 23 endDate
24 24
25 25 startTime
26 26
27 27 endTime
28 28 '''
29 29
30 30 ext = ".hdf5"
31 31
32 32 optchar = "D"
33 33
34 34 timezone = None
35 35
36 36 startTime = None
37 37
38 38 endTime = None
39 39
40 40 fileIndex = None
41 41
42 42 utcList = None #To select data in the utctime list
43 43
44 44 blockList = None #List to blocks to be read from the file
45 45
46 46 blocksPerFile = None #Number of blocks to be read
47 47
48 48 blockIndex = None
49 49
50 50 path = None
51 51
52 52 #List of Files
53 53
54 54 filenameList = None
55 55
56 56 datetimeList = None
57 57
58 58 #Hdf5 File
59 59
60 60 listMetaname = None
61 61
62 62 listMeta = None
63 63
64 64 listDataname = None
65 65
66 66 listData = None
67 67
68 68 listShapes = None
69 69
70 70 fp = None
71 71
72 72 #dataOut reconstruction
73 73
74 74 dataOut = None
75 75
76 76
77 77 def __init__(self, **kwargs):
78 78 ProcessingUnit.__init__(self, **kwargs)
79 79 self.dataOut = Parameters()
80 80 return
81 81
82 82 def setup(self, **kwargs):
83 83
84 84 path = kwargs['path']
85 85 startDate = kwargs['startDate']
86 86 endDate = kwargs['endDate']
87 87 startTime = kwargs['startTime']
88 88 endTime = kwargs['endTime']
89 89 walk = kwargs['walk']
90 90 if kwargs.has_key('ext'):
91 91 ext = kwargs['ext']
92 92 else:
93 93 ext = '.hdf5'
94 94 if kwargs.has_key('timezone'):
95 95 self.timezone = kwargs['timezone']
96 96 else:
97 97 self.timezone = 'lt'
98 98
99 99 print "[Reading] Searching files in offline mode ..."
100 100 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
101 101 startTime=startTime, endTime=endTime,
102 102 ext=ext, walk=walk)
103 103
104 104 if not(filenameList):
105 105 print "There is no files into the folder: %s"%(path)
106 106 sys.exit(-1)
107 107
108 108 self.fileIndex = -1
109 109 self.startTime = startTime
110 110 self.endTime = endTime
111 111
112 112 self.__readMetadata()
113 113
114 114 self.__setNextFileOffline()
115 115
116 116 return
117 117
118 118 def __searchFilesOffLine(self,
119 119 path,
120 120 startDate=None,
121 121 endDate=None,
122 122 startTime=datetime.time(0,0,0),
123 123 endTime=datetime.time(23,59,59),
124 124 ext='.hdf5',
125 125 walk=True):
126 126
127 127 expLabel = ''
128 128 self.filenameList = []
129 129 self.datetimeList = []
130 130
131 131 pathList = []
132 132
133 133 JRODataObj = JRODataReader()
134 134 dateList, pathList = JRODataObj.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
135 135
136 136 if dateList == []:
137 137 print "[Reading] No *%s files in %s from %s to %s)"%(ext, path,
138 138 datetime.datetime.combine(startDate,startTime).ctime(),
139 139 datetime.datetime.combine(endDate,endTime).ctime())
140 140
141 141 return None, None
142 142
143 143 if len(dateList) > 1:
144 144 print "[Reading] %d days were found in date range: %s - %s" %(len(dateList), startDate, endDate)
145 145 else:
146 146 print "[Reading] data was found for the date %s" %(dateList[0])
147 147
148 148 filenameList = []
149 149 datetimeList = []
150 150
151 151 #----------------------------------------------------------------------------------
152 152
153 153 for thisPath in pathList:
154 154 # thisPath = pathList[pathDict[file]]
155 155
156 156 fileList = glob.glob1(thisPath, "*%s" %ext)
157 157 fileList.sort()
158 158
159 159 for file in fileList:
160 160
161 161 filename = os.path.join(thisPath,file)
162 162
163 163 if not isFileInDateRange(filename, startDate, endDate):
164 164 continue
165 165
166 166 thisDatetime = self.__isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
167 167
168 168 if not(thisDatetime):
169 169 continue
170 170
171 171 filenameList.append(filename)
172 172 datetimeList.append(thisDatetime)
173 173
174 174 if not(filenameList):
175 175 print "[Reading] Any file was found int time range %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
176 176 return None, None
177 177
178 178 print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)
179 179 print
180 180
181 181 # for i in range(len(filenameList)):
182 182 # print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
183 183
184 184 self.filenameList = filenameList
185 185 self.datetimeList = datetimeList
186 186
187 187 return pathList, filenameList
188 188
189 189 def __isFileInTimeRange(self,filename, startDate, endDate, startTime, endTime):
190 190
191 191 """
192 192 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
193 193
194 194 Inputs:
195 195 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
196 196
197 197 startDate : fecha inicial del rango seleccionado en formato datetime.date
198 198
199 199 endDate : fecha final del rango seleccionado en formato datetime.date
200 200
201 201 startTime : tiempo inicial del rango seleccionado en formato datetime.time
202 202
203 203 endTime : tiempo final del rango seleccionado en formato datetime.time
204 204
205 205 Return:
206 206 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
207 207 fecha especificado, de lo contrario retorna False.
208 208
209 209 Excepciones:
210 210 Si el archivo no existe o no puede ser abierto
211 211 Si la cabecera no puede ser leida.
212 212
213 213 """
214 214
215 215 try:
216 216 fp = h5py.File(filename,'r')
217 217 grp1 = fp['Data']
218 218
219 219 except IOError:
220 220 traceback.print_exc()
221 221 raise IOError, "The file %s can't be opened" %(filename)
222 222 #chino rata
223 223 #In case has utctime attribute
224 224 grp2 = grp1['utctime']
225 225 # thisUtcTime = grp2.value[0] - 5*3600 #To convert to local time
226 226 thisUtcTime = grp2.value[0]
227 227
228 228 fp.close()
229 229
230 230 if self.timezone == 'lt':
231 231 thisUtcTime -= 5*3600
232 232
233 233 thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0] + 5*3600)
234 234 # thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0])
235 235 thisDate = thisDatetime.date()
236 236 thisTime = thisDatetime.time()
237 237
238 238 startUtcTime = (datetime.datetime.combine(thisDate,startTime)- datetime.datetime(1970, 1, 1)).total_seconds()
239 239 endUtcTime = (datetime.datetime.combine(thisDate,endTime)- datetime.datetime(1970, 1, 1)).total_seconds()
240 240
241 241 #General case
242 242 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
243 243 #-----------o----------------------------o-----------
244 244 # startTime endTime
245 245
246 246 if endTime >= startTime:
247 247 thisUtcLog = numpy.logical_and(thisUtcTime > startUtcTime, thisUtcTime < endUtcTime)
248 248 if numpy.any(thisUtcLog): #If there is one block between the hours mentioned
249 249 return thisDatetime
250 250 return None
251 251
252 252 #If endTime < startTime then endTime belongs to the next day
253 253 #<<<<<<<<<<<o o>>>>>>>>>>>
254 254 #-----------o----------------------------o-----------
255 255 # endTime startTime
256 256
257 257 if (thisDate == startDate) and numpy.all(thisUtcTime < startUtcTime):
258 258 return None
259 259
260 260 if (thisDate == endDate) and numpy.all(thisUtcTime > endUtcTime):
261 261 return None
262 262
263 263 if numpy.all(thisUtcTime < startUtcTime) and numpy.all(thisUtcTime > endUtcTime):
264 264 return None
265 265
266 266 return thisDatetime
267 267
268 268 def __setNextFileOffline(self):
269 269
270 270 self.fileIndex += 1
271 271 idFile = self.fileIndex
272 272
273 273 if not(idFile < len(self.filenameList)):
274 274 print "No more Files"
275 275 return 0
276 276
277 277 filename = self.filenameList[idFile]
278 278
279 279 filePointer = h5py.File(filename,'r')
280 280
281 281 self.filename = filename
282 282
283 283 self.fp = filePointer
284 284
285 285 print "Setting the file: %s"%self.filename
286 286
287 287 # self.__readMetadata()
288 288 self.__setBlockList()
289 289 self.__readData()
290 290 # self.nRecords = self.fp['Data'].attrs['blocksPerFile']
291 291 # self.nRecords = self.fp['Data'].attrs['nRecords']
292 292 self.blockIndex = 0
293 293 return 1
294 294
295 295 def __setBlockList(self):
296 296 '''
297 297 Selects the data within the times defined
298 298
299 299 self.fp
300 300 self.startTime
301 301 self.endTime
302 302
303 303 self.blockList
304 304 self.blocksPerFile
305 305
306 306 '''
307 307 fp = self.fp
308 308 startTime = self.startTime
309 309 endTime = self.endTime
310 310
311 311 grp = fp['Data']
312 312 thisUtcTime = grp['utctime'].value.astype(numpy.float)[0]
313 313
314 314 #ERROOOOR
315 315 if self.timezone == 'lt':
316 316 thisUtcTime -= 5*3600
317 317
318 318 thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0] + 5*3600)
319 319
320 320 thisDate = thisDatetime.date()
321 321 thisTime = thisDatetime.time()
322 322
323 323 startUtcTime = (datetime.datetime.combine(thisDate,startTime) - datetime.datetime(1970, 1, 1)).total_seconds()
324 324 endUtcTime = (datetime.datetime.combine(thisDate,endTime) - datetime.datetime(1970, 1, 1)).total_seconds()
325 325
326 326 ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0]
327 327
328 328 self.blockList = ind
329 329 self.blocksPerFile = len(ind)
330 330
331 331 return
332 332
333 333 def __readMetadata(self):
334 334 '''
335 335 Reads Metadata
336 336
337 337 self.pathMeta
338 338
339 339 self.listShapes
340 340 self.listMetaname
341 341 self.listMeta
342 342
343 343 '''
344 344
345 345 # grp = self.fp['Data']
346 346 # pathMeta = os.path.join(self.path, grp.attrs['metadata'])
347 347 #
348 348 # if pathMeta == self.pathMeta:
349 349 # return
350 350 # else:
351 351 # self.pathMeta = pathMeta
352 352 #
353 353 # filePointer = h5py.File(self.pathMeta,'r')
354 354 # groupPointer = filePointer['Metadata']
355 355
356 356 filename = self.filenameList[0]
357 357
358 358 fp = h5py.File(filename,'r')
359 359
360 360 gp = fp['Metadata']
361 361
362 362 listMetaname = []
363 363 listMetadata = []
364 364 for item in gp.items():
365 365 name = item[0]
366 366
367 367 if name=='array dimensions':
368 368 table = gp[name][:]
369 369 listShapes = {}
370 370 for shapes in table:
371 371 listShapes[shapes[0]] = numpy.array([shapes[1],shapes[2],shapes[3],shapes[4],shapes[5]])
372 372 else:
373 373 data = gp[name].value
374 374 listMetaname.append(name)
375 375 listMetadata.append(data)
376 376
377 377 # if name=='type':
378 378 # self.__initDataOut(data)
379 379
380 380 self.listShapes = listShapes
381 381 self.listMetaname = listMetaname
382 382 self.listMeta = listMetadata
383 383
384 384 fp.close()
385 385 return
386 386
387 387 def __readData(self):
388 388 grp = self.fp['Data']
389 389 listdataname = []
390 390 listdata = []
391 391
392 392 for item in grp.items():
393 393 name = item[0]
394 394 listdataname.append(name)
395 395
396 396 array = self.__setDataArray(grp[name],self.listShapes[name])
397 397 listdata.append(array)
398 398
399 399 self.listDataname = listdataname
400 400 self.listData = listdata
401 401 return
402 402
403 403 def __setDataArray(self, dataset, shapes):
404 404
405 405 nDims = shapes[0]
406 406
407 407 nDim2 = shapes[1] #Dimension 0
408 408
409 409 nDim1 = shapes[2] #Dimension 1, number of Points or Parameters
410 410
411 411 nDim0 = shapes[3] #Dimension 2, number of samples or ranges
412 412
413 413 mode = shapes[4] #Mode of storing
414 414
415 415 blockList = self.blockList
416 416
417 417 blocksPerFile = self.blocksPerFile
418 418
419 419 #Depending on what mode the data was stored
420 420 if mode == 0: #Divided in channels
421 421 arrayData = dataset.value.astype(numpy.float)[0][blockList]
422 422 if mode == 1: #Divided in parameter
423 423 strds = 'table'
424 424 nDatas = nDim1
425 425 newShapes = (blocksPerFile,nDim2,nDim0)
426 426 elif mode==2: #Concatenated in a table
427 427 strds = 'table0'
428 428 arrayData = dataset[strds].value
429 429 #Selecting part of the dataset
430 430 utctime = arrayData[:,0]
431 431 u, indices = numpy.unique(utctime, return_index=True)
432 432
433 433 if blockList.size != indices.size:
434 434 indMin = indices[blockList[0]]
435 435 if blockList[1] + 1 >= indices.size:
436 436 arrayData = arrayData[indMin:,:]
437 437 else:
438 438 indMax = indices[blockList[1] + 1]
439 439 arrayData = arrayData[indMin:indMax,:]
440 440 return arrayData
441 441
442 442 # One dimension
443 443 if nDims == 0:
444 444 arrayData = dataset.value.astype(numpy.float)[0][blockList]
445 445
446 446 # Two dimensions
447 447 elif nDims == 2:
448 448 arrayData = numpy.zeros((blocksPerFile,nDim1,nDim0))
449 449 newShapes = (blocksPerFile,nDim0)
450 450 nDatas = nDim1
451 451
452 452 for i in range(nDatas):
453 453 data = dataset[strds + str(i)].value
454 454 arrayData[:,i,:] = data[blockList,:]
455 455
456 456 # Three dimensions
457 457 else:
458 458 arrayData = numpy.zeros((blocksPerFile,nDim2,nDim1,nDim0))
459 459 for i in range(nDatas):
460 460
461 461 data = dataset[strds + str(i)].value
462 462
463 463 for b in range(blockList.size):
464 464 arrayData[b,:,i,:] = data[:,:,blockList[b]]
465 465
466 466 return arrayData
467 467
468 468 def __setDataOut(self):
469 469 listMeta = self.listMeta
470 470 listMetaname = self.listMetaname
471 471 listDataname = self.listDataname
472 472 listData = self.listData
473 473 listShapes = self.listShapes
474 474
475 475 blockIndex = self.blockIndex
476 476 # blockList = self.blockList
477 477
478 478 for i in range(len(listMeta)):
479 479 setattr(self.dataOut,listMetaname[i],listMeta[i])
480 480
481 481 for j in range(len(listData)):
482 482 nShapes = listShapes[listDataname[j]][0]
483 483 mode = listShapes[listDataname[j]][4]
484 484 if nShapes == 1:
485 485 setattr(self.dataOut,listDataname[j],listData[j][blockIndex])
486 486 elif nShapes > 1:
487 487 setattr(self.dataOut,listDataname[j],listData[j][blockIndex,:])
488 488 elif mode==0:
489 489 setattr(self.dataOut,listDataname[j],listData[j][blockIndex])
490 490 #Mode Meteors
491 491 elif mode ==2:
492 492 selectedData = self.__selectDataMode2(listData[j], blockIndex)
493 493 setattr(self.dataOut, listDataname[j], selectedData)
494 494 return
495 495
496 496 def __selectDataMode2(self, data, blockIndex):
497 497 utctime = data[:,0]
498 498 aux, indices = numpy.unique(utctime, return_inverse=True)
499 499 selInd = numpy.where(indices == blockIndex)[0]
500 500 selData = data[selInd,:]
501 501
502 502 return selData
503 503
504 504 def getData(self):
505 505
506 506 # if self.flagNoMoreFiles:
507 507 # self.dataOut.flagNoData = True
508 508 # print 'Process finished'
509 509 # return 0
510 510 #
511 511 if self.blockIndex==self.blocksPerFile:
512 512 if not( self.__setNextFileOffline() ):
513 513 self.dataOut.flagNoData = True
514 514 return 0
515 515
516 516 # if self.datablock == None: # setear esta condicion cuando no hayan datos por leers
517 517 # self.dataOut.flagNoData = True
518 518 # return 0
519 519 # self.__readData()
520 520 self.__setDataOut()
521 521 self.dataOut.flagNoData = False
522 522
523 523 self.blockIndex += 1
524 524
525 525 return
526 526
527 527 def run(self, **kwargs):
528 528
529 529 if not(self.isConfig):
530 530 self.setup(**kwargs)
531 531 # self.setObjProperties()
532 532 self.isConfig = True
533 533
534 534 self.getData()
535 535
536 536 return
537 537
538 538 class ParamWriter(Operation):
539 539 '''
540 540 HDF5 Writer, stores parameters data in HDF5 format files
541 541
542 542 path: path where the files will be stored
543 543
544 544 blocksPerFile: number of blocks that will be saved in per HDF5 format file
545 545
546 546 mode: selects the data stacking mode: '0' channels, '1' parameters, '3' table (for meteors)
547 547
548 548 metadataList: list of attributes that will be stored as metadata
549 549
550 550 dataList: list of attributes that will be stores as data
551 551
552 552 '''
553 553
554 554
555 555 ext = ".hdf5"
556 556
557 557 optchar = "D"
558 558
559 559 metaoptchar = "M"
560 560
561 561 metaFile = None
562 562
563 563 filename = None
564 564
565 565 path = None
566 566
567 567 setFile = None
568 568
569 569 fp = None
570 570
571 571 grp = None
572 572
573 573 ds = None
574 574
575 575 firsttime = True
576 576
577 577 #Configurations
578 578
579 579 blocksPerFile = None
580 580
581 581 blockIndex = None
582 582
583 583 dataOut = None
584 584
585 585 #Data Arrays
586 586
587 587 dataList = None
588 588
589 589 metadataList = None
590 590
591 591 # arrayDim = None
592 592
593 593 dsList = None #List of dictionaries with dataset properties
594 594
595 595 tableDim = None
596 596
597 597 # dtype = [('arrayName', 'S20'),('nChannels', 'i'), ('nPoints', 'i'), ('nSamples', 'i'),('mode', 'b')]
598 598
599 599 dtype = [('arrayName', 'S20'),('nDimensions', 'i'), ('dim2', 'i'), ('dim1', 'i'),('dim0', 'i'),('mode', 'b')]
600 600
601 601 currentDay = None
602 602
603 603 lastTime = None
604 604
605 605 def __init__(self, **kwargs):
606 606 Operation.__init__(self, **kwargs)
607 607 self.isConfig = False
608 608 return
609 609
610 610 def setup(self, dataOut, **kwargs):
611 611
612 612 self.path = kwargs['path']
613 613
614 614 if kwargs.has_key('blocksPerFile'):
615 615 self.blocksPerFile = kwargs['blocksPerFile']
616 616 else:
617 617 self.blocksPerFile = 10
618 618
619 619 self.metadataList = kwargs['metadataList']
620 620 self.dataList = kwargs['dataList']
621 621 self.dataOut = dataOut
622 622
623 623 if kwargs.has_key('mode'):
624 624 mode = kwargs['mode']
625 625
626 626 if type(mode) == int:
627 627 mode = numpy.zeros(len(self.dataList)) + mode
628 628 else:
629 629 mode = numpy.ones(len(self.dataList))
630 630
631 631 self.mode = mode
632 632
633 633 arrayDim = numpy.zeros((len(self.dataList),5))
634 634
635 635 #Table dimensions
636 636 dtype0 = self.dtype
637 637 tableList = []
638 638
639 639 #Dictionary and list of tables
640 640 dsList = []
641 641
642 642 for i in range(len(self.dataList)):
643 643 dsDict = {}
644 644 dataAux = getattr(self.dataOut, self.dataList[i])
645 645 dsDict['variable'] = self.dataList[i]
646 646 #--------------------- Conditionals ------------------------
647 647 #There is no data
648
649
648 650 if dataAux is None:
651
649 652 return 0
650 653
651 654 #Not array, just a number
652 655 #Mode 0
653 656 if type(dataAux)==float or type(dataAux)==int:
654 657 dsDict['mode'] = 0
655 658 dsDict['nDim'] = 0
656 659 arrayDim[i,0] = 0
657 660 dsList.append(dsDict)
658 661
659 662 #Mode 2: meteors
660 663 elif mode[i] == 2:
661 664 # dsDict['nDim'] = 0
662 665 dsDict['dsName'] = 'table0'
663 666 dsDict['mode'] = 2 # Mode meteors
664 667 dsDict['shape'] = dataAux.shape[-1]
665 668 dsDict['nDim'] = 0
666 669 dsDict['dsNumber'] = 1
667 670
668 671 arrayDim[i,3] = dataAux.shape[-1]
669 672 arrayDim[i,4] = mode[i] #Mode the data was stored
670 673
671 674 dsList.append(dsDict)
672 675
673 676 #Mode 1
674 677 else:
675 678 arrayDim0 = dataAux.shape #Data dimensions
676 679 arrayDim[i,0] = len(arrayDim0) #Number of array dimensions
677 680 arrayDim[i,4] = mode[i] #Mode the data was stored
678 681
679 682 strtable = 'table'
680 683 dsDict['mode'] = 1 # Mode parameters
681 684
682 685 # Three-dimension arrays
683 686 if len(arrayDim0) == 3:
684 687 arrayDim[i,1:-1] = numpy.array(arrayDim0)
685 688 nTables = int(arrayDim[i,2])
686 689 dsDict['dsNumber'] = nTables
687 690 dsDict['shape'] = arrayDim[i,2:4]
688 691 dsDict['nDim'] = 3
689 692
690 693 for j in range(nTables):
691 694 dsDict = dsDict.copy()
692 695 dsDict['dsName'] = strtable + str(j)
693 696 dsList.append(dsDict)
694 697
695 698 # Two-dimension arrays
696 699 elif len(arrayDim0) == 2:
697 700 arrayDim[i,2:-1] = numpy.array(arrayDim0)
698 701 nTables = int(arrayDim[i,2])
699 702 dsDict['dsNumber'] = nTables
700 703 dsDict['shape'] = arrayDim[i,3]
701 704 dsDict['nDim'] = 2
702 705
703 706 for j in range(nTables):
704 707 dsDict = dsDict.copy()
705 708 dsDict['dsName'] = strtable + str(j)
706 709 dsList.append(dsDict)
707 710
708 711 # One-dimension arrays
709 712 elif len(arrayDim0) == 1:
710 713 arrayDim[i,3] = arrayDim0[0]
711 714 dsDict['shape'] = arrayDim0[0]
712 715 dsDict['dsNumber'] = 1
713 716 dsDict['dsName'] = strtable + str(0)
714 717 dsDict['nDim'] = 1
715 718 dsList.append(dsDict)
716 719
717 720 table = numpy.array((self.dataList[i],) + tuple(arrayDim[i,:]),dtype = dtype0)
718 721 tableList.append(table)
719 722
720 723 # self.arrayDim = arrayDim
721 724 self.dsList = dsList
722 725 self.tableDim = numpy.array(tableList, dtype = dtype0)
723 726 self.blockIndex = 0
724 727
725 728 timeTuple = time.localtime(dataOut.utctime)
726 729 self.currentDay = timeTuple.tm_yday
727 730 return 1
728 731
729 732 def putMetadata(self):
730 733
731 734 fp = self.createMetadataFile()
732 735 self.writeMetadata(fp)
733 736 fp.close()
734 737 return
735 738
736 739 def createMetadataFile(self):
737 740 ext = self.ext
738 741 path = self.path
739 742 setFile = self.setFile
740 743
741 744 timeTuple = time.localtime(self.dataOut.utctime)
742 745
743 746 subfolder = ''
744 747 fullpath = os.path.join( path, subfolder )
745 748
746 749 if not( os.path.exists(fullpath) ):
747 750 os.mkdir(fullpath)
748 751 setFile = -1 #inicializo mi contador de seteo
749 752
750 753 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
751 754 fullpath = os.path.join( path, subfolder )
752 755
753 756 if not( os.path.exists(fullpath) ):
754 757 os.mkdir(fullpath)
755 758 setFile = -1 #inicializo mi contador de seteo
756 759
757 760 else:
758 761 filesList = os.listdir( fullpath )
759 762 filesList = sorted( filesList, key=str.lower )
760 763 if len( filesList ) > 0:
761 764 filesList = [k for k in filesList if 'M' in k]
762 765 filen = filesList[-1]
763 766 # el filename debera tener el siguiente formato
764 767 # 0 1234 567 89A BCDE (hex)
765 768 # x YYYY DDD SSS .ext
766 769 if isNumber( filen[8:11] ):
767 770 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
768 771 else:
769 772 setFile = -1
770 773 else:
771 774 setFile = -1 #inicializo mi contador de seteo
772 775
773 776 setFile += 1
774 777
775 778 file = '%s%4.4d%3.3d%3.3d%s' % (self.metaoptchar,
776 779 timeTuple.tm_year,
777 780 timeTuple.tm_yday,
778 781 setFile,
779 782 ext )
780 783
781 784 filename = os.path.join( path, subfolder, file )
782 785 self.metaFile = file
783 786 #Setting HDF5 File
784 787 fp = h5py.File(filename,'w')
785 788
786 789 return fp
787 790
788 791 def writeMetadata(self, fp):
789 792
790 793 grp = fp.create_group("Metadata")
791 794 grp.create_dataset('array dimensions', data = self.tableDim, dtype = self.dtype)
792 795
793 796 for i in range(len(self.metadataList)):
794 797 print '#####',self.metadataList[i], getattr(self.dataOut, self.metadataList[i])
795 798 grp.create_dataset(self.metadataList[i], data=getattr(self.dataOut, self.metadataList[i]))
796 799 return
797 800
798 801 def timeFlag(self):
799 802 currentTime = self.dataOut.utctime
800 803
801 804 if self.lastTime is None:
802 805 self.lastTime = currentTime
803 806
804 807 #Day
805 808 timeTuple = time.localtime(currentTime)
806 809 dataDay = timeTuple.tm_yday
807 810
808 811 #Time
809 812 timeDiff = currentTime - self.lastTime
810 813
811 814 #Si el dia es diferente o si la diferencia entre un dato y otro supera la hora
812 815 if dataDay != self.currentDay:
813 816 self.currentDay = dataDay
814 817 return True
815 818 elif timeDiff > 3*60*60:
816 819 self.lastTime = currentTime
817 820 return True
818 821 else:
819 822 self.lastTime = currentTime
820 823 return False
821 824
822 825 def setNextFile(self):
823
826
824 827 ext = self.ext
825 828 path = self.path
826 829 setFile = self.setFile
827 830 mode = self.mode
828 831
829 832 timeTuple = time.localtime(self.dataOut.utctime)
830 833 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
831 834
832 835 fullpath = os.path.join( path, subfolder )
833 836
834 837 if os.path.exists(fullpath):
835 838 filesList = os.listdir( fullpath )
836 839 filesList = [k for k in filesList if 'D' in k]
837 840 if len( filesList ) > 0:
838 841 filesList = sorted( filesList, key=str.lower )
839 842 filen = filesList[-1]
840 843 # el filename debera tener el siguiente formato
841 844 # 0 1234 567 89A BCDE (hex)
842 845 # x YYYY DDD SSS .ext
843 846 if isNumber( filen[8:11] ):
844 847 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
845 848 else:
846 849 setFile = -1
847 850 else:
848 851 setFile = -1 #inicializo mi contador de seteo
849 852 else:
850 853 os.makedirs(fullpath)
851 854 setFile = -1 #inicializo mi contador de seteo
852 855
853 856 setFile += 1
854 857
855 858 file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar,
856 859 timeTuple.tm_year,
857 860 timeTuple.tm_yday,
858 861 setFile,
859 862 ext )
860 863
861 864 filename = os.path.join( path, subfolder, file )
862 865
863 866 #Setting HDF5 File
864 867 fp = h5py.File(filename,'w')
865 868 #write metadata
866 869 self.writeMetadata(fp)
867 870 #Write data
868 871 grp = fp.create_group("Data")
869 872 # grp.attrs['metadata'] = self.metaFile
870 873
871 874 # grp.attrs['blocksPerFile'] = 0
872 875 ds = []
873 876 data = []
874 877 dsList = self.dsList
875 878 i = 0
876 879 while i < len(dsList):
877 880 dsInfo = dsList[i]
878 881 #One-dimension data
879 882 if dsInfo['mode'] == 0:
880 883 # ds0 = grp.create_dataset(self.dataList[i], (1,1), maxshape=(1,self.blocksPerFile) , chunks = True, dtype='S20')
881 884 ds0 = grp.create_dataset(dsInfo['variable'], (1,1), maxshape=(1,self.blocksPerFile) , chunks = True, dtype=numpy.float64)
882 885 ds.append(ds0)
883 886 data.append([])
884 887 i += 1
885 888 continue
886 889 # nDimsForDs.append(nDims[i])
887 890
888 891 elif dsInfo['mode'] == 2:
889 892 grp0 = grp.create_group(dsInfo['variable'])
890 893 ds0 = grp0.create_dataset(dsInfo['dsName'], (1,dsInfo['shape']), data = numpy.zeros((1,dsInfo['shape'])) , maxshape=(None,dsInfo['shape']), chunks=True)
891 894 ds.append(ds0)
892 895 data.append([])
893 896 i += 1
894 897 continue
895 898
896 899 elif dsInfo['mode'] == 1:
897 900 grp0 = grp.create_group(dsInfo['variable'])
898 901
899 902 for j in range(dsInfo['dsNumber']):
900 903 dsInfo = dsList[i]
901 904 tableName = dsInfo['dsName']
902 905 shape = int(dsInfo['shape'])
903 906
904 907 if dsInfo['nDim'] == 3:
905 908 ds0 = grp0.create_dataset(tableName, (shape[0],shape[1],1) , data = numpy.zeros((shape[0],shape[1],1)), maxshape = (None,shape[1],None), chunks=True)
906 909 else:
907 910 ds0 = grp0.create_dataset(tableName, (1,shape), data = numpy.zeros((1,shape)) , maxshape=(None,shape), chunks=True)
908 911
909 912 ds.append(ds0)
910 913 data.append([])
911 914 i += 1
912 915 # nDimsForDs.append(nDims[i])
913 916
914 917 fp.flush()
915 918 fp.close()
916 919
917 920 # self.nDatas = nDatas
918 921 # self.nDims = nDims
919 922 # self.nDimsForDs = nDimsForDs
920 923 #Saving variables
921 924 print 'Writing the file: %s'%filename
922 925 self.filename = filename
923 926 # self.fp = fp
924 927 # self.grp = grp
925 928 # self.grp.attrs.modify('nRecords', 1)
926 929 self.ds = ds
927 930 self.data = data
928 931 # self.setFile = setFile
929 932 self.firsttime = True
930 933 self.blockIndex = 0
931 934 return
932 935
933 936 def putData(self):
934 937
935 938 if self.blockIndex == self.blocksPerFile or self.timeFlag():
936 939 self.setNextFile()
937 940
938 941 # if not self.firsttime:
939 942 self.readBlock()
940 943 self.setBlock() #Prepare data to be written
941 944 self.writeBlock() #Write data
942 945
943 946 return
944 947
945 948 def readBlock(self):
946 949
947 950 '''
948 951 data Array configured
949 952
950 953
951 954 self.data
952 955 '''
953 956 dsList = self.dsList
954 957 ds = self.ds
955 958 #Setting HDF5 File
956 959 fp = h5py.File(self.filename,'r+')
957 960 grp = fp["Data"]
958 961 ind = 0
959 962
960 963 # grp.attrs['blocksPerFile'] = 0
961 964 while ind < len(dsList):
962 965 dsInfo = dsList[ind]
963 966
964 967 if dsInfo['mode'] == 0:
965 968 ds0 = grp[dsInfo['variable']]
966 969 ds[ind] = ds0
967 970 ind += 1
968 971 else:
969 972
970 973 grp0 = grp[dsInfo['variable']]
971 974
972 975 for j in range(dsInfo['dsNumber']):
973 976 dsInfo = dsList[ind]
974 977 ds0 = grp0[dsInfo['dsName']]
975 978 ds[ind] = ds0
976 979 ind += 1
977 980
978 981 self.fp = fp
979 982 self.grp = grp
980 983 self.ds = ds
981 984
982 985 return
983 986
984 987 def setBlock(self):
985 988 '''
986 989 data Array configured
987 990
988 991
989 992 self.data
990 993 '''
991 994 #Creating Arrays
992 995 dsList = self.dsList
993 996 data = self.data
994 997 ind = 0
995 998
996 999 while ind < len(dsList):
997 1000 dsInfo = dsList[ind]
998 1001 dataAux = getattr(self.dataOut, dsInfo['variable'])
999 1002
1000 1003 mode = dsInfo['mode']
1001 1004 nDim = dsInfo['nDim']
1002 1005
1003 1006 if mode == 0 or mode == 2 or nDim == 1:
1004 1007 data[ind] = dataAux
1005 1008 ind += 1
1006 1009 # elif nDim == 1:
1007 1010 # data[ind] = numpy.reshape(dataAux,(numpy.size(dataAux),1))
1008 1011 # ind += 1
1009 1012 elif nDim == 2:
1010 1013 for j in range(dsInfo['dsNumber']):
1011 1014 data[ind] = dataAux[j,:]
1012 1015 ind += 1
1013 1016 elif nDim == 3:
1014 1017 for j in range(dsInfo['dsNumber']):
1015 1018 data[ind] = dataAux[:,j,:]
1016 1019 ind += 1
1017 1020
1018 1021 self.data = data
1019 1022 return
1020 1023
1021 1024 def writeBlock(self):
1022 1025 '''
1023 1026 Saves the block in the HDF5 file
1024 1027 '''
1025 1028 dsList = self.dsList
1026 1029
1027 1030 for i in range(len(self.ds)):
1028 1031 dsInfo = dsList[i]
1029 1032 nDim = dsInfo['nDim']
1030 1033 mode = dsInfo['mode']
1031 1034
1032 1035 # First time
1033 1036 if self.firsttime:
1034 1037 # self.ds[i].resize(self.data[i].shape)
1035 1038 # self.ds[i][self.blockIndex,:] = self.data[i]
1036 1039 if type(self.data[i]) == numpy.ndarray:
1037 1040
1038 1041 if nDim == 3:
1039 1042 self.data[i] = self.data[i].reshape((self.data[i].shape[0],self.data[i].shape[1],1))
1040 1043 self.ds[i].resize(self.data[i].shape)
1041 1044 if mode == 2:
1042 1045 self.ds[i].resize(self.data[i].shape)
1043 1046 self.ds[i][:] = self.data[i]
1044 1047 else:
1045 1048
1046 1049 # From second time
1047 1050 # Meteors!
1048 1051 if mode == 2:
1049 1052 dataShape = self.data[i].shape
1050 1053 dsShape = self.ds[i].shape
1051 1054 self.ds[i].resize((self.ds[i].shape[0] + dataShape[0],self.ds[i].shape[1]))
1052 1055 self.ds[i][dsShape[0]:,:] = self.data[i]
1053 1056 # No dimension
1054 1057 elif mode == 0:
1055 1058 self.ds[i].resize((self.ds[i].shape[0], self.ds[i].shape[1] + 1))
1056 1059 self.ds[i][0,-1] = self.data[i]
1057 1060 # One dimension
1058 1061 elif nDim == 1:
1059 1062 self.ds[i].resize((self.ds[i].shape[0] + 1, self.ds[i].shape[1]))
1060 1063 self.ds[i][-1,:] = self.data[i]
1061 1064 # Two dimension
1062 1065 elif nDim == 2:
1063 1066 self.ds[i].resize((self.ds[i].shape[0] + 1,self.ds[i].shape[1]))
1064 1067 self.ds[i][self.blockIndex,:] = self.data[i]
1065 1068 # Three dimensions
1066 1069 elif nDim == 3:
1067 1070 self.ds[i].resize((self.ds[i].shape[0],self.ds[i].shape[1],self.ds[i].shape[2]+1))
1068 1071 self.ds[i][:,:,-1] = self.data[i]
1069 1072
1070 1073 self.firsttime = False
1071 1074 self.blockIndex += 1
1072 1075
1073 1076 #Close to save changes
1074 1077 self.fp.flush()
1075 1078 self.fp.close()
1076 1079 return
1077 1080
1078 1081 def run(self, dataOut, **kwargs):
1079
1082
1080 1083 if not(self.isConfig):
1084
1081 1085 flagdata = self.setup(dataOut, **kwargs)
1082
1086
1083 1087 if not(flagdata):
1084 1088 return
1085 1089
1086 1090 self.isConfig = True
1087 1091 # self.putMetadata()
1088 1092 self.setNextFile()
1089 1093
1090 1094 self.putData()
1091 1095 return
@@ -1,4027 +1,3809
1 1 import numpy
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
14 14
15 15
16 16 import time
17 17 #from sklearn.cluster import KMeans
18 18
19 19 import matplotlib.pyplot as plt
20 20
21 21 from scipy.optimize import fmin_l_bfgs_b #optimize with bounds on state papameters
22 22 from jroproc_base import ProcessingUnit, Operation
23 23 from schainpy.model.data.jrodata import Parameters, hildebrand_sekhon
24 24 from scipy import asarray as ar,exp
25 25 from scipy.optimize import curve_fit
26 26
27 27 import warnings
28 28 from numpy import NaN
29 29 from scipy.optimize.optimize import OptimizeWarning
30 30 warnings.filterwarnings('ignore')
31 31
32 32
33 33 SPEED_OF_LIGHT = 299792458
34 34
35 35
36 36 '''solving pickling issue'''
37 37
38 38 def _pickle_method(method):
39 39 func_name = method.im_func.__name__
40 40 obj = method.im_self
41 41 cls = method.im_class
42 42 return _unpickle_method, (func_name, obj, cls)
43 43
44 44 def _unpickle_method(func_name, obj, cls):
45 45 for cls in cls.mro():
46 46 try:
47 47 func = cls.__dict__[func_name]
48 48 except KeyError:
49 49 pass
50 50 else:
51 51 break
52 52 return func.__get__(obj, cls)
53 53
54 54 class ParametersProc(ProcessingUnit):
55 55
56 56 nSeconds = None
57 57
58 58 def __init__(self):
59 59 ProcessingUnit.__init__(self)
60 60
61 61 # self.objectDict = {}
62 62 self.buffer = None
63 63 self.firstdatatime = None
64 64 self.profIndex = 0
65 65 self.dataOut = Parameters()
66 66
67 67 def __updateObjFromInput(self):
68 68
69 69 self.dataOut.inputUnit = self.dataIn.type
70 70
71 71 self.dataOut.timeZone = self.dataIn.timeZone
72 72 self.dataOut.dstFlag = self.dataIn.dstFlag
73 73 self.dataOut.errorCount = self.dataIn.errorCount
74 74 self.dataOut.useLocalTime = self.dataIn.useLocalTime
75 75
76 76 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
77 77 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
78 78 self.dataOut.channelList = self.dataIn.channelList
79 79 self.dataOut.heightList = self.dataIn.heightList
80 80 self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')])
81 81 # self.dataOut.nHeights = self.dataIn.nHeights
82 82 # self.dataOut.nChannels = self.dataIn.nChannels
83 83 self.dataOut.nBaud = self.dataIn.nBaud
84 84 self.dataOut.nCode = self.dataIn.nCode
85 85 self.dataOut.code = self.dataIn.code
86 86 # self.dataOut.nProfiles = self.dataOut.nFFTPoints
87 87 self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock
88 88 # self.dataOut.utctime = self.firstdatatime
89 89 self.dataOut.utctime = self.dataIn.utctime
90 90 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada
91 91 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip
92 92 self.dataOut.nCohInt = self.dataIn.nCohInt
93 93 # self.dataOut.nIncohInt = 1
94 94 self.dataOut.ippSeconds = self.dataIn.ippSeconds
95 95 # self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
96 96 self.dataOut.timeInterval1 = self.dataIn.timeInterval
97 97 self.dataOut.heightList = self.dataIn.getHeiRange()
98 98 self.dataOut.frequency = self.dataIn.frequency
99 99 self.dataOut.noise = self.dataIn.noise
100 100
101 101
102 102
103 103 def run(self):
104 104
105 105 #---------------------- Voltage Data ---------------------------
106 106
107 107 if self.dataIn.type == "Voltage":
108 108
109 109 self.__updateObjFromInput()
110 110 self.dataOut.data_pre = self.dataIn.data.copy()
111 111 self.dataOut.flagNoData = False
112 112 self.dataOut.utctimeInit = self.dataIn.utctime
113 113 self.dataOut.paramInterval = self.dataIn.nProfiles*self.dataIn.nCohInt*self.dataIn.ippSeconds
114 114 return
115 115
116 116 #---------------------- Spectra Data ---------------------------
117 117
118 118 if self.dataIn.type == "Spectra":
119 119
120 120 self.dataOut.data_pre = (self.dataIn.data_spc , self.dataIn.data_cspc)
121 121 print 'self.dataIn.data_spc', self.dataIn.data_spc.shape
122 122 self.dataOut.abscissaList = self.dataIn.getVelRange(1)
123 123 self.dataOut.spc_noise = self.dataIn.getNoise()
124 124 self.dataOut.spc_range = numpy.asanyarray((self.dataIn.getFreqRange(1) , self.dataIn.getAcfRange(1) , self.dataIn.getVelRange(1) ))
125 125
126 126 self.dataOut.normFactor = self.dataIn.normFactor
127 127 #self.dataOut.outputInterval = self.dataIn.outputInterval
128 128 self.dataOut.groupList = self.dataIn.pairsList
129 129 self.dataOut.flagNoData = False
130 130 #print 'datain chandist ',self.dataIn.ChanDist
131 131 if hasattr(self.dataIn, 'ChanDist'): #Distances of receiver channels
132 132 self.dataOut.ChanDist = self.dataIn.ChanDist
133 133 else: self.dataOut.ChanDist = None
134 134
135 135 print 'datain chandist ',self.dataOut.ChanDist
136 136
137 137 #if hasattr(self.dataIn, 'VelRange'): #Velocities range
138 138 # self.dataOut.VelRange = self.dataIn.VelRange
139 139 #else: self.dataOut.VelRange = None
140 140
141 141 if hasattr(self.dataIn, 'RadarConst'): #Radar Constant
142 142 self.dataOut.RadarConst = self.dataIn.RadarConst
143 143
144 144 if hasattr(self.dataIn, 'NPW'): #NPW
145 145 self.dataOut.NPW = self.dataIn.NPW
146 146
147 147 if hasattr(self.dataIn, 'COFA'): #COFA
148 148 self.dataOut.COFA = self.dataIn.COFA
149 149
150 150
151 151
152 152 #---------------------- Correlation Data ---------------------------
153 153
154 154 if self.dataIn.type == "Correlation":
155 155 acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf = self.dataIn.splitFunctions()
156 156
157 157 self.dataOut.data_pre = (self.dataIn.data_cf[acf_ind,:], self.dataIn.data_cf[ccf_ind,:,:])
158 158 self.dataOut.normFactor = (self.dataIn.normFactor[acf_ind,:], self.dataIn.normFactor[ccf_ind,:])
159 159 self.dataOut.groupList = (acf_pairs, ccf_pairs)
160 160
161 161 self.dataOut.abscissaList = self.dataIn.lagRange
162 162 self.dataOut.noise = self.dataIn.noise
163 163 self.dataOut.data_SNR = self.dataIn.SNR
164 164 self.dataOut.flagNoData = False
165 165 self.dataOut.nAvg = self.dataIn.nAvg
166 166
167 167 #---------------------- Parameters Data ---------------------------
168 168
169 169 if self.dataIn.type == "Parameters":
170 170 self.dataOut.copy(self.dataIn)
171 171 self.dataOut.flagNoData = False
172 172
173 173 return True
174 174
175 175 self.__updateObjFromInput()
176 176 self.dataOut.utctimeInit = self.dataIn.utctime
177 177 self.dataOut.paramInterval = self.dataIn.timeInterval
178 178
179 179 return
180 180
181 181
182 182 def target(tups):
183 183
184 184 obj, args = tups
185 #print 'TARGETTT', obj, args
185
186 186 return obj.FitGau(args)
187 187
188 188
189 189 class SpectralFilters(Operation):
190 190
191 191 '''This class allows the Rainfall / Wind Selection for CLAIRE RADAR
192 192
193 193 LimitR : It is the limit in m/s of Rainfall
194 194 LimitW : It is the limit in m/s for Winds
195 195
196 196 Input:
197 197
198 198 self.dataOut.data_pre : SPC and CSPC
199 199 self.dataOut.spc_range : To select wind and rainfall velocities
200 200
201 201 Affected:
202 202
203 203 self.dataOut.data_pre : It is used for the new SPC and CSPC ranges of wind
204 204 self.dataOut.spcparam_range : Used in SpcParamPlot
205 205 self.dataOut.SPCparam : Used in PrecipitationProc
206 206
207 207
208 208 '''
209 209
210 210 def __init__(self, **kwargs):
211 211 Operation.__init__(self, **kwargs)
212 212 self.i=0
213 213
214 214 def run(self, dataOut, PositiveLimit=1.5, NegativeLimit=2.5):
215 215
216 216
217 217 #Limite de vientos
218 218 LimitR = PositiveLimit
219 219 LimitN = NegativeLimit
220 220
221 221 self.spc = dataOut.data_pre[0].copy()
222 222 self.cspc = dataOut.data_pre[1].copy()
223 223
224 224 self.Num_Hei = self.spc.shape[2]
225 225 self.Num_Bin = self.spc.shape[1]
226 226 self.Num_Chn = self.spc.shape[0]
227 227
228 228 VelRange = dataOut.spc_range[2]
229 229 TimeRange = dataOut.spc_range[1]
230 230 FrecRange = dataOut.spc_range[0]
231 231
232 232 Vmax= 2*numpy.max(dataOut.spc_range[2])
233 233 Tmax= 2*numpy.max(dataOut.spc_range[1])
234 234 Fmax= 2*numpy.max(dataOut.spc_range[0])
235 235
236 236 Breaker1R=VelRange[numpy.abs(VelRange-(-LimitN)).argmin()]
237 237 Breaker1R=numpy.where(VelRange == Breaker1R)
238 238
239 239 Delta = self.Num_Bin/2 - Breaker1R[0]
240 240
241 #Breaker1W=VelRange[numpy.abs(VelRange-(-LimitW)).argmin()]
242 #Breaker1W=numpy.where(VelRange == Breaker1W)
243
244 #Breaker2W=VelRange[numpy.abs(VelRange-(LimitW)).argmin()]
245 #Breaker2W=numpy.where(VelRange == Breaker2W)
246
247 241
248 242 '''Reacomodando SPCrange'''
249 243
250 244 VelRange=numpy.roll(VelRange,-(self.Num_Bin/2) ,axis=0)
251 245
252 246 VelRange[-(self.Num_Bin/2):]+= Vmax
253 247
254 248 FrecRange=numpy.roll(FrecRange,-(self.Num_Bin/2),axis=0)
255 249
256 250 FrecRange[-(self.Num_Bin/2):]+= Fmax
257 251
258 252 TimeRange=numpy.roll(TimeRange,-(self.Num_Bin/2),axis=0)
259 253
260 254 TimeRange[-(self.Num_Bin/2):]+= Tmax
261 255
262 256 ''' ------------------ '''
263 257
264 258 Breaker2R=VelRange[numpy.abs(VelRange-(LimitR)).argmin()]
265 259 Breaker2R=numpy.where(VelRange == Breaker2R)
266 260
267 261
268 262 SPCroll = numpy.roll(self.spc,-(self.Num_Bin/2) ,axis=1)
269 263
270 264 SPCcut = SPCroll.copy()
271 265 for i in range(self.Num_Chn):
272 266
273 267 SPCcut[i,0:int(Breaker2R[0]),:] = dataOut.noise[i]
274 268 SPCcut[i,-int(Delta):,:] = dataOut.noise[i]
275 269
276 270 SPCcut[i]=SPCcut[i]- dataOut.noise[i]
277 271 SPCcut[ numpy.where( SPCcut<0 ) ] = 1e-20
278 272
279 273 SPCroll[i]=SPCroll[i]-dataOut.noise[i]
280 274 SPCroll[ numpy.where( SPCroll<0 ) ] = 1e-20
281 275
282 #self.spc[i, 0:int(Breaker1W[0]) ,:] = dataOut.noise[i]
283 #self.spc[i, int(Breaker2W[0]):self.Num_Bin ,:] = dataOut.noise[i]
284
285 #self.cspc[i, 0:int(Breaker1W[0]) ,:] = dataOut.noise[i]
286 #self.cspc[i, int(Breaker2W[0]):self.Num_Bin ,:] = dataOut.noise[i]
287
288
289
290
291
292 276 SPC_ch1 = SPCroll
293 277
294 278 SPC_ch2 = SPCcut
295 279
296 280 SPCparam = (SPC_ch1, SPC_ch2, self.spc)
297 281 dataOut.SPCparam = numpy.asarray(SPCparam)
298 282
299 #dataOut.data_pre= (self.spc , self.cspc)
300
301 #dataOut.data_preParam = (self.spc , self.cspc)
302 283
303 284 dataOut.spcparam_range=numpy.zeros([self.Num_Chn,self.Num_Bin+1])
304 285
305 286 dataOut.spcparam_range[2]=VelRange
306 287 dataOut.spcparam_range[1]=TimeRange
307 288 dataOut.spcparam_range[0]=FrecRange
308 289
309
310
311 290
312 291 class GaussianFit(Operation):
313 292
314 293 '''
315 294 Function that fit of one and two generalized gaussians (gg) based
316 295 on the PSD shape across an "power band" identified from a cumsum of
317 296 the measured spectrum - noise.
318 297
319 298 Input:
320 299 self.dataOut.data_pre : SelfSpectra
321 300
322 301 Output:
323 302 self.dataOut.SPCparam : SPC_ch1, SPC_ch2
324 303
325 304 '''
326 305 def __init__(self, **kwargs):
327 306 Operation.__init__(self, **kwargs)
328 307 self.i=0
329 308
330 309
331 310 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
332 311 """This routine will find a couple of generalized Gaussians to a power spectrum
333 312 input: spc
334 313 output:
335 314 Amplitude0,shift0,width0,p0,Amplitude1,shift1,width1,p1,noise
336 315 """
337 316
338 317 self.spc = dataOut.data_pre[0].copy()
339 318
340 319
341 print 'SelfSpectra Shape', numpy.asarray(self.spc).shape
342
343
344 #plt.figure(50)
345 #plt.subplot(121)
346 #plt.plot(self.spc,'k',label='spc(66)')
347 #plt.plot(xFrec,ySamples[1],'g',label='Ch1')
348 #plt.plot(xFrec,ySamples[2],'r',label='Ch2')
349 #plt.plot(xFrec,FitGauss,'yo:',label='fit')
350 #plt.legend()
351 #plt.title('DATOS A ALTURA DE 7500 METROS')
352 #plt.show()
353
354 320 self.Num_Hei = self.spc.shape[2]
355 #self.Num_Bin = len(self.spc)
356 321 self.Num_Bin = self.spc.shape[1]
357 322 self.Num_Chn = self.spc.shape[0]
358 323 Vrange = dataOut.abscissaList
359 324
360 325 GauSPC = numpy.empty([self.Num_Chn,self.Num_Bin,self.Num_Hei])
361 326 SPC_ch1 = numpy.empty([self.Num_Bin,self.Num_Hei])
362 327 SPC_ch2 = numpy.empty([self.Num_Bin,self.Num_Hei])
363 328 SPC_ch1[:] = numpy.NaN
364 329 SPC_ch2[:] = numpy.NaN
365 330
366 331
367 332 start_time = time.time()
368 333
369 334 noise_ = dataOut.spc_noise[0].copy()
370 335
371 336
372 337 pool = Pool(processes=self.Num_Chn)
373 338 args = [(Vrange, Ch, pnoise, noise_, num_intg, SNRlimit) for Ch in range(self.Num_Chn)]
374 339 objs = [self for __ in range(self.Num_Chn)]
375 340 attrs = zip(objs, args)
376 341 gauSPC = pool.map(target, attrs)
377 342 dataOut.SPCparam = numpy.asarray(SPCparam)
378 343
379
380
381 print '========================================================'
382 print 'total_time: ', time.time()-start_time
383
384 344 # re-normalizing spc and noise
385 345 # This part differs from gg1
386
387
388 346
389 347 ''' Parameters:
390 348 1. Amplitude
391 349 2. Shift
392 350 3. Width
393 351 4. Power
394 352 '''
395 353
396 354
397 355 ###############################################################################
398 356 def FitGau(self, X):
399 357
400 358 Vrange, ch, pnoise, noise_, num_intg, SNRlimit = X
401 #print 'VARSSSS', ch, pnoise, noise, num_intg
402
403 #print 'HEIGHTS', self.Num_Hei
404
359
405 360 SPCparam = []
406 361 SPC_ch1 = numpy.empty([self.Num_Bin,self.Num_Hei])
407 362 SPC_ch2 = numpy.empty([self.Num_Bin,self.Num_Hei])
408 363 SPC_ch1[:] = 0#numpy.NaN
409 364 SPC_ch2[:] = 0#numpy.NaN
410 365
411 366
412 367
413 368 for ht in range(self.Num_Hei):
414 #print (numpy.asarray(self.spc).shape)
415
416 #print 'TTTTT', ch , ht
417 #print self.spc.shape
418 369
419 370
420 371 spc = numpy.asarray(self.spc)[ch,:,ht]
421 372
422 373 #############################################
423 374 # normalizing spc and noise
424 375 # This part differs from gg1
425 376 spc_norm_max = max(spc)
426 377 #spc = spc / spc_norm_max
427 378 pnoise = pnoise #/ spc_norm_max
428 379 #############################################
429 380
430 381 fatspectra=1.0
431 382
432 383 wnoise = noise_ #/ spc_norm_max
433 384 #wnoise,stdv,i_max,index =enoise(spc,num_intg) #noise estimate using Hildebrand Sekhon, only wnoise is used
434 385 #if wnoise>1.1*pnoise: # to be tested later
435 386 # wnoise=pnoise
436 387 noisebl=wnoise*0.9;
437 388 noisebh=wnoise*1.1
438 389 spc=spc-wnoise
439 # print 'wnoise', noise_[0], spc_norm_max, wnoise
390
440 391 minx=numpy.argmin(spc)
441 392 #spcs=spc.copy()
442 393 spcs=numpy.roll(spc,-minx)
443 394 cum=numpy.cumsum(spcs)
444 395 tot_noise=wnoise * self.Num_Bin #64;
445 #print 'spc' , spcs[5:8] , 'tot_noise', tot_noise
446 #tot_signal=sum(cum[-5:])/5.; ''' How does this line work? '''
447 #snr=tot_signal/tot_noise
448 #snr=cum[-1]/tot_noise
396
449 397 snr = sum(spcs)/tot_noise
450 398 snrdB=10.*numpy.log10(snr)
451 399
452 400 if snrdB < SNRlimit :
453 401 snr = numpy.NaN
454 402 SPC_ch1[:,ht] = 0#numpy.NaN
455 403 SPC_ch1[:,ht] = 0#numpy.NaN
456 404 SPCparam = (SPC_ch1,SPC_ch2)
457 405 continue
458 #print 'snr',snrdB #, sum(spcs) , tot_noise
459
460 406
461 407
462 408 #if snrdB<-18 or numpy.isnan(snrdB) or num_intg<4:
463 409 # return [None,]*4,[None,]*4,None,snrdB,None,None,[None,]*5,[None,]*9,None
464 410
465 411 cummax=max(cum);
466 412 epsi=0.08*fatspectra # cumsum to narrow down the energy region
467 413 cumlo=cummax*epsi;
468 414 cumhi=cummax*(1-epsi)
469 415 powerindex=numpy.array(numpy.where(numpy.logical_and(cum>cumlo, cum<cumhi))[0])
470 416
471 417
472 418 if len(powerindex) < 1:# case for powerindex 0
473 419 continue
474 420 powerlo=powerindex[0]
475 421 powerhi=powerindex[-1]
476 422 powerwidth=powerhi-powerlo
477 423
478 424 firstpeak=powerlo+powerwidth/10.# first gaussian energy location
479 425 secondpeak=powerhi-powerwidth/10.#second gaussian energy location
480 426 midpeak=(firstpeak+secondpeak)/2.
481 427 firstamp=spcs[int(firstpeak)]
482 428 secondamp=spcs[int(secondpeak)]
483 429 midamp=spcs[int(midpeak)]
484 430
485 431 x=numpy.arange( self.Num_Bin )
486 432 y_data=spc+wnoise
487 433
488 434 ''' single Gaussian '''
489 435 shift0=numpy.mod(midpeak+minx, self.Num_Bin )
490 436 width0=powerwidth/4.#Initialization entire power of spectrum divided by 4
491 437 power0=2.
492 438 amplitude0=midamp
493 439 state0=[shift0,width0,amplitude0,power0,wnoise]
494 440 bnds=(( 0,(self.Num_Bin-1) ),(1,powerwidth),(0,None),(0.5,3.),(noisebl,noisebh))
495 441 lsq1=fmin_l_bfgs_b(self.misfit1,state0,args=(y_data,x,num_intg),bounds=bnds,approx_grad=True)
496 442
497 443 chiSq1=lsq1[1];
498 444
499 445
500 446 if fatspectra<1.0 and powerwidth<4:
501 447 choice=0
502 448 Amplitude0=lsq1[0][2]
503 449 shift0=lsq1[0][0]
504 450 width0=lsq1[0][1]
505 451 p0=lsq1[0][3]
506 452 Amplitude1=0.
507 453 shift1=0.
508 454 width1=0.
509 455 p1=0.
510 456 noise=lsq1[0][4]
511 457 #return (numpy.array([shift0,width0,Amplitude0,p0]),
512 458 # numpy.array([shift1,width1,Amplitude1,p1]),noise,snrdB,chiSq1,6.,sigmas1,[None,]*9,choice)
513 459
514 460 ''' two gaussians '''
515 461 #shift0=numpy.mod(firstpeak+minx,64); shift1=numpy.mod(secondpeak+minx,64)
516 462 shift0=numpy.mod(firstpeak+minx, self.Num_Bin );
517 463 shift1=numpy.mod(secondpeak+minx, self.Num_Bin )
518 464 width0=powerwidth/6.;
519 465 width1=width0
520 466 power0=2.;
521 467 power1=power0
522 468 amplitude0=firstamp;
523 469 amplitude1=secondamp
524 470 state0=[shift0,width0,amplitude0,power0,shift1,width1,amplitude1,power1,wnoise]
525 471 #bnds=((0,63),(1,powerwidth/2.),(0,None),(0.5,3.),(0,63),(1,powerwidth/2.),(0,None),(0.5,3.),(noisebl,noisebh))
526 472 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))
527 473 #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))
528 474
529 475 lsq2 = fmin_l_bfgs_b( self.misfit2 , state0 , args=(y_data,x,num_intg) , bounds=bnds , approx_grad=True )
530 476
531 477
532 478 chiSq2=lsq2[1];
533 479
534 480
535 481
536 482 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)
537 483
538 484 if snrdB>-12: # when SNR is strong pick the peak with least shift (LOS velocity) error
539 485 if oneG:
540 486 choice=0
541 487 else:
542 488 w1=lsq2[0][1]; w2=lsq2[0][5]
543 489 a1=lsq2[0][2]; a2=lsq2[0][6]
544 490 p1=lsq2[0][3]; p2=lsq2[0][7]
545 491 s1=(2**(1+1./p1))*scipy.special.gamma(1./p1)/p1;
546 492 s2=(2**(1+1./p2))*scipy.special.gamma(1./p2)/p2;
547 493 gp1=a1*w1*s1; gp2=a2*w2*s2 # power content of each ggaussian with proper p scaling
548 494
549 495 if gp1>gp2:
550 496 if a1>0.7*a2:
551 497 choice=1
552 498 else:
553 499 choice=2
554 500 elif gp2>gp1:
555 501 if a2>0.7*a1:
556 502 choice=2
557 503 else:
558 504 choice=1
559 505 else:
560 506 choice=numpy.argmax([a1,a2])+1
561 507 #else:
562 508 #choice=argmin([std2a,std2b])+1
563 509
564 510 else: # with low SNR go to the most energetic peak
565 511 choice=numpy.argmax([lsq1[0][2]*lsq1[0][1],lsq2[0][2]*lsq2[0][1],lsq2[0][6]*lsq2[0][5]])
566 512
567 513
568 514 shift0=lsq2[0][0];
569 515 vel0=Vrange[0] + shift0*(Vrange[1]-Vrange[0])
570 516 shift1=lsq2[0][4];
571 517 vel1=Vrange[0] + shift1*(Vrange[1]-Vrange[0])
572 518
573 519 max_vel = 1.0
574 520
575 521 #first peak will be 0, second peak will be 1
576 522 if vel0 > -1.0 and vel0 < max_vel : #first peak is in the correct range
577 523 shift0=lsq2[0][0]
578 524 width0=lsq2[0][1]
579 525 Amplitude0=lsq2[0][2]
580 526 p0=lsq2[0][3]
581 527
582 528 shift1=lsq2[0][4]
583 529 width1=lsq2[0][5]
584 530 Amplitude1=lsq2[0][6]
585 531 p1=lsq2[0][7]
586 532 noise=lsq2[0][8]
587 533 else:
588 534 shift1=lsq2[0][0]
589 535 width1=lsq2[0][1]
590 536 Amplitude1=lsq2[0][2]
591 537 p1=lsq2[0][3]
592 538
593 539 shift0=lsq2[0][4]
594 540 width0=lsq2[0][5]
595 541 Amplitude0=lsq2[0][6]
596 542 p0=lsq2[0][7]
597 543 noise=lsq2[0][8]
598 544
599 545 if Amplitude0<0.05: # in case the peak is noise
600 546 shift0,width0,Amplitude0,p0 = [0,0,0,0]#4*[numpy.NaN]
601 547 if Amplitude1<0.05:
602 548 shift1,width1,Amplitude1,p1 = [0,0,0,0]#4*[numpy.NaN]
603 549
604 550
605 # if choice==0: # pick the single gaussian fit
606 # Amplitude0=lsq1[0][2]
607 # shift0=lsq1[0][0]
608 # width0=lsq1[0][1]
609 # p0=lsq1[0][3]
610 # Amplitude1=0.
611 # shift1=0.
612 # width1=0.
613 # p1=0.
614 # noise=lsq1[0][4]
615 # elif choice==1: # take the first one of the 2 gaussians fitted
616 # Amplitude0 = lsq2[0][2]
617 # shift0 = lsq2[0][0]
618 # width0 = lsq2[0][1]
619 # p0 = lsq2[0][3]
620 # Amplitude1 = lsq2[0][6] # This is 0 in gg1
621 # shift1 = lsq2[0][4] # This is 0 in gg1
622 # width1 = lsq2[0][5] # This is 0 in gg1
623 # p1 = lsq2[0][7] # This is 0 in gg1
624 # noise = lsq2[0][8]
625 # else: # the second one
626 # Amplitude0 = lsq2[0][6]
627 # shift0 = lsq2[0][4]
628 # width0 = lsq2[0][5]
629 # p0 = lsq2[0][7]
630 # Amplitude1 = lsq2[0][2] # This is 0 in gg1
631 # shift1 = lsq2[0][0] # This is 0 in gg1
632 # width1 = lsq2[0][1] # This is 0 in gg1
633 # p1 = lsq2[0][3] # This is 0 in gg1
634 # noise = lsq2[0][8]
635
636 #print len(noise + Amplitude0*numpy.exp(-0.5*(abs(x-shift0))/width0)**p0)
637 551 SPC_ch1[:,ht] = noise + Amplitude0*numpy.exp(-0.5*(abs(x-shift0))/width0)**p0
638 552 SPC_ch2[:,ht] = noise + Amplitude1*numpy.exp(-0.5*(abs(x-shift1))/width1)**p1
639 #print 'SPC_ch1.shape',SPC_ch1.shape
640 #print 'SPC_ch2.shape',SPC_ch2.shape
641 #dataOut.data_param = SPC_ch1
642 553 SPCparam = (SPC_ch1,SPC_ch2)
643 #GauSPC[1] = SPC_ch2
644
645 # print 'shift0', shift0
646 # print 'Amplitude0', Amplitude0
647 # print 'width0', width0
648 # print 'p0', p0
649 # print '========================'
650 # print 'shift1', shift1
651 # print 'Amplitude1', Amplitude1
652 # print 'width1', width1
653 # print 'p1', p1
654 # print 'noise', noise
655 # print 's_noise', wnoise
554
656 555
657 556 return GauSPC
658 557
659 558 def y_model1(self,x,state):
660 559 shift0,width0,amplitude0,power0,noise=state
661 560 model0=amplitude0*numpy.exp(-0.5*abs((x-shift0)/width0)**power0)
662 561
663 562 model0u=amplitude0*numpy.exp(-0.5*abs((x-shift0- self.Num_Bin )/width0)**power0)
664 563
665 564 model0d=amplitude0*numpy.exp(-0.5*abs((x-shift0+ self.Num_Bin )/width0)**power0)
666 565 return model0+model0u+model0d+noise
667 566
668 567 def y_model2(self,x,state): #Equation for two generalized Gaussians with Nyquist
669 568 shift0,width0,amplitude0,power0,shift1,width1,amplitude1,power1,noise=state
670 569 model0=amplitude0*numpy.exp(-0.5*abs((x-shift0)/width0)**power0)
671 570
672 571 model0u=amplitude0*numpy.exp(-0.5*abs((x-shift0- self.Num_Bin )/width0)**power0)
673 572
674 573 model0d=amplitude0*numpy.exp(-0.5*abs((x-shift0+ self.Num_Bin )/width0)**power0)
675 574 model1=amplitude1*numpy.exp(-0.5*abs((x-shift1)/width1)**power1)
676 575
677 576 model1u=amplitude1*numpy.exp(-0.5*abs((x-shift1- self.Num_Bin )/width1)**power1)
678 577
679 578 model1d=amplitude1*numpy.exp(-0.5*abs((x-shift1+ self.Num_Bin )/width1)**power1)
680 579 return model0+model0u+model0d+model1+model1u+model1d+noise
681 580
682 581 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.
683 582
684 583 return num_intg*sum((numpy.log(y_data)-numpy.log(self.y_model1(x,state)))**2)#/(64-5.) # /(64-5.) can be commented
685 584
686 585 def misfit2(self,state,y_data,x,num_intg):
687 586 return num_intg*sum((numpy.log(y_data)-numpy.log(self.y_model2(x,state)))**2)#/(64-9.)
688 587
689
588
690 589
691 590 class PrecipitationProc(Operation):
692 591
693 592 '''
694 593 Operator that estimates Reflectivity factor (Z), and estimates rainfall Rate (R)
695 594
696 595 Input:
697 596 self.dataOut.data_pre : SelfSpectra
698 597
699 598 Output:
700 599
701 600 self.dataOut.data_output : Reflectivity factor, rainfall Rate
702 601
703 602
704 603 Parameters affected:
705 604 '''
605
606 def __init__(self, **kwargs):
607 Operation.__init__(self, **kwargs)
608 self.i=0
609
610
706 611 def gaus(self,xSamples,Amp,Mu,Sigma):
707 612 return ( Amp / ((2*numpy.pi)**0.5 * Sigma) ) * numpy.exp( -( xSamples - Mu )**2 / ( 2 * (Sigma**2) ))
708 613
709 614
710 615
711 616 def Moments(self, ySamples, xSamples):
712 617 Pot = numpy.nansum( ySamples ) # Potencia, momento 0
713 618 yNorm = ySamples / Pot
714 619
715 620 Vr = numpy.nansum( yNorm * xSamples ) # Velocidad radial, mu, corrimiento doppler, primer momento
716 621 Sigma2 = abs(numpy.nansum( yNorm * ( xSamples - Vr )**2 )) # Segundo Momento
717 622 Desv = Sigma2**0.5 # Desv. Estandar, Ancho espectral
718 623
719 624 return numpy.array([Pot, Vr, Desv])
720 625
721 626 def run(self, dataOut, radar=None, Pt=5000, Gt=295.1209, Gr=70.7945, Lambda=0.6741, aL=2.5118,
722 627 tauW=4e-06, ThetaT=0.1656317, ThetaR=0.36774087, Km = 0.93, Altitude=3350):
723 628
724 629
725 630 Velrange = dataOut.spcparam_range[2]
726 631 FrecRange = dataOut.spcparam_range[0]
727 632
728 633 dV= Velrange[1]-Velrange[0]
729 634 dF= FrecRange[1]-FrecRange[0]
730 635
731 636 if radar == "MIRA35C" :
732 637
733 638 self.spc = dataOut.data_pre[0].copy()
734 639 self.Num_Hei = self.spc.shape[2]
735 640 self.Num_Bin = self.spc.shape[1]
736 641 self.Num_Chn = self.spc.shape[0]
737 642 Ze = self.dBZeMODE2(dataOut)
738 643
739 644 else:
740 645
741 646 self.spc = dataOut.SPCparam[1].copy() #dataOut.data_pre[0].copy() #
647
648 """NOTA SE DEBE REMOVER EL RANGO DEL PULSO TX"""
649
650 self.spc[:,:,0:7]= numpy.NaN
651
652 """##########################################"""
653
742 654 self.Num_Hei = self.spc.shape[2]
743 655 self.Num_Bin = self.spc.shape[1]
744 656 self.Num_Chn = self.spc.shape[0]
745 print '==================== SPC SHAPE',numpy.shape(self.spc)
746
747 657
748 658 ''' Se obtiene la constante del RADAR '''
749 659
750 660 self.Pt = Pt
751 661 self.Gt = Gt
752 662 self.Gr = Gr
753 663 self.Lambda = Lambda
754 664 self.aL = aL
755 665 self.tauW = tauW
756 666 self.ThetaT = ThetaT
757 667 self.ThetaR = ThetaR
758 668
759 669 Numerator = ( (4*numpy.pi)**3 * aL**2 * 16 * numpy.log(2) )
760 670 Denominator = ( Pt * Gt * Gr * Lambda**2 * SPEED_OF_LIGHT * tauW * numpy.pi * ThetaT * ThetaR)
761 RadarConstant = 5e-27 * Numerator / Denominator #
762 print '***'
763 print '*** RadarConstant' , RadarConstant, '****'
764 print '***'
671 RadarConstant = 5e-26 * Numerator / Denominator #
672
765 673 ''' ============================= '''
766 674
767 675 self.spc[0] = (self.spc[0]-dataOut.noise[0])
768 676 self.spc[1] = (self.spc[1]-dataOut.noise[1])
769 677 self.spc[2] = (self.spc[2]-dataOut.noise[2])
770 678
771 679 self.spc[ numpy.where(self.spc < 0)] = 0
772 680
773 681 SPCmean = (numpy.mean(self.spc,0) - numpy.mean(dataOut.noise))
774 682 SPCmean[ numpy.where(SPCmean < 0)] = 0
775 683
776 684 ETAn = numpy.zeros([self.Num_Bin,self.Num_Hei])
777 685 ETAv = numpy.zeros([self.Num_Bin,self.Num_Hei])
778 686 ETAd = numpy.zeros([self.Num_Bin,self.Num_Hei])
779 687
780 688 Pr = SPCmean[:,:]
781 689
782 690 VelMeteoro = numpy.mean(SPCmean,axis=0)
783 691
784 #print '==================== Vel SHAPE',VelMeteoro
785
786 692 D_range = numpy.zeros([self.Num_Bin,self.Num_Hei])
787 693 SIGMA = numpy.zeros([self.Num_Bin,self.Num_Hei])
788 694 N_dist = numpy.zeros([self.Num_Bin,self.Num_Hei])
789 695 V_mean = numpy.zeros(self.Num_Hei)
790 696 del_V = numpy.zeros(self.Num_Hei)
791 697 Z = numpy.zeros(self.Num_Hei)
792 698 Ze = numpy.zeros(self.Num_Hei)
793 699 RR = numpy.zeros(self.Num_Hei)
794 700
795 701 Range = dataOut.heightList*1000.
796 702
797 703 for R in range(self.Num_Hei):
798 704
799 705 h = Range[R] + Altitude #Range from ground to radar pulse altitude
800 706 del_V[R] = 1 + 3.68 * 10**-5 * h + 1.71 * 10**-9 * h**2 #Density change correction for velocity
801 707
802 708 D_range[:,R] = numpy.log( (9.65 - (Velrange[0:self.Num_Bin] / del_V[R])) / 10.3 ) / -0.6 #Diameter range [m]x10**-3
803 709
804 710 '''NOTA: ETA(n) dn = ETA(f) df
805 711
806 712 dn = 1 Diferencial de muestreo
807 713 df = ETA(n) / ETA(f)
808 714
809 715 '''
810 716
811 717 ETAn[:,R] = RadarConstant * Pr[:,R] * (Range[R] )**2 #Reflectivity (ETA)
812 718
813 719 ETAv[:,R]=ETAn[:,R]/dV
814 720
815 721 ETAd[:,R]=ETAv[:,R]*6.18*exp(-0.6*D_range[:,R])
816 722
817 723 SIGMA[:,R] = Km * (D_range[:,R] * 1e-3 )**6 * numpy.pi**5 / Lambda**4 #Equivalent Section of drops (sigma)
818 724
819 725 N_dist[:,R] = ETAn[:,R] / SIGMA[:,R]
820 726
821 727 DMoments = self.Moments(Pr[:,R], Velrange[0:self.Num_Bin])
822 728
823 729 try:
824 730 popt01,pcov = curve_fit(self.gaus, Velrange[0:self.Num_Bin] , Pr[:,R] , p0=DMoments)
825 731 except:
826 732 popt01=numpy.zeros(3)
827 733 popt01[1]= DMoments[1]
828 734
829 735 if popt01[1]<0 or popt01[1]>20:
830 736 popt01[1]=numpy.NaN
831 737
832 738
833 739 V_mean[R]=popt01[1]
834 740
835 741 Z[R] = numpy.nansum( N_dist[:,R] * (D_range[:,R])**6 )#*10**-18
836 742
837 743 RR[R] = 0.0006*numpy.pi * numpy.nansum( D_range[:,R]**3 * N_dist[:,R] * Velrange[0:self.Num_Bin] ) #Rainfall rate
838 744
839 745 Ze[R] = (numpy.nansum( ETAn[:,R]) * Lambda**4) / ( 10**-18*numpy.pi**5 * Km)
840 746
841 747
842 748
843 749 RR2 = (Z/200)**(1/1.6)
844 750 dBRR = 10*numpy.log10(RR)
845 751 dBRR2 = 10*numpy.log10(RR2)
846 752
847 753 dBZe = 10*numpy.log10(Ze)
848 754 dBZ = 10*numpy.log10(Z)
849 755
850 dataOut.data_output = Z
756 dataOut.data_output = RR[8]
851 757 dataOut.data_param = numpy.ones([3,self.Num_Hei])
852 758 dataOut.channelList = [0,1,2]
853 759
854 760 dataOut.data_param[0]=dBZ
855 761 dataOut.data_param[1]=V_mean
856 762 dataOut.data_param[2]=RR
857 763
858 #print 'VELRANGE', Velrange
859 #print 'Range', len(Range)
860 #print 'delv',del_V
861 # print 'DRANGE', D_range[:,56]
862 # #print 'NOISE', dataOut.noise[0], 10*numpy.log10(dataOut.noise[0])
863 # print 'radarconstant', RadarConstant
864 # #print 'ETAn SHAPE', ETAn.shape
865 # # print 'ETAn ', numpy.nansum(ETAn, axis=0)
866 # # print 'ETAd ', numpy.nansum(ETAd, axis=0)
867 # print 'Pr ', numpy.nansum(Pr, axis=0)
868 # print 'dataOut.SPCparam[1]', numpy.nansum(dataOut.SPCparam[1][0], axis=0)
869 # print 'Ze ', dBZe
870 print 'Z ', dBZ
871 # print 'Ndist',N_dist[:,56]
872 print 'RR2 ', RR2
873 print 'RR ', RR
874 print 'Vr', V_mean
875 #print 'RR2 ', dBRR2
876 #print 'D_mean', D_mean
877 #print 'del_V', del_V
878 #print 'D_range',D_range.shape, D_range[:,30]
879 #print 'Velrange', Velrange
880 #print 'numpy.nansum( N_dist[:,R]', numpy.nansum( N_dist, axis=0)
881 #print 'dataOut.data_param SHAPE', dataOut.data_param.shape
882
883 764
884 765 def dBZeMODE2(self, dataOut): # Processing for MIRA35C
885 766
886 767 NPW = dataOut.NPW
887 768 COFA = dataOut.COFA
888 769
889 770 SNR = numpy.array([self.spc[0,:,:] / NPW[0]]) #, self.spc[1,:,:] / NPW[1]])
890 771 RadarConst = dataOut.RadarConst
891 772 #frequency = 34.85*10**9
892 773
893 774 ETA = numpy.zeros(([self.Num_Chn ,self.Num_Hei]))
894 775 data_output = numpy.ones([self.Num_Chn , self.Num_Hei])*numpy.NaN
895 776
896 777 ETA = numpy.sum(SNR,1)
897 print 'ETA' , ETA
778
898 779 ETA = numpy.where(ETA is not 0. , ETA, numpy.NaN)
899 780
900 781 Ze = numpy.ones([self.Num_Chn, self.Num_Hei] )
901 782
902 783 for r in range(self.Num_Hei):
903 784
904 785 Ze[0,r] = ( ETA[0,r] ) * COFA[0,r][0] * RadarConst * ((r/5000.)**2)
905 786 #Ze[1,r] = ( ETA[1,r] ) * COFA[1,r][0] * RadarConst * ((r/5000.)**2)
906 787
907 788 return Ze
908 789
909 790 # def GetRadarConstant(self):
910 791 #
911 792 # """
912 793 # Constants:
913 794 #
914 795 # Pt: Transmission Power dB 5kW 5000
915 796 # Gt: Transmission Gain dB 24.7 dB 295.1209
916 797 # Gr: Reception Gain dB 18.5 dB 70.7945
917 798 # Lambda: Wavelenght m 0.6741 m 0.6741
918 799 # aL: Attenuation loses dB 4dB 2.5118
919 800 # tauW: Width of transmission pulse s 4us 4e-6
920 801 # ThetaT: Transmission antenna bean angle rad 0.1656317 rad 0.1656317
921 802 # ThetaR: Reception antenna beam angle rad 0.36774087 rad 0.36774087
922 803 #
923 804 # """
924 805 #
925 806 # Numerator = ( (4*numpy.pi)**3 * aL**2 * 16 * numpy.log(2) )
926 807 # Denominator = ( Pt * Gt * Gr * Lambda**2 * SPEED_OF_LIGHT * TauW * numpy.pi * ThetaT * TheraR)
927 808 # RadarConstant = Numerator / Denominator
928 809 #
929 810 # return RadarConstant
930 811
931 812
932 813
933 814 class FullSpectralAnalysis(Operation):
934 815
935 816 """
936 817 Function that implements Full Spectral Analisys technique.
937 818
938 819 Input:
939 820 self.dataOut.data_pre : SelfSpectra and CrossSPectra data
940 821 self.dataOut.groupList : Pairlist of channels
941 822 self.dataOut.ChanDist : Physical distance between receivers
942 823
943 824
944 825 Output:
945 826
946 827 self.dataOut.data_output : Zonal wind, Meridional wind and Vertical wind
947 828
948 829
949 830 Parameters affected: Winds, height range, SNR
950 831
951 832 """
952 833 def run(self, dataOut, Xi01=None, Xi02=None, Xi12=None, Eta01=None, Eta02=None, Eta12=None, SNRlimit=7):
953 834
954 835 self.indice=int(numpy.random.rand()*1000)
955 836
956 837 spc = dataOut.data_pre[0].copy()
957 838 cspc = dataOut.data_pre[1]
958 839
840 """NOTA SE DEBE REMOVER EL RANGO DEL PULSO TX"""
841
842 SNRspc = spc.copy()
843 SNRspc[:,:,0:7]= numpy.NaN
844
845 """##########################################"""
846
847
959 848 nChannel = spc.shape[0]
960 849 nProfiles = spc.shape[1]
961 850 nHeights = spc.shape[2]
962 851
963 852 pairsList = dataOut.groupList
964 853 if dataOut.ChanDist is not None :
965 854 ChanDist = dataOut.ChanDist
966 855 else:
967 856 ChanDist = numpy.array([[Xi01, Eta01],[Xi02,Eta02],[Xi12,Eta12]])
968 857
969 858 FrecRange = dataOut.spc_range[0]
970 859
971 860 ySamples=numpy.ones([nChannel,nProfiles])
972 861 phase=numpy.ones([nChannel,nProfiles])
973 862 CSPCSamples=numpy.ones([nChannel,nProfiles],dtype=numpy.complex_)
974 863 coherence=numpy.ones([nChannel,nProfiles])
975 864 PhaseSlope=numpy.ones(nChannel)
976 865 PhaseInter=numpy.ones(nChannel)
977 866 data_SNR=numpy.zeros([nProfiles])
978 867
979 868 data = dataOut.data_pre
980 869 noise = dataOut.noise
981 870
982 dataOut.data_SNR = (numpy.mean(spc,axis=1)- noise[0]) / noise[0]
871 dataOut.data_SNR = (numpy.mean(SNRspc,axis=1)- noise[0]) / noise[0]
983 872
984 873 dataOut.data_SNR[numpy.where( dataOut.data_SNR <0 )] = 1e-20
985
986 874
987 #FirstMoment = dataOut.moments[0,1,:]#numpy.average(dataOut.data_param[:,1,:],0)
988 #SecondMoment = numpy.average(dataOut.moments[:,2,:],0)
989
990 #SNRdBMean = []
991 875
992 876 data_output=numpy.ones([spc.shape[0],spc.shape[2]])*numpy.NaN
993 877
994 878 velocityX=[]
995 879 velocityY=[]
996 880 velocityV=[]
997 881 PhaseLine=[]
998 882
999 883 dbSNR = 10*numpy.log10(dataOut.data_SNR)
1000 884 dbSNR = numpy.average(dbSNR,0)
1001 885
1002 886 for Height in range(nHeights):
1003
887
1004 888 [Vzon,Vmer,Vver, GaussCenter, PhaseSlope, FitGaussCSPC]= self.WindEstimation(spc, cspc, pairsList, ChanDist, Height, noise, dataOut.spc_range.copy(), dbSNR[Height], SNRlimit)
1005 889 PhaseLine = numpy.append(PhaseLine, PhaseSlope)
1006 890
1007 891 if abs(Vzon)<100. and abs(Vzon)> 0.:
1008 892 velocityX=numpy.append(velocityX, Vzon)#Vmag
1009 893
1010 894 else:
1011 #print 'Vzon',Vzon
1012 895 velocityX=numpy.append(velocityX, numpy.NaN)
1013 896
1014 897 if abs(Vmer)<100. and abs(Vmer) > 0.:
1015 898 velocityY=numpy.append(velocityY, -Vmer)#Vang
1016 899
1017 900 else:
1018 #print 'Vmer',Vmer
1019 901 velocityY=numpy.append(velocityY, numpy.NaN)
1020 902
1021 903 if dbSNR[Height] > SNRlimit:
1022 904 velocityV=numpy.append(velocityV, -Vver)#FirstMoment[Height])
1023 905 else:
1024 906 velocityV=numpy.append(velocityV, numpy.NaN)
1025 #FirstMoment[Height]= numpy.NaN
1026 # if SNRdBMean[Height] <12:
1027 # FirstMoment[Height] = numpy.NaN
1028 # velocityX[Height] = numpy.NaN
1029 # velocityY[Height] = numpy.NaN
907
1030 908
1031 909
1032 910
1033 911 data_output[0] = numpy.array(velocityX) #self.moving_average(numpy.array(velocityX) , N=1)
1034 912 data_output[1] = numpy.array(velocityY) #self.moving_average(numpy.array(velocityY) , N=1)
1035 913 data_output[2] = velocityV#FirstMoment
1036 914
1037 print 'data_output', data_output.shape
1038 #print FirstMoment
1039 # print 'velocityX',numpy.shape(data_output[0])
1040 # print 'velocityX',data_output[0]
1041 # print ' '
1042 # print 'velocityY',numpy.shape(data_output[1])
1043 # print 'velocityY',data_output[1]
1044 # print 'velocityV',data_output[2]
1045 # print 'PhaseLine',PhaseLine
1046 #print numpy.array(velocityY)
1047 #print 'SNR'
1048 #print 10*numpy.log10(dataOut.data_SNR)
1049 #print numpy.shape(10*numpy.log10(dataOut.data_SNR))
1050 print ' '
1051
1052 915 xFrec=FrecRange[0:spc.shape[1]]
1053 916
1054 917 dataOut.data_output=data_output
1055 918
1056 919 return
1057 920
1058 921
1059 922 def moving_average(self,x, N=2):
1060 923 return numpy.convolve(x, numpy.ones((N,))/N)[(N-1):]
1061 924
1062 925 def gaus(self,xSamples,Amp,Mu,Sigma):
1063 926 return ( Amp / ((2*numpy.pi)**0.5 * Sigma) ) * numpy.exp( -( xSamples - Mu )**2 / ( 2 * (Sigma**2) ))
1064 927
1065 928
1066 929
1067 930 def Moments(self, ySamples, xSamples):
1068 931 Pot = numpy.nansum( ySamples ) # Potencia, momento 0
1069 932 yNorm = ySamples / Pot
1070 933 Vr = numpy.nansum( yNorm * xSamples ) # Velocidad radial, mu, corrimiento doppler, primer momento
1071 934 Sigma2 = abs(numpy.nansum( yNorm * ( xSamples - Vr )**2 )) # Segundo Momento
1072 935 Desv = Sigma2**0.5 # Desv. Estandar, Ancho espectral
1073 936
1074 937 return numpy.array([Pot, Vr, Desv])
1075 938
1076 939 def WindEstimation(self, spc, cspc, pairsList, ChanDist, Height, noise, AbbsisaRange, dbSNR, SNRlimit):
1077 940
1078 941
1079 942
1080 943 ySamples=numpy.ones([spc.shape[0],spc.shape[1]])
1081 944 phase=numpy.ones([spc.shape[0],spc.shape[1]])
1082 945 CSPCSamples=numpy.ones([spc.shape[0],spc.shape[1]],dtype=numpy.complex_)
1083 946 coherence=numpy.ones([spc.shape[0],spc.shape[1]])
1084 947 PhaseSlope=numpy.zeros(spc.shape[0])
1085 948 PhaseInter=numpy.ones(spc.shape[0])
1086 949 xFrec=AbbsisaRange[0][0:spc.shape[1]]
1087 950 xVel =AbbsisaRange[2][0:spc.shape[1]]
1088 951 Vv=numpy.empty(spc.shape[2])*0
1089 952 SPCav = numpy.average(spc, axis=0)-numpy.average(noise) #spc[0]-noise[0]#
1090 953
1091 954 SPCmoments = self.Moments(SPCav[:,Height], xVel )
1092 955 CSPCmoments = []
1093 956 cspcNoise = numpy.empty(3)
1094 957
1095 958 '''Getting Eij and Nij'''
1096 959
1097 960 Xi01=ChanDist[0][0]
1098 961 Eta01=ChanDist[0][1]
1099 962
1100 963 Xi02=ChanDist[1][0]
1101 964 Eta02=ChanDist[1][1]
1102 965
1103 966 Xi12=ChanDist[2][0]
1104 967 Eta12=ChanDist[2][1]
1105 968
1106 969 z = spc.copy()
1107 970 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
1108 971
1109 972 for i in range(spc.shape[0]):
1110 973
1111 974 '''****** Line of Data SPC ******'''
1112 975 zline=z[i,:,Height].copy() - noise[i] # Se resta ruido
1113 976
1114 977 '''****** SPC is normalized ******'''
1115 978 SmoothSPC =self.moving_average(zline.copy(),N=1) # Se suaviza el ruido
1116 979 FactNorm = SmoothSPC/numpy.nansum(SmoothSPC) # SPC Normalizado y suavizado
1117 980
1118 981 xSamples = xFrec # Se toma el rango de frecuncias
1119 982 ySamples[i] = FactNorm # Se toman los valores de SPC normalizado
1120 983
1121 984 for i in range(spc.shape[0]):
1122 985
1123 986 '''****** Line of Data CSPC ******'''
1124 987 cspcLine = ( cspc[i,:,Height].copy())# - noise[i] ) # no! Se resta el ruido
1125 988 SmoothCSPC =self.moving_average(cspcLine,N=1) # Se suaviza el ruido
1126 989 cspcNorm = SmoothCSPC/numpy.nansum(SmoothCSPC) # CSPC normalizado y suavizado
1127 990
1128 991 '''****** CSPC is normalized with respect to Briggs and Vincent ******'''
1129 992 chan_index0 = pairsList[i][0]
1130 993 chan_index1 = pairsList[i][1]
1131 994
1132 995 CSPCFactor= numpy.abs(numpy.nansum(ySamples[chan_index0]))**2 * numpy.abs(numpy.nansum(ySamples[chan_index1]))**2
1133 996 CSPCNorm = cspcNorm / numpy.sqrt(CSPCFactor)
1134 997
1135 998 CSPCSamples[i] = CSPCNorm
1136 999
1137 1000 coherence[i] = numpy.abs(CSPCSamples[i]) / numpy.sqrt(CSPCFactor)
1138 1001
1139 1002 #coherence[i]= self.moving_average(coherence[i],N=1)
1140 1003
1141 1004 phase[i] = self.moving_average( numpy.arctan2(CSPCSamples[i].imag, CSPCSamples[i].real),N=1)#*180/numpy.pi
1142 1005
1143 1006 CSPCmoments = numpy.vstack([self.Moments(numpy.abs(CSPCSamples[0]), xSamples),
1144 1007 self.Moments(numpy.abs(CSPCSamples[1]), xSamples),
1145 1008 self.Moments(numpy.abs(CSPCSamples[2]), xSamples)])
1146 1009
1147 #print '##### SUMA de SPC #####', len(ySamples)
1148 #print numpy.sum(ySamples[0])
1149 #print '##### SUMA de CSPC #####', len(coherence)
1150 #print numpy.sum(numpy.abs(CSPCNorm))
1151 #print numpy.sum(coherence[0])
1152 # print 'len',len(xSamples)
1153 # print 'CSPCmoments', numpy.shape(CSPCmoments)
1154 # print CSPCmoments
1155 # print '#######################'
1156
1010
1157 1011 popt=[1e-10,0,1e-10]
1158 1012 popt01, popt02, popt12 = [1e-10,1e-10,1e-10], [1e-10,1e-10,1e-10] ,[1e-10,1e-10,1e-10]
1159 1013 FitGauss01, FitGauss02, FitGauss12 = numpy.empty(len(xSamples))*0, numpy.empty(len(xSamples))*0, numpy.empty(len(xSamples))*0
1160 1014
1161 1015 CSPCMask01 = numpy.abs(CSPCSamples[0])
1162 1016 CSPCMask02 = numpy.abs(CSPCSamples[1])
1163 1017 CSPCMask12 = numpy.abs(CSPCSamples[2])
1164 1018
1165 1019 mask01 = ~numpy.isnan(CSPCMask01)
1166 1020 mask02 = ~numpy.isnan(CSPCMask02)
1167 1021 mask12 = ~numpy.isnan(CSPCMask12)
1168 1022
1169 1023 #mask = ~numpy.isnan(CSPCMask01)
1170 1024 CSPCMask01 = CSPCMask01[mask01]
1171 1025 CSPCMask02 = CSPCMask02[mask02]
1172 1026 CSPCMask12 = CSPCMask12[mask12]
1173 1027 #CSPCMask01 = numpy.ma.masked_invalid(CSPCMask01)
1174 1028
1175 1029
1176 1030
1177 1031 '''***Fit Gauss CSPC01***'''
1178 1032 if dbSNR > SNRlimit and numpy.abs(SPCmoments[1])<3 :
1179 1033 try:
1180 1034 popt01,pcov = curve_fit(self.gaus,xSamples[mask01],numpy.abs(CSPCMask01),p0=CSPCmoments[0])
1181 1035 popt02,pcov = curve_fit(self.gaus,xSamples[mask02],numpy.abs(CSPCMask02),p0=CSPCmoments[1])
1182 1036 popt12,pcov = curve_fit(self.gaus,xSamples[mask12],numpy.abs(CSPCMask12),p0=CSPCmoments[2])
1183 1037 FitGauss01 = self.gaus(xSamples,*popt01)
1184 1038 FitGauss02 = self.gaus(xSamples,*popt02)
1185 1039 FitGauss12 = self.gaus(xSamples,*popt12)
1186 1040 except:
1187 1041 FitGauss01=numpy.ones(len(xSamples))*numpy.mean(numpy.abs(CSPCSamples[0]))
1188 1042 FitGauss02=numpy.ones(len(xSamples))*numpy.mean(numpy.abs(CSPCSamples[1]))
1189 1043 FitGauss12=numpy.ones(len(xSamples))*numpy.mean(numpy.abs(CSPCSamples[2]))
1190 1044
1191 1045
1192 1046 CSPCopt = numpy.vstack([popt01,popt02,popt12])
1193 1047
1194 1048 '''****** Getting fij width ******'''
1195 1049
1196 1050 yMean = numpy.average(ySamples, axis=0) # ySamples[0]
1197 1051
1198 1052 '''******* Getting fitting Gaussian *******'''
1199 1053 meanGauss = sum(xSamples*yMean) / len(xSamples) # Mu, velocidad radial (frecuencia)
1200 1054 sigma2 = sum(yMean*(xSamples-meanGauss)**2) / len(xSamples) # Varianza, Ancho espectral (frecuencia)
1201 1055
1202 1056 yMoments = self.Moments(yMean, xSamples)
1203 1057
1204 1058 if dbSNR > SNRlimit and numpy.abs(SPCmoments[1])<3: # and abs(meanGauss/sigma2) > 0.00001:
1205 1059 try:
1206 1060 popt,pcov = curve_fit(self.gaus,xSamples,yMean,p0=yMoments)
1207 1061 FitGauss=self.gaus(xSamples,*popt)
1208 1062
1209 1063 except :#RuntimeError:
1210 1064 FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
1211 1065
1212 1066
1213 1067 else:
1214 1068 FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
1215 1069
1216 1070
1217 1071
1218 1072 '''****** Getting Fij ******'''
1219 1073 Fijcspc = CSPCopt[:,2]/2*3
1220 1074
1221 1075
1222 1076 GaussCenter = popt[1] #xFrec[GCpos]
1223 1077 #Punto en Eje X de la Gaussiana donde se encuentra el centro
1224 1078 ClosestCenter = xSamples[numpy.abs(xSamples-GaussCenter).argmin()]
1225 1079 PointGauCenter = numpy.where(xSamples==ClosestCenter)[0][0]
1226 1080
1227 1081 #Punto e^-1 hubicado en la Gaussiana
1228 1082 PeMinus1 = numpy.max(FitGauss)* numpy.exp(-1)
1229 1083 FijClosest = FitGauss[numpy.abs(FitGauss-PeMinus1).argmin()] # El punto mas cercano a "Peminus1" dentro de "FitGauss"
1230 1084 PointFij = numpy.where(FitGauss==FijClosest)[0][0]
1231 1085
1232 1086 if xSamples[PointFij] > xSamples[PointGauCenter]:
1233 1087 Fij = xSamples[PointFij] - xSamples[PointGauCenter]
1234 1088
1235 1089 else:
1236 1090 Fij = xSamples[PointGauCenter] - xSamples[PointFij]
1237 1091
1238 # print 'CSPCopt'
1239 # print CSPCopt
1240 # print 'popt'
1241 # print popt
1242 # print '#######################################'
1243 #print 'dataOut.data_param', numpy.shape(data_param)
1244 #print 'dataOut.data_param0', data_param[0,0,Height]
1245 #print 'dataOut.data_param1', data_param[0,1,Height]
1246 #print 'dataOut.data_param2', data_param[0,2,Height]
1247
1248
1249 # print 'yMoments', yMoments
1250 # print 'Moments', SPCmoments
1251 # print 'Fij2 Moment', Fij
1252 # #print 'Fij', Fij, 'popt[2]/2',popt[2]/2
1253 # print 'Fijcspc',Fijcspc
1254 # print '#######################################'
1255
1256 1092
1257 1093 '''****** Taking frequency ranges from SPCs ******'''
1258 1094
1259 1095
1260 1096 #GaussCenter = popt[1] #Primer momento 01
1261 1097 GauWidth = popt[2] *3/2 #Ancho de banda de Gau01
1262 1098 Range = numpy.empty(2)
1263 1099 Range[0] = GaussCenter - GauWidth
1264 1100 Range[1] = GaussCenter + GauWidth
1265 1101 #Punto en Eje X de la Gaussiana donde se encuentra ancho de banda (min:max)
1266 1102 ClosRangeMin = xSamples[numpy.abs(xSamples-Range[0]).argmin()]
1267 1103 ClosRangeMax = xSamples[numpy.abs(xSamples-Range[1]).argmin()]
1268 1104
1269 1105 PointRangeMin = numpy.where(xSamples==ClosRangeMin)[0][0]
1270 1106 PointRangeMax = numpy.where(xSamples==ClosRangeMax)[0][0]
1271 1107
1272 1108 Range=numpy.array([ PointRangeMin, PointRangeMax ])
1273 1109
1274 1110 FrecRange = xFrec[ Range[0] : Range[1] ]
1275 1111 VelRange = xVel[ Range[0] : Range[1] ]
1276 1112
1277 1113
1278 #print 'RANGE: ', Range
1279 #print 'FrecRange', numpy.shape(FrecRange)#,FrecRange
1280 #print 'len: ', len(FrecRange)
1281
1282 1114 '''****** Getting SCPC Slope ******'''
1283 1115
1284 1116 for i in range(spc.shape[0]):
1285 1117
1286 1118 if len(FrecRange)>5 and len(FrecRange)<spc.shape[1]*0.3:
1287 1119 PhaseRange=self.moving_average(phase[i,Range[0]:Range[1]],N=3)
1288
1289 #print 'Ancho espectral Frecuencias', FrecRange[-1]-FrecRange[0], 'Hz'
1290 #print 'Ancho espectral Velocidades', VelRange[-1]-VelRange[0], 'm/s'
1291 #print 'FrecRange', len(FrecRange) , FrecRange
1292 #print 'VelRange', len(VelRange) , VelRange
1293 #print 'PhaseRange', numpy.shape(PhaseRange), PhaseRange
1294 #print ' '
1295
1120
1296 1121 '''***********************VelRange******************'''
1297 1122
1298 1123 mask = ~numpy.isnan(FrecRange) & ~numpy.isnan(PhaseRange)
1299 1124
1300 1125 if len(FrecRange) == len(PhaseRange):
1301 1126 try:
1302 1127 slope, intercept, r_value, p_value, std_err = stats.linregress(FrecRange[mask], PhaseRange[mask])
1303 1128 PhaseSlope[i]=slope
1304 1129 PhaseInter[i]=intercept
1305 1130 except:
1306 1131 PhaseSlope[i]=0
1307 1132 PhaseInter[i]=0
1308 1133 else:
1309 1134 PhaseSlope[i]=0
1310 1135 PhaseInter[i]=0
1311 1136 else:
1312 1137 PhaseSlope[i]=0
1313 1138 PhaseInter[i]=0
1314 1139
1315 1140
1316 1141 '''Getting constant C'''
1317 1142 cC=(Fij*numpy.pi)**2
1318 1143
1319 1144 '''****** Getting constants F and G ******'''
1320 1145 MijEijNij=numpy.array([[Xi02,Eta02], [Xi12,Eta12]])
1321 1146 MijResult0=(-PhaseSlope[1]*cC) / (2*numpy.pi)
1322 1147 MijResult1=(-PhaseSlope[2]*cC) / (2*numpy.pi)
1323 1148 MijResults=numpy.array([MijResult0,MijResult1])
1324 1149 (cF,cG) = numpy.linalg.solve(MijEijNij, MijResults)
1325 1150
1326 1151 '''****** Getting constants A, B and H ******'''
1327 1152 W01=numpy.nanmax( FitGauss01 ) #numpy.abs(CSPCSamples[0]))
1328 1153 W02=numpy.nanmax( FitGauss02 ) #numpy.abs(CSPCSamples[1]))
1329 1154 W12=numpy.nanmax( FitGauss12 ) #numpy.abs(CSPCSamples[2]))
1330 1155
1331 1156 WijResult0=((cF*Xi01+cG*Eta01)**2)/cC - numpy.log(W01 / numpy.sqrt(numpy.pi/cC))
1332 1157 WijResult1=((cF*Xi02+cG*Eta02)**2)/cC - numpy.log(W02 / numpy.sqrt(numpy.pi/cC))
1333 1158 WijResult2=((cF*Xi12+cG*Eta12)**2)/cC - numpy.log(W12 / numpy.sqrt(numpy.pi/cC))
1334 1159
1335 1160 WijResults=numpy.array([WijResult0, WijResult1, WijResult2])
1336 1161
1337 1162 WijEijNij=numpy.array([ [Xi01**2, Eta01**2, 2*Xi01*Eta01] , [Xi02**2, Eta02**2, 2*Xi02*Eta02] , [Xi12**2, Eta12**2, 2*Xi12*Eta12] ])
1338 1163 (cA,cB,cH) = numpy.linalg.solve(WijEijNij, WijResults)
1339 1164
1340 1165 VxVy=numpy.array([[cA,cH],[cH,cB]])
1341 1166 VxVyResults=numpy.array([-cF,-cG])
1342 1167 (Vx,Vy) = numpy.linalg.solve(VxVy, VxVyResults)
1343 1168
1344 #print 'MijResults, cC, PhaseSlope', MijResults, cC, PhaseSlope
1345 #print 'W01,02,12', W01, W02, W12
1346 #print 'WijResult0,1,2',WijResult0, WijResult1, WijResult2, 'Results', WijResults
1347 #print 'cA,cB,cH, cF, cG', cA, cB, cH, cF, cG
1348 #print 'VxVy', VxVyResults
1349 #print '###########################****************************************'
1350 1169 Vzon = Vy
1351 1170 Vmer = Vx
1352 1171 Vmag=numpy.sqrt(Vzon**2+Vmer**2)
1353 1172 Vang=numpy.arctan2(Vmer,Vzon)
1354 1173 if numpy.abs( popt[1] ) < 3.5 and len(FrecRange)>4:
1355 1174 Vver=popt[1]
1356 1175 else:
1357 1176 Vver=numpy.NaN
1358 1177 FitGaussCSPC = numpy.array([FitGauss01,FitGauss02,FitGauss12])
1359 1178
1360 1179
1361 # ''' Ploteo por altura '''
1362 # if Height == 28:
1363 # for i in range(3):
1364 # #print 'FASE', numpy.shape(phase), y[25]
1365 # #print numpy.shape(coherence)
1366 # fig = plt.figure(10+self.indice)
1367 # #plt.plot( x[0:256],coherence[:,25] )
1368 # #cohAv = numpy.average(coherence[i],1)
1369 # Pendiente = FrecRange * PhaseSlope[i]
1370 # plt.plot( FrecRange, Pendiente)
1371 # plt.plot( xFrec,phase[i])
1372 #
1373 # CSPCmean = numpy.mean(numpy.abs(CSPCSamples),0)
1374 # #plt.plot(xFrec, FitGauss01)
1375 # #plt.plot(xFrec, CSPCmean)
1376 # #plt.plot(xFrec, numpy.abs(CSPCSamples[0]))
1377 # #plt.plot(xFrec, FitGauss)
1378 # #plt.plot(xFrec, yMean)
1379 # #plt.plot(xFrec, numpy.abs(coherence[0]))
1380 #
1381 # #plt.axis([-12, 12, 15, 50])
1382 # #plt.title("%s" %( '%s %s, Channel %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S") , i)))
1383 # plt.ylabel('Desfase [rad]')
1384 # #plt.ylabel('CSPC normalizado')
1385 # plt.xlabel('Frec range [Hz]')
1386
1387 #fig.savefig('/home/erick/Documents/Pics/to{}.png'.format(self.indice))
1388
1389 # plt.show()
1390 # self.indice=self.indice+1
1391
1392
1393
1394
1395
1396 # print 'vzon y vmer', Vzon, Vmer
1397 1180 return Vzon, Vmer, Vver, GaussCenter, PhaseSlope, FitGaussCSPC
1398 1181
1399 1182 class SpectralMoments(Operation):
1400 1183
1401 1184 '''
1402 1185 Function SpectralMoments()
1403 1186
1404 1187 Calculates moments (power, mean, standard deviation) and SNR of the signal
1405 1188
1406 1189 Type of dataIn: Spectra
1407 1190
1408 1191 Configuration Parameters:
1409 1192
1410 1193 dirCosx : Cosine director in X axis
1411 1194 dirCosy : Cosine director in Y axis
1412 1195
1413 1196 elevation :
1414 1197 azimuth :
1415 1198
1416 1199 Input:
1417 1200 channelList : simple channel list to select e.g. [2,3,7]
1418 1201 self.dataOut.data_pre : Spectral data
1419 1202 self.dataOut.abscissaList : List of frequencies
1420 1203 self.dataOut.noise : Noise level per channel
1421 1204
1422 1205 Affected:
1423 1206 self.dataOut.moments : Parameters per channel
1424 1207 self.dataOut.data_SNR : SNR per channel
1425 1208
1426 1209 '''
1427 1210
1428 1211 def run(self, dataOut):
1429 1212
1430 1213 #dataOut.data_pre = dataOut.data_pre[0]
1431 1214 data = dataOut.data_pre[0]
1432 1215 absc = dataOut.abscissaList[:-1]
1433 1216 noise = dataOut.noise
1434 1217 nChannel = data.shape[0]
1435 1218 data_param = numpy.zeros((nChannel, 4, data.shape[2]))
1436 1219
1437 1220 for ind in range(nChannel):
1438 1221 data_param[ind,:,:] = self.__calculateMoments( data[ind,:,:] , absc , noise[ind] )
1439 1222
1440 1223 dataOut.moments = data_param[:,1:,:]
1441 1224 dataOut.data_SNR = data_param[:,0]
1442 1225 return
1443 1226
1444 1227 def __calculateMoments(self, oldspec, oldfreq, n0,
1445 1228 nicoh = None, graph = None, smooth = None, type1 = None, fwindow = None, snrth = None, dc = None, aliasing = None, oldfd = None, wwauto = None):
1446 1229
1447 1230 if (nicoh == None): nicoh = 1
1448 1231 if (graph == None): graph = 0
1449 1232 if (smooth == None): smooth = 0
1450 1233 elif (self.smooth < 3): smooth = 0
1451 1234
1452 1235 if (type1 == None): type1 = 0
1453 1236 if (fwindow == None): fwindow = numpy.zeros(oldfreq.size) + 1
1454 1237 if (snrth == None): snrth = -3
1455 1238 if (dc == None): dc = 0
1456 1239 if (aliasing == None): aliasing = 0
1457 1240 if (oldfd == None): oldfd = 0
1458 1241 if (wwauto == None): wwauto = 0
1459 1242
1460 1243 if (n0 < 1.e-20): n0 = 1.e-20
1461 1244
1462 1245 freq = oldfreq
1463 1246 vec_power = numpy.zeros(oldspec.shape[1])
1464 1247 vec_fd = numpy.zeros(oldspec.shape[1])
1465 1248 vec_w = numpy.zeros(oldspec.shape[1])
1466 1249 vec_snr = numpy.zeros(oldspec.shape[1])
1467 1250
1468 1251 oldspec = numpy.ma.masked_invalid(oldspec)
1469 1252
1470 1253 for ind in range(oldspec.shape[1]):
1471 1254
1472 1255 spec = oldspec[:,ind]
1473 1256 aux = spec*fwindow
1474 1257 max_spec = aux.max()
1475 1258 m = list(aux).index(max_spec)
1476 1259
1477 1260 #Smooth
1478 1261 if (smooth == 0): spec2 = spec
1479 1262 else: spec2 = scipy.ndimage.filters.uniform_filter1d(spec,size=smooth)
1480 1263
1481 1264 # Calculo de Momentos
1482 1265 bb = spec2[range(m,spec2.size)]
1483 1266 bb = (bb<n0).nonzero()
1484 1267 bb = bb[0]
1485 1268
1486 1269 ss = spec2[range(0,m + 1)]
1487 1270 ss = (ss<n0).nonzero()
1488 1271 ss = ss[0]
1489 1272
1490 1273 if (bb.size == 0):
1491 1274 bb0 = spec.size - 1 - m
1492 1275 else:
1493 1276 bb0 = bb[0] - 1
1494 1277 if (bb0 < 0):
1495 1278 bb0 = 0
1496 1279
1497 1280 if (ss.size == 0): ss1 = 1
1498 1281 else: ss1 = max(ss) + 1
1499 1282
1500 1283 if (ss1 > m): ss1 = m
1501 1284
1502 1285 valid = numpy.asarray(range(int(m + bb0 - ss1 + 1))) + ss1
1503 1286 power = ( (spec2[valid] - n0) * fwindow[valid] ).sum()
1504 1287 fd = ( (spec2[valid]- n0) * freq[valid] * fwindow[valid] ).sum() / power
1505 1288
1506 1289 w = math.sqrt(((spec2[valid] - n0)*fwindow[valid]*(freq[valid]- fd)**2).sum()/power)
1507 1290 snr = (spec2.mean()-n0)/n0
1508 1291
1509 1292 if (snr < 1.e-20) :
1510 1293 snr = 1.e-20
1511 1294
1512 1295 vec_power[ind] = power
1513 1296 vec_fd[ind] = fd
1514 1297 vec_w[ind] = w
1515 1298 vec_snr[ind] = snr
1516 1299
1517 1300 moments = numpy.vstack((vec_snr, vec_power, vec_fd, vec_w))
1518 1301 return moments
1519 1302
1520 1303 #------------------ Get SA Parameters --------------------------
1521 1304
1522 1305 def GetSAParameters(self):
1523 1306 #SA en frecuencia
1524 1307 pairslist = self.dataOut.groupList
1525 1308 num_pairs = len(pairslist)
1526 1309
1527 1310 vel = self.dataOut.abscissaList
1528 1311 spectra = self.dataOut.data_pre
1529 1312 cspectra = self.dataIn.data_cspc
1530 1313 delta_v = vel[1] - vel[0]
1531 1314
1532 1315 #Calculating the power spectrum
1533 1316 spc_pow = numpy.sum(spectra, 3)*delta_v
1534 1317 #Normalizing Spectra
1535 1318 norm_spectra = spectra/spc_pow
1536 1319 #Calculating the norm_spectra at peak
1537 1320 max_spectra = numpy.max(norm_spectra, 3)
1538 1321
1539 1322 #Normalizing Cross Spectra
1540 1323 norm_cspectra = numpy.zeros(cspectra.shape)
1541 1324
1542 1325 for i in range(num_chan):
1543 1326 norm_cspectra[i,:,:] = cspectra[i,:,:]/numpy.sqrt(spc_pow[pairslist[i][0],:]*spc_pow[pairslist[i][1],:])
1544 1327
1545 1328 max_cspectra = numpy.max(norm_cspectra,2)
1546 1329 max_cspectra_index = numpy.argmax(norm_cspectra, 2)
1547 1330
1548 1331 for i in range(num_pairs):
1549 1332 cspc_par[i,:,:] = __calculateMoments(norm_cspectra)
1550 1333 #------------------- Get Lags ----------------------------------
1551 1334
1552 1335 class SALags(Operation):
1553 1336 '''
1554 1337 Function GetMoments()
1555 1338
1556 1339 Input:
1557 1340 self.dataOut.data_pre
1558 1341 self.dataOut.abscissaList
1559 1342 self.dataOut.noise
1560 1343 self.dataOut.normFactor
1561 1344 self.dataOut.data_SNR
1562 1345 self.dataOut.groupList
1563 1346 self.dataOut.nChannels
1564 1347
1565 1348 Affected:
1566 1349 self.dataOut.data_param
1567 1350
1568 1351 '''
1569 1352 def run(self, dataOut):
1570 1353 data_acf = dataOut.data_pre[0]
1571 1354 data_ccf = dataOut.data_pre[1]
1572 1355 normFactor_acf = dataOut.normFactor[0]
1573 1356 normFactor_ccf = dataOut.normFactor[1]
1574 1357 pairs_acf = dataOut.groupList[0]
1575 1358 pairs_ccf = dataOut.groupList[1]
1576 1359
1577 1360 nHeights = dataOut.nHeights
1578 1361 absc = dataOut.abscissaList
1579 1362 noise = dataOut.noise
1580 1363 SNR = dataOut.data_SNR
1581 1364 nChannels = dataOut.nChannels
1582 1365 # pairsList = dataOut.groupList
1583 1366 # pairsAutoCorr, pairsCrossCorr = self.__getPairsAutoCorr(pairsList, nChannels)
1584 1367
1585 1368 for l in range(len(pairs_acf)):
1586 1369 data_acf[l,:,:] = data_acf[l,:,:]/normFactor_acf[l,:]
1587 1370
1588 1371 for l in range(len(pairs_ccf)):
1589 1372 data_ccf[l,:,:] = data_ccf[l,:,:]/normFactor_ccf[l,:]
1590 1373
1591 1374 dataOut.data_param = numpy.zeros((len(pairs_ccf)*2 + 1, nHeights))
1592 1375 dataOut.data_param[:-1,:] = self.__calculateTaus(data_acf, data_ccf, absc)
1593 1376 dataOut.data_param[-1,:] = self.__calculateLag1Phase(data_acf, absc)
1594 1377 return
1595 1378
1596 1379 # def __getPairsAutoCorr(self, pairsList, nChannels):
1597 1380 #
1598 1381 # pairsAutoCorr = numpy.zeros(nChannels, dtype = 'int')*numpy.nan
1599 1382 #
1600 1383 # for l in range(len(pairsList)):
1601 1384 # firstChannel = pairsList[l][0]
1602 1385 # secondChannel = pairsList[l][1]
1603 1386 #
1604 1387 # #Obteniendo pares de Autocorrelacion
1605 1388 # if firstChannel == secondChannel:
1606 1389 # pairsAutoCorr[firstChannel] = int(l)
1607 1390 #
1608 1391 # pairsAutoCorr = pairsAutoCorr.astype(int)
1609 1392 #
1610 1393 # pairsCrossCorr = range(len(pairsList))
1611 1394 # pairsCrossCorr = numpy.delete(pairsCrossCorr,pairsAutoCorr)
1612 1395 #
1613 1396 # return pairsAutoCorr, pairsCrossCorr
1614 1397
1615 1398 def __calculateTaus(self, data_acf, data_ccf, lagRange):
1616 1399
1617 1400 lag0 = data_acf.shape[1]/2
1618 1401 #Funcion de Autocorrelacion
1619 1402 mean_acf = stats.nanmean(data_acf, axis = 0)
1620 1403
1621 1404 #Obtencion Indice de TauCross
1622 1405 ind_ccf = data_ccf.argmax(axis = 1)
1623 1406 #Obtencion Indice de TauAuto
1624 1407 ind_acf = numpy.zeros(ind_ccf.shape,dtype = 'int')
1625 1408 ccf_lag0 = data_ccf[:,lag0,:]
1626 1409
1627 1410 for i in range(ccf_lag0.shape[0]):
1628 1411 ind_acf[i,:] = numpy.abs(mean_acf - ccf_lag0[i,:]).argmin(axis = 0)
1629 1412
1630 1413 #Obtencion de TauCross y TauAuto
1631 1414 tau_ccf = lagRange[ind_ccf]
1632 1415 tau_acf = lagRange[ind_acf]
1633 1416
1634 1417 Nan1, Nan2 = numpy.where(tau_ccf == lagRange[0])
1635 1418
1636 1419 tau_ccf[Nan1,Nan2] = numpy.nan
1637 1420 tau_acf[Nan1,Nan2] = numpy.nan
1638 1421 tau = numpy.vstack((tau_ccf,tau_acf))
1639 1422
1640 1423 return tau
1641 1424
1642 1425 def __calculateLag1Phase(self, data, lagTRange):
1643 1426 data1 = stats.nanmean(data, axis = 0)
1644 1427 lag1 = numpy.where(lagTRange == 0)[0][0] + 1
1645 1428
1646 1429 phase = numpy.angle(data1[lag1,:])
1647 1430
1648 1431 return phase
1649 1432
1650 1433 class SpectralFitting(Operation):
1651 1434 '''
1652 1435 Function GetMoments()
1653 1436
1654 1437 Input:
1655 1438 Output:
1656 1439 Variables modified:
1657 1440 '''
1658 1441
1659 1442 def run(self, dataOut, getSNR = True, path=None, file=None, groupList=None):
1660 1443
1661 1444
1662 1445 if path != None:
1663 1446 sys.path.append(path)
1664 1447 self.dataOut.library = importlib.import_module(file)
1665 1448
1666 1449 #To be inserted as a parameter
1667 1450 groupArray = numpy.array(groupList)
1668 1451 # groupArray = numpy.array([[0,1],[2,3]])
1669 1452 self.dataOut.groupList = groupArray
1670 1453
1671 1454 nGroups = groupArray.shape[0]
1672 1455 nChannels = self.dataIn.nChannels
1673 1456 nHeights=self.dataIn.heightList.size
1674 1457
1675 1458 #Parameters Array
1676 1459 self.dataOut.data_param = None
1677 1460
1678 1461 #Set constants
1679 1462 constants = self.dataOut.library.setConstants(self.dataIn)
1680 1463 self.dataOut.constants = constants
1681 1464 M = self.dataIn.normFactor
1682 1465 N = self.dataIn.nFFTPoints
1683 1466 ippSeconds = self.dataIn.ippSeconds
1684 1467 K = self.dataIn.nIncohInt
1685 1468 pairsArray = numpy.array(self.dataIn.pairsList)
1686 1469
1687 1470 #List of possible combinations
1688 1471 listComb = itertools.combinations(numpy.arange(groupArray.shape[1]),2)
1689 1472 indCross = numpy.zeros(len(list(listComb)), dtype = 'int')
1690 1473
1691 1474 if getSNR:
1692 1475 listChannels = groupArray.reshape((groupArray.size))
1693 1476 listChannels.sort()
1694 1477 noise = self.dataIn.getNoise()
1695 1478 self.dataOut.data_SNR = self.__getSNR(self.dataIn.data_spc[listChannels,:,:], noise[listChannels])
1696 1479
1697 1480 for i in range(nGroups):
1698 1481 coord = groupArray[i,:]
1699 1482
1700 1483 #Input data array
1701 1484 data = self.dataIn.data_spc[coord,:,:]/(M*N)
1702 1485 data = data.reshape((data.shape[0]*data.shape[1],data.shape[2]))
1703 1486
1704 1487 #Cross Spectra data array for Covariance Matrixes
1705 1488 ind = 0
1706 1489 for pairs in listComb:
1707 1490 pairsSel = numpy.array([coord[x],coord[y]])
1708 1491 indCross[ind] = int(numpy.where(numpy.all(pairsArray == pairsSel, axis = 1))[0][0])
1709 1492 ind += 1
1710 1493 dataCross = self.dataIn.data_cspc[indCross,:,:]/(M*N)
1711 1494 dataCross = dataCross**2/K
1712 1495
1713 1496 for h in range(nHeights):
1714 # print self.dataOut.heightList[h]
1715 1497
1716 1498 #Input
1717 1499 d = data[:,h]
1718 1500
1719 1501 #Covariance Matrix
1720 1502 D = numpy.diag(d**2/K)
1721 1503 ind = 0
1722 1504 for pairs in listComb:
1723 1505 #Coordinates in Covariance Matrix
1724 1506 x = pairs[0]
1725 1507 y = pairs[1]
1726 1508 #Channel Index
1727 1509 S12 = dataCross[ind,:,h]
1728 1510 D12 = numpy.diag(S12)
1729 1511 #Completing Covariance Matrix with Cross Spectras
1730 1512 D[x*N:(x+1)*N,y*N:(y+1)*N] = D12
1731 1513 D[y*N:(y+1)*N,x*N:(x+1)*N] = D12
1732 1514 ind += 1
1733 1515 Dinv=numpy.linalg.inv(D)
1734 1516 L=numpy.linalg.cholesky(Dinv)
1735 1517 LT=L.T
1736 1518
1737 1519 dp = numpy.dot(LT,d)
1738 1520
1739 1521 #Initial values
1740 1522 data_spc = self.dataIn.data_spc[coord,:,h]
1741 1523
1742 1524 if (h>0)and(error1[3]<5):
1743 1525 p0 = self.dataOut.data_param[i,:,h-1]
1744 1526 else:
1745 1527 p0 = numpy.array(self.dataOut.library.initialValuesFunction(data_spc, constants, i))
1746 1528
1747 1529 try:
1748 1530 #Least Squares
1749 1531 minp,covp,infodict,mesg,ier = optimize.leastsq(self.__residFunction,p0,args=(dp,LT,constants),full_output=True)
1750 1532 # minp,covp = optimize.leastsq(self.__residFunction,p0,args=(dp,LT,constants))
1751 1533 #Chi square error
1752 1534 error0 = numpy.sum(infodict['fvec']**2)/(2*N)
1753 1535 #Error with Jacobian
1754 1536 error1 = self.dataOut.library.errorFunction(minp,constants,LT)
1755 1537 except:
1756 1538 minp = p0*numpy.nan
1757 1539 error0 = numpy.nan
1758 1540 error1 = p0*numpy.nan
1759 1541
1760 1542 #Save
1761 1543 if self.dataOut.data_param == None:
1762 1544 self.dataOut.data_param = numpy.zeros((nGroups, p0.size, nHeights))*numpy.nan
1763 1545 self.dataOut.data_error = numpy.zeros((nGroups, p0.size + 1, nHeights))*numpy.nan
1764 1546
1765 1547 self.dataOut.data_error[i,:,h] = numpy.hstack((error0,error1))
1766 1548 self.dataOut.data_param[i,:,h] = minp
1767 1549 return
1768 1550
1769 1551 def __residFunction(self, p, dp, LT, constants):
1770 1552
1771 1553 fm = self.dataOut.library.modelFunction(p, constants)
1772 1554 fmp=numpy.dot(LT,fm)
1773 1555
1774 1556 return dp-fmp
1775 1557
1776 1558 def __getSNR(self, z, noise):
1777 1559
1778 1560 avg = numpy.average(z, axis=1)
1779 1561 SNR = (avg.T-noise)/noise
1780 1562 SNR = SNR.T
1781 1563 return SNR
1782 1564
1783 1565 def __chisq(p,chindex,hindex):
1784 1566 #similar to Resid but calculates CHI**2
1785 1567 [LT,d,fm]=setupLTdfm(p,chindex,hindex)
1786 1568 dp=numpy.dot(LT,d)
1787 1569 fmp=numpy.dot(LT,fm)
1788 1570 chisq=numpy.dot((dp-fmp).T,(dp-fmp))
1789 1571 return chisq
1790 1572
1791 1573 class WindProfiler(Operation):
1792 1574
1793 1575 __isConfig = False
1794 1576
1795 1577 __initime = None
1796 1578 __lastdatatime = None
1797 1579 __integrationtime = None
1798 1580
1799 1581 __buffer = None
1800 1582
1801 1583 __dataReady = False
1802 1584
1803 1585 __firstdata = None
1804 1586
1805 1587 n = None
1806 1588
1807 1589 def __init__(self, **kwargs):
1808 1590 Operation.__init__(self, **kwargs)
1809 1591
1810 1592 def __calculateCosDir(self, elev, azim):
1811 1593 zen = (90 - elev)*numpy.pi/180
1812 1594 azim = azim*numpy.pi/180
1813 1595 cosDirX = numpy.sqrt((1-numpy.cos(zen)**2)/((1+numpy.tan(azim)**2)))
1814 1596 cosDirY = numpy.sqrt(1-numpy.cos(zen)**2-cosDirX**2)
1815 1597
1816 1598 signX = numpy.sign(numpy.cos(azim))
1817 1599 signY = numpy.sign(numpy.sin(azim))
1818 1600
1819 1601 cosDirX = numpy.copysign(cosDirX, signX)
1820 1602 cosDirY = numpy.copysign(cosDirY, signY)
1821 1603 return cosDirX, cosDirY
1822 1604
1823 1605 def __calculateAngles(self, theta_x, theta_y, azimuth):
1824 1606
1825 1607 dir_cosw = numpy.sqrt(1-theta_x**2-theta_y**2)
1826 1608 zenith_arr = numpy.arccos(dir_cosw)
1827 1609 azimuth_arr = numpy.arctan2(theta_x,theta_y) + azimuth*math.pi/180
1828 1610
1829 1611 dir_cosu = numpy.sin(azimuth_arr)*numpy.sin(zenith_arr)
1830 1612 dir_cosv = numpy.cos(azimuth_arr)*numpy.sin(zenith_arr)
1831 1613
1832 1614 return azimuth_arr, zenith_arr, dir_cosu, dir_cosv, dir_cosw
1833 1615
1834 1616 def __calculateMatA(self, dir_cosu, dir_cosv, dir_cosw, horOnly):
1835 1617
1836 1618 #
1837 1619 if horOnly:
1838 1620 A = numpy.c_[dir_cosu,dir_cosv]
1839 1621 else:
1840 1622 A = numpy.c_[dir_cosu,dir_cosv,dir_cosw]
1841 1623 A = numpy.asmatrix(A)
1842 1624 A1 = numpy.linalg.inv(A.transpose()*A)*A.transpose()
1843 1625
1844 1626 return A1
1845 1627
1846 1628 def __correctValues(self, heiRang, phi, velRadial, SNR):
1847 1629 listPhi = phi.tolist()
1848 1630 maxid = listPhi.index(max(listPhi))
1849 1631 minid = listPhi.index(min(listPhi))
1850 1632
1851 1633 rango = range(len(phi))
1852 1634 # rango = numpy.delete(rango,maxid)
1853 1635
1854 1636 heiRang1 = heiRang*math.cos(phi[maxid])
1855 1637 heiRangAux = heiRang*math.cos(phi[minid])
1856 1638 indOut = (heiRang1 < heiRangAux[0]).nonzero()
1857 1639 heiRang1 = numpy.delete(heiRang1,indOut)
1858 1640
1859 1641 velRadial1 = numpy.zeros([len(phi),len(heiRang1)])
1860 1642 SNR1 = numpy.zeros([len(phi),len(heiRang1)])
1861 1643
1862 1644 for i in rango:
1863 1645 x = heiRang*math.cos(phi[i])
1864 1646 y1 = velRadial[i,:]
1865 1647 f1 = interpolate.interp1d(x,y1,kind = 'cubic')
1866 1648
1867 1649 x1 = heiRang1
1868 1650 y11 = f1(x1)
1869 1651
1870 1652 y2 = SNR[i,:]
1871 1653 f2 = interpolate.interp1d(x,y2,kind = 'cubic')
1872 1654 y21 = f2(x1)
1873 1655
1874 1656 velRadial1[i,:] = y11
1875 1657 SNR1[i,:] = y21
1876 1658
1877 1659 return heiRang1, velRadial1, SNR1
1878 1660
1879 1661 def __calculateVelUVW(self, A, velRadial):
1880 1662
1881 1663 #Operacion Matricial
1882 1664 # velUVW = numpy.zeros((velRadial.shape[1],3))
1883 1665 # for ind in range(velRadial.shape[1]):
1884 1666 # velUVW[ind,:] = numpy.dot(A,velRadial[:,ind])
1885 1667 # velUVW = velUVW.transpose()
1886 1668 velUVW = numpy.zeros((A.shape[0],velRadial.shape[1]))
1887 1669 velUVW[:,:] = numpy.dot(A,velRadial)
1888 1670
1889 1671
1890 1672 return velUVW
1891 1673
1892 1674 # def techniqueDBS(self, velRadial0, dirCosx, disrCosy, azimuth, correct, horizontalOnly, heiRang, SNR0):
1893 1675
1894 1676 def techniqueDBS(self, kwargs):
1895 1677 """
1896 1678 Function that implements Doppler Beam Swinging (DBS) technique.
1897 1679
1898 1680 Input: Radial velocities, Direction cosines (x and y) of the Beam, Antenna azimuth,
1899 1681 Direction correction (if necessary), Ranges and SNR
1900 1682
1901 1683 Output: Winds estimation (Zonal, Meridional and Vertical)
1902 1684
1903 1685 Parameters affected: Winds, height range, SNR
1904 1686 """
1905 1687 velRadial0 = kwargs['velRadial']
1906 1688 heiRang = kwargs['heightList']
1907 1689 SNR0 = kwargs['SNR']
1908 1690
1909 1691 if kwargs.has_key('dirCosx') and kwargs.has_key('dirCosy'):
1910 1692 theta_x = numpy.array(kwargs['dirCosx'])
1911 1693 theta_y = numpy.array(kwargs['dirCosy'])
1912 1694 else:
1913 1695 elev = numpy.array(kwargs['elevation'])
1914 1696 azim = numpy.array(kwargs['azimuth'])
1915 1697 theta_x, theta_y = self.__calculateCosDir(elev, azim)
1916 1698 azimuth = kwargs['correctAzimuth']
1917 1699 if kwargs.has_key('horizontalOnly'):
1918 1700 horizontalOnly = kwargs['horizontalOnly']
1919 1701 else: horizontalOnly = False
1920 1702 if kwargs.has_key('correctFactor'):
1921 1703 correctFactor = kwargs['correctFactor']
1922 1704 else: correctFactor = 1
1923 1705 if kwargs.has_key('channelList'):
1924 1706 channelList = kwargs['channelList']
1925 1707 if len(channelList) == 2:
1926 1708 horizontalOnly = True
1927 1709 arrayChannel = numpy.array(channelList)
1928 1710 param = param[arrayChannel,:,:]
1929 1711 theta_x = theta_x[arrayChannel]
1930 1712 theta_y = theta_y[arrayChannel]
1931 1713
1932 1714 azimuth_arr, zenith_arr, dir_cosu, dir_cosv, dir_cosw = self.__calculateAngles(theta_x, theta_y, azimuth)
1933 1715 heiRang1, velRadial1, SNR1 = self.__correctValues(heiRang, zenith_arr, correctFactor*velRadial0, SNR0)
1934 1716 A = self.__calculateMatA(dir_cosu, dir_cosv, dir_cosw, horizontalOnly)
1935 1717
1936 1718 #Calculo de Componentes de la velocidad con DBS
1937 1719 winds = self.__calculateVelUVW(A,velRadial1)
1938 1720
1939 1721 return winds, heiRang1, SNR1
1940 1722
1941 1723 def __calculateDistance(self, posx, posy, pairs_ccf, azimuth = None):
1942 1724
1943 1725 nPairs = len(pairs_ccf)
1944 1726 posx = numpy.asarray(posx)
1945 1727 posy = numpy.asarray(posy)
1946 1728
1947 1729 #Rotacion Inversa para alinear con el azimuth
1948 1730 if azimuth!= None:
1949 1731 azimuth = azimuth*math.pi/180
1950 1732 posx1 = posx*math.cos(azimuth) + posy*math.sin(azimuth)
1951 1733 posy1 = -posx*math.sin(azimuth) + posy*math.cos(azimuth)
1952 1734 else:
1953 1735 posx1 = posx
1954 1736 posy1 = posy
1955 1737
1956 1738 #Calculo de Distancias
1957 1739 distx = numpy.zeros(nPairs)
1958 1740 disty = numpy.zeros(nPairs)
1959 1741 dist = numpy.zeros(nPairs)
1960 1742 ang = numpy.zeros(nPairs)
1961 1743
1962 1744 for i in range(nPairs):
1963 1745 distx[i] = posx1[pairs_ccf[i][1]] - posx1[pairs_ccf[i][0]]
1964 1746 disty[i] = posy1[pairs_ccf[i][1]] - posy1[pairs_ccf[i][0]]
1965 1747 dist[i] = numpy.sqrt(distx[i]**2 + disty[i]**2)
1966 1748 ang[i] = numpy.arctan2(disty[i],distx[i])
1967 1749
1968 1750 return distx, disty, dist, ang
1969 1751 #Calculo de Matrices
1970 1752 # nPairs = len(pairs)
1971 1753 # ang1 = numpy.zeros((nPairs, 2, 1))
1972 1754 # dist1 = numpy.zeros((nPairs, 2, 1))
1973 1755 #
1974 1756 # for j in range(nPairs):
1975 1757 # dist1[j,0,0] = dist[pairs[j][0]]
1976 1758 # dist1[j,1,0] = dist[pairs[j][1]]
1977 1759 # ang1[j,0,0] = ang[pairs[j][0]]
1978 1760 # ang1[j,1,0] = ang[pairs[j][1]]
1979 1761 #
1980 1762 # return distx,disty, dist1,ang1
1981 1763
1982 1764
1983 1765 def __calculateVelVer(self, phase, lagTRange, _lambda):
1984 1766
1985 1767 Ts = lagTRange[1] - lagTRange[0]
1986 1768 velW = -_lambda*phase/(4*math.pi*Ts)
1987 1769
1988 1770 return velW
1989 1771
1990 1772 def __calculateVelHorDir(self, dist, tau1, tau2, ang):
1991 1773 nPairs = tau1.shape[0]
1992 1774 nHeights = tau1.shape[1]
1993 1775 vel = numpy.zeros((nPairs,3,nHeights))
1994 1776 dist1 = numpy.reshape(dist, (dist.size,1))
1995 1777
1996 1778 angCos = numpy.cos(ang)
1997 1779 angSin = numpy.sin(ang)
1998 1780
1999 1781 vel0 = dist1*tau1/(2*tau2**2)
2000 1782 vel[:,0,:] = (vel0*angCos).sum(axis = 1)
2001 1783 vel[:,1,:] = (vel0*angSin).sum(axis = 1)
2002 1784
2003 1785 ind = numpy.where(numpy.isinf(vel))
2004 1786 vel[ind] = numpy.nan
2005 1787
2006 1788 return vel
2007 1789
2008 1790 # def __getPairsAutoCorr(self, pairsList, nChannels):
2009 1791 #
2010 1792 # pairsAutoCorr = numpy.zeros(nChannels, dtype = 'int')*numpy.nan
2011 1793 #
2012 1794 # for l in range(len(pairsList)):
2013 1795 # firstChannel = pairsList[l][0]
2014 1796 # secondChannel = pairsList[l][1]
2015 1797 #
2016 1798 # #Obteniendo pares de Autocorrelacion
2017 1799 # if firstChannel == secondChannel:
2018 1800 # pairsAutoCorr[firstChannel] = int(l)
2019 1801 #
2020 1802 # pairsAutoCorr = pairsAutoCorr.astype(int)
2021 1803 #
2022 1804 # pairsCrossCorr = range(len(pairsList))
2023 1805 # pairsCrossCorr = numpy.delete(pairsCrossCorr,pairsAutoCorr)
2024 1806 #
2025 1807 # return pairsAutoCorr, pairsCrossCorr
2026 1808
2027 1809 # def techniqueSA(self, pairsSelected, pairsList, nChannels, tau, azimuth, _lambda, position_x, position_y, lagTRange, correctFactor):
2028 1810 def techniqueSA(self, kwargs):
2029 1811
2030 1812 """
2031 1813 Function that implements Spaced Antenna (SA) technique.
2032 1814
2033 1815 Input: Radial velocities, Direction cosines (x and y) of the Beam, Antenna azimuth,
2034 1816 Direction correction (if necessary), Ranges and SNR
2035 1817
2036 1818 Output: Winds estimation (Zonal, Meridional and Vertical)
2037 1819
2038 1820 Parameters affected: Winds
2039 1821 """
2040 1822 position_x = kwargs['positionX']
2041 1823 position_y = kwargs['positionY']
2042 1824 azimuth = kwargs['azimuth']
2043 1825
2044 1826 if kwargs.has_key('correctFactor'):
2045 1827 correctFactor = kwargs['correctFactor']
2046 1828 else:
2047 1829 correctFactor = 1
2048 1830
2049 1831 groupList = kwargs['groupList']
2050 1832 pairs_ccf = groupList[1]
2051 1833 tau = kwargs['tau']
2052 1834 _lambda = kwargs['_lambda']
2053 1835
2054 1836 #Cross Correlation pairs obtained
2055 1837 # pairsAutoCorr, pairsCrossCorr = self.__getPairsAutoCorr(pairssList, nChannels)
2056 1838 # pairsArray = numpy.array(pairsList)[pairsCrossCorr]
2057 1839 # pairsSelArray = numpy.array(pairsSelected)
2058 1840 # pairs = []
2059 1841 #
2060 1842 # #Wind estimation pairs obtained
2061 1843 # for i in range(pairsSelArray.shape[0]/2):
2062 1844 # ind1 = numpy.where(numpy.all(pairsArray == pairsSelArray[2*i], axis = 1))[0][0]
2063 1845 # ind2 = numpy.where(numpy.all(pairsArray == pairsSelArray[2*i + 1], axis = 1))[0][0]
2064 1846 # pairs.append((ind1,ind2))
2065 1847
2066 1848 indtau = tau.shape[0]/2
2067 1849 tau1 = tau[:indtau,:]
2068 1850 tau2 = tau[indtau:-1,:]
2069 1851 # tau1 = tau1[pairs,:]
2070 1852 # tau2 = tau2[pairs,:]
2071 1853 phase1 = tau[-1,:]
2072 1854
2073 1855 #---------------------------------------------------------------------
2074 1856 #Metodo Directo
2075 1857 distx, disty, dist, ang = self.__calculateDistance(position_x, position_y, pairs_ccf,azimuth)
2076 1858 winds = self.__calculateVelHorDir(dist, tau1, tau2, ang)
2077 1859 winds = stats.nanmean(winds, axis=0)
2078 1860 #---------------------------------------------------------------------
2079 1861 #Metodo General
2080 1862 # distx, disty, dist = self.calculateDistance(position_x,position_y,pairsCrossCorr, pairsList, azimuth)
2081 1863 # #Calculo Coeficientes de Funcion de Correlacion
2082 1864 # F,G,A,B,H = self.calculateCoef(tau1,tau2,distx,disty,n)
2083 1865 # #Calculo de Velocidades
2084 1866 # winds = self.calculateVelUV(F,G,A,B,H)
2085 1867
2086 1868 #---------------------------------------------------------------------
2087 1869 winds[2,:] = self.__calculateVelVer(phase1, lagTRange, _lambda)
2088 1870 winds = correctFactor*winds
2089 1871 return winds
2090 1872
2091 1873 def __checkTime(self, currentTime, paramInterval, outputInterval):
2092 1874
2093 1875 dataTime = currentTime + paramInterval
2094 1876 deltaTime = dataTime - self.__initime
2095 1877
2096 1878 if deltaTime >= outputInterval or deltaTime < 0:
2097 1879 self.__dataReady = True
2098 1880 return
2099 1881
2100 1882 def techniqueMeteors(self, arrayMeteor, meteorThresh, heightMin, heightMax):
2101 1883 '''
2102 1884 Function that implements winds estimation technique with detected meteors.
2103 1885
2104 1886 Input: Detected meteors, Minimum meteor quantity to wind estimation
2105 1887
2106 1888 Output: Winds estimation (Zonal and Meridional)
2107 1889
2108 1890 Parameters affected: Winds
2109 1891 '''
2110 1892 # print arrayMeteor.shape
2111 1893 #Settings
2112 1894 nInt = (heightMax - heightMin)/2
2113 1895 # print nInt
2114 1896 nInt = int(nInt)
2115 1897 # print nInt
2116 1898 winds = numpy.zeros((2,nInt))*numpy.nan
2117 1899
2118 1900 #Filter errors
2119 1901 error = numpy.where(arrayMeteor[:,-1] == 0)[0]
2120 1902 finalMeteor = arrayMeteor[error,:]
2121 1903
2122 1904 #Meteor Histogram
2123 1905 finalHeights = finalMeteor[:,2]
2124 1906 hist = numpy.histogram(finalHeights, bins = nInt, range = (heightMin,heightMax))
2125 1907 nMeteorsPerI = hist[0]
2126 1908 heightPerI = hist[1]
2127 1909
2128 1910 #Sort of meteors
2129 1911 indSort = finalHeights.argsort()
2130 1912 finalMeteor2 = finalMeteor[indSort,:]
2131 1913
2132 1914 # Calculating winds
2133 1915 ind1 = 0
2134 1916 ind2 = 0
2135 1917
2136 1918 for i in range(nInt):
2137 1919 nMet = nMeteorsPerI[i]
2138 1920 ind1 = ind2
2139 1921 ind2 = ind1 + nMet
2140 1922
2141 1923 meteorAux = finalMeteor2[ind1:ind2,:]
2142 1924
2143 1925 if meteorAux.shape[0] >= meteorThresh:
2144 1926 vel = meteorAux[:, 6]
2145 1927 zen = meteorAux[:, 4]*numpy.pi/180
2146 1928 azim = meteorAux[:, 3]*numpy.pi/180
2147 1929
2148 1930 n = numpy.cos(zen)
2149 1931 # m = (1 - n**2)/(1 - numpy.tan(azim)**2)
2150 1932 # l = m*numpy.tan(azim)
2151 1933 l = numpy.sin(zen)*numpy.sin(azim)
2152 1934 m = numpy.sin(zen)*numpy.cos(azim)
2153 1935
2154 1936 A = numpy.vstack((l, m)).transpose()
2155 1937 A1 = numpy.dot(numpy.linalg.inv( numpy.dot(A.transpose(),A) ),A.transpose())
2156 1938 windsAux = numpy.dot(A1, vel)
2157 1939
2158 1940 winds[0,i] = windsAux[0]
2159 1941 winds[1,i] = windsAux[1]
2160 1942
2161 1943 return winds, heightPerI[:-1]
2162 1944
2163 1945 def techniqueNSM_SA(self, **kwargs):
2164 1946 metArray = kwargs['metArray']
2165 1947 heightList = kwargs['heightList']
2166 1948 timeList = kwargs['timeList']
2167 1949
2168 1950 rx_location = kwargs['rx_location']
2169 1951 groupList = kwargs['groupList']
2170 1952 azimuth = kwargs['azimuth']
2171 1953 dfactor = kwargs['dfactor']
2172 1954 k = kwargs['k']
2173 1955
2174 1956 azimuth1, dist = self.__calculateAzimuth1(rx_location, groupList, azimuth)
2175 1957 d = dist*dfactor
2176 1958 #Phase calculation
2177 1959 metArray1 = self.__getPhaseSlope(metArray, heightList, timeList)
2178 1960
2179 1961 metArray1[:,-2] = metArray1[:,-2]*metArray1[:,2]*1000/(k*d[metArray1[:,1].astype(int)]) #angles into velocities
2180 1962
2181 1963 velEst = numpy.zeros((heightList.size,2))*numpy.nan
2182 1964 azimuth1 = azimuth1*numpy.pi/180
2183 1965
2184 1966 for i in range(heightList.size):
2185 1967 h = heightList[i]
2186 1968 indH = numpy.where((metArray1[:,2] == h)&(numpy.abs(metArray1[:,-2]) < 100))[0]
2187 1969 metHeight = metArray1[indH,:]
2188 1970 if metHeight.shape[0] >= 2:
2189 1971 velAux = numpy.asmatrix(metHeight[:,-2]).T #Radial Velocities
2190 1972 iazim = metHeight[:,1].astype(int)
2191 1973 azimAux = numpy.asmatrix(azimuth1[iazim]).T #Azimuths
2192 1974 A = numpy.hstack((numpy.cos(azimAux),numpy.sin(azimAux)))
2193 1975 A = numpy.asmatrix(A)
2194 1976 A1 = numpy.linalg.pinv(A.transpose()*A)*A.transpose()
2195 1977 velHor = numpy.dot(A1,velAux)
2196 1978
2197 1979 velEst[i,:] = numpy.squeeze(velHor)
2198 1980 return velEst
2199 1981
2200 1982 def __getPhaseSlope(self, metArray, heightList, timeList):
2201 1983 meteorList = []
2202 1984 #utctime sec1 height SNR velRad ph0 ph1 ph2 coh0 coh1 coh2
2203 1985 #Putting back together the meteor matrix
2204 1986 utctime = metArray[:,0]
2205 1987 uniqueTime = numpy.unique(utctime)
2206 1988
2207 1989 phaseDerThresh = 0.5
2208 1990 ippSeconds = timeList[1] - timeList[0]
2209 1991 sec = numpy.where(timeList>1)[0][0]
2210 1992 nPairs = metArray.shape[1] - 6
2211 1993 nHeights = len(heightList)
2212 1994
2213 1995 for t in uniqueTime:
2214 1996 metArray1 = metArray[utctime==t,:]
2215 1997 # phaseDerThresh = numpy.pi/4 #reducir Phase thresh
2216 1998 tmet = metArray1[:,1].astype(int)
2217 1999 hmet = metArray1[:,2].astype(int)
2218 2000
2219 2001 metPhase = numpy.zeros((nPairs, heightList.size, timeList.size - 1))
2220 2002 metPhase[:,:] = numpy.nan
2221 2003 metPhase[:,hmet,tmet] = metArray1[:,6:].T
2222 2004
2223 2005 #Delete short trails
2224 2006 metBool = ~numpy.isnan(metPhase[0,:,:])
2225 2007 heightVect = numpy.sum(metBool, axis = 1)
2226 2008 metBool[heightVect<sec,:] = False
2227 2009 metPhase[:,heightVect<sec,:] = numpy.nan
2228 2010
2229 2011 #Derivative
2230 2012 metDer = numpy.abs(metPhase[:,:,1:] - metPhase[:,:,:-1])
2231 2013 phDerAux = numpy.dstack((numpy.full((nPairs,nHeights,1), False, dtype=bool),metDer > phaseDerThresh))
2232 2014 metPhase[phDerAux] = numpy.nan
2233 2015
2234 2016 #--------------------------METEOR DETECTION -----------------------------------------
2235 2017 indMet = numpy.where(numpy.any(metBool,axis=1))[0]
2236 2018
2237 2019 for p in numpy.arange(nPairs):
2238 2020 phase = metPhase[p,:,:]
2239 2021 phDer = metDer[p,:,:]
2240 2022
2241 2023 for h in indMet:
2242 2024 height = heightList[h]
2243 2025 phase1 = phase[h,:] #82
2244 2026 phDer1 = phDer[h,:]
2245 2027
2246 2028 phase1[~numpy.isnan(phase1)] = numpy.unwrap(phase1[~numpy.isnan(phase1)]) #Unwrap
2247 2029
2248 2030 indValid = numpy.where(~numpy.isnan(phase1))[0]
2249 2031 initMet = indValid[0]
2250 2032 endMet = 0
2251 2033
2252 2034 for i in range(len(indValid)-1):
2253 2035
2254 2036 #Time difference
2255 2037 inow = indValid[i]
2256 2038 inext = indValid[i+1]
2257 2039 idiff = inext - inow
2258 2040 #Phase difference
2259 2041 phDiff = numpy.abs(phase1[inext] - phase1[inow])
2260 2042
2261 2043 if idiff>sec or phDiff>numpy.pi/4 or inext==indValid[-1]: #End of Meteor
2262 2044 sizeTrail = inow - initMet + 1
2263 2045 if sizeTrail>3*sec: #Too short meteors
2264 2046 x = numpy.arange(initMet,inow+1)*ippSeconds
2265 2047 y = phase1[initMet:inow+1]
2266 2048 ynnan = ~numpy.isnan(y)
2267 2049 x = x[ynnan]
2268 2050 y = y[ynnan]
2269 2051 slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
2270 2052 ylin = x*slope + intercept
2271 2053 rsq = r_value**2
2272 2054 if rsq > 0.5:
2273 2055 vel = slope#*height*1000/(k*d)
2274 2056 estAux = numpy.array([utctime,p,height, vel, rsq])
2275 2057 meteorList.append(estAux)
2276 2058 initMet = inext
2277 2059 metArray2 = numpy.array(meteorList)
2278 2060
2279 2061 return metArray2
2280 2062
2281 2063 def __calculateAzimuth1(self, rx_location, pairslist, azimuth0):
2282 2064
2283 2065 azimuth1 = numpy.zeros(len(pairslist))
2284 2066 dist = numpy.zeros(len(pairslist))
2285 2067
2286 2068 for i in range(len(rx_location)):
2287 2069 ch0 = pairslist[i][0]
2288 2070 ch1 = pairslist[i][1]
2289 2071
2290 2072 diffX = rx_location[ch0][0] - rx_location[ch1][0]
2291 2073 diffY = rx_location[ch0][1] - rx_location[ch1][1]
2292 2074 azimuth1[i] = numpy.arctan2(diffY,diffX)*180/numpy.pi
2293 2075 dist[i] = numpy.sqrt(diffX**2 + diffY**2)
2294 2076
2295 2077 azimuth1 -= azimuth0
2296 2078 return azimuth1, dist
2297 2079
2298 2080 def techniqueNSM_DBS(self, **kwargs):
2299 2081 metArray = kwargs['metArray']
2300 2082 heightList = kwargs['heightList']
2301 2083 timeList = kwargs['timeList']
2302 2084 zenithList = kwargs['zenithList']
2303 2085 nChan = numpy.max(cmet) + 1
2304 2086 nHeights = len(heightList)
2305 2087
2306 2088 utctime = metArray[:,0]
2307 2089 cmet = metArray[:,1]
2308 2090 hmet = metArray1[:,3].astype(int)
2309 2091 h1met = heightList[hmet]*zenithList[cmet]
2310 2092 vmet = metArray1[:,5]
2311 2093
2312 2094 for i in range(nHeights - 1):
2313 2095 hmin = heightList[i]
2314 2096 hmax = heightList[i + 1]
2315 2097
2316 2098 vthisH = vmet[(h1met>=hmin) & (h1met<hmax)]
2317 2099
2318 2100
2319 2101
2320 2102 return data_output
2321 2103
2322 2104 def run(self, dataOut, technique, positionY, positionX, azimuth, **kwargs):
2323 2105
2324 2106 param = dataOut.data_param
2325 2107 if dataOut.abscissaList != None:
2326 2108 absc = dataOut.abscissaList[:-1]
2327 2109 noise = dataOut.noise
2328 2110 heightList = dataOut.heightList
2329 2111 SNR = dataOut.data_SNR
2330 2112
2331 2113 if technique == 'DBS':
2332 2114
2333 2115 kwargs['velRadial'] = param[:,1,:] #Radial velocity
2334 2116 kwargs['heightList'] = heightList
2335 2117 kwargs['SNR'] = SNR
2336 2118
2337 2119 dataOut.data_output, dataOut.heightList, dataOut.data_SNR = self.techniqueDBS(kwargs) #DBS Function
2338 2120 dataOut.utctimeInit = dataOut.utctime
2339 2121 dataOut.outputInterval = dataOut.paramInterval
2340 2122
2341 2123 elif technique == 'SA':
2342 2124
2343 2125 #Parameters
2344 2126 # position_x = kwargs['positionX']
2345 2127 # position_y = kwargs['positionY']
2346 2128 # azimuth = kwargs['azimuth']
2347 2129 #
2348 2130 # if kwargs.has_key('crosspairsList'):
2349 2131 # pairs = kwargs['crosspairsList']
2350 2132 # else:
2351 2133 # pairs = None
2352 2134 #
2353 2135 # if kwargs.has_key('correctFactor'):
2354 2136 # correctFactor = kwargs['correctFactor']
2355 2137 # else:
2356 2138 # correctFactor = 1
2357 2139
2358 2140 # tau = dataOut.data_param
2359 2141 # _lambda = dataOut.C/dataOut.frequency
2360 2142 # pairsList = dataOut.groupList
2361 2143 # nChannels = dataOut.nChannels
2362 2144
2363 2145 kwargs['groupList'] = dataOut.groupList
2364 2146 kwargs['tau'] = dataOut.data_param
2365 2147 kwargs['_lambda'] = dataOut.C/dataOut.frequency
2366 2148 # dataOut.data_output = self.techniqueSA(pairs, pairsList, nChannels, tau, azimuth, _lambda, position_x, position_y, absc, correctFactor)
2367 2149 dataOut.data_output = self.techniqueSA(kwargs)
2368 2150 dataOut.utctimeInit = dataOut.utctime
2369 2151 dataOut.outputInterval = dataOut.timeInterval
2370 2152
2371 2153 elif technique == 'Meteors':
2372 2154 dataOut.flagNoData = True
2373 2155 self.__dataReady = False
2374 2156
2375 2157 if kwargs.has_key('nHours'):
2376 2158 nHours = kwargs['nHours']
2377 2159 else:
2378 2160 nHours = 1
2379 2161
2380 2162 if kwargs.has_key('meteorsPerBin'):
2381 2163 meteorThresh = kwargs['meteorsPerBin']
2382 2164 else:
2383 2165 meteorThresh = 6
2384 2166
2385 2167 if kwargs.has_key('hmin'):
2386 2168 hmin = kwargs['hmin']
2387 2169 else: hmin = 70
2388 2170 if kwargs.has_key('hmax'):
2389 2171 hmax = kwargs['hmax']
2390 2172 else: hmax = 110
2391 2173
2392 2174 dataOut.outputInterval = nHours*3600
2393 2175
2394 2176 if self.__isConfig == False:
2395 2177 # self.__initime = dataOut.datatime.replace(minute = 0, second = 0, microsecond = 03)
2396 2178 #Get Initial LTC time
2397 2179 self.__initime = datetime.datetime.utcfromtimestamp(dataOut.utctime)
2398 2180 self.__initime = (self.__initime.replace(minute = 0, second = 0, microsecond = 0) - datetime.datetime(1970, 1, 1)).total_seconds()
2399 2181
2400 2182 self.__isConfig = True
2401 2183
2402 2184 if self.__buffer == None:
2403 2185 self.__buffer = dataOut.data_param
2404 2186 self.__firstdata = copy.copy(dataOut)
2405 2187
2406 2188 else:
2407 2189 self.__buffer = numpy.vstack((self.__buffer, dataOut.data_param))
2408 2190
2409 2191 self.__checkTime(dataOut.utctime, dataOut.paramInterval, dataOut.outputInterval) #Check if the buffer is ready
2410 2192
2411 2193 if self.__dataReady:
2412 2194 dataOut.utctimeInit = self.__initime
2413 2195
2414 2196 self.__initime += dataOut.outputInterval #to erase time offset
2415 2197
2416 2198 dataOut.data_output, dataOut.heightList = self.techniqueMeteors(self.__buffer, meteorThresh, hmin, hmax)
2417 2199 dataOut.flagNoData = False
2418 2200 self.__buffer = None
2419 2201
2420 2202 elif technique == 'Meteors1':
2421 2203 dataOut.flagNoData = True
2422 2204 self.__dataReady = False
2423 2205
2424 2206 if kwargs.has_key('nMins'):
2425 2207 nMins = kwargs['nMins']
2426 2208 else: nMins = 20
2427 2209 if kwargs.has_key('rx_location'):
2428 2210 rx_location = kwargs['rx_location']
2429 2211 else: rx_location = [(0,1),(1,1),(1,0)]
2430 2212 if kwargs.has_key('azimuth'):
2431 2213 azimuth = kwargs['azimuth']
2432 2214 else: azimuth = 51
2433 2215 if kwargs.has_key('dfactor'):
2434 2216 dfactor = kwargs['dfactor']
2435 2217 if kwargs.has_key('mode'):
2436 2218 mode = kwargs['mode']
2437 2219 else: mode = 'SA'
2438 2220
2439 2221 #Borrar luego esto
2440 2222 if dataOut.groupList == None:
2441 2223 dataOut.groupList = [(0,1),(0,2),(1,2)]
2442 2224 groupList = dataOut.groupList
2443 2225 C = 3e8
2444 2226 freq = 50e6
2445 2227 lamb = C/freq
2446 2228 k = 2*numpy.pi/lamb
2447 2229
2448 2230 timeList = dataOut.abscissaList
2449 2231 heightList = dataOut.heightList
2450 2232
2451 2233 if self.__isConfig == False:
2452 2234 dataOut.outputInterval = nMins*60
2453 2235 # self.__initime = dataOut.datatime.replace(minute = 0, second = 0, microsecond = 03)
2454 2236 #Get Initial LTC time
2455 2237 initime = datetime.datetime.utcfromtimestamp(dataOut.utctime)
2456 2238 minuteAux = initime.minute
2457 2239 minuteNew = int(numpy.floor(minuteAux/nMins)*nMins)
2458 2240 self.__initime = (initime.replace(minute = minuteNew, second = 0, microsecond = 0) - datetime.datetime(1970, 1, 1)).total_seconds()
2459 2241
2460 2242 self.__isConfig = True
2461 2243
2462 2244 if self.__buffer == None:
2463 2245 self.__buffer = dataOut.data_param
2464 2246 self.__firstdata = copy.copy(dataOut)
2465 2247
2466 2248 else:
2467 2249 self.__buffer = numpy.vstack((self.__buffer, dataOut.data_param))
2468 2250
2469 2251 self.__checkTime(dataOut.utctime, dataOut.paramInterval, dataOut.outputInterval) #Check if the buffer is ready
2470 2252
2471 2253 if self.__dataReady:
2472 2254 dataOut.utctimeInit = self.__initime
2473 2255 self.__initime += dataOut.outputInterval #to erase time offset
2474 2256
2475 2257 metArray = self.__buffer
2476 2258 if mode == 'SA':
2477 2259 dataOut.data_output = self.techniqueNSM_SA(rx_location=rx_location, groupList=groupList, azimuth=azimuth, dfactor=dfactor, k=k,metArray=metArray, heightList=heightList,timeList=timeList)
2478 2260 elif mode == 'DBS':
2479 2261 dataOut.data_output = self.techniqueNSM_DBS(metArray=metArray,heightList=heightList,timeList=timeList)
2480 2262 dataOut.data_output = dataOut.data_output.T
2481 2263 dataOut.flagNoData = False
2482 2264 self.__buffer = None
2483 2265
2484 2266 return
2485 2267
2486 2268 class EWDriftsEstimation(Operation):
2487 2269
2488 2270 def __init__(self):
2489 2271 Operation.__init__(self)
2490 2272
2491 2273 def __correctValues(self, heiRang, phi, velRadial, SNR):
2492 2274 listPhi = phi.tolist()
2493 2275 maxid = listPhi.index(max(listPhi))
2494 2276 minid = listPhi.index(min(listPhi))
2495 2277
2496 2278 rango = range(len(phi))
2497 2279 # rango = numpy.delete(rango,maxid)
2498 2280
2499 2281 heiRang1 = heiRang*math.cos(phi[maxid])
2500 2282 heiRangAux = heiRang*math.cos(phi[minid])
2501 2283 indOut = (heiRang1 < heiRangAux[0]).nonzero()
2502 2284 heiRang1 = numpy.delete(heiRang1,indOut)
2503 2285
2504 2286 velRadial1 = numpy.zeros([len(phi),len(heiRang1)])
2505 2287 SNR1 = numpy.zeros([len(phi),len(heiRang1)])
2506 2288
2507 2289 for i in rango:
2508 2290 x = heiRang*math.cos(phi[i])
2509 2291 y1 = velRadial[i,:]
2510 2292 f1 = interpolate.interp1d(x,y1,kind = 'cubic')
2511 2293
2512 2294 x1 = heiRang1
2513 2295 y11 = f1(x1)
2514 2296
2515 2297 y2 = SNR[i,:]
2516 2298 f2 = interpolate.interp1d(x,y2,kind = 'cubic')
2517 2299 y21 = f2(x1)
2518 2300
2519 2301 velRadial1[i,:] = y11
2520 2302 SNR1[i,:] = y21
2521 2303
2522 2304 return heiRang1, velRadial1, SNR1
2523 2305
2524 2306 def run(self, dataOut, zenith, zenithCorrection):
2525 2307 heiRang = dataOut.heightList
2526 2308 velRadial = dataOut.data_param[:,3,:]
2527 2309 SNR = dataOut.data_SNR
2528 2310
2529 2311 zenith = numpy.array(zenith)
2530 2312 zenith -= zenithCorrection
2531 2313 zenith *= numpy.pi/180
2532 2314
2533 2315 heiRang1, velRadial1, SNR1 = self.__correctValues(heiRang, numpy.abs(zenith), velRadial, SNR)
2534 2316
2535 2317 alp = zenith[0]
2536 2318 bet = zenith[1]
2537 2319
2538 2320 w_w = velRadial1[0,:]
2539 2321 w_e = velRadial1[1,:]
2540 2322
2541 2323 w = (w_w*numpy.sin(bet) - w_e*numpy.sin(alp))/(numpy.cos(alp)*numpy.sin(bet) - numpy.cos(bet)*numpy.sin(alp))
2542 2324 u = (w_w*numpy.cos(bet) - w_e*numpy.cos(alp))/(numpy.sin(alp)*numpy.cos(bet) - numpy.sin(bet)*numpy.cos(alp))
2543 2325
2544 2326 winds = numpy.vstack((u,w))
2545 2327
2546 2328 dataOut.heightList = heiRang1
2547 2329 dataOut.data_output = winds
2548 2330 dataOut.data_SNR = SNR1
2549 2331
2550 2332 dataOut.utctimeInit = dataOut.utctime
2551 2333 dataOut.outputInterval = dataOut.timeInterval
2552 2334 return
2553 2335
2554 2336 #--------------- Non Specular Meteor ----------------
2555 2337
2556 2338 class NonSpecularMeteorDetection(Operation):
2557 2339
2558 2340 def run(self, mode, SNRthresh=8, phaseDerThresh=0.5, cohThresh=0.8, allData = False):
2559 2341 data_acf = self.dataOut.data_pre[0]
2560 2342 data_ccf = self.dataOut.data_pre[1]
2561 2343
2562 2344 lamb = self.dataOut.C/self.dataOut.frequency
2563 2345 tSamp = self.dataOut.ippSeconds*self.dataOut.nCohInt
2564 2346 paramInterval = self.dataOut.paramInterval
2565 2347
2566 2348 nChannels = data_acf.shape[0]
2567 2349 nLags = data_acf.shape[1]
2568 2350 nProfiles = data_acf.shape[2]
2569 2351 nHeights = self.dataOut.nHeights
2570 2352 nCohInt = self.dataOut.nCohInt
2571 2353 sec = numpy.round(nProfiles/self.dataOut.paramInterval)
2572 2354 heightList = self.dataOut.heightList
2573 2355 ippSeconds = self.dataOut.ippSeconds*self.dataOut.nCohInt*self.dataOut.nAvg
2574 2356 utctime = self.dataOut.utctime
2575 2357
2576 2358 self.dataOut.abscissaList = numpy.arange(0,paramInterval+ippSeconds,ippSeconds)
2577 2359
2578 2360 #------------------------ SNR --------------------------------------
2579 2361 power = data_acf[:,0,:,:].real
2580 2362 noise = numpy.zeros(nChannels)
2581 2363 SNR = numpy.zeros(power.shape)
2582 2364 for i in range(nChannels):
2583 2365 noise[i] = hildebrand_sekhon(power[i,:], nCohInt)
2584 2366 SNR[i] = (power[i]-noise[i])/noise[i]
2585 2367 SNRm = numpy.nanmean(SNR, axis = 0)
2586 2368 SNRdB = 10*numpy.log10(SNR)
2587 2369
2588 2370 if mode == 'SA':
2589 2371 nPairs = data_ccf.shape[0]
2590 2372 #---------------------- Coherence and Phase --------------------------
2591 2373 phase = numpy.zeros(data_ccf[:,0,:,:].shape)
2592 2374 # phase1 = numpy.copy(phase)
2593 2375 coh1 = numpy.zeros(data_ccf[:,0,:,:].shape)
2594 2376
2595 2377 for p in range(nPairs):
2596 2378 ch0 = self.dataOut.groupList[p][0]
2597 2379 ch1 = self.dataOut.groupList[p][1]
2598 2380 ccf = data_ccf[p,0,:,:]/numpy.sqrt(data_acf[ch0,0,:,:]*data_acf[ch1,0,:,:])
2599 2381 phase[p,:,:] = ndimage.median_filter(numpy.angle(ccf), size = (5,1)) #median filter
2600 2382 # phase1[p,:,:] = numpy.angle(ccf) #median filter
2601 2383 coh1[p,:,:] = ndimage.median_filter(numpy.abs(ccf), 5) #median filter
2602 2384 # coh1[p,:,:] = numpy.abs(ccf) #median filter
2603 2385 coh = numpy.nanmax(coh1, axis = 0)
2604 2386 # struc = numpy.ones((5,1))
2605 2387 # coh = ndimage.morphology.grey_dilation(coh, size=(10,1))
2606 2388 #---------------------- Radial Velocity ----------------------------
2607 2389 phaseAux = numpy.mean(numpy.angle(data_acf[:,1,:,:]), axis = 0)
2608 2390 velRad = phaseAux*lamb/(4*numpy.pi*tSamp)
2609 2391
2610 2392 if allData:
2611 2393 boolMetFin = ~numpy.isnan(SNRm)
2612 2394 # coh[:-1,:] = numpy.nanmean(numpy.abs(phase[:,1:,:] - phase[:,:-1,:]),axis=0)
2613 2395 else:
2614 2396 #------------------------ Meteor mask ---------------------------------
2615 2397 # #SNR mask
2616 2398 # boolMet = (SNRdB>SNRthresh)#|(~numpy.isnan(SNRdB))
2617 2399 #
2618 2400 # #Erase small objects
2619 2401 # boolMet1 = self.__erase_small(boolMet, 2*sec, 5)
2620 2402 #
2621 2403 # auxEEJ = numpy.sum(boolMet1,axis=0)
2622 2404 # indOver = auxEEJ>nProfiles*0.8 #Use this later
2623 2405 # indEEJ = numpy.where(indOver)[0]
2624 2406 # indNEEJ = numpy.where(~indOver)[0]
2625 2407 #
2626 2408 # boolMetFin = boolMet1
2627 2409 #
2628 2410 # if indEEJ.size > 0:
2629 2411 # boolMet1[:,indEEJ] = False #Erase heights with EEJ
2630 2412 #
2631 2413 # boolMet2 = coh > cohThresh
2632 2414 # boolMet2 = self.__erase_small(boolMet2, 2*sec,5)
2633 2415 #
2634 2416 # #Final Meteor mask
2635 2417 # boolMetFin = boolMet1|boolMet2
2636 2418
2637 2419 #Coherence mask
2638 2420 boolMet1 = coh > 0.75
2639 2421 struc = numpy.ones((30,1))
2640 2422 boolMet1 = ndimage.morphology.binary_dilation(boolMet1, structure=struc)
2641 2423
2642 2424 #Derivative mask
2643 2425 derPhase = numpy.nanmean(numpy.abs(phase[:,1:,:] - phase[:,:-1,:]),axis=0)
2644 2426 boolMet2 = derPhase < 0.2
2645 2427 # boolMet2 = ndimage.morphology.binary_opening(boolMet2)
2646 2428 # boolMet2 = ndimage.morphology.binary_closing(boolMet2, structure = numpy.ones((10,1)))
2647 2429 boolMet2 = ndimage.median_filter(boolMet2,size=5)
2648 2430 boolMet2 = numpy.vstack((boolMet2,numpy.full((1,nHeights), True, dtype=bool)))
2649 2431 # #Final mask
2650 2432 # boolMetFin = boolMet2
2651 2433 boolMetFin = boolMet1&boolMet2
2652 2434 # boolMetFin = ndimage.morphology.binary_dilation(boolMetFin)
2653 2435 #Creating data_param
2654 2436 coordMet = numpy.where(boolMetFin)
2655 2437
2656 2438 tmet = coordMet[0]
2657 2439 hmet = coordMet[1]
2658 2440
2659 2441 data_param = numpy.zeros((tmet.size, 6 + nPairs))
2660 2442 data_param[:,0] = utctime
2661 2443 data_param[:,1] = tmet
2662 2444 data_param[:,2] = hmet
2663 2445 data_param[:,3] = SNRm[tmet,hmet]
2664 2446 data_param[:,4] = velRad[tmet,hmet]
2665 2447 data_param[:,5] = coh[tmet,hmet]
2666 2448 data_param[:,6:] = phase[:,tmet,hmet].T
2667 2449
2668 2450 elif mode == 'DBS':
2669 2451 self.dataOut.groupList = numpy.arange(nChannels)
2670 2452
2671 2453 #Radial Velocities
2672 2454 # phase = numpy.angle(data_acf[:,1,:,:])
2673 2455 phase = ndimage.median_filter(numpy.angle(data_acf[:,1,:,:]), size = (1,5,1))
2674 2456 velRad = phase*lamb/(4*numpy.pi*tSamp)
2675 2457
2676 2458 #Spectral width
2677 2459 acf1 = ndimage.median_filter(numpy.abs(data_acf[:,1,:,:]), size = (1,5,1))
2678 2460 acf2 = ndimage.median_filter(numpy.abs(data_acf[:,2,:,:]), size = (1,5,1))
2679 2461
2680 2462 spcWidth = (lamb/(2*numpy.sqrt(6)*numpy.pi*tSamp))*numpy.sqrt(numpy.log(acf1/acf2))
2681 2463 # velRad = ndimage.median_filter(velRad, size = (1,5,1))
2682 2464 if allData:
2683 2465 boolMetFin = ~numpy.isnan(SNRdB)
2684 2466 else:
2685 2467 #SNR
2686 2468 boolMet1 = (SNRdB>SNRthresh) #SNR mask
2687 2469 boolMet1 = ndimage.median_filter(boolMet1, size=(1,5,5))
2688 2470
2689 2471 #Radial velocity
2690 2472 boolMet2 = numpy.abs(velRad) < 30
2691 2473 boolMet2 = ndimage.median_filter(boolMet2, (1,5,5))
2692 2474
2693 2475 #Spectral Width
2694 2476 boolMet3 = spcWidth < 30
2695 2477 boolMet3 = ndimage.median_filter(boolMet3, (1,5,5))
2696 2478 # boolMetFin = self.__erase_small(boolMet1, 10,5)
2697 2479 boolMetFin = boolMet1&boolMet2&boolMet3
2698 2480
2699 2481 #Creating data_param
2700 2482 coordMet = numpy.where(boolMetFin)
2701 2483
2702 2484 cmet = coordMet[0]
2703 2485 tmet = coordMet[1]
2704 2486 hmet = coordMet[2]
2705 2487
2706 2488 data_param = numpy.zeros((tmet.size, 7))
2707 2489 data_param[:,0] = utctime
2708 2490 data_param[:,1] = cmet
2709 2491 data_param[:,2] = tmet
2710 2492 data_param[:,3] = hmet
2711 2493 data_param[:,4] = SNR[cmet,tmet,hmet].T
2712 2494 data_param[:,5] = velRad[cmet,tmet,hmet].T
2713 2495 data_param[:,6] = spcWidth[cmet,tmet,hmet].T
2714 2496
2715 2497 # self.dataOut.data_param = data_int
2716 2498 if len(data_param) == 0:
2717 2499 self.dataOut.flagNoData = True
2718 2500 else:
2719 2501 self.dataOut.data_param = data_param
2720 2502
2721 2503 def __erase_small(self, binArray, threshX, threshY):
2722 2504 labarray, numfeat = ndimage.measurements.label(binArray)
2723 2505 binArray1 = numpy.copy(binArray)
2724 2506
2725 2507 for i in range(1,numfeat + 1):
2726 2508 auxBin = (labarray==i)
2727 2509 auxSize = auxBin.sum()
2728 2510
2729 2511 x,y = numpy.where(auxBin)
2730 2512 widthX = x.max() - x.min()
2731 2513 widthY = y.max() - y.min()
2732 2514
2733 2515 #width X: 3 seg -> 12.5*3
2734 2516 #width Y:
2735 2517
2736 2518 if (auxSize < 50) or (widthX < threshX) or (widthY < threshY):
2737 2519 binArray1[auxBin] = False
2738 2520
2739 2521 return binArray1
2740 2522
2741 2523 #--------------- Specular Meteor ----------------
2742 2524
2743 2525 class SMDetection(Operation):
2744 2526 '''
2745 2527 Function DetectMeteors()
2746 2528 Project developed with paper:
2747 2529 HOLDSWORTH ET AL. 2004
2748 2530
2749 2531 Input:
2750 2532 self.dataOut.data_pre
2751 2533
2752 2534 centerReceiverIndex: From the channels, which is the center receiver
2753 2535
2754 2536 hei_ref: Height reference for the Beacon signal extraction
2755 2537 tauindex:
2756 2538 predefinedPhaseShifts: Predefined phase offset for the voltge signals
2757 2539
2758 2540 cohDetection: Whether to user Coherent detection or not
2759 2541 cohDet_timeStep: Coherent Detection calculation time step
2760 2542 cohDet_thresh: Coherent Detection phase threshold to correct phases
2761 2543
2762 2544 noise_timeStep: Noise calculation time step
2763 2545 noise_multiple: Noise multiple to define signal threshold
2764 2546
2765 2547 multDet_timeLimit: Multiple Detection Removal time limit in seconds
2766 2548 multDet_rangeLimit: Multiple Detection Removal range limit in km
2767 2549
2768 2550 phaseThresh: Maximum phase difference between receiver to be consider a meteor
2769 2551 SNRThresh: Minimum SNR threshold of the meteor signal to be consider a meteor
2770 2552
2771 2553 hmin: Minimum Height of the meteor to use it in the further wind estimations
2772 2554 hmax: Maximum Height of the meteor to use it in the further wind estimations
2773 2555 azimuth: Azimuth angle correction
2774 2556
2775 2557 Affected:
2776 2558 self.dataOut.data_param
2777 2559
2778 2560 Rejection Criteria (Errors):
2779 2561 0: No error; analysis OK
2780 2562 1: SNR < SNR threshold
2781 2563 2: angle of arrival (AOA) ambiguously determined
2782 2564 3: AOA estimate not feasible
2783 2565 4: Large difference in AOAs obtained from different antenna baselines
2784 2566 5: echo at start or end of time series
2785 2567 6: echo less than 5 examples long; too short for analysis
2786 2568 7: echo rise exceeds 0.3s
2787 2569 8: echo decay time less than twice rise time
2788 2570 9: large power level before echo
2789 2571 10: large power level after echo
2790 2572 11: poor fit to amplitude for estimation of decay time
2791 2573 12: poor fit to CCF phase variation for estimation of radial drift velocity
2792 2574 13: height unresolvable echo: not valid height within 70 to 110 km
2793 2575 14: height ambiguous echo: more then one possible height within 70 to 110 km
2794 2576 15: radial drift velocity or projected horizontal velocity exceeds 200 m/s
2795 2577 16: oscilatory echo, indicating event most likely not an underdense echo
2796 2578
2797 2579 17: phase difference in meteor Reestimation
2798 2580
2799 2581 Data Storage:
2800 2582 Meteors for Wind Estimation (8):
2801 2583 Utc Time | Range Height
2802 2584 Azimuth Zenith errorCosDir
2803 2585 VelRad errorVelRad
2804 2586 Phase0 Phase1 Phase2 Phase3
2805 2587 TypeError
2806 2588
2807 2589 '''
2808 2590
2809 2591 def run(self, dataOut, hei_ref = None, tauindex = 0,
2810 2592 phaseOffsets = None,
2811 2593 cohDetection = False, cohDet_timeStep = 1, cohDet_thresh = 25,
2812 2594 noise_timeStep = 4, noise_multiple = 4,
2813 2595 multDet_timeLimit = 1, multDet_rangeLimit = 3,
2814 2596 phaseThresh = 20, SNRThresh = 5,
2815 2597 hmin = 50, hmax=150, azimuth = 0,
2816 2598 channelPositions = None) :
2817 2599
2818 2600
2819 2601 #Getting Pairslist
2820 2602 if channelPositions == None:
2821 2603 # channelPositions = [(2.5,0), (0,2.5), (0,0), (0,4.5), (-2,0)] #T
2822 2604 channelPositions = [(4.5,2), (2,4.5), (2,2), (2,0), (0,2)] #Estrella
2823 2605 meteorOps = SMOperations()
2824 2606 pairslist0, distances = meteorOps.getPhasePairs(channelPositions)
2825 2607 heiRang = dataOut.getHeiRange()
2826 2608 #Get Beacon signal - No Beacon signal anymore
2827 2609 # newheis = numpy.where(self.dataOut.heightList>self.dataOut.radarControllerHeaderObj.Taus[tauindex])
2828 2610 #
2829 2611 # if hei_ref != None:
2830 2612 # newheis = numpy.where(self.dataOut.heightList>hei_ref)
2831 2613 #
2832 2614
2833 2615
2834 2616 #****************REMOVING HARDWARE PHASE DIFFERENCES***************
2835 2617 # see if the user put in pre defined phase shifts
2836 2618 voltsPShift = dataOut.data_pre.copy()
2837 2619
2838 2620 # if predefinedPhaseShifts != None:
2839 2621 # hardwarePhaseShifts = numpy.array(predefinedPhaseShifts)*numpy.pi/180
2840 2622 #
2841 2623 # # elif beaconPhaseShifts:
2842 2624 # # #get hardware phase shifts using beacon signal
2843 2625 # # hardwarePhaseShifts = self.__getHardwarePhaseDiff(self.dataOut.data_pre, pairslist, newheis, 10)
2844 2626 # # hardwarePhaseShifts = numpy.insert(hardwarePhaseShifts,centerReceiverIndex,0)
2845 2627 #
2846 2628 # else:
2847 2629 # hardwarePhaseShifts = numpy.zeros(5)
2848 2630 #
2849 2631 # voltsPShift = numpy.zeros((self.dataOut.data_pre.shape[0],self.dataOut.data_pre.shape[1],self.dataOut.data_pre.shape[2]), dtype = 'complex')
2850 2632 # for i in range(self.dataOut.data_pre.shape[0]):
2851 2633 # voltsPShift[i,:,:] = self.__shiftPhase(self.dataOut.data_pre[i,:,:], hardwarePhaseShifts[i])
2852 2634
2853 2635 #******************END OF REMOVING HARDWARE PHASE DIFFERENCES*********
2854 2636
2855 2637 #Remove DC
2856 2638 voltsDC = numpy.mean(voltsPShift,1)
2857 2639 voltsDC = numpy.mean(voltsDC,1)
2858 2640 for i in range(voltsDC.shape[0]):
2859 2641 voltsPShift[i] = voltsPShift[i] - voltsDC[i]
2860 2642
2861 2643 #Don't considerate last heights, theyre used to calculate Hardware Phase Shift
2862 2644 # voltsPShift = voltsPShift[:,:,:newheis[0][0]]
2863 2645
2864 2646 #************ FIND POWER OF DATA W/COH OR NON COH DETECTION (3.4) **********
2865 2647 #Coherent Detection
2866 2648 if cohDetection:
2867 2649 #use coherent detection to get the net power
2868 2650 cohDet_thresh = cohDet_thresh*numpy.pi/180
2869 2651 voltsPShift = self.__coherentDetection(voltsPShift, cohDet_timeStep, dataOut.timeInterval, pairslist0, cohDet_thresh)
2870 2652
2871 2653 #Non-coherent detection!
2872 2654 powerNet = numpy.nansum(numpy.abs(voltsPShift[:,:,:])**2,0)
2873 2655 #********** END OF COH/NON-COH POWER CALCULATION**********************
2874 2656
2875 2657 #********** FIND THE NOISE LEVEL AND POSSIBLE METEORS ****************
2876 2658 #Get noise
2877 2659 noise, noise1 = self.__getNoise(powerNet, noise_timeStep, dataOut.timeInterval)
2878 2660 # noise = self.getNoise1(powerNet, noise_timeStep, self.dataOut.timeInterval)
2879 2661 #Get signal threshold
2880 2662 signalThresh = noise_multiple*noise
2881 2663 #Meteor echoes detection
2882 2664 listMeteors = self.__findMeteors(powerNet, signalThresh)
2883 2665 #******* END OF NOISE LEVEL AND POSSIBLE METEORS CACULATION **********
2884 2666
2885 2667 #************** REMOVE MULTIPLE DETECTIONS (3.5) ***************************
2886 2668 #Parameters
2887 2669 heiRange = dataOut.getHeiRange()
2888 2670 rangeInterval = heiRange[1] - heiRange[0]
2889 2671 rangeLimit = multDet_rangeLimit/rangeInterval
2890 2672 timeLimit = multDet_timeLimit/dataOut.timeInterval
2891 2673 #Multiple detection removals
2892 2674 listMeteors1 = self.__removeMultipleDetections(listMeteors, rangeLimit, timeLimit)
2893 2675 #************ END OF REMOVE MULTIPLE DETECTIONS **********************
2894 2676
2895 2677 #********************* METEOR REESTIMATION (3.7, 3.8, 3.9, 3.10) ********************
2896 2678 #Parameters
2897 2679 phaseThresh = phaseThresh*numpy.pi/180
2898 2680 thresh = [phaseThresh, noise_multiple, SNRThresh]
2899 2681 #Meteor reestimation (Errors N 1, 6, 12, 17)
2900 2682 listMeteors2, listMeteorsPower, listMeteorsVolts = self.__meteorReestimation(listMeteors1, voltsPShift, pairslist0, thresh, noise, dataOut.timeInterval, dataOut.frequency)
2901 2683 # listMeteors2, listMeteorsPower, listMeteorsVolts = self.meteorReestimation3(listMeteors2, listMeteorsPower, listMeteorsVolts, voltsPShift, pairslist, thresh, noise)
2902 2684 #Estimation of decay times (Errors N 7, 8, 11)
2903 2685 listMeteors3 = self.__estimateDecayTime(listMeteors2, listMeteorsPower, dataOut.timeInterval, dataOut.frequency)
2904 2686 #******************* END OF METEOR REESTIMATION *******************
2905 2687
2906 2688 #********************* METEOR PARAMETERS CALCULATION (3.11, 3.12, 3.13) **************************
2907 2689 #Calculating Radial Velocity (Error N 15)
2908 2690 radialStdThresh = 10
2909 2691 listMeteors4 = self.__getRadialVelocity(listMeteors3, listMeteorsVolts, radialStdThresh, pairslist0, dataOut.timeInterval)
2910 2692
2911 2693 if len(listMeteors4) > 0:
2912 2694 #Setting New Array
2913 2695 date = dataOut.utctime
2914 2696 arrayParameters = self.__setNewArrays(listMeteors4, date, heiRang)
2915 2697
2916 2698 #Correcting phase offset
2917 2699 if phaseOffsets != None:
2918 2700 phaseOffsets = numpy.array(phaseOffsets)*numpy.pi/180
2919 2701 arrayParameters[:,8:12] = numpy.unwrap(arrayParameters[:,8:12] + phaseOffsets)
2920 2702
2921 2703 #Second Pairslist
2922 2704 pairsList = []
2923 2705 pairx = (0,1)
2924 2706 pairy = (2,3)
2925 2707 pairsList.append(pairx)
2926 2708 pairsList.append(pairy)
2927 2709
2928 2710 jph = numpy.array([0,0,0,0])
2929 2711 h = (hmin,hmax)
2930 2712 arrayParameters = meteorOps.getMeteorParams(arrayParameters, azimuth, h, pairsList, distances, jph)
2931 2713
2932 2714 # #Calculate AOA (Error N 3, 4)
2933 2715 # #JONES ET AL. 1998
2934 2716 # error = arrayParameters[:,-1]
2935 2717 # AOAthresh = numpy.pi/8
2936 2718 # phases = -arrayParameters[:,9:13]
2937 2719 # arrayParameters[:,4:7], arrayParameters[:,-1] = meteorOps.getAOA(phases, pairsList, error, AOAthresh, azimuth)
2938 2720 #
2939 2721 # #Calculate Heights (Error N 13 and 14)
2940 2722 # error = arrayParameters[:,-1]
2941 2723 # Ranges = arrayParameters[:,2]
2942 2724 # zenith = arrayParameters[:,5]
2943 2725 # arrayParameters[:,3], arrayParameters[:,-1] = meteorOps.getHeights(Ranges, zenith, error, hmin, hmax)
2944 2726 # error = arrayParameters[:,-1]
2945 2727 #********************* END OF PARAMETERS CALCULATION **************************
2946 2728
2947 2729 #***************************+ PASS DATA TO NEXT STEP **********************
2948 2730 # arrayFinal = arrayParameters.reshape((1,arrayParameters.shape[0],arrayParameters.shape[1]))
2949 2731 dataOut.data_param = arrayParameters
2950 2732
2951 2733 if arrayParameters == None:
2952 2734 dataOut.flagNoData = True
2953 2735 else:
2954 2736 dataOut.flagNoData = True
2955 2737
2956 2738 return
2957 2739
2958 2740 def __getHardwarePhaseDiff(self, voltage0, pairslist, newheis, n):
2959 2741
2960 2742 minIndex = min(newheis[0])
2961 2743 maxIndex = max(newheis[0])
2962 2744
2963 2745 voltage = voltage0[:,:,minIndex:maxIndex+1]
2964 2746 nLength = voltage.shape[1]/n
2965 2747 nMin = 0
2966 2748 nMax = 0
2967 2749 phaseOffset = numpy.zeros((len(pairslist),n))
2968 2750
2969 2751 for i in range(n):
2970 2752 nMax += nLength
2971 2753 phaseCCF = -numpy.angle(self.__calculateCCF(voltage[:,nMin:nMax,:], pairslist, [0]))
2972 2754 phaseCCF = numpy.mean(phaseCCF, axis = 2)
2973 2755 phaseOffset[:,i] = phaseCCF.transpose()
2974 2756 nMin = nMax
2975 2757 # phaseDiff, phaseArrival = self.estimatePhaseDifference(voltage, pairslist)
2976 2758
2977 2759 #Remove Outliers
2978 2760 factor = 2
2979 2761 wt = phaseOffset - signal.medfilt(phaseOffset,(1,5))
2980 2762 dw = numpy.std(wt,axis = 1)
2981 2763 dw = dw.reshape((dw.size,1))
2982 2764 ind = numpy.where(numpy.logical_or(wt>dw*factor,wt<-dw*factor))
2983 2765 phaseOffset[ind] = numpy.nan
2984 2766 phaseOffset = stats.nanmean(phaseOffset, axis=1)
2985 2767
2986 2768 return phaseOffset
2987 2769
2988 2770 def __shiftPhase(self, data, phaseShift):
2989 2771 #this will shift the phase of a complex number
2990 2772 dataShifted = numpy.abs(data) * numpy.exp((numpy.angle(data)+phaseShift)*1j)
2991 2773 return dataShifted
2992 2774
2993 2775 def __estimatePhaseDifference(self, array, pairslist):
2994 2776 nChannel = array.shape[0]
2995 2777 nHeights = array.shape[2]
2996 2778 numPairs = len(pairslist)
2997 2779 # phaseCCF = numpy.zeros((nChannel, 5, nHeights))
2998 2780 phaseCCF = numpy.angle(self.__calculateCCF(array, pairslist, [-2,-1,0,1,2]))
2999 2781
3000 2782 #Correct phases
3001 2783 derPhaseCCF = phaseCCF[:,1:,:] - phaseCCF[:,0:-1,:]
3002 2784 indDer = numpy.where(numpy.abs(derPhaseCCF) > numpy.pi)
3003 2785
3004 2786 if indDer[0].shape[0] > 0:
3005 2787 for i in range(indDer[0].shape[0]):
3006 2788 signo = -numpy.sign(derPhaseCCF[indDer[0][i],indDer[1][i],indDer[2][i]])
3007 2789 phaseCCF[indDer[0][i],indDer[1][i]+1:,:] += signo*2*numpy.pi
3008 2790
3009 2791 # for j in range(numSides):
3010 2792 # phaseCCFAux = self.calculateCCF(arrayCenter, arraySides[j,:,:], [-2,1,0,1,2])
3011 2793 # phaseCCF[j,:,:] = numpy.angle(phaseCCFAux)
3012 2794 #
3013 2795 #Linear
3014 2796 phaseInt = numpy.zeros((numPairs,1))
3015 2797 angAllCCF = phaseCCF[:,[0,1,3,4],0]
3016 2798 for j in range(numPairs):
3017 2799 fit = stats.linregress([-2,-1,1,2],angAllCCF[j,:])
3018 2800 phaseInt[j] = fit[1]
3019 2801 #Phase Differences
3020 2802 phaseDiff = phaseInt - phaseCCF[:,2,:]
3021 2803 phaseArrival = phaseInt.reshape(phaseInt.size)
3022 2804
3023 2805 #Dealias
3024 2806 phaseArrival = numpy.angle(numpy.exp(1j*phaseArrival))
3025 2807 # indAlias = numpy.where(phaseArrival > numpy.pi)
3026 2808 # phaseArrival[indAlias] -= 2*numpy.pi
3027 2809 # indAlias = numpy.where(phaseArrival < -numpy.pi)
3028 2810 # phaseArrival[indAlias] += 2*numpy.pi
3029 2811
3030 2812 return phaseDiff, phaseArrival
3031 2813
3032 2814 def __coherentDetection(self, volts, timeSegment, timeInterval, pairslist, thresh):
3033 2815 #this function will run the coherent detection used in Holdworth et al. 2004 and return the net power
3034 2816 #find the phase shifts of each channel over 1 second intervals
3035 2817 #only look at ranges below the beacon signal
3036 2818 numProfPerBlock = numpy.ceil(timeSegment/timeInterval)
3037 2819 numBlocks = int(volts.shape[1]/numProfPerBlock)
3038 2820 numHeights = volts.shape[2]
3039 2821 nChannel = volts.shape[0]
3040 2822 voltsCohDet = volts.copy()
3041 2823
3042 2824 pairsarray = numpy.array(pairslist)
3043 2825 indSides = pairsarray[:,1]
3044 2826 # indSides = numpy.array(range(nChannel))
3045 2827 # indSides = numpy.delete(indSides, indCenter)
3046 2828 #
3047 2829 # listCenter = numpy.array_split(volts[indCenter,:,:], numBlocks, 0)
3048 2830 listBlocks = numpy.array_split(volts, numBlocks, 1)
3049 2831
3050 2832 startInd = 0
3051 2833 endInd = 0
3052 2834
3053 2835 for i in range(numBlocks):
3054 2836 startInd = endInd
3055 2837 endInd = endInd + listBlocks[i].shape[1]
3056 2838
3057 2839 arrayBlock = listBlocks[i]
3058 2840 # arrayBlockCenter = listCenter[i]
3059 2841
3060 2842 #Estimate the Phase Difference
3061 2843 phaseDiff, aux = self.__estimatePhaseDifference(arrayBlock, pairslist)
3062 2844 #Phase Difference RMS
3063 2845 arrayPhaseRMS = numpy.abs(phaseDiff)
3064 2846 phaseRMSaux = numpy.sum(arrayPhaseRMS < thresh,0)
3065 2847 indPhase = numpy.where(phaseRMSaux==4)
3066 2848 #Shifting
3067 2849 if indPhase[0].shape[0] > 0:
3068 2850 for j in range(indSides.size):
3069 2851 arrayBlock[indSides[j],:,indPhase] = self.__shiftPhase(arrayBlock[indSides[j],:,indPhase], phaseDiff[j,indPhase].transpose())
3070 2852 voltsCohDet[:,startInd:endInd,:] = arrayBlock
3071 2853
3072 2854 return voltsCohDet
3073 2855
3074 2856 def __calculateCCF(self, volts, pairslist ,laglist):
3075 2857
3076 2858 nHeights = volts.shape[2]
3077 2859 nPoints = volts.shape[1]
3078 2860 voltsCCF = numpy.zeros((len(pairslist), len(laglist), nHeights),dtype = 'complex')
3079 2861
3080 2862 for i in range(len(pairslist)):
3081 2863 volts1 = volts[pairslist[i][0]]
3082 2864 volts2 = volts[pairslist[i][1]]
3083 2865
3084 2866 for t in range(len(laglist)):
3085 2867 idxT = laglist[t]
3086 2868 if idxT >= 0:
3087 2869 vStacked = numpy.vstack((volts2[idxT:,:],
3088 2870 numpy.zeros((idxT, nHeights),dtype='complex')))
3089 2871 else:
3090 2872 vStacked = numpy.vstack((numpy.zeros((-idxT, nHeights),dtype='complex'),
3091 2873 volts2[:(nPoints + idxT),:]))
3092 2874 voltsCCF[i,t,:] = numpy.sum((numpy.conjugate(volts1)*vStacked),axis=0)
3093 2875
3094 2876 vStacked = None
3095 2877 return voltsCCF
3096 2878
3097 2879 def __getNoise(self, power, timeSegment, timeInterval):
3098 2880 numProfPerBlock = numpy.ceil(timeSegment/timeInterval)
3099 2881 numBlocks = int(power.shape[0]/numProfPerBlock)
3100 2882 numHeights = power.shape[1]
3101 2883
3102 2884 listPower = numpy.array_split(power, numBlocks, 0)
3103 2885 noise = numpy.zeros((power.shape[0], power.shape[1]))
3104 2886 noise1 = numpy.zeros((power.shape[0], power.shape[1]))
3105 2887
3106 2888 startInd = 0
3107 2889 endInd = 0
3108 2890
3109 2891 for i in range(numBlocks): #split por canal
3110 2892 startInd = endInd
3111 2893 endInd = endInd + listPower[i].shape[0]
3112 2894
3113 2895 arrayBlock = listPower[i]
3114 2896 noiseAux = numpy.mean(arrayBlock, 0)
3115 2897 # noiseAux = numpy.median(noiseAux)
3116 2898 # noiseAux = numpy.mean(arrayBlock)
3117 2899 noise[startInd:endInd,:] = noise[startInd:endInd,:] + noiseAux
3118 2900
3119 2901 noiseAux1 = numpy.mean(arrayBlock)
3120 2902 noise1[startInd:endInd,:] = noise1[startInd:endInd,:] + noiseAux1
3121 2903
3122 2904 return noise, noise1
3123 2905
3124 2906 def __findMeteors(self, power, thresh):
3125 2907 nProf = power.shape[0]
3126 2908 nHeights = power.shape[1]
3127 2909 listMeteors = []
3128 2910
3129 2911 for i in range(nHeights):
3130 2912 powerAux = power[:,i]
3131 2913 threshAux = thresh[:,i]
3132 2914
3133 2915 indUPthresh = numpy.where(powerAux > threshAux)[0]
3134 2916 indDNthresh = numpy.where(powerAux <= threshAux)[0]
3135 2917
3136 2918 j = 0
3137 2919
3138 2920 while (j < indUPthresh.size - 2):
3139 2921 if (indUPthresh[j + 2] == indUPthresh[j] + 2):
3140 2922 indDNAux = numpy.where(indDNthresh > indUPthresh[j])
3141 2923 indDNthresh = indDNthresh[indDNAux]
3142 2924
3143 2925 if (indDNthresh.size > 0):
3144 2926 indEnd = indDNthresh[0] - 1
3145 2927 indInit = indUPthresh[j]
3146 2928
3147 2929 meteor = powerAux[indInit:indEnd + 1]
3148 2930 indPeak = meteor.argmax() + indInit
3149 2931 FLA = sum(numpy.conj(meteor)*numpy.hstack((meteor[1:],0)))
3150 2932
3151 2933 listMeteors.append(numpy.array([i,indInit,indPeak,indEnd,FLA])) #CHEQUEAR!!!!!
3152 2934 j = numpy.where(indUPthresh == indEnd)[0] + 1
3153 2935 else: j+=1
3154 2936 else: j+=1
3155 2937
3156 2938 return listMeteors
3157 2939
3158 2940 def __removeMultipleDetections(self,listMeteors, rangeLimit, timeLimit):
3159 2941
3160 2942 arrayMeteors = numpy.asarray(listMeteors)
3161 2943 listMeteors1 = []
3162 2944
3163 2945 while arrayMeteors.shape[0] > 0:
3164 2946 FLAs = arrayMeteors[:,4]
3165 2947 maxFLA = FLAs.argmax()
3166 2948 listMeteors1.append(arrayMeteors[maxFLA,:])
3167 2949
3168 2950 MeteorInitTime = arrayMeteors[maxFLA,1]
3169 2951 MeteorEndTime = arrayMeteors[maxFLA,3]
3170 2952 MeteorHeight = arrayMeteors[maxFLA,0]
3171 2953
3172 2954 #Check neighborhood
3173 2955 maxHeightIndex = MeteorHeight + rangeLimit
3174 2956 minHeightIndex = MeteorHeight - rangeLimit
3175 2957 minTimeIndex = MeteorInitTime - timeLimit
3176 2958 maxTimeIndex = MeteorEndTime + timeLimit
3177 2959
3178 2960 #Check Heights
3179 2961 indHeight = numpy.logical_and(arrayMeteors[:,0] >= minHeightIndex, arrayMeteors[:,0] <= maxHeightIndex)
3180 2962 indTime = numpy.logical_and(arrayMeteors[:,3] >= minTimeIndex, arrayMeteors[:,1] <= maxTimeIndex)
3181 2963 indBoth = numpy.where(numpy.logical_and(indTime,indHeight))
3182 2964
3183 2965 arrayMeteors = numpy.delete(arrayMeteors, indBoth, axis = 0)
3184 2966
3185 2967 return listMeteors1
3186 2968
3187 2969 def __meteorReestimation(self, listMeteors, volts, pairslist, thresh, noise, timeInterval,frequency):
3188 2970 numHeights = volts.shape[2]
3189 2971 nChannel = volts.shape[0]
3190 2972
3191 2973 thresholdPhase = thresh[0]
3192 2974 thresholdNoise = thresh[1]
3193 2975 thresholdDB = float(thresh[2])
3194 2976
3195 2977 thresholdDB1 = 10**(thresholdDB/10)
3196 2978 pairsarray = numpy.array(pairslist)
3197 2979 indSides = pairsarray[:,1]
3198 2980
3199 2981 pairslist1 = list(pairslist)
3200 2982 pairslist1.append((0,1))
3201 2983 pairslist1.append((3,4))
3202 2984
3203 2985 listMeteors1 = []
3204 2986 listPowerSeries = []
3205 2987 listVoltageSeries = []
3206 2988 #volts has the war data
3207 2989
3208 2990 if frequency == 30e6:
3209 2991 timeLag = 45*10**-3
3210 2992 else:
3211 2993 timeLag = 15*10**-3
3212 2994 lag = numpy.ceil(timeLag/timeInterval)
3213 2995
3214 2996 for i in range(len(listMeteors)):
3215 2997
3216 2998 ###################### 3.6 - 3.7 PARAMETERS REESTIMATION #########################
3217 2999 meteorAux = numpy.zeros(16)
3218 3000
3219 3001 #Loading meteor Data (mHeight, mStart, mPeak, mEnd)
3220 3002 mHeight = listMeteors[i][0]
3221 3003 mStart = listMeteors[i][1]
3222 3004 mPeak = listMeteors[i][2]
3223 3005 mEnd = listMeteors[i][3]
3224 3006
3225 3007 #get the volt data between the start and end times of the meteor
3226 3008 meteorVolts = volts[:,mStart:mEnd+1,mHeight]
3227 3009 meteorVolts = meteorVolts.reshape(meteorVolts.shape[0], meteorVolts.shape[1], 1)
3228 3010
3229 3011 #3.6. Phase Difference estimation
3230 3012 phaseDiff, aux = self.__estimatePhaseDifference(meteorVolts, pairslist)
3231 3013
3232 3014 #3.7. Phase difference removal & meteor start, peak and end times reestimated
3233 3015 #meteorVolts0.- all Channels, all Profiles
3234 3016 meteorVolts0 = volts[:,:,mHeight]
3235 3017 meteorThresh = noise[:,mHeight]*thresholdNoise
3236 3018 meteorNoise = noise[:,mHeight]
3237 3019 meteorVolts0[indSides,:] = self.__shiftPhase(meteorVolts0[indSides,:], phaseDiff) #Phase Shifting
3238 3020 powerNet0 = numpy.nansum(numpy.abs(meteorVolts0)**2, axis = 0) #Power
3239 3021
3240 3022 #Times reestimation
3241 3023 mStart1 = numpy.where(powerNet0[:mPeak] < meteorThresh[:mPeak])[0]
3242 3024 if mStart1.size > 0:
3243 3025 mStart1 = mStart1[-1] + 1
3244 3026
3245 3027 else:
3246 3028 mStart1 = mPeak
3247 3029
3248 3030 mEnd1 = numpy.where(powerNet0[mPeak:] < meteorThresh[mPeak:])[0][0] + mPeak - 1
3249 3031 mEndDecayTime1 = numpy.where(powerNet0[mPeak:] < meteorNoise[mPeak:])[0]
3250 3032 if mEndDecayTime1.size == 0:
3251 3033 mEndDecayTime1 = powerNet0.size
3252 3034 else:
3253 3035 mEndDecayTime1 = mEndDecayTime1[0] + mPeak - 1
3254 3036 # mPeak1 = meteorVolts0[mStart1:mEnd1 + 1].argmax()
3255 3037
3256 3038 #meteorVolts1.- all Channels, from start to end
3257 3039 meteorVolts1 = meteorVolts0[:,mStart1:mEnd1 + 1]
3258 3040 meteorVolts2 = meteorVolts0[:,mPeak + lag:mEnd1 + 1]
3259 3041 if meteorVolts2.shape[1] == 0:
3260 3042 meteorVolts2 = meteorVolts0[:,mPeak:mEnd1 + 1]
3261 3043 meteorVolts1 = meteorVolts1.reshape(meteorVolts1.shape[0], meteorVolts1.shape[1], 1)
3262 3044 meteorVolts2 = meteorVolts2.reshape(meteorVolts2.shape[0], meteorVolts2.shape[1], 1)
3263 3045 ##################### END PARAMETERS REESTIMATION #########################
3264 3046
3265 3047 ##################### 3.8 PHASE DIFFERENCE REESTIMATION ########################
3266 3048 # if mEnd1 - mStart1 > 4: #Error Number 6: echo less than 5 samples long; too short for analysis
3267 3049 if meteorVolts2.shape[1] > 0:
3268 3050 #Phase Difference re-estimation
3269 3051 phaseDiff1, phaseDiffint = self.__estimatePhaseDifference(meteorVolts2, pairslist1) #Phase Difference Estimation
3270 3052 # phaseDiff1, phaseDiffint = self.estimatePhaseDifference(meteorVolts2, pairslist)
3271 3053 meteorVolts2 = meteorVolts2.reshape(meteorVolts2.shape[0], meteorVolts2.shape[1])
3272 3054 phaseDiff11 = numpy.reshape(phaseDiff1, (phaseDiff1.shape[0],1))
3273 3055 meteorVolts2[indSides,:] = self.__shiftPhase(meteorVolts2[indSides,:], phaseDiff11[0:4]) #Phase Shifting
3274 3056
3275 3057 #Phase Difference RMS
3276 3058 phaseRMS1 = numpy.sqrt(numpy.mean(numpy.square(phaseDiff1)))
3277 3059 powerNet1 = numpy.nansum(numpy.abs(meteorVolts1[:,:])**2,0)
3278 3060 #Data from Meteor
3279 3061 mPeak1 = powerNet1.argmax() + mStart1
3280 3062 mPeakPower1 = powerNet1.max()
3281 3063 noiseAux = sum(noise[mStart1:mEnd1 + 1,mHeight])
3282 3064 mSNR1 = (sum(powerNet1)-noiseAux)/noiseAux
3283 3065 Meteor1 = numpy.array([mHeight, mStart1, mPeak1, mEnd1, mPeakPower1, mSNR1, phaseRMS1])
3284 3066 Meteor1 = numpy.hstack((Meteor1,phaseDiffint))
3285 3067 PowerSeries = powerNet0[mStart1:mEndDecayTime1 + 1]
3286 3068 #Vectorize
3287 3069 meteorAux[0:7] = [mHeight, mStart1, mPeak1, mEnd1, mPeakPower1, mSNR1, phaseRMS1]
3288 3070 meteorAux[7:11] = phaseDiffint[0:4]
3289 3071
3290 3072 #Rejection Criterions
3291 3073 if phaseRMS1 > thresholdPhase: #Error Number 17: Phase variation
3292 3074 meteorAux[-1] = 17
3293 3075 elif mSNR1 < thresholdDB1: #Error Number 1: SNR < threshold dB
3294 3076 meteorAux[-1] = 1
3295 3077
3296 3078
3297 3079 else:
3298 3080 meteorAux[0:4] = [mHeight, mStart, mPeak, mEnd]
3299 3081 meteorAux[-1] = 6 #Error Number 6: echo less than 5 samples long; too short for analysis
3300 3082 PowerSeries = 0
3301 3083
3302 3084 listMeteors1.append(meteorAux)
3303 3085 listPowerSeries.append(PowerSeries)
3304 3086 listVoltageSeries.append(meteorVolts1)
3305 3087
3306 3088 return listMeteors1, listPowerSeries, listVoltageSeries
3307 3089
3308 3090 def __estimateDecayTime(self, listMeteors, listPower, timeInterval, frequency):
3309 3091
3310 3092 threshError = 10
3311 3093 #Depending if it is 30 or 50 MHz
3312 3094 if frequency == 30e6:
3313 3095 timeLag = 45*10**-3
3314 3096 else:
3315 3097 timeLag = 15*10**-3
3316 3098 lag = numpy.ceil(timeLag/timeInterval)
3317 3099
3318 3100 listMeteors1 = []
3319 3101
3320 3102 for i in range(len(listMeteors)):
3321 3103 meteorPower = listPower[i]
3322 3104 meteorAux = listMeteors[i]
3323 3105
3324 3106 if meteorAux[-1] == 0:
3325 3107
3326 3108 try:
3327 3109 indmax = meteorPower.argmax()
3328 3110 indlag = indmax + lag
3329 3111
3330 3112 y = meteorPower[indlag:]
3331 3113 x = numpy.arange(0, y.size)*timeLag
3332 3114
3333 3115 #first guess
3334 3116 a = y[0]
3335 3117 tau = timeLag
3336 3118 #exponential fit
3337 3119 popt, pcov = optimize.curve_fit(self.__exponential_function, x, y, p0 = [a, tau])
3338 3120 y1 = self.__exponential_function(x, *popt)
3339 3121 #error estimation
3340 3122 error = sum((y - y1)**2)/(numpy.var(y)*(y.size - popt.size))
3341 3123
3342 3124 decayTime = popt[1]
3343 3125 riseTime = indmax*timeInterval
3344 3126 meteorAux[11:13] = [decayTime, error]
3345 3127
3346 3128 #Table items 7, 8 and 11
3347 3129 if (riseTime > 0.3): #Number 7: Echo rise exceeds 0.3s
3348 3130 meteorAux[-1] = 7
3349 3131 elif (decayTime < 2*riseTime) : #Number 8: Echo decay time less than than twice rise time
3350 3132 meteorAux[-1] = 8
3351 3133 if (error > threshError): #Number 11: Poor fit to amplitude for estimation of decay time
3352 3134 meteorAux[-1] = 11
3353 3135
3354 3136
3355 3137 except:
3356 3138 meteorAux[-1] = 11
3357 3139
3358 3140
3359 3141 listMeteors1.append(meteorAux)
3360 3142
3361 3143 return listMeteors1
3362 3144
3363 3145 #Exponential Function
3364 3146
3365 3147 def __exponential_function(self, x, a, tau):
3366 3148 y = a*numpy.exp(-x/tau)
3367 3149 return y
3368 3150
3369 3151 def __getRadialVelocity(self, listMeteors, listVolts, radialStdThresh, pairslist, timeInterval):
3370 3152
3371 3153 pairslist1 = list(pairslist)
3372 3154 pairslist1.append((0,1))
3373 3155 pairslist1.append((3,4))
3374 3156 numPairs = len(pairslist1)
3375 3157 #Time Lag
3376 3158 timeLag = 45*10**-3
3377 3159 c = 3e8
3378 3160 lag = numpy.ceil(timeLag/timeInterval)
3379 3161 freq = 30e6
3380 3162
3381 3163 listMeteors1 = []
3382 3164
3383 3165 for i in range(len(listMeteors)):
3384 3166 meteorAux = listMeteors[i]
3385 3167 if meteorAux[-1] == 0:
3386 3168 mStart = listMeteors[i][1]
3387 3169 mPeak = listMeteors[i][2]
3388 3170 mLag = mPeak - mStart + lag
3389 3171
3390 3172 #get the volt data between the start and end times of the meteor
3391 3173 meteorVolts = listVolts[i]
3392 3174 meteorVolts = meteorVolts.reshape(meteorVolts.shape[0], meteorVolts.shape[1], 1)
3393 3175
3394 3176 #Get CCF
3395 3177 allCCFs = self.__calculateCCF(meteorVolts, pairslist1, [-2,-1,0,1,2])
3396 3178
3397 3179 #Method 2
3398 3180 slopes = numpy.zeros(numPairs)
3399 3181 time = numpy.array([-2,-1,1,2])*timeInterval
3400 3182 angAllCCF = numpy.angle(allCCFs[:,[0,1,3,4],0])
3401 3183
3402 3184 #Correct phases
3403 3185 derPhaseCCF = angAllCCF[:,1:] - angAllCCF[:,0:-1]
3404 3186 indDer = numpy.where(numpy.abs(derPhaseCCF) > numpy.pi)
3405 3187
3406 3188 if indDer[0].shape[0] > 0:
3407 3189 for i in range(indDer[0].shape[0]):
3408 3190 signo = -numpy.sign(derPhaseCCF[indDer[0][i],indDer[1][i]])
3409 3191 angAllCCF[indDer[0][i],indDer[1][i]+1:] += signo*2*numpy.pi
3410 3192
3411 3193 # fit = scipy.stats.linregress(numpy.array([-2,-1,1,2])*timeInterval, numpy.array([phaseLagN2s[i],phaseLagN1s[i],phaseLag1s[i],phaseLag2s[i]]))
3412 3194 for j in range(numPairs):
3413 3195 fit = stats.linregress(time, angAllCCF[j,:])
3414 3196 slopes[j] = fit[0]
3415 3197
3416 3198 #Remove Outlier
3417 3199 # indOut = numpy.argmax(numpy.abs(slopes - numpy.mean(slopes)))
3418 3200 # slopes = numpy.delete(slopes,indOut)
3419 3201 # indOut = numpy.argmax(numpy.abs(slopes - numpy.mean(slopes)))
3420 3202 # slopes = numpy.delete(slopes,indOut)
3421 3203
3422 3204 radialVelocity = -numpy.mean(slopes)*(0.25/numpy.pi)*(c/freq)
3423 3205 radialError = numpy.std(slopes)*(0.25/numpy.pi)*(c/freq)
3424 3206 meteorAux[-2] = radialError
3425 3207 meteorAux[-3] = radialVelocity
3426 3208
3427 3209 #Setting Error
3428 3210 #Number 15: Radial Drift velocity or projected horizontal velocity exceeds 200 m/s
3429 3211 if numpy.abs(radialVelocity) > 200:
3430 3212 meteorAux[-1] = 15
3431 3213 #Number 12: Poor fit to CCF variation for estimation of radial drift velocity
3432 3214 elif radialError > radialStdThresh:
3433 3215 meteorAux[-1] = 12
3434 3216
3435 3217 listMeteors1.append(meteorAux)
3436 3218 return listMeteors1
3437 3219
3438 3220 def __setNewArrays(self, listMeteors, date, heiRang):
3439 3221
3440 3222 #New arrays
3441 3223 arrayMeteors = numpy.array(listMeteors)
3442 3224 arrayParameters = numpy.zeros((len(listMeteors), 13))
3443 3225
3444 3226 #Date inclusion
3445 3227 # date = re.findall(r'\((.*?)\)', date)
3446 3228 # date = date[0].split(',')
3447 3229 # date = map(int, date)
3448 3230 #
3449 3231 # if len(date)<6:
3450 3232 # date.append(0)
3451 3233 #
3452 3234 # date = [date[0]*10000 + date[1]*100 + date[2], date[3]*10000 + date[4]*100 + date[5]]
3453 3235 # arrayDate = numpy.tile(date, (len(listMeteors), 1))
3454 3236 arrayDate = numpy.tile(date, (len(listMeteors)))
3455 3237
3456 3238 #Meteor array
3457 3239 # arrayMeteors[:,0] = heiRang[arrayMeteors[:,0].astype(int)]
3458 3240 # arrayMeteors = numpy.hstack((arrayDate, arrayMeteors))
3459 3241
3460 3242 #Parameters Array
3461 3243 arrayParameters[:,0] = arrayDate #Date
3462 3244 arrayParameters[:,1] = heiRang[arrayMeteors[:,0].astype(int)] #Range
3463 3245 arrayParameters[:,6:8] = arrayMeteors[:,-3:-1] #Radial velocity and its error
3464 3246 arrayParameters[:,8:12] = arrayMeteors[:,7:11] #Phases
3465 3247 arrayParameters[:,-1] = arrayMeteors[:,-1] #Error
3466 3248
3467 3249
3468 3250 return arrayParameters
3469 3251
3470 3252 class CorrectSMPhases(Operation):
3471 3253
3472 3254 def run(self, dataOut, phaseOffsets, hmin = 50, hmax = 150, azimuth = 45, channelPositions = None):
3473 3255
3474 3256 arrayParameters = dataOut.data_param
3475 3257 pairsList = []
3476 3258 pairx = (0,1)
3477 3259 pairy = (2,3)
3478 3260 pairsList.append(pairx)
3479 3261 pairsList.append(pairy)
3480 3262 jph = numpy.zeros(4)
3481 3263
3482 3264 phaseOffsets = numpy.array(phaseOffsets)*numpy.pi/180
3483 3265 # arrayParameters[:,8:12] = numpy.unwrap(arrayParameters[:,8:12] + phaseOffsets)
3484 3266 arrayParameters[:,8:12] = numpy.angle(numpy.exp(1j*(arrayParameters[:,8:12] + phaseOffsets)))
3485 3267
3486 3268 meteorOps = SMOperations()
3487 3269 if channelPositions == None:
3488 3270 # channelPositions = [(2.5,0), (0,2.5), (0,0), (0,4.5), (-2,0)] #T
3489 3271 channelPositions = [(4.5,2), (2,4.5), (2,2), (2,0), (0,2)] #Estrella
3490 3272
3491 3273 pairslist0, distances = meteorOps.getPhasePairs(channelPositions)
3492 3274 h = (hmin,hmax)
3493 3275
3494 3276 arrayParameters = meteorOps.getMeteorParams(arrayParameters, azimuth, h, pairsList, distances, jph)
3495 3277
3496 3278 dataOut.data_param = arrayParameters
3497 3279 return
3498 3280
3499 3281 class SMPhaseCalibration(Operation):
3500 3282
3501 3283 __buffer = None
3502 3284
3503 3285 __initime = None
3504 3286
3505 3287 __dataReady = False
3506 3288
3507 3289 __isConfig = False
3508 3290
3509 3291 def __checkTime(self, currentTime, initTime, paramInterval, outputInterval):
3510 3292
3511 3293 dataTime = currentTime + paramInterval
3512 3294 deltaTime = dataTime - initTime
3513 3295
3514 3296 if deltaTime >= outputInterval or deltaTime < 0:
3515 3297 return True
3516 3298
3517 3299 return False
3518 3300
3519 3301 def __getGammas(self, pairs, d, phases):
3520 3302 gammas = numpy.zeros(2)
3521 3303
3522 3304 for i in range(len(pairs)):
3523 3305
3524 3306 pairi = pairs[i]
3525 3307
3526 3308 phip3 = phases[:,pairi[1]]
3527 3309 d3 = d[pairi[1]]
3528 3310 phip2 = phases[:,pairi[0]]
3529 3311 d2 = d[pairi[0]]
3530 3312 #Calculating gamma
3531 3313 # jdcos = alp1/(k*d1)
3532 3314 # jgamma = numpy.angle(numpy.exp(1j*(d0*alp1/d1 - alp0)))
3533 3315 jgamma = -phip2*d3/d2 - phip3
3534 3316 jgamma = numpy.angle(numpy.exp(1j*jgamma))
3535 3317 # jgamma[jgamma>numpy.pi] -= 2*numpy.pi
3536 3318 # jgamma[jgamma<-numpy.pi] += 2*numpy.pi
3537 3319
3538 3320 #Revised distribution
3539 3321 jgammaArray = numpy.hstack((jgamma,jgamma+0.5*numpy.pi,jgamma-0.5*numpy.pi))
3540 3322
3541 3323 #Histogram
3542 3324 nBins = 64.0
3543 3325 rmin = -0.5*numpy.pi
3544 3326 rmax = 0.5*numpy.pi
3545 3327 phaseHisto = numpy.histogram(jgammaArray, bins=nBins, range=(rmin,rmax))
3546 3328
3547 3329 meteorsY = phaseHisto[0]
3548 3330 phasesX = phaseHisto[1][:-1]
3549 3331 width = phasesX[1] - phasesX[0]
3550 3332 phasesX += width/2
3551 3333
3552 3334 #Gaussian aproximation
3553 3335 bpeak = meteorsY.argmax()
3554 3336 peak = meteorsY.max()
3555 3337 jmin = bpeak - 5
3556 3338 jmax = bpeak + 5 + 1
3557 3339
3558 3340 if jmin<0:
3559 3341 jmin = 0
3560 3342 jmax = 6
3561 3343 elif jmax > meteorsY.size:
3562 3344 jmin = meteorsY.size - 6
3563 3345 jmax = meteorsY.size
3564 3346
3565 3347 x0 = numpy.array([peak,bpeak,50])
3566 3348 coeff = optimize.leastsq(self.__residualFunction, x0, args=(meteorsY[jmin:jmax], phasesX[jmin:jmax]))
3567 3349
3568 3350 #Gammas
3569 3351 gammas[i] = coeff[0][1]
3570 3352
3571 3353 return gammas
3572 3354
3573 3355 def __residualFunction(self, coeffs, y, t):
3574 3356
3575 3357 return y - self.__gauss_function(t, coeffs)
3576 3358
3577 3359 def __gauss_function(self, t, coeffs):
3578 3360
3579 3361 return coeffs[0]*numpy.exp(-0.5*((t - coeffs[1]) / coeffs[2])**2)
3580 3362
3581 3363 def __getPhases(self, azimuth, h, pairsList, d, gammas, meteorsArray):
3582 3364 meteorOps = SMOperations()
3583 3365 nchan = 4
3584 3366 pairx = pairsList[0]
3585 3367 pairy = pairsList[1]
3586 3368 center_xangle = 0
3587 3369 center_yangle = 0
3588 3370 range_angle = numpy.array([10*numpy.pi,numpy.pi,numpy.pi/2,numpy.pi/4])
3589 3371 ntimes = len(range_angle)
3590 3372
3591 3373 nstepsx = 20.0
3592 3374 nstepsy = 20.0
3593 3375
3594 3376 for iz in range(ntimes):
3595 3377 min_xangle = -range_angle[iz]/2 + center_xangle
3596 3378 max_xangle = range_angle[iz]/2 + center_xangle
3597 3379 min_yangle = -range_angle[iz]/2 + center_yangle
3598 3380 max_yangle = range_angle[iz]/2 + center_yangle
3599 3381
3600 3382 inc_x = (max_xangle-min_xangle)/nstepsx
3601 3383 inc_y = (max_yangle-min_yangle)/nstepsy
3602 3384
3603 3385 alpha_y = numpy.arange(nstepsy)*inc_y + min_yangle
3604 3386 alpha_x = numpy.arange(nstepsx)*inc_x + min_xangle
3605 3387 penalty = numpy.zeros((nstepsx,nstepsy))
3606 3388 jph_array = numpy.zeros((nchan,nstepsx,nstepsy))
3607 3389 jph = numpy.zeros(nchan)
3608 3390
3609 3391 # Iterations looking for the offset
3610 3392 for iy in range(int(nstepsy)):
3611 3393 for ix in range(int(nstepsx)):
3612 3394 jph[pairy[1]] = alpha_y[iy]
3613 3395 jph[pairy[0]] = -gammas[1] - alpha_y[iy]*d[pairy[1]]/d[pairy[0]]
3614 3396
3615 3397 jph[pairx[1]] = alpha_x[ix]
3616 3398 jph[pairx[0]] = -gammas[0] - alpha_x[ix]*d[pairx[1]]/d[pairx[0]]
3617 3399
3618 3400 jph_array[:,ix,iy] = jph
3619 3401
3620 3402 meteorsArray1 = meteorOps.getMeteorParams(meteorsArray, azimuth, h, pairsList, d, jph)
3621 3403 error = meteorsArray1[:,-1]
3622 3404 ind1 = numpy.where(error==0)[0]
3623 3405 penalty[ix,iy] = ind1.size
3624 3406
3625 3407 i,j = numpy.unravel_index(penalty.argmax(), penalty.shape)
3626 3408 phOffset = jph_array[:,i,j]
3627 3409
3628 3410 center_xangle = phOffset[pairx[1]]
3629 3411 center_yangle = phOffset[pairy[1]]
3630 3412
3631 3413 phOffset = numpy.angle(numpy.exp(1j*jph_array[:,i,j]))
3632 3414 phOffset = phOffset*180/numpy.pi
3633 3415 return phOffset
3634 3416
3635 3417
3636 3418 def run(self, dataOut, hmin, hmax, channelPositions=None, nHours = 1):
3637 3419
3638 3420 dataOut.flagNoData = True
3639 3421 self.__dataReady = False
3640 3422 dataOut.outputInterval = nHours*3600
3641 3423
3642 3424 if self.__isConfig == False:
3643 3425 # self.__initime = dataOut.datatime.replace(minute = 0, second = 0, microsecond = 03)
3644 3426 #Get Initial LTC time
3645 3427 self.__initime = datetime.datetime.utcfromtimestamp(dataOut.utctime)
3646 3428 self.__initime = (self.__initime.replace(minute = 0, second = 0, microsecond = 0) - datetime.datetime(1970, 1, 1)).total_seconds()
3647 3429
3648 3430 self.__isConfig = True
3649 3431
3650 3432 if self.__buffer == None:
3651 3433 self.__buffer = dataOut.data_param.copy()
3652 3434
3653 3435 else:
3654 3436 self.__buffer = numpy.vstack((self.__buffer, dataOut.data_param))
3655 3437
3656 3438 self.__dataReady = self.__checkTime(dataOut.utctime, self.__initime, dataOut.paramInterval, dataOut.outputInterval) #Check if the buffer is ready
3657 3439
3658 3440 if self.__dataReady:
3659 3441 dataOut.utctimeInit = self.__initime
3660 3442 self.__initime += dataOut.outputInterval #to erase time offset
3661 3443
3662 3444 freq = dataOut.frequency
3663 3445 c = dataOut.C #m/s
3664 3446 lamb = c/freq
3665 3447 k = 2*numpy.pi/lamb
3666 3448 azimuth = 0
3667 3449 h = (hmin, hmax)
3668 3450 pairs = ((0,1),(2,3))
3669 3451
3670 3452 if channelPositions == None:
3671 3453 # channelPositions = [(2.5,0), (0,2.5), (0,0), (0,4.5), (-2,0)] #T
3672 3454 channelPositions = [(4.5,2), (2,4.5), (2,2), (2,0), (0,2)] #Estrella
3673 3455 meteorOps = SMOperations()
3674 3456 pairslist0, distances = meteorOps.getPhasePairs(channelPositions)
3675 3457
3676 3458 # distances1 = [-distances[0]*lamb, distances[1]*lamb, -distances[2]*lamb, distances[3]*lamb]
3677 3459
3678 3460 meteorsArray = self.__buffer
3679 3461 error = meteorsArray[:,-1]
3680 3462 boolError = (error==0)|(error==3)|(error==4)|(error==13)|(error==14)
3681 3463 ind1 = numpy.where(boolError)[0]
3682 3464 meteorsArray = meteorsArray[ind1,:]
3683 3465 meteorsArray[:,-1] = 0
3684 3466 phases = meteorsArray[:,8:12]
3685 3467
3686 3468 #Calculate Gammas
3687 3469 gammas = self.__getGammas(pairs, distances, phases)
3688 3470 # gammas = numpy.array([-21.70409463,45.76935864])*numpy.pi/180
3689 3471 #Calculate Phases
3690 3472 phasesOff = self.__getPhases(azimuth, h, pairs, distances, gammas, meteorsArray)
3691 3473 phasesOff = phasesOff.reshape((1,phasesOff.size))
3692 3474 dataOut.data_output = -phasesOff
3693 3475 dataOut.flagNoData = False
3694 3476 self.__buffer = None
3695 3477
3696 3478
3697 3479 return
3698 3480
3699 3481 class SMOperations():
3700 3482
3701 3483 def __init__(self):
3702 3484
3703 3485 return
3704 3486
3705 3487 def getMeteorParams(self, arrayParameters0, azimuth, h, pairsList, distances, jph):
3706 3488
3707 3489 arrayParameters = arrayParameters0.copy()
3708 3490 hmin = h[0]
3709 3491 hmax = h[1]
3710 3492
3711 3493 #Calculate AOA (Error N 3, 4)
3712 3494 #JONES ET AL. 1998
3713 3495 AOAthresh = numpy.pi/8
3714 3496 error = arrayParameters[:,-1]
3715 3497 phases = -arrayParameters[:,8:12] + jph
3716 3498 # phases = numpy.unwrap(phases)
3717 3499 arrayParameters[:,3:6], arrayParameters[:,-1] = self.__getAOA(phases, pairsList, distances, error, AOAthresh, azimuth)
3718 3500
3719 3501 #Calculate Heights (Error N 13 and 14)
3720 3502 error = arrayParameters[:,-1]
3721 3503 Ranges = arrayParameters[:,1]
3722 3504 zenith = arrayParameters[:,4]
3723 3505 arrayParameters[:,2], arrayParameters[:,-1] = self.__getHeights(Ranges, zenith, error, hmin, hmax)
3724 3506
3725 3507 #----------------------- Get Final data ------------------------------------
3726 3508 # error = arrayParameters[:,-1]
3727 3509 # ind1 = numpy.where(error==0)[0]
3728 3510 # arrayParameters = arrayParameters[ind1,:]
3729 3511
3730 3512 return arrayParameters
3731 3513
3732 3514 def __getAOA(self, phases, pairsList, directions, error, AOAthresh, azimuth):
3733 3515
3734 3516 arrayAOA = numpy.zeros((phases.shape[0],3))
3735 3517 cosdir0, cosdir = self.__getDirectionCosines(phases, pairsList,directions)
3736 3518
3737 3519 arrayAOA[:,:2] = self.__calculateAOA(cosdir, azimuth)
3738 3520 cosDirError = numpy.sum(numpy.abs(cosdir0 - cosdir), axis = 1)
3739 3521 arrayAOA[:,2] = cosDirError
3740 3522
3741 3523 azimuthAngle = arrayAOA[:,0]
3742 3524 zenithAngle = arrayAOA[:,1]
3743 3525
3744 3526 #Setting Error
3745 3527 indError = numpy.where(numpy.logical_or(error == 3, error == 4))[0]
3746 3528 error[indError] = 0
3747 3529 #Number 3: AOA not fesible
3748 3530 indInvalid = numpy.where(numpy.logical_and((numpy.logical_or(numpy.isnan(zenithAngle), numpy.isnan(azimuthAngle))),error == 0))[0]
3749 3531 error[indInvalid] = 3
3750 3532 #Number 4: Large difference in AOAs obtained from different antenna baselines
3751 3533 indInvalid = numpy.where(numpy.logical_and(cosDirError > AOAthresh,error == 0))[0]
3752 3534 error[indInvalid] = 4
3753 3535 return arrayAOA, error
3754 3536
3755 3537 def __getDirectionCosines(self, arrayPhase, pairsList, distances):
3756 3538
3757 3539 #Initializing some variables
3758 3540 ang_aux = numpy.array([-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8])*2*numpy.pi
3759 3541 ang_aux = ang_aux.reshape(1,ang_aux.size)
3760 3542
3761 3543 cosdir = numpy.zeros((arrayPhase.shape[0],2))
3762 3544 cosdir0 = numpy.zeros((arrayPhase.shape[0],2))
3763 3545
3764 3546
3765 3547 for i in range(2):
3766 3548 ph0 = arrayPhase[:,pairsList[i][0]]
3767 3549 ph1 = arrayPhase[:,pairsList[i][1]]
3768 3550 d0 = distances[pairsList[i][0]]
3769 3551 d1 = distances[pairsList[i][1]]
3770 3552
3771 3553 ph0_aux = ph0 + ph1
3772 3554 ph0_aux = numpy.angle(numpy.exp(1j*ph0_aux))
3773 3555 # ph0_aux[ph0_aux > numpy.pi] -= 2*numpy.pi
3774 3556 # ph0_aux[ph0_aux < -numpy.pi] += 2*numpy.pi
3775 3557 #First Estimation
3776 3558 cosdir0[:,i] = (ph0_aux)/(2*numpy.pi*(d0 - d1))
3777 3559
3778 3560 #Most-Accurate Second Estimation
3779 3561 phi1_aux = ph0 - ph1
3780 3562 phi1_aux = phi1_aux.reshape(phi1_aux.size,1)
3781 3563 #Direction Cosine 1
3782 3564 cosdir1 = (phi1_aux + ang_aux)/(2*numpy.pi*(d0 + d1))
3783 3565
3784 3566 #Searching the correct Direction Cosine
3785 3567 cosdir0_aux = cosdir0[:,i]
3786 3568 cosdir0_aux = cosdir0_aux.reshape(cosdir0_aux.size,1)
3787 3569 #Minimum Distance
3788 3570 cosDiff = (cosdir1 - cosdir0_aux)**2
3789 3571 indcos = cosDiff.argmin(axis = 1)
3790 3572 #Saving Value obtained
3791 3573 cosdir[:,i] = cosdir1[numpy.arange(len(indcos)),indcos]
3792 3574
3793 3575 return cosdir0, cosdir
3794 3576
3795 3577 def __calculateAOA(self, cosdir, azimuth):
3796 3578 cosdirX = cosdir[:,0]
3797 3579 cosdirY = cosdir[:,1]
3798 3580
3799 3581 zenithAngle = numpy.arccos(numpy.sqrt(1 - cosdirX**2 - cosdirY**2))*180/numpy.pi
3800 3582 azimuthAngle = numpy.arctan2(cosdirX,cosdirY)*180/numpy.pi + azimuth#0 deg north, 90 deg east
3801 3583 angles = numpy.vstack((azimuthAngle, zenithAngle)).transpose()
3802 3584
3803 3585 return angles
3804 3586
3805 3587 def __getHeights(self, Ranges, zenith, error, minHeight, maxHeight):
3806 3588
3807 3589 Ramb = 375 #Ramb = c/(2*PRF)
3808 3590 Re = 6371 #Earth Radius
3809 3591 heights = numpy.zeros(Ranges.shape)
3810 3592
3811 3593 R_aux = numpy.array([0,1,2])*Ramb
3812 3594 R_aux = R_aux.reshape(1,R_aux.size)
3813 3595
3814 3596 Ranges = Ranges.reshape(Ranges.size,1)
3815 3597
3816 3598 Ri = Ranges + R_aux
3817 3599 hi = numpy.sqrt(Re**2 + Ri**2 + (2*Re*numpy.cos(zenith*numpy.pi/180)*Ri.transpose()).transpose()) - Re
3818 3600
3819 3601 #Check if there is a height between 70 and 110 km
3820 3602 h_bool = numpy.sum(numpy.logical_and(hi > minHeight, hi < maxHeight), axis = 1)
3821 3603 ind_h = numpy.where(h_bool == 1)[0]
3822 3604
3823 3605 hCorr = hi[ind_h, :]
3824 3606 ind_hCorr = numpy.where(numpy.logical_and(hi > minHeight, hi < maxHeight))
3825 3607
3826 3608 hCorr = hi[ind_hCorr]
3827 3609 heights[ind_h] = hCorr
3828 3610
3829 3611 #Setting Error
3830 3612 #Number 13: Height unresolvable echo: not valid height within 70 to 110 km
3831 3613 #Number 14: Height ambiguous echo: more than one possible height within 70 to 110 km
3832 3614 indError = numpy.where(numpy.logical_or(error == 13, error == 14))[0]
3833 3615 error[indError] = 0
3834 3616 indInvalid2 = numpy.where(numpy.logical_and(h_bool > 1, error == 0))[0]
3835 3617 error[indInvalid2] = 14
3836 3618 indInvalid1 = numpy.where(numpy.logical_and(h_bool == 0, error == 0))[0]
3837 3619 error[indInvalid1] = 13
3838 3620
3839 3621 return heights, error
3840 3622
3841 3623 def getPhasePairs(self, channelPositions):
3842 3624 chanPos = numpy.array(channelPositions)
3843 3625 listOper = list(itertools.combinations(range(5),2))
3844 3626
3845 3627 distances = numpy.zeros(4)
3846 3628 axisX = []
3847 3629 axisY = []
3848 3630 distX = numpy.zeros(3)
3849 3631 distY = numpy.zeros(3)
3850 3632 ix = 0
3851 3633 iy = 0
3852 3634
3853 3635 pairX = numpy.zeros((2,2))
3854 3636 pairY = numpy.zeros((2,2))
3855 3637
3856 3638 for i in range(len(listOper)):
3857 3639 pairi = listOper[i]
3858 3640
3859 3641 posDif = numpy.abs(chanPos[pairi[0],:] - chanPos[pairi[1],:])
3860 3642
3861 3643 if posDif[0] == 0:
3862 3644 axisY.append(pairi)
3863 3645 distY[iy] = posDif[1]
3864 3646 iy += 1
3865 3647 elif posDif[1] == 0:
3866 3648 axisX.append(pairi)
3867 3649 distX[ix] = posDif[0]
3868 3650 ix += 1
3869 3651
3870 3652 for i in range(2):
3871 3653 if i==0:
3872 3654 dist0 = distX
3873 3655 axis0 = axisX
3874 3656 else:
3875 3657 dist0 = distY
3876 3658 axis0 = axisY
3877 3659
3878 3660 side = numpy.argsort(dist0)[:-1]
3879 3661 axis0 = numpy.array(axis0)[side,:]
3880 3662 chanC = int(numpy.intersect1d(axis0[0,:], axis0[1,:])[0])
3881 3663 axis1 = numpy.unique(numpy.reshape(axis0,4))
3882 3664 side = axis1[axis1 != chanC]
3883 3665 diff1 = chanPos[chanC,i] - chanPos[side[0],i]
3884 3666 diff2 = chanPos[chanC,i] - chanPos[side[1],i]
3885 3667 if diff1<0:
3886 3668 chan2 = side[0]
3887 3669 d2 = numpy.abs(diff1)
3888 3670 chan1 = side[1]
3889 3671 d1 = numpy.abs(diff2)
3890 3672 else:
3891 3673 chan2 = side[1]
3892 3674 d2 = numpy.abs(diff2)
3893 3675 chan1 = side[0]
3894 3676 d1 = numpy.abs(diff1)
3895 3677
3896 3678 if i==0:
3897 3679 chanCX = chanC
3898 3680 chan1X = chan1
3899 3681 chan2X = chan2
3900 3682 distances[0:2] = numpy.array([d1,d2])
3901 3683 else:
3902 3684 chanCY = chanC
3903 3685 chan1Y = chan1
3904 3686 chan2Y = chan2
3905 3687 distances[2:4] = numpy.array([d1,d2])
3906 3688 # axisXsides = numpy.reshape(axisX[ix,:],4)
3907 3689 #
3908 3690 # channelCentX = int(numpy.intersect1d(pairX[0,:], pairX[1,:])[0])
3909 3691 # channelCentY = int(numpy.intersect1d(pairY[0,:], pairY[1,:])[0])
3910 3692 #
3911 3693 # ind25X = numpy.where(pairX[0,:] != channelCentX)[0][0]
3912 3694 # ind20X = numpy.where(pairX[1,:] != channelCentX)[0][0]
3913 3695 # channel25X = int(pairX[0,ind25X])
3914 3696 # channel20X = int(pairX[1,ind20X])
3915 3697 # ind25Y = numpy.where(pairY[0,:] != channelCentY)[0][0]
3916 3698 # ind20Y = numpy.where(pairY[1,:] != channelCentY)[0][0]
3917 3699 # channel25Y = int(pairY[0,ind25Y])
3918 3700 # channel20Y = int(pairY[1,ind20Y])
3919 3701
3920 3702 # pairslist = [(channelCentX, channel25X),(channelCentX, channel20X),(channelCentY,channel25Y),(channelCentY, channel20Y)]
3921 3703 pairslist = [(chanCX, chan1X),(chanCX, chan2X),(chanCY,chan1Y),(chanCY, chan2Y)]
3922 3704
3923 3705 return pairslist, distances
3924 3706 # def __getAOA(self, phases, pairsList, error, AOAthresh, azimuth):
3925 3707 #
3926 3708 # arrayAOA = numpy.zeros((phases.shape[0],3))
3927 3709 # cosdir0, cosdir = self.__getDirectionCosines(phases, pairsList)
3928 3710 #
3929 3711 # arrayAOA[:,:2] = self.__calculateAOA(cosdir, azimuth)
3930 3712 # cosDirError = numpy.sum(numpy.abs(cosdir0 - cosdir), axis = 1)
3931 3713 # arrayAOA[:,2] = cosDirError
3932 3714 #
3933 3715 # azimuthAngle = arrayAOA[:,0]
3934 3716 # zenithAngle = arrayAOA[:,1]
3935 3717 #
3936 3718 # #Setting Error
3937 3719 # #Number 3: AOA not fesible
3938 3720 # indInvalid = numpy.where(numpy.logical_and((numpy.logical_or(numpy.isnan(zenithAngle), numpy.isnan(azimuthAngle))),error == 0))[0]
3939 3721 # error[indInvalid] = 3
3940 3722 # #Number 4: Large difference in AOAs obtained from different antenna baselines
3941 3723 # indInvalid = numpy.where(numpy.logical_and(cosDirError > AOAthresh,error == 0))[0]
3942 3724 # error[indInvalid] = 4
3943 3725 # return arrayAOA, error
3944 3726 #
3945 3727 # def __getDirectionCosines(self, arrayPhase, pairsList):
3946 3728 #
3947 3729 # #Initializing some variables
3948 3730 # ang_aux = numpy.array([-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8])*2*numpy.pi
3949 3731 # ang_aux = ang_aux.reshape(1,ang_aux.size)
3950 3732 #
3951 3733 # cosdir = numpy.zeros((arrayPhase.shape[0],2))
3952 3734 # cosdir0 = numpy.zeros((arrayPhase.shape[0],2))
3953 3735 #
3954 3736 #
3955 3737 # for i in range(2):
3956 3738 # #First Estimation
3957 3739 # phi0_aux = arrayPhase[:,pairsList[i][0]] + arrayPhase[:,pairsList[i][1]]
3958 3740 # #Dealias
3959 3741 # indcsi = numpy.where(phi0_aux > numpy.pi)
3960 3742 # phi0_aux[indcsi] -= 2*numpy.pi
3961 3743 # indcsi = numpy.where(phi0_aux < -numpy.pi)
3962 3744 # phi0_aux[indcsi] += 2*numpy.pi
3963 3745 # #Direction Cosine 0
3964 3746 # cosdir0[:,i] = -(phi0_aux)/(2*numpy.pi*0.5)
3965 3747 #
3966 3748 # #Most-Accurate Second Estimation
3967 3749 # phi1_aux = arrayPhase[:,pairsList[i][0]] - arrayPhase[:,pairsList[i][1]]
3968 3750 # phi1_aux = phi1_aux.reshape(phi1_aux.size,1)
3969 3751 # #Direction Cosine 1
3970 3752 # cosdir1 = -(phi1_aux + ang_aux)/(2*numpy.pi*4.5)
3971 3753 #
3972 3754 # #Searching the correct Direction Cosine
3973 3755 # cosdir0_aux = cosdir0[:,i]
3974 3756 # cosdir0_aux = cosdir0_aux.reshape(cosdir0_aux.size,1)
3975 3757 # #Minimum Distance
3976 3758 # cosDiff = (cosdir1 - cosdir0_aux)**2
3977 3759 # indcos = cosDiff.argmin(axis = 1)
3978 3760 # #Saving Value obtained
3979 3761 # cosdir[:,i] = cosdir1[numpy.arange(len(indcos)),indcos]
3980 3762 #
3981 3763 # return cosdir0, cosdir
3982 3764 #
3983 3765 # def __calculateAOA(self, cosdir, azimuth):
3984 3766 # cosdirX = cosdir[:,0]
3985 3767 # cosdirY = cosdir[:,1]
3986 3768 #
3987 3769 # zenithAngle = numpy.arccos(numpy.sqrt(1 - cosdirX**2 - cosdirY**2))*180/numpy.pi
3988 3770 # azimuthAngle = numpy.arctan2(cosdirX,cosdirY)*180/numpy.pi + azimuth #0 deg north, 90 deg east
3989 3771 # angles = numpy.vstack((azimuthAngle, zenithAngle)).transpose()
3990 3772 #
3991 3773 # return angles
3992 3774 #
3993 3775 # def __getHeights(self, Ranges, zenith, error, minHeight, maxHeight):
3994 3776 #
3995 3777 # Ramb = 375 #Ramb = c/(2*PRF)
3996 3778 # Re = 6371 #Earth Radius
3997 3779 # heights = numpy.zeros(Ranges.shape)
3998 3780 #
3999 3781 # R_aux = numpy.array([0,1,2])*Ramb
4000 3782 # R_aux = R_aux.reshape(1,R_aux.size)
4001 3783 #
4002 3784 # Ranges = Ranges.reshape(Ranges.size,1)
4003 3785 #
4004 3786 # Ri = Ranges + R_aux
4005 3787 # hi = numpy.sqrt(Re**2 + Ri**2 + (2*Re*numpy.cos(zenith*numpy.pi/180)*Ri.transpose()).transpose()) - Re
4006 3788 #
4007 3789 # #Check if there is a height between 70 and 110 km
4008 3790 # h_bool = numpy.sum(numpy.logical_and(hi > minHeight, hi < maxHeight), axis = 1)
4009 3791 # ind_h = numpy.where(h_bool == 1)[0]
4010 3792 #
4011 3793 # hCorr = hi[ind_h, :]
4012 3794 # ind_hCorr = numpy.where(numpy.logical_and(hi > minHeight, hi < maxHeight))
4013 3795 #
4014 3796 # hCorr = hi[ind_hCorr]
4015 3797 # heights[ind_h] = hCorr
4016 3798 #
4017 3799 # #Setting Error
4018 3800 # #Number 13: Height unresolvable echo: not valid height within 70 to 110 km
4019 3801 # #Number 14: Height ambiguous echo: more than one possible height within 70 to 110 km
4020 3802 #
4021 3803 # indInvalid2 = numpy.where(numpy.logical_and(h_bool > 1, error == 0))[0]
4022 3804 # error[indInvalid2] = 14
4023 3805 # indInvalid1 = numpy.where(numpy.logical_and(h_bool == 0, error == 0))[0]
4024 3806 # error[indInvalid1] = 13
4025 3807 #
4026 3808 # return heights, error
4027 3809 No newline at end of file
@@ -1,1067 +1,1078
1 1 import numpy
2 2
3 3 from jroproc_base import ProcessingUnit, Operation
4 4 from schainpy.model.data.jrodata import Spectra
5 5 from schainpy.model.data.jrodata import hildebrand_sekhon
6 6
7 7 import matplotlib.pyplot as plt
8 8
9 9 class SpectraProc(ProcessingUnit):
10 10
11 11 def __init__(self, **kwargs):
12 12
13 13 ProcessingUnit.__init__(self, **kwargs)
14 14
15 15 self.buffer = None
16 16 self.firstdatatime = None
17 17 self.profIndex = 0
18 18 self.dataOut = Spectra()
19 19 self.id_min = None
20 20 self.id_max = None
21 21
22 22 def __updateSpecFromVoltage(self):
23 23
24 24 self.dataOut.timeZone = self.dataIn.timeZone
25 25 self.dataOut.dstFlag = self.dataIn.dstFlag
26 26 self.dataOut.errorCount = self.dataIn.errorCount
27 27 self.dataOut.useLocalTime = self.dataIn.useLocalTime
28 28
29 29 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
30 30 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
31 31 self.dataOut.channelList = self.dataIn.channelList
32 32 self.dataOut.heightList = self.dataIn.heightList
33 33 self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')])
34 34
35 35 self.dataOut.nBaud = self.dataIn.nBaud
36 36 self.dataOut.nCode = self.dataIn.nCode
37 37 self.dataOut.code = self.dataIn.code
38 38 self.dataOut.nProfiles = self.dataOut.nFFTPoints
39 39
40 40 self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock
41 41 self.dataOut.utctime = self.firstdatatime
42 42 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada
43 43 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip
44 44 self.dataOut.flagShiftFFT = False
45 45
46 46 self.dataOut.nCohInt = self.dataIn.nCohInt
47 47 self.dataOut.nIncohInt = 1
48 48
49 49 self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
50 50
51 51 self.dataOut.frequency = self.dataIn.frequency
52 52 self.dataOut.realtime = self.dataIn.realtime
53 53
54 54 self.dataOut.azimuth = self.dataIn.azimuth
55 55 self.dataOut.zenith = self.dataIn.zenith
56 56
57 57 self.dataOut.beam.codeList = self.dataIn.beam.codeList
58 58 self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList
59 59 self.dataOut.beam.zenithList = self.dataIn.beam.zenithList
60 60
61 61 def __getFft(self):
62 62 """
63 63 Convierte valores de Voltaje a Spectra
64 64
65 65 Affected:
66 66 self.dataOut.data_spc
67 67 self.dataOut.data_cspc
68 68 self.dataOut.data_dc
69 69 self.dataOut.heightList
70 70 self.profIndex
71 71 self.buffer
72 72 self.dataOut.flagNoData
73 73 """
74 74 fft_volt = numpy.fft.fft(self.buffer,n=self.dataOut.nFFTPoints,axis=1)
75 75
76 76 fft_volt = fft_volt.astype(numpy.dtype('complex'))
77 77 dc = fft_volt[:,0,:]
78 78
79 79 #calculo de self-spectra
80 80 fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,))
81 81 spc = fft_volt * numpy.conjugate(fft_volt)
82 82 spc = spc.real
83 83
84 84 blocksize = 0
85 85 blocksize += dc.size
86 86 blocksize += spc.size
87 87
88 88 cspc = None
89 89 pairIndex = 0
90 90 if self.dataOut.pairsList != None:
91 91 #calculo de cross-spectra
92 92 cspc = numpy.zeros((self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex')
93 93 for pair in self.dataOut.pairsList:
94 94 if pair[0] not in self.dataOut.channelList:
95 95 raise ValueError, "Error getting CrossSpectra: pair 0 of %s is not in channelList = %s" %(str(pair), str(self.dataOut.channelList))
96 96 if pair[1] not in self.dataOut.channelList:
97 97 raise ValueError, "Error getting CrossSpectra: pair 1 of %s is not in channelList = %s" %(str(pair), str(self.dataOut.channelList))
98 98
99 99 cspc[pairIndex,:,:] = fft_volt[pair[0],:,:] * numpy.conjugate(fft_volt[pair[1],:,:])
100 100 pairIndex += 1
101 101 blocksize += cspc.size
102 102
103 103 self.dataOut.data_spc = spc
104 104 self.dataOut.data_cspc = cspc
105 105 self.dataOut.data_dc = dc
106 106 self.dataOut.blockSize = blocksize
107 107 self.dataOut.flagShiftFFT = True
108 108
109 109 def run(self, nProfiles=None, nFFTPoints=None, pairsList=[], ippFactor=None):
110 110
111 111 self.dataOut.flagNoData = True
112 112
113 113 if self.dataIn.type == "Spectra":
114 114 self.dataOut.copy(self.dataIn)
115 115 # self.__selectPairs(pairsList)
116 116 return True
117 117
118 118 if self.dataIn.type == "Voltage":
119 119
120 120 if nFFTPoints == None:
121 121 raise ValueError, "This SpectraProc.run() need nFFTPoints input variable"
122 122
123 123 if nProfiles == None:
124 124 nProfiles = nFFTPoints
125 125
126 126 if ippFactor == None:
127 127 ippFactor = 1
128 128
129 129 self.dataOut.ippFactor = ippFactor
130 130
131 131 self.dataOut.nFFTPoints = nFFTPoints
132 132 self.dataOut.pairsList = pairsList
133 133
134 134 if self.buffer is None:
135 135 self.buffer = numpy.zeros( (self.dataIn.nChannels,
136 136 nProfiles,
137 137 self.dataIn.nHeights),
138 138 dtype='complex')
139 139
140 140 if self.dataIn.flagDataAsBlock:
141 141 #data dimension: [nChannels, nProfiles, nSamples]
142 142
143 143 nVoltProfiles = self.dataIn.data.shape[1]
144 144 # nVoltProfiles = self.dataIn.nProfiles
145 145
146 146 if nVoltProfiles == nProfiles:
147 147 self.buffer = self.dataIn.data.copy()
148 148 self.profIndex = nVoltProfiles
149 149
150 150 elif nVoltProfiles < nProfiles:
151 151
152 152 if self.profIndex == 0:
153 153 self.id_min = 0
154 154 self.id_max = nVoltProfiles
155 155
156 156 self.buffer[:,self.id_min:self.id_max,:] = self.dataIn.data
157 157 self.profIndex += nVoltProfiles
158 158 self.id_min += nVoltProfiles
159 159 self.id_max += nVoltProfiles
160 160 else:
161 161 raise ValueError, "The type object %s has %d profiles, it should just has %d profiles"%(self.dataIn.type,self.dataIn.data.shape[1],nProfiles)
162 162 self.dataOut.flagNoData = True
163 163 return 0
164 164 else:
165 165
166 166 self.buffer[:,self.profIndex,:] = self.dataIn.data.copy()
167 167 self.profIndex += 1
168 168
169 169 if self.firstdatatime == None:
170 170 self.firstdatatime = self.dataIn.utctime
171 171
172 172 if self.profIndex == nProfiles:
173 173 self.__updateSpecFromVoltage()
174 174 self.__getFft()
175 175
176 176 self.dataOut.flagNoData = False
177 177 self.firstdatatime = None
178 178 self.profIndex = 0
179 179
180 180 return True
181 181
182 182 raise ValueError, "The type of input object '%s' is not valid"%(self.dataIn.type)
183 183
184 184 def __selectPairs(self, pairsList):
185 185
186 186 if channelList == None:
187 187 return
188 188
189 189 pairsIndexListSelected = []
190 190
191 191 for thisPair in pairsList:
192 192
193 193 if thisPair not in self.dataOut.pairsList:
194 194 continue
195 195
196 196 pairIndex = self.dataOut.pairsList.index(thisPair)
197 197
198 198 pairsIndexListSelected.append(pairIndex)
199 199
200 200 if not pairsIndexListSelected:
201 201 self.dataOut.data_cspc = None
202 202 self.dataOut.pairsList = []
203 203 return
204 204
205 205 self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndexListSelected]
206 206 self.dataOut.pairsList = [self.dataOut.pairsList[i] for i in pairsIndexListSelected]
207 207
208 208 return
209 209
210 210 def __selectPairsByChannel(self, channelList=None):
211 211
212 212 if channelList == None:
213 213 return
214 214
215 215 pairsIndexListSelected = []
216 216 for pairIndex in self.dataOut.pairsIndexList:
217 217 #First pair
218 218 if self.dataOut.pairsList[pairIndex][0] not in channelList:
219 219 continue
220 220 #Second pair
221 221 if self.dataOut.pairsList[pairIndex][1] not in channelList:
222 222 continue
223 223
224 224 pairsIndexListSelected.append(pairIndex)
225 225
226 226 if not pairsIndexListSelected:
227 227 self.dataOut.data_cspc = None
228 228 self.dataOut.pairsList = []
229 229 return
230 230
231 231 self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndexListSelected]
232 232 self.dataOut.pairsList = [self.dataOut.pairsList[i] for i in pairsIndexListSelected]
233 233
234 234 return
235 235
236 236 def selectChannels(self, channelList):
237 237
238 238 channelIndexList = []
239 239
240 240 for channel in channelList:
241 241 if channel not in self.dataOut.channelList:
242 242 raise ValueError, "Error selecting channels, Channel %d is not valid.\nAvailable channels = %s" %(channel, str(self.dataOut.channelList))
243 243
244 244 index = self.dataOut.channelList.index(channel)
245 245 channelIndexList.append(index)
246 246
247 247 self.selectChannelsByIndex(channelIndexList)
248 248
249 249 def selectChannelsByIndex(self, channelIndexList):
250 250 """
251 251 Selecciona un bloque de datos en base a canales segun el channelIndexList
252 252
253 253 Input:
254 254 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
255 255
256 256 Affected:
257 257 self.dataOut.data_spc
258 258 self.dataOut.channelIndexList
259 259 self.dataOut.nChannels
260 260
261 261 Return:
262 262 None
263 263 """
264 264
265 265 for channelIndex in channelIndexList:
266 266 if channelIndex not in self.dataOut.channelIndexList:
267 267 raise ValueError, "Error selecting channels: The value %d in channelIndexList is not valid.\nAvailable channel indexes = " %(channelIndex, self.dataOut.channelIndexList)
268 268
269 269 # nChannels = len(channelIndexList)
270 270
271 271 data_spc = self.dataOut.data_spc[channelIndexList,:]
272 272 data_dc = self.dataOut.data_dc[channelIndexList,:]
273 273
274 274 self.dataOut.data_spc = data_spc
275 275 self.dataOut.data_dc = data_dc
276 276
277 277 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
278 278 # self.dataOut.nChannels = nChannels
279 279
280 280 self.__selectPairsByChannel(self.dataOut.channelList)
281 281
282 282 return 1
283 283
284 284
285 285 def selectFFTs(self, minFFT, maxFFT ):
286 286 """
287 287 Selecciona un bloque de datos en base a un grupo de valores de puntos FFTs segun el rango
288 288 minFFT<= FFT <= maxFFT
289 289 """
290 290
291 291 if (minFFT > maxFFT):
292 292 raise ValueError, "Error selecting heights: Height range (%d,%d) is not valid" % (minFFT, maxFFT)
293 293
294 294 if (minFFT < self.dataOut.getFreqRange()[0]):
295 295 minFFT = self.dataOut.getFreqRange()[0]
296 296
297 297 if (maxFFT > self.dataOut.getFreqRange()[-1]):
298 298 maxFFT = self.dataOut.getFreqRange()[-1]
299 299
300 300 minIndex = 0
301 301 maxIndex = 0
302 302 FFTs = self.dataOut.getFreqRange()
303 303
304 304 inda = numpy.where(FFTs >= minFFT)
305 305 indb = numpy.where(FFTs <= maxFFT)
306 306
307 307 try:
308 308 minIndex = inda[0][0]
309 309 except:
310 310 minIndex = 0
311 311
312 312 try:
313 313 maxIndex = indb[0][-1]
314 314 except:
315 315 maxIndex = len(FFTs)
316 316
317 317 self.selectFFTsByIndex(minIndex, maxIndex)
318 318
319 319 return 1
320 320
321 321
322 def setH0(self, h0, deltaHeight = None):
323
324 if not deltaHeight:
325 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
326
327 nHeights = self.dataOut.nHeights
328
329 newHeiRange = h0 + numpy.arange(nHeights)*deltaHeight
330
331 self.dataOut.heightList = newHeiRange
332
322 333
323 334 def selectHeights(self, minHei, maxHei):
324 335 """
325 336 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
326 337 minHei <= height <= maxHei
327 338
328 339 Input:
329 340 minHei : valor minimo de altura a considerar
330 341 maxHei : valor maximo de altura a considerar
331 342
332 343 Affected:
333 344 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
334 345
335 346 Return:
336 347 1 si el metodo se ejecuto con exito caso contrario devuelve 0
337 348 """
338 349
339 350
340 351 if (minHei > maxHei):
341 352 raise ValueError, "Error selecting heights: Height range (%d,%d) is not valid" % (minHei, maxHei)
342 353
343 354 if (minHei < self.dataOut.heightList[0]):
344 355 minHei = self.dataOut.heightList[0]
345 356
346 357 if (maxHei > self.dataOut.heightList[-1]):
347 358 maxHei = self.dataOut.heightList[-1]
348 359
349 360 minIndex = 0
350 361 maxIndex = 0
351 362 heights = self.dataOut.heightList
352 363
353 364 inda = numpy.where(heights >= minHei)
354 365 indb = numpy.where(heights <= maxHei)
355 366
356 367 try:
357 368 minIndex = inda[0][0]
358 369 except:
359 370 minIndex = 0
360 371
361 372 try:
362 373 maxIndex = indb[0][-1]
363 374 except:
364 375 maxIndex = len(heights)
365 376
366 377 self.selectHeightsByIndex(minIndex, maxIndex)
367 378
368 379
369 380 return 1
370 381
371 382 def getBeaconSignal(self, tauindex = 0, channelindex = 0, hei_ref=None):
372 383 newheis = numpy.where(self.dataOut.heightList>self.dataOut.radarControllerHeaderObj.Taus[tauindex])
373 384
374 385 if hei_ref != None:
375 386 newheis = numpy.where(self.dataOut.heightList>hei_ref)
376 387
377 388 minIndex = min(newheis[0])
378 389 maxIndex = max(newheis[0])
379 390 data_spc = self.dataOut.data_spc[:,:,minIndex:maxIndex+1]
380 391 heightList = self.dataOut.heightList[minIndex:maxIndex+1]
381 392
382 393 # determina indices
383 394 nheis = int(self.dataOut.radarControllerHeaderObj.txB/(self.dataOut.heightList[1]-self.dataOut.heightList[0]))
384 395 avg_dB = 10*numpy.log10(numpy.sum(data_spc[channelindex,:,:],axis=0))
385 396 beacon_dB = numpy.sort(avg_dB)[-nheis:]
386 397 beacon_heiIndexList = []
387 398 for val in avg_dB.tolist():
388 399 if val >= beacon_dB[0]:
389 400 beacon_heiIndexList.append(avg_dB.tolist().index(val))
390 401
391 402 #data_spc = data_spc[:,:,beacon_heiIndexList]
392 403 data_cspc = None
393 404 if self.dataOut.data_cspc is not None:
394 405 data_cspc = self.dataOut.data_cspc[:,:,minIndex:maxIndex+1]
395 406 #data_cspc = data_cspc[:,:,beacon_heiIndexList]
396 407
397 408 data_dc = None
398 409 if self.dataOut.data_dc is not None:
399 410 data_dc = self.dataOut.data_dc[:,minIndex:maxIndex+1]
400 411 #data_dc = data_dc[:,beacon_heiIndexList]
401 412
402 413 self.dataOut.data_spc = data_spc
403 414 self.dataOut.data_cspc = data_cspc
404 415 self.dataOut.data_dc = data_dc
405 416 self.dataOut.heightList = heightList
406 417 self.dataOut.beacon_heiIndexList = beacon_heiIndexList
407 418
408 419 return 1
409 420
410 421 def selectFFTsByIndex(self, minIndex, maxIndex):
411 422 """
412 423
413 424 """
414 425
415 426 if (minIndex < 0) or (minIndex > maxIndex):
416 427 raise ValueError, "Error selecting heights: Index range (%d,%d) is not valid" % (minIndex, maxIndex)
417 428
418 429 if (maxIndex >= self.dataOut.nProfiles):
419 430 maxIndex = self.dataOut.nProfiles-1
420 431
421 432 #Spectra
422 433 data_spc = self.dataOut.data_spc[:,minIndex:maxIndex+1,:]
423 434
424 435 data_cspc = None
425 436 if self.dataOut.data_cspc is not None:
426 437 data_cspc = self.dataOut.data_cspc[:,minIndex:maxIndex+1,:]
427 438
428 439 data_dc = None
429 440 if self.dataOut.data_dc is not None:
430 441 data_dc = self.dataOut.data_dc[minIndex:maxIndex+1,:]
431 442
432 443 self.dataOut.data_spc = data_spc
433 444 self.dataOut.data_cspc = data_cspc
434 445 self.dataOut.data_dc = data_dc
435 446
436 447 self.dataOut.ippSeconds = self.dataOut.ippSeconds*(self.dataOut.nFFTPoints / numpy.shape(data_cspc)[1])
437 448 self.dataOut.nFFTPoints = numpy.shape(data_cspc)[1]
438 449 self.dataOut.profilesPerBlock = numpy.shape(data_cspc)[1]
439 450
440 451 #self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex+1]
441 452
442 453 return 1
443 454
444 455
445 456
446 457 def selectHeightsByIndex(self, minIndex, maxIndex):
447 458 """
448 459 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
449 460 minIndex <= index <= maxIndex
450 461
451 462 Input:
452 463 minIndex : valor de indice minimo de altura a considerar
453 464 maxIndex : valor de indice maximo de altura a considerar
454 465
455 466 Affected:
456 467 self.dataOut.data_spc
457 468 self.dataOut.data_cspc
458 469 self.dataOut.data_dc
459 470 self.dataOut.heightList
460 471
461 472 Return:
462 473 1 si el metodo se ejecuto con exito caso contrario devuelve 0
463 474 """
464 475
465 476 if (minIndex < 0) or (minIndex > maxIndex):
466 477 raise ValueError, "Error selecting heights: Index range (%d,%d) is not valid" % (minIndex, maxIndex)
467 478
468 479 if (maxIndex >= self.dataOut.nHeights):
469 480 maxIndex = self.dataOut.nHeights-1
470 481
471 482 #Spectra
472 483 data_spc = self.dataOut.data_spc[:,:,minIndex:maxIndex+1]
473 484
474 485 data_cspc = None
475 486 if self.dataOut.data_cspc is not None:
476 487 data_cspc = self.dataOut.data_cspc[:,:,minIndex:maxIndex+1]
477 488
478 489 data_dc = None
479 490 if self.dataOut.data_dc is not None:
480 491 data_dc = self.dataOut.data_dc[:,minIndex:maxIndex+1]
481 492
482 493 self.dataOut.data_spc = data_spc
483 494 self.dataOut.data_cspc = data_cspc
484 495 self.dataOut.data_dc = data_dc
485 496
486 497 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex+1]
487 498
488 499 return 1
489 500
490 501
491 502 def removeDC(self, mode = 2):
492 503 jspectra = self.dataOut.data_spc
493 504 jcspectra = self.dataOut.data_cspc
494 505
495 506
496 507 num_chan = jspectra.shape[0]
497 508 num_hei = jspectra.shape[2]
498 509
499 510 if jcspectra is not None:
500 511 jcspectraExist = True
501 512 num_pairs = jcspectra.shape[0]
502 513 else: jcspectraExist = False
503 514
504 515 freq_dc = jspectra.shape[1]/2
505 516 ind_vel = numpy.array([-2,-1,1,2]) + freq_dc
506 517
507 518 if ind_vel[0]<0:
508 519 ind_vel[range(0,1)] = ind_vel[range(0,1)] + self.num_prof
509 520
510 521 if mode == 1:
511 522 jspectra[:,freq_dc,:] = (jspectra[:,ind_vel[1],:] + jspectra[:,ind_vel[2],:])/2 #CORRECCION
512 523
513 524 if jcspectraExist:
514 525 jcspectra[:,freq_dc,:] = (jcspectra[:,ind_vel[1],:] + jcspectra[:,ind_vel[2],:])/2
515 526
516 527 if mode == 2:
517 528
518 529 vel = numpy.array([-2,-1,1,2])
519 530 xx = numpy.zeros([4,4])
520 531
521 532 for fil in range(4):
522 533 xx[fil,:] = vel[fil]**numpy.asarray(range(4))
523 534
524 535 xx_inv = numpy.linalg.inv(xx)
525 536 xx_aux = xx_inv[0,:]
526 537
527 538 for ich in range(num_chan):
528 539 yy = jspectra[ich,ind_vel,:]
529 540 jspectra[ich,freq_dc,:] = numpy.dot(xx_aux,yy)
530 541
531 542 junkid = jspectra[ich,freq_dc,:]<=0
532 543 cjunkid = sum(junkid)
533 544
534 545 if cjunkid.any():
535 546 jspectra[ich,freq_dc,junkid.nonzero()] = (jspectra[ich,ind_vel[1],junkid] + jspectra[ich,ind_vel[2],junkid])/2
536 547
537 548 if jcspectraExist:
538 549 for ip in range(num_pairs):
539 550 yy = jcspectra[ip,ind_vel,:]
540 551 jcspectra[ip,freq_dc,:] = numpy.dot(xx_aux,yy)
541 552
542 553
543 554 self.dataOut.data_spc = jspectra
544 555 self.dataOut.data_cspc = jcspectra
545 556
546 557 return 1
547 558
548 559 def removeInterference2(self):
549 560
550 561 cspc = self.dataOut.data_cspc
551 562 spc = self.dataOut.data_spc
552 563 print numpy.shape(spc)
553 564 Heights = numpy.arange(cspc.shape[2])
554 565 realCspc = numpy.abs(cspc)
555 566
556 567 for i in range(cspc.shape[0]):
557 568 LinePower= numpy.sum(realCspc[i], axis=0)
558 569 Threshold = numpy.amax(LinePower)-numpy.sort(LinePower)[len(Heights)-int(len(Heights)*0.1)]
559 570 SelectedHeights = Heights[ numpy.where( LinePower < Threshold ) ]
560 571 #print numpy.shape(realCspc)
561 572 #print '',SelectedHeights, '', numpy.shape(realCspc[i,:,SelectedHeights])
562 573 InterferenceSum = numpy.sum( realCspc[i,:,SelectedHeights], axis=0 )
563 574 print SelectedHeights
564 575 InterferenceThresholdMin = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.98)]
565 576 InterferenceThresholdMax = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.99)]
566 577
567 578
568 579 InterferenceRange = numpy.where( ([InterferenceSum > InterferenceThresholdMin]))# , InterferenceSum < InterferenceThresholdMax]) )
569 580 #InterferenceRange = numpy.where( ([InterferenceRange < InterferenceThresholdMax]))
570 581 if len(InterferenceRange)<int(cspc.shape[1]*0.3):
571 582 cspc[i,InterferenceRange,:] = numpy.NaN
572 583
573 584 print '########################################################################################'
574 585 print 'Len interference sum',len(InterferenceSum)
575 586 print 'InterferenceThresholdMin', InterferenceThresholdMin, 'InterferenceThresholdMax', InterferenceThresholdMax
576 587 print 'InterferenceRange',InterferenceRange
577 588 print '########################################################################################'
578 589
579 590 ''' Ploteo '''
580 591
581 592 #for i in range(3):
582 593 #print 'FASE', numpy.shape(phase), y[25]
583 594 #print numpy.shape(coherence)
584 595 #fig = plt.figure(10+ int(numpy.random.rand()*100))
585 596 #plt.plot( x[0:256],coherence[:,25] )
586 597 #cohAv = numpy.average(coherence[i],1)
587 598 #Pendiente = FrecRange * PhaseSlope[i]
588 599 #plt.plot( InterferenceSum)
589 600 #plt.plot( numpy.sort(InterferenceSum))
590 601 #plt.plot( LinePower )
591 602 #plt.plot( xFrec,phase[i])
592 603
593 604 #CSPCmean = numpy.mean(numpy.abs(CSPCSamples),0)
594 605 #plt.plot(xFrec, FitGauss01)
595 606 #plt.plot(xFrec, CSPCmean)
596 607 #plt.plot(xFrec, numpy.abs(CSPCSamples[0]))
597 608 #plt.plot(xFrec, FitGauss)
598 609 #plt.plot(xFrec, yMean)
599 610 #plt.plot(xFrec, numpy.abs(coherence[0]))
600 611
601 612 #plt.axis([-12, 12, 15, 50])
602 613 #plt.title("%s" %( '%s %s, Channel %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S") , i)))
603 614
604 615
605 616 #fig.savefig('/home/erick/Documents/Pics/nom{}.png'.format(int(numpy.random.rand()*100)))
606 617
607 618 #plt.show()
608 619 #self.indice=self.indice+1
609 620 #raise
610 621
611 622
612 623 self.dataOut.data_cspc = cspc
613 624
614 625 # for i in range(spc.shape[0]):
615 626 # LinePower= numpy.sum(spc[i], axis=0)
616 627 # Threshold = numpy.amax(LinePower)-numpy.sort(LinePower)[len(Heights)-int(len(Heights)*0.1)]
617 628 # SelectedHeights = Heights[ numpy.where( LinePower < Threshold ) ]
618 629 # #print numpy.shape(realCspc)
619 630 # #print '',SelectedHeights, '', numpy.shape(realCspc[i,:,SelectedHeights])
620 631 # InterferenceSum = numpy.sum( spc[i,:,SelectedHeights], axis=0 )
621 632 # InterferenceThreshold = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.98)]
622 633 # InterferenceRange = numpy.where( InterferenceSum > InterferenceThreshold )
623 634 # if len(InterferenceRange)<int(spc.shape[1]*0.03):
624 635 # spc[i,InterferenceRange,:] = numpy.NaN
625 636
626 637 #self.dataOut.data_spc = spc
627 638
628 639 def removeInterference(self, interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None):
629 640
630 641 jspectra = self.dataOut.data_spc
631 642 jcspectra = self.dataOut.data_cspc
632 643 jnoise = self.dataOut.getNoise()
633 644 num_incoh = self.dataOut.nIncohInt
634 645
635 646 num_channel = jspectra.shape[0]
636 647 num_prof = jspectra.shape[1]
637 648 num_hei = jspectra.shape[2]
638 649
639 650 #hei_interf
640 651 if hei_interf is None:
641 652 count_hei = num_hei/2 #Como es entero no importa
642 653 hei_interf = numpy.asmatrix(range(count_hei)) + num_hei - count_hei
643 654 hei_interf = numpy.asarray(hei_interf)[0]
644 655 #nhei_interf
645 656 if (nhei_interf == None):
646 657 nhei_interf = 5
647 658 if (nhei_interf < 1):
648 659 nhei_interf = 1
649 660 if (nhei_interf > count_hei):
650 661 nhei_interf = count_hei
651 662 if (offhei_interf == None):
652 663 offhei_interf = 0
653 664
654 665 ind_hei = range(num_hei)
655 666 # mask_prof = numpy.asarray(range(num_prof - 2)) + 1
656 667 # mask_prof[range(num_prof/2 - 1,len(mask_prof))] += 1
657 668 mask_prof = numpy.asarray(range(num_prof))
658 669 num_mask_prof = mask_prof.size
659 670 comp_mask_prof = [0, num_prof/2]
660 671
661 672
662 673 #noise_exist: Determina si la variable jnoise ha sido definida y contiene la informacion del ruido de cada canal
663 674 if (jnoise.size < num_channel or numpy.isnan(jnoise).any()):
664 675 jnoise = numpy.nan
665 676 noise_exist = jnoise[0] < numpy.Inf
666 677
667 678 #Subrutina de Remocion de la Interferencia
668 679 for ich in range(num_channel):
669 680 #Se ordena los espectros segun su potencia (menor a mayor)
670 681 power = jspectra[ich,mask_prof,:]
671 682 power = power[:,hei_interf]
672 683 power = power.sum(axis = 0)
673 684 psort = power.ravel().argsort()
674 685
675 686 #Se estima la interferencia promedio en los Espectros de Potencia empleando
676 687 junkspc_interf = jspectra[ich,:,hei_interf[psort[range(offhei_interf, nhei_interf + offhei_interf)]]]
677 688
678 689 if noise_exist:
679 690 # tmp_noise = jnoise[ich] / num_prof
680 691 tmp_noise = jnoise[ich]
681 692 junkspc_interf = junkspc_interf - tmp_noise
682 693 #junkspc_interf[:,comp_mask_prof] = 0
683 694
684 695 jspc_interf = junkspc_interf.sum(axis = 0) / nhei_interf
685 696 jspc_interf = jspc_interf.transpose()
686 697 #Calculando el espectro de interferencia promedio
687 698 noiseid = numpy.where(jspc_interf <= tmp_noise/ numpy.sqrt(num_incoh))
688 699 noiseid = noiseid[0]
689 700 cnoiseid = noiseid.size
690 701 interfid = numpy.where(jspc_interf > tmp_noise/ numpy.sqrt(num_incoh))
691 702 interfid = interfid[0]
692 703 cinterfid = interfid.size
693 704
694 705 if (cnoiseid > 0): jspc_interf[noiseid] = 0
695 706
696 707 #Expandiendo los perfiles a limpiar
697 708 if (cinterfid > 0):
698 709 new_interfid = (numpy.r_[interfid - 1, interfid, interfid + 1] + num_prof)%num_prof
699 710 new_interfid = numpy.asarray(new_interfid)
700 711 new_interfid = {x for x in new_interfid}
701 712 new_interfid = numpy.array(list(new_interfid))
702 713 new_cinterfid = new_interfid.size
703 714 else: new_cinterfid = 0
704 715
705 716 for ip in range(new_cinterfid):
706 717 ind = junkspc_interf[:,new_interfid[ip]].ravel().argsort()
707 718 jspc_interf[new_interfid[ip]] = junkspc_interf[ind[nhei_interf/2],new_interfid[ip]]
708 719
709 720
710 721 jspectra[ich,:,ind_hei] = jspectra[ich,:,ind_hei] - jspc_interf #Corregir indices
711 722
712 723 #Removiendo la interferencia del punto de mayor interferencia
713 724 ListAux = jspc_interf[mask_prof].tolist()
714 725 maxid = ListAux.index(max(ListAux))
715 726
716 727
717 728 if cinterfid > 0:
718 729 for ip in range(cinterfid*(interf == 2) - 1):
719 730 ind = (jspectra[ich,interfid[ip],:] < tmp_noise*(1 + 1/numpy.sqrt(num_incoh))).nonzero()
720 731 cind = len(ind)
721 732
722 733 if (cind > 0):
723 734 jspectra[ich,interfid[ip],ind] = tmp_noise*(1 + (numpy.random.uniform(cind) - 0.5)/numpy.sqrt(num_incoh))
724 735
725 736 ind = numpy.array([-2,-1,1,2])
726 737 xx = numpy.zeros([4,4])
727 738
728 739 for id1 in range(4):
729 740 xx[:,id1] = ind[id1]**numpy.asarray(range(4))
730 741
731 742 xx_inv = numpy.linalg.inv(xx)
732 743 xx = xx_inv[:,0]
733 744 ind = (ind + maxid + num_mask_prof)%num_mask_prof
734 745 yy = jspectra[ich,mask_prof[ind],:]
735 746 jspectra[ich,mask_prof[maxid],:] = numpy.dot(yy.transpose(),xx)
736 747
737 748
738 749 indAux = (jspectra[ich,:,:] < tmp_noise*(1-1/numpy.sqrt(num_incoh))).nonzero()
739 750 jspectra[ich,indAux[0],indAux[1]] = tmp_noise * (1 - 1/numpy.sqrt(num_incoh))
740 751
741 752 #Remocion de Interferencia en el Cross Spectra
742 753 if jcspectra is None: return jspectra, jcspectra
743 754 num_pairs = jcspectra.size/(num_prof*num_hei)
744 755 jcspectra = jcspectra.reshape(num_pairs, num_prof, num_hei)
745 756
746 757 for ip in range(num_pairs):
747 758
748 759 #-------------------------------------------
749 760
750 761 cspower = numpy.abs(jcspectra[ip,mask_prof,:])
751 762 cspower = cspower[:,hei_interf]
752 763 cspower = cspower.sum(axis = 0)
753 764
754 765 cspsort = cspower.ravel().argsort()
755 766 junkcspc_interf = jcspectra[ip,:,hei_interf[cspsort[range(offhei_interf, nhei_interf + offhei_interf)]]]
756 767 junkcspc_interf = junkcspc_interf.transpose()
757 768 jcspc_interf = junkcspc_interf.sum(axis = 1)/nhei_interf
758 769
759 770 ind = numpy.abs(jcspc_interf[mask_prof]).ravel().argsort()
760 771
761 772 median_real = numpy.median(numpy.real(junkcspc_interf[mask_prof[ind[range(3*num_prof/4)]],:]))
762 773 median_imag = numpy.median(numpy.imag(junkcspc_interf[mask_prof[ind[range(3*num_prof/4)]],:]))
763 774 junkcspc_interf[comp_mask_prof,:] = numpy.complex(median_real, median_imag)
764 775
765 776 for iprof in range(num_prof):
766 777 ind = numpy.abs(junkcspc_interf[iprof,:]).ravel().argsort()
767 778 jcspc_interf[iprof] = junkcspc_interf[iprof, ind[nhei_interf/2]]
768 779
769 780 #Removiendo la Interferencia
770 781 jcspectra[ip,:,ind_hei] = jcspectra[ip,:,ind_hei] - jcspc_interf
771 782
772 783 ListAux = numpy.abs(jcspc_interf[mask_prof]).tolist()
773 784 maxid = ListAux.index(max(ListAux))
774 785
775 786 ind = numpy.array([-2,-1,1,2])
776 787 xx = numpy.zeros([4,4])
777 788
778 789 for id1 in range(4):
779 790 xx[:,id1] = ind[id1]**numpy.asarray(range(4))
780 791
781 792 xx_inv = numpy.linalg.inv(xx)
782 793 xx = xx_inv[:,0]
783 794
784 795 ind = (ind + maxid + num_mask_prof)%num_mask_prof
785 796 yy = jcspectra[ip,mask_prof[ind],:]
786 797 jcspectra[ip,mask_prof[maxid],:] = numpy.dot(yy.transpose(),xx)
787 798
788 799 #Guardar Resultados
789 800 self.dataOut.data_spc = jspectra
790 801 self.dataOut.data_cspc = jcspectra
791 802
792 803 return 1
793 804
794 805 def setRadarFrequency(self, frequency=None):
795 806
796 807 if frequency != None:
797 808 self.dataOut.frequency = frequency
798 809
799 810 return 1
800 811
801 812 def getNoise(self, minHei=None, maxHei=None, minVel=None, maxVel=None):
802 813 #validacion de rango
803 814 if minHei == None:
804 815 minHei = self.dataOut.heightList[0]
805 816
806 817 if maxHei == None:
807 818 maxHei = self.dataOut.heightList[-1]
808 819
809 820 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
810 821 print 'minHei: %.2f is out of the heights range'%(minHei)
811 822 print 'minHei is setting to %.2f'%(self.dataOut.heightList[0])
812 823 minHei = self.dataOut.heightList[0]
813 824
814 825 if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei):
815 826 print 'maxHei: %.2f is out of the heights range'%(maxHei)
816 827 print 'maxHei is setting to %.2f'%(self.dataOut.heightList[-1])
817 828 maxHei = self.dataOut.heightList[-1]
818 829
819 830 # validacion de velocidades
820 831 velrange = self.dataOut.getVelRange(1)
821 832
822 833 if minVel == None:
823 834 minVel = velrange[0]
824 835
825 836 if maxVel == None:
826 837 maxVel = velrange[-1]
827 838
828 839 if (minVel < velrange[0]) or (minVel > maxVel):
829 840 print 'minVel: %.2f is out of the velocity range'%(minVel)
830 841 print 'minVel is setting to %.2f'%(velrange[0])
831 842 minVel = velrange[0]
832 843
833 844 if (maxVel > velrange[-1]) or (maxVel < minVel):
834 845 print 'maxVel: %.2f is out of the velocity range'%(maxVel)
835 846 print 'maxVel is setting to %.2f'%(velrange[-1])
836 847 maxVel = velrange[-1]
837 848
838 849 # seleccion de indices para rango
839 850 minIndex = 0
840 851 maxIndex = 0
841 852 heights = self.dataOut.heightList
842 853
843 854 inda = numpy.where(heights >= minHei)
844 855 indb = numpy.where(heights <= maxHei)
845 856
846 857 try:
847 858 minIndex = inda[0][0]
848 859 except:
849 860 minIndex = 0
850 861
851 862 try:
852 863 maxIndex = indb[0][-1]
853 864 except:
854 865 maxIndex = len(heights)
855 866
856 867 if (minIndex < 0) or (minIndex > maxIndex):
857 868 raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
858 869
859 870 if (maxIndex >= self.dataOut.nHeights):
860 871 maxIndex = self.dataOut.nHeights-1
861 872
862 873 # seleccion de indices para velocidades
863 874 indminvel = numpy.where(velrange >= minVel)
864 875 indmaxvel = numpy.where(velrange <= maxVel)
865 876 try:
866 877 minIndexVel = indminvel[0][0]
867 878 except:
868 879 minIndexVel = 0
869 880
870 881 try:
871 882 maxIndexVel = indmaxvel[0][-1]
872 883 except:
873 884 maxIndexVel = len(velrange)
874 885
875 886 #seleccion del espectro
876 887 data_spc = self.dataOut.data_spc[:,minIndexVel:maxIndexVel+1,minIndex:maxIndex+1]
877 888 #estimacion de ruido
878 889 noise = numpy.zeros(self.dataOut.nChannels)
879 890
880 891 for channel in range(self.dataOut.nChannels):
881 892 daux = data_spc[channel,:,:]
882 893 noise[channel] = hildebrand_sekhon(daux, self.dataOut.nIncohInt)
883 894
884 895 self.dataOut.noise_estimation = noise.copy()
885 896
886 897 return 1
887 898
888 899 class IncohInt(Operation):
889 900
890 901
891 902 __profIndex = 0
892 903 __withOverapping = False
893 904
894 905 __byTime = False
895 906 __initime = None
896 907 __lastdatatime = None
897 908 __integrationtime = None
898 909
899 910 __buffer_spc = None
900 911 __buffer_cspc = None
901 912 __buffer_dc = None
902 913
903 914 __dataReady = False
904 915
905 916 __timeInterval = None
906 917
907 918 n = None
908 919
909 920
910 921
911 922 def __init__(self, **kwargs):
912 923
913 924 Operation.__init__(self, **kwargs)
914 925 # self.isConfig = False
915 926
916 927 def setup(self, n=None, timeInterval=None, overlapping=False):
917 928 """
918 929 Set the parameters of the integration class.
919 930
920 931 Inputs:
921 932
922 933 n : Number of coherent integrations
923 934 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
924 935 overlapping :
925 936
926 937 """
927 938
928 939 self.__initime = None
929 940 self.__lastdatatime = 0
930 941
931 942 self.__buffer_spc = 0
932 943 self.__buffer_cspc = 0
933 944 self.__buffer_dc = 0
934 945
935 946 self.__profIndex = 0
936 947 self.__dataReady = False
937 948 self.__byTime = False
938 949
939 950 if n is None and timeInterval is None:
940 951 raise ValueError, "n or timeInterval should be specified ..."
941 952
942 953 if n is not None:
943 954 self.n = int(n)
944 955 else:
945 956 self.__integrationtime = int(timeInterval) #if (type(timeInterval)!=integer) -> change this line
946 957 self.n = None
947 958 self.__byTime = True
948 959
949 960 def putData(self, data_spc, data_cspc, data_dc):
950 961
951 962 """
952 963 Add a profile to the __buffer_spc and increase in one the __profileIndex
953 964
954 965 """
955 966
956 967 self.__buffer_spc += data_spc
957 968
958 969 if data_cspc is None:
959 970 self.__buffer_cspc = None
960 971 else:
961 972 self.__buffer_cspc += data_cspc
962 973
963 974 if data_dc is None:
964 975 self.__buffer_dc = None
965 976 else:
966 977 self.__buffer_dc += data_dc
967 978
968 979 self.__profIndex += 1
969 980
970 981 return
971 982
972 983 def pushData(self):
973 984 """
974 985 Return the sum of the last profiles and the profiles used in the sum.
975 986
976 987 Affected:
977 988
978 989 self.__profileIndex
979 990
980 991 """
981 992
982 993 data_spc = self.__buffer_spc
983 994 data_cspc = self.__buffer_cspc
984 995 data_dc = self.__buffer_dc
985 996 n = self.__profIndex
986 997
987 998 self.__buffer_spc = 0
988 999 self.__buffer_cspc = 0
989 1000 self.__buffer_dc = 0
990 1001 self.__profIndex = 0
991 1002
992 1003 return data_spc, data_cspc, data_dc, n
993 1004
994 1005 def byProfiles(self, *args):
995 1006
996 1007 self.__dataReady = False
997 1008 avgdata_spc = None
998 1009 avgdata_cspc = None
999 1010 avgdata_dc = None
1000 1011
1001 1012 self.putData(*args)
1002 1013
1003 1014 if self.__profIndex == self.n:
1004 1015
1005 1016 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1006 1017 self.n = n
1007 1018 self.__dataReady = True
1008 1019
1009 1020 return avgdata_spc, avgdata_cspc, avgdata_dc
1010 1021
1011 1022 def byTime(self, datatime, *args):
1012 1023
1013 1024 self.__dataReady = False
1014 1025 avgdata_spc = None
1015 1026 avgdata_cspc = None
1016 1027 avgdata_dc = None
1017 1028
1018 1029 self.putData(*args)
1019 1030
1020 1031 if (datatime - self.__initime) >= self.__integrationtime:
1021 1032 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1022 1033 self.n = n
1023 1034 self.__dataReady = True
1024 1035
1025 1036 return avgdata_spc, avgdata_cspc, avgdata_dc
1026 1037
1027 1038 def integrate(self, datatime, *args):
1028 1039
1029 1040 if self.__profIndex == 0:
1030 1041 self.__initime = datatime
1031 1042
1032 1043 if self.__byTime:
1033 1044 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(datatime, *args)
1034 1045 else:
1035 1046 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
1036 1047
1037 1048 if not self.__dataReady:
1038 1049 return None, None, None, None
1039 1050
1040 1051 return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc
1041 1052
1042 1053 def run(self, dataOut, n=None, timeInterval=None, overlapping=False):
1043 1054
1044 1055 if n==1:
1045 1056 return
1046 1057
1047 1058 dataOut.flagNoData = True
1048 1059
1049 1060 if not self.isConfig:
1050 1061 self.setup(n, timeInterval, overlapping)
1051 1062 self.isConfig = True
1052 1063
1053 1064 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime,
1054 1065 dataOut.data_spc,
1055 1066 dataOut.data_cspc,
1056 1067 dataOut.data_dc)
1057 1068
1058 1069 if self.__dataReady:
1059 1070
1060 1071 dataOut.data_spc = avgdata_spc
1061 1072 dataOut.data_cspc = avgdata_cspc
1062 1073 dataOut.data_dc = avgdata_dc
1063 1074
1064 1075 dataOut.nIncohInt *= self.n
1065 1076 dataOut.utctime = avgdatatime
1066 1077 dataOut.flagNoData = False
1067 1078
@@ -1,1 +1,1
1 <Project description="Segundo Test" id="191" name="test01"><ReadUnit datatype="SpectraReader" id="1911" inputId="0" name="SpectraReader"><Operation id="19111" name="run" priority="1" type="self"><Parameter format="str" id="191111" name="datatype" value="SpectraReader" /><Parameter format="str" id="191112" name="path" value="/data/CLAIRE/CLAIRE_WINDS_2MHZ/DATA/pdataCLAIRE/2018" /><Parameter format="date" id="191113" name="startDate" value="2018/01/26" /><Parameter format="date" id="191114" name="endDate" value="2018/01/26" /><Parameter format="time" id="191115" name="startTime" value="17:45:00" /><Parameter format="time" id="191116" name="endTime" value="23:59:00" /><Parameter format="int" id="191118" name="online" value="0" /><Parameter format="int" id="191119" name="walk" value="1" /></Operation><Operation id="19112" name="printInfo" priority="2" type="self" /><Operation id="19113" name="printNumberOfBlock" priority="3" type="self" /></ReadUnit><ProcUnit datatype="Parameters" id="1913" inputId="1912" name="ParametersProc"><Operation id="19131" name="run" priority="1" type="self" /><Operation id="19132" name="SpectralFilters" priority="2" type="other"><Parameter format="float" id="191321" name="PositiveLimit" value="1.5" /><Parameter format="float" id="191322" name="NegativeLimit" value="12.5" /></Operation><Operation id="19133" name="PrecipitationProc" priority="3" type="other" /><Operation id="19134" name="ParametersPlot" priority="4" type="other"><Parameter format="int" id="191341" name="id" value="10" /><Parameter format="str" id="191342" name="wintitle" value="First_gg" /><Parameter format="str" id="191343" name="colormap" value="ocean_r" /><Parameter format="int" id="191344" name="zmin" value="00" /><Parameter format="int" id="191345" name="zmax" value="40" /><Parameter format="int" id="191346" name="ymin" value="0" /><Parameter format="int" id="191347" name="ymax" value="11" /><Parameter format="int" id="191348" name="xmin" value="17" /><Parameter format="int" id="191349" name="xmax" value="24" /><Parameter format="int" id="191350" name="save" value="1" /><Parameter format="str" id="191351" name="figpath" value="/data/CLAIRE/CLAIRE_WINDS_2MHZ/DATA/pdataCLAIRE/2018" /></Operation><Operation id="19135" name="SpcParamPlot" priority="5" type="other"><Parameter format="int" id="191351" name="id" value="21" /><Parameter format="str" id="191352" name="wintitle" value="Primer eco removido" /><Parameter format="str" id="191353" name="xaxis" value="velocity" /><Parameter format="int" id="191354" name="showprofile" value="1" /><Parameter format="int" id="191355" name="zmin" value="10" /><Parameter format="int" id="191356" name="zmax" value="40" /><Parameter format="int" id="191357" name="ymin" value="0" /><Parameter format="int" id="191358" name="ymax" value="10" /><Parameter format="int" id="191359" name="Selector" value="1" /></Operation></ProcUnit><ProcUnit datatype="SpectraProc" id="1912" inputId="1911" name="SpectraProc"><Operation id="19121" name="run" priority="1" type="self" /><Operation id="19122" name="setRadarFrequency" priority="2" type="self"><Parameter format="float" id="191221" name="frequency" value="445.09e6" /></Operation></ProcUnit></Project> No newline at end of file
1 <Project description="Segundo Test" id="191" name="test01"><ReadUnit datatype="SpectraReader" id="1911" inputId="0" name="SpectraReader"><Operation id="19111" name="run" priority="1" type="self"><Parameter format="str" id="191111" name="datatype" value="SpectraReader" /><Parameter format="str" id="191112" name="path" value="/data/CLAIRE/CLAIRE_WINDS_2MHZ/DATA/pdataCLAIRE/Extra" /><Parameter format="date" id="191113" name="startDate" value="2018/02/23" /><Parameter format="date" id="191114" name="endDate" value="2018/02/23" /><Parameter format="time" id="191115" name="startTime" value="00:00:00" /><Parameter format="time" id="191116" name="endTime" value="03:59:50" /><Parameter format="int" id="191118" name="online" value="0" /><Parameter format="int" id="191119" name="walk" value="1" /></Operation><Operation id="19112" name="printInfo" priority="2" type="self" /><Operation id="19113" name="printNumberOfBlock" priority="3" type="self" /></ReadUnit><ProcUnit datatype="Parameters" id="1913" inputId="1912" name="ParametersProc"><Operation id="19131" name="run" priority="1" type="self" /><Operation id="19132" name="SpectralFilters" priority="2" type="other"><Parameter format="float" id="191321" name="PositiveLimit" value="1.5" /><Parameter format="float" id="191322" name="NegativeLimit" value="12.5" /></Operation><Operation id="19133" name="FullSpectralAnalysis" priority="3" type="other"><Parameter format="float" id="191331" name="SNRlimit" value="-16" /><Parameter format="float" id="191332" name="Xi01" value="1.500" /><Parameter format="float" id="191333" name="Xi02" value="1.500" /><Parameter format="float" id="191334" name="Xi12" value="0" /><Parameter format="float" id="191335" name="Eta01" value="0.875" /><Parameter format="float" id="191336" name="Eta02" value="-0.875" /><Parameter format="float" id="191337" name="Eta12" value="-1.750" /></Operation><Operation id="19134" name="WindProfilerPlot" priority="4" type="other"><Parameter format="int" id="191341" name="id" value="4" /><Parameter format="str" id="191342" name="wintitle" value="Wind Profiler" /><Parameter format="float" id="191343" name="xmin" value="0" /><Parameter format="float" id="191344" name="xmax" value="4" /><Parameter format="float" id="191345" name="ymin" value="0" /><Parameter format="int" id="191346" name="ymax" value="4" /><Parameter format="float" id="191347" name="zmin" value="-20" /><Parameter format="float" id="191348" name="zmax" value="20" /><Parameter format="float" id="191349" name="SNRmin" value="-20" /><Parameter format="float" id="191350" name="SNRmax" value="20" /><Parameter format="float" id="191351" name="zmin_ver" value="-200" /><Parameter format="float" id="191352" name="zmax_ver" value="200" /><Parameter format="float" id="191353" name="SNRthresh" value="-20" /><Parameter format="int" id="191354" name="save" value="1" /><Parameter format="str" id="191355" name="figpath" value="/data/CLAIRE/CLAIRE_WINDS_2MHZ/DATA/pdataCLAIRE/ImagenesTesis" /></Operation><Operation id="19135" name="ParamWriter" priority="5" type="other"><Parameter format="str" id="191351" name="path" value="/data/CLAIRE/CLAIRE_WINDS_2MHZ/DATA/pdataCLAIRE/ImagenesTesis" /><Parameter format="int" id="191352" name="blocksPerFile" value="500" /><Parameter format="list" id="191353" name="metadataList" value="heightList,timeZone,paramInterval" /><Parameter format="list" id="191354" name="dataList" value="data_output,utctime,utctimeInit" /></Operation></ProcUnit><ProcUnit datatype="SpectraProc" id="1912" inputId="1911" name="SpectraProc"><Operation id="19121" name="run" priority="1" type="self" /><Operation id="19122" name="setRadarFrequency" priority="2" type="self"><Parameter format="float" id="191221" name="frequency" value="445.09e6" /></Operation><Operation id="19123" name="setH0" priority="3" type="self"><Parameter format="float" id="191231" name="h0" value="0.500" /></Operation></ProcUnit></Project> No newline at end of file
@@ -1,151 +1,152
1 1 #!/usr/bin/env python
2 2 import os, sys
3 3
4 4 # path = os.path.dirname(os.getcwd())
5 5 # path = os.path.join(path, 'source')
6 6 # sys.path.insert(0, '../')
7 7
8 8 from schainpy.controller import Project
9 9
10 10 xmin = '15.5'
11 11 xmax = '24'
12 12
13 13
14 14 desc = "ProcBLTR Test"
15 15 filename = "ProcBLTR.xml"
16 figpath = '/media/erick/6F60F7113095A154/BLTR'
16 figpath = '/data/CLAIRE/CLAIRE_WINDS_2MHZ/DATA/pdataCLAIRE/2018'
17 17
18 18 controllerObj = Project()
19 19
20 20
21 21 controllerObj.setup(id='191', name='test01', description=desc)
22 22
23 23 readUnitConfObj = controllerObj.addReadUnit(datatype='BLTRSpectraReader',
24 24 #path='/media/erick/6F60F7113095A154/BLTR/',
25 #path='/data/BLTR',
25 26 path='/home/erick/Documents/Data/BLTR_Data/fdt/',
26 endDate='2017/10/19',
27 startTime='13:00:00',
28 startDate='2016/11/8',
27 endDate='2016/11/01',
28 startTime='0:00:00',
29 startDate='2016/11/01',
29 30 endTime='23:59:59',
30 31
31 32
32 33 online=0,
33 34 walk=0,
34 35 ReadMode='1')
35 36 # expLabel='')
36 37
37 38 # opObj11 = readUnitConfObj.addOperation(name='printNumberOfBlock')
38 39
39 40 procUnitConfObj1 = controllerObj.addProcUnit(datatype='Spectra', inputId=readUnitConfObj.getId())
40 41
41 42
42 43 opObj11 = procUnitConfObj1.addOperation(name='IncohInt', optype='other')
43 44 opObj11.addParameter(name='n', value='2', format='float')
44 45
45 46 opObj10 = procUnitConfObj1.addOperation(name='removeDC')
46 47 #opObj10 = procUnitConfObj1.addOperation(name='removeInterference2')
47 48
48 49 # opObj10 = procUnitConfObj1.addOperation(name='calcMag')
49 50
50 51 # opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='other')
51 52 # opObj11.addParameter(name='id', value='21', format='int')
52 53 # opObj11.addParameter(name='wintitle', value='SpectraCutPlot', format='str')
53 54 # opObj11.addParameter(name='xaxis', value='frequency', format='str')
54 55 # opObj11.addParameter(name='colormap', value='winter', format='str')
55 56 # opObj11.addParameter(name='xmin', value='-0.005', format='float')
56 57 # opObj11.addParameter(name='xmax', value='0.005', format='float')
57 58 # #opObj10 = procUnitConfObj1.addOperation(name='selectChannels')
58 59 # #opObj10.addParameter(name='channelList', value='0,1', format='intlist')
59 60 # opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='other')
60 61 # opObj11.addParameter(name='id', value='21', format='int')
61 62 # opObj11.addParameter(name='wintitle', value='SpectraPlot', format='str')
62 63 # #opObj11.addParameter(name='xaxis', value='Velocity', format='str')
63 64
64 65 # opObj11.addParameter(name='xaxis', value='velocity', format='str')
65 66 # opObj11.addParameter(name='xmin', value='-0.005', format='float')
66 67 # opObj11.addParameter(name='xmax', value='0.005', format='float')
67 68
68 69 # opObj11.addParameter(name='ymin', value='225', format='float')
69 70 # opObj11.addParameter(name='ymax', value='3000', format='float')
70 71 # opObj11.addParameter(name='zmin', value='-100', format='int')
71 72 # opObj11.addParameter(name='zmax', value='-65', format='int')
72 73
73 74 # opObj11 = procUnitConfObj1.addOperation(name='RTIPlot', optype='other')
74 75 # opObj11.addParameter(name='id', value='10', format='int')
75 76 # opObj11.addParameter(name='wintitle', value='RTI', format='str')
76 77 # opObj11.addParameter(name='ymin', value='0', format='float')
77 78 # opObj11.addParameter(name='ymax', value='4000', format='float')
78 79 # #opObj11.addParameter(name='zmin', value='-100', format='int')
79 80 # #opObj11.addParameter(name='zmax', value='-70', format='int')
80 81 # opObj11.addParameter(name='zmin', value='-90', format='int')
81 82 # opObj11.addParameter(name='zmax', value='-40', format='int')
82 83 # opObj11.addParameter(name='showprofile', value='1', format='int')
83 84 # opObj11.addParameter(name='timerange', value=str(2*60*60), format='int')
84 85
85 opObj11 = procUnitConfObj1.addOperation(name='CrossSpectraPlot', optype='other')
86 procUnitConfObj1.addParameter(name='pairsList', value='(0,1),(0,2),(1,2)', format='pairsList')
87 opObj11.addParameter(name='id', value='2005', format='int')
88 opObj11.addParameter(name='wintitle', value='CrossSpectraPlot_ShortPulse', format='str')
89 # opObj11.addParameter(name='exp_code', value='13', format='int')
90 opObj11.addParameter(name='xaxis', value='Velocity', format='str')
86 # opObj11 = procUnitConfObj1.addOperation(name='CrossSpectraPlot', optype='other')
87 # procUnitConfObj1.addParameter(name='pairsList', value='(0,1),(0,2),(1,2)', format='pairsList')
88 # opObj11.addParameter(name='id', value='2005', format='int')
89 # opObj11.addParameter(name='wintitle', value='CrossSpectraPlot_ShortPulse', format='str')
90 # # opObj11.addParameter(name='exp_code', value='13', format='int')
91 # opObj11.addParameter(name='xaxis', value='Velocity', format='str')
91 92 #opObj11.addParameter(name='xmin', value='-10', format='float')
92 93 #opObj11.addParameter(name='xmax', value='10', format='float')
93 94 #opObj11.addParameter(name='ymin', value='225', format='float')
94 95 #opObj11.addParameter(name='ymax', value='3000', format='float')
95 96 #opObj11.addParameter(name='phase_min', value='-4', format='int')
96 97 #opObj11.addParameter(name='phase_max', value='4', format='int')
97 98
98 99 # procUnitConfObj2 = controllerObj.addProcUnit(datatype='CorrelationProc', inputId=procUnitConfObj1.getId())
99 100 # procUnitConfObj2.addParameter(name='pairsList', value='(0,1),(0,2),(1,2)', format='pairsList')
100 101
101 102 procUnitConfObj2 = controllerObj.addProcUnit(datatype='Parameters', inputId=procUnitConfObj1.getId())
102 opObj11 = procUnitConfObj2.addOperation(name='SpectralMoments', optype='other')
103 #opObj11 = procUnitConfObj2.addOperation(name='SpectralMoments', optype='other')
103 104 opObj22 = procUnitConfObj2.addOperation(name='FullSpectralAnalysis', optype='other')
104 opObj22.addParameter(name='SNRlimit', value='7', format='float')
105 opObj22.addParameter(name='SNRlimit', value='2', format='float')
105 106
106 107 opObj22 = procUnitConfObj2.addOperation(name='WindProfilerPlot', optype='other')
107 108 opObj22.addParameter(name='id', value='4', format='int')
108 109 opObj22.addParameter(name='wintitle', value='Wind Profiler', format='str')
109 opObj22.addParameter(name='save', value='1', format='bool')
110 #opObj22.addParameter(name='save', value='1', format='bool')
110 111 # opObj22.addParameter(name='figpath', value = '/home/erick/Pictures', format='str')
111 112
112 113 opObj22.addParameter(name='zmin', value='-20', format='int')
113 114 opObj22.addParameter(name='zmax', value='20', format='int')
114 115 opObj22.addParameter(name='zmin_ver', value='-300', format='float')
115 116 opObj22.addParameter(name='zmax_ver', value='300', format='float')
116 117 opObj22.addParameter(name='SNRmin', value='-5', format='int')
117 118 opObj22.addParameter(name='SNRmax', value='30', format='int')
118 119 # opObj22.addParameter(name='SNRthresh', value='-3.5', format='float')
119 120 opObj22.addParameter(name='xmin', value='0', format='float')
120 121 opObj22.addParameter(name='xmax', value='24', format='float')
121 122 opObj22.addParameter(name='ymin', value='225', format='float')
122 123 #opObj22.addParameter(name='ymax', value='2000', format='float')
123 124 opObj22.addParameter(name='save', value='1', format='int')
124 125 opObj22.addParameter(name='figpath', value=figpath, format='str')
125 126
126 127
127 128 # opObj11.addParameter(name='pairlist', value='(1,0),(0,2),(1,2)', format='pairsList')
128 129 #opObj10 = procUnitConfObj1.addOperation(name='selectHeights')
129 130 #opObj10.addParameter(name='minHei', value='225', format='float')
130 131 #opObj10.addParameter(name='maxHei', value='1000', format='float')
131 132
132 133 # opObj11 = procUnitConfObj1.addOperation(name='CoherenceMap', optype='other')
133 134 # opObj11.addParameter(name='id', value='102', format='int')
134 135 # opObj11.addParameter(name='wintitle', value='Coherence', format='str')
135 136 # opObj11.addParameter(name='ymin', value='225', format='float')
136 137 # opObj11.addParameter(name='ymax', value='4000', format='float')
137 138
138 139 # opObj11.addParameter(name='phase_cmap', value='jet', format='str')
139 140 # opObj11.addParameter(name='xmin', value='8.5', format='float')
140 141 # opObj11.addParameter(name='xmax', value='9.5', format='float')
141 142 # opObj11.addParameter(name='figpath', value=figpath, format='str')
142 143 # opObj11.addParameter(name='save', value=1, format='bool')
143 144 # opObj11.addParameter(name='pairsList', value='(1,0),(3,2)', format='pairsList')
144 145
145 146 # opObj12 = procUnitConfObj1.addOperation(name='PublishData', optype='other')
146 147 # opObj12.addParameter(name='zeromq', value=1, format='int')
147 148 # opObj12.addParameter(name='verbose', value=0, format='bool')
148 149 # opObj12.addParameter(name='server', value='erick2', format='str')
149 150 controllerObj.start()
150 151
151 152
@@ -1,239 +1,239
1 1 import os, sys
2 2
3 3 from schainpy.controller import Project
4 4
5 5 if __name__ == '__main__':
6 6
7 7 desc = "Segundo Test"
8 8 filename = "schain.xml"
9 9
10 pathW='/media/erick/6F60F7113095A154/CLAIRE/CLAIRE_WINDS_2MHZ/DATA/pdataCLAIRE/Extra'
11 figpath = '/media/erick/6F60F7113095A154/CLAIRE/CLAIRE_WINDS_2MHZ/DATA/pdataCLAIRE/Extra'
10 pathW='/data/CLAIRE/CLAIRE_WINDS_2MHZ/DATA/pdataCLAIRE/Extra'
11 figpath = '/data/CLAIRE/CLAIRE_WINDS_2MHZ/DATA/pdataCLAIRE/Extra'
12 12
13 13 controllerObj = Project()
14 14
15 15 controllerObj.setup(id='191', name='test01', description=desc)
16 16
17 17 readUnitConfObj = controllerObj.addReadUnit(datatype='VoltageReader',
18 path='/media/erick/6F60F7113095A154/CLAIRE/CLAIRE_WINDS_2MHZ/DATA/',
18 path='/data/CLAIRE/CLAIRE_WINDS_2MHZ/DATA',
19 19 #path='/home/erick/Documents/Data/Claire_Data/raw',
20 startDate='2018/02/01',
21 endDate='2018/02/01',
22 startTime='12:00:00',
23 endTime='20:00:00',
20 startDate='2018/02/22',
21 endDate='2018/02/24',
22 startTime='00:00:00',
23 endTime='23:59:00',
24 24 online=0,
25 25 walk=1)
26 26
27 27 opObj00 = readUnitConfObj.addOperation(name='printInfo')
28 28 #
29 29 # procUnitConfObj0 = controllerObj.addProcUnit(datatype='VoltageProc',
30 30 # inputId=readUnitConfObj.getId())
31 31 #
32 32 # opObj10 = procUnitConfObj0.addOperation(name='selectHeights')
33 33 # opObj10.addParameter(name='minHei', value='0', format='float')
34 34 # opObj10.addParameter(name='maxHei', value='8', format='float')
35 35 #
36 36 # opObj10 = procUnitConfObj0.addOperation(name='filterByHeights')
37 37 # opObj10.addParameter(name='window', value='2', format='float')
38 38 #
39 39 # opObj10 = procUnitConfObj0.addOperation(name='Decoder', optype='external')
40 40 # opObj10.addParameter(name='code', value='1,-1', format='intlist')
41 41 # opObj10.addParameter(name='nCode', value='2', format='float')
42 42 # opObj10.addParameter(name='nBaud', value='1', format='float')
43 43 #
44 44 #
45 45 # opObj10 = procUnitConfObj0.addOperation(name='CohInt', optype='external')
46 46 # opObj10.addParameter(name='n', value='1296', format='float')
47 47
48 48 opObj00 = readUnitConfObj.addOperation(name='printNumberOfBlock')
49 49
50 50 procUnitConfObj0 = controllerObj.addProcUnit(datatype='VoltageProc',
51 51 inputId=readUnitConfObj.getId())
52 52
53 53
54 54 opObj10 = procUnitConfObj0.addOperation(name='setRadarFrequency')
55 55 opObj10.addParameter(name='frequency', value='445.09e6', format='float')
56 56
57 57 opObj10 = procUnitConfObj0.addOperation(name='CohInt', optype='external')
58 58 opObj10.addParameter(name='n', value='2', format='float')
59 59
60 60 #opObj10 = procUnitConfObj0.addOperation(name='selectHeights')
61 61 #opObj10.addParameter(name='minHei', value='1', format='float')
62 62 #opObj10.addParameter(name='maxHei', value='15', format='float')
63 63
64 64 #opObj10 = procUnitConfObj0.addOperation(name='selectFFTs')
65 65 #opObj10.addParameter(name='minHei', value='', format='float')
66 66 #opObj10.addParameter(name='maxHei', value='', format='float')
67 67
68 68 procUnitConfObj1 = controllerObj.addProcUnit(datatype='SpectraProc',
69 69 inputId=procUnitConfObj0.getId())
70 70
71 71 # Creating a processing object with its parameters
72 72 # schainpy.model.proc.jroproc_spectra.SpectraProc.run()
73 73 # If you need to add more parameters can use the "addParameter method"
74 74 procUnitConfObj1.addParameter(name='nFFTPoints', value='256', format='int')
75 75
76 76
77 77 opObj10 = procUnitConfObj1.addOperation(name='removeDC')
78 78 #opObj10 = procUnitConfObj1.addOperation(name='removeInterference')
79 79 opObj10 = procUnitConfObj1.addOperation(name='IncohInt', optype='external')
80 80 opObj10.addParameter(name='n', value='10', format='float')
81 81
82 82
83 83
84 84 #opObj10 = procUnitConfObj1.addOperation(name='selectFFTs')
85 85 #opObj10.addParameter(name='minFFT', value='-15', format='float')
86 86 #opObj10.addParameter(name='maxFFT', value='15', format='float')
87 87
88 88
89 89
90 90 opObj10 = procUnitConfObj1.addOperation(name='SpectraWriter', optype='other')
91 91 opObj10.addParameter(name='blocksPerFile', value='64', format = 'int')
92 92 opObj10.addParameter(name='path', value=pathW)
93 93 # Using internal methods
94 94 # schainpy.model.proc.jroproc_spectra.SpectraProc.selectChannels()
95 95 # opObj10 = procUnitConfObj1.addOperation(name='selectChannels')
96 96 # opObj10.addParameter(name='channelList', value='0,1', format='intlist')
97 97
98 98 # Using internal methods
99 99 # schainpy.model.proc.jroproc_spectra.SpectraProc.selectHeights()
100 100 # opObj10 = procUnitConfObj1.addOperation(name='selectHeights')
101 101 # opObj10.addParameter(name='minHei', value='90', format='float')
102 102 # opObj10.addParameter(name='maxHei', value='180', format='float')
103 103
104 104 # Using external methods (new modules)
105 105 # #schainpy.model.proc.jroproc_spectra.IncohInt.setup()
106 106 # opObj12 = procUnitConfObj1.addOperation(name='IncohInt', optype='other')
107 107 # opObj12.addParameter(name='n', value='1', format='int')
108 108
109 109 # Using external methods (new modules)
110 110 # schainpy.model.graphics.jroplot_spectra.SpectraPlot.setup()
111 111 opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='external')
112 112 opObj11.addParameter(name='id', value='11', format='int')
113 113 opObj11.addParameter(name='wintitle', value='SpectraPlot', format='str')
114 114 opObj11.addParameter(name='xaxis', value='velocity', format='str')
115 115 # opObj11.addParameter(name='xmin', value='-10', format='int')
116 116 # opObj11.addParameter(name='xmax', value='10', format='int')
117 117
118 118 # opObj11.addParameter(name='ymin', value='1', format='float')
119 119 # opObj11.addParameter(name='ymax', value='3', format='int')
120 120 #opObj11.addParameter(name='zmin', value='10', format='int')
121 121 #opObj11.addParameter(name='zmax', value='35', format='int')
122 122 # opObj11.addParameter(name='save', value='2', format='int')
123 123 # opObj11.addParameter(name='save', value='5', format='int')
124 124 # opObj11.addParameter(name='figpath', value=figpath, format='str')
125 125
126 126
127 127 opObj11 = procUnitConfObj1.addOperation(name='CrossSpectraPlot', optype='other')
128 128 procUnitConfObj1.addParameter(name='pairsList', value='(0,1),(0,2),(1,2)', format='pairsList')
129 129 opObj11.addParameter(name='id', value='2005', format='int')
130 130 #opObj11.addParameter(name='wintitle', value='CrossSpectraPlot_ShortPulse', format='str')
131 131 #opObj11.addParameter(name='exp_code', value='13', format='int')
132 132 opObj11.addParameter(name='xaxis', value='Velocity', format='str')
133 133 #opObj11.addParameter(name='xmin', value='-6', format='float')
134 134 #opObj11.addParameter(name='xmax', value='6', format='float')
135 135 opObj11.addParameter(name='zmin', value='15', format='float')
136 136 opObj11.addParameter(name='zmax', value='50', format='float')
137 137 opObj11.addParameter(name='ymin', value='0', format='float')
138 138 opObj11.addParameter(name='ymax', value='7', format='float')
139 139 #opObj11.addParameter(name='phase_min', value='-4', format='int')
140 140 #opObj11.addParameter(name='phase_max', value='4', format='int')
141 141 #
142 142
143 143 # Using external methods (new modules)
144 144 # schainpy.model.graphics.jroplot_spectra.RTIPlot.setup()
145 145 opObj11 = procUnitConfObj1.addOperation(name='RTIPlot', optype='other')
146 146 opObj11.addParameter(name='id', value='30', format='int')
147 147 opObj11.addParameter(name='wintitle', value='RTI', format='str')
148 148 opObj11.addParameter(name='zmin', value='15', format='int')
149 149 opObj11.addParameter(name='zmax', value='40', format='int')
150 150 opObj11.addParameter(name='ymin', value='1', format='int')
151 151 opObj11.addParameter(name='ymax', value='7', format='int')
152 152 opObj11.addParameter(name='showprofile', value='1', format='int')
153 153 # opObj11.addParameter(name='timerange', value=str(5*60*60*60), format='int')
154 154 #opObj11.addParameter(name='xmin', value='1', format='float')
155 155 #opObj11.addParameter(name='xmax', value='6', format='float')
156 156 opObj11.addParameter(name='save', value='1', format='int')
157 157 opObj11.addParameter(name='figpath', value=figpath, format='str')
158 158
159 159
160 160 # '''#########################################################################################'''
161 161 #
162 162 #
163 163 # procUnitConfObj2 = controllerObj.addProcUnit(datatype='Parameters', inputId=procUnitConfObj1.getId())
164 164 # opObj11 = procUnitConfObj2.addOperation(name='SpectralMoments', optype='other')
165 165 #
166 166 # '''
167 167 # # Discriminacion de ecos
168 168 # opObj11 = procUnitConfObj2.addOperation(name='GaussianFit', optype='other')
169 169 # opObj11.addParameter(name='SNRlimit', value='0', format='int')
170 170 # '''
171 171 #
172 172 # '''
173 173 # # Estimacion de Precipitacion
174 174 # opObj11 = procUnitConfObj2.addOperation(name='PrecipitationProc', optype='other')
175 175 # '''
176 176 #
177 177 # opObj22 = procUnitConfObj2.addOperation(name='FullSpectralAnalysis', optype='other')
178 178 #
179 179 # opObj22.addParameter(name='SNRlimit', value='-10', format='float')
180 180 # opObj22.addParameter(name='E01', value='1.500', format='float')
181 181 # opObj22.addParameter(name='E02', value='1.500', format='float')
182 182 # opObj22.addParameter(name='E12', value='0', format='float')
183 183 # opObj22.addParameter(name='N01', value='0.875', format='float')
184 184 # opObj22.addParameter(name='N02', value='-0.875', format='float')
185 185 # opObj22.addParameter(name='N12', value='-1.750', format='float')
186 186 #
187 187 #
188 188 # opObj22 = procUnitConfObj2.addOperation(name='WindProfilerPlot', optype='other')
189 189 # opObj22.addParameter(name='id', value='4', format='int')
190 190 # opObj22.addParameter(name='wintitle', value='Wind Profiler', format='str')
191 191 # opObj22.addParameter(name='save', value='1', format='bool')
192 192 # opObj22.addParameter(name='xmin', value='0', format='float')
193 193 # opObj22.addParameter(name='xmax', value='6', format='float')
194 194 # opObj22.addParameter(name='ymin', value='1', format='float')
195 195 # opObj22.addParameter(name='ymax', value='3.5', format='float')
196 196 # opObj22.addParameter(name='zmin', value='-1', format='float')
197 197 # opObj22.addParameter(name='zmax', value='1', format='float')
198 198 # opObj22.addParameter(name='SNRmin', value='-15', format='float')
199 199 # opObj22.addParameter(name='SNRmax', value='20', format='float')
200 200 # opObj22.addParameter(name='zmin_ver', value='-200', format='float')
201 201 # opObj22.addParameter(name='zmax_ver', value='200', format='float')
202 202 # opObj22.addParameter(name='save', value='1', format='int')
203 203 # opObj22.addParameter(name='figpath', value=figpath, format='str')
204 204 #
205 205 #
206 206 #
207 207 # #opObj11.addParameter(name='zmin', value='75', format='int')
208 208 #
209 209 # #opObj12 = procUnitConfObj2.addOperation(name='ParametersPlot', optype='other')
210 210 # #opObj12.addParameter(name='id',value='4',format='int')
211 211 # #opObj12.addParameter(name='wintitle',value='First_gg',format='str')
212 212 # '''
213 213 # #Ploteo de Discriminacion de Gaussianas
214 214 #
215 215 # opObj11 = procUnitConfObj2.addOperation(name='FitGauPlot', optype='other')
216 216 # opObj11.addParameter(name='id', value='21', format='int')
217 217 # opObj11.addParameter(name='wintitle', value='Rainfall Gaussian', format='str')
218 218 # opObj11.addParameter(name='xaxis', value='velocity', format='str')
219 219 # opObj11.addParameter(name='showprofile', value='1', format='int')
220 220 # opObj11.addParameter(name='zmin', value='75', format='int')
221 221 # opObj11.addParameter(name='zmax', value='100', format='int')
222 222 # opObj11.addParameter(name='GauSelector', value='1', format='int')
223 223 # #opObj11.addParameter(name='save', value='1', format='int')
224 224 # #opObj11.addParameter(name='figpath', value='/home/erick/Documents/Data/d2015106')
225 225 #
226 226 # opObj11 = procUnitConfObj2.addOperation(name='FitGauPlot', optype='other')
227 227 # opObj11.addParameter(name='id', value='22', format='int')
228 228 # opObj11.addParameter(name='wintitle', value='Wind Gaussian', format='str')
229 229 # opObj11.addParameter(name='xaxis', value='velocity', format='str')
230 230 # opObj11.addParameter(name='showprofile', value='1', format='int')
231 231 # opObj11.addParameter(name='zmin', value='75', format='int')
232 232 # opObj11.addParameter(name='zmax', value='100', format='int')
233 233 # opObj11.addParameter(name='GauSelector', value='0', format='int')
234 234 # '''
235 235 #
236 236 #
237 237
238 238
239 239 controllerObj.start()
General Comments 0
You need to be logged in to leave comments. Login now