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