##// END OF EJS Templates
GetMoments bug fixed
Julio Valdez -
r549:7ef3a32aff91
parent child
Show More
@@ -1,1770 +1,1780
1 1 import numpy
2 2 import math
3 3 from scipy import optimize
4 4 from scipy import interpolate
5 5 from scipy import signal
6 6 from scipy import stats
7 7 import re
8 8 import datetime
9 9 import copy
10 10 import sys
11 11 import importlib
12 12 import itertools
13 13
14 14 from jroproc_base import ProcessingUnit, Operation
15 15 from model.data.jrodata import Parameters
16 16
17 17
18 18 class ParametersProc(ProcessingUnit):
19 19
20 20 nSeconds = None
21 21
22 22 def __init__(self):
23 23 ProcessingUnit.__init__(self)
24 24
25 25 # self.objectDict = {}
26 26 self.buffer = None
27 27 self.firstdatatime = None
28 28 self.profIndex = 0
29 29 self.dataOut = Parameters()
30 30
31 31 def __updateObjFromInput(self):
32 32
33 33 self.dataOut.inputUnit = self.dataIn.type
34 34
35 35 self.dataOut.timeZone = self.dataIn.timeZone
36 36 self.dataOut.dstFlag = self.dataIn.dstFlag
37 37 self.dataOut.errorCount = self.dataIn.errorCount
38 38 self.dataOut.useLocalTime = self.dataIn.useLocalTime
39 39
40 40 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
41 41 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
42 42 self.dataOut.channelList = self.dataIn.channelList
43 43 self.dataOut.heightList = self.dataIn.heightList
44 44 self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')])
45 45 # self.dataOut.nHeights = self.dataIn.nHeights
46 46 # self.dataOut.nChannels = self.dataIn.nChannels
47 47 self.dataOut.nBaud = self.dataIn.nBaud
48 48 self.dataOut.nCode = self.dataIn.nCode
49 49 self.dataOut.code = self.dataIn.code
50 50 # self.dataOut.nProfiles = self.dataOut.nFFTPoints
51 51 self.dataOut.flagTimeBlock = self.dataIn.flagTimeBlock
52 52 self.dataOut.utctime = self.firstdatatime
53 53 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada
54 54 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip
55 55 # self.dataOut.nCohInt = self.dataIn.nCohInt
56 56 # self.dataOut.nIncohInt = 1
57 57 self.dataOut.ippSeconds = self.dataIn.ippSeconds
58 58 # self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
59 59 self.dataOut.timeInterval = self.dataIn.timeInterval
60 60 self.dataOut.heightList = self.dataIn.getHeiRange()
61 61 self.dataOut.frequency = self.dataIn.frequency
62 62
63 63 def run(self, nSeconds = None, nProfiles = None):
64 64
65 65
66 66
67 67 if self.firstdatatime == None:
68 68 self.firstdatatime = self.dataIn.utctime
69 69
70 70 #---------------------- Voltage Data ---------------------------
71 71
72 72 if self.dataIn.type == "Voltage":
73 73 self.dataOut.flagNoData = True
74 74 if nSeconds != None:
75 75 self.nSeconds = nSeconds
76 76 self.nProfiles= int(numpy.floor(nSeconds/(self.dataIn.ippSeconds*self.dataIn.nCohInt)))
77 77
78 78 if self.buffer == None:
79 79 self.buffer = numpy.zeros((self.dataIn.nChannels,
80 80 self.nProfiles,
81 81 self.dataIn.nHeights),
82 82 dtype='complex')
83 83
84 84 self.buffer[:,self.profIndex,:] = self.dataIn.data.copy()
85 85 self.profIndex += 1
86 86
87 87 if self.profIndex == self.nProfiles:
88 88
89 89 self.__updateObjFromInput()
90 90 self.dataOut.data_pre = self.buffer.copy()
91 91 self.dataOut.paramInterval = nSeconds
92 92 self.dataOut.flagNoData = False
93 93
94 94 self.buffer = None
95 95 self.firstdatatime = None
96 96 self.profIndex = 0
97 97 return
98 98
99 99 #---------------------- Spectra Data ---------------------------
100 100
101 101 if self.dataIn.type == "Spectra":
102 102 self.dataOut.data_pre = self.dataIn.data_spc.copy()
103 103 self.dataOut.abscissaList = self.dataIn.getVelRange(1)
104 104 self.dataOut.noise = self.dataIn.getNoise()
105 105 self.dataOut.normFactor = self.dataIn.normFactor
106 self.dataOut.groupList = self.dataIn.pairsList
106 107 self.dataOut.flagNoData = False
107 108
108 109 #---------------------- Correlation Data ---------------------------
109 110
110 111 if self.dataIn.type == "Correlation":
111 112 lagRRange = self.dataIn.lagR
112 113 indR = numpy.where(lagRRange == 0)[0][0]
113 114
114 115 self.dataOut.data_pre = self.dataIn.data_corr.copy()[:,:,indR,:]
115 116 self.dataOut.abscissaList = self.dataIn.getLagTRange(1)
116 117 self.dataOut.noise = self.dataIn.noise
117 118 self.dataOut.normFactor = self.dataIn.normFactor
118 119 self.dataOut.data_SNR = self.dataIn.SNR
119 120 self.dataOut.groupList = self.dataIn.pairsList
120 121 self.dataOut.flagNoData = False
121 122
122 123 #---------------------- Correlation Data ---------------------------
123 124
124 125 if self.dataIn.type == "Parameters":
125 126 self.dataOut.copy(self.dataIn)
126 127 self.dataOut.flagNoData = False
127 128
128 129 return True
129 130
130 131 self.__updateObjFromInput()
131 132 self.firstdatatime = None
132 133 self.dataOut.utctimeInit = self.dataIn.utctime
133 134 self.dataOut.outputInterval = self.dataIn.timeInterval
134 135
135 136 #------------------- Get Moments ----------------------------------
136 137 def GetMoments(self, channelList = None):
137 138 '''
138 139 Function GetMoments()
139 140
140 141 Input:
141 142 channelList : simple channel list to select e.g. [2,3,7]
142 143 self.dataOut.data_pre
143 144 self.dataOut.abscissaList
144 145 self.dataOut.noise
145 146
146 147 Affected:
147 148 self.dataOut.data_param
148 149 self.dataOut.data_SNR
149 150
150 151 '''
151 152 data = self.dataOut.data_pre
152 153 absc = self.dataOut.abscissaList[:-1]
153 154 noise = self.dataOut.noise
154 155
155 156 data_param = numpy.zeros((data.shape[0], 4, data.shape[2]))
156 157
157 158 if channelList== None:
158 159 channelList = self.dataIn.channelList
159 160 self.dataOut.channelList = channelList
160 161
161 162 for ind in channelList:
162 163 data_param[ind,:,:] = self.__calculateMoments(data[ind,:,:], absc, noise[ind])
163 164
164 165 self.dataOut.data_param = data_param[:,1:,:]
165 166 self.dataOut.data_SNR = data_param[:,0]
166 167 return
167 168
168 169 def __calculateMoments(self, oldspec, oldfreq, n0, nicoh = None, graph = None, smooth = None, type1 = None, fwindow = None, snrth = None, dc = None, aliasing = None, oldfd = None, wwauto = None):
169 170
170 171 if (nicoh == None): nicoh = 1
171 172 if (graph == None): graph = 0
172 173 if (smooth == None): smooth = 0
173 174 elif (self.smooth < 3): smooth = 0
174 175
175 176 if (type1 == None): type1 = 0
176 177 if (fwindow == None): fwindow = numpy.zeros(oldfreq.size) + 1
177 178 if (snrth == None): snrth = -3
178 179 if (dc == None): dc = 0
179 180 if (aliasing == None): aliasing = 0
180 181 if (oldfd == None): oldfd = 0
181 182 if (wwauto == None): wwauto = 0
182 183
183 184 if (n0 < 1.e-20): n0 = 1.e-20
184 185
185 186 freq = oldfreq
186 187 vec_power = numpy.zeros(oldspec.shape[1])
187 188 vec_fd = numpy.zeros(oldspec.shape[1])
188 189 vec_w = numpy.zeros(oldspec.shape[1])
189 190 vec_snr = numpy.zeros(oldspec.shape[1])
190 191
191 192 for ind in range(oldspec.shape[1]):
192 193
193 194 spec = oldspec[:,ind]
194 195 aux = spec*fwindow
195 196 max_spec = aux.max()
196 197 m = list(aux).index(max_spec)
197 198
198 199 #Smooth
199 200 if (smooth == 0): spec2 = spec
200 201 else: spec2 = scipy.ndimage.filters.uniform_filter1d(spec,size=smooth)
201 202
202 203 # Calculo de Momentos
203 204 bb = spec2[range(m,spec2.size)]
204 205 bb = (bb<n0).nonzero()
205 206 bb = bb[0]
206 207
207 208 ss = spec2[range(0,m + 1)]
208 209 ss = (ss<n0).nonzero()
209 210 ss = ss[0]
210 211
211 212 if (bb.size == 0):
212 213 bb0 = spec.size - 1 - m
213 214 else:
214 215 bb0 = bb[0] - 1
215 216 if (bb0 < 0):
216 217 bb0 = 0
217 218
218 219 if (ss.size == 0): ss1 = 1
219 220 else: ss1 = max(ss) + 1
220 221
221 222 if (ss1 > m): ss1 = m
222 223
223 224 valid = numpy.asarray(range(int(m + bb0 - ss1 + 1))) + ss1
224 225 power = ((spec2[valid] - n0)*fwindow[valid]).sum()
225 226 fd = ((spec2[valid]- n0)*freq[valid]*fwindow[valid]).sum()/power
226 227 w = math.sqrt(((spec2[valid] - n0)*fwindow[valid]*(freq[valid]- fd)**2).sum()/power)
227 228 snr = (spec2.mean()-n0)/n0
228 229
229 230 if (snr < 1.e-20) :
230 231 snr = 1.e-20
231 232
232 233 vec_power[ind] = power
233 234 vec_fd[ind] = fd
234 235 vec_w[ind] = w
235 236 vec_snr[ind] = snr
236 237
237 238 moments = numpy.vstack((vec_snr, vec_power, vec_fd, vec_w))
238 239 return moments
239 240
241 #------------------ Get SA Parameters --------------------------
242 def GetSAParameters(self):
243 data = self.dataOut.data_pre
244 crossdata = self.dataIn.data_cspc
245 a = 1
246
247
248
240 249 #------------------- Get Lags ----------------------------------
241 250
242 251 def GetLags(self):
243 252 '''
244 253 Function GetMoments()
245 254
246 255 Input:
247 256 self.dataOut.data_pre
248 257 self.dataOut.abscissaList
249 258 self.dataOut.noise
250 259 self.dataOut.normFactor
251 260 self.dataOut.data_SNR
252 261 self.dataOut.groupList
253 262 self.dataOut.nChannels
254 263
255 264 Affected:
256 265 self.dataOut.data_param
257 266
258 267 '''
268
259 269 data = self.dataOut.data_pre
260 270 normFactor = self.dataOut.normFactor
261 271 nHeights = self.dataOut.nHeights
262 272 absc = self.dataOut.abscissaList[:-1]
263 273 noise = self.dataOut.noise
264 274 SNR = self.dataOut.data_SNR
265 275 pairsList = self.dataOut.groupList
266 276 nChannels = self.dataOut.nChannels
267 277 pairsAutoCorr, pairsCrossCorr = self.__getPairsAutoCorr(pairsList, nChannels)
268 278 self.dataOut.data_param = numpy.zeros((len(pairsCrossCorr)*2 + 1, nHeights))
269 279
270 280 dataNorm = numpy.abs(data)
271 281 for l in range(len(pairsList)):
272 282 dataNorm[l,:,:] = dataNorm[l,:,:]/normFactor[l,:]
273 283
274 284 self.dataOut.data_param[:-1,:] = self.__calculateTaus(dataNorm, pairsCrossCorr, pairsAutoCorr, absc)
275 285 self.dataOut.data_param[-1,:] = self.__calculateLag1Phase(data, pairsAutoCorr, absc)
276 286 return
277 287
278 288 def __getPairsAutoCorr(self, pairsList, nChannels):
279 289
280 290 pairsAutoCorr = numpy.zeros(nChannels, dtype = 'int')*numpy.nan
281 291
282 292 for l in range(len(pairsList)):
283 293 firstChannel = pairsList[l][0]
284 294 secondChannel = pairsList[l][1]
285 295
286 296 #Obteniendo pares de Autocorrelacion
287 297 if firstChannel == secondChannel:
288 298 pairsAutoCorr[firstChannel] = int(l)
289 299
290 300 pairsAutoCorr = pairsAutoCorr.astype(int)
291 301
292 302 pairsCrossCorr = range(len(pairsList))
293 303 pairsCrossCorr = numpy.delete(pairsCrossCorr,pairsAutoCorr)
294 304
295 305 return pairsAutoCorr, pairsCrossCorr
296 306
297 307 def __calculateTaus(self, data, pairsCrossCorr, pairsAutoCorr, lagTRange):
298 308
299 309 Pt0 = data.shape[1]/2
300 310 #Funcion de Autocorrelacion
301 311 dataAutoCorr = stats.nanmean(data[pairsAutoCorr,:,:], axis = 0)
302 312
303 313 #Obtencion Indice de TauCross
304 314 indCross = data[pairsCrossCorr,:,:].argmax(axis = 1)
305 315 #Obtencion Indice de TauAuto
306 316 indAuto = numpy.zeros(indCross.shape,dtype = 'int')
307 317 CCValue = data[pairsCrossCorr,Pt0,:]
308 318 for i in range(pairsCrossCorr.size):
309 319 indAuto[i,:] = numpy.abs(dataAutoCorr - CCValue[i,:]).argmin(axis = 0)
310 320
311 321 #Obtencion de TauCross y TauAuto
312 322 tauCross = lagTRange[indCross]
313 323 tauAuto = lagTRange[indAuto]
314 324
315 325 Nan1, Nan2 = numpy.where(tauCross == lagTRange[0])
316 326
317 327 tauCross[Nan1,Nan2] = numpy.nan
318 328 tauAuto[Nan1,Nan2] = numpy.nan
319 329 tau = numpy.vstack((tauCross,tauAuto))
320 330
321 331 return tau
322 332
323 333 def __calculateLag1Phase(self, data, pairs, lagTRange):
324 334 data1 = stats.nanmean(data[pairs,:,:], axis = 0)
325 335 lag1 = numpy.where(lagTRange == 0)[0][0] + 1
326 336
327 337 phase = numpy.angle(data1[lag1,:])
328 338
329 339 return phase
330 340 #------------------- Detect Meteors ------------------------------
331 341
332 342 def DetectMeteors(self, hei_ref = None, tauindex = 0,
333 343 predefinedPhaseShifts = None, centerReceiverIndex = 2,
334 344 cohDetection = False, cohDet_timeStep = 1, cohDet_thresh = 25,
335 345 noise_timeStep = 4, noise_multiple = 4,
336 346 multDet_timeLimit = 1, multDet_rangeLimit = 3,
337 347 phaseThresh = 20, SNRThresh = 8,
338 348 hmin = 70, hmax=110, azimuth = 0) :
339 349
340 350 '''
341 351 Function DetectMeteors()
342 352 Project developed with paper:
343 353 HOLDSWORTH ET AL. 2004
344 354
345 355 Input:
346 356 self.dataOut.data_pre
347 357
348 358 centerReceiverIndex: From the channels, which is the center receiver
349 359
350 360 hei_ref: Height reference for the Beacon signal extraction
351 361 tauindex:
352 362 predefinedPhaseShifts: Predefined phase offset for the voltge signals
353 363
354 364 cohDetection: Whether to user Coherent detection or not
355 365 cohDet_timeStep: Coherent Detection calculation time step
356 366 cohDet_thresh: Coherent Detection phase threshold to correct phases
357 367
358 368 noise_timeStep: Noise calculation time step
359 369 noise_multiple: Noise multiple to define signal threshold
360 370
361 371 multDet_timeLimit: Multiple Detection Removal time limit in seconds
362 372 multDet_rangeLimit: Multiple Detection Removal range limit in km
363 373
364 374 phaseThresh: Maximum phase difference between receiver to be consider a meteor
365 375 SNRThresh: Minimum SNR threshold of the meteor signal to be consider a meteor
366 376
367 377 hmin: Minimum Height of the meteor to use it in the further wind estimations
368 378 hmax: Maximum Height of the meteor to use it in the further wind estimations
369 379 azimuth: Azimuth angle correction
370 380
371 381 Affected:
372 382 self.dataOut.data_param
373 383
374 384 Rejection Criteria (Errors):
375 385 0: No error; analysis OK
376 386 1: SNR < SNR threshold
377 387 2: angle of arrival (AOA) ambiguously determined
378 388 3: AOA estimate not feasible
379 389 4: Large difference in AOAs obtained from different antenna baselines
380 390 5: echo at start or end of time series
381 391 6: echo less than 5 examples long; too short for analysis
382 392 7: echo rise exceeds 0.3s
383 393 8: echo decay time less than twice rise time
384 394 9: large power level before echo
385 395 10: large power level after echo
386 396 11: poor fit to amplitude for estimation of decay time
387 397 12: poor fit to CCF phase variation for estimation of radial drift velocity
388 398 13: height unresolvable echo: not valid height within 70 to 110 km
389 399 14: height ambiguous echo: more then one possible height within 70 to 110 km
390 400 15: radial drift velocity or projected horizontal velocity exceeds 200 m/s
391 401 16: oscilatory echo, indicating event most likely not an underdense echo
392 402
393 403 17: phase difference in meteor Reestimation
394 404
395 405 Data Storage:
396 406 Meteors for Wind Estimation (8):
397 407 Day Hour | Range Height
398 408 Azimuth Zenith errorCosDir
399 409 VelRad errorVelRad
400 410 TypeError
401 411
402 412 '''
403 413 #Get Beacon signal
404 414 newheis = numpy.where(self.dataOut.heightList>self.dataOut.radarControllerHeaderObj.Taus[tauindex])
405 415
406 416 if hei_ref != None:
407 417 newheis = numpy.where(self.dataOut.heightList>hei_ref)
408 418
409 419 heiRang = self.dataOut.getHeiRange()
410 420 #Pairs List
411 421 pairslist = []
412 422 nChannel = self.dataOut.nChannels
413 423 for i in range(nChannel):
414 424 if i != centerReceiverIndex:
415 425 pairslist.append((centerReceiverIndex,i))
416 426
417 427 #****************REMOVING HARDWARE PHASE DIFFERENCES***************
418 428 # see if the user put in pre defined phase shifts
419 429 voltsPShift = self.dataOut.data_pre.copy()
420 430
421 431 if predefinedPhaseShifts != None:
422 432 hardwarePhaseShifts = numpy.array(predefinedPhaseShifts)*numpy.pi/180
423 433 else:
424 434 #get hardware phase shifts using beacon signal
425 435 hardwarePhaseShifts = self.__getHardwarePhaseDiff(self.dataOut.data_pre, pairslist, newheis, 10)
426 436 hardwarePhaseShifts = numpy.insert(hardwarePhaseShifts,centerReceiverIndex,0)
427 437
428 438 voltsPShift = numpy.zeros((self.dataOut.data_pre.shape[0],self.dataOut.data_pre.shape[1],self.dataOut.data_pre.shape[2]), dtype = 'complex')
429 439 for i in range(self.dataOut.data_pre.shape[0]):
430 440 voltsPShift[i,:,:] = self.__shiftPhase(self.dataOut.data_pre[i,:,:], hardwarePhaseShifts[i])
431 441 #******************END OF REMOVING HARDWARE PHASE DIFFERENCES*********
432 442
433 443 #Remove DC
434 444 voltsDC = numpy.mean(voltsPShift,1)
435 445 voltsDC = numpy.mean(voltsDC,1)
436 446 for i in range(voltsDC.shape[0]):
437 447 voltsPShift[i] = voltsPShift[i] - voltsDC[i]
438 448
439 449 #Don't considerate last heights, theyre used to calculate Hardware Phase Shift
440 450 voltsPShift = voltsPShift[:,:,:newheis[0][0]]
441 451
442 452 #************ FIND POWER OF DATA W/COH OR NON COH DETECTION (3.4) **********
443 453 #Coherent Detection
444 454 if cohDetection:
445 455 #use coherent detection to get the net power
446 456 cohDet_thresh = cohDet_thresh*numpy.pi/180
447 457 voltsPShift = self.__coherentDetection(voltsPShift, cohDet_timeStep, self.dataOut.timeInterval, pairslist, cohDet_thresh)
448 458
449 459 #Non-coherent detection!
450 460 powerNet = numpy.nansum(numpy.abs(voltsPShift[:,:,:])**2,0)
451 461 #********** END OF COH/NON-COH POWER CALCULATION**********************
452 462
453 463 #********** FIND THE NOISE LEVEL AND POSSIBLE METEORS ****************
454 464 #Get noise
455 465 noise, noise1 = self.__getNoise(powerNet, noise_timeStep, self.dataOut.timeInterval)
456 466 # noise = self.getNoise1(powerNet, noise_timeStep, self.dataOut.timeInterval)
457 467 #Get signal threshold
458 468 signalThresh = noise_multiple*noise
459 469 #Meteor echoes detection
460 470 listMeteors = self.__findMeteors(powerNet, signalThresh)
461 471 #******* END OF NOISE LEVEL AND POSSIBLE METEORS CACULATION **********
462 472
463 473 #************** REMOVE MULTIPLE DETECTIONS (3.5) ***************************
464 474 #Parameters
465 475 heiRange = self.dataOut.getHeiRange()
466 476 rangeInterval = heiRange[1] - heiRange[0]
467 477 rangeLimit = multDet_rangeLimit/rangeInterval
468 478 timeLimit = multDet_timeLimit/self.dataOut.timeInterval
469 479 #Multiple detection removals
470 480 listMeteors1 = self.__removeMultipleDetections(listMeteors, rangeLimit, timeLimit)
471 481 #************ END OF REMOVE MULTIPLE DETECTIONS **********************
472 482
473 483 #********************* METEOR REESTIMATION (3.7, 3.8, 3.9, 3.10) ********************
474 484 #Parameters
475 485 phaseThresh = phaseThresh*numpy.pi/180
476 486 thresh = [phaseThresh, noise_multiple, SNRThresh]
477 487 #Meteor reestimation (Errors N 1, 6, 12, 17)
478 488 listMeteors2, listMeteorsPower, listMeteorsVolts = self.__meteorReestimation(listMeteors1, voltsPShift, pairslist, thresh, noise, self.dataOut.timeInterval, self.dataOut.frequency)
479 489 # listMeteors2, listMeteorsPower, listMeteorsVolts = self.meteorReestimation3(listMeteors2, listMeteorsPower, listMeteorsVolts, voltsPShift, pairslist, thresh, noise)
480 490 #Estimation of decay times (Errors N 7, 8, 11)
481 491 listMeteors3 = self.__estimateDecayTime(listMeteors2, listMeteorsPower, self.dataOut.timeInterval, self.dataOut.frequency)
482 492 #******************* END OF METEOR REESTIMATION *******************
483 493
484 494 #********************* METEOR PARAMETERS CALCULATION (3.11, 3.12, 3.13) **************************
485 495 #Calculating Radial Velocity (Error N 15)
486 496 radialStdThresh = 10
487 497 listMeteors4 = self.__getRadialVelocity(listMeteors3, listMeteorsVolts, radialStdThresh, pairslist, self.dataOut.timeInterval)
488 498
489 499 if len(listMeteors4) > 0:
490 500 #Setting New Array
491 501 date = repr(self.dataOut.datatime)
492 502 arrayMeteors4, arrayParameters = self.__setNewArrays(listMeteors4, date, heiRang)
493 503
494 504 #Calculate AOA (Error N 3, 4)
495 505 #JONES ET AL. 1998
496 506 AOAthresh = numpy.pi/8
497 507 error = arrayParameters[:,-1]
498 508 phases = -arrayMeteors4[:,9:13]
499 509 pairsList = []
500 510 pairsList.append((0,3))
501 511 pairsList.append((1,2))
502 512 arrayParameters[:,4:7], arrayParameters[:,-1] = self.__getAOA(phases, pairsList, error, AOAthresh, azimuth)
503 513
504 514 #Calculate Heights (Error N 13 and 14)
505 515 error = arrayParameters[:,-1]
506 516 Ranges = arrayParameters[:,2]
507 517 zenith = arrayParameters[:,5]
508 518 arrayParameters[:,3], arrayParameters[:,-1] = self.__getHeights(Ranges, zenith, error, hmin, hmax)
509 519 #********************* END OF PARAMETERS CALCULATION **************************
510 520
511 521 #***************************+ SAVE DATA IN HDF5 FORMAT **********************
512 522 self.dataOut.data_param = arrayParameters
513 523
514 524 return
515 525
516 526 def __getHardwarePhaseDiff(self, voltage0, pairslist, newheis, n):
517 527
518 528 minIndex = min(newheis[0])
519 529 maxIndex = max(newheis[0])
520 530
521 531 voltage = voltage0[:,:,minIndex:maxIndex+1]
522 532 nLength = voltage.shape[1]/n
523 533 nMin = 0
524 534 nMax = 0
525 535 phaseOffset = numpy.zeros((len(pairslist),n))
526 536
527 537 for i in range(n):
528 538 nMax += nLength
529 539 phaseCCF = -numpy.angle(self.__calculateCCF(voltage[:,nMin:nMax,:], pairslist, [0]))
530 540 phaseCCF = numpy.mean(phaseCCF, axis = 2)
531 541 phaseOffset[:,i] = phaseCCF.transpose()
532 542 nMin = nMax
533 543 # phaseDiff, phaseArrival = self.estimatePhaseDifference(voltage, pairslist)
534 544
535 545 #Remove Outliers
536 546 factor = 2
537 547 wt = phaseOffset - signal.medfilt(phaseOffset,(1,5))
538 548 dw = numpy.std(wt,axis = 1)
539 549 dw = dw.reshape((dw.size,1))
540 550 ind = numpy.where(numpy.logical_or(wt>dw*factor,wt<-dw*factor))
541 551 phaseOffset[ind] = numpy.nan
542 552 phaseOffset = stats.nanmean(phaseOffset, axis=1)
543 553
544 554 return phaseOffset
545 555
546 556 def __shiftPhase(self, data, phaseShift):
547 557 #this will shift the phase of a complex number
548 558 dataShifted = numpy.abs(data) * numpy.exp((numpy.angle(data)+phaseShift)*1j)
549 559 return dataShifted
550 560
551 561 def __estimatePhaseDifference(self, array, pairslist):
552 562 nChannel = array.shape[0]
553 563 nHeights = array.shape[2]
554 564 numPairs = len(pairslist)
555 565 # phaseCCF = numpy.zeros((nChannel, 5, nHeights))
556 566 phaseCCF = numpy.angle(self.__calculateCCF(array, pairslist, [-2,-1,0,1,2]))
557 567
558 568 #Correct phases
559 569 derPhaseCCF = phaseCCF[:,1:,:] - phaseCCF[:,0:-1,:]
560 570 indDer = numpy.where(numpy.abs(derPhaseCCF) > numpy.pi)
561 571
562 572 if indDer[0].shape[0] > 0:
563 573 for i in range(indDer[0].shape[0]):
564 574 signo = -numpy.sign(derPhaseCCF[indDer[0][i],indDer[1][i],indDer[2][i]])
565 575 phaseCCF[indDer[0][i],indDer[1][i]+1:,:] += signo*2*numpy.pi
566 576
567 577 # for j in range(numSides):
568 578 # phaseCCFAux = self.calculateCCF(arrayCenter, arraySides[j,:,:], [-2,1,0,1,2])
569 579 # phaseCCF[j,:,:] = numpy.angle(phaseCCFAux)
570 580 #
571 581 #Linear
572 582 phaseInt = numpy.zeros((numPairs,1))
573 583 angAllCCF = phaseCCF[:,[0,1,3,4],0]
574 584 for j in range(numPairs):
575 585 fit = stats.linregress([-2,-1,1,2],angAllCCF[j,:])
576 586 phaseInt[j] = fit[1]
577 587 #Phase Differences
578 588 phaseDiff = phaseInt - phaseCCF[:,2,:]
579 589 phaseArrival = phaseInt.reshape(phaseInt.size)
580 590
581 591 #Dealias
582 592 indAlias = numpy.where(phaseArrival > numpy.pi)
583 593 phaseArrival[indAlias] -= 2*numpy.pi
584 594 indAlias = numpy.where(phaseArrival < -numpy.pi)
585 595 phaseArrival[indAlias] += 2*numpy.pi
586 596
587 597 return phaseDiff, phaseArrival
588 598
589 599 def __coherentDetection(self, volts, timeSegment, timeInterval, pairslist, thresh):
590 600 #this function will run the coherent detection used in Holdworth et al. 2004 and return the net power
591 601 #find the phase shifts of each channel over 1 second intervals
592 602 #only look at ranges below the beacon signal
593 603 numProfPerBlock = numpy.ceil(timeSegment/timeInterval)
594 604 numBlocks = int(volts.shape[1]/numProfPerBlock)
595 605 numHeights = volts.shape[2]
596 606 nChannel = volts.shape[0]
597 607 voltsCohDet = volts.copy()
598 608
599 609 pairsarray = numpy.array(pairslist)
600 610 indSides = pairsarray[:,1]
601 611 # indSides = numpy.array(range(nChannel))
602 612 # indSides = numpy.delete(indSides, indCenter)
603 613 #
604 614 # listCenter = numpy.array_split(volts[indCenter,:,:], numBlocks, 0)
605 615 listBlocks = numpy.array_split(volts, numBlocks, 1)
606 616
607 617 startInd = 0
608 618 endInd = 0
609 619
610 620 for i in range(numBlocks):
611 621 startInd = endInd
612 622 endInd = endInd + listBlocks[i].shape[1]
613 623
614 624 arrayBlock = listBlocks[i]
615 625 # arrayBlockCenter = listCenter[i]
616 626
617 627 #Estimate the Phase Difference
618 628 phaseDiff, aux = self.__estimatePhaseDifference(arrayBlock, pairslist)
619 629 #Phase Difference RMS
620 630 arrayPhaseRMS = numpy.abs(phaseDiff)
621 631 phaseRMSaux = numpy.sum(arrayPhaseRMS < thresh,0)
622 632 indPhase = numpy.where(phaseRMSaux==4)
623 633 #Shifting
624 634 if indPhase[0].shape[0] > 0:
625 635 for j in range(indSides.size):
626 636 arrayBlock[indSides[j],:,indPhase] = self.__shiftPhase(arrayBlock[indSides[j],:,indPhase], phaseDiff[j,indPhase].transpose())
627 637 voltsCohDet[:,startInd:endInd,:] = arrayBlock
628 638
629 639 return voltsCohDet
630 640
631 641 def __calculateCCF(self, volts, pairslist ,laglist):
632 642
633 643 nHeights = volts.shape[2]
634 644 nPoints = volts.shape[1]
635 645 voltsCCF = numpy.zeros((len(pairslist), len(laglist), nHeights),dtype = 'complex')
636 646
637 647 for i in range(len(pairslist)):
638 648 volts1 = volts[pairslist[i][0]]
639 649 volts2 = volts[pairslist[i][1]]
640 650
641 651 for t in range(len(laglist)):
642 652 idxT = laglist[t]
643 653 if idxT >= 0:
644 654 vStacked = numpy.vstack((volts2[idxT:,:],
645 655 numpy.zeros((idxT, nHeights),dtype='complex')))
646 656 else:
647 657 vStacked = numpy.vstack((numpy.zeros((-idxT, nHeights),dtype='complex'),
648 658 volts2[:(nPoints + idxT),:]))
649 659 voltsCCF[i,t,:] = numpy.sum((numpy.conjugate(volts1)*vStacked),axis=0)
650 660
651 661 vStacked = None
652 662 return voltsCCF
653 663
654 664 def __getNoise(self, power, timeSegment, timeInterval):
655 665 numProfPerBlock = numpy.ceil(timeSegment/timeInterval)
656 666 numBlocks = int(power.shape[0]/numProfPerBlock)
657 667 numHeights = power.shape[1]
658 668
659 669 listPower = numpy.array_split(power, numBlocks, 0)
660 670 noise = numpy.zeros((power.shape[0], power.shape[1]))
661 671 noise1 = numpy.zeros((power.shape[0], power.shape[1]))
662 672
663 673 startInd = 0
664 674 endInd = 0
665 675
666 676 for i in range(numBlocks): #split por canal
667 677 startInd = endInd
668 678 endInd = endInd + listPower[i].shape[0]
669 679
670 680 arrayBlock = listPower[i]
671 681 noiseAux = numpy.mean(arrayBlock, 0)
672 682 # noiseAux = numpy.median(noiseAux)
673 683 # noiseAux = numpy.mean(arrayBlock)
674 684 noise[startInd:endInd,:] = noise[startInd:endInd,:] + noiseAux
675 685
676 686 noiseAux1 = numpy.mean(arrayBlock)
677 687 noise1[startInd:endInd,:] = noise1[startInd:endInd,:] + noiseAux1
678 688
679 689 return noise, noise1
680 690
681 691 def __findMeteors(self, power, thresh):
682 692 nProf = power.shape[0]
683 693 nHeights = power.shape[1]
684 694 listMeteors = []
685 695
686 696 for i in range(nHeights):
687 697 powerAux = power[:,i]
688 698 threshAux = thresh[:,i]
689 699
690 700 indUPthresh = numpy.where(powerAux > threshAux)[0]
691 701 indDNthresh = numpy.where(powerAux <= threshAux)[0]
692 702
693 703 j = 0
694 704
695 705 while (j < indUPthresh.size - 2):
696 706 if (indUPthresh[j + 2] == indUPthresh[j] + 2):
697 707 indDNAux = numpy.where(indDNthresh > indUPthresh[j])
698 708 indDNthresh = indDNthresh[indDNAux]
699 709
700 710 if (indDNthresh.size > 0):
701 711 indEnd = indDNthresh[0] - 1
702 712 indInit = indUPthresh[j]
703 713
704 714 meteor = powerAux[indInit:indEnd + 1]
705 715 indPeak = meteor.argmax() + indInit
706 716 FLA = sum(numpy.conj(meteor)*numpy.hstack((meteor[1:],0)))
707 717
708 718 listMeteors.append(numpy.array([i,indInit,indPeak,indEnd,FLA])) #CHEQUEAR!!!!!
709 719 j = numpy.where(indUPthresh == indEnd)[0] + 1
710 720 else: j+=1
711 721 else: j+=1
712 722
713 723 return listMeteors
714 724
715 725 def __removeMultipleDetections(self,listMeteors, rangeLimit, timeLimit):
716 726
717 727 arrayMeteors = numpy.asarray(listMeteors)
718 728 listMeteors1 = []
719 729
720 730 while arrayMeteors.shape[0] > 0:
721 731 FLAs = arrayMeteors[:,4]
722 732 maxFLA = FLAs.argmax()
723 733 listMeteors1.append(arrayMeteors[maxFLA,:])
724 734
725 735 MeteorInitTime = arrayMeteors[maxFLA,1]
726 736 MeteorEndTime = arrayMeteors[maxFLA,3]
727 737 MeteorHeight = arrayMeteors[maxFLA,0]
728 738
729 739 #Check neighborhood
730 740 maxHeightIndex = MeteorHeight + rangeLimit
731 741 minHeightIndex = MeteorHeight - rangeLimit
732 742 minTimeIndex = MeteorInitTime - timeLimit
733 743 maxTimeIndex = MeteorEndTime + timeLimit
734 744
735 745 #Check Heights
736 746 indHeight = numpy.logical_and(arrayMeteors[:,0] >= minHeightIndex, arrayMeteors[:,0] <= maxHeightIndex)
737 747 indTime = numpy.logical_and(arrayMeteors[:,3] >= minTimeIndex, arrayMeteors[:,1] <= maxTimeIndex)
738 748 indBoth = numpy.where(numpy.logical_and(indTime,indHeight))
739 749
740 750 arrayMeteors = numpy.delete(arrayMeteors, indBoth, axis = 0)
741 751
742 752 return listMeteors1
743 753
744 754 def __meteorReestimation(self, listMeteors, volts, pairslist, thresh, noise, timeInterval,frequency):
745 755 numHeights = volts.shape[2]
746 756 nChannel = volts.shape[0]
747 757
748 758 thresholdPhase = thresh[0]
749 759 thresholdNoise = thresh[1]
750 760 thresholdDB = float(thresh[2])
751 761
752 762 thresholdDB1 = 10**(thresholdDB/10)
753 763 pairsarray = numpy.array(pairslist)
754 764 indSides = pairsarray[:,1]
755 765
756 766 pairslist1 = list(pairslist)
757 767 pairslist1.append((0,1))
758 768 pairslist1.append((3,4))
759 769
760 770 listMeteors1 = []
761 771 listPowerSeries = []
762 772 listVoltageSeries = []
763 773 #volts has the war data
764 774
765 775 if frequency == 30e6:
766 776 timeLag = 45*10**-3
767 777 else:
768 778 timeLag = 15*10**-3
769 779 lag = numpy.ceil(timeLag/timeInterval)
770 780
771 781 for i in range(len(listMeteors)):
772 782
773 783 ###################### 3.6 - 3.7 PARAMETERS REESTIMATION #########################
774 784 meteorAux = numpy.zeros(16)
775 785
776 786 #Loading meteor Data (mHeight, mStart, mPeak, mEnd)
777 787 mHeight = listMeteors[i][0]
778 788 mStart = listMeteors[i][1]
779 789 mPeak = listMeteors[i][2]
780 790 mEnd = listMeteors[i][3]
781 791
782 792 #get the volt data between the start and end times of the meteor
783 793 meteorVolts = volts[:,mStart:mEnd+1,mHeight]
784 794 meteorVolts = meteorVolts.reshape(meteorVolts.shape[0], meteorVolts.shape[1], 1)
785 795
786 796 #3.6. Phase Difference estimation
787 797 phaseDiff, aux = self.__estimatePhaseDifference(meteorVolts, pairslist)
788 798
789 799 #3.7. Phase difference removal & meteor start, peak and end times reestimated
790 800 #meteorVolts0.- all Channels, all Profiles
791 801 meteorVolts0 = volts[:,:,mHeight]
792 802 meteorThresh = noise[:,mHeight]*thresholdNoise
793 803 meteorNoise = noise[:,mHeight]
794 804 meteorVolts0[indSides,:] = self.__shiftPhase(meteorVolts0[indSides,:], phaseDiff) #Phase Shifting
795 805 powerNet0 = numpy.nansum(numpy.abs(meteorVolts0)**2, axis = 0) #Power
796 806
797 807 #Times reestimation
798 808 mStart1 = numpy.where(powerNet0[:mPeak] < meteorThresh[:mPeak])[0]
799 809 if mStart1.size > 0:
800 810 mStart1 = mStart1[-1] + 1
801 811
802 812 else:
803 813 mStart1 = mPeak
804 814
805 815 mEnd1 = numpy.where(powerNet0[mPeak:] < meteorThresh[mPeak:])[0][0] + mPeak - 1
806 816 mEndDecayTime1 = numpy.where(powerNet0[mPeak:] < meteorNoise[mPeak:])[0]
807 817 if mEndDecayTime1.size == 0:
808 818 mEndDecayTime1 = powerNet0.size
809 819 else:
810 820 mEndDecayTime1 = mEndDecayTime1[0] + mPeak - 1
811 821 # mPeak1 = meteorVolts0[mStart1:mEnd1 + 1].argmax()
812 822
813 823 #meteorVolts1.- all Channels, from start to end
814 824 meteorVolts1 = meteorVolts0[:,mStart1:mEnd1 + 1]
815 825 meteorVolts2 = meteorVolts0[:,mPeak + lag:mEnd1 + 1]
816 826 if meteorVolts2.shape[1] == 0:
817 827 meteorVolts2 = meteorVolts0[:,mPeak:mEnd1 + 1]
818 828 meteorVolts1 = meteorVolts1.reshape(meteorVolts1.shape[0], meteorVolts1.shape[1], 1)
819 829 meteorVolts2 = meteorVolts2.reshape(meteorVolts2.shape[0], meteorVolts2.shape[1], 1)
820 830 ##################### END PARAMETERS REESTIMATION #########################
821 831
822 832 ##################### 3.8 PHASE DIFFERENCE REESTIMATION ########################
823 833 # if mEnd1 - mStart1 > 4: #Error Number 6: echo less than 5 samples long; too short for analysis
824 834 if meteorVolts2.shape[1] > 0:
825 835 #Phase Difference re-estimation
826 836 phaseDiff1, phaseDiffint = self.__estimatePhaseDifference(meteorVolts2, pairslist1) #Phase Difference Estimation
827 837 # phaseDiff1, phaseDiffint = self.estimatePhaseDifference(meteorVolts2, pairslist)
828 838 meteorVolts2 = meteorVolts2.reshape(meteorVolts2.shape[0], meteorVolts2.shape[1])
829 839 phaseDiff11 = numpy.reshape(phaseDiff1, (phaseDiff1.shape[0],1))
830 840 meteorVolts2[indSides,:] = self.__shiftPhase(meteorVolts2[indSides,:], phaseDiff11[0:4]) #Phase Shifting
831 841
832 842 #Phase Difference RMS
833 843 phaseRMS1 = numpy.sqrt(numpy.mean(numpy.square(phaseDiff1)))
834 844 powerNet1 = numpy.nansum(numpy.abs(meteorVolts1[:,:])**2,0)
835 845 #Data from Meteor
836 846 mPeak1 = powerNet1.argmax() + mStart1
837 847 mPeakPower1 = powerNet1.max()
838 848 noiseAux = sum(noise[mStart1:mEnd1 + 1,mHeight])
839 849 mSNR1 = (sum(powerNet1)-noiseAux)/noiseAux
840 850 Meteor1 = numpy.array([mHeight, mStart1, mPeak1, mEnd1, mPeakPower1, mSNR1, phaseRMS1])
841 851 Meteor1 = numpy.hstack((Meteor1,phaseDiffint))
842 852 PowerSeries = powerNet0[mStart1:mEndDecayTime1 + 1]
843 853 #Vectorize
844 854 meteorAux[0:7] = [mHeight, mStart1, mPeak1, mEnd1, mPeakPower1, mSNR1, phaseRMS1]
845 855 meteorAux[7:11] = phaseDiffint[0:4]
846 856
847 857 #Rejection Criterions
848 858 if phaseRMS1 > thresholdPhase: #Error Number 17: Phase variation
849 859 meteorAux[-1] = 17
850 860 elif mSNR1 < thresholdDB1: #Error Number 1: SNR < threshold dB
851 861 meteorAux[-1] = 1
852 862
853 863
854 864 else:
855 865 meteorAux[0:4] = [mHeight, mStart, mPeak, mEnd]
856 866 meteorAux[-1] = 6 #Error Number 6: echo less than 5 samples long; too short for analysis
857 867 PowerSeries = 0
858 868
859 869 listMeteors1.append(meteorAux)
860 870 listPowerSeries.append(PowerSeries)
861 871 listVoltageSeries.append(meteorVolts1)
862 872
863 873 return listMeteors1, listPowerSeries, listVoltageSeries
864 874
865 875 def __estimateDecayTime(self, listMeteors, listPower, timeInterval, frequency):
866 876
867 877 threshError = 10
868 878 #Depending if it is 30 or 50 MHz
869 879 if frequency == 30e6:
870 880 timeLag = 45*10**-3
871 881 else:
872 882 timeLag = 15*10**-3
873 883 lag = numpy.ceil(timeLag/timeInterval)
874 884
875 885 listMeteors1 = []
876 886
877 887 for i in range(len(listMeteors)):
878 888 meteorPower = listPower[i]
879 889 meteorAux = listMeteors[i]
880 890
881 891 if meteorAux[-1] == 0:
882 892
883 893 try:
884 894 indmax = meteorPower.argmax()
885 895 indlag = indmax + lag
886 896
887 897 y = meteorPower[indlag:]
888 898 x = numpy.arange(0, y.size)*timeLag
889 899
890 900 #first guess
891 901 a = y[0]
892 902 tau = timeLag
893 903 #exponential fit
894 904 popt, pcov = optimize.curve_fit(self.__exponential_function, x, y, p0 = [a, tau])
895 905 y1 = self.__exponential_function(x, *popt)
896 906 #error estimation
897 907 error = sum((y - y1)**2)/(numpy.var(y)*(y.size - popt.size))
898 908
899 909 decayTime = popt[1]
900 910 riseTime = indmax*timeInterval
901 911 meteorAux[11:13] = [decayTime, error]
902 912
903 913 #Table items 7, 8 and 11
904 914 if (riseTime > 0.3): #Number 7: Echo rise exceeds 0.3s
905 915 meteorAux[-1] = 7
906 916 elif (decayTime < 2*riseTime) : #Number 8: Echo decay time less than than twice rise time
907 917 meteorAux[-1] = 8
908 918 if (error > threshError): #Number 11: Poor fit to amplitude for estimation of decay time
909 919 meteorAux[-1] = 11
910 920
911 921
912 922 except:
913 923 meteorAux[-1] = 11
914 924
915 925
916 926 listMeteors1.append(meteorAux)
917 927
918 928 return listMeteors1
919 929
920 930 #Exponential Function
921 931
922 932 def __exponential_function(self, x, a, tau):
923 933 y = a*numpy.exp(-x/tau)
924 934 return y
925 935
926 936 def __getRadialVelocity(self, listMeteors, listVolts, radialStdThresh, pairslist, timeInterval):
927 937
928 938 pairslist1 = list(pairslist)
929 939 pairslist1.append((0,1))
930 940 pairslist1.append((3,4))
931 941 numPairs = len(pairslist1)
932 942 #Time Lag
933 943 timeLag = 45*10**-3
934 944 c = 3e8
935 945 lag = numpy.ceil(timeLag/timeInterval)
936 946 freq = 30e6
937 947
938 948 listMeteors1 = []
939 949
940 950 for i in range(len(listMeteors)):
941 951 meteor = listMeteors[i]
942 952 meteorAux = numpy.hstack((meteor[:-1], 0, 0, meteor[-1]))
943 953 if meteor[-1] == 0:
944 954 mStart = listMeteors[i][1]
945 955 mPeak = listMeteors[i][2]
946 956 mLag = mPeak - mStart + lag
947 957
948 958 #get the volt data between the start and end times of the meteor
949 959 meteorVolts = listVolts[i]
950 960 meteorVolts = meteorVolts.reshape(meteorVolts.shape[0], meteorVolts.shape[1], 1)
951 961
952 962 #Get CCF
953 963 allCCFs = self.__calculateCCF(meteorVolts, pairslist1, [-2,-1,0,1,2])
954 964
955 965 #Method 2
956 966 slopes = numpy.zeros(numPairs)
957 967 time = numpy.array([-2,-1,1,2])*timeInterval
958 968 angAllCCF = numpy.angle(allCCFs[:,[0,1,3,4],0])
959 969
960 970 #Correct phases
961 971 derPhaseCCF = angAllCCF[:,1:] - angAllCCF[:,0:-1]
962 972 indDer = numpy.where(numpy.abs(derPhaseCCF) > numpy.pi)
963 973
964 974 if indDer[0].shape[0] > 0:
965 975 for i in range(indDer[0].shape[0]):
966 976 signo = -numpy.sign(derPhaseCCF[indDer[0][i],indDer[1][i]])
967 977 angAllCCF[indDer[0][i],indDer[1][i]+1:] += signo*2*numpy.pi
968 978
969 979 # fit = scipy.stats.linregress(numpy.array([-2,-1,1,2])*timeInterval, numpy.array([phaseLagN2s[i],phaseLagN1s[i],phaseLag1s[i],phaseLag2s[i]]))
970 980 for j in range(numPairs):
971 981 fit = stats.linregress(time, angAllCCF[j,:])
972 982 slopes[j] = fit[0]
973 983
974 984 #Remove Outlier
975 985 # indOut = numpy.argmax(numpy.abs(slopes - numpy.mean(slopes)))
976 986 # slopes = numpy.delete(slopes,indOut)
977 987 # indOut = numpy.argmax(numpy.abs(slopes - numpy.mean(slopes)))
978 988 # slopes = numpy.delete(slopes,indOut)
979 989
980 990 radialVelocity = -numpy.mean(slopes)*(0.25/numpy.pi)*(c/freq)
981 991 radialError = numpy.std(slopes)*(0.25/numpy.pi)*(c/freq)
982 992 meteorAux[-2] = radialError
983 993 meteorAux[-3] = radialVelocity
984 994
985 995 #Setting Error
986 996 #Number 15: Radial Drift velocity or projected horizontal velocity exceeds 200 m/s
987 997 if numpy.abs(radialVelocity) > 200:
988 998 meteorAux[-1] = 15
989 999 #Number 12: Poor fit to CCF variation for estimation of radial drift velocity
990 1000 elif radialError > radialStdThresh:
991 1001 meteorAux[-1] = 12
992 1002
993 1003 listMeteors1.append(meteorAux)
994 1004 return listMeteors1
995 1005
996 1006 def __setNewArrays(self, listMeteors, date, heiRang):
997 1007
998 1008 #New arrays
999 1009 arrayMeteors = numpy.array(listMeteors)
1000 1010 arrayParameters = numpy.zeros((len(listMeteors),10))
1001 1011
1002 1012 #Date inclusion
1003 1013 date = re.findall(r'\((.*?)\)', date)
1004 1014 date = date[0].split(',')
1005 1015 date = map(int, date)
1006 1016 date = [date[0]*10000 + date[1]*100 + date[2], date[3]*10000 + date[4]*100 + date[5]]
1007 1017 arrayDate = numpy.tile(date, (len(listMeteors), 1))
1008 1018
1009 1019 #Meteor array
1010 1020 arrayMeteors[:,0] = heiRang[arrayMeteors[:,0].astype(int)]
1011 1021 arrayMeteors = numpy.hstack((arrayDate, arrayMeteors))
1012 1022
1013 1023 #Parameters Array
1014 1024 arrayParameters[:,0:3] = arrayMeteors[:,0:3]
1015 1025 arrayParameters[:,-3:] = arrayMeteors[:,-3:]
1016 1026
1017 1027 return arrayMeteors, arrayParameters
1018 1028
1019 1029 def __getAOA(self, phases, pairsList, error, AOAthresh, azimuth):
1020 1030
1021 1031 arrayAOA = numpy.zeros((phases.shape[0],3))
1022 1032 cosdir0, cosdir = self.__getDirectionCosines(phases, pairsList)
1023 1033
1024 1034 arrayAOA[:,:2] = self.__calculateAOA(cosdir, azimuth)
1025 1035 cosDirError = numpy.sum(numpy.abs(cosdir0 - cosdir), axis = 1)
1026 1036 arrayAOA[:,2] = cosDirError
1027 1037
1028 1038 azimuthAngle = arrayAOA[:,0]
1029 1039 zenithAngle = arrayAOA[:,1]
1030 1040
1031 1041 #Setting Error
1032 1042 #Number 3: AOA not fesible
1033 1043 indInvalid = numpy.where(numpy.logical_and((numpy.logical_or(numpy.isnan(zenithAngle), numpy.isnan(azimuthAngle))),error == 0))[0]
1034 1044 error[indInvalid] = 3
1035 1045 #Number 4: Large difference in AOAs obtained from different antenna baselines
1036 1046 indInvalid = numpy.where(numpy.logical_and(cosDirError > AOAthresh,error == 0))[0]
1037 1047 error[indInvalid] = 4
1038 1048 return arrayAOA, error
1039 1049
1040 1050 def __getDirectionCosines(self, arrayPhase, pairsList):
1041 1051
1042 1052 #Initializing some variables
1043 1053 ang_aux = numpy.array([-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8])*2*numpy.pi
1044 1054 ang_aux = ang_aux.reshape(1,ang_aux.size)
1045 1055
1046 1056 cosdir = numpy.zeros((arrayPhase.shape[0],2))
1047 1057 cosdir0 = numpy.zeros((arrayPhase.shape[0],2))
1048 1058
1049 1059
1050 1060 for i in range(2):
1051 1061 #First Estimation
1052 1062 phi0_aux = arrayPhase[:,pairsList[i][0]] + arrayPhase[:,pairsList[i][1]]
1053 1063 #Dealias
1054 1064 indcsi = numpy.where(phi0_aux > numpy.pi)
1055 1065 phi0_aux[indcsi] -= 2*numpy.pi
1056 1066 indcsi = numpy.where(phi0_aux < -numpy.pi)
1057 1067 phi0_aux[indcsi] += 2*numpy.pi
1058 1068 #Direction Cosine 0
1059 1069 cosdir0[:,i] = -(phi0_aux)/(2*numpy.pi*0.5)
1060 1070
1061 1071 #Most-Accurate Second Estimation
1062 1072 phi1_aux = arrayPhase[:,pairsList[i][0]] - arrayPhase[:,pairsList[i][1]]
1063 1073 phi1_aux = phi1_aux.reshape(phi1_aux.size,1)
1064 1074 #Direction Cosine 1
1065 1075 cosdir1 = -(phi1_aux + ang_aux)/(2*numpy.pi*4.5)
1066 1076
1067 1077 #Searching the correct Direction Cosine
1068 1078 cosdir0_aux = cosdir0[:,i]
1069 1079 cosdir0_aux = cosdir0_aux.reshape(cosdir0_aux.size,1)
1070 1080 #Minimum Distance
1071 1081 cosDiff = (cosdir1 - cosdir0_aux)**2
1072 1082 indcos = cosDiff.argmin(axis = 1)
1073 1083 #Saving Value obtained
1074 1084 cosdir[:,i] = cosdir1[numpy.arange(len(indcos)),indcos]
1075 1085
1076 1086 return cosdir0, cosdir
1077 1087
1078 1088 def __calculateAOA(self, cosdir, azimuth):
1079 1089 cosdirX = cosdir[:,0]
1080 1090 cosdirY = cosdir[:,1]
1081 1091
1082 1092 zenithAngle = numpy.arccos(numpy.sqrt(1 - cosdirX**2 - cosdirY**2))*180/numpy.pi
1083 1093 azimuthAngle = numpy.arctan2(cosdirX,cosdirY)*180/numpy.pi + azimuth #0 deg north, 90 deg east
1084 1094 angles = numpy.vstack((azimuthAngle, zenithAngle)).transpose()
1085 1095
1086 1096 return angles
1087 1097
1088 1098 def __getHeights(self, Ranges, zenith, error, minHeight, maxHeight):
1089 1099
1090 1100 Ramb = 375 #Ramb = c/(2*PRF)
1091 1101 Re = 6371 #Earth Radius
1092 1102 heights = numpy.zeros(Ranges.shape)
1093 1103
1094 1104 R_aux = numpy.array([0,1,2])*Ramb
1095 1105 R_aux = R_aux.reshape(1,R_aux.size)
1096 1106
1097 1107 Ranges = Ranges.reshape(Ranges.size,1)
1098 1108
1099 1109 Ri = Ranges + R_aux
1100 1110 hi = numpy.sqrt(Re**2 + Ri**2 + (2*Re*numpy.cos(zenith*numpy.pi/180)*Ri.transpose()).transpose()) - Re
1101 1111
1102 1112 #Check if there is a height between 70 and 110 km
1103 1113 h_bool = numpy.sum(numpy.logical_and(hi > minHeight, hi < maxHeight), axis = 1)
1104 1114 ind_h = numpy.where(h_bool == 1)[0]
1105 1115
1106 1116 hCorr = hi[ind_h, :]
1107 1117 ind_hCorr = numpy.where(numpy.logical_and(hi > minHeight, hi < maxHeight))
1108 1118
1109 1119 hCorr = hi[ind_hCorr]
1110 1120 heights[ind_h] = hCorr
1111 1121
1112 1122 #Setting Error
1113 1123 #Number 13: Height unresolvable echo: not valid height within 70 to 110 km
1114 1124 #Number 14: Height ambiguous echo: more than one possible height within 70 to 110 km
1115 1125
1116 1126 indInvalid2 = numpy.where(numpy.logical_and(h_bool > 1, error == 0))[0]
1117 1127 error[indInvalid2] = 14
1118 1128 indInvalid1 = numpy.where(numpy.logical_and(h_bool == 0, error == 0))[0]
1119 1129 error[indInvalid1] = 13
1120 1130
1121 1131 return heights, error
1122 1132
1123 1133 def SpectralFitting(self, getSNR = True, path=None, file=None, groupList=None):
1124 1134
1125 1135 '''
1126 1136 Function GetMoments()
1127 1137
1128 1138 Input:
1129 1139 Output:
1130 1140 Variables modified:
1131 1141 '''
1132 1142 if path != None:
1133 1143 sys.path.append(path)
1134 1144 self.dataOut.library = importlib.import_module(file)
1135 1145
1136 1146 #To be inserted as a parameter
1137 1147 groupArray = numpy.array(groupList)
1138 1148 # groupArray = numpy.array([[0,1],[2,3]])
1139 1149 self.dataOut.groupList = groupArray
1140 1150
1141 1151 nGroups = groupArray.shape[0]
1142 1152 nChannels = self.dataIn.nChannels
1143 1153 nHeights=self.dataIn.heightList.size
1144 1154
1145 1155 #Parameters Array
1146 1156 self.dataOut.data_param = None
1147 1157
1148 1158 #Set constants
1149 1159 constants = self.dataOut.library.setConstants(self.dataIn)
1150 1160 self.dataOut.constants = constants
1151 1161 M = self.dataIn.normFactor
1152 1162 N = self.dataIn.nFFTPoints
1153 1163 ippSeconds = self.dataIn.ippSeconds
1154 1164 K = self.dataIn.nIncohInt
1155 1165 pairsArray = numpy.array(self.dataIn.pairsList)
1156 1166
1157 1167 #List of possible combinations
1158 1168 listComb = itertools.combinations(numpy.arange(groupArray.shape[1]),2)
1159 1169 indCross = numpy.zeros(len(list(listComb)), dtype = 'int')
1160 1170
1161 1171 if getSNR:
1162 1172 listChannels = groupArray.reshape((groupArray.size))
1163 1173 listChannels.sort()
1164 1174 noise = self.dataIn.getNoise()
1165 1175 self.dataOut.data_SNR = self.__getSNR(self.dataIn.data_spc[listChannels,:,:], noise[listChannels])
1166 1176
1167 1177 for i in range(nGroups):
1168 1178 coord = groupArray[i,:]
1169 1179
1170 1180 #Input data array
1171 1181 data = self.dataIn.data_spc[coord,:,:]/(M*N)
1172 1182 data = data.reshape((data.shape[0]*data.shape[1],data.shape[2]))
1173 1183
1174 1184 #Cross Spectra data array for Covariance Matrixes
1175 1185 ind = 0
1176 1186 for pairs in listComb:
1177 1187 pairsSel = numpy.array([coord[x],coord[y]])
1178 1188 indCross[ind] = int(numpy.where(numpy.all(pairsArray == pairsSel, axis = 1))[0][0])
1179 1189 ind += 1
1180 1190 dataCross = self.dataIn.data_cspc[indCross,:,:]/(M*N)
1181 1191 dataCross = dataCross**2/K
1182 1192
1183 1193 for h in range(nHeights):
1184 1194 # print self.dataOut.heightList[h]
1185 1195
1186 1196 #Input
1187 1197 d = data[:,h]
1188 1198
1189 1199 #Covariance Matrix
1190 1200 D = numpy.diag(d**2/K)
1191 1201 ind = 0
1192 1202 for pairs in listComb:
1193 1203 #Coordinates in Covariance Matrix
1194 1204 x = pairs[0]
1195 1205 y = pairs[1]
1196 1206 #Channel Index
1197 1207 S12 = dataCross[ind,:,h]
1198 1208 D12 = numpy.diag(S12)
1199 1209 #Completing Covariance Matrix with Cross Spectras
1200 1210 D[x*N:(x+1)*N,y*N:(y+1)*N] = D12
1201 1211 D[y*N:(y+1)*N,x*N:(x+1)*N] = D12
1202 1212 ind += 1
1203 1213 Dinv=numpy.linalg.inv(D)
1204 1214 L=numpy.linalg.cholesky(Dinv)
1205 1215 LT=L.T
1206 1216
1207 1217 dp = numpy.dot(LT,d)
1208 1218
1209 1219 #Initial values
1210 1220 data_spc = self.dataIn.data_spc[coord,:,h]
1211 1221
1212 1222 if (h>0)and(error1[3]<5):
1213 1223 p0 = self.dataOut.data_param[i,:,h-1]
1214 1224 else:
1215 1225 p0 = numpy.array(self.dataOut.library.initialValuesFunction(data_spc, constants, i))
1216 1226
1217 1227 try:
1218 1228 #Least Squares
1219 1229 minp,covp,infodict,mesg,ier = optimize.leastsq(self.__residFunction,p0,args=(dp,LT,constants),full_output=True)
1220 1230 # minp,covp = optimize.leastsq(self.__residFunction,p0,args=(dp,LT,constants))
1221 1231 #Chi square error
1222 1232 error0 = numpy.sum(infodict['fvec']**2)/(2*N)
1223 1233 #Error with Jacobian
1224 1234 error1 = self.dataOut.library.errorFunction(minp,constants,LT)
1225 1235 except:
1226 1236 minp = p0*numpy.nan
1227 1237 error0 = numpy.nan
1228 1238 error1 = p0*numpy.nan
1229 1239
1230 1240 #Save
1231 1241 if self.dataOut.data_param == None:
1232 1242 self.dataOut.data_param = numpy.zeros((nGroups, p0.size, nHeights))*numpy.nan
1233 1243 self.dataOut.data_error = numpy.zeros((nGroups, p0.size + 1, nHeights))*numpy.nan
1234 1244
1235 1245 self.dataOut.data_error[i,:,h] = numpy.hstack((error0,error1))
1236 1246 self.dataOut.data_param[i,:,h] = minp
1237 1247 return
1238 1248
1239 1249
1240 1250 def __residFunction(self, p, dp, LT, constants):
1241 1251
1242 1252 fm = self.dataOut.library.modelFunction(p, constants)
1243 1253 fmp=numpy.dot(LT,fm)
1244 1254
1245 1255 return dp-fmp
1246 1256
1247 1257 def __getSNR(self, z, noise):
1248 1258
1249 1259 avg = numpy.average(z, axis=1)
1250 1260 SNR = (avg.T-noise)/noise
1251 1261 SNR = SNR.T
1252 1262 return SNR
1253 1263
1254 1264 def __chisq(p,chindex,hindex):
1255 1265 #similar to Resid but calculates CHI**2
1256 1266 [LT,d,fm]=setupLTdfm(p,chindex,hindex)
1257 1267 dp=numpy.dot(LT,d)
1258 1268 fmp=numpy.dot(LT,fm)
1259 1269 chisq=numpy.dot((dp-fmp).T,(dp-fmp))
1260 1270 return chisq
1261 1271
1262 1272
1263 1273
1264 1274 class WindProfiler(Operation):
1265 1275
1266 1276 __isConfig = False
1267 1277
1268 1278 __initime = None
1269 1279 __lastdatatime = None
1270 1280 __integrationtime = None
1271 1281
1272 1282 __buffer = None
1273 1283
1274 1284 __dataReady = False
1275 1285
1276 1286 __firstdata = None
1277 1287
1278 1288 n = None
1279 1289
1280 1290 def __init__(self):
1281 1291 Operation.__init__(self)
1282 1292
1283 1293 def __calculateCosDir(self, elev, azim):
1284 1294 zen = (90 - elev)*numpy.pi/180
1285 1295 azim = azim*numpy.pi/180
1286 1296 cosDirX = numpy.sqrt((1-numpy.cos(zen)**2)/((1+numpy.tan(azim)**2)))
1287 1297 cosDirY = numpy.sqrt(1-numpy.cos(zen)**2-cosDirX**2)
1288 1298
1289 1299 signX = numpy.sign(numpy.cos(azim))
1290 1300 signY = numpy.sign(numpy.sin(azim))
1291 1301
1292 1302 cosDirX = numpy.copysign(cosDirX, signX)
1293 1303 cosDirY = numpy.copysign(cosDirY, signY)
1294 1304 return cosDirX, cosDirY
1295 1305
1296 1306 def __calculateAngles(self, theta_x, theta_y, azimuth):
1297 1307
1298 1308 dir_cosw = numpy.sqrt(1-theta_x**2-theta_y**2)
1299 1309 zenith_arr = numpy.arccos(dir_cosw)
1300 1310 azimuth_arr = numpy.arctan2(theta_x,theta_y) + azimuth*math.pi/180
1301 1311
1302 1312 dir_cosu = numpy.sin(azimuth_arr)*numpy.sin(zenith_arr)
1303 1313 dir_cosv = numpy.cos(azimuth_arr)*numpy.sin(zenith_arr)
1304 1314
1305 1315 return azimuth_arr, zenith_arr, dir_cosu, dir_cosv, dir_cosw
1306 1316
1307 1317 def __calculateMatA(self, dir_cosu, dir_cosv, dir_cosw, horOnly):
1308 1318
1309 1319 #
1310 1320 if horOnly:
1311 1321 A = numpy.c_[dir_cosu,dir_cosv]
1312 1322 else:
1313 1323 A = numpy.c_[dir_cosu,dir_cosv,dir_cosw]
1314 1324 A = numpy.asmatrix(A)
1315 1325 A1 = numpy.linalg.inv(A.transpose()*A)*A.transpose()
1316 1326
1317 1327 return A1
1318 1328
1319 1329 def __correctValues(self, heiRang, phi, velRadial, SNR):
1320 1330 listPhi = phi.tolist()
1321 1331 maxid = listPhi.index(max(listPhi))
1322 1332 minid = listPhi.index(min(listPhi))
1323 1333
1324 1334 rango = range(len(phi))
1325 1335 # rango = numpy.delete(rango,maxid)
1326 1336
1327 1337 heiRang1 = heiRang*math.cos(phi[maxid])
1328 1338 heiRangAux = heiRang*math.cos(phi[minid])
1329 1339 indOut = (heiRang1 < heiRangAux[0]).nonzero()
1330 1340 heiRang1 = numpy.delete(heiRang1,indOut)
1331 1341
1332 1342 velRadial1 = numpy.zeros([len(phi),len(heiRang1)])
1333 1343 SNR1 = numpy.zeros([len(phi),len(heiRang1)])
1334 1344
1335 1345 for i in rango:
1336 1346 x = heiRang*math.cos(phi[i])
1337 1347 y1 = velRadial[i,:]
1338 1348 f1 = interpolate.interp1d(x,y1,kind = 'cubic')
1339 1349
1340 1350 x1 = heiRang1
1341 1351 y11 = f1(x1)
1342 1352
1343 1353 y2 = SNR[i,:]
1344 1354 f2 = interpolate.interp1d(x,y2,kind = 'cubic')
1345 1355 y21 = f2(x1)
1346 1356
1347 1357 velRadial1[i,:] = y11
1348 1358 SNR1[i,:] = y21
1349 1359
1350 1360 return heiRang1, velRadial1, SNR1
1351 1361
1352 1362 def __calculateVelUVW(self, A, velRadial):
1353 1363
1354 1364 #Operacion Matricial
1355 1365 # velUVW = numpy.zeros((velRadial.shape[1],3))
1356 1366 # for ind in range(velRadial.shape[1]):
1357 1367 # velUVW[ind,:] = numpy.dot(A,velRadial[:,ind])
1358 1368 # velUVW = velUVW.transpose()
1359 1369 velUVW = numpy.zeros((A.shape[0],velRadial.shape[1]))
1360 1370 velUVW[:,:] = numpy.dot(A,velRadial)
1361 1371
1362 1372
1363 1373 return velUVW
1364 1374
1365 1375 def techniqueDBS(self, velRadial0, dirCosx, disrCosy, azimuth, correct, horizontalOnly, heiRang, SNR0):
1366 1376 """
1367 1377 Function that implements Doppler Beam Swinging (DBS) technique.
1368 1378
1369 1379 Input: Radial velocities, Direction cosines (x and y) of the Beam, Antenna azimuth,
1370 1380 Direction correction (if necessary), Ranges and SNR
1371 1381
1372 1382 Output: Winds estimation (Zonal, Meridional and Vertical)
1373 1383
1374 1384 Parameters affected: Winds, height range, SNR
1375 1385 """
1376 1386 azimuth_arr, zenith_arr, dir_cosu, dir_cosv, dir_cosw = self.__calculateAngles(dirCosx, disrCosy, azimuth)
1377 1387 heiRang1, velRadial1, SNR1 = self.__correctValues(heiRang, zenith_arr, correct*velRadial0, SNR0)
1378 1388 A = self.__calculateMatA(dir_cosu, dir_cosv, dir_cosw, horizontalOnly)
1379 1389
1380 1390 #Calculo de Componentes de la velocidad con DBS
1381 1391 winds = self.__calculateVelUVW(A,velRadial1)
1382 1392
1383 1393 return winds, heiRang1, SNR1
1384 1394
1385 1395 def __calculateDistance(self, posx, posy, pairsCrossCorr, pairsList, pairs, azimuth = None):
1386 1396
1387 1397 posx = numpy.asarray(posx)
1388 1398 posy = numpy.asarray(posy)
1389 1399
1390 1400 #Rotacion Inversa para alinear con el azimuth
1391 1401 if azimuth!= None:
1392 1402 azimuth = azimuth*math.pi/180
1393 1403 posx1 = posx*math.cos(azimuth) + posy*math.sin(azimuth)
1394 1404 posy1 = -posx*math.sin(azimuth) + posy*math.cos(azimuth)
1395 1405 else:
1396 1406 posx1 = posx
1397 1407 posy1 = posy
1398 1408
1399 1409 #Calculo de Distancias
1400 1410 distx = numpy.zeros(pairsCrossCorr.size)
1401 1411 disty = numpy.zeros(pairsCrossCorr.size)
1402 1412 dist = numpy.zeros(pairsCrossCorr.size)
1403 1413 ang = numpy.zeros(pairsCrossCorr.size)
1404 1414
1405 1415 for i in range(pairsCrossCorr.size):
1406 1416 distx[i] = posx1[pairsList[pairsCrossCorr[i]][1]] - posx1[pairsList[pairsCrossCorr[i]][0]]
1407 1417 disty[i] = posy1[pairsList[pairsCrossCorr[i]][1]] - posy1[pairsList[pairsCrossCorr[i]][0]]
1408 1418 dist[i] = numpy.sqrt(distx[i]**2 + disty[i]**2)
1409 1419 ang[i] = numpy.arctan2(disty[i],distx[i])
1410 1420 #Calculo de Matrices
1411 1421 nPairs = len(pairs)
1412 1422 ang1 = numpy.zeros((nPairs, 2, 1))
1413 1423 dist1 = numpy.zeros((nPairs, 2, 1))
1414 1424
1415 1425 for j in range(nPairs):
1416 1426 dist1[j,0,0] = dist[pairs[j][0]]
1417 1427 dist1[j,1,0] = dist[pairs[j][1]]
1418 1428 ang1[j,0,0] = ang[pairs[j][0]]
1419 1429 ang1[j,1,0] = ang[pairs[j][1]]
1420 1430
1421 1431 return distx,disty, dist1,ang1
1422 1432
1423 1433 def __calculateVelVer(self, phase, lagTRange, _lambda):
1424 1434
1425 1435 Ts = lagTRange[1] - lagTRange[0]
1426 1436 velW = -_lambda*phase/(4*math.pi*Ts)
1427 1437
1428 1438 return velW
1429 1439
1430 1440 def __calculateVelHorDir(self, dist, tau1, tau2, ang):
1431 1441 nPairs = tau1.shape[0]
1432 1442 vel = numpy.zeros((nPairs,3,tau1.shape[2]))
1433 1443
1434 1444 angCos = numpy.cos(ang)
1435 1445 angSin = numpy.sin(ang)
1436 1446
1437 1447 vel0 = dist*tau1/(2*tau2**2)
1438 1448 vel[:,0,:] = (vel0*angCos).sum(axis = 1)
1439 1449 vel[:,1,:] = (vel0*angSin).sum(axis = 1)
1440 1450
1441 1451 ind = numpy.where(numpy.isinf(vel))
1442 1452 vel[ind] = numpy.nan
1443 1453
1444 1454 return vel
1445 1455
1446 1456 def __getPairsAutoCorr(self, pairsList, nChannels):
1447 1457
1448 1458 pairsAutoCorr = numpy.zeros(nChannels, dtype = 'int')*numpy.nan
1449 1459
1450 1460 for l in range(len(pairsList)):
1451 1461 firstChannel = pairsList[l][0]
1452 1462 secondChannel = pairsList[l][1]
1453 1463
1454 1464 #Obteniendo pares de Autocorrelacion
1455 1465 if firstChannel == secondChannel:
1456 1466 pairsAutoCorr[firstChannel] = int(l)
1457 1467
1458 1468 pairsAutoCorr = pairsAutoCorr.astype(int)
1459 1469
1460 1470 pairsCrossCorr = range(len(pairsList))
1461 1471 pairsCrossCorr = numpy.delete(pairsCrossCorr,pairsAutoCorr)
1462 1472
1463 1473 return pairsAutoCorr, pairsCrossCorr
1464 1474
1465 1475 def techniqueSA(self, pairsSelected, pairsList, nChannels, tau, azimuth, _lambda, position_x, position_y, lagTRange, correctFactor):
1466 1476 """
1467 1477 Function that implements Spaced Antenna (SA) technique.
1468 1478
1469 1479 Input: Radial velocities, Direction cosines (x and y) of the Beam, Antenna azimuth,
1470 1480 Direction correction (if necessary), Ranges and SNR
1471 1481
1472 1482 Output: Winds estimation (Zonal, Meridional and Vertical)
1473 1483
1474 1484 Parameters affected: Winds
1475 1485 """
1476 1486 #Cross Correlation pairs obtained
1477 1487 pairsAutoCorr, pairsCrossCorr = self.__getPairsAutoCorr(pairsList, nChannels)
1478 1488 pairsArray = numpy.array(pairsList)[pairsCrossCorr]
1479 1489 pairsSelArray = numpy.array(pairsSelected)
1480 1490 pairs = []
1481 1491
1482 1492 #Wind estimation pairs obtained
1483 1493 for i in range(pairsSelArray.shape[0]/2):
1484 1494 ind1 = numpy.where(numpy.all(pairsArray == pairsSelArray[2*i], axis = 1))[0][0]
1485 1495 ind2 = numpy.where(numpy.all(pairsArray == pairsSelArray[2*i + 1], axis = 1))[0][0]
1486 1496 pairs.append((ind1,ind2))
1487 1497
1488 1498 indtau = tau.shape[0]/2
1489 1499 tau1 = tau[:indtau,:]
1490 1500 tau2 = tau[indtau:-1,:]
1491 1501 tau1 = tau1[pairs,:]
1492 1502 tau2 = tau2[pairs,:]
1493 1503 phase1 = tau[-1,:]
1494 1504
1495 1505 #---------------------------------------------------------------------
1496 1506 #Metodo Directo
1497 1507 distx, disty, dist, ang = self.__calculateDistance(position_x, position_y, pairsCrossCorr, pairsList, pairs,azimuth)
1498 1508 winds = self.__calculateVelHorDir(dist, tau1, tau2, ang)
1499 1509 winds = stats.nanmean(winds, axis=0)
1500 1510 #---------------------------------------------------------------------
1501 1511 #Metodo General
1502 1512 # distx, disty, dist = self.calculateDistance(position_x,position_y,pairsCrossCorr, pairsList, azimuth)
1503 1513 # #Calculo Coeficientes de Funcion de Correlacion
1504 1514 # F,G,A,B,H = self.calculateCoef(tau1,tau2,distx,disty,n)
1505 1515 # #Calculo de Velocidades
1506 1516 # winds = self.calculateVelUV(F,G,A,B,H)
1507 1517
1508 1518 #---------------------------------------------------------------------
1509 1519 winds[2,:] = self.__calculateVelVer(phase1, lagTRange, _lambda)
1510 1520 winds = correctFactor*winds
1511 1521 return winds
1512 1522
1513 1523 def __checkTime(self, currentTime, paramInterval, outputInterval):
1514 1524
1515 1525 dataTime = currentTime + paramInterval
1516 1526 deltaTime = dataTime - self.__initime
1517 1527
1518 1528 if deltaTime >= outputInterval or deltaTime < 0:
1519 1529 self.__dataReady = True
1520 1530 return
1521 1531
1522 1532 def techniqueMeteors(self, arrayMeteor, meteorThresh, heightMin, heightMax):
1523 1533 '''
1524 1534 Function that implements winds estimation technique with detected meteors.
1525 1535
1526 1536 Input: Detected meteors, Minimum meteor quantity to wind estimation
1527 1537
1528 1538 Output: Winds estimation (Zonal and Meridional)
1529 1539
1530 1540 Parameters affected: Winds
1531 1541 '''
1532 1542 #Settings
1533 1543 nInt = (heightMax - heightMin)/2
1534 1544 winds = numpy.zeros((2,nInt))*numpy.nan
1535 1545
1536 1546 #Filter errors
1537 1547 error = numpy.where(arrayMeteor[:,-1] == 0)[0]
1538 1548 finalMeteor = arrayMeteor[error,:]
1539 1549
1540 1550 #Meteor Histogram
1541 1551 finalHeights = finalMeteor[:,3]
1542 1552 hist = numpy.histogram(finalHeights, bins = nInt, range = (heightMin,heightMax))
1543 1553 nMeteorsPerI = hist[0]
1544 1554 heightPerI = hist[1]
1545 1555
1546 1556 #Sort of meteors
1547 1557 indSort = finalHeights.argsort()
1548 1558 finalMeteor2 = finalMeteor[indSort,:]
1549 1559
1550 1560 # Calculating winds
1551 1561 ind1 = 0
1552 1562 ind2 = 0
1553 1563
1554 1564 for i in range(nInt):
1555 1565 nMet = nMeteorsPerI[i]
1556 1566 ind1 = ind2
1557 1567 ind2 = ind1 + nMet
1558 1568
1559 1569 meteorAux = finalMeteor2[ind1:ind2,:]
1560 1570
1561 1571 if meteorAux.shape[0] >= meteorThresh:
1562 1572 vel = meteorAux[:, 7]
1563 1573 zen = meteorAux[:, 5]*numpy.pi/180
1564 1574 azim = meteorAux[:, 4]*numpy.pi/180
1565 1575
1566 1576 n = numpy.cos(zen)
1567 1577 # m = (1 - n**2)/(1 - numpy.tan(azim)**2)
1568 1578 # l = m*numpy.tan(azim)
1569 1579 l = numpy.sin(zen)*numpy.sin(azim)
1570 1580 m = numpy.sin(zen)*numpy.cos(azim)
1571 1581
1572 1582 A = numpy.vstack((l, m)).transpose()
1573 1583 A1 = numpy.dot(numpy.linalg.inv( numpy.dot(A.transpose(),A) ),A.transpose())
1574 1584 windsAux = numpy.dot(A1, vel)
1575 1585
1576 1586 winds[0,i] = windsAux[0]
1577 1587 winds[1,i] = windsAux[1]
1578 1588
1579 1589 return winds, heightPerI[:-1]
1580 1590
1581 1591 def run(self, dataOut, technique, **kwargs):
1582 1592
1583 1593 param = dataOut.data_param
1584 1594 if dataOut.abscissaList != None:
1585 1595 absc = dataOut.abscissaList[:-1]
1586 1596 noise = dataOut.noise
1587 1597 heightList = dataOut.getHeiRange()
1588 1598 SNR = dataOut.data_SNR
1589 1599
1590 1600 if technique == 'DBS':
1591 1601
1592 1602 if kwargs.has_key('dirCosx') and kwargs.has_key('dirCosy'):
1593 1603 theta_x = numpy.array(kwargs['dirCosx'])
1594 1604 theta_y = numpy.array(kwargs['dirCosy'])
1595 1605 else:
1596 1606 elev = numpy.array(kwargs['elevation'])
1597 1607 azim = numpy.array(kwargs['azimuth'])
1598 1608 theta_x, theta_y = self.__calculateCosDir(elev, azim)
1599 1609 azimuth = kwargs['correctAzimuth']
1600 1610 if kwargs.has_key('horizontalOnly'):
1601 1611 horizontalOnly = kwargs['horizontalOnly']
1602 1612 else: horizontalOnly = False
1603 1613 if kwargs.has_key('correctFactor'):
1604 1614 correctFactor = kwargs['correctFactor']
1605 1615 else: correctFactor = 1
1606 1616 if kwargs.has_key('channelList'):
1607 1617 channelList = kwargs['channelList']
1608 1618 if len(channelList) == 2:
1609 1619 horizontalOnly = True
1610 1620 arrayChannel = numpy.array(channelList)
1611 1621 param = param[arrayChannel,:,:]
1612 1622 theta_x = theta_x[arrayChannel]
1613 1623 theta_y = theta_y[arrayChannel]
1614 1624
1615 1625 velRadial0 = param[:,1,:] #Radial velocity
1616 1626 dataOut.data_output, dataOut.heightList, dataOut.data_SNR = self.techniqueDBS(velRadial0, theta_x, theta_y, azimuth, correctFactor, horizontalOnly, heightList, SNR) #DBS Function
1617 1627 dataOut.utctimeInit = dataOut.utctime
1618 1628 dataOut.outputInterval = dataOut.timeInterval
1619 1629
1620 1630 elif technique == 'SA':
1621 1631
1622 1632 #Parameters
1623 1633 position_x = kwargs['positionX']
1624 1634 position_y = kwargs['positionY']
1625 1635 azimuth = kwargs['azimuth']
1626 1636
1627 1637 if kwargs.has_key('crosspairsList'):
1628 1638 pairs = kwargs['crosspairsList']
1629 1639 else:
1630 1640 pairs = None
1631 1641
1632 1642 if kwargs.has_key('correctFactor'):
1633 1643 correctFactor = kwargs['correctFactor']
1634 1644 else:
1635 1645 correctFactor = 1
1636 1646
1637 1647 tau = dataOut.data_param
1638 1648 _lambda = dataOut.C/dataOut.frequency
1639 1649 pairsList = dataOut.groupList
1640 1650 nChannels = dataOut.nChannels
1641 1651
1642 1652 dataOut.data_output = self.techniqueSA(pairs, pairsList, nChannels, tau, azimuth, _lambda, position_x, position_y, absc, correctFactor)
1643 1653 dataOut.utctimeInit = dataOut.utctime
1644 1654 dataOut.outputInterval = dataOut.timeInterval
1645 1655
1646 1656 elif technique == 'Meteors':
1647 1657 dataOut.flagNoData = True
1648 1658 self.__dataReady = False
1649 1659
1650 1660 if kwargs.has_key('nHours'):
1651 1661 nHours = kwargs['nHours']
1652 1662 else:
1653 1663 nHours = 1
1654 1664
1655 1665 if kwargs.has_key('meteorsPerBin'):
1656 1666 meteorThresh = kwargs['meteorsPerBin']
1657 1667 else:
1658 1668 meteorThresh = 6
1659 1669
1660 1670 if kwargs.has_key('hmin'):
1661 1671 hmin = kwargs['hmin']
1662 1672 else: hmin = 70
1663 1673 if kwargs.has_key('hmax'):
1664 1674 hmax = kwargs['hmax']
1665 1675 else: hmax = 110
1666 1676
1667 1677 dataOut.outputInterval = nHours*3600
1668 1678
1669 1679 if self.__isConfig == False:
1670 1680 # self.__initime = dataOut.datatime.replace(minute = 0, second = 0, microsecond = 03)
1671 1681 #Get Initial LTC time
1672 1682 self.__initime = datetime.datetime.utcfromtimestamp(self.dataOut.utctime)
1673 1683 self.__initime = (self.__initime.replace(minute = 0, second = 0, microsecond = 0) - datetime.datetime(1970, 1, 1)).total_seconds()
1674 1684
1675 1685 self.__isConfig = True
1676 1686
1677 1687 if self.__buffer == None:
1678 1688 self.__buffer = dataOut.data_param
1679 1689 self.__firstdata = copy.copy(dataOut)
1680 1690
1681 1691 else:
1682 1692 self.__buffer = numpy.vstack((self.__buffer, dataOut.data_param))
1683 1693
1684 1694 self.__checkTime(dataOut.utctime, dataOut.paramInterval, dataOut.outputInterval) #Check if the buffer is ready
1685 1695
1686 1696 if self.__dataReady:
1687 1697 dataOut.utctimeInit = self.__initime
1688 1698
1689 1699 self.__initime += dataOut.outputInterval #to erase time offset
1690 1700
1691 1701 dataOut.data_output, dataOut.heightList = self.techniqueMeteors(self.__buffer, meteorThresh, hmin, hmax)
1692 1702 dataOut.flagNoData = False
1693 1703 self.__buffer = None
1694 1704
1695 1705 return
1696 1706
1697 1707 class EWDriftsEstimation(Operation):
1698 1708
1699 1709
1700 1710 def __init__(self):
1701 1711 Operation.__init__(self)
1702 1712
1703 1713 def __correctValues(self, heiRang, phi, velRadial, SNR):
1704 1714 listPhi = phi.tolist()
1705 1715 maxid = listPhi.index(max(listPhi))
1706 1716 minid = listPhi.index(min(listPhi))
1707 1717
1708 1718 rango = range(len(phi))
1709 1719 # rango = numpy.delete(rango,maxid)
1710 1720
1711 1721 heiRang1 = heiRang*math.cos(phi[maxid])
1712 1722 heiRangAux = heiRang*math.cos(phi[minid])
1713 1723 indOut = (heiRang1 < heiRangAux[0]).nonzero()
1714 1724 heiRang1 = numpy.delete(heiRang1,indOut)
1715 1725
1716 1726 velRadial1 = numpy.zeros([len(phi),len(heiRang1)])
1717 1727 SNR1 = numpy.zeros([len(phi),len(heiRang1)])
1718 1728
1719 1729 for i in rango:
1720 1730 x = heiRang*math.cos(phi[i])
1721 1731 y1 = velRadial[i,:]
1722 1732 f1 = interpolate.interp1d(x,y1,kind = 'cubic')
1723 1733
1724 1734 x1 = heiRang1
1725 1735 y11 = f1(x1)
1726 1736
1727 1737 y2 = SNR[i,:]
1728 1738 f2 = interpolate.interp1d(x,y2,kind = 'cubic')
1729 1739 y21 = f2(x1)
1730 1740
1731 1741 velRadial1[i,:] = y11
1732 1742 SNR1[i,:] = y21
1733 1743
1734 1744 return heiRang1, velRadial1, SNR1
1735 1745
1736 1746 def run(self, dataOut, zenith, zenithCorrection):
1737 1747 heiRang = dataOut.heightList
1738 1748 velRadial = dataOut.data_param[:,3,:]
1739 1749 SNR = dataOut.data_SNR
1740 1750
1741 1751 zenith = numpy.array(zenith)
1742 1752 zenith -= zenithCorrection
1743 1753 zenith *= numpy.pi/180
1744 1754
1745 1755 heiRang1, velRadial1, SNR1 = self.__correctValues(heiRang, numpy.abs(zenith), velRadial, SNR)
1746 1756
1747 1757 alp = zenith[0]
1748 1758 bet = zenith[1]
1749 1759
1750 1760 w_w = velRadial1[0,:]
1751 1761 w_e = velRadial1[1,:]
1752 1762
1753 1763 w = (w_w*numpy.sin(bet) - w_e*numpy.sin(alp))/(numpy.cos(alp)*numpy.sin(bet) - numpy.cos(bet)*numpy.sin(alp))
1754 1764 u = (w_w*numpy.cos(bet) - w_e*numpy.cos(alp))/(numpy.sin(alp)*numpy.cos(bet) - numpy.sin(bet)*numpy.cos(alp))
1755 1765
1756 1766 winds = numpy.vstack((u,w))
1757 1767
1758 1768 dataOut.heightList = heiRang1
1759 1769 dataOut.data_output = winds
1760 1770 dataOut.data_SNR = SNR1
1761 1771
1762 1772 dataOut.utctimeInit = dataOut.utctime
1763 1773 dataOut.outputInterval = dataOut.timeInterval
1764 1774 return
1765 1775
1766 1776
1767 1777
1768 1778
1769 1779
1770 1780 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now