@@ -1,1267 +1,1267 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on 23/01/2012 |
|
3 | 3 | |
|
4 | 4 | @author $Author$ |
|
5 | 5 | @version $Id$ |
|
6 | 6 | @version $Id$ |
|
7 | 7 | ''' |
|
8 | 8 | |
|
9 | 9 | import os, sys |
|
10 | 10 | import glob |
|
11 | 11 | import time |
|
12 | 12 | import numpy |
|
13 | 13 | import fnmatch |
|
14 | 14 | import time, datetime |
|
15 | 15 | |
|
16 | 16 | path = os.path.split(os.getcwd())[0] |
|
17 | 17 | sys.path.append(path) |
|
18 | 18 | |
|
19 | 19 | from Model.JROHeader import * |
|
20 | 20 | from Model.JROData import JROData |
|
21 | 21 | |
|
22 | 22 | def checkForRealPath(path, year, doy, set, ext): |
|
23 | 23 | """ |
|
24 | 24 | Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path, |
|
25 | 25 | Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar |
|
26 | 26 | el path exacto de un determinado file. |
|
27 | 27 | |
|
28 | 28 | Example : |
|
29 | 29 | nombre correcto del file es .../.../D2009307/P2009307367.ext |
|
30 | 30 | |
|
31 | 31 | Entonces la funcion prueba con las siguientes combinaciones |
|
32 | 32 | .../.../x2009307/y2009307367.ext |
|
33 | 33 | .../.../x2009307/Y2009307367.ext |
|
34 | 34 | .../.../X2009307/y2009307367.ext |
|
35 | 35 | .../.../X2009307/Y2009307367.ext |
|
36 | 36 | siendo para este caso, la ultima combinacion de letras, identica al file buscado |
|
37 | 37 | |
|
38 | 38 | Return: |
|
39 | 39 | Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file |
|
40 | 40 | caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas |
|
41 | 41 | para el filename |
|
42 | 42 | """ |
|
43 | 43 | filepath = None |
|
44 | 44 | find_flag = False |
|
45 | 45 | filename = None |
|
46 | 46 | |
|
47 | 47 | if ext.lower() == ".r": #voltage |
|
48 | 48 | header1 = "dD" |
|
49 | 49 | header2 = "dD" |
|
50 | 50 | elif ext.lower() == ".pdata": #spectra |
|
51 | 51 | header1 = "dD" |
|
52 | 52 | header2 = "pP" |
|
53 | 53 | else: |
|
54 | 54 | return None, filename |
|
55 | 55 | |
|
56 | 56 | for dir in header1: #barrido por las dos combinaciones posibles de "D" |
|
57 | 57 | for fil in header2: #barrido por las dos combinaciones posibles de "D" |
|
58 | 58 | doypath = "%s%04d%03d" % ( dir, year, doy ) #formo el nombre del directorio xYYYYDDD (x=d o x=D) |
|
59 | 59 | filename = "%s%04d%03d%03d%s" % ( fil, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext |
|
60 | 60 | filepath = os.path.join( path, doypath, filename ) #formo el path completo |
|
61 | 61 | if os.path.exists( filepath ): #verifico que exista |
|
62 | 62 | find_flag = True |
|
63 | 63 | break |
|
64 | 64 | if find_flag: |
|
65 | 65 | break |
|
66 | 66 | |
|
67 | 67 | if not(find_flag): |
|
68 | 68 | return None, filename |
|
69 | 69 | |
|
70 | 70 | return filepath, filename |
|
71 | 71 | |
|
72 | 72 | |
|
73 | 73 | def isNumber(str): |
|
74 | 74 | """ |
|
75 | 75 | Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. |
|
76 | 76 | |
|
77 | 77 | Excepciones: |
|
78 | 78 | Si un determinado string no puede ser convertido a numero |
|
79 | 79 | Input: |
|
80 | 80 | str, string al cual se le analiza para determinar si convertible a un numero o no |
|
81 | 81 | |
|
82 | 82 | Return: |
|
83 | 83 | True : si el string es uno numerico |
|
84 | 84 | False : no es un string numerico |
|
85 | 85 | """ |
|
86 | 86 | try: |
|
87 | 87 | float( str ) |
|
88 | 88 | return True |
|
89 | 89 | except: |
|
90 | 90 | return False |
|
91 | 91 | |
|
92 | 92 | |
|
93 | 93 | def isThisFileinRange(filename, startUTSeconds, endUTSeconds): |
|
94 | 94 | """ |
|
95 | 95 | Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado. |
|
96 | 96 | |
|
97 | 97 | Inputs: |
|
98 | 98 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
99 | 99 | |
|
100 | 100 | startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en |
|
101 | 101 | segundos contados desde 01/01/1970. |
|
102 | 102 | endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en |
|
103 | 103 | segundos contados desde 01/01/1970. |
|
104 | 104 | |
|
105 | 105 | Return: |
|
106 | 106 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
107 | 107 | fecha especificado, de lo contrario retorna False. |
|
108 | 108 | |
|
109 | 109 | Excepciones: |
|
110 | 110 | Si el archivo no existe o no puede ser abierto |
|
111 | 111 | Si la cabecera no puede ser leida. |
|
112 | 112 | |
|
113 | 113 | """ |
|
114 | 114 | m_BasicHeader = BasicHeader() |
|
115 | 115 | |
|
116 | 116 | try: |
|
117 | 117 | fp = open(filename,'rb') |
|
118 | 118 | except: |
|
119 | 119 | raise IOError, "The file %s can't be opened" %(filename) |
|
120 | 120 | |
|
121 | 121 | sts = m_BasicHeader.read(fp) |
|
122 | 122 | fp.close() |
|
123 | 123 | |
|
124 | 124 | if not(sts): |
|
125 | 125 | print "Skipping the file %s because it has not a valid header" %(filename) |
|
126 | 126 | return 0 |
|
127 | 127 | |
|
128 | 128 | if not ((startUTSeconds <= m_BasicHeader.utc) and (endUTSeconds > m_BasicHeader.utc)): |
|
129 | 129 | return 0 |
|
130 | 130 | |
|
131 | 131 | return 1 |
|
132 | 132 | |
|
133 | 133 | |
|
134 | 134 | def getlastFileFromPath(path, ext): |
|
135 | 135 | """ |
|
136 | 136 | Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext" |
|
137 | 137 | al final de la depuracion devuelve el ultimo file de la lista que quedo. |
|
138 | 138 | |
|
139 | 139 | Input: |
|
140 | 140 | fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta |
|
141 | 141 | ext : extension de los files contenidos en una carpeta |
|
142 | 142 | |
|
143 | 143 | Return: |
|
144 | 144 | El ultimo file de una determinada carpeta, no se considera el path. |
|
145 | 145 | """ |
|
146 | 146 | validFilelist = [] |
|
147 | 147 | fileList = os.listdir(path) |
|
148 | 148 | |
|
149 | 149 | # 0 1234 567 89A BCDE |
|
150 | 150 | # H YYYY DDD SSS .ext |
|
151 | 151 | |
|
152 | 152 | for file in fileList: |
|
153 | 153 | try: |
|
154 | 154 | year = int(file[1:5]) |
|
155 | 155 | doy = int(file[5:8]) |
|
156 | 156 | |
|
157 | 157 | if (os.path.splitext(file)[-1].upper() != ext.upper()) : continue |
|
158 | 158 | except: |
|
159 | 159 | continue |
|
160 | 160 | |
|
161 | 161 | validFilelist.append(file) |
|
162 | 162 | |
|
163 | 163 | if validFilelist: |
|
164 | 164 | validFilelist = sorted( validFilelist, key=str.lower ) |
|
165 | 165 | return validFilelist[-1] |
|
166 | 166 | |
|
167 | 167 | return None |
|
168 | 168 | |
|
169 | 169 | class JRODataIO: |
|
170 | 170 | |
|
171 | 171 | #speed of light |
|
172 | 172 | c = 3E8 |
|
173 | 173 | |
|
174 | 174 | m_BasicHeader = BasicHeader() |
|
175 | 175 | |
|
176 |
|
|
|
176 | systemHeaderObj = SystemHeader() | |
|
177 | 177 | |
|
178 |
|
|
|
178 | radarControllerHeaderObj = RadarControllerHeader() | |
|
179 | 179 | |
|
180 | 180 | m_ProcessingHeader = ProcessingHeader() |
|
181 | 181 | |
|
182 | 182 | dataOutObj = None |
|
183 | 183 | |
|
184 | 184 | online = 0 |
|
185 | 185 | |
|
186 | 186 | fp = None |
|
187 | 187 | |
|
188 | 188 | dataType = None |
|
189 | 189 | |
|
190 | 190 | fileSizeByHeader = None |
|
191 | 191 | |
|
192 | 192 | filenameList = [] |
|
193 | 193 | |
|
194 | 194 | filename = None |
|
195 | 195 | |
|
196 | 196 | fileSize = None |
|
197 | 197 | |
|
198 | 198 | firstHeaderSize = 0 |
|
199 | 199 | |
|
200 | 200 | basicHeaderSize = 24 |
|
201 | 201 | |
|
202 | 202 | nTotalBlocks = 0 |
|
203 | 203 | |
|
204 | 204 | ippSeconds = 0 |
|
205 | 205 | |
|
206 | 206 | blocksize = 0 |
|
207 | 207 | |
|
208 | 208 | set = 0 |
|
209 | 209 | |
|
210 | 210 | ext = None |
|
211 | 211 | |
|
212 | 212 | path = None |
|
213 | 213 | |
|
214 | 214 | maxTimeStep = 30 |
|
215 | 215 | |
|
216 | 216 | |
|
217 | 217 | delay = 3 #seconds |
|
218 | 218 | |
|
219 | 219 | nTries = 3 #quantity tries |
|
220 | 220 | |
|
221 | 221 | nFiles = 3 #number of files for searching |
|
222 | 222 | |
|
223 | 223 | |
|
224 | 224 | flagNoMoreFiles = 0 |
|
225 | 225 | |
|
226 | 226 | flagIsNewFile = 1 |
|
227 | 227 | |
|
228 | 228 | flagResetProcessing = 0 |
|
229 | 229 | |
|
230 | 230 | flagIsNewBlock = 0 |
|
231 | 231 | |
|
232 | 232 | def __init__(self): |
|
233 | 233 | pass |
|
234 | 234 | |
|
235 | 235 | class JRODataReader(JRODataIO): |
|
236 | 236 | |
|
237 | 237 | """ |
|
238 | 238 | Esta clase es usada como la clase padre de las clases VoltageReader y SpectraReader. |
|
239 | 239 | Contiene todos lo metodos necesarios para leer datos desde archivos en formato |
|
240 | 240 | jicamarca o pdata (.r o .pdata). La lectura de los datos siempre se realiza por bloques. Los datos |
|
241 | 241 | leidos son array de 3 dimensiones: |
|
242 | 242 | |
|
243 | 243 | Para Voltajes - perfiles * alturas * canales |
|
244 | 244 | |
|
245 | 245 | Para Spectra - paresCanalesIguales * alturas * perfiles (Self Spectra) |
|
246 | 246 | paresCanalesDiferentes * alturas * perfiles (Cross Spectra) |
|
247 | 247 | canales * alturas (DC Channels) |
|
248 | 248 | |
|
249 | 249 | y son almacenados en su buffer respectivo. |
|
250 | 250 | |
|
251 | 251 | Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader, |
|
252 | 252 | RadarControllerHeader y DataObj. Los tres primeros se usan para almacenar informacion de la |
|
253 | 253 | cabecera de datos (metadata), y el cuarto (DataObj) para obtener y almacenar los datos desde |
|
254 | 254 | el buffer de datos cada vez que se ejecute el metodo "getData". |
|
255 | 255 | """ |
|
256 | 256 | |
|
257 | 257 | nReadBlocks = 0 |
|
258 | 258 | |
|
259 | 259 | def __init__(self, dataOutObj=None): |
|
260 | 260 | |
|
261 | 261 | raise ValueError, "This class can't be instanced" |
|
262 | 262 | |
|
263 | 263 | |
|
264 | 264 | def hasNotDataInBuffer(self): |
|
265 | 265 | |
|
266 | 266 | raise ValueError, "Not implemented" |
|
267 | 267 | |
|
268 | 268 | def getBlockDimension(self): |
|
269 | 269 | |
|
270 | 270 | raise ValueError, "No implemented" |
|
271 | 271 | |
|
272 | 272 | def readBlock(self): |
|
273 | 273 | |
|
274 | 274 | self.nTotalBlocks += 1 |
|
275 | 275 | self.nReadBlocks += 1 |
|
276 | 276 | |
|
277 | 277 | raise ValueError, "This method has not been implemented" |
|
278 | 278 | |
|
279 | 279 | def getData( self ): |
|
280 | 280 | |
|
281 | 281 | raise ValueError, "This method has not been implemented" |
|
282 | 282 | |
|
283 | 283 | def createObjByDefault(self): |
|
284 | 284 | """ |
|
285 | 285 | Los objetos creados por defecto por cada clase (Voltaje o Espectro) difieren en el tipo |
|
286 | 286 | raise ValueError, "This method has not been implemented |
|
287 | 287 | """ |
|
288 | 288 | raise ValueError, "This method has not been implemented" |
|
289 | 289 | |
|
290 | 290 | # def setup(self, dataOutObj=None, path=None, startDateTime=None, endDateTime=None, set=0, expLabel = "", ext = None, online = 0): |
|
291 | 291 | |
|
292 | 292 | def __searchFilesOnLine(self, path, expLabel = "", ext = None, startDate=None, endDate=None, startTime=None, endTime=None): |
|
293 | 293 | """ |
|
294 | 294 | Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y |
|
295 | 295 | devuelve el archivo encontrado ademas de otros datos. |
|
296 | 296 | |
|
297 | 297 | Input: |
|
298 | 298 | path : carpeta donde estan contenidos los files que contiene data |
|
299 | 299 | |
|
300 | 300 | startDate : Fecha inicial. Rechaza todos los directorios donde |
|
301 | 301 | file end time < startDate (obejto datetime.date) |
|
302 | 302 | |
|
303 | 303 | endDate : Fecha final. Rechaza todos los directorios donde |
|
304 | 304 | file start time > endDate (obejto datetime.date) |
|
305 | 305 | |
|
306 | 306 | startTime : Tiempo inicial. Rechaza todos los archivos donde |
|
307 | 307 | file end time < startTime (obejto datetime.time) |
|
308 | 308 | |
|
309 | 309 | endTime : Tiempo final. Rechaza todos los archivos donde |
|
310 | 310 | file start time > endTime (obejto datetime.time) |
|
311 | 311 | |
|
312 | 312 | ext : extension de los files |
|
313 | 313 | |
|
314 | 314 | Return: |
|
315 | 315 | directory : eL directorio donde esta el file encontrado |
|
316 | 316 | filename : el ultimo file de una determinada carpeta |
|
317 | 317 | year : el anho |
|
318 | 318 | doy : el numero de dia del anho |
|
319 | 319 | set : el set del archivo |
|
320 | 320 | |
|
321 | 321 | |
|
322 | 322 | """ |
|
323 | 323 | dirList = [] |
|
324 | 324 | pathList = [] |
|
325 | 325 | directory = None |
|
326 | 326 | |
|
327 | 327 | #Filtra solo los directorios |
|
328 | 328 | for thisPath in os.listdir(path): |
|
329 | 329 | if os.path.isdir(os.path.join(path, thisPath)): |
|
330 | 330 | dirList.append(thisPath) |
|
331 | 331 | |
|
332 | 332 | if not(dirList): |
|
333 | 333 | return None, None, None, None, None |
|
334 | 334 | |
|
335 | 335 | dirList = sorted( dirList, key=str.lower ) |
|
336 | 336 | |
|
337 | 337 | if startDate: |
|
338 | 338 | startDateTime = datetime.datetime.combine(startDate, startTime) |
|
339 | 339 | thisDateTime = startDateTime |
|
340 | 340 | if endDate == None: endDateTime = startDateTime |
|
341 | 341 | else: endDateTime = datetime.datetime.combine(endDate, endTime) |
|
342 | 342 | |
|
343 | 343 | while(thisDateTime <= endDateTime): |
|
344 | 344 | year = thisDateTime.timetuple().tm_year |
|
345 | 345 | doy = thisDateTime.timetuple().tm_yday |
|
346 | 346 | |
|
347 | 347 | match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy)) |
|
348 | 348 | if len(match) == 0: |
|
349 | 349 | thisDateTime += datetime.timedelta(1) |
|
350 | 350 | continue |
|
351 | 351 | |
|
352 | 352 | pathList.append(os.path.join(path,match[0], expLabel)) |
|
353 | 353 | thisDateTime += datetime.timedelta(1) |
|
354 | 354 | |
|
355 | 355 | if not(pathList): |
|
356 | 356 | print "\tNo files in range: %s - %s" %(startDateTime.ctime(), endDateTime.ctime()) |
|
357 | 357 | return None, None, None, None, None |
|
358 | 358 | |
|
359 | 359 | directory = pathList[0] |
|
360 | 360 | |
|
361 | 361 | else: |
|
362 | 362 | directory = dirList[-1] |
|
363 | 363 | directory = os.path.join(path,directory) |
|
364 | 364 | |
|
365 | 365 | filename = getlastFileFromPath(directory, ext) |
|
366 | 366 | |
|
367 | 367 | if not(filename): |
|
368 | 368 | return None, None, None, None, None |
|
369 | 369 | |
|
370 | 370 | if not(self.__verifyFile(os.path.join(directory, filename))): |
|
371 | 371 | return None, None, None, None, None |
|
372 | 372 | |
|
373 | 373 | year = int( filename[1:5] ) |
|
374 | 374 | doy = int( filename[5:8] ) |
|
375 | 375 | set = int( filename[8:11] ) |
|
376 | 376 | |
|
377 | 377 | return directory, filename, year, doy, set |
|
378 | 378 | |
|
379 | 379 | |
|
380 | 380 | def __searchFilesOffLine(self, path, startDate, endDate, startTime=datetime.time(0,0,0), endTime=datetime.time(23,59,59),set=None, expLabel = "", ext = ".r"): |
|
381 | 381 | """ |
|
382 | 382 | Realiza una busqueda de los archivos que coincidan con los parametros |
|
383 | 383 | especificados y se encuentren ubicados en el path indicado. Para realizar una busqueda |
|
384 | 384 | correcta la estructura de directorios debe ser la siguiente: |
|
385 | 385 | |
|
386 | 386 | ...path/D[yyyy][ddd]/expLabel/D[yyyy][ddd][sss].ext |
|
387 | 387 | |
|
388 | 388 | [yyyy]: anio |
|
389 | 389 | [ddd] : dia del anio |
|
390 | 390 | [sss] : set del archivo |
|
391 | 391 | |
|
392 | 392 | Inputs: |
|
393 | 393 | path : Directorio de datos donde se realizara la busqueda. Todos los |
|
394 | 394 | ficheros que concidan con el criterio de busqueda seran |
|
395 | 395 | almacenados en una lista y luego retornados. |
|
396 | 396 | startDate : Fecha inicial. Rechaza todos los directorios donde |
|
397 | 397 | file end time < startDate (obejto datetime.date) |
|
398 | 398 | |
|
399 | 399 | endDate : Fecha final. Rechaza todos los directorios donde |
|
400 | 400 | file start time > endDate (obejto datetime.date) |
|
401 | 401 | |
|
402 | 402 | startTime : Tiempo inicial. Rechaza todos los archivos donde |
|
403 | 403 | file end time < startTime (obejto datetime.time) |
|
404 | 404 | |
|
405 | 405 | endTime : Tiempo final. Rechaza todos los archivos donde |
|
406 | 406 | file start time > endTime (obejto datetime.time) |
|
407 | 407 | |
|
408 | 408 | set : Set del primer archivo a leer. Por defecto None |
|
409 | 409 | |
|
410 | 410 | expLabel : Nombre del subdirectorio de datos. Por defecto "" |
|
411 | 411 | |
|
412 | 412 | ext : Extension de los archivos a leer. Por defecto .r |
|
413 | 413 | |
|
414 | 414 | Return: |
|
415 | 415 | |
|
416 | 416 | (pathList, filenameList) |
|
417 | 417 | |
|
418 | 418 | pathList : Lista de directorios donde se encontraron archivos dentro |
|
419 | 419 | de los parametros especificados |
|
420 | 420 | filenameList : Lista de archivos (ruta completa) que coincidieron con los |
|
421 | 421 | parametros especificados. |
|
422 | 422 | |
|
423 | 423 | Variables afectadas: |
|
424 | 424 | |
|
425 | 425 | self.filenameList: Lista de archivos (ruta completa) que la clase utiliza |
|
426 | 426 | como fuente para leer los bloque de datos, si se termina |
|
427 | 427 | de leer todos los bloques de datos de un determinado |
|
428 | 428 | archivo se pasa al siguiente archivo de la lista. |
|
429 | 429 | |
|
430 | 430 | Excepciones: |
|
431 | 431 | |
|
432 | 432 | """ |
|
433 | 433 | |
|
434 | 434 | dirList = [] |
|
435 | 435 | for thisPath in os.listdir(path): |
|
436 | 436 | if os.path.isdir(os.path.join(path,thisPath)): |
|
437 | 437 | dirList.append(thisPath) |
|
438 | 438 | |
|
439 | 439 | if not(dirList): |
|
440 | 440 | return None, None |
|
441 | 441 | |
|
442 | 442 | pathList = [] |
|
443 | 443 | dateList = [] |
|
444 | 444 | |
|
445 | 445 | thisDate = startDate |
|
446 | 446 | |
|
447 | 447 | while(thisDate <= endDate): |
|
448 | 448 | year = thisDate.timetuple().tm_year |
|
449 | 449 | doy = thisDate.timetuple().tm_yday |
|
450 | 450 | |
|
451 | 451 | match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy)) |
|
452 | 452 | if len(match) == 0: |
|
453 | 453 | thisDate += datetime.timedelta(1) |
|
454 | 454 | continue |
|
455 | 455 | |
|
456 | 456 | pathList.append(os.path.join(path,match[0],expLabel)) |
|
457 | 457 | dateList.append(thisDate) |
|
458 | 458 | thisDate += datetime.timedelta(1) |
|
459 | 459 | |
|
460 | 460 | filenameList = [] |
|
461 | 461 | for index in range(len(pathList)): |
|
462 | 462 | |
|
463 | 463 | thisPath = pathList[index] |
|
464 | 464 | fileList = glob.glob1(thisPath, "*%s" %ext) |
|
465 | 465 | fileList.sort() |
|
466 | 466 | |
|
467 | 467 | #Busqueda de datos en el rango de horas indicados |
|
468 | 468 | thisDate = dateList[index] |
|
469 | 469 | startDT = datetime.datetime.combine(thisDate, startTime) |
|
470 | 470 | endDT = datetime.datetime.combine(thisDate, endTime) |
|
471 | 471 | |
|
472 | 472 | startUtSeconds = time.mktime(startDT.timetuple()) |
|
473 | 473 | endUtSeconds = time.mktime(endDT.timetuple()) |
|
474 | 474 | |
|
475 | 475 | for file in fileList: |
|
476 | 476 | |
|
477 | 477 | filename = os.path.join(thisPath,file) |
|
478 | 478 | |
|
479 | 479 | if isThisFileinRange(filename, startUtSeconds, endUtSeconds): |
|
480 | 480 | filenameList.append(filename) |
|
481 | 481 | |
|
482 | 482 | if not(filenameList): |
|
483 | 483 | return None, None |
|
484 | 484 | |
|
485 | 485 | self.filenameList = filenameList |
|
486 | 486 | |
|
487 | 487 | return pathList, filenameList |
|
488 | 488 | |
|
489 | 489 | def setup(self, dataOutObj=None, path=None, startDate=None, endDate=None, startTime=datetime.time(0,0,0), endTime=datetime.time(23,59,59), set=0, expLabel = "", ext = None, online = 0): |
|
490 | 490 | """ |
|
491 | 491 | setup configura los parametros de lectura de la clase DataReader. |
|
492 | 492 | |
|
493 | 493 | Si el modo de lectura es offline, primero se realiza una busqueda de todos los archivos |
|
494 | 494 | que coincidan con los parametros especificados; esta lista de archivos son almacenados en |
|
495 | 495 | self.filenameList. |
|
496 | 496 | |
|
497 | 497 | Input: |
|
498 | 498 | path : Directorios donde se ubican los datos a leer. Dentro de este |
|
499 | 499 | directorio deberia de estar subdirectorios de la forma: |
|
500 | 500 | |
|
501 | 501 | path/D[yyyy][ddd]/expLabel/P[yyyy][ddd][sss][ext] |
|
502 | 502 | |
|
503 | 503 | startDate : Fecha inicial. Rechaza todos los directorios donde |
|
504 | 504 | file end time < startDate (obejto datetime.date) |
|
505 | 505 | |
|
506 | 506 | endDate : Fecha final. Rechaza todos los directorios donde |
|
507 | 507 | file start time > endDate (obejto datetime.date) |
|
508 | 508 | |
|
509 | 509 | startTime : Tiempo inicial. Rechaza todos los archivos donde |
|
510 | 510 | file end time < startTime (obejto datetime.time) |
|
511 | 511 | |
|
512 | 512 | endTime : Tiempo final. Rechaza todos los archivos donde |
|
513 | 513 | file start time > endTime (obejto datetime.time) |
|
514 | 514 | |
|
515 | 515 | set : Set del primer archivo a leer. Por defecto None |
|
516 | 516 | |
|
517 | 517 | expLabel : Nombre del subdirectorio de datos. Por defecto "" |
|
518 | 518 | |
|
519 | 519 | ext : Extension de los archivos a leer. Por defecto .r |
|
520 | 520 | |
|
521 | 521 | online : Si es == a 0 entonces busca files que cumplan con las condiciones dadas |
|
522 | 522 | |
|
523 | 523 | Return: |
|
524 | 524 | 0 : Si no encuentra files que cumplan con las condiciones dadas |
|
525 | 525 | 1 : Si encuentra files que cumplan con las condiciones dadas |
|
526 | 526 | |
|
527 | 527 | Affected: |
|
528 | 528 | self.startYear |
|
529 | 529 | self.endYear |
|
530 | 530 | self.startDoy |
|
531 | 531 | self.endDoy |
|
532 | 532 | self.pathList |
|
533 | 533 | self.filenameList |
|
534 | 534 | self.online |
|
535 | 535 | """ |
|
536 | 536 | if path == None: |
|
537 | 537 | raise ValueError, "The path is not valid" |
|
538 | 538 | |
|
539 | 539 | if ext == None: |
|
540 | 540 | ext = self.ext |
|
541 | 541 | |
|
542 | 542 | if dataOutObj == None: |
|
543 | 543 | dataOutObj = self.createObjByDefault() |
|
544 | 544 | |
|
545 | 545 | self.dataOutObj = dataOutObj |
|
546 | 546 | |
|
547 | 547 | if online: |
|
548 | 548 | print "Searching files in online mode..." |
|
549 | 549 | doypath, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=exp) |
|
550 | 550 | |
|
551 | 551 | if not(doypath): |
|
552 | 552 | for nTries in range( self.nTries ): |
|
553 | 553 | print '\tWaiting %0.2f sec for valid file in %s: try %02d ...' % (self.delay, path, nTries+1) |
|
554 | 554 | time.sleep( self.delay ) |
|
555 | 555 | doypath, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=exp) |
|
556 | 556 | if doypath: |
|
557 | 557 | break |
|
558 | 558 | |
|
559 | 559 | if not(doypath): |
|
560 | 560 | print "There 'isn't valied files in %s" % path |
|
561 | 561 | return None |
|
562 | 562 | |
|
563 | 563 | self.year = year |
|
564 | 564 | self.doy = doy |
|
565 | 565 | self.set = set - 1 |
|
566 | 566 | self.path = path |
|
567 | 567 | |
|
568 | 568 | else: # offline |
|
569 | 569 | print "Searching files in offline mode..." |
|
570 | 570 | pathList, filenameList = self.__searchFilesOffLine(path, startDate, endDate, startTime, endTime, set, expLabel, ext) |
|
571 | 571 | if not(pathList): |
|
572 | 572 | print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime()) |
|
573 | 573 | return None |
|
574 | 574 | |
|
575 | 575 | self.fileIndex = -1 |
|
576 | 576 | self.pathList = pathList |
|
577 | 577 | self.filenameList = filenameList |
|
578 | 578 | |
|
579 | 579 | self.online = online |
|
580 | 580 | self.ext = ext |
|
581 | 581 | |
|
582 | 582 | ext = ext.lower() |
|
583 | 583 | |
|
584 | 584 | if not( self.setNextFile() ): |
|
585 | 585 | if (startDate != None) and (endDate != None): |
|
586 | 586 | print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime()) |
|
587 | 587 | |
|
588 | 588 | elif startDate != None: |
|
589 | 589 | print "No files in : %s" % datetime.datetime.combine(startDate,startTime).ctime() |
|
590 | 590 | else: |
|
591 | 591 | print "No files" |
|
592 | 592 | return None |
|
593 | 593 | |
|
594 | 594 | #call fillHeaderValues() - to Data Object |
|
595 | 595 | |
|
596 | 596 | self.updateDataHeader() |
|
597 | 597 | |
|
598 | 598 | return self.dataOutObj |
|
599 | 599 | |
|
600 | 600 | def __rdSystemHeader(self, fp=None): |
|
601 | 601 | |
|
602 | 602 | if fp == None: |
|
603 | 603 | fp = self.fp |
|
604 | 604 | |
|
605 |
self. |
|
|
605 | self.systemHeaderObj.read(fp) | |
|
606 | 606 | |
|
607 | 607 | |
|
608 | 608 | def __rdRadarControllerHeader(self, fp=None): |
|
609 | 609 | if fp == None: |
|
610 | 610 | fp = self.fp |
|
611 | 611 | |
|
612 |
self. |
|
|
612 | self.radarControllerHeaderObj.read(fp) | |
|
613 | 613 | |
|
614 | 614 | |
|
615 | 615 | def __rdProcessingHeader(self, fp=None): |
|
616 | 616 | if fp == None: |
|
617 | 617 | fp = self.fp |
|
618 | 618 | |
|
619 | 619 | self.m_ProcessingHeader.read(fp) |
|
620 | 620 | |
|
621 | 621 | |
|
622 | 622 | def __rdBasicHeader(self, fp=None): |
|
623 | 623 | |
|
624 | 624 | if fp == None: |
|
625 | 625 | fp = self.fp |
|
626 | 626 | |
|
627 | 627 | self.m_BasicHeader.read(fp) |
|
628 | 628 | |
|
629 | 629 | def __readFirstHeader(self): |
|
630 | 630 | """ |
|
631 | 631 | Lectura del First Header, es decir el Basic Header y el Long Header |
|
632 | 632 | |
|
633 | 633 | Affected: |
|
634 | 634 | self.m_BasicHeader |
|
635 |
self. |
|
|
636 |
self. |
|
|
635 | self.systemHeaderObj | |
|
636 | self.radarControllerHeaderObj | |
|
637 | 637 | self.m_ProcessingHeader |
|
638 | 638 | self.firstHeaderSize |
|
639 | 639 | self.dataType |
|
640 | 640 | self.fileSizeByHeader |
|
641 | 641 | self.ippSeconds |
|
642 | 642 | |
|
643 | 643 | Return: |
|
644 | 644 | None |
|
645 | 645 | """ |
|
646 | 646 | self.__rdBasicHeader() |
|
647 | 647 | self.__rdSystemHeader() |
|
648 | 648 | self.__rdRadarControllerHeader() |
|
649 | 649 | self.__rdProcessingHeader() |
|
650 | 650 | self.firstHeaderSize = self.m_BasicHeader.size |
|
651 | 651 | |
|
652 | 652 | datatype = int(numpy.log2((self.m_ProcessingHeader.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR)) |
|
653 | 653 | if datatype == 0: |
|
654 | 654 | datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
655 | 655 | |
|
656 | 656 | elif datatype == 1: |
|
657 | 657 | datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
658 | 658 | |
|
659 | 659 | elif datatype == 2: |
|
660 | 660 | datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
661 | 661 | |
|
662 | 662 | elif datatype == 3: |
|
663 | 663 | datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
664 | 664 | |
|
665 | 665 | elif datatype == 4: |
|
666 | 666 | datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
667 | 667 | |
|
668 | 668 | elif datatype == 5: |
|
669 | 669 | datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
670 | 670 | |
|
671 | 671 | else: |
|
672 | 672 | raise ValueError, 'Data type was not defined' |
|
673 | 673 | |
|
674 | 674 | self.dataType = datatype_str |
|
675 |
self.ippSeconds = 2 * 1000 * self. |
|
|
675 | self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c | |
|
676 | 676 | |
|
677 | 677 | self.fileSizeByHeader = self.m_ProcessingHeader.dataBlocksPerFile * self.m_ProcessingHeader.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.m_ProcessingHeader.dataBlocksPerFile - 1) |
|
678 |
self.dataOutObj.channelList = numpy.arange(self. |
|
|
679 |
self.dataOutObj.channelIndexList = numpy.arange(self. |
|
|
678 | self.dataOutObj.channelList = numpy.arange(self.systemHeaderObj.numChannels) | |
|
679 | self.dataOutObj.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels) | |
|
680 | 680 | |
|
681 | 681 | self.getBlockDimension() |
|
682 | 682 | |
|
683 | 683 | |
|
684 | 684 | def __setNewBlock(self): |
|
685 | 685 | """ |
|
686 | 686 | Lee el Basic Header y posiciona le file pointer en la posicion inicial del bloque a leer |
|
687 | 687 | |
|
688 | 688 | Affected: |
|
689 | 689 | self.m_BasicHeader |
|
690 | 690 | self.flagNoContinuousBlock |
|
691 | 691 | self.ns |
|
692 | 692 | |
|
693 | 693 | Return: |
|
694 | 694 | 0 : Si el file no tiene un Basic Header que pueda ser leido |
|
695 | 695 | 1 : Si se pudo leer el Basic Header |
|
696 | 696 | """ |
|
697 | 697 | if self.fp == None: |
|
698 | 698 | return 0 |
|
699 | 699 | |
|
700 | 700 | if self.flagIsNewFile: |
|
701 | 701 | return 1 |
|
702 | 702 | |
|
703 | 703 | self.lastUTTime = self.m_BasicHeader.utc |
|
704 | 704 | |
|
705 | 705 | currentSize = self.fileSize - self.fp.tell() |
|
706 | 706 | neededSize = self.m_ProcessingHeader.blockSize + self.basicHeaderSize |
|
707 | 707 | |
|
708 | 708 | #If there is enough data setting new data block |
|
709 | 709 | if ( currentSize >= neededSize ): |
|
710 | 710 | self.__rdBasicHeader() |
|
711 | 711 | return 1 |
|
712 | 712 | |
|
713 | 713 | #si es OnLine y ademas aun no se han leido un bloque completo entonces se espera por uno valido |
|
714 | 714 | if self.online and (self.nReadBlocks < self.m_ProcessingHeader.dataBlocksPerFile): |
|
715 | 715 | |
|
716 | 716 | fpointer = self.fp.tell() |
|
717 | 717 | |
|
718 | 718 | for nTries in range( self.nTries ): |
|
719 | 719 | #self.fp.close() |
|
720 | 720 | |
|
721 | 721 | print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1) |
|
722 | 722 | time.sleep( self.delay ) |
|
723 | 723 | |
|
724 | 724 | #self.fp = open( self.filename, 'rb' ) |
|
725 | 725 | #self.fp.seek( fpointer ) |
|
726 | 726 | |
|
727 | 727 | self.fileSize = os.path.getsize( self.filename ) |
|
728 | 728 | currentSize = self.fileSize - fpointer |
|
729 | 729 | |
|
730 | 730 | if ( currentSize >= neededSize ): |
|
731 | 731 | self.__rdBasicHeader() |
|
732 | 732 | return 1 |
|
733 | 733 | |
|
734 | 734 | #Setting new file |
|
735 | 735 | if not( self.setNextFile() ): |
|
736 | 736 | return 0 |
|
737 | 737 | |
|
738 | 738 | deltaTime = self.m_BasicHeader.utc - self.lastUTTime # check this |
|
739 | 739 | |
|
740 | 740 | self.flagResetProcessing = 0 |
|
741 | 741 | |
|
742 | 742 | if deltaTime > self.maxTimeStep: |
|
743 | 743 | self.flagResetProcessing = 1 |
|
744 | 744 | |
|
745 | 745 | return 1 |
|
746 | 746 | |
|
747 | 747 | def readNextBlock(self): |
|
748 | 748 | """ |
|
749 | 749 | Establece un nuevo bloque de datos a leer y los lee, si es que no existiese |
|
750 | 750 | mas bloques disponibles en el archivo actual salta al siguiente. |
|
751 | 751 | |
|
752 | 752 | Affected: |
|
753 | 753 | self.lastUTTime |
|
754 | 754 | |
|
755 | 755 | Return: None |
|
756 | 756 | """ |
|
757 | 757 | |
|
758 | 758 | if not(self.__setNewBlock()): |
|
759 | 759 | return 0 |
|
760 | 760 | |
|
761 | 761 | if not(self.readBlock()): |
|
762 | 762 | return 0 |
|
763 | 763 | |
|
764 | 764 | return 1 |
|
765 | 765 | |
|
766 | 766 | def __setNextFileOnline(self): |
|
767 | 767 | """ |
|
768 | 768 | Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si |
|
769 | 769 | no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files |
|
770 | 770 | siguientes. |
|
771 | 771 | |
|
772 | 772 | Affected: |
|
773 | 773 | self.flagIsNewFile |
|
774 | 774 | self.filename |
|
775 | 775 | self.fileSize |
|
776 | 776 | self.fp |
|
777 | 777 | self.set |
|
778 | 778 | self.flagNoMoreFiles |
|
779 | 779 | |
|
780 | 780 | Return: |
|
781 | 781 | 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado |
|
782 | 782 | 1 : si el file fue abierto con exito y esta listo a ser leido |
|
783 | 783 | |
|
784 | 784 | Excepciones: |
|
785 | 785 | Si un determinado file no puede ser abierto |
|
786 | 786 | """ |
|
787 | 787 | nFiles = 0 |
|
788 | 788 | fileOk_flag = False |
|
789 | 789 | firstTime_flag = True |
|
790 | 790 | |
|
791 | 791 | self.set += 1 |
|
792 | 792 | |
|
793 | 793 | #busca el 1er file disponible |
|
794 | 794 | file, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext ) |
|
795 | 795 | if file: |
|
796 | 796 | if self.__verifyFile(file, False): |
|
797 | 797 | fileOk_flag = True |
|
798 | 798 | |
|
799 | 799 | #si no encuentra un file entonces espera y vuelve a buscar |
|
800 | 800 | if not(fileOk_flag): |
|
801 | 801 | for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles |
|
802 | 802 | |
|
803 | 803 | if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces |
|
804 | 804 | tries = self.nTries |
|
805 | 805 | else: |
|
806 | 806 | tries = 1 #si no es la 1era vez entonces solo lo hace una vez |
|
807 | 807 | |
|
808 | 808 | for nTries in range( tries ): |
|
809 | 809 | if firstTime_flag: |
|
810 | 810 | print "\tWaiting %0.2f sec for new \"%s\" file, try %03d ..." % ( self.delay, filename, nTries+1 ) |
|
811 | 811 | time.sleep( self.delay ) |
|
812 | 812 | else: |
|
813 | 813 | print "\tSearching next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext) |
|
814 | 814 | |
|
815 | 815 | file, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext ) |
|
816 | 816 | if file: |
|
817 | 817 | if self.__verifyFile(file): |
|
818 | 818 | fileOk_flag = True |
|
819 | 819 | break |
|
820 | 820 | |
|
821 | 821 | if fileOk_flag: |
|
822 | 822 | break |
|
823 | 823 | |
|
824 | 824 | firstTime_flag = False |
|
825 | 825 | |
|
826 | 826 | print "\tSkipping the file \"%s\" due to this file doesn't exist yet" % filename |
|
827 | 827 | self.set += 1 |
|
828 | 828 | |
|
829 | 829 | if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta |
|
830 | 830 | self.set = 0 |
|
831 | 831 | self.doy += 1 |
|
832 | 832 | |
|
833 | 833 | if fileOk_flag: |
|
834 | 834 | self.fileSize = os.path.getsize( file ) |
|
835 | 835 | self.filename = file |
|
836 | 836 | self.flagIsNewFile = 1 |
|
837 | 837 | if self.fp != None: self.fp.close() |
|
838 | 838 | self.fp = open(file) |
|
839 | 839 | self.flagNoMoreFiles = 0 |
|
840 | 840 | print 'Setting the file: %s' % file |
|
841 | 841 | else: |
|
842 | 842 | self.fileSize = 0 |
|
843 | 843 | self.filename = None |
|
844 | 844 | self.flagIsNewFile = 0 |
|
845 | 845 | self.fp = None |
|
846 | 846 | self.flagNoMoreFiles = 1 |
|
847 | 847 | print 'No more Files' |
|
848 | 848 | |
|
849 | 849 | return fileOk_flag |
|
850 | 850 | |
|
851 | 851 | |
|
852 | 852 | def __setNextFileOffline(self): |
|
853 | 853 | """ |
|
854 | 854 | Busca el siguiente file dentro de un folder que tenga suficiente data para ser leida |
|
855 | 855 | |
|
856 | 856 | Affected: |
|
857 | 857 | self.flagIsNewFile |
|
858 | 858 | self.fileIndex |
|
859 | 859 | self.filename |
|
860 | 860 | self.fileSize |
|
861 | 861 | self.fp |
|
862 | 862 | |
|
863 | 863 | Return: |
|
864 | 864 | 0 : si un determinado file no puede ser abierto |
|
865 | 865 | 1 : si el file fue abierto con exito |
|
866 | 866 | |
|
867 | 867 | Excepciones: |
|
868 | 868 | Si un determinado file no puede ser abierto |
|
869 | 869 | """ |
|
870 | 870 | idFile = self.fileIndex |
|
871 | 871 | |
|
872 | 872 | while(True): |
|
873 | 873 | |
|
874 | 874 | idFile += 1 |
|
875 | 875 | |
|
876 | 876 | if not(idFile < len(self.filenameList)): |
|
877 | 877 | self.flagNoMoreFiles = 1 |
|
878 | 878 | print 'No more Files' |
|
879 | 879 | return 0 |
|
880 | 880 | |
|
881 | 881 | filename = self.filenameList[idFile] |
|
882 | 882 | |
|
883 | 883 | if not(self.__verifyFile(filename)): |
|
884 | 884 | continue |
|
885 | 885 | |
|
886 | 886 | fileSize = os.path.getsize(filename) |
|
887 | 887 | fp = open(filename,'rb') |
|
888 | 888 | break |
|
889 | 889 | |
|
890 | 890 | self.flagIsNewFile = 1 |
|
891 | 891 | self.fileIndex = idFile |
|
892 | 892 | self.filename = filename |
|
893 | 893 | self.fileSize = fileSize |
|
894 | 894 | self.fp = fp |
|
895 | 895 | |
|
896 | 896 | print 'Setting the file: %s'%self.filename |
|
897 | 897 | |
|
898 | 898 | return 1 |
|
899 | 899 | |
|
900 | 900 | |
|
901 | 901 | def setNextFile(self): |
|
902 | 902 | """ |
|
903 | 903 | Determina el siguiente file a leer y si hay uno disponible lee el First Header |
|
904 | 904 | |
|
905 | 905 | Affected: |
|
906 | 906 | self.m_BasicHeader |
|
907 |
self. |
|
|
908 |
self. |
|
|
907 | self.systemHeaderObj | |
|
908 | self.radarControllerHeaderObj | |
|
909 | 909 | self.m_ProcessingHeader |
|
910 | 910 | self.firstHeaderSize |
|
911 | 911 | |
|
912 | 912 | Return: |
|
913 | 913 | 0 : Si no hay files disponibles |
|
914 | 914 | 1 : Si hay mas files disponibles |
|
915 | 915 | """ |
|
916 | 916 | if self.fp != None: |
|
917 | 917 | self.fp.close() |
|
918 | 918 | |
|
919 | 919 | if self.online: |
|
920 | 920 | newFile = self.__setNextFileOnline() |
|
921 | 921 | else: |
|
922 | 922 | newFile = self.__setNextFileOffline() |
|
923 | 923 | |
|
924 | 924 | if not(newFile): |
|
925 | 925 | return 0 |
|
926 | 926 | |
|
927 | 927 | self.__readFirstHeader() |
|
928 | 928 | self.nReadBlocks = 0 |
|
929 | 929 | return 1 |
|
930 | 930 | |
|
931 | 931 | def __verifyFile(self, filename, msgFlag=True): |
|
932 | 932 | """ |
|
933 | 933 | Verifica que el filename tenga data valida, para ello leo el FirstHeader del file |
|
934 | 934 | |
|
935 | 935 | Return: |
|
936 | 936 | 0 : file no valido para ser leido |
|
937 | 937 | 1 : file valido para ser leido |
|
938 | 938 | """ |
|
939 | 939 | msg = None |
|
940 | 940 | |
|
941 | 941 | try: |
|
942 | 942 | fp = open( filename,'rb' ) #lectura binaria |
|
943 | 943 | currentPosition = fp.tell() |
|
944 | 944 | except: |
|
945 | 945 | if msgFlag: |
|
946 | 946 | print "The file %s can't be opened" % (filename) |
|
947 | 947 | return False |
|
948 | 948 | |
|
949 | 949 | neededSize = self.m_ProcessingHeader.blockSize + self.firstHeaderSize |
|
950 | 950 | |
|
951 | 951 | if neededSize == 0: |
|
952 | 952 | |
|
953 | 953 | m_BasicHeader = BasicHeader() |
|
954 |
|
|
|
955 |
|
|
|
954 | systemHeaderObj = SystemHeader() | |
|
955 | radarControllerHeaderObj = RadarControllerHeader() | |
|
956 | 956 | m_ProcessingHeader = ProcessingHeader() |
|
957 | 957 | |
|
958 | 958 | try: |
|
959 | 959 | if not( m_BasicHeader.read(fp) ): raise ValueError |
|
960 |
if not( |
|
|
961 |
if not( |
|
|
960 | if not( systemHeaderObj.read(fp) ): raise ValueError | |
|
961 | if not( radarControllerHeaderObj.read(fp) ): raise ValueError | |
|
962 | 962 | if not( m_ProcessingHeader.read(fp) ): raise ValueError |
|
963 | 963 | data_type = int(numpy.log2((m_ProcessingHeader.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR)) |
|
964 | 964 | |
|
965 | 965 | neededSize = m_ProcessingHeader.blockSize + m_BasicHeader.size |
|
966 | 966 | |
|
967 | 967 | except: |
|
968 | 968 | if msgFlag: |
|
969 | 969 | print "\tThe file %s is empty or it hasn't enough data" % filename |
|
970 | 970 | |
|
971 | 971 | fp.close() |
|
972 | 972 | return False |
|
973 | 973 | |
|
974 | 974 | else: |
|
975 | 975 | msg = "\tSkipping the file %s due to it hasn't enough data" %filename |
|
976 | 976 | |
|
977 | 977 | fp.close() |
|
978 | 978 | fileSize = os.path.getsize(filename) |
|
979 | 979 | currentSize = fileSize - currentPosition |
|
980 | 980 | |
|
981 | 981 | if currentSize < neededSize: |
|
982 | 982 | if msgFlag and (msg != None): |
|
983 | 983 | print msg #print"\tSkipping the file %s due to it hasn't enough data" %filename |
|
984 | 984 | return False |
|
985 | 985 | |
|
986 | 986 | return True |
|
987 | 987 | |
|
988 | 988 | def updateDataHeader(self): |
|
989 | 989 | |
|
990 | 990 | self.dataOutObj.m_BasicHeader = self.m_BasicHeader.copy() |
|
991 | 991 | self.dataOutObj.m_ProcessingHeader = self.m_ProcessingHeader.copy() |
|
992 |
self.dataOutObj. |
|
|
993 |
self.dataOutObj. |
|
|
992 | self.dataOutObj.radarControllerHeaderObj = self.radarControllerHeaderObj.copy() | |
|
993 | self.dataOutObj.systemHeaderObj = self.systemHeaderObj.copy() | |
|
994 | 994 | |
|
995 | 995 | self.dataOutObj.dataType = self.dataType |
|
996 | 996 | self.dataOutObj.updateObjFromHeader() |
|
997 | 997 | |
|
998 | 998 | |
|
999 | 999 | class JRODataWriter(JRODataIO): |
|
1000 | 1000 | |
|
1001 | 1001 | """ |
|
1002 | 1002 | Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura |
|
1003 | 1003 | de los datos siempre se realiza por bloques. |
|
1004 | 1004 | """ |
|
1005 | 1005 | |
|
1006 | 1006 | nWriteBlocks = 0 |
|
1007 | 1007 | |
|
1008 | 1008 | setFile = None |
|
1009 | 1009 | |
|
1010 | 1010 | |
|
1011 | 1011 | def __init__(self, dataOutObj=None): |
|
1012 | 1012 | raise ValueError, "Not implemented" |
|
1013 | 1013 | |
|
1014 | 1014 | |
|
1015 | 1015 | def hasAllDataInBuffer(self): |
|
1016 | 1016 | raise ValueError, "Not implemented" |
|
1017 | 1017 | |
|
1018 | 1018 | |
|
1019 | 1019 | def setBlockDimension(self): |
|
1020 | 1020 | raise ValueError, "Not implemented" |
|
1021 | 1021 | |
|
1022 | 1022 | |
|
1023 | 1023 | def writeBlock(self): |
|
1024 | 1024 | raise ValueError, "No implemented" |
|
1025 | 1025 | |
|
1026 | 1026 | |
|
1027 | 1027 | def putData(self): |
|
1028 | 1028 | raise ValueError, "No implemented" |
|
1029 | 1029 | |
|
1030 | 1030 | |
|
1031 | 1031 | def __writeFirstHeader(self): |
|
1032 | 1032 | """ |
|
1033 | 1033 | Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader) |
|
1034 | 1034 | |
|
1035 | 1035 | Affected: |
|
1036 | 1036 | __dataType |
|
1037 | 1037 | |
|
1038 | 1038 | Return: |
|
1039 | 1039 | None |
|
1040 | 1040 | """ |
|
1041 | 1041 | self.__writeBasicHeader() |
|
1042 | 1042 | self.__wrSystemHeader() |
|
1043 | 1043 | self.__wrRadarControllerHeader() |
|
1044 | 1044 | self.__wrProcessingHeader() |
|
1045 | 1045 | self.dataType = self.dataOutObj.dataType |
|
1046 | 1046 | |
|
1047 | 1047 | |
|
1048 | 1048 | def __writeBasicHeader(self, fp=None): |
|
1049 | 1049 | """ |
|
1050 | 1050 | Escribe solo el Basic header en el file creado |
|
1051 | 1051 | |
|
1052 | 1052 | Return: |
|
1053 | 1053 | None |
|
1054 | 1054 | """ |
|
1055 | 1055 | if fp == None: |
|
1056 | 1056 | fp = self.fp |
|
1057 | 1057 | |
|
1058 | 1058 | self.dataOutObj.m_BasicHeader.write(fp) |
|
1059 | 1059 | |
|
1060 | 1060 | |
|
1061 | 1061 | def __wrSystemHeader(self, fp=None): |
|
1062 | 1062 | """ |
|
1063 | 1063 | Escribe solo el System header en el file creado |
|
1064 | 1064 | |
|
1065 | 1065 | Return: |
|
1066 | 1066 | None |
|
1067 | 1067 | """ |
|
1068 | 1068 | if fp == None: |
|
1069 | 1069 | fp = self.fp |
|
1070 | 1070 | |
|
1071 |
self.dataOutObj. |
|
|
1071 | self.dataOutObj.systemHeaderObj.write(fp) | |
|
1072 | 1072 | |
|
1073 | 1073 | |
|
1074 | 1074 | def __wrRadarControllerHeader(self, fp=None): |
|
1075 | 1075 | """ |
|
1076 | 1076 | Escribe solo el RadarController header en el file creado |
|
1077 | 1077 | |
|
1078 | 1078 | Return: |
|
1079 | 1079 | None |
|
1080 | 1080 | """ |
|
1081 | 1081 | if fp == None: |
|
1082 | 1082 | fp = self.fp |
|
1083 | 1083 | |
|
1084 |
self.dataOutObj. |
|
|
1084 | self.dataOutObj.radarControllerHeaderObj.write(fp) | |
|
1085 | 1085 | |
|
1086 | 1086 | |
|
1087 | 1087 | def __wrProcessingHeader(self, fp=None): |
|
1088 | 1088 | """ |
|
1089 | 1089 | Escribe solo el Processing header en el file creado |
|
1090 | 1090 | |
|
1091 | 1091 | Return: |
|
1092 | 1092 | None |
|
1093 | 1093 | """ |
|
1094 | 1094 | if fp == None: |
|
1095 | 1095 | fp = self.fp |
|
1096 | 1096 | |
|
1097 | 1097 | self.dataOutObj.m_ProcessingHeader.write(fp) |
|
1098 | 1098 | |
|
1099 | 1099 | |
|
1100 | 1100 | def setNextFile(self): |
|
1101 | 1101 | """ |
|
1102 | 1102 | Determina el siguiente file que sera escrito |
|
1103 | 1103 | |
|
1104 | 1104 | Affected: |
|
1105 | 1105 | self.filename |
|
1106 | 1106 | self.subfolder |
|
1107 | 1107 | self.fp |
|
1108 | 1108 | self.setFile |
|
1109 | 1109 | self.flagIsNewFile |
|
1110 | 1110 | |
|
1111 | 1111 | Return: |
|
1112 | 1112 | 0 : Si el archivo no puede ser escrito |
|
1113 | 1113 | 1 : Si el archivo esta listo para ser escrito |
|
1114 | 1114 | """ |
|
1115 | 1115 | ext = self.ext |
|
1116 | 1116 | path = self.path |
|
1117 | 1117 | |
|
1118 | 1118 | if self.fp != None: |
|
1119 | 1119 | self.fp.close() |
|
1120 | 1120 | |
|
1121 | 1121 | timeTuple = time.localtime( self.dataOutObj.m_BasicHeader.utc ) |
|
1122 | 1122 | subfolder = 'D%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) |
|
1123 | 1123 | |
|
1124 | 1124 | doypath = os.path.join( path, subfolder ) |
|
1125 | 1125 | if not( os.path.exists(doypath) ): |
|
1126 | 1126 | os.mkdir(doypath) |
|
1127 | 1127 | self.setFile = -1 #inicializo mi contador de seteo |
|
1128 | 1128 | else: |
|
1129 | 1129 | filesList = os.listdir( doypath ) |
|
1130 | 1130 | if len( filesList ) > 0: |
|
1131 | 1131 | filesList = sorted( filesList, key=str.lower ) |
|
1132 | 1132 | filen = filesList[-1] |
|
1133 | 1133 | # el filename debera tener el siguiente formato |
|
1134 | 1134 | # 0 1234 567 89A BCDE (hex) |
|
1135 | 1135 | # x YYYY DDD SSS .ext |
|
1136 | 1136 | if isNumber( filen[8:11] ): |
|
1137 | 1137 | self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file |
|
1138 | 1138 | else: |
|
1139 | 1139 | self.setFile = -1 |
|
1140 | 1140 | else: |
|
1141 | 1141 | self.setFile = -1 #inicializo mi contador de seteo |
|
1142 | 1142 | |
|
1143 | 1143 | setFile = self.setFile |
|
1144 | 1144 | setFile += 1 |
|
1145 | 1145 | |
|
1146 | 1146 | file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, |
|
1147 | 1147 | timeTuple.tm_year, |
|
1148 | 1148 | timeTuple.tm_yday, |
|
1149 | 1149 | setFile, |
|
1150 | 1150 | ext ) |
|
1151 | 1151 | |
|
1152 | 1152 | filename = os.path.join( path, subfolder, file ) |
|
1153 | 1153 | |
|
1154 | 1154 | fp = open( filename,'wb' ) |
|
1155 | 1155 | |
|
1156 | 1156 | self.nWriteBlocks = 0 |
|
1157 | 1157 | |
|
1158 | 1158 | #guardando atributos |
|
1159 | 1159 | self.filename = filename |
|
1160 | 1160 | self.subfolder = subfolder |
|
1161 | 1161 | self.fp = fp |
|
1162 | 1162 | self.setFile = setFile |
|
1163 | 1163 | self.flagIsNewFile = 1 |
|
1164 | 1164 | |
|
1165 | 1165 | print 'Writing the file: %s'%self.filename |
|
1166 | 1166 | |
|
1167 | 1167 | self.__writeFirstHeader() |
|
1168 | 1168 | |
|
1169 | 1169 | return 1 |
|
1170 | 1170 | |
|
1171 | 1171 | |
|
1172 | 1172 | def __setNewBlock(self): |
|
1173 | 1173 | """ |
|
1174 | 1174 | Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header |
|
1175 | 1175 | |
|
1176 | 1176 | Return: |
|
1177 | 1177 | 0 : si no pudo escribir nada |
|
1178 | 1178 | 1 : Si escribio el Basic el First Header |
|
1179 | 1179 | """ |
|
1180 | 1180 | if self.fp == None: |
|
1181 | 1181 | self.setNextFile() |
|
1182 | 1182 | |
|
1183 | 1183 | if self.flagIsNewFile: |
|
1184 | 1184 | return 1 |
|
1185 | 1185 | |
|
1186 | 1186 | if self.nWriteBlocks < self.m_ProcessingHeader.dataBlocksPerFile: |
|
1187 | 1187 | self.__writeBasicHeader() |
|
1188 | 1188 | return 1 |
|
1189 | 1189 | |
|
1190 | 1190 | if not( self.setNextFile() ): |
|
1191 | 1191 | return 0 |
|
1192 | 1192 | |
|
1193 | 1193 | return 1 |
|
1194 | 1194 | |
|
1195 | 1195 | |
|
1196 | 1196 | def writeNextBlock(self): |
|
1197 | 1197 | """ |
|
1198 | 1198 | Selecciona el bloque siguiente de datos y los escribe en un file |
|
1199 | 1199 | |
|
1200 | 1200 | Return: |
|
1201 | 1201 | 0 : Si no hizo pudo escribir el bloque de datos |
|
1202 | 1202 | 1 : Si no pudo escribir el bloque de datos |
|
1203 | 1203 | """ |
|
1204 | 1204 | if not( self.__setNewBlock() ): |
|
1205 | 1205 | return 0 |
|
1206 | 1206 | |
|
1207 | 1207 | self.writeBlock() |
|
1208 | 1208 | |
|
1209 | 1209 | return 1 |
|
1210 | 1210 | |
|
1211 | 1211 | |
|
1212 | 1212 | def getDataHeader(self): |
|
1213 | 1213 | """ |
|
1214 | 1214 | Obtiene una copia del First Header |
|
1215 | 1215 | |
|
1216 | 1216 | Affected: |
|
1217 | 1217 | self.m_BasicHeader |
|
1218 |
self. |
|
|
1219 |
self. |
|
|
1218 | self.systemHeaderObj | |
|
1219 | self.radarControllerHeaderObj | |
|
1220 | 1220 | self.m_ProcessingHeader |
|
1221 | 1221 | self.dataType |
|
1222 | 1222 | |
|
1223 | 1223 | Return: |
|
1224 | 1224 | None |
|
1225 | 1225 | """ |
|
1226 | 1226 | self.dataOutObj.updateHeaderFromObj() |
|
1227 | 1227 | |
|
1228 | 1228 | self.m_BasicHeader = self.dataOutObj.m_BasicHeader.copy() |
|
1229 |
self. |
|
|
1230 |
self. |
|
|
1229 | self.systemHeaderObj = self.dataOutObj.systemHeaderObj.copy() | |
|
1230 | self.radarControllerHeaderObj = self.dataOutObj.radarControllerHeaderObj.copy() | |
|
1231 | 1231 | self.m_ProcessingHeader = self.dataOutObj.m_ProcessingHeader.copy() |
|
1232 | 1232 | |
|
1233 | 1233 | self.dataType = self.dataOutObj.dataType |
|
1234 | 1234 | |
|
1235 | 1235 | |
|
1236 | 1236 | def setup(self, path, set=0, ext=None): |
|
1237 | 1237 | """ |
|
1238 | 1238 | Setea el tipo de formato en la cual sera guardada la data y escribe el First Header |
|
1239 | 1239 | |
|
1240 | 1240 | Inputs: |
|
1241 | 1241 | path : el path destino en el cual se escribiran los files a crear |
|
1242 | 1242 | format : formato en el cual sera salvado un file |
|
1243 | 1243 | set : el setebo del file |
|
1244 | 1244 | |
|
1245 | 1245 | Return: |
|
1246 | 1246 | 0 : Si no realizo un buen seteo |
|
1247 | 1247 | 1 : Si realizo un buen seteo |
|
1248 | 1248 | """ |
|
1249 | 1249 | |
|
1250 | 1250 | if ext == None: |
|
1251 | 1251 | ext = self.ext |
|
1252 | 1252 | |
|
1253 | 1253 | ext = ext.lower() |
|
1254 | 1254 | |
|
1255 | 1255 | self.path = path |
|
1256 | 1256 | self.setFile = set - 1 |
|
1257 | 1257 | self.ext = ext |
|
1258 | 1258 | #self.format = format |
|
1259 | 1259 | self.getDataHeader() |
|
1260 | 1260 | |
|
1261 | 1261 | self.setBlockDimension() |
|
1262 | 1262 | |
|
1263 | 1263 | if not( self.setNextFile() ): |
|
1264 | 1264 | print "There isn't a next file" |
|
1265 | 1265 | return 0 |
|
1266 | 1266 | |
|
1267 | 1267 | return 1 |
@@ -1,535 +1,535 | |||
|
1 | 1 | ''' |
|
2 | 2 | File: SpectraIO.py |
|
3 | 3 | Created on 20/02/2012 |
|
4 | 4 | |
|
5 | 5 | @author $Author$ |
|
6 | 6 | @version $Id$ |
|
7 | 7 | ''' |
|
8 | 8 | |
|
9 | 9 | import os, sys |
|
10 | 10 | import numpy |
|
11 | 11 | import glob |
|
12 | 12 | import fnmatch |
|
13 | 13 | import time, datetime |
|
14 | 14 | |
|
15 | 15 | path = os.path.split(os.getcwd())[0] |
|
16 | 16 | sys.path.append(path) |
|
17 | 17 | |
|
18 | 18 | from Model.JROHeader import * |
|
19 | 19 | from Model.Spectra import Spectra |
|
20 | 20 | |
|
21 | 21 | from JRODataIO import JRODataReader |
|
22 | 22 | from JRODataIO import JRODataWriter |
|
23 | 23 | from JRODataIO import isNumber |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | class SpectraReader(JRODataReader): |
|
27 | 27 | """ |
|
28 | 28 | Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura |
|
29 | 29 | de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones) |
|
30 | 30 | son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel. |
|
31 | 31 | |
|
32 | 32 | paresCanalesIguales * alturas * perfiles (Self Spectra) |
|
33 | 33 | paresCanalesDiferentes * alturas * perfiles (Cross Spectra) |
|
34 | 34 | canales * alturas (DC Channels) |
|
35 | 35 | |
|
36 | 36 | Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader, |
|
37 | 37 | RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la |
|
38 | 38 | cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de |
|
39 | 39 | datos desde el "buffer" cada vez que se ejecute el metodo "getData". |
|
40 | 40 | |
|
41 | 41 | Example: |
|
42 | 42 | dpath = "/home/myuser/data" |
|
43 | 43 | |
|
44 | 44 | startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0) |
|
45 | 45 | |
|
46 | 46 | endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0) |
|
47 | 47 | |
|
48 | 48 | readerObj = SpectraReader() |
|
49 | 49 | |
|
50 | 50 | readerObj.setup(dpath, startTime, endTime) |
|
51 | 51 | |
|
52 | 52 | while(True): |
|
53 | 53 | |
|
54 | 54 | readerObj.getData() |
|
55 | 55 | |
|
56 | 56 | print readerObj.dataOutObj.data |
|
57 | 57 | |
|
58 | 58 | if readerObj.flagNoMoreFiles: |
|
59 | 59 | break |
|
60 | 60 | |
|
61 | 61 | """ |
|
62 | 62 | dataOutObj = None |
|
63 | 63 | |
|
64 | 64 | data_spc = None |
|
65 | 65 | data_cspc = None |
|
66 | 66 | data_dc = None |
|
67 | 67 | |
|
68 | 68 | pts2read_SelfSpectra = 0 |
|
69 | 69 | pts2read_CrossSpectra = 0 |
|
70 | 70 | pts2read_DCchannels = 0 |
|
71 | 71 | |
|
72 | 72 | nChannels = 0 |
|
73 | 73 | nPairs = 0 |
|
74 | 74 | |
|
75 | 75 | flag_cspc = False |
|
76 | 76 | |
|
77 | 77 | def __init__(self, dataOutObj=None): |
|
78 | 78 | """ |
|
79 | 79 | Inicializador de la clase SpectraReader para la lectura de datos de espectros. |
|
80 | 80 | |
|
81 | 81 | Inputs: |
|
82 | 82 | dataOutObj : Objeto de la clase Spectra. Este objeto sera utilizado para |
|
83 | 83 | almacenar un perfil de datos cada vez que se haga un requerimiento |
|
84 | 84 | (getData). El perfil sera obtenido a partir del buffer de datos, |
|
85 | 85 | si el buffer esta vacio se hara un nuevo proceso de lectura de un |
|
86 | 86 | bloque de datos. |
|
87 | 87 | Si este parametro no es pasado se creara uno internamente. |
|
88 | 88 | |
|
89 | 89 | Affected: |
|
90 | 90 | self.dataOutObj |
|
91 | 91 | |
|
92 | 92 | Return : None |
|
93 | 93 | """ |
|
94 | 94 | |
|
95 | 95 | self.data_spc = None |
|
96 | 96 | self.data_cspc = None |
|
97 | 97 | self.data_dc = None |
|
98 | 98 | |
|
99 | 99 | self.pts2read_SelfSpectra = 0 |
|
100 | 100 | self.pts2read_CrossSpectra = 0 |
|
101 | 101 | self.pts2read_DCs = 0 |
|
102 | 102 | |
|
103 | 103 | self.nChannels = 0 |
|
104 | 104 | |
|
105 | 105 | self.nPairs = 0 |
|
106 | 106 | |
|
107 | 107 | self.ext = ".pdata" |
|
108 | 108 | |
|
109 | 109 | self.optchar = "P" |
|
110 | 110 | |
|
111 | 111 | ###################### |
|
112 | 112 | |
|
113 | 113 | self.m_BasicHeader = BasicHeader() |
|
114 | 114 | |
|
115 |
self. |
|
|
115 | self.systemHeaderObj = SystemHeader() | |
|
116 | 116 | |
|
117 |
self. |
|
|
117 | self.radarControllerHeaderObj = RadarControllerHeader() | |
|
118 | 118 | |
|
119 | 119 | self.m_ProcessingHeader = ProcessingHeader() |
|
120 | 120 | |
|
121 | 121 | self.online = 0 |
|
122 | 122 | |
|
123 | 123 | self.fp = None |
|
124 | 124 | |
|
125 | 125 | self.fileSizeByHeader = None |
|
126 | 126 | |
|
127 | 127 | self.filenameList = [] |
|
128 | 128 | |
|
129 | 129 | self.filename = None |
|
130 | 130 | |
|
131 | 131 | self.fileSize = None |
|
132 | 132 | |
|
133 | 133 | self.firstHeaderSize = 0 |
|
134 | 134 | |
|
135 | 135 | self.basicHeaderSize = 24 |
|
136 | 136 | |
|
137 | 137 | self.dataType = None |
|
138 | 138 | |
|
139 | 139 | self.maxTimeStep = 30 |
|
140 | 140 | |
|
141 | 141 | self.flagNoMoreFiles = 0 |
|
142 | 142 | |
|
143 | 143 | self.set = 0 |
|
144 | 144 | |
|
145 | 145 | self.path = None |
|
146 | 146 | |
|
147 |
self.delay = |
|
|
147 | self.delay = 60 #seconds | |
|
148 | 148 | |
|
149 | 149 | self.nTries = 3 #quantity tries |
|
150 | 150 | |
|
151 | 151 | self.nFiles = 3 #number of files for searching |
|
152 | 152 | |
|
153 | 153 | self.nReadBlocks = 0 |
|
154 | 154 | |
|
155 | 155 | self.flagIsNewFile = 1 |
|
156 | 156 | |
|
157 | 157 | self.ippSeconds = 0 |
|
158 | 158 | |
|
159 | 159 | self.flagResetProcessing = 0 |
|
160 | 160 | |
|
161 | 161 | self.flagIsNewBlock = 0 |
|
162 | 162 | |
|
163 | 163 | self.nTotalBlocks = 0 |
|
164 | 164 | |
|
165 | 165 | self.blocksize = 0 |
|
166 | 166 | |
|
167 | 167 | def createObjByDefault(self): |
|
168 | 168 | |
|
169 | 169 | dataObj = Spectra() |
|
170 | 170 | |
|
171 | 171 | return dataObj |
|
172 | 172 | |
|
173 | 173 | def __hasNotDataInBuffer(self): |
|
174 | 174 | return 1 |
|
175 | 175 | |
|
176 | 176 | |
|
177 | 177 | def getBlockDimension(self): |
|
178 | 178 | """ |
|
179 | 179 | Obtiene la cantidad de puntos a leer por cada bloque de datos |
|
180 | 180 | |
|
181 | 181 | Affected: |
|
182 | 182 | self.nChannels |
|
183 | 183 | self.nPairs |
|
184 | 184 | self.pts2read_SelfSpectra |
|
185 | 185 | self.pts2read_CrossSpectra |
|
186 | 186 | self.pts2read_DCchannels |
|
187 | 187 | self.blocksize |
|
188 | 188 | self.dataOutObj.nChannels |
|
189 | 189 | self.dataOutObj.nPairs |
|
190 | 190 | |
|
191 | 191 | Return: |
|
192 | 192 | None |
|
193 | 193 | """ |
|
194 | 194 | self.nChannels = 0 |
|
195 | 195 | self.nPairs = 0 |
|
196 | 196 | self.pairList = [] |
|
197 | 197 | |
|
198 | 198 | for i in range( 0, self.m_ProcessingHeader.totalSpectra*2, 2 ): |
|
199 | 199 | if self.m_ProcessingHeader.spectraComb[i] == self.m_ProcessingHeader.spectraComb[i+1]: |
|
200 | 200 | self.nChannels = self.nChannels + 1 #par de canales iguales |
|
201 | 201 | else: |
|
202 | 202 | self.nPairs = self.nPairs + 1 #par de canales diferentes |
|
203 | 203 | self.pairList.append( (self.m_ProcessingHeader.spectraComb[i], self.m_ProcessingHeader.spectraComb[i+1]) ) |
|
204 | 204 | |
|
205 | 205 | pts2read = self.m_ProcessingHeader.numHeights * self.m_ProcessingHeader.profilesPerBlock |
|
206 | 206 | |
|
207 | 207 | self.pts2read_SelfSpectra = int(self.nChannels * pts2read) |
|
208 | 208 | self.blocksize = self.pts2read_SelfSpectra |
|
209 | 209 | |
|
210 | 210 | if self.m_ProcessingHeader.flag_cspc: |
|
211 | 211 | self.pts2read_CrossSpectra = int(self.nPairs * pts2read) |
|
212 | 212 | self.blocksize += self.pts2read_CrossSpectra |
|
213 | 213 | |
|
214 | 214 | if self.m_ProcessingHeader.flag_dc: |
|
215 |
self.pts2read_DCchannels = int(self. |
|
|
215 | self.pts2read_DCchannels = int(self.systemHeaderObj.numChannels * self.m_ProcessingHeader.numHeights) | |
|
216 | 216 | self.blocksize += self.pts2read_DCchannels |
|
217 | 217 | |
|
218 | 218 | # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels |
|
219 | 219 | |
|
220 | 220 | |
|
221 | 221 | def readBlock(self): |
|
222 | 222 | """ |
|
223 | 223 | Lee el bloque de datos desde la posicion actual del puntero del archivo |
|
224 | 224 | (self.fp) y actualiza todos los parametros relacionados al bloque de datos |
|
225 | 225 | (metadata + data). La data leida es almacenada en el buffer y el contador del buffer |
|
226 | 226 | es seteado a 0 |
|
227 | 227 | |
|
228 | 228 | Return: None |
|
229 | 229 | |
|
230 | 230 | Variables afectadas: |
|
231 | 231 | self.datablockIndex |
|
232 | 232 | self.flagIsNewFile |
|
233 | 233 | self.flagIsNewBlock |
|
234 | 234 | self.nTotalBlocks |
|
235 | 235 | self.data_spc |
|
236 | 236 | self.data_cspc |
|
237 | 237 | self.data_dc |
|
238 | 238 | |
|
239 | 239 | Exceptions: |
|
240 | 240 | Si un bloque leido no es un bloque valido |
|
241 | 241 | """ |
|
242 | 242 | blockOk_flag = False |
|
243 | 243 | fpointer = self.fp.tell() |
|
244 | 244 | |
|
245 | 245 | spc = numpy.fromfile( self.fp, self.dataType[0], self.pts2read_SelfSpectra ) |
|
246 | 246 | spc = spc.reshape( (self.nChannels, self.m_ProcessingHeader.numHeights, self.m_ProcessingHeader.profilesPerBlock) ) #transforma a un arreglo 3D |
|
247 | 247 | |
|
248 | 248 | if self.flag_cspc: |
|
249 | 249 | cspc = numpy.fromfile( self.fp, self.dataType, self.pts2read_CrossSpectra ) |
|
250 | 250 | cspc = cspc.reshape( (self.nPairs, self.m_ProcessingHeader.numHeights, self.m_ProcessingHeader.profilesPerBlock) ) #transforma a un arreglo 3D |
|
251 | 251 | |
|
252 | 252 | if self.m_ProcessingHeader.flag_dc: |
|
253 |
dc = numpy.fromfile( self.fp, self.dataType, self.pts2read_DCchannels ) #int(self.m_ProcessingHeader.numHeights*self. |
|
|
254 |
dc = dc.reshape( (self. |
|
|
253 | dc = numpy.fromfile( self.fp, self.dataType, self.pts2read_DCchannels ) #int(self.m_ProcessingHeader.numHeights*self.systemHeaderObj.numChannels) ) | |
|
254 | dc = dc.reshape( (self.systemHeaderObj.numChannels, self.m_ProcessingHeader.numHeights) ) #transforma a un arreglo 2D | |
|
255 | 255 | |
|
256 | 256 | |
|
257 | 257 | if not(self.m_ProcessingHeader.shif_fft): |
|
258 | 258 | spc = numpy.roll( spc, self.m_ProcessingHeader.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones |
|
259 | 259 | |
|
260 | 260 | if self.flag_cspc: |
|
261 | 261 | cspc = numpy.roll( cspc, self.m_ProcessingHeader.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones |
|
262 | 262 | |
|
263 | 263 | |
|
264 | 264 | spc = numpy.transpose( spc, (0,2,1) ) |
|
265 | 265 | self.data_spc = spc |
|
266 | 266 | |
|
267 | 267 | if self.flag_cspc: |
|
268 | 268 | cspc = numpy.transpose( cspc, (0,2,1) ) |
|
269 | 269 | self.data_cspc = cspc['real'] + cspc['imag']*1j |
|
270 | 270 | else: |
|
271 | 271 | self.data_cspc = None |
|
272 | 272 | |
|
273 | 273 | if self.m_ProcessingHeader.flag_dc: |
|
274 | 274 | self.data_dc = dc['real'] + dc['imag']*1j |
|
275 | 275 | else: |
|
276 | 276 | self.data_dc = None |
|
277 | 277 | |
|
278 | 278 | self.datablockIndex = 0 |
|
279 | 279 | self.flagIsNewFile = 0 |
|
280 | 280 | self.flagIsNewBlock = 1 |
|
281 | 281 | |
|
282 | 282 | self.nTotalBlocks += 1 |
|
283 | 283 | self.nReadBlocks += 1 |
|
284 | 284 | |
|
285 | 285 | return 1 |
|
286 | 286 | |
|
287 | 287 | |
|
288 | 288 | def getData(self): |
|
289 | 289 | """ |
|
290 | 290 | Copia el buffer de lectura a la clase "Spectra", |
|
291 | 291 | con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de |
|
292 | 292 | lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock" |
|
293 | 293 | |
|
294 | 294 | Return: |
|
295 | 295 | 0 : Si no hay mas archivos disponibles |
|
296 | 296 | 1 : Si hizo una buena copia del buffer |
|
297 | 297 | |
|
298 | 298 | Affected: |
|
299 | 299 | self.dataOutObj |
|
300 | 300 | self.datablockIndex |
|
301 | 301 | self.flagResetProcessing |
|
302 | 302 | self.flagIsNewBlock |
|
303 | 303 | """ |
|
304 | 304 | |
|
305 | 305 | if self.flagNoMoreFiles: return 0 |
|
306 | 306 | |
|
307 | 307 | self.flagResetProcessing = 0 |
|
308 | 308 | self.flagIsNewBlock = 0 |
|
309 | 309 | |
|
310 | 310 | if self.__hasNotDataInBuffer(): |
|
311 | 311 | |
|
312 | 312 | if not( self.readNextBlock() ): |
|
313 | 313 | return 0 |
|
314 | 314 | |
|
315 | 315 | self.updateDataHeader() |
|
316 | 316 | |
|
317 | 317 | if self.flagNoMoreFiles == 1: |
|
318 | 318 | print 'Process finished' |
|
319 | 319 | return 0 |
|
320 | 320 | |
|
321 | 321 | #data es un numpy array de 3 dmensiones (perfiles, alturas y canales) |
|
322 | 322 | |
|
323 | 323 | if self.data_dc == None: |
|
324 | 324 | self.dataOutObj.flagNoData = True |
|
325 | 325 | return 0 |
|
326 | 326 | |
|
327 | 327 | self.dataOutObj.flagNoData = False |
|
328 | 328 | self.dataOutObj.flagResetProcessing = self.flagResetProcessing |
|
329 | 329 | |
|
330 | 330 | self.dataOutObj.data_spc = self.data_spc |
|
331 | 331 | self.dataOutObj.data_cspc = self.data_cspc |
|
332 | 332 | self.dataOutObj.data_dc = self.data_dc |
|
333 | 333 | |
|
334 | 334 | return 1 |
|
335 | 335 | |
|
336 | 336 | |
|
337 | 337 | class SpectraWriter(JRODataWriter): |
|
338 | 338 | |
|
339 | 339 | """ |
|
340 | 340 | Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura |
|
341 | 341 | de los datos siempre se realiza por bloques. |
|
342 | 342 | """ |
|
343 | 343 | |
|
344 | 344 | dataOutObj = None |
|
345 | 345 | |
|
346 | 346 | shape_spc_Buffer = None |
|
347 | 347 | shape_cspc_Buffer = None |
|
348 | 348 | shape_dc_Buffer = None |
|
349 | 349 | |
|
350 | 350 | data_spc = None |
|
351 | 351 | data_cspc = None |
|
352 | 352 | data_dc = None |
|
353 | 353 | |
|
354 | 354 | |
|
355 | 355 | def __init__(self, dataOutObj=None): |
|
356 | 356 | """ |
|
357 | 357 | Inicializador de la clase SpectraWriter para la escritura de datos de espectros. |
|
358 | 358 | |
|
359 | 359 | Affected: |
|
360 | 360 | self.dataOutObj |
|
361 | 361 | self.m_BasicHeader |
|
362 |
self. |
|
|
363 |
self. |
|
|
362 | self.systemHeaderObj | |
|
363 | self.radarControllerHeaderObj | |
|
364 | 364 | self.m_ProcessingHeader |
|
365 | 365 | |
|
366 | 366 | Return: None |
|
367 | 367 | """ |
|
368 | 368 | if dataOutObj == None: |
|
369 | 369 | dataOutObj = Spectra() |
|
370 | 370 | |
|
371 | 371 | if not( isinstance(dataOutObj, Spectra) ): |
|
372 | 372 | raise ValueError, "in SpectraReader, dataOutObj must be an Spectra class object" |
|
373 | 373 | |
|
374 | 374 | self.dataOutObj = dataOutObj |
|
375 | 375 | |
|
376 | 376 | self.ext = ".pdata" |
|
377 | 377 | |
|
378 | 378 | self.optchar = "P" |
|
379 | 379 | |
|
380 | 380 | self.shape_spc_Buffer = None |
|
381 | 381 | self.shape_cspc_Buffer = None |
|
382 | 382 | self.shape_dc_Buffer = None |
|
383 | 383 | |
|
384 | 384 | self.data_spc = None |
|
385 | 385 | self.data_cspc = None |
|
386 | 386 | self.data_dc = None |
|
387 | 387 | |
|
388 | 388 | #################################### |
|
389 | 389 | |
|
390 | 390 | self.fp = None |
|
391 | 391 | |
|
392 | 392 | self.nWriteBlocks = 0 |
|
393 | 393 | |
|
394 | 394 | self.flagIsNewFile = 1 |
|
395 | 395 | |
|
396 | 396 | self.nTotalBlocks = 0 |
|
397 | 397 | |
|
398 | 398 | self.flagIsNewBlock = 0 |
|
399 | 399 | |
|
400 | 400 | self.flagNoMoreFiles = 0 |
|
401 | 401 | |
|
402 | 402 | self.setFile = None |
|
403 | 403 | |
|
404 | 404 | self.dataType = None |
|
405 | 405 | |
|
406 | 406 | self.path = None |
|
407 | 407 | |
|
408 | 408 | self.noMoreFiles = 0 |
|
409 | 409 | |
|
410 | 410 | self.filename = None |
|
411 | 411 | |
|
412 | 412 | self.m_BasicHeader= BasicHeader() |
|
413 | 413 | |
|
414 |
self. |
|
|
414 | self.systemHeaderObj = SystemHeader() | |
|
415 | 415 | |
|
416 |
self. |
|
|
416 | self.radarControllerHeaderObj = RadarControllerHeader() | |
|
417 | 417 | |
|
418 | 418 | self.m_ProcessingHeader = ProcessingHeader() |
|
419 | 419 | |
|
420 | 420 | |
|
421 | 421 | def hasAllDataInBuffer(self): |
|
422 | 422 | return 1 |
|
423 | 423 | |
|
424 | 424 | |
|
425 | 425 | def setBlockDimension(self): |
|
426 | 426 | """ |
|
427 | 427 | Obtiene las formas dimensionales del los subbloques de datos que componen un bloque |
|
428 | 428 | |
|
429 | 429 | Affected: |
|
430 | 430 | self.shape_spc_Buffer |
|
431 | 431 | self.shape_cspc_Buffer |
|
432 | 432 | self.shape_dc_Buffer |
|
433 | 433 | |
|
434 | 434 | Return: None |
|
435 | 435 | """ |
|
436 | 436 | self.shape_spc_Buffer = (self.dataOutObj.nChannels, |
|
437 | 437 | self.m_ProcessingHeader.numHeights, |
|
438 | 438 | self.m_ProcessingHeader.profilesPerBlock) |
|
439 | 439 | |
|
440 | 440 | self.shape_cspc_Buffer = (self.dataOutObj.nPairs, |
|
441 | 441 | self.m_ProcessingHeader.numHeights, |
|
442 | 442 | self.m_ProcessingHeader.profilesPerBlock) |
|
443 | 443 | |
|
444 |
self.shape_dc_Buffer = (self. |
|
|
444 | self.shape_dc_Buffer = (self.systemHeaderObj.numChannels, | |
|
445 | 445 | self.m_ProcessingHeader.numHeights) |
|
446 | 446 | |
|
447 | 447 | |
|
448 | 448 | def writeBlock(self): |
|
449 | 449 | """ |
|
450 | 450 | Escribe el buffer en el file designado |
|
451 | 451 | |
|
452 | 452 | Affected: |
|
453 | 453 | self.data_spc |
|
454 | 454 | self.data_cspc |
|
455 | 455 | self.data_dc |
|
456 | 456 | self.flagIsNewFile |
|
457 | 457 | self.flagIsNewBlock |
|
458 | 458 | self.nTotalBlocks |
|
459 | 459 | self.nWriteBlocks |
|
460 | 460 | |
|
461 | 461 | Return: None |
|
462 | 462 | """ |
|
463 | 463 | |
|
464 | 464 | spc = numpy.transpose( self.data_spc, (0,2,1) ) |
|
465 | 465 | if not( self.m_ProcessingHeader.shif_fft ): |
|
466 | 466 | spc = numpy.roll( spc, self.m_ProcessingHeader.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones |
|
467 | 467 | data = spc.reshape((-1)) |
|
468 | 468 | data.tofile(self.fp) |
|
469 | 469 | |
|
470 | 470 | if self.data_cspc != None: |
|
471 | 471 | data = numpy.zeros( self.shape_cspc_Buffer, self.dataType ) |
|
472 | 472 | cspc = numpy.transpose( self.data_cspc, (0,2,1) ) |
|
473 | 473 | if not( self.m_ProcessingHeader.shif_fft ): |
|
474 | 474 | cspc = numpy.roll( cspc, self.m_ProcessingHeader.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones |
|
475 | 475 | data['real'] = cspc.real |
|
476 | 476 | data['imag'] = cspc.imag |
|
477 | 477 | data = data.reshape((-1)) |
|
478 | 478 | data.tofile(self.fp) |
|
479 | 479 | |
|
480 | 480 | data = numpy.zeros( self.shape_dc_Buffer, self.dataType ) |
|
481 | 481 | dc = self.data_dc |
|
482 | 482 | data['real'] = dc.real |
|
483 | 483 | data['imag'] = dc.imag |
|
484 | 484 | data = data.reshape((-1)) |
|
485 | 485 | data.tofile(self.fp) |
|
486 | 486 | |
|
487 | 487 | self.data_spc.fill(0) |
|
488 | 488 | self.data_dc.fill(0) |
|
489 | 489 | if self.data_cspc != None: |
|
490 | 490 | self.data_cspc.fill(0) |
|
491 | 491 | |
|
492 | 492 | self.flagIsNewFile = 0 |
|
493 | 493 | self.flagIsNewBlock = 1 |
|
494 | 494 | self.nTotalBlocks += 1 |
|
495 | 495 | self.nWriteBlocks += 1 |
|
496 | 496 | |
|
497 | 497 | |
|
498 | 498 | def putData(self): |
|
499 | 499 | """ |
|
500 | 500 | Setea un bloque de datos y luego los escribe en un file |
|
501 | 501 | |
|
502 | 502 | Affected: |
|
503 | 503 | self.data_spc |
|
504 | 504 | self.data_cspc |
|
505 | 505 | self.data_dc |
|
506 | 506 | |
|
507 | 507 | Return: |
|
508 | 508 | 0 : Si no hay data o no hay mas files que puedan escribirse |
|
509 | 509 | 1 : Si se escribio la data de un bloque en un file |
|
510 | 510 | """ |
|
511 | 511 | self.flagIsNewBlock = 0 |
|
512 | 512 | |
|
513 | 513 | if self.dataOutObj.flagNoData: |
|
514 | 514 | return 0 |
|
515 | 515 | |
|
516 | 516 | if self.dataOutObj.flagResetProcessing: |
|
517 | 517 | self.data_spc.fill(0) |
|
518 | 518 | self.data_cspc.fill(0) |
|
519 | 519 | self.data_dc.fill(0) |
|
520 | 520 | self.setNextFile() |
|
521 | 521 | |
|
522 | 522 | self.data_spc = self.dataOutObj.data_spc |
|
523 | 523 | self.data_cspc = self.dataOutObj.data_cspc |
|
524 | 524 | self.data_dc = self.dataOutObj.data_dc |
|
525 | 525 | |
|
526 | 526 | # #self.m_ProcessingHeader.dataBlocksPerFile) |
|
527 | 527 | if self.hasAllDataInBuffer(): |
|
528 | 528 | self.getDataHeader() |
|
529 | 529 | self.writeNextBlock() |
|
530 | 530 | |
|
531 | 531 | if self.flagNoMoreFiles: |
|
532 | 532 | #print 'Process finished' |
|
533 | 533 | return 0 |
|
534 | 534 | |
|
535 | 535 | return 1 No newline at end of file |
@@ -1,433 +1,433 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on 23/01/2012 |
|
3 | 3 | |
|
4 | 4 | @author $Author$ |
|
5 | 5 | @version $Id$ |
|
6 | 6 | ''' |
|
7 | 7 | |
|
8 | 8 | import os, sys |
|
9 | 9 | import numpy |
|
10 | 10 | import glob |
|
11 | 11 | import fnmatch |
|
12 | 12 | import time, datetime |
|
13 | 13 | |
|
14 | 14 | path = os.path.split(os.getcwd())[0] |
|
15 | 15 | sys.path.append(path) |
|
16 | 16 | |
|
17 | 17 | from Model.JROHeader import * |
|
18 | 18 | from Model.Voltage import Voltage |
|
19 | 19 | |
|
20 | 20 | from IO.JRODataIO import JRODataReader |
|
21 | 21 | from IO.JRODataIO import JRODataWriter |
|
22 | 22 | |
|
23 | 23 | |
|
24 | 24 | class VoltageReader(JRODataReader): |
|
25 | 25 | """ |
|
26 | 26 | Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura |
|
27 | 27 | de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones: |
|
28 | 28 | perfiles*alturas*canales) son almacenados en la variable "buffer". |
|
29 | 29 | |
|
30 | 30 | perfiles * alturas * canales |
|
31 | 31 | |
|
32 | 32 | Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader, |
|
33 | 33 | RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la |
|
34 | 34 | cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de |
|
35 | 35 | datos desde el "buffer" cada vez que se ejecute el metodo "getData". |
|
36 | 36 | |
|
37 | 37 | Example: |
|
38 | 38 | |
|
39 | 39 | dpath = "/home/myuser/data" |
|
40 | 40 | |
|
41 | 41 | startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0) |
|
42 | 42 | |
|
43 | 43 | endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0) |
|
44 | 44 | |
|
45 | 45 | readerObj = VoltageReader() |
|
46 | 46 | |
|
47 | 47 | readerObj.setup(dpath, startTime, endTime) |
|
48 | 48 | |
|
49 | 49 | while(True): |
|
50 | 50 | |
|
51 | 51 | #to get one profile |
|
52 | 52 | profile = readerObj.getData() |
|
53 | 53 | |
|
54 | 54 | #print the profile |
|
55 | 55 | print profile |
|
56 | 56 | |
|
57 | 57 | #If you want to see all datablock |
|
58 | 58 | print readerObj.datablock |
|
59 | 59 | |
|
60 | 60 | if readerObj.flagNoMoreFiles: |
|
61 | 61 | break |
|
62 | 62 | |
|
63 | 63 | """ |
|
64 | 64 | dataOutObj = None |
|
65 | 65 | |
|
66 | 66 | datablock = None |
|
67 | 67 | |
|
68 | 68 | ext = ".r" |
|
69 | 69 | |
|
70 | 70 | optchar = "D" |
|
71 | 71 | |
|
72 | 72 | |
|
73 | 73 | def __init__(self, dataOutObj=None): |
|
74 | 74 | """ |
|
75 | 75 | Inicializador de la clase VoltageReader para la lectura de datos de voltage. |
|
76 | 76 | |
|
77 | 77 | Input: |
|
78 | 78 | dataOutObj : Objeto de la clase Voltage. Este objeto sera utilizado para |
|
79 | 79 | almacenar un perfil de datos cada vez que se haga un requerimiento |
|
80 | 80 | (getData). El perfil sera obtenido a partir del buffer de datos, |
|
81 | 81 | si el buffer esta vacio se hara un nuevo proceso de lectura de un |
|
82 | 82 | bloque de datos. |
|
83 | 83 | Si este parametro no es pasado se creara uno internamente. |
|
84 | 84 | |
|
85 | 85 | Variables afectadas: |
|
86 | 86 | self.dataOutObj |
|
87 | 87 | |
|
88 | 88 | Return: |
|
89 | 89 | None |
|
90 | 90 | """ |
|
91 | 91 | |
|
92 | 92 | self.datablock = None |
|
93 | 93 | |
|
94 | 94 | self.utc = 0 |
|
95 | 95 | |
|
96 | 96 | self.ext = ".r" |
|
97 | 97 | |
|
98 | 98 | self.optchar = "D" |
|
99 | 99 | |
|
100 | 100 | self.m_BasicHeader = BasicHeader() |
|
101 | 101 | |
|
102 |
self. |
|
|
102 | self.systemHeaderObj = SystemHeader() | |
|
103 | 103 | |
|
104 |
self. |
|
|
104 | self.radarControllerHeaderObj = RadarControllerHeader() | |
|
105 | 105 | |
|
106 | 106 | self.m_ProcessingHeader = ProcessingHeader() |
|
107 | 107 | |
|
108 | 108 | self.online = 0 |
|
109 | 109 | |
|
110 | 110 | self.fp = None |
|
111 | 111 | |
|
112 | 112 | self.idFile = None |
|
113 | 113 | |
|
114 | 114 | self.startDateTime = None |
|
115 | 115 | |
|
116 | 116 | self.endDateTime = None |
|
117 | 117 | |
|
118 | 118 | self.dataType = None |
|
119 | 119 | |
|
120 | 120 | self.fileSizeByHeader = None |
|
121 | 121 | |
|
122 | 122 | self.filenameList = [] |
|
123 | 123 | |
|
124 | 124 | self.filename = None |
|
125 | 125 | |
|
126 | 126 | self.fileSize = None |
|
127 | 127 | |
|
128 | 128 | self.firstHeaderSize = 0 |
|
129 | 129 | |
|
130 | 130 | self.basicHeaderSize = 24 |
|
131 | 131 | |
|
132 | 132 | self.pathList = [] |
|
133 | 133 | |
|
134 | 134 | self.filenameList = [] |
|
135 | 135 | |
|
136 | 136 | self.lastUTTime = 0 |
|
137 | 137 | |
|
138 | 138 | self.maxTimeStep = 30 |
|
139 | 139 | |
|
140 | 140 | self.flagNoMoreFiles = 0 |
|
141 | 141 | |
|
142 | 142 | self.set = 0 |
|
143 | 143 | |
|
144 | 144 | self.path = None |
|
145 | 145 | |
|
146 | 146 | self.profileIndex = 9999 |
|
147 | 147 | |
|
148 |
self.delay = |
|
|
148 | self.delay = 60 #seconds | |
|
149 | 149 | |
|
150 | 150 | self.nTries = 3 #quantity tries |
|
151 | 151 | |
|
152 | 152 | self.nFiles = 3 #number of files for searching |
|
153 | 153 | |
|
154 | 154 | self.nReadBlocks = 0 |
|
155 | 155 | |
|
156 | 156 | self.flagIsNewFile = 1 |
|
157 | 157 | |
|
158 | 158 | self.ippSeconds = 0 |
|
159 | 159 | |
|
160 | 160 | self.flagResetProcessing = 0 |
|
161 | 161 | |
|
162 | 162 | self.flagIsNewBlock = 0 |
|
163 | 163 | |
|
164 | 164 | self.nTotalBlocks = 0 |
|
165 | 165 | |
|
166 | 166 | self.blocksize = 0 |
|
167 | 167 | |
|
168 | 168 | def createObjByDefault(self): |
|
169 | 169 | |
|
170 | 170 | dataObj = Voltage() |
|
171 | 171 | |
|
172 | 172 | return dataObj |
|
173 | 173 | |
|
174 | 174 | def __hasNotDataInBuffer(self): |
|
175 | 175 | if self.profileIndex >= self.m_ProcessingHeader.profilesPerBlock: |
|
176 | 176 | return 1 |
|
177 | 177 | return 0 |
|
178 | 178 | |
|
179 | 179 | |
|
180 | 180 | def getBlockDimension(self): |
|
181 | 181 | """ |
|
182 | 182 | Obtiene la cantidad de puntos a leer por cada bloque de datos |
|
183 | 183 | |
|
184 | 184 | Affected: |
|
185 | 185 | self.blocksize |
|
186 | 186 | |
|
187 | 187 | Return: |
|
188 | 188 | None |
|
189 | 189 | """ |
|
190 |
pts2read = self.m_ProcessingHeader.profilesPerBlock * self.m_ProcessingHeader.numHeights * self. |
|
|
190 | pts2read = self.m_ProcessingHeader.profilesPerBlock * self.m_ProcessingHeader.numHeights * self.systemHeaderObj.numChannels | |
|
191 | 191 | self.blocksize = pts2read |
|
192 | 192 | |
|
193 | 193 | |
|
194 | 194 | def readBlock(self): |
|
195 | 195 | """ |
|
196 | 196 | readBlock lee el bloque de datos desde la posicion actual del puntero del archivo |
|
197 | 197 | (self.fp) y actualiza todos los parametros relacionados al bloque de datos |
|
198 | 198 | (metadata + data). La data leida es almacenada en el buffer y el contador del buffer |
|
199 | 199 | es seteado a 0 |
|
200 | 200 | |
|
201 | 201 | Inputs: |
|
202 | 202 | None |
|
203 | 203 | |
|
204 | 204 | Return: |
|
205 | 205 | None |
|
206 | 206 | |
|
207 | 207 | Affected: |
|
208 | 208 | self.profileIndex |
|
209 | 209 | self.datablock |
|
210 | 210 | self.flagIsNewFile |
|
211 | 211 | self.flagIsNewBlock |
|
212 | 212 | self.nTotalBlocks |
|
213 | 213 | |
|
214 | 214 | Exceptions: |
|
215 | 215 | Si un bloque leido no es un bloque valido |
|
216 | 216 | """ |
|
217 | 217 | |
|
218 | 218 | junk = numpy.fromfile( self.fp, self.dataType, self.blocksize ) |
|
219 | 219 | |
|
220 | 220 | try: |
|
221 |
junk = junk.reshape( (self.m_ProcessingHeader.profilesPerBlock, self.m_ProcessingHeader.numHeights, self. |
|
|
221 | junk = junk.reshape( (self.m_ProcessingHeader.profilesPerBlock, self.m_ProcessingHeader.numHeights, self.systemHeaderObj.numChannels) ) | |
|
222 | 222 | except: |
|
223 | 223 | print "The read block (%3d) has not enough data" %self.nReadBlocks |
|
224 | 224 | return 0 |
|
225 | 225 | |
|
226 | 226 | junk = numpy.transpose(junk, (2,0,1)) |
|
227 | 227 | self.datablock = junk['real'] + junk['imag']*1j |
|
228 | 228 | |
|
229 | 229 | self.profileIndex = 0 |
|
230 | 230 | |
|
231 | 231 | self.flagIsNewFile = 0 |
|
232 | 232 | self.flagIsNewBlock = 1 |
|
233 | 233 | |
|
234 | 234 | self.nTotalBlocks += 1 |
|
235 | 235 | self.nReadBlocks += 1 |
|
236 | 236 | |
|
237 | 237 | return 1 |
|
238 | 238 | |
|
239 | 239 | |
|
240 | 240 | def getData(self): |
|
241 | 241 | """ |
|
242 | 242 | getData obtiene una unidad de datos del buffer de lectura y la copia a la clase "Voltage" |
|
243 | 243 | con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de |
|
244 | 244 | lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock" |
|
245 | 245 | |
|
246 | 246 | Ademas incrementa el contador del buffer en 1. |
|
247 | 247 | |
|
248 | 248 | Return: |
|
249 | 249 | data : retorna un perfil de voltages (alturas * canales) copiados desde el |
|
250 | 250 | buffer. Si no hay mas archivos a leer retorna None. |
|
251 | 251 | |
|
252 | 252 | Variables afectadas: |
|
253 | 253 | self.dataOutObj |
|
254 | 254 | self.profileIndex |
|
255 | 255 | |
|
256 | 256 | Affected: |
|
257 | 257 | self.dataOutObj |
|
258 | 258 | self.profileIndex |
|
259 | 259 | self.flagResetProcessing |
|
260 | 260 | self.flagIsNewBlock |
|
261 | 261 | """ |
|
262 | 262 | if self.flagNoMoreFiles: return 0 |
|
263 | 263 | |
|
264 | 264 | self.flagResetProcessing = 0 |
|
265 | 265 | self.flagIsNewBlock = 0 |
|
266 | 266 | |
|
267 | 267 | if self.__hasNotDataInBuffer(): |
|
268 | 268 | |
|
269 | 269 | if not( self.readNextBlock() ): |
|
270 | 270 | return 0 |
|
271 | 271 | |
|
272 | 272 | self.updateDataHeader() |
|
273 | 273 | |
|
274 | 274 | if self.flagNoMoreFiles == 1: |
|
275 | 275 | print 'Process finished' |
|
276 | 276 | return 0 |
|
277 | 277 | |
|
278 | 278 | #data es un numpy array de 3 dmensiones (perfiles, alturas y canales) |
|
279 | 279 | |
|
280 | 280 | if self.datablock == None: |
|
281 | 281 | self.dataOutObj.flagNoData = True |
|
282 | 282 | return 0 |
|
283 | 283 | |
|
284 | 284 | time = self.m_BasicHeader.utc + self.profileIndex * self.ippSeconds |
|
285 | 285 | self.dataOutObj.m_BasicHeader.utc = time |
|
286 | 286 | |
|
287 | 287 | self.dataOutObj.flagNoData = False |
|
288 | 288 | self.dataOutObj.flagResetProcessing = self.flagResetProcessing |
|
289 | 289 | |
|
290 | 290 | self.dataOutObj.data = self.datablock[:,self.profileIndex,:] |
|
291 | 291 | |
|
292 | 292 | self.profileIndex += 1 |
|
293 | 293 | |
|
294 | 294 | #call setData - to Data Object |
|
295 | 295 | |
|
296 | 296 | return 1 #self.dataOutObj.data |
|
297 | 297 | |
|
298 | 298 | |
|
299 | 299 | class VoltageWriter(JRODataWriter): |
|
300 | 300 | """ |
|
301 | 301 | Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura |
|
302 | 302 | de los datos siempre se realiza por bloques. |
|
303 | 303 | """ |
|
304 | 304 | __configHeaderFile = 'wrSetHeadet.txt' |
|
305 | 305 | |
|
306 | 306 | dataOutObj = None |
|
307 | 307 | |
|
308 | 308 | ext = ".r" |
|
309 | 309 | |
|
310 | 310 | optchar = "D" |
|
311 | 311 | |
|
312 | 312 | datablock = None |
|
313 | 313 | |
|
314 | 314 | profileIndex = 0 |
|
315 | 315 | |
|
316 | 316 | shapeBuffer = None |
|
317 | 317 | |
|
318 | 318 | |
|
319 | 319 | def __init__(self, dataOutObj=None): |
|
320 | 320 | """ |
|
321 | 321 | Inicializador de la clase VoltageWriter para la escritura de datos de espectros. |
|
322 | 322 | |
|
323 | 323 | Affected: |
|
324 | 324 | self.dataOutObj |
|
325 | 325 | |
|
326 | 326 | Return: None |
|
327 | 327 | """ |
|
328 | 328 | if dataOutObj == None: |
|
329 | 329 | dataOutObj = Voltage() |
|
330 | 330 | |
|
331 | 331 | if not( isinstance(dataOutObj, Voltage) ): |
|
332 | 332 | raise ValueError, "in VoltageReader, dataOutObj must be an Spectra class object" |
|
333 | 333 | |
|
334 | 334 | self.dataOutObj = dataOutObj |
|
335 | 335 | |
|
336 | 336 | |
|
337 | 337 | def hasAllDataInBuffer(self): |
|
338 | 338 | if self.profileIndex >= self.m_ProcessingHeader.profilesPerBlock: |
|
339 | 339 | return 1 |
|
340 | 340 | return 0 |
|
341 | 341 | |
|
342 | 342 | |
|
343 | 343 | def setBlockDimension(self): |
|
344 | 344 | """ |
|
345 | 345 | Obtiene las formas dimensionales del los subbloques de datos que componen un bloque |
|
346 | 346 | |
|
347 | 347 | Affected: |
|
348 | 348 | self.shape_spc_Buffer |
|
349 | 349 | self.shape_cspc_Buffer |
|
350 | 350 | self.shape_dc_Buffer |
|
351 | 351 | |
|
352 | 352 | Return: None |
|
353 | 353 | """ |
|
354 | 354 | self.shapeBuffer = (self.m_ProcessingHeader.profilesPerBlock, |
|
355 | 355 | self.m_ProcessingHeader.numHeights, |
|
356 |
self. |
|
|
356 | self.systemHeaderObj.numChannels ) | |
|
357 | 357 | |
|
358 |
self.datablock = numpy.zeros((self. |
|
|
358 | self.datablock = numpy.zeros((self.systemHeaderObj.numChannels, | |
|
359 | 359 | self.m_ProcessingHeader.profilesPerBlock, |
|
360 | 360 | self.m_ProcessingHeader.numHeights), |
|
361 | 361 | dtype=numpy.dtype('complex')) |
|
362 | 362 | |
|
363 | 363 | |
|
364 | 364 | def writeBlock(self): |
|
365 | 365 | """ |
|
366 | 366 | Escribe el buffer en el file designado |
|
367 | 367 | |
|
368 | 368 | Affected: |
|
369 | 369 | self.profileIndex |
|
370 | 370 | self.flagIsNewFile |
|
371 | 371 | self.flagIsNewBlock |
|
372 | 372 | self.nTotalBlocks |
|
373 | 373 | self.nWriteBlocks |
|
374 | 374 | |
|
375 | 375 | Return: None |
|
376 | 376 | """ |
|
377 | 377 | data = numpy.zeros( self.shapeBuffer, self.dataType ) |
|
378 | 378 | |
|
379 | 379 | junk = numpy.transpose(self.datablock, (1,2,0)) |
|
380 | 380 | |
|
381 | 381 | data['real'] = junk.real |
|
382 | 382 | data['imag'] = junk.imag |
|
383 | 383 | |
|
384 | 384 | data = data.reshape( (-1) ) |
|
385 | 385 | |
|
386 | 386 | data.tofile( self.fp ) |
|
387 | 387 | |
|
388 | 388 | self.datablock.fill(0) |
|
389 | 389 | self.profileIndex = 0 |
|
390 | 390 | self.flagIsNewFile = 0 |
|
391 | 391 | self.flagIsNewBlock = 1 |
|
392 | 392 | self.nTotalBlocks += 1 |
|
393 | 393 | self.nWriteBlocks += 1 |
|
394 | 394 | |
|
395 | 395 | |
|
396 | 396 | def putData(self): |
|
397 | 397 | """ |
|
398 | 398 | Setea un bloque de datos y luego los escribe en un file |
|
399 | 399 | |
|
400 | 400 | Affected: |
|
401 | 401 | self.flagIsNewBlock |
|
402 | 402 | self.profileIndex |
|
403 | 403 | |
|
404 | 404 | Return: |
|
405 | 405 | 0 : Si no hay data o no hay mas files que puedan escribirse |
|
406 | 406 | 1 : Si se escribio la data de un bloque en un file |
|
407 | 407 | """ |
|
408 | 408 | self.flagIsNewBlock = 0 |
|
409 | 409 | |
|
410 | 410 | if self.dataOutObj.flagNoData: |
|
411 | 411 | return 0 |
|
412 | 412 | |
|
413 | 413 | if self.dataOutObj.flagResetProcessing: |
|
414 | 414 | |
|
415 | 415 | self.datablock.fill(0) |
|
416 | 416 | self.profileIndex = 0 |
|
417 | 417 | self.setNextFile() |
|
418 | 418 | |
|
419 | 419 | self.datablock[:,self.profileIndex,:] = self.dataOutObj.data |
|
420 | 420 | |
|
421 | 421 | self.profileIndex += 1 |
|
422 | 422 | |
|
423 | 423 | if self.hasAllDataInBuffer(): |
|
424 | 424 | #if self.flagIsNewFile: |
|
425 | 425 | self.getDataHeader() |
|
426 | 426 | self.writeNextBlock() |
|
427 | 427 | |
|
428 | 428 | if self.flagNoMoreFiles: |
|
429 | 429 | #print 'Process finished' |
|
430 | 430 | return 0 |
|
431 | 431 | |
|
432 | 432 | return 1 |
|
433 | 433 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now