##// END OF EJS Templates
fixing merge conflicts
jespinoza -
r1090:36f30f86830d merge
parent child
Show More

The requested changes are too big and content was truncated. Show full diff

This diff has been collapsed as it changes many lines, (751 lines changed) Show them Hide them
@@ -0,0 +1,751
1
2 '''
3 Created on Jul 3, 2014
4
5 @author: roj-idl71
6 '''
7 # SUBCHANNELS EN VEZ DE CHANNELS
8 # BENCHMARKS -> PROBLEMAS CON ARCHIVOS GRANDES -> INCONSTANTE EN EL TIEMPO
9 # ACTUALIZACION DE VERSION
10 # HEADERS
11 # MODULO DE ESCRITURA
12 # METADATA
13
14 import os
15 import datetime
16 import numpy
17 import timeit
18 from profilehooks import coverage, profile
19 from fractions import Fraction
20
21 try:
22 from gevent import sleep
23 except:
24 from time import sleep
25
26 from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader
27 from schainpy.model.data.jrodata import Voltage
28 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
29 from time import time
30
31 import cPickle
32 try:
33 import digital_rf
34 except:
35 print 'You should install "digital_rf" module if you want to read Digital RF data'
36
37 class DigitalRFReader(ProcessingUnit):
38 '''
39 classdocs
40 '''
41
42 def __init__(self, **kwargs):
43 '''
44 Constructor
45 '''
46
47 ProcessingUnit.__init__(self, **kwargs)
48
49 self.dataOut = Voltage()
50 self.__printInfo = True
51 self.__flagDiscontinuousBlock = False
52 self.__bufferIndex = 9999999
53 self.__ippKm = None
54 self.__codeType = 0
55 self.__nCode = None
56 self.__nBaud = None
57 self.__code = None
58 self.dtype = None
59
60 def close(self):
61 print 'Average of writing to digital rf format is ', self.oldAverage * 1000
62 return
63
64 def __getCurrentSecond(self):
65
66 return self.__thisUnixSample/self.__sample_rate
67
68 thisSecond = property(__getCurrentSecond, "I'm the 'thisSecond' property.")
69
70 def __setFileHeader(self):
71 '''
72 In this method will be initialized every parameter of dataOut object (header, no data)
73 '''
74 ippSeconds = 1.0*self.__nSamples/self.__sample_rate
75
76 nProfiles = 1.0/ippSeconds # Number of profiles in one second
77
78 try:
79 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(self.__radarControllerHeader)
80 except:
81 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(
82 txA=0,
83 txB=0,
84 nWindows=1,
85 nHeights=self.__nSamples,
86 firstHeight=self.__firstHeigth,
87 deltaHeight=self.__deltaHeigth,
88 codeType=self.__codeType,
89 nCode=self.__nCode, nBaud=self.__nBaud,
90 code = self.__code)
91
92 try:
93 self.dataOut.systemHeaderObj = SystemHeader(self.__systemHeader)
94 except:
95 self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples,
96 nProfiles=nProfiles,
97 nChannels=len(self.__channelList),
98 adcResolution=14)
99 self.dataOut.type = "Voltage"
100
101 self.dataOut.data = None
102
103 self.dataOut.dtype = self.dtype
104
105 # self.dataOut.nChannels = 0
106
107 # self.dataOut.nHeights = 0
108
109 self.dataOut.nProfiles = int(nProfiles)
110
111 self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype = numpy.float)*self.__deltaHeigth
112
113 self.dataOut.channelList = range(self.__num_subchannels)
114
115 self.dataOut.blocksize = self.dataOut.getNChannels() * self.dataOut.getNHeights()
116
117 # self.dataOut.channelIndexList = None
118
119 self.dataOut.flagNoData = True
120
121 self.dataOut.flagDataAsBlock = False
122 # Set to TRUE if the data is discontinuous
123 self.dataOut.flagDiscontinuousBlock = False
124
125 self.dataOut.utctime = None
126
127 self.dataOut.timeZone = self.__timezone/60 # timezone like jroheader, difference in minutes between UTC and localtime
128
129 self.dataOut.dstFlag = 0
130
131 self.dataOut.errorCount = 0
132
133 try:
134 self.dataOut.nCohInt = self.fixed_metadata_dict.get('nCohInt', 1)
135
136 self.dataOut.flagDecodeData = self.fixed_metadata_dict['flagDecodeData'] # asumo que la data esta decodificada
137
138 self.dataOut.flagDeflipData = self.fixed_metadata_dict['flagDeflipData'] # asumo que la data esta sin flip
139
140 self.dataOut.flagShiftFFT = self.fixed_metadata_dict['flagShiftFFT']
141
142 self.dataOut.useLocalTime = self.fixed_metadata_dict['useLocalTime']
143 except:
144 pass
145
146
147 self.dataOut.ippSeconds = ippSeconds
148
149 # Time interval between profiles
150 # self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt
151
152 self.dataOut.frequency = self.__frequency
153
154 self.dataOut.realtime = self.__online
155
156 def findDatafiles(self, path, startDate=None, endDate=None):
157
158 if not os.path.isdir(path):
159 return []
160
161 try:
162 digitalReadObj = digital_rf.DigitalRFReader(path, load_all_metadata=True)
163 except:
164 digitalReadObj = digital_rf.DigitalRFReader(path)
165
166 channelNameList = digitalReadObj.get_channels()
167
168 if not channelNameList:
169 return []
170
171 metadata_dict = digitalReadObj.get_rf_file_metadata(channelNameList[0])
172
173 sample_rate = metadata_dict['sample_rate'][0]
174
175 this_metadata_file = digitalReadObj.get_metadata(channelNameList[0])
176
177 try:
178 timezone = this_metadata_file['timezone'].value
179 except:
180 timezone = 0
181
182 startUTCSecond, endUTCSecond = digitalReadObj.get_bounds(channelNameList[0])/sample_rate - timezone
183
184 startDatetime = datetime.datetime.utcfromtimestamp(startUTCSecond)
185 endDatatime = datetime.datetime.utcfromtimestamp(endUTCSecond)
186
187 if not startDate:
188 startDate = startDatetime.date()
189
190 if not endDate:
191 endDate = endDatatime.date()
192
193 dateList = []
194
195 thisDatetime = startDatetime
196
197 while(thisDatetime<=endDatatime):
198
199 thisDate = thisDatetime.date()
200
201 if thisDate < startDate:
202 continue
203
204 if thisDate > endDate:
205 break
206
207 dateList.append(thisDate)
208 thisDatetime += datetime.timedelta(1)
209
210 return dateList
211
212 def setup(self, path = None,
213 startDate = None,
214 endDate = None,
215 startTime = datetime.time(0,0,0),
216 endTime = datetime.time(23,59,59),
217 channelList = None,
218 nSamples = None,
219 online = False,
220 delay = 60,
221 buffer_size = 1024,
222 ippKm=None,
223 **kwargs):
224 '''
225 In this method we should set all initial parameters.
226
227 Inputs:
228 path
229 startDate
230 endDate
231 startTime
232 endTime
233 set
234 expLabel
235 ext
236 online
237 delay
238 '''
239 self.i = 0
240 if not os.path.isdir(path):
241 raise ValueError, "[Reading] Directory %s does not exist" %path
242
243 try:
244 self.digitalReadObj = digital_rf.DigitalRFReader(path, load_all_metadata=True)
245 except:
246 self.digitalReadObj = digital_rf.DigitalRFReader(path)
247
248 channelNameList = self.digitalReadObj.get_channels()
249
250 if not channelNameList:
251 raise ValueError, "[Reading] Directory %s does not have any files" %path
252
253 if not channelList:
254 channelList = range(len(channelNameList))
255
256
257 ########## Reading metadata ######################
258
259 top_properties = self.digitalReadObj.get_properties(channelNameList[channelList[0]])
260
261
262 self.__num_subchannels = top_properties['num_subchannels']
263 self.__sample_rate = 1.0 * top_properties['sample_rate_numerator'] / top_properties['sample_rate_denominator']
264 # self.__samples_per_file = top_properties['samples_per_file'][0]
265 self.__deltaHeigth = 1e6*0.15/self.__sample_rate ## why 0.15?
266
267 this_metadata_file = self.digitalReadObj.get_digital_metadata(channelNameList[channelList[0]])
268 metadata_bounds = this_metadata_file.get_bounds()
269 self.fixed_metadata_dict = this_metadata_file.read(metadata_bounds[0])[metadata_bounds[0]] ## GET FIRST HEADER
270
271 try:
272 self.__processingHeader = self.fixed_metadata_dict['processingHeader']
273 self.__radarControllerHeader = self.fixed_metadata_dict['radarControllerHeader']
274 self.__systemHeader = self.fixed_metadata_dict['systemHeader']
275 self.dtype = cPickle.loads(self.fixed_metadata_dict['dtype'])
276 except:
277 pass
278
279
280 self.__frequency = None
281
282 self.__frequency = self.fixed_metadata_dict.get('frequency', 1)
283
284 self.__timezone = self.fixed_metadata_dict.get('timezone', 300)
285
286
287 try:
288 nSamples = self.fixed_metadata_dict['nSamples']
289 except:
290 nSamples = None
291
292 self.__firstHeigth = 0
293
294 try:
295 codeType = self.__radarControllerHeader['codeType']
296 except:
297 codeType = 0
298
299 nCode = 1
300 nBaud = 1
301 code = numpy.ones((nCode, nBaud), dtype=numpy.int)
302
303 try:
304 if codeType:
305 nCode = self.__radarControllerHeader['nCode']
306 nBaud = self.__radarControllerHeader['nBaud']
307 code = self.__radarControllerHeader['code']
308 except:
309 pass
310
311
312 if not ippKm:
313 try:
314 # seconds to km
315 ippKm = self.__radarControllerHeader['ipp']
316 except:
317 ippKm = None
318 ####################################################
319 self.__ippKm = ippKm
320 startUTCSecond = None
321 endUTCSecond = None
322
323 if startDate:
324 startDatetime = datetime.datetime.combine(startDate, startTime)
325 startUTCSecond = (startDatetime-datetime.datetime(1970,1,1)).total_seconds() + self.__timezone
326
327 if endDate:
328 endDatetime = datetime.datetime.combine(endDate, endTime)
329 endUTCSecond = (endDatetime-datetime.datetime(1970,1,1)).total_seconds() + self.__timezone
330
331 start_index, end_index = self.digitalReadObj.get_bounds(channelNameList[channelList[0]])
332
333 if not startUTCSecond:
334 startUTCSecond = start_index/self.__sample_rate
335
336 if start_index > startUTCSecond*self.__sample_rate:
337 startUTCSecond = start_index/self.__sample_rate
338
339 if not endUTCSecond:
340 endUTCSecond = end_index/self.__sample_rate
341
342 if end_index < endUTCSecond*self.__sample_rate:
343 endUTCSecond = end_index/self.__sample_rate
344 if not nSamples:
345 if not ippKm:
346 raise ValueError, "[Reading] nSamples or ippKm should be defined"
347 nSamples = int(ippKm / (1e6*0.15/self.__sample_rate))
348 channelBoundList = []
349 channelNameListFiltered = []
350
351 for thisIndexChannel in channelList:
352 thisChannelName = channelNameList[thisIndexChannel]
353 start_index, end_index = self.digitalReadObj.get_bounds(thisChannelName)
354 channelBoundList.append((start_index, end_index))
355 channelNameListFiltered.append(thisChannelName)
356
357 self.profileIndex = 0
358 self.i= 0
359 self.__delay = delay
360
361 self.__codeType = codeType
362 self.__nCode = nCode
363 self.__nBaud = nBaud
364 self.__code = code
365
366 self.__datapath = path
367 self.__online = online
368 self.__channelList = channelList
369 self.__channelNameList = channelNameListFiltered
370 self.__channelBoundList = channelBoundList
371 self.__nSamples = nSamples
372 self.__samples_to_read = long(nSamples) # FIJO: AHORA 40
373 self.__nChannels = len(self.__channelList)
374
375 self.__startUTCSecond = startUTCSecond
376 self.__endUTCSecond = endUTCSecond
377
378 self.__timeInterval = 1.0 * self.__samples_to_read/self.__sample_rate # Time interval
379
380 if online:
381 # self.__thisUnixSample = int(endUTCSecond*self.__sample_rate - 4*self.__samples_to_read)
382 startUTCSecond = numpy.floor(endUTCSecond)
383
384 self.__thisUnixSample = long(startUTCSecond*self.__sample_rate) - self.__samples_to_read ## por que en el otro metodo lo primero q se hace es sumar samplestoread
385
386 self.__data_buffer = numpy.zeros((self.__num_subchannels, self.__samples_to_read), dtype = numpy.complex)
387
388 self.__setFileHeader()
389 self.isConfig = True
390
391 print "[Reading] Digital RF Data was found from %s to %s " %(
392 datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
393 datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
394 )
395
396 print "[Reading] Starting process from %s to %s" %(datetime.datetime.utcfromtimestamp(startUTCSecond - self.__timezone),
397 datetime.datetime.utcfromtimestamp(endUTCSecond - self.__timezone)
398 )
399 self.oldAverage = None
400 self.count = 0
401 self.executionTime = 0
402 def __reload(self):
403 # print
404 # print "%s not in range [%s, %s]" %(
405 # datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
406 # datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
407 # datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
408 # )
409 print "[Reading] reloading metadata ..."
410
411 try:
412 self.digitalReadObj.reload(complete_update=True)
413 except:
414 self.digitalReadObj.reload()
415
416 start_index, end_index = self.digitalReadObj.get_bounds(self.__channelNameList[self.__channelList[0]])
417
418 if start_index > self.__startUTCSecond*self.__sample_rate:
419 self.__startUTCSecond = 1.0*start_index/self.__sample_rate
420
421 if end_index > self.__endUTCSecond*self.__sample_rate:
422 self.__endUTCSecond = 1.0*end_index/self.__sample_rate
423 print
424 print "[Reading] New timerange found [%s, %s] " %(
425 datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
426 datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
427 )
428
429 return True
430
431 return False
432
433 def timeit(self, toExecute):
434 t0 = time()
435 toExecute()
436 self.executionTime = time() - t0
437 if self.oldAverage is None: self.oldAverage = self.executionTime
438 self.oldAverage = (self.executionTime + self.count*self.oldAverage) / (self.count + 1.0)
439 self.count = self.count + 1.0
440 return
441
442 def __readNextBlock(self, seconds=30, volt_scale = 1):
443 '''
444 '''
445
446 # Set the next data
447 self.__flagDiscontinuousBlock = False
448 self.__thisUnixSample += self.__samples_to_read
449
450 if self.__thisUnixSample + 2*self.__samples_to_read > self.__endUTCSecond*self.__sample_rate:
451 print "[Reading] There are no more data into selected time-range"
452 if self.__online:
453 self.__reload()
454 else:
455 return False
456
457 if self.__thisUnixSample + 2*self.__samples_to_read > self.__endUTCSecond*self.__sample_rate:
458 return False
459 self.__thisUnixSample -= self.__samples_to_read
460
461 indexChannel = 0
462
463 dataOk = False
464 for thisChannelName in self.__channelNameList: ##TODO VARIOS CHANNELS?
465 for indexSubchannel in range(self.__num_subchannels):
466 try:
467 t0 = time()
468 result = self.digitalReadObj.read_vector_c81d(self.__thisUnixSample,
469 self.__samples_to_read,
470 thisChannelName, sub_channel=indexSubchannel)
471 self.executionTime = time() - t0
472 if self.oldAverage is None: self.oldAverage = self.executionTime
473 self.oldAverage = (self.executionTime + self.count*self.oldAverage) / (self.count + 1.0)
474 self.count = self.count + 1.0
475
476 except IOError, e:
477 #read next profile
478 self.__flagDiscontinuousBlock = True
479 print "[Reading] %s" %datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), e
480 break
481
482 if result.shape[0] != self.__samples_to_read:
483 self.__flagDiscontinuousBlock = True
484 print "[Reading] %s: Too few samples were found, just %d/%d samples" %(datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
485 result.shape[0],
486 self.__samples_to_read)
487 break
488
489 self.__data_buffer[indexSubchannel,:] = result*volt_scale
490
491 indexChannel += 1
492
493 dataOk = True
494
495 self.__utctime = self.__thisUnixSample/self.__sample_rate
496
497 if not dataOk:
498 return False
499
500 print "[Reading] %s: %d samples <> %f sec" %(datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
501 self.__samples_to_read,
502 self.__timeInterval)
503
504 self.__bufferIndex = 0
505
506 return True
507
508 def __isBufferEmpty(self):
509 return self.__bufferIndex > self.__samples_to_read - self.__nSamples #40960 - 40
510
511 def getData(self, seconds=30, nTries=5):
512
513 '''
514 This method gets the data from files and put the data into the dataOut object
515
516 In addition, increase el the buffer counter in one.
517
518 Return:
519 data : retorna un perfil de voltages (alturas * canales) copiados desde el
520 buffer. Si no hay mas archivos a leer retorna None.
521
522 Affected:
523 self.dataOut
524 self.profileIndex
525 self.flagDiscontinuousBlock
526 self.flagIsNewBlock
527 '''
528
529 err_counter = 0
530 self.dataOut.flagNoData = True
531
532 if self.__isBufferEmpty():
533 self.__flagDiscontinuousBlock = False
534
535 while True:
536 if self.__readNextBlock():
537 break
538 if self.__thisUnixSample > self.__endUTCSecond*self.__sample_rate:
539 return False
540
541 if self.__flagDiscontinuousBlock:
542 print '[Reading] discontinuous block found ... continue with the next block'
543 continue
544
545 if not self.__online:
546 return False
547
548 err_counter += 1
549 if err_counter > nTries:
550 return False
551
552 print '[Reading] waiting %d seconds to read a new block' %seconds
553 sleep(seconds)
554
555 self.dataOut.data = self.__data_buffer[:,self.__bufferIndex:self.__bufferIndex+self.__nSamples]
556 self.dataOut.utctime = (self.__thisUnixSample + self.__bufferIndex)/self.__sample_rate
557 self.dataOut.flagNoData = False
558 self.dataOut.flagDiscontinuousBlock = self.__flagDiscontinuousBlock
559 self.dataOut.profileIndex = self.profileIndex
560
561 self.__bufferIndex += self.__nSamples
562 self.profileIndex += 1
563
564 if self.profileIndex == self.dataOut.nProfiles:
565 self.profileIndex = 0
566
567 return True
568
569 def printInfo(self):
570 '''
571 '''
572 if self.__printInfo == False:
573 return
574
575 # self.systemHeaderObj.printInfo()
576 # self.radarControllerHeaderObj.printInfo()
577
578 self.__printInfo = False
579
580 def printNumberOfBlock(self):
581 '''
582 '''
583 return
584 # print self.profileIndex
585
586
587 def run(self, **kwargs):
588 '''
589 This method will be called many times so here you should put all your code
590 '''
591
592 if not self.isConfig:
593 self.setup(**kwargs)
594 #self.i = self.i+1
595 self.getData(seconds=self.__delay)
596
597 return
598
599 class DigitalRFWriter(Operation):
600 '''
601 classdocs
602 '''
603
604 def __init__(self, **kwargs):
605 '''
606 Constructor
607 '''
608 Operation.__init__(self, **kwargs)
609 self.metadata_dict = {}
610 self.dataOut = None
611 self.dtype = None
612
613 def setHeader(self):
614
615 self.metadata_dict['frequency'] = self.dataOut.frequency
616 self.metadata_dict['timezone'] = self.dataOut.timeZone
617 self.metadata_dict['dtype'] = cPickle.dumps(self.dataOut.dtype)
618 self.metadata_dict['nProfiles'] = self.dataOut.nProfiles
619 self.metadata_dict['heightList'] = self.dataOut.heightList
620 self.metadata_dict['channelList'] = self.dataOut.channelList
621 self.metadata_dict['flagDecodeData'] = self.dataOut.flagDecodeData
622 self.metadata_dict['flagDeflipData'] = self.dataOut.flagDeflipData
623 self.metadata_dict['flagShiftFFT'] = self.dataOut.flagShiftFFT
624 self.metadata_dict['flagDataAsBlock'] = self.dataOut.flagDataAsBlock
625 self.metadata_dict['useLocalTime'] = self.dataOut.useLocalTime
626 self.metadata_dict['nCohInt'] = self.dataOut.nCohInt
627
628 return
629
630 def setup(self, dataOut, path, frequency, fileCadence, dirCadence, metadataCadence, set=0, metadataFile='metadata', ext='.h5'):
631 '''
632 In this method we should set all initial parameters.
633 Input:
634 dataOut: Input data will also be outputa data
635 '''
636 self.setHeader()
637 self.__ippSeconds = dataOut.ippSeconds
638 self.__deltaH = dataOut.getDeltaH()
639 self.__sample_rate = 1e6*0.15/self.__deltaH
640 self.__dtype = dataOut.dtype
641 if len(dataOut.dtype) == 2:
642 self.__dtype = dataOut.dtype[0]
643 self.__nSamples = dataOut.systemHeaderObj.nSamples
644 self.__nProfiles = dataOut.nProfiles
645 self.__blocks_per_file = dataOut.processingHeaderObj.dataBlocksPerFile
646
647 self.arr_data = arr_data = numpy.ones((self.__nSamples, len(self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)])
648
649 file_cadence_millisecs = long(1.0 * self.__blocks_per_file * self.__nProfiles * self.__nSamples / self.__sample_rate) * 1000
650 sub_cadence_secs = file_cadence_millisecs / 500
651
652 sample_rate_fraction = Fraction(self.__sample_rate).limit_denominator()
653 sample_rate_numerator = long(sample_rate_fraction.numerator)
654 sample_rate_denominator = long(sample_rate_fraction.denominator)
655 start_global_index = dataOut.utctime * self.__sample_rate
656
657 uuid = 'prueba'
658 compression_level = 1
659 checksum = False
660 is_complex = True
661 num_subchannels = len(dataOut.channelList)
662 is_continuous = True
663 marching_periods = False
664
665 self.digitalWriteObj = digital_rf.DigitalRFWriter(path, self.__dtype, dirCadence,
666 fileCadence, start_global_index,
667 sample_rate_numerator, sample_rate_denominator, uuid, compression_level, checksum,
668 is_complex, num_subchannels, is_continuous, marching_periods)
669
670 metadata_dir = os.path.join(path, 'metadata')
671 os.system('mkdir %s' % (metadata_dir))
672
673 self.digitalMetadataWriteObj = digital_rf.DigitalMetadataWriter(metadata_dir, dirCadence, 1, ##236, file_cadence_millisecs / 1000
674 sample_rate_numerator, sample_rate_denominator,
675 metadataFile)
676
677
678 self.isConfig = True
679 self.currentSample = 0
680 self.oldAverage = 0
681 self.count = 0
682 return
683
684 def writeMetadata(self):
685 print '[Writing] - Writing metadata'
686 start_idx = self.__sample_rate * self.dataOut.utctime
687
688 self.metadata_dict['processingHeader'] = self.dataOut.processingHeaderObj.getAsDict()
689 self.metadata_dict['radarControllerHeader'] = self.dataOut.radarControllerHeaderObj.getAsDict()
690 self.metadata_dict['systemHeader'] = self.dataOut.systemHeaderObj.getAsDict()
691 self.digitalMetadataWriteObj.write(start_idx, self.metadata_dict)
692 return
693
694
695 def timeit(self, toExecute):
696 t0 = time()
697 toExecute()
698 self.executionTime = time() - t0
699 if self.oldAverage is None: self.oldAverage = self.executionTime
700 self.oldAverage = (self.executionTime + self.count*self.oldAverage) / (self.count + 1.0)
701 self.count = self.count + 1.0
702 return
703
704
705 def writeData(self):
706 for i in range(self.dataOut.systemHeaderObj.nSamples):
707 for channel in self.dataOut.channelList:
708 self.arr_data[i][channel]['r'] = self.dataOut.data[channel][i].real
709 self.arr_data[i][channel]['i'] = self.dataOut.data[channel][i].imag
710
711 def f(): return self.digitalWriteObj.rf_write(self.arr_data)
712 self.timeit(f)
713
714 return
715
716 def run(self, dataOut, frequency=49.92e6, path=None, fileCadence=100, dirCadence=25, metadataCadence=1, **kwargs):
717 '''
718 This method will be called many times so here you should put all your code
719 Inputs:
720 dataOut: object with the data
721 '''
722 # print dataOut.__dict__
723 self.dataOut = dataOut
724 if not self.isConfig:
725 self.setup(dataOut, path, frequency, fileCadence, dirCadence, metadataCadence, **kwargs)
726 self.writeMetadata()
727
728 self.writeData()
729
730 ## self.currentSample += 1
731 ## if self.dataOut.flagDataAsBlock or self.currentSample == 1:
732 ## self.writeMetadata()
733 ## if self.currentSample == self.__nProfiles: self.currentSample = 0
734
735 def close(self):
736 print '[Writing] - Closing files '
737 print 'Average of writing to digital rf format is ', self.oldAverage * 1000
738 try:
739 self.digitalWriteObj.close()
740 except:
741 pass
742
743 # raise
744 if __name__ == '__main__':
745
746 readObj = DigitalRFReader()
747
748 while True:
749 readObj.run(path='/home/jchavez/jicamarca/mocked_data/')
750 # readObj.printInfo()
751 # readObj.printNumberOfBlock()
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,115 +1,115
1 1 # Byte-compiled / optimized / DLL files
2 2 __pycache__/
3 3 *.py[cod]
4 4 *$py.class
5 5
6 6 # C extensions
7 7 *.so
8 8
9 9 # Distribution / packaging
10 10 .Python
11 11 env/
12 12 build/
13 13 develop-eggs/
14 14 dist/
15 15 downloads/
16 16 eggs/
17 17 .eggs/
18 18 lib/
19 19 lib64/
20 20 parts/
21 21 sdist/
22 22 var/
23 23 wheels/
24 24 *.egg-info/
25 25 .installed.cfg
26 26 *.egg
27 27
28 28 # PyInstaller
29 29 # Usually these files are written by a python script from a template
30 30 # before PyInstaller builds the exe, so as to inject date/other infos into it.
31 31 *.manifest
32 32 *.spec
33 33
34 34 # Installer logs
35 35 pip-log.txt
36 36 pip-delete-this-directory.txt
37 37
38 38 # Unit test / coverage reports
39 39 htmlcov/
40 40 .tox/
41 41 .coverage
42 42 .coverage.*
43 43 .cache
44 44 nosetests.xml
45 45 coverage.xml
46 46 *,cover
47 47 .hypothesis/
48 48
49 49 # Translations
50 50 *.mo
51 51 *.pot
52 52
53 53 # Django stuff:
54 54 *.log
55 55 local_settings.py
56 56
57 57 # Flask stuff:
58 58 instance/
59 59 .webassets-cache
60 60
61 61 # Scrapy stuff:
62 62 .scrapy
63 63
64 64 # Sphinx documentation
65 65 docs/_build/
66 66
67 67 # PyBuilder
68 68 target/
69 69
70 70 # Jupyter Notebook
71 71 .ipynb_checkpoints
72 72
73 73 # pyenv
74 74 .python-version
75 75
76 76 # celery beat schedule file
77 77 celerybeat-schedule
78 78
79 79 # SageMath parsed files
80 80 *.sage.py
81 81
82 82 # dotenv
83 83 .env
84 84
85 85 # virtualenv
86 86 .venv
87 87 venv/
88 88 ENV/
89 89
90 90 # Spyder project settings
91 91 .spyderproject
92 92 .spyproject
93 93
94 94 # Rope project settings
95 95 .ropeproject
96 96
97 97 # mkdocs documentation
98 98 /site
99 99
100 100 # eclipse
101 101 .project
102 102 .pydevproject
103
104 103 # vscode
105 104
106 105 .vscode
107 106
108 schainpy/scripts/
109 107 schaingui/node_modules/
108 schainpy/scripts/
110 109 .svn/
111 110 *.png
112 111 *.pyc
113 *.xml
112 schainpy/scripts
113 .vscode
114 trash
114 115 *.log
115 trash No newline at end of file
@@ -1,9 +1,9
1 # schaing
1 # schain
2 2
3 3 Command Line Interface for SIGNAL CHAIN - jro
4 4
5 5 # Usage
6 6
7 7 To use it:
8 8
9 9 $ schain-cli --help
@@ -1,157 +1,168
1 1 import click
2 2 import schainpy
3 3 import subprocess
4 4 import os
5 5 import sys
6 6 import glob
7 7 save_stdout = sys.stdout
8 8 sys.stdout = open('trash', 'w')
9 9 from multiprocessing import cpu_count
10 10 from schaincli import templates
11 11 from schainpy.controller import Project
12 12 from schainpy.model import Operation, ProcessingUnit
13 13 from schainpy.utils import log
14 14 from importlib import import_module
15 15 from pydoc import locate
16 16 from fuzzywuzzy import process
17 17 from schainpy.utils import paramsFinder
18 18 sys.stdout = save_stdout
19 19
20 20
21 21 def print_version(ctx, param, value):
22 22 if not value or ctx.resilient_parsing:
23 23 return
24 24 click.echo(schainpy.__version__)
25 25 ctx.exit()
26 26
27 27
28 28 cliLogger = log.makelogger('schain cli')
29 29 PREFIX = 'experiment'
30 30
31 31
32 32 @click.command()
33 33 @click.option('--version', '-v', is_flag=True, callback=print_version, help='SChain version', type=str)
34 @click.option('--xml', '-x', default=None, help='run an XML file', type=click.Path(exists=True, resolve_path=True))
35 34 @click.argument('command', default='run', required=True)
36 35 @click.argument('nextcommand', default=None, required=False, type=str)
37 def main(command, nextcommand, version, xml):
36 def main(command, nextcommand, version):
38 37 """COMMAND LINE INTERFACE FOR SIGNAL CHAIN - JICAMARCA RADIO OBSERVATORY \n
39 38 Available commands.\n
40 39 --xml: runs a schain XML generated file\n
41 40 run: runs any python script starting 'experiment_'\n
42 41 generate: generates a template schain script\n
43 42 search: return avilable operations, procs or arguments of the give operation/proc\n"""
44 if xml is not None:
45 runFromXML(xml)
43 if command == 'xml':
44 runFromXML(nextcommand)
46 45 elif command == 'generate':
47 46 generate()
48 47 elif command == 'test':
49 48 test()
50 49 elif command == 'run':
51 50 runschain(nextcommand)
52 51 elif command == 'search':
53 52 search(nextcommand)
54 53 else:
55 54 log.error('Command {} is not defined'.format(command))
56 55
56
57 57 def check_module(possible, instance):
58 58 def check(x):
59 59 try:
60 60 instancia = locate('schainpy.model.{}'.format(x))
61 61 return isinstance(instancia(), instance)
62 62 except Exception as e:
63 63 return False
64 64 clean = clean_modules(possible)
65 65 return [x for x in clean if check(x)]
66 66
67 67
68 68 def clean_modules(module):
69 69 noEndsUnder = [x for x in module if not x.endswith('__')]
70 70 noStartUnder = [x for x in noEndsUnder if not x.startswith('__')]
71 71 noFullUpper = [x for x in noStartUnder if not x.isupper()]
72 72 return noFullUpper
73 73
74 74
75 75 def search(nextcommand):
76 76 if nextcommand is None:
77 77 log.error('There is no Operation/ProcessingUnit to search')
78 78 elif nextcommand == 'procs':
79 79 procs = paramsFinder.getProcs()
80 log.success('Current ProcessingUnits are:\n\033[1m{}\033[0m'.format('\n'.join(procs)))
80 log.success(
81 'Current ProcessingUnits are:\n\033[1m{}\033[0m'.format('\n'.join(procs)))
81 82
82 83 elif nextcommand == 'operations':
83 84 operations = paramsFinder.getOperations()
84 log.success('Current Operations are:\n\033[1m{}\033[0m'.format('\n'.join(operations)))
85 log.success('Current Operations are:\n\033[1m{}\033[0m'.format(
86 '\n'.join(operations)))
85 87 else:
86 88 try:
87 89 args = paramsFinder.getArgs(nextcommand)
88 log.warning('Use this feature with caution. It may not return all the allowed arguments')
90 log.warning(
91 'Use this feature with caution. It may not return all the allowed arguments')
89 92 if len(args) == 0:
90 93 log.success('{} has no arguments'.format(nextcommand))
91 94 else:
92 log.success('Showing arguments of {} are:\n\033[1m{}\033[0m'.format(nextcommand, '\n'.join(args)))
95 log.success('Showing {} arguments:\n\033[1m{}\033[0m'.format(
96 nextcommand, '\n'.join(args)))
93 97 except Exception as e:
94 98 log.error('Module {} does not exists'.format(nextcommand))
95 99 allModules = paramsFinder.getAll()
96 100 similar = process.extractOne(nextcommand, allModules)[0]
97 101 log.success('Showing {} instead'.format(similar))
98 102 search(similar)
99 103
100 104
101 105 def runschain(nextcommand):
102 106 if nextcommand is None:
103 107 currentfiles = glob.glob('./{}_*.py'.format(PREFIX))
104 108 numberfiles = len(currentfiles)
105 109 if numberfiles > 1:
106 110 log.error('There is more than one file to run')
107 111 elif numberfiles == 1:
108 112 subprocess.call(['python ' + currentfiles[0]], shell=True)
109 113 else:
110 114 log.error('There is no file to run')
111 115 else:
112 116 try:
113 117 subprocess.call(['python ' + nextcommand], shell=True)
114 118 except Exception as e:
115 119 log.error("I cannot run the file. Does it exists?")
116 120
117 121
118 122 def basicInputs():
119 123 inputs = {}
120 inputs['desc'] = click.prompt('Enter a description', default="A schain project", type=str)
121 inputs['name'] = click.prompt('Name of the project', default="project", type=str)
122 inputs['path'] = click.prompt('Data path', default=os.getcwd(), type=click.Path(exists=True, resolve_path=True))
123 inputs['startDate'] = click.prompt('Start date', default='1970/01/01', type=str)
124 inputs['endDate'] = click.prompt('End date', default='2017/12/31', type=str)
125 inputs['startHour'] = click.prompt('Start hour', default='00:00:00', type=str)
124 inputs['desc'] = click.prompt(
125 'Enter a description', default="A schain project", type=str)
126 inputs['name'] = click.prompt(
127 'Name of the project', default="project", type=str)
128 inputs['path'] = click.prompt('Data path', default=os.getcwd(
129 ), type=click.Path(exists=True, resolve_path=True))
130 inputs['startDate'] = click.prompt(
131 'Start date', default='1970/01/01', type=str)
132 inputs['endDate'] = click.prompt(
133 'End date', default='2017/12/31', type=str)
134 inputs['startHour'] = click.prompt(
135 'Start hour', default='00:00:00', type=str)
126 136 inputs['endHour'] = click.prompt('End hour', default='23:59:59', type=str)
127 137 inputs['figpath'] = inputs['path'] + '/figs'
128 138 return inputs
129 139
130 140
131 141 def generate():
132 142 inputs = basicInputs()
133 143 inputs['multiprocess'] = click.confirm('Is this a multiprocess script?')
134 144 if inputs['multiprocess']:
135 inputs['nProcess'] = click.prompt('How many process?', default=cpu_count(), type=int)
145 inputs['nProcess'] = click.prompt(
146 'How many process?', default=cpu_count(), type=int)
136 147 current = templates.multiprocess.format(**inputs)
137 148 else:
138 149 current = templates.basic.format(**inputs)
139 150 scriptname = '{}_{}.py'.format(PREFIX, inputs['name'])
140 151 script = open(scriptname, 'w')
141 152 try:
142 153 script.write(current)
143 154 log.success('Script {} generated'.format(scriptname))
144 155 except Exception as e:
145 156 log.error('I cannot create the file. Do you have writing permissions?')
146 157
147 158
148 159 def test():
149 160 log.warning('testing')
150 161
151 162
152 163 def runFromXML(filename):
153 164 controller = Project()
154 165 if not controller.readXml(filename):
155 166 return
156 167 controller.start()
157 168 return
@@ -1,75 +1,90
1 1 basic = '''from schainpy.controller import Project
2 2
3 3 desc = "{desc}"
4 project = Project()
5 project.setup(id='200', name="{name}", description=desc)
4 6
5 controller = Project()
6 controller.setup(id='191', name="{name}", description=desc)
7
8 readUnitConf = controller.addReadUnit(datatype='VoltageReader',
7 voltage_reader = project.addReadUnit(datatype='VoltageReader',
9 8 path="{path}",
10 9 startDate="{startDate}",
11 10 endDate="{endDate}",
12 11 startTime="{startHour}",
13 12 endTime="{endHour}",
14 13 online=0,
15 14 verbose=1,
16 15 walk=1,
17 16 )
18 17
19 procUnitConf1 = controller.addProcUnit(datatype='VoltageProc', inputId=readUnitConf.getId())
18 voltage_proc = project.addProcUnit(datatype='VoltageProc', inputId=voltage_reader.getId())
20 19
21 opObj11 = procUnitConf1.addOperation(name='ProfileSelector', optype='other')
22 opObj11.addParameter(name='profileRangeList', value='120,183', format='intlist')
20 profile = voltage_proc.addOperation(name='ProfileSelector', optype='other')
21 profile.addParameter(name='profileRangeList', value='120,183', format='intlist')
23 22
24 opObj11 = procUnitConf1.addOperation(name='RTIPlot', optype='other')
25 opObj11.addParameter(name='wintitle', value='Jicamarca Radio Observatory', format='str')
26 opObj11.addParameter(name='showprofile', value='0', format='int')
27 opObj11.addParameter(name='xmin', value='0', format='int')
28 opObj11.addParameter(name='xmax', value='24', format='int')
29 opObj11.addParameter(name='figpath', value="{figpath}", format='str')
30 opObj11.addParameter(name='wr_period', value='5', format='int')
31 opObj11.addParameter(name='exp_code', value='22', format='int')
23 rti = voltage_proc.addOperation(name='RTIPlot', optype='other')
24 rti.addParameter(name='wintitle', value='Jicamarca Radio Observatory', format='str')
25 rti.addParameter(name='showprofile', value='0', format='int')
26 rti.addParameter(name='xmin', value='0', format='int')
27 rti.addParameter(name='xmax', value='24', format='int')
28 rti.addParameter(name='figpath', value="{figpath}", format='str')
29 rti.addParameter(name='wr_period', value='5', format='int')
30 rti.addParameter(name='exp_code', value='22', format='int')
32 31
33 32
34 33 controller.start()
35 34 '''
36 35
37 multiprocess = '''from schainpy.controller import Project, multiSchain
38
36 multiprocess = '''from schainpy.controller import Project, MPProject
37 from time import sleep
39 38 desc = "{desc}"
40 39
41 def fiber(cursor, skip, q, day):
42 controller = Project()
43 controller.setup(id='191', name="{name}", description=desc)
44
45 readUnitConf = controller.addReadUnit(datatype='SpectraReader',
46 path="{path}",
47 startDate=day,
48 endDate=day,
49 startTime="{startHour}",
50 endTime="{endHour}",
51 online=0,
52 queue=q,
53 cursor=cursor,
54 skip=skip,
55 verbose=1,
56 walk=1,
57 )
58
59 procUnitConf1 = controller.addProcUnit(datatype='Spectra', inputId=readUnitConf.getId())
60
61 procUnitConf2 = controller.addProcUnit(datatype='ParametersProc', inputId=readUnitConf.getId())
62 opObj11 = procUnitConf2.addOperation(name='SpectralMoments', optype='other')
63
64 opObj12 = procUnitConf2.addOperation(name='PublishData', optype='other')
65 opObj12.addParameter(name='zeromq', value=1, format='int')
66 opObj12.addParameter(name='verbose', value=0, format='bool')
67
68 controller.start()
69
70
71 if __name__ == '__main__':
72 multiSchain(fiber, nProcess={nProcess}, startDate="{startDate}", endDate="{endDate}")
40 ####################
41 # PLOTTER RECEIVER #
42 ####################
43 plotter = Project()
44 plotter.setup(id='100', name='receiver', description=desc)
45
46 receiver_plot = plotter.addProcUnit(name='PlotterReceiver')
47 receiver_plot.addParameter(name='throttle', value=20, format='int')
48 receiver_plot.addParameter(name='plottypes', value='rti', format='str')
49
50 rti = receiver_plot.addOperation(name='PlotRTIData', optype='other')
51 rti.addParameter(name='zmin', value='-40.0', format='float')
52 rti.addParameter(name='zmax', value='100.0', format='float')
53 rti.addParameter(name='decimation', value='200', format='int')
54 rti.addParameter(name='xmin', value='0.0', format='int')
55 rti.addParameter(name='colormap', value='jet', format='str')
56
57 plotter.start()
58
59 sleep(2)
60
61 ################
62 # DATA EMITTER #
63 ################
64 project = Project()
65 project.setup(id='200', name="{name}", description=desc)
66
67 spectra_reader = project.addReadUnit(datatype='SpectraReader',
68 path="{path}",
69 startDate={startDate},
70 endDate={endDate},
71 startTime="{startHour}",
72 endTime="{endHour}",
73 online=0,
74 verbose=1,
75 walk=1,
76 )
77
78 spectra_proc = project.addProcUnit(datatype='Spectra', inputId=spectra_reader.getId())
79
80 parameters_proc = project.addProcUnit(datatype='ParametersProc', inputId=spectra_proc.getId())
81 moments = parameters_proc.addOperation(name='SpectralMoments', optype='other')
82
83 publish = parameters_proc.addOperation(name='PublishData', optype='other')
84 publish.addParameter(name='zeromq', value=1, format='int')
85 publish.addParameter(name='verbose', value=0, format='bool')
86
87 MPProject(project, 16)
73 88
74 89
75 90 '''
@@ -1,109 +1,109
1 1 ## CHANGELOG:
2 2
3 3 ### 2.3
4 * Added high order function `multiSchain` for multiprocessing scripts.
4 * Added high order function `MPProject` for multiprocessing scripts.
5 5 * Added two new Processing Units `PublishData` and `ReceiverData` for receiving and sending dataOut through multiple ways (tcp, ipc, inproc).
6 6 * Added a new graphics Processing Unit `PlotterReceiver`. It is decoupled from normal processing sequence with support for data generated by multiprocessing scripts.
7 7 * Added support for sending realtime graphic to web server.
8 * GUI command `schain` is now `schainGUI`.
8 * GUI command `schain` is now `schainGUI`.
9 9 * Added a CLI tool named `schain`.
10 10 * Scripts templates can be now generated with `schain generate`.
11 11 * Now it is possible to search Processing Units and Operations with `schain search [module]` to get the right name and its allowed parameters.
12 12 * `schain xml` to run xml scripts.
13 13 * Added suggestions when parameters are poorly written.
14 14 * `Controller.start()` now runs in a different process than the process calling it.
15 15 * Added `schainpy.utils.log` for log standarization.
16 16 * Running script on online mode no longer ignores date and hour. Issue #1109.
17 17 * Added support for receving voltage data directly from JARS (tcp, ipc).
18 18 * Updated README for MAC OS GUI installation.
19 19 * Setup now installs numpy.
20 20
21 21 ### 2.2.6
22 22 * Graphics generated by the GUI are now the same as generated by scripts. Issue #1074.
23 23 * Added support for C extensions.
24 * function `hildebrand_sehkon` optimized with a C wrapper.
24 * Function `hildebrand_sehkon` optimized with a C wrapper.
25 25 * Numpy version updated.
26 26 * Migration to GIT.
27 27
28 28 ### 2.2.5:
29 29 * splitProfiles and combineProfiles modules were added to VoltageProc and Signal Chain GUI.
30 30 * nProfiles of USRP data (hdf5) is the number of profiles thera are in one second.
31 31 * jroPlotter works directly with data objects instead of dictionaries
32 32 * script "schain" was added to Signal Chain installer
33 33
34 34 ### 2.2.4.1:
35 35 * jroIO_usrp.py is update to read Sandra's data
36 36 * decimation in Spectra and RTI plots is always enabled.
37 37 * time* window option added to GUI
38 38
39 39 ### 2.2.4:
40 40 * jroproc_spectra_lags.py added to schainpy
41 41 * Bug fixed in schainGUI: ProcUnit was created with the same id in some cases.
42 42 * Bug fixed in jroHeaderIO: Header size validation.
43 43
44 44 ### 2.2.3.1:
45 45 * Filtering block by time has been added.
46 46 * Bug fixed plotting RTI, CoherenceMap and others using xmin and xmax parameters. The first day worked
47 47 properly but the next days did not.
48 48
49 49 ### 2.2.3:
50 50 * Bug fixed in GUI: Error getting(reading) Code value
51 51 * Bug fixed in GUI: Flip option always needs channelList field
52 52 * Bug fixed in jrodata: when one branch modified a value in "dataOut" (example: dataOut.code) this value
53 53 was modified for every branch (because this was a reference). It was modified in data.copy()
54 54 * Bug fixed in jroproc_voltage.profileSelector(): rangeList replaces to profileRangeList.
55 55
56 56 ### 2.2.2:
57 57 * VoltageProc: ProfileSelector, Reshape, Decoder with nTxs!=1 and getblock=True was tested
58 58 * Rawdata and testRawdata.py added to Signal Chain project
59 59
60 60 ### 2.2.1:
61 61 * Bugs fixed in GUI
62 62 * Views were improved in GUI
63 63 * Support to MST* ISR experiments
64 64 * Bug fixed getting noise using hyldebrant. (minimum number of points > 20%)
65 65 * handleError added to jroplotter.py
66 66
67 67 ### 2.2.0:
68 68 * GUI: use of external plotter
69 69 * Compatible with matplotlib 1.5.0
70 70
71 71 ### 2.1.5:
72 72 * serializer module added to Signal Chain
73 73 * jroplotter.py added to Signal Chain
74 74
75 75 ### 2.1.4.2:
76 76 * A new Plotter Class was added
77 77 * Project.start() does not accept filename as a parameter anymore
78 78
79 79 ### 2.1.4.1:
80 80 * Send notifications when an error different to ValueError is detected
81 81
82 82 ### 2.1.4:
83 83 * Sending error notifications to signal chain administrator
84 84 * Login to email server added
85 85
86 86 ### 2.1.3.3:
87 87 * Colored Button Icons were added to GUI
88 88
89 89 ### 2.1.3.2:
90 90 * GUI: user interaction enhanced
91 91 * controller_api.py: Safe access to ControllerThead
92 92
93 93 ### 2.1.3.1:
94 94 * GUI: every icon were resized
95 95 * jroproc_voltage.py: Print a message when "Read from code" option is selected and the code is not defined inside data file
96 96
97 97 ### 2.1.3:
98 98 * jroplot_heispectra.py: SpectraHeisScope was not showing the right channels
99 99 * jroproc_voltage.py: Bug fixed selecting profiles (self.nProfiles took a wrong value),
100 100 Bug fixed selecting heights by block (selecting profiles instead heights)
101 101 * jroproc_voltage.py: New feature added: decoding data by block using FFT.
102 102 * jroIO_heispectra.py: Bug fixed in FitsReader. Using local Fits instance instead schainpy.mode.data.jrodata.Fits.
103 103 * jroIO_heispectra.py: Channel index list does not exist.
104 104
105 105 ### 2.1.2:
106 106 * jroutils_ftp.py: Bug fixed, Any error sending file stopped the Server Thread
107 107 Server thread opens and closes remote server each time file list is sent
108 108 * jroplot_spectra.py: Noise path was not being created when noise data is saved.
109 109 * jroIO_base.py: startTime can be greater than endTime. Example: SpreadF [18:00 * 07:00] No newline at end of file
@@ -1,1302 +1,1317
1 1 '''
2 2 Created on September , 2012
3 3 @author:
4 4 '''
5 5
6 6 import sys
7 7 import ast
8 8 import datetime
9 9 import traceback
10 10 import math
11 11 import time
12 12 from multiprocessing import Process, cpu_count
13 13
14 14 from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring
15 15 from xml.dom import minidom
16 16
17 17 import schainpy
18 18 import schainpy.admin
19 19 from schainpy.model import *
20 20 from schainpy.utils import log
21 21
22 22 DTYPES = {
23 23 'Voltage': '.r',
24 24 'Spectra': '.pdata'
25 25 }
26 26
27
27 28 def MPProject(project, n=cpu_count()):
28 29 '''
29 30 Project wrapper to run schain in n processes
30 31 '''
31 32
32 33 rconf = project.getReadUnitObj()
33 34 op = rconf.getOperationObj('run')
34 35 dt1 = op.getParameterValue('startDate')
35 36 dt2 = op.getParameterValue('endDate')
36 37 days = (dt2 - dt1).days
37
38 for day in range(days+1):
38
39 for day in range(days + 1):
39 40 skip = 0
40 41 cursor = 0
41 42 processes = []
42 43 dt = dt1 + datetime.timedelta(day)
43 44 dt_str = dt.strftime('%Y/%m/%d')
44 45 reader = JRODataReader()
45 46 paths, files = reader.searchFilesOffLine(path=rconf.path,
46 startDate=dt,
47 endDate=dt,
48 ext=DTYPES[rconf.datatype])
47 startDate=dt,
48 endDate=dt,
49 ext=DTYPES[rconf.datatype])
49 50 nFiles = len(files)
50 51 if nFiles == 0:
51 52 continue
52 skip = int(math.ceil(nFiles/n))
53 while nFiles > cursor*skip:
54 rconf.update(startDate=dt_str, endDate=dt_str, cursor=cursor,
55 skip=skip)
56 p = project.clone()
53 skip = int(math.ceil(nFiles / n))
54 while nFiles > cursor * skip:
55 rconf.update(startDate=dt_str, endDate=dt_str, cursor=cursor,
56 skip=skip)
57 p = project.clone()
57 58 p.start()
58 59 processes.append(p)
59 60 cursor += 1
60 61
61 62 def beforeExit(exctype, value, trace):
62 63 for process in processes:
63 64 process.terminate()
64 65 process.join()
65 66 print traceback.print_tb(trace)
66 67
67 68 sys.excepthook = beforeExit
68 69
69 70 for process in processes:
70 71 process.join()
71 72 process.terminate()
72 73
73 74 time.sleep(3)
74 75
76
75 77 class ParameterConf():
76 78
77 79 id = None
78 80 name = None
79 81 value = None
80 82 format = None
81 83
82 84 __formated_value = None
83 85
84 86 ELEMENTNAME = 'Parameter'
85 87
86 88 def __init__(self):
87 89
88 90 self.format = 'str'
89 91
90 92 def getElementName(self):
91 93
92 94 return self.ELEMENTNAME
93 95
94 96 def getValue(self):
95 97
96 98 value = self.value
97 99 format = self.format
98 100
99 101 if self.__formated_value != None:
100 102
101 103 return self.__formated_value
102 104
103 105 if format == 'obj':
104 106 return value
105 107
106 108 if format == 'str':
107 109 self.__formated_value = str(value)
108 110 return self.__formated_value
109 111
110 112 if value == '':
111 raise ValueError, '%s: This parameter value is empty' %self.name
113 raise ValueError, '%s: This parameter value is empty' % self.name
112 114
113 115 if format == 'list':
114 116 strList = value.split(',')
115 117
116 118 self.__formated_value = strList
117 119
118 120 return self.__formated_value
119 121
120 122 if format == 'intlist':
121 123 '''
122 124 Example:
123 125 value = (0,1,2)
124 126 '''
125 127
126 128 new_value = ast.literal_eval(value)
127 129
128 130 if type(new_value) not in (tuple, list):
129 131 new_value = [int(new_value)]
130 132
131 133 self.__formated_value = new_value
132 134
133 135 return self.__formated_value
134 136
135 137 if format == 'floatlist':
136 138 '''
137 139 Example:
138 140 value = (0.5, 1.4, 2.7)
139 141 '''
140 142
141 143 new_value = ast.literal_eval(value)
142 144
143 145 if type(new_value) not in (tuple, list):
144 146 new_value = [float(new_value)]
145 147
146 148 self.__formated_value = new_value
147 149
148 150 return self.__formated_value
149 151
150 152 if format == 'date':
151 153 strList = value.split('/')
152 154 intList = [int(x) for x in strList]
153 155 date = datetime.date(intList[0], intList[1], intList[2])
154 156
155 157 self.__formated_value = date
156 158
157 159 return self.__formated_value
158 160
159 161 if format == 'time':
160 162 strList = value.split(':')
161 163 intList = [int(x) for x in strList]
162 164 time = datetime.time(intList[0], intList[1], intList[2])
163 165
164 166 self.__formated_value = time
165 167
166 168 return self.__formated_value
167 169
168 170 if format == 'pairslist':
169 171 '''
170 172 Example:
171 173 value = (0,1),(1,2)
172 174 '''
173 175
174 176 new_value = ast.literal_eval(value)
175 177
176 178 if type(new_value) not in (tuple, list):
177 raise ValueError, '%s has to be a tuple or list of pairs' %value
179 raise ValueError, '%s has to be a tuple or list of pairs' % value
178 180
179 181 if type(new_value[0]) not in (tuple, list):
180 182 if len(new_value) != 2:
181 raise ValueError, '%s has to be a tuple or list of pairs' %value
183 raise ValueError, '%s has to be a tuple or list of pairs' % value
182 184 new_value = [new_value]
183 185
184 186 for thisPair in new_value:
185 187 if len(thisPair) != 2:
186 raise ValueError, '%s has to be a tuple or list of pairs' %value
188 raise ValueError, '%s has to be a tuple or list of pairs' % value
187 189
188 190 self.__formated_value = new_value
189 191
190 192 return self.__formated_value
191 193
192 194 if format == 'multilist':
193 195 '''
194 196 Example:
195 197 value = (0,1,2),(3,4,5)
196 198 '''
197 199 multiList = ast.literal_eval(value)
198 200
199 201 if type(multiList[0]) == int:
200 202 multiList = ast.literal_eval('(' + value + ')')
201 203
202 204 self.__formated_value = multiList
203 205
204 206 return self.__formated_value
205 207
206 208 if format == 'bool':
207 209 value = int(value)
208 210
209 211 if format == 'int':
210 212 value = float(value)
211 213
212 214 format_func = eval(format)
213 215
214 216 self.__formated_value = format_func(value)
215 217
216 218 return self.__formated_value
217 219
218 220 def updateId(self, new_id):
219 221
220 222 self.id = str(new_id)
221 223
222 224 def setup(self, id, name, value, format='str'):
223 225 self.id = str(id)
224 226 self.name = name
225 227 if format == 'obj':
226 228 self.value = value
227 229 else:
228 230 self.value = str(value)
229 231 self.format = str.lower(format)
230 232
231 233 self.getValue()
232 234
233 235 return 1
234 236
235 237 def update(self, name, value, format='str'):
236 238
237 239 self.name = name
238 240 self.value = str(value)
239 241 self.format = format
240 242
241 243 def makeXml(self, opElement):
242 244 if self.name not in ('queue',):
243 245 parmElement = SubElement(opElement, self.ELEMENTNAME)
244 246 parmElement.set('id', str(self.id))
245 247 parmElement.set('name', self.name)
246 248 parmElement.set('value', self.value)
247 249 parmElement.set('format', self.format)
248 250
249 251 def readXml(self, parmElement):
250 252
251 253 self.id = parmElement.get('id')
252 254 self.name = parmElement.get('name')
253 255 self.value = parmElement.get('value')
254 256 self.format = str.lower(parmElement.get('format'))
255 257
256 #Compatible with old signal chain version
258 # Compatible with old signal chain version
257 259 if self.format == 'int' and self.name == 'idfigure':
258 260 self.name = 'id'
259 261
260 262 def printattr(self):
261 263
262 print 'Parameter[%s]: name = %s, value = %s, format = %s' %(self.id, self.name, self.value, self.format)
264 print 'Parameter[%s]: name = %s, value = %s, format = %s' % (self.id, self.name, self.value, self.format)
265
263 266
264 267 class OperationConf():
265 268
266 269 id = None
267 270 name = None
268 271 priority = None
269 272 type = None
270 273
271 274 parmConfObjList = []
272 275
273 276 ELEMENTNAME = 'Operation'
274 277
275 278 def __init__(self):
276 279
277 280 self.id = '0'
278 281 self.name = None
279 282 self.priority = None
280 283 self.type = 'self'
281 284
282
283 285 def __getNewId(self):
284 286
285 return int(self.id)*10 + len(self.parmConfObjList) + 1
287 return int(self.id) * 10 + len(self.parmConfObjList) + 1
286 288
287 289 def updateId(self, new_id):
288 290
289 291 self.id = str(new_id)
290 292
291 293 n = 1
292 294 for parmObj in self.parmConfObjList:
293 295
294 idParm = str(int(new_id)*10 + n)
296 idParm = str(int(new_id) * 10 + n)
295 297 parmObj.updateId(idParm)
296 298
297 299 n += 1
298 300
299 301 def getElementName(self):
300 302
301 303 return self.ELEMENTNAME
302 304
303 305 def getParameterObjList(self):
304 306
305 307 return self.parmConfObjList
306 308
307 309 def getParameterObj(self, parameterName):
308 310
309 311 for parmConfObj in self.parmConfObjList:
310 312
311 313 if parmConfObj.name != parameterName:
312 314 continue
313 315
314 316 return parmConfObj
315 317
316 318 return None
317 319
318 320 def getParameterObjfromValue(self, parameterValue):
319 321
320 322 for parmConfObj in self.parmConfObjList:
321 323
322 324 if parmConfObj.getValue() != parameterValue:
323 325 continue
324 326
325 327 return parmConfObj.getValue()
326 328
327 329 return None
328 330
329 331 def getParameterValue(self, parameterName):
330 332
331 333 parameterObj = self.getParameterObj(parameterName)
332
334
333 335 # if not parameterObj:
334 336 # return None
335
337
336 338 value = parameterObj.getValue()
337 339
338 340 return value
339 341
340
341 342 def getKwargs(self):
342 343
343 344 kwargs = {}
344 345
345 346 for parmConfObj in self.parmConfObjList:
346 347 if self.name == 'run' and parmConfObj.name == 'datatype':
347 348 continue
348 349
349 350 kwargs[parmConfObj.name] = parmConfObj.getValue()
350 351
351 352 return kwargs
352 353
353 354 def setup(self, id, name, priority, type):
354 355
355 356 self.id = str(id)
356 357 self.name = name
357 358 self.type = type
358 359 self.priority = priority
359 360
360 361 self.parmConfObjList = []
361 362
362 363 def removeParameters(self):
363 364
364 365 for obj in self.parmConfObjList:
365 366 del obj
366 367
367 368 self.parmConfObjList = []
368 369
369 370 def addParameter(self, name, value, format='str'):
370
371
371 372 if value is None:
372 373 return None
373 374 id = self.__getNewId()
374 375
375 376 parmConfObj = ParameterConf()
376 377 if not parmConfObj.setup(id, name, value, format):
377 378 return None
378 379
379 380 self.parmConfObjList.append(parmConfObj)
380 381
381 382 return parmConfObj
382 383
383 384 def changeParameter(self, name, value, format='str'):
384 385
385 386 parmConfObj = self.getParameterObj(name)
386 387 parmConfObj.update(name, value, format)
387 388
388 389 return parmConfObj
389 390
390 391 def makeXml(self, procUnitElement):
391 392
392 393 opElement = SubElement(procUnitElement, self.ELEMENTNAME)
393 394 opElement.set('id', str(self.id))
394 395 opElement.set('name', self.name)
395 396 opElement.set('type', self.type)
396 397 opElement.set('priority', str(self.priority))
397 398
398 399 for parmConfObj in self.parmConfObjList:
399 400 parmConfObj.makeXml(opElement)
400 401
401 402 def readXml(self, opElement):
402 403
403 404 self.id = opElement.get('id')
404 405 self.name = opElement.get('name')
405 406 self.type = opElement.get('type')
406 407 self.priority = opElement.get('priority')
407 408
408 #Compatible with old signal chain version
409 #Use of 'run' method instead 'init'
409 # Compatible with old signal chain version
410 # Use of 'run' method instead 'init'
410 411 if self.type == 'self' and self.name == 'init':
411 412 self.name = 'run'
412 413
413 414 self.parmConfObjList = []
414 415
415 416 parmElementList = opElement.iter(ParameterConf().getElementName())
416 417
417 418 for parmElement in parmElementList:
418 419 parmConfObj = ParameterConf()
419 420 parmConfObj.readXml(parmElement)
420 421
421 #Compatible with old signal chain version
422 #If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER
422 # Compatible with old signal chain version
423 # If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER
423 424 if self.type != 'self' and self.name == 'Plot':
424 425 if parmConfObj.format == 'str' and parmConfObj.name == 'type':
425 426 self.name = parmConfObj.value
426 427 continue
427 428
428 429 self.parmConfObjList.append(parmConfObj)
429 430
430 431 def printattr(self):
431 432
432 print '%s[%s]: name = %s, type = %s, priority = %s' %(self.ELEMENTNAME,
433 self.id,
434 self.name,
435 self.type,
436 self.priority)
433 print '%s[%s]: name = %s, type = %s, priority = %s' % (self.ELEMENTNAME,
434 self.id,
435 self.name,
436 self.type,
437 self.priority)
437 438
438 439 for parmConfObj in self.parmConfObjList:
439 440 parmConfObj.printattr()
440 441
441 442 def createObject(self, plotter_queue=None):
442 443
443
444 444 if self.type == 'self':
445 445 raise ValueError, 'This operation type cannot be created'
446 446
447 if self.type == 'plotter':
447 if self.type == 'plotter':
448 448 if not plotter_queue:
449 449 raise ValueError, 'plotter_queue is not defined. Use:\nmyProject = Project()\nmyProject.setPlotterQueue(plotter_queue)'
450 450
451 451 opObj = Plotter(self.name, plotter_queue)
452 452
453 453 if self.type == 'external' or self.type == 'other':
454 454
455 455 className = eval(self.name)
456 456 kwargs = self.getKwargs()
457 457
458 458 opObj = className(**kwargs)
459 459
460 460 return opObj
461 461
462 462
463 463 class ProcUnitConf():
464 464
465 465 id = None
466 466 name = None
467 467 datatype = None
468 468 inputId = None
469 469 parentId = None
470 470
471 471 opConfObjList = []
472 472
473 473 procUnitObj = None
474 474 opObjList = []
475 475
476 476 ELEMENTNAME = 'ProcUnit'
477 477
478 478 def __init__(self):
479 479
480 480 self.id = None
481 481 self.datatype = None
482 482 self.name = None
483 483 self.inputId = None
484 484
485 485 self.opConfObjList = []
486 486
487 487 self.procUnitObj = None
488 488 self.opObjDict = {}
489 489
490 490 def __getPriority(self):
491 491
492 return len(self.opConfObjList)+1
492 return len(self.opConfObjList) + 1
493 493
494 494 def __getNewId(self):
495 495
496 return int(self.id)*10 + len(self.opConfObjList) + 1
496 return int(self.id) * 10 + len(self.opConfObjList) + 1
497 497
498 498 def getElementName(self):
499 499
500 500 return self.ELEMENTNAME
501 501
502 502 def getId(self):
503 503
504 504 return self.id
505 505
506 506 def updateId(self, new_id, parentId=parentId):
507 507
508 new_id = int(parentId) * 10 + (int(self.id) % 10)
509 new_inputId = int(parentId) * 10 + (int(self.inputId) % 10)
508 510
509 new_id = int(parentId)*10 + (int(self.id) % 10)
510 new_inputId = int(parentId)*10 + (int(self.inputId) % 10)
511
512 #If this proc unit has not inputs
511 # If this proc unit has not inputs
513 512 if self.inputId == '0':
514 513 new_inputId = 0
515 514
516 515 n = 1
517 516 for opConfObj in self.opConfObjList:
518 517
519 idOp = str(int(new_id)*10 + n)
518 idOp = str(int(new_id) * 10 + n)
520 519 opConfObj.updateId(idOp)
521 520
522 521 n += 1
523 522
524 523 self.parentId = str(parentId)
525 524 self.id = str(new_id)
526 525 self.inputId = str(new_inputId)
527 526
528
529 527 def getInputId(self):
530 528
531 529 return self.inputId
532 530
533 531 def getOperationObjList(self):
534 532
535 533 return self.opConfObjList
536 534
537 535 def getOperationObj(self, name=None):
538 536
539 537 for opConfObj in self.opConfObjList:
540 538
541 539 if opConfObj.name != name:
542 540 continue
543 541
544 542 return opConfObj
545 543
546 544 return None
547 545
548 546 def getOpObjfromParamValue(self, value=None):
549 547
550 548 for opConfObj in self.opConfObjList:
551 549 if opConfObj.getParameterObjfromValue(parameterValue=value) != value:
552 550 continue
553 551 return opConfObj
554 552 return None
555 553
556 554 def getProcUnitObj(self):
557 555
558 556 return self.procUnitObj
559 557
560 558 def setup(self, id, name, datatype, inputId, parentId=None):
561 559
562 #Compatible with old signal chain version
563 if datatype==None and name==None:
560 # Compatible with old signal chain version
561 if datatype == None and name == None:
564 562 raise ValueError, 'datatype or name should be defined'
565 563
566 if name==None:
564 if name == None:
567 565 if 'Proc' in datatype:
568 566 name = datatype
569 567 else:
570 name = '%sProc' %(datatype)
568 name = '%sProc' % (datatype)
571 569
572 if datatype==None:
573 datatype = name.replace('Proc','')
570 if datatype == None:
571 datatype = name.replace('Proc', '')
574 572
575 573 self.id = str(id)
576 574 self.name = name
577 575 self.datatype = datatype
578 576 self.inputId = inputId
579 577 self.parentId = parentId
580 578
581 579 self.opConfObjList = []
582 580
583 581 self.addOperation(name='run', optype='self')
584 582
585 583 def removeOperations(self):
586 584
587 585 for obj in self.opConfObjList:
588 586 del obj
589 587
590 588 self.opConfObjList = []
591 589 self.addOperation(name='run')
592 590
593 591 def addParameter(self, **kwargs):
594 592 '''
595 593 Add parameters to 'run' operation
596 594 '''
597 595 opObj = self.opConfObjList[0]
598 596
599 597 opObj.addParameter(**kwargs)
600 598
601 599 return opObj
602 600
603 601 def addOperation(self, name, optype='self'):
604 602
605 603 id = self.__getNewId()
606 604 priority = self.__getPriority()
607 605
608 606 opConfObj = OperationConf()
609 607 opConfObj.setup(id, name=name, priority=priority, type=optype)
610 608
611 609 self.opConfObjList.append(opConfObj)
612 610
613 611 return opConfObj
614 612
615 613 def makeXml(self, projectElement):
616 614
617 615 procUnitElement = SubElement(projectElement, self.ELEMENTNAME)
618 616 procUnitElement.set('id', str(self.id))
619 617 procUnitElement.set('name', self.name)
620 618 procUnitElement.set('datatype', self.datatype)
621 619 procUnitElement.set('inputId', str(self.inputId))
622 620
623 621 for opConfObj in self.opConfObjList:
624 622 opConfObj.makeXml(procUnitElement)
625 623
626 624 def readXml(self, upElement):
627 625
628 626 self.id = upElement.get('id')
629 627 self.name = upElement.get('name')
630 628 self.datatype = upElement.get('datatype')
631 629 self.inputId = upElement.get('inputId')
632 630
633 631 if self.ELEMENTNAME == 'ReadUnit':
634 632 self.datatype = self.datatype.replace('Reader', '')
635 633
636 634 if self.ELEMENTNAME == 'ProcUnit':
637 635 self.datatype = self.datatype.replace('Proc', '')
638 636
639 637 if self.inputId == 'None':
640 638 self.inputId = '0'
641 639
642 640 self.opConfObjList = []
643 641
644 642 opElementList = upElement.iter(OperationConf().getElementName())
645 643
646 644 for opElement in opElementList:
647 645 opConfObj = OperationConf()
648 646 opConfObj.readXml(opElement)
649 647 self.opConfObjList.append(opConfObj)
650 648
651 649 def printattr(self):
652 650
653 print '%s[%s]: name = %s, datatype = %s, inputId = %s' %(self.ELEMENTNAME,
654 self.id,
655 self.name,
656 self.datatype,
657 self.inputId)
658
651 print '%s[%s]: name = %s, datatype = %s, inputId = %s' % (self.ELEMENTNAME,
652 self.id,
653 self.name,
654 self.datatype,
655 self.inputId)
656
659 657 for opConfObj in self.opConfObjList:
660 658 opConfObj.printattr()
661 659
662
663 660 def getKwargs(self):
664 661
665 662 opObj = self.opConfObjList[0]
666 663 kwargs = opObj.getKwargs()
667 664
668 665 return kwargs
669 666
670 667 def createObjects(self, plotter_queue=None):
671 668
672 669 className = eval(self.name)
673 670 kwargs = self.getKwargs()
674 671 procUnitObj = className(**kwargs)
675 672
676 673 for opConfObj in self.opConfObjList:
677 674
678 if opConfObj.type=='self' and self.name=='run':
675 if opConfObj.type == 'self' and self.name == 'run':
679 676 continue
680 elif opConfObj.type=='self':
681 procUnitObj.addOperationKwargs(opConfObj.id, **opConfObj.getKwargs())
677 elif opConfObj.type == 'self':
678 procUnitObj.addOperationKwargs(
679 opConfObj.id, **opConfObj.getKwargs())
682 680 continue
683 681
684 682 opObj = opConfObj.createObject(plotter_queue)
685 683
686 684 self.opObjDict[opConfObj.id] = opObj
687 685
688 686 procUnitObj.addOperation(opObj, opConfObj.id)
689 687
690 688 self.procUnitObj = procUnitObj
691 689
692 690 return procUnitObj
693 691
694 692 def run(self):
695 693
696 694 is_ok = False
697 695
698 696 for opConfObj in self.opConfObjList:
699 697
700 698 kwargs = {}
701 699 for parmConfObj in opConfObj.getParameterObjList():
702 700 if opConfObj.name == 'run' and parmConfObj.name == 'datatype':
703 701 continue
704 702
705 703 kwargs[parmConfObj.name] = parmConfObj.getValue()
706 704
707 sts = self.procUnitObj.call(opType = opConfObj.type,
708 opName = opConfObj.name,
709 opId = opConfObj.id)
710
705 sts = self.procUnitObj.call(opType=opConfObj.type,
706 opName=opConfObj.name,
707 opId=opConfObj.id)
708
711 709 is_ok = is_ok or sts
712 710
713 711 return is_ok
714 712
715 713 def close(self):
716 714
717 715 for opConfObj in self.opConfObjList:
718 716 if opConfObj.type == 'self':
719 717 continue
720 718
721 719 opObj = self.procUnitObj.getOperationObj(opConfObj.id)
722 720 opObj.close()
723 721
724 722 self.procUnitObj.close()
725 723
726 724 return
727 725
726
728 727 class ReadUnitConf(ProcUnitConf):
729 728
730 729 path = None
731 730 startDate = None
732 731 endDate = None
733 732 startTime = None
734 733 endTime = None
735 734
736 735 ELEMENTNAME = 'ReadUnit'
737 736
738 737 def __init__(self):
739 738
740 739 self.id = None
741 740 self.datatype = None
742 741 self.name = None
743 742 self.inputId = None
744 743
745 744 self.parentId = None
746 745
747 746 self.opConfObjList = []
748 747 self.opObjList = []
749 748
750 749 def getElementName(self):
751 750
752 751 return self.ELEMENTNAME
753 752
754 753 def setup(self, id, name, datatype, path='', startDate='', endDate='',
755 754 startTime='', endTime='', parentId=None, server=None, **kwargs):
756 755
757 #Compatible with old signal chain version
758 if datatype==None and name==None:
756 # Compatible with old signal chain version
757 if datatype == None and name == None:
759 758 raise ValueError, 'datatype or name should be defined'
760
761 759 if name == None:
762 760 if 'Reader' in datatype:
763 761 name = datatype
764 762 datatype = name.replace('Reader','')
765 763 else:
766 764 name = '{}Reader'.format(datatype)
767 765 if datatype == None:
768 766 if 'Reader' in name:
769 767 datatype = name.replace('Reader','')
770 768 else:
771 769 datatype = name
772 770 name = '{}Reader'.format(name)
773 771
774 772 self.id = id
775 773 self.name = name
776 774 self.datatype = datatype
777 775 if path != '':
778 776 self.path = os.path.abspath(path)
779 777 self.startDate = startDate
780 778 self.endDate = endDate
781 779 self.startTime = startTime
782 780 self.endTime = endTime
783 781 self.inputId = '0'
784 782 self.parentId = parentId
785 783 self.server = server
786 784 self.addRunOperation(**kwargs)
787 785
788 786 def update(self, **kwargs):
789 787
790 788 if 'datatype' in kwargs:
791 789 datatype = kwargs.pop('datatype')
792 790 if 'Reader' in datatype:
793 791 self.name = datatype
794 792 else:
795 self.name = '%sReader' %(datatype)
793 self.name = '%sReader' % (datatype)
796 794 self.datatype = self.name.replace('Reader', '')
797 795
798 attrs = ('path', 'startDate', 'endDate', 'startTime', 'endTime', 'parentId')
799
796 attrs = ('path', 'startDate', 'endDate',
797 'startTime', 'endTime', 'parentId')
798
800 799 for attr in attrs:
801 800 if attr in kwargs:
802 801 setattr(self, attr, kwargs.pop(attr))
803
802
804 803 self.inputId = '0'
805 804 self.updateRunOperation(**kwargs)
806 805
807 806 def removeOperations(self):
808 807
809 808 for obj in self.opConfObjList:
810 809 del obj
811 810
812 811 self.opConfObjList = []
813 812
814 813 def addRunOperation(self, **kwargs):
815 814
816 opObj = self.addOperation(name = 'run', optype = 'self')
815 opObj = self.addOperation(name='run', optype='self')
817 816
818 817 if self.server is None:
819 opObj.addParameter(name='datatype', value=self.datatype, format='str')
818 opObj.addParameter(
819 name='datatype', value=self.datatype, format='str')
820 820 opObj.addParameter(name='path', value=self.path, format='str')
821 opObj.addParameter(name='startDate', value=self.startDate, format='date')
822 opObj.addParameter(name='endDate', value=self.endDate, format='date')
823 opObj.addParameter(name='startTime', value=self.startTime, format='time')
824 opObj.addParameter(name='endTime', value=self.endTime, format='time')
825
821 opObj.addParameter(
822 name='startDate', value=self.startDate, format='date')
823 opObj.addParameter(
824 name='endDate', value=self.endDate, format='date')
825 opObj.addParameter(
826 name='startTime', value=self.startTime, format='time')
827 opObj.addParameter(
828 name='endTime', value=self.endTime, format='time')
829
826 830 for key, value in kwargs.items():
827 opObj.addParameter(name=key, value=value, format=type(value).__name__)
831 opObj.addParameter(name=key, value=value,
832 format=type(value).__name__)
828 833 else:
829 opObj.addParameter(name='server' , value=self.server, format='str')
830
834 opObj.addParameter(name='server', value=self.server, format='str')
831 835
832 836 return opObj
833 837
834 838 def updateRunOperation(self, **kwargs):
835 839
836 840 opObj = self.getOperationObj(name='run')
837 841 opObj.removeParameters()
838 842
839 843 opObj.addParameter(name='datatype', value=self.datatype, format='str')
840 844 opObj.addParameter(name='path', value=self.path, format='str')
841 opObj.addParameter(name='startDate', value=self.startDate, format='date')
845 opObj.addParameter(
846 name='startDate', value=self.startDate, format='date')
842 847 opObj.addParameter(name='endDate', value=self.endDate, format='date')
843 opObj.addParameter(name='startTime', value=self.startTime, format='time')
848 opObj.addParameter(
849 name='startTime', value=self.startTime, format='time')
844 850 opObj.addParameter(name='endTime', value=self.endTime, format='time')
845
851
846 852 for key, value in kwargs.items():
847 opObj.addParameter(name=key, value=value, format=type(value).__name__)
853 opObj.addParameter(name=key, value=value,
854 format=type(value).__name__)
848 855
849 856 return opObj
850
857
851 858 def readXml(self, upElement):
852 859
853 860 self.id = upElement.get('id')
854 861 self.name = upElement.get('name')
855 862 self.datatype = upElement.get('datatype')
856 863 self.inputId = upElement.get('inputId')
857 864
858 865 if self.ELEMENTNAME == 'ReadUnit':
859 866 self.datatype = self.datatype.replace('Reader', '')
860 867
861 868 if self.inputId == 'None':
862 869 self.inputId = '0'
863 870
864 871 self.opConfObjList = []
865 872
866 873 opElementList = upElement.iter(OperationConf().getElementName())
867 874
868 875 for opElement in opElementList:
869 876 opConfObj = OperationConf()
870 877 opConfObj.readXml(opElement)
871 878 self.opConfObjList.append(opConfObj)
872 879
873 880 if opConfObj.name == 'run':
874 881 self.path = opConfObj.getParameterValue('path')
875 882 self.startDate = opConfObj.getParameterValue('startDate')
876 883 self.endDate = opConfObj.getParameterValue('endDate')
877 884 self.startTime = opConfObj.getParameterValue('startTime')
878 885 self.endTime = opConfObj.getParameterValue('endTime')
879 886
887
880 888 class Project(Process):
881 889
882 890 id = None
883 891 # name = None
884 892 description = None
885 893 filename = None
886 894
887 895 procUnitConfObjDict = None
888 896
889 897 ELEMENTNAME = 'Project'
890 898
891 899 plotterQueue = None
892 900
893 901 def __init__(self, plotter_queue=None):
894 902
895 903 Process.__init__(self)
896 904 self.id = None
897 905 # self.name = None
898 906 self.description = None
899 907
900 908 self.plotterQueue = plotter_queue
901 909
902 910 self.procUnitConfObjDict = {}
903 911
904 912 def __getNewId(self):
905 913
906 914 idList = self.procUnitConfObjDict.keys()
907 915
908 id = int(self.id)*10
916 id = int(self.id) * 10
909 917
910 918 while True:
911 919 id += 1
912 920
913 921 if str(id) in idList:
914 922 continue
915 923
916 924 break
917 925
918 926 return str(id)
919 927
920 928 def getElementName(self):
921 929
922 930 return self.ELEMENTNAME
923 931
924 932 def getId(self):
925 933
926 934 return self.id
927 935
928 936 def updateId(self, new_id):
929 937
930 938 self.id = str(new_id)
931 939
932 940 keyList = self.procUnitConfObjDict.keys()
933 941 keyList.sort()
934 942
935 943 n = 1
936 944 newProcUnitConfObjDict = {}
937 945
938 946 for procKey in keyList:
939 947
940 948 procUnitConfObj = self.procUnitConfObjDict[procKey]
941 idProcUnit = str(int(self.id)*10 + n)
942 procUnitConfObj.updateId(idProcUnit, parentId = self.id)
949 idProcUnit = str(int(self.id) * 10 + n)
950 procUnitConfObj.updateId(idProcUnit, parentId=self.id)
943 951
944 952 newProcUnitConfObjDict[idProcUnit] = procUnitConfObj
945 953 n += 1
946 954
947 955 self.procUnitConfObjDict = newProcUnitConfObjDict
948 956
949 957 def setup(self, id, name='', description=''):
950 958
951 959 print
952 print '*'*60
960 print '*' * 60
953 961 print ' Starting SIGNAL CHAIN PROCESSING v%s ' % schainpy.__version__
954 print '*'*60
962 print '*' * 60
955 963 print
956 964 self.id = str(id)
957 965 self.description = description
958 966
959 967 def update(self, name, description):
960 968
961 969 self.description = description
962 970
963 971 def clone(self):
964 972
965 973 p = Project()
966 974 p.procUnitConfObjDict = self.procUnitConfObjDict
967 975 return p
968 976
969 977 def addReadUnit(self, id=None, datatype=None, name=None, **kwargs):
970 978
971 979 if id is None:
972 980 idReadUnit = self.__getNewId()
973 981 else:
974 982 idReadUnit = str(id)
975 983
976 984 readUnitConfObj = ReadUnitConf()
977 readUnitConfObj.setup(idReadUnit, name, datatype, parentId=self.id, **kwargs)
985 readUnitConfObj.setup(idReadUnit, name, datatype,
986 parentId=self.id, **kwargs)
978 987
979 988 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
980 989
981 990 return readUnitConfObj
982 991
983 992 def addProcUnit(self, inputId='0', datatype=None, name=None):
984 993
985 994 idProcUnit = self.__getNewId()
986 995
987 996 procUnitConfObj = ProcUnitConf()
988 procUnitConfObj.setup(idProcUnit, name, datatype, inputId, parentId=self.id)
997 procUnitConfObj.setup(idProcUnit, name, datatype,
998 inputId, parentId=self.id)
989 999
990 1000 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
991 1001
992 1002 return procUnitConfObj
993 1003
994 1004 def removeProcUnit(self, id):
995 1005
996 1006 if id in self.procUnitConfObjDict.keys():
997 1007 self.procUnitConfObjDict.pop(id)
998 1008
999 1009 def getReadUnitId(self):
1000 1010
1001 1011 readUnitConfObj = self.getReadUnitObj()
1002 1012
1003 1013 return readUnitConfObj.id
1004 1014
1005 1015 def getReadUnitObj(self):
1006 1016
1007 1017 for obj in self.procUnitConfObjDict.values():
1008 1018 if obj.getElementName() == 'ReadUnit':
1009 1019 return obj
1010 1020
1011 1021 return None
1012 1022
1013 1023 def getProcUnitObj(self, id=None, name=None):
1014 1024
1015 1025 if id != None:
1016 1026 return self.procUnitConfObjDict[id]
1017 1027
1018 1028 if name != None:
1019 1029 return self.getProcUnitObjByName(name)
1020 1030
1021 1031 return None
1022 1032
1023 1033 def getProcUnitObjByName(self, name):
1024 1034
1025 1035 for obj in self.procUnitConfObjDict.values():
1026 1036 if obj.name == name:
1027 1037 return obj
1028 1038
1029 1039 return None
1030 1040
1031 1041 def procUnitItems(self):
1032 1042
1033 1043 return self.procUnitConfObjDict.items()
1034 1044
1035 1045 def makeXml(self):
1036 1046
1037 1047 projectElement = Element('Project')
1038 1048 projectElement.set('id', str(self.id))
1039 1049 projectElement.set('name', self.name)
1040 1050 projectElement.set('description', self.description)
1041 1051
1042 1052 for procUnitConfObj in self.procUnitConfObjDict.values():
1043 1053 procUnitConfObj.makeXml(projectElement)
1044 1054
1045 1055 self.projectElement = projectElement
1046 1056
1047 1057 def writeXml(self, filename=None):
1048 1058
1049 1059 if filename == None:
1050 1060 if self.filename:
1051 1061 filename = self.filename
1052 1062 else:
1053 1063 filename = 'schain.xml'
1054 1064
1055 1065 if not filename:
1056 1066 print 'filename has not been defined. Use setFilename(filename) for do it.'
1057 1067 return 0
1058 1068
1059 1069 abs_file = os.path.abspath(filename)
1060 1070
1061 1071 if not os.access(os.path.dirname(abs_file), os.W_OK):
1062 print 'No write permission on %s' %os.path.dirname(abs_file)
1072 print 'No write permission on %s' % os.path.dirname(abs_file)
1063 1073 return 0
1064 1074
1065 1075 if os.path.isfile(abs_file) and not(os.access(abs_file, os.W_OK)):
1066 print 'File %s already exists and it could not be overwriten' %abs_file
1076 print 'File %s already exists and it could not be overwriten' % abs_file
1067 1077 return 0
1068 1078
1069 1079 self.makeXml()
1070 1080
1071 1081 ElementTree(self.projectElement).write(abs_file, method='xml')
1072 1082
1073 1083 self.filename = abs_file
1074 1084
1075 1085 return 1
1076 1086
1077 def readXml(self, filename = None):
1087 def readXml(self, filename=None):
1078 1088
1079 1089 if not filename:
1080 1090 print 'filename is not defined'
1081 1091 return 0
1082 1092
1083 1093 abs_file = os.path.abspath(filename)
1084 1094
1085 1095 if not os.path.isfile(abs_file):
1086 print '%s file does not exist' %abs_file
1096 print '%s file does not exist' % abs_file
1087 1097 return 0
1088 1098
1089 1099 self.projectElement = None
1090 1100 self.procUnitConfObjDict = {}
1091 1101
1092 1102 try:
1093 1103 self.projectElement = ElementTree().parse(abs_file)
1094 1104 except:
1095 print 'Error reading %s, verify file format' %filename
1105 print 'Error reading %s, verify file format' % filename
1096 1106 return 0
1097 1107
1098 1108 self.project = self.projectElement.tag
1099 1109
1100 1110 self.id = self.projectElement.get('id')
1101 1111 self.name = self.projectElement.get('name')
1102 self.description = self.projectElement.get('description')
1103
1104 readUnitElementList = self.projectElement.iter(ReadUnitConf().getElementName())
1112 self.description = self.projectElement.get('description')
1113
1114 readUnitElementList = self.projectElement.iter(
1115 ReadUnitConf().getElementName())
1105 1116
1106 1117 for readUnitElement in readUnitElementList:
1107 1118 readUnitConfObj = ReadUnitConf()
1108 1119 readUnitConfObj.readXml(readUnitElement)
1109 1120
1110 1121 if readUnitConfObj.parentId == None:
1111 1122 readUnitConfObj.parentId = self.id
1112 1123
1113 1124 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
1114 1125
1115 procUnitElementList = self.projectElement.iter(ProcUnitConf().getElementName())
1126 procUnitElementList = self.projectElement.iter(
1127 ProcUnitConf().getElementName())
1116 1128
1117 1129 for procUnitElement in procUnitElementList:
1118 1130 procUnitConfObj = ProcUnitConf()
1119 1131 procUnitConfObj.readXml(procUnitElement)
1120 1132
1121 1133 if procUnitConfObj.parentId == None:
1122 1134 procUnitConfObj.parentId = self.id
1123 1135
1124 1136 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
1125 1137
1126 1138 self.filename = abs_file
1127 1139
1128 1140 return 1
1129 1141
1130 1142 def printattr(self):
1131 1143
1132 print 'Project[%s]: name = %s, description = %s' %(self.id,
1133 self.name,
1134 self.description)
1135
1144 print 'Project[%s]: name = %s, description = %s' % (self.id,
1145 self.name,
1146 self.description)
1147
1136 1148 for procUnitConfObj in self.procUnitConfObjDict.values():
1137 1149 procUnitConfObj.printattr()
1138 1150
1139 1151 def createObjects(self):
1140 1152
1141 1153 for procUnitConfObj in self.procUnitConfObjDict.values():
1142 1154 procUnitConfObj.createObjects(self.plotterQueue)
1143 1155
1144 1156 def __connect(self, objIN, thisObj):
1145 1157
1146 1158 thisObj.setInput(objIN.getOutputObj())
1147 1159
1148 1160 def connectObjects(self):
1149 1161
1150 1162 for thisPUConfObj in self.procUnitConfObjDict.values():
1151 1163
1152 1164 inputId = thisPUConfObj.getInputId()
1153 1165
1154 1166 if int(inputId) == 0:
1155 1167 continue
1156 1168
1157 #Get input object
1169 # Get input object
1158 1170 puConfINObj = self.procUnitConfObjDict[inputId]
1159 1171 puObjIN = puConfINObj.getProcUnitObj()
1160 1172
1161 #Get current object
1173 # Get current object
1162 1174 thisPUObj = thisPUConfObj.getProcUnitObj()
1163 1175
1164 1176 self.__connect(puObjIN, thisPUObj)
1165 1177
1166 1178 def __handleError(self, procUnitConfObj, send_email=False):
1167 1179
1168 1180 import socket
1169 1181
1170 1182 err = traceback.format_exception(sys.exc_info()[0],
1171 sys.exc_info()[1],
1172 sys.exc_info()[2])
1173
1174 print '***** Error occurred in %s *****' %(procUnitConfObj.name)
1175 print '***** %s' %err[-1]
1183 sys.exc_info()[1],
1184 sys.exc_info()[2])
1185
1186 print '***** Error occurred in %s *****' % (procUnitConfObj.name)
1187 print '***** %s' % err[-1]
1176 1188
1177 1189 message = ''.join(err)
1178 1190
1179 1191 sys.stderr.write(message)
1180 1192
1181 1193 if not send_email:
1182 1194 return
1183 1195
1184 subject = 'SChain v%s: Error running %s\n' %(schainpy.__version__, procUnitConfObj.name)
1196 subject = 'SChain v%s: Error running %s\n' % (
1197 schainpy.__version__, procUnitConfObj.name)
1185 1198
1186 subtitle = '%s: %s\n' %(procUnitConfObj.getElementName() ,procUnitConfObj.name)
1187 subtitle += 'Hostname: %s\n' %socket.gethostbyname(socket.gethostname())
1188 subtitle += 'Working directory: %s\n' %os.path.abspath('./')
1189 subtitle += 'Configuration file: %s\n' %self.filename
1190 subtitle += 'Time: %s\n' %str(datetime.datetime.now())
1199 subtitle = '%s: %s\n' % (
1200 procUnitConfObj.getElementName(), procUnitConfObj.name)
1201 subtitle += 'Hostname: %s\n' % socket.gethostbyname(
1202 socket.gethostname())
1203 subtitle += 'Working directory: %s\n' % os.path.abspath('./')
1204 subtitle += 'Configuration file: %s\n' % self.filename
1205 subtitle += 'Time: %s\n' % str(datetime.datetime.now())
1191 1206
1192 1207 readUnitConfObj = self.getReadUnitObj()
1193 1208 if readUnitConfObj:
1194 1209 subtitle += '\nInput parameters:\n'
1195 subtitle += '[Data path = %s]\n' %readUnitConfObj.path
1196 subtitle += '[Data type = %s]\n' %readUnitConfObj.datatype
1197 subtitle += '[Start date = %s]\n' %readUnitConfObj.startDate
1198 subtitle += '[End date = %s]\n' %readUnitConfObj.endDate
1199 subtitle += '[Start time = %s]\n' %readUnitConfObj.startTime
1200 subtitle += '[End time = %s]\n' %readUnitConfObj.endTime
1210 subtitle += '[Data path = %s]\n' % readUnitConfObj.path
1211 subtitle += '[Data type = %s]\n' % readUnitConfObj.datatype
1212 subtitle += '[Start date = %s]\n' % readUnitConfObj.startDate
1213 subtitle += '[End date = %s]\n' % readUnitConfObj.endDate
1214 subtitle += '[Start time = %s]\n' % readUnitConfObj.startTime
1215 subtitle += '[End time = %s]\n' % readUnitConfObj.endTime
1201 1216
1202 1217 adminObj = schainpy.admin.SchainNotify()
1203 1218 adminObj.sendAlert(message=message,
1204 subject=subject,
1205 subtitle=subtitle,
1206 filename=self.filename)
1207
1219 subject=subject,
1220 subtitle=subtitle,
1221 filename=self.filename)
1222
1208 1223 def isPaused(self):
1209 1224 return 0
1210 1225
1211 1226 def isStopped(self):
1212 1227 return 0
1213 1228
1214 1229 def runController(self):
1215 1230 '''
1216 1231 returns 0 when this process has been stopped, 1 otherwise
1217 1232 '''
1218 1233
1219 1234 if self.isPaused():
1220 1235 print 'Process suspended'
1221 1236
1222 1237 while True:
1223 1238 time.sleep(0.1)
1224 1239
1225 1240 if not self.isPaused():
1226 1241 break
1227 1242
1228 1243 if self.isStopped():
1229 1244 break
1230 1245
1231 1246 print 'Process reinitialized'
1232 1247
1233 1248 if self.isStopped():
1234 1249 print 'Process stopped'
1235 1250 return 0
1236 1251
1237 1252 return 1
1238 1253
1239 1254 def setFilename(self, filename):
1240 1255
1241 1256 self.filename = filename
1242 1257
1243 1258 def setPlotterQueue(self, plotter_queue):
1244 1259
1245 1260 raise NotImplementedError, 'Use schainpy.controller_api.ControllerThread instead Project class'
1246 1261
1247 1262 def getPlotterQueue(self):
1248 1263
1249 1264 raise NotImplementedError, 'Use schainpy.controller_api.ControllerThread instead Project class'
1250 1265
1251 1266 def useExternalPlotter(self):
1252 1267
1253 1268 raise NotImplementedError, 'Use schainpy.controller_api.ControllerThread instead Project class'
1254 1269
1255 1270 def run(self):
1256 1271
1257 1272 log.success('Starting {}'.format(self.name))
1258
1273
1259 1274 self.createObjects()
1260 1275 self.connectObjects()
1261 1276
1262 1277 keyList = self.procUnitConfObjDict.keys()
1263 1278 keyList.sort()
1264 1279
1265 1280 while(True):
1266 1281
1267 1282 is_ok = False
1268 1283
1269 1284 for procKey in keyList:
1270 1285
1271 1286 procUnitConfObj = self.procUnitConfObjDict[procKey]
1272 1287
1273 1288 try:
1274 1289 sts = procUnitConfObj.run()
1275 1290 is_ok = is_ok or sts
1276 1291 except KeyboardInterrupt:
1277 1292 is_ok = False
1278 1293 break
1279 1294 except ValueError, e:
1280 1295 time.sleep(0.5)
1281 1296 self.__handleError(procUnitConfObj, send_email=True)
1282 1297 is_ok = False
1283 1298 break
1284 1299 except:
1285 1300 time.sleep(0.5)
1286 1301 self.__handleError(procUnitConfObj)
1287 1302 is_ok = False
1288 1303 break
1289 1304
1290 #If every process unit finished so end process
1305 # If every process unit finished so end process
1291 1306 if not(is_ok):
1292 1307 break
1293 1308
1294 1309 if not self.runController():
1295 1310 break
1296 1311
1297 #Closing every process
1312 # Closing every process
1298 1313 for procKey in keyList:
1299 1314 procUnitConfObj = self.procUnitConfObjDict[procKey]
1300 1315 procUnitConfObj.close()
1301 1316
1302 1317 log.success('{} finished'.format(self.name))
@@ -1,94 +1,92
1 1 """
2 2 Classes to save parameters from Windows.
3 3
4 4 -Project window
5 5 -Voltage window
6 6 -Spectra window
7 7 -SpectraHeis window
8 8 -Correlation window
9 9
10 10 """
11 11
12 12 class ProjectParms():
13 13
14 14 parmsOk = False
15 15 name = None
16 16 description = None
17 17 datatype = None
18 18 ext = None
19 19 dpath = None
20 20 startDate = None
21 21 endDate = None
22 22 startTime = None
23 23 endTime = None
24 24 online = None
25 25 delay = None
26 26 walk = None
27 27 expLabel = None
28 28 set = None
29 29 ippKm = None
30 30
31 31 def __init__(self):
32 32
33 33 self.parmsOk = True
34 34 self.description = ''
35 35 self.expLabel = ''
36 36 self.set = ''
37 37 self.ippKm = ''
38 38 self.walk = None
39 39 self.delay = ''
40 40
41 41 def getDatatypeIndex(self):
42 42
43 43 indexDatatype = None
44 44
45 45 if 'voltage' in self.datatype.lower():
46 46 indexDatatype = 0
47 47 if 'spectra' in self.datatype.lower():
48 48 indexDatatype = 1
49 49 if 'fits' in self.datatype.lower():
50 50 indexDatatype = 2
51 51 if 'usrp' in self.datatype.lower():
52 52 indexDatatype = 3
53
54 53 return indexDatatype
55 54
56 55 def getExt(self):
57 56
58 57 ext = None
59 58
60 59 if self.datatype.lower() == 'voltage':
61 60 ext = '.r'
62 61 if self.datatype.lower() == 'spectra':
63 62 ext = '.pdata'
64 63 if self.datatype.lower() == 'fits':
65 64 ext = '.fits'
66 65 if self.datatype.lower() == 'usrp':
67 66 ext = '.hdf5'
68
69 67 return ext
70 68
71 69 def set(self, project_name, datatype, ext, dpath, online,
72 70 startDate=None, endDate=None, startTime=None, endTime=None,
73 71 delay=None, walk=None, set=None, ippKm=None, parmsOk=True, expLabel=''):
74 72
75 73 name = project_name
76 74 datatype = datatype
77 75 ext = ext
78 76 dpath = dpath
79 77 startDate = startDate
80 78 endDate = endDate
81 79 startTime = startTime
82 80 endTime = endTime
83 81 online = online
84 82 delay = delay
85 83 walk = walk
86 84 set = set
87 85 ippKm = ippKm
88 86 expLabel = expLabel
89 87
90 88 self.parmsOk = parmsOk
91 89
92 90 def isValid(self):
93 91
94 92 return self.parmsOk No newline at end of file
@@ -1,12 +1,12
1 1 #from schainpy.model.data.jrodata import *
2 2 # from schainpy.model.io.jrodataIO import *
3 3 # from schainpy.model.proc.jroprocessing import *
4 4 # from schainpy.model.graphics.jroplot import *
5 5 # from schainpy.model.utils.jroutils import *
6 6 # from schainpy.serializer import *
7 7
8 from graphics import *
8 9 from data import *
9 10 from io import *
10 11 from proc import *
11 from graphics import *
12 from utils import * No newline at end of file
12 from utils import *
@@ -1,1229 +1,1227
1 1 '''
2 2
3 3 $Author: murco $
4 4 $Id: JROData.py 173 2012-11-20 15:06:21Z murco $
5 5 '''
6 6
7 7 import copy
8 8 import numpy
9 9 import datetime
10 10
11 11 from jroheaderIO import SystemHeader, RadarControllerHeader
12 12 from schainpy import cSchain
13 13
14 14
15 15 def getNumpyDtype(dataTypeCode):
16 16
17 17 if dataTypeCode == 0:
18 18 numpyDtype = numpy.dtype([('real','<i1'),('imag','<i1')])
19 19 elif dataTypeCode == 1:
20 20 numpyDtype = numpy.dtype([('real','<i2'),('imag','<i2')])
21 21 elif dataTypeCode == 2:
22 22 numpyDtype = numpy.dtype([('real','<i4'),('imag','<i4')])
23 23 elif dataTypeCode == 3:
24 24 numpyDtype = numpy.dtype([('real','<i8'),('imag','<i8')])
25 25 elif dataTypeCode == 4:
26 26 numpyDtype = numpy.dtype([('real','<f4'),('imag','<f4')])
27 27 elif dataTypeCode == 5:
28 28 numpyDtype = numpy.dtype([('real','<f8'),('imag','<f8')])
29 29 else:
30 30 raise ValueError, 'dataTypeCode was not defined'
31 31
32 32 return numpyDtype
33 33
34 34 def getDataTypeCode(numpyDtype):
35 35
36 36 if numpyDtype == numpy.dtype([('real','<i1'),('imag','<i1')]):
37 37 datatype = 0
38 38 elif numpyDtype == numpy.dtype([('real','<i2'),('imag','<i2')]):
39 39 datatype = 1
40 40 elif numpyDtype == numpy.dtype([('real','<i4'),('imag','<i4')]):
41 41 datatype = 2
42 42 elif numpyDtype == numpy.dtype([('real','<i8'),('imag','<i8')]):
43 43 datatype = 3
44 44 elif numpyDtype == numpy.dtype([('real','<f4'),('imag','<f4')]):
45 45 datatype = 4
46 46 elif numpyDtype == numpy.dtype([('real','<f8'),('imag','<f8')]):
47 47 datatype = 5
48 48 else:
49 49 datatype = None
50 50
51 51 return datatype
52 52
53 53 def hildebrand_sekhon(data, navg):
54 54 """
55 55 This method is for the objective determination of the noise level in Doppler spectra. This
56 56 implementation technique is based on the fact that the standard deviation of the spectral
57 57 densities is equal to the mean spectral density for white Gaussian noise
58 58
59 59 Inputs:
60 60 Data : heights
61 61 navg : numbers of averages
62 62
63 63 Return:
64 64 -1 : any error
65 65 anoise : noise's level
66 66 """
67 67
68 68 sortdata = numpy.sort(data, axis=None)
69 69 # lenOfData = len(sortdata)
70 70 # nums_min = lenOfData*0.2
71 71 #
72 72 # if nums_min <= 5:
73 73 # nums_min = 5
74 74 #
75 75 # sump = 0.
76 76 #
77 77 # sumq = 0.
78 78 #
79 79 # j = 0
80 80 #
81 81 # cont = 1
82 82 #
83 83 # while((cont==1)and(j<lenOfData)):
84 84 #
85 85 # sump += sortdata[j]
86 86 #
87 87 # sumq += sortdata[j]**2
88 88 #
89 89 # if j > nums_min:
90 90 # rtest = float(j)/(j-1) + 1.0/navg
91 91 # if ((sumq*j) > (rtest*sump**2)):
92 92 # j = j - 1
93 93 # sump = sump - sortdata[j]
94 94 # sumq = sumq - sortdata[j]**2
95 95 # cont = 0
96 96 #
97 97 # j += 1
98 98 #
99 99 # lnoise = sump /j
100 100 #
101 101 # return lnoise
102 102
103 103 return cSchain.hildebrand_sekhon(sortdata, navg)
104 104
105 105
106 106 class Beam:
107 107
108 108 def __init__(self):
109 109 self.codeList = []
110 110 self.azimuthList = []
111 111 self.zenithList = []
112 112
113 113 class GenericData(object):
114 114
115 115 flagNoData = True
116 116
117 117 def copy(self, inputObj=None):
118 118
119 119 if inputObj == None:
120 120 return copy.deepcopy(self)
121 121
122 122 for key in inputObj.__dict__.keys():
123 123
124 124 attribute = inputObj.__dict__[key]
125 125
126 126 #If this attribute is a tuple or list
127 127 if type(inputObj.__dict__[key]) in (tuple, list):
128 128 self.__dict__[key] = attribute[:]
129 129 continue
130 130
131 131 #If this attribute is another object or instance
132 132 if hasattr(attribute, '__dict__'):
133 133 self.__dict__[key] = attribute.copy()
134 134 continue
135 135
136 136 self.__dict__[key] = inputObj.__dict__[key]
137 137
138 138 def deepcopy(self):
139 139
140 140 return copy.deepcopy(self)
141 141
142 142 def isEmpty(self):
143 143
144 144 return self.flagNoData
145 145
146 146 class JROData(GenericData):
147 147
148 148 # m_BasicHeader = BasicHeader()
149 149 # m_ProcessingHeader = ProcessingHeader()
150 150
151 151 systemHeaderObj = SystemHeader()
152 152
153 153 radarControllerHeaderObj = RadarControllerHeader()
154 154
155 155 # data = None
156 156
157 157 type = None
158 158
159 159 datatype = None #dtype but in string
160 160
161 161 # dtype = None
162 162
163 163 # nChannels = None
164 164
165 165 # nHeights = None
166 166
167 167 nProfiles = None
168 168
169 169 heightList = None
170 170
171 171 channelList = None
172 172
173 173 flagDiscontinuousBlock = False
174 174
175 175 useLocalTime = False
176 176
177 177 utctime = None
178 178
179 179 timeZone = None
180 180
181 181 dstFlag = None
182 182
183 183 errorCount = None
184 184
185 185 blocksize = None
186 186
187 187 # nCode = None
188 188 #
189 189 # nBaud = None
190 190 #
191 191 # code = None
192 192
193 193 flagDecodeData = False #asumo q la data no esta decodificada
194 194
195 195 flagDeflipData = False #asumo q la data no esta sin flip
196 196
197 197 flagShiftFFT = False
198 198
199 199 # ippSeconds = None
200 200
201 201 # timeInterval = None
202 202
203 203 nCohInt = None
204 204
205 205 # noise = None
206 206
207 207 windowOfFilter = 1
208 208
209 209 #Speed of ligth
210 210 C = 3e8
211 211
212 212 frequency = 49.92e6
213 213
214 214 realtime = False
215 215
216 216 beacon_heiIndexList = None
217 217
218 218 last_block = None
219 219
220 220 blocknow = None
221 221
222 222 azimuth = None
223 223
224 224 zenith = None
225 225
226 226 beam = Beam()
227 227
228 228 profileIndex = None
229 229
230 230 def getNoise(self):
231 231
232 232 raise NotImplementedError
233 233
234 234 def getNChannels(self):
235 235
236 236 return len(self.channelList)
237 237
238 238 def getChannelIndexList(self):
239 239
240 240 return range(self.nChannels)
241 241
242 242 def getNHeights(self):
243 243
244 244 return len(self.heightList)
245 245
246 246 def getHeiRange(self, extrapoints=0):
247 247
248 248 heis = self.heightList
249 249 # deltah = self.heightList[1] - self.heightList[0]
250 250 #
251 251 # heis.append(self.heightList[-1])
252 252
253 253 return heis
254 254
255 255 def getDeltaH(self):
256 256
257 257 delta = self.heightList[1] - self.heightList[0]
258 258
259 259 return delta
260 260
261 261 def getltctime(self):
262 262
263 263 if self.useLocalTime:
264 264 return self.utctime - self.timeZone*60
265 265
266 266 return self.utctime
267 267
268 268 def getDatatime(self):
269 269
270 270 datatimeValue = datetime.datetime.utcfromtimestamp(self.ltctime)
271 271 return datatimeValue
272 272
273 273 def getTimeRange(self):
274 274
275 275 datatime = []
276 276
277 277 datatime.append(self.ltctime)
278 278 datatime.append(self.ltctime + self.timeInterval+1)
279 279
280 280 datatime = numpy.array(datatime)
281 281
282 282 return datatime
283 283
284 284 def getFmaxTimeResponse(self):
285 285
286 286 period = (10**-6)*self.getDeltaH()/(0.15)
287 287
288 288 PRF = 1./(period * self.nCohInt)
289 289
290 290 fmax = PRF
291 291
292 292 return fmax
293 293
294 294 def getFmax(self):
295
296 295 PRF = 1./(self.ippSeconds * self.nCohInt)
297 296
298 297 fmax = PRF
299
300 298 return fmax
301 299
302 300 def getVmax(self):
303 301
304 302 _lambda = self.C/self.frequency
305 303
306 304 vmax = self.getFmax() * _lambda/2
307 305
308 306 return vmax
309 307
310 308 def get_ippSeconds(self):
311 309 '''
312 310 '''
313 311 return self.radarControllerHeaderObj.ippSeconds
314 312
315 313 def set_ippSeconds(self, ippSeconds):
316 314 '''
317 315 '''
318 316
319 317 self.radarControllerHeaderObj.ippSeconds = ippSeconds
320 318
321 319 return
322 320
323 321 def get_dtype(self):
324 322 '''
325 323 '''
326 324 return getNumpyDtype(self.datatype)
327 325
328 326 def set_dtype(self, numpyDtype):
329 327 '''
330 328 '''
331 329
332 330 self.datatype = getDataTypeCode(numpyDtype)
333 331
334 332 def get_code(self):
335 333 '''
336 334 '''
337 335 return self.radarControllerHeaderObj.code
338 336
339 337 def set_code(self, code):
340 338 '''
341 339 '''
342 340 self.radarControllerHeaderObj.code = code
343 341
344 342 return
345 343
346 344 def get_ncode(self):
347 345 '''
348 346 '''
349 347 return self.radarControllerHeaderObj.nCode
350 348
351 349 def set_ncode(self, nCode):
352 350 '''
353 351 '''
354 352 self.radarControllerHeaderObj.nCode = nCode
355 353
356 354 return
357 355
358 356 def get_nbaud(self):
359 357 '''
360 358 '''
361 359 return self.radarControllerHeaderObj.nBaud
362 360
363 361 def set_nbaud(self, nBaud):
364 362 '''
365 363 '''
366 364 self.radarControllerHeaderObj.nBaud = nBaud
367 365
368 366 return
369 367
370 368 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
371 369 channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.")
372 370 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
373 371 #noise = property(getNoise, "I'm the 'nHeights' property.")
374 372 datatime = property(getDatatime, "I'm the 'datatime' property")
375 373 ltctime = property(getltctime, "I'm the 'ltctime' property")
376 374 ippSeconds = property(get_ippSeconds, set_ippSeconds)
377 375 dtype = property(get_dtype, set_dtype)
378 376 # timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
379 377 code = property(get_code, set_code)
380 378 nCode = property(get_ncode, set_ncode)
381 379 nBaud = property(get_nbaud, set_nbaud)
382 380
383 381 class Voltage(JROData):
384 382
385 383 #data es un numpy array de 2 dmensiones (canales, alturas)
386 384 data = None
387 385
388 386 def __init__(self):
389 387 '''
390 388 Constructor
391 389 '''
392 390
393 391 self.useLocalTime = True
394 392
395 393 self.radarControllerHeaderObj = RadarControllerHeader()
396 394
397 395 self.systemHeaderObj = SystemHeader()
398 396
399 397 self.type = "Voltage"
400 398
401 399 self.data = None
402 400
403 401 # self.dtype = None
404 402
405 403 # self.nChannels = 0
406 404
407 405 # self.nHeights = 0
408 406
409 407 self.nProfiles = None
410 408
411 409 self.heightList = None
412 410
413 411 self.channelList = None
414 412
415 413 # self.channelIndexList = None
416 414
417 415 self.flagNoData = True
418 416
419 417 self.flagDiscontinuousBlock = False
420 418
421 419 self.utctime = None
422 420
423 421 self.timeZone = None
424 422
425 423 self.dstFlag = None
426 424
427 425 self.errorCount = None
428 426
429 427 self.nCohInt = None
430 428
431 429 self.blocksize = None
432 430
433 431 self.flagDecodeData = False #asumo q la data no esta decodificada
434 432
435 433 self.flagDeflipData = False #asumo q la data no esta sin flip
436 434
437 435 self.flagShiftFFT = False
438 436
439 437 self.flagDataAsBlock = False #Asumo que la data es leida perfil a perfil
440 438
441 439 self.profileIndex = 0
442 440
443 441 def getNoisebyHildebrand(self, channel = None):
444 442 """
445 443 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
446 444
447 445 Return:
448 446 noiselevel
449 447 """
450 448
451 449 if channel != None:
452 450 data = self.data[channel]
453 451 nChannels = 1
454 452 else:
455 453 data = self.data
456 454 nChannels = self.nChannels
457 455
458 456 noise = numpy.zeros(nChannels)
459 457 power = data * numpy.conjugate(data)
460 458
461 459 for thisChannel in range(nChannels):
462 460 if nChannels == 1:
463 461 daux = power[:].real
464 462 else:
465 463 daux = power[thisChannel,:].real
466 464 noise[thisChannel] = hildebrand_sekhon(daux, self.nCohInt)
467 465
468 466 return noise
469 467
470 468 def getNoise(self, type = 1, channel = None):
471 469
472 470 if type == 1:
473 471 noise = self.getNoisebyHildebrand(channel)
474 472
475 473 return noise
476 474
477 475 def getPower(self, channel = None):
478 476
479 477 if channel != None:
480 478 data = self.data[channel]
481 479 else:
482 480 data = self.data
483 481
484 482 power = data * numpy.conjugate(data)
485 483 powerdB = 10*numpy.log10(power.real)
486 484 powerdB = numpy.squeeze(powerdB)
487 485
488 486 return powerdB
489 487
490 488 def getTimeInterval(self):
491 489
492 490 timeInterval = self.ippSeconds * self.nCohInt
493 491
494 492 return timeInterval
495 493
496 494 noise = property(getNoise, "I'm the 'nHeights' property.")
497 495 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
498 496
499 497 class Spectra(JROData):
500 498
501 499 #data spc es un numpy array de 2 dmensiones (canales, perfiles, alturas)
502 500 data_spc = None
503 501
504 502 #data cspc es un numpy array de 2 dmensiones (canales, pares, alturas)
505 503 data_cspc = None
506 504
507 505 #data dc es un numpy array de 2 dmensiones (canales, alturas)
508 506 data_dc = None
509 507
510 508 #data power
511 509 data_pwr = None
512 510
513 511 nFFTPoints = None
514 512
515 513 # nPairs = None
516 514
517 515 pairsList = None
518 516
519 517 nIncohInt = None
520 518
521 519 wavelength = None #Necesario para cacular el rango de velocidad desde la frecuencia
522 520
523 521 nCohInt = None #se requiere para determinar el valor de timeInterval
524 522
525 523 ippFactor = None
526 524
527 525 profileIndex = 0
528 526
529 527 plotting = "spectra"
530 528
531 529 def __init__(self):
532 530 '''
533 531 Constructor
534 532 '''
535 533
536 534 self.useLocalTime = True
537 535
538 536 self.radarControllerHeaderObj = RadarControllerHeader()
539 537
540 538 self.systemHeaderObj = SystemHeader()
541 539
542 540 self.type = "Spectra"
543 541
544 542 # self.data = None
545 543
546 544 # self.dtype = None
547 545
548 546 # self.nChannels = 0
549 547
550 548 # self.nHeights = 0
551 549
552 550 self.nProfiles = None
553 551
554 552 self.heightList = None
555 553
556 554 self.channelList = None
557 555
558 556 # self.channelIndexList = None
559 557
560 558 self.pairsList = None
561 559
562 560 self.flagNoData = True
563 561
564 562 self.flagDiscontinuousBlock = False
565 563
566 564 self.utctime = None
567 565
568 566 self.nCohInt = None
569 567
570 568 self.nIncohInt = None
571 569
572 570 self.blocksize = None
573 571
574 572 self.nFFTPoints = None
575 573
576 574 self.wavelength = None
577 575
578 576 self.flagDecodeData = False #asumo q la data no esta decodificada
579 577
580 578 self.flagDeflipData = False #asumo q la data no esta sin flip
581 579
582 580 self.flagShiftFFT = False
583 581
584 582 self.ippFactor = 1
585 583
586 584 #self.noise = None
587 585
588 586 self.beacon_heiIndexList = []
589 587
590 588 self.noise_estimation = None
591 589
592 590
593 591 def getNoisebyHildebrand(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
594 592 """
595 593 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
596 594
597 595 Return:
598 596 noiselevel
599 597 """
600 598
601 599 noise = numpy.zeros(self.nChannels)
602 600
603 601 for channel in range(self.nChannels):
604 602 daux = self.data_spc[channel,xmin_index:xmax_index,ymin_index:ymax_index]
605 603 noise[channel] = hildebrand_sekhon(daux, self.nIncohInt)
606 604
607 605 return noise
608 606
609 607 def getNoise(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
610 608
611 609 if self.noise_estimation is not None:
612 610 return self.noise_estimation #this was estimated by getNoise Operation defined in jroproc_spectra.py
613 611 else:
614 612 noise = self.getNoisebyHildebrand(xmin_index, xmax_index, ymin_index, ymax_index)
615 613 return noise
616 614
617 615 def getFreqRangeTimeResponse(self, extrapoints=0):
618 616
619 617 deltafreq = self.getFmaxTimeResponse() / (self.nFFTPoints*self.ippFactor)
620 618 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
621 619
622 620 return freqrange
623 621
624 622 def getAcfRange(self, extrapoints=0):
625 623
626 624 deltafreq = 10./(self.getFmax() / (self.nFFTPoints*self.ippFactor))
627 625 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
628 626
629 627 return freqrange
630 628
631 629 def getFreqRange(self, extrapoints=0):
632 630
633 631 deltafreq = self.getFmax() / (self.nFFTPoints*self.ippFactor)
634 632 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
635 633
636 634 return freqrange
637 635
638 636 def getVelRange(self, extrapoints=0):
639 637
640 638 deltav = self.getVmax() / (self.nFFTPoints*self.ippFactor)
641 639 velrange = deltav*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) #- deltav/2
642 640
643 641 return velrange
644 642
645 643 def getNPairs(self):
646 644
647 645 return len(self.pairsList)
648 646
649 647 def getPairsIndexList(self):
650 648
651 649 return range(self.nPairs)
652 650
653 651 def getNormFactor(self):
654 652
655 653 pwcode = 1
656 654
657 655 if self.flagDecodeData:
658 656 pwcode = numpy.sum(self.code[0]**2)
659 657 #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
660 658 normFactor = self.nProfiles*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
661 659
662 660 return normFactor
663 661
664 662 def getFlagCspc(self):
665 663
666 664 if self.data_cspc is None:
667 665 return True
668 666
669 667 return False
670 668
671 669 def getFlagDc(self):
672 670
673 671 if self.data_dc is None:
674 672 return True
675 673
676 674 return False
677 675
678 676 def getTimeInterval(self):
679 677
680 678 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt * self.nProfiles
681 679
682 680 return timeInterval
683 681
684 682 def getPower(self):
685 683
686 684 factor = self.normFactor
687 685 z = self.data_spc/factor
688 686 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
689 687 avg = numpy.average(z, axis=1)
690 688
691 689 return 10*numpy.log10(avg)
692 690
693 691 def getCoherence(self, pairsList=None, phase=False):
694 692
695 693 z = []
696 694 if pairsList is None:
697 695 pairsIndexList = self.pairsIndexList
698 696 else:
699 697 pairsIndexList = []
700 698 for pair in pairsList:
701 699 if pair not in self.pairsList:
702 700 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
703 701 pairsIndexList.append(self.pairsList.index(pair))
704 702 for i in range(len(pairsIndexList)):
705 703 pair = self.pairsList[pairsIndexList[i]]
706 704 ccf = numpy.average(self.data_cspc[pairsIndexList[i], :, :], axis=0)
707 705 powa = numpy.average(self.data_spc[pair[0], :, :], axis=0)
708 706 powb = numpy.average(self.data_spc[pair[1], :, :], axis=0)
709 707 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
710 708 if phase:
711 709 data = numpy.arctan2(avgcoherenceComplex.imag,
712 710 avgcoherenceComplex.real)*180/numpy.pi
713 711 else:
714 712 data = numpy.abs(avgcoherenceComplex)
715 713
716 714 z.append(data)
717 715
718 716 return numpy.array(z)
719 717
720 718 def setValue(self, value):
721 719
722 720 print "This property should not be initialized"
723 721
724 722 return
725 723
726 724 nPairs = property(getNPairs, setValue, "I'm the 'nPairs' property.")
727 725 pairsIndexList = property(getPairsIndexList, setValue, "I'm the 'pairsIndexList' property.")
728 726 normFactor = property(getNormFactor, setValue, "I'm the 'getNormFactor' property.")
729 727 flag_cspc = property(getFlagCspc, setValue)
730 728 flag_dc = property(getFlagDc, setValue)
731 729 noise = property(getNoise, setValue, "I'm the 'nHeights' property.")
732 730 timeInterval = property(getTimeInterval, setValue, "I'm the 'timeInterval' property")
733 731
734 732 class SpectraHeis(Spectra):
735 733
736 734 data_spc = None
737 735
738 736 data_cspc = None
739 737
740 738 data_dc = None
741 739
742 740 nFFTPoints = None
743 741
744 742 # nPairs = None
745 743
746 744 pairsList = None
747 745
748 746 nCohInt = None
749 747
750 748 nIncohInt = None
751 749
752 750 def __init__(self):
753 751
754 752 self.radarControllerHeaderObj = RadarControllerHeader()
755 753
756 754 self.systemHeaderObj = SystemHeader()
757 755
758 756 self.type = "SpectraHeis"
759 757
760 758 # self.dtype = None
761 759
762 760 # self.nChannels = 0
763 761
764 762 # self.nHeights = 0
765 763
766 764 self.nProfiles = None
767 765
768 766 self.heightList = None
769 767
770 768 self.channelList = None
771 769
772 770 # self.channelIndexList = None
773 771
774 772 self.flagNoData = True
775 773
776 774 self.flagDiscontinuousBlock = False
777 775
778 776 # self.nPairs = 0
779 777
780 778 self.utctime = None
781 779
782 780 self.blocksize = None
783 781
784 782 self.profileIndex = 0
785 783
786 784 self.nCohInt = 1
787 785
788 786 self.nIncohInt = 1
789 787
790 788 def getNormFactor(self):
791 789 pwcode = 1
792 790 if self.flagDecodeData:
793 791 pwcode = numpy.sum(self.code[0]**2)
794 792
795 793 normFactor = self.nIncohInt*self.nCohInt*pwcode
796 794
797 795 return normFactor
798 796
799 797 def getTimeInterval(self):
800 798
801 799 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
802 800
803 801 return timeInterval
804 802
805 803 normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.")
806 804 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
807 805
808 806 class Fits(JROData):
809 807
810 808 heightList = None
811 809
812 810 channelList = None
813 811
814 812 flagNoData = True
815 813
816 814 flagDiscontinuousBlock = False
817 815
818 816 useLocalTime = False
819 817
820 818 utctime = None
821 819
822 820 timeZone = None
823 821
824 822 # ippSeconds = None
825 823
826 824 # timeInterval = None
827 825
828 826 nCohInt = None
829 827
830 828 nIncohInt = None
831 829
832 830 noise = None
833 831
834 832 windowOfFilter = 1
835 833
836 834 #Speed of ligth
837 835 C = 3e8
838 836
839 837 frequency = 49.92e6
840 838
841 839 realtime = False
842 840
843 841
844 842 def __init__(self):
845 843
846 844 self.type = "Fits"
847 845
848 846 self.nProfiles = None
849 847
850 848 self.heightList = None
851 849
852 850 self.channelList = None
853 851
854 852 # self.channelIndexList = None
855 853
856 854 self.flagNoData = True
857 855
858 856 self.utctime = None
859 857
860 858 self.nCohInt = 1
861 859
862 860 self.nIncohInt = 1
863 861
864 862 self.useLocalTime = True
865 863
866 864 self.profileIndex = 0
867 865
868 866 # self.utctime = None
869 867 # self.timeZone = None
870 868 # self.ltctime = None
871 869 # self.timeInterval = None
872 870 # self.header = None
873 871 # self.data_header = None
874 872 # self.data = None
875 873 # self.datatime = None
876 874 # self.flagNoData = False
877 875 # self.expName = ''
878 876 # self.nChannels = None
879 877 # self.nSamples = None
880 878 # self.dataBlocksPerFile = None
881 879 # self.comments = ''
882 880 #
883 881
884 882
885 883 def getltctime(self):
886 884
887 885 if self.useLocalTime:
888 886 return self.utctime - self.timeZone*60
889 887
890 888 return self.utctime
891 889
892 890 def getDatatime(self):
893 891
894 892 datatime = datetime.datetime.utcfromtimestamp(self.ltctime)
895 893 return datatime
896 894
897 895 def getTimeRange(self):
898 896
899 897 datatime = []
900 898
901 899 datatime.append(self.ltctime)
902 900 datatime.append(self.ltctime + self.timeInterval)
903 901
904 902 datatime = numpy.array(datatime)
905 903
906 904 return datatime
907 905
908 906 def getHeiRange(self):
909 907
910 908 heis = self.heightList
911 909
912 910 return heis
913 911
914 912 def getNHeights(self):
915 913
916 914 return len(self.heightList)
917 915
918 916 def getNChannels(self):
919 917
920 918 return len(self.channelList)
921 919
922 920 def getChannelIndexList(self):
923 921
924 922 return range(self.nChannels)
925 923
926 924 def getNoise(self, type = 1):
927 925
928 926 #noise = numpy.zeros(self.nChannels)
929 927
930 928 if type == 1:
931 929 noise = self.getNoisebyHildebrand()
932 930
933 931 if type == 2:
934 932 noise = self.getNoisebySort()
935 933
936 934 if type == 3:
937 935 noise = self.getNoisebyWindow()
938 936
939 937 return noise
940 938
941 939 def getTimeInterval(self):
942 940
943 941 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
944 942
945 943 return timeInterval
946 944
947 945 datatime = property(getDatatime, "I'm the 'datatime' property")
948 946 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
949 947 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
950 948 channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.")
951 949 noise = property(getNoise, "I'm the 'nHeights' property.")
952 950
953 951 ltctime = property(getltctime, "I'm the 'ltctime' property")
954 952 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
955 953
956 954
957 955 class Correlation(JROData):
958 956
959 957 noise = None
960 958
961 959 SNR = None
962 960
963 961 #--------------------------------------------------
964 962
965 963 mode = None
966 964
967 965 split = False
968 966
969 967 data_cf = None
970 968
971 969 lags = None
972 970
973 971 lagRange = None
974 972
975 973 pairsList = None
976 974
977 975 normFactor = None
978 976
979 977 #--------------------------------------------------
980 978
981 979 # calculateVelocity = None
982 980
983 981 nLags = None
984 982
985 983 nPairs = None
986 984
987 985 nAvg = None
988 986
989 987
990 988 def __init__(self):
991 989 '''
992 990 Constructor
993 991 '''
994 992 self.radarControllerHeaderObj = RadarControllerHeader()
995 993
996 994 self.systemHeaderObj = SystemHeader()
997 995
998 996 self.type = "Correlation"
999 997
1000 998 self.data = None
1001 999
1002 1000 self.dtype = None
1003 1001
1004 1002 self.nProfiles = None
1005 1003
1006 1004 self.heightList = None
1007 1005
1008 1006 self.channelList = None
1009 1007
1010 1008 self.flagNoData = True
1011 1009
1012 1010 self.flagDiscontinuousBlock = False
1013 1011
1014 1012 self.utctime = None
1015 1013
1016 1014 self.timeZone = None
1017 1015
1018 1016 self.dstFlag = None
1019 1017
1020 1018 self.errorCount = None
1021 1019
1022 1020 self.blocksize = None
1023 1021
1024 1022 self.flagDecodeData = False #asumo q la data no esta decodificada
1025 1023
1026 1024 self.flagDeflipData = False #asumo q la data no esta sin flip
1027 1025
1028 1026 self.pairsList = None
1029 1027
1030 1028 self.nPoints = None
1031 1029
1032 1030 def getPairsList(self):
1033 1031
1034 1032 return self.pairsList
1035 1033
1036 1034 def getNoise(self, mode = 2):
1037 1035
1038 1036 indR = numpy.where(self.lagR == 0)[0][0]
1039 1037 indT = numpy.where(self.lagT == 0)[0][0]
1040 1038
1041 1039 jspectra0 = self.data_corr[:,:,indR,:]
1042 1040 jspectra = copy.copy(jspectra0)
1043 1041
1044 1042 num_chan = jspectra.shape[0]
1045 1043 num_hei = jspectra.shape[2]
1046 1044
1047 1045 freq_dc = jspectra.shape[1]/2
1048 1046 ind_vel = numpy.array([-2,-1,1,2]) + freq_dc
1049 1047
1050 1048 if ind_vel[0]<0:
1051 1049 ind_vel[range(0,1)] = ind_vel[range(0,1)] + self.num_prof
1052 1050
1053 1051 if mode == 1:
1054 1052 jspectra[:,freq_dc,:] = (jspectra[:,ind_vel[1],:] + jspectra[:,ind_vel[2],:])/2 #CORRECCION
1055 1053
1056 1054 if mode == 2:
1057 1055
1058 1056 vel = numpy.array([-2,-1,1,2])
1059 1057 xx = numpy.zeros([4,4])
1060 1058
1061 1059 for fil in range(4):
1062 1060 xx[fil,:] = vel[fil]**numpy.asarray(range(4))
1063 1061
1064 1062 xx_inv = numpy.linalg.inv(xx)
1065 1063 xx_aux = xx_inv[0,:]
1066 1064
1067 1065 for ich in range(num_chan):
1068 1066 yy = jspectra[ich,ind_vel,:]
1069 1067 jspectra[ich,freq_dc,:] = numpy.dot(xx_aux,yy)
1070 1068
1071 1069 junkid = jspectra[ich,freq_dc,:]<=0
1072 1070 cjunkid = sum(junkid)
1073 1071
1074 1072 if cjunkid.any():
1075 1073 jspectra[ich,freq_dc,junkid.nonzero()] = (jspectra[ich,ind_vel[1],junkid] + jspectra[ich,ind_vel[2],junkid])/2
1076 1074
1077 1075 noise = jspectra0[:,freq_dc,:] - jspectra[:,freq_dc,:]
1078 1076
1079 1077 return noise
1080 1078
1081 1079 def getTimeInterval(self):
1082 1080
1083 1081 timeInterval = self.ippSeconds * self.nCohInt * self.nProfiles
1084 1082
1085 1083 return timeInterval
1086 1084
1087 1085 def splitFunctions(self):
1088 1086
1089 1087 pairsList = self.pairsList
1090 1088 ccf_pairs = []
1091 1089 acf_pairs = []
1092 1090 ccf_ind = []
1093 1091 acf_ind = []
1094 1092 for l in range(len(pairsList)):
1095 1093 chan0 = pairsList[l][0]
1096 1094 chan1 = pairsList[l][1]
1097 1095
1098 1096 #Obteniendo pares de Autocorrelacion
1099 1097 if chan0 == chan1:
1100 1098 acf_pairs.append(chan0)
1101 1099 acf_ind.append(l)
1102 1100 else:
1103 1101 ccf_pairs.append(pairsList[l])
1104 1102 ccf_ind.append(l)
1105 1103
1106 1104 data_acf = self.data_cf[acf_ind]
1107 1105 data_ccf = self.data_cf[ccf_ind]
1108 1106
1109 1107 return acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf
1110 1108
1111 1109 def getNormFactor(self):
1112 1110 acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf = self.splitFunctions()
1113 1111 acf_pairs = numpy.array(acf_pairs)
1114 1112 normFactor = numpy.zeros((self.nPairs,self.nHeights))
1115 1113
1116 1114 for p in range(self.nPairs):
1117 1115 pair = self.pairsList[p]
1118 1116
1119 1117 ch0 = pair[0]
1120 1118 ch1 = pair[1]
1121 1119
1122 1120 ch0_max = numpy.max(data_acf[acf_pairs==ch0,:,:], axis=1)
1123 1121 ch1_max = numpy.max(data_acf[acf_pairs==ch1,:,:], axis=1)
1124 1122 normFactor[p,:] = numpy.sqrt(ch0_max*ch1_max)
1125 1123
1126 1124 return normFactor
1127 1125
1128 1126 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
1129 1127 normFactor = property(getNormFactor, "I'm the 'normFactor property'")
1130 1128
1131 1129 class Parameters(Spectra):
1132 1130
1133 1131 experimentInfo = None #Information about the experiment
1134 1132
1135 1133 #Information from previous data
1136 1134
1137 1135 inputUnit = None #Type of data to be processed
1138 1136
1139 1137 operation = None #Type of operation to parametrize
1140 1138
1141 1139 #normFactor = None #Normalization Factor
1142 1140
1143 1141 groupList = None #List of Pairs, Groups, etc
1144 1142
1145 1143 #Parameters
1146 1144
1147 1145 data_param = None #Parameters obtained
1148 1146
1149 1147 data_pre = None #Data Pre Parametrization
1150 1148
1151 1149 data_SNR = None #Signal to Noise Ratio
1152 1150
1153 1151 # heightRange = None #Heights
1154 1152
1155 1153 abscissaList = None #Abscissa, can be velocities, lags or time
1156 1154
1157 1155 # noise = None #Noise Potency
1158 1156
1159 1157 utctimeInit = None #Initial UTC time
1160 1158
1161 1159 paramInterval = None #Time interval to calculate Parameters in seconds
1162 1160
1163 1161 useLocalTime = True
1164 1162
1165 1163 #Fitting
1166 1164
1167 1165 data_error = None #Error of the estimation
1168 1166
1169 1167 constants = None
1170 1168
1171 1169 library = None
1172 1170
1173 1171 #Output signal
1174 1172
1175 1173 outputInterval = None #Time interval to calculate output signal in seconds
1176 1174
1177 1175 data_output = None #Out signal
1178 1176
1179 1177 nAvg = None
1180 1178
1181 1179 noise_estimation = None
1182 1180
1183 1181 GauSPC = None #Fit gaussian SPC
1184 1182
1185 1183
1186 1184 def __init__(self):
1187 1185 '''
1188 1186 Constructor
1189 1187 '''
1190 1188 self.radarControllerHeaderObj = RadarControllerHeader()
1191 1189
1192 1190 self.systemHeaderObj = SystemHeader()
1193 1191
1194 1192 self.type = "Parameters"
1195 1193
1196 1194 def getTimeRange1(self, interval):
1197 1195
1198 1196 datatime = []
1199 1197
1200 1198 if self.useLocalTime:
1201 1199 time1 = self.utctimeInit - self.timeZone*60
1202 1200 else:
1203 1201 time1 = self.utctimeInit
1204 1202
1205 1203 datatime.append(time1)
1206 1204 datatime.append(time1 + interval)
1207 1205 datatime = numpy.array(datatime)
1208 1206
1209 1207 return datatime
1210 1208
1211 1209 def getTimeInterval(self):
1212 1210
1213 1211 if hasattr(self, 'timeInterval1'):
1214 1212 return self.timeInterval1
1215 1213 else:
1216 1214 return self.paramInterval
1217 1215
1218 1216 def setValue(self, value):
1219 1217
1220 1218 print "This property should not be initialized"
1221 1219
1222 1220 return
1223 1221
1224 1222 def getNoise(self):
1225 1223
1226 1224 return self.spc_noise
1227 1225
1228 1226 timeInterval = property(getTimeInterval)
1229 1227 noise = property(getNoise, setValue, "I'm the 'Noise' property.")
@@ -1,851 +1,876
1 1 '''
2 2
3 3 $Author: murco $
4 4 $Id: JROHeaderIO.py 151 2012-10-31 19:00:51Z murco $
5 5 '''
6 6 import sys
7 7 import numpy
8 8 import copy
9 9 import datetime
10 import inspect
10 11
11 12 SPEED_OF_LIGHT = 299792458
12 13 SPEED_OF_LIGHT = 3e8
13 14
14 15 BASIC_STRUCTURE = numpy.dtype([
15 16 ('nSize','<u4'),
16 17 ('nVersion','<u2'),
17 18 ('nDataBlockId','<u4'),
18 19 ('nUtime','<u4'),
19 20 ('nMilsec','<u2'),
20 21 ('nTimezone','<i2'),
21 22 ('nDstflag','<i2'),
22 23 ('nErrorCount','<u4')
23 24 ])
24 25
25 26 SYSTEM_STRUCTURE = numpy.dtype([
26 27 ('nSize','<u4'),
27 28 ('nNumSamples','<u4'),
28 29 ('nNumProfiles','<u4'),
29 30 ('nNumChannels','<u4'),
30 31 ('nADCResolution','<u4'),
31 32 ('nPCDIOBusWidth','<u4'),
32 33 ])
33 34
34 35 RADAR_STRUCTURE = numpy.dtype([
35 36 ('nSize','<u4'),
36 37 ('nExpType','<u4'),
37 38 ('nNTx','<u4'),
38 39 ('fIpp','<f4'),
39 40 ('fTxA','<f4'),
40 41 ('fTxB','<f4'),
41 42 ('nNumWindows','<u4'),
42 43 ('nNumTaus','<u4'),
43 44 ('nCodeType','<u4'),
44 45 ('nLine6Function','<u4'),
45 46 ('nLine5Function','<u4'),
46 47 ('fClock','<f4'),
47 48 ('nPrePulseBefore','<u4'),
48 49 ('nPrePulseAfter','<u4'),
49 50 ('sRangeIPP','<a20'),
50 51 ('sRangeTxA','<a20'),
51 52 ('sRangeTxB','<a20'),
52 53 ])
53 54
54 55 SAMPLING_STRUCTURE = numpy.dtype([('h0','<f4'),('dh','<f4'),('nsa','<u4')])
55 56
56 57
57 58 PROCESSING_STRUCTURE = numpy.dtype([
58 59 ('nSize','<u4'),
59 60 ('nDataType','<u4'),
60 61 ('nSizeOfDataBlock','<u4'),
61 62 ('nProfilesperBlock','<u4'),
62 63 ('nDataBlocksperFile','<u4'),
63 64 ('nNumWindows','<u4'),
64 65 ('nProcessFlags','<u4'),
65 66 ('nCoherentIntegrations','<u4'),
66 67 ('nIncoherentIntegrations','<u4'),
67 68 ('nTotalSpectra','<u4')
68 69 ])
69 70
70 71 class Header(object):
71 72
72 73 def __init__(self):
73 74 raise NotImplementedError
74 75
75 76 def copy(self):
76 77 return copy.deepcopy(self)
77 78
78 79 def read(self):
79 80
80 81 raise NotImplementedError
81 82
82 83 def write(self):
83 84
84 85 raise NotImplementedError
86
87 def getAllowedArgs(self):
88 args = inspect.getargspec(self.__init__).args
89 try:
90 args.remove('self')
91 except:
92 pass
93 return args
94
95 def getAsDict(self):
96 args = self.getAllowedArgs()
97 asDict = {}
98 for x in args:
99 asDict[x] = self[x]
100 return asDict
85 101
102 def __getitem__(self, name):
103 return getattr(self, name)
104
86 105 def printInfo(self):
87 106
88 107 message = "#"*50 + "\n"
89 108 message += self.__class__.__name__.upper() + "\n"
90 109 message += "#"*50 + "\n"
91 110
92 111 keyList = self.__dict__.keys()
93 112 keyList.sort()
94 113
95 114 for key in keyList:
96 115 message += "%s = %s" %(key, self.__dict__[key]) + "\n"
97 116
98 117 if "size" not in keyList:
99 118 attr = getattr(self, "size")
100 119
101 120 if attr:
102 121 message += "%s = %s" %("size", attr) + "\n"
103 122
104 123 print message
105 124
106 125 class BasicHeader(Header):
107 126
108 127 size = None
109 128 version = None
110 129 dataBlock = None
111 130 utc = None
112 131 ltc = None
113 132 miliSecond = None
114 133 timeZone = None
115 134 dstFlag = None
116 135 errorCount = None
117 136 datatime = None
137 structure = BASIC_STRUCTURE
118 138 __LOCALTIME = None
119 139
120 140 def __init__(self, useLocalTime=True):
121 141
122 142 self.size = 24
123 143 self.version = 0
124 144 self.dataBlock = 0
125 145 self.utc = 0
126 146 self.miliSecond = 0
127 147 self.timeZone = 0
128 148 self.dstFlag = 0
129 149 self.errorCount = 0
130 150
131 151 self.useLocalTime = useLocalTime
132 152
133 153 def read(self, fp):
134 154
135 155 self.length = 0
136 156 try:
137 157 if hasattr(fp, 'read'):
138 158 header = numpy.fromfile(fp, BASIC_STRUCTURE,1)
139 159 else:
140 160 header = numpy.fromstring(fp, BASIC_STRUCTURE,1)
141 161 except Exception, e:
142 162 print "BasicHeader: "
143 163 print e
144 164 return 0
145 165
146 166 self.size = int(header['nSize'][0])
147 167 self.version = int(header['nVersion'][0])
148 168 self.dataBlock = int(header['nDataBlockId'][0])
149 169 self.utc = int(header['nUtime'][0])
150 170 self.miliSecond = int(header['nMilsec'][0])
151 171 self.timeZone = int(header['nTimezone'][0])
152 172 self.dstFlag = int(header['nDstflag'][0])
153 173 self.errorCount = int(header['nErrorCount'][0])
154 174
155 175 if self.size < 24:
156 176 return 0
157 177
158 178 self.length = header.nbytes
159 179 return 1
160 180
161 181 def write(self, fp):
162 182
163 183 headerTuple = (self.size,self.version,self.dataBlock,self.utc,self.miliSecond,self.timeZone,self.dstFlag,self.errorCount)
164 184 header = numpy.array(headerTuple, BASIC_STRUCTURE)
165 185 header.tofile(fp)
166 186
167 187 return 1
168 188
169 189 def get_ltc(self):
170 190
171 191 return self.utc - self.timeZone*60
172 192
173 193 def set_ltc(self, value):
174 194
175 195 self.utc = value + self.timeZone*60
176 196
177 197 def get_datatime(self):
178 198
179 199 return datetime.datetime.utcfromtimestamp(self.ltc)
180 200
181 201 ltc = property(get_ltc, set_ltc)
182 202 datatime = property(get_datatime)
183 203
184 204 class SystemHeader(Header):
185 205
186 206 size = None
187 207 nSamples = None
188 208 nProfiles = None
189 209 nChannels = None
190 210 adcResolution = None
191 211 pciDioBusWidth = None
192
193 def __init__(self, nSamples=0, nProfiles=0, nChannels=0, adcResolution=14, pciDioBusWith=0):
212 structure = SYSTEM_STRUCTURE
213
214 def __init__(self, nSamples=0, nProfiles=0, nChannels=0, adcResolution=14, pciDioBusWidth=0):
194 215
195 216 self.size = 24
196 217 self.nSamples = nSamples
197 218 self.nProfiles = nProfiles
198 219 self.nChannels = nChannels
199 220 self.adcResolution = adcResolution
200 self.pciDioBusWidth = pciDioBusWith
201
221 self.pciDioBusWidth = pciDioBusWidth
222
202 223 def read(self, fp):
203 224 self.length = 0
204 225 try:
205 226 startFp = fp.tell()
206 227 except Exception, e:
207 228 startFp = None
208 229 pass
209 230
210 231 try:
211 232 if hasattr(fp, 'read'):
212 233 header = numpy.fromfile(fp, SYSTEM_STRUCTURE,1)
213 234 else:
214 235 header = numpy.fromstring(fp, SYSTEM_STRUCTURE,1)
215 236 except Exception, e:
216 237 print "System Header: " + str(e)
217 238 return 0
218 239
219 240 self.size = header['nSize'][0]
220 241 self.nSamples = header['nNumSamples'][0]
221 242 self.nProfiles = header['nNumProfiles'][0]
222 243 self.nChannels = header['nNumChannels'][0]
223 244 self.adcResolution = header['nADCResolution'][0]
224 245 self.pciDioBusWidth = header['nPCDIOBusWidth'][0]
225 246
226 247
227 248 if startFp is not None:
228 249 endFp = self.size + startFp
229 250
230 251 if fp.tell() > endFp:
231 252 sys.stderr.write("Warning %s: Size value read from System Header is lower than it has to be\n" %fp.name)
232 253 return 0
233 254
234 255 if fp.tell() < endFp:
235 256 sys.stderr.write("Warning %s: Size value read from System Header size is greater than it has to be\n" %fp.name)
236 257 return 0
237 258
238 259 self.length = header.nbytes
239 260 return 1
240 261
241 262 def write(self, fp):
242 263
243 264 headerTuple = (self.size,self.nSamples,self.nProfiles,self.nChannels,self.adcResolution,self.pciDioBusWidth)
244 265 header = numpy.array(headerTuple,SYSTEM_STRUCTURE)
245 266 header.tofile(fp)
246 267
247 268 return 1
248 269
249 270 class RadarControllerHeader(Header):
250 271
251 272 expType = None
252 273 nTx = None
253 274 ipp = None
254 275 txA = None
255 276 txB = None
256 277 nWindows = None
257 278 numTaus = None
258 279 codeType = None
259 280 line6Function = None
260 281 line5Function = None
261 282 fClock = None
262 283 prePulseBefore = None
263 prePulserAfter = None
284 prePulseAfter = None
264 285 rangeIpp = None
265 286 rangeTxA = None
266 287 rangeTxB = None
267
288 structure = RADAR_STRUCTURE
268 289 __size = None
269 290
270 291 def __init__(self, expType=2, nTx=1,
271 ippKm=None, txA=0, txB=0,
292 ipp=None, txA=0, txB=0,
272 293 nWindows=None, nHeights=None, firstHeight=None, deltaHeight=None,
273 294 numTaus=0, line6Function=0, line5Function=0, fClock=None,
274 295 prePulseBefore=0, prePulseAfter=0,
275 296 codeType=0, nCode=0, nBaud=0, code=None,
276 297 flip1=0, flip2=0):
277 298
278 299 # self.size = 116
279 300 self.expType = expType
280 301 self.nTx = nTx
281 self.ipp = ippKm
302 self.ipp = ipp
282 303 self.txA = txA
283 304 self.txB = txB
284 self.rangeIpp = ippKm
305 self.rangeIpp = ipp
285 306 self.rangeTxA = txA
286 307 self.rangeTxB = txB
287 308
288 309 self.nWindows = nWindows
289 310 self.numTaus = numTaus
290 311 self.codeType = codeType
291 312 self.line6Function = line6Function
292 313 self.line5Function = line5Function
293 314 self.fClock = fClock
294 315 self.prePulseBefore = prePulseBefore
295 self.prePulserAfter = prePulseAfter
316 self.prePulseAfter = prePulseAfter
296 317
297 318 self.nHeights = nHeights
298 319 self.firstHeight = firstHeight
299 320 self.deltaHeight = deltaHeight
300 321 self.samplesWin = nHeights
301 322
302 323 self.nCode = nCode
303 324 self.nBaud = nBaud
304 325 self.code = code
305 326 self.flip1 = flip1
306 327 self.flip2 = flip2
307 328
308 329 self.code_size = int(numpy.ceil(self.nBaud/32.))*self.nCode*4
309 330 # self.dynamic = numpy.array([],numpy.dtype('byte'))
310 331
311 332 if self.fClock is None and self.deltaHeight is not None:
312 333 self.fClock = 0.15/(deltaHeight*1e-6) #0.15Km / (height * 1u)
313 334
314 335 def read(self, fp):
315 336 self.length = 0
316 337 try:
317 338 startFp = fp.tell()
318 339 except Exception, e:
319 340 startFp = None
320 341 pass
321 342
322 343 try:
323 344 if hasattr(fp, 'read'):
324 345 header = numpy.fromfile(fp, RADAR_STRUCTURE,1)
325 346 else:
326 347 header = numpy.fromstring(fp, RADAR_STRUCTURE,1)
327 348 self.length += header.nbytes
328 349 except Exception, e:
329 350 print "RadarControllerHeader: " + str(e)
330 351 return 0
331 352
332 353 size = int(header['nSize'][0])
333 354 self.expType = int(header['nExpType'][0])
334 355 self.nTx = int(header['nNTx'][0])
335 356 self.ipp = float(header['fIpp'][0])
336 357 self.txA = float(header['fTxA'][0])
337 358 self.txB = float(header['fTxB'][0])
338 359 self.nWindows = int(header['nNumWindows'][0])
339 360 self.numTaus = int(header['nNumTaus'][0])
340 361 self.codeType = int(header['nCodeType'][0])
341 362 self.line6Function = int(header['nLine6Function'][0])
342 363 self.line5Function = int(header['nLine5Function'][0])
343 364 self.fClock = float(header['fClock'][0])
344 365 self.prePulseBefore = int(header['nPrePulseBefore'][0])
345 self.prePulserAfter = int(header['nPrePulseAfter'][0])
366 self.prePulseAfter = int(header['nPrePulseAfter'][0])
346 367 self.rangeIpp = header['sRangeIPP'][0]
347 368 self.rangeTxA = header['sRangeTxA'][0]
348 369 self.rangeTxB = header['sRangeTxB'][0]
349 370
350 371 try:
351 372 if hasattr(fp, 'read'):
352 373 samplingWindow = numpy.fromfile(fp, SAMPLING_STRUCTURE, self.nWindows)
353 374 else:
354 375 samplingWindow = numpy.fromstring(fp[self.length:], SAMPLING_STRUCTURE, self.nWindows)
355 376 self.length += samplingWindow.nbytes
356 377 except Exception, e:
357 378 print "RadarControllerHeader: " + str(e)
358 379 return 0
359 380 self.nHeights = int(numpy.sum(samplingWindow['nsa']))
360 381 self.firstHeight = samplingWindow['h0']
361 382 self.deltaHeight = samplingWindow['dh']
362 383 self.samplesWin = samplingWindow['nsa']
363 384
364 385
365 386
366 387 try:
367 388 if hasattr(fp, 'read'):
368 389 self.Taus = numpy.fromfile(fp, '<f4', self.numTaus)
369 390 else:
370 391 self.Taus = numpy.fromstring(fp[self.length:], '<f4', self.numTaus)
371 392 self.length += self.Taus.nbytes
372 393 except Exception, e:
373 394 print "RadarControllerHeader: " + str(e)
374 395 return 0
375 396
376 397
377 398
378 399 self.code_size = 0
379 400 if self.codeType != 0:
380 401
381 402 try:
382 403 if hasattr(fp, 'read'):
383 404 self.nCode = numpy.fromfile(fp, '<u4', 1)[0]
384 405 self.length += self.nCode.nbytes
385 406 self.nBaud = numpy.fromfile(fp, '<u4', 1)[0]
386 407 self.length += self.nBaud.nbytes
387 408 else:
388 409 self.nCode = numpy.fromstring(fp[self.length:], '<u4', 1)[0]
389 410 self.length += self.nCode.nbytes
390 411 self.nBaud = numpy.fromstring(fp[self.length:], '<u4', 1)[0]
391 412 self.length += self.nBaud.nbytes
392 413 except Exception, e:
393 414 print "RadarControllerHeader: " + str(e)
394 415 return 0
395 416 code = numpy.empty([self.nCode,self.nBaud],dtype='i1')
396 417
397 418 for ic in range(self.nCode):
398 419 try:
399 420 if hasattr(fp, 'read'):
400 421 temp = numpy.fromfile(fp,'u4', int(numpy.ceil(self.nBaud/32.)))
401 422 else:
402 423 temp = numpy.fromstring(fp,'u4', int(numpy.ceil(self.nBaud/32.)))
403 424 self.length += temp.nbytes
404 425 except Exception, e:
405 426 print "RadarControllerHeader: " + str(e)
406 427 return 0
407 428
408 429 for ib in range(self.nBaud-1,-1,-1):
409 430 code[ic,ib] = temp[ib/32]%2
410 431 temp[ib/32] = temp[ib/32]/2
411 432
412 433 self.code = 2.0*code - 1.0
413 434 self.code_size = int(numpy.ceil(self.nBaud/32.))*self.nCode*4
414 435
415 436 # if self.line5Function == RCfunction.FLIP:
416 437 # self.flip1 = numpy.fromfile(fp,'<u4',1)
417 438 #
418 439 # if self.line6Function == RCfunction.FLIP:
419 440 # self.flip2 = numpy.fromfile(fp,'<u4',1)
420 441 if startFp is not None:
421 442 endFp = size + startFp
422 443
423 444 if fp.tell() != endFp:
424 445 # fp.seek(endFp)
425 446 print "%s: Radar Controller Header size is not consistent: from data [%d] != from header field [%d]" %(fp.name, fp.tell()-startFp, size)
426 447 # return 0
427 448
428 449 if fp.tell() > endFp:
429 450 sys.stderr.write("Warning %s: Size value read from Radar Controller header is lower than it has to be\n" %fp.name)
430 451 # return 0
431 452
432 453 if fp.tell() < endFp:
433 454 sys.stderr.write("Warning %s: Size value read from Radar Controller header is greater than it has to be\n" %fp.name)
434 455
435 456
436 457 return 1
437 458
438 459 def write(self, fp):
439 460
440 461 headerTuple = (self.size,
441 462 self.expType,
442 463 self.nTx,
443 464 self.ipp,
444 465 self.txA,
445 466 self.txB,
446 467 self.nWindows,
447 468 self.numTaus,
448 469 self.codeType,
449 470 self.line6Function,
450 471 self.line5Function,
451 472 self.fClock,
452 473 self.prePulseBefore,
453 self.prePulserAfter,
474 self.prePulseAfter,
454 475 self.rangeIpp,
455 476 self.rangeTxA,
456 477 self.rangeTxB)
457 478
458 479 header = numpy.array(headerTuple,RADAR_STRUCTURE)
459 480 header.tofile(fp)
460 481
461 482 sampleWindowTuple = (self.firstHeight,self.deltaHeight,self.samplesWin)
462 483 samplingWindow = numpy.array(sampleWindowTuple,SAMPLING_STRUCTURE)
463 484 samplingWindow.tofile(fp)
464 485
465 486 if self.numTaus > 0:
466 487 self.Taus.tofile(fp)
467 488
468 489 if self.codeType !=0:
469 490 nCode = numpy.array(self.nCode, '<u4')
470 491 nCode.tofile(fp)
471 492 nBaud = numpy.array(self.nBaud, '<u4')
472 493 nBaud.tofile(fp)
473 494 code1 = (self.code + 1.0)/2.
474 495
475 496 for ic in range(self.nCode):
476 497 tempx = numpy.zeros(numpy.ceil(self.nBaud/32.))
477 498 start = 0
478 499 end = 32
479 500 for i in range(len(tempx)):
480 501 code_selected = code1[ic,start:end]
481 502 for j in range(len(code_selected)-1,-1,-1):
482 503 if code_selected[j] == 1:
483 504 tempx[i] = tempx[i] + 2**(len(code_selected)-1-j)
484 505 start = start + 32
485 506 end = end + 32
486 507
487 508 tempx = tempx.astype('u4')
488 509 tempx.tofile(fp)
489 510
490 511 # if self.line5Function == RCfunction.FLIP:
491 512 # self.flip1.tofile(fp)
492 513 #
493 514 # if self.line6Function == RCfunction.FLIP:
494 515 # self.flip2.tofile(fp)
495 516
496 517 return 1
497 518
498 519 def get_ippSeconds(self):
499 520 '''
500 521 '''
501 522 ippSeconds = 2.0 * 1000 * self.ipp / SPEED_OF_LIGHT
502 523
503 524 return ippSeconds
504 525
505 526 def set_ippSeconds(self, ippSeconds):
506 527 '''
507 528 '''
508 529
509 530 self.ipp = ippSeconds * SPEED_OF_LIGHT / (2.0*1000)
510 531
511 532 return
512 533
513 534 def get_size(self):
514 535
515 536 self.__size = 116 + 12*self.nWindows + 4*self.numTaus
516 537
517 538 if self.codeType != 0:
518 539 self.__size += 4 + 4 + 4*self.nCode*numpy.ceil(self.nBaud/32.)
519 540
520 541 return self.__size
521 542
522 543 def set_size(self, value):
523 544
524 545 raise IOError, "size is a property and it cannot be set, just read"
525 546
526 547 return
527 548
528 549 ippSeconds = property(get_ippSeconds, set_ippSeconds)
529 550 size = property(get_size, set_size)
530 551
531 552 class ProcessingHeader(Header):
532 553
533 554 # size = None
534 555 dtype = None
535 556 blockSize = None
536 557 profilesPerBlock = None
537 558 dataBlocksPerFile = None
538 559 nWindows = None
539 560 processFlags = None
540 561 nCohInt = None
541 562 nIncohInt = None
542 563 totalSpectra = None
543
564 structure = PROCESSING_STRUCTURE
544 565 flag_dc = None
545 566 flag_cspc = None
546 567
547 def __init__(self):
568 def __init__(self, dtype=0, blockSize=0, profilesPerBlock=0, dataBlocksPerFile=0, nWindows=0,processFlags=0, nCohInt=0,
569 nIncohInt=0, totalSpectra=0, nHeights=0, firstHeight=0, deltaHeight=0, samplesWin=0, spectraComb=0, nCode=0,
570 code=0, nBaud=None, shif_fft=False, flag_dc=False, flag_cspc=False, flag_decode=False, flag_deflip=False
571 ):
548 572
549 573 # self.size = 0
550 self.dtype = 0
551 self.blockSize = 0
574 self.dtype = dtype
575 self.blockSize = blockSize
552 576 self.profilesPerBlock = 0
553 577 self.dataBlocksPerFile = 0
554 578 self.nWindows = 0
555 579 self.processFlags = 0
556 580 self.nCohInt = 0
557 581 self.nIncohInt = 0
558 582 self.totalSpectra = 0
559 583
560 584 self.nHeights = 0
561 585 self.firstHeight = 0
562 586 self.deltaHeight = 0
563 587 self.samplesWin = 0
564 588 self.spectraComb = 0
565 589 self.nCode = None
566 590 self.code = None
567 591 self.nBaud = None
568 592
569 593 self.shif_fft = False
570 594 self.flag_dc = False
571 595 self.flag_cspc = False
572 596 self.flag_decode = False
573 597 self.flag_deflip = False
574 598 self.length = 0
599
575 600 def read(self, fp):
576 601 self.length = 0
577 602 try:
578 603 startFp = fp.tell()
579 604 except Exception, e:
580 605 startFp = None
581 606 pass
582 607
583 608 try:
584 609 if hasattr(fp, 'read'):
585 610 header = numpy.fromfile(fp, PROCESSING_STRUCTURE, 1)
586 611 else:
587 612 header = numpy.fromstring(fp, PROCESSING_STRUCTURE, 1)
588 613 self.length += header.nbytes
589 614 except Exception, e:
590 615 print "ProcessingHeader: " + str(e)
591 616 return 0
592 617
593 618 size = int(header['nSize'][0])
594 619 self.dtype = int(header['nDataType'][0])
595 620 self.blockSize = int(header['nSizeOfDataBlock'][0])
596 621 self.profilesPerBlock = int(header['nProfilesperBlock'][0])
597 622 self.dataBlocksPerFile = int(header['nDataBlocksperFile'][0])
598 623 self.nWindows = int(header['nNumWindows'][0])
599 624 self.processFlags = header['nProcessFlags']
600 625 self.nCohInt = int(header['nCoherentIntegrations'][0])
601 626 self.nIncohInt = int(header['nIncoherentIntegrations'][0])
602 627 self.totalSpectra = int(header['nTotalSpectra'][0])
603 628
604 629 try:
605 630 if hasattr(fp, 'read'):
606 631 samplingWindow = numpy.fromfile(fp, SAMPLING_STRUCTURE, self.nWindows)
607 632 else:
608 633 samplingWindow = numpy.fromstring(fp[self.length:], SAMPLING_STRUCTURE, self.nWindows)
609 634 self.length += samplingWindow.nbytes
610 635 except Exception, e:
611 636 print "ProcessingHeader: " + str(e)
612 637 return 0
613 638
614 639 self.nHeights = int(numpy.sum(samplingWindow['nsa']))
615 640 self.firstHeight = float(samplingWindow['h0'][0])
616 641 self.deltaHeight = float(samplingWindow['dh'][0])
617 642 self.samplesWin = samplingWindow['nsa'][0]
618 643
619 644
620 645 try:
621 646 if hasattr(fp, 'read'):
622 647 self.spectraComb = numpy.fromfile(fp, 'u1', 2*self.totalSpectra)
623 648 else:
624 649 self.spectraComb = numpy.fromstring(fp[self.length:], 'u1', 2*self.totalSpectra)
625 650 self.length += self.spectraComb.nbytes
626 651 except Exception, e:
627 652 print "ProcessingHeader: " + str(e)
628 653 return 0
629 654
630 655 if ((self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE) == PROCFLAG.DEFINE_PROCESS_CODE):
631 656 self.nCode = int(numpy.fromfile(fp,'<u4',1))
632 657 self.nBaud = int(numpy.fromfile(fp,'<u4',1))
633 658 self.code = numpy.fromfile(fp,'<f4',self.nCode*self.nBaud).reshape(self.nCode,self.nBaud)
634 659
635 660 if ((self.processFlags & PROCFLAG.EXP_NAME_ESP) == PROCFLAG.EXP_NAME_ESP):
636 661 exp_name_len = int(numpy.fromfile(fp,'<u4',1))
637 662 exp_name = numpy.fromfile(fp,'u1',exp_name_len+1)
638 663
639 664 if ((self.processFlags & PROCFLAG.SHIFT_FFT_DATA) == PROCFLAG.SHIFT_FFT_DATA):
640 665 self.shif_fft = True
641 666 else:
642 667 self.shif_fft = False
643 668
644 669 if ((self.processFlags & PROCFLAG.SAVE_CHANNELS_DC) == PROCFLAG.SAVE_CHANNELS_DC):
645 670 self.flag_dc = True
646 671 else:
647 672 self.flag_dc = False
648 673
649 674 if ((self.processFlags & PROCFLAG.DECODE_DATA) == PROCFLAG.DECODE_DATA):
650 675 self.flag_decode = True
651 676 else:
652 677 self.flag_decode = False
653 678
654 679 if ((self.processFlags & PROCFLAG.DEFLIP_DATA) == PROCFLAG.DEFLIP_DATA):
655 680 self.flag_deflip = True
656 681 else:
657 682 self.flag_deflip = False
658 683
659 684 nChannels = 0
660 685 nPairs = 0
661 686 pairList = []
662 687
663 688 for i in range( 0, self.totalSpectra*2, 2 ):
664 689 if self.spectraComb[i] == self.spectraComb[i+1]:
665 690 nChannels = nChannels + 1 #par de canales iguales
666 691 else:
667 692 nPairs = nPairs + 1 #par de canales diferentes
668 693 pairList.append( (self.spectraComb[i], self.spectraComb[i+1]) )
669 694
670 695 self.flag_cspc = False
671 696 if nPairs > 0:
672 697 self.flag_cspc = True
673 698
674 699
675 700
676 701 if startFp is not None:
677 702 endFp = size + startFp
678 703 if fp.tell() > endFp:
679 704 sys.stderr.write("Warning: Processing header size is lower than it has to be")
680 705 return 0
681 706
682 707 if fp.tell() < endFp:
683 708 sys.stderr.write("Warning: Processing header size is greater than it is considered")
684 709
685 710 return 1
686 711
687 712 def write(self, fp):
688 713 #Clear DEFINE_PROCESS_CODE
689 714 self.processFlags = self.processFlags & (~PROCFLAG.DEFINE_PROCESS_CODE)
690 715
691 716 headerTuple = (self.size,
692 717 self.dtype,
693 718 self.blockSize,
694 719 self.profilesPerBlock,
695 720 self.dataBlocksPerFile,
696 721 self.nWindows,
697 722 self.processFlags,
698 723 self.nCohInt,
699 724 self.nIncohInt,
700 725 self.totalSpectra)
701 726
702 727 header = numpy.array(headerTuple,PROCESSING_STRUCTURE)
703 728 header.tofile(fp)
704 729
705 730 if self.nWindows != 0:
706 731 sampleWindowTuple = (self.firstHeight,self.deltaHeight,self.samplesWin)
707 732 samplingWindow = numpy.array(sampleWindowTuple,SAMPLING_STRUCTURE)
708 733 samplingWindow.tofile(fp)
709 734
710 735 if self.totalSpectra != 0:
711 736 # spectraComb = numpy.array([],numpy.dtype('u1'))
712 737 spectraComb = self.spectraComb
713 738 spectraComb.tofile(fp)
714 739
715 740 # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
716 741 # nCode = numpy.array([self.nCode], numpy.dtype('u4')) #Probar con un dato que almacene codigo, hasta el momento no se hizo la prueba
717 742 # nCode.tofile(fp)
718 743 #
719 744 # nBaud = numpy.array([self.nBaud], numpy.dtype('u4'))
720 745 # nBaud.tofile(fp)
721 746 #
722 747 # code = self.code.reshape(self.nCode*self.nBaud)
723 748 # code = code.astype(numpy.dtype('<f4'))
724 749 # code.tofile(fp)
725 750
726 751 return 1
727 752
728 753 def get_size(self):
729 754
730 755 self.__size = 40 + 12*self.nWindows + 2*self.totalSpectra
731 756
732 757 # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
733 758 # self.__size += 4 + 4 + 4*self.nCode*numpy.ceil(self.nBaud/32.)
734 759 # self.__size += 4 + 4 + 4 * self.nCode * self.nBaud
735 760
736 761 return self.__size
737 762
738 763 def set_size(self, value):
739 764
740 765 raise IOError, "size is a property and it cannot be set, just read"
741 766
742 767 return
743 768
744 769 size = property(get_size, set_size)
745 770
746 771 class RCfunction:
747 772 NONE=0
748 773 FLIP=1
749 774 CODE=2
750 775 SAMPLING=3
751 776 LIN6DIV256=4
752 777 SYNCHRO=5
753 778
754 779 class nCodeType:
755 780 NONE=0
756 781 USERDEFINE=1
757 782 BARKER2=2
758 783 BARKER3=3
759 784 BARKER4=4
760 785 BARKER5=5
761 786 BARKER7=6
762 787 BARKER11=7
763 788 BARKER13=8
764 789 AC128=9
765 790 COMPLEMENTARYCODE2=10
766 791 COMPLEMENTARYCODE4=11
767 792 COMPLEMENTARYCODE8=12
768 793 COMPLEMENTARYCODE16=13
769 794 COMPLEMENTARYCODE32=14
770 795 COMPLEMENTARYCODE64=15
771 796 COMPLEMENTARYCODE128=16
772 797 CODE_BINARY28=17
773 798
774 799 class PROCFLAG:
775 800
776 801 COHERENT_INTEGRATION = numpy.uint32(0x00000001)
777 802 DECODE_DATA = numpy.uint32(0x00000002)
778 803 SPECTRA_CALC = numpy.uint32(0x00000004)
779 804 INCOHERENT_INTEGRATION = numpy.uint32(0x00000008)
780 805 POST_COHERENT_INTEGRATION = numpy.uint32(0x00000010)
781 806 SHIFT_FFT_DATA = numpy.uint32(0x00000020)
782 807
783 808 DATATYPE_CHAR = numpy.uint32(0x00000040)
784 809 DATATYPE_SHORT = numpy.uint32(0x00000080)
785 810 DATATYPE_LONG = numpy.uint32(0x00000100)
786 811 DATATYPE_INT64 = numpy.uint32(0x00000200)
787 812 DATATYPE_FLOAT = numpy.uint32(0x00000400)
788 813 DATATYPE_DOUBLE = numpy.uint32(0x00000800)
789 814
790 815 DATAARRANGE_CONTIGUOUS_CH = numpy.uint32(0x00001000)
791 816 DATAARRANGE_CONTIGUOUS_H = numpy.uint32(0x00002000)
792 817 DATAARRANGE_CONTIGUOUS_P = numpy.uint32(0x00004000)
793 818
794 819 SAVE_CHANNELS_DC = numpy.uint32(0x00008000)
795 820 DEFLIP_DATA = numpy.uint32(0x00010000)
796 821 DEFINE_PROCESS_CODE = numpy.uint32(0x00020000)
797 822
798 823 ACQ_SYS_NATALIA = numpy.uint32(0x00040000)
799 824 ACQ_SYS_ECHOTEK = numpy.uint32(0x00080000)
800 825 ACQ_SYS_ADRXD = numpy.uint32(0x000C0000)
801 826 ACQ_SYS_JULIA = numpy.uint32(0x00100000)
802 827 ACQ_SYS_XXXXXX = numpy.uint32(0x00140000)
803 828
804 829 EXP_NAME_ESP = numpy.uint32(0x00200000)
805 830 CHANNEL_NAMES_ESP = numpy.uint32(0x00400000)
806 831
807 832 OPERATION_MASK = numpy.uint32(0x0000003F)
808 833 DATATYPE_MASK = numpy.uint32(0x00000FC0)
809 834 DATAARRANGE_MASK = numpy.uint32(0x00007000)
810 835 ACQ_SYS_MASK = numpy.uint32(0x001C0000)
811 836
812 837 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
813 838 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
814 839 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
815 840 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
816 841 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
817 842 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
818 843
819 844 NUMPY_DTYPE_LIST = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
820 845
821 846 PROCFLAG_DTYPE_LIST = [PROCFLAG.DATATYPE_CHAR,
822 847 PROCFLAG.DATATYPE_SHORT,
823 848 PROCFLAG.DATATYPE_LONG,
824 849 PROCFLAG.DATATYPE_INT64,
825 850 PROCFLAG.DATATYPE_FLOAT,
826 851 PROCFLAG.DATATYPE_DOUBLE]
827 852
828 853 DTYPE_WIDTH = [1, 2, 4, 8, 4, 8]
829 854
830 855 def get_dtype_index(numpy_dtype):
831 856
832 857 index = None
833 858
834 859 for i in range(len(NUMPY_DTYPE_LIST)):
835 860 if numpy_dtype == NUMPY_DTYPE_LIST[i]:
836 861 index = i
837 862 break
838 863
839 864 return index
840 865
841 866 def get_numpy_dtype(index):
842 867
843 868 return NUMPY_DTYPE_LIST[index]
844 869
845 870 def get_procflag_dtype(index):
846 871
847 872 return PROCFLAG_DTYPE_LIST[index]
848 873
849 874 def get_dtype_width(index):
850 875
851 876 return DTYPE_WIDTH[index] No newline at end of file
@@ -1,187 +1,187
1 1 import os
2 2 import datetime
3 3 import numpy
4 4 import copy
5 5 from schainpy.model import *
6 6 from figure import Figure, isRealtime
7 7
8 8 class CorrelationPlot(Figure):
9 9 isConfig = None
10 10 __nsubplots = None
11
11
12 12 WIDTHPROF = None
13 13 HEIGHTPROF = None
14 14 PREFIX = 'corr'
15
16 def __init__(self):
17
15
16 def __init__(self, **kwargs):
17 Figure.__init__(self, **kwargs)
18 18 self.isConfig = False
19 19 self.__nsubplots = 1
20
20
21 21 self.WIDTH = 280
22 22 self.HEIGHT = 250
23 23 self.WIDTHPROF = 120
24 24 self.HEIGHTPROF = 0
25 25 self.counter_imagwr = 0
26
26
27 27 self.PLOT_CODE = 1
28 28 self.FTP_WEI = None
29 29 self.EXP_CODE = None
30 30 self.SUB_EXP_CODE = None
31 31 self.PLOT_POS = None
32
32
33 33 def getSubplots(self):
34
34
35 35 ncol = int(numpy.sqrt(self.nplots)+0.9)
36 36 nrow = int(self.nplots*1./ncol + 0.9)
37
37
38 38 return nrow, ncol
39
39
40 40 def setup(self, id, nplots, wintitle, showprofile=False, show=True):
41
42 showprofile = False
41
42 showprofile = False
43 43 self.__showprofile = showprofile
44 44 self.nplots = nplots
45
45
46 46 ncolspan = 1
47 47 colspan = 1
48 48 if showprofile:
49 49 ncolspan = 3
50 50 colspan = 2
51 51 self.__nsubplots = 2
52
52
53 53 self.createFigure(id = id,
54 54 wintitle = wintitle,
55 55 widthplot = self.WIDTH + self.WIDTHPROF,
56 56 heightplot = self.HEIGHT + self.HEIGHTPROF,
57 57 show=show)
58
58
59 59 nrow, ncol = self.getSubplots()
60
60
61 61 counter = 0
62 62 for y in range(nrow):
63 63 for x in range(ncol):
64
64
65 65 if counter >= self.nplots:
66 66 break
67
67
68 68 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
69
69
70 70 if showprofile:
71 71 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
72
72
73 73 counter += 1
74
74
75 75 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=False,
76 76 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
77 77 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
78 78 server=None, folder=None, username=None, password=None,
79 79 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False):
80
80
81 81 """
82
82
83 83 Input:
84 84 dataOut :
85 85 id :
86 86 wintitle :
87 87 channelList :
88 88 showProfile :
89 89 xmin : None,
90 90 xmax : None,
91 91 ymin : None,
92 92 ymax : None,
93 93 zmin : None,
94 94 zmax : None
95 95 """
96
96
97 97 if dataOut.flagNoData:
98 98 return None
99
99
100 100 if realtime:
101 101 if not(isRealtime(utcdatatime = dataOut.utctime)):
102 102 print 'Skipping this plot function'
103 103 return
104
104
105 105 if channelList == None:
106 106 channelIndexList = dataOut.channelIndexList
107 107 else:
108 108 channelIndexList = []
109 109 for channel in channelList:
110 110 if channel not in dataOut.channelList:
111 111 raise ValueError, "Channel %d is not in dataOut.channelList"
112 112 channelIndexList.append(dataOut.channelList.index(channel))
113
113
114 114 factor = dataOut.normFactor
115 115 lenfactor = factor.shape[1]
116 116 x = dataOut.getLagTRange(1)
117 117 y = dataOut.getHeiRange()
118
118
119 119 z = copy.copy(dataOut.data_corr[:,:,0,:])
120 120 for i in range(dataOut.data_corr.shape[0]):
121 z[i,:,:] = z[i,:,:]/factor[i,:]
121 z[i,:,:] = z[i,:,:]/factor[i,:]
122 122 zdB = numpy.abs(z)
123
123
124 124 avg = numpy.average(z, axis=1)
125 125 # avg = numpy.nanmean(z, axis=1)
126 126 # noise = dataOut.noise/factor
127
127
128 128 #thisDatetime = dataOut.datatime
129 129 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
130 title = wintitle + " Correlation"
130 title = wintitle + " Correlation"
131 131 xlabel = "Lag T (s)"
132 132 ylabel = "Range (Km)"
133
133
134 134 if not self.isConfig:
135
136 nplots = dataOut.data_corr.shape[0]
137
135
136 nplots = dataOut.data_corr.shape[0]
137
138 138 self.setup(id=id,
139 139 nplots=nplots,
140 140 wintitle=wintitle,
141 141 showprofile=showprofile,
142 142 show=show)
143
143
144 144 if xmin == None: xmin = numpy.nanmin(x)
145 145 if xmax == None: xmax = numpy.nanmax(x)
146 146 if ymin == None: ymin = numpy.nanmin(y)
147 147 if ymax == None: ymax = numpy.nanmax(y)
148 148 if zmin == None: zmin = 0
149 149 if zmax == None: zmax = 1
150
150
151 151 self.FTP_WEI = ftp_wei
152 152 self.EXP_CODE = exp_code
153 153 self.SUB_EXP_CODE = sub_exp_code
154 154 self.PLOT_POS = plot_pos
155
155
156 156 self.isConfig = True
157
157
158 158 self.setWinTitle(title)
159
159
160 160 for i in range(self.nplots):
161 161 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
162 162 title = "Channel %d and %d: : %s" %(dataOut.pairsList[i][0],dataOut.pairsList[i][1] , str_datetime)
163 163 axes = self.axesList[i*self.__nsubplots]
164 164 axes.pcolor(x, y, zdB[i,:,:],
165 165 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
166 166 xlabel=xlabel, ylabel=ylabel, title=title,
167 167 ticksize=9, cblabel='')
168
168
169 169 # if self.__showprofile:
170 170 # axes = self.axesList[i*self.__nsubplots +1]
171 171 # axes.pline(avgdB[i], y,
172 172 # xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
173 173 # xlabel='dB', ylabel='', title='',
174 174 # ytick_visible=False,
175 175 # grid='x')
176 #
176 #
177 177 # noiseline = numpy.repeat(noisedB[i], len(y))
178 178 # axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
179
179
180 180 self.draw()
181
181
182 182 self.save(figpath=figpath,
183 183 figfile=figfile,
184 184 save=save,
185 185 ftp=ftp,
186 186 wr_period=wr_period,
187 thisDatetime=thisDatetime)
187 thisDatetime=thisDatetime)
@@ -1,922 +1,937
1 1
2 2 import os
3 3 import time
4 4 import glob
5 5 import datetime
6 6 from multiprocessing import Process
7 7
8 8 import zmq
9 9 import numpy
10 10 import matplotlib
11 11 import matplotlib.pyplot as plt
12 12 from mpl_toolkits.axes_grid1 import make_axes_locatable
13 13 from matplotlib.ticker import FuncFormatter, LinearLocator, MultipleLocator
14 14
15 15 from schainpy.model.proc.jroproc_base import Operation
16 16 from schainpy.utils import log
17 17
18 18 jet_values = matplotlib.pyplot.get_cmap("jet", 100)(numpy.arange(100))[10:90]
19 blu_values = matplotlib.pyplot.get_cmap("seismic_r", 20)(numpy.arange(20))[10:15]
20 ncmap = matplotlib.colors.LinearSegmentedColormap.from_list("jro", numpy.vstack((blu_values, jet_values)))
19 blu_values = matplotlib.pyplot.get_cmap(
20 "seismic_r", 20)(numpy.arange(20))[10:15]
21 ncmap = matplotlib.colors.LinearSegmentedColormap.from_list(
22 "jro", numpy.vstack((blu_values, jet_values)))
21 23 matplotlib.pyplot.register_cmap(cmap=ncmap)
22 24
23 25 CMAPS = [plt.get_cmap(s) for s in ('jro', 'jet', 'RdBu_r', 'seismic')]
24 26
25 27 def figpause(interval):
26 28 backend = plt.rcParams['backend']
27 29 if backend in matplotlib.rcsetup.interactive_bk:
28 30 figManager = matplotlib._pylab_helpers.Gcf.get_active()
29 31 if figManager is not None:
30 32 canvas = figManager.canvas
31 33 if canvas.figure.stale:
32 34 canvas.draw()
33 35 canvas.start_event_loop(interval)
34 36 return
35 37
36 38 class PlotData(Operation, Process):
37 39 '''
38 40 Base class for Schain plotting operations
39 41 '''
40 42
41 43 CODE = 'Figure'
42 44 colormap = 'jro'
43 45 bgcolor = 'white'
44 46 CONFLATE = False
45 47 __MAXNUMX = 80
46 48 __missing = 1E30
47 49
48 50 def __init__(self, **kwargs):
49 51
50 52 Operation.__init__(self, plot=True, **kwargs)
51 53 Process.__init__(self)
52 54 self.kwargs['code'] = self.CODE
53 55 self.mp = False
54 56 self.data = None
55 self.isConfig = False
57 self.isConfig = False
56 58 self.figures = []
57 59 self.axes = []
58 60 self.cb_axes = []
59 61 self.localtime = kwargs.pop('localtime', True)
60 62 self.show = kwargs.get('show', True)
61 63 self.save = kwargs.get('save', False)
62 64 self.colormap = kwargs.get('colormap', self.colormap)
63 65 self.colormap_coh = kwargs.get('colormap_coh', 'jet')
64 66 self.colormap_phase = kwargs.get('colormap_phase', 'RdBu_r')
65 67 self.colormaps = kwargs.get('colormaps', None)
66 68 self.bgcolor = kwargs.get('bgcolor', self.bgcolor)
67 69 self.showprofile = kwargs.get('showprofile', False)
68 70 self.title = kwargs.get('wintitle', self.CODE.upper())
69 71 self.cb_label = kwargs.get('cb_label', None)
70 72 self.cb_labels = kwargs.get('cb_labels', None)
71 73 self.xaxis = kwargs.get('xaxis', 'frequency')
72 74 self.zmin = kwargs.get('zmin', None)
73 75 self.zmax = kwargs.get('zmax', None)
74 76 self.zlimits = kwargs.get('zlimits', None)
75 self.xmin = kwargs.get('xmin', None)
77 self.xmin = kwargs.get('xmin', None)
76 78 self.xmax = kwargs.get('xmax', None)
77 79 self.xrange = kwargs.get('xrange', 24)
78 80 self.ymin = kwargs.get('ymin', None)
79 81 self.ymax = kwargs.get('ymax', None)
80 82 self.xlabel = kwargs.get('xlabel', None)
81 self.__MAXNUMY = kwargs.get('decimation', 100)
83 self.__MAXNUMY = kwargs.get('decimation', 100)
82 84 self.showSNR = kwargs.get('showSNR', False)
83 85 self.oneFigure = kwargs.get('oneFigure', True)
84 86 self.width = kwargs.get('width', None)
85 87 self.height = kwargs.get('height', None)
86 88 self.colorbar = kwargs.get('colorbar', True)
87 89 self.factors = kwargs.get('factors', [1, 1, 1, 1, 1, 1, 1, 1])
88 90 self.titles = ['' for __ in range(16)]
89 91
90 92 def __fmtTime(self, x, pos):
91 93 '''
92 94 '''
93 95
94 96 return '{}'.format(self.getDateTime(x).strftime('%H:%M'))
95 97
96 98 def __setup(self):
97 99 '''
98 100 Common setup for all figures, here figures and axes are created
99 101 '''
100 102
101 103 self.setup()
102 104
103 105 self.time_label = 'LT' if self.localtime else 'UTC'
104 106 if self.data.localtime:
105 107 self.getDateTime = datetime.datetime.fromtimestamp
106 108 else:
107 109 self.getDateTime = datetime.datetime.utcfromtimestamp
108 110
109 111 if self.width is None:
110 112 self.width = 8
111 113
112 114 self.figures = []
113 115 self.axes = []
114 116 self.cb_axes = []
115 117 self.pf_axes = []
116 118 self.cmaps = []
117 119
118 size = '15%' if self.ncols==1 else '30%'
119 pad = '4%' if self.ncols==1 else '8%'
120 size = '15%' if self.ncols == 1 else '30%'
121 pad = '4%' if self.ncols == 1 else '8%'
120 122
121 123 if self.oneFigure:
122 124 if self.height is None:
123 self.height = 1.4*self.nrows + 1
125 self.height = 1.4 * self.nrows + 1
124 126 fig = plt.figure(figsize=(self.width, self.height),
125 127 edgecolor='k',
126 128 facecolor='w')
127 129 self.figures.append(fig)
128 for n in range(self.nplots):
129 ax = fig.add_subplot(self.nrows, self.ncols, n+1)
130 for n in range(self.nplots):
131 ax = fig.add_subplot(self.nrows, self.ncols, n + 1)
130 132 ax.tick_params(labelsize=8)
131 133 ax.firsttime = True
132 134 ax.index = 0
133 135 ax.press = None
134 136 self.axes.append(ax)
135 137 if self.showprofile:
136 138 cax = self.__add_axes(ax, size=size, pad=pad)
137 cax.tick_params(labelsize=8)
139 cax.tick_params(labelsize=8)
138 140 self.pf_axes.append(cax)
139 141 else:
140 142 if self.height is None:
141 143 self.height = 3
142 144 for n in range(self.nplots):
143 145 fig = plt.figure(figsize=(self.width, self.height),
144 edgecolor='k',
145 facecolor='w')
146 edgecolor='k',
147 facecolor='w')
146 148 ax = fig.add_subplot(1, 1, 1)
147 149 ax.tick_params(labelsize=8)
148 150 ax.firsttime = True
149 151 ax.index = 0
150 152 ax.press = None
151 153 self.figures.append(fig)
152 154 self.axes.append(ax)
153 155 if self.showprofile:
154 156 cax = self.__add_axes(ax, size=size, pad=pad)
155 cax.tick_params(labelsize=8)
157 cax.tick_params(labelsize=8)
156 158 self.pf_axes.append(cax)
157
159
158 160 for n in range(self.nrows):
159 161 if self.colormaps is not None:
160 cmap = plt.get_cmap(self.colormaps[n])
162 cmap = plt.get_cmap(self.colormaps[n])
161 163 else:
162 164 cmap = plt.get_cmap(self.colormap)
163 165 cmap.set_bad(self.bgcolor, 1.)
164 166 self.cmaps.append(cmap)
165 167
166 168 for fig in self.figures:
167 169 fig.canvas.mpl_connect('key_press_event', self.OnKeyPress)
168 170 fig.canvas.mpl_connect('scroll_event', self.OnBtnScroll)
169 171 fig.canvas.mpl_connect('button_press_event', self.onBtnPress)
170 172 fig.canvas.mpl_connect('motion_notify_event', self.onMotion)
171 173 fig.canvas.mpl_connect('button_release_event', self.onBtnRelease)
172 174 if self.show:
173 175 fig.show()
174 176
175 177 def OnKeyPress(self, event):
176 178 '''
177 179 Event for pressing keys (up, down) change colormap
178 180 '''
179 181 ax = event.inaxes
180 182 if ax in self.axes:
181 183 if event.key == 'down':
182 184 ax.index += 1
183 185 elif event.key == 'up':
184 186 ax.index -= 1
185 187 if ax.index < 0:
186 188 ax.index = len(CMAPS) - 1
187 189 elif ax.index == len(CMAPS):
188 190 ax.index = 0
189 191 cmap = CMAPS[ax.index]
190 192 ax.cbar.set_cmap(cmap)
191 193 ax.cbar.draw_all()
192 194 ax.plt.set_cmap(cmap)
193 195 ax.cbar.patch.figure.canvas.draw()
194 196 self.colormap = cmap.name
195 197
196 198 def OnBtnScroll(self, event):
197 199 '''
198 200 Event for scrolling, scale figure
199 201 '''
200 202 cb_ax = event.inaxes
201 203 if cb_ax in [ax.cbar.ax for ax in self.axes]:
202 204 ax = [ax for ax in self.axes if cb_ax == ax.cbar.ax][0]
203 205 pt = ax.cbar.ax.bbox.get_points()[:,1]
204 206 nrm = ax.cbar.norm
205 207 vmin, vmax, p0, p1, pS = (nrm.vmin, nrm.vmax, pt[0], pt[1], event.y)
206 208 scale = 2 if event.step == 1 else 0.5
207 209 point = vmin + (vmax - vmin) / (p1 - p0)*(pS - p0)
208 210 ax.cbar.norm.vmin = point - scale*(point - vmin)
209 211 ax.cbar.norm.vmax = point - scale*(point - vmax)
210 212 ax.plt.set_norm(ax.cbar.norm)
211 213 ax.cbar.draw_all()
212 214 ax.cbar.patch.figure.canvas.draw()
213 215
214 216 def onBtnPress(self, event):
215 217 '''
216 218 Event for mouse button press
217 219 '''
218 220 cb_ax = event.inaxes
219 221 if cb_ax is None:
220 222 return
221 223
222 224 if cb_ax in [ax.cbar.ax for ax in self.axes]:
223 225 cb_ax.press = event.x, event.y
224 226 else:
225 227 cb_ax.press = None
226 228
227 229 def onMotion(self, event):
228 230 '''
229 231 Event for move inside colorbar
230 232 '''
231 233 cb_ax = event.inaxes
232 234 if cb_ax is None:
233 235 return
234 236 if cb_ax not in [ax.cbar.ax for ax in self.axes]:
235 237 return
236 238 if cb_ax.press is None:
237 239 return
238 240
239 241 ax = [ax for ax in self.axes if cb_ax == ax.cbar.ax][0]
240 242 xprev, yprev = cb_ax.press
241 243 dx = event.x - xprev
242 244 dy = event.y - yprev
243 245 cb_ax.press = event.x, event.y
244 246 scale = ax.cbar.norm.vmax - ax.cbar.norm.vmin
245 247 perc = 0.03
246 248
247 249 if event.button == 1:
248 250 ax.cbar.norm.vmin -= (perc*scale)*numpy.sign(dy)
249 251 ax.cbar.norm.vmax -= (perc*scale)*numpy.sign(dy)
250 252 elif event.button == 3:
251 253 ax.cbar.norm.vmin -= (perc*scale)*numpy.sign(dy)
252 254 ax.cbar.norm.vmax += (perc*scale)*numpy.sign(dy)
253 255
254 256 ax.cbar.draw_all()
255 257 ax.plt.set_norm(ax.cbar.norm)
256 258 ax.cbar.patch.figure.canvas.draw()
257 259
258 260 def onBtnRelease(self, event):
259 261 '''
260 262 Event for mouse button release
261 263 '''
262 264 cb_ax = event.inaxes
263 265 if cb_ax is not None:
264 266 cb_ax.press = None
265 267
266 268 def __add_axes(self, ax, size='30%', pad='8%'):
267 269 '''
268 270 Add new axes to the given figure
269 271 '''
270 272 divider = make_axes_locatable(ax)
271 273 nax = divider.new_horizontal(size=size, pad=pad)
272 ax.figure.add_axes(nax)
274 ax.figure.add_axes(nax)
273 275 return nax
274 276
275 277 self.setup()
276 278
277 279 def setup(self):
278 280 '''
279 281 This method should be implemented in the child class, the following
280 282 attributes should be set:
281
283
282 284 self.nrows: number of rows
283 285 self.ncols: number of cols
284 286 self.nplots: number of plots (channels or pairs)
285 287 self.ylabel: label for Y axes
286 288 self.titles: list of axes title
287 289
288 290 '''
289 291 raise(NotImplementedError, 'Implement this method in child class')
290 292
291 293 def fill_gaps(self, x_buffer, y_buffer, z_buffer):
292 294 '''
293 295 Create a masked array for missing data
294 296 '''
295 297 if x_buffer.shape[0] < 2:
296 298 return x_buffer, y_buffer, z_buffer
297 299
298 300 deltas = x_buffer[1:] - x_buffer[0:-1]
299 301 x_median = numpy.median(deltas)
300 302
301 index = numpy.where(deltas > 5*x_median)
303 index = numpy.where(deltas > 5 * x_median)
302 304
303 305 if len(index[0]) != 0:
304 306 z_buffer[::, index[0], ::] = self.__missing
305 307 z_buffer = numpy.ma.masked_inside(z_buffer,
306 0.99*self.__missing,
307 1.01*self.__missing)
308 0.99 * self.__missing,
309 1.01 * self.__missing)
308 310
309 311 return x_buffer, y_buffer, z_buffer
310 312
311 313 def decimate(self):
312 314
313 315 # dx = int(len(self.x)/self.__MAXNUMX) + 1
314 dy = int(len(self.y)/self.__MAXNUMY) + 1
316 dy = int(len(self.y) / self.__MAXNUMY) + 1
315 317
316 318 # x = self.x[::dx]
317 319 x = self.x
318 320 y = self.y[::dy]
319 321 z = self.z[::, ::, ::dy]
320
322
321 323 return x, y, z
322 324
323 325 def format(self):
324 326 '''
325 327 Set min and max values, labels, ticks and titles
326 328 '''
327 329
328 330 if self.xmin is None:
329 331 xmin = self.min_time
330 332 else:
331 333 if self.xaxis is 'time':
332 334 dt = self.getDateTime(self.min_time)
333 335 xmin = (dt.replace(hour=int(self.xmin), minute=0, second=0) - datetime.datetime(1970, 1, 1)).total_seconds()
334 336 if self.data.localtime:
335 337 xmin += time.timezone
336 338 else:
337 339 xmin = self.xmin
338 340
339 341 if self.xmax is None:
340 xmax = xmin+self.xrange*60*60
342 xmax = xmin + self.xrange * 60 * 60
341 343 else:
342 344 if self.xaxis is 'time':
343 345 dt = self.getDateTime(self.max_time)
344 346 xmax = (dt.replace(hour=int(self.xmax), minute=0, second=0) - datetime.datetime(1970, 1, 1)).total_seconds()
345 347 if self.data.localtime:
346 348 xmax += time.timezone
347 349 else:
348 350 xmax = self.xmax
349 351
350 352 ymin = self.ymin if self.ymin else numpy.nanmin(self.y)
351 353 ymax = self.ymax if self.ymax else numpy.nanmax(self.y)
352 354
353 355 Y = numpy.array([10, 20, 50, 100, 200, 500, 1000, 2000])
354 356 i = 1 if numpy.where(ymax < Y)[0][0] < 0 else numpy.where(ymax < Y)[0][0]
355 357 ystep = Y[i-1]/5
356 358
357 for n, ax in enumerate(self.axes):
359 ystep = 200 if ymax >= 800 else 100 if ymax >= 400 else 50 if ymax >= 200 else 20
360
361 for n, ax in enumerate(self.axes):
358 362 if ax.firsttime:
359 363 ax.set_facecolor(self.bgcolor)
360 364 ax.yaxis.set_major_locator(MultipleLocator(ystep))
361 365 if self.xaxis is 'time':
362 366 ax.xaxis.set_major_formatter(FuncFormatter(self.__fmtTime))
363 367 ax.xaxis.set_major_locator(LinearLocator(9))
364 368 if self.xlabel is not None:
365 369 ax.set_xlabel(self.xlabel)
366 ax.set_ylabel(self.ylabel)
370 ax.set_ylabel(self.ylabel)
367 371 ax.firsttime = False
368 372 if self.showprofile:
369 373 self.pf_axes[n].set_ylim(ymin, ymax)
370 self.pf_axes[n].set_xlim(self.zmin, self.zmax)
374 self.pf_axes[n].set_xlim(self.zmin, self.zmax)
371 375 self.pf_axes[n].set_xlabel('dB')
372 376 self.pf_axes[n].grid(b=True, axis='x')
373 [tick.set_visible(False) for tick in self.pf_axes[n].get_yticklabels()]
377 [tick.set_visible(False)
378 for tick in self.pf_axes[n].get_yticklabels()]
374 379 if self.colorbar:
375 380 ax.cbar = plt.colorbar(ax.plt, ax=ax, pad=0.02, aspect=10)
376 381 ax.cbar.ax.tick_params(labelsize=8)
377 382 ax.cbar.ax.press = None
378 383 if self.cb_label:
379 384 ax.cbar.set_label(self.cb_label, size=8)
380 385 elif self.cb_labels:
381 386 ax.cbar.set_label(self.cb_labels[n], size=8)
382
387
383 388 ax.set_title('{} - {} {}'.format(
384 389 self.titles[n],
385 390 self.getDateTime(self.max_time).strftime('%H:%M:%S'),
386 391 self.time_label),
387 392 size=8)
388 393 ax.set_xlim(xmin, xmax)
389 394 ax.set_ylim(ymin, ymax)
390 395
391 396 def __plot(self):
392 397 '''
393 398 '''
394 399 log.success('Plotting', self.name)
395
400
396 401 self.plot()
397 402 self.format()
398
403
399 404 for n, fig in enumerate(self.figures):
400 405 if self.nrows == 0 or self.nplots == 0:
401 406 log.warning('No data', self.name)
402 407 continue
403 408
404 409 fig.tight_layout()
405 410 fig.canvas.manager.set_window_title('{} - {}'.format(self.title,
406 411 self.getDateTime(self.max_time).strftime('%Y/%m/%d')))
407 412 # fig.canvas.draw()
408
413
409 414 if self.save and self.data.ended:
410 415 channels = range(self.nrows)
411 416 if self.oneFigure:
412 417 label = ''
413 418 else:
414 419 label = '_{}'.format(channels[n])
415 420 figname = os.path.join(
416 421 self.save,
417 422 '{}{}_{}.png'.format(
418 423 self.CODE,
419 424 label,
420 425 self.getDateTime(self.saveTime).strftime('%y%m%d_%H%M%S')
421 426 )
422 427 )
423 428 print 'Saving figure: {}'.format(figname)
424 429 fig.savefig(figname)
425 430
426 431 def plot(self):
427 432 '''
428 433 '''
429 434 raise(NotImplementedError, 'Implement this method in child class')
430 435
431 436 def run(self):
432 437
433 438 log.success('Starting', self.name)
434 439
435 440 context = zmq.Context()
436 441 receiver = context.socket(zmq.SUB)
437 442 receiver.setsockopt(zmq.SUBSCRIBE, '')
438 443 receiver.setsockopt(zmq.CONFLATE, self.CONFLATE)
439 444
440 445 if 'server' in self.kwargs['parent']:
441 receiver.connect('ipc:///tmp/{}.plots'.format(self.kwargs['parent']['server']))
446 receiver.connect(
447 'ipc:///tmp/{}.plots'.format(self.kwargs['parent']['server']))
442 448 else:
443 receiver.connect("ipc:///tmp/zmq.plots")
449 receiver.connect("ipc:///tmp/zmq.plots")
444 450
445 451 while True:
446 452 try:
447 453 self.data = receiver.recv_pyobj(flags=zmq.NOBLOCK)
448 454 if self.data.localtime and self.localtime:
449 455 self.times = self.data.times
450 456 elif self.data.localtime and not self.localtime:
451 457 self.times = self.data.times + time.timezone
452 458 elif not self.data.localtime and self.localtime:
453 459 self.times = self.data.times - time.timezone
454 460 else:
455 461 self.times = self.data.times
456 462
457 463 self.min_time = self.times[0]
458 464 self.max_time = self.times[-1]
459 465
460 466 if self.isConfig is False:
461 467 self.__setup()
462 468 self.isConfig = True
463
469
464 470 self.__plot()
465 471
466 472 except zmq.Again as e:
467 473 log.log('Waiting for data...')
468 474 if self.data:
469 475 figpause(self.data.throttle)
470 476 else:
471 477 time.sleep(2)
472 478
473 479 def close(self):
474 480 if self.data:
475 481 self.__plot()
476 482
483
477 484 class PlotSpectraData(PlotData):
478 485 '''
479 486 Plot for Spectra data
480 487 '''
481 488
482 489 CODE = 'spc'
483 colormap = 'jro'
490 colormap = 'jro'
484 491
485 492 def setup(self):
486 493 self.nplots = len(self.data.channels)
487 self.ncols = int(numpy.sqrt(self.nplots)+ 0.9)
488 self.nrows = int((1.0*self.nplots/self.ncols) + 0.9)
489 self.width = 3.4*self.ncols
490 self.height = 3*self.nrows
494 self.ncols = int(numpy.sqrt(self.nplots) + 0.9)
495 self.nrows = int((1.0 * self.nplots / self.ncols) + 0.9)
496 self.width = 3.4 * self.ncols
497 self.height = 3 * self.nrows
491 498 self.cb_label = 'dB'
492 if self.showprofile:
493 self.width += 0.8*self.ncols
499 if self.showprofile:
500 self.width += 0.8 * self.ncols
494 501
495 502 self.ylabel = 'Range [Km]'
496 503
497 504 def plot(self):
498 505 if self.xaxis == "frequency":
499 506 x = self.data.xrange[0]
500 507 self.xlabel = "Frequency (kHz)"
501 508 elif self.xaxis == "time":
502 509 x = self.data.xrange[1]
503 510 self.xlabel = "Time (ms)"
504 511 else:
505 512 x = self.data.xrange[2]
506 513 self.xlabel = "Velocity (m/s)"
507 514
508 515 if self.CODE == 'spc_mean':
509 516 x = self.data.xrange[2]
510 517 self.xlabel = "Velocity (m/s)"
511 518
512 519 self.titles = []
513 520
514 521 y = self.data.heights
515 522 self.y = y
516 523 z = self.data['spc']
517
524
518 525 for n, ax in enumerate(self.axes):
519 526 noise = self.data['noise'][n][-1]
520 527 if self.CODE == 'spc_mean':
521 528 mean = self.data['mean'][n][-1]
522 529 if ax.firsttime:
523 530 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
524 531 self.xmin = self.xmin if self.xmin else -self.xmax
525 532 self.zmin = self.zmin if self.zmin else numpy.nanmin(z)
526 533 self.zmax = self.zmax if self.zmax else numpy.nanmax(z)
527 534 ax.plt = ax.pcolormesh(x, y, z[n].T,
528 vmin=self.zmin,
529 vmax=self.zmax,
530 cmap=plt.get_cmap(self.colormap)
531 )
535 vmin=self.zmin,
536 vmax=self.zmax,
537 cmap=plt.get_cmap(self.colormap)
538 )
532 539
533 540 if self.showprofile:
534 ax.plt_profile= self.pf_axes[n].plot(self.data['rti'][n][-1], y)[0]
541 ax.plt_profile = self.pf_axes[n].plot(
542 self.data['rti'][n][-1], y)[0]
535 543 ax.plt_noise = self.pf_axes[n].plot(numpy.repeat(noise, len(y)), y,
536 color="k", linestyle="dashed", lw=1)[0]
544 color="k", linestyle="dashed", lw=1)[0]
537 545 if self.CODE == 'spc_mean':
538 546 ax.plt_mean = ax.plot(mean, y, color='k')[0]
539 547 else:
540 548 ax.plt.set_array(z[n].T.ravel())
541 549 if self.showprofile:
542 550 ax.plt_profile.set_data(self.data['rti'][n][-1], y)
543 551 ax.plt_noise.set_data(numpy.repeat(noise, len(y)), y)
544 552 if self.CODE == 'spc_mean':
545 553 ax.plt_mean.set_data(mean, y)
546 554
547 555 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
548 556 self.saveTime = self.max_time
549 557
550 558
551 559 class PlotCrossSpectraData(PlotData):
552 560
553 561 CODE = 'cspc'
554 562 zmin_coh = None
555 563 zmax_coh = None
556 564 zmin_phase = None
557 zmax_phase = None
565 zmax_phase = None
558 566
559 567 def setup(self):
560 568
561 569 self.ncols = 4
562 570 self.nrows = len(self.data.pairs)
563 self.nplots = self.nrows*4
564 self.width = 3.4*self.ncols
565 self.height = 3*self.nrows
571 self.nplots = self.nrows * 4
572 self.width = 3.4 * self.ncols
573 self.height = 3 * self.nrows
566 574 self.ylabel = 'Range [Km]'
567 self.showprofile = False
575 self.showprofile = False
568 576
569 577 def plot(self):
570 578
571 579 if self.xaxis == "frequency":
572 580 x = self.data.xrange[0]
573 581 self.xlabel = "Frequency (kHz)"
574 582 elif self.xaxis == "time":
575 583 x = self.data.xrange[1]
576 584 self.xlabel = "Time (ms)"
577 585 else:
578 586 x = self.data.xrange[2]
579 587 self.xlabel = "Velocity (m/s)"
580 588
581 589 self.titles = []
582 590
583 591 y = self.data.heights
584 592 self.y = y
585 593 spc = self.data['spc']
586 594 cspc = self.data['cspc']
587 595
588 596 for n in range(self.nrows):
589 597 noise = self.data['noise'][n][-1]
590 598 pair = self.data.pairs[n]
591 ax = self.axes[4*n]
592 ax3 = self.axes[4*n+3]
599 ax = self.axes[4 * n]
600 ax3 = self.axes[4 * n + 3]
593 601 if ax.firsttime:
594 602 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
595 603 self.xmin = self.xmin if self.xmin else -self.xmax
596 604 self.zmin = self.zmin if self.zmin else numpy.nanmin(spc)
597 self.zmax = self.zmax if self.zmax else numpy.nanmax(spc)
605 self.zmax = self.zmax if self.zmax else numpy.nanmax(spc)
598 606 ax.plt = ax.pcolormesh(x, y, spc[pair[0]].T,
599 607 vmin=self.zmin,
600 608 vmax=self.zmax,
601 609 cmap=plt.get_cmap(self.colormap)
602 )
610 )
603 611 else:
604 612 ax.plt.set_array(spc[pair[0]].T.ravel())
605 613 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
606 614
607 ax = self.axes[4*n+1]
608 if ax.firsttime:
615 ax = self.axes[4 * n + 1]
616 if ax.firsttime:
609 617 ax.plt = ax.pcolormesh(x, y, spc[pair[1]].T,
610 618 vmin=self.zmin,
611 619 vmax=self.zmax,
612 620 cmap=plt.get_cmap(self.colormap)
613 621 )
614 622 else:
615 623 ax.plt.set_array(spc[pair[1]].T.ravel())
616 624 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
617 625
618 out = cspc[n]/numpy.sqrt(spc[pair[0]]*spc[pair[1]])
626 out = cspc[n] / numpy.sqrt(spc[pair[0]] * spc[pair[1]])
619 627 coh = numpy.abs(out)
620 phase = numpy.arctan2(out.imag, out.real)*180/numpy.pi
621
622 ax = self.axes[4*n+2]
623 if ax.firsttime:
628 phase = numpy.arctan2(out.imag, out.real) * 180 / numpy.pi
629
630 ax = self.axes[4 * n + 2]
631 if ax.firsttime:
624 632 ax.plt = ax.pcolormesh(x, y, coh.T,
625 633 vmin=0,
626 634 vmax=1,
627 635 cmap=plt.get_cmap(self.colormap_coh)
628 636 )
629 637 else:
630 638 ax.plt.set_array(coh.T.ravel())
631 self.titles.append('Coherence Ch{} * Ch{}'.format(pair[0], pair[1]))
639 self.titles.append(
640 'Coherence Ch{} * Ch{}'.format(pair[0], pair[1]))
632 641
633 ax = self.axes[4*n+3]
642 ax = self.axes[4 * n + 3]
634 643 if ax.firsttime:
635 644 ax.plt = ax.pcolormesh(x, y, phase.T,
636 645 vmin=-180,
637 646 vmax=180,
638 647 cmap=plt.get_cmap(self.colormap_phase)
639 648 )
640 649 else:
641 650 ax.plt.set_array(phase.T.ravel())
642 651 self.titles.append('Phase CH{} * CH{}'.format(pair[0], pair[1]))
643
652
644 653 self.saveTime = self.max_time
645 654
646 655
647 656 class PlotSpectraMeanData(PlotSpectraData):
648 657 '''
649 658 Plot for Spectra and Mean
650 659 '''
651 660 CODE = 'spc_mean'
652 661 colormap = 'jro'
653 662
654 663
655 664 class PlotRTIData(PlotData):
656 665 '''
657 666 Plot for RTI data
658 667 '''
659 668
660 669 CODE = 'rti'
661 670 colormap = 'jro'
662 671
663 672 def setup(self):
664 673 self.xaxis = 'time'
665 self.ncols = 1
674 self.ncols = 1
666 675 self.nrows = len(self.data.channels)
667 676 self.nplots = len(self.data.channels)
668 677 self.ylabel = 'Range [Km]'
669 678 self.cb_label = 'dB'
670 self.titles = ['{} Channel {}'.format(self.CODE.upper(), x) for x in range(self.nrows)]
679 self.titles = ['{} Channel {}'.format(
680 self.CODE.upper(), x) for x in range(self.nrows)]
671 681
672 682 def plot(self):
673 683 self.x = self.times
674 684 self.y = self.data.heights
675 685 self.z = self.data[self.CODE]
676 686 self.z = numpy.ma.masked_invalid(self.z)
677 687
678 688 for n, ax in enumerate(self.axes):
679 x, y, z = self.fill_gaps(*self.decimate())
689 x, y, z = self.fill_gaps(*self.decimate())
680 690 self.zmin = self.zmin if self.zmin else numpy.min(self.z)
681 691 self.zmax = self.zmax if self.zmax else numpy.max(self.z)
682 if ax.firsttime:
692 if ax.firsttime:
683 693 ax.plt = ax.pcolormesh(x, y, z[n].T,
684 vmin=self.zmin,
685 vmax=self.zmax,
686 cmap=plt.get_cmap(self.colormap)
687 )
694 vmin=self.zmin,
695 vmax=self.zmax,
696 cmap=plt.get_cmap(self.colormap)
697 )
688 698 if self.showprofile:
689 ax.plot_profile= self.pf_axes[n].plot(self.data['rti'][n][-1], self.y)[0]
699 ax.plot_profile = self.pf_axes[n].plot(
700 self.data['rti'][n][-1], self.y)[0]
690 701 ax.plot_noise = self.pf_axes[n].plot(numpy.repeat(self.data['noise'][n][-1], len(self.y)), self.y,
691 702 color="k", linestyle="dashed", lw=1)[0]
692 703 else:
693 704 ax.collections.remove(ax.collections[0])
694 705 ax.plt = ax.pcolormesh(x, y, z[n].T,
695 706 vmin=self.zmin,
696 707 vmax=self.zmax,
697 708 cmap=plt.get_cmap(self.colormap)
698 )
709 )
699 710 if self.showprofile:
700 711 ax.plot_profile.set_data(self.data['rti'][n][-1], self.y)
701 ax.plot_noise.set_data(numpy.repeat(self.data['noise'][n][-1], len(self.y)), self.y)
712 ax.plot_noise.set_data(numpy.repeat(
713 self.data['noise'][n][-1], len(self.y)), self.y)
702 714
703 self.saveTime = self.min_time
715 self.saveTime = self.min_time
704 716
705 717
706 718 class PlotCOHData(PlotRTIData):
707 719 '''
708 720 Plot for Coherence data
709 721 '''
710 722
711 723 CODE = 'coh'
712 724
713 725 def setup(self):
714 726 self.xaxis = 'time'
715 727 self.ncols = 1
716 728 self.nrows = len(self.data.pairs)
717 729 self.nplots = len(self.data.pairs)
718 self.ylabel = 'Range [Km]'
730 self.ylabel = 'Range [Km]'
719 731 if self.CODE == 'coh':
720 732 self.cb_label = ''
721 self.titles = ['Coherence Map Ch{} * Ch{}'.format(x[0], x[1]) for x in self.data.pairs]
733 self.titles = [
734 'Coherence Map Ch{} * Ch{}'.format(x[0], x[1]) for x in self.data.pairs]
722 735 else:
723 736 self.cb_label = 'Degrees'
724 self.titles = ['Phase Map Ch{} * Ch{}'.format(x[0], x[1]) for x in self.data.pairs]
737 self.titles = [
738 'Phase Map Ch{} * Ch{}'.format(x[0], x[1]) for x in self.data.pairs]
725 739
726 740
727 741 class PlotPHASEData(PlotCOHData):
728 742 '''
729 743 Plot for Phase map data
730 744 '''
731 745
732 746 CODE = 'phase'
733 747 colormap = 'seismic'
734 748
735 749
736 750 class PlotNoiseData(PlotData):
737 751 '''
738 752 Plot for noise
739 753 '''
740 754
741 755 CODE = 'noise'
742 756
743 757 def setup(self):
744 758 self.xaxis = 'time'
745 759 self.ncols = 1
746 760 self.nrows = 1
747 761 self.nplots = 1
748 762 self.ylabel = 'Intensity [dB]'
749 763 self.titles = ['Noise']
750 764 self.colorbar = False
751 765
752 766 def plot(self):
753 767
754 768 x = self.times
755 769 xmin = self.min_time
756 xmax = xmin+self.xrange*60*60
770 xmax = xmin + self.xrange * 60 * 60
757 771 Y = self.data[self.CODE]
758
772
759 773 if self.axes[0].firsttime:
760 774 for ch in self.data.channels:
761 775 y = Y[ch]
762 776 self.axes[0].plot(x, y, lw=1, label='Ch{}'.format(ch))
763 777 plt.legend()
764 778 else:
765 779 for ch in self.data.channels:
766 780 y = Y[ch]
767 781 self.axes[0].lines[ch].set_data(x, y)
768
782
769 783 self.ymin = numpy.nanmin(Y) - 5
770 784 self.ymax = numpy.nanmax(Y) + 5
771 785 self.saveTime = self.min_time
772 786
773 787
774 788 class PlotSNRData(PlotRTIData):
775 789 '''
776 790 Plot for SNR Data
777 791 '''
778 792
779 793 CODE = 'snr'
780 794 colormap = 'jet'
781 795
782 796
783 797 class PlotDOPData(PlotRTIData):
784 798 '''
785 799 Plot for DOPPLER Data
786 800 '''
787 801
788 802 CODE = 'dop'
789 803 colormap = 'jet'
790 804
791 805
792 806 class PlotSkyMapData(PlotData):
793 807 '''
794 808 Plot for meteors detection data
795 809 '''
796 810
797 811 CODE = 'met'
798 812
799 813 def setup(self):
800 814
801 815 self.ncols = 1
802 816 self.nrows = 1
803 817 self.width = 7.2
804 818 self.height = 7.2
805 819
806 820 self.xlabel = 'Zonal Zenith Angle (deg)'
807 821 self.ylabel = 'Meridional Zenith Angle (deg)'
808 822
809 823 if self.figure is None:
810 824 self.figure = plt.figure(figsize=(self.width, self.height),
811 825 edgecolor='k',
812 826 facecolor='w')
813 827 else:
814 828 self.figure.clf()
815 829
816 self.ax = plt.subplot2grid((self.nrows, self.ncols), (0, 0), 1, 1, polar=True)
830 self.ax = plt.subplot2grid(
831 (self.nrows, self.ncols), (0, 0), 1, 1, polar=True)
817 832 self.ax.firsttime = True
818 833
819
820 834 def plot(self):
821 835
822 arrayParameters = numpy.concatenate([self.data['param'][t] for t in self.times])
823 error = arrayParameters[:,-1]
836 arrayParameters = numpy.concatenate(
837 [self.data['param'][t] for t in self.times])
838 error = arrayParameters[:, -1]
824 839 indValid = numpy.where(error == 0)[0]
825 finalMeteor = arrayParameters[indValid,:]
826 finalAzimuth = finalMeteor[:,3]
827 finalZenith = finalMeteor[:,4]
840 finalMeteor = arrayParameters[indValid, :]
841 finalAzimuth = finalMeteor[:, 3]
842 finalZenith = finalMeteor[:, 4]
828 843
829 x = finalAzimuth*numpy.pi/180
844 x = finalAzimuth * numpy.pi / 180
830 845 y = finalZenith
831 846
832 847 if self.ax.firsttime:
833 848 self.ax.plot = self.ax.plot(x, y, 'bo', markersize=5)[0]
834 self.ax.set_ylim(0,90)
835 self.ax.set_yticks(numpy.arange(0,90,20))
849 self.ax.set_ylim(0, 90)
850 self.ax.set_yticks(numpy.arange(0, 90, 20))
836 851 self.ax.set_xlabel(self.xlabel)
837 852 self.ax.set_ylabel(self.ylabel)
838 853 self.ax.yaxis.labelpad = 40
839 854 self.ax.firsttime = False
840 855 else:
841 856 self.ax.plot.set_data(x, y)
842 857
843 858
844 859 dt1 = self.getDateTime(self.min_time).strftime('%y/%m/%d %H:%M:%S')
845 860 dt2 = self.getDateTime(self.max_time).strftime('%y/%m/%d %H:%M:%S')
846 861 title = 'Meteor Detection Sky Map\n %s - %s \n Number of events: %5.0f\n' % (dt1,
847 862 dt2,
848 863 len(x))
849 864 self.ax.set_title(title, size=8)
850
851 865 self.saveTime = self.max_time
852 866
867
853 868 class PlotParamData(PlotRTIData):
854 869 '''
855 870 Plot for data_param object
856 871 '''
857 872
858 873 CODE = 'param'
859 874 colormap = 'seismic'
860 875
861 876 def setup(self):
862 877 self.xaxis = 'time'
863 878 self.ncols = 1
864 879 self.nrows = self.data.shape(self.CODE)[0]
865 880 self.nplots = self.nrows
866 881 if self.showSNR:
867 882 self.nrows += 1
868 883 self.nplots += 1
869
884
870 885 self.ylabel = 'Height [Km]'
871 886 self.titles = self.data.parameters \
872 887 if self.data.parameters else ['Param {}'.format(x) for x in xrange(self.nrows)]
873 888 if self.showSNR:
874 889 self.titles.append('SNR')
875 890
876 891 def plot(self):
877 self.data.normalize_heights()
892 self.data.normalize_heights()
878 893 self.x = self.times
879 894 self.y = self.data.heights
880 if self.showSNR:
895 if self.showSNR:
881 896 self.z = numpy.concatenate(
882 897 (self.data[self.CODE], self.data['snr'])
883 898 )
884 899 else:
885 900 self.z = self.data[self.CODE]
886 901
887 902 self.z = numpy.ma.masked_invalid(self.z)
888 903
889 904 for n, ax in enumerate(self.axes):
890 905
891 906 x, y, z = self.fill_gaps(*self.decimate())
892 907 self.zmax = self.zmax if self.zmax is not None else numpy.max(self.z[n])
893 908 self.zmin = self.zmin if self.zmin is not None else numpy.min(self.z[n])
894 909
895 910 if ax.firsttime:
896 911 if self.zlimits is not None:
897 912 self.zmin, self.zmax = self.zlimits[n]
898 913
899 914 ax.plt = ax.pcolormesh(x, y, z[n].T*self.factors[n],
900 915 vmin=self.zmin,
901 916 vmax=self.zmax,
902 917 cmap=self.cmaps[n]
903 )
918 )
904 919 else:
905 920 if self.zlimits is not None:
906 921 self.zmin, self.zmax = self.zlimits[n]
907 922 ax.collections.remove(ax.collections[0])
908 923 ax.plt = ax.pcolormesh(x, y, z[n].T*self.factors[n],
909 924 vmin=self.zmin,
910 925 vmax=self.zmax,
911 926 cmap=self.cmaps[n]
912 )
927 )
913 928
914 929 self.saveTime = self.min_time
915 930
916 931 class PlotOutputData(PlotParamData):
917 932 '''
918 933 Plot data_output object
919 934 '''
920 935
921 936 CODE = 'output'
922 937 colormap = 'seismic'
@@ -1,328 +1,329
1 1 '''
2 2 Created on Jul 9, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import os
7 7 import datetime
8 8 import numpy
9 9
10 10 from figure import Figure, isRealtime
11 11 from plotting_codes import *
12 12
13 13 class SpectraHeisScope(Figure):
14
15
14
15
16 16 isConfig = None
17 17 __nsubplots = None
18
18
19 19 WIDTHPROF = None
20 20 HEIGHTPROF = None
21 21 PREFIX = 'spc'
22
23 def __init__(self):
24
22
23 def __init__(self, **kwargs):
24
25 Figure.__init__(self, **kwargs)
25 26 self.isConfig = False
26 27 self.__nsubplots = 1
27
28
28 29 self.WIDTH = 230
29 30 self.HEIGHT = 250
30 31 self.WIDTHPROF = 120
31 32 self.HEIGHTPROF = 0
32 33 self.counter_imagwr = 0
33
34
34 35 self.PLOT_CODE = SPEC_CODE
35
36
36 37 def getSubplots(self):
37
38
38 39 ncol = int(numpy.sqrt(self.nplots)+0.9)
39 40 nrow = int(self.nplots*1./ncol + 0.9)
40
41
41 42 return nrow, ncol
42
43
43 44 def setup(self, id, nplots, wintitle, show):
44
45
45 46 showprofile = False
46 47 self.__showprofile = showprofile
47 48 self.nplots = nplots
48
49
49 50 ncolspan = 1
50 51 colspan = 1
51 52 if showprofile:
52 53 ncolspan = 3
53 54 colspan = 2
54 55 self.__nsubplots = 2
55
56
56 57 self.createFigure(id = id,
57 58 wintitle = wintitle,
58 59 widthplot = self.WIDTH + self.WIDTHPROF,
59 60 heightplot = self.HEIGHT + self.HEIGHTPROF,
60 61 show = show)
61
62
62 63 nrow, ncol = self.getSubplots()
63
64
64 65 counter = 0
65 66 for y in range(nrow):
66 67 for x in range(ncol):
67
68
68 69 if counter >= self.nplots:
69 70 break
70
71
71 72 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
72
73
73 74 if showprofile:
74 75 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
75
76
76 77 counter += 1
77 78
78
79
79 80 def run(self, dataOut, id, wintitle="", channelList=None,
80 81 xmin=None, xmax=None, ymin=None, ymax=None, save=False,
81 82 figpath='./', figfile=None, ftp=False, wr_period=1, show=True,
82 83 server=None, folder=None, username=None, password=None,
83 84 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
84
85
85 86 """
86
87
87 88 Input:
88 89 dataOut :
89 90 id :
90 91 wintitle :
91 92 channelList :
92 93 xmin : None,
93 94 xmax : None,
94 95 ymin : None,
95 96 ymax : None,
96 97 """
97
98
98 99 if dataOut.realtime:
99 100 if not(isRealtime(utcdatatime = dataOut.utctime)):
100 101 print 'Skipping this plot function'
101 102 return
102
103
103 104 if channelList == None:
104 105 channelIndexList = dataOut.channelIndexList
105 106 else:
106 107 channelIndexList = []
107 108 for channel in channelList:
108 109 if channel not in dataOut.channelList:
109 110 raise ValueError, "Channel %d is not in dataOut.channelList"
110 111 channelIndexList.append(dataOut.channelList.index(channel))
111
112
112 113 # x = dataOut.heightList
113 c = 3E8
114 c = 3E8
114 115 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
115 116 #deberia cambiar para el caso de 1Mhz y 100KHz
116 117 x = numpy.arange(-1*dataOut.nHeights/2.,dataOut.nHeights/2.)*(c/(2*deltaHeight*dataOut.nHeights*1000))
117 118 #para 1Mhz descomentar la siguiente linea
118 119 #x= x/(10000.0)
119 120 # y = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:])
120 121 # y = y.real
121 122 factor = dataOut.normFactor
122 123 data = dataOut.data_spc / factor
123 124 datadB = 10.*numpy.log10(data)
124 125 y = datadB
125
126
126 127 #thisDatetime = dataOut.datatime
127 128 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
128 129 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
129 130 xlabel = ""
130 131 #para 1Mhz descomentar la siguiente linea
131 132 #xlabel = "Frequency x 10000"
132 133 ylabel = "Intensity (dB)"
133
134
134 135 if not self.isConfig:
135 136 nplots = len(channelIndexList)
136
137
137 138 self.setup(id=id,
138 139 nplots=nplots,
139 140 wintitle=wintitle,
140 141 show=show)
141
142
142 143 if xmin == None: xmin = numpy.nanmin(x)
143 144 if xmax == None: xmax = numpy.nanmax(x)
144 145 if ymin == None: ymin = numpy.nanmin(y)
145 146 if ymax == None: ymax = numpy.nanmax(y)
146
147
147 148 self.FTP_WEI = ftp_wei
148 149 self.EXP_CODE = exp_code
149 150 self.SUB_EXP_CODE = sub_exp_code
150 151 self.PLOT_POS = plot_pos
151
152
152 153 self.isConfig = True
153
154
154 155 self.setWinTitle(title)
155
156
156 157 for i in range(len(self.axesList)):
157 158 ychannel = y[i,:]
158 159 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
159 160 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[channelIndexList[i]], numpy.max(ychannel), str_datetime)
160 161 axes = self.axesList[i]
161 162 axes.pline(x, ychannel,
162 163 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
163 164 xlabel=xlabel, ylabel=ylabel, title=title, grid='both')
164
165
165
166
166 167 self.draw()
167
168
168 169 self.save(figpath=figpath,
169 170 figfile=figfile,
170 171 save=save,
171 172 ftp=ftp,
172 173 wr_period=wr_period,
173 174 thisDatetime=thisDatetime)
174 175
175 176 class RTIfromSpectraHeis(Figure):
176
177
177 178 isConfig = None
178 179 __nsubplots = None
179 180
180 181 PREFIX = 'rtinoise'
181
182 def __init__(self):
183
182
183 def __init__(self, **kwargs):
184 Figure.__init__(self, **kwargs)
184 185 self.timerange = 24*60*60
185 186 self.isConfig = False
186 187 self.__nsubplots = 1
187
188
188 189 self.WIDTH = 820
189 190 self.HEIGHT = 200
190 191 self.WIDTHPROF = 120
191 192 self.HEIGHTPROF = 0
192 193 self.counter_imagwr = 0
193 194 self.xdata = None
194 195 self.ydata = None
195 196 self.figfile = None
196
197
197 198 self.PLOT_CODE = RTI_CODE
198
199
199 200 def getSubplots(self):
200
201
201 202 ncol = 1
202 203 nrow = 1
203
204
204 205 return nrow, ncol
205
206
206 207 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
207
208
208 209 self.__showprofile = showprofile
209 210 self.nplots = nplots
210
211
211 212 ncolspan = 7
212 213 colspan = 6
213 214 self.__nsubplots = 2
214
215
215 216 self.createFigure(id = id,
216 217 wintitle = wintitle,
217 218 widthplot = self.WIDTH+self.WIDTHPROF,
218 219 heightplot = self.HEIGHT+self.HEIGHTPROF,
219 220 show = show)
220
221
221 222 nrow, ncol = self.getSubplots()
222
223
223 224 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
224
225
225
226
226 227 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
227 228 xmin=None, xmax=None, ymin=None, ymax=None,
228 229 timerange=None,
229 230 save=False, figpath='./', figfile=None, ftp=False, wr_period=1, show=True,
230 231 server=None, folder=None, username=None, password=None,
231 232 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
232
233
233 234 if channelList == None:
234 235 channelIndexList = dataOut.channelIndexList
235 236 channelList = dataOut.channelList
236 237 else:
237 238 channelIndexList = []
238 239 for channel in channelList:
239 240 if channel not in dataOut.channelList:
240 241 raise ValueError, "Channel %d is not in dataOut.channelList"
241 242 channelIndexList.append(dataOut.channelList.index(channel))
242
243
243 244 if timerange != None:
244 245 self.timerange = timerange
245
246
246 247 x = dataOut.getTimeRange()
247 248 y = dataOut.getHeiRange()
248
249
249 250 factor = dataOut.normFactor
250 251 data = dataOut.data_spc / factor
251 252 data = numpy.average(data,axis=1)
252 253 datadB = 10*numpy.log10(data)
253
254
254 255 # factor = dataOut.normFactor
255 256 # noise = dataOut.getNoise()/factor
256 257 # noisedB = 10*numpy.log10(noise)
257
258
258 259 #thisDatetime = dataOut.datatime
259 260 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
260 261 title = wintitle + " RTI: %s" %(thisDatetime.strftime("%d-%b-%Y"))
261 262 xlabel = "Local Time"
262 263 ylabel = "Intensity (dB)"
263
264
264 265 if not self.isConfig:
265
266
266 267 nplots = 1
267
268
268 269 self.setup(id=id,
269 270 nplots=nplots,
270 271 wintitle=wintitle,
271 272 showprofile=showprofile,
272 273 show=show)
273
274
274 275 self.tmin, self.tmax = self.getTimeLim(x, xmin, xmax)
275
276
276 277 if ymin == None: ymin = numpy.nanmin(datadB)
277 278 if ymax == None: ymax = numpy.nanmax(datadB)
278
279
279 280 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
280 281 self.isConfig = True
281 282 self.figfile = figfile
282 283 self.xdata = numpy.array([])
283 284 self.ydata = numpy.array([])
284
285
285 286 self.FTP_WEI = ftp_wei
286 287 self.EXP_CODE = exp_code
287 288 self.SUB_EXP_CODE = sub_exp_code
288 289 self.PLOT_POS = plot_pos
289
290
290 291 self.setWinTitle(title)
291
292
292
293
293 294 # title = "RTI %s" %(thisDatetime.strftime("%d-%b-%Y"))
294 295 title = "RTI - %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
295
296
296 297 legendlabels = ["channel %d"%idchannel for idchannel in channelList]
297 298 axes = self.axesList[0]
298
299
299 300 self.xdata = numpy.hstack((self.xdata, x[0:1]))
300
301
301 302 if len(self.ydata)==0:
302 303 self.ydata = datadB[channelIndexList].reshape(-1,1)
303 304 else:
304 305 self.ydata = numpy.hstack((self.ydata, datadB[channelIndexList].reshape(-1,1)))
305
306
306
307
307 308 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
308 309 xmin=self.tmin, xmax=self.tmax, ymin=ymin, ymax=ymax,
309 310 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='.', markersize=8, linestyle="solid", grid='both',
310 311 XAxisAsTime=True
311 312 )
312
313
313 314 self.draw()
314
315
315 316 update_figfile = False
316
317
317 318 if dataOut.ltctime >= self.tmax:
318 319 self.counter_imagwr = wr_period
319 320 self.isConfig = False
320 321 update_figfile = True
321
322
322 323 self.save(figpath=figpath,
323 324 figfile=figfile,
324 325 save=save,
325 326 ftp=ftp,
326 327 wr_period=wr_period,
327 328 thisDatetime=thisDatetime,
328 329 update_figfile=update_figfile)
@@ -1,1542 +1,1542
1 1 '''
2 2 Created on Jul 9, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import os
7 7 import datetime
8 8 import numpy
9 9
10 10 from figure import Figure, isRealtime, isTimeInHourRange
11 11 from plotting_codes import *
12 12
13 13
14 14 class SpectraPlot(Figure):
15 15
16 16 isConfig = None
17 17 __nsubplots = None
18 18
19 19 WIDTHPROF = None
20 20 HEIGHTPROF = None
21 21 PREFIX = 'spc'
22 22
23 23 def __init__(self, **kwargs):
24 24 Figure.__init__(self, **kwargs)
25 25 self.isConfig = False
26 26 self.__nsubplots = 1
27 27
28 self.WIDTH = 250
29 self.HEIGHT = 250
28 self.WIDTH = 300
29 self.HEIGHT = 300
30 30 self.WIDTHPROF = 120
31 31 self.HEIGHTPROF = 0
32 32 self.counter_imagwr = 0
33 33
34 34 self.PLOT_CODE = SPEC_CODE
35 35
36 36 self.FTP_WEI = None
37 37 self.EXP_CODE = None
38 38 self.SUB_EXP_CODE = None
39 39 self.PLOT_POS = None
40 40
41 41 self.__xfilter_ena = False
42 42 self.__yfilter_ena = False
43 43
44 44 def getSubplots(self):
45 45
46 46 ncol = int(numpy.sqrt(self.nplots)+0.9)
47 47 nrow = int(self.nplots*1./ncol + 0.9)
48 48
49 49 return nrow, ncol
50 50
51 51 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
52 52
53 53 self.__showprofile = showprofile
54 54 self.nplots = nplots
55 55
56 56 ncolspan = 1
57 57 colspan = 1
58 58 if showprofile:
59 59 ncolspan = 3
60 60 colspan = 2
61 61 self.__nsubplots = 2
62 62
63 63 self.createFigure(id = id,
64 64 wintitle = wintitle,
65 65 widthplot = self.WIDTH + self.WIDTHPROF,
66 66 heightplot = self.HEIGHT + self.HEIGHTPROF,
67 67 show=show)
68 68
69 69 nrow, ncol = self.getSubplots()
70 70
71 71 counter = 0
72 72 for y in range(nrow):
73 73 for x in range(ncol):
74 74
75 75 if counter >= self.nplots:
76 76 break
77 77
78 78 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
79 79
80 80 if showprofile:
81 81 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
82 82
83 83 counter += 1
84 84
85 85 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
86 86 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
87 87 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
88 88 server=None, folder=None, username=None, password=None,
89 89 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
90 90 xaxis="frequency", colormap='jet', normFactor=None):
91 91
92 92 """
93 93
94 94 Input:
95 95 dataOut :
96 96 id :
97 97 wintitle :
98 98 channelList :
99 99 showProfile :
100 100 xmin : None,
101 101 xmax : None,
102 102 ymin : None,
103 103 ymax : None,
104 104 zmin : None,
105 105 zmax : None
106 106 """
107 107 if realtime:
108 108 if not(isRealtime(utcdatatime = dataOut.utctime)):
109 109 print 'Skipping this plot function'
110 110 return
111 111
112 112 if channelList == None:
113 113 channelIndexList = dataOut.channelIndexList
114 114 else:
115 115 channelIndexList = []
116 116 for channel in channelList:
117 117 if channel not in dataOut.channelList:
118 118 raise ValueError, "Channel %d is not in dataOut.channelList" %channel
119 119 channelIndexList.append(dataOut.channelList.index(channel))
120 120
121 121 if normFactor is None:
122 122 factor = dataOut.normFactor
123 123 else:
124 124 factor = normFactor
125 125 if xaxis == "frequency":
126 126 x = dataOut.getFreqRange(1)/1000.
127 127 xlabel = "Frequency (kHz)"
128 128
129 129 elif xaxis == "time":
130 130 x = dataOut.getAcfRange(1)
131 131 xlabel = "Time (ms)"
132 132
133 133 else:
134 134 x = dataOut.getVelRange(1)
135 135 xlabel = "Velocity (m/s)"
136 136
137 137 ylabel = "Range (Km)"
138 138
139 139 y = dataOut.getHeiRange()
140 140
141 141 z = dataOut.data_spc/factor
142 142 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
143 143 zdB = 10*numpy.log10(z)
144 144
145 145 avg = numpy.average(z, axis=1)
146 146 avgdB = 10*numpy.log10(avg)
147 147
148 148 noise = dataOut.getNoise()/factor
149 149 noisedB = 10*numpy.log10(noise)
150 150
151 151 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
152 152 title = wintitle + " Spectra"
153 153 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
154 154 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
155 155
156 156 if not self.isConfig:
157 157
158 158 nplots = len(channelIndexList)
159 159
160 160 self.setup(id=id,
161 161 nplots=nplots,
162 162 wintitle=wintitle,
163 163 showprofile=showprofile,
164 164 show=show)
165 165
166 166 if xmin == None: xmin = numpy.nanmin(x)
167 167 if xmax == None: xmax = numpy.nanmax(x)
168 168 if ymin == None: ymin = numpy.nanmin(y)
169 169 if ymax == None: ymax = numpy.nanmax(y)
170 170 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
171 171 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
172 172
173 173 self.FTP_WEI = ftp_wei
174 174 self.EXP_CODE = exp_code
175 175 self.SUB_EXP_CODE = sub_exp_code
176 176 self.PLOT_POS = plot_pos
177 177
178 178 self.isConfig = True
179 179
180 180 self.setWinTitle(title)
181 181
182 182 for i in range(self.nplots):
183 183 index = channelIndexList[i]
184 184 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
185 185 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[index], noisedB[index], str_datetime)
186 186 if len(dataOut.beam.codeList) != 0:
187 187 title = "Ch%d:%4.2fdB,%2.2f,%2.2f:%s" %(dataOut.channelList[index], noisedB[index], dataOut.beam.azimuthList[index], dataOut.beam.zenithList[index], str_datetime)
188 188
189 189 axes = self.axesList[i*self.__nsubplots]
190 190 axes.pcolor(x, y, zdB[index,:,:],
191 191 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
192 192 xlabel=xlabel, ylabel=ylabel, title=title, colormap=colormap,
193 193 ticksize=9, cblabel='')
194 194
195 195 if self.__showprofile:
196 196 axes = self.axesList[i*self.__nsubplots +1]
197 197 axes.pline(avgdB[index,:], y,
198 198 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
199 199 xlabel='dB', ylabel='', title='',
200 200 ytick_visible=False,
201 201 grid='x')
202 202
203 203 noiseline = numpy.repeat(noisedB[index], len(y))
204 204 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
205 205
206 206 self.draw()
207 207
208 208 if figfile == None:
209 209 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
210 210 name = str_datetime
211 211 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
212 212 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
213 213 figfile = self.getFilename(name)
214 214
215 215 self.save(figpath=figpath,
216 216 figfile=figfile,
217 217 save=save,
218 218 ftp=ftp,
219 219 wr_period=wr_period,
220 220 thisDatetime=thisDatetime)
221 221
222 222 class CrossSpectraPlot(Figure):
223 223
224 224 isConfig = None
225 225 __nsubplots = None
226 226
227 227 WIDTH = None
228 228 HEIGHT = None
229 229 WIDTHPROF = None
230 230 HEIGHTPROF = None
231 231 PREFIX = 'cspc'
232 232
233 233 def __init__(self, **kwargs):
234 234 Figure.__init__(self, **kwargs)
235 235 self.isConfig = False
236 236 self.__nsubplots = 4
237 237 self.counter_imagwr = 0
238 238 self.WIDTH = 250
239 239 self.HEIGHT = 250
240 240 self.WIDTHPROF = 0
241 241 self.HEIGHTPROF = 0
242 242
243 243 self.PLOT_CODE = CROSS_CODE
244 244 self.FTP_WEI = None
245 245 self.EXP_CODE = None
246 246 self.SUB_EXP_CODE = None
247 247 self.PLOT_POS = None
248 248
249 249 def getSubplots(self):
250 250
251 251 ncol = 4
252 252 nrow = self.nplots
253 253
254 254 return nrow, ncol
255 255
256 256 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
257 257
258 258 self.__showprofile = showprofile
259 259 self.nplots = nplots
260 260
261 261 ncolspan = 1
262 262 colspan = 1
263 263
264 264 self.createFigure(id = id,
265 265 wintitle = wintitle,
266 266 widthplot = self.WIDTH + self.WIDTHPROF,
267 267 heightplot = self.HEIGHT + self.HEIGHTPROF,
268 268 show=True)
269 269
270 270 nrow, ncol = self.getSubplots()
271 271
272 272 counter = 0
273 273 for y in range(nrow):
274 274 for x in range(ncol):
275 275 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
276 276
277 277 counter += 1
278 278
279 279 def run(self, dataOut, id, wintitle="", pairsList=None,
280 280 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
281 281 coh_min=None, coh_max=None, phase_min=None, phase_max=None,
282 282 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
283 283 power_cmap='jet', coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
284 284 server=None, folder=None, username=None, password=None,
285 285 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, normFactor=None,
286 286 xaxis='frequency'):
287 287
288 288 """
289 289
290 290 Input:
291 291 dataOut :
292 292 id :
293 293 wintitle :
294 294 channelList :
295 295 showProfile :
296 296 xmin : None,
297 297 xmax : None,
298 298 ymin : None,
299 299 ymax : None,
300 300 zmin : None,
301 301 zmax : None
302 302 """
303 303
304 304 if pairsList == None:
305 305 pairsIndexList = dataOut.pairsIndexList
306 306 else:
307 307 pairsIndexList = []
308 308 for pair in pairsList:
309 309 if pair not in dataOut.pairsList:
310 310 raise ValueError, "Pair %s is not in dataOut.pairsList" %str(pair)
311 311 pairsIndexList.append(dataOut.pairsList.index(pair))
312 312
313 313 if not pairsIndexList:
314 314 return
315 315
316 316 if len(pairsIndexList) > 4:
317 317 pairsIndexList = pairsIndexList[0:4]
318 318
319 319 if normFactor is None:
320 320 factor = dataOut.normFactor
321 321 else:
322 322 factor = normFactor
323 323 x = dataOut.getVelRange(1)
324 324 y = dataOut.getHeiRange()
325 325 z = dataOut.data_spc[:,:,:]/factor
326 326 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
327 327
328 328 noise = dataOut.noise/factor
329 329
330 330 zdB = 10*numpy.log10(z)
331 331 noisedB = 10*numpy.log10(noise)
332 332
333 333 if coh_min == None:
334 334 coh_min = 0.0
335 335 if coh_max == None:
336 336 coh_max = 1.0
337 337
338 338 if phase_min == None:
339 339 phase_min = -180
340 340 if phase_max == None:
341 341 phase_max = 180
342 342
343 343 #thisDatetime = dataOut.datatime
344 344 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
345 345 title = wintitle + " Cross-Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
346 346 # xlabel = "Velocity (m/s)"
347 347 ylabel = "Range (Km)"
348 348
349 349 if xaxis == "frequency":
350 350 x = dataOut.getFreqRange(1)/1000.
351 351 xlabel = "Frequency (kHz)"
352 352
353 353 elif xaxis == "time":
354 354 x = dataOut.getAcfRange(1)
355 355 xlabel = "Time (ms)"
356 356
357 357 else:
358 358 x = dataOut.getVelRange(1)
359 359 xlabel = "Velocity (m/s)"
360 360
361 361 if not self.isConfig:
362 362
363 363 nplots = len(pairsIndexList)
364 364
365 365 self.setup(id=id,
366 366 nplots=nplots,
367 367 wintitle=wintitle,
368 368 showprofile=False,
369 369 show=show)
370 370
371 371 avg = numpy.abs(numpy.average(z, axis=1))
372 372 avgdB = 10*numpy.log10(avg)
373 373
374 374 if xmin == None: xmin = numpy.nanmin(x)
375 375 if xmax == None: xmax = numpy.nanmax(x)
376 376 if ymin == None: ymin = numpy.nanmin(y)
377 377 if ymax == None: ymax = numpy.nanmax(y)
378 378 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
379 379 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
380 380
381 381 self.FTP_WEI = ftp_wei
382 382 self.EXP_CODE = exp_code
383 383 self.SUB_EXP_CODE = sub_exp_code
384 384 self.PLOT_POS = plot_pos
385 385
386 386 self.isConfig = True
387 387
388 388 self.setWinTitle(title)
389 389
390 390 for i in range(self.nplots):
391 391 pair = dataOut.pairsList[pairsIndexList[i]]
392 392
393 393 chan_index0 = dataOut.channelList.index(pair[0])
394 394 chan_index1 = dataOut.channelList.index(pair[1])
395 395
396 396 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
397 397 title = "Ch%d: %4.2fdB: %s" %(pair[0], noisedB[chan_index0], str_datetime)
398 398 zdB = 10.*numpy.log10(dataOut.data_spc[chan_index0,:,:]/factor)
399 399 axes0 = self.axesList[i*self.__nsubplots]
400 400 axes0.pcolor(x, y, zdB,
401 401 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
402 402 xlabel=xlabel, ylabel=ylabel, title=title,
403 403 ticksize=9, colormap=power_cmap, cblabel='')
404 404
405 405 title = "Ch%d: %4.2fdB: %s" %(pair[1], noisedB[chan_index1], str_datetime)
406 406 zdB = 10.*numpy.log10(dataOut.data_spc[chan_index1,:,:]/factor)
407 407 axes0 = self.axesList[i*self.__nsubplots+1]
408 408 axes0.pcolor(x, y, zdB,
409 409 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
410 410 xlabel=xlabel, ylabel=ylabel, title=title,
411 411 ticksize=9, colormap=power_cmap, cblabel='')
412 412
413 413 coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[chan_index0,:,:]*dataOut.data_spc[chan_index1,:,:])
414 414 coherence = numpy.abs(coherenceComplex)
415 415 # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi
416 416 phase = numpy.arctan2(coherenceComplex.imag, coherenceComplex.real)*180/numpy.pi
417 417
418 418 title = "Coherence Ch%d * Ch%d" %(pair[0], pair[1])
419 419 axes0 = self.axesList[i*self.__nsubplots+2]
420 420 axes0.pcolor(x, y, coherence,
421 421 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=coh_min, zmax=coh_max,
422 422 xlabel=xlabel, ylabel=ylabel, title=title,
423 423 ticksize=9, colormap=coherence_cmap, cblabel='')
424 424
425 425 title = "Phase Ch%d * Ch%d" %(pair[0], pair[1])
426 426 axes0 = self.axesList[i*self.__nsubplots+3]
427 427 axes0.pcolor(x, y, phase,
428 428 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=phase_min, zmax=phase_max,
429 429 xlabel=xlabel, ylabel=ylabel, title=title,
430 430 ticksize=9, colormap=phase_cmap, cblabel='')
431 431
432 432
433 433
434 434 self.draw()
435 435
436 436 self.save(figpath=figpath,
437 437 figfile=figfile,
438 438 save=save,
439 439 ftp=ftp,
440 440 wr_period=wr_period,
441 441 thisDatetime=thisDatetime)
442 442
443 443
444 444 class RTIPlot(Figure):
445 445
446 446 __isConfig = None
447 447 __nsubplots = None
448 448
449 449 WIDTHPROF = None
450 450 HEIGHTPROF = None
451 451 PREFIX = 'rti'
452 452
453 453 def __init__(self, **kwargs):
454 454
455 455 Figure.__init__(self, **kwargs)
456 456 self.timerange = None
457 457 self.isConfig = False
458 458 self.__nsubplots = 1
459 459
460 460 self.WIDTH = 800
461 461 self.HEIGHT = 180
462 462 self.WIDTHPROF = 120
463 463 self.HEIGHTPROF = 0
464 464 self.counter_imagwr = 0
465 465
466 466 self.PLOT_CODE = RTI_CODE
467 467
468 468 self.FTP_WEI = None
469 469 self.EXP_CODE = None
470 470 self.SUB_EXP_CODE = None
471 471 self.PLOT_POS = None
472 472 self.tmin = None
473 473 self.tmax = None
474 474
475 475 self.xmin = None
476 476 self.xmax = None
477 477
478 478 self.figfile = None
479 479
480 480 def getSubplots(self):
481 481
482 482 ncol = 1
483 483 nrow = self.nplots
484 484
485 485 return nrow, ncol
486 486
487 487 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
488 488
489 489 self.__showprofile = showprofile
490 490 self.nplots = nplots
491 491
492 492 ncolspan = 1
493 493 colspan = 1
494 494 if showprofile:
495 495 ncolspan = 7
496 496 colspan = 6
497 497 self.__nsubplots = 2
498 498
499 499 self.createFigure(id = id,
500 500 wintitle = wintitle,
501 501 widthplot = self.WIDTH + self.WIDTHPROF,
502 502 heightplot = self.HEIGHT + self.HEIGHTPROF,
503 503 show=show)
504 504
505 505 nrow, ncol = self.getSubplots()
506 506
507 507 counter = 0
508 508 for y in range(nrow):
509 509 for x in range(ncol):
510 510
511 511 if counter >= self.nplots:
512 512 break
513 513
514 514 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
515 515
516 516 if showprofile:
517 517 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
518 518
519 519 counter += 1
520 520
521 521 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
522 522 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
523 523 timerange=None, colormap='jet',
524 524 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
525 525 server=None, folder=None, username=None, password=None,
526 526 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, normFactor=None, HEIGHT=None):
527 527
528 528 """
529 529
530 530 Input:
531 531 dataOut :
532 532 id :
533 533 wintitle :
534 534 channelList :
535 535 showProfile :
536 536 xmin : None,
537 537 xmax : None,
538 538 ymin : None,
539 539 ymax : None,
540 540 zmin : None,
541 541 zmax : None
542 542 """
543 543
544 544 #colormap = kwargs.get('colormap', 'jet')
545 545 if HEIGHT is not None:
546 546 self.HEIGHT = HEIGHT
547 547
548 548 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
549 549 return
550 550
551 551 if channelList == None:
552 552 channelIndexList = dataOut.channelIndexList
553 553 else:
554 554 channelIndexList = []
555 555 for channel in channelList:
556 556 if channel not in dataOut.channelList:
557 557 raise ValueError, "Channel %d is not in dataOut.channelList"
558 558 channelIndexList.append(dataOut.channelList.index(channel))
559 559
560 560 if normFactor is None:
561 561 factor = dataOut.normFactor
562 562 else:
563 563 factor = normFactor
564 564
565 565 # factor = dataOut.normFactor
566 566 x = dataOut.getTimeRange()
567 567 y = dataOut.getHeiRange()
568 568
569 569 z = dataOut.data_spc/factor
570 570 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
571 571 avg = numpy.average(z, axis=1)
572 572 avgdB = 10.*numpy.log10(avg)
573 573 # avgdB = dataOut.getPower()
574 574
575 575
576 576 thisDatetime = dataOut.datatime
577 577 # thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
578 578 title = wintitle + " RTI" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
579 579 xlabel = ""
580 580 ylabel = "Range (Km)"
581 581
582 582 update_figfile = False
583 583
584 584 if dataOut.ltctime >= self.xmax:
585 585 self.counter_imagwr = wr_period
586 586 self.isConfig = False
587 587 update_figfile = True
588 588
589 589 if not self.isConfig:
590 590
591 591 nplots = len(channelIndexList)
592 592
593 593 self.setup(id=id,
594 594 nplots=nplots,
595 595 wintitle=wintitle,
596 596 showprofile=showprofile,
597 597 show=show)
598 598
599 599 if timerange != None:
600 600 self.timerange = timerange
601 601
602 602 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
603 603
604 604 noise = dataOut.noise/factor
605 605 noisedB = 10*numpy.log10(noise)
606 606
607 607 if ymin == None: ymin = numpy.nanmin(y)
608 608 if ymax == None: ymax = numpy.nanmax(y)
609 609 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
610 610 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
611 611
612 612 self.FTP_WEI = ftp_wei
613 613 self.EXP_CODE = exp_code
614 614 self.SUB_EXP_CODE = sub_exp_code
615 615 self.PLOT_POS = plot_pos
616 616
617 617 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
618 618 self.isConfig = True
619 619 self.figfile = figfile
620 620 update_figfile = True
621 621
622 622 self.setWinTitle(title)
623 623
624 624 for i in range(self.nplots):
625 625 index = channelIndexList[i]
626 626 title = "Channel %d: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
627 627 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
628 628 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
629 629 axes = self.axesList[i*self.__nsubplots]
630 630 zdB = avgdB[index].reshape((1,-1))
631 631 axes.pcolorbuffer(x, y, zdB,
632 632 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
633 633 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
634 634 ticksize=9, cblabel='', cbsize="1%", colormap=colormap)
635 635
636 636 if self.__showprofile:
637 637 axes = self.axesList[i*self.__nsubplots +1]
638 638 axes.pline(avgdB[index], y,
639 639 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
640 640 xlabel='dB', ylabel='', title='',
641 641 ytick_visible=False,
642 642 grid='x')
643 643
644 644 self.draw()
645 645
646 646 self.save(figpath=figpath,
647 647 figfile=figfile,
648 648 save=save,
649 649 ftp=ftp,
650 650 wr_period=wr_period,
651 651 thisDatetime=thisDatetime,
652 652 update_figfile=update_figfile)
653 653
654 654 class CoherenceMap(Figure):
655 655 isConfig = None
656 656 __nsubplots = None
657 657
658 658 WIDTHPROF = None
659 659 HEIGHTPROF = None
660 660 PREFIX = 'cmap'
661 661
662 662 def __init__(self, **kwargs):
663 663 Figure.__init__(self, **kwargs)
664 664 self.timerange = 2*60*60
665 665 self.isConfig = False
666 666 self.__nsubplots = 1
667 667
668 668 self.WIDTH = 800
669 669 self.HEIGHT = 180
670 670 self.WIDTHPROF = 120
671 671 self.HEIGHTPROF = 0
672 672 self.counter_imagwr = 0
673 673
674 674 self.PLOT_CODE = COH_CODE
675 675
676 676 self.FTP_WEI = None
677 677 self.EXP_CODE = None
678 678 self.SUB_EXP_CODE = None
679 679 self.PLOT_POS = None
680 680 self.counter_imagwr = 0
681 681
682 682 self.xmin = None
683 683 self.xmax = None
684 684
685 685 def getSubplots(self):
686 686 ncol = 1
687 687 nrow = self.nplots*2
688 688
689 689 return nrow, ncol
690 690
691 691 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
692 692 self.__showprofile = showprofile
693 693 self.nplots = nplots
694 694
695 695 ncolspan = 1
696 696 colspan = 1
697 697 if showprofile:
698 698 ncolspan = 7
699 699 colspan = 6
700 700 self.__nsubplots = 2
701 701
702 702 self.createFigure(id = id,
703 703 wintitle = wintitle,
704 704 widthplot = self.WIDTH + self.WIDTHPROF,
705 705 heightplot = self.HEIGHT + self.HEIGHTPROF,
706 706 show=True)
707 707
708 708 nrow, ncol = self.getSubplots()
709 709
710 710 for y in range(nrow):
711 711 for x in range(ncol):
712 712
713 713 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
714 714
715 715 if showprofile:
716 716 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
717 717
718 718 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
719 719 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
720 720 timerange=None, phase_min=None, phase_max=None,
721 721 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
722 722 coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
723 723 server=None, folder=None, username=None, password=None,
724 724 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
725 725
726 726 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
727 727 return
728 728
729 729 if pairsList == None:
730 730 pairsIndexList = dataOut.pairsIndexList
731 731 else:
732 732 pairsIndexList = []
733 733 for pair in pairsList:
734 734 if pair not in dataOut.pairsList:
735 735 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
736 736 pairsIndexList.append(dataOut.pairsList.index(pair))
737 737
738 738 if pairsIndexList == []:
739 739 return
740 740
741 741 if len(pairsIndexList) > 4:
742 742 pairsIndexList = pairsIndexList[0:4]
743 743
744 744 if phase_min == None:
745 745 phase_min = -180
746 746 if phase_max == None:
747 747 phase_max = 180
748 748
749 749 x = dataOut.getTimeRange()
750 750 y = dataOut.getHeiRange()
751 751
752 752 thisDatetime = dataOut.datatime
753 753
754 754 title = wintitle + " CoherenceMap" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
755 755 xlabel = ""
756 756 ylabel = "Range (Km)"
757 757 update_figfile = False
758 758
759 759 if not self.isConfig:
760 760 nplots = len(pairsIndexList)
761 761 self.setup(id=id,
762 762 nplots=nplots,
763 763 wintitle=wintitle,
764 764 showprofile=showprofile,
765 765 show=show)
766 766
767 767 if timerange != None:
768 768 self.timerange = timerange
769 769
770 770 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
771 771
772 772 if ymin == None: ymin = numpy.nanmin(y)
773 773 if ymax == None: ymax = numpy.nanmax(y)
774 774 if zmin == None: zmin = 0.
775 775 if zmax == None: zmax = 1.
776 776
777 777 self.FTP_WEI = ftp_wei
778 778 self.EXP_CODE = exp_code
779 779 self.SUB_EXP_CODE = sub_exp_code
780 780 self.PLOT_POS = plot_pos
781 781
782 782 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
783 783
784 784 self.isConfig = True
785 785 update_figfile = True
786 786
787 787 self.setWinTitle(title)
788 788
789 789 for i in range(self.nplots):
790 790
791 791 pair = dataOut.pairsList[pairsIndexList[i]]
792 792
793 793 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i],:,:],axis=0)
794 794 powa = numpy.average(dataOut.data_spc[pair[0],:,:],axis=0)
795 795 powb = numpy.average(dataOut.data_spc[pair[1],:,:],axis=0)
796 796
797 797
798 798 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
799 799 coherence = numpy.abs(avgcoherenceComplex)
800 800
801 801 z = coherence.reshape((1,-1))
802 802
803 803 counter = 0
804 804
805 805 title = "Coherence Ch%d * Ch%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
806 806 axes = self.axesList[i*self.__nsubplots*2]
807 807 axes.pcolorbuffer(x, y, z,
808 808 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
809 809 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
810 810 ticksize=9, cblabel='', colormap=coherence_cmap, cbsize="1%")
811 811
812 812 if self.__showprofile:
813 813 counter += 1
814 814 axes = self.axesList[i*self.__nsubplots*2 + counter]
815 815 axes.pline(coherence, y,
816 816 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
817 817 xlabel='', ylabel='', title='', ticksize=7,
818 818 ytick_visible=False, nxticks=5,
819 819 grid='x')
820 820
821 821 counter += 1
822 822
823 823 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
824 824
825 825 z = phase.reshape((1,-1))
826 826
827 827 title = "Phase Ch%d * Ch%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
828 828 axes = self.axesList[i*self.__nsubplots*2 + counter]
829 829 axes.pcolorbuffer(x, y, z,
830 830 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=phase_min, zmax=phase_max,
831 831 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
832 832 ticksize=9, cblabel='', colormap=phase_cmap, cbsize="1%")
833 833
834 834 if self.__showprofile:
835 835 counter += 1
836 836 axes = self.axesList[i*self.__nsubplots*2 + counter]
837 837 axes.pline(phase, y,
838 838 xmin=phase_min, xmax=phase_max, ymin=ymin, ymax=ymax,
839 839 xlabel='', ylabel='', title='', ticksize=7,
840 840 ytick_visible=False, nxticks=4,
841 841 grid='x')
842 842
843 843 self.draw()
844 844
845 845 if dataOut.ltctime >= self.xmax:
846 846 self.counter_imagwr = wr_period
847 847 self.isConfig = False
848 848 update_figfile = True
849 849
850 850 self.save(figpath=figpath,
851 851 figfile=figfile,
852 852 save=save,
853 853 ftp=ftp,
854 854 wr_period=wr_period,
855 855 thisDatetime=thisDatetime,
856 856 update_figfile=update_figfile)
857 857
858 858 class PowerProfilePlot(Figure):
859 859
860 860 isConfig = None
861 861 __nsubplots = None
862 862
863 863 WIDTHPROF = None
864 864 HEIGHTPROF = None
865 865 PREFIX = 'spcprofile'
866 866
867 867 def __init__(self, **kwargs):
868 868 Figure.__init__(self, **kwargs)
869 869 self.isConfig = False
870 870 self.__nsubplots = 1
871 871
872 872 self.PLOT_CODE = POWER_CODE
873 873
874 874 self.WIDTH = 300
875 875 self.HEIGHT = 500
876 876 self.counter_imagwr = 0
877 877
878 878 def getSubplots(self):
879 879 ncol = 1
880 880 nrow = 1
881 881
882 882 return nrow, ncol
883 883
884 884 def setup(self, id, nplots, wintitle, show):
885 885
886 886 self.nplots = nplots
887 887
888 888 ncolspan = 1
889 889 colspan = 1
890 890
891 891 self.createFigure(id = id,
892 892 wintitle = wintitle,
893 893 widthplot = self.WIDTH,
894 894 heightplot = self.HEIGHT,
895 895 show=show)
896 896
897 897 nrow, ncol = self.getSubplots()
898 898
899 899 counter = 0
900 900 for y in range(nrow):
901 901 for x in range(ncol):
902 902 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
903 903
904 904 def run(self, dataOut, id, wintitle="", channelList=None,
905 905 xmin=None, xmax=None, ymin=None, ymax=None,
906 906 save=False, figpath='./', figfile=None, show=True,
907 907 ftp=False, wr_period=1, server=None,
908 908 folder=None, username=None, password=None):
909 909
910 910
911 911 if channelList == None:
912 912 channelIndexList = dataOut.channelIndexList
913 913 channelList = dataOut.channelList
914 914 else:
915 915 channelIndexList = []
916 916 for channel in channelList:
917 917 if channel not in dataOut.channelList:
918 918 raise ValueError, "Channel %d is not in dataOut.channelList"
919 919 channelIndexList.append(dataOut.channelList.index(channel))
920 920
921 921 factor = dataOut.normFactor
922 922
923 923 y = dataOut.getHeiRange()
924 924
925 925 #for voltage
926 926 if dataOut.type == 'Voltage':
927 927 x = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:])
928 928 x = x.real
929 929 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
930 930
931 931 #for spectra
932 932 if dataOut.type == 'Spectra':
933 933 x = dataOut.data_spc[channelIndexList,:,:]/factor
934 934 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
935 935 x = numpy.average(x, axis=1)
936 936
937 937
938 938 xdB = 10*numpy.log10(x)
939 939
940 940 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
941 941 title = wintitle + " Power Profile %s" %(thisDatetime.strftime("%d-%b-%Y"))
942 942 xlabel = "dB"
943 943 ylabel = "Range (Km)"
944 944
945 945 if not self.isConfig:
946 946
947 947 nplots = 1
948 948
949 949 self.setup(id=id,
950 950 nplots=nplots,
951 951 wintitle=wintitle,
952 952 show=show)
953 953
954 954 if ymin == None: ymin = numpy.nanmin(y)
955 955 if ymax == None: ymax = numpy.nanmax(y)
956 956 if xmin == None: xmin = numpy.nanmin(xdB)*0.9
957 957 if xmax == None: xmax = numpy.nanmax(xdB)*1.1
958 958
959 959 self.isConfig = True
960 960
961 961 self.setWinTitle(title)
962 962
963 963 title = "Power Profile: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
964 964 axes = self.axesList[0]
965 965
966 966 legendlabels = ["channel %d"%x for x in channelList]
967 967 axes.pmultiline(xdB, y,
968 968 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
969 969 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
970 970 ytick_visible=True, nxticks=5,
971 971 grid='x')
972 972
973 973 self.draw()
974 974
975 975 self.save(figpath=figpath,
976 976 figfile=figfile,
977 977 save=save,
978 978 ftp=ftp,
979 979 wr_period=wr_period,
980 980 thisDatetime=thisDatetime)
981 981
982 982 class SpectraCutPlot(Figure):
983 983
984 984 isConfig = None
985 985 __nsubplots = None
986 986
987 987 WIDTHPROF = None
988 988 HEIGHTPROF = None
989 989 PREFIX = 'spc_cut'
990 990
991 991 def __init__(self, **kwargs):
992 992 Figure.__init__(self, **kwargs)
993 993 self.isConfig = False
994 994 self.__nsubplots = 1
995 995
996 996 self.PLOT_CODE = POWER_CODE
997 997
998 998 self.WIDTH = 700
999 999 self.HEIGHT = 500
1000 1000 self.counter_imagwr = 0
1001 1001
1002 1002 def getSubplots(self):
1003 1003 ncol = 1
1004 1004 nrow = 1
1005 1005
1006 1006 return nrow, ncol
1007 1007
1008 1008 def setup(self, id, nplots, wintitle, show):
1009 1009
1010 1010 self.nplots = nplots
1011 1011
1012 1012 ncolspan = 1
1013 1013 colspan = 1
1014 1014
1015 1015 self.createFigure(id = id,
1016 1016 wintitle = wintitle,
1017 1017 widthplot = self.WIDTH,
1018 1018 heightplot = self.HEIGHT,
1019 1019 show=show)
1020 1020
1021 1021 nrow, ncol = self.getSubplots()
1022 1022
1023 1023 counter = 0
1024 1024 for y in range(nrow):
1025 1025 for x in range(ncol):
1026 1026 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1027 1027
1028 1028 def run(self, dataOut, id, wintitle="", channelList=None,
1029 1029 xmin=None, xmax=None, ymin=None, ymax=None,
1030 1030 save=False, figpath='./', figfile=None, show=True,
1031 1031 ftp=False, wr_period=1, server=None,
1032 1032 folder=None, username=None, password=None,
1033 1033 xaxis="frequency"):
1034 1034
1035 1035
1036 1036 if channelList == None:
1037 1037 channelIndexList = dataOut.channelIndexList
1038 1038 channelList = dataOut.channelList
1039 1039 else:
1040 1040 channelIndexList = []
1041 1041 for channel in channelList:
1042 1042 if channel not in dataOut.channelList:
1043 1043 raise ValueError, "Channel %d is not in dataOut.channelList"
1044 1044 channelIndexList.append(dataOut.channelList.index(channel))
1045 1045
1046 1046 factor = dataOut.normFactor
1047 1047
1048 1048 y = dataOut.getHeiRange()
1049 1049
1050 1050 z = dataOut.data_spc/factor
1051 1051 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
1052 1052
1053 1053 hei_index = numpy.arange(25)*3 + 20
1054 1054
1055 1055 if xaxis == "frequency":
1056 1056 x = dataOut.getFreqRange()/1000.
1057 1057 zdB = 10*numpy.log10(z[0,:,hei_index])
1058 1058 xlabel = "Frequency (kHz)"
1059 1059 ylabel = "Power (dB)"
1060 1060
1061 1061 elif xaxis == "time":
1062 1062 x = dataOut.getAcfRange()
1063 1063 zdB = z[0,:,hei_index]
1064 1064 xlabel = "Time (ms)"
1065 1065 ylabel = "ACF"
1066 1066
1067 1067 else:
1068 1068 x = dataOut.getVelRange()
1069 1069 zdB = 10*numpy.log10(z[0,:,hei_index])
1070 1070 xlabel = "Velocity (m/s)"
1071 1071 ylabel = "Power (dB)"
1072 1072
1073 1073 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1074 1074 title = wintitle + " Range Cuts %s" %(thisDatetime.strftime("%d-%b-%Y"))
1075 1075
1076 1076 if not self.isConfig:
1077 1077
1078 1078 nplots = 1
1079 1079
1080 1080 self.setup(id=id,
1081 1081 nplots=nplots,
1082 1082 wintitle=wintitle,
1083 1083 show=show)
1084 1084
1085 1085 if xmin == None: xmin = numpy.nanmin(x)*0.9
1086 1086 if xmax == None: xmax = numpy.nanmax(x)*1.1
1087 1087 if ymin == None: ymin = numpy.nanmin(zdB)
1088 1088 if ymax == None: ymax = numpy.nanmax(zdB)
1089 1089
1090 1090 self.isConfig = True
1091 1091
1092 1092 self.setWinTitle(title)
1093 1093
1094 1094 title = "Spectra Cuts: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
1095 1095 axes = self.axesList[0]
1096 1096
1097 1097 legendlabels = ["Range = %dKm" %y[i] for i in hei_index]
1098 1098
1099 1099 axes.pmultilineyaxis( x, zdB,
1100 1100 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1101 1101 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
1102 1102 ytick_visible=True, nxticks=5,
1103 1103 grid='x')
1104 1104
1105 1105 self.draw()
1106 1106
1107 1107 self.save(figpath=figpath,
1108 1108 figfile=figfile,
1109 1109 save=save,
1110 1110 ftp=ftp,
1111 1111 wr_period=wr_period,
1112 1112 thisDatetime=thisDatetime)
1113 1113
1114 1114 class Noise(Figure):
1115 1115
1116 1116 isConfig = None
1117 1117 __nsubplots = None
1118 1118
1119 1119 PREFIX = 'noise'
1120 1120
1121 1121
1122 1122 def __init__(self, **kwargs):
1123 1123 Figure.__init__(self, **kwargs)
1124 1124 self.timerange = 24*60*60
1125 1125 self.isConfig = False
1126 1126 self.__nsubplots = 1
1127 1127 self.counter_imagwr = 0
1128 1128 self.WIDTH = 800
1129 1129 self.HEIGHT = 400
1130 1130 self.WIDTHPROF = 120
1131 1131 self.HEIGHTPROF = 0
1132 1132 self.xdata = None
1133 1133 self.ydata = None
1134 1134
1135 1135 self.PLOT_CODE = NOISE_CODE
1136 1136
1137 1137 self.FTP_WEI = None
1138 1138 self.EXP_CODE = None
1139 1139 self.SUB_EXP_CODE = None
1140 1140 self.PLOT_POS = None
1141 1141 self.figfile = None
1142 1142
1143 1143 self.xmin = None
1144 1144 self.xmax = None
1145 1145
1146 1146 def getSubplots(self):
1147 1147
1148 1148 ncol = 1
1149 1149 nrow = 1
1150 1150
1151 1151 return nrow, ncol
1152 1152
1153 1153 def openfile(self, filename):
1154 1154 dirname = os.path.dirname(filename)
1155 1155
1156 1156 if not os.path.exists(dirname):
1157 1157 os.mkdir(dirname)
1158 1158
1159 1159 f = open(filename,'w+')
1160 1160 f.write('\n\n')
1161 1161 f.write('JICAMARCA RADIO OBSERVATORY - Noise \n')
1162 1162 f.write('DD MM YYYY HH MM SS Channel0 Channel1 Channel2 Channel3\n\n' )
1163 1163 f.close()
1164 1164
1165 1165 def save_data(self, filename_phase, data, data_datetime):
1166 1166
1167 1167 f=open(filename_phase,'a')
1168 1168
1169 1169 timetuple_data = data_datetime.timetuple()
1170 1170 day = str(timetuple_data.tm_mday)
1171 1171 month = str(timetuple_data.tm_mon)
1172 1172 year = str(timetuple_data.tm_year)
1173 1173 hour = str(timetuple_data.tm_hour)
1174 1174 minute = str(timetuple_data.tm_min)
1175 1175 second = str(timetuple_data.tm_sec)
1176 1176
1177 1177 data_msg = ''
1178 1178 for i in range(len(data)):
1179 1179 data_msg += str(data[i]) + ' '
1180 1180
1181 1181 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' ' + data_msg + '\n')
1182 1182 f.close()
1183 1183
1184 1184
1185 1185 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1186 1186
1187 1187 self.__showprofile = showprofile
1188 1188 self.nplots = nplots
1189 1189
1190 1190 ncolspan = 7
1191 1191 colspan = 6
1192 1192 self.__nsubplots = 2
1193 1193
1194 1194 self.createFigure(id = id,
1195 1195 wintitle = wintitle,
1196 1196 widthplot = self.WIDTH+self.WIDTHPROF,
1197 1197 heightplot = self.HEIGHT+self.HEIGHTPROF,
1198 1198 show=show)
1199 1199
1200 1200 nrow, ncol = self.getSubplots()
1201 1201
1202 1202 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1203 1203
1204 1204
1205 1205 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
1206 1206 xmin=None, xmax=None, ymin=None, ymax=None,
1207 1207 timerange=None,
1208 1208 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1209 1209 server=None, folder=None, username=None, password=None,
1210 1210 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1211 1211
1212 1212 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
1213 1213 return
1214 1214
1215 1215 if channelList == None:
1216 1216 channelIndexList = dataOut.channelIndexList
1217 1217 channelList = dataOut.channelList
1218 1218 else:
1219 1219 channelIndexList = []
1220 1220 for channel in channelList:
1221 1221 if channel not in dataOut.channelList:
1222 1222 raise ValueError, "Channel %d is not in dataOut.channelList"
1223 1223 channelIndexList.append(dataOut.channelList.index(channel))
1224 1224
1225 1225 x = dataOut.getTimeRange()
1226 1226 #y = dataOut.getHeiRange()
1227 1227 factor = dataOut.normFactor
1228 1228 noise = dataOut.noise[channelIndexList]/factor
1229 1229 noisedB = 10*numpy.log10(noise)
1230 1230
1231 1231 thisDatetime = dataOut.datatime
1232 1232
1233 1233 title = wintitle + " Noise" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1234 1234 xlabel = ""
1235 1235 ylabel = "Intensity (dB)"
1236 1236 update_figfile = False
1237 1237
1238 1238 if not self.isConfig:
1239 1239
1240 1240 nplots = 1
1241 1241
1242 1242 self.setup(id=id,
1243 1243 nplots=nplots,
1244 1244 wintitle=wintitle,
1245 1245 showprofile=showprofile,
1246 1246 show=show)
1247 1247
1248 1248 if timerange != None:
1249 1249 self.timerange = timerange
1250 1250
1251 1251 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1252 1252
1253 1253 if ymin == None: ymin = numpy.floor(numpy.nanmin(noisedB)) - 10.0
1254 1254 if ymax == None: ymax = numpy.nanmax(noisedB) + 10.0
1255 1255
1256 1256 self.FTP_WEI = ftp_wei
1257 1257 self.EXP_CODE = exp_code
1258 1258 self.SUB_EXP_CODE = sub_exp_code
1259 1259 self.PLOT_POS = plot_pos
1260 1260
1261 1261
1262 1262 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1263 1263 self.isConfig = True
1264 1264 self.figfile = figfile
1265 1265 self.xdata = numpy.array([])
1266 1266 self.ydata = numpy.array([])
1267 1267
1268 1268 update_figfile = True
1269 1269
1270 1270 #open file beacon phase
1271 1271 path = '%s%03d' %(self.PREFIX, self.id)
1272 1272 noise_file = os.path.join(path,'%s.txt'%self.name)
1273 1273 self.filename_noise = os.path.join(figpath,noise_file)
1274 1274
1275 1275 self.setWinTitle(title)
1276 1276
1277 1277 title = "Noise %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1278 1278
1279 1279 legendlabels = ["channel %d"%(idchannel) for idchannel in channelList]
1280 1280 axes = self.axesList[0]
1281 1281
1282 1282 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1283 1283
1284 1284 if len(self.ydata)==0:
1285 1285 self.ydata = noisedB.reshape(-1,1)
1286 1286 else:
1287 1287 self.ydata = numpy.hstack((self.ydata, noisedB.reshape(-1,1)))
1288 1288
1289 1289
1290 1290 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1291 1291 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1292 1292 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1293 1293 XAxisAsTime=True, grid='both'
1294 1294 )
1295 1295
1296 1296 self.draw()
1297 1297
1298 1298 if dataOut.ltctime >= self.xmax:
1299 1299 self.counter_imagwr = wr_period
1300 1300 self.isConfig = False
1301 1301 update_figfile = True
1302 1302
1303 1303 self.save(figpath=figpath,
1304 1304 figfile=figfile,
1305 1305 save=save,
1306 1306 ftp=ftp,
1307 1307 wr_period=wr_period,
1308 1308 thisDatetime=thisDatetime,
1309 1309 update_figfile=update_figfile)
1310 1310
1311 1311 #store data beacon phase
1312 1312 if save:
1313 1313 self.save_data(self.filename_noise, noisedB, thisDatetime)
1314 1314
1315 1315 class BeaconPhase(Figure):
1316 1316
1317 1317 __isConfig = None
1318 1318 __nsubplots = None
1319 1319
1320 1320 PREFIX = 'beacon_phase'
1321 1321
1322 1322 def __init__(self, **kwargs):
1323 1323 Figure.__init__(self, **kwargs)
1324 1324 self.timerange = 24*60*60
1325 1325 self.isConfig = False
1326 1326 self.__nsubplots = 1
1327 1327 self.counter_imagwr = 0
1328 1328 self.WIDTH = 800
1329 1329 self.HEIGHT = 400
1330 1330 self.WIDTHPROF = 120
1331 1331 self.HEIGHTPROF = 0
1332 1332 self.xdata = None
1333 1333 self.ydata = None
1334 1334
1335 1335 self.PLOT_CODE = BEACON_CODE
1336 1336
1337 1337 self.FTP_WEI = None
1338 1338 self.EXP_CODE = None
1339 1339 self.SUB_EXP_CODE = None
1340 1340 self.PLOT_POS = None
1341 1341
1342 1342 self.filename_phase = None
1343 1343
1344 1344 self.figfile = None
1345 1345
1346 1346 self.xmin = None
1347 1347 self.xmax = None
1348 1348
1349 1349 def getSubplots(self):
1350 1350
1351 1351 ncol = 1
1352 1352 nrow = 1
1353 1353
1354 1354 return nrow, ncol
1355 1355
1356 1356 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1357 1357
1358 1358 self.__showprofile = showprofile
1359 1359 self.nplots = nplots
1360 1360
1361 1361 ncolspan = 7
1362 1362 colspan = 6
1363 1363 self.__nsubplots = 2
1364 1364
1365 1365 self.createFigure(id = id,
1366 1366 wintitle = wintitle,
1367 1367 widthplot = self.WIDTH+self.WIDTHPROF,
1368 1368 heightplot = self.HEIGHT+self.HEIGHTPROF,
1369 1369 show=show)
1370 1370
1371 1371 nrow, ncol = self.getSubplots()
1372 1372
1373 1373 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1374 1374
1375 1375 def save_phase(self, filename_phase):
1376 1376 f = open(filename_phase,'w+')
1377 1377 f.write('\n\n')
1378 1378 f.write('JICAMARCA RADIO OBSERVATORY - Beacon Phase \n')
1379 1379 f.write('DD MM YYYY HH MM SS pair(2,0) pair(2,1) pair(2,3) pair(2,4)\n\n' )
1380 1380 f.close()
1381 1381
1382 1382 def save_data(self, filename_phase, data, data_datetime):
1383 1383 f=open(filename_phase,'a')
1384 1384 timetuple_data = data_datetime.timetuple()
1385 1385 day = str(timetuple_data.tm_mday)
1386 1386 month = str(timetuple_data.tm_mon)
1387 1387 year = str(timetuple_data.tm_year)
1388 1388 hour = str(timetuple_data.tm_hour)
1389 1389 minute = str(timetuple_data.tm_min)
1390 1390 second = str(timetuple_data.tm_sec)
1391 1391 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' '+str(data[0])+' '+str(data[1])+' '+str(data[2])+' '+str(data[3])+'\n')
1392 1392 f.close()
1393 1393
1394 1394
1395 1395 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
1396 1396 xmin=None, xmax=None, ymin=None, ymax=None, hmin=None, hmax=None,
1397 1397 timerange=None,
1398 1398 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1399 1399 server=None, folder=None, username=None, password=None,
1400 1400 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1401 1401
1402 1402 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
1403 1403 return
1404 1404
1405 1405 if pairsList == None:
1406 1406 pairsIndexList = dataOut.pairsIndexList[:10]
1407 1407 else:
1408 1408 pairsIndexList = []
1409 1409 for pair in pairsList:
1410 1410 if pair not in dataOut.pairsList:
1411 1411 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
1412 1412 pairsIndexList.append(dataOut.pairsList.index(pair))
1413 1413
1414 1414 if pairsIndexList == []:
1415 1415 return
1416 1416
1417 1417 # if len(pairsIndexList) > 4:
1418 1418 # pairsIndexList = pairsIndexList[0:4]
1419 1419
1420 1420 hmin_index = None
1421 1421 hmax_index = None
1422 1422
1423 1423 if hmin != None and hmax != None:
1424 1424 indexes = numpy.arange(dataOut.nHeights)
1425 1425 hmin_list = indexes[dataOut.heightList >= hmin]
1426 1426 hmax_list = indexes[dataOut.heightList <= hmax]
1427 1427
1428 1428 if hmin_list.any():
1429 1429 hmin_index = hmin_list[0]
1430 1430
1431 1431 if hmax_list.any():
1432 1432 hmax_index = hmax_list[-1]+1
1433 1433
1434 1434 x = dataOut.getTimeRange()
1435 1435 #y = dataOut.getHeiRange()
1436 1436
1437 1437
1438 1438 thisDatetime = dataOut.datatime
1439 1439
1440 1440 title = wintitle + " Signal Phase" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1441 1441 xlabel = "Local Time"
1442 1442 ylabel = "Phase (degrees)"
1443 1443
1444 1444 update_figfile = False
1445 1445
1446 1446 nplots = len(pairsIndexList)
1447 1447 #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList)))
1448 1448 phase_beacon = numpy.zeros(len(pairsIndexList))
1449 1449 for i in range(nplots):
1450 1450 pair = dataOut.pairsList[pairsIndexList[i]]
1451 1451 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i], :, hmin_index:hmax_index], axis=0)
1452 1452 powa = numpy.average(dataOut.data_spc[pair[0], :, hmin_index:hmax_index], axis=0)
1453 1453 powb = numpy.average(dataOut.data_spc[pair[1], :, hmin_index:hmax_index], axis=0)
1454 1454 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
1455 1455 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
1456 1456
1457 1457 #print "Phase %d%d" %(pair[0], pair[1])
1458 1458 #print phase[dataOut.beacon_heiIndexList]
1459 1459
1460 1460 if dataOut.beacon_heiIndexList:
1461 1461 phase_beacon[i] = numpy.average(phase[dataOut.beacon_heiIndexList])
1462 1462 else:
1463 1463 phase_beacon[i] = numpy.average(phase)
1464 1464
1465 1465 if not self.isConfig:
1466 1466
1467 1467 nplots = len(pairsIndexList)
1468 1468
1469 1469 self.setup(id=id,
1470 1470 nplots=nplots,
1471 1471 wintitle=wintitle,
1472 1472 showprofile=showprofile,
1473 1473 show=show)
1474 1474
1475 1475 if timerange != None:
1476 1476 self.timerange = timerange
1477 1477
1478 1478 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1479 1479
1480 1480 if ymin == None: ymin = 0
1481 1481 if ymax == None: ymax = 360
1482 1482
1483 1483 self.FTP_WEI = ftp_wei
1484 1484 self.EXP_CODE = exp_code
1485 1485 self.SUB_EXP_CODE = sub_exp_code
1486 1486 self.PLOT_POS = plot_pos
1487 1487
1488 1488 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1489 1489 self.isConfig = True
1490 1490 self.figfile = figfile
1491 1491 self.xdata = numpy.array([])
1492 1492 self.ydata = numpy.array([])
1493 1493
1494 1494 update_figfile = True
1495 1495
1496 1496 #open file beacon phase
1497 1497 path = '%s%03d' %(self.PREFIX, self.id)
1498 1498 beacon_file = os.path.join(path,'%s.txt'%self.name)
1499 1499 self.filename_phase = os.path.join(figpath,beacon_file)
1500 1500 #self.save_phase(self.filename_phase)
1501 1501
1502 1502
1503 1503 #store data beacon phase
1504 1504 #self.save_data(self.filename_phase, phase_beacon, thisDatetime)
1505 1505
1506 1506 self.setWinTitle(title)
1507 1507
1508 1508
1509 1509 title = "Phase Plot %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1510 1510
1511 1511 legendlabels = ["Pair (%d,%d)"%(pair[0], pair[1]) for pair in dataOut.pairsList]
1512 1512
1513 1513 axes = self.axesList[0]
1514 1514
1515 1515 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1516 1516
1517 1517 if len(self.ydata)==0:
1518 1518 self.ydata = phase_beacon.reshape(-1,1)
1519 1519 else:
1520 1520 self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1)))
1521 1521
1522 1522
1523 1523 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1524 1524 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1525 1525 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1526 1526 XAxisAsTime=True, grid='both'
1527 1527 )
1528 1528
1529 1529 self.draw()
1530 1530
1531 1531 if dataOut.ltctime >= self.xmax:
1532 1532 self.counter_imagwr = wr_period
1533 1533 self.isConfig = False
1534 1534 update_figfile = True
1535 1535
1536 1536 self.save(figpath=figpath,
1537 1537 figfile=figfile,
1538 1538 save=save,
1539 1539 ftp=ftp,
1540 1540 wr_period=wr_period,
1541 1541 thisDatetime=thisDatetime,
1542 1542 update_figfile=update_figfile)
@@ -1,225 +1,225
1 1 '''
2 2 Created on Jul 9, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import os
7 7 import datetime
8 8 import numpy
9 9
10 10 from figure import Figure
11 11
12 12 class Scope(Figure):
13 13
14 14 isConfig = None
15 15
16 16 def __init__(self, **kwargs):
17 17 Figure.__init__(self, **kwargs)
18 18 self.isConfig = False
19 19 self.WIDTH = 300
20 20 self.HEIGHT = 200
21 21 self.counter_imagwr = 0
22 22
23 23 def getSubplots(self):
24 24
25 25 nrow = self.nplots
26 26 ncol = 3
27 27 return nrow, ncol
28 28
29 29 def setup(self, id, nplots, wintitle, show):
30 30
31 31 self.nplots = nplots
32 32
33 33 self.createFigure(id=id,
34 34 wintitle=wintitle,
35 35 show=show)
36 36
37 37 nrow,ncol = self.getSubplots()
38 38 colspan = 3
39 39 rowspan = 1
40 40
41 41 for i in range(nplots):
42 42 self.addAxes(nrow, ncol, i, 0, colspan, rowspan)
43 43
44 44 def plot_iq(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax):
45 45 yreal = y[channelIndexList,:].real
46 46 yimag = y[channelIndexList,:].imag
47 47
48 48 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
49 49 xlabel = "Range (Km)"
50 50 ylabel = "Intensity - IQ"
51 51
52 52 if not self.isConfig:
53 53 nplots = len(channelIndexList)
54 54
55 55 self.setup(id=id,
56 56 nplots=nplots,
57 57 wintitle='',
58 58 show=show)
59 59
60 60 if xmin == None: xmin = numpy.nanmin(x)
61 61 if xmax == None: xmax = numpy.nanmax(x)
62 62 if ymin == None: ymin = min(numpy.nanmin(yreal),numpy.nanmin(yimag))
63 63 if ymax == None: ymax = max(numpy.nanmax(yreal),numpy.nanmax(yimag))
64 64
65 65 self.isConfig = True
66 66
67 67 self.setWinTitle(title)
68 68
69 69 for i in range(len(self.axesList)):
70 70 title = "Channel %d" %(i)
71 71 axes = self.axesList[i]
72 72
73 73 axes.pline(x, yreal[i,:],
74 74 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
75 75 xlabel=xlabel, ylabel=ylabel, title=title)
76 76
77 77 axes.addpline(x, yimag[i,:], idline=1, color="red", linestyle="solid", lw=2)
78 78
79 79 def plot_power(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax):
80 80 y = y[channelIndexList,:] * numpy.conjugate(y[channelIndexList,:])
81 81 yreal = y.real
82 82
83 83 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
84 84 xlabel = "Range (Km)"
85 85 ylabel = "Intensity"
86 86
87 87 if not self.isConfig:
88 88 nplots = len(channelIndexList)
89 89
90 90 self.setup(id=id,
91 91 nplots=nplots,
92 92 wintitle='',
93 93 show=show)
94 94
95 95 if xmin == None: xmin = numpy.nanmin(x)
96 96 if xmax == None: xmax = numpy.nanmax(x)
97 97 if ymin == None: ymin = numpy.nanmin(yreal)
98 98 if ymax == None: ymax = numpy.nanmax(yreal)
99 99
100 100 self.isConfig = True
101 101
102 102 self.setWinTitle(title)
103 103
104 104 for i in range(len(self.axesList)):
105 105 title = "Channel %d" %(i)
106 106 axes = self.axesList[i]
107 107 ychannel = yreal[i,:]
108 108 axes.pline(x, ychannel,
109 109 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
110 110 xlabel=xlabel, ylabel=ylabel, title=title)
111 111
112 112
113 113 def run(self, dataOut, id, wintitle="", channelList=None,
114 114 xmin=None, xmax=None, ymin=None, ymax=None, save=False,
115 115 figpath='./', figfile=None, show=True, wr_period=1,
116 ftp=False, server=None, folder=None, username=None, password=None, type='power'):
116 ftp=False, server=None, folder=None, username=None, password=None, type='power', **kwargs):
117 117
118 118 """
119 119
120 120 Input:
121 121 dataOut :
122 122 id :
123 123 wintitle :
124 124 channelList :
125 125 xmin : None,
126 126 xmax : None,
127 127 ymin : None,
128 128 ymax : None,
129 129 """
130 130
131 131 if channelList == None:
132 132 channelIndexList = dataOut.channelIndexList
133 133 else:
134 134 channelIndexList = []
135 135 for channel in channelList:
136 136 if channel not in dataOut.channelList:
137 137 raise ValueError, "Channel %d is not in dataOut.channelList"
138 138 channelIndexList.append(dataOut.channelList.index(channel))
139 139
140 140 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
141 141
142 142 if dataOut.flagDataAsBlock:
143 143
144 144 for i in range(dataOut.nProfiles):
145 145
146 146 wintitle1 = wintitle + " [Profile = %d] " %i
147 147
148 148 if type == "power":
149 149 self.plot_power(dataOut.heightList,
150 150 dataOut.data[:,i,:],
151 151 id,
152 152 channelIndexList,
153 153 thisDatetime,
154 154 wintitle1,
155 155 show,
156 156 xmin,
157 157 xmax,
158 158 ymin,
159 159 ymax)
160 160
161 161 if type == "iq":
162 162 self.plot_iq(dataOut.heightList,
163 163 dataOut.data[:,i,:],
164 164 id,
165 165 channelIndexList,
166 166 thisDatetime,
167 167 wintitle1,
168 168 show,
169 169 xmin,
170 170 xmax,
171 171 ymin,
172 172 ymax)
173 173
174 174 self.draw()
175 175
176 176 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
177 177 figfile = self.getFilename(name = str_datetime) + "_" + str(i)
178 178
179 179 self.save(figpath=figpath,
180 180 figfile=figfile,
181 181 save=save,
182 182 ftp=ftp,
183 183 wr_period=wr_period,
184 184 thisDatetime=thisDatetime)
185 185
186 186 else:
187 187 wintitle += " [Profile = %d] " %dataOut.profileIndex
188 188
189 189 if type == "power":
190 190 self.plot_power(dataOut.heightList,
191 191 dataOut.data,
192 192 id,
193 193 channelIndexList,
194 194 thisDatetime,
195 195 wintitle,
196 196 show,
197 197 xmin,
198 198 xmax,
199 199 ymin,
200 200 ymax)
201 201
202 202 if type == "iq":
203 203 self.plot_iq(dataOut.heightList,
204 204 dataOut.data,
205 205 id,
206 206 channelIndexList,
207 207 thisDatetime,
208 208 wintitle,
209 209 show,
210 210 xmin,
211 211 xmax,
212 212 ymin,
213 213 ymax)
214 214
215 215 self.draw()
216 216
217 217 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S") + "_" + str(dataOut.profileIndex)
218 218 figfile = self.getFilename(name = str_datetime)
219 219
220 220 self.save(figpath=figpath,
221 221 figfile=figfile,
222 222 save=save,
223 223 ftp=ftp,
224 224 wr_period=wr_period,
225 225 thisDatetime=thisDatetime)
@@ -1,481 +1,495
1 1 import numpy
2 2 import datetime
3 3 import sys
4 4 import matplotlib
5 5
6 6 if 'linux' in sys.platform:
7 matplotlib.use("GTK3Agg")
7 matplotlib.use("TkAgg")
8 8
9 9 if 'darwin' in sys.platform:
10 10 matplotlib.use('TKAgg')
11 #Qt4Agg', 'GTK', 'GTKAgg', 'ps', 'agg', 'cairo', 'MacOSX', 'GTKCairo', 'WXAgg', 'template', 'TkAgg', 'GTK3Cairo', 'GTK3Agg', 'svg', 'WebAgg', 'CocoaAgg', 'emf', 'gdk', 'WX'
11 # Qt4Agg', 'GTK', 'GTKAgg', 'ps', 'agg', 'cairo', 'MacOSX', 'GTKCairo', 'WXAgg', 'template', 'TkAgg', 'GTK3Cairo', 'GTK3Agg', 'svg', 'WebAgg', 'CocoaAgg', 'emf', 'gdk', 'WX'
12 12 import matplotlib.pyplot
13 13
14 14 from mpl_toolkits.axes_grid1 import make_axes_locatable
15 15 from matplotlib.ticker import FuncFormatter, LinearLocator
16 16
17 17 ###########################################
18 #Actualizacion de las funciones del driver
18 # Actualizacion de las funciones del driver
19 19 ###########################################
20 20
21 21 # create jro colormap
22 22
23 23 jet_values = matplotlib.pyplot.get_cmap("jet", 100)(numpy.arange(100))[10:90]
24 blu_values = matplotlib.pyplot.get_cmap("seismic_r", 20)(numpy.arange(20))[10:15]
25 ncmap = matplotlib.colors.LinearSegmentedColormap.from_list("jro", numpy.vstack((blu_values, jet_values)))
24 blu_values = matplotlib.pyplot.get_cmap(
25 "seismic_r", 20)(numpy.arange(20))[10:15]
26 ncmap = matplotlib.colors.LinearSegmentedColormap.from_list(
27 "jro", numpy.vstack((blu_values, jet_values)))
26 28 matplotlib.pyplot.register_cmap(cmap=ncmap)
27 29
28 def createFigure(id, wintitle, width, height, facecolor="w", show=True, dpi = 80):
30
31 def createFigure(id, wintitle, width, height, facecolor="w", show=True, dpi=80):
29 32
30 33 matplotlib.pyplot.ioff()
31 34
32 fig = matplotlib.pyplot.figure(num=id, facecolor=facecolor, figsize=(1.0*width/dpi, 1.0*height/dpi))
35 fig = matplotlib.pyplot.figure(num=id, facecolor=facecolor, figsize=(
36 1.0 * width / dpi, 1.0 * height / dpi))
33 37 fig.canvas.manager.set_window_title(wintitle)
34 38 # fig.canvas.manager.resize(width, height)
35 39 matplotlib.pyplot.ion()
36 40
37 41 if show:
38 42 matplotlib.pyplot.show()
39 43
40 44 return fig
41 45
46
42 47 def closeFigure(show=False, fig=None):
43 48
44 # matplotlib.pyplot.ioff()
45 # matplotlib.pyplot.pause(0)
49 # matplotlib.pyplot.ioff()
50 # matplotlib.pyplot.pause(0)
46 51
47 52 if show:
48 53 matplotlib.pyplot.show()
49 54
50 55 if fig != None:
51 56 matplotlib.pyplot.close(fig)
52 57 # matplotlib.pyplot.pause(0)
53 58 # matplotlib.pyplot.ion()
54 59
55 60 return
56 61
57 62 matplotlib.pyplot.close("all")
58 63 # matplotlib.pyplot.pause(0)
59 64 # matplotlib.pyplot.ion()
60 65
61 66 return
62 67
68
63 69 def saveFigure(fig, filename):
64 70
65 # matplotlib.pyplot.ioff()
71 # matplotlib.pyplot.ioff()
66 72 fig.savefig(filename, dpi=matplotlib.pyplot.gcf().dpi)
67 73 # matplotlib.pyplot.ion()
68 74
75
69 76 def clearFigure(fig):
70 77
71 78 fig.clf()
72 79
80
73 81 def setWinTitle(fig, title):
74 82
75 83 fig.canvas.manager.set_window_title(title)
76 84
85
77 86 def setTitle(fig, title):
78 87
79 88 fig.suptitle(title)
80 89
90
81 91 def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan, polar=False):
82 92
83 93 matplotlib.pyplot.ioff()
84 94 matplotlib.pyplot.figure(fig.number)
85 95 axes = matplotlib.pyplot.subplot2grid((nrow, ncol),
86 (xpos, ypos),
87 colspan=colspan,
88 rowspan=rowspan,
89 polar=polar)
90
91 axes.grid(True)
96 (xpos, ypos),
97 colspan=colspan,
98 rowspan=rowspan,
99 polar=polar)
92 100
93 101 matplotlib.pyplot.ion()
94 102 return axes
95 103
104
96 105 def setAxesText(ax, text):
97 106
98 107 ax.annotate(text,
99 xy = (.1, .99),
100 xycoords = 'figure fraction',
101 horizontalalignment = 'left',
102 verticalalignment = 'top',
103 fontsize = 10)
108 xy=(.1, .99),
109 xycoords='figure fraction',
110 horizontalalignment='left',
111 verticalalignment='top',
112 fontsize=10)
113
104 114
105 115 def printLabels(ax, xlabel, ylabel, title):
106 116
107 117 ax.set_xlabel(xlabel, size=11)
108 118 ax.set_ylabel(ylabel, size=11)
109 119 ax.set_title(title, size=8)
110 120
121
111 122 def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='',
112 123 ticksize=9, xtick_visible=True, ytick_visible=True,
113 124 nxticks=4, nyticks=10,
114 grid=None,color='blue'):
115
125 grid=None, color='blue'):
116 126 """
117 127
118 128 Input:
119 129 grid : None, 'both', 'x', 'y'
120 130 """
121 131
122 132 matplotlib.pyplot.ioff()
123 133
124 ax.set_xlim([xmin,xmax])
125 ax.set_ylim([ymin,ymax])
134 ax.set_xlim([xmin, xmax])
135 ax.set_ylim([ymin, ymax])
126 136
127 137 printLabels(ax, xlabel, ylabel, title)
128 138
129 139 ######################################################
130 if (xmax-xmin)<=1:
131 xtickspos = numpy.linspace(xmin,xmax,nxticks)
132 xtickspos = numpy.array([float("%.1f"%i) for i in xtickspos])
140 if (xmax - xmin) <= 1:
141 xtickspos = numpy.linspace(xmin, xmax, nxticks)
142 xtickspos = numpy.array([float("%.1f" % i) for i in xtickspos])
133 143 ax.set_xticks(xtickspos)
134 144 else:
135 xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
145 xtickspos = numpy.arange(nxticks) * \
146 int((xmax - xmin) / (nxticks)) + int(xmin)
136 147 # xtickspos = numpy.arange(nxticks)*float(xmax-xmin)/float(nxticks) + int(xmin)
137 148 ax.set_xticks(xtickspos)
138 149
139 150 for tick in ax.get_xticklabels():
140 151 tick.set_visible(xtick_visible)
141 152
142 153 for tick in ax.xaxis.get_major_ticks():
143 154 tick.label.set_fontsize(ticksize)
144 155
145 156 ######################################################
146 157 for tick in ax.get_yticklabels():
147 158 tick.set_visible(ytick_visible)
148 159
149 160 for tick in ax.yaxis.get_major_ticks():
150 161 tick.label.set_fontsize(ticksize)
151 162
152 163 ax.plot(x, y, color=color)
153 164 iplot = ax.lines[-1]
154 165
155 166 ######################################################
156 167 if '0.' in matplotlib.__version__[0:2]:
157 168 print "The matplotlib version has to be updated to 1.1 or newer"
158 169 return iplot
159 170
160 171 if '1.0.' in matplotlib.__version__[0:4]:
161 172 print "The matplotlib version has to be updated to 1.1 or newer"
162 173 return iplot
163 174
164 175 if grid != None:
165 176 ax.grid(b=True, which='major', axis=grid)
166 177
167 178 matplotlib.pyplot.tight_layout()
168 179
169 180 matplotlib.pyplot.ion()
170 181
171 182 return iplot
172 183
184
173 185 def set_linedata(ax, x, y, idline):
174 186
175 ax.lines[idline].set_data(x,y)
187 ax.lines[idline].set_data(x, y)
188
176 189
177 190 def pline(iplot, x, y, xlabel='', ylabel='', title=''):
178 191
179 ax = iplot.get_axes()
192 ax = iplot.axes
180 193
181 194 printLabels(ax, xlabel, ylabel, title)
182 195
183 196 set_linedata(ax, x, y, idline=0)
184 197
198
185 199 def addpline(ax, x, y, color, linestyle, lw):
186 200
187 ax.plot(x,y,color=color,linestyle=linestyle,lw=lw)
201 ax.plot(x, y, color=color, linestyle=linestyle, lw=lw)
188 202
189 203
190 204 def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax,
191 xlabel='', ylabel='', title='', ticksize = 9,
192 colormap='jet',cblabel='', cbsize="5%",
205 xlabel='', ylabel='', title='', ticksize=9,
206 colormap='jet', cblabel='', cbsize="5%",
193 207 XAxisAsTime=False):
194 208
195 209 matplotlib.pyplot.ioff()
196 210
197 211 divider = make_axes_locatable(ax)
198 212 ax_cb = divider.new_horizontal(size=cbsize, pad=0.05)
199 213 fig = ax.get_figure()
200 214 fig.add_axes(ax_cb)
201 215
202 ax.set_xlim([xmin,xmax])
203 ax.set_ylim([ymin,ymax])
216 ax.set_xlim([xmin, xmax])
217 ax.set_ylim([ymin, ymax])
204 218
205 219 printLabels(ax, xlabel, ylabel, title)
206 220
207 221 z = numpy.ma.masked_invalid(z)
208 cmap=matplotlib.pyplot.get_cmap(colormap)
209 cmap.set_bad('white',1.)
210 imesh = ax.pcolormesh(x,y,z.T, vmin=zmin, vmax=zmax, cmap=cmap)
211 cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb)
222 cmap = matplotlib.pyplot.get_cmap(colormap)
223 cmap.set_bad('black', 1.)
224 imesh = ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax, cmap=cmap)
225 cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb)
212 226 cb.set_label(cblabel)
213 227
214 228 # for tl in ax_cb.get_yticklabels():
215 229 # tl.set_visible(True)
216 230
217 231 for tick in ax.yaxis.get_major_ticks():
218 232 tick.label.set_fontsize(ticksize)
219 233
220 234 for tick in ax.xaxis.get_major_ticks():
221 235 tick.label.set_fontsize(ticksize)
222 236
223 237 for tick in cb.ax.get_yticklabels():
224 238 tick.set_fontsize(ticksize)
225 239
226 240 ax_cb.yaxis.tick_right()
227 241
228 242 if '0.' in matplotlib.__version__[0:2]:
229 243 print "The matplotlib version has to be updated to 1.1 or newer"
230 244 return imesh
231 245
232 246 if '1.0.' in matplotlib.__version__[0:4]:
233 247 print "The matplotlib version has to be updated to 1.1 or newer"
234 248 return imesh
235 249
236 250 matplotlib.pyplot.tight_layout()
237 251
238 252 if XAxisAsTime:
239 253
240 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
254 def func(x, pos): return ('%s') % (
255 datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
241 256 ax.xaxis.set_major_formatter(FuncFormatter(func))
242 257 ax.xaxis.set_major_locator(LinearLocator(7))
243 258
244 ax.grid(True)
245 259 matplotlib.pyplot.ion()
246 260 return imesh
247 261
248 def pcolor(imesh, z, xlabel='', ylabel='', title=''):
249 262
250 z = numpy.ma.masked_invalid(z)
251
252 cmap=matplotlib.pyplot.get_cmap('jet')
253 cmap.set_bad('white',1.)
263 def pcolor(imesh, z, xlabel='', ylabel='', title=''):
254 264
255 265 z = z.T
256 ax = imesh.get_axes()
266 ax = imesh.axes
257 267 printLabels(ax, xlabel, ylabel, title)
258 268 imesh.set_array(z.ravel())
259 ax.grid(True)
260 269
261 270
262 271 def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
263 272
264 273 printLabels(ax, xlabel, ylabel, title)
265 z = numpy.ma.masked_invalid(z)
266 cmap=matplotlib.pyplot.get_cmap(colormap)
267 cmap.set_bad('white',1.)
268 ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap))
269 ax.grid(True)
274
275 ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax,
276 cmap=matplotlib.pyplot.get_cmap(colormap))
277
270 278
271 279 def addpcolorbuffer(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
272 280
273 281 printLabels(ax, xlabel, ylabel, title)
274 282
275 283 ax.collections.remove(ax.collections[0])
276 284
277 285 z = numpy.ma.masked_invalid(z)
278
279 cmap=matplotlib.pyplot.get_cmap(colormap)
280 cmap.set_bad('white',1.)
281 286
282 ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=cmap)
283 ax.grid(True)
287 cmap = matplotlib.pyplot.get_cmap(colormap)
288 cmap.set_bad('black', 1.)
284 289
290 ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax, cmap=cmap)
285 291
286 def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
287 ticksize=9, xtick_visible=True, ytick_visible=True,
288 nxticks=4, nyticks=10,
289 grid=None):
290 292
293 def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
294 ticksize=9, xtick_visible=True, ytick_visible=True,
295 nxticks=4, nyticks=10,
296 grid=None):
291 297 """
292 298
293 299 Input:
294 300 grid : None, 'both', 'x', 'y'
295 301 """
296 302
297 303 matplotlib.pyplot.ioff()
298 304
299 305 lines = ax.plot(x.T, y)
300 306 leg = ax.legend(lines, legendlabels, loc='upper right')
301 307 leg.get_frame().set_alpha(0.5)
302 ax.set_xlim([xmin,xmax])
303 ax.set_ylim([ymin,ymax])
308 ax.set_xlim([xmin, xmax])
309 ax.set_ylim([ymin, ymax])
304 310 printLabels(ax, xlabel, ylabel, title)
305 311
306 xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
312 xtickspos = numpy.arange(nxticks) * \
313 int((xmax - xmin) / (nxticks)) + int(xmin)
307 314 ax.set_xticks(xtickspos)
308 315
309 316 for tick in ax.get_xticklabels():
310 317 tick.set_visible(xtick_visible)
311 318
312 319 for tick in ax.xaxis.get_major_ticks():
313 320 tick.label.set_fontsize(ticksize)
314 321
315 322 for tick in ax.get_yticklabels():
316 323 tick.set_visible(ytick_visible)
317 324
318 325 for tick in ax.yaxis.get_major_ticks():
319 326 tick.label.set_fontsize(ticksize)
320 327
321 328 iplot = ax.lines[-1]
322 329
323 330 if '0.' in matplotlib.__version__[0:2]:
324 331 print "The matplotlib version has to be updated to 1.1 or newer"
325 332 return iplot
326 333
327 334 if '1.0.' in matplotlib.__version__[0:4]:
328 335 print "The matplotlib version has to be updated to 1.1 or newer"
329 336 return iplot
330 337
331 338 if grid != None:
332 339 ax.grid(b=True, which='major', axis=grid)
333 340
334 341 matplotlib.pyplot.tight_layout()
335 342
336 343 matplotlib.pyplot.ion()
337 344
338 345 return iplot
339 346
340 347
341 348 def pmultiline(iplot, x, y, xlabel='', ylabel='', title=''):
342 349
343 ax = iplot.get_axes()
350 ax = iplot.axes
344 351
345 352 printLabels(ax, xlabel, ylabel, title)
346 353
347 354 for i in range(len(ax.lines)):
348 355 line = ax.lines[i]
349 line.set_data(x[i,:],y)
356 line.set_data(x[i, :], y)
350 357
351 def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
352 ticksize=9, xtick_visible=True, ytick_visible=True,
353 nxticks=4, nyticks=10, marker='.', markersize=10, linestyle="None",
354 grid=None, XAxisAsTime=False):
355 358
359 def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
360 ticksize=9, xtick_visible=True, ytick_visible=True,
361 nxticks=4, nyticks=10, marker='.', markersize=10, linestyle="None",
362 grid=None, XAxisAsTime=False):
356 363 """
357 364
358 365 Input:
359 366 grid : None, 'both', 'x', 'y'
360 367 """
361 368
362 369 matplotlib.pyplot.ioff()
363 370
364 371 # lines = ax.plot(x, y.T, marker=marker,markersize=markersize,linestyle=linestyle)
365 372 lines = ax.plot(x, y.T)
366 373 # leg = ax.legend(lines, legendlabels, loc=2, bbox_to_anchor=(1.01, 1.00), numpoints=1, handlelength=1.5, \
367 374 # handletextpad=0.5, borderpad=0.5, labelspacing=0.5, borderaxespad=0.)
368 375
369 376 leg = ax.legend(lines, legendlabels,
370 377 loc='upper right', bbox_to_anchor=(1.16, 1), borderaxespad=0)
371 378
372 for label in leg.get_texts(): label.set_fontsize(9)
379 for label in leg.get_texts():
380 label.set_fontsize(9)
373 381
374 ax.set_xlim([xmin,xmax])
375 ax.set_ylim([ymin,ymax])
382 ax.set_xlim([xmin, xmax])
383 ax.set_ylim([ymin, ymax])
376 384 printLabels(ax, xlabel, ylabel, title)
377 385
378 386 # xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
379 387 # ax.set_xticks(xtickspos)
380 388
381 389 for tick in ax.get_xticklabels():
382 390 tick.set_visible(xtick_visible)
383 391
384 392 for tick in ax.xaxis.get_major_ticks():
385 393 tick.label.set_fontsize(ticksize)
386 394
387 395 for tick in ax.get_yticklabels():
388 396 tick.set_visible(ytick_visible)
389 397
390 398 for tick in ax.yaxis.get_major_ticks():
391 399 tick.label.set_fontsize(ticksize)
392 400
393 401 iplot = ax.lines[-1]
394 402
395 403 if '0.' in matplotlib.__version__[0:2]:
396 404 print "The matplotlib version has to be updated to 1.1 or newer"
397 405 return iplot
398 406
399 407 if '1.0.' in matplotlib.__version__[0:4]:
400 408 print "The matplotlib version has to be updated to 1.1 or newer"
401 409 return iplot
402 410
403 411 if grid != None:
404 412 ax.grid(b=True, which='major', axis=grid)
405 413
406 414 matplotlib.pyplot.tight_layout()
407 415
408 416 if XAxisAsTime:
409 417
410 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
418 def func(x, pos): return ('%s') % (
419 datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
411 420 ax.xaxis.set_major_formatter(FuncFormatter(func))
412 421 ax.xaxis.set_major_locator(LinearLocator(7))
413 422
414 423 matplotlib.pyplot.ion()
415 424
416 425 return iplot
417 426
427
418 428 def pmultilineyaxis(iplot, x, y, xlabel='', ylabel='', title=''):
419 429
420 ax = iplot.get_axes()
430 ax = iplot.axes
431
421 432 printLabels(ax, xlabel, ylabel, title)
422 433
423 434 for i in range(len(ax.lines)):
424 435 line = ax.lines[i]
425 line.set_data(x,y[i,:])
436 line.set_data(x, y[i, :])
437
426 438
427 439 def createPolar(ax, x, y,
428 xlabel='', ylabel='', title='', ticksize = 9,
429 colormap='jet',cblabel='', cbsize="5%",
430 XAxisAsTime=False):
440 xlabel='', ylabel='', title='', ticksize=9,
441 colormap='jet', cblabel='', cbsize="5%",
442 XAxisAsTime=False):
431 443
432 444 matplotlib.pyplot.ioff()
433 445
434 ax.plot(x,y,'bo', markersize=5)
446 ax.plot(x, y, 'bo', markersize=5)
435 447 # ax.set_rmax(90)
436 ax.set_ylim(0,90)
437 ax.set_yticks(numpy.arange(0,90,20))
448 ax.set_ylim(0, 90)
449 ax.set_yticks(numpy.arange(0, 90, 20))
438 450 # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center' ,size='11')
439 451 # ax.text(0, 50, ylabel, rotation='vertical', va ='center', ha = 'left' ,size='11')
440 452 # ax.text(100, 100, 'example', ha='left', va='center', rotation='vertical')
441 ax.yaxis.labelpad = 230
453 ax.yaxis.labelpad = 40
442 454 printLabels(ax, xlabel, ylabel, title)
443 455 iplot = ax.lines[-1]
444 456
445 457 if '0.' in matplotlib.__version__[0:2]:
446 458 print "The matplotlib version has to be updated to 1.1 or newer"
447 459 return iplot
448 460
449 461 if '1.0.' in matplotlib.__version__[0:4]:
450 462 print "The matplotlib version has to be updated to 1.1 or newer"
451 463 return iplot
452 464
453 465 # if grid != None:
454 466 # ax.grid(b=True, which='major', axis=grid)
455 467
456 468 matplotlib.pyplot.tight_layout()
457 469
458 470 matplotlib.pyplot.ion()
459 471
460
461 472 return iplot
462 473
474
463 475 def polar(iplot, x, y, xlabel='', ylabel='', title=''):
464 476
465 ax = iplot.get_axes()
477 ax = iplot.axes
466 478
467 479 # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center',size='11')
468 480 printLabels(ax, xlabel, ylabel, title)
469 481
470 482 set_linedata(ax, x, y, idline=0)
471 483
484
472 485 def draw(fig):
473 486
474 487 if type(fig) == 'int':
475 488 raise ValueError, "Error drawing: Fig parameter should be a matplotlib figure object figure"
476 489
477 490 fig.canvas.draw()
478 491
492
479 493 def pause(interval=0.000001):
480 494
481 495 matplotlib.pyplot.pause(interval)
@@ -1,321 +1,331
1 import os, sys
1 import os
2 import sys
2 3 import glob
3 4 import fnmatch
4 5 import datetime
5 6 import time
6 7 import re
7 8 import h5py
8 9 import numpy
9 import matplotlib.pyplot as plt
10 10
11 import pylab as plb
12 11 from scipy.optimize import curve_fit
13 from scipy import asarray as ar,exp
12 from scipy import asarray as ar, exp
14 13 from scipy import stats
15 14
16 15 from duplicity.path import Path
17 16 from numpy.ma.core import getdata
18 17
19 18 SPEED_OF_LIGHT = 299792458
20 19 SPEED_OF_LIGHT = 3e8
21 20
22 21 try:
23 22 from gevent import sleep
24 23 except:
25 24 from time import sleep
26 25
27 26 from schainpy.model.data.jrodata import Spectra
28 27 #from schainpy.model.data.BLTRheaderIO import FileHeader, RecordHeader
29 28 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
30 29 #from schainpy.model.io.jroIO_bltr import BLTRReader
31 30 from numpy import imag, shape, NaN
32 31
33 32
34 startFp = open('/home/erick/Documents/MIRA35C/20160117/20160117_0000.zspc',"rb")
35
36
37 FILE_HEADER = numpy.dtype([ #HEADER 1024bytes
38 ('Hname',numpy.str_,32), #Original file name
39 ('Htime',numpy.str_,32), #Date and time when the file was created
40 ('Hoper',numpy.str_,64), #Name of operator who created the file
41 ('Hplace',numpy.str_,128), #Place where the measurements was carried out
42 ('Hdescr',numpy.str_,256), #Description of measurements
43 ('Hdummy',numpy.str_,512), #Reserved space
44 #Main chunk
45 ('Msign','<i4'), #Main chunk signature FZKF or NUIG
46 ('MsizeData','<i4'), #Size of data block main chunk
47 #Processing DSP parameters
48 ('PPARsign','<i4'), #PPAR signature
49 ('PPARsize','<i4'), #PPAR size of block
50 ('PPARprf','<i4'), #Pulse repetition frequency
51 ('PPARpdr','<i4'), #Pulse duration
52 ('PPARsft','<i4'), #FFT length
53 ('PPARavc','<i4'), #Number of spectral (in-coherent) averages
54 ('PPARihp','<i4'), #Number of lowest range gate for moment estimation
55 ('PPARchg','<i4'), #Count for gates for moment estimation
56 ('PPARpol','<i4'), #switch on/off polarimetric measurements. Should be 1.
57 #Service DSP parameters
58 ('SPARatt','<i4'), #STC attenuation on the lowest ranges on/off
59 ('SPARtx','<i4'), #OBSOLETE
60 ('SPARaddGain0','<f4'), #OBSOLETE
61 ('SPARaddGain1','<f4'), #OBSOLETE
62 ('SPARwnd','<i4'), #Debug only. It normal mode it is 0.
63 ('SPARpos','<i4'), #Delay between sync pulse and tx pulse for phase corr, ns
64 ('SPARadd','<i4'), #"add to pulse" to compensate for delay between the leading edge of driver pulse and envelope of the RF signal.
65 ('SPARlen','<i4'), #Time for measuring txn pulse phase. OBSOLETE
66 ('SPARcal','<i4'), #OBSOLETE
67 ('SPARnos','<i4'), #OBSOLETE
68 ('SPARof0','<i4'), #detection threshold
69 ('SPARof1','<i4'), #OBSOLETE
70 ('SPARswt','<i4'), #2nd moment estimation threshold
71 ('SPARsum','<i4'), #OBSOLETE
72 ('SPARosc','<i4'), #flag Oscillosgram mode
73 ('SPARtst','<i4'), #OBSOLETE
74 ('SPARcor','<i4'), #OBSOLETE
75 ('SPARofs','<i4'), #OBSOLETE
76 ('SPARhsn','<i4'), #Hildebrand div noise detection on noise gate
77 ('SPARhsa','<f4'), #Hildebrand div noise detection on all gates
78 ('SPARcalibPow_M','<f4'), #OBSOLETE
79 ('SPARcalibSNR_M','<f4'), #OBSOLETE
80 ('SPARcalibPow_S','<f4'), #OBSOLETE
81 ('SPARcalibSNR_S','<f4'), #OBSOLETE
82 ('SPARrawGate1','<i4'), #Lowest range gate for spectra saving Raw_Gate1 >=5
83 ('SPARrawGate2','<i4'), #Number of range gates with atmospheric signal
84 ('SPARraw','<i4'), #flag - IQ or spectra saving on/off
85 ('SPARprc','<i4'),]) #flag - Moment estimation switched on/off
86
87
88
89 self.Hname= None
90 self.Htime= None
91 self.Hoper= None
92 self.Hplace= None
93 self.Hdescr= None
94 self.Hdummy= None
95
96 self.Msign=None
97 self.MsizeData=None
98
99 self.PPARsign=None
100 self.PPARsize=None
101 self.PPARprf=None
102 self.PPARpdr=None
103 self.PPARsft=None
104 self.PPARavc=None
105 self.PPARihp=None
106 self.PPARchg=None
107 self.PPARpol=None
108 #Service DSP parameters
109 self.SPARatt=None
110 self.SPARtx=None
111 self.SPARaddGain0=None
112 self.SPARaddGain1=None
113 self.SPARwnd=None
114 self.SPARpos=None
115 self.SPARadd=None
116 self.SPARlen=None
117 self.SPARcal=None
118 self.SPARnos=None
119 self.SPARof0=None
120 self.SPARof1=None
121 self.SPARswt=None
122 self.SPARsum=None
123 self.SPARosc=None
124 self.SPARtst=None
125 self.SPARcor=None
126 self.SPARofs=None
127 self.SPARhsn=None
128 self.SPARhsa=None
129 self.SPARcalibPow_M=None
130 self.SPARcalibSNR_M=None
131 self.SPARcalibPow_S=None
132 self.SPARcalibSNR_S=None
133 self.SPARrawGate1=None
134 self.SPARrawGate2=None
135 self.SPARraw=None
136 self.SPARprc=None
137
138
139
140 header = numpy.fromfile(fp, FILE_HEADER,1)
33 startFp = open(
34 '/home/erick/Documents/MIRA35C/20160117/20160117_0000.zspc', "rb")
35
36
37 FILE_HEADER = numpy.dtype([ # HEADER 1024bytes
38 ('Hname', numpy.str_, 32), # Original file name
39 # Date and time when the file was created
40 ('Htime', numpy.str_, 32),
41 # Name of operator who created the file
42 ('Hoper', numpy.str_, 64),
43 # Place where the measurements was carried out
44 ('Hplace', numpy.str_, 128),
45 # Description of measurements
46 ('Hdescr', numpy.str_, 256),
47 ('Hdummy', numpy.str_, 512), # Reserved space
48 # Main chunk
49 ('Msign', '<i4'), # Main chunk signature FZKF or NUIG
50 ('MsizeData', '<i4'), # Size of data block main chunk
51 # Processing DSP parameters
52 ('PPARsign', '<i4'), # PPAR signature
53 ('PPARsize', '<i4'), # PPAR size of block
54 ('PPARprf', '<i4'), # Pulse repetition frequency
55 ('PPARpdr', '<i4'), # Pulse duration
56 ('PPARsft', '<i4'), # FFT length
57 # Number of spectral (in-coherent) averages
58 ('PPARavc', '<i4'),
59 # Number of lowest range gate for moment estimation
60 ('PPARihp', '<i4'),
61 # Count for gates for moment estimation
62 ('PPARchg', '<i4'),
63 # switch on/off polarimetric measurements. Should be 1.
64 ('PPARpol', '<i4'),
65 # Service DSP parameters
66 # STC attenuation on the lowest ranges on/off
67 ('SPARatt', '<i4'),
68 ('SPARtx', '<i4'), # OBSOLETE
69 ('SPARaddGain0', '<f4'), # OBSOLETE
70 ('SPARaddGain1', '<f4'), # OBSOLETE
71 # Debug only. It normal mode it is 0.
72 ('SPARwnd', '<i4'),
73 # Delay between sync pulse and tx pulse for phase corr, ns
74 ('SPARpos', '<i4'),
75 # "add to pulse" to compensate for delay between the leading edge of driver pulse and envelope of the RF signal.
76 ('SPARadd', '<i4'),
77 # Time for measuring txn pulse phase. OBSOLETE
78 ('SPARlen', '<i4'),
79 ('SPARcal', '<i4'), # OBSOLETE
80 ('SPARnos', '<i4'), # OBSOLETE
81 ('SPARof0', '<i4'), # detection threshold
82 ('SPARof1', '<i4'), # OBSOLETE
83 ('SPARswt', '<i4'), # 2nd moment estimation threshold
84 ('SPARsum', '<i4'), # OBSOLETE
85 ('SPARosc', '<i4'), # flag Oscillosgram mode
86 ('SPARtst', '<i4'), # OBSOLETE
87 ('SPARcor', '<i4'), # OBSOLETE
88 ('SPARofs', '<i4'), # OBSOLETE
89 # Hildebrand div noise detection on noise gate
90 ('SPARhsn', '<i4'),
91 # Hildebrand div noise detection on all gates
92 ('SPARhsa', '<f4'),
93 ('SPARcalibPow_M', '<f4'), # OBSOLETE
94 ('SPARcalibSNR_M', '<f4'), # OBSOLETE
95 ('SPARcalibPow_S', '<f4'), # OBSOLETE
96 ('SPARcalibSNR_S', '<f4'), # OBSOLETE
97 # Lowest range gate for spectra saving Raw_Gate1 >=5
98 ('SPARrawGate1', '<i4'),
99 # Number of range gates with atmospheric signal
100 ('SPARrawGate2', '<i4'),
101 # flag - IQ or spectra saving on/off
102 ('SPARraw', '<i4'),
103 ('SPARprc', '<i4'), ]) # flag - Moment estimation switched on/off
104
105
106 self.Hname = None
107 self.Htime = None
108 self.Hoper = None
109 self.Hplace = None
110 self.Hdescr = None
111 self.Hdummy = None
112
113 self.Msign = None
114 self.MsizeData = None
115
116 self.PPARsign = None
117 self.PPARsize = None
118 self.PPARprf = None
119 self.PPARpdr = None
120 self.PPARsft = None
121 self.PPARavc = None
122 self.PPARihp = None
123 self.PPARchg = None
124 self.PPARpol = None
125 # Service DSP parameters
126 self.SPARatt = None
127 self.SPARtx = None
128 self.SPARaddGain0 = None
129 self.SPARaddGain1 = None
130 self.SPARwnd = None
131 self.SPARpos = None
132 self.SPARadd = None
133 self.SPARlen = None
134 self.SPARcal = None
135 self.SPARnos = None
136 self.SPARof0 = None
137 self.SPARof1 = None
138 self.SPARswt = None
139 self.SPARsum = None
140 self.SPARosc = None
141 self.SPARtst = None
142 self.SPARcor = None
143 self.SPARofs = None
144 self.SPARhsn = None
145 self.SPARhsa = None
146 self.SPARcalibPow_M = None
147 self.SPARcalibSNR_M = None
148 self.SPARcalibPow_S = None
149 self.SPARcalibSNR_S = None
150 self.SPARrawGate1 = None
151 self.SPARrawGate2 = None
152 self.SPARraw = None
153 self.SPARprc = None
154
155
156 header = numpy.fromfile(fp, FILE_HEADER, 1)
141 157 ''' numpy.fromfile(file, dtype, count, sep='')
142 158 file : file or str
143 159 Open file object or filename.
144 160
145 161 dtype : data-type
146 162 Data type of the returned array. For binary files, it is used to determine
147 163 the size and byte-order of the items in the file.
148 164
149 165 count : int
150 166 Number of items to read. -1 means all items (i.e., the complete file).
151 167
152 168 sep : str
153 169 Separator between items if file is a text file. Empty ("") separator means
154 170 the file should be treated as binary. Spaces (" ") in the separator match zero
155 171 or more whitespace characters. A separator consisting only of spaces must match
156 172 at least one whitespace.
157 173
158 174 '''
159 175
160 Hname= str(header['Hname'][0])
161 Htime= str(header['Htime'][0])
162 Hoper= str(header['Hoper'][0])
163 Hplace= str(header['Hplace'][0])
164 Hdescr= str(header['Hdescr'][0])
165 Hdummy= str(header['Hdummy'][0])
166
167 Msign=header['Msign'][0]
168 MsizeData=header['MsizeData'][0]
169
170 PPARsign=header['PPARsign'][0]
171 PPARsize=header['PPARsize'][0]
172 PPARprf=header['PPARprf'][0]
173 PPARpdr=header['PPARpdr'][0]
174 PPARsft=header['PPARsft'][0]
175 PPARavc=header['PPARavc'][0]
176 PPARihp=header['PPARihp'][0]
177 PPARchg=header['PPARchg'][0]
178 PPARpol=header['PPARpol'][0]
179 #Service DSP parameters
180 SPARatt=header['SPARatt'][0]
181 SPARtx=header['SPARtx'][0]
182 SPARaddGain0=header['SPARaddGain0'][0]
183 SPARaddGain1=header['SPARaddGain1'][0]
184 SPARwnd=header['SPARwnd'][0]
185 SPARpos=header['SPARpos'][0]
186 SPARadd=header['SPARadd'][0]
187 SPARlen=header['SPARlen'][0]
188 SPARcal=header['SPARcal'][0]
189 SPARnos=header['SPARnos'][0]
190 SPARof0=header['SPARof0'][0]
191 SPARof1=header['SPARof1'][0]
192 SPARswt=header['SPARswt'][0]
193 SPARsum=header['SPARsum'][0]
194 SPARosc=header['SPARosc'][0]
195 SPARtst=header['SPARtst'][0]
196 SPARcor=header['SPARcor'][0]
197 SPARofs=header['SPARofs'][0]
198 SPARhsn=header['SPARhsn'][0]
199 SPARhsa=header['SPARhsa'][0]
200 SPARcalibPow_M=header['SPARcalibPow_M'][0]
201 SPARcalibSNR_M=header['SPARcalibSNR_M'][0]
202 SPARcalibPow_S=header['SPARcalibPow_S'][0]
203 SPARcalibSNR_S=header['SPARcalibSNR_S'][0]
204 SPARrawGate1=header['SPARrawGate1'][0]
205 SPARrawGate2=header['SPARrawGate2'][0]
206 SPARraw=header['SPARraw'][0]
207 SPARprc=header['SPARprc'][0]
208
209
210
211 SRVI_STRUCTURE = numpy.dtype([
212 ('frame_cnt','<u4'),#
213 ('time_t','<u4'), #
214 ('tpow','<f4'), #
215 ('npw1','<f4'), #
216 ('npw2','<f4'), #
217 ('cpw1','<f4'), #
218 ('pcw2','<f4'), #
219 ('ps_err','<u4'), #
220 ('te_err','<u4'), #
221 ('rc_err','<u4'), #
222 ('grs1','<u4'), #
223 ('grs2','<u4'), #
224 ('azipos','<f4'), #
225 ('azivel','<f4'), #
226 ('elvpos','<f4'), #
227 ('elvvel','<f4'), #
228 ('northAngle','<f4'), #
229 ('microsec','<u4'), #
230 ('azisetvel','<f4'), #
231 ('elvsetpos','<f4'), #
232 ('RadarConst','<f4'),]) #
233
234 JUMP_STRUCTURE = numpy.dtype([
235 ('jump','<u140'),#
236 ('SizeOfDataBlock1',numpy.str_,32),#
237 ('jump','<i4'),#
238 ('DataBlockTitleSRVI1',numpy.str_,32),#
239 ('SizeOfSRVI1','<i4'),])#
240
241
242
243 #frame_cnt=0, time_t= 0, tpow=0, npw1=0, npw2=0,
244 #cpw1=0, pcw2=0, ps_err=0, te_err=0, rc_err=0, grs1=0,
245 #grs2=0, azipos=0, azivel=0, elvpos=0, elvvel=0, northangle=0,
246 #microsec=0, azisetvel=0, elvsetpos=0, RadarConst=0
247
248
176 Hname = str(header['Hname'][0])
177 Htime = str(header['Htime'][0])
178 Hoper = str(header['Hoper'][0])
179 Hplace = str(header['Hplace'][0])
180 Hdescr = str(header['Hdescr'][0])
181 Hdummy = str(header['Hdummy'][0])
182
183 Msign = header['Msign'][0]
184 MsizeData = header['MsizeData'][0]
185
186 PPARsign = header['PPARsign'][0]
187 PPARsize = header['PPARsize'][0]
188 PPARprf = header['PPARprf'][0]
189 PPARpdr = header['PPARpdr'][0]
190 PPARsft = header['PPARsft'][0]
191 PPARavc = header['PPARavc'][0]
192 PPARihp = header['PPARihp'][0]
193 PPARchg = header['PPARchg'][0]
194 PPARpol = header['PPARpol'][0]
195 # Service DSP parameters
196 SPARatt = header['SPARatt'][0]
197 SPARtx = header['SPARtx'][0]
198 SPARaddGain0 = header['SPARaddGain0'][0]
199 SPARaddGain1 = header['SPARaddGain1'][0]
200 SPARwnd = header['SPARwnd'][0]
201 SPARpos = header['SPARpos'][0]
202 SPARadd = header['SPARadd'][0]
203 SPARlen = header['SPARlen'][0]
204 SPARcal = header['SPARcal'][0]
205 SPARnos = header['SPARnos'][0]
206 SPARof0 = header['SPARof0'][0]
207 SPARof1 = header['SPARof1'][0]
208 SPARswt = header['SPARswt'][0]
209 SPARsum = header['SPARsum'][0]
210 SPARosc = header['SPARosc'][0]
211 SPARtst = header['SPARtst'][0]
212 SPARcor = header['SPARcor'][0]
213 SPARofs = header['SPARofs'][0]
214 SPARhsn = header['SPARhsn'][0]
215 SPARhsa = header['SPARhsa'][0]
216 SPARcalibPow_M = header['SPARcalibPow_M'][0]
217 SPARcalibSNR_M = header['SPARcalibSNR_M'][0]
218 SPARcalibPow_S = header['SPARcalibPow_S'][0]
219 SPARcalibSNR_S = header['SPARcalibSNR_S'][0]
220 SPARrawGate1 = header['SPARrawGate1'][0]
221 SPARrawGate2 = header['SPARrawGate2'][0]
222 SPARraw = header['SPARraw'][0]
223 SPARprc = header['SPARprc'][0]
224
225
226 SRVI_STRUCTURE = numpy.dtype([
227 ('frame_cnt', '<u4'),
228 ('time_t', '<u4'), #
229 ('tpow', '<f4'), #
230 ('npw1', '<f4'), #
231 ('npw2', '<f4'), #
232 ('cpw1', '<f4'), #
233 ('pcw2', '<f4'), #
234 ('ps_err', '<u4'), #
235 ('te_err', '<u4'), #
236 ('rc_err', '<u4'), #
237 ('grs1', '<u4'), #
238 ('grs2', '<u4'), #
239 ('azipos', '<f4'), #
240 ('azivel', '<f4'), #
241 ('elvpos', '<f4'), #
242 ('elvvel', '<f4'), #
243 ('northAngle', '<f4'),
244 ('microsec', '<u4'), #
245 ('azisetvel', '<f4'), #
246 ('elvsetpos', '<f4'), #
247 ('RadarConst', '<f4'), ]) #
248
249 JUMP_STRUCTURE = numpy.dtype([
250 ('jump', '<u140'),
251 ('SizeOfDataBlock1', numpy.str_, 32),
252 ('jump', '<i4'),
253 ('DataBlockTitleSRVI1', numpy.str_, 32),
254 ('SizeOfSRVI1', '<i4'), ])
255
256
257 # frame_cnt=0, time_t= 0, tpow=0, npw1=0, npw2=0,
258 # cpw1=0, pcw2=0, ps_err=0, te_err=0, rc_err=0, grs1=0,
259 # grs2=0, azipos=0, azivel=0, elvpos=0, elvvel=0, northangle=0,
260 # microsec=0, azisetvel=0, elvsetpos=0, RadarConst=0
261
262
249 263 frame_cnt = frame_cnt
250 264 dwell = time_t
251 265 tpow = tpow
252 266 npw1 = npw1
253 267 npw2 = npw2
254 268 cpw1 = cpw1
255 269 pcw2 = pcw2
256 270 ps_err = ps_err
257 271 te_err = te_err
258 272 rc_err = rc_err
259 273 grs1 = grs1
260 274 grs2 = grs2
261 275 azipos = azipos
262 276 azivel = azivel
263 277 elvpos = elvpos
264 278 elvvel = elvvel
265 279 northAngle = northAngle
266 280 microsec = microsec
267 281 azisetvel = azisetvel
268 282 elvsetpos = elvsetpos
269 283 RadarConst5 = RadarConst
270
271 284
272 285
273 #print fp
274 #startFp = open('/home/erick/Documents/Data/huancayo.20161019.22.fdt',"rb") #The method tell() returns the current position of the file read/write pointer within the file.
275 #startFp = open(fp,"rb") #The method tell() returns the current position of the file read/write pointer within the file.
276 #RecCounter=0
277 #Off2StartNxtRec=811248
278 #print 'OffsetStartHeader ',self.OffsetStartHeader,'RecCounter ', self.RecCounter, 'Off2StartNxtRec ' , self.Off2StartNxtRec
286 # print fp
287 # startFp = open('/home/erick/Documents/Data/huancayo.20161019.22.fdt',"rb") #The method tell() returns the current position of the file read/write pointer within the file.
288 # startFp = open(fp,"rb") #The method tell() returns the current position of the file read/write pointer within the file.
289 # RecCounter=0
290 # Off2StartNxtRec=811248
291 # print 'OffsetStartHeader ',self.OffsetStartHeader,'RecCounter ', self.RecCounter, 'Off2StartNxtRec ' , self.Off2StartNxtRec
279 292 #OffRHeader= self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
280 293 #startFp.seek(OffRHeader, os.SEEK_SET)
281 print 'debe ser 48, RecCounter*811248', self.OffsetStartHeader,self.RecCounter,self.Off2StartNxtRec
282 print 'Posicion del bloque: ',OffRHeader
294 print 'debe ser 48, RecCounter*811248', self.OffsetStartHeader, self.RecCounter, self.Off2StartNxtRec
295 print 'Posicion del bloque: ', OffRHeader
283 296
284 header = numpy.fromfile(startFp,SRVI_STRUCTURE,1)
297 header = numpy.fromfile(startFp, SRVI_STRUCTURE, 1)
285 298
286 self.frame_cnt = header['frame_cnt'][0]#
299 self.frame_cnt = header['frame_cnt'][0]
287 300 self.time_t = header['frame_cnt'][0] #
288 301 self.tpow = header['frame_cnt'][0] #
289 302 self.npw1 = header['frame_cnt'][0] #
290 303 self.npw2 = header['frame_cnt'][0] #
291 304 self.cpw1 = header['frame_cnt'][0] #
292 305 self.pcw2 = header['frame_cnt'][0] #
293 306 self.ps_err = header['frame_cnt'][0] #
294 307 self.te_err = header['frame_cnt'][0] #
295 308 self.rc_err = header['frame_cnt'][0] #
296 309 self.grs1 = header['frame_cnt'][0] #
297 310 self.grs2 = header['frame_cnt'][0] #
298 311 self.azipos = header['frame_cnt'][0] #
299 312 self.azivel = header['frame_cnt'][0] #
300 313 self.elvpos = header['frame_cnt'][0] #
301 314 self.elvvel = header['frame_cnt'][0] #
302 315 self.northAngle = header['frame_cnt'][0] #
303 316 self.microsec = header['frame_cnt'][0] #
304 317 self.azisetvel = header['frame_cnt'][0] #
305 318 self.elvsetpos = header['frame_cnt'][0] #
306 self.RadarConst = header['frame_cnt'][0] #
319 self.RadarConst = header['frame_cnt'][0] #
307 320
308 321
309 self.ipp= 0.5*(SPEED_OF_LIGHT/self.PRFhz)
322 self.ipp = 0.5 * (SPEED_OF_LIGHT / self.PRFhz)
310 323
311 self.RHsize = 180+20*self.nChannels
312 self.Datasize= self.nProfiles*self.nChannels*self.nHeights*2*4
313 #print 'Datasize',self.Datasize
314 endFp = self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
324 self.RHsize = 180 + 20 * self.nChannels
325 self.Datasize = self.nProfiles * self.nChannels * self.nHeights * 2 * 4
326 # print 'Datasize',self.Datasize
327 endFp = self.OffsetStartHeader + self.RecCounter * self.Off2StartNxtRec
315 328
316 329 print '=============================================='
317 330
318 331 print '=============================================='
319
320
321 No newline at end of file
@@ -1,20 +1,20
1 1 '''
2 2
3 3 $Author: murco $
4 4 $Id: JRODataIO.py 169 2012-11-19 21:57:03Z murco $
5 5 '''
6 6
7 7 from jroIO_voltage import *
8 8 from jroIO_spectra import *
9 9 from jroIO_heispectra import *
10 10 from jroIO_usrp import *
11
11 from jroIO_digitalRF import *
12 12 from jroIO_kamisr import *
13 13 from jroIO_param import *
14 14 from jroIO_hf import *
15 15
16 16 from jroIO_madrigal import *
17 17
18 18 from bltrIO_param import *
19 19 from jroIO_bltr import *
20 20 from jroIO_mira35c import *
@@ -1,366 +1,369
1 1 '''
2 2 Created on Nov 9, 2016
3 3
4 4 @author: roj- LouVD
5 5 '''
6 6
7 7
8 8 import os
9 9 import sys
10 10 import time
11 11 import glob
12 12 import datetime
13 13
14 14 import numpy
15 15
16 16 from schainpy.model.proc.jroproc_base import ProcessingUnit
17 17 from schainpy.model.data.jrodata import Parameters
18 18 from schainpy.model.io.jroIO_base import JRODataReader, isNumber
19 19 from schainpy.utils import log
20 20
21 21 FILE_HEADER_STRUCTURE = numpy.dtype([
22 22 ('FMN', '<u4'),
23 23 ('nrec', '<u4'),
24 24 ('fr_offset', '<u4'),
25 25 ('id', '<u4'),
26 26 ('site', 'u1', (32,))
27 27 ])
28 28
29 29 REC_HEADER_STRUCTURE = numpy.dtype([
30 30 ('rmn', '<u4'),
31 31 ('rcounter', '<u4'),
32 32 ('nr_offset', '<u4'),
33 33 ('tr_offset', '<u4'),
34 34 ('time', '<u4'),
35 35 ('time_msec', '<u4'),
36 36 ('tag', 'u1', (32,)),
37 37 ('comments', 'u1', (32,)),
38 38 ('lat', '<f4'),
39 39 ('lon', '<f4'),
40 40 ('gps_status', '<u4'),
41 41 ('freq', '<u4'),
42 42 ('freq0', '<u4'),
43 43 ('nchan', '<u4'),
44 44 ('delta_r', '<u4'),
45 45 ('nranges', '<u4'),
46 46 ('r0', '<u4'),
47 47 ('prf', '<u4'),
48 48 ('ncoh', '<u4'),
49 49 ('npoints', '<u4'),
50 50 ('polarization', '<i4'),
51 51 ('rx_filter', '<u4'),
52 52 ('nmodes', '<u4'),
53 53 ('dmode_index', '<u4'),
54 54 ('dmode_rngcorr', '<u4'),
55 55 ('nrxs', '<u4'),
56 56 ('acf_length', '<u4'),
57 57 ('acf_lags', '<u4'),
58 58 ('sea_to_atmos', '<f4'),
59 59 ('sea_notch', '<u4'),
60 60 ('lh_sea', '<u4'),
61 61 ('hh_sea', '<u4'),
62 62 ('nbins_sea', '<u4'),
63 63 ('min_snr', '<f4'),
64 64 ('min_cc', '<f4'),
65 65 ('max_time_diff', '<f4')
66 66 ])
67 67
68 68 DATA_STRUCTURE = numpy.dtype([
69 69 ('range', '<u4'),
70 70 ('status', '<u4'),
71 71 ('zonal', '<f4'),
72 72 ('meridional', '<f4'),
73 73 ('vertical', '<f4'),
74 74 ('zonal_a', '<f4'),
75 75 ('meridional_a', '<f4'),
76 76 ('corrected_fading', '<f4'), # seconds
77 77 ('uncorrected_fading', '<f4'), # seconds
78 78 ('time_diff', '<f4'),
79 79 ('major_axis', '<f4'),
80 80 ('axial_ratio', '<f4'),
81 81 ('orientation', '<f4'),
82 82 ('sea_power', '<u4'),
83 83 ('sea_algorithm', '<u4')
84 84 ])
85 85
86
86 87 class BLTRParamReader(JRODataReader, ProcessingUnit):
87 88 '''
88 89 Boundary Layer and Tropospheric Radar (BLTR) reader, Wind velocities and SNR from *.sswma files
89 90 '''
90 91
91 92 ext = '.sswma'
92 93
93 94 def __init__(self, **kwargs):
94 95
95 ProcessingUnit.__init__(self , **kwargs)
96 ProcessingUnit.__init__(self, **kwargs)
96 97
97 self.dataOut = Parameters()
98 self.dataOut = Parameters()
98 99 self.counter_records = 0
99 100 self.flagNoMoreFiles = 0
100 self.isConfig = False
101 self.isConfig = False
101 102 self.filename = None
102
103
103 104 def setup(self,
104 105 path=None,
105 106 startDate=None,
106 107 endDate=None,
107 108 ext=None,
108 109 startTime=datetime.time(0, 0, 0),
109 110 endTime=datetime.time(23, 59, 59),
110 111 timezone=0,
111 112 status_value=0,
112 113 **kwargs):
113 114
114 115 self.path = path
115 116 self.startDate = startDate
116 117 self.endDate = endDate
117 118 self.startTime = startTime
118 119 self.endTime = endTime
119 120 self.status_value = status_value
120 121 self.datatime = datetime.datetime(1900,1,1)
121 122
122 123 if self.path is None:
123 124 raise ValueError, "The path is not valid"
124 125
125 if ext is None:
126 if ext is None:
126 127 ext = self.ext
127 128
128 129 self.search_files(self.path, startDate, endDate, ext)
129 130 self.timezone = timezone
130 131 self.fileIndex = 0
131 132
132 133 if not self.fileList:
133 raise Warning, "There is no files matching these date in the folder: %s. \n Check 'startDate' and 'endDate' "%(path)
134 raise Warning, "There is no files matching these date in the folder: %s. \n Check 'startDate' and 'endDate' " % (
135 path)
134 136
135 137 self.setNextFile()
136
138
137 139 def search_files(self, path, startDate, endDate, ext):
138 140 '''
139 141 Searching for BLTR rawdata file in path
140 142 Creating a list of file to proces included in [startDate,endDate]
141
143
142 144 Input:
143 145 path - Path to find BLTR rawdata files
144 146 startDate - Select file from this date
145 147 enDate - Select file until this date
146 148 ext - Extension of the file to read
147
148 '''
149
149 '''
150
150 151 log.success('Searching files in {} '.format(path), 'BLTRParamReader')
151 152 foldercounter = 0
152 153 fileList0 = glob.glob1(path, "*%s" % ext)
153 154 fileList0.sort()
154
155
155 156 self.fileList = []
156 157 self.dateFileList = []
157
158 for thisFile in fileList0:
158
159 for thisFile in fileList0:
159 160 year = thisFile[-14:-10]
160 161 if not isNumber(year):
161 162 continue
162
163
163 164 month = thisFile[-10:-8]
164 165 if not isNumber(month):
165 166 continue
166
167
167 168 day = thisFile[-8:-6]
168 169 if not isNumber(day):
169 continue
170
170 continue
171
171 172 year, month, day = int(year), int(month), int(day)
172 173 dateFile = datetime.date(year, month, day)
173
174
174 175 if (startDate > dateFile) or (endDate < dateFile):
175 176 continue
176
177
177 178 self.fileList.append(thisFile)
178 179 self.dateFileList.append(dateFile)
179 180
180 181 return
181 182
182 183 def setNextFile(self):
183 184
184 185 file_id = self.fileIndex
185 186
186 187 if file_id == len(self.fileList):
187 188 log.success('No more files in the folder', 'BLTRParamReader')
188 189 self.flagNoMoreFiles = 1
189 190 return 0
190 191
191 192 log.success('Opening {}'.format(self.fileList[file_id]), 'BLTRParamReader')
192 193 filename = os.path.join(self.path, self.fileList[file_id])
193 194
194 195 dirname, name = os.path.split(filename)
195 self.siteFile = name.split('.')[0] # 'peru2' ---> Piura - 'peru1' ---> Huancayo or Porcuya
196 # 'peru2' ---> Piura - 'peru1' ---> Huancayo or Porcuya
197 self.siteFile = name.split('.')[0]
196 198 if self.filename is not None:
197 199 self.fp.close()
198 200 self.filename = filename
199 201 self.fp = open(self.filename, 'rb')
200 202 self.header_file = numpy.fromfile(self.fp, FILE_HEADER_STRUCTURE, 1)
201 203 self.nrecords = self.header_file['nrec'][0]
202 self.sizeOfFile = os.path.getsize(self.filename)
204 self.sizeOfFile = os.path.getsize(self.filename)
203 205 self.counter_records = 0
204 206 self.flagIsNewFile = 0
205 207 self.fileIndex += 1
206 208
207 209 return 1
208 210
209 211 def readNextBlock(self):
210 212
211 213 while True:
212 214 if self.counter_records == self.nrecords:
213 215 self.flagIsNewFile = 1
214 216 if not self.setNextFile():
215 217 return 0
216 218
217 219 self.readBlock()
218 220
219 221 if (self.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or \
220 222 (self.datatime > datetime.datetime.combine(self.endDate, self.endTime)):
221 223 log.warning(
222 224 'Reading Record No. {}/{} -> {} [Skipping]'.format(
223 225 self.counter_records,
224 226 self.nrecords,
225 227 self.datatime.ctime()),
226 228 'BLTRParamReader')
227 229 continue
228 230 break
229 231
230 232 log.log('Reading Record No. {}/{} -> {}'.format(
231 233 self.counter_records,
232 234 self.nrecords,
233 235 self.datatime.ctime()), 'BLTRParamReader')
234 236
235 237 return 1
236 238
237 239 def readBlock(self):
238 240
239 241 pointer = self.fp.tell()
240 242 header_rec = numpy.fromfile(self.fp, REC_HEADER_STRUCTURE, 1)
241 self.nchannels = header_rec['nchan'][0]/2
243 self.nchannels = header_rec['nchan'][0] / 2
242 244 self.kchan = header_rec['nrxs'][0]
243 245 self.nmodes = header_rec['nmodes'][0]
244 246 self.nranges = header_rec['nranges'][0]
245 247 self.fp.seek(pointer)
246 248 self.height = numpy.empty((self.nmodes, self.nranges))
247 249 self.snr = numpy.empty((self.nmodes, self.nchannels, self.nranges))
248 250 self.buffer = numpy.empty((self.nmodes, 3, self.nranges))
249 251 self.flagDiscontinuousBlock = 0
250 252
251 253 for mode in range(self.nmodes):
252 self.readHeader()
254 self.readHeader()
253 255 data = self.readData()
254 256 self.height[mode] = (data[0] - self.correction) / 1000.
255 257 self.buffer[mode] = data[1]
256 258 self.snr[mode] = data[2]
257 259
258 260 self.counter_records = self.counter_records + self.nmodes
259 261
260 262 return
261 263
262 264 def readHeader(self):
263 265 '''
264 266 RecordHeader of BLTR rawdata file
265 267 '''
266
268
267 269 header_structure = numpy.dtype(
268 270 REC_HEADER_STRUCTURE.descr + [
269 271 ('antenna_coord', 'f4', (2, self.nchannels)),
270 272 ('rx_gains', 'u4', (self.nchannels,)),
271 273 ('rx_analysis', 'u4', (self.nchannels,))
272 274 ]
273 275 )
274 276
275 277 self.header_rec = numpy.fromfile(self.fp, header_structure, 1)
276 278 self.lat = self.header_rec['lat'][0]
277 279 self.lon = self.header_rec['lon'][0]
278 280 self.delta = self.header_rec['delta_r'][0]
279 281 self.correction = self.header_rec['dmode_rngcorr'][0]
280 self.imode = self.header_rec['dmode_index'][0]
282 self.imode = self.header_rec['dmode_index'][0]
281 283 self.antenna = self.header_rec['antenna_coord']
282 284 self.rx_gains = self.header_rec['rx_gains']
283 285 self.time = self.header_rec['time'][0]
284 286 dt = datetime.datetime.utcfromtimestamp(self.time)
285 287 if dt.date()>self.datatime.date():
286 288 self.flagDiscontinuousBlock = 1
287 289 self.datatime = dt
288 290
289 291 def readData(self):
290 292 '''
291 293 Reading and filtering data block record of BLTR rawdata file, filtering is according to status_value.
292 294
293 295 Input:
294 296 status_value - Array data is set to NAN for values that are not equal to status_value
295 297
296 298 '''
297 299
298 300 data_structure = numpy.dtype(
299 301 DATA_STRUCTURE.descr + [
300 302 ('rx_saturation', 'u4', (self.nchannels,)),
301 303 ('chan_offset', 'u4', (2 * self.nchannels,)),
302 304 ('rx_amp', 'u4', (self.nchannels,)),
303 305 ('rx_snr', 'f4', (self.nchannels,)),
304 306 ('cross_snr', 'f4', (self.kchan,)),
305 307 ('sea_power_relative', 'f4', (self.kchan,))]
306 308 )
307 309
308 310 data = numpy.fromfile(self.fp, data_structure, self.nranges)
309 311
310 312 height = data['range']
311 winds = numpy.array((data['zonal'], data['meridional'], data['vertical']))
313 winds = numpy.array(
314 (data['zonal'], data['meridional'], data['vertical']))
312 315 snr = data['rx_snr'].T
313 316
314 winds[numpy.where(winds == -9999.)] = numpy.nan
317 winds[numpy.where(winds == -9999.)] = numpy.nan
315 318 winds[:, numpy.where(data['status'] != self.status_value)] = numpy.nan
316 319 snr[numpy.where(snr == -9999.)] = numpy.nan
317 320 snr[:, numpy.where(data['status'] != self.status_value)] = numpy.nan
318 snr = numpy.power(10, snr / 10)
319
321 snr = numpy.power(10, snr / 10)
322
320 323 return height, winds, snr
321 324
322 325 def set_output(self):
323 326 '''
324 327 Storing data from databuffer to dataOut object
325 328 '''
326
329
327 330 self.dataOut.data_SNR = self.snr
328 331 self.dataOut.height = self.height
329 332 self.dataOut.data = self.buffer
330 333 self.dataOut.utctimeInit = self.time
331 334 self.dataOut.utctime = self.dataOut.utctimeInit
332 335 self.dataOut.useLocalTime = False
333 336 self.dataOut.paramInterval = 157
334 337 self.dataOut.timezone = self.timezone
335 338 self.dataOut.site = self.siteFile
336 self.dataOut.nrecords = self.nrecords/self.nmodes
339 self.dataOut.nrecords = self.nrecords / self.nmodes
337 340 self.dataOut.sizeOfFile = self.sizeOfFile
338 341 self.dataOut.lat = self.lat
339 342 self.dataOut.lon = self.lon
340 343 self.dataOut.channelList = range(self.nchannels)
341 344 self.dataOut.kchan = self.kchan
342 345 self.dataOut.delta = self.delta
343 346 self.dataOut.correction = self.correction
344 347 self.dataOut.nmodes = self.nmodes
345 348 self.dataOut.imode = self.imode
346 349 self.dataOut.antenna = self.antenna
347 350 self.dataOut.rx_gains = self.rx_gains
348 351 self.dataOut.flagNoData = False
349 352 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
350 353
351 354 def getData(self):
352 355 '''
353 356 Storing data from databuffer to dataOut object
354 357 '''
355 358 if self.flagNoMoreFiles:
356 359 self.dataOut.flagNoData = True
357 360 log.success('No file left to process', 'BLTRParamReader')
358 361 return 0
359 362
360 if not self.readNextBlock():
363 if not self.readNextBlock():
361 364 self.dataOut.flagNoData = True
362 365 return 0
363 366
364 367 self.set_output()
365 368
366 369 return 1
This diff has been collapsed as it changes many lines, (535 lines changed) Show them Hide them
@@ -1,1807 +1,1830
1 1 '''
2 2 Created on Jul 2, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import os
7 7 import sys
8 8 import glob
9 9 import time
10 10 import numpy
11 11 import fnmatch
12 12 import inspect
13 import time, datetime
13 import time
14 import datetime
14 15 import traceback
15 16 import zmq
16 17
17 18 try:
18 19 from gevent import sleep
19 20 except:
20 21 from time import sleep
21 22
22 23 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
23 24 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
24 25
25 26 LOCALTIME = True
26 27
28
27 29 def isNumber(cad):
28 30 """
29 31 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
30 32
31 33 Excepciones:
32 34 Si un determinado string no puede ser convertido a numero
33 35 Input:
34 36 str, string al cual se le analiza para determinar si convertible a un numero o no
35 37
36 38 Return:
37 39 True : si el string es uno numerico
38 40 False : no es un string numerico
39 41 """
40 42 try:
41 float( cad )
43 float(cad)
42 44 return True
43 45 except:
44 46 return False
45 47
48
46 49 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
47 50 """
48 51 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
49 52
50 53 Inputs:
51 54 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
52 55
53 56 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
54 57 segundos contados desde 01/01/1970.
55 58 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
56 59 segundos contados desde 01/01/1970.
57 60
58 61 Return:
59 62 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
60 63 fecha especificado, de lo contrario retorna False.
61 64
62 65 Excepciones:
63 66 Si el archivo no existe o no puede ser abierto
64 67 Si la cabecera no puede ser leida.
65 68
66 69 """
67 70 basicHeaderObj = BasicHeader(LOCALTIME)
68 71
69 72 try:
70 fp = open(filename,'rb')
73 fp = open(filename, 'rb')
71 74 except IOError:
72 print "The file %s can't be opened" %(filename)
75 print "The file %s can't be opened" % (filename)
73 76 return 0
74 77
75 78 sts = basicHeaderObj.read(fp)
76 79 fp.close()
77 80
78 81 if not(sts):
79 print "Skipping the file %s because it has not a valid header" %(filename)
82 print "Skipping the file %s because it has not a valid header" % (filename)
80 83 return 0
81 84
82 85 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
83 86 return 0
84 87
85 88 return 1
86 89
87 def isTimeInRange(thisTime, startTime, endTime):
88 90
91 def isTimeInRange(thisTime, startTime, endTime):
89 92 if endTime >= startTime:
90 93 if (thisTime < startTime) or (thisTime > endTime):
91 94 return 0
92
93 95 return 1
94 96 else:
95 97 if (thisTime < startTime) and (thisTime > endTime):
96 98 return 0
97
98 99 return 1
99 100
101
100 102 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
101 103 """
102 104 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
103 105
104 106 Inputs:
105 107 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
106 108
107 109 startDate : fecha inicial del rango seleccionado en formato datetime.date
108 110
109 111 endDate : fecha final del rango seleccionado en formato datetime.date
110 112
111 113 startTime : tiempo inicial del rango seleccionado en formato datetime.time
112 114
113 115 endTime : tiempo final del rango seleccionado en formato datetime.time
114 116
115 117 Return:
116 118 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
117 119 fecha especificado, de lo contrario retorna False.
118 120
119 121 Excepciones:
120 122 Si el archivo no existe o no puede ser abierto
121 123 Si la cabecera no puede ser leida.
122 124
123 125 """
124 126
125
126 127 try:
127 fp = open(filename,'rb')
128 fp = open(filename, 'rb')
128 129 except IOError:
129 print "The file %s can't be opened" %(filename)
130 print "The file %s can't be opened" % (filename)
130 131 return None
131 132
132 133 firstBasicHeaderObj = BasicHeader(LOCALTIME)
133 134 systemHeaderObj = SystemHeader()
134 135 radarControllerHeaderObj = RadarControllerHeader()
135 136 processingHeaderObj = ProcessingHeader()
136 137
137 138 lastBasicHeaderObj = BasicHeader(LOCALTIME)
138 139
139 140 sts = firstBasicHeaderObj.read(fp)
140 141
141 142 if not(sts):
142 print "[Reading] Skipping the file %s because it has not a valid header" %(filename)
143 print "[Reading] Skipping the file %s because it has not a valid header" % (filename)
143 144 return None
144 145
145 146 if not systemHeaderObj.read(fp):
146 147 return None
147 148
148 149 if not radarControllerHeaderObj.read(fp):
149 150 return None
150 151
151 152 if not processingHeaderObj.read(fp):
152 153 return None
153 154
154 155 filesize = os.path.getsize(filename)
155 156
156 offset = processingHeaderObj.blockSize + 24 #header size
157 offset = processingHeaderObj.blockSize + 24 # header size
157 158
158 159 if filesize <= offset:
159 print "[Reading] %s: This file has not enough data" %filename
160 print "[Reading] %s: This file has not enough data" % filename
160 161 return None
161 162
162 163 fp.seek(-offset, 2)
163 164
164 165 sts = lastBasicHeaderObj.read(fp)
165 166
166 167 fp.close()
167 168
168 169 thisDatetime = lastBasicHeaderObj.datatime
169 170 thisTime_last_block = thisDatetime.time()
170 171
171 172 thisDatetime = firstBasicHeaderObj.datatime
172 173 thisDate = thisDatetime.date()
173 174 thisTime_first_block = thisDatetime.time()
174 175
175 #General case
176 # General case
176 177 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
177 178 #-----------o----------------------------o-----------
178 179 # startTime endTime
179 180
180 181 if endTime >= startTime:
181 182 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
182 183 return None
183 184
184 185 return thisDatetime
185 186
186 #If endTime < startTime then endTime belongs to the next day
187
187 # If endTime < startTime then endTime belongs to the next day
188 188
189 189 #<<<<<<<<<<<o o>>>>>>>>>>>
190 190 #-----------o----------------------------o-----------
191 191 # endTime startTime
192 192
193 193 if (thisDate == startDate) and (thisTime_last_block < startTime):
194 194 return None
195 195
196 196 if (thisDate == endDate) and (thisTime_first_block > endTime):
197 197 return None
198 198
199 199 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
200 200 return None
201 201
202 202 return thisDatetime
203 203
204
204 205 def isFolderInDateRange(folder, startDate=None, endDate=None):
205 206 """
206 207 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
207 208
208 209 Inputs:
209 210 folder : nombre completo del directorio.
210 211 Su formato deberia ser "/path_root/?YYYYDDD"
211 212
212 213 siendo:
213 214 YYYY : Anio (ejemplo 2015)
214 215 DDD : Dia del anio (ejemplo 305)
215 216
216 217 startDate : fecha inicial del rango seleccionado en formato datetime.date
217 218
218 219 endDate : fecha final del rango seleccionado en formato datetime.date
219 220
220 221 Return:
221 222 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
222 223 fecha especificado, de lo contrario retorna False.
223 224 Excepciones:
224 225 Si el directorio no tiene el formato adecuado
225 226 """
226 227
227 228 basename = os.path.basename(folder)
228 229
229 230 if not isRadarFolder(basename):
230 print "The folder %s has not the rigth format" %folder
231 print "The folder %s has not the rigth format" % folder
231 232 return 0
232 233
233 234 if startDate and endDate:
234 235 thisDate = getDateFromRadarFolder(basename)
235 236
236 237 if thisDate < startDate:
237 238 return 0
238 239
239 240 if thisDate > endDate:
240 241 return 0
241 242
242 243 return 1
243 244
245
244 246 def isFileInDateRange(filename, startDate=None, endDate=None):
245 247 """
246 248 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
247 249
248 250 Inputs:
249 251 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
250 252
251 253 Su formato deberia ser "?YYYYDDDsss"
252 254
253 255 siendo:
254 256 YYYY : Anio (ejemplo 2015)
255 257 DDD : Dia del anio (ejemplo 305)
256 258 sss : set
257 259
258 260 startDate : fecha inicial del rango seleccionado en formato datetime.date
259 261
260 262 endDate : fecha final del rango seleccionado en formato datetime.date
261 263
262 264 Return:
263 265 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
264 266 fecha especificado, de lo contrario retorna False.
265 267 Excepciones:
266 268 Si el archivo no tiene el formato adecuado
267 269 """
268 270
269 271 basename = os.path.basename(filename)
270 272
271 273 if not isRadarFile(basename):
272 print "The filename %s has not the rigth format" %filename
274 print "The filename %s has not the rigth format" % filename
273 275 return 0
274 276
275 277 if startDate and endDate:
276 278 thisDate = getDateFromRadarFile(basename)
277 279
278 280 if thisDate < startDate:
279 281 return 0
280 282
281 283 if thisDate > endDate:
282 284 return 0
283 285
284 286 return 1
285 287
288
286 289 def getFileFromSet(path, ext, set):
287 290 validFilelist = []
288 291 fileList = os.listdir(path)
289 292
290 293 # 0 1234 567 89A BCDE
291 294 # H YYYY DDD SSS .ext
292 295
293 296 for thisFile in fileList:
294 297 try:
295 298 year = int(thisFile[1:5])
296 doy = int(thisFile[5:8])
299 doy = int(thisFile[5:8])
297 300 except:
298 301 continue
299 302
300 303 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
301 304 continue
302 305
303 306 validFilelist.append(thisFile)
304 307
305 myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
308 myfile = fnmatch.filter(
309 validFilelist, '*%4.4d%3.3d%3.3d*' % (year, doy, set))
306 310
307 if len(myfile)!= 0:
311 if len(myfile) != 0:
308 312 return myfile[0]
309 313 else:
310 filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower())
311 print 'the filename %s does not exist'%filename
314 filename = '*%4.4d%3.3d%3.3d%s' % (year, doy, set, ext.lower())
315 print 'the filename %s does not exist' % filename
312 316 print '...going to the last file: '
313 317
314 318 if validFilelist:
315 validFilelist = sorted( validFilelist, key=str.lower )
319 validFilelist = sorted(validFilelist, key=str.lower)
316 320 return validFilelist[-1]
317 321
318 322 return None
319 323
324
320 325 def getlastFileFromPath(path, ext):
321 326 """
322 327 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
323 328 al final de la depuracion devuelve el ultimo file de la lista que quedo.
324 329
325 330 Input:
326 331 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
327 332 ext : extension de los files contenidos en una carpeta
328 333
329 334 Return:
330 335 El ultimo file de una determinada carpeta, no se considera el path.
331 336 """
332 337 validFilelist = []
333 338 fileList = os.listdir(path)
334 339
335 340 # 0 1234 567 89A BCDE
336 341 # H YYYY DDD SSS .ext
337 342
338 343 for thisFile in fileList:
339 344
340 345 year = thisFile[1:5]
341 346 if not isNumber(year):
342 347 continue
343 348
344 349 doy = thisFile[5:8]
345 350 if not isNumber(doy):
346 351 continue
347 352
348 353 year = int(year)
349 354 doy = int(doy)
350 355
351 356 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
352 357 continue
353 358
354 359 validFilelist.append(thisFile)
355 360
356 361 if validFilelist:
357 validFilelist = sorted( validFilelist, key=str.lower )
362 validFilelist = sorted(validFilelist, key=str.lower)
358 363 return validFilelist[-1]
359 364
360 365 return None
361 366
367
362 368 def checkForRealPath(path, foldercounter, year, doy, set, ext):
363 369 """
364 370 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
365 371 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
366 372 el path exacto de un determinado file.
367 373
368 374 Example :
369 375 nombre correcto del file es .../.../D2009307/P2009307367.ext
370 376
371 377 Entonces la funcion prueba con las siguientes combinaciones
372 378 .../.../y2009307367.ext
373 379 .../.../Y2009307367.ext
374 380 .../.../x2009307/y2009307367.ext
375 381 .../.../x2009307/Y2009307367.ext
376 382 .../.../X2009307/y2009307367.ext
377 383 .../.../X2009307/Y2009307367.ext
378 384 siendo para este caso, la ultima combinacion de letras, identica al file buscado
379 385
380 386 Return:
381 387 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
382 388 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
383 389 para el filename
384 390 """
385 391 fullfilename = None
386 392 find_flag = False
387 393 filename = None
388 394
389 prefixDirList = [None,'d','D']
390 if ext.lower() == ".r": #voltage
391 prefixFileList = ['d','D']
392 elif ext.lower() == ".pdata": #spectra
393 prefixFileList = ['p','P']
395 prefixDirList = [None, 'd', 'D']
396 if ext.lower() == ".r": # voltage
397 prefixFileList = ['d', 'D']
398 elif ext.lower() == ".pdata": # spectra
399 prefixFileList = ['p', 'P']
394 400 else:
395 401 return None, filename
396 402
397 #barrido por las combinaciones posibles
403 # barrido por las combinaciones posibles
398 404 for prefixDir in prefixDirList:
399 405 thispath = path
400 406 if prefixDir != None:
401 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
407 # formo el nombre del directorio xYYYYDDD (x=d o x=D)
402 408 if foldercounter == 0:
403 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
409 thispath = os.path.join(path, "%s%04d%03d" %
410 (prefixDir, year, doy))
404 411 else:
405 thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter))
406 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
407 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
408 fullfilename = os.path.join( thispath, filename ) #formo el path completo
409
410 if os.path.exists( fullfilename ): #verifico que exista
412 thispath = os.path.join(path, "%s%04d%03d_%02d" % (
413 prefixDir, year, doy, foldercounter))
414 for prefixFile in prefixFileList: # barrido por las dos combinaciones posibles de "D"
415 # formo el nombre del file xYYYYDDDSSS.ext
416 filename = "%s%04d%03d%03d%s" % (prefixFile, year, doy, set, ext)
417 fullfilename = os.path.join(
418 thispath, filename) # formo el path completo
419
420 if os.path.exists(fullfilename): # verifico que exista
411 421 find_flag = True
412 422 break
413 423 if find_flag:
414 424 break
415 425
416 426 if not(find_flag):
417 427 return None, filename
418 428
419 429 return fullfilename, filename
420 430
431
421 432 def isRadarFolder(folder):
422 433 try:
423 434 year = int(folder[1:5])
424 435 doy = int(folder[5:8])
425 436 except:
426 437 return 0
427 438
428 439 return 1
429 440
441
430 442 def isRadarFile(file):
431 try:
432 year = int(file[1:5])
433 doy = int(file[5:8])
434 set = int(file[8:11])
435 except:
436 return 0
443 try:
444 year = int(file[1:5])
445 doy = int(file[5:8])
446 set = int(file[8:11])
447 except:
448 return 0
449
450 return 1
437 451
438 return 1
439 452
440 453 def getDateFromRadarFile(file):
441 454 try:
442 455 year = int(file[1:5])
443 456 doy = int(file[5:8])
444 457 set = int(file[8:11])
445 458 except:
446 459 return None
447 460
448 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
461 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
449 462 return thisDate
450 463
464
451 465 def getDateFromRadarFolder(folder):
452 466 try:
453 467 year = int(folder[1:5])
454 468 doy = int(folder[5:8])
455 469 except:
456 470 return None
457 471
458 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
472 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
459 473 return thisDate
460 474
475
461 476 class JRODataIO:
462 477
463 478 c = 3E8
464 479
465 480 isConfig = False
466 481
467 482 basicHeaderObj = None
468 483
469 484 systemHeaderObj = None
470 485
471 486 radarControllerHeaderObj = None
472 487
473 488 processingHeaderObj = None
474 489
475 490 dtype = None
476 491
477 492 pathList = []
478 493
479 494 filenameList = []
480 495
481 496 filename = None
482 497
483 498 ext = None
484 499
485 500 flagIsNewFile = 1
486 501
487 502 flagDiscontinuousBlock = 0
488 503
489 504 flagIsNewBlock = 0
490 505
491 506 fp = None
492 507
493 508 firstHeaderSize = 0
494 509
495 510 basicHeaderSize = 24
496 511
497 512 versionFile = 1103
498 513
499 514 fileSize = None
500 515
501 516 # ippSeconds = None
502 517
503 518 fileSizeByHeader = None
504 519
505 520 fileIndex = None
506 521
507 522 profileIndex = None
508 523
509 524 blockIndex = None
510 525
511 526 nTotalBlocks = None
512 527
513 528 maxTimeStep = 30
514 529
515 530 lastUTTime = None
516 531
517 532 datablock = None
518 533
519 534 dataOut = None
520 535
521 536 blocksize = None
522 537
523 538 getByBlock = False
524 539
525 540 def __init__(self):
526 541
527 542 raise NotImplementedError
528 543
529 544 def run(self):
530 545
531 546 raise NotImplementedError
532 547
533 548 def getDtypeWidth(self):
534 549
535 550 dtype_index = get_dtype_index(self.dtype)
536 551 dtype_width = get_dtype_width(dtype_index)
537 552
538 553 return dtype_width
539 554
540 555 def getAllowedArgs(self):
541 556 return inspect.getargspec(self.run).args
542 557
558
543 559 class JRODataReader(JRODataIO):
544 560
545 561 online = 0
546 562
547 563 realtime = 0
548 564
549 565 nReadBlocks = 0
550 566
551 delay = 10 #number of seconds waiting a new file
567 delay = 10 # number of seconds waiting a new file
552 568
553 nTries = 3 #quantity tries
569 nTries = 3 # quantity tries
554 570
555 nFiles = 3 #number of files for searching
571 nFiles = 3 # number of files for searching
556 572
557 573 path = None
558 574
559 575 foldercounter = 0
560 576
561 577 flagNoMoreFiles = 0
562 578
563 579 datetimeList = []
564 580
565 581 __isFirstTimeOnline = 1
566 582
567 583 __printInfo = True
568 584
569 585 profileIndex = None
570 586
571 587 nTxs = 1
572 588
573 589 txIndex = None
574 590
575 #Added--------------------
591 # Added--------------------
576 592
577 593 selBlocksize = None
578 594
579 595 selBlocktime = None
580 596
581 597 def __init__(self):
582
583 598 """
584 599 This class is used to find data files
585 600
586 601 Example:
587 602 reader = JRODataReader()
588 603 fileList = reader.findDataFiles()
589 604
590 605 """
591 606 pass
592 607
593
594 608 def createObjByDefault(self):
595 609 """
596 610
597 611 """
598 612 raise NotImplementedError
599 613
600 614 def getBlockDimension(self):
601 615
602 616 raise NotImplementedError
603 617
604 618 def searchFilesOffLine(self,
605 619 path,
606 620 startDate=None,
607 621 endDate=None,
608 startTime=datetime.time(0,0,0),
609 endTime=datetime.time(23,59,59),
622 startTime=datetime.time(0, 0, 0),
623 endTime=datetime.time(23, 59, 59),
610 624 set=None,
611 625 expLabel='',
612 626 ext='.r',
613 627 cursor=None,
614 628 skip=None,
615 629 walk=True):
616 630
617 631 self.filenameList = []
618 632 self.datetimeList = []
619 633
620 634 pathList = []
621 635
622 dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
636 dateList, pathList = self.findDatafiles(
637 path, startDate, endDate, expLabel, ext, walk, include_path=True)
623 638
624 639 if dateList == []:
625 640 return [], []
626 641
627 642 if len(dateList) > 1:
628 print "[Reading] Data found for date range [%s - %s]: total days = %d" %(startDate, endDate, len(dateList))
643 print "[Reading] Data found for date range [%s - %s]: total days = %d" % (startDate, endDate, len(dateList))
629 644 else:
630 print "[Reading] Data found for date range [%s - %s]: date = %s" %(startDate, endDate, dateList[0])
645 print "[Reading] Data found for date range [%s - %s]: date = %s" % (startDate, endDate, dateList[0])
631 646
632 647 filenameList = []
633 648 datetimeList = []
634 649
635 650 for thisPath in pathList:
636 651
637 fileList = glob.glob1(thisPath, "*%s" %ext)
652 fileList = glob.glob1(thisPath, "*%s" % ext)
638 653 fileList.sort()
639 654
640 655 skippedFileList = []
641 656
642 657 if cursor is not None and skip is not None:
643 658
644 659 if skip == 0:
645 660 skippedFileList = []
646 661 else:
647 skippedFileList = fileList[cursor*skip: cursor*skip + skip]
662 skippedFileList = fileList[cursor *
663 skip: cursor * skip + skip]
648 664
649 665 else:
650 666 skippedFileList = fileList
651 667
652 668 for file in skippedFileList:
653 669
654 filename = os.path.join(thisPath,file)
670 filename = os.path.join(thisPath, file)
655 671
656 672 if not isFileInDateRange(filename, startDate, endDate):
657 673 continue
658 674
659 thisDatetime = isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
675 thisDatetime = isFileInTimeRange(
676 filename, startDate, endDate, startTime, endTime)
660 677
661 678 if not(thisDatetime):
662 679 continue
663 680
664 681 filenameList.append(filename)
665 682 datetimeList.append(thisDatetime)
666 683
667 684 if not(filenameList):
668 print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" %(startTime, endTime, ext, path)
685 print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" % (startTime, endTime, ext, path)
669 686 return [], []
670 687
671 print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)
688 print "[Reading] %d file(s) was(were) found in time range: %s - %s" % (len(filenameList), startTime, endTime)
672 689 print
673 690
674 691 # for i in range(len(filenameList)):
675 692 # print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
676 693
677 694 self.filenameList = filenameList
678 695 self.datetimeList = datetimeList
679 696
680 697 return pathList, filenameList
681 698
682 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None):
683
699 def __searchFilesOnLine(self, path, expLabel="", ext=None, walk=True, set=None):
684 700 """
685 701 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
686 702 devuelve el archivo encontrado ademas de otros datos.
687 703
688 704 Input:
689 705 path : carpeta donde estan contenidos los files que contiene data
690 706
691 707 expLabel : Nombre del subexperimento (subfolder)
692 708
693 709 ext : extension de los files
694 710
695 711 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
696 712
697 713 Return:
698 714 directory : eL directorio donde esta el file encontrado
699 715 filename : el ultimo file de una determinada carpeta
700 716 year : el anho
701 717 doy : el numero de dia del anho
702 718 set : el set del archivo
703 719
704 720
705 721 """
706 722 if not os.path.isdir(path):
707 723 return None, None, None, None, None, None
708 724
709 725 dirList = []
710 726
711 727 if not walk:
712 728 fullpath = path
713 729 foldercounter = 0
714 730 else:
715 #Filtra solo los directorios
731 # Filtra solo los directorios
716 732 for thisPath in os.listdir(path):
717 if not os.path.isdir(os.path.join(path,thisPath)):
733 if not os.path.isdir(os.path.join(path, thisPath)):
718 734 continue
719 735 if not isRadarFolder(thisPath):
720 736 continue
721 737
722 738 dirList.append(thisPath)
723 739
724 740 if not(dirList):
725 741 return None, None, None, None, None, None
726 742
727 dirList = sorted( dirList, key=str.lower )
743 dirList = sorted(dirList, key=str.lower)
728 744
729 745 doypath = dirList[-1]
730 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
746 foldercounter = int(doypath.split('_')[1]) if len(
747 doypath.split('_')) > 1 else 0
731 748 fullpath = os.path.join(path, doypath, expLabel)
732 749
733
734 print "[Reading] %s folder was found: " %(fullpath )
750 print "[Reading] %s folder was found: " % (fullpath)
735 751
736 752 if set == None:
737 753 filename = getlastFileFromPath(fullpath, ext)
738 754 else:
739 755 filename = getFileFromSet(fullpath, ext, set)
740 756
741 757 if not(filename):
742 758 return None, None, None, None, None, None
743 759
744 print "[Reading] %s file was found" %(filename)
760 print "[Reading] %s file was found" % (filename)
745 761
746 762 if not(self.__verifyFile(os.path.join(fullpath, filename))):
747 763 return None, None, None, None, None, None
748 764
749 year = int( filename[1:5] )
750 doy = int( filename[5:8] )
751 set = int( filename[8:11] )
765 year = int(filename[1:5])
766 doy = int(filename[5:8])
767 set = int(filename[8:11])
752 768
753 769 return fullpath, foldercounter, filename, year, doy, set
754 770
755 771 def __setNextFileOffline(self):
756 772
757 773 idFile = self.fileIndex
758 774
759 775 while (True):
760 776 idFile += 1
761 777 if not(idFile < len(self.filenameList)):
762 778 self.flagNoMoreFiles = 1
763 779 # print "[Reading] No more Files"
764 780 return 0
765 781
766 782 filename = self.filenameList[idFile]
767 783
768 784 if not(self.__verifyFile(filename)):
769 785 continue
770 786
771 787 fileSize = os.path.getsize(filename)
772 fp = open(filename,'rb')
788 fp = open(filename, 'rb')
773 789 break
774 790
775 791 self.flagIsNewFile = 1
776 792 self.fileIndex = idFile
777 793 self.filename = filename
778 794 self.fileSize = fileSize
779 795 self.fp = fp
780 796
781 797 # print "[Reading] Setting the file: %s"%self.filename
782 798
783 799 return 1
784 800
785 801 def __setNextFileOnline(self):
786 802 """
787 803 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
788 804 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
789 805 siguientes.
790 806
791 807 Affected:
792 808 self.flagIsNewFile
793 809 self.filename
794 810 self.fileSize
795 811 self.fp
796 812 self.set
797 813 self.flagNoMoreFiles
798 814
799 815 Return:
800 816 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
801 817 1 : si el file fue abierto con exito y esta listo a ser leido
802 818
803 819 Excepciones:
804 820 Si un determinado file no puede ser abierto
805 821 """
806 822 nFiles = 0
807 823 fileOk_flag = False
808 824 firstTime_flag = True
809 825
810 826 self.set += 1
811 827
812 828 if self.set > 999:
813 829 self.set = 0
814 830 self.foldercounter += 1
815 831
816 #busca el 1er file disponible
817 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
832 # busca el 1er file disponible
833 fullfilename, filename = checkForRealPath(
834 self.path, self.foldercounter, self.year, self.doy, self.set, self.ext)
818 835 if fullfilename:
819 836 if self.__verifyFile(fullfilename, False):
820 837 fileOk_flag = True
821 838
822 #si no encuentra un file entonces espera y vuelve a buscar
839 # si no encuentra un file entonces espera y vuelve a buscar
823 840 if not(fileOk_flag):
824 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
841 # busco en los siguientes self.nFiles+1 files posibles
842 for nFiles in range(self.nFiles + 1):
825 843
826 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
844 if firstTime_flag: # si es la 1era vez entonces hace el for self.nTries veces
827 845 tries = self.nTries
828 846 else:
829 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
847 tries = 1 # si no es la 1era vez entonces solo lo hace una vez
830 848
831 for nTries in range( tries ):
849 for nTries in range(tries):
832 850 if firstTime_flag:
833 print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
834 sleep( self.delay )
851 print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % (self.delay, filename, nTries + 1)
852 sleep(self.delay)
835 853 else:
836 854 print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
837 855
838 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
856 fullfilename, filename = checkForRealPath(
857 self.path, self.foldercounter, self.year, self.doy, self.set, self.ext)
839 858 if fullfilename:
840 859 if self.__verifyFile(fullfilename):
841 860 fileOk_flag = True
842 861 break
843 862
844 863 if fileOk_flag:
845 864 break
846 865
847 866 firstTime_flag = False
848 867
849 868 print "\t[Reading] Skipping the file \"%s\" due to this file doesn't exist" % filename
850 869 self.set += 1
851 870
852 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
871 # si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
872 if nFiles == (self.nFiles - 1):
853 873 self.set = 0
854 874 self.doy += 1
855 875 self.foldercounter = 0
856 876
857 877 if fileOk_flag:
858 self.fileSize = os.path.getsize( fullfilename )
878 self.fileSize = os.path.getsize(fullfilename)
859 879 self.filename = fullfilename
860 880 self.flagIsNewFile = 1
861 if self.fp != None: self.fp.close()
881 if self.fp != None:
882 self.fp.close()
862 883 self.fp = open(fullfilename, 'rb')
863 884 self.flagNoMoreFiles = 0
864 885 # print '[Reading] Setting the file: %s' % fullfilename
865 886 else:
866 887 self.fileSize = 0
867 888 self.filename = None
868 889 self.flagIsNewFile = 0
869 890 self.fp = None
870 891 self.flagNoMoreFiles = 1
871 892 # print '[Reading] No more files to read'
872 893
873 894 return fileOk_flag
874 895
875 896 def setNextFile(self):
876 897 if self.fp != None:
877 898 self.fp.close()
878 899
879 900 if self.online:
880 901 newFile = self.__setNextFileOnline()
881 902 else:
882 903 newFile = self.__setNextFileOffline()
883 904
884 905 if not(newFile):
885 906 print '[Reading] No more files to read'
886 907 return 0
887 908
888 909 if self.verbose:
889 910 print '[Reading] Setting the file: %s' % self.filename
890 911
891 912 self.__readFirstHeader()
892 913 self.nReadBlocks = 0
893 914 return 1
894 915
895 916 def __waitNewBlock(self):
896 917 """
897 918 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
898 919
899 920 Si el modo de lectura es OffLine siempre retorn 0
900 921 """
901 922 if not self.online:
902 923 return 0
903 924
904 925 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
905 926 return 0
906 927
907 928 currentPointer = self.fp.tell()
908 929
909 930 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
910 931
911 for nTries in range( self.nTries ):
932 for nTries in range(self.nTries):
912 933
913 934 self.fp.close()
914 self.fp = open( self.filename, 'rb' )
915 self.fp.seek( currentPointer )
935 self.fp = open(self.filename, 'rb')
936 self.fp.seek(currentPointer)
916 937
917 self.fileSize = os.path.getsize( self.filename )
938 self.fileSize = os.path.getsize(self.filename)
918 939 currentSize = self.fileSize - currentPointer
919 940
920 if ( currentSize >= neededSize ):
941 if (currentSize >= neededSize):
921 942 self.basicHeaderObj.read(self.fp)
922 943 return 1
923 944
924 945 if self.fileSize == self.fileSizeByHeader:
925 # self.flagEoF = True
946 # self.flagEoF = True
926 947 return 0
927 948
928 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
929 sleep( self.delay )
930
949 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1)
950 sleep(self.delay)
931 951
932 952 return 0
933 953
934 def waitDataBlock(self,pointer_location):
954 def waitDataBlock(self, pointer_location):
935 955
936 956 currentPointer = pointer_location
937 957
938 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
958 neededSize = self.processingHeaderObj.blockSize # + self.basicHeaderSize
939 959
940 for nTries in range( self.nTries ):
960 for nTries in range(self.nTries):
941 961 self.fp.close()
942 self.fp = open( self.filename, 'rb' )
943 self.fp.seek( currentPointer )
962 self.fp = open(self.filename, 'rb')
963 self.fp.seek(currentPointer)
944 964
945 self.fileSize = os.path.getsize( self.filename )
965 self.fileSize = os.path.getsize(self.filename)
946 966 currentSize = self.fileSize - currentPointer
947 967
948 if ( currentSize >= neededSize ):
968 if (currentSize >= neededSize):
949 969 return 1
950 970
951 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
952 sleep( self.delay )
971 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1)
972 sleep(self.delay)
953 973
954 974 return 0
955 975
956 976 def __jumpToLastBlock(self):
957 977
958 978 if not(self.__isFirstTimeOnline):
959 979 return
960 980
961 981 csize = self.fileSize - self.fp.tell()
962 982 blocksize = self.processingHeaderObj.blockSize
963 983
964 #salta el primer bloque de datos
984 # salta el primer bloque de datos
965 985 if csize > self.processingHeaderObj.blockSize:
966 986 self.fp.seek(self.fp.tell() + blocksize)
967 987 else:
968 988 return
969 989
970 990 csize = self.fileSize - self.fp.tell()
971 991 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
972 992 while True:
973 993
974 if self.fp.tell()<self.fileSize:
994 if self.fp.tell() < self.fileSize:
975 995 self.fp.seek(self.fp.tell() + neededsize)
976 996 else:
977 997 self.fp.seek(self.fp.tell() - neededsize)
978 998 break
979 999
980 1000 # csize = self.fileSize - self.fp.tell()
981 1001 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
982 1002 # factor = int(csize/neededsize)
983 1003 # if factor > 0:
984 1004 # self.fp.seek(self.fp.tell() + factor*neededsize)
985 1005
986 1006 self.flagIsNewFile = 0
987 1007 self.__isFirstTimeOnline = 0
988 1008
989 1009 def __setNewBlock(self):
990 #if self.server is None:
1010 # if self.server is None:
991 1011 if self.fp == None:
992 1012 return 0
993 1013
994 1014 # if self.online:
995 # self.__jumpToLastBlock()
1015 # self.__jumpToLastBlock()
996 1016
997 1017 if self.flagIsNewFile:
998 1018 self.lastUTTime = self.basicHeaderObj.utc
999 1019 return 1
1000 1020
1001 1021 if self.realtime:
1002 1022 self.flagDiscontinuousBlock = 1
1003 1023 if not(self.setNextFile()):
1004 1024 return 0
1005 1025 else:
1006 return 1
1007 #if self.server is None:
1026 return 1
1027 # if self.server is None:
1008 1028 currentSize = self.fileSize - self.fp.tell()
1009 1029 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1010 1030 if (currentSize >= neededSize):
1011 1031 self.basicHeaderObj.read(self.fp)
1012 1032 self.lastUTTime = self.basicHeaderObj.utc
1013 1033 return 1
1014 1034 # else:
1015 1035 # self.basicHeaderObj.read(self.zHeader)
1016 1036 # self.lastUTTime = self.basicHeaderObj.utc
1017 1037 # return 1
1018 1038 if self.__waitNewBlock():
1019 1039 self.lastUTTime = self.basicHeaderObj.utc
1020 1040 return 1
1021 #if self.server is None:
1041 # if self.server is None:
1022 1042 if not(self.setNextFile()):
1023 1043 return 0
1024 1044
1025 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
1045 deltaTime = self.basicHeaderObj.utc - self.lastUTTime
1026 1046 self.lastUTTime = self.basicHeaderObj.utc
1027 1047
1028 1048 self.flagDiscontinuousBlock = 0
1029 1049
1030 1050 if deltaTime > self.maxTimeStep:
1031 1051 self.flagDiscontinuousBlock = 1
1032 1052
1033 1053 return 1
1034 1054
1035 1055 def readNextBlock(self):
1036 1056
1037 #Skip block out of startTime and endTime
1038 while True:
1039 if not(self.__setNewBlock()):
1057 # Skip block out of startTime and endTime
1058 while True:
1059 if not(self.__setNewBlock()):
1040 1060 return 0
1041
1061
1042 1062 if not(self.readBlock()):
1043 1063 return 0
1044 1064
1045 1065 self.getBasicHeader()
1046
1047 if not isTimeInRange(self.dataOut.datatime.time(), self.startTime, self.endTime):
1048
1049 print "[Reading] Block No. %d/%d -> %s [Skipping]" %(self.nReadBlocks,
1050 self.processingHeaderObj.dataBlocksPerFile,
1051 self.dataOut.datatime.ctime())
1066 if (self.dataOut.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or (self.dataOut.datatime > datetime.datetime.combine(self.endDate, self.endTime)):
1067 print "[Reading] Block No. %d/%d -> %s [Skipping]" % (self.nReadBlocks,
1068 self.processingHeaderObj.dataBlocksPerFile,
1069 self.dataOut.datatime.ctime())
1052 1070 continue
1053 1071
1054 1072 break
1055 1073
1056 1074 if self.verbose:
1057 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1058 self.processingHeaderObj.dataBlocksPerFile,
1059 self.dataOut.datatime.ctime())
1075 print "[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks,
1076 self.processingHeaderObj.dataBlocksPerFile,
1077 self.dataOut.datatime.ctime())
1060 1078 return 1
1061 1079
1062 1080 def __readFirstHeader(self):
1063 1081
1064 1082 self.basicHeaderObj.read(self.fp)
1065 1083 self.systemHeaderObj.read(self.fp)
1066 1084 self.radarControllerHeaderObj.read(self.fp)
1067 1085 self.processingHeaderObj.read(self.fp)
1068 1086
1069 1087 self.firstHeaderSize = self.basicHeaderObj.size
1070 1088
1071 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
1089 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
1090 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
1072 1091 if datatype == 0:
1073 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
1092 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
1074 1093 elif datatype == 1:
1075 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
1094 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
1076 1095 elif datatype == 2:
1077 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
1096 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
1078 1097 elif datatype == 3:
1079 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
1098 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
1080 1099 elif datatype == 4:
1081 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
1100 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
1082 1101 elif datatype == 5:
1083 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
1102 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
1084 1103 else:
1085 1104 raise ValueError, 'Data type was not defined'
1086 1105
1087 1106 self.dtype = datatype_str
1088 1107 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1089 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
1090 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1091 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1108 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \
1109 self.firstHeaderSize + self.basicHeaderSize * \
1110 (self.processingHeaderObj.dataBlocksPerFile - 1)
1111 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1112 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1092 1113 self.getBlockDimension()
1093 1114
1094 1115 def __verifyFile(self, filename, msgFlag=True):
1095 1116
1096 1117 msg = None
1097 1118
1098 1119 try:
1099 1120 fp = open(filename, 'rb')
1100 1121 except IOError:
1101 1122
1102 1123 if msgFlag:
1103 1124 print "[Reading] File %s can't be opened" % (filename)
1104 1125
1105 1126 return False
1106 1127
1107 1128 currentPosition = fp.tell()
1108 1129 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1109 1130
1110 1131 if neededSize == 0:
1111 1132 basicHeaderObj = BasicHeader(LOCALTIME)
1112 1133 systemHeaderObj = SystemHeader()
1113 1134 radarControllerHeaderObj = RadarControllerHeader()
1114 1135 processingHeaderObj = ProcessingHeader()
1115 1136
1116 if not( basicHeaderObj.read(fp) ):
1137 if not(basicHeaderObj.read(fp)):
1117 1138 fp.close()
1118 1139 return False
1119 1140
1120 if not( systemHeaderObj.read(fp) ):
1141 if not(systemHeaderObj.read(fp)):
1121 1142 fp.close()
1122 1143 return False
1123 1144
1124 if not( radarControllerHeaderObj.read(fp) ):
1145 if not(radarControllerHeaderObj.read(fp)):
1125 1146 fp.close()
1126 1147 return False
1127 1148
1128 if not( processingHeaderObj.read(fp) ):
1149 if not(processingHeaderObj.read(fp)):
1129 1150 fp.close()
1130 1151 return False
1131 1152
1132 1153 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1133 1154 else:
1134 msg = "[Reading] Skipping the file %s due to it hasn't enough data" %filename
1155 msg = "[Reading] Skipping the file %s due to it hasn't enough data" % filename
1135 1156
1136 1157 fp.close()
1137 1158
1138 1159 fileSize = os.path.getsize(filename)
1139 1160 currentSize = fileSize - currentPosition
1140 1161
1141 1162 if currentSize < neededSize:
1142 1163 if msgFlag and (msg != None):
1143 1164 print msg
1144 1165 return False
1145 1166
1146 1167 return True
1147 1168
1148 1169 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1149 1170
1150 1171 path_empty = True
1151 1172
1152 1173 dateList = []
1153 1174 pathList = []
1154 1175
1155 1176 multi_path = path.split(',')
1156 1177
1157 1178 if not walk:
1158 1179
1159 1180 for single_path in multi_path:
1160 1181
1161 1182 if not os.path.isdir(single_path):
1162 1183 continue
1163 1184
1164 fileList = glob.glob1(single_path, "*"+ext)
1185 fileList = glob.glob1(single_path, "*" + ext)
1165 1186
1166 1187 if not fileList:
1167 1188 continue
1168 1189
1169 1190 path_empty = False
1170 1191
1171 1192 fileList.sort()
1172 1193
1173 1194 for thisFile in fileList:
1174 1195
1175 1196 if not os.path.isfile(os.path.join(single_path, thisFile)):
1176 1197 continue
1177 1198
1178 1199 if not isRadarFile(thisFile):
1179 1200 continue
1180 1201
1181 1202 if not isFileInDateRange(thisFile, startDate, endDate):
1182 1203 continue
1183 1204
1184 1205 thisDate = getDateFromRadarFile(thisFile)
1185 1206
1186 1207 if thisDate in dateList:
1187 1208 continue
1188 1209
1189 1210 dateList.append(thisDate)
1190 1211 pathList.append(single_path)
1191 1212
1192 1213 else:
1193 1214 for single_path in multi_path:
1194 1215
1195 1216 if not os.path.isdir(single_path):
1196 1217 continue
1197 1218
1198 1219 dirList = []
1199 1220
1200 1221 for thisPath in os.listdir(single_path):
1201 1222
1202 if not os.path.isdir(os.path.join(single_path,thisPath)):
1223 if not os.path.isdir(os.path.join(single_path, thisPath)):
1203 1224 continue
1204 1225
1205 1226 if not isRadarFolder(thisPath):
1206 1227 continue
1207 1228
1208 1229 if not isFolderInDateRange(thisPath, startDate, endDate):
1209 1230 continue
1210 1231
1211 1232 dirList.append(thisPath)
1212 1233
1213 1234 if not dirList:
1214 1235 continue
1215 1236
1216 1237 dirList.sort()
1217 1238
1218 1239 for thisDir in dirList:
1219 1240
1220 1241 datapath = os.path.join(single_path, thisDir, expLabel)
1221 fileList = glob.glob1(datapath, "*"+ext)
1242 fileList = glob.glob1(datapath, "*" + ext)
1222 1243
1223 1244 if not fileList:
1224 1245 continue
1225 1246
1226 1247 path_empty = False
1227 1248
1228 1249 thisDate = getDateFromRadarFolder(thisDir)
1229 1250
1230 1251 pathList.append(datapath)
1231 1252 dateList.append(thisDate)
1232 1253
1233 1254 dateList.sort()
1234 1255
1235 1256 if walk:
1236 1257 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1237 1258 else:
1238 1259 pattern_path = multi_path[0]
1239 1260
1240 1261 if path_empty:
1241 print "[Reading] No *%s files in %s for %s to %s" %(ext, pattern_path, startDate, endDate)
1262 print "[Reading] No *%s files in %s for %s to %s" % (ext, pattern_path, startDate, endDate)
1242 1263 else:
1243 1264 if not dateList:
1244 print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
1265 print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" % (startDate, endDate, ext, path)
1245 1266
1246 1267 if include_path:
1247 1268 return dateList, pathList
1248 1269
1249 1270 return dateList
1250 1271
1251 1272 def setup(self,
1252 path=None,
1253 startDate=None,
1254 endDate=None,
1255 startTime=datetime.time(0,0,0),
1256 endTime=datetime.time(23,59,59),
1257 set=None,
1258 expLabel = "",
1259 ext = None,
1260 online = False,
1261 delay = 60,
1262 walk = True,
1263 getblock = False,
1264 nTxs = 1,
1265 realtime=False,
1266 blocksize=None,
1267 blocktime=None,
1268 skip=None,
1269 cursor=None,
1270 warnings=True,
1271 verbose=True,
1272 server=None,
1273 format=None,
1274 oneDDict=None,
1275 twoDDict=None,
1276 ind2DList=None):
1273 path=None,
1274 startDate=None,
1275 endDate=None,
1276 startTime=datetime.time(0, 0, 0),
1277 endTime=datetime.time(23, 59, 59),
1278 set=None,
1279 expLabel="",
1280 ext=None,
1281 online=False,
1282 delay=60,
1283 walk=True,
1284 getblock=False,
1285 nTxs=1,
1286 realtime=False,
1287 blocksize=None,
1288 blocktime=None,
1289 skip=None,
1290 cursor=None,
1291 warnings=True,
1292 verbose=True,
1293 server=None,
1294 format=None,
1295 oneDDict=None,
1296 twoDDict=None,
1297 ind2DList=None):
1277 1298 if server is not None:
1278 1299 if 'tcp://' in server:
1279 1300 address = server
1280 1301 else:
1281 1302 address = 'ipc:///tmp/%s' % server
1282 1303 self.server = address
1283 1304 self.context = zmq.Context()
1284 1305 self.receiver = self.context.socket(zmq.PULL)
1285 1306 self.receiver.connect(self.server)
1286 1307 time.sleep(0.5)
1287 1308 print '[Starting] ReceiverData from {}'.format(self.server)
1288 else:
1309 else:
1289 1310 self.server = None
1290 1311 if path == None:
1291 1312 raise ValueError, "[Reading] The path is not valid"
1292 1313
1293 1314 if ext == None:
1294 1315 ext = self.ext
1295 1316
1296 1317 if online:
1297 1318 print "[Reading] Searching files in online mode..."
1298 1319
1299 for nTries in range( self.nTries ):
1300 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1320 for nTries in range(self.nTries):
1321 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(
1322 path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1301 1323
1302 1324 if fullpath:
1303 1325 break
1304 1326
1305 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
1306 sleep( self.delay )
1327 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries + 1)
1328 sleep(self.delay)
1307 1329
1308 1330 if not(fullpath):
1309 1331 print "[Reading] There 'isn't any valid file in %s" % path
1310 1332 return
1311 1333
1312 1334 self.year = year
1313 self.doy = doy
1314 self.set = set - 1
1335 self.doy = doy
1336 self.set = set - 1
1315 1337 self.path = path
1316 1338 self.foldercounter = foldercounter
1317 1339 last_set = None
1318 1340 else:
1319 1341 print "[Reading] Searching files in offline mode ..."
1320 1342 pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1321 startTime=startTime, endTime=endTime,
1322 set=set, expLabel=expLabel, ext=ext,
1323 walk=walk, cursor=cursor,
1324 skip=skip)
1343 startTime=startTime, endTime=endTime,
1344 set=set, expLabel=expLabel, ext=ext,
1345 walk=walk, cursor=cursor,
1346 skip=skip)
1325 1347
1326 1348 if not(pathList):
1327 1349 self.fileIndex = -1
1328 1350 self.pathList = []
1329 1351 self.filenameList = []
1330 1352 return
1331 1353
1332 1354 self.fileIndex = -1
1333 1355 self.pathList = pathList
1334 1356 self.filenameList = filenameList
1335 1357 file_name = os.path.basename(filenameList[-1])
1336 1358 basename, ext = os.path.splitext(file_name)
1337 1359 last_set = int(basename[-3:])
1338 1360
1339 1361 self.online = online
1340 1362 self.realtime = realtime
1341 1363 self.delay = delay
1342 1364 ext = ext.lower()
1343 1365 self.ext = ext
1344 1366 self.getByBlock = getblock
1345 1367 self.nTxs = nTxs
1346 1368 self.startTime = startTime
1347 1369 self.endTime = endTime
1348
1349 #Added-----------------
1370 self.endDate = endDate
1371 self.startDate = startDate
1372 # Added-----------------
1350 1373 self.selBlocksize = blocksize
1351 1374 self.selBlocktime = blocktime
1352 1375
1353 1376 # Verbose-----------
1354 1377 self.verbose = verbose
1355 1378 self.warnings = warnings
1356 1379
1357 1380 if not(self.setNextFile()):
1358 if (startDate!=None) and (endDate!=None):
1359 print "[Reading] No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
1381 if (startDate != None) and (endDate != None):
1382 print "[Reading] No files in range: %s - %s" % (datetime.datetime.combine(startDate, startTime).ctime(), datetime.datetime.combine(endDate, endTime).ctime())
1360 1383 elif startDate != None:
1361 print "[Reading] No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
1384 print "[Reading] No files in range: %s" % (datetime.datetime.combine(startDate, startTime).ctime())
1362 1385 else:
1363 1386 print "[Reading] No files"
1364 1387
1365 1388 self.fileIndex = -1
1366 1389 self.pathList = []
1367 1390 self.filenameList = []
1368 1391 return
1369 1392
1370 1393 # self.getBasicHeader()
1371 1394
1372 1395 if last_set != None:
1373 self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1396 self.dataOut.last_block = last_set * \
1397 self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1374 1398 return
1375 1399
1376 1400 def getBasicHeader(self):
1377 1401
1378 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1402 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond / \
1403 1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1379 1404
1380 1405 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1381 1406
1382 1407 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1383 1408
1384 1409 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1385 1410
1386 1411 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1387 1412
1388 1413 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1389 1414
1390 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
1415 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
1391 1416
1392 1417 # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1393 1418
1394
1395 1419 def getFirstHeader(self):
1396 1420
1397 1421 raise NotImplementedError
1398 1422
1399 1423 def getData(self):
1400 1424
1401 1425 raise NotImplementedError
1402 1426
1403 1427 def hasNotDataInBuffer(self):
1404 1428
1405 1429 raise NotImplementedError
1406 1430
1407 1431 def readBlock(self):
1408 1432
1409 1433 raise NotImplementedError
1410 1434
1411 1435 def isEndProcess(self):
1412 1436
1413 1437 return self.flagNoMoreFiles
1414 1438
1415 1439 def printReadBlocks(self):
1416 1440
1417 print "[Reading] Number of read blocks per file %04d" %self.nReadBlocks
1441 print "[Reading] Number of read blocks per file %04d" % self.nReadBlocks
1418 1442
1419 1443 def printTotalBlocks(self):
1420 1444
1421 print "[Reading] Number of read blocks %04d" %self.nTotalBlocks
1445 print "[Reading] Number of read blocks %04d" % self.nTotalBlocks
1422 1446
1423 1447 def printNumberOfBlock(self):
1424 1448 'SPAM!'
1425 1449
1426 1450 # if self.flagIsNewBlock:
1427 1451 # print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1428 1452 # self.processingHeaderObj.dataBlocksPerFile,
1429 1453 # self.dataOut.datatime.ctime())
1430 1454
1431 1455 def printInfo(self):
1432 1456
1433 1457 if self.__printInfo == False:
1434 1458 return
1435 1459
1436 1460 self.basicHeaderObj.printInfo()
1437 1461 self.systemHeaderObj.printInfo()
1438 1462 self.radarControllerHeaderObj.printInfo()
1439 1463 self.processingHeaderObj.printInfo()
1440 1464
1441 1465 self.__printInfo = False
1442 1466
1443 1467 def run(self,
1444 path=None,
1445 startDate=None,
1446 endDate=None,
1447 startTime=datetime.time(0,0,0),
1448 endTime=datetime.time(23,59,59),
1449 set=None,
1450 expLabel = "",
1451 ext = None,
1452 online = False,
1453 delay = 60,
1454 walk = True,
1455 getblock = False,
1456 nTxs = 1,
1457 realtime=False,
1458 blocksize=None,
1459 blocktime=None,
1460 skip=None,
1461 cursor=None,
1462 warnings=True,
1463 server=None,
1464 verbose=True,
1465 format=None,
1466 oneDDict=None,
1467 twoDDict=None,
1468 ind2DList=None, **kwargs):
1468 path=None,
1469 startDate=None,
1470 endDate=None,
1471 startTime=datetime.time(0, 0, 0),
1472 endTime=datetime.time(23, 59, 59),
1473 set=None,
1474 expLabel="",
1475 ext=None,
1476 online=False,
1477 delay=60,
1478 walk=True,
1479 getblock=False,
1480 nTxs=1,
1481 realtime=False,
1482 blocksize=None,
1483 blocktime=None,
1484 skip=None,
1485 cursor=None,
1486 warnings=True,
1487 server=None,
1488 verbose=True,
1489 format=None,
1490 oneDDict=None,
1491 twoDDict=None,
1492 ind2DList=None, **kwargs):
1469 1493
1470 1494 if not(self.isConfig):
1471 1495 self.setup(path=path,
1472 1496 startDate=startDate,
1473 1497 endDate=endDate,
1474 1498 startTime=startTime,
1475 1499 endTime=endTime,
1476 1500 set=set,
1477 1501 expLabel=expLabel,
1478 1502 ext=ext,
1479 1503 online=online,
1480 1504 delay=delay,
1481 1505 walk=walk,
1482 1506 getblock=getblock,
1483 1507 nTxs=nTxs,
1484 1508 realtime=realtime,
1485 1509 blocksize=blocksize,
1486 1510 blocktime=blocktime,
1487 1511 skip=skip,
1488 1512 cursor=cursor,
1489 1513 warnings=warnings,
1490 1514 server=server,
1491 1515 verbose=verbose,
1492 1516 format=format,
1493 1517 oneDDict=oneDDict,
1494 1518 twoDDict=twoDDict,
1495 1519 ind2DList=ind2DList)
1496 1520 self.isConfig = True
1497 1521 if server is None:
1498 1522 self.getData()
1499 else:
1523 else:
1500 1524 self.getFromServer()
1501 1525
1526
1502 1527 class JRODataWriter(JRODataIO):
1503 1528
1504 1529 """
1505 1530 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1506 1531 de los datos siempre se realiza por bloques.
1507 1532 """
1508 1533
1509 1534 blockIndex = 0
1510 1535
1511 1536 path = None
1512 1537
1513 1538 setFile = None
1514 1539
1515 1540 profilesPerBlock = None
1516 1541
1517 1542 blocksPerFile = None
1518 1543
1519 1544 nWriteBlocks = 0
1520 1545
1521 1546 fileDate = None
1522 1547
1523 1548 def __init__(self, dataOut=None):
1524 1549 raise NotImplementedError
1525 1550
1526
1527 1551 def hasAllDataInBuffer(self):
1528 1552 raise NotImplementedError
1529 1553
1530
1531 1554 def setBlockDimension(self):
1532 1555 raise NotImplementedError
1533 1556
1534
1535 1557 def writeBlock(self):
1536 1558 raise NotImplementedError
1537 1559
1538
1539 1560 def putData(self):
1540 1561 raise NotImplementedError
1541 1562
1542
1543 1563 def getProcessFlags(self):
1544 1564
1545 1565 processFlags = 0
1546 1566
1547 1567 dtype_index = get_dtype_index(self.dtype)
1548 1568 procflag_dtype = get_procflag_dtype(dtype_index)
1549 1569
1550 1570 processFlags += procflag_dtype
1551 1571
1552 1572 if self.dataOut.flagDecodeData:
1553 1573 processFlags += PROCFLAG.DECODE_DATA
1554 1574
1555 1575 if self.dataOut.flagDeflipData:
1556 1576 processFlags += PROCFLAG.DEFLIP_DATA
1557 1577
1558 1578 if self.dataOut.code is not None:
1559 1579 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1560 1580
1561 1581 if self.dataOut.nCohInt > 1:
1562 1582 processFlags += PROCFLAG.COHERENT_INTEGRATION
1563 1583
1564 1584 if self.dataOut.type == "Spectra":
1565 1585 if self.dataOut.nIncohInt > 1:
1566 1586 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1567 1587
1568 1588 if self.dataOut.data_dc is not None:
1569 1589 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1570 1590
1571 1591 if self.dataOut.flagShiftFFT:
1572 1592 processFlags += PROCFLAG.SHIFT_FFT_DATA
1573 1593
1574 1594 return processFlags
1575 1595
1576 1596 def setBasicHeader(self):
1577 1597
1578 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1598 self.basicHeaderObj.size = self.basicHeaderSize # bytes
1579 1599 self.basicHeaderObj.version = self.versionFile
1580 1600 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1581 1601
1582 1602 utc = numpy.floor(self.dataOut.utctime)
1583 milisecond = (self.dataOut.utctime - utc)* 1000.0
1603 milisecond = (self.dataOut.utctime - utc) * 1000.0
1584 1604
1585 1605 self.basicHeaderObj.utc = utc
1586 1606 self.basicHeaderObj.miliSecond = milisecond
1587 1607 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1588 1608 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1589 1609 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1590 1610
1591 1611 def setFirstHeader(self):
1592 1612 """
1593 1613 Obtiene una copia del First Header
1594 1614
1595 1615 Affected:
1596 1616
1597 1617 self.basicHeaderObj
1598 1618 self.systemHeaderObj
1599 1619 self.radarControllerHeaderObj
1600 1620 self.processingHeaderObj self.
1601 1621
1602 1622 Return:
1603 1623 None
1604 1624 """
1605 1625
1606 1626 raise NotImplementedError
1607 1627
1608 1628 def __writeFirstHeader(self):
1609 1629 """
1610 1630 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1611 1631
1612 1632 Affected:
1613 1633 __dataType
1614 1634
1615 1635 Return:
1616 1636 None
1617 1637 """
1618 1638
1619 1639 # CALCULAR PARAMETROS
1620 1640
1621 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1641 sizeLongHeader = self.systemHeaderObj.size + \
1642 self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1622 1643 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1623 1644
1624 1645 self.basicHeaderObj.write(self.fp)
1625 1646 self.systemHeaderObj.write(self.fp)
1626 1647 self.radarControllerHeaderObj.write(self.fp)
1627 1648 self.processingHeaderObj.write(self.fp)
1628 1649
1629 1650 def __setNewBlock(self):
1630 1651 """
1631 1652 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1632 1653
1633 1654 Return:
1634 1655 0 : si no pudo escribir nada
1635 1656 1 : Si escribio el Basic el First Header
1636 1657 """
1637 1658 if self.fp == None:
1638 1659 self.setNextFile()
1639 1660
1640 1661 if self.flagIsNewFile:
1641 1662 return 1
1642 1663
1643 1664 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1644 1665 self.basicHeaderObj.write(self.fp)
1645 1666 return 1
1646 1667
1647 if not( self.setNextFile() ):
1668 if not(self.setNextFile()):
1648 1669 return 0
1649 1670
1650 1671 return 1
1651 1672
1652
1653 1673 def writeNextBlock(self):
1654 1674 """
1655 1675 Selecciona el bloque siguiente de datos y los escribe en un file
1656 1676
1657 1677 Return:
1658 1678 0 : Si no hizo pudo escribir el bloque de datos
1659 1679 1 : Si no pudo escribir el bloque de datos
1660 1680 """
1661 if not( self.__setNewBlock() ):
1681 if not(self.__setNewBlock()):
1662 1682 return 0
1663 1683
1664 1684 self.writeBlock()
1665 1685
1666 print "[Writing] Block No. %d/%d" %(self.blockIndex,
1667 self.processingHeaderObj.dataBlocksPerFile)
1686 print "[Writing] Block No. %d/%d" % (self.blockIndex,
1687 self.processingHeaderObj.dataBlocksPerFile)
1668 1688
1669 1689 return 1
1670 1690
1671 1691 def setNextFile(self):
1672 1692 """
1673 1693 Determina el siguiente file que sera escrito
1674 1694
1675 1695 Affected:
1676 1696 self.filename
1677 1697 self.subfolder
1678 1698 self.fp
1679 1699 self.setFile
1680 1700 self.flagIsNewFile
1681 1701
1682 1702 Return:
1683 1703 0 : Si el archivo no puede ser escrito
1684 1704 1 : Si el archivo esta listo para ser escrito
1685 1705 """
1686 1706 ext = self.ext
1687 1707 path = self.path
1688 1708
1689 1709 if self.fp != None:
1690 1710 self.fp.close()
1691 1711
1692 timeTuple = time.localtime( self.dataOut.utctime)
1693 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1712 timeTuple = time.localtime(self.dataOut.utctime)
1713 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday)
1694 1714
1695 fullpath = os.path.join( path, subfolder )
1715 fullpath = os.path.join(path, subfolder)
1696 1716 setFile = self.setFile
1697 1717
1698 if not( os.path.exists(fullpath) ):
1718 if not(os.path.exists(fullpath)):
1699 1719 os.mkdir(fullpath)
1700 setFile = -1 #inicializo mi contador de seteo
1720 setFile = -1 # inicializo mi contador de seteo
1701 1721 else:
1702 filesList = os.listdir( fullpath )
1703 if len( filesList ) > 0:
1704 filesList = sorted( filesList, key=str.lower )
1722 filesList = os.listdir(fullpath)
1723 if len(filesList) > 0:
1724 filesList = sorted(filesList, key=str.lower)
1705 1725 filen = filesList[-1]
1706 1726 # el filename debera tener el siguiente formato
1707 1727 # 0 1234 567 89A BCDE (hex)
1708 1728 # x YYYY DDD SSS .ext
1709 if isNumber( filen[8:11] ):
1710 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1729 if isNumber(filen[8:11]):
1730 # inicializo mi contador de seteo al seteo del ultimo file
1731 setFile = int(filen[8:11])
1711 1732 else:
1712 1733 setFile = -1
1713 1734 else:
1714 setFile = -1 #inicializo mi contador de seteo
1735 setFile = -1 # inicializo mi contador de seteo
1715 1736
1716 1737 setFile += 1
1717 1738
1718 #If this is a new day it resets some values
1739 # If this is a new day it resets some values
1719 1740 if self.dataOut.datatime.date() > self.fileDate:
1720 1741 setFile = 0
1721 1742 self.nTotalBlocks = 0
1722 1743
1723 filen = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext )
1744 filen = '%s%4.4d%3.3d%3.3d%s' % (
1745 self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext)
1724 1746
1725 filename = os.path.join( path, subfolder, filen )
1747 filename = os.path.join(path, subfolder, filen)
1726 1748
1727 fp = open( filename,'wb' )
1749 fp = open(filename, 'wb')
1728 1750
1729 1751 self.blockIndex = 0
1730 1752
1731 #guardando atributos
1753 # guardando atributos
1732 1754 self.filename = filename
1733 1755 self.subfolder = subfolder
1734 1756 self.fp = fp
1735 1757 self.setFile = setFile
1736 1758 self.flagIsNewFile = 1
1737 1759 self.fileDate = self.dataOut.datatime.date()
1738 1760
1739 1761 self.setFirstHeader()
1740 1762
1741 print '[Writing] Opening file: %s'%self.filename
1763 print '[Writing] Opening file: %s' % self.filename
1742 1764
1743 1765 self.__writeFirstHeader()
1744 1766
1745 1767 return 1
1746 1768
1747 1769 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1748 1770 """
1749 1771 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1750 1772
1751 1773 Inputs:
1752 1774 path : directory where data will be saved
1753 1775 profilesPerBlock : number of profiles per block
1754 1776 set : initial file set
1755 1777 datatype : An integer number that defines data type:
1756 1778 0 : int8 (1 byte)
1757 1779 1 : int16 (2 bytes)
1758 1780 2 : int32 (4 bytes)
1759 1781 3 : int64 (8 bytes)
1760 1782 4 : float32 (4 bytes)
1761 1783 5 : double64 (8 bytes)
1762 1784
1763 1785 Return:
1764 1786 0 : Si no realizo un buen seteo
1765 1787 1 : Si realizo un buen seteo
1766 1788 """
1767 1789
1768 1790 if ext == None:
1769 1791 ext = self.ext
1770 1792
1771 1793 self.ext = ext.lower()
1772 1794
1773 1795 self.path = path
1774 1796
1775 1797 if set is None:
1776 1798 self.setFile = -1
1777 1799 else:
1778 1800 self.setFile = set - 1
1779 1801
1780 1802 self.blocksPerFile = blocksPerFile
1781 1803
1782 1804 self.profilesPerBlock = profilesPerBlock
1783 1805
1784 1806 self.dataOut = dataOut
1785 1807 self.fileDate = self.dataOut.datatime.date()
1786 #By default
1808 # By default
1787 1809 self.dtype = self.dataOut.dtype
1788 1810
1789 1811 if datatype is not None:
1790 1812 self.dtype = get_numpy_dtype(datatype)
1791 1813
1792 1814 if not(self.setNextFile()):
1793 1815 print "[Writing] There isn't a next file"
1794 1816 return 0
1795 1817
1796 1818 self.setBlockDimension()
1797 1819
1798 1820 return 1
1799 1821
1800 1822 def run(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1801 1823
1802 1824 if not(self.isConfig):
1803 1825
1804 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock, set=set, ext=ext, datatype=datatype, **kwargs)
1826 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock,
1827 set=set, ext=ext, datatype=datatype, **kwargs)
1805 1828 self.isConfig = True
1806 1829
1807 1830 self.putData()
This diff has been collapsed as it changes many lines, (1270 lines changed) Show them Hide them
@@ -1,1154 +1,1182
1 import os, sys
1 import os
2 import sys
2 3 import glob
3 4 import fnmatch
4 5 import datetime
5 6 import time
6 7 import re
7 8 import h5py
8 9 import numpy
9 import matplotlib.pyplot as plt
10 10
11 11 import pylab as plb
12 12 from scipy.optimize import curve_fit
13 13 from scipy import asarray as ar, exp
14 14 from scipy import stats
15 15
16 16 from numpy.ma.core import getdata
17 17
18 18 SPEED_OF_LIGHT = 299792458
19 19 SPEED_OF_LIGHT = 3e8
20 20
21 21 try:
22 22 from gevent import sleep
23 23 except:
24 24 from time import sleep
25 25
26 26 from schainpy.model.data.jrodata import Spectra
27 27 #from schainpy.model.data.BLTRheaderIO import FileHeader, RecordHeader
28 28 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
29 29 #from schainpy.model.io.jroIO_bltr import BLTRReader
30 30 from numpy import imag, shape, NaN
31 31
32 32 from jroIO_base import JRODataReader
33 33
34 34
35 35 class Header(object):
36
36
37 37 def __init__(self):
38 38 raise NotImplementedError
39
40
39
41 40 def read(self):
42
41
43 42 raise NotImplementedError
44
43
45 44 def write(self):
46
45
47 46 raise NotImplementedError
48
47
49 48 def printInfo(self):
50
51 message = "#"*50 + "\n"
49
50 message = "#" * 50 + "\n"
52 51 message += self.__class__.__name__.upper() + "\n"
53 message += "#"*50 + "\n"
54
52 message += "#" * 50 + "\n"
53
55 54 keyList = self.__dict__.keys()
56 55 keyList.sort()
57
56
58 57 for key in keyList:
59 message += "%s = %s" %(key, self.__dict__[key]) + "\n"
60
58 message += "%s = %s" % (key, self.__dict__[key]) + "\n"
59
61 60 if "size" not in keyList:
62 61 attr = getattr(self, "size")
63
64 if attr:
65 message += "%s = %s" %("size", attr) + "\n"
66
67 #print message
68 62
63 if attr:
64 message += "%s = %s" % ("size", attr) + "\n"
69 65
66 # print message
70 67
71 68
69 FILE_STRUCTURE = numpy.dtype([ # HEADER 48bytes
70 ('FileMgcNumber', '<u4'), # 0x23020100
71 # No Of FDT data records in this file (0 or more)
72 ('nFDTdataRecors', '<u4'),
73 ('OffsetStartHeader', '<u4'),
74 ('RadarUnitId', '<u4'),
75 ('SiteName', numpy.str_, 32), # Null terminated
76 ])
72 77
73 FILE_STRUCTURE = numpy.dtype([ #HEADER 48bytes
74 ('FileMgcNumber','<u4'), #0x23020100
75 ('nFDTdataRecors','<u4'), #No Of FDT data records in this file (0 or more)
76 ('OffsetStartHeader','<u4'),
77 ('RadarUnitId','<u4'),
78 ('SiteName',numpy.str_,32), #Null terminated
79 ])
80 78
81 79 class FileHeaderBLTR(Header):
82
80
83 81 def __init__(self):
84
85 self.FileMgcNumber= 0 #0x23020100
86 self.nFDTdataRecors=0 #No Of FDT data records in this file (0 or more)
87 self.RadarUnitId= 0
88 self.OffsetStartHeader=0
89 self.SiteName= ""
82
83 self.FileMgcNumber = 0 # 0x23020100
84 # No Of FDT data records in this file (0 or more)
85 self.nFDTdataRecors = 0
86 self.RadarUnitId = 0
87 self.OffsetStartHeader = 0
88 self.SiteName = ""
90 89 self.size = 48
91
90
92 91 def FHread(self, fp):
93 #try:
94 startFp = open(fp,"rb")
95
96 header = numpy.fromfile(startFp, FILE_STRUCTURE,1)
97
92 # try:
93 startFp = open(fp, "rb")
94
95 header = numpy.fromfile(startFp, FILE_STRUCTURE, 1)
96
98 97 print ' '
99 98 print 'puntero file header', startFp.tell()
100 99 print ' '
101
102
100
103 101 ''' numpy.fromfile(file, dtype, count, sep='')
104 102 file : file or str
105 103 Open file object or filename.
106 104
107 105 dtype : data-type
108 106 Data type of the returned array. For binary files, it is used to determine
109 107 the size and byte-order of the items in the file.
110 108
111 109 count : int
112 110 Number of items to read. -1 means all items (i.e., the complete file).
113 111
114 112 sep : str
115 113 Separator between items if file is a text file. Empty ("") separator means
116 114 the file should be treated as binary. Spaces (" ") in the separator match zero
117 115 or more whitespace characters. A separator consisting only of spaces must match
118 116 at least one whitespace.
119 117
120 118 '''
121 119
120 self.FileMgcNumber = hex(header['FileMgcNumber'][0])
121 # No Of FDT data records in this file (0 or more)
122 self.nFDTdataRecors = int(header['nFDTdataRecors'][0])
123 self.RadarUnitId = int(header['RadarUnitId'][0])
124 self.OffsetStartHeader = int(header['OffsetStartHeader'][0])
125 self.SiteName = str(header['SiteName'][0])
126
127 # print 'Numero de bloques', self.nFDTdataRecors
122 128
123
124 self.FileMgcNumber= hex(header['FileMgcNumber'][0])
125 self.nFDTdataRecors=int(header['nFDTdataRecors'][0]) #No Of FDT data records in this file (0 or more)
126 self.RadarUnitId= int(header['RadarUnitId'][0])
127 self.OffsetStartHeader= int(header['OffsetStartHeader'][0])
128 self.SiteName= str(header['SiteName'][0])
129
130 #print 'Numero de bloques', self.nFDTdataRecors
131
132
133 if self.size <48:
129 if self.size < 48:
134 130 return 0
135
131
136 132 return 1
137
138
133
139 134 def write(self, fp):
140
135
141 136 headerTuple = (self.FileMgcNumber,
142 137 self.nFDTdataRecors,
143 138 self.RadarUnitId,
144 139 self.SiteName,
145 140 self.size)
146
147
141
148 142 header = numpy.array(headerTuple, FILE_STRUCTURE)
149 143 # numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
150 144 header.tofile(fp)
151 145 ''' ndarray.tofile(fid, sep, format) Write array to a file as text or binary (default).
152 146
153 147 fid : file or str
154 148 An open file object, or a string containing a filename.
155 149
156 150 sep : str
157 151 Separator between array items for text output. If "" (empty), a binary file is written,
158 152 equivalent to file.write(a.tobytes()).
159 153
160 154 format : str
161 155 Format string for text file output. Each entry in the array is formatted to text by
162 156 first converting it to the closest Python type, and then using "format" % item.
163 157
164 158 '''
165
166 return 1
167 159
160 return 1
168 161
169 162
163 RECORD_STRUCTURE = numpy.dtype([ # RECORD HEADER 180+20N bytes
164 ('RecMgcNumber', '<u4'), # 0x23030001
165 ('RecCounter', '<u4'), # Record counter(0,1, ...)
166 # Offset to start of next record form start of this record
167 ('Off2StartNxtRec', '<u4'),
168 # Offset to start of data from start of this record
169 ('Off2StartData', '<u4'),
170 # Epoch time stamp of start of acquisition (seconds)
171 ('nUtime', '<i4'),
172 # Millisecond component of time stamp (0,...,999)
173 ('nMilisec', '<u4'),
174 # Experiment tag name (null terminated)
175 ('ExpTagName', numpy.str_, 32),
176 # Experiment comment (null terminated)
177 ('ExpComment', numpy.str_, 32),
178 # Site latitude (from GPS) in degrees (positive implies North)
179 ('SiteLatDegrees', '<f4'),
180 # Site longitude (from GPS) in degrees (positive implies East)
181 ('SiteLongDegrees', '<f4'),
182 # RTC GPS engine status (0=SEEK, 1=LOCK, 2=NOT FITTED, 3=UNAVAILABLE)
183 ('RTCgpsStatus', '<u4'),
184 ('TransmitFrec', '<u4'), # Transmit frequency (Hz)
185 ('ReceiveFrec', '<u4'), # Receive frequency
186 # First local oscillator frequency (Hz)
187 ('FirstOsciFrec', '<u4'),
188 # (0="O", 1="E", 2="linear 1", 3="linear2")
189 ('Polarisation', '<u4'),
190 # Receiver filter settings (0,1,2,3)
191 ('ReceiverFiltSett', '<u4'),
192 # Number of modes in use (1 or 2)
193 ('nModesInUse', '<u4'),
194 # Dual Mode index number for these data (0 or 1)
195 ('DualModeIndex', '<u4'),
196 # Dual Mode range correction for these data (m)
197 ('DualModeRange', '<u4'),
198 # Number of digital channels acquired (2*N)
199 ('nDigChannels', '<u4'),
200 # Sampling resolution (meters)
201 ('SampResolution', '<u4'),
202 # Number of range gates sampled
203 ('nHeights', '<u4'),
204 # Start range of sampling (meters)
205 ('StartRangeSamp', '<u4'),
206 ('PRFhz', '<u4'), # PRF (Hz)
207 ('nCohInt', '<u4'), # Integrations
208 # Number of data points transformed
209 ('nProfiles', '<u4'),
210 # Number of receive beams stored in file (1 or N)
211 ('nChannels', '<u4'),
212 ('nIncohInt', '<u4'), # Number of spectral averages
213 # FFT windowing index (0 = no window)
214 ('FFTwindowingInd', '<u4'),
215 # Beam steer angle (azimuth) in degrees (clockwise from true North)
216 ('BeamAngleAzim', '<f4'),
217 # Beam steer angle (zenith) in degrees (0=> vertical)
218 ('BeamAngleZen', '<f4'),
219 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
220 ('AntennaCoord0', '<f4'),
221 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
222 ('AntennaAngl0', '<f4'),
223 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
224 ('AntennaCoord1', '<f4'),
225 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
226 ('AntennaAngl1', '<f4'),
227 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
228 ('AntennaCoord2', '<f4'),
229 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
230 ('AntennaAngl2', '<f4'),
231 # Receiver phase calibration (degrees) - N values
232 ('RecPhaseCalibr0', '<f4'),
233 # Receiver phase calibration (degrees) - N values
234 ('RecPhaseCalibr1', '<f4'),
235 # Receiver phase calibration (degrees) - N values
236 ('RecPhaseCalibr2', '<f4'),
237 # Receiver amplitude calibration (ratio relative to receiver one) - N values
238 ('RecAmpCalibr0', '<f4'),
239 # Receiver amplitude calibration (ratio relative to receiver one) - N values
240 ('RecAmpCalibr1', '<f4'),
241 # Receiver amplitude calibration (ratio relative to receiver one) - N values
242 ('RecAmpCalibr2', '<f4'),
243 # Receiver gains in dB - N values
244 ('ReceiverGaindB0', '<i4'),
245 # Receiver gains in dB - N values
246 ('ReceiverGaindB1', '<i4'),
247 # Receiver gains in dB - N values
248 ('ReceiverGaindB2', '<i4'),
249 ])
170 250
171 251
172 RECORD_STRUCTURE = numpy.dtype([ #RECORD HEADER 180+20N bytes
173 ('RecMgcNumber','<u4'), #0x23030001
174 ('RecCounter','<u4'), #Record counter(0,1, ...)
175 ('Off2StartNxtRec','<u4'), #Offset to start of next record form start of this record
176 ('Off2StartData','<u4'), #Offset to start of data from start of this record
177 ('nUtime','<i4'), #Epoch time stamp of start of acquisition (seconds)
178 ('nMilisec','<u4'), #Millisecond component of time stamp (0,...,999)
179 ('ExpTagName',numpy.str_,32), #Experiment tag name (null terminated)
180 ('ExpComment',numpy.str_,32), #Experiment comment (null terminated)
181 ('SiteLatDegrees','<f4'), #Site latitude (from GPS) in degrees (positive implies North)
182 ('SiteLongDegrees','<f4'), #Site longitude (from GPS) in degrees (positive implies East)
183 ('RTCgpsStatus','<u4'), #RTC GPS engine status (0=SEEK, 1=LOCK, 2=NOT FITTED, 3=UNAVAILABLE)
184 ('TransmitFrec','<u4'), #Transmit frequency (Hz)
185 ('ReceiveFrec','<u4'), #Receive frequency
186 ('FirstOsciFrec','<u4'), #First local oscillator frequency (Hz)
187 ('Polarisation','<u4'), #(0="O", 1="E", 2="linear 1", 3="linear2")
188 ('ReceiverFiltSett','<u4'), #Receiver filter settings (0,1,2,3)
189 ('nModesInUse','<u4'), #Number of modes in use (1 or 2)
190 ('DualModeIndex','<u4'), #Dual Mode index number for these data (0 or 1)
191 ('DualModeRange','<u4'), #Dual Mode range correction for these data (m)
192 ('nDigChannels','<u4'), #Number of digital channels acquired (2*N)
193 ('SampResolution','<u4'), #Sampling resolution (meters)
194 ('nHeights','<u4'), #Number of range gates sampled
195 ('StartRangeSamp','<u4'), #Start range of sampling (meters)
196 ('PRFhz','<u4'), #PRF (Hz)
197 ('nCohInt','<u4'), #Integrations
198 ('nProfiles','<u4'), #Number of data points transformed
199 ('nChannels','<u4'), #Number of receive beams stored in file (1 or N)
200 ('nIncohInt','<u4'), #Number of spectral averages
201 ('FFTwindowingInd','<u4'), #FFT windowing index (0 = no window)
202 ('BeamAngleAzim','<f4'), #Beam steer angle (azimuth) in degrees (clockwise from true North)
203 ('BeamAngleZen','<f4'), #Beam steer angle (zenith) in degrees (0=> vertical)
204 ('AntennaCoord0','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
205 ('AntennaAngl0','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
206 ('AntennaCoord1','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
207 ('AntennaAngl1','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
208 ('AntennaCoord2','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
209 ('AntennaAngl2','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
210 ('RecPhaseCalibr0','<f4'), #Receiver phase calibration (degrees) - N values
211 ('RecPhaseCalibr1','<f4'), #Receiver phase calibration (degrees) - N values
212 ('RecPhaseCalibr2','<f4'), #Receiver phase calibration (degrees) - N values
213 ('RecAmpCalibr0','<f4'), #Receiver amplitude calibration (ratio relative to receiver one) - N values
214 ('RecAmpCalibr1','<f4'), #Receiver amplitude calibration (ratio relative to receiver one) - N values
215 ('RecAmpCalibr2','<f4'), #Receiver amplitude calibration (ratio relative to receiver one) - N values
216 ('ReceiverGaindB0','<i4'), #Receiver gains in dB - N values
217 ('ReceiverGaindB1','<i4'), #Receiver gains in dB - N values
218 ('ReceiverGaindB2','<i4'), #Receiver gains in dB - N values
219 ])
252 class RecordHeaderBLTR(Header):
220 253
254 def __init__(self, RecMgcNumber=None, RecCounter=0, Off2StartNxtRec=811248,
255 nUtime=0, nMilisec=0, ExpTagName=None,
256 ExpComment=None, SiteLatDegrees=0, SiteLongDegrees=0,
257 RTCgpsStatus=0, TransmitFrec=0, ReceiveFrec=0,
258 FirstOsciFrec=0, Polarisation=0, ReceiverFiltSett=0,
259 nModesInUse=0, DualModeIndex=0, DualModeRange=0,
260 nDigChannels=0, SampResolution=0, nHeights=0,
261 StartRangeSamp=0, PRFhz=0, nCohInt=0,
262 nProfiles=0, nChannels=0, nIncohInt=0,
263 FFTwindowingInd=0, BeamAngleAzim=0, BeamAngleZen=0,
264 AntennaCoord0=0, AntennaCoord1=0, AntennaCoord2=0,
265 RecPhaseCalibr0=0, RecPhaseCalibr1=0, RecPhaseCalibr2=0,
266 RecAmpCalibr0=0, RecAmpCalibr1=0, RecAmpCalibr2=0,
267 AntennaAngl0=0, AntennaAngl1=0, AntennaAngl2=0,
268 ReceiverGaindB0=0, ReceiverGaindB1=0, ReceiverGaindB2=0, Off2StartData=0, OffsetStartHeader=0):
221 269
222 class RecordHeaderBLTR(Header):
223
224 def __init__(self, RecMgcNumber=None, RecCounter= 0, Off2StartNxtRec= 811248,
225 nUtime= 0, nMilisec= 0, ExpTagName= None,
226 ExpComment=None, SiteLatDegrees=0, SiteLongDegrees= 0,
227 RTCgpsStatus= 0, TransmitFrec= 0, ReceiveFrec= 0,
228 FirstOsciFrec= 0, Polarisation= 0, ReceiverFiltSett= 0,
229 nModesInUse= 0, DualModeIndex= 0, DualModeRange= 0,
230 nDigChannels= 0, SampResolution= 0, nHeights= 0,
231 StartRangeSamp= 0, PRFhz= 0, nCohInt= 0,
232 nProfiles= 0, nChannels= 0, nIncohInt= 0,
233 FFTwindowingInd= 0, BeamAngleAzim= 0, BeamAngleZen= 0,
234 AntennaCoord0= 0, AntennaCoord1= 0, AntennaCoord2= 0,
235 RecPhaseCalibr0= 0, RecPhaseCalibr1= 0, RecPhaseCalibr2= 0,
236 RecAmpCalibr0= 0, RecAmpCalibr1= 0, RecAmpCalibr2= 0,
237 AntennaAngl0=0, AntennaAngl1=0, AntennaAngl2=0,
238 ReceiverGaindB0= 0, ReceiverGaindB1= 0, ReceiverGaindB2= 0, Off2StartData=0, OffsetStartHeader=0):
239
240 self.RecMgcNumber = RecMgcNumber #0x23030001
241 self.RecCounter = RecCounter
242 self.Off2StartNxtRec = Off2StartNxtRec
270 self.RecMgcNumber = RecMgcNumber # 0x23030001
271 self.RecCounter = RecCounter
272 self.Off2StartNxtRec = Off2StartNxtRec
243 273 self.Off2StartData = Off2StartData
244 self.nUtime = nUtime
245 self.nMilisec = nMilisec
246 self.ExpTagName = ExpTagName
247 self.ExpComment = ExpComment
248 self.SiteLatDegrees = SiteLatDegrees
249 self.SiteLongDegrees = SiteLongDegrees
250 self.RTCgpsStatus = RTCgpsStatus
251 self.TransmitFrec = TransmitFrec
252 self.ReceiveFrec = ReceiveFrec
253 self.FirstOsciFrec = FirstOsciFrec
254 self.Polarisation = Polarisation
255 self.ReceiverFiltSett = ReceiverFiltSett
256 self.nModesInUse = nModesInUse
257 self.DualModeIndex = DualModeIndex
258 self.DualModeRange = DualModeRange
274 self.nUtime = nUtime
275 self.nMilisec = nMilisec
276 self.ExpTagName = ExpTagName
277 self.ExpComment = ExpComment
278 self.SiteLatDegrees = SiteLatDegrees
279 self.SiteLongDegrees = SiteLongDegrees
280 self.RTCgpsStatus = RTCgpsStatus
281 self.TransmitFrec = TransmitFrec
282 self.ReceiveFrec = ReceiveFrec
283 self.FirstOsciFrec = FirstOsciFrec
284 self.Polarisation = Polarisation
285 self.ReceiverFiltSett = ReceiverFiltSett
286 self.nModesInUse = nModesInUse
287 self.DualModeIndex = DualModeIndex
288 self.DualModeRange = DualModeRange
259 289 self.nDigChannels = nDigChannels
260 self.SampResolution = SampResolution
261 self.nHeights = nHeights
262 self.StartRangeSamp = StartRangeSamp
263 self.PRFhz = PRFhz
264 self.nCohInt = nCohInt
265 self.nProfiles = nProfiles
266 self.nChannels = nChannels
267 self.nIncohInt = nIncohInt
268 self.FFTwindowingInd = FFTwindowingInd
269 self.BeamAngleAzim = BeamAngleAzim
270 self.BeamAngleZen = BeamAngleZen
290 self.SampResolution = SampResolution
291 self.nHeights = nHeights
292 self.StartRangeSamp = StartRangeSamp
293 self.PRFhz = PRFhz
294 self.nCohInt = nCohInt
295 self.nProfiles = nProfiles
296 self.nChannels = nChannels
297 self.nIncohInt = nIncohInt
298 self.FFTwindowingInd = FFTwindowingInd
299 self.BeamAngleAzim = BeamAngleAzim
300 self.BeamAngleZen = BeamAngleZen
271 301 self.AntennaCoord0 = AntennaCoord0
272 302 self.AntennaAngl0 = AntennaAngl0
273 303 self.AntennaAngl1 = AntennaAngl1
274 304 self.AntennaAngl2 = AntennaAngl2
275 305 self.AntennaCoord1 = AntennaCoord1
276 self.AntennaCoord2 = AntennaCoord2
306 self.AntennaCoord2 = AntennaCoord2
277 307 self.RecPhaseCalibr0 = RecPhaseCalibr0
278 308 self.RecPhaseCalibr1 = RecPhaseCalibr1
279 self.RecPhaseCalibr2 = RecPhaseCalibr2
309 self.RecPhaseCalibr2 = RecPhaseCalibr2
280 310 self.RecAmpCalibr0 = RecAmpCalibr0
281 311 self.RecAmpCalibr1 = RecAmpCalibr1
282 self.RecAmpCalibr2 = RecAmpCalibr2
312 self.RecAmpCalibr2 = RecAmpCalibr2
283 313 self.ReceiverGaindB0 = ReceiverGaindB0
284 314 self.ReceiverGaindB1 = ReceiverGaindB1
285 self.ReceiverGaindB2 = ReceiverGaindB2
286 self.OffsetStartHeader = 48
287
288
289
315 self.ReceiverGaindB2 = ReceiverGaindB2
316 self.OffsetStartHeader = 48
317
290 318 def RHread(self, fp):
291 #print fp
292 #startFp = open('/home/erick/Documents/Data/huancayo.20161019.22.fdt',"rb") #The method tell() returns the current position of the file read/write pointer within the file.
293 startFp = open(fp,"rb") #The method tell() returns the current position of the file read/write pointer within the file.
294 #RecCounter=0
295 #Off2StartNxtRec=811248
296 OffRHeader= self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
319 # print fp
320 # startFp = open('/home/erick/Documents/Data/huancayo.20161019.22.fdt',"rb") #The method tell() returns the current position of the file read/write pointer within the file.
321 # The method tell() returns the current position of the file read/write pointer within the file.
322 startFp = open(fp, "rb")
323 # RecCounter=0
324 # Off2StartNxtRec=811248
325 OffRHeader = self.OffsetStartHeader + self.RecCounter * self.Off2StartNxtRec
297 326 print ' '
298 327 print 'puntero Record Header', startFp.tell()
299 328 print ' '
300
301
329
302 330 startFp.seek(OffRHeader, os.SEEK_SET)
303
331
304 332 print ' '
305 333 print 'puntero Record Header con seek', startFp.tell()
306 334 print ' '
307
308 #print 'Posicion del bloque: ',OffRHeader
309
310 header = numpy.fromfile(startFp,RECORD_STRUCTURE,1)
311
335
336 # print 'Posicion del bloque: ',OffRHeader
337
338 header = numpy.fromfile(startFp, RECORD_STRUCTURE, 1)
339
312 340 print ' '
313 341 print 'puntero Record Header con seek', startFp.tell()
314 342 print ' '
315
343
316 344 print ' '
317 345 #
318 #print 'puntero Record Header despues de seek', header.tell()
346 # print 'puntero Record Header despues de seek', header.tell()
319 347 print ' '
320
321 self.RecMgcNumber = hex(header['RecMgcNumber'][0]) #0x23030001
322 self.RecCounter = int(header['RecCounter'][0])
323 self.Off2StartNxtRec = int(header['Off2StartNxtRec'][0])
324 self.Off2StartData = int(header['Off2StartData'][0])
325 self.nUtime = header['nUtime'][0]
326 self.nMilisec = header['nMilisec'][0]
327 self.ExpTagName = str(header['ExpTagName'][0])
328 self.ExpComment = str(header['ExpComment'][0])
329 self.SiteLatDegrees = header['SiteLatDegrees'][0]
330 self.SiteLongDegrees = header['SiteLongDegrees'][0]
331 self.RTCgpsStatus = header['RTCgpsStatus'][0]
332 self.TransmitFrec = header['TransmitFrec'][0]
333 self.ReceiveFrec = header['ReceiveFrec'][0]
334 self.FirstOsciFrec = header['FirstOsciFrec'][0]
335 self.Polarisation = header['Polarisation'][0]
336 self.ReceiverFiltSett = header['ReceiverFiltSett'][0]
337 self.nModesInUse = header['nModesInUse'][0]
338 self.DualModeIndex = header['DualModeIndex'][0]
339 self.DualModeRange = header['DualModeRange'][0]
348
349 self.RecMgcNumber = hex(header['RecMgcNumber'][0]) # 0x23030001
350 self.RecCounter = int(header['RecCounter'][0])
351 self.Off2StartNxtRec = int(header['Off2StartNxtRec'][0])
352 self.Off2StartData = int(header['Off2StartData'][0])
353 self.nUtime = header['nUtime'][0]
354 self.nMilisec = header['nMilisec'][0]
355 self.ExpTagName = str(header['ExpTagName'][0])
356 self.ExpComment = str(header['ExpComment'][0])
357 self.SiteLatDegrees = header['SiteLatDegrees'][0]
358 self.SiteLongDegrees = header['SiteLongDegrees'][0]
359 self.RTCgpsStatus = header['RTCgpsStatus'][0]
360 self.TransmitFrec = header['TransmitFrec'][0]
361 self.ReceiveFrec = header['ReceiveFrec'][0]
362 self.FirstOsciFrec = header['FirstOsciFrec'][0]
363 self.Polarisation = header['Polarisation'][0]
364 self.ReceiverFiltSett = header['ReceiverFiltSett'][0]
365 self.nModesInUse = header['nModesInUse'][0]
366 self.DualModeIndex = header['DualModeIndex'][0]
367 self.DualModeRange = header['DualModeRange'][0]
340 368 self.nDigChannels = header['nDigChannels'][0]
341 self.SampResolution = header['SampResolution'][0]
342 self.nHeights = header['nHeights'][0]
343 self.StartRangeSamp = header['StartRangeSamp'][0]
344 self.PRFhz = header['PRFhz'][0]
345 self.nCohInt = header['nCohInt'][0]
346 self.nProfiles = header['nProfiles'][0]
347 self.nChannels = header['nChannels'][0]
348 self.nIncohInt = header['nIncohInt'][0]
349 self.FFTwindowingInd = header['FFTwindowingInd'][0]
350 self.BeamAngleAzim = header['BeamAngleAzim'][0]
351 self.BeamAngleZen = header['BeamAngleZen'][0]
369 self.SampResolution = header['SampResolution'][0]
370 self.nHeights = header['nHeights'][0]
371 self.StartRangeSamp = header['StartRangeSamp'][0]
372 self.PRFhz = header['PRFhz'][0]
373 self.nCohInt = header['nCohInt'][0]
374 self.nProfiles = header['nProfiles'][0]
375 self.nChannels = header['nChannels'][0]
376 self.nIncohInt = header['nIncohInt'][0]
377 self.FFTwindowingInd = header['FFTwindowingInd'][0]
378 self.BeamAngleAzim = header['BeamAngleAzim'][0]
379 self.BeamAngleZen = header['BeamAngleZen'][0]
352 380 self.AntennaCoord0 = header['AntennaCoord0'][0]
353 381 self.AntennaAngl0 = header['AntennaAngl0'][0]
354 382 self.AntennaCoord1 = header['AntennaCoord1'][0]
355 self.AntennaAngl1 = header['AntennaAngl1'][0]
383 self.AntennaAngl1 = header['AntennaAngl1'][0]
356 384 self.AntennaCoord2 = header['AntennaCoord2'][0]
357 self.AntennaAngl2 = header['AntennaAngl2'][0]
385 self.AntennaAngl2 = header['AntennaAngl2'][0]
358 386 self.RecPhaseCalibr0 = header['RecPhaseCalibr0'][0]
359 387 self.RecPhaseCalibr1 = header['RecPhaseCalibr1'][0]
360 self.RecPhaseCalibr2 = header['RecPhaseCalibr2'][0]
388 self.RecPhaseCalibr2 = header['RecPhaseCalibr2'][0]
361 389 self.RecAmpCalibr0 = header['RecAmpCalibr0'][0]
362 390 self.RecAmpCalibr1 = header['RecAmpCalibr1'][0]
363 self.RecAmpCalibr2 = header['RecAmpCalibr2'][0]
391 self.RecAmpCalibr2 = header['RecAmpCalibr2'][0]
364 392 self.ReceiverGaindB0 = header['ReceiverGaindB0'][0]
365 393 self.ReceiverGaindB1 = header['ReceiverGaindB1'][0]
366 394 self.ReceiverGaindB2 = header['ReceiverGaindB2'][0]
367 395
368 self.ipp= 0.5*(SPEED_OF_LIGHT/self.PRFhz)
369
370 self.RHsize = 180+20*self.nChannels
371 self.Datasize= self.nProfiles*self.nChannels*self.nHeights*2*4
372 #print 'Datasize',self.Datasize
373 endFp = self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
374
396 self.ipp = 0.5 * (SPEED_OF_LIGHT / self.PRFhz)
397
398 self.RHsize = 180 + 20 * self.nChannels
399 self.Datasize = self.nProfiles * self.nChannels * self.nHeights * 2 * 4
400 # print 'Datasize',self.Datasize
401 endFp = self.OffsetStartHeader + self.RecCounter * self.Off2StartNxtRec
402
375 403 print '=============================================='
376 print 'RecMgcNumber ',self.RecMgcNumber
377 print 'RecCounter ',self.RecCounter
378 print 'Off2StartNxtRec ',self.Off2StartNxtRec
379 print 'Off2StartData ',self.Off2StartData
380 print 'Range Resolution ',self.SampResolution
381 print 'First Height ',self.StartRangeSamp
382 print 'PRF (Hz) ',self.PRFhz
383 print 'Heights (K) ',self.nHeights
384 print 'Channels (N) ',self.nChannels
385 print 'Profiles (J) ',self.nProfiles
386 print 'iCoh ',self.nCohInt
387 print 'iInCoh ',self.nIncohInt
388 print 'BeamAngleAzim ',self.BeamAngleAzim
389 print 'BeamAngleZen ',self.BeamAngleZen
390
391 #print 'ModoEnUso ',self.DualModeIndex
392 #print 'UtcTime ',self.nUtime
393 #print 'MiliSec ',self.nMilisec
394 #print 'Exp TagName ',self.ExpTagName
395 #print 'Exp Comment ',self.ExpComment
396 #print 'FFT Window Index ',self.FFTwindowingInd
397 #print 'N Dig. Channels ',self.nDigChannels
398 print 'Size de bloque ',self.RHsize
399 print 'DataSize ',self.Datasize
400 print 'BeamAngleAzim ',self.BeamAngleAzim
401 #print 'AntennaCoord0 ',self.AntennaCoord0
402 #print 'AntennaAngl0 ',self.AntennaAngl0
403 #print 'AntennaCoord1 ',self.AntennaCoord1
404 #print 'AntennaAngl1 ',self.AntennaAngl1
405 #print 'AntennaCoord2 ',self.AntennaCoord2
406 #print 'AntennaAngl2 ',self.AntennaAngl2
407 print 'RecPhaseCalibr0 ',self.RecPhaseCalibr0
408 print 'RecPhaseCalibr1 ',self.RecPhaseCalibr1
409 print 'RecPhaseCalibr2 ',self.RecPhaseCalibr2
410 print 'RecAmpCalibr0 ',self.RecAmpCalibr0
411 print 'RecAmpCalibr1 ',self.RecAmpCalibr1
412 print 'RecAmpCalibr2 ',self.RecAmpCalibr2
413 print 'ReceiverGaindB0 ',self.ReceiverGaindB0
414 print 'ReceiverGaindB1 ',self.ReceiverGaindB1
415 print 'ReceiverGaindB2 ',self.ReceiverGaindB2
404 print 'RecMgcNumber ', self.RecMgcNumber
405 print 'RecCounter ', self.RecCounter
406 print 'Off2StartNxtRec ', self.Off2StartNxtRec
407 print 'Off2StartData ', self.Off2StartData
408 print 'Range Resolution ', self.SampResolution
409 print 'First Height ', self.StartRangeSamp
410 print 'PRF (Hz) ', self.PRFhz
411 print 'Heights (K) ', self.nHeights
412 print 'Channels (N) ', self.nChannels
413 print 'Profiles (J) ', self.nProfiles
414 print 'iCoh ', self.nCohInt
415 print 'iInCoh ', self.nIncohInt
416 print 'BeamAngleAzim ', self.BeamAngleAzim
417 print 'BeamAngleZen ', self.BeamAngleZen
418
419 # print 'ModoEnUso ',self.DualModeIndex
420 # print 'UtcTime ',self.nUtime
421 # print 'MiliSec ',self.nMilisec
422 # print 'Exp TagName ',self.ExpTagName
423 # print 'Exp Comment ',self.ExpComment
424 # print 'FFT Window Index ',self.FFTwindowingInd
425 # print 'N Dig. Channels ',self.nDigChannels
426 print 'Size de bloque ', self.RHsize
427 print 'DataSize ', self.Datasize
428 print 'BeamAngleAzim ', self.BeamAngleAzim
429 # print 'AntennaCoord0 ',self.AntennaCoord0
430 # print 'AntennaAngl0 ',self.AntennaAngl0
431 # print 'AntennaCoord1 ',self.AntennaCoord1
432 # print 'AntennaAngl1 ',self.AntennaAngl1
433 # print 'AntennaCoord2 ',self.AntennaCoord2
434 # print 'AntennaAngl2 ',self.AntennaAngl2
435 print 'RecPhaseCalibr0 ', self.RecPhaseCalibr0
436 print 'RecPhaseCalibr1 ', self.RecPhaseCalibr1
437 print 'RecPhaseCalibr2 ', self.RecPhaseCalibr2
438 print 'RecAmpCalibr0 ', self.RecAmpCalibr0
439 print 'RecAmpCalibr1 ', self.RecAmpCalibr1
440 print 'RecAmpCalibr2 ', self.RecAmpCalibr2
441 print 'ReceiverGaindB0 ', self.ReceiverGaindB0
442 print 'ReceiverGaindB1 ', self.ReceiverGaindB1
443 print 'ReceiverGaindB2 ', self.ReceiverGaindB2
416 444 print '=============================================='
417
445
418 446 if OffRHeader > endFp:
419 sys.stderr.write("Warning %s: Size value read from System Header is lower than it has to be\n" %fp)
447 sys.stderr.write(
448 "Warning %s: Size value read from System Header is lower than it has to be\n" % fp)
420 449 return 0
421
450
422 451 if OffRHeader < endFp:
423 sys.stderr.write("Warning %s: Size value read from System Header size is greater than it has to be\n" %fp)
452 sys.stderr.write(
453 "Warning %s: Size value read from System Header size is greater than it has to be\n" % fp)
424 454 return 0
425
455
426 456 return 1
427 457
428
458
429 459 class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODataReader):
430
460
431 461 path = None
432 462 startDate = None
433 463 endDate = None
434 464 startTime = None
435 465 endTime = None
436 466 walk = None
437 467 isConfig = False
438
439
440 fileList= None
441
442 #metadata
443 TimeZone= None
444 Interval= None
445 heightList= None
446
447 #data
448 data= None
449 utctime= None
450
451
452
468
469 fileList = None
470
471 # metadata
472 TimeZone = None
473 Interval = None
474 heightList = None
475
476 # data
477 data = None
478 utctime = None
479
453 480 def __init__(self, **kwargs):
454
455 #Eliminar de la base la herencia
481
482 # Eliminar de la base la herencia
456 483 ProcessingUnit.__init__(self, **kwargs)
457
484
458 485 #self.isConfig = False
459
486
460 487 #self.pts2read_SelfSpectra = 0
461 488 #self.pts2read_CrossSpectra = 0
462 489 #self.pts2read_DCchannels = 0
463 490 #self.datablock = None
464 491 self.utc = None
465 492 self.ext = ".fdt"
466 493 self.optchar = "P"
467 self.fpFile=None
494 self.fpFile = None
468 495 self.fp = None
469 self.BlockCounter=0
496 self.BlockCounter = 0
470 497 self.dtype = None
471 498 self.fileSizeByHeader = None
472 499 self.filenameList = []
473 500 self.fileSelector = 0
474 self.Off2StartNxtRec=0
475 self.RecCounter=0
501 self.Off2StartNxtRec = 0
502 self.RecCounter = 0
476 503 self.flagNoMoreFiles = 0
477 self.data_spc=None
478 self.data_cspc=None
479 self.data_output=None
504 self.data_spc = None
505 self.data_cspc = None
506 self.data_output = None
480 507 self.path = None
481 self.OffsetStartHeader=0
482 self.Off2StartData=0
508 self.OffsetStartHeader = 0
509 self.Off2StartData = 0
483 510 self.ipp = 0
484 self.nFDTdataRecors=0
511 self.nFDTdataRecors = 0
485 512 self.blocksize = 0
486 513 self.dataOut = Spectra()
487 self.profileIndex = 1 #Always
488 self.dataOut.flagNoData=False
514 self.profileIndex = 1 # Always
515 self.dataOut.flagNoData = False
489 516 self.dataOut.nRdPairs = 0
490 517 self.dataOut.pairsList = []
491 self.dataOut.data_spc=None
492 self.dataOut.noise=[]
493 self.dataOut.velocityX=[]
494 self.dataOut.velocityY=[]
495 self.dataOut.velocityV=[]
496
497
518 self.dataOut.data_spc = None
519 self.dataOut.noise = []
520 self.dataOut.velocityX = []
521 self.dataOut.velocityY = []
522 self.dataOut.velocityV = []
498 523
499 524 def Files2Read(self, fp):
500 525 '''
501 526 Function that indicates the number of .fdt files that exist in the folder to be read.
502 527 It also creates an organized list with the names of the files to read.
503 528 '''
504 #self.__checkPath()
505
506 ListaData=os.listdir(fp) #Gets the list of files within the fp address
507 ListaData=sorted(ListaData) #Sort the list of files from least to largest by names
508 nFiles=0 #File Counter
509 FileList=[] #A list is created that will contain the .fdt files
510 for IndexFile in ListaData :
511 if '.fdt' in IndexFile:
529 # self.__checkPath()
530
531 # Gets the list of files within the fp address
532 ListaData = os.listdir(fp)
533 # Sort the list of files from least to largest by names
534 ListaData = sorted(ListaData)
535 nFiles = 0 # File Counter
536 FileList = [] # A list is created that will contain the .fdt files
537 for IndexFile in ListaData:
538 if '.fdt' in IndexFile:
512 539 FileList.append(IndexFile)
513 nFiles+=1
514
515 #print 'Files2Read'
516 #print 'Existen '+str(nFiles)+' archivos .fdt'
517
518 self.filenameList=FileList #List of files from least to largest by names
519
520
540 nFiles += 1
541
542 # print 'Files2Read'
543 # print 'Existen '+str(nFiles)+' archivos .fdt'
544
545 self.filenameList = FileList # List of files from least to largest by names
546
521 547 def run(self, **kwargs):
522 548 '''
523 549 This method will be the one that will initiate the data entry, will be called constantly.
524 550 You should first verify that your Setup () is set up and then continue to acquire
525 551 the data to be processed with getData ().
526 552 '''
527 553 if not self.isConfig:
528 554 self.setup(**kwargs)
529 555 self.isConfig = True
530
556
531 557 self.getData()
532 #print 'running'
533
534
558 # print 'running'
559
535 560 def setup(self, path=None,
536 startDate=None,
537 endDate=None,
538 startTime=None,
539 endTime=None,
540 walk=True,
541 timezone='utc',
542 code = None,
543 online=False,
544 ReadMode=None,
545 **kwargs):
546
561 startDate=None,
562 endDate=None,
563 startTime=None,
564 endTime=None,
565 walk=True,
566 timezone='utc',
567 code=None,
568 online=False,
569 ReadMode=None,
570 **kwargs):
571
547 572 self.isConfig = True
548
549 self.path=path
550 self.startDate=startDate
551 self.endDate=endDate
552 self.startTime=startTime
553 self.endTime=endTime
554 self.walk=walk
555 self.ReadMode=int(ReadMode)
556
573
574 self.path = path
575 self.startDate = startDate
576 self.endDate = endDate
577 self.startTime = startTime
578 self.endTime = endTime
579 self.walk = walk
580 self.ReadMode = int(ReadMode)
581
557 582 pass
558
559
583
560 584 def getData(self):
561 585 '''
562 586 Before starting this function, you should check that there is still an unread file,
563 587 If there are still blocks to read or if the data block is empty.
564
588
565 589 You should call the file "read".
566
590
567 591 '''
568
592
569 593 if self.flagNoMoreFiles:
570 594 self.dataOut.flagNoData = True
571 595 print 'NoData se vuelve true'
572 596 return 0
573 597
574 self.fp=self.path
598 self.fp = self.path
575 599 self.Files2Read(self.fp)
576 600 self.readFile(self.fp)
577 601 self.dataOut.data_spc = self.data_spc
578 self.dataOut.data_cspc =self.data_cspc
579 self.dataOut.data_output=self.data_output
580
602 self.dataOut.data_cspc = self.data_cspc
603 self.dataOut.data_output = self.data_output
604
581 605 print 'self.dataOut.data_output', shape(self.dataOut.data_output)
582
583 #self.removeDC()
584 return self.dataOut.data_spc
585
586
587 def readFile(self,fp):
606
607 # self.removeDC()
608 return self.dataOut.data_spc
609
610 def readFile(self, fp):
588 611 '''
589 612 You must indicate if you are reading in Online or Offline mode and load the
590 613 The parameters for this file reading mode.
591
614
592 615 Then you must do 2 actions:
593
616
594 617 1. Get the BLTR FileHeader.
595 618 2. Start reading the first block.
596 619 '''
597
598 #The address of the folder is generated the name of the .fdt file that will be read
599 print "File: ",self.fileSelector+1
600
620
621 # The address of the folder is generated the name of the .fdt file that will be read
622 print "File: ", self.fileSelector + 1
623
601 624 if self.fileSelector < len(self.filenameList):
602
603 self.fpFile=str(fp)+'/'+str(self.filenameList[self.fileSelector])
604 #print self.fpFile
625
626 self.fpFile = str(fp) + '/' + \
627 str(self.filenameList[self.fileSelector])
628 # print self.fpFile
605 629 fheader = FileHeaderBLTR()
606 fheader.FHread(self.fpFile) #Bltr FileHeader Reading
607 self.nFDTdataRecors=fheader.nFDTdataRecors
608
609 self.readBlock() #Block reading
630 fheader.FHread(self.fpFile) # Bltr FileHeader Reading
631 self.nFDTdataRecors = fheader.nFDTdataRecors
632
633 self.readBlock() # Block reading
610 634 else:
611 635 print 'readFile FlagNoData becomes true'
612 self.flagNoMoreFiles=True
636 self.flagNoMoreFiles = True
613 637 self.dataOut.flagNoData = True
614 return 0
615
638 return 0
639
616 640 def getVelRange(self, extrapoints=0):
617 Lambda= SPEED_OF_LIGHT/50000000
618 PRF = self.dataOut.PRF#1./(self.dataOut.ippSeconds * self.dataOut.nCohInt)
619 Vmax=-Lambda/(4.*(1./PRF)*self.dataOut.nCohInt*2.)
620 deltafreq = PRF / (self.nProfiles)
621 deltavel = (Vmax*2) / (self.nProfiles)
622 freqrange = deltafreq*(numpy.arange(self.nProfiles)-self.nProfiles/2.) - deltafreq/2
623 velrange = deltavel*(numpy.arange(self.nProfiles)-self.nProfiles/2.)
624 return velrange
625
626 def readBlock(self):
641 Lambda = SPEED_OF_LIGHT / 50000000
642 # 1./(self.dataOut.ippSeconds * self.dataOut.nCohInt)
643 PRF = self.dataOut.PRF
644 Vmax = -Lambda / (4. * (1. / PRF) * self.dataOut.nCohInt * 2.)
645 deltafreq = PRF / (self.nProfiles)
646 deltavel = (Vmax * 2) / (self.nProfiles)
647 freqrange = deltafreq * \
648 (numpy.arange(self.nProfiles) - self.nProfiles / 2.) - deltafreq / 2
649 velrange = deltavel * \
650 (numpy.arange(self.nProfiles) - self.nProfiles / 2.)
651 return velrange
652
653 def readBlock(self):
627 654 '''
628 655 It should be checked if the block has data, if it is not passed to the next file.
629
656
630 657 Then the following is done:
631
658
632 659 1. Read the RecordHeader
633 660 2. Fill the buffer with the current block number.
634
661
635 662 '''
636
637 if self.BlockCounter < self.nFDTdataRecors-2:
663
664 if self.BlockCounter < self.nFDTdataRecors - 2:
638 665 print self.nFDTdataRecors, 'CONDICION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
639 if self.ReadMode==1:
640 rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter+1)
641 elif self.ReadMode==0:
666 if self.ReadMode == 1:
667 rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter + 1)
668 elif self.ReadMode == 0:
642 669 rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter)
643
644 rheader.RHread(self.fpFile) #Bltr FileHeader Reading
645
646 self.OffsetStartHeader=rheader.OffsetStartHeader
647 self.RecCounter=rheader.RecCounter
648 self.Off2StartNxtRec=rheader.Off2StartNxtRec
649 self.Off2StartData=rheader.Off2StartData
650 self.nProfiles=rheader.nProfiles
651 self.nChannels=rheader.nChannels
652 self.nHeights=rheader.nHeights
653 self.frequency=rheader.TransmitFrec
654 self.DualModeIndex=rheader.DualModeIndex
655
656 self.pairsList =[(0,1),(0,2),(1,2)]
670
671 rheader.RHread(self.fpFile) # Bltr FileHeader Reading
672
673 self.OffsetStartHeader = rheader.OffsetStartHeader
674 self.RecCounter = rheader.RecCounter
675 self.Off2StartNxtRec = rheader.Off2StartNxtRec
676 self.Off2StartData = rheader.Off2StartData
677 self.nProfiles = rheader.nProfiles
678 self.nChannels = rheader.nChannels
679 self.nHeights = rheader.nHeights
680 self.frequency = rheader.TransmitFrec
681 self.DualModeIndex = rheader.DualModeIndex
682
683 self.pairsList = [(0, 1), (0, 2), (1, 2)]
657 684 self.dataOut.pairsList = self.pairsList
658
659 self.nRdPairs=len(self.dataOut.pairsList)
685
686 self.nRdPairs = len(self.dataOut.pairsList)
660 687 self.dataOut.nRdPairs = self.nRdPairs
661
662 self.__firstHeigth=rheader.StartRangeSamp
663 self.__deltaHeigth=rheader.SampResolution
664 self.dataOut.heightList= self.__firstHeigth + numpy.array(range(self.nHeights))*self.__deltaHeigth
688
689 self.__firstHeigth = rheader.StartRangeSamp
690 self.__deltaHeigth = rheader.SampResolution
691 self.dataOut.heightList = self.__firstHeigth + \
692 numpy.array(range(self.nHeights)) * self.__deltaHeigth
665 693 self.dataOut.channelList = range(self.nChannels)
666 self.dataOut.nProfiles=rheader.nProfiles
667 self.dataOut.nIncohInt=rheader.nIncohInt
668 self.dataOut.nCohInt=rheader.nCohInt
669 self.dataOut.ippSeconds= 1/float(rheader.PRFhz)
670 self.dataOut.PRF=rheader.PRFhz
671 self.dataOut.nFFTPoints=rheader.nProfiles
672 self.dataOut.utctime=rheader.nUtime
673 self.dataOut.timeZone=0
674 self.dataOut.normFactor= self.dataOut.nProfiles*self.dataOut.nIncohInt*self.dataOut.nCohInt
675 self.dataOut.outputInterval= self.dataOut.ippSeconds * self.dataOut.nCohInt * self.dataOut.nIncohInt * self.nProfiles
676
677 self.data_output=numpy.ones([3,rheader.nHeights])*numpy.NaN
694 self.dataOut.nProfiles = rheader.nProfiles
695 self.dataOut.nIncohInt = rheader.nIncohInt
696 self.dataOut.nCohInt = rheader.nCohInt
697 self.dataOut.ippSeconds = 1 / float(rheader.PRFhz)
698 self.dataOut.PRF = rheader.PRFhz
699 self.dataOut.nFFTPoints = rheader.nProfiles
700 self.dataOut.utctime = rheader.nUtime
701 self.dataOut.timeZone = 0
702 self.dataOut.normFactor = self.dataOut.nProfiles * \
703 self.dataOut.nIncohInt * self.dataOut.nCohInt
704 self.dataOut.outputInterval = self.dataOut.ippSeconds * \
705 self.dataOut.nCohInt * self.dataOut.nIncohInt * self.nProfiles
706
707 self.data_output = numpy.ones([3, rheader.nHeights]) * numpy.NaN
678 708 print 'self.data_output', shape(self.data_output)
679 self.dataOut.velocityX=[]
680 self.dataOut.velocityY=[]
681 self.dataOut.velocityV=[]
682
709 self.dataOut.velocityX = []
710 self.dataOut.velocityY = []
711 self.dataOut.velocityV = []
712
683 713 '''Block Reading, the Block Data is received and Reshape is used to give it
684 714 shape.
685 '''
686
687 #Procedure to take the pointer to where the date block starts
688 startDATA = open(self.fpFile,"rb")
689 OffDATA= self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec+self.Off2StartData
715 '''
716
717 # Procedure to take the pointer to where the date block starts
718 startDATA = open(self.fpFile, "rb")
719 OffDATA = self.OffsetStartHeader + self.RecCounter * \
720 self.Off2StartNxtRec + self.Off2StartData
690 721 startDATA.seek(OffDATA, os.SEEK_SET)
691
722
692 723 def moving_average(x, N=2):
693 return numpy.convolve(x, numpy.ones((N,))/N)[(N-1):]
694
695 def gaus(xSamples,a,x0,sigma):
696 return a*exp(-(xSamples-x0)**2/(2*sigma**2))
697
698 def Find(x,value):
724 return numpy.convolve(x, numpy.ones((N,)) / N)[(N - 1):]
725
726 def gaus(xSamples, a, x0, sigma):
727 return a * exp(-(xSamples - x0)**2 / (2 * sigma**2))
728
729 def Find(x, value):
699 730 for index in range(len(x)):
700 if x[index]==value:
701 return index
702
731 if x[index] == value:
732 return index
733
703 734 def pol2cart(rho, phi):
704 735 x = rho * numpy.cos(phi)
705 736 y = rho * numpy.sin(phi)
706 737 return(x, y)
707
708
709
710
711 if self.DualModeIndex==self.ReadMode:
712
713 self.data_fft = numpy.fromfile( startDATA, [('complex','<c8')],self.nProfiles*self.nChannels*self.nHeights )
714
715 self.data_fft=self.data_fft.astype(numpy.dtype('complex'))
716
717 self.data_block=numpy.reshape(self.data_fft,(self.nHeights, self.nChannels, self.nProfiles ))
718
719 self.data_block = numpy.transpose(self.data_block, (1,2,0))
720
738
739 if self.DualModeIndex == self.ReadMode:
740
741 self.data_fft = numpy.fromfile(
742 startDATA, [('complex', '<c8')], self.nProfiles * self.nChannels * self.nHeights)
743
744 self.data_fft = self.data_fft.astype(numpy.dtype('complex'))
745
746 self.data_block = numpy.reshape(
747 self.data_fft, (self.nHeights, self.nChannels, self.nProfiles))
748
749 self.data_block = numpy.transpose(self.data_block, (1, 2, 0))
750
721 751 copy = self.data_block.copy()
722 spc = copy * numpy.conjugate(copy)
723
724 self.data_spc = numpy.absolute(spc) # valor absoluto o magnitud
725
752 spc = copy * numpy.conjugate(copy)
753
754 self.data_spc = numpy.absolute(
755 spc) # valor absoluto o magnitud
756
726 757 factor = self.dataOut.normFactor
727
728
729 z = self.data_spc.copy()#/factor
758
759 z = self.data_spc.copy() # /factor
730 760 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
731 761 #zdB = 10*numpy.log10(z)
732 762 print ' '
733 763 print 'Z: '
734 print shape(z)
764 print shape(z)
735 765 print ' '
736 766 print ' '
737
738 self.dataOut.data_spc=self.data_spc
739
740 self.noise = self.dataOut.getNoise(ymin_index=80, ymax_index=132)#/factor
767
768 self.dataOut.data_spc = self.data_spc
769
770 self.noise = self.dataOut.getNoise(
771 ymin_index=80, ymax_index=132) # /factor
741 772 #noisedB = 10*numpy.log10(self.noise)
742
743
744 ySamples=numpy.ones([3,self.nProfiles])
745 phase=numpy.ones([3,self.nProfiles])
746 CSPCSamples=numpy.ones([3,self.nProfiles],dtype=numpy.complex_)
747 coherence=numpy.ones([3,self.nProfiles])
748 PhaseSlope=numpy.ones(3)
749 PhaseInter=numpy.ones(3)
750
773
774 ySamples = numpy.ones([3, self.nProfiles])
775 phase = numpy.ones([3, self.nProfiles])
776 CSPCSamples = numpy.ones(
777 [3, self.nProfiles], dtype=numpy.complex_)
778 coherence = numpy.ones([3, self.nProfiles])
779 PhaseSlope = numpy.ones(3)
780 PhaseInter = numpy.ones(3)
781
751 782 '''****** Getting CrossSpectra ******'''
752 cspc=self.data_block.copy()
753 self.data_cspc=self.data_block.copy()
754
755 xFrec=self.getVelRange(1)
756 VelRange=self.getVelRange(1)
757 self.dataOut.VelRange=VelRange
758 #print ' '
759 #print ' '
760 #print 'xFrec',xFrec
761 #print ' '
762 #print ' '
763 #Height=35
764 for i in range(self.nRdPairs):
765
783 cspc = self.data_block.copy()
784 self.data_cspc = self.data_block.copy()
785
786 xFrec = self.getVelRange(1)
787 VelRange = self.getVelRange(1)
788 self.dataOut.VelRange = VelRange
789 # print ' '
790 # print ' '
791 # print 'xFrec',xFrec
792 # print ' '
793 # print ' '
794 # Height=35
795 for i in range(self.nRdPairs):
796
766 797 chan_index0 = self.dataOut.pairsList[i][0]
767 798 chan_index1 = self.dataOut.pairsList[i][1]
768
769 self.data_cspc[i,:,:]=cspc[chan_index0,:,:] * numpy.conjugate(cspc[chan_index1,:,:])
770
771
799
800 self.data_cspc[i, :, :] = cspc[chan_index0, :,
801 :] * numpy.conjugate(cspc[chan_index1, :, :])
802
772 803 '''Getting Eij and Nij'''
773 (AntennaX0,AntennaY0)=pol2cart(rheader.AntennaCoord0, rheader.AntennaAngl0*numpy.pi/180)
774 (AntennaX1,AntennaY1)=pol2cart(rheader.AntennaCoord1, rheader.AntennaAngl1*numpy.pi/180)
775 (AntennaX2,AntennaY2)=pol2cart(rheader.AntennaCoord2, rheader.AntennaAngl2*numpy.pi/180)
776
777 E01=AntennaX0-AntennaX1
778 N01=AntennaY0-AntennaY1
779
780 E02=AntennaX0-AntennaX2
781 N02=AntennaY0-AntennaY2
782
783 E12=AntennaX1-AntennaX2
784 N12=AntennaY1-AntennaY2
785
786 self.ChanDist= numpy.array([[E01, N01],[E02,N02],[E12,N12]])
787
804 (AntennaX0, AntennaY0) = pol2cart(
805 rheader.AntennaCoord0, rheader.AntennaAngl0 * numpy.pi / 180)
806 (AntennaX1, AntennaY1) = pol2cart(
807 rheader.AntennaCoord1, rheader.AntennaAngl1 * numpy.pi / 180)
808 (AntennaX2, AntennaY2) = pol2cart(
809 rheader.AntennaCoord2, rheader.AntennaAngl2 * numpy.pi / 180)
810
811 E01 = AntennaX0 - AntennaX1
812 N01 = AntennaY0 - AntennaY1
813
814 E02 = AntennaX0 - AntennaX2
815 N02 = AntennaY0 - AntennaY2
816
817 E12 = AntennaX1 - AntennaX2
818 N12 = AntennaY1 - AntennaY2
819
820 self.ChanDist = numpy.array(
821 [[E01, N01], [E02, N02], [E12, N12]])
822
788 823 self.dataOut.ChanDist = self.ChanDist
789
790
824
825
791 826 # for Height in range(self.nHeights):
792 #
827 #
793 828 # for i in range(self.nRdPairs):
794 #
829 #
795 830 # '''****** Line of Data SPC ******'''
796 831 # zline=z[i,:,Height]
797 #
832 #
798 833 # '''****** DC is removed ******'''
799 834 # DC=Find(zline,numpy.amax(zline))
800 835 # zline[DC]=(zline[DC-1]+zline[DC+1])/2
801 #
802 #
836 #
837 #
803 838 # '''****** SPC is normalized ******'''
804 839 # FactNorm= zline.copy() / numpy.sum(zline.copy())
805 840 # FactNorm= FactNorm/numpy.sum(FactNorm)
806 #
841 #
807 842 # SmoothSPC=moving_average(FactNorm,N=3)
808 #
843 #
809 844 # xSamples = ar(range(len(SmoothSPC)))
810 845 # ySamples[i] = SmoothSPC-self.noise[i]
811 #
846 #
812 847 # for i in range(self.nRdPairs):
813 #
848 #
814 849 # '''****** Line of Data CSPC ******'''
815 850 # cspcLine=self.data_cspc[i,:,Height].copy()
816 #
817 #
818 #
851 #
852 #
853 #
819 854 # '''****** CSPC is normalized ******'''
820 855 # chan_index0 = self.dataOut.pairsList[i][0]
821 # chan_index1 = self.dataOut.pairsList[i][1]
856 # chan_index1 = self.dataOut.pairsList[i][1]
822 857 # CSPCFactor= numpy.sum(ySamples[chan_index0]) * numpy.sum(ySamples[chan_index1])
823 #
824 #
858 #
859 #
825 860 # CSPCNorm= cspcLine.copy() / numpy.sqrt(CSPCFactor)
826 #
827 #
861 #
862 #
828 863 # CSPCSamples[i] = CSPCNorm-self.noise[i]
829 864 # coherence[i] = numpy.abs(CSPCSamples[i]) / numpy.sqrt(CSPCFactor)
830 #
865 #
831 866 # '''****** DC is removed ******'''
832 867 # DC=Find(coherence[i],numpy.amax(coherence[i]))
833 868 # coherence[i][DC]=(coherence[i][DC-1]+coherence[i][DC+1])/2
834 869 # coherence[i]= moving_average(coherence[i],N=2)
835 #
870 #
836 871 # phase[i] = moving_average( numpy.arctan2(CSPCSamples[i].imag, CSPCSamples[i].real),N=1)#*180/numpy.pi
837 #
838 #
872 #
873 #
839 874 # '''****** Getting fij width ******'''
840 #
875 #
841 876 # yMean=[]
842 # yMean2=[]
843 #
877 # yMean2=[]
878 #
844 879 # for j in range(len(ySamples[1])):
845 880 # yMean=numpy.append(yMean,numpy.average([ySamples[0,j],ySamples[1,j],ySamples[2,j]]))
846 #
881 #
847 882 # '''******* Getting fitting Gaussian ******'''
848 883 # meanGauss=sum(xSamples*yMean) / len(xSamples)
849 884 # sigma=sum(yMean*(xSamples-meanGauss)**2) / len(xSamples)
850 885 # #print 'Height',Height,'SNR', meanGauss/sigma**2
851 #
886 #
852 887 # if (abs(meanGauss/sigma**2) > 0.0001) :
853 #
854 # try:
888 #
889 # try:
855 890 # popt,pcov = curve_fit(gaus,xSamples,yMean,p0=[1,meanGauss,sigma])
856 #
891 #
857 892 # if numpy.amax(popt)>numpy.amax(yMean)*0.3:
858 893 # FitGauss=gaus(xSamples,*popt)
859 #
860 # else:
894 #
895 # else:
861 896 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
862 897 # print 'Verificador: Dentro', Height
863 898 # except RuntimeError:
864 #
899 #
865 900 # try:
866 901 # for j in range(len(ySamples[1])):
867 902 # yMean2=numpy.append(yMean2,numpy.average([ySamples[1,j],ySamples[2,j]]))
868 903 # popt,pcov = curve_fit(gaus,xSamples,yMean2,p0=[1,meanGauss,sigma])
869 904 # FitGauss=gaus(xSamples,*popt)
870 905 # print 'Verificador: Exepcion1', Height
871 906 # except RuntimeError:
872 #
907 #
873 908 # try:
874 909 # popt,pcov = curve_fit(gaus,xSamples,ySamples[1],p0=[1,meanGauss,sigma])
875 910 # FitGauss=gaus(xSamples,*popt)
876 911 # print 'Verificador: Exepcion2', Height
877 912 # except RuntimeError:
878 913 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
879 914 # print 'Verificador: Exepcion3', Height
880 915 # else:
881 916 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
882 917 # #print 'Verificador: Fuera', Height
883 #
884 #
885 #
918 #
919 #
920 #
886 921 # Maximun=numpy.amax(yMean)
887 922 # eMinus1=Maximun*numpy.exp(-1)
888 #
923 #
889 924 # HWpos=Find(FitGauss,min(FitGauss, key=lambda value:abs(value-eMinus1)))
890 925 # HalfWidth= xFrec[HWpos]
891 926 # GCpos=Find(FitGauss, numpy.amax(FitGauss))
892 927 # Vpos=Find(FactNorm, numpy.amax(FactNorm))
893 928 # #Vpos=numpy.sum(FactNorm)/len(FactNorm)
894 929 # #Vpos=Find(FactNorm, min(FactNorm, key=lambda value:abs(value- numpy.mean(FactNorm) )))
895 930 # #print 'GCpos',GCpos, numpy.amax(FitGauss), 'HWpos',HWpos
896 931 # '''****** Getting Fij ******'''
897 #
932 #
898 933 # GaussCenter=xFrec[GCpos]
899 934 # if (GaussCenter<0 and HalfWidth>0) or (GaussCenter>0 and HalfWidth<0):
900 935 # Fij=abs(GaussCenter)+abs(HalfWidth)+0.0000001
901 936 # else:
902 937 # Fij=abs(GaussCenter-HalfWidth)+0.0000001
903 #
938 #
904 939 # '''****** Getting Frecuency range of significant data ******'''
905 #
940 #
906 941 # Rangpos=Find(FitGauss,min(FitGauss, key=lambda value:abs(value-Maximun*0.10)))
907 #
942 #
908 943 # if Rangpos<GCpos:
909 944 # Range=numpy.array([Rangpos,2*GCpos-Rangpos])
910 945 # else:
911 946 # Range=numpy.array([2*GCpos-Rangpos,Rangpos])
912 #
947 #
913 948 # FrecRange=xFrec[Range[0]:Range[1]]
914 #
949 #
915 950 # #print 'FrecRange', FrecRange
916 951 # '''****** Getting SCPC Slope ******'''
917 #
952 #
918 953 # for i in range(self.nRdPairs):
919 #
954 #
920 955 # if len(FrecRange)>5 and len(FrecRange)<self.nProfiles*0.5:
921 # PhaseRange=moving_average(phase[i,Range[0]:Range[1]],N=3)
922 #
956 # PhaseRange=moving_average(phase[i,Range[0]:Range[1]],N=3)
957 #
923 958 # slope, intercept, r_value, p_value, std_err = stats.linregress(FrecRange,PhaseRange)
924 959 # PhaseSlope[i]=slope
925 960 # PhaseInter[i]=intercept
926 961 # else:
927 962 # PhaseSlope[i]=0
928 963 # PhaseInter[i]=0
929 #
964 #
930 965 # # plt.figure(i+15)
931 966 # # plt.title('FASE ( CH%s*CH%s )' %(self.dataOut.pairsList[i][0],self.dataOut.pairsList[i][1]))
932 967 # # plt.xlabel('Frecuencia (KHz)')
933 968 # # plt.ylabel('Magnitud')
934 969 # # #plt.subplot(311+i)
935 970 # # plt.plot(FrecRange,PhaseRange,'b')
936 971 # # plt.plot(FrecRange,FrecRange*PhaseSlope[i]+PhaseInter[i],'r')
937 #
972 #
938 973 # #plt.axis([-0.6, 0.2, -3.2, 3.2])
939 #
940 #
974 #
975 #
941 976 # '''Getting constant C'''
942 977 # cC=(Fij*numpy.pi)**2
943 #
978 #
944 979 # # '''Getting Eij and Nij'''
945 980 # # (AntennaX0,AntennaY0)=pol2cart(rheader.AntennaCoord0, rheader.AntennaAngl0*numpy.pi/180)
946 981 # # (AntennaX1,AntennaY1)=pol2cart(rheader.AntennaCoord1, rheader.AntennaAngl1*numpy.pi/180)
947 982 # # (AntennaX2,AntennaY2)=pol2cart(rheader.AntennaCoord2, rheader.AntennaAngl2*numpy.pi/180)
948 # #
983 # #
949 984 # # E01=AntennaX0-AntennaX1
950 985 # # N01=AntennaY0-AntennaY1
951 # #
986 # #
952 987 # # E02=AntennaX0-AntennaX2
953 988 # # N02=AntennaY0-AntennaY2
954 # #
989 # #
955 990 # # E12=AntennaX1-AntennaX2
956 991 # # N12=AntennaY1-AntennaY2
957 #
992 #
958 993 # '''****** Getting constants F and G ******'''
959 994 # MijEijNij=numpy.array([[E02,N02], [E12,N12]])
960 995 # MijResult0=(-PhaseSlope[1]*cC) / (2*numpy.pi)
961 # MijResult1=(-PhaseSlope[2]*cC) / (2*numpy.pi)
996 # MijResult1=(-PhaseSlope[2]*cC) / (2*numpy.pi)
962 997 # MijResults=numpy.array([MijResult0,MijResult1])
963 998 # (cF,cG) = numpy.linalg.solve(MijEijNij, MijResults)
964 #
999 #
965 1000 # '''****** Getting constants A, B and H ******'''
966 1001 # W01=numpy.amax(coherence[0])
967 1002 # W02=numpy.amax(coherence[1])
968 1003 # W12=numpy.amax(coherence[2])
969 #
1004 #
970 1005 # WijResult0=((cF*E01+cG*N01)**2)/cC - numpy.log(W01 / numpy.sqrt(numpy.pi/cC))
971 1006 # WijResult1=((cF*E02+cG*N02)**2)/cC - numpy.log(W02 / numpy.sqrt(numpy.pi/cC))
972 1007 # WijResult2=((cF*E12+cG*N12)**2)/cC - numpy.log(W12 / numpy.sqrt(numpy.pi/cC))
973 #
1008 #
974 1009 # WijResults=numpy.array([WijResult0, WijResult1, WijResult2])
975 #
976 # WijEijNij=numpy.array([ [E01**2, N01**2, 2*E01*N01] , [E02**2, N02**2, 2*E02*N02] , [E12**2, N12**2, 2*E12*N12] ])
1010 #
1011 # WijEijNij=numpy.array([ [E01**2, N01**2, 2*E01*N01] , [E02**2, N02**2, 2*E02*N02] , [E12**2, N12**2, 2*E12*N12] ])
977 1012 # (cA,cB,cH) = numpy.linalg.solve(WijEijNij, WijResults)
978 #
1013 #
979 1014 # VxVy=numpy.array([[cA,cH],[cH,cB]])
980 #
1015 #
981 1016 # VxVyResults=numpy.array([-cF,-cG])
982 1017 # (Vx,Vy) = numpy.linalg.solve(VxVy, VxVyResults)
983 1018 # Vzon = Vy
984 1019 # Vmer = Vx
985 1020 # Vmag=numpy.sqrt(Vzon**2+Vmer**2)
986 1021 # Vang=numpy.arctan2(Vmer,Vzon)
987 #
1022 #
988 1023 # if abs(Vy)<100 and abs(Vy)> 0.:
989 1024 # self.dataOut.velocityX=numpy.append(self.dataOut.velocityX, Vzon) #Vmag
990 1025 # #print 'Vmag',Vmag
991 1026 # else:
992 1027 # self.dataOut.velocityX=numpy.append(self.dataOut.velocityX, NaN)
993 #
1028 #
994 1029 # if abs(Vx)<100 and abs(Vx) > 0.:
995 1030 # self.dataOut.velocityY=numpy.append(self.dataOut.velocityY, Vmer) #Vang
996 # #print 'Vang',Vang
1031 # #print 'Vang',Vang
997 1032 # else:
998 1033 # self.dataOut.velocityY=numpy.append(self.dataOut.velocityY, NaN)
999 #
1034 #
1000 1035 # if abs(GaussCenter)<2:
1001 1036 # self.dataOut.velocityV=numpy.append(self.dataOut.velocityV, xFrec[Vpos])
1002 #
1037 #
1003 1038 # else:
1004 1039 # self.dataOut.velocityV=numpy.append(self.dataOut.velocityV, NaN)
1005 #
1006 #
1040 #
1041 #
1007 1042 # # print '********************************************'
1008 1043 # # print 'HalfWidth ', HalfWidth
1009 1044 # # print 'Maximun ', Maximun
1010 1045 # # print 'eMinus1 ', eMinus1
1011 1046 # # print 'Rangpos ', Rangpos
1012 1047 # # print 'GaussCenter ',GaussCenter
1013 1048 # # print 'E01 ',E01
1014 1049 # # print 'N01 ',N01
1015 1050 # # print 'E02 ',E02
1016 1051 # # print 'N02 ',N02
1017 1052 # # print 'E12 ',E12
1018 1053 # # print 'N12 ',N12
1019 1054 # #print 'self.dataOut.velocityX ', self.dataOut.velocityX
1020 1055 # # print 'Fij ', Fij
1021 1056 # # print 'cC ', cC
1022 1057 # # print 'cF ', cF
1023 1058 # # print 'cG ', cG
1024 1059 # # print 'cA ', cA
1025 1060 # # print 'cB ', cB
1026 1061 # # print 'cH ', cH
1027 1062 # # print 'Vx ', Vx
1028 1063 # # print 'Vy ', Vy
1029 1064 # # print 'Vmag ', Vmag
1030 1065 # # print 'Vang ', Vang*180/numpy.pi
1031 1066 # # print 'PhaseSlope ',PhaseSlope[0]
1032 1067 # # print 'PhaseSlope ',PhaseSlope[1]
1033 1068 # # print 'PhaseSlope ',PhaseSlope[2]
1034 1069 # # print '********************************************'
1035 1070 # #print 'data_output',shape(self.dataOut.velocityX), shape(self.dataOut.velocityY)
1036 #
1071 #
1037 1072 # #print 'self.dataOut.velocityX', len(self.dataOut.velocityX)
1038 1073 # #print 'self.dataOut.velocityY', len(self.dataOut.velocityY)
1039 1074 # #print 'self.dataOut.velocityV', self.dataOut.velocityV
1040 #
1075 #
1041 1076 # self.data_output[0]=numpy.array(self.dataOut.velocityX)
1042 1077 # self.data_output[1]=numpy.array(self.dataOut.velocityY)
1043 1078 # self.data_output[2]=numpy.array(self.dataOut.velocityV)
1044 #
1079 #
1045 1080 # prin= self.data_output[0][~numpy.isnan(self.data_output[0])]
1046 1081 # print ' '
1047 # print 'VmagAverage',numpy.mean(prin)
1082 # print 'VmagAverage',numpy.mean(prin)
1048 1083 # print ' '
1049 1084 # # plt.figure(5)
1050 1085 # # plt.subplot(211)
1051 1086 # # plt.plot(self.dataOut.velocityX,'yo:')
1052 1087 # # plt.subplot(212)
1053 1088 # # plt.plot(self.dataOut.velocityY,'yo:')
1054 #
1089 #
1055 1090 # # plt.figure(1)
1056 1091 # # # plt.subplot(121)
1057 1092 # # # plt.plot(xFrec,ySamples[0],'k',label='Ch0')
1058 1093 # # # plt.plot(xFrec,ySamples[1],'g',label='Ch1')
1059 1094 # # # plt.plot(xFrec,ySamples[2],'r',label='Ch2')
1060 1095 # # # plt.plot(xFrec,FitGauss,'yo:',label='fit')
1061 1096 # # # plt.legend()
1062 1097 # # plt.title('DATOS A ALTURA DE 2850 METROS')
1063 # #
1098 # #
1064 1099 # # plt.xlabel('Frecuencia (KHz)')
1065 1100 # # plt.ylabel('Magnitud')
1066 1101 # # # plt.subplot(122)
1067 1102 # # # plt.title('Fit for Time Constant')
1068 1103 # # #plt.plot(xFrec,zline)
1069 1104 # # #plt.plot(xFrec,SmoothSPC,'g')
1070 1105 # # plt.plot(xFrec,FactNorm)
1071 1106 # # plt.axis([-4, 4, 0, 0.15])
1072 1107 # # # plt.xlabel('SelfSpectra KHz')
1073 # #
1108 # #
1074 1109 # # plt.figure(10)
1075 1110 # # # plt.subplot(121)
1076 1111 # # plt.plot(xFrec,ySamples[0],'b',label='Ch0')
1077 1112 # # plt.plot(xFrec,ySamples[1],'y',label='Ch1')
1078 1113 # # plt.plot(xFrec,ySamples[2],'r',label='Ch2')
1079 1114 # # # plt.plot(xFrec,FitGauss,'yo:',label='fit')
1080 1115 # # plt.legend()
1081 1116 # # plt.title('SELFSPECTRA EN CANALES')
1082 # #
1117 # #
1083 1118 # # plt.xlabel('Frecuencia (KHz)')
1084 1119 # # plt.ylabel('Magnitud')
1085 1120 # # # plt.subplot(122)
1086 1121 # # # plt.title('Fit for Time Constant')
1087 1122 # # #plt.plot(xFrec,zline)
1088 1123 # # #plt.plot(xFrec,SmoothSPC,'g')
1089 1124 # # # plt.plot(xFrec,FactNorm)
1090 1125 # # # plt.axis([-4, 4, 0, 0.15])
1091 1126 # # # plt.xlabel('SelfSpectra KHz')
1092 # #
1127 # #
1093 1128 # # plt.figure(9)
1094 # #
1095 # #
1129 # #
1130 # #
1096 1131 # # plt.title('DATOS SUAVIZADOS')
1097 1132 # # plt.xlabel('Frecuencia (KHz)')
1098 1133 # # plt.ylabel('Magnitud')
1099 1134 # # plt.plot(xFrec,SmoothSPC,'g')
1100 # #
1135 # #
1101 1136 # # #plt.plot(xFrec,FactNorm)
1102 1137 # # plt.axis([-4, 4, 0, 0.15])
1103 1138 # # # plt.xlabel('SelfSpectra KHz')
1104 # # #
1139 # # #
1105 1140 # # plt.figure(2)
1106 1141 # # # #plt.subplot(121)
1107 1142 # # plt.plot(xFrec,yMean,'r',label='Mean SelfSpectra')
1108 1143 # # plt.plot(xFrec,FitGauss,'yo:',label='Ajuste Gaussiano')
1109 1144 # # # plt.plot(xFrec[Rangpos],FitGauss[Find(FitGauss,min(FitGauss, key=lambda value:abs(value-Maximun*0.1)))],'bo')
1110 1145 # # # #plt.plot(xFrec,phase)
1111 1146 # # # plt.xlabel('Suavizado, promediado KHz')
1112 1147 # # plt.title('SELFSPECTRA PROMEDIADO')
1113 1148 # # # #plt.subplot(122)
1114 1149 # # # #plt.plot(xSamples,zline)
1115 1150 # # plt.xlabel('Frecuencia (KHz)')
1116 1151 # # plt.ylabel('Magnitud')
1117 1152 # # plt.legend()
1118 # # #
1153 # # #
1119 1154 # # # plt.figure(3)
1120 1155 # # # plt.subplot(311)
1121 1156 # # # #plt.plot(xFrec,phase[0])
1122 1157 # # # plt.plot(xFrec,phase[0],'g')
1123 1158 # # # plt.subplot(312)
1124 1159 # # # plt.plot(xFrec,phase[1],'g')
1125 1160 # # # plt.subplot(313)
1126 1161 # # # plt.plot(xFrec,phase[2],'g')
1127 1162 # # # #plt.plot(xFrec,phase[2])
1128 # # #
1163 # # #
1129 1164 # # # plt.figure(4)
1130 # # #
1165 # # #
1131 1166 # # # plt.plot(xSamples,coherence[0],'b')
1132 1167 # # # plt.plot(xSamples,coherence[1],'r')
1133 1168 # # # plt.plot(xSamples,coherence[2],'g')
1134 1169 # # plt.show()
1135 # # #
1170 # # #
1136 1171 # # # plt.clf()
1137 1172 # # # plt.cla()
1138 # # # plt.close()
1139 #
1140 # print ' '
1141
1142
1143
1144 self.BlockCounter+=2
1145
1173 # # # plt.close()
1174 #
1175 # print ' '
1176
1177 self.BlockCounter += 2
1178
1146 1179 else:
1147 self.fileSelector+=1
1148 self.BlockCounter=0
1180 self.fileSelector += 1
1181 self.BlockCounter = 0
1149 1182 print "Next File"
1150
1151
1152
1153
1154
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now