##// END OF EJS Templates
añadido FaradayIntegration para limpieza de datos, restringida la operación al funcionamiento con la pdata regular
joabAM -
r1399:101813202a42
parent child
Show More
@@ -1,1576 +1,1577
1 """
1 """
2 Created on Jul 2, 2014
2 Created on Jul 2, 2014
3
3
4 @author: roj-idl71
4 @author: roj-idl71
5 """
5 """
6 import os
6 import os
7 import sys
7 import sys
8 import glob
8 import glob
9 import time
9 import time
10 import numpy
10 import numpy
11 import fnmatch
11 import fnmatch
12 import inspect
12 import inspect
13 import time
13 import time
14 import datetime
14 import datetime
15 import zmq
15 import zmq
16
16
17 from schainpy.model.proc.jroproc_base import Operation, MPDecorator
17 from schainpy.model.proc.jroproc_base import Operation, MPDecorator
18 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
18 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
19 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
19 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
20 from schainpy.utils import log
20 from schainpy.utils import log
21 import schainpy.admin
21 import schainpy.admin
22
22
23 LOCALTIME = True
23 LOCALTIME = True
24 DT_DIRECTIVES = {
24 DT_DIRECTIVES = {
25 '%Y': 4,
25 '%Y': 4,
26 '%y': 2,
26 '%y': 2,
27 '%m': 2,
27 '%m': 2,
28 '%d': 2,
28 '%d': 2,
29 '%j': 3,
29 '%j': 3,
30 '%H': 2,
30 '%H': 2,
31 '%M': 2,
31 '%M': 2,
32 '%S': 2,
32 '%S': 2,
33 '%f': 6
33 '%f': 6
34 }
34 }
35
35
36
36
37 def isNumber(cad):
37 def isNumber(cad):
38 """
38 """
39 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
39 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
40
40
41 Excepciones:
41 Excepciones:
42 Si un determinado string no puede ser convertido a numero
42 Si un determinado string no puede ser convertido a numero
43 Input:
43 Input:
44 str, string al cual se le analiza para determinar si convertible a un numero o no
44 str, string al cual se le analiza para determinar si convertible a un numero o no
45
45
46 Return:
46 Return:
47 True : si el string es uno numerico
47 True : si el string es uno numerico
48 False : no es un string numerico
48 False : no es un string numerico
49 """
49 """
50 try:
50 try:
51 float(cad)
51 float(cad)
52 return True
52 return True
53 except:
53 except:
54 return False
54 return False
55
55
56
56
57 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
57 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
58 """
58 """
59 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
59 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
60
60
61 Inputs:
61 Inputs:
62 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
62 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
63
63
64 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
64 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
65 segundos contados desde 01/01/1970.
65 segundos contados desde 01/01/1970.
66 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
66 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
67 segundos contados desde 01/01/1970.
67 segundos contados desde 01/01/1970.
68
68
69 Return:
69 Return:
70 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
70 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
71 fecha especificado, de lo contrario retorna False.
71 fecha especificado, de lo contrario retorna False.
72
72
73 Excepciones:
73 Excepciones:
74 Si el archivo no existe o no puede ser abierto
74 Si el archivo no existe o no puede ser abierto
75 Si la cabecera no puede ser leida.
75 Si la cabecera no puede ser leida.
76
76
77 """
77 """
78 basicHeaderObj = BasicHeader(LOCALTIME)
78 basicHeaderObj = BasicHeader(LOCALTIME)
79
79
80 try:
80 try:
81 fp = open(filename, 'rb')
81 fp = open(filename, 'rb')
82 except IOError:
82 except IOError:
83 print("The file %s can't be opened" % (filename))
83 print("The file %s can't be opened" % (filename))
84 return 0
84 return 0
85
85
86 sts = basicHeaderObj.read(fp)
86 sts = basicHeaderObj.read(fp)
87 fp.close()
87 fp.close()
88
88
89 if not(sts):
89 if not(sts):
90 print("Skipping the file %s because it has not a valid header" % (filename))
90 print("Skipping the file %s because it has not a valid header" % (filename))
91 return 0
91 return 0
92
92
93 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
93 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
94 return 0
94 return 0
95
95
96 return 1
96 return 1
97
97
98
98
99 def isTimeInRange(thisTime, startTime, endTime):
99 def isTimeInRange(thisTime, startTime, endTime):
100 if endTime >= startTime:
100 if endTime >= startTime:
101 if (thisTime < startTime) or (thisTime > endTime):
101 if (thisTime < startTime) or (thisTime > endTime):
102 return 0
102 return 0
103 return 1
103 return 1
104 else:
104 else:
105 if (thisTime < startTime) and (thisTime > endTime):
105 if (thisTime < startTime) and (thisTime > endTime):
106 return 0
106 return 0
107 return 1
107 return 1
108
108
109
109
110 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
110 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
111 """
111 """
112 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
112 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
113
113
114 Inputs:
114 Inputs:
115 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
115 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
116
116
117 startDate : fecha inicial del rango seleccionado en formato datetime.date
117 startDate : fecha inicial del rango seleccionado en formato datetime.date
118
118
119 endDate : fecha final del rango seleccionado en formato datetime.date
119 endDate : fecha final del rango seleccionado en formato datetime.date
120
120
121 startTime : tiempo inicial del rango seleccionado en formato datetime.time
121 startTime : tiempo inicial del rango seleccionado en formato datetime.time
122
122
123 endTime : tiempo final del rango seleccionado en formato datetime.time
123 endTime : tiempo final del rango seleccionado en formato datetime.time
124
124
125 Return:
125 Return:
126 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
126 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
127 fecha especificado, de lo contrario retorna False.
127 fecha especificado, de lo contrario retorna False.
128
128
129 Excepciones:
129 Excepciones:
130 Si el archivo no existe o no puede ser abierto
130 Si el archivo no existe o no puede ser abierto
131 Si la cabecera no puede ser leida.
131 Si la cabecera no puede ser leida.
132
132
133 """
133 """
134
134
135 try:
135 try:
136 fp = open(filename, 'rb')
136 fp = open(filename, 'rb')
137 except IOError:
137 except IOError:
138 print("The file %s can't be opened" % (filename))
138 print("The file %s can't be opened" % (filename))
139 return None
139 return None
140
140
141 firstBasicHeaderObj = BasicHeader(LOCALTIME)
141 firstBasicHeaderObj = BasicHeader(LOCALTIME)
142 systemHeaderObj = SystemHeader()
142 systemHeaderObj = SystemHeader()
143 radarControllerHeaderObj = RadarControllerHeader()
143 radarControllerHeaderObj = RadarControllerHeader()
144 processingHeaderObj = ProcessingHeader()
144 processingHeaderObj = ProcessingHeader()
145
145
146 lastBasicHeaderObj = BasicHeader(LOCALTIME)
146 lastBasicHeaderObj = BasicHeader(LOCALTIME)
147
147
148 sts = firstBasicHeaderObj.read(fp)
148 sts = firstBasicHeaderObj.read(fp)
149
149
150 if not(sts):
150 if not(sts):
151 print("[Reading] Skipping the file %s because it has not a valid header" % (filename))
151 print("[Reading] Skipping the file %s because it has not a valid header" % (filename))
152 return None
152 return None
153
153
154 if not systemHeaderObj.read(fp):
154 if not systemHeaderObj.read(fp):
155 return None
155 return None
156
156
157 if not radarControllerHeaderObj.read(fp):
157 if not radarControllerHeaderObj.read(fp):
158 return None
158 return None
159
159
160 if not processingHeaderObj.read(fp):
160 if not processingHeaderObj.read(fp):
161 return None
161 return None
162
162
163 filesize = os.path.getsize(filename)
163 filesize = os.path.getsize(filename)
164
164
165 offset = processingHeaderObj.blockSize + 24 # header size
165 offset = processingHeaderObj.blockSize + 24 # header size
166
166
167 if filesize <= offset:
167 if filesize <= offset:
168 print("[Reading] %s: This file has not enough data" % filename)
168 print("[Reading] %s: This file has not enough data" % filename)
169 return None
169 return None
170
170
171 fp.seek(-offset, 2)
171 fp.seek(-offset, 2)
172
172
173 sts = lastBasicHeaderObj.read(fp)
173 sts = lastBasicHeaderObj.read(fp)
174
174
175 fp.close()
175 fp.close()
176
176
177 thisDatetime = lastBasicHeaderObj.datatime
177 thisDatetime = lastBasicHeaderObj.datatime
178 thisTime_last_block = thisDatetime.time()
178 thisTime_last_block = thisDatetime.time()
179
179
180 thisDatetime = firstBasicHeaderObj.datatime
180 thisDatetime = firstBasicHeaderObj.datatime
181 thisDate = thisDatetime.date()
181 thisDate = thisDatetime.date()
182 thisTime_first_block = thisDatetime.time()
182 thisTime_first_block = thisDatetime.time()
183
183
184 # General case
184 # General case
185 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
185 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
186 #-----------o----------------------------o-----------
186 #-----------o----------------------------o-----------
187 # startTime endTime
187 # startTime endTime
188
188
189 if endTime >= startTime:
189 if endTime >= startTime:
190 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
190 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
191 return None
191 return None
192
192
193 return thisDatetime
193 return thisDatetime
194
194
195 # If endTime < startTime then endTime belongs to the next day
195 # If endTime < startTime then endTime belongs to the next day
196
196
197 #<<<<<<<<<<<o o>>>>>>>>>>>
197 #<<<<<<<<<<<o o>>>>>>>>>>>
198 #-----------o----------------------------o-----------
198 #-----------o----------------------------o-----------
199 # endTime startTime
199 # endTime startTime
200
200
201 if (thisDate == startDate) and (thisTime_last_block < startTime):
201 if (thisDate == startDate) and (thisTime_last_block < startTime):
202 return None
202 return None
203
203
204 if (thisDate == endDate) and (thisTime_first_block > endTime):
204 if (thisDate == endDate) and (thisTime_first_block > endTime):
205 return None
205 return None
206
206
207 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
207 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
208 return None
208 return None
209
209
210 return thisDatetime
210 return thisDatetime
211
211
212
212
213 def isFolderInDateRange(folder, startDate=None, endDate=None):
213 def isFolderInDateRange(folder, startDate=None, endDate=None):
214 """
214 """
215 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
215 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
216
216
217 Inputs:
217 Inputs:
218 folder : nombre completo del directorio.
218 folder : nombre completo del directorio.
219 Su formato deberia ser "/path_root/?YYYYDDD"
219 Su formato deberia ser "/path_root/?YYYYDDD"
220
220
221 siendo:
221 siendo:
222 YYYY : Anio (ejemplo 2015)
222 YYYY : Anio (ejemplo 2015)
223 DDD : Dia del anio (ejemplo 305)
223 DDD : Dia del anio (ejemplo 305)
224
224
225 startDate : fecha inicial del rango seleccionado en formato datetime.date
225 startDate : fecha inicial del rango seleccionado en formato datetime.date
226
226
227 endDate : fecha final del rango seleccionado en formato datetime.date
227 endDate : fecha final del rango seleccionado en formato datetime.date
228
228
229 Return:
229 Return:
230 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
230 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
231 fecha especificado, de lo contrario retorna False.
231 fecha especificado, de lo contrario retorna False.
232 Excepciones:
232 Excepciones:
233 Si el directorio no tiene el formato adecuado
233 Si el directorio no tiene el formato adecuado
234 """
234 """
235
235
236 basename = os.path.basename(folder)
236 basename = os.path.basename(folder)
237
237
238 if not isRadarFolder(basename):
238 if not isRadarFolder(basename):
239 print("The folder %s has not the rigth format" % folder)
239 print("The folder %s has not the rigth format" % folder)
240 return 0
240 return 0
241
241
242 if startDate and endDate:
242 if startDate and endDate:
243 thisDate = getDateFromRadarFolder(basename)
243 thisDate = getDateFromRadarFolder(basename)
244
244
245 if thisDate < startDate:
245 if thisDate < startDate:
246 return 0
246 return 0
247
247
248 if thisDate > endDate:
248 if thisDate > endDate:
249 return 0
249 return 0
250
250
251 return 1
251 return 1
252
252
253
253
254 def isFileInDateRange(filename, startDate=None, endDate=None):
254 def isFileInDateRange(filename, startDate=None, endDate=None):
255 """
255 """
256 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
256 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
257
257
258 Inputs:
258 Inputs:
259 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
259 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
260
260
261 Su formato deberia ser "?YYYYDDDsss"
261 Su formato deberia ser "?YYYYDDDsss"
262
262
263 siendo:
263 siendo:
264 YYYY : Anio (ejemplo 2015)
264 YYYY : Anio (ejemplo 2015)
265 DDD : Dia del anio (ejemplo 305)
265 DDD : Dia del anio (ejemplo 305)
266 sss : set
266 sss : set
267
267
268 startDate : fecha inicial del rango seleccionado en formato datetime.date
268 startDate : fecha inicial del rango seleccionado en formato datetime.date
269
269
270 endDate : fecha final del rango seleccionado en formato datetime.date
270 endDate : fecha final del rango seleccionado en formato datetime.date
271
271
272 Return:
272 Return:
273 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
273 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
274 fecha especificado, de lo contrario retorna False.
274 fecha especificado, de lo contrario retorna False.
275 Excepciones:
275 Excepciones:
276 Si el archivo no tiene el formato adecuado
276 Si el archivo no tiene el formato adecuado
277 """
277 """
278
278
279 basename = os.path.basename(filename)
279 basename = os.path.basename(filename)
280
280
281 if not isRadarFile(basename):
281 if not isRadarFile(basename):
282 print("The filename %s has not the rigth format" % filename)
282 print("The filename %s has not the rigth format" % filename)
283 return 0
283 return 0
284
284
285 if startDate and endDate:
285 if startDate and endDate:
286 thisDate = getDateFromRadarFile(basename)
286 thisDate = getDateFromRadarFile(basename)
287
287
288 if thisDate < startDate:
288 if thisDate < startDate:
289 return 0
289 return 0
290
290
291 if thisDate > endDate:
291 if thisDate > endDate:
292 return 0
292 return 0
293
293
294 return 1
294 return 1
295
295
296
296
297 def getFileFromSet(path, ext, set):
297 def getFileFromSet(path, ext, set):
298 validFilelist = []
298 validFilelist = []
299 fileList = os.listdir(path)
299 fileList = os.listdir(path)
300
300
301 # 0 1234 567 89A BCDE
301 # 0 1234 567 89A BCDE
302 # H YYYY DDD SSS .ext
302 # H YYYY DDD SSS .ext
303
303
304 for thisFile in fileList:
304 for thisFile in fileList:
305 try:
305 try:
306 year = int(thisFile[1:5])
306 year = int(thisFile[1:5])
307 doy = int(thisFile[5:8])
307 doy = int(thisFile[5:8])
308 except:
308 except:
309 continue
309 continue
310
310
311 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
311 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
312 continue
312 continue
313
313
314 validFilelist.append(thisFile)
314 validFilelist.append(thisFile)
315
315
316 myfile = fnmatch.filter(
316 myfile = fnmatch.filter(
317 validFilelist, '*%4.4d%3.3d%3.3d*' % (year, doy, set))
317 validFilelist, '*%4.4d%3.3d%3.3d*' % (year, doy, set))
318
318
319 if len(myfile) != 0:
319 if len(myfile) != 0:
320 return myfile[0]
320 return myfile[0]
321 else:
321 else:
322 filename = '*%4.4d%3.3d%3.3d%s' % (year, doy, set, ext.lower())
322 filename = '*%4.4d%3.3d%3.3d%s' % (year, doy, set, ext.lower())
323 print('the filename %s does not exist' % filename)
323 print('the filename %s does not exist' % filename)
324 print('...going to the last file: ')
324 print('...going to the last file: ')
325
325
326 if validFilelist:
326 if validFilelist:
327 validFilelist = sorted(validFilelist, key=str.lower)
327 validFilelist = sorted(validFilelist, key=str.lower)
328 return validFilelist[-1]
328 return validFilelist[-1]
329
329
330 return None
330 return None
331
331
332
332
333 def getlastFileFromPath(path, ext):
333 def getlastFileFromPath(path, ext):
334 """
334 """
335 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
335 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
336 al final de la depuracion devuelve el ultimo file de la lista que quedo.
336 al final de la depuracion devuelve el ultimo file de la lista que quedo.
337
337
338 Input:
338 Input:
339 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
339 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
340 ext : extension de los files contenidos en una carpeta
340 ext : extension de los files contenidos en una carpeta
341
341
342 Return:
342 Return:
343 El ultimo file de una determinada carpeta, no se considera el path.
343 El ultimo file de una determinada carpeta, no se considera el path.
344 """
344 """
345 validFilelist = []
345 validFilelist = []
346 fileList = os.listdir(path)
346 fileList = os.listdir(path)
347
347
348 # 0 1234 567 89A BCDE
348 # 0 1234 567 89A BCDE
349 # H YYYY DDD SSS .ext
349 # H YYYY DDD SSS .ext
350
350
351 for thisFile in fileList:
351 for thisFile in fileList:
352
352
353 year = thisFile[1:5]
353 year = thisFile[1:5]
354 if not isNumber(year):
354 if not isNumber(year):
355 continue
355 continue
356
356
357 doy = thisFile[5:8]
357 doy = thisFile[5:8]
358 if not isNumber(doy):
358 if not isNumber(doy):
359 continue
359 continue
360
360
361 year = int(year)
361 year = int(year)
362 doy = int(doy)
362 doy = int(doy)
363
363
364 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
364 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
365 continue
365 continue
366
366
367 validFilelist.append(thisFile)
367 validFilelist.append(thisFile)
368
368
369 if validFilelist:
369 if validFilelist:
370 validFilelist = sorted(validFilelist, key=str.lower)
370 validFilelist = sorted(validFilelist, key=str.lower)
371 return validFilelist[-1]
371 return validFilelist[-1]
372
372
373 return None
373 return None
374
374
375
375
376 def isRadarFolder(folder):
376 def isRadarFolder(folder):
377 try:
377 try:
378 year = int(folder[1:5])
378 year = int(folder[1:5])
379 doy = int(folder[5:8])
379 doy = int(folder[5:8])
380 except:
380 except:
381 return 0
381 return 0
382
382
383 return 1
383 return 1
384
384
385
385
386 def isRadarFile(file):
386 def isRadarFile(file):
387 try:
387 try:
388 year = int(file[1:5])
388 year = int(file[1:5])
389 doy = int(file[5:8])
389 doy = int(file[5:8])
390 set = int(file[8:11])
390 set = int(file[8:11])
391 except:
391 except:
392 return 0
392 return 0
393
393
394 return 1
394 return 1
395
395
396
396
397 def getDateFromRadarFile(file):
397 def getDateFromRadarFile(file):
398 try:
398 try:
399 year = int(file[1:5])
399 year = int(file[1:5])
400 doy = int(file[5:8])
400 doy = int(file[5:8])
401 set = int(file[8:11])
401 set = int(file[8:11])
402 except:
402 except:
403 return None
403 return None
404
404
405 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
405 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
406 return thisDate
406 return thisDate
407
407
408
408
409 def getDateFromRadarFolder(folder):
409 def getDateFromRadarFolder(folder):
410 try:
410 try:
411 year = int(folder[1:5])
411 year = int(folder[1:5])
412 doy = int(folder[5:8])
412 doy = int(folder[5:8])
413 except:
413 except:
414 return None
414 return None
415
415
416 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
416 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
417 return thisDate
417 return thisDate
418
418
419 def parse_format(s, fmt):
419 def parse_format(s, fmt):
420
420
421 for i in range(fmt.count('%')):
421 for i in range(fmt.count('%')):
422 x = fmt.index('%')
422 x = fmt.index('%')
423 d = DT_DIRECTIVES[fmt[x:x+2]]
423 d = DT_DIRECTIVES[fmt[x:x+2]]
424 fmt = fmt.replace(fmt[x:x+2], s[x:x+d])
424 fmt = fmt.replace(fmt[x:x+2], s[x:x+d])
425 return fmt
425 return fmt
426
426
427 class Reader(object):
427 class Reader(object):
428
428
429 c = 3E8
429 c = 3E8
430 isConfig = False
430 isConfig = False
431 dtype = None
431 dtype = None
432 pathList = []
432 pathList = []
433 filenameList = []
433 filenameList = []
434 datetimeList = []
434 datetimeList = []
435 filename = None
435 filename = None
436 ext = None
436 ext = None
437 flagIsNewFile = 1
437 flagIsNewFile = 1
438 flagDiscontinuousBlock = 0
438 flagDiscontinuousBlock = 0
439 flagIsNewBlock = 0
439 flagIsNewBlock = 0
440 flagNoMoreFiles = 0
440 flagNoMoreFiles = 0
441 fp = None
441 fp = None
442 firstHeaderSize = 0
442 firstHeaderSize = 0
443 basicHeaderSize = 24
443 basicHeaderSize = 24
444 versionFile = 1103
444 versionFile = 1103
445 fileSize = None
445 fileSize = None
446 fileSizeByHeader = None
446 fileSizeByHeader = None
447 fileIndex = -1
447 fileIndex = -1
448 profileIndex = None
448 profileIndex = None
449 blockIndex = 0
449 blockIndex = 0
450 nTotalBlocks = 0
450 nTotalBlocks = 0
451 maxTimeStep = 30
451 maxTimeStep = 30
452 lastUTTime = None
452 lastUTTime = None
453 datablock = None
453 datablock = None
454 dataOut = None
454 dataOut = None
455 getByBlock = False
455 getByBlock = False
456 path = None
456 path = None
457 startDate = None
457 startDate = None
458 endDate = None
458 endDate = None
459 startTime = datetime.time(0, 0, 0)
459 startTime = datetime.time(0, 0, 0)
460 endTime = datetime.time(23, 59, 59)
460 endTime = datetime.time(23, 59, 59)
461 set = None
461 set = None
462 expLabel = ""
462 expLabel = ""
463 online = False
463 online = False
464 delay = 60
464 delay = 60
465 nTries = 3 # quantity tries
465 nTries = 3 # quantity tries
466 nFiles = 3 # number of files for searching
466 nFiles = 3 # number of files for searching
467 walk = True
467 walk = True
468 getblock = False
468 getblock = False
469 nTxs = 1
469 nTxs = 1
470 realtime = False
470 realtime = False
471 blocksize = 0
471 blocksize = 0
472 blocktime = None
472 blocktime = None
473 warnings = True
473 warnings = True
474 verbose = True
474 verbose = True
475 server = None
475 server = None
476 format = None
476 format = None
477 oneDDict = None
477 oneDDict = None
478 twoDDict = None
478 twoDDict = None
479 independentParam = None
479 independentParam = None
480 filefmt = None
480 filefmt = None
481 folderfmt = None
481 folderfmt = None
482 open_file = open
482 open_file = open
483 open_mode = 'rb'
483 open_mode = 'rb'
484
484
485 def run(self):
485 def run(self):
486
486
487 raise NotImplementedError
487 raise NotImplementedError
488
488
489 def getAllowedArgs(self):
489 def getAllowedArgs(self):
490 if hasattr(self, '__attrs__'):
490 if hasattr(self, '__attrs__'):
491 return self.__attrs__
491 return self.__attrs__
492 else:
492 else:
493 return inspect.getargspec(self.run).args
493 return inspect.getargspec(self.run).args
494
494
495 def set_kwargs(self, **kwargs):
495 def set_kwargs(self, **kwargs):
496
496
497 for key, value in kwargs.items():
497 for key, value in kwargs.items():
498 setattr(self, key, value)
498 setattr(self, key, value)
499
499
500 def find_folders(self, path, startDate, endDate, folderfmt, last=False):
500 def find_folders(self, path, startDate, endDate, folderfmt, last=False):
501
501
502 folders = [x for f in path.split(',')
502 folders = [x for f in path.split(',')
503 for x in os.listdir(f) if os.path.isdir(os.path.join(f, x))]
503 for x in os.listdir(f) if os.path.isdir(os.path.join(f, x))]
504 folders.sort()
504 folders.sort()
505
505
506 if last:
506 if last:
507 folders = [folders[-1]]
507 folders = [folders[-1]]
508
508
509 for folder in folders:
509 for folder in folders:
510 try:
510 try:
511 dt = datetime.datetime.strptime(parse_format(folder, folderfmt), folderfmt).date()
511 dt = datetime.datetime.strptime(parse_format(folder, folderfmt), folderfmt).date()
512 if dt >= startDate and dt <= endDate:
512 if dt >= startDate and dt <= endDate:
513 yield os.path.join(path, folder)
513 yield os.path.join(path, folder)
514 else:
514 else:
515 log.log('Skiping folder {}'.format(folder), self.name)
515 log.log('Skiping folder {}'.format(folder), self.name)
516 except Exception as e:
516 except Exception as e:
517 log.log('Skiping folder {}'.format(folder), self.name)
517 log.log('Skiping folder {}'.format(folder), self.name)
518 continue
518 continue
519 return
519 return
520
520
521 def find_files(self, folders, ext, filefmt, startDate=None, endDate=None,
521 def find_files(self, folders, ext, filefmt, startDate=None, endDate=None,
522 expLabel='', last=False):
522 expLabel='', last=False):
523
523
524 for path in folders:
524 for path in folders:
525 files = glob.glob1(path, '*{}'.format(ext))
525 files = glob.glob1(path, '*{}'.format(ext))
526 files.sort()
526 files.sort()
527 if last:
527 if last:
528 if files:
528 if files:
529 fo = files[-1]
529 fo = files[-1]
530 try:
530 try:
531 dt = datetime.datetime.strptime(parse_format(fo, filefmt), filefmt).date()
531 dt = datetime.datetime.strptime(parse_format(fo, filefmt), filefmt).date()
532 yield os.path.join(path, expLabel, fo)
532 yield os.path.join(path, expLabel, fo)
533 except Exception as e:
533 except Exception as e:
534 pass
534 pass
535 return
535 return
536 else:
536 else:
537 return
537 return
538
538
539 for fo in files:
539 for fo in files:
540 try:
540 try:
541 dt = datetime.datetime.strptime(parse_format(fo, filefmt), filefmt).date()
541 dt = datetime.datetime.strptime(parse_format(fo, filefmt), filefmt).date()
542 if dt >= startDate and dt <= endDate:
542 if dt >= startDate and dt <= endDate:
543 yield os.path.join(path, expLabel, fo)
543 yield os.path.join(path, expLabel, fo)
544 else:
544 else:
545 log.log('Skiping file {}'.format(fo), self.name)
545 log.log('Skiping file {}'.format(fo), self.name)
546 except Exception as e:
546 except Exception as e:
547 log.log('Skiping file {}'.format(fo), self.name)
547 log.log('Skiping file {}'.format(fo), self.name)
548 continue
548 continue
549
549
550 def searchFilesOffLine(self, path, startDate, endDate,
550 def searchFilesOffLine(self, path, startDate, endDate,
551 expLabel, ext, walk,
551 expLabel, ext, walk,
552 filefmt, folderfmt):
552 filefmt, folderfmt):
553 """Search files in offline mode for the given arguments
553 """Search files in offline mode for the given arguments
554
554
555 Return:
555 Return:
556 Generator of files
556 Generator of files
557 """
557 """
558
558
559 if walk:
559 if walk:
560 folders = self.find_folders(
560 folders = self.find_folders(
561 path, startDate, endDate, folderfmt)
561 path, startDate, endDate, folderfmt)
562 else:
562 else:
563 folders = path.split(',')
563 folders = path.split(',')
564
564
565 return self.find_files(
565 return self.find_files(
566 folders, ext, filefmt, startDate, endDate, expLabel)
566 folders, ext, filefmt, startDate, endDate, expLabel)
567
567
568 def searchFilesOnLine(self, path, startDate, endDate,
568 def searchFilesOnLine(self, path, startDate, endDate,
569 expLabel, ext, walk,
569 expLabel, ext, walk,
570 filefmt, folderfmt):
570 filefmt, folderfmt):
571 """Search for the last file of the last folder
571 """Search for the last file of the last folder
572
572
573 Arguments:
573 Arguments:
574 path : carpeta donde estan contenidos los files que contiene data
574 path : carpeta donde estan contenidos los files que contiene data
575 expLabel : Nombre del subexperimento (subfolder)
575 expLabel : Nombre del subexperimento (subfolder)
576 ext : extension de los files
576 ext : extension de los files
577 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
577 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
578
578
579 Return:
579 Return:
580 generator with the full path of last filename
580 generator with the full path of last filename
581 """
581 """
582
582
583 if walk:
583 if walk:
584 folders = self.find_folders(
584 folders = self.find_folders(
585 path, startDate, endDate, folderfmt, last=True)
585 path, startDate, endDate, folderfmt, last=True)
586 else:
586 else:
587 folders = path.split(',')
587 folders = path.split(',')
588
588
589 return self.find_files(
589 return self.find_files(
590 folders, ext, filefmt, startDate, endDate, expLabel, last=True)
590 folders, ext, filefmt, startDate, endDate, expLabel, last=True)
591
591
592 def setNextFile(self):
592 def setNextFile(self):
593 """Set the next file to be readed open it and parse de file header"""
593 """Set the next file to be readed open it and parse de file header"""
594
594
595 while True:
595 while True:
596 if self.fp != None:
596 if self.fp != None:
597 self.fp.close()
597 self.fp.close()
598
598
599 if self.online:
599 if self.online:
600 newFile = self.setNextFileOnline()
600 newFile = self.setNextFileOnline()
601 else:
601 else:
602 newFile = self.setNextFileOffline()
602 newFile = self.setNextFileOffline()
603
603
604 if not(newFile):
604 if not(newFile):
605 if self.online:
605 if self.online:
606 raise schainpy.admin.SchainError('Time to wait for new files reach')
606 raise schainpy.admin.SchainError('Time to wait for new files reach')
607 else:
607 else:
608 if self.fileIndex == -1:
608 if self.fileIndex == -1:
609 raise schainpy.admin.SchainWarning('No files found in the given path')
609 raise schainpy.admin.SchainWarning('No files found in the given path')
610 else:
610 else:
611 raise schainpy.admin.SchainWarning('No more files to read')
611 raise schainpy.admin.SchainWarning('No more files to read')
612
612
613 if self.verifyFile(self.filename):
613 if self.verifyFile(self.filename):
614 break
614 break
615
615
616 log.log('Opening file: %s' % self.filename, self.name)
616 log.log('Opening file: %s' % self.filename, self.name)
617
617
618 self.readFirstHeader()
618 self.readFirstHeader()
619 self.nReadBlocks = 0
619 self.nReadBlocks = 0
620
620
621 def setNextFileOnline(self):
621 def setNextFileOnline(self):
622 """Check for the next file to be readed in online mode.
622 """Check for the next file to be readed in online mode.
623
623
624 Set:
624 Set:
625 self.filename
625 self.filename
626 self.fp
626 self.fp
627 self.filesize
627 self.filesize
628
628
629 Return:
629 Return:
630 boolean
630 boolean
631
631
632 """
632 """
633 nextFile = True
633 nextFile = True
634 nextDay = False
634 nextDay = False
635
635
636 for nFiles in range(self.nFiles+1):
636 for nFiles in range(self.nFiles+1):
637 for nTries in range(self.nTries):
637 for nTries in range(self.nTries):
638 fullfilename, filename = self.checkForRealPath(nextFile, nextDay)
638 fullfilename, filename = self.checkForRealPath(nextFile, nextDay)
639 if fullfilename is not None:
639 if fullfilename is not None:
640 break
640 break
641 log.warning(
641 log.warning(
642 "Waiting %0.2f sec for the next file: \"%s\" , try %02d ..." % (self.delay, filename, nTries + 1),
642 "Waiting %0.2f sec for the next file: \"%s\" , try %02d ..." % (self.delay, filename, nTries + 1),
643 self.name)
643 self.name)
644 time.sleep(self.delay)
644 time.sleep(self.delay)
645 nextFile = False
645 nextFile = False
646 continue
646 continue
647
647
648 if fullfilename is not None:
648 if fullfilename is not None:
649 break
649 break
650
650
651 self.nTries = 1
651 self.nTries = 1
652 nextFile = True
652 nextFile = True
653
653
654 if nFiles == (self.nFiles - 1):
654 if nFiles == (self.nFiles - 1):
655 log.log('Trying with next day...', self.name)
655 log.log('Trying with next day...', self.name)
656 nextDay = True
656 nextDay = True
657 self.nTries = 3
657 self.nTries = 3
658
658
659 if fullfilename:
659 if fullfilename:
660 self.fileSize = os.path.getsize(fullfilename)
660 self.fileSize = os.path.getsize(fullfilename)
661 self.filename = fullfilename
661 self.filename = fullfilename
662 self.flagIsNewFile = 1
662 self.flagIsNewFile = 1
663 if self.fp != None:
663 if self.fp != None:
664 self.fp.close()
664 self.fp.close()
665 self.fp = self.open_file(fullfilename, self.open_mode)
665 self.fp = self.open_file(fullfilename, self.open_mode)
666 self.flagNoMoreFiles = 0
666 self.flagNoMoreFiles = 0
667 self.fileIndex += 1
667 self.fileIndex += 1
668 return 1
668 return 1
669 else:
669 else:
670 return 0
670 return 0
671
671
672 def setNextFileOffline(self):
672 def setNextFileOffline(self):
673 """Open the next file to be readed in offline mode"""
673 """Open the next file to be readed in offline mode"""
674
674
675 try:
675 try:
676 filename = next(self.filenameList)
676 filename = next(self.filenameList)
677 self.fileIndex +=1
677 self.fileIndex +=1
678 except StopIteration:
678 except StopIteration:
679 self.flagNoMoreFiles = 1
679 self.flagNoMoreFiles = 1
680 return 0
680 return 0
681
681
682 self.filename = filename
682 self.filename = filename
683 self.fileSize = os.path.getsize(filename)
683 self.fileSize = os.path.getsize(filename)
684 self.fp = self.open_file(filename, self.open_mode)
684 self.fp = self.open_file(filename, self.open_mode)
685 self.flagIsNewFile = 1
685 self.flagIsNewFile = 1
686
686
687 return 1
687 return 1
688
688
689 @staticmethod
689 @staticmethod
690 def isDateTimeInRange(dt, startDate, endDate, startTime, endTime):
690 def isDateTimeInRange(dt, startDate, endDate, startTime, endTime):
691 """Check if the given datetime is in range"""
691 """Check if the given datetime is in range"""
692 startDateTime= datetime.datetime.combine(startDate,startTime)
692 startDateTime= datetime.datetime.combine(startDate,startTime)
693 endDateTime = datetime.datetime.combine(endDate,endTime)
693 endDateTime = datetime.datetime.combine(endDate,endTime)
694 #print("dt eval: ", dt, startDateTime,endDateTime)
694 if startDateTime <= dt <= endDateTime:
695 if startDateTime <= dt <= endDateTime:
695 return True
696 return True
696 return False
697 return False
697
698
698 def verifyFile(self, filename):
699 def verifyFile(self, filename):
699 """Check for a valid file
700 """Check for a valid file
700
701
701 Arguments:
702 Arguments:
702 filename -- full path filename
703 filename -- full path filename
703
704
704 Return:
705 Return:
705 boolean
706 boolean
706 """
707 """
707
708
708 return True
709 return True
709
710
710 def checkForRealPath(self, nextFile, nextDay):
711 def checkForRealPath(self, nextFile, nextDay):
711 """Check if the next file to be readed exists"""
712 """Check if the next file to be readed exists"""
712
713
713 raise NotImplementedError
714 raise NotImplementedError
714
715
715 def readFirstHeader(self):
716 def readFirstHeader(self):
716 """Parse the file header"""
717 """Parse the file header"""
717
718
718 pass
719 pass
719
720
720 def waitDataBlock(self, pointer_location, blocksize=None):
721 def waitDataBlock(self, pointer_location, blocksize=None):
721 """
722 """
722 """
723 """
723
724
724 currentPointer = pointer_location
725 currentPointer = pointer_location
725 if blocksize is None:
726 if blocksize is None:
726 neededSize = self.processingHeaderObj.blockSize # + self.basicHeaderSize
727 neededSize = self.processingHeaderObj.blockSize # + self.basicHeaderSize
727 else:
728 else:
728 neededSize = blocksize
729 neededSize = blocksize
729
730
730 for nTries in range(self.nTries):
731 for nTries in range(self.nTries):
731 self.fp.close()
732 self.fp.close()
732 self.fp = open(self.filename, 'rb')
733 self.fp = open(self.filename, 'rb')
733 self.fp.seek(currentPointer)
734 self.fp.seek(currentPointer)
734
735
735 self.fileSize = os.path.getsize(self.filename)
736 self.fileSize = os.path.getsize(self.filename)
736 currentSize = self.fileSize - currentPointer
737 currentSize = self.fileSize - currentPointer
737
738
738 if (currentSize >= neededSize):
739 if (currentSize >= neededSize):
739 return 1
740 return 1
740
741
741 log.warning(
742 log.warning(
742 "Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1),
743 "Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1),
743 self.name
744 self.name
744 )
745 )
745 time.sleep(self.delay)
746 time.sleep(self.delay)
746
747
747 return 0
748 return 0
748
749
749 class JRODataReader(Reader):
750 class JRODataReader(Reader):
750
751
751 utc = 0
752 utc = 0
752 nReadBlocks = 0
753 nReadBlocks = 0
753 foldercounter = 0
754 foldercounter = 0
754 firstHeaderSize = 0
755 firstHeaderSize = 0
755 basicHeaderSize = 24
756 basicHeaderSize = 24
756 __isFirstTimeOnline = 1
757 __isFirstTimeOnline = 1
757 filefmt = "*%Y%j***"
758 filefmt = "*%Y%j***"
758 folderfmt = "*%Y%j"
759 folderfmt = "*%Y%j"
759 __attrs__ = ['path', 'startDate', 'endDate', 'startTime', 'endTime', 'online', 'delay', 'walk']
760 __attrs__ = ['path', 'startDate', 'endDate', 'startTime', 'endTime', 'online', 'delay', 'walk']
760
761
761 def getDtypeWidth(self):
762 def getDtypeWidth(self):
762
763
763 dtype_index = get_dtype_index(self.dtype)
764 dtype_index = get_dtype_index(self.dtype)
764 dtype_width = get_dtype_width(dtype_index)
765 dtype_width = get_dtype_width(dtype_index)
765
766
766 return dtype_width
767 return dtype_width
767
768
768 def checkForRealPath(self, nextFile, nextDay):
769 def checkForRealPath(self, nextFile, nextDay):
769 """Check if the next file to be readed exists.
770 """Check if the next file to be readed exists.
770
771
771 Example :
772 Example :
772 nombre correcto del file es .../.../D2009307/P2009307367.ext
773 nombre correcto del file es .../.../D2009307/P2009307367.ext
773
774
774 Entonces la funcion prueba con las siguientes combinaciones
775 Entonces la funcion prueba con las siguientes combinaciones
775 .../.../y2009307367.ext
776 .../.../y2009307367.ext
776 .../.../Y2009307367.ext
777 .../.../Y2009307367.ext
777 .../.../x2009307/y2009307367.ext
778 .../.../x2009307/y2009307367.ext
778 .../.../x2009307/Y2009307367.ext
779 .../.../x2009307/Y2009307367.ext
779 .../.../X2009307/y2009307367.ext
780 .../.../X2009307/y2009307367.ext
780 .../.../X2009307/Y2009307367.ext
781 .../.../X2009307/Y2009307367.ext
781 siendo para este caso, la ultima combinacion de letras, identica al file buscado
782 siendo para este caso, la ultima combinacion de letras, identica al file buscado
782
783
783 Return:
784 Return:
784 str -- fullpath of the file
785 str -- fullpath of the file
785 """
786 """
786
787
787
788
788 if nextFile:
789 if nextFile:
789 self.set += 1
790 self.set += 1
790 if nextDay:
791 if nextDay:
791 self.set = 0
792 self.set = 0
792 self.doy += 1
793 self.doy += 1
793 foldercounter = 0
794 foldercounter = 0
794 prefixDirList = [None, 'd', 'D']
795 prefixDirList = [None, 'd', 'D']
795 if self.ext.lower() == ".r": # voltage
796 if self.ext.lower() == ".r": # voltage
796 prefixFileList = ['d', 'D']
797 prefixFileList = ['d', 'D']
797 elif self.ext.lower() == ".pdata": # spectra
798 elif self.ext.lower() == ".pdata": # spectra
798 prefixFileList = ['p', 'P']
799 prefixFileList = ['p', 'P']
799
800
800 # barrido por las combinaciones posibles
801 # barrido por las combinaciones posibles
801 for prefixDir in prefixDirList:
802 for prefixDir in prefixDirList:
802 thispath = self.path
803 thispath = self.path
803 if prefixDir != None:
804 if prefixDir != None:
804 # formo el nombre del directorio xYYYYDDD (x=d o x=D)
805 # formo el nombre del directorio xYYYYDDD (x=d o x=D)
805 if foldercounter == 0:
806 if foldercounter == 0:
806 thispath = os.path.join(self.path, "%s%04d%03d" %
807 thispath = os.path.join(self.path, "%s%04d%03d" %
807 (prefixDir, self.year, self.doy))
808 (prefixDir, self.year, self.doy))
808 else:
809 else:
809 thispath = os.path.join(self.path, "%s%04d%03d_%02d" % (
810 thispath = os.path.join(self.path, "%s%04d%03d_%02d" % (
810 prefixDir, self.year, self.doy, foldercounter))
811 prefixDir, self.year, self.doy, foldercounter))
811 for prefixFile in prefixFileList: # barrido por las dos combinaciones posibles de "D"
812 for prefixFile in prefixFileList: # barrido por las dos combinaciones posibles de "D"
812 # formo el nombre del file xYYYYDDDSSS.ext
813 # formo el nombre del file xYYYYDDDSSS.ext
813 filename = "%s%04d%03d%03d%s" % (prefixFile, self.year, self.doy, self.set, self.ext)
814 filename = "%s%04d%03d%03d%s" % (prefixFile, self.year, self.doy, self.set, self.ext)
814 fullfilename = os.path.join(
815 fullfilename = os.path.join(
815 thispath, filename)
816 thispath, filename)
816
817
817 if os.path.exists(fullfilename):
818 if os.path.exists(fullfilename):
818 return fullfilename, filename
819 return fullfilename, filename
819
820
820 return None, filename
821 return None, filename
821
822
822 def __waitNewBlock(self):
823 def __waitNewBlock(self):
823 """
824 """
824 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
825 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
825
826
826 Si el modo de lectura es OffLine siempre retorn 0
827 Si el modo de lectura es OffLine siempre retorn 0
827 """
828 """
828 if not self.online:
829 if not self.online:
829 return 0
830 return 0
830
831
831 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
832 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
832 return 0
833 return 0
833
834
834 currentPointer = self.fp.tell()
835 currentPointer = self.fp.tell()
835
836
836 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
837 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
837
838
838 for nTries in range(self.nTries):
839 for nTries in range(self.nTries):
839
840
840 self.fp.close()
841 self.fp.close()
841 self.fp = open(self.filename, 'rb')
842 self.fp = open(self.filename, 'rb')
842 self.fp.seek(currentPointer)
843 self.fp.seek(currentPointer)
843
844
844 self.fileSize = os.path.getsize(self.filename)
845 self.fileSize = os.path.getsize(self.filename)
845 currentSize = self.fileSize - currentPointer
846 currentSize = self.fileSize - currentPointer
846
847
847 if (currentSize >= neededSize):
848 if (currentSize >= neededSize):
848 self.basicHeaderObj.read(self.fp)
849 self.basicHeaderObj.read(self.fp)
849 return 1
850 return 1
850
851
851 if self.fileSize == self.fileSizeByHeader:
852 if self.fileSize == self.fileSizeByHeader:
852 # self.flagEoF = True
853 # self.flagEoF = True
853 return 0
854 return 0
854
855
855 print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1))
856 print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1))
856 time.sleep(self.delay)
857 time.sleep(self.delay)
857
858
858 return 0
859 return 0
859
860
860 def __setNewBlock(self):
861 def __setNewBlock(self):
861
862
862 if self.fp == None:
863 if self.fp == None:
863 return 0
864 return 0
864
865
865 if self.flagIsNewFile:
866 if self.flagIsNewFile:
866 self.lastUTTime = self.basicHeaderObj.utc
867 self.lastUTTime = self.basicHeaderObj.utc
867 return 1
868 return 1
868
869
869 if self.realtime:
870 if self.realtime:
870 self.flagDiscontinuousBlock = 1
871 self.flagDiscontinuousBlock = 1
871 if not(self.setNextFile()):
872 if not(self.setNextFile()):
872 return 0
873 return 0
873 else:
874 else:
874 return 1
875 return 1
875
876
876 currentSize = self.fileSize - self.fp.tell()
877 currentSize = self.fileSize - self.fp.tell()
877 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
878 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
878
879
879 if (currentSize >= neededSize):
880 if (currentSize >= neededSize):
880 self.basicHeaderObj.read(self.fp)
881 self.basicHeaderObj.read(self.fp)
881 self.lastUTTime = self.basicHeaderObj.utc
882 self.lastUTTime = self.basicHeaderObj.utc
882 return 1
883 return 1
883
884
884 if self.__waitNewBlock():
885 if self.__waitNewBlock():
885 self.lastUTTime = self.basicHeaderObj.utc
886 self.lastUTTime = self.basicHeaderObj.utc
886 return 1
887 return 1
887
888
888 if not(self.setNextFile()):
889 if not(self.setNextFile()):
889 return 0
890 return 0
890
891
891 deltaTime = self.basicHeaderObj.utc - self.lastUTTime
892 deltaTime = self.basicHeaderObj.utc - self.lastUTTime
892 self.lastUTTime = self.basicHeaderObj.utc
893 self.lastUTTime = self.basicHeaderObj.utc
893
894
894 self.flagDiscontinuousBlock = 0
895 self.flagDiscontinuousBlock = 0
895
896
896 if deltaTime > self.maxTimeStep:
897 if deltaTime > self.maxTimeStep:
897 self.flagDiscontinuousBlock = 1
898 self.flagDiscontinuousBlock = 1
898
899
899 return 1
900 return 1
900
901
901 def readNextBlock(self):
902 def readNextBlock(self):
902
903
903 while True:
904 while True:
904 if not(self.__setNewBlock()):
905 if not(self.__setNewBlock()):
905 continue
906 continue
906
907
907 if not(self.readBlock()):
908 if not(self.readBlock()):
908 return 0
909 return 0
909
910
910 self.getBasicHeader()
911 self.getBasicHeader()
911
912
912 if not self.isDateTimeInRange(self.dataOut.datatime, self.startDate, self.endDate, self.startTime, self.endTime):
913 if not self.isDateTimeInRange(self.dataOut.datatime, self.startDate, self.endDate, self.startTime, self.endTime):
913 print("[Reading] Block No. %d/%d -> %s [Skipping]" % (self.nReadBlocks,
914 print("[Reading] Block No. %d/%d -> %s [Skipping]" % (self.nReadBlocks,
914 self.processingHeaderObj.dataBlocksPerFile,
915 self.processingHeaderObj.dataBlocksPerFile,
915 self.dataOut.datatime.ctime()))
916 self.dataOut.datatime.ctime()))
916 continue
917 continue
917
918
918 break
919 break
919
920
920 if self.verbose:
921 if self.verbose:
921 print("[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks,
922 print("[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks,
922 self.processingHeaderObj.dataBlocksPerFile,
923 self.processingHeaderObj.dataBlocksPerFile,
923 self.dataOut.datatime.ctime()))
924 self.dataOut.datatime.ctime()))
924 return 1
925 return 1
925
926
926 def readFirstHeader(self):
927 def readFirstHeader(self):
927
928
928 self.basicHeaderObj.read(self.fp)
929 self.basicHeaderObj.read(self.fp)
929 self.systemHeaderObj.read(self.fp)
930 self.systemHeaderObj.read(self.fp)
930 self.radarControllerHeaderObj.read(self.fp)
931 self.radarControllerHeaderObj.read(self.fp)
931 self.processingHeaderObj.read(self.fp)
932 self.processingHeaderObj.read(self.fp)
932 self.firstHeaderSize = self.basicHeaderObj.size
933 self.firstHeaderSize = self.basicHeaderObj.size
933
934
934 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
935 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
935 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
936 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
936 if datatype == 0:
937 if datatype == 0:
937 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
938 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
938 elif datatype == 1:
939 elif datatype == 1:
939 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
940 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
940 elif datatype == 2:
941 elif datatype == 2:
941 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
942 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
942 elif datatype == 3:
943 elif datatype == 3:
943 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
944 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
944 elif datatype == 4:
945 elif datatype == 4:
945 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
946 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
946 elif datatype == 5:
947 elif datatype == 5:
947 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
948 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
948 else:
949 else:
949 raise ValueError('Data type was not defined')
950 raise ValueError('Data type was not defined')
950
951
951 self.dtype = datatype_str
952 self.dtype = datatype_str
952 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
953 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
953 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \
954 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \
954 self.firstHeaderSize + self.basicHeaderSize * \
955 self.firstHeaderSize + self.basicHeaderSize * \
955 (self.processingHeaderObj.dataBlocksPerFile - 1)
956 (self.processingHeaderObj.dataBlocksPerFile - 1)
956 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
957 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
957 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
958 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
958 self.getBlockDimension()
959 self.getBlockDimension()
959
960
960 def verifyFile(self, filename):
961 def verifyFile(self, filename):
961
962
962 flag = True
963 flag = True
963
964
964 try:
965 try:
965 fp = open(filename, 'rb')
966 fp = open(filename, 'rb')
966 except IOError:
967 except IOError:
967 log.error("File {} can't be opened".format(filename), self.name)
968 log.error("File {} can't be opened".format(filename), self.name)
968 return False
969 return False
969
970
970 if self.online and self.waitDataBlock(0):
971 if self.online and self.waitDataBlock(0):
971 pass
972 pass
972
973
973 basicHeaderObj = BasicHeader(LOCALTIME)
974 basicHeaderObj = BasicHeader(LOCALTIME)
974 systemHeaderObj = SystemHeader()
975 systemHeaderObj = SystemHeader()
975 radarControllerHeaderObj = RadarControllerHeader()
976 radarControllerHeaderObj = RadarControllerHeader()
976 processingHeaderObj = ProcessingHeader()
977 processingHeaderObj = ProcessingHeader()
977
978
978 if not(basicHeaderObj.read(fp)):
979 if not(basicHeaderObj.read(fp)):
979 flag = False
980 flag = False
980 if not(systemHeaderObj.read(fp)):
981 if not(systemHeaderObj.read(fp)):
981 flag = False
982 flag = False
982 if not(radarControllerHeaderObj.read(fp)):
983 if not(radarControllerHeaderObj.read(fp)):
983 flag = False
984 flag = False
984 if not(processingHeaderObj.read(fp)):
985 if not(processingHeaderObj.read(fp)):
985 flag = False
986 flag = False
986 if not self.online:
987 if not self.online:
987 dt1 = basicHeaderObj.datatime
988 dt1 = basicHeaderObj.datatime
988 pos = self.fileSize-processingHeaderObj.blockSize-24
989 pos = self.fileSize-processingHeaderObj.blockSize-24
989 if pos<0:
990 if pos<0:
990 flag = False
991 flag = False
991 log.error('Invalid size for file: {}'.format(self.filename), self.name)
992 log.error('Invalid size for file: {}'.format(self.filename), self.name)
992 else:
993 else:
993 fp.seek(pos)
994 fp.seek(pos)
994 if not(basicHeaderObj.read(fp)):
995 if not(basicHeaderObj.read(fp)):
995 flag = False
996 flag = False
996 dt2 = basicHeaderObj.datatime
997 dt2 = basicHeaderObj.datatime
997 if not self.isDateTimeInRange(dt1, self.startDate, self.endDate, self.startTime, self.endTime) and not \
998 if not self.isDateTimeInRange(dt1, self.startDate, self.endDate, self.startTime, self.endTime) and not \
998 self.isDateTimeInRange(dt2, self.startDate, self.endDate, self.startTime, self.endTime):
999 self.isDateTimeInRange(dt2, self.startDate, self.endDate, self.startTime, self.endTime):
999 flag = False
1000 flag = False
1000
1001
1001 fp.close()
1002 fp.close()
1002 return flag
1003 return flag
1003
1004
1004 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1005 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1005
1006
1006 path_empty = True
1007 path_empty = True
1007
1008
1008 dateList = []
1009 dateList = []
1009 pathList = []
1010 pathList = []
1010
1011
1011 multi_path = path.split(',')
1012 multi_path = path.split(',')
1012
1013
1013 if not walk:
1014 if not walk:
1014
1015
1015 for single_path in multi_path:
1016 for single_path in multi_path:
1016
1017
1017 if not os.path.isdir(single_path):
1018 if not os.path.isdir(single_path):
1018 continue
1019 continue
1019
1020
1020 fileList = glob.glob1(single_path, "*" + ext)
1021 fileList = glob.glob1(single_path, "*" + ext)
1021
1022
1022 if not fileList:
1023 if not fileList:
1023 continue
1024 continue
1024
1025
1025 path_empty = False
1026 path_empty = False
1026
1027
1027 fileList.sort()
1028 fileList.sort()
1028
1029
1029 for thisFile in fileList:
1030 for thisFile in fileList:
1030
1031
1031 if not os.path.isfile(os.path.join(single_path, thisFile)):
1032 if not os.path.isfile(os.path.join(single_path, thisFile)):
1032 continue
1033 continue
1033
1034
1034 if not isRadarFile(thisFile):
1035 if not isRadarFile(thisFile):
1035 continue
1036 continue
1036
1037
1037 if not isFileInDateRange(thisFile, startDate, endDate):
1038 if not isFileInDateRange(thisFile, startDate, endDate):
1038 continue
1039 continue
1039
1040
1040 thisDate = getDateFromRadarFile(thisFile)
1041 thisDate = getDateFromRadarFile(thisFile)
1041
1042
1042 if thisDate in dateList or single_path in pathList:
1043 if thisDate in dateList or single_path in pathList:
1043 continue
1044 continue
1044
1045
1045 dateList.append(thisDate)
1046 dateList.append(thisDate)
1046 pathList.append(single_path)
1047 pathList.append(single_path)
1047
1048
1048 else:
1049 else:
1049 for single_path in multi_path:
1050 for single_path in multi_path:
1050
1051
1051 if not os.path.isdir(single_path):
1052 if not os.path.isdir(single_path):
1052 continue
1053 continue
1053
1054
1054 dirList = []
1055 dirList = []
1055
1056
1056 for thisPath in os.listdir(single_path):
1057 for thisPath in os.listdir(single_path):
1057
1058
1058 if not os.path.isdir(os.path.join(single_path, thisPath)):
1059 if not os.path.isdir(os.path.join(single_path, thisPath)):
1059 continue
1060 continue
1060
1061
1061 if not isRadarFolder(thisPath):
1062 if not isRadarFolder(thisPath):
1062 continue
1063 continue
1063
1064
1064 if not isFolderInDateRange(thisPath, startDate, endDate):
1065 if not isFolderInDateRange(thisPath, startDate, endDate):
1065 continue
1066 continue
1066
1067
1067 dirList.append(thisPath)
1068 dirList.append(thisPath)
1068
1069
1069 if not dirList:
1070 if not dirList:
1070 continue
1071 continue
1071
1072
1072 dirList.sort()
1073 dirList.sort()
1073
1074
1074 for thisDir in dirList:
1075 for thisDir in dirList:
1075
1076
1076 datapath = os.path.join(single_path, thisDir, expLabel)
1077 datapath = os.path.join(single_path, thisDir, expLabel)
1077 fileList = glob.glob1(datapath, "*" + ext)
1078 fileList = glob.glob1(datapath, "*" + ext)
1078
1079
1079 if not fileList:
1080 if not fileList:
1080 continue
1081 continue
1081
1082
1082 path_empty = False
1083 path_empty = False
1083
1084
1084 thisDate = getDateFromRadarFolder(thisDir)
1085 thisDate = getDateFromRadarFolder(thisDir)
1085
1086
1086 pathList.append(datapath)
1087 pathList.append(datapath)
1087 dateList.append(thisDate)
1088 dateList.append(thisDate)
1088
1089
1089 dateList.sort()
1090 dateList.sort()
1090
1091
1091 if walk:
1092 if walk:
1092 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1093 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1093 else:
1094 else:
1094 pattern_path = multi_path[0]
1095 pattern_path = multi_path[0]
1095
1096
1096 if path_empty:
1097 if path_empty:
1097 raise schainpy.admin.SchainError("[Reading] No *%s files in %s for %s to %s" % (ext, pattern_path, startDate, endDate))
1098 raise schainpy.admin.SchainError("[Reading] No *%s files in %s for %s to %s" % (ext, pattern_path, startDate, endDate))
1098 else:
1099 else:
1099 if not dateList:
1100 if not dateList:
1100 raise schainpy.admin.SchainError("[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" % (startDate, endDate, ext, path))
1101 raise schainpy.admin.SchainError("[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" % (startDate, endDate, ext, path))
1101
1102
1102 if include_path:
1103 if include_path:
1103 return dateList, pathList
1104 return dateList, pathList
1104
1105
1105 return dateList
1106 return dateList
1106
1107
1107 def setup(self, **kwargs):
1108 def setup(self, **kwargs):
1108
1109
1109 self.set_kwargs(**kwargs)
1110 self.set_kwargs(**kwargs)
1110 if not self.ext.startswith('.'):
1111 if not self.ext.startswith('.'):
1111 self.ext = '.{}'.format(self.ext)
1112 self.ext = '.{}'.format(self.ext)
1112
1113
1113 if self.server is not None:
1114 if self.server is not None:
1114 if 'tcp://' in self.server:
1115 if 'tcp://' in self.server:
1115 address = server
1116 address = server
1116 else:
1117 else:
1117 address = 'ipc:///tmp/%s' % self.server
1118 address = 'ipc:///tmp/%s' % self.server
1118 self.server = address
1119 self.server = address
1119 self.context = zmq.Context()
1120 self.context = zmq.Context()
1120 self.receiver = self.context.socket(zmq.PULL)
1121 self.receiver = self.context.socket(zmq.PULL)
1121 self.receiver.connect(self.server)
1122 self.receiver.connect(self.server)
1122 time.sleep(0.5)
1123 time.sleep(0.5)
1123 print('[Starting] ReceiverData from {}'.format(self.server))
1124 print('[Starting] ReceiverData from {}'.format(self.server))
1124 else:
1125 else:
1125 self.server = None
1126 self.server = None
1126 if self.path == None:
1127 if self.path == None:
1127 raise ValueError("[Reading] The path is not valid")
1128 raise ValueError("[Reading] The path is not valid")
1128
1129
1129 if self.online:
1130 if self.online:
1130 log.log("[Reading] Searching files in online mode...", self.name)
1131 log.log("[Reading] Searching files in online mode...", self.name)
1131
1132
1132 for nTries in range(self.nTries):
1133 for nTries in range(self.nTries):
1133 fullpath = self.searchFilesOnLine(self.path, self.startDate,
1134 fullpath = self.searchFilesOnLine(self.path, self.startDate,
1134 self.endDate, self.expLabel, self.ext, self.walk,
1135 self.endDate, self.expLabel, self.ext, self.walk,
1135 self.filefmt, self.folderfmt)
1136 self.filefmt, self.folderfmt)
1136
1137
1137 try:
1138 try:
1138 fullpath = next(fullpath)
1139 fullpath = next(fullpath)
1139 except:
1140 except:
1140 fullpath = None
1141 fullpath = None
1141
1142
1142 if fullpath:
1143 if fullpath:
1143 break
1144 break
1144
1145
1145 log.warning(
1146 log.warning(
1146 'Waiting {} sec for a valid file in {}: try {} ...'.format(
1147 'Waiting {} sec for a valid file in {}: try {} ...'.format(
1147 self.delay, self.path, nTries + 1),
1148 self.delay, self.path, nTries + 1),
1148 self.name)
1149 self.name)
1149 time.sleep(self.delay)
1150 time.sleep(self.delay)
1150
1151
1151 if not(fullpath):
1152 if not(fullpath):
1152 raise schainpy.admin.SchainError(
1153 raise schainpy.admin.SchainError(
1153 'There isn\'t any valid file in {}'.format(self.path))
1154 'There isn\'t any valid file in {}'.format(self.path))
1154
1155
1155 pathname, filename = os.path.split(fullpath)
1156 pathname, filename = os.path.split(fullpath)
1156 self.year = int(filename[1:5])
1157 self.year = int(filename[1:5])
1157 self.doy = int(filename[5:8])
1158 self.doy = int(filename[5:8])
1158 self.set = int(filename[8:11]) - 1
1159 self.set = int(filename[8:11]) - 1
1159 else:
1160 else:
1160 log.log("Searching files in {}".format(self.path), self.name)
1161 log.log("Searching files in {}".format(self.path), self.name)
1161 self.filenameList = self.searchFilesOffLine(self.path, self.startDate,
1162 self.filenameList = self.searchFilesOffLine(self.path, self.startDate,
1162 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
1163 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
1163
1164
1164 self.setNextFile()
1165 self.setNextFile()
1165
1166
1166 return
1167 return
1167
1168
1168 def getBasicHeader(self):
1169 def getBasicHeader(self):
1169
1170
1170 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond / \
1171 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond / \
1171 1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1172 1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1172
1173
1173 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1174 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1174
1175
1175 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1176 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1176
1177
1177 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1178 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1178
1179
1179 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1180 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1180
1181
1181 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1182 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1182
1183
1183 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
1184 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
1184
1185
1185 def getFirstHeader(self):
1186 def getFirstHeader(self):
1186
1187
1187 raise NotImplementedError
1188 raise NotImplementedError
1188
1189
1189 def getData(self):
1190 def getData(self):
1190
1191
1191 raise NotImplementedError
1192 raise NotImplementedError
1192
1193
1193 def hasNotDataInBuffer(self):
1194 def hasNotDataInBuffer(self):
1194
1195
1195 raise NotImplementedError
1196 raise NotImplementedError
1196
1197
1197 def readBlock(self):
1198 def readBlock(self):
1198
1199
1199 raise NotImplementedError
1200 raise NotImplementedError
1200
1201
1201 def isEndProcess(self):
1202 def isEndProcess(self):
1202
1203
1203 return self.flagNoMoreFiles
1204 return self.flagNoMoreFiles
1204
1205
1205 def printReadBlocks(self):
1206 def printReadBlocks(self):
1206
1207
1207 print("[Reading] Number of read blocks per file %04d" % self.nReadBlocks)
1208 print("[Reading] Number of read blocks per file %04d" % self.nReadBlocks)
1208
1209
1209 def printTotalBlocks(self):
1210 def printTotalBlocks(self):
1210
1211
1211 print("[Reading] Number of read blocks %04d" % self.nTotalBlocks)
1212 print("[Reading] Number of read blocks %04d" % self.nTotalBlocks)
1212
1213
1213 def run(self, **kwargs):
1214 def run(self, **kwargs):
1214 """
1215 """
1215
1216
1216 Arguments:
1217 Arguments:
1217 path :
1218 path :
1218 startDate :
1219 startDate :
1219 endDate :
1220 endDate :
1220 startTime :
1221 startTime :
1221 endTime :
1222 endTime :
1222 set :
1223 set :
1223 expLabel :
1224 expLabel :
1224 ext :
1225 ext :
1225 online :
1226 online :
1226 delay :
1227 delay :
1227 walk :
1228 walk :
1228 getblock :
1229 getblock :
1229 nTxs :
1230 nTxs :
1230 realtime :
1231 realtime :
1231 blocksize :
1232 blocksize :
1232 blocktime :
1233 blocktime :
1233 skip :
1234 skip :
1234 cursor :
1235 cursor :
1235 warnings :
1236 warnings :
1236 server :
1237 server :
1237 verbose :
1238 verbose :
1238 format :
1239 format :
1239 oneDDict :
1240 oneDDict :
1240 twoDDict :
1241 twoDDict :
1241 independentParam :
1242 independentParam :
1242 """
1243 """
1243
1244
1244 if not(self.isConfig):
1245 if not(self.isConfig):
1245 self.setup(**kwargs)
1246 self.setup(**kwargs)
1246 self.isConfig = True
1247 self.isConfig = True
1247 if self.server is None:
1248 if self.server is None:
1248 self.getData()
1249 self.getData()
1249 else:
1250 else:
1250 self.getFromServer()
1251 self.getFromServer()
1251
1252
1252
1253
1253 class JRODataWriter(Reader):
1254 class JRODataWriter(Reader):
1254
1255
1255 """
1256 """
1256 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1257 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1257 de los datos siempre se realiza por bloques.
1258 de los datos siempre se realiza por bloques.
1258 """
1259 """
1259
1260
1260 setFile = None
1261 setFile = None
1261 profilesPerBlock = None
1262 profilesPerBlock = None
1262 blocksPerFile = None
1263 blocksPerFile = None
1263 nWriteBlocks = 0
1264 nWriteBlocks = 0
1264 fileDate = None
1265 fileDate = None
1265
1266
1266 def __init__(self, dataOut=None):
1267 def __init__(self, dataOut=None):
1267 raise NotImplementedError
1268 raise NotImplementedError
1268
1269
1269 def hasAllDataInBuffer(self):
1270 def hasAllDataInBuffer(self):
1270 raise NotImplementedError
1271 raise NotImplementedError
1271
1272
1272 def setBlockDimension(self):
1273 def setBlockDimension(self):
1273 raise NotImplementedError
1274 raise NotImplementedError
1274
1275
1275 def writeBlock(self):
1276 def writeBlock(self):
1276 raise NotImplementedError
1277 raise NotImplementedError
1277
1278
1278 def putData(self):
1279 def putData(self):
1279 raise NotImplementedError
1280 raise NotImplementedError
1280
1281
1281 def getDtypeWidth(self):
1282 def getDtypeWidth(self):
1282
1283
1283 dtype_index = get_dtype_index(self.dtype)
1284 dtype_index = get_dtype_index(self.dtype)
1284 dtype_width = get_dtype_width(dtype_index)
1285 dtype_width = get_dtype_width(dtype_index)
1285
1286
1286 return dtype_width
1287 return dtype_width
1287
1288
1288 def getProcessFlags(self):
1289 def getProcessFlags(self):
1289
1290
1290 processFlags = 0
1291 processFlags = 0
1291
1292
1292 dtype_index = get_dtype_index(self.dtype)
1293 dtype_index = get_dtype_index(self.dtype)
1293 procflag_dtype = get_procflag_dtype(dtype_index)
1294 procflag_dtype = get_procflag_dtype(dtype_index)
1294
1295
1295 processFlags += procflag_dtype
1296 processFlags += procflag_dtype
1296
1297
1297 if self.dataOut.flagDecodeData:
1298 if self.dataOut.flagDecodeData:
1298 processFlags += PROCFLAG.DECODE_DATA
1299 processFlags += PROCFLAG.DECODE_DATA
1299
1300
1300 if self.dataOut.flagDeflipData:
1301 if self.dataOut.flagDeflipData:
1301 processFlags += PROCFLAG.DEFLIP_DATA
1302 processFlags += PROCFLAG.DEFLIP_DATA
1302
1303
1303 if self.dataOut.code is not None:
1304 if self.dataOut.code is not None:
1304 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1305 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1305
1306
1306 if self.dataOut.nCohInt > 1:
1307 if self.dataOut.nCohInt > 1:
1307 processFlags += PROCFLAG.COHERENT_INTEGRATION
1308 processFlags += PROCFLAG.COHERENT_INTEGRATION
1308
1309
1309 if self.dataOut.type == "Spectra":
1310 if self.dataOut.type == "Spectra":
1310 if self.dataOut.nIncohInt > 1:
1311 if self.dataOut.nIncohInt > 1:
1311 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1312 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1312
1313
1313 if self.dataOut.data_dc is not None:
1314 if self.dataOut.data_dc is not None:
1314 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1315 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1315
1316
1316 if self.dataOut.flagShiftFFT:
1317 if self.dataOut.flagShiftFFT:
1317 processFlags += PROCFLAG.SHIFT_FFT_DATA
1318 processFlags += PROCFLAG.SHIFT_FFT_DATA
1318
1319
1319 return processFlags
1320 return processFlags
1320
1321
1321 def setBasicHeader(self):
1322 def setBasicHeader(self):
1322
1323
1323 self.basicHeaderObj.size = self.basicHeaderSize # bytes
1324 self.basicHeaderObj.size = self.basicHeaderSize # bytes
1324 self.basicHeaderObj.version = self.versionFile
1325 self.basicHeaderObj.version = self.versionFile
1325 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1326 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1326 utc = numpy.floor(self.dataOut.utctime)
1327 utc = numpy.floor(self.dataOut.utctime)
1327 milisecond = (self.dataOut.utctime - utc) * 1000.0
1328 milisecond = (self.dataOut.utctime - utc) * 1000.0
1328 self.basicHeaderObj.utc = utc
1329 self.basicHeaderObj.utc = utc
1329 self.basicHeaderObj.miliSecond = milisecond
1330 self.basicHeaderObj.miliSecond = milisecond
1330 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1331 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1331 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1332 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1332 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1333 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1333
1334
1334 def setFirstHeader(self):
1335 def setFirstHeader(self):
1335 """
1336 """
1336 Obtiene una copia del First Header
1337 Obtiene una copia del First Header
1337
1338
1338 Affected:
1339 Affected:
1339
1340
1340 self.basicHeaderObj
1341 self.basicHeaderObj
1341 self.systemHeaderObj
1342 self.systemHeaderObj
1342 self.radarControllerHeaderObj
1343 self.radarControllerHeaderObj
1343 self.processingHeaderObj self.
1344 self.processingHeaderObj self.
1344
1345
1345 Return:
1346 Return:
1346 None
1347 None
1347 """
1348 """
1348
1349
1349 raise NotImplementedError
1350 raise NotImplementedError
1350
1351
1351 def __writeFirstHeader(self):
1352 def __writeFirstHeader(self):
1352 """
1353 """
1353 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1354 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1354
1355
1355 Affected:
1356 Affected:
1356 __dataType
1357 __dataType
1357
1358
1358 Return:
1359 Return:
1359 None
1360 None
1360 """
1361 """
1361
1362
1362 # CALCULAR PARAMETROS
1363 # CALCULAR PARAMETROS
1363
1364
1364 sizeLongHeader = self.systemHeaderObj.size + \
1365 sizeLongHeader = self.systemHeaderObj.size + \
1365 self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1366 self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1366 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1367 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1367
1368
1368 self.basicHeaderObj.write(self.fp)
1369 self.basicHeaderObj.write(self.fp)
1369 self.systemHeaderObj.write(self.fp)
1370 self.systemHeaderObj.write(self.fp)
1370 self.radarControllerHeaderObj.write(self.fp)
1371 self.radarControllerHeaderObj.write(self.fp)
1371 self.processingHeaderObj.write(self.fp)
1372 self.processingHeaderObj.write(self.fp)
1372
1373
1373 def __setNewBlock(self):
1374 def __setNewBlock(self):
1374 """
1375 """
1375 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1376 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1376
1377
1377 Return:
1378 Return:
1378 0 : si no pudo escribir nada
1379 0 : si no pudo escribir nada
1379 1 : Si escribio el Basic el First Header
1380 1 : Si escribio el Basic el First Header
1380 """
1381 """
1381 if self.fp == None:
1382 if self.fp == None:
1382 self.setNextFile()
1383 self.setNextFile()
1383
1384
1384 if self.flagIsNewFile:
1385 if self.flagIsNewFile:
1385 return 1
1386 return 1
1386
1387
1387 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1388 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1388 self.basicHeaderObj.write(self.fp)
1389 self.basicHeaderObj.write(self.fp)
1389 return 1
1390 return 1
1390
1391
1391 if not(self.setNextFile()):
1392 if not(self.setNextFile()):
1392 return 0
1393 return 0
1393
1394
1394 return 1
1395 return 1
1395
1396
1396 def writeNextBlock(self):
1397 def writeNextBlock(self):
1397 """
1398 """
1398 Selecciona el bloque siguiente de datos y los escribe en un file
1399 Selecciona el bloque siguiente de datos y los escribe en un file
1399
1400
1400 Return:
1401 Return:
1401 0 : Si no hizo pudo escribir el bloque de datos
1402 0 : Si no hizo pudo escribir el bloque de datos
1402 1 : Si no pudo escribir el bloque de datos
1403 1 : Si no pudo escribir el bloque de datos
1403 """
1404 """
1404 if not(self.__setNewBlock()):
1405 if not(self.__setNewBlock()):
1405 return 0
1406 return 0
1406
1407
1407 self.writeBlock()
1408 self.writeBlock()
1408
1409
1409 print("[Writing] Block No. %d/%d" % (self.blockIndex,
1410 print("[Writing] Block No. %d/%d" % (self.blockIndex,
1410 self.processingHeaderObj.dataBlocksPerFile))
1411 self.processingHeaderObj.dataBlocksPerFile))
1411
1412
1412 return 1
1413 return 1
1413
1414
1414 def setNextFile(self):
1415 def setNextFile(self):
1415 """Determina el siguiente file que sera escrito
1416 """Determina el siguiente file que sera escrito
1416
1417
1417 Affected:
1418 Affected:
1418 self.filename
1419 self.filename
1419 self.subfolder
1420 self.subfolder
1420 self.fp
1421 self.fp
1421 self.setFile
1422 self.setFile
1422 self.flagIsNewFile
1423 self.flagIsNewFile
1423
1424
1424 Return:
1425 Return:
1425 0 : Si el archivo no puede ser escrito
1426 0 : Si el archivo no puede ser escrito
1426 1 : Si el archivo esta listo para ser escrito
1427 1 : Si el archivo esta listo para ser escrito
1427 """
1428 """
1428 ext = self.ext
1429 ext = self.ext
1429 path = self.path
1430 path = self.path
1430
1431
1431 if self.fp != None:
1432 if self.fp != None:
1432 self.fp.close()
1433 self.fp.close()
1433
1434
1434
1435
1435 if not os.path.exists(path):
1436 if not os.path.exists(path):
1436 os.mkdir(path)
1437 os.mkdir(path)
1437
1438
1438 timeTuple = time.localtime(self.dataOut.utctime)
1439 timeTuple = time.localtime(self.dataOut.utctime)
1439 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday)
1440 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday)
1440
1441
1441 fullpath = os.path.join(path, subfolder)
1442 fullpath = os.path.join(path, subfolder)
1442 setFile = self.setFile
1443 setFile = self.setFile
1443
1444
1444 if not(os.path.exists(fullpath)):
1445 if not(os.path.exists(fullpath)):
1445 os.mkdir(fullpath)
1446 os.mkdir(fullpath)
1446 setFile = -1 # inicializo mi contador de seteo
1447 setFile = -1 # inicializo mi contador de seteo
1447 else:
1448 else:
1448 filesList = os.listdir(fullpath)
1449 filesList = os.listdir(fullpath)
1449 if len(filesList) > 0:
1450 if len(filesList) > 0:
1450 filesList = sorted(filesList, key=str.lower)
1451 filesList = sorted(filesList, key=str.lower)
1451 filen = filesList[-1]
1452 filen = filesList[-1]
1452 # el filename debera tener el siguiente formato
1453 # el filename debera tener el siguiente formato
1453 # 0 1234 567 89A BCDE (hex)
1454 # 0 1234 567 89A BCDE (hex)
1454 # x YYYY DDD SSS .ext
1455 # x YYYY DDD SSS .ext
1455 if isNumber(filen[8:11]):
1456 if isNumber(filen[8:11]):
1456 # inicializo mi contador de seteo al seteo del ultimo file
1457 # inicializo mi contador de seteo al seteo del ultimo file
1457 setFile = int(filen[8:11])
1458 setFile = int(filen[8:11])
1458 else:
1459 else:
1459 setFile = -1
1460 setFile = -1
1460 else:
1461 else:
1461 setFile = -1 # inicializo mi contador de seteo
1462 setFile = -1 # inicializo mi contador de seteo
1462
1463
1463 setFile += 1
1464 setFile += 1
1464
1465
1465 # If this is a new day it resets some values
1466 # If this is a new day it resets some values
1466 if self.dataOut.datatime.date() > self.fileDate:
1467 if self.dataOut.datatime.date() > self.fileDate:
1467 setFile = 0
1468 setFile = 0
1468 self.nTotalBlocks = 0
1469 self.nTotalBlocks = 0
1469
1470
1470 filen = '{}{:04d}{:03d}{:03d}{}'.format(
1471 filen = '{}{:04d}{:03d}{:03d}{}'.format(
1471 self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext)
1472 self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext)
1472
1473
1473 filename = os.path.join(path, subfolder, filen)
1474 filename = os.path.join(path, subfolder, filen)
1474
1475
1475 fp = open(filename, 'wb')
1476 fp = open(filename, 'wb')
1476
1477
1477 self.blockIndex = 0
1478 self.blockIndex = 0
1478 self.filename = filename
1479 self.filename = filename
1479 self.subfolder = subfolder
1480 self.subfolder = subfolder
1480 self.fp = fp
1481 self.fp = fp
1481 self.setFile = setFile
1482 self.setFile = setFile
1482 self.flagIsNewFile = 1
1483 self.flagIsNewFile = 1
1483 self.fileDate = self.dataOut.datatime.date()
1484 self.fileDate = self.dataOut.datatime.date()
1484 self.setFirstHeader()
1485 self.setFirstHeader()
1485
1486
1486 print('[Writing] Opening file: %s' % self.filename)
1487 print('[Writing] Opening file: %s' % self.filename)
1487
1488
1488 self.__writeFirstHeader()
1489 self.__writeFirstHeader()
1489
1490
1490 return 1
1491 return 1
1491
1492
1492 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1493 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1493 """
1494 """
1494 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1495 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1495
1496
1496 Inputs:
1497 Inputs:
1497 path : directory where data will be saved
1498 path : directory where data will be saved
1498 profilesPerBlock : number of profiles per block
1499 profilesPerBlock : number of profiles per block
1499 set : initial file set
1500 set : initial file set
1500 datatype : An integer number that defines data type:
1501 datatype : An integer number that defines data type:
1501 0 : int8 (1 byte)
1502 0 : int8 (1 byte)
1502 1 : int16 (2 bytes)
1503 1 : int16 (2 bytes)
1503 2 : int32 (4 bytes)
1504 2 : int32 (4 bytes)
1504 3 : int64 (8 bytes)
1505 3 : int64 (8 bytes)
1505 4 : float32 (4 bytes)
1506 4 : float32 (4 bytes)
1506 5 : double64 (8 bytes)
1507 5 : double64 (8 bytes)
1507
1508
1508 Return:
1509 Return:
1509 0 : Si no realizo un buen seteo
1510 0 : Si no realizo un buen seteo
1510 1 : Si realizo un buen seteo
1511 1 : Si realizo un buen seteo
1511 """
1512 """
1512
1513
1513 if ext == None:
1514 if ext == None:
1514 ext = self.ext
1515 ext = self.ext
1515
1516
1516 self.ext = ext.lower()
1517 self.ext = ext.lower()
1517
1518
1518 self.path = path
1519 self.path = path
1519
1520
1520 if set is None:
1521 if set is None:
1521 self.setFile = -1
1522 self.setFile = -1
1522 else:
1523 else:
1523 self.setFile = set - 1
1524 self.setFile = set - 1
1524
1525
1525 self.blocksPerFile = blocksPerFile
1526 self.blocksPerFile = blocksPerFile
1526 self.profilesPerBlock = profilesPerBlock
1527 self.profilesPerBlock = profilesPerBlock
1527 self.dataOut = dataOut
1528 self.dataOut = dataOut
1528 self.fileDate = self.dataOut.datatime.date()
1529 self.fileDate = self.dataOut.datatime.date()
1529 self.dtype = self.dataOut.dtype
1530 self.dtype = self.dataOut.dtype
1530
1531
1531 if datatype is not None:
1532 if datatype is not None:
1532 self.dtype = get_numpy_dtype(datatype)
1533 self.dtype = get_numpy_dtype(datatype)
1533
1534
1534 if not(self.setNextFile()):
1535 if not(self.setNextFile()):
1535 print("[Writing] There isn't a next file")
1536 print("[Writing] There isn't a next file")
1536 return 0
1537 return 0
1537
1538
1538 self.setBlockDimension()
1539 self.setBlockDimension()
1539
1540
1540 return 1
1541 return 1
1541
1542
1542 def run(self, dataOut, path, blocksPerFile=100, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1543 def run(self, dataOut, path, blocksPerFile=100, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1543
1544
1544 if not(self.isConfig):
1545 if not(self.isConfig):
1545
1546
1546 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock,
1547 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock,
1547 set=set, ext=ext, datatype=datatype, **kwargs)
1548 set=set, ext=ext, datatype=datatype, **kwargs)
1548 self.isConfig = True
1549 self.isConfig = True
1549
1550
1550 self.dataOut = dataOut
1551 self.dataOut = dataOut
1551 self.putData()
1552 self.putData()
1552 return self.dataOut
1553 return self.dataOut
1553
1554
1554 @MPDecorator
1555 @MPDecorator
1555 class printInfo(Operation):
1556 class printInfo(Operation):
1556
1557
1557 def __init__(self):
1558 def __init__(self):
1558
1559
1559 Operation.__init__(self)
1560 Operation.__init__(self)
1560 self.__printInfo = True
1561 self.__printInfo = True
1561
1562
1562 def run(self, dataOut, headers = ['systemHeaderObj', 'radarControllerHeaderObj', 'processingHeaderObj']):
1563 def run(self, dataOut, headers = ['systemHeaderObj', 'radarControllerHeaderObj', 'processingHeaderObj']):
1563 if self.__printInfo == False:
1564 if self.__printInfo == False:
1564 return
1565 return
1565
1566
1566 for header in headers:
1567 for header in headers:
1567 if hasattr(dataOut, header):
1568 if hasattr(dataOut, header):
1568 obj = getattr(dataOut, header)
1569 obj = getattr(dataOut, header)
1569 if hasattr(obj, 'printInfo'):
1570 if hasattr(obj, 'printInfo'):
1570 obj.printInfo()
1571 obj.printInfo()
1571 else:
1572 else:
1572 print(obj)
1573 print(obj)
1573 else:
1574 else:
1574 log.warning('Header {} Not found in object'.format(header))
1575 log.warning('Header {} Not found in object'.format(header))
1575
1576
1576 self.__printInfo = False
1577 self.__printInfo = False
@@ -1,651 +1,652
1 import os
1 import os
2 import time
2 import time
3 import datetime
3 import datetime
4
4
5 import numpy
5 import numpy
6 import h5py
6 import h5py
7
7
8 import schainpy.admin
8 import schainpy.admin
9 from schainpy.model.data.jrodata import *
9 from schainpy.model.data.jrodata import *
10 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
10 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
11 from schainpy.model.io.jroIO_base import *
11 from schainpy.model.io.jroIO_base import *
12 from schainpy.utils import log
12 from schainpy.utils import log
13
13
14
14
15 class HDFReader(Reader, ProcessingUnit):
15 class HDFReader(Reader, ProcessingUnit):
16 """Processing unit to read HDF5 format files
16 """Processing unit to read HDF5 format files
17
17
18 This unit reads HDF5 files created with `HDFWriter` operation contains
18 This unit reads HDF5 files created with `HDFWriter` operation contains
19 by default two groups Data and Metadata all variables would be saved as `dataOut`
19 by default two groups Data and Metadata all variables would be saved as `dataOut`
20 attributes.
20 attributes.
21 It is possible to read any HDF5 file by given the structure in the `description`
21 It is possible to read any HDF5 file by given the structure in the `description`
22 parameter, also you can add extra values to metadata with the parameter `extras`.
22 parameter, also you can add extra values to metadata with the parameter `extras`.
23
23
24 Parameters:
24 Parameters:
25 -----------
25 -----------
26 path : str
26 path : str
27 Path where files are located.
27 Path where files are located.
28 startDate : date
28 startDate : date
29 Start date of the files
29 Start date of the files
30 endDate : list
30 endDate : list
31 End date of the files
31 End date of the files
32 startTime : time
32 startTime : time
33 Start time of the files
33 Start time of the files
34 endTime : time
34 endTime : time
35 End time of the files
35 End time of the files
36 description : dict, optional
36 description : dict, optional
37 Dictionary with the description of the HDF5 file
37 Dictionary with the description of the HDF5 file
38 extras : dict, optional
38 extras : dict, optional
39 Dictionary with extra metadata to be be added to `dataOut`
39 Dictionary with extra metadata to be be added to `dataOut`
40
40
41 Examples
41 Examples
42 --------
42 --------
43
43
44 desc = {
44 desc = {
45 'Data': {
45 'Data': {
46 'data_output': ['u', 'v', 'w'],
46 'data_output': ['u', 'v', 'w'],
47 'utctime': 'timestamps',
47 'utctime': 'timestamps',
48 } ,
48 } ,
49 'Metadata': {
49 'Metadata': {
50 'heightList': 'heights'
50 'heightList': 'heights'
51 }
51 }
52 }
52 }
53
53
54 desc = {
54 desc = {
55 'Data': {
55 'Data': {
56 'data_output': 'winds',
56 'data_output': 'winds',
57 'utctime': 'timestamps'
57 'utctime': 'timestamps'
58 },
58 },
59 'Metadata': {
59 'Metadata': {
60 'heightList': 'heights'
60 'heightList': 'heights'
61 }
61 }
62 }
62 }
63
63
64 extras = {
64 extras = {
65 'timeZone': 300
65 'timeZone': 300
66 }
66 }
67
67
68 reader = project.addReadUnit(
68 reader = project.addReadUnit(
69 name='HDFReader',
69 name='HDFReader',
70 path='/path/to/files',
70 path='/path/to/files',
71 startDate='2019/01/01',
71 startDate='2019/01/01',
72 endDate='2019/01/31',
72 endDate='2019/01/31',
73 startTime='00:00:00',
73 startTime='00:00:00',
74 endTime='23:59:59',
74 endTime='23:59:59',
75 # description=json.dumps(desc),
75 # description=json.dumps(desc),
76 # extras=json.dumps(extras),
76 # extras=json.dumps(extras),
77 )
77 )
78
78
79 """
79 """
80
80
81 __attrs__ = ['path', 'startDate', 'endDate', 'startTime', 'endTime', 'description', 'extras']
81 __attrs__ = ['path', 'startDate', 'endDate', 'startTime', 'endTime', 'description', 'extras']
82
82
83 def __init__(self):
83 def __init__(self):
84 ProcessingUnit.__init__(self)
84 ProcessingUnit.__init__(self)
85 self.dataOut = Parameters()
85 self.dataOut = Parameters()
86 self.ext = ".hdf5"
86 self.ext = ".hdf5"
87 self.optchar = "D"
87 self.optchar = "D"
88 self.meta = {}
88 self.meta = {}
89 self.data = {}
89 self.data = {}
90 self.open_file = h5py.File
90 self.open_file = h5py.File
91 self.open_mode = 'r'
91 self.open_mode = 'r'
92 self.description = {}
92 self.description = {}
93 self.extras = {}
93 self.extras = {}
94 self.filefmt = "*%Y%j***"
94 self.filefmt = "*%Y%j***"
95 self.folderfmt = "*%Y%j"
95 self.folderfmt = "*%Y%j"
96 self.utcoffset = 0
96 self.utcoffset = 0
97
97
98 def setup(self, **kwargs):
98 def setup(self, **kwargs):
99
99
100 self.set_kwargs(**kwargs)
100 self.set_kwargs(**kwargs)
101 if not self.ext.startswith('.'):
101 if not self.ext.startswith('.'):
102 self.ext = '.{}'.format(self.ext)
102 self.ext = '.{}'.format(self.ext)
103
103
104 if self.online:
104 if self.online:
105 log.log("Searching files in online mode...", self.name)
105 log.log("Searching files in online mode...", self.name)
106
106
107 for nTries in range(self.nTries):
107 for nTries in range(self.nTries):
108 fullpath = self.searchFilesOnLine(self.path, self.startDate,
108 fullpath = self.searchFilesOnLine(self.path, self.startDate,
109 self.endDate, self.expLabel, self.ext, self.walk,
109 self.endDate, self.expLabel, self.ext, self.walk,
110 self.filefmt, self.folderfmt)
110 self.filefmt, self.folderfmt)
111 pathname, filename = os.path.split(fullpath)
111 pathname, filename = os.path.split(fullpath)
112 #print(pathname,filename)
112 #print(pathname,filename)
113 try:
113 try:
114 fullpath = next(fullpath)
114 fullpath = next(fullpath)
115
115
116 except:
116 except:
117 fullpath = None
117 fullpath = None
118
118
119 if fullpath:
119 if fullpath:
120 break
120 break
121
121
122 log.warning(
122 log.warning(
123 'Waiting {} sec for a valid file in {}: try {} ...'.format(
123 'Waiting {} sec for a valid file in {}: try {} ...'.format(
124 self.delay, self.path, nTries + 1),
124 self.delay, self.path, nTries + 1),
125 self.name)
125 self.name)
126 time.sleep(self.delay)
126 time.sleep(self.delay)
127
127
128 if not(fullpath):
128 if not(fullpath):
129 raise schainpy.admin.SchainError(
129 raise schainpy.admin.SchainError(
130 'There isn\'t any valid file in {}'.format(self.path))
130 'There isn\'t any valid file in {}'.format(self.path))
131
131
132 pathname, filename = os.path.split(fullpath)
132 pathname, filename = os.path.split(fullpath)
133 self.year = int(filename[1:5])
133 self.year = int(filename[1:5])
134 self.doy = int(filename[5:8])
134 self.doy = int(filename[5:8])
135 self.set = int(filename[8:11]) - 1
135 self.set = int(filename[8:11]) - 1
136 else:
136 else:
137 log.log("Searching files in {}".format(self.path), self.name)
137 log.log("Searching files in {}".format(self.path), self.name)
138 self.filenameList = self.searchFilesOffLine(self.path, self.startDate,
138 self.filenameList = self.searchFilesOffLine(self.path, self.startDate,
139 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
139 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
140
140
141 self.setNextFile()
141 self.setNextFile()
142
142
143 return
143 return
144
144
145
145
146 def readFirstHeader(self):
146 def readFirstHeader(self):
147 '''Read metadata and data'''
147 '''Read metadata and data'''
148
148
149 self.__readMetadata()
149 self.__readMetadata()
150 self.__readData()
150 self.__readData()
151 self.__setBlockList()
151 self.__setBlockList()
152
152
153 if 'type' in self.meta:
153 if 'type' in self.meta:
154 self.dataOut = eval(self.meta['type'])()
154 self.dataOut = eval(self.meta['type'])()
155
155
156 for attr in self.meta:
156 for attr in self.meta:
157 print("attr: ", attr)
157 #print("attr: ", attr)
158 setattr(self.dataOut, attr, self.meta[attr])
158 setattr(self.dataOut, attr, self.meta[attr])
159
159
160
160
161 self.blockIndex = 0
161 self.blockIndex = 0
162
162
163 return
163 return
164
164
165 def __setBlockList(self):
165 def __setBlockList(self):
166 '''
166 '''
167 Selects the data within the times defined
167 Selects the data within the times defined
168
168
169 self.fp
169 self.fp
170 self.startTime
170 self.startTime
171 self.endTime
171 self.endTime
172 self.blockList
172 self.blockList
173 self.blocksPerFile
173 self.blocksPerFile
174
174
175 '''
175 '''
176
176
177 startTime = self.startTime
177 startTime = self.startTime
178 endTime = self.endTime
178 endTime = self.endTime
179 thisUtcTime = self.data['utctime'] + self.utcoffset
179 thisUtcTime = self.data['utctime'] + self.utcoffset
180 self.interval = numpy.min(thisUtcTime[1:] - thisUtcTime[:-1])
180 self.interval = numpy.min(thisUtcTime[1:] - thisUtcTime[:-1])
181 thisDatetime = datetime.datetime.utcfromtimestamp(thisUtcTime[0])
181 thisDatetime = datetime.datetime.utcfromtimestamp(thisUtcTime[0])
182 self.startFileDatetime = thisDatetime
182 self.startFileDatetime = thisDatetime
183 thisDate = thisDatetime.date()
183 thisDate = thisDatetime.date()
184 thisTime = thisDatetime.time()
184 thisTime = thisDatetime.time()
185
185
186 startUtcTime = (datetime.datetime.combine(thisDate, startTime) - datetime.datetime(1970, 1, 1)).total_seconds()
186 startUtcTime = (datetime.datetime.combine(thisDate, startTime) - datetime.datetime(1970, 1, 1)).total_seconds()
187 endUtcTime = (datetime.datetime.combine(thisDate, endTime) - datetime.datetime(1970, 1, 1)).total_seconds()
187 endUtcTime = (datetime.datetime.combine(thisDate, endTime) - datetime.datetime(1970, 1, 1)).total_seconds()
188
188
189 ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0]
189 ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0]
190
190
191 self.blockList = ind
191 self.blockList = ind
192 self.blocksPerFile = len(ind)
192 self.blocksPerFile = len(ind)
193 self.blocksPerFile = len(thisUtcTime)
193 self.blocksPerFile = len(thisUtcTime)
194 return
194 return
195
195
196 def __readMetadata(self):
196 def __readMetadata(self):
197 '''
197 '''
198 Reads Metadata
198 Reads Metadata
199 '''
199 '''
200
200
201 meta = {}
201 meta = {}
202
202
203 if self.description:
203 if self.description:
204 for key, value in self.description['Metadata'].items():
204 for key, value in self.description['Metadata'].items():
205 meta[key] = self.fp[value][()]
205 meta[key] = self.fp[value][()]
206 else:
206 else:
207 grp = self.fp['Metadata']
207 grp = self.fp['Metadata']
208 for name in grp:
208 for name in grp:
209 meta[name] = grp[name][()]
209 meta[name] = grp[name][()]
210
210
211 if self.extras:
211 if self.extras:
212 for key, value in self.extras.items():
212 for key, value in self.extras.items():
213 meta[key] = value
213 meta[key] = value
214 self.meta = meta
214 self.meta = meta
215
215
216 return
216 return
217
217
218
218
219
219
220 def checkForRealPath(self, nextFile, nextDay):
220 def checkForRealPath(self, nextFile, nextDay):
221
221
222 # print("check FRP")
222 # print("check FRP")
223 # dt = self.startFileDatetime + datetime.timedelta(1)
223 # dt = self.startFileDatetime + datetime.timedelta(1)
224 # filename = '{}.{}{}'.format(self.path, dt.strftime('%Y%m%d'), self.ext)
224 # filename = '{}.{}{}'.format(self.path, dt.strftime('%Y%m%d'), self.ext)
225 # fullfilename = os.path.join(self.path, filename)
225 # fullfilename = os.path.join(self.path, filename)
226 # print("check Path ",fullfilename,filename)
226 # print("check Path ",fullfilename,filename)
227 # if os.path.exists(fullfilename):
227 # if os.path.exists(fullfilename):
228 # return fullfilename, filename
228 # return fullfilename, filename
229 # return None, filename
229 # return None, filename
230 return None,None
230 return None,None
231
231
232 def __readData(self):
232 def __readData(self):
233
233
234 data = {}
234 data = {}
235
235
236 if self.description:
236 if self.description:
237 for key, value in self.description['Data'].items():
237 for key, value in self.description['Data'].items():
238 if isinstance(value, str):
238 if isinstance(value, str):
239 if isinstance(self.fp[value], h5py.Dataset):
239 if isinstance(self.fp[value], h5py.Dataset):
240 data[key] = self.fp[value][()]
240 data[key] = self.fp[value][()]
241 elif isinstance(self.fp[value], h5py.Group):
241 elif isinstance(self.fp[value], h5py.Group):
242 array = []
242 array = []
243 for ch in self.fp[value]:
243 for ch in self.fp[value]:
244 array.append(self.fp[value][ch][()])
244 array.append(self.fp[value][ch][()])
245 data[key] = numpy.array(array)
245 data[key] = numpy.array(array)
246 elif isinstance(value, list):
246 elif isinstance(value, list):
247 array = []
247 array = []
248 for ch in value:
248 for ch in value:
249 array.append(self.fp[ch][()])
249 array.append(self.fp[ch][()])
250 data[key] = numpy.array(array)
250 data[key] = numpy.array(array)
251 else:
251 else:
252 grp = self.fp['Data']
252 grp = self.fp['Data']
253 for name in grp:
253 for name in grp:
254 if isinstance(grp[name], h5py.Dataset):
254 if isinstance(grp[name], h5py.Dataset):
255 array = grp[name][()]
255 array = grp[name][()]
256 elif isinstance(grp[name], h5py.Group):
256 elif isinstance(grp[name], h5py.Group):
257 array = []
257 array = []
258 for ch in grp[name]:
258 for ch in grp[name]:
259 array.append(grp[name][ch][()])
259 array.append(grp[name][ch][()])
260 array = numpy.array(array)
260 array = numpy.array(array)
261 else:
261 else:
262 log.warning('Unknown type: {}'.format(name))
262 log.warning('Unknown type: {}'.format(name))
263
263
264 if name in self.description:
264 if name in self.description:
265 key = self.description[name]
265 key = self.description[name]
266 else:
266 else:
267 key = name
267 key = name
268 data[key] = array
268 data[key] = array
269
269
270 self.data = data
270 self.data = data
271 return
271 return
272
272
273 def getData(self):
273 def getData(self):
274 if not self.isDateTimeInRange(self.startFileDatetime, self.startDate, self.endDate, self.startTime, self.endTime):
274 if not self.isDateTimeInRange(self.startFileDatetime, self.startDate, self.endDate, self.startTime, self.endTime):
275 self.dataOut.flagNoData = True
275 self.dataOut.flagNoData = True
276 self.dataOut.error = True
276 self.blockIndex = self.blocksPerFile
277 #self.dataOut.error = True TERMINA EL PROGRAMA, removido
277 return
278 return
278 for attr in self.data:
279 for attr in self.data:
279 if self.data[attr].ndim == 1:
280 if self.data[attr].ndim == 1:
280 setattr(self.dataOut, attr, self.data[attr][self.blockIndex])
281 setattr(self.dataOut, attr, self.data[attr][self.blockIndex])
281 else:
282 else:
282 setattr(self.dataOut, attr, self.data[attr][:, self.blockIndex])
283 setattr(self.dataOut, attr, self.data[attr][:, self.blockIndex])
283
284
284 self.dataOut.flagNoData = False
285 self.dataOut.flagNoData = False
285 self.blockIndex += 1
286 self.blockIndex += 1
286
287
287 log.log("Block No. {}/{} -> {}".format(
288 log.log("Block No. {}/{} -> {}".format(
288 self.blockIndex,
289 self.blockIndex,
289 self.blocksPerFile,
290 self.blocksPerFile,
290 self.dataOut.datatime.ctime()), self.name)
291 self.dataOut.datatime.ctime()), self.name)
291
292
292 return
293 return
293
294
294 def run(self, **kwargs):
295 def run(self, **kwargs):
295
296
296 if not(self.isConfig):
297 if not(self.isConfig):
297 self.setup(**kwargs)
298 self.setup(**kwargs)
298 self.isConfig = True
299 self.isConfig = True
299
300
300 if self.blockIndex == self.blocksPerFile:
301 if self.blockIndex == self.blocksPerFile:
301 self.setNextFile()
302 self.setNextFile()
302
303
303 self.getData()
304 self.getData()
304
305
305 return
306 return
306
307
307 @MPDecorator
308 @MPDecorator
308 class HDFWriter(Operation):
309 class HDFWriter(Operation):
309 """Operation to write HDF5 files.
310 """Operation to write HDF5 files.
310
311
311 The HDF5 file contains by default two groups Data and Metadata where
312 The HDF5 file contains by default two groups Data and Metadata where
312 you can save any `dataOut` attribute specified by `dataList` and `metadataList`
313 you can save any `dataOut` attribute specified by `dataList` and `metadataList`
313 parameters, data attributes are normaly time dependent where the metadata
314 parameters, data attributes are normaly time dependent where the metadata
314 are not.
315 are not.
315 It is possible to customize the structure of the HDF5 file with the
316 It is possible to customize the structure of the HDF5 file with the
316 optional description parameter see the examples.
317 optional description parameter see the examples.
317
318
318 Parameters:
319 Parameters:
319 -----------
320 -----------
320 path : str
321 path : str
321 Path where files will be saved.
322 Path where files will be saved.
322 blocksPerFile : int
323 blocksPerFile : int
323 Number of blocks per file
324 Number of blocks per file
324 metadataList : list
325 metadataList : list
325 List of the dataOut attributes that will be saved as metadata
326 List of the dataOut attributes that will be saved as metadata
326 dataList : int
327 dataList : int
327 List of the dataOut attributes that will be saved as data
328 List of the dataOut attributes that will be saved as data
328 setType : bool
329 setType : bool
329 If True the name of the files corresponds to the timestamp of the data
330 If True the name of the files corresponds to the timestamp of the data
330 description : dict, optional
331 description : dict, optional
331 Dictionary with the desired description of the HDF5 file
332 Dictionary with the desired description of the HDF5 file
332
333
333 Examples
334 Examples
334 --------
335 --------
335
336
336 desc = {
337 desc = {
337 'data_output': {'winds': ['z', 'w', 'v']},
338 'data_output': {'winds': ['z', 'w', 'v']},
338 'utctime': 'timestamps',
339 'utctime': 'timestamps',
339 'heightList': 'heights'
340 'heightList': 'heights'
340 }
341 }
341 desc = {
342 desc = {
342 'data_output': ['z', 'w', 'v'],
343 'data_output': ['z', 'w', 'v'],
343 'utctime': 'timestamps',
344 'utctime': 'timestamps',
344 'heightList': 'heights'
345 'heightList': 'heights'
345 }
346 }
346 desc = {
347 desc = {
347 'Data': {
348 'Data': {
348 'data_output': 'winds',
349 'data_output': 'winds',
349 'utctime': 'timestamps'
350 'utctime': 'timestamps'
350 },
351 },
351 'Metadata': {
352 'Metadata': {
352 'heightList': 'heights'
353 'heightList': 'heights'
353 }
354 }
354 }
355 }
355
356
356 writer = proc_unit.addOperation(name='HDFWriter')
357 writer = proc_unit.addOperation(name='HDFWriter')
357 writer.addParameter(name='path', value='/path/to/file')
358 writer.addParameter(name='path', value='/path/to/file')
358 writer.addParameter(name='blocksPerFile', value='32')
359 writer.addParameter(name='blocksPerFile', value='32')
359 writer.addParameter(name='metadataList', value='heightList,timeZone')
360 writer.addParameter(name='metadataList', value='heightList,timeZone')
360 writer.addParameter(name='dataList',value='data_output,utctime')
361 writer.addParameter(name='dataList',value='data_output,utctime')
361 # writer.addParameter(name='description',value=json.dumps(desc))
362 # writer.addParameter(name='description',value=json.dumps(desc))
362
363
363 """
364 """
364
365
365 ext = ".hdf5"
366 ext = ".hdf5"
366 optchar = "D"
367 optchar = "D"
367 filename = None
368 filename = None
368 path = None
369 path = None
369 setFile = None
370 setFile = None
370 fp = None
371 fp = None
371 firsttime = True
372 firsttime = True
372 #Configurations
373 #Configurations
373 blocksPerFile = None
374 blocksPerFile = None
374 blockIndex = None
375 blockIndex = None
375 dataOut = None
376 dataOut = None
376 #Data Arrays
377 #Data Arrays
377 dataList = None
378 dataList = None
378 metadataList = None
379 metadataList = None
379 currentDay = None
380 currentDay = None
380 lastTime = None
381 lastTime = None
381
382
382 def __init__(self):
383 def __init__(self):
383
384
384 Operation.__init__(self)
385 Operation.__init__(self)
385 return
386 return
386
387
387 def setup(self, path=None, blocksPerFile=10, metadataList=None, dataList=None, setType=None, description=None):
388 def setup(self, path=None, blocksPerFile=10, metadataList=None, dataList=None, setType=None, description=None):
388 self.path = path
389 self.path = path
389 self.blocksPerFile = blocksPerFile
390 self.blocksPerFile = blocksPerFile
390 self.metadataList = metadataList
391 self.metadataList = metadataList
391 self.dataList = [s.strip() for s in dataList]
392 self.dataList = [s.strip() for s in dataList]
392 self.setType = setType
393 self.setType = setType
393 self.description = description
394 self.description = description
394
395
395 if self.metadataList is None:
396 if self.metadataList is None:
396 self.metadataList = self.dataOut.metadata_list
397 self.metadataList = self.dataOut.metadata_list
397
398
398 tableList = []
399 tableList = []
399 dsList = []
400 dsList = []
400
401
401 for i in range(len(self.dataList)):
402 for i in range(len(self.dataList)):
402 dsDict = {}
403 dsDict = {}
403 if hasattr(self.dataOut, self.dataList[i]):
404 if hasattr(self.dataOut, self.dataList[i]):
404 dataAux = getattr(self.dataOut, self.dataList[i])
405 dataAux = getattr(self.dataOut, self.dataList[i])
405 dsDict['variable'] = self.dataList[i]
406 dsDict['variable'] = self.dataList[i]
406 else:
407 else:
407 log.warning('Attribute {} not found in dataOut', self.name)
408 log.warning('Attribute {} not found in dataOut', self.name)
408 continue
409 continue
409
410
410 if dataAux is None:
411 if dataAux is None:
411 continue
412 continue
412 elif isinstance(dataAux, (int, float, numpy.integer, numpy.float)):
413 elif isinstance(dataAux, (int, float, numpy.integer, numpy.float)):
413 dsDict['nDim'] = 0
414 dsDict['nDim'] = 0
414 else:
415 else:
415 dsDict['nDim'] = len(dataAux.shape)
416 dsDict['nDim'] = len(dataAux.shape)
416 dsDict['shape'] = dataAux.shape
417 dsDict['shape'] = dataAux.shape
417 dsDict['dsNumber'] = dataAux.shape[0]
418 dsDict['dsNumber'] = dataAux.shape[0]
418 dsDict['dtype'] = dataAux.dtype
419 dsDict['dtype'] = dataAux.dtype
419
420
420 dsList.append(dsDict)
421 dsList.append(dsDict)
421
422
422 self.dsList = dsList
423 self.dsList = dsList
423 self.currentDay = self.dataOut.datatime.date()
424 self.currentDay = self.dataOut.datatime.date()
424
425
425 def timeFlag(self):
426 def timeFlag(self):
426 currentTime = self.dataOut.utctime
427 currentTime = self.dataOut.utctime
427 timeTuple = time.localtime(currentTime)
428 timeTuple = time.localtime(currentTime)
428 dataDay = timeTuple.tm_yday
429 dataDay = timeTuple.tm_yday
429 #print("time UTC: ",currentTime, self.dataOut.datatime)
430 #print("time UTC: ",currentTime, self.dataOut.datatime)
430 if self.lastTime is None:
431 if self.lastTime is None:
431 self.lastTime = currentTime
432 self.lastTime = currentTime
432 self.currentDay = dataDay
433 self.currentDay = dataDay
433 return False
434 return False
434
435
435 timeDiff = currentTime - self.lastTime
436 timeDiff = currentTime - self.lastTime
436
437
437 #Si el dia es diferente o si la diferencia entre un dato y otro supera la hora
438 #Si el dia es diferente o si la diferencia entre un dato y otro supera la hora
438 if dataDay != self.currentDay:
439 if dataDay != self.currentDay:
439 self.currentDay = dataDay
440 self.currentDay = dataDay
440 return True
441 return True
441 elif timeDiff > 3*60*60:
442 elif timeDiff > 3*60*60:
442 self.lastTime = currentTime
443 self.lastTime = currentTime
443 return True
444 return True
444 else:
445 else:
445 self.lastTime = currentTime
446 self.lastTime = currentTime
446 return False
447 return False
447
448
448 def run(self, dataOut, path, blocksPerFile=10, metadataList=None,
449 def run(self, dataOut, path, blocksPerFile=10, metadataList=None,
449 dataList=[], setType=None, description={}):
450 dataList=[], setType=None, description={}):
450
451
451 self.dataOut = dataOut
452 self.dataOut = dataOut
452 if not(self.isConfig):
453 if not(self.isConfig):
453 self.setup(path=path, blocksPerFile=blocksPerFile,
454 self.setup(path=path, blocksPerFile=blocksPerFile,
454 metadataList=metadataList, dataList=dataList,
455 metadataList=metadataList, dataList=dataList,
455 setType=setType, description=description)
456 setType=setType, description=description)
456
457
457 self.isConfig = True
458 self.isConfig = True
458 self.setNextFile()
459 self.setNextFile()
459
460
460 self.putData()
461 self.putData()
461 return
462 return
462
463
463 def setNextFile(self):
464 def setNextFile(self):
464
465
465 ext = self.ext
466 ext = self.ext
466 path = self.path
467 path = self.path
467 setFile = self.setFile
468 setFile = self.setFile
468
469
469 timeTuple = time.gmtime(self.dataOut.utctime)
470 timeTuple = time.gmtime(self.dataOut.utctime)
470 #print("path: ",timeTuple)
471 #print("path: ",timeTuple)
471 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
472 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
472 fullpath = os.path.join(path, subfolder)
473 fullpath = os.path.join(path, subfolder)
473
474
474 if os.path.exists(fullpath):
475 if os.path.exists(fullpath):
475 filesList = os.listdir(fullpath)
476 filesList = os.listdir(fullpath)
476 filesList = [k for k in filesList if k.startswith(self.optchar)]
477 filesList = [k for k in filesList if k.startswith(self.optchar)]
477 if len( filesList ) > 0:
478 if len( filesList ) > 0:
478 filesList = sorted(filesList, key=str.lower)
479 filesList = sorted(filesList, key=str.lower)
479 filen = filesList[-1]
480 filen = filesList[-1]
480 # el filename debera tener el siguiente formato
481 # el filename debera tener el siguiente formato
481 # 0 1234 567 89A BCDE (hex)
482 # 0 1234 567 89A BCDE (hex)
482 # x YYYY DDD SSS .ext
483 # x YYYY DDD SSS .ext
483 if isNumber(filen[8:11]):
484 if isNumber(filen[8:11]):
484 setFile = int(filen[8:11]) #inicializo mi contador de seteo al seteo del ultimo file
485 setFile = int(filen[8:11]) #inicializo mi contador de seteo al seteo del ultimo file
485 else:
486 else:
486 setFile = -1
487 setFile = -1
487 else:
488 else:
488 setFile = -1 #inicializo mi contador de seteo
489 setFile = -1 #inicializo mi contador de seteo
489 else:
490 else:
490 os.makedirs(fullpath)
491 os.makedirs(fullpath)
491 setFile = -1 #inicializo mi contador de seteo
492 setFile = -1 #inicializo mi contador de seteo
492
493
493 if self.setType is None:
494 if self.setType is None:
494 setFile += 1
495 setFile += 1
495 file = '%s%4.4d%3.3d%03d%s' % (self.optchar,
496 file = '%s%4.4d%3.3d%03d%s' % (self.optchar,
496 timeTuple.tm_year,
497 timeTuple.tm_year,
497 timeTuple.tm_yday,
498 timeTuple.tm_yday,
498 setFile,
499 setFile,
499 ext )
500 ext )
500 else:
501 else:
501 setFile = timeTuple.tm_hour*60+timeTuple.tm_min
502 setFile = timeTuple.tm_hour*60+timeTuple.tm_min
502 file = '%s%4.4d%3.3d%04d%s' % (self.optchar,
503 file = '%s%4.4d%3.3d%04d%s' % (self.optchar,
503 timeTuple.tm_year,
504 timeTuple.tm_year,
504 timeTuple.tm_yday,
505 timeTuple.tm_yday,
505 setFile,
506 setFile,
506 ext )
507 ext )
507
508
508 self.filename = os.path.join( path, subfolder, file )
509 self.filename = os.path.join( path, subfolder, file )
509
510
510 #Setting HDF5 File
511 #Setting HDF5 File
511 self.fp = h5py.File(self.filename, 'w')
512 self.fp = h5py.File(self.filename, 'w')
512 #write metadata
513 #write metadata
513 self.writeMetadata(self.fp)
514 self.writeMetadata(self.fp)
514 #Write data
515 #Write data
515 self.writeData(self.fp)
516 self.writeData(self.fp)
516
517
517 def getLabel(self, name, x=None):
518 def getLabel(self, name, x=None):
518
519
519 if x is None:
520 if x is None:
520 if 'Data' in self.description:
521 if 'Data' in self.description:
521 data = self.description['Data']
522 data = self.description['Data']
522 if 'Metadata' in self.description:
523 if 'Metadata' in self.description:
523 data.update(self.description['Metadata'])
524 data.update(self.description['Metadata'])
524 else:
525 else:
525 data = self.description
526 data = self.description
526 if name in data:
527 if name in data:
527 if isinstance(data[name], str):
528 if isinstance(data[name], str):
528 return data[name]
529 return data[name]
529 elif isinstance(data[name], list):
530 elif isinstance(data[name], list):
530 return None
531 return None
531 elif isinstance(data[name], dict):
532 elif isinstance(data[name], dict):
532 for key, value in data[name].items():
533 for key, value in data[name].items():
533 return key
534 return key
534 return name
535 return name
535 else:
536 else:
536 if 'Metadata' in self.description:
537 if 'Metadata' in self.description:
537 meta = self.description['Metadata']
538 meta = self.description['Metadata']
538 else:
539 else:
539 meta = self.description
540 meta = self.description
540 if name in meta:
541 if name in meta:
541 if isinstance(meta[name], list):
542 if isinstance(meta[name], list):
542 return meta[name][x]
543 return meta[name][x]
543 elif isinstance(meta[name], dict):
544 elif isinstance(meta[name], dict):
544 for key, value in meta[name].items():
545 for key, value in meta[name].items():
545 return value[x]
546 return value[x]
546 if 'cspc' in name:
547 if 'cspc' in name:
547 return 'pair{:02d}'.format(x)
548 return 'pair{:02d}'.format(x)
548 else:
549 else:
549 return 'channel{:02d}'.format(x)
550 return 'channel{:02d}'.format(x)
550
551
551 def writeMetadata(self, fp):
552 def writeMetadata(self, fp):
552
553
553 if self.description:
554 if self.description:
554 if 'Metadata' in self.description:
555 if 'Metadata' in self.description:
555 grp = fp.create_group('Metadata')
556 grp = fp.create_group('Metadata')
556 else:
557 else:
557 grp = fp
558 grp = fp
558 else:
559 else:
559 grp = fp.create_group('Metadata')
560 grp = fp.create_group('Metadata')
560
561
561 for i in range(len(self.metadataList)):
562 for i in range(len(self.metadataList)):
562 if not hasattr(self.dataOut, self.metadataList[i]):
563 if not hasattr(self.dataOut, self.metadataList[i]):
563 log.warning('Metadata: `{}` not found'.format(self.metadataList[i]), self.name)
564 log.warning('Metadata: `{}` not found'.format(self.metadataList[i]), self.name)
564 continue
565 continue
565 value = getattr(self.dataOut, self.metadataList[i])
566 value = getattr(self.dataOut, self.metadataList[i])
566 if isinstance(value, bool):
567 if isinstance(value, bool):
567 if value is True:
568 if value is True:
568 value = 1
569 value = 1
569 else:
570 else:
570 value = 0
571 value = 0
571 grp.create_dataset(self.getLabel(self.metadataList[i]), data=value)
572 grp.create_dataset(self.getLabel(self.metadataList[i]), data=value)
572 return
573 return
573
574
574 def writeData(self, fp):
575 def writeData(self, fp):
575
576
576 if self.description:
577 if self.description:
577 if 'Data' in self.description:
578 if 'Data' in self.description:
578 grp = fp.create_group('Data')
579 grp = fp.create_group('Data')
579 else:
580 else:
580 grp = fp
581 grp = fp
581 else:
582 else:
582 grp = fp.create_group('Data')
583 grp = fp.create_group('Data')
583
584
584 dtsets = []
585 dtsets = []
585 data = []
586 data = []
586
587
587 for dsInfo in self.dsList:
588 for dsInfo in self.dsList:
588 if dsInfo['nDim'] == 0:
589 if dsInfo['nDim'] == 0:
589 ds = grp.create_dataset(
590 ds = grp.create_dataset(
590 self.getLabel(dsInfo['variable']),
591 self.getLabel(dsInfo['variable']),
591 (self.blocksPerFile, ),
592 (self.blocksPerFile, ),
592 chunks=True,
593 chunks=True,
593 dtype=numpy.float64)
594 dtype=numpy.float64)
594 dtsets.append(ds)
595 dtsets.append(ds)
595 data.append((dsInfo['variable'], -1))
596 data.append((dsInfo['variable'], -1))
596 else:
597 else:
597 label = self.getLabel(dsInfo['variable'])
598 label = self.getLabel(dsInfo['variable'])
598 if label is not None:
599 if label is not None:
599 sgrp = grp.create_group(label)
600 sgrp = grp.create_group(label)
600 else:
601 else:
601 sgrp = grp
602 sgrp = grp
602 for i in range(dsInfo['dsNumber']):
603 for i in range(dsInfo['dsNumber']):
603 ds = sgrp.create_dataset(
604 ds = sgrp.create_dataset(
604 self.getLabel(dsInfo['variable'], i),
605 self.getLabel(dsInfo['variable'], i),
605 (self.blocksPerFile, ) + dsInfo['shape'][1:],
606 (self.blocksPerFile, ) + dsInfo['shape'][1:],
606 chunks=True,
607 chunks=True,
607 dtype=dsInfo['dtype'])
608 dtype=dsInfo['dtype'])
608 dtsets.append(ds)
609 dtsets.append(ds)
609 data.append((dsInfo['variable'], i))
610 data.append((dsInfo['variable'], i))
610 fp.flush()
611 fp.flush()
611
612
612 log.log('Creating file: {}'.format(fp.filename), self.name)
613 log.log('Creating file: {}'.format(fp.filename), self.name)
613
614
614 self.ds = dtsets
615 self.ds = dtsets
615 self.data = data
616 self.data = data
616 self.firsttime = True
617 self.firsttime = True
617 self.blockIndex = 0
618 self.blockIndex = 0
618 return
619 return
619
620
620 def putData(self):
621 def putData(self):
621
622
622 if (self.blockIndex == self.blocksPerFile) or self.timeFlag():
623 if (self.blockIndex == self.blocksPerFile) or self.timeFlag():
623 self.closeFile()
624 self.closeFile()
624 self.setNextFile()
625 self.setNextFile()
625
626
626 for i, ds in enumerate(self.ds):
627 for i, ds in enumerate(self.ds):
627 attr, ch = self.data[i]
628 attr, ch = self.data[i]
628 if ch == -1:
629 if ch == -1:
629 ds[self.blockIndex] = getattr(self.dataOut, attr)
630 ds[self.blockIndex] = getattr(self.dataOut, attr)
630 else:
631 else:
631 ds[self.blockIndex] = getattr(self.dataOut, attr)[ch]
632 ds[self.blockIndex] = getattr(self.dataOut, attr)[ch]
632
633
633 self.fp.flush()
634 self.fp.flush()
634 self.blockIndex += 1
635 self.blockIndex += 1
635 log.log('Block No. {}/{}'.format(self.blockIndex, self.blocksPerFile), self.name)
636 log.log('Block No. {}/{}'.format(self.blockIndex, self.blocksPerFile), self.name)
636
637
637 return
638 return
638
639
639 def closeFile(self):
640 def closeFile(self):
640
641
641 if self.blockIndex != self.blocksPerFile:
642 if self.blockIndex != self.blocksPerFile:
642 for ds in self.ds:
643 for ds in self.ds:
643 ds.resize(self.blockIndex, axis=0)
644 ds.resize(self.blockIndex, axis=0)
644
645
645 if self.fp:
646 if self.fp:
646 self.fp.flush()
647 self.fp.flush()
647 self.fp.close()
648 self.fp.close()
648
649
649 def close(self):
650 def close(self):
650
651
651 self.closeFile()
652 self.closeFile()
@@ -1,1357 +1,1683
1 # Copyright (c) 2012-2020 Jicamarca Radio Observatory
1 # Copyright (c) 2012-2020 Jicamarca Radio Observatory
2 # All rights reserved.
2 # All rights reserved.
3 #
3 #
4 # Distributed under the terms of the BSD 3-clause license.
4 # Distributed under the terms of the BSD 3-clause license.
5 """Spectra processing Unit and operations
5 """Spectra processing Unit and operations
6
6
7 Here you will find the processing unit `SpectraProc` and several operations
7 Here you will find the processing unit `SpectraProc` and several operations
8 to work with Spectra data type
8 to work with Spectra data type
9 """
9 """
10
10
11 import time
11 import time
12 import itertools
12 import itertools
13
13
14 import numpy
14 import numpy
15 import math
15 import math
16
16
17 from schainpy.model.proc.jroproc_base import ProcessingUnit, MPDecorator, Operation
17 from schainpy.model.proc.jroproc_base import ProcessingUnit, MPDecorator, Operation
18 from schainpy.model.data.jrodata import Spectra
18 from schainpy.model.data.jrodata import Spectra
19 from schainpy.model.data.jrodata import hildebrand_sekhon
19 from schainpy.model.data.jrodata import hildebrand_sekhon
20 from schainpy.utils import log
20 from schainpy.utils import log
21
21
22 from scipy.optimize import curve_fit
22 from scipy.optimize import curve_fit
23
23
24
24
25 class SpectraProc(ProcessingUnit):
25 class SpectraProc(ProcessingUnit):
26
26
27 def __init__(self):
27 def __init__(self):
28
28
29 ProcessingUnit.__init__(self)
29 ProcessingUnit.__init__(self)
30
30
31 self.buffer = None
31 self.buffer = None
32 self.firstdatatime = None
32 self.firstdatatime = None
33 self.profIndex = 0
33 self.profIndex = 0
34 self.dataOut = Spectra()
34 self.dataOut = Spectra()
35 self.id_min = None
35 self.id_min = None
36 self.id_max = None
36 self.id_max = None
37 self.setupReq = False #Agregar a todas las unidades de proc
37 self.setupReq = False #Agregar a todas las unidades de proc
38
38
39 def __updateSpecFromVoltage(self):
39 def __updateSpecFromVoltage(self):
40
40
41 self.dataOut.timeZone = self.dataIn.timeZone
41 self.dataOut.timeZone = self.dataIn.timeZone
42 self.dataOut.dstFlag = self.dataIn.dstFlag
42 self.dataOut.dstFlag = self.dataIn.dstFlag
43 self.dataOut.errorCount = self.dataIn.errorCount
43 self.dataOut.errorCount = self.dataIn.errorCount
44 self.dataOut.useLocalTime = self.dataIn.useLocalTime
44 self.dataOut.useLocalTime = self.dataIn.useLocalTime
45 try:
45 try:
46 self.dataOut.processingHeaderObj = self.dataIn.processingHeaderObj.copy()
46 self.dataOut.processingHeaderObj = self.dataIn.processingHeaderObj.copy()
47 except:
47 except:
48 pass
48 pass
49 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
49 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
50 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
50 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
51 self.dataOut.channelList = self.dataIn.channelList
51 self.dataOut.channelList = self.dataIn.channelList
52 self.dataOut.heightList = self.dataIn.heightList
52 self.dataOut.heightList = self.dataIn.heightList
53 self.dataOut.dtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
53 self.dataOut.dtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
54 self.dataOut.nProfiles = self.dataOut.nFFTPoints
54 self.dataOut.nProfiles = self.dataOut.nFFTPoints
55 self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock
55 self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock
56 self.dataOut.utctime = self.firstdatatime
56 self.dataOut.utctime = self.firstdatatime
57 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData
57 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData
58 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData
58 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData
59 self.dataOut.flagShiftFFT = False
59 self.dataOut.flagShiftFFT = False
60 self.dataOut.nCohInt = self.dataIn.nCohInt
60 self.dataOut.nCohInt = self.dataIn.nCohInt
61 self.dataOut.nIncohInt = 1
61 self.dataOut.nIncohInt = 1
62 self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
62 self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
63 self.dataOut.frequency = self.dataIn.frequency
63 self.dataOut.frequency = self.dataIn.frequency
64 self.dataOut.realtime = self.dataIn.realtime
64 self.dataOut.realtime = self.dataIn.realtime
65 self.dataOut.azimuth = self.dataIn.azimuth
65 self.dataOut.azimuth = self.dataIn.azimuth
66 self.dataOut.zenith = self.dataIn.zenith
66 self.dataOut.zenith = self.dataIn.zenith
67 self.dataOut.codeList = self.dataIn.codeList
67 self.dataOut.codeList = self.dataIn.codeList
68 self.dataOut.azimuthList = self.dataIn.azimuthList
68 self.dataOut.azimuthList = self.dataIn.azimuthList
69 self.dataOut.elevationList = self.dataIn.elevationList
69 self.dataOut.elevationList = self.dataIn.elevationList
70
70
71 def __getFft(self):
71 def __getFft(self):
72 """
72 """
73 Convierte valores de Voltaje a Spectra
73 Convierte valores de Voltaje a Spectra
74
74
75 Affected:
75 Affected:
76 self.dataOut.data_spc
76 self.dataOut.data_spc
77 self.dataOut.data_cspc
77 self.dataOut.data_cspc
78 self.dataOut.data_dc
78 self.dataOut.data_dc
79 self.dataOut.heightList
79 self.dataOut.heightList
80 self.profIndex
80 self.profIndex
81 self.buffer
81 self.buffer
82 self.dataOut.flagNoData
82 self.dataOut.flagNoData
83 """
83 """
84 fft_volt = numpy.fft.fft(
84 fft_volt = numpy.fft.fft(
85 self.buffer, n=self.dataOut.nFFTPoints, axis=1)
85 self.buffer, n=self.dataOut.nFFTPoints, axis=1)
86 fft_volt = fft_volt.astype(numpy.dtype('complex'))
86 fft_volt = fft_volt.astype(numpy.dtype('complex'))
87 dc = fft_volt[:, 0, :]
87 dc = fft_volt[:, 0, :]
88
88
89 # calculo de self-spectra
89 # calculo de self-spectra
90 fft_volt = numpy.fft.fftshift(fft_volt, axes=(1,))
90 fft_volt = numpy.fft.fftshift(fft_volt, axes=(1,))
91 spc = fft_volt * numpy.conjugate(fft_volt)
91 spc = fft_volt * numpy.conjugate(fft_volt)
92 spc = spc.real
92 spc = spc.real
93
93
94 blocksize = 0
94 blocksize = 0
95 blocksize += dc.size
95 blocksize += dc.size
96 blocksize += spc.size
96 blocksize += spc.size
97
97
98 cspc = None
98 cspc = None
99 pairIndex = 0
99 pairIndex = 0
100 if self.dataOut.pairsList != None:
100 if self.dataOut.pairsList != None:
101 # calculo de cross-spectra
101 # calculo de cross-spectra
102 cspc = numpy.zeros(
102 cspc = numpy.zeros(
103 (self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex')
103 (self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex')
104 for pair in self.dataOut.pairsList:
104 for pair in self.dataOut.pairsList:
105 if pair[0] not in self.dataOut.channelList:
105 if pair[0] not in self.dataOut.channelList:
106 raise ValueError("Error getting CrossSpectra: pair 0 of %s is not in channelList = %s" % (
106 raise ValueError("Error getting CrossSpectra: pair 0 of %s is not in channelList = %s" % (
107 str(pair), str(self.dataOut.channelList)))
107 str(pair), str(self.dataOut.channelList)))
108 if pair[1] not in self.dataOut.channelList:
108 if pair[1] not in self.dataOut.channelList:
109 raise ValueError("Error getting CrossSpectra: pair 1 of %s is not in channelList = %s" % (
109 raise ValueError("Error getting CrossSpectra: pair 1 of %s is not in channelList = %s" % (
110 str(pair), str(self.dataOut.channelList)))
110 str(pair), str(self.dataOut.channelList)))
111
111
112 cspc[pairIndex, :, :] = fft_volt[pair[0], :, :] * \
112 cspc[pairIndex, :, :] = fft_volt[pair[0], :, :] * \
113 numpy.conjugate(fft_volt[pair[1], :, :])
113 numpy.conjugate(fft_volt[pair[1], :, :])
114 pairIndex += 1
114 pairIndex += 1
115 blocksize += cspc.size
115 blocksize += cspc.size
116
116
117 self.dataOut.data_spc = spc
117 self.dataOut.data_spc = spc
118 self.dataOut.data_cspc = cspc
118 self.dataOut.data_cspc = cspc
119 self.dataOut.data_dc = dc
119 self.dataOut.data_dc = dc
120 self.dataOut.blockSize = blocksize
120 self.dataOut.blockSize = blocksize
121 self.dataOut.flagShiftFFT = False
121 self.dataOut.flagShiftFFT = False
122
122
123 def run(self, nProfiles=None, nFFTPoints=None, pairsList=None, ippFactor=None, shift_fft=False):
123 def run(self, nProfiles=None, nFFTPoints=None, pairsList=None, ippFactor=None, shift_fft=False):
124
124
125 if self.dataIn.type == "Spectra":
125 if self.dataIn.type == "Spectra":
126 self.dataOut.copy(self.dataIn)
126 self.dataOut.copy(self.dataIn)
127 if shift_fft:
127 if shift_fft:
128 #desplaza a la derecha en el eje 2 determinadas posiciones
128 #desplaza a la derecha en el eje 2 determinadas posiciones
129 shift = int(self.dataOut.nFFTPoints/2)
129 shift = int(self.dataOut.nFFTPoints/2)
130 self.dataOut.data_spc = numpy.roll(self.dataOut.data_spc, shift , axis=1)
130 self.dataOut.data_spc = numpy.roll(self.dataOut.data_spc, shift , axis=1)
131
131
132 if self.dataOut.data_cspc is not None:
132 if self.dataOut.data_cspc is not None:
133 #desplaza a la derecha en el eje 2 determinadas posiciones
133 #desplaza a la derecha en el eje 2 determinadas posiciones
134 self.dataOut.data_cspc = numpy.roll(self.dataOut.data_cspc, shift, axis=1)
134 self.dataOut.data_cspc = numpy.roll(self.dataOut.data_cspc, shift, axis=1)
135 if pairsList:
135 if pairsList:
136 self.__selectPairs(pairsList)
136 self.__selectPairs(pairsList)
137
137
138 elif self.dataIn.type == "Voltage":
138 elif self.dataIn.type == "Voltage":
139
139
140 self.dataOut.flagNoData = True
140 self.dataOut.flagNoData = True
141
141
142 if nFFTPoints == None:
142 if nFFTPoints == None:
143 raise ValueError("This SpectraProc.run() need nFFTPoints input variable")
143 raise ValueError("This SpectraProc.run() need nFFTPoints input variable")
144
144
145 if nProfiles == None:
145 if nProfiles == None:
146 nProfiles = nFFTPoints
146 nProfiles = nFFTPoints
147
147
148 if ippFactor == None:
148 if ippFactor == None:
149 self.dataOut.ippFactor = 1
149 self.dataOut.ippFactor = 1
150
150
151 self.dataOut.nFFTPoints = nFFTPoints
151 self.dataOut.nFFTPoints = nFFTPoints
152
152
153 if self.buffer is None:
153 if self.buffer is None:
154 self.buffer = numpy.zeros((self.dataIn.nChannels,
154 self.buffer = numpy.zeros((self.dataIn.nChannels,
155 nProfiles,
155 nProfiles,
156 self.dataIn.nHeights),
156 self.dataIn.nHeights),
157 dtype='complex')
157 dtype='complex')
158
158
159 if self.dataIn.flagDataAsBlock:
159 if self.dataIn.flagDataAsBlock:
160 nVoltProfiles = self.dataIn.data.shape[1]
160 nVoltProfiles = self.dataIn.data.shape[1]
161
161
162 if nVoltProfiles == nProfiles:
162 if nVoltProfiles == nProfiles:
163 self.buffer = self.dataIn.data.copy()
163 self.buffer = self.dataIn.data.copy()
164 self.profIndex = nVoltProfiles
164 self.profIndex = nVoltProfiles
165
165
166 elif nVoltProfiles < nProfiles:
166 elif nVoltProfiles < nProfiles:
167
167
168 if self.profIndex == 0:
168 if self.profIndex == 0:
169 self.id_min = 0
169 self.id_min = 0
170 self.id_max = nVoltProfiles
170 self.id_max = nVoltProfiles
171
171
172 self.buffer[:, self.id_min:self.id_max,
172 self.buffer[:, self.id_min:self.id_max,
173 :] = self.dataIn.data
173 :] = self.dataIn.data
174 self.profIndex += nVoltProfiles
174 self.profIndex += nVoltProfiles
175 self.id_min += nVoltProfiles
175 self.id_min += nVoltProfiles
176 self.id_max += nVoltProfiles
176 self.id_max += nVoltProfiles
177 else:
177 else:
178 raise ValueError("The type object %s has %d profiles, it should just has %d profiles" % (
178 raise ValueError("The type object %s has %d profiles, it should just has %d profiles" % (
179 self.dataIn.type, self.dataIn.data.shape[1], nProfiles))
179 self.dataIn.type, self.dataIn.data.shape[1], nProfiles))
180 self.dataOut.flagNoData = True
180 self.dataOut.flagNoData = True
181 else:
181 else:
182 self.buffer[:, self.profIndex, :] = self.dataIn.data.copy()
182 self.buffer[:, self.profIndex, :] = self.dataIn.data.copy()
183 self.profIndex += 1
183 self.profIndex += 1
184
184
185 if self.firstdatatime == None:
185 if self.firstdatatime == None:
186 self.firstdatatime = self.dataIn.utctime
186 self.firstdatatime = self.dataIn.utctime
187
187
188 if self.profIndex == nProfiles:
188 if self.profIndex == nProfiles:
189 self.__updateSpecFromVoltage()
189 self.__updateSpecFromVoltage()
190 if pairsList == None:
190 if pairsList == None:
191 self.dataOut.pairsList = [pair for pair in itertools.combinations(self.dataOut.channelList, 2)]
191 self.dataOut.pairsList = [pair for pair in itertools.combinations(self.dataOut.channelList, 2)]
192 else:
192 else:
193 self.dataOut.pairsList = pairsList
193 self.dataOut.pairsList = pairsList
194 self.__getFft()
194 self.__getFft()
195 self.dataOut.flagNoData = False
195 self.dataOut.flagNoData = False
196 self.firstdatatime = None
196 self.firstdatatime = None
197 self.profIndex = 0
197 self.profIndex = 0
198 else:
198 else:
199 raise ValueError("The type of input object '%s' is not valid".format(
199 raise ValueError("The type of input object '%s' is not valid".format(
200 self.dataIn.type))
200 self.dataIn.type))
201
201
202 def __selectPairs(self, pairsList):
202 def __selectPairs(self, pairsList):
203
203
204 if not pairsList:
204 if not pairsList:
205 return
205 return
206
206
207 pairs = []
207 pairs = []
208 pairsIndex = []
208 pairsIndex = []
209
209
210 for pair in pairsList:
210 for pair in pairsList:
211 if pair[0] not in self.dataOut.channelList or pair[1] not in self.dataOut.channelList:
211 if pair[0] not in self.dataOut.channelList or pair[1] not in self.dataOut.channelList:
212 continue
212 continue
213 pairs.append(pair)
213 pairs.append(pair)
214 pairsIndex.append(pairs.index(pair))
214 pairsIndex.append(pairs.index(pair))
215
215
216 self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndex]
216 self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndex]
217 self.dataOut.pairsList = pairs
217 self.dataOut.pairsList = pairs
218
218
219 return
219 return
220
220
221 def selectFFTs(self, minFFT, maxFFT ):
221 def selectFFTs(self, minFFT, maxFFT ):
222 """
222 """
223 Selecciona un bloque de datos en base a un grupo de valores de puntos FFTs segun el rango
223 Selecciona un bloque de datos en base a un grupo de valores de puntos FFTs segun el rango
224 minFFT<= FFT <= maxFFT
224 minFFT<= FFT <= maxFFT
225 """
225 """
226
226
227 if (minFFT > maxFFT):
227 if (minFFT > maxFFT):
228 raise ValueError("Error selecting heights: Height range (%d,%d) is not valid" % (minFFT, maxFFT))
228 raise ValueError("Error selecting heights: Height range (%d,%d) is not valid" % (minFFT, maxFFT))
229
229
230 if (minFFT < self.dataOut.getFreqRange()[0]):
230 if (minFFT < self.dataOut.getFreqRange()[0]):
231 minFFT = self.dataOut.getFreqRange()[0]
231 minFFT = self.dataOut.getFreqRange()[0]
232
232
233 if (maxFFT > self.dataOut.getFreqRange()[-1]):
233 if (maxFFT > self.dataOut.getFreqRange()[-1]):
234 maxFFT = self.dataOut.getFreqRange()[-1]
234 maxFFT = self.dataOut.getFreqRange()[-1]
235
235
236 minIndex = 0
236 minIndex = 0
237 maxIndex = 0
237 maxIndex = 0
238 FFTs = self.dataOut.getFreqRange()
238 FFTs = self.dataOut.getFreqRange()
239
239
240 inda = numpy.where(FFTs >= minFFT)
240 inda = numpy.where(FFTs >= minFFT)
241 indb = numpy.where(FFTs <= maxFFT)
241 indb = numpy.where(FFTs <= maxFFT)
242
242
243 try:
243 try:
244 minIndex = inda[0][0]
244 minIndex = inda[0][0]
245 except:
245 except:
246 minIndex = 0
246 minIndex = 0
247
247
248 try:
248 try:
249 maxIndex = indb[0][-1]
249 maxIndex = indb[0][-1]
250 except:
250 except:
251 maxIndex = len(FFTs)
251 maxIndex = len(FFTs)
252
252
253 self.selectFFTsByIndex(minIndex, maxIndex)
253 self.selectFFTsByIndex(minIndex, maxIndex)
254
254
255 return 1
255 return 1
256
256
257 def getBeaconSignal(self, tauindex=0, channelindex=0, hei_ref=None):
257 def getBeaconSignal(self, tauindex=0, channelindex=0, hei_ref=None):
258 newheis = numpy.where(
258 newheis = numpy.where(
259 self.dataOut.heightList > self.dataOut.radarControllerHeaderObj.Taus[tauindex])
259 self.dataOut.heightList > self.dataOut.radarControllerHeaderObj.Taus[tauindex])
260
260
261 if hei_ref != None:
261 if hei_ref != None:
262 newheis = numpy.where(self.dataOut.heightList > hei_ref)
262 newheis = numpy.where(self.dataOut.heightList > hei_ref)
263
263
264 minIndex = min(newheis[0])
264 minIndex = min(newheis[0])
265 maxIndex = max(newheis[0])
265 maxIndex = max(newheis[0])
266 data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1]
266 data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1]
267 heightList = self.dataOut.heightList[minIndex:maxIndex + 1]
267 heightList = self.dataOut.heightList[minIndex:maxIndex + 1]
268
268
269 # determina indices
269 # determina indices
270 nheis = int(self.dataOut.radarControllerHeaderObj.txB /
270 nheis = int(self.dataOut.radarControllerHeaderObj.txB /
271 (self.dataOut.heightList[1] - self.dataOut.heightList[0]))
271 (self.dataOut.heightList[1] - self.dataOut.heightList[0]))
272 avg_dB = 10 * \
272 avg_dB = 10 * \
273 numpy.log10(numpy.sum(data_spc[channelindex, :, :], axis=0))
273 numpy.log10(numpy.sum(data_spc[channelindex, :, :], axis=0))
274 beacon_dB = numpy.sort(avg_dB)[-nheis:]
274 beacon_dB = numpy.sort(avg_dB)[-nheis:]
275 beacon_heiIndexList = []
275 beacon_heiIndexList = []
276 for val in avg_dB.tolist():
276 for val in avg_dB.tolist():
277 if val >= beacon_dB[0]:
277 if val >= beacon_dB[0]:
278 beacon_heiIndexList.append(avg_dB.tolist().index(val))
278 beacon_heiIndexList.append(avg_dB.tolist().index(val))
279
279
280 #data_spc = data_spc[:,:,beacon_heiIndexList]
280 #data_spc = data_spc[:,:,beacon_heiIndexList]
281 data_cspc = None
281 data_cspc = None
282 if self.dataOut.data_cspc is not None:
282 if self.dataOut.data_cspc is not None:
283 data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1]
283 data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1]
284 #data_cspc = data_cspc[:,:,beacon_heiIndexList]
284 #data_cspc = data_cspc[:,:,beacon_heiIndexList]
285
285
286 data_dc = None
286 data_dc = None
287 if self.dataOut.data_dc is not None:
287 if self.dataOut.data_dc is not None:
288 data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1]
288 data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1]
289 #data_dc = data_dc[:,beacon_heiIndexList]
289 #data_dc = data_dc[:,beacon_heiIndexList]
290
290
291 self.dataOut.data_spc = data_spc
291 self.dataOut.data_spc = data_spc
292 self.dataOut.data_cspc = data_cspc
292 self.dataOut.data_cspc = data_cspc
293 self.dataOut.data_dc = data_dc
293 self.dataOut.data_dc = data_dc
294 self.dataOut.heightList = heightList
294 self.dataOut.heightList = heightList
295 self.dataOut.beacon_heiIndexList = beacon_heiIndexList
295 self.dataOut.beacon_heiIndexList = beacon_heiIndexList
296
296
297 return 1
297 return 1
298
298
299 def selectFFTsByIndex(self, minIndex, maxIndex):
299 def selectFFTsByIndex(self, minIndex, maxIndex):
300 """
300 """
301
301
302 """
302 """
303
303
304 if (minIndex < 0) or (minIndex > maxIndex):
304 if (minIndex < 0) or (minIndex > maxIndex):
305 raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % (minIndex, maxIndex))
305 raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % (minIndex, maxIndex))
306
306
307 if (maxIndex >= self.dataOut.nProfiles):
307 if (maxIndex >= self.dataOut.nProfiles):
308 maxIndex = self.dataOut.nProfiles-1
308 maxIndex = self.dataOut.nProfiles-1
309
309
310 #Spectra
310 #Spectra
311 data_spc = self.dataOut.data_spc[:,minIndex:maxIndex+1,:]
311 data_spc = self.dataOut.data_spc[:,minIndex:maxIndex+1,:]
312
312
313 data_cspc = None
313 data_cspc = None
314 if self.dataOut.data_cspc is not None:
314 if self.dataOut.data_cspc is not None:
315 data_cspc = self.dataOut.data_cspc[:,minIndex:maxIndex+1,:]
315 data_cspc = self.dataOut.data_cspc[:,minIndex:maxIndex+1,:]
316
316
317 data_dc = None
317 data_dc = None
318 if self.dataOut.data_dc is not None:
318 if self.dataOut.data_dc is not None:
319 data_dc = self.dataOut.data_dc[minIndex:maxIndex+1,:]
319 data_dc = self.dataOut.data_dc[minIndex:maxIndex+1,:]
320
320
321 self.dataOut.data_spc = data_spc
321 self.dataOut.data_spc = data_spc
322 self.dataOut.data_cspc = data_cspc
322 self.dataOut.data_cspc = data_cspc
323 self.dataOut.data_dc = data_dc
323 self.dataOut.data_dc = data_dc
324
324
325 self.dataOut.ippSeconds = self.dataOut.ippSeconds*(self.dataOut.nFFTPoints / numpy.shape(data_cspc)[1])
325 self.dataOut.ippSeconds = self.dataOut.ippSeconds*(self.dataOut.nFFTPoints / numpy.shape(data_cspc)[1])
326 self.dataOut.nFFTPoints = numpy.shape(data_cspc)[1]
326 self.dataOut.nFFTPoints = numpy.shape(data_cspc)[1]
327 self.dataOut.profilesPerBlock = numpy.shape(data_cspc)[1]
327 self.dataOut.profilesPerBlock = numpy.shape(data_cspc)[1]
328
328
329 return 1
329 return 1
330
330
331 def getNoise(self, minHei=None, maxHei=None, minVel=None, maxVel=None):
331 def getNoise(self, minHei=None, maxHei=None, minVel=None, maxVel=None):
332 # validacion de rango
332 # validacion de rango
333 if minHei == None:
333 if minHei == None:
334 minHei = self.dataOut.heightList[0]
334 minHei = self.dataOut.heightList[0]
335
335
336 if maxHei == None:
336 if maxHei == None:
337 maxHei = self.dataOut.heightList[-1]
337 maxHei = self.dataOut.heightList[-1]
338
338
339 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
339 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
340 print('minHei: %.2f is out of the heights range' % (minHei))
340 print('minHei: %.2f is out of the heights range' % (minHei))
341 print('minHei is setting to %.2f' % (self.dataOut.heightList[0]))
341 print('minHei is setting to %.2f' % (self.dataOut.heightList[0]))
342 minHei = self.dataOut.heightList[0]
342 minHei = self.dataOut.heightList[0]
343
343
344 if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei):
344 if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei):
345 print('maxHei: %.2f is out of the heights range' % (maxHei))
345 print('maxHei: %.2f is out of the heights range' % (maxHei))
346 print('maxHei is setting to %.2f' % (self.dataOut.heightList[-1]))
346 print('maxHei is setting to %.2f' % (self.dataOut.heightList[-1]))
347 maxHei = self.dataOut.heightList[-1]
347 maxHei = self.dataOut.heightList[-1]
348
348
349 # validacion de velocidades
349 # validacion de velocidades
350 velrange = self.dataOut.getVelRange(1)
350 velrange = self.dataOut.getVelRange(1)
351
351
352 if minVel == None:
352 if minVel == None:
353 minVel = velrange[0]
353 minVel = velrange[0]
354
354
355 if maxVel == None:
355 if maxVel == None:
356 maxVel = velrange[-1]
356 maxVel = velrange[-1]
357
357
358 if (minVel < velrange[0]) or (minVel > maxVel):
358 if (minVel < velrange[0]) or (minVel > maxVel):
359 print('minVel: %.2f is out of the velocity range' % (minVel))
359 print('minVel: %.2f is out of the velocity range' % (minVel))
360 print('minVel is setting to %.2f' % (velrange[0]))
360 print('minVel is setting to %.2f' % (velrange[0]))
361 minVel = velrange[0]
361 minVel = velrange[0]
362
362
363 if (maxVel > velrange[-1]) or (maxVel < minVel):
363 if (maxVel > velrange[-1]) or (maxVel < minVel):
364 print('maxVel: %.2f is out of the velocity range' % (maxVel))
364 print('maxVel: %.2f is out of the velocity range' % (maxVel))
365 print('maxVel is setting to %.2f' % (velrange[-1]))
365 print('maxVel is setting to %.2f' % (velrange[-1]))
366 maxVel = velrange[-1]
366 maxVel = velrange[-1]
367
367
368 # seleccion de indices para rango
368 # seleccion de indices para rango
369 minIndex = 0
369 minIndex = 0
370 maxIndex = 0
370 maxIndex = 0
371 heights = self.dataOut.heightList
371 heights = self.dataOut.heightList
372
372
373 inda = numpy.where(heights >= minHei)
373 inda = numpy.where(heights >= minHei)
374 indb = numpy.where(heights <= maxHei)
374 indb = numpy.where(heights <= maxHei)
375
375
376 try:
376 try:
377 minIndex = inda[0][0]
377 minIndex = inda[0][0]
378 except:
378 except:
379 minIndex = 0
379 minIndex = 0
380
380
381 try:
381 try:
382 maxIndex = indb[0][-1]
382 maxIndex = indb[0][-1]
383 except:
383 except:
384 maxIndex = len(heights)
384 maxIndex = len(heights)
385
385
386 if (minIndex < 0) or (minIndex > maxIndex):
386 if (minIndex < 0) or (minIndex > maxIndex):
387 raise ValueError("some value in (%d,%d) is not valid" % (
387 raise ValueError("some value in (%d,%d) is not valid" % (
388 minIndex, maxIndex))
388 minIndex, maxIndex))
389
389
390 if (maxIndex >= self.dataOut.nHeights):
390 if (maxIndex >= self.dataOut.nHeights):
391 maxIndex = self.dataOut.nHeights - 1
391 maxIndex = self.dataOut.nHeights - 1
392
392
393 # seleccion de indices para velocidades
393 # seleccion de indices para velocidades
394 indminvel = numpy.where(velrange >= minVel)
394 indminvel = numpy.where(velrange >= minVel)
395 indmaxvel = numpy.where(velrange <= maxVel)
395 indmaxvel = numpy.where(velrange <= maxVel)
396 try:
396 try:
397 minIndexVel = indminvel[0][0]
397 minIndexVel = indminvel[0][0]
398 except:
398 except:
399 minIndexVel = 0
399 minIndexVel = 0
400
400
401 try:
401 try:
402 maxIndexVel = indmaxvel[0][-1]
402 maxIndexVel = indmaxvel[0][-1]
403 except:
403 except:
404 maxIndexVel = len(velrange)
404 maxIndexVel = len(velrange)
405
405
406 # seleccion del espectro
406 # seleccion del espectro
407 data_spc = self.dataOut.data_spc[:,
407 data_spc = self.dataOut.data_spc[:,
408 minIndexVel:maxIndexVel + 1, minIndex:maxIndex + 1]
408 minIndexVel:maxIndexVel + 1, minIndex:maxIndex + 1]
409 # estimacion de ruido
409 # estimacion de ruido
410 noise = numpy.zeros(self.dataOut.nChannels)
410 noise = numpy.zeros(self.dataOut.nChannels)
411
411
412 for channel in range(self.dataOut.nChannels):
412 for channel in range(self.dataOut.nChannels):
413 daux = data_spc[channel, :, :]
413 daux = data_spc[channel, :, :]
414 sortdata = numpy.sort(daux, axis=None)
414 sortdata = numpy.sort(daux, axis=None)
415 noise[channel] = hildebrand_sekhon(sortdata, self.dataOut.nIncohInt)
415 noise[channel] = hildebrand_sekhon(sortdata, self.dataOut.nIncohInt)
416
416
417 self.dataOut.noise_estimation = noise.copy()
417 self.dataOut.noise_estimation = noise.copy()
418
418
419 return 1
419 return 1
420
420
421 class removeDC(Operation):
421 class removeDC(Operation):
422
422
423 def run(self, dataOut, mode=2):
423 def run(self, dataOut, mode=2):
424 self.dataOut = dataOut
424 self.dataOut = dataOut
425 jspectra = self.dataOut.data_spc
425 jspectra = self.dataOut.data_spc
426 jcspectra = self.dataOut.data_cspc
426 jcspectra = self.dataOut.data_cspc
427
427
428 num_chan = jspectra.shape[0]
428 num_chan = jspectra.shape[0]
429 num_hei = jspectra.shape[2]
429 num_hei = jspectra.shape[2]
430
430
431 if jcspectra is not None:
431 if jcspectra is not None:
432 jcspectraExist = True
432 jcspectraExist = True
433 num_pairs = jcspectra.shape[0]
433 num_pairs = jcspectra.shape[0]
434 else:
434 else:
435 jcspectraExist = False
435 jcspectraExist = False
436
436
437 freq_dc = int(jspectra.shape[1] / 2)
437 freq_dc = int(jspectra.shape[1] / 2)
438 ind_vel = numpy.array([-2, -1, 1, 2]) + freq_dc
438 ind_vel = numpy.array([-2, -1, 1, 2]) + freq_dc
439 ind_vel = ind_vel.astype(int)
439 ind_vel = ind_vel.astype(int)
440
440
441 if ind_vel[0] < 0:
441 if ind_vel[0] < 0:
442 ind_vel[list(range(0, 1))] = ind_vel[list(range(0, 1))] + self.num_prof
442 ind_vel[list(range(0, 1))] = ind_vel[list(range(0, 1))] + self.num_prof
443
443
444 if mode == 1:
444 if mode == 1:
445 jspectra[:, freq_dc, :] = (
445 jspectra[:, freq_dc, :] = (
446 jspectra[:, ind_vel[1], :] + jspectra[:, ind_vel[2], :]) / 2 # CORRECCION
446 jspectra[:, ind_vel[1], :] + jspectra[:, ind_vel[2], :]) / 2 # CORRECCION
447
447
448 if jcspectraExist:
448 if jcspectraExist:
449 jcspectra[:, freq_dc, :] = (
449 jcspectra[:, freq_dc, :] = (
450 jcspectra[:, ind_vel[1], :] + jcspectra[:, ind_vel[2], :]) / 2
450 jcspectra[:, ind_vel[1], :] + jcspectra[:, ind_vel[2], :]) / 2
451
451
452 if mode == 2:
452 if mode == 2:
453
453
454 vel = numpy.array([-2, -1, 1, 2])
454 vel = numpy.array([-2, -1, 1, 2])
455 xx = numpy.zeros([4, 4])
455 xx = numpy.zeros([4, 4])
456
456
457 for fil in range(4):
457 for fil in range(4):
458 xx[fil, :] = vel[fil]**numpy.asarray(list(range(4)))
458 xx[fil, :] = vel[fil]**numpy.asarray(list(range(4)))
459
459
460 xx_inv = numpy.linalg.inv(xx)
460 xx_inv = numpy.linalg.inv(xx)
461 xx_aux = xx_inv[0, :]
461 xx_aux = xx_inv[0, :]
462
462
463 for ich in range(num_chan):
463 for ich in range(num_chan):
464 yy = jspectra[ich, ind_vel, :]
464 yy = jspectra[ich, ind_vel, :]
465 jspectra[ich, freq_dc, :] = numpy.dot(xx_aux, yy)
465 jspectra[ich, freq_dc, :] = numpy.dot(xx_aux, yy)
466
466
467 junkid = jspectra[ich, freq_dc, :] <= 0
467 junkid = jspectra[ich, freq_dc, :] <= 0
468 cjunkid = sum(junkid)
468 cjunkid = sum(junkid)
469
469
470 if cjunkid.any():
470 if cjunkid.any():
471 jspectra[ich, freq_dc, junkid.nonzero()] = (
471 jspectra[ich, freq_dc, junkid.nonzero()] = (
472 jspectra[ich, ind_vel[1], junkid] + jspectra[ich, ind_vel[2], junkid]) / 2
472 jspectra[ich, ind_vel[1], junkid] + jspectra[ich, ind_vel[2], junkid]) / 2
473
473
474 if jcspectraExist:
474 if jcspectraExist:
475 for ip in range(num_pairs):
475 for ip in range(num_pairs):
476 yy = jcspectra[ip, ind_vel, :]
476 yy = jcspectra[ip, ind_vel, :]
477 jcspectra[ip, freq_dc, :] = numpy.dot(xx_aux, yy)
477 jcspectra[ip, freq_dc, :] = numpy.dot(xx_aux, yy)
478
478
479 self.dataOut.data_spc = jspectra
479 self.dataOut.data_spc = jspectra
480 self.dataOut.data_cspc = jcspectra
480 self.dataOut.data_cspc = jcspectra
481
481
482 return self.dataOut
482 return self.dataOut
483
483
484 # import matplotlib.pyplot as plt
484 # import matplotlib.pyplot as plt
485
485
486 def fit_func( x, a0, a1, a2): #, a3, a4, a5):
486 def fit_func( x, a0, a1, a2): #, a3, a4, a5):
487 z = (x - a1) / a2
487 z = (x - a1) / a2
488 y = a0 * numpy.exp(-z**2 / a2) #+ a3 + a4 * x + a5 * x**2
488 y = a0 * numpy.exp(-z**2 / a2) #+ a3 + a4 * x + a5 * x**2
489 return y
489 return y
490
490
491
491
492 class CleanRayleigh(Operation):
492 class CleanRayleigh(Operation):
493
493
494 def __init__(self):
494 def __init__(self):
495
495
496 Operation.__init__(self)
496 Operation.__init__(self)
497 self.i=0
497 self.i=0
498 self.isConfig = False
498 self.isConfig = False
499 self.__dataReady = False
499 self.__dataReady = False
500 self.__profIndex = 0
500 self.__profIndex = 0
501 self.byTime = False
501 self.byTime = False
502 self.byProfiles = False
502 self.byProfiles = False
503
503
504 self.bloques = None
504 self.bloques = None
505 self.bloque0 = None
505 self.bloque0 = None
506
506
507 self.index = 0
507 self.index = 0
508
508
509 self.buffer = 0
509 self.buffer = 0
510 self.buffer2 = 0
510 self.buffer2 = 0
511 self.buffer3 = 0
511 self.buffer3 = 0
512
512
513
513
514 def setup(self,dataOut,min_hei,max_hei,n, timeInterval,factor_stdv):
514 def setup(self,dataOut,min_hei,max_hei,n, timeInterval,factor_stdv):
515
515
516 self.nChannels = dataOut.nChannels
516 self.nChannels = dataOut.nChannels
517 self.nProf = dataOut.nProfiles
517 self.nProf = dataOut.nProfiles
518 self.nPairs = dataOut.data_cspc.shape[0]
518 self.nPairs = dataOut.data_cspc.shape[0]
519 self.pairsArray = numpy.array(dataOut.pairsList)
519 self.pairsArray = numpy.array(dataOut.pairsList)
520 self.spectra = dataOut.data_spc
520 self.spectra = dataOut.data_spc
521 self.cspectra = dataOut.data_cspc
521 self.cspectra = dataOut.data_cspc
522 self.heights = dataOut.heightList #alturas totales
522 self.heights = dataOut.heightList #alturas totales
523 self.nHeights = len(self.heights)
523 self.nHeights = len(self.heights)
524 self.min_hei = min_hei
524 self.min_hei = min_hei
525 self.max_hei = max_hei
525 self.max_hei = max_hei
526 if (self.min_hei == None):
526 if (self.min_hei == None):
527 self.min_hei = 0
527 self.min_hei = 0
528 if (self.max_hei == None):
528 if (self.max_hei == None):
529 self.max_hei = dataOut.heightList[-1]
529 self.max_hei = dataOut.heightList[-1]
530 self.hval = ((self.max_hei>=self.heights) & (self.heights >= self.min_hei)).nonzero()
530 self.hval = ((self.max_hei>=self.heights) & (self.heights >= self.min_hei)).nonzero()
531 self.heightsClean = self.heights[self.hval] #alturas filtradas
531 self.heightsClean = self.heights[self.hval] #alturas filtradas
532 self.hval = self.hval[0] # forma (N,), an solo N elementos -> Indices de alturas
532 self.hval = self.hval[0] # forma (N,), an solo N elementos -> Indices de alturas
533 self.nHeightsClean = len(self.heightsClean)
533 self.nHeightsClean = len(self.heightsClean)
534 self.channels = dataOut.channelList
534 self.channels = dataOut.channelList
535 self.nChan = len(self.channels)
535 self.nChan = len(self.channels)
536 self.nIncohInt = dataOut.nIncohInt
536 self.nIncohInt = dataOut.nIncohInt
537 self.__initime = dataOut.utctime
537 self.__initime = dataOut.utctime
538 self.maxAltInd = self.hval[-1]+1
538 self.maxAltInd = self.hval[-1]+1
539 self.minAltInd = self.hval[0]
539 self.minAltInd = self.hval[0]
540
540
541 self.crosspairs = dataOut.pairsList
541 self.crosspairs = dataOut.pairsList
542 self.nPairs = len(self.crosspairs)
542 self.nPairs = len(self.crosspairs)
543 self.normFactor = dataOut.normFactor
543 self.normFactor = dataOut.normFactor
544 self.nFFTPoints = dataOut.nFFTPoints
544 self.nFFTPoints = dataOut.nFFTPoints
545 self.ippSeconds = dataOut.ippSeconds
545 self.ippSeconds = dataOut.ippSeconds
546 self.currentTime = self.__initime
546 self.currentTime = self.__initime
547 self.pairsArray = numpy.array(dataOut.pairsList)
547 self.pairsArray = numpy.array(dataOut.pairsList)
548 self.factor_stdv = factor_stdv
548 self.factor_stdv = factor_stdv
549 #print("CHANNELS: ",[x for x in self.channels])
549 #print("CHANNELS: ",[x for x in self.channels])
550
550
551 if n != None :
551 if n != None :
552 self.byProfiles = True
552 self.byProfiles = True
553 self.nIntProfiles = n
553 self.nIntProfiles = n
554 else:
554 else:
555 self.__integrationtime = timeInterval
555 self.__integrationtime = timeInterval
556
556
557 self.__dataReady = False
557 self.__dataReady = False
558 self.isConfig = True
558 self.isConfig = True
559
559
560
560
561
561
562 def run(self, dataOut,min_hei=None,max_hei=None, n=None, timeInterval=10,factor_stdv=2.5):
562 def run(self, dataOut,min_hei=None,max_hei=None, n=None, timeInterval=10,factor_stdv=2.5):
563 #print (dataOut.utctime)
563 #print (dataOut.utctime)
564 if not self.isConfig :
564 if not self.isConfig :
565 #print("Setting config")
565 #print("Setting config")
566 self.setup(dataOut, min_hei,max_hei,n,timeInterval,factor_stdv)
566 self.setup(dataOut, min_hei,max_hei,n,timeInterval,factor_stdv)
567 #print("Config Done")
567 #print("Config Done")
568 tini=dataOut.utctime
568 tini=dataOut.utctime
569
569
570 if self.byProfiles:
570 if self.byProfiles:
571 if self.__profIndex == self.nIntProfiles:
571 if self.__profIndex == self.nIntProfiles:
572 self.__dataReady = True
572 self.__dataReady = True
573 else:
573 else:
574 if (tini - self.__initime) >= self.__integrationtime:
574 if (tini - self.__initime) >= self.__integrationtime:
575 #print(tini - self.__initime,self.__profIndex)
575 #print(tini - self.__initime,self.__profIndex)
576 self.__dataReady = True
576 self.__dataReady = True
577 self.__initime = tini
577 self.__initime = tini
578
578
579 #if (tini.tm_min % 2) == 0 and (tini.tm_sec < 5 and self.fint==0):
579 #if (tini.tm_min % 2) == 0 and (tini.tm_sec < 5 and self.fint==0):
580
580
581 if self.__dataReady:
581 if self.__dataReady:
582 #print("Data ready",self.__profIndex)
582 #print("Data ready",self.__profIndex)
583 self.__profIndex = 0
583 self.__profIndex = 0
584 jspc = self.buffer
584 jspc = self.buffer
585 jcspc = self.buffer2
585 jcspc = self.buffer2
586 #jnoise = self.buffer3
586 #jnoise = self.buffer3
587 self.buffer = dataOut.data_spc
587 self.buffer = dataOut.data_spc
588 self.buffer2 = dataOut.data_cspc
588 self.buffer2 = dataOut.data_cspc
589 #self.buffer3 = dataOut.noise
589 #self.buffer3 = dataOut.noise
590 self.currentTime = dataOut.utctime
590 self.currentTime = dataOut.utctime
591 if numpy.any(jspc) :
591 if numpy.any(jspc) :
592 #print( jspc.shape, jcspc.shape)
592 #print( jspc.shape, jcspc.shape)
593 jspc = numpy.reshape(jspc,(int(len(jspc)/self.nChannels),self.nChannels,self.nFFTPoints,self.nHeights))
593 jspc = numpy.reshape(jspc,(int(len(jspc)/self.nChannels),self.nChannels,self.nFFTPoints,self.nHeights))
594 jcspc= numpy.reshape(jcspc,(int(len(jcspc)/self.nPairs),self.nPairs,self.nFFTPoints,self.nHeights))
594 jcspc= numpy.reshape(jcspc,(int(len(jcspc)/self.nPairs),self.nPairs,self.nFFTPoints,self.nHeights))
595 self.__dataReady = False
595 self.__dataReady = False
596 #print( jspc.shape, jcspc.shape)
596 #print( jspc.shape, jcspc.shape)
597 dataOut.flagNoData = False
597 dataOut.flagNoData = False
598 else:
598 else:
599 dataOut.flagNoData = True
599 dataOut.flagNoData = True
600 self.__dataReady = False
600 self.__dataReady = False
601 return dataOut
601 return dataOut
602 else:
602 else:
603 #print( len(self.buffer))
603 #print( len(self.buffer))
604 if numpy.any(self.buffer):
604 if numpy.any(self.buffer):
605 self.buffer = numpy.concatenate((self.buffer,dataOut.data_spc), axis=0)
605 self.buffer = numpy.concatenate((self.buffer,dataOut.data_spc), axis=0)
606 self.buffer2 = numpy.concatenate((self.buffer2,dataOut.data_cspc), axis=0)
606 self.buffer2 = numpy.concatenate((self.buffer2,dataOut.data_cspc), axis=0)
607 self.buffer3 += dataOut.data_dc
607 self.buffer3 += dataOut.data_dc
608 else:
608 else:
609 self.buffer = dataOut.data_spc
609 self.buffer = dataOut.data_spc
610 self.buffer2 = dataOut.data_cspc
610 self.buffer2 = dataOut.data_cspc
611 self.buffer3 = dataOut.data_dc
611 self.buffer3 = dataOut.data_dc
612 #print self.index, self.fint
612 #print self.index, self.fint
613 #print self.buffer2.shape
613 #print self.buffer2.shape
614 dataOut.flagNoData = True ## NOTE: ?? revisar LUEGO
614 dataOut.flagNoData = True ## NOTE: ?? revisar LUEGO
615 self.__profIndex += 1
615 self.__profIndex += 1
616 return dataOut ## NOTE: REV
616 return dataOut ## NOTE: REV
617
617
618
618
619 #index = tini.tm_hour*12+tini.tm_min/5
619 #index = tini.tm_hour*12+tini.tm_min/5
620 '''REVISAR'''
620 '''REVISAR'''
621 # jspc = jspc/self.nFFTPoints/self.normFactor
621 # jspc = jspc/self.nFFTPoints/self.normFactor
622 # jcspc = jcspc/self.nFFTPoints/self.normFactor
622 # jcspc = jcspc/self.nFFTPoints/self.normFactor
623
623
624
624
625
625
626 tmp_spectra,tmp_cspectra = self.cleanRayleigh(dataOut,jspc,jcspc,self.factor_stdv)
626 tmp_spectra,tmp_cspectra = self.cleanRayleigh(dataOut,jspc,jcspc,self.factor_stdv)
627 dataOut.data_spc = tmp_spectra
627 dataOut.data_spc = tmp_spectra
628 dataOut.data_cspc = tmp_cspectra
628 dataOut.data_cspc = tmp_cspectra
629
629
630 #dataOut.data_spc,dataOut.data_cspc = self.cleanRayleigh(dataOut,jspc,jcspc,self.factor_stdv)
630 #dataOut.data_spc,dataOut.data_cspc = self.cleanRayleigh(dataOut,jspc,jcspc,self.factor_stdv)
631
631
632 dataOut.data_dc = self.buffer3
632 dataOut.data_dc = self.buffer3
633 dataOut.nIncohInt *= self.nIntProfiles
633 dataOut.nIncohInt *= self.nIntProfiles
634 dataOut.utctime = self.currentTime #tiempo promediado
634 dataOut.utctime = self.currentTime #tiempo promediado
635 #print("Time: ",time.localtime(dataOut.utctime))
635 #print("Time: ",time.localtime(dataOut.utctime))
636 # dataOut.data_spc = sat_spectra
636 # dataOut.data_spc = sat_spectra
637 # dataOut.data_cspc = sat_cspectra
637 # dataOut.data_cspc = sat_cspectra
638 self.buffer = 0
638 self.buffer = 0
639 self.buffer2 = 0
639 self.buffer2 = 0
640 self.buffer3 = 0
640 self.buffer3 = 0
641
641
642 return dataOut
642 return dataOut
643
643
644 def cleanRayleigh(self,dataOut,spectra,cspectra,factor_stdv):
644 def cleanRayleigh(self,dataOut,spectra,cspectra,factor_stdv):
645 #print("OP cleanRayleigh")
645 #print("OP cleanRayleigh")
646 #import matplotlib.pyplot as plt
646 #import matplotlib.pyplot as plt
647 #for k in range(149):
647 #for k in range(149):
648 #channelsProcssd = []
648 #channelsProcssd = []
649 #channelA_ok = False
649 #channelA_ok = False
650 #rfunc = cspectra.copy() #self.bloques
650 #rfunc = cspectra.copy() #self.bloques
651 rfunc = spectra.copy()
651 rfunc = spectra.copy()
652 #rfunc = cspectra
652 #rfunc = cspectra
653 #val_spc = spectra*0.0 #self.bloque0*0.0
653 #val_spc = spectra*0.0 #self.bloque0*0.0
654 #val_cspc = cspectra*0.0 #self.bloques*0.0
654 #val_cspc = cspectra*0.0 #self.bloques*0.0
655 #in_sat_spectra = spectra.copy() #self.bloque0
655 #in_sat_spectra = spectra.copy() #self.bloque0
656 #in_sat_cspectra = cspectra.copy() #self.bloques
656 #in_sat_cspectra = cspectra.copy() #self.bloques
657
657
658
658
659 ###ONLY FOR TEST:
659 ###ONLY FOR TEST:
660 raxs = math.ceil(math.sqrt(self.nPairs))
660 raxs = math.ceil(math.sqrt(self.nPairs))
661 caxs = math.ceil(self.nPairs/raxs)
661 caxs = math.ceil(self.nPairs/raxs)
662 if self.nPairs <4:
662 if self.nPairs <4:
663 raxs = 2
663 raxs = 2
664 caxs = 2
664 caxs = 2
665 #print(raxs, caxs)
665 #print(raxs, caxs)
666 fft_rev = 14 #nFFT to plot
666 fft_rev = 14 #nFFT to plot
667 hei_rev = ((self.heights >= 550) & (self.heights <= 551)).nonzero() #hei to plot
667 hei_rev = ((self.heights >= 550) & (self.heights <= 551)).nonzero() #hei to plot
668 hei_rev = hei_rev[0]
668 hei_rev = hei_rev[0]
669 #print(hei_rev)
669 #print(hei_rev)
670
670
671 #print numpy.absolute(rfunc[:,0,0,14])
671 #print numpy.absolute(rfunc[:,0,0,14])
672
672
673 gauss_fit, covariance = None, None
673 gauss_fit, covariance = None, None
674 for ih in range(self.minAltInd,self.maxAltInd):
674 for ih in range(self.minAltInd,self.maxAltInd):
675 for ifreq in range(self.nFFTPoints):
675 for ifreq in range(self.nFFTPoints):
676 '''
676 '''
677 ###ONLY FOR TEST:
677 ###ONLY FOR TEST:
678 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
678 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
679 fig, axs = plt.subplots(raxs, caxs)
679 fig, axs = plt.subplots(raxs, caxs)
680 fig2, axs2 = plt.subplots(raxs, caxs)
680 fig2, axs2 = plt.subplots(raxs, caxs)
681 col_ax = 0
681 col_ax = 0
682 row_ax = 0
682 row_ax = 0
683 '''
683 '''
684 #print(self.nPairs)
684 #print(self.nPairs)
685 for ii in range(self.nChan): #PARES DE CANALES SELF y CROSS
685 for ii in range(self.nChan): #PARES DE CANALES SELF y CROSS
686 # if self.crosspairs[ii][1]-self.crosspairs[ii][0] > 1: # APLICAR SOLO EN PARES CONTIGUOS
686 # if self.crosspairs[ii][1]-self.crosspairs[ii][0] > 1: # APLICAR SOLO EN PARES CONTIGUOS
687 # continue
687 # continue
688 # if not self.crosspairs[ii][0] in channelsProcssd:
688 # if not self.crosspairs[ii][0] in channelsProcssd:
689 # channelA_ok = True
689 # channelA_ok = True
690 #print("pair: ",self.crosspairs[ii])
690 #print("pair: ",self.crosspairs[ii])
691 '''
691 '''
692 ###ONLY FOR TEST:
692 ###ONLY FOR TEST:
693 if (col_ax%caxs==0 and col_ax!=0 and self.nPairs !=1):
693 if (col_ax%caxs==0 and col_ax!=0 and self.nPairs !=1):
694 col_ax = 0
694 col_ax = 0
695 row_ax += 1
695 row_ax += 1
696 '''
696 '''
697 func2clean = 10*numpy.log10(numpy.absolute(rfunc[:,ii,ifreq,ih])) #Potencia?
697 func2clean = 10*numpy.log10(numpy.absolute(rfunc[:,ii,ifreq,ih])) #Potencia?
698 #print(func2clean.shape)
698 #print(func2clean.shape)
699 val = (numpy.isfinite(func2clean)==True).nonzero()
699 val = (numpy.isfinite(func2clean)==True).nonzero()
700
700
701 if len(val)>0: #limitador
701 if len(val)>0: #limitador
702 min_val = numpy.around(numpy.amin(func2clean)-2) #> (-40)
702 min_val = numpy.around(numpy.amin(func2clean)-2) #> (-40)
703 if min_val <= -40 :
703 if min_val <= -40 :
704 min_val = -40
704 min_val = -40
705 max_val = numpy.around(numpy.amax(func2clean)+2) #< 200
705 max_val = numpy.around(numpy.amax(func2clean)+2) #< 200
706 if max_val >= 200 :
706 if max_val >= 200 :
707 max_val = 200
707 max_val = 200
708 #print min_val, max_val
708 #print min_val, max_val
709 step = 1
709 step = 1
710 #print("Getting bins and the histogram")
710 #print("Getting bins and the histogram")
711 x_dist = min_val + numpy.arange(1 + ((max_val-(min_val))/step))*step
711 x_dist = min_val + numpy.arange(1 + ((max_val-(min_val))/step))*step
712 y_dist,binstep = numpy.histogram(func2clean,bins=range(int(min_val),int(max_val+2),step))
712 y_dist,binstep = numpy.histogram(func2clean,bins=range(int(min_val),int(max_val+2),step))
713 #print(len(y_dist),len(binstep[:-1]))
713 #print(len(y_dist),len(binstep[:-1]))
714 #print(row_ax,col_ax, " ..")
714 #print(row_ax,col_ax, " ..")
715 #print(self.pairsArray[ii][0],self.pairsArray[ii][1])
715 #print(self.pairsArray[ii][0],self.pairsArray[ii][1])
716 mean = numpy.sum(x_dist * y_dist) / numpy.sum(y_dist)
716 mean = numpy.sum(x_dist * y_dist) / numpy.sum(y_dist)
717 sigma = numpy.sqrt(numpy.sum(y_dist * (x_dist - mean)**2) / numpy.sum(y_dist))
717 sigma = numpy.sqrt(numpy.sum(y_dist * (x_dist - mean)**2) / numpy.sum(y_dist))
718 parg = [numpy.amax(y_dist),mean,sigma]
718 parg = [numpy.amax(y_dist),mean,sigma]
719
719
720 newY = None
720 newY = None
721
721
722 try :
722 try :
723 gauss_fit, covariance = curve_fit(fit_func, x_dist, y_dist,p0=parg)
723 gauss_fit, covariance = curve_fit(fit_func, x_dist, y_dist,p0=parg)
724 mode = gauss_fit[1]
724 mode = gauss_fit[1]
725 stdv = gauss_fit[2]
725 stdv = gauss_fit[2]
726 #print(" FIT OK",gauss_fit)
726 #print(" FIT OK",gauss_fit)
727 '''
727 '''
728 ###ONLY FOR TEST:
728 ###ONLY FOR TEST:
729 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
729 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
730 newY = fit_func(x_dist,gauss_fit[0],gauss_fit[1],gauss_fit[2])
730 newY = fit_func(x_dist,gauss_fit[0],gauss_fit[1],gauss_fit[2])
731 axs[row_ax,col_ax].plot(binstep[:-1],y_dist,color='green')
731 axs[row_ax,col_ax].plot(binstep[:-1],y_dist,color='green')
732 axs[row_ax,col_ax].plot(binstep[:-1],newY,color='red')
732 axs[row_ax,col_ax].plot(binstep[:-1],newY,color='red')
733 axs[row_ax,col_ax].set_title("CH "+str(self.channels[ii]))
733 axs[row_ax,col_ax].set_title("CH "+str(self.channels[ii]))
734 '''
734 '''
735 except:
735 except:
736 mode = mean
736 mode = mean
737 stdv = sigma
737 stdv = sigma
738 #print("FIT FAIL")
738 #print("FIT FAIL")
739 #continue
739 #continue
740
740
741
741
742 #print(mode,stdv)
742 #print(mode,stdv)
743 #Removing echoes greater than mode + std_factor*stdv
743 #Removing echoes greater than mode + std_factor*stdv
744 noval = (abs(func2clean - mode)>=(factor_stdv*stdv)).nonzero()
744 noval = (abs(func2clean - mode)>=(factor_stdv*stdv)).nonzero()
745 #noval tiene los indices que se van a remover
745 #noval tiene los indices que se van a remover
746 #print("Chan ",ii," novals: ",len(noval[0]))
746 #print("Chan ",ii," novals: ",len(noval[0]))
747 if len(noval[0]) > 0: #forma de array (N,) es igual a longitud (N)
747 if len(noval[0]) > 0: #forma de array (N,) es igual a longitud (N)
748 novall = ((func2clean - mode) >= (factor_stdv*stdv)).nonzero()
748 novall = ((func2clean - mode) >= (factor_stdv*stdv)).nonzero()
749 #print(novall)
749 #print(novall)
750 #print(" ",self.pairsArray[ii])
750 #print(" ",self.pairsArray[ii])
751 #cross_pairs = self.pairsArray[ii]
751 #cross_pairs = self.pairsArray[ii]
752 #Getting coherent echoes which are removed.
752 #Getting coherent echoes which are removed.
753 # if len(novall[0]) > 0:
753 # if len(novall[0]) > 0:
754 #
754 #
755 # val_spc[novall[0],cross_pairs[0],ifreq,ih] = 1
755 # val_spc[novall[0],cross_pairs[0],ifreq,ih] = 1
756 # val_spc[novall[0],cross_pairs[1],ifreq,ih] = 1
756 # val_spc[novall[0],cross_pairs[1],ifreq,ih] = 1
757 # val_cspc[novall[0],ii,ifreq,ih] = 1
757 # val_cspc[novall[0],ii,ifreq,ih] = 1
758 #print("OUT NOVALL 1")
758 #print("OUT NOVALL 1")
759 try:
759 try:
760 pair = (self.channels[ii],self.channels[ii + 1])
760 pair = (self.channels[ii],self.channels[ii + 1])
761 except:
761 except:
762 pair = (99,99)
762 pair = (99,99)
763 #print("par ", pair)
763 #print("par ", pair)
764 if ( pair in self.crosspairs):
764 if ( pair in self.crosspairs):
765 q = self.crosspairs.index(pair)
765 q = self.crosspairs.index(pair)
766 #print("está aqui: ", q, (ii,ii + 1))
766 #print("está aqui: ", q, (ii,ii + 1))
767 new_a = numpy.delete(cspectra[:,q,ifreq,ih], noval[0])
767 new_a = numpy.delete(cspectra[:,q,ifreq,ih], noval[0])
768 cspectra[noval,q,ifreq,ih] = numpy.mean(new_a) #mean CrossSpectra
768 cspectra[noval,q,ifreq,ih] = numpy.mean(new_a) #mean CrossSpectra
769
769
770 #if channelA_ok:
770 #if channelA_ok:
771 #chA = self.channels.index(cross_pairs[0])
771 #chA = self.channels.index(cross_pairs[0])
772 new_b = numpy.delete(spectra[:,ii,ifreq,ih], noval[0])
772 new_b = numpy.delete(spectra[:,ii,ifreq,ih], noval[0])
773 spectra[noval,ii,ifreq,ih] = numpy.mean(new_b) #mean Spectra Pair A
773 spectra[noval,ii,ifreq,ih] = numpy.mean(new_b) #mean Spectra Pair A
774 #channelA_ok = False
774 #channelA_ok = False
775
775
776 # chB = self.channels.index(cross_pairs[1])
776 # chB = self.channels.index(cross_pairs[1])
777 # new_c = numpy.delete(spectra[:,chB,ifreq,ih], noval[0])
777 # new_c = numpy.delete(spectra[:,chB,ifreq,ih], noval[0])
778 # spectra[noval,chB,ifreq,ih] = numpy.mean(new_c) #mean Spectra Pair B
778 # spectra[noval,chB,ifreq,ih] = numpy.mean(new_c) #mean Spectra Pair B
779 #
779 #
780 # channelsProcssd.append(self.crosspairs[ii][0]) # save channel A
780 # channelsProcssd.append(self.crosspairs[ii][0]) # save channel A
781 # channelsProcssd.append(self.crosspairs[ii][1]) # save channel B
781 # channelsProcssd.append(self.crosspairs[ii][1]) # save channel B
782 '''
782 '''
783 ###ONLY FOR TEST:
783 ###ONLY FOR TEST:
784 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
784 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
785 func2clean = 10*numpy.log10(numpy.absolute(spectra[:,ii,ifreq,ih]))
785 func2clean = 10*numpy.log10(numpy.absolute(spectra[:,ii,ifreq,ih]))
786 y_dist,binstep = numpy.histogram(func2clean,bins=range(int(min_val),int(max_val+2),step))
786 y_dist,binstep = numpy.histogram(func2clean,bins=range(int(min_val),int(max_val+2),step))
787 axs2[row_ax,col_ax].plot(binstep[:-1],newY,color='red')
787 axs2[row_ax,col_ax].plot(binstep[:-1],newY,color='red')
788 axs2[row_ax,col_ax].plot(binstep[:-1],y_dist,color='green')
788 axs2[row_ax,col_ax].plot(binstep[:-1],y_dist,color='green')
789 axs2[row_ax,col_ax].set_title("CH "+str(self.channels[ii]))
789 axs2[row_ax,col_ax].set_title("CH "+str(self.channels[ii]))
790 '''
790 '''
791 '''
791 '''
792 ###ONLY FOR TEST:
792 ###ONLY FOR TEST:
793 col_ax += 1 #contador de ploteo columnas
793 col_ax += 1 #contador de ploteo columnas
794 ##print(col_ax)
794 ##print(col_ax)
795 ###ONLY FOR TEST:
795 ###ONLY FOR TEST:
796 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
796 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
797 title = str(dataOut.datatime)+" nFFT: "+str(ifreq)+" Alt: "+str(self.heights[ih])+ " km"
797 title = str(dataOut.datatime)+" nFFT: "+str(ifreq)+" Alt: "+str(self.heights[ih])+ " km"
798 title2 = str(dataOut.datatime)+" nFFT: "+str(ifreq)+" Alt: "+str(self.heights[ih])+ " km CLEANED"
798 title2 = str(dataOut.datatime)+" nFFT: "+str(ifreq)+" Alt: "+str(self.heights[ih])+ " km CLEANED"
799 fig.suptitle(title)
799 fig.suptitle(title)
800 fig2.suptitle(title2)
800 fig2.suptitle(title2)
801 plt.show()
801 plt.show()
802 '''
802 '''
803 ##################################################################################################
803 ##################################################################################################
804
804
805 #print("Getting average of the spectra and cross-spectra from incoherent echoes.")
805 #print("Getting average of the spectra and cross-spectra from incoherent echoes.")
806 out_spectra = numpy.zeros([self.nChan,self.nFFTPoints,self.nHeights], dtype=float) #+numpy.nan
806 out_spectra = numpy.zeros([self.nChan,self.nFFTPoints,self.nHeights], dtype=float) #+numpy.nan
807 out_cspectra = numpy.zeros([self.nPairs,self.nFFTPoints,self.nHeights], dtype=complex) #+numpy.nan
807 out_cspectra = numpy.zeros([self.nPairs,self.nFFTPoints,self.nHeights], dtype=complex) #+numpy.nan
808 for ih in range(self.nHeights):
808 for ih in range(self.nHeights):
809 for ifreq in range(self.nFFTPoints):
809 for ifreq in range(self.nFFTPoints):
810 for ich in range(self.nChan):
810 for ich in range(self.nChan):
811 tmp = spectra[:,ich,ifreq,ih]
811 tmp = spectra[:,ich,ifreq,ih]
812 valid = (numpy.isfinite(tmp[:])==True).nonzero()
812 valid = (numpy.isfinite(tmp[:])==True).nonzero()
813
813
814 if len(valid[0]) >0 :
814 if len(valid[0]) >0 :
815 out_spectra[ich,ifreq,ih] = numpy.nansum(tmp)#/len(valid[0])
815 out_spectra[ich,ifreq,ih] = numpy.nansum(tmp)#/len(valid[0])
816
816
817 for icr in range(self.nPairs):
817 for icr in range(self.nPairs):
818 tmp = numpy.squeeze(cspectra[:,icr,ifreq,ih])
818 tmp = numpy.squeeze(cspectra[:,icr,ifreq,ih])
819 valid = (numpy.isfinite(tmp)==True).nonzero()
819 valid = (numpy.isfinite(tmp)==True).nonzero()
820 if len(valid[0]) > 0:
820 if len(valid[0]) > 0:
821 out_cspectra[icr,ifreq,ih] = numpy.nansum(tmp)#/len(valid[0])
821 out_cspectra[icr,ifreq,ih] = numpy.nansum(tmp)#/len(valid[0])
822
822
823 return out_spectra, out_cspectra
823 return out_spectra, out_cspectra
824
824
825 def REM_ISOLATED_POINTS(self,array,rth):
825 def REM_ISOLATED_POINTS(self,array,rth):
826 # import matplotlib.pyplot as plt
826 # import matplotlib.pyplot as plt
827 if rth == None :
827 if rth == None :
828 rth = 4
828 rth = 4
829 #print("REM ISO")
829 #print("REM ISO")
830 num_prof = len(array[0,:,0])
830 num_prof = len(array[0,:,0])
831 num_hei = len(array[0,0,:])
831 num_hei = len(array[0,0,:])
832 n2d = len(array[:,0,0])
832 n2d = len(array[:,0,0])
833
833
834 for ii in range(n2d) :
834 for ii in range(n2d) :
835 #print ii,n2d
835 #print ii,n2d
836 tmp = array[ii,:,:]
836 tmp = array[ii,:,:]
837 #print tmp.shape, array[ii,101,:],array[ii,102,:]
837 #print tmp.shape, array[ii,101,:],array[ii,102,:]
838
838
839 # fig = plt.figure(figsize=(6,5))
839 # fig = plt.figure(figsize=(6,5))
840 # left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
840 # left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
841 # ax = fig.add_axes([left, bottom, width, height])
841 # ax = fig.add_axes([left, bottom, width, height])
842 # x = range(num_prof)
842 # x = range(num_prof)
843 # y = range(num_hei)
843 # y = range(num_hei)
844 # cp = ax.contour(y,x,tmp)
844 # cp = ax.contour(y,x,tmp)
845 # ax.clabel(cp, inline=True,fontsize=10)
845 # ax.clabel(cp, inline=True,fontsize=10)
846 # plt.show()
846 # plt.show()
847
847
848 #indxs = WHERE(FINITE(tmp) AND tmp GT 0,cindxs)
848 #indxs = WHERE(FINITE(tmp) AND tmp GT 0,cindxs)
849 tmp = numpy.reshape(tmp,num_prof*num_hei)
849 tmp = numpy.reshape(tmp,num_prof*num_hei)
850 indxs1 = (numpy.isfinite(tmp)==True).nonzero()
850 indxs1 = (numpy.isfinite(tmp)==True).nonzero()
851 indxs2 = (tmp > 0).nonzero()
851 indxs2 = (tmp > 0).nonzero()
852
852
853 indxs1 = (indxs1[0])
853 indxs1 = (indxs1[0])
854 indxs2 = indxs2[0]
854 indxs2 = indxs2[0]
855 #indxs1 = numpy.array(indxs1[0])
855 #indxs1 = numpy.array(indxs1[0])
856 #indxs2 = numpy.array(indxs2[0])
856 #indxs2 = numpy.array(indxs2[0])
857 indxs = None
857 indxs = None
858 #print indxs1 , indxs2
858 #print indxs1 , indxs2
859 for iv in range(len(indxs2)):
859 for iv in range(len(indxs2)):
860 indv = numpy.array((indxs1 == indxs2[iv]).nonzero())
860 indv = numpy.array((indxs1 == indxs2[iv]).nonzero())
861 #print len(indxs2), indv
861 #print len(indxs2), indv
862 if len(indv[0]) > 0 :
862 if len(indv[0]) > 0 :
863 indxs = numpy.concatenate((indxs,indxs2[iv]), axis=None)
863 indxs = numpy.concatenate((indxs,indxs2[iv]), axis=None)
864 # print indxs
864 # print indxs
865 indxs = indxs[1:]
865 indxs = indxs[1:]
866 #print(indxs, len(indxs))
866 #print(indxs, len(indxs))
867 if len(indxs) < 4 :
867 if len(indxs) < 4 :
868 array[ii,:,:] = 0.
868 array[ii,:,:] = 0.
869 return
869 return
870
870
871 xpos = numpy.mod(indxs ,num_hei)
871 xpos = numpy.mod(indxs ,num_hei)
872 ypos = (indxs / num_hei)
872 ypos = (indxs / num_hei)
873 sx = numpy.argsort(xpos) # Ordering respect to "x" (time)
873 sx = numpy.argsort(xpos) # Ordering respect to "x" (time)
874 #print sx
874 #print sx
875 xpos = xpos[sx]
875 xpos = xpos[sx]
876 ypos = ypos[sx]
876 ypos = ypos[sx]
877
877
878 # *********************************** Cleaning isolated points **********************************
878 # *********************************** Cleaning isolated points **********************************
879 ic = 0
879 ic = 0
880 while True :
880 while True :
881 r = numpy.sqrt(list(numpy.power((xpos[ic]-xpos),2)+ numpy.power((ypos[ic]-ypos),2)))
881 r = numpy.sqrt(list(numpy.power((xpos[ic]-xpos),2)+ numpy.power((ypos[ic]-ypos),2)))
882 #no_coh = WHERE(FINITE(r) AND (r LE rth),cno_coh)
882 #no_coh = WHERE(FINITE(r) AND (r LE rth),cno_coh)
883 #plt.plot(r)
883 #plt.plot(r)
884 #plt.show()
884 #plt.show()
885 no_coh1 = (numpy.isfinite(r)==True).nonzero()
885 no_coh1 = (numpy.isfinite(r)==True).nonzero()
886 no_coh2 = (r <= rth).nonzero()
886 no_coh2 = (r <= rth).nonzero()
887 #print r, no_coh1, no_coh2
887 #print r, no_coh1, no_coh2
888 no_coh1 = numpy.array(no_coh1[0])
888 no_coh1 = numpy.array(no_coh1[0])
889 no_coh2 = numpy.array(no_coh2[0])
889 no_coh2 = numpy.array(no_coh2[0])
890 no_coh = None
890 no_coh = None
891 #print valid1 , valid2
891 #print valid1 , valid2
892 for iv in range(len(no_coh2)):
892 for iv in range(len(no_coh2)):
893 indv = numpy.array((no_coh1 == no_coh2[iv]).nonzero())
893 indv = numpy.array((no_coh1 == no_coh2[iv]).nonzero())
894 if len(indv[0]) > 0 :
894 if len(indv[0]) > 0 :
895 no_coh = numpy.concatenate((no_coh,no_coh2[iv]), axis=None)
895 no_coh = numpy.concatenate((no_coh,no_coh2[iv]), axis=None)
896 no_coh = no_coh[1:]
896 no_coh = no_coh[1:]
897 #print len(no_coh), no_coh
897 #print len(no_coh), no_coh
898 if len(no_coh) < 4 :
898 if len(no_coh) < 4 :
899 #print xpos[ic], ypos[ic], ic
899 #print xpos[ic], ypos[ic], ic
900 # plt.plot(r)
900 # plt.plot(r)
901 # plt.show()
901 # plt.show()
902 xpos[ic] = numpy.nan
902 xpos[ic] = numpy.nan
903 ypos[ic] = numpy.nan
903 ypos[ic] = numpy.nan
904
904
905 ic = ic + 1
905 ic = ic + 1
906 if (ic == len(indxs)) :
906 if (ic == len(indxs)) :
907 break
907 break
908 #print( xpos, ypos)
908 #print( xpos, ypos)
909
909
910 indxs = (numpy.isfinite(list(xpos))==True).nonzero()
910 indxs = (numpy.isfinite(list(xpos))==True).nonzero()
911 #print indxs[0]
911 #print indxs[0]
912 if len(indxs[0]) < 4 :
912 if len(indxs[0]) < 4 :
913 array[ii,:,:] = 0.
913 array[ii,:,:] = 0.
914 return
914 return
915
915
916 xpos = xpos[indxs[0]]
916 xpos = xpos[indxs[0]]
917 ypos = ypos[indxs[0]]
917 ypos = ypos[indxs[0]]
918 for i in range(0,len(ypos)):
918 for i in range(0,len(ypos)):
919 ypos[i]=int(ypos[i])
919 ypos[i]=int(ypos[i])
920 junk = tmp
920 junk = tmp
921 tmp = junk*0.0
921 tmp = junk*0.0
922
922
923 tmp[list(xpos + (ypos*num_hei))] = junk[list(xpos + (ypos*num_hei))]
923 tmp[list(xpos + (ypos*num_hei))] = junk[list(xpos + (ypos*num_hei))]
924 array[ii,:,:] = numpy.reshape(tmp,(num_prof,num_hei))
924 array[ii,:,:] = numpy.reshape(tmp,(num_prof,num_hei))
925
925
926 #print array.shape
926 #print array.shape
927 #tmp = numpy.reshape(tmp,(num_prof,num_hei))
927 #tmp = numpy.reshape(tmp,(num_prof,num_hei))
928 #print tmp.shape
928 #print tmp.shape
929
929
930 # fig = plt.figure(figsize=(6,5))
930 # fig = plt.figure(figsize=(6,5))
931 # left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
931 # left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
932 # ax = fig.add_axes([left, bottom, width, height])
932 # ax = fig.add_axes([left, bottom, width, height])
933 # x = range(num_prof)
933 # x = range(num_prof)
934 # y = range(num_hei)
934 # y = range(num_hei)
935 # cp = ax.contour(y,x,array[ii,:,:])
935 # cp = ax.contour(y,x,array[ii,:,:])
936 # ax.clabel(cp, inline=True,fontsize=10)
936 # ax.clabel(cp, inline=True,fontsize=10)
937 # plt.show()
937 # plt.show()
938 return array
938 return array
939
939
940
941 class IntegrationFaradaySpectra(Operation):
942
943 __profIndex = 0
944 __withOverapping = False
945
946 __byTime = False
947 __initime = None
948 __lastdatatime = None
949 __integrationtime = None
950
951 __buffer_spc = None
952 __buffer_cspc = None
953 __buffer_dc = None
954
955 __dataReady = False
956
957 __timeInterval = None
958
959 n = None
960
961 def __init__(self):
962
963 Operation.__init__(self)
964
965 def setup(self, dataOut,n=None, timeInterval=None, overlapping=False, DPL=None):
966 """
967 Set the parameters of the integration class.
968
969 Inputs:
970
971 n : Number of coherent integrations
972 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
973 overlapping :
974
975 """
976
977 self.__initime = None
978 self.__lastdatatime = 0
979
980 self.__buffer_spc = []
981 self.__buffer_cspc = []
982 self.__buffer_dc = 0
983
984 self.__profIndex = 0
985 self.__dataReady = False
986 self.__byTime = False
987
988 #self.ByLags = dataOut.ByLags ###REDEFINIR
989 self.ByLags = False
990
991 if DPL != None:
992 self.DPL=DPL
993 else:
994 #self.DPL=dataOut.DPL ###REDEFINIR
995 self.DPL=0
996
997 if n is None and timeInterval is None:
998 raise ValueError("n or timeInterval should be specified ...")
999
1000 if n is not None:
1001 self.n = int(n)
1002 else:
1003
1004 self.__integrationtime = int(timeInterval)
1005 self.n = None
1006 self.__byTime = True
1007
1008 def putData(self, data_spc, data_cspc, data_dc):
1009 """
1010 Add a profile to the __buffer_spc and increase in one the __profileIndex
1011
1012 """
1013
1014 self.__buffer_spc.append(data_spc)
1015
1016 if data_cspc is None:
1017 self.__buffer_cspc = None
1018 else:
1019 self.__buffer_cspc.append(data_cspc)
1020
1021 if data_dc is None:
1022 self.__buffer_dc = None
1023 else:
1024 self.__buffer_dc += data_dc
1025
1026 self.__profIndex += 1
1027
1028 return
1029
1030 def hildebrand_sekhon_Integration(self,data,navg):
1031
1032 sortdata = numpy.sort(data, axis=None)
1033 sortID=data.argsort()
1034 lenOfData = len(sortdata)
1035 nums_min = lenOfData*0.75
1036 if nums_min <= 5:
1037 nums_min = 5
1038 sump = 0.
1039 sumq = 0.
1040 j = 0
1041 cont = 1
1042 while((cont == 1)and(j < lenOfData)):
1043 sump += sortdata[j]
1044 sumq += sortdata[j]**2
1045 if j > nums_min:
1046 rtest = float(j)/(j-1) + 1.0/navg
1047 if ((sumq*j) > (rtest*sump**2)):
1048 j = j - 1
1049 sump = sump - sortdata[j]
1050 sumq = sumq - sortdata[j]**2
1051 cont = 0
1052 j += 1
1053 #lnoise = sump / j
1054
1055 return j,sortID
1056
1057 def pushData(self):
1058 """
1059 Return the sum of the last profiles and the profiles used in the sum.
1060
1061 Affected:
1062
1063 self.__profileIndex
1064
1065 """
1066 bufferH=None
1067 buffer=None
1068 buffer1=None
1069 buffer_cspc=None
1070 self.__buffer_spc=numpy.array(self.__buffer_spc)
1071 self.__buffer_cspc=numpy.array(self.__buffer_cspc)
1072 freq_dc = int(self.__buffer_spc.shape[2] / 2)
1073 #print("FREQ_DC",freq_dc,self.__buffer_spc.shape,self.nHeights)
1074 for k in range(7,self.nHeights):
1075 buffer_cspc=numpy.copy(self.__buffer_cspc[:,:,:,k])
1076 outliers_IDs_cspc=[]
1077 cspc_outliers_exist=False
1078 #print("AQUIII")
1079 for i in range(self.nChannels):#dataOut.nChannels):
1080
1081 buffer1=numpy.copy(self.__buffer_spc[:,i,:,k])
1082 indexes=[]
1083 #sortIDs=[]
1084 outliers_IDs=[]
1085
1086 for j in range(self.nProfiles):
1087 # if i==0 and j==freq_dc: #NOT CONSIDERING DC PROFILE AT CHANNEL 0
1088 # continue
1089 # if i==1 and j==0: #NOT CONSIDERING DC PROFILE AT CHANNEL 1
1090 # continue
1091 buffer=buffer1[:,j]
1092 index,sortID=self.hildebrand_sekhon_Integration(buffer,1)
1093
1094 indexes.append(index)
1095 #sortIDs.append(sortID)
1096 outliers_IDs=numpy.append(outliers_IDs,sortID[index:])
1097
1098 outliers_IDs=numpy.array(outliers_IDs)
1099 outliers_IDs=outliers_IDs.ravel()
1100 outliers_IDs=numpy.unique(outliers_IDs)
1101 outliers_IDs=outliers_IDs.astype(numpy.dtype('int64'))
1102 indexes=numpy.array(indexes)
1103 indexmin=numpy.min(indexes)
1104
1105 if indexmin != buffer1.shape[0]:
1106 cspc_outliers_exist=True
1107 ###sortdata=numpy.sort(buffer1,axis=0)
1108 ###avg2=numpy.mean(sortdata[:indexmin,:],axis=0)
1109 lt=outliers_IDs
1110 avg=numpy.mean(buffer1[[t for t in range(buffer1.shape[0]) if t not in lt],:],axis=0)
1111
1112 for p in list(outliers_IDs):
1113 buffer1[p,:]=avg
1114
1115 self.__buffer_spc[:,i,:,k]=numpy.copy(buffer1)
1116 ###cspc IDs
1117 #indexmin_cspc+=indexmin_cspc
1118 outliers_IDs_cspc=numpy.append(outliers_IDs_cspc,outliers_IDs)
1119
1120 #if not breakFlag:
1121 outliers_IDs_cspc=outliers_IDs_cspc.astype(numpy.dtype('int64'))
1122 if cspc_outliers_exist:
1123 #sortdata=numpy.sort(buffer_cspc,axis=0)
1124 #avg=numpy.mean(sortdata[:indexmin_cpsc,:],axis=0)
1125 lt=outliers_IDs_cspc
1126
1127 avg=numpy.mean(buffer_cspc[[t for t in range(buffer_cspc.shape[0]) if t not in lt],:],axis=0)
1128 for p in list(outliers_IDs_cspc):
1129 buffer_cspc[p,:]=avg
1130
1131 self.__buffer_cspc[:,:,:,k]=numpy.copy(buffer_cspc)
1132 #else:
1133 #break
1134
1135
1136
1137
1138 buffer=None
1139 bufferH=None
1140 buffer1=None
1141 buffer_cspc=None
1142
1143 #print("cpsc",self.__buffer_cspc[:,0,0,0,0])
1144 #print(self.__profIndex)
1145 #exit()
1146
1147 buffer=None
1148 #print(self.__buffer_spc[:,1,3,20,0])
1149 #print(self.__buffer_spc[:,1,5,37,0])
1150 data_spc = numpy.sum(self.__buffer_spc,axis=0)
1151 data_cspc = numpy.sum(self.__buffer_cspc,axis=0)
1152
1153 #print(numpy.shape(data_spc))
1154 #data_spc[1,4,20,0]=numpy.nan
1155
1156 #data_cspc = self.__buffer_cspc
1157 data_dc = self.__buffer_dc
1158 n = self.__profIndex
1159
1160 self.__buffer_spc = []
1161 self.__buffer_cspc = []
1162 self.__buffer_dc = 0
1163 self.__profIndex = 0
1164
1165 return data_spc, data_cspc, data_dc, n
1166
1167 def byProfiles(self, *args):
1168
1169 self.__dataReady = False
1170 avgdata_spc = None
1171 avgdata_cspc = None
1172 avgdata_dc = None
1173
1174 self.putData(*args)
1175
1176 if self.__profIndex == self.n:
1177
1178 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1179 self.n = n
1180 self.__dataReady = True
1181
1182 return avgdata_spc, avgdata_cspc, avgdata_dc
1183
1184 def byTime(self, datatime, *args):
1185
1186 self.__dataReady = False
1187 avgdata_spc = None
1188 avgdata_cspc = None
1189 avgdata_dc = None
1190
1191 self.putData(*args)
1192
1193 if (datatime - self.__initime) >= self.__integrationtime:
1194 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1195 self.n = n
1196 self.__dataReady = True
1197
1198 return avgdata_spc, avgdata_cspc, avgdata_dc
1199
1200 def integrate(self, datatime, *args):
1201
1202 if self.__profIndex == 0:
1203 self.__initime = datatime
1204
1205 if self.__byTime:
1206 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(
1207 datatime, *args)
1208 else:
1209 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
1210
1211 if not self.__dataReady:
1212 return None, None, None, None
1213
1214 return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc
1215
1216 def run(self, dataOut, n=None, DPL = None,timeInterval=None, overlapping=False):
1217 if n == 1:
1218 return dataOut
1219
1220 dataOut.flagNoData = True
1221
1222 if not self.isConfig:
1223 self.setup(dataOut, n, timeInterval, overlapping,DPL )
1224 self.isConfig = True
1225
1226 if not self.ByLags:
1227 self.nProfiles=dataOut.nProfiles
1228 self.nChannels=dataOut.nChannels
1229 self.nHeights=dataOut.nHeights
1230 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime,
1231 dataOut.data_spc,
1232 dataOut.data_cspc,
1233 dataOut.data_dc)
1234 else:
1235 self.nProfiles=dataOut.nProfiles
1236 self.nChannels=dataOut.nChannels
1237 self.nHeights=dataOut.nHeights
1238 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime,
1239 dataOut.dataLag_spc,
1240 dataOut.dataLag_cspc,
1241 dataOut.dataLag_dc)
1242
1243 if self.__dataReady:
1244
1245 if not self.ByLags:
1246
1247 dataOut.data_spc = numpy.squeeze(avgdata_spc)
1248 dataOut.data_cspc = numpy.squeeze(avgdata_cspc)
1249 dataOut.data_dc = avgdata_dc
1250 else:
1251 dataOut.dataLag_spc = avgdata_spc
1252 dataOut.dataLag_cspc = avgdata_cspc
1253 dataOut.dataLag_dc = avgdata_dc
1254
1255 dataOut.data_spc=dataOut.dataLag_spc[:,:,:,dataOut.LagPlot]
1256 dataOut.data_cspc=dataOut.dataLag_cspc[:,:,:,dataOut.LagPlot]
1257 dataOut.data_dc=dataOut.dataLag_dc[:,:,dataOut.LagPlot]
1258
1259
1260 dataOut.nIncohInt *= self.n
1261 dataOut.utctime = avgdatatime
1262 dataOut.flagNoData = False
1263
1264 return dataOut
1265
940 class removeInterference(Operation):
1266 class removeInterference(Operation):
941
1267
942 def removeInterference2(self):
1268 def removeInterference2(self):
943
1269
944 cspc = self.dataOut.data_cspc
1270 cspc = self.dataOut.data_cspc
945 spc = self.dataOut.data_spc
1271 spc = self.dataOut.data_spc
946 Heights = numpy.arange(cspc.shape[2])
1272 Heights = numpy.arange(cspc.shape[2])
947 realCspc = numpy.abs(cspc)
1273 realCspc = numpy.abs(cspc)
948
1274
949 for i in range(cspc.shape[0]):
1275 for i in range(cspc.shape[0]):
950 LinePower= numpy.sum(realCspc[i], axis=0)
1276 LinePower= numpy.sum(realCspc[i], axis=0)
951 Threshold = numpy.amax(LinePower)-numpy.sort(LinePower)[len(Heights)-int(len(Heights)*0.1)]
1277 Threshold = numpy.amax(LinePower)-numpy.sort(LinePower)[len(Heights)-int(len(Heights)*0.1)]
952 SelectedHeights = Heights[ numpy.where( LinePower < Threshold ) ]
1278 SelectedHeights = Heights[ numpy.where( LinePower < Threshold ) ]
953 InterferenceSum = numpy.sum( realCspc[i,:,SelectedHeights], axis=0 )
1279 InterferenceSum = numpy.sum( realCspc[i,:,SelectedHeights], axis=0 )
954 InterferenceThresholdMin = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.98)]
1280 InterferenceThresholdMin = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.98)]
955 InterferenceThresholdMax = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.99)]
1281 InterferenceThresholdMax = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.99)]
956
1282
957
1283
958 InterferenceRange = numpy.where( ([InterferenceSum > InterferenceThresholdMin]))# , InterferenceSum < InterferenceThresholdMax]) )
1284 InterferenceRange = numpy.where( ([InterferenceSum > InterferenceThresholdMin]))# , InterferenceSum < InterferenceThresholdMax]) )
959 #InterferenceRange = numpy.where( ([InterferenceRange < InterferenceThresholdMax]))
1285 #InterferenceRange = numpy.where( ([InterferenceRange < InterferenceThresholdMax]))
960 if len(InterferenceRange)<int(cspc.shape[1]*0.3):
1286 if len(InterferenceRange)<int(cspc.shape[1]*0.3):
961 cspc[i,InterferenceRange,:] = numpy.NaN
1287 cspc[i,InterferenceRange,:] = numpy.NaN
962
1288
963 self.dataOut.data_cspc = cspc
1289 self.dataOut.data_cspc = cspc
964
1290
965 def removeInterference(self, interf = 2, hei_interf = None, nhei_interf = None, offhei_interf = None):
1291 def removeInterference(self, interf = 2, hei_interf = None, nhei_interf = None, offhei_interf = None):
966
1292
967 jspectra = self.dataOut.data_spc
1293 jspectra = self.dataOut.data_spc
968 jcspectra = self.dataOut.data_cspc
1294 jcspectra = self.dataOut.data_cspc
969 jnoise = self.dataOut.getNoise()
1295 jnoise = self.dataOut.getNoise()
970 num_incoh = self.dataOut.nIncohInt
1296 num_incoh = self.dataOut.nIncohInt
971
1297
972 num_channel = jspectra.shape[0]
1298 num_channel = jspectra.shape[0]
973 num_prof = jspectra.shape[1]
1299 num_prof = jspectra.shape[1]
974 num_hei = jspectra.shape[2]
1300 num_hei = jspectra.shape[2]
975
1301
976 # hei_interf
1302 # hei_interf
977 if hei_interf is None:
1303 if hei_interf is None:
978 count_hei = int(num_hei / 2)
1304 count_hei = int(num_hei / 2)
979 hei_interf = numpy.asmatrix(list(range(count_hei))) + num_hei - count_hei
1305 hei_interf = numpy.asmatrix(list(range(count_hei))) + num_hei - count_hei
980 hei_interf = numpy.asarray(hei_interf)[0]
1306 hei_interf = numpy.asarray(hei_interf)[0]
981 # nhei_interf
1307 # nhei_interf
982 if (nhei_interf == None):
1308 if (nhei_interf == None):
983 nhei_interf = 5
1309 nhei_interf = 5
984 if (nhei_interf < 1):
1310 if (nhei_interf < 1):
985 nhei_interf = 1
1311 nhei_interf = 1
986 if (nhei_interf > count_hei):
1312 if (nhei_interf > count_hei):
987 nhei_interf = count_hei
1313 nhei_interf = count_hei
988 if (offhei_interf == None):
1314 if (offhei_interf == None):
989 offhei_interf = 0
1315 offhei_interf = 0
990
1316
991 ind_hei = list(range(num_hei))
1317 ind_hei = list(range(num_hei))
992 # mask_prof = numpy.asarray(range(num_prof - 2)) + 1
1318 # mask_prof = numpy.asarray(range(num_prof - 2)) + 1
993 # mask_prof[range(num_prof/2 - 1,len(mask_prof))] += 1
1319 # mask_prof[range(num_prof/2 - 1,len(mask_prof))] += 1
994 mask_prof = numpy.asarray(list(range(num_prof)))
1320 mask_prof = numpy.asarray(list(range(num_prof)))
995 num_mask_prof = mask_prof.size
1321 num_mask_prof = mask_prof.size
996 comp_mask_prof = [0, num_prof / 2]
1322 comp_mask_prof = [0, num_prof / 2]
997
1323
998 # noise_exist: Determina si la variable jnoise ha sido definida y contiene la informacion del ruido de cada canal
1324 # noise_exist: Determina si la variable jnoise ha sido definida y contiene la informacion del ruido de cada canal
999 if (jnoise.size < num_channel or numpy.isnan(jnoise).any()):
1325 if (jnoise.size < num_channel or numpy.isnan(jnoise).any()):
1000 jnoise = numpy.nan
1326 jnoise = numpy.nan
1001 noise_exist = jnoise[0] < numpy.Inf
1327 noise_exist = jnoise[0] < numpy.Inf
1002
1328
1003 # Subrutina de Remocion de la Interferencia
1329 # Subrutina de Remocion de la Interferencia
1004 for ich in range(num_channel):
1330 for ich in range(num_channel):
1005 # Se ordena los espectros segun su potencia (menor a mayor)
1331 # Se ordena los espectros segun su potencia (menor a mayor)
1006 power = jspectra[ich, mask_prof, :]
1332 power = jspectra[ich, mask_prof, :]
1007 power = power[:, hei_interf]
1333 power = power[:, hei_interf]
1008 power = power.sum(axis=0)
1334 power = power.sum(axis=0)
1009 psort = power.ravel().argsort()
1335 psort = power.ravel().argsort()
1010
1336
1011 # Se estima la interferencia promedio en los Espectros de Potencia empleando
1337 # Se estima la interferencia promedio en los Espectros de Potencia empleando
1012 junkspc_interf = jspectra[ich, :, hei_interf[psort[list(range(
1338 junkspc_interf = jspectra[ich, :, hei_interf[psort[list(range(
1013 offhei_interf, nhei_interf + offhei_interf))]]]
1339 offhei_interf, nhei_interf + offhei_interf))]]]
1014
1340
1015 if noise_exist:
1341 if noise_exist:
1016 # tmp_noise = jnoise[ich] / num_prof
1342 # tmp_noise = jnoise[ich] / num_prof
1017 tmp_noise = jnoise[ich]
1343 tmp_noise = jnoise[ich]
1018 junkspc_interf = junkspc_interf - tmp_noise
1344 junkspc_interf = junkspc_interf - tmp_noise
1019 #junkspc_interf[:,comp_mask_prof] = 0
1345 #junkspc_interf[:,comp_mask_prof] = 0
1020
1346
1021 jspc_interf = junkspc_interf.sum(axis=0) / nhei_interf
1347 jspc_interf = junkspc_interf.sum(axis=0) / nhei_interf
1022 jspc_interf = jspc_interf.transpose()
1348 jspc_interf = jspc_interf.transpose()
1023 # Calculando el espectro de interferencia promedio
1349 # Calculando el espectro de interferencia promedio
1024 noiseid = numpy.where(
1350 noiseid = numpy.where(
1025 jspc_interf <= tmp_noise / numpy.sqrt(num_incoh))
1351 jspc_interf <= tmp_noise / numpy.sqrt(num_incoh))
1026 noiseid = noiseid[0]
1352 noiseid = noiseid[0]
1027 cnoiseid = noiseid.size
1353 cnoiseid = noiseid.size
1028 interfid = numpy.where(
1354 interfid = numpy.where(
1029 jspc_interf > tmp_noise / numpy.sqrt(num_incoh))
1355 jspc_interf > tmp_noise / numpy.sqrt(num_incoh))
1030 interfid = interfid[0]
1356 interfid = interfid[0]
1031 cinterfid = interfid.size
1357 cinterfid = interfid.size
1032
1358
1033 if (cnoiseid > 0):
1359 if (cnoiseid > 0):
1034 jspc_interf[noiseid] = 0
1360 jspc_interf[noiseid] = 0
1035
1361
1036 # Expandiendo los perfiles a limpiar
1362 # Expandiendo los perfiles a limpiar
1037 if (cinterfid > 0):
1363 if (cinterfid > 0):
1038 new_interfid = (
1364 new_interfid = (
1039 numpy.r_[interfid - 1, interfid, interfid + 1] + num_prof) % num_prof
1365 numpy.r_[interfid - 1, interfid, interfid + 1] + num_prof) % num_prof
1040 new_interfid = numpy.asarray(new_interfid)
1366 new_interfid = numpy.asarray(new_interfid)
1041 new_interfid = {x for x in new_interfid}
1367 new_interfid = {x for x in new_interfid}
1042 new_interfid = numpy.array(list(new_interfid))
1368 new_interfid = numpy.array(list(new_interfid))
1043 new_cinterfid = new_interfid.size
1369 new_cinterfid = new_interfid.size
1044 else:
1370 else:
1045 new_cinterfid = 0
1371 new_cinterfid = 0
1046
1372
1047 for ip in range(new_cinterfid):
1373 for ip in range(new_cinterfid):
1048 ind = junkspc_interf[:, new_interfid[ip]].ravel().argsort()
1374 ind = junkspc_interf[:, new_interfid[ip]].ravel().argsort()
1049 jspc_interf[new_interfid[ip]
1375 jspc_interf[new_interfid[ip]
1050 ] = junkspc_interf[ind[nhei_interf // 2], new_interfid[ip]]
1376 ] = junkspc_interf[ind[nhei_interf // 2], new_interfid[ip]]
1051
1377
1052 jspectra[ich, :, ind_hei] = jspectra[ich, :,
1378 jspectra[ich, :, ind_hei] = jspectra[ich, :,
1053 ind_hei] - jspc_interf # Corregir indices
1379 ind_hei] - jspc_interf # Corregir indices
1054
1380
1055 # Removiendo la interferencia del punto de mayor interferencia
1381 # Removiendo la interferencia del punto de mayor interferencia
1056 ListAux = jspc_interf[mask_prof].tolist()
1382 ListAux = jspc_interf[mask_prof].tolist()
1057 maxid = ListAux.index(max(ListAux))
1383 maxid = ListAux.index(max(ListAux))
1058
1384
1059 if cinterfid > 0:
1385 if cinterfid > 0:
1060 for ip in range(cinterfid * (interf == 2) - 1):
1386 for ip in range(cinterfid * (interf == 2) - 1):
1061 ind = (jspectra[ich, interfid[ip], :] < tmp_noise *
1387 ind = (jspectra[ich, interfid[ip], :] < tmp_noise *
1062 (1 + 1 / numpy.sqrt(num_incoh))).nonzero()
1388 (1 + 1 / numpy.sqrt(num_incoh))).nonzero()
1063 cind = len(ind)
1389 cind = len(ind)
1064
1390
1065 if (cind > 0):
1391 if (cind > 0):
1066 jspectra[ich, interfid[ip], ind] = tmp_noise * \
1392 jspectra[ich, interfid[ip], ind] = tmp_noise * \
1067 (1 + (numpy.random.uniform(cind) - 0.5) /
1393 (1 + (numpy.random.uniform(cind) - 0.5) /
1068 numpy.sqrt(num_incoh))
1394 numpy.sqrt(num_incoh))
1069
1395
1070 ind = numpy.array([-2, -1, 1, 2])
1396 ind = numpy.array([-2, -1, 1, 2])
1071 xx = numpy.zeros([4, 4])
1397 xx = numpy.zeros([4, 4])
1072
1398
1073 for id1 in range(4):
1399 for id1 in range(4):
1074 xx[:, id1] = ind[id1]**numpy.asarray(list(range(4)))
1400 xx[:, id1] = ind[id1]**numpy.asarray(list(range(4)))
1075
1401
1076 xx_inv = numpy.linalg.inv(xx)
1402 xx_inv = numpy.linalg.inv(xx)
1077 xx = xx_inv[:, 0]
1403 xx = xx_inv[:, 0]
1078 ind = (ind + maxid + num_mask_prof) % num_mask_prof
1404 ind = (ind + maxid + num_mask_prof) % num_mask_prof
1079 yy = jspectra[ich, mask_prof[ind], :]
1405 yy = jspectra[ich, mask_prof[ind], :]
1080 jspectra[ich, mask_prof[maxid], :] = numpy.dot(
1406 jspectra[ich, mask_prof[maxid], :] = numpy.dot(
1081 yy.transpose(), xx)
1407 yy.transpose(), xx)
1082
1408
1083 indAux = (jspectra[ich, :, :] < tmp_noise *
1409 indAux = (jspectra[ich, :, :] < tmp_noise *
1084 (1 - 1 / numpy.sqrt(num_incoh))).nonzero()
1410 (1 - 1 / numpy.sqrt(num_incoh))).nonzero()
1085 jspectra[ich, indAux[0], indAux[1]] = tmp_noise * \
1411 jspectra[ich, indAux[0], indAux[1]] = tmp_noise * \
1086 (1 - 1 / numpy.sqrt(num_incoh))
1412 (1 - 1 / numpy.sqrt(num_incoh))
1087
1413
1088 # Remocion de Interferencia en el Cross Spectra
1414 # Remocion de Interferencia en el Cross Spectra
1089 if jcspectra is None:
1415 if jcspectra is None:
1090 return jspectra, jcspectra
1416 return jspectra, jcspectra
1091 num_pairs = int(jcspectra.size / (num_prof * num_hei))
1417 num_pairs = int(jcspectra.size / (num_prof * num_hei))
1092 jcspectra = jcspectra.reshape(num_pairs, num_prof, num_hei)
1418 jcspectra = jcspectra.reshape(num_pairs, num_prof, num_hei)
1093
1419
1094 for ip in range(num_pairs):
1420 for ip in range(num_pairs):
1095
1421
1096 #-------------------------------------------
1422 #-------------------------------------------
1097
1423
1098 cspower = numpy.abs(jcspectra[ip, mask_prof, :])
1424 cspower = numpy.abs(jcspectra[ip, mask_prof, :])
1099 cspower = cspower[:, hei_interf]
1425 cspower = cspower[:, hei_interf]
1100 cspower = cspower.sum(axis=0)
1426 cspower = cspower.sum(axis=0)
1101
1427
1102 cspsort = cspower.ravel().argsort()
1428 cspsort = cspower.ravel().argsort()
1103 junkcspc_interf = jcspectra[ip, :, hei_interf[cspsort[list(range(
1429 junkcspc_interf = jcspectra[ip, :, hei_interf[cspsort[list(range(
1104 offhei_interf, nhei_interf + offhei_interf))]]]
1430 offhei_interf, nhei_interf + offhei_interf))]]]
1105 junkcspc_interf = junkcspc_interf.transpose()
1431 junkcspc_interf = junkcspc_interf.transpose()
1106 jcspc_interf = junkcspc_interf.sum(axis=1) / nhei_interf
1432 jcspc_interf = junkcspc_interf.sum(axis=1) / nhei_interf
1107
1433
1108 ind = numpy.abs(jcspc_interf[mask_prof]).ravel().argsort()
1434 ind = numpy.abs(jcspc_interf[mask_prof]).ravel().argsort()
1109
1435
1110 median_real = int(numpy.median(numpy.real(
1436 median_real = int(numpy.median(numpy.real(
1111 junkcspc_interf[mask_prof[ind[list(range(3 * num_prof // 4))]], :])))
1437 junkcspc_interf[mask_prof[ind[list(range(3 * num_prof // 4))]], :])))
1112 median_imag = int(numpy.median(numpy.imag(
1438 median_imag = int(numpy.median(numpy.imag(
1113 junkcspc_interf[mask_prof[ind[list(range(3 * num_prof // 4))]], :])))
1439 junkcspc_interf[mask_prof[ind[list(range(3 * num_prof // 4))]], :])))
1114 comp_mask_prof = [int(e) for e in comp_mask_prof]
1440 comp_mask_prof = [int(e) for e in comp_mask_prof]
1115 junkcspc_interf[comp_mask_prof, :] = numpy.complex(
1441 junkcspc_interf[comp_mask_prof, :] = numpy.complex(
1116 median_real, median_imag)
1442 median_real, median_imag)
1117
1443
1118 for iprof in range(num_prof):
1444 for iprof in range(num_prof):
1119 ind = numpy.abs(junkcspc_interf[iprof, :]).ravel().argsort()
1445 ind = numpy.abs(junkcspc_interf[iprof, :]).ravel().argsort()
1120 jcspc_interf[iprof] = junkcspc_interf[iprof, ind[nhei_interf // 2]]
1446 jcspc_interf[iprof] = junkcspc_interf[iprof, ind[nhei_interf // 2]]
1121
1447
1122 # Removiendo la Interferencia
1448 # Removiendo la Interferencia
1123 jcspectra[ip, :, ind_hei] = jcspectra[ip,
1449 jcspectra[ip, :, ind_hei] = jcspectra[ip,
1124 :, ind_hei] - jcspc_interf
1450 :, ind_hei] - jcspc_interf
1125
1451
1126 ListAux = numpy.abs(jcspc_interf[mask_prof]).tolist()
1452 ListAux = numpy.abs(jcspc_interf[mask_prof]).tolist()
1127 maxid = ListAux.index(max(ListAux))
1453 maxid = ListAux.index(max(ListAux))
1128
1454
1129 ind = numpy.array([-2, -1, 1, 2])
1455 ind = numpy.array([-2, -1, 1, 2])
1130 xx = numpy.zeros([4, 4])
1456 xx = numpy.zeros([4, 4])
1131
1457
1132 for id1 in range(4):
1458 for id1 in range(4):
1133 xx[:, id1] = ind[id1]**numpy.asarray(list(range(4)))
1459 xx[:, id1] = ind[id1]**numpy.asarray(list(range(4)))
1134
1460
1135 xx_inv = numpy.linalg.inv(xx)
1461 xx_inv = numpy.linalg.inv(xx)
1136 xx = xx_inv[:, 0]
1462 xx = xx_inv[:, 0]
1137
1463
1138 ind = (ind + maxid + num_mask_prof) % num_mask_prof
1464 ind = (ind + maxid + num_mask_prof) % num_mask_prof
1139 yy = jcspectra[ip, mask_prof[ind], :]
1465 yy = jcspectra[ip, mask_prof[ind], :]
1140 jcspectra[ip, mask_prof[maxid], :] = numpy.dot(yy.transpose(), xx)
1466 jcspectra[ip, mask_prof[maxid], :] = numpy.dot(yy.transpose(), xx)
1141
1467
1142 # Guardar Resultados
1468 # Guardar Resultados
1143 self.dataOut.data_spc = jspectra
1469 self.dataOut.data_spc = jspectra
1144 self.dataOut.data_cspc = jcspectra
1470 self.dataOut.data_cspc = jcspectra
1145
1471
1146 return 1
1472 return 1
1147
1473
1148 def run(self, dataOut, interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None, mode=1):
1474 def run(self, dataOut, interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None, mode=1):
1149
1475
1150 self.dataOut = dataOut
1476 self.dataOut = dataOut
1151
1477
1152 if mode == 1:
1478 if mode == 1:
1153 self.removeInterference(interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None)
1479 self.removeInterference(interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None)
1154 elif mode == 2:
1480 elif mode == 2:
1155 self.removeInterference2()
1481 self.removeInterference2()
1156
1482
1157 return self.dataOut
1483 return self.dataOut
1158
1484
1159
1485
1160 class IncohInt(Operation):
1486 class IncohInt(Operation):
1161
1487
1162 __profIndex = 0
1488 __profIndex = 0
1163 __withOverapping = False
1489 __withOverapping = False
1164
1490
1165 __byTime = False
1491 __byTime = False
1166 __initime = None
1492 __initime = None
1167 __lastdatatime = None
1493 __lastdatatime = None
1168 __integrationtime = None
1494 __integrationtime = None
1169
1495
1170 __buffer_spc = None
1496 __buffer_spc = None
1171 __buffer_cspc = None
1497 __buffer_cspc = None
1172 __buffer_dc = None
1498 __buffer_dc = None
1173
1499
1174 __dataReady = False
1500 __dataReady = False
1175
1501
1176 __timeInterval = None
1502 __timeInterval = None
1177
1503
1178 n = None
1504 n = None
1179
1505
1180 def __init__(self):
1506 def __init__(self):
1181
1507
1182 Operation.__init__(self)
1508 Operation.__init__(self)
1183
1509
1184 def setup(self, n=None, timeInterval=None, overlapping=False):
1510 def setup(self, n=None, timeInterval=None, overlapping=False):
1185 """
1511 """
1186 Set the parameters of the integration class.
1512 Set the parameters of the integration class.
1187
1513
1188 Inputs:
1514 Inputs:
1189
1515
1190 n : Number of coherent integrations
1516 n : Number of coherent integrations
1191 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
1517 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
1192 overlapping :
1518 overlapping :
1193
1519
1194 """
1520 """
1195
1521
1196 self.__initime = None
1522 self.__initime = None
1197 self.__lastdatatime = 0
1523 self.__lastdatatime = 0
1198
1524
1199 self.__buffer_spc = 0
1525 self.__buffer_spc = 0
1200 self.__buffer_cspc = 0
1526 self.__buffer_cspc = 0
1201 self.__buffer_dc = 0
1527 self.__buffer_dc = 0
1202
1528
1203 self.__profIndex = 0
1529 self.__profIndex = 0
1204 self.__dataReady = False
1530 self.__dataReady = False
1205 self.__byTime = False
1531 self.__byTime = False
1206
1532
1207 if n is None and timeInterval is None:
1533 if n is None and timeInterval is None:
1208 raise ValueError("n or timeInterval should be specified ...")
1534 raise ValueError("n or timeInterval should be specified ...")
1209
1535
1210 if n is not None:
1536 if n is not None:
1211 self.n = int(n)
1537 self.n = int(n)
1212 else:
1538 else:
1213
1539
1214 self.__integrationtime = int(timeInterval)
1540 self.__integrationtime = int(timeInterval)
1215 self.n = None
1541 self.n = None
1216 self.__byTime = True
1542 self.__byTime = True
1217
1543
1218 def putData(self, data_spc, data_cspc, data_dc):
1544 def putData(self, data_spc, data_cspc, data_dc):
1219 """
1545 """
1220 Add a profile to the __buffer_spc and increase in one the __profileIndex
1546 Add a profile to the __buffer_spc and increase in one the __profileIndex
1221
1547
1222 """
1548 """
1223
1549
1224 self.__buffer_spc += data_spc
1550 self.__buffer_spc += data_spc
1225
1551
1226 if data_cspc is None:
1552 if data_cspc is None:
1227 self.__buffer_cspc = None
1553 self.__buffer_cspc = None
1228 else:
1554 else:
1229 self.__buffer_cspc += data_cspc
1555 self.__buffer_cspc += data_cspc
1230
1556
1231 if data_dc is None:
1557 if data_dc is None:
1232 self.__buffer_dc = None
1558 self.__buffer_dc = None
1233 else:
1559 else:
1234 self.__buffer_dc += data_dc
1560 self.__buffer_dc += data_dc
1235
1561
1236 self.__profIndex += 1
1562 self.__profIndex += 1
1237
1563
1238 return
1564 return
1239
1565
1240 def pushData(self):
1566 def pushData(self):
1241 """
1567 """
1242 Return the sum of the last profiles and the profiles used in the sum.
1568 Return the sum of the last profiles and the profiles used in the sum.
1243
1569
1244 Affected:
1570 Affected:
1245
1571
1246 self.__profileIndex
1572 self.__profileIndex
1247
1573
1248 """
1574 """
1249
1575
1250 data_spc = self.__buffer_spc
1576 data_spc = self.__buffer_spc
1251 data_cspc = self.__buffer_cspc
1577 data_cspc = self.__buffer_cspc
1252 data_dc = self.__buffer_dc
1578 data_dc = self.__buffer_dc
1253 n = self.__profIndex
1579 n = self.__profIndex
1254
1580
1255 self.__buffer_spc = 0
1581 self.__buffer_spc = 0
1256 self.__buffer_cspc = 0
1582 self.__buffer_cspc = 0
1257 self.__buffer_dc = 0
1583 self.__buffer_dc = 0
1258 self.__profIndex = 0
1584 self.__profIndex = 0
1259
1585
1260 return data_spc, data_cspc, data_dc, n
1586 return data_spc, data_cspc, data_dc, n
1261
1587
1262 def byProfiles(self, *args):
1588 def byProfiles(self, *args):
1263
1589
1264 self.__dataReady = False
1590 self.__dataReady = False
1265 avgdata_spc = None
1591 avgdata_spc = None
1266 avgdata_cspc = None
1592 avgdata_cspc = None
1267 avgdata_dc = None
1593 avgdata_dc = None
1268
1594
1269 self.putData(*args)
1595 self.putData(*args)
1270
1596
1271 if self.__profIndex == self.n:
1597 if self.__profIndex == self.n:
1272
1598
1273 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1599 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1274 self.n = n
1600 self.n = n
1275 self.__dataReady = True
1601 self.__dataReady = True
1276
1602
1277 return avgdata_spc, avgdata_cspc, avgdata_dc
1603 return avgdata_spc, avgdata_cspc, avgdata_dc
1278
1604
1279 def byTime(self, datatime, *args):
1605 def byTime(self, datatime, *args):
1280
1606
1281 self.__dataReady = False
1607 self.__dataReady = False
1282 avgdata_spc = None
1608 avgdata_spc = None
1283 avgdata_cspc = None
1609 avgdata_cspc = None
1284 avgdata_dc = None
1610 avgdata_dc = None
1285
1611
1286 self.putData(*args)
1612 self.putData(*args)
1287
1613
1288 if (datatime - self.__initime) >= self.__integrationtime:
1614 if (datatime - self.__initime) >= self.__integrationtime:
1289 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1615 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1290 self.n = n
1616 self.n = n
1291 self.__dataReady = True
1617 self.__dataReady = True
1292
1618
1293 return avgdata_spc, avgdata_cspc, avgdata_dc
1619 return avgdata_spc, avgdata_cspc, avgdata_dc
1294
1620
1295 def integrate(self, datatime, *args):
1621 def integrate(self, datatime, *args):
1296
1622
1297 if self.__profIndex == 0:
1623 if self.__profIndex == 0:
1298 self.__initime = datatime
1624 self.__initime = datatime
1299
1625
1300 if self.__byTime:
1626 if self.__byTime:
1301 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(
1627 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(
1302 datatime, *args)
1628 datatime, *args)
1303 else:
1629 else:
1304 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
1630 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
1305
1631
1306 if not self.__dataReady:
1632 if not self.__dataReady:
1307 return None, None, None, None
1633 return None, None, None, None
1308
1634
1309 return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc
1635 return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc
1310
1636
1311 def run(self, dataOut, n=None, timeInterval=None, overlapping=False):
1637 def run(self, dataOut, n=None, timeInterval=None, overlapping=False):
1312 if n == 1:
1638 if n == 1:
1313 return dataOut
1639 return dataOut
1314
1640
1315 dataOut.flagNoData = True
1641 dataOut.flagNoData = True
1316
1642
1317 if not self.isConfig:
1643 if not self.isConfig:
1318 self.setup(n, timeInterval, overlapping)
1644 self.setup(n, timeInterval, overlapping)
1319 self.isConfig = True
1645 self.isConfig = True
1320
1646
1321 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime,
1647 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime,
1322 dataOut.data_spc,
1648 dataOut.data_spc,
1323 dataOut.data_cspc,
1649 dataOut.data_cspc,
1324 dataOut.data_dc)
1650 dataOut.data_dc)
1325
1651
1326 if self.__dataReady:
1652 if self.__dataReady:
1327
1653
1328 dataOut.data_spc = avgdata_spc
1654 dataOut.data_spc = avgdata_spc
1329 dataOut.data_cspc = avgdata_cspc
1655 dataOut.data_cspc = avgdata_cspc
1330 dataOut.data_dc = avgdata_dc
1656 dataOut.data_dc = avgdata_dc
1331 dataOut.nIncohInt *= self.n
1657 dataOut.nIncohInt *= self.n
1332 dataOut.utctime = avgdatatime
1658 dataOut.utctime = avgdatatime
1333 dataOut.flagNoData = False
1659 dataOut.flagNoData = False
1334
1660
1335 return dataOut
1661 return dataOut
1336
1662
1337 class dopplerFlip(Operation):
1663 class dopplerFlip(Operation):
1338
1664
1339 def run(self, dataOut):
1665 def run(self, dataOut):
1340 # arreglo 1: (num_chan, num_profiles, num_heights)
1666 # arreglo 1: (num_chan, num_profiles, num_heights)
1341 self.dataOut = dataOut
1667 self.dataOut = dataOut
1342 # JULIA-oblicua, indice 2
1668 # JULIA-oblicua, indice 2
1343 # arreglo 2: (num_profiles, num_heights)
1669 # arreglo 2: (num_profiles, num_heights)
1344 jspectra = self.dataOut.data_spc[2]
1670 jspectra = self.dataOut.data_spc[2]
1345 jspectra_tmp = numpy.zeros(jspectra.shape)
1671 jspectra_tmp = numpy.zeros(jspectra.shape)
1346 num_profiles = jspectra.shape[0]
1672 num_profiles = jspectra.shape[0]
1347 freq_dc = int(num_profiles / 2)
1673 freq_dc = int(num_profiles / 2)
1348 # Flip con for
1674 # Flip con for
1349 for j in range(num_profiles):
1675 for j in range(num_profiles):
1350 jspectra_tmp[num_profiles-j-1]= jspectra[j]
1676 jspectra_tmp[num_profiles-j-1]= jspectra[j]
1351 # Intercambio perfil de DC con perfil inmediato anterior
1677 # Intercambio perfil de DC con perfil inmediato anterior
1352 jspectra_tmp[freq_dc-1]= jspectra[freq_dc-1]
1678 jspectra_tmp[freq_dc-1]= jspectra[freq_dc-1]
1353 jspectra_tmp[freq_dc]= jspectra[freq_dc]
1679 jspectra_tmp[freq_dc]= jspectra[freq_dc]
1354 # canal modificado es re-escrito en el arreglo de canales
1680 # canal modificado es re-escrito en el arreglo de canales
1355 self.dataOut.data_spc[2] = jspectra_tmp
1681 self.dataOut.data_spc[2] = jspectra_tmp
1356
1682
1357 return self.dataOut
1683 return self.dataOut
General Comments 0
You need to be logged in to leave comments. Login now