@@ -1,1669 +1,1673 | |||
|
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 HeaderIO import * |
|
19 | 19 | from DataIO import DataReader |
|
20 | 20 | from DataIO import DataWriter |
|
21 | 21 | |
|
22 | 22 | from Model.Spectra import Spectra |
|
23 | 23 | |
|
24 | 24 | |
|
25 | 25 | def getlastFileFromPath( pathList, ext ): |
|
26 | 26 | """ |
|
27 | 27 | Depura el pathList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext" |
|
28 | 28 | al final de la depuracion devuelve el ultimo file de la lista que quedo. |
|
29 | 29 | |
|
30 | 30 | Input: |
|
31 | 31 | pathList : lista conteniendo todos los filename completos que componen una determinada carpeta |
|
32 | 32 | ext : extension de los files contenidos en una carpeta |
|
33 | 33 | |
|
34 | 34 | Return: |
|
35 | 35 | El ultimo file de una determinada carpeta |
|
36 | 36 | """ |
|
37 | 37 | |
|
38 | 38 | filesList = [] |
|
39 | 39 | filename = None |
|
40 | 40 | |
|
41 | 41 | # 0 1234 567 89A BCDE |
|
42 | 42 | # P YYYY DDD SSS .ext |
|
43 | 43 | |
|
44 | 44 | for filename in pathList: |
|
45 | 45 | year = filename[1:5] |
|
46 | 46 | doy = filename[5:8] |
|
47 | 47 | leng = len( ext ) |
|
48 | 48 | |
|
49 | 49 | if ( filename[-leng:].upper() != ext.upper() ) : continue |
|
50 | 50 | if not( isNumber( year ) ) : continue |
|
51 | 51 | if not( isNumber( doy ) ) : continue |
|
52 | 52 | |
|
53 | 53 | filesList.append(filename) |
|
54 | 54 | |
|
55 | 55 | if len( filesList ) > 0: |
|
56 | #filesList.sort() | |
|
57 | 56 | filesList = sorted( filesList, key=str.lower ) |
|
58 | 57 | filename = filesList[-1] |
|
59 | 58 | |
|
60 | 59 | return filename |
|
61 | 60 | |
|
62 | 61 | |
|
63 | 62 | def checkForRealPath( path, year, doy, set, ext ): |
|
64 | 63 | """ |
|
65 | 64 | Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path, |
|
66 | 65 | Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar |
|
67 | 66 | el path exacto de un determinado file. |
|
68 | 67 | |
|
69 | 68 | Example : |
|
70 | 69 | nombre correcto del file es ../RAWDATA/D2009307/P2009307367 |
|
71 | 70 | |
|
72 | 71 | Entonces la funcion prueba con las siguientes combinaciones |
|
73 | 72 | ../RAWDATA/d2009307/p2009307367 |
|
74 | 73 | ../RAWDATA/d2009307/P2009307367 |
|
75 | 74 | ../RAWDATA/D2009307/p2009307367 |
|
76 | 75 | ../RAWDATA/D2009307/P2009307367 |
|
77 | 76 | siendo para este caso, la ultima combinacion de letras, identica al file buscado |
|
78 | 77 | |
|
79 | 78 | Return: |
|
80 | 79 | Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file |
|
81 | 80 | caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas |
|
82 | 81 | para el filename |
|
83 | 82 | """ |
|
84 | 83 | filepath = None |
|
85 | 84 | find_flag = False |
|
86 | 85 | filename = None |
|
87 | 86 | |
|
88 | 87 | for dir in "dD": #barrido por las dos combinaciones posibles de "D" |
|
89 | 88 | for fil in "pP": #barrido por las dos combinaciones posibles de "D" |
|
90 | 89 | doypath = "%s%04d%03d" % ( dir, year, doy ) #formo el nombre del directorio xYYYYDDD (x=d o x=D) |
|
91 | 90 | filename = "%s%04d%03d%03d%s" % ( fil, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext (p=d o p=D) |
|
92 | 91 | filepath = os.path.join( path, doypath, filename ) #formo el path completo |
|
93 | 92 | if os.path.exists( filepath ): #verifico que exista |
|
94 | 93 | find_flag = True |
|
95 | 94 | break |
|
96 | 95 | if find_flag: |
|
97 | 96 | break |
|
98 | 97 | |
|
99 | 98 | if not(find_flag): |
|
100 | 99 | return None, filename |
|
101 | 100 | |
|
102 | 101 | return filepath, filename |
|
103 | 102 | |
|
104 | 103 | def isNumber( str ): |
|
105 | 104 | """ |
|
106 | 105 | Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. |
|
107 | 106 | |
|
108 | 107 | Excepciones: |
|
109 | 108 | Si un determinado string no puede ser convertido a numero |
|
110 | 109 | Input: |
|
111 | 110 | str, string al cual se le analiza para determinar si convertible a un numero o no |
|
112 | 111 | |
|
113 | 112 | Return: |
|
114 | 113 | True : si el string es uno numerico |
|
115 | 114 | False : no es un string numerico |
|
116 | 115 | """ |
|
117 | 116 | try: |
|
118 | 117 | float( str ) |
|
119 | 118 | return True |
|
120 | 119 | except: |
|
121 | 120 | return False |
|
122 | 121 | |
|
123 | 122 | |
|
124 | 123 | |
|
125 | 124 | def isThisFileinRange( filename, startUTSeconds, endUTSeconds ): |
|
126 | 125 | """ |
|
127 | 126 | Esta funcion determina si un archivo de datos en formato Jicamarca(.r) se encuentra |
|
128 | 127 | o no dentro del rango de fecha especificado. |
|
129 | 128 | |
|
130 | 129 | Inputs: |
|
131 | 130 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
132 | 131 | |
|
133 | 132 | startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en |
|
134 | 133 | segundos contados desde 01/01/1970. |
|
135 | 134 | |
|
136 | 135 | endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en |
|
137 | 136 | segundos contados desde 01/01/1970. |
|
138 | 137 | |
|
139 | 138 | Return: |
|
140 | 139 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
141 | 140 | fecha especificado, de lo contrario retorna False. |
|
142 | 141 | |
|
143 | 142 | Excepciones: |
|
144 | 143 | Si el archivo no existe o no puede ser abierto |
|
145 | 144 | Si la cabecera no puede ser leida. |
|
146 | 145 | """ |
|
147 | 146 | m_BasicHeader = BasicHeader() |
|
148 | 147 | |
|
149 | 148 | try: |
|
150 | 149 | fp = open( filename,'rb' ) #lectura binaria |
|
151 | 150 | except: |
|
152 | 151 | raise IOError, "The file %s can't be opened" %(filename) |
|
153 | 152 | |
|
154 | 153 | if not(m_BasicHeader.read(fp)): |
|
155 | 154 | raise IOError, "The file %s has not a valid header" %(filename) |
|
156 | 155 | |
|
157 | 156 | fp.close() |
|
158 | 157 | |
|
159 | 158 | if not ((startUTSeconds <= m_BasicHeader.utc) and (endUTSeconds >= m_BasicHeader.utc)): |
|
160 | 159 | return 0 |
|
161 | 160 | |
|
162 | 161 | return 1 |
|
163 | 162 | |
|
164 | 163 | |
|
165 | 164 | class SpectraReader( DataReader ): |
|
166 | 165 | """ |
|
167 | 166 | Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura |
|
168 | 167 | de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones) |
|
169 | 168 | son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel. |
|
170 | 169 | |
|
171 | 170 | Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader, |
|
172 | 171 | RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la |
|
173 | 172 | cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de |
|
174 | 173 | datos desde el "buffer" cada vez que se ejecute el metodo "getData". |
|
175 | 174 | |
|
176 | 175 | Example: |
|
177 | 176 | dpath = "/home/myuser/data" |
|
178 | 177 | |
|
179 | 178 | startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0) |
|
180 | 179 | |
|
181 | 180 | endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0) |
|
182 | 181 | |
|
183 | 182 | readerObj = SpectraReader() |
|
184 | 183 | |
|
185 | 184 | readerObj.setup(dpath, startTime, endTime) |
|
186 | 185 | |
|
187 | 186 | while(True): |
|
188 | 187 | |
|
189 | 188 | readerObj.getData() |
|
190 | 189 | |
|
191 | 190 | print readerObj.m_Spectra.data |
|
192 | 191 | |
|
193 | 192 | if readerObj.noMoreFiles: |
|
194 | 193 | break |
|
195 | 194 | |
|
196 | 195 | """ |
|
197 | 196 | |
|
198 | 197 | #speed of light |
|
199 | 198 | __c = 3E8 |
|
200 | 199 | |
|
201 | 200 | |
|
202 | 201 | def __init__( self, m_Spectra = None ): |
|
203 | 202 | """ |
|
204 | 203 | Inicializador de la clase SpectraReader para la lectura de datos de espectros. |
|
205 | 204 | |
|
206 | 205 | Inputs: |
|
207 | 206 | m_Spectra : Objeto de la clase Spectra. Este objeto sera utilizado para |
|
208 | 207 | almacenar un perfil de datos cada vez que se haga un requerimiento |
|
209 | 208 | (getData). El perfil sera obtenido a partir del buffer de datos, |
|
210 | 209 | si el buffer esta vacio se hara un nuevo proceso de lectura de un |
|
211 | 210 | bloque de datos. |
|
212 | 211 | Si este parametro no es pasado se creara uno internamente. |
|
213 | 212 | |
|
214 | 213 | Affected: |
|
215 | 214 | self.m_Spectra |
|
216 | 215 | self.m_BasicHeader |
|
217 | 216 | self.m_SystemHeader |
|
218 | 217 | self.m_RadarControllerHeader |
|
219 | 218 | self.m_ProcessingHeader |
|
220 | 219 | |
|
221 | 220 | Return : None |
|
222 | 221 | """ |
|
223 | 222 | if m_Spectra == None: |
|
224 | 223 | m_Spectra = Spectra() |
|
225 | 224 | |
|
226 | 225 | if not( isinstance(m_Spectra, Spectra) ): |
|
227 | 226 | raise ValueError, "in SpectraReader, m_Spectra must be an Spectra class object" |
|
228 | 227 | |
|
229 | 228 | self.m_Spectra = m_Spectra |
|
230 | 229 | |
|
231 | 230 | self.m_BasicHeader = BasicHeader() |
|
232 | 231 | |
|
233 | 232 | self.m_SystemHeader = SystemHeader() |
|
234 | 233 | |
|
235 | 234 | self.m_RadarControllerHeader = RadarControllerHeader() |
|
236 | 235 | |
|
237 | 236 | self.m_ProcessingHeader = ProcessingHeader() |
|
238 | 237 | |
|
239 | 238 | self.__fp = None |
|
240 | 239 | |
|
241 | 240 | self.__idFile = None |
|
242 | 241 | |
|
243 | 242 | self.__startDateTime = None |
|
244 | 243 | |
|
245 | 244 | self.__endDateTime = None |
|
246 | 245 | |
|
247 | 246 | self.__dataType = None |
|
248 | 247 | |
|
249 | 248 | self.__fileSizeByHeader = 0 |
|
250 | 249 | |
|
251 | 250 | self.__pathList = [] |
|
252 | 251 | |
|
253 | 252 | self.filenameList = [] |
|
254 | 253 | |
|
255 | 254 | self.__lastUTTime = 0 |
|
256 | 255 | |
|
257 | 256 | self.__maxTimeStep = 30 |
|
258 | 257 | |
|
259 | 258 | self.__flagIsNewFile = 0 |
|
260 | 259 | |
|
261 | 260 | self.flagResetProcessing = 0 |
|
262 | 261 | |
|
263 | 262 | self.flagIsNewBlock = 0 |
|
264 | 263 | |
|
265 | 264 | self.noMoreFiles = 0 |
|
266 | 265 | |
|
267 | 266 | self.nReadBlocks = 0 |
|
268 | 267 | |
|
269 | 268 | self.online = 0 |
|
270 | 269 | |
|
271 | 270 | self.firstHeaderSize = 0 |
|
272 | 271 | |
|
273 | 272 | self.basicHeaderSize = 24 |
|
274 | 273 | |
|
275 | 274 | self.filename = None |
|
276 | 275 | |
|
277 | 276 | self.fileSize = None |
|
278 | 277 | |
|
279 | 278 | self.__data_spc = None |
|
280 | 279 | self.__data_cspc = None |
|
281 | 280 | self.__data_dc = None |
|
282 | 281 | |
|
283 | 282 | self.nChannels = 0 |
|
284 | 283 | self.nPairs = 0 |
|
285 | 284 | |
|
286 | 285 | self.__pts2read_SelfSpectra = 0 |
|
287 | 286 | self.__pts2read_CrossSpectra = 0 |
|
288 | 287 | self.__pts2read_DCchannels = 0 |
|
289 | 288 | self.__blocksize = 0 |
|
290 | 289 | |
|
291 | 290 | self.__datablockIndex = 0 |
|
292 | 291 | |
|
293 | 292 | self.__ippSeconds = 0 |
|
294 | 293 | |
|
295 | 294 | self.nSelfChannels = 0 |
|
296 | 295 | |
|
297 | 296 | self.nCrossPairs = 0 |
|
298 | 297 | |
|
298 | self.datablock_id = 9999 | |
|
299 | ||
|
299 | 300 | self.__delay = 7 #seconds |
|
300 | 301 | self.__nTries = 3 #quantity tries |
|
301 | 302 | self.__nFiles = 3 #number of files for searching |
|
302 | 303 | self.__year = 0 |
|
303 | 304 | self.__doy = 0 |
|
304 | 305 | self.__set = 0 |
|
305 | 306 | self.__ext = None |
|
306 | 307 | self.__path = None |
|
307 | self.datablock_id = 9999 | |
|
308 | 308 | |
|
309 | 309 | |
|
310 | 310 | def __rdSystemHeader( self, fp=None ): |
|
311 | 311 | """ |
|
312 | 312 | Lectura del System Header |
|
313 | 313 | |
|
314 | 314 | Inputs: |
|
315 | 315 | fp : file pointer |
|
316 | 316 | |
|
317 | 317 | Affected: |
|
318 | 318 | self.m_SystemHeader |
|
319 | 319 | |
|
320 | 320 | Return: None |
|
321 | 321 | """ |
|
322 | 322 | if fp == None: |
|
323 | 323 | fp = self.__fp |
|
324 | 324 | |
|
325 | 325 | self.m_SystemHeader.read( fp ) |
|
326 | 326 | |
|
327 | 327 | |
|
328 | 328 | |
|
329 | 329 | def __rdRadarControllerHeader( self, fp=None ): |
|
330 | 330 | """ |
|
331 | 331 | Lectura del Radar Controller Header |
|
332 | 332 | |
|
333 | 333 | Inputs: |
|
334 | 334 | fp : file pointer |
|
335 | 335 | |
|
336 | 336 | Affected: |
|
337 | 337 | self.m_RadarControllerHeader |
|
338 | 338 | |
|
339 | 339 | Return: None |
|
340 | 340 | """ |
|
341 | 341 | if fp == None: |
|
342 | 342 | fp = self.__fp |
|
343 | 343 | |
|
344 | 344 | self.m_RadarControllerHeader.read(fp) |
|
345 | 345 | |
|
346 | 346 | |
|
347 | 347 | def __rdProcessingHeader( self,fp=None ): |
|
348 | 348 | """ |
|
349 | 349 | Lectura del Processing Header |
|
350 | 350 | |
|
351 | 351 | Inputs: |
|
352 | 352 | fp : file pointer |
|
353 | 353 | |
|
354 | 354 | Affected: |
|
355 | 355 | self.m_ProcessingHeader |
|
356 | 356 | |
|
357 | 357 | Return: None |
|
358 | 358 | """ |
|
359 | 359 | if fp == None: |
|
360 | 360 | fp = self.__fp |
|
361 | 361 | |
|
362 | 362 | self.m_ProcessingHeader.read(fp) |
|
363 | 363 | |
|
364 | 364 | |
|
365 | 365 | def __rdBasicHeader( self, fp=None ): |
|
366 | 366 | """ |
|
367 | 367 | Lectura del Basic Header |
|
368 | 368 | |
|
369 | 369 | Inputs: |
|
370 | 370 | fp : file pointer |
|
371 | 371 | |
|
372 | 372 | Affected: |
|
373 | 373 | self.m_BasicHeader |
|
374 | 374 | |
|
375 | 375 | Return: None |
|
376 | 376 | """ |
|
377 | 377 | if fp == None: |
|
378 | 378 | fp = self.__fp |
|
379 | 379 | |
|
380 | 380 | self.m_BasicHeader.read(fp) |
|
381 | 381 | |
|
382 | 382 | |
|
383 | 383 | def __readFirstHeader( self ): |
|
384 | 384 | """ |
|
385 | 385 | Lectura del First Header, es decir el Basic Header y el Long Header |
|
386 | 386 | |
|
387 | 387 | Affected: |
|
388 | 388 | self.m_BasicHeader |
|
389 | 389 | self.m_SystemHeader |
|
390 | 390 | self.m_RadarControllerHeader |
|
391 | 391 | self.m_ProcessingHeader |
|
392 | 392 | self.firstHeaderSize |
|
393 | 393 | self.__heights |
|
394 | 394 | self.__dataType |
|
395 | 395 | self.__fileSizeByHeader |
|
396 | 396 | self.__ippSeconds |
|
397 | 397 | self.nChannels |
|
398 | 398 | self.nPairs |
|
399 | 399 | self.__pts2read_SelfSpectra |
|
400 | 400 | self.__pts2read_CrossSpectra |
|
401 | 401 | |
|
402 | 402 | Return: None |
|
403 | 403 | """ |
|
404 | 404 | self.__rdBasicHeader() |
|
405 | 405 | self.__rdSystemHeader() |
|
406 | 406 | self.__rdRadarControllerHeader() |
|
407 | 407 | self.__rdProcessingHeader() |
|
408 | 408 | self.firstHeaderSize = self.m_BasicHeader.size |
|
409 | 409 | |
|
410 | 410 | data_type = int( numpy.log2((self.m_ProcessingHeader.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR) ) |
|
411 | 411 | if data_type == 0: |
|
412 | 412 | tmp = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
413 | 413 | |
|
414 | 414 | elif data_type == 1: |
|
415 | 415 | tmp = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
416 | 416 | |
|
417 | 417 | elif data_type == 2: |
|
418 | 418 | tmp = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
419 | 419 | |
|
420 | 420 | elif data_type == 3: |
|
421 | 421 | tmp = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
422 | 422 | |
|
423 | 423 | elif data_type == 4: |
|
424 | 424 | tmp = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
425 | 425 | |
|
426 | 426 | elif data_type == 5: |
|
427 | 427 | tmp = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
428 | 428 | |
|
429 | 429 | else: |
|
430 | 430 | raise ValueError, 'Data type was not defined' |
|
431 | 431 | |
|
432 | 432 | xi = self.m_ProcessingHeader.firstHeight |
|
433 | 433 | step = self.m_ProcessingHeader.deltaHeight |
|
434 | 434 | xf = xi + self.m_ProcessingHeader.numHeights*step |
|
435 | 435 | |
|
436 | 436 | self.__heights = numpy.arange(xi, xf, step) |
|
437 | 437 | |
|
438 | 438 | self.__dataType = tmp |
|
439 | 439 | self.__fileSizeByHeader = self.m_ProcessingHeader.dataBlocksPerFile * self.m_ProcessingHeader.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.m_ProcessingHeader.dataBlocksPerFile - 1) |
|
440 | 440 | self.__ippSeconds = 2 * 1000 * self.m_RadarControllerHeader.ipp / self.__c |
|
441 | 441 | |
|
442 | 442 | self.nChannels = 0 |
|
443 | 443 | self.nPairs = 0 |
|
444 | 444 | |
|
445 | 445 | for i in range( 0, self.m_ProcessingHeader.totalSpectra*2, 2 ): |
|
446 | 446 | if self.m_ProcessingHeader.spectraComb[i] == self.m_ProcessingHeader.spectraComb[i+1]: |
|
447 | 447 | self.nChannels = self.nChannels + 1 |
|
448 | 448 | else: |
|
449 | 449 | self.nPairs = self.nPairs + 1 |
|
450 | 450 | |
|
451 | 451 | pts2read = self.m_ProcessingHeader.profilesPerBlock * self.m_ProcessingHeader.numHeights |
|
452 | 452 | self.__pts2read_SelfSpectra = int( pts2read * self.nChannels ) |
|
453 | 453 | self.__pts2read_CrossSpectra = int( pts2read * self.nPairs ) |
|
454 | 454 | self.__pts2read_DCchannels = int( self.m_ProcessingHeader.numHeights * self.m_SystemHeader.numChannels ) |
|
455 | 455 | |
|
456 | 456 | self.__blocksize = self.__pts2read_SelfSpectra + self.__pts2read_CrossSpectra + self.__pts2read_DCchannels |
|
457 | 457 | |
|
458 | print "SIZEEEE ",self.__blocksize, self.m_ProcessingHeader.blockSize | |
|
459 | ||
|
458 | 460 | self.m_Spectra.nChannels = self.nChannels |
|
459 | 461 | self.m_Spectra.nPairs = self.nPairs |
|
460 | 462 | |
|
461 | 463 | |
|
462 | 464 | def __setNextFileOnline( self ): |
|
463 | 465 | """ |
|
464 | 466 | Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si |
|
465 | 467 | no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files |
|
466 | 468 | siguientes. |
|
467 | 469 | |
|
468 | 470 | Affected: |
|
469 | 471 | self.__flagIsNewFile |
|
470 | 472 | self.filename |
|
471 | 473 | self.fileSize |
|
472 | 474 | self.__fp |
|
473 | 475 | self.__set |
|
474 | 476 | self.noMoreFiles |
|
475 | 477 | |
|
476 | 478 | Return: |
|
477 | 479 | 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado |
|
478 | 480 | 1 : si el file fue abierto con exito y esta listo a ser leido |
|
479 | 481 | |
|
480 | 482 | Excepciones: |
|
481 | 483 | Si un determinado file no puede ser abierto |
|
482 | 484 | """ |
|
483 | 485 | countFiles = 0 |
|
484 | 486 | countTries = 0 |
|
485 | 487 | |
|
486 | 488 | fileStatus = 0 |
|
487 | 489 | notFirstTime_flag = False |
|
488 | 490 | bChangeDir = False |
|
489 | 491 | |
|
490 | 492 | fileSize = 0 |
|
491 | 493 | fp = None |
|
492 | 494 | |
|
493 | 495 | self.__flagIsNewFile = 0 |
|
494 | 496 | |
|
495 | 497 | while(True): #este loop permite llevar la cuenta de intentos, de files y carpetas, |
|
496 | 498 | #si no encuentra alguno sale del bucle |
|
497 | 499 | |
|
498 | 500 | countFiles += 1 |
|
499 | 501 | |
|
500 | 502 | if countFiles > (self.__nFiles + 1): |
|
501 | 503 | break |
|
502 | 504 | |
|
503 | 505 | self.__set += 1 |
|
504 | 506 | |
|
505 | 507 | if countFiles > self.__nFiles: #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta |
|
506 | 508 | self.__set = 0 |
|
507 | 509 | self.__doy += 1 |
|
508 | 510 | bChangeDir = True |
|
509 | 511 | |
|
510 | 512 | file = None |
|
511 | 513 | filename = None |
|
512 | 514 | |
|
513 | 515 | countTries = 0 |
|
514 | 516 | |
|
515 | 517 | #espero hasta encontrar el 1er file disponible |
|
516 | 518 | while( True ): |
|
517 | 519 | |
|
518 | 520 | countTries += 1 |
|
519 | 521 | if( countTries >= self.__nTries ): #checkeo que no haya ido mas alla de la cantidad de intentos |
|
520 | 522 | break |
|
521 | 523 | |
|
522 | 524 | file, filename = checkForRealPath( self.__path, self.__year, self.__doy, self.__set, self.__ext ) |
|
523 | 525 | if file != None: |
|
524 | 526 | break |
|
525 | 527 | |
|
526 | 528 | if notFirstTime_flag: #este flag me sirve solo para esperar por el 1er file, en lo siguientes no espera solo checkea si existe o no |
|
527 | 529 | countTries = self.__nTries |
|
528 | 530 | print "\tsearching next \"%s\" file ..." % filename |
|
529 | 531 | break |
|
530 | 532 | |
|
531 | 533 | print "\twaiting new \"%s\" file ..." % filename |
|
532 | 534 | time.sleep( self.__delay ) |
|
533 | 535 | |
|
534 | 536 | if countTries >= self.__nTries: #se realizaron n intentos y no hubo un file nuevo |
|
535 | 537 | notFirstTime_flag = True |
|
536 | 538 | continue #vuelvo al inico del while principal |
|
537 | 539 | |
|
538 | 540 | countTries = 0 |
|
539 | 541 | |
|
540 | 542 | #una vez que se obtuvo el 1er file valido se procede a checkear su contenido, y se espera una cierta cantidad |
|
541 | 543 | #de tiempo por una cierta cantidad de veces hasta que el contenido del file sea un contenido valido |
|
542 | 544 | while( True ): |
|
543 | 545 | countTries += 1 |
|
544 | 546 | if countTries > self.__nTries: |
|
545 | 547 | break |
|
546 | 548 | |
|
547 | 549 | try: |
|
548 | 550 | fp = open(file) |
|
549 | 551 | except: |
|
550 | 552 | print "The file \"%s\" can't be opened" % file |
|
551 | 553 | break |
|
552 | 554 | |
|
553 | 555 | fileSize = os.path.getsize( file ) |
|
554 | 556 | currentSize = fileSize - fp.tell() |
|
555 | 557 | neededSize = self.m_ProcessingHeader.blockSize + self.firstHeaderSize |
|
556 | 558 | |
|
557 | 559 | if currentSize > neededSize: |
|
558 | 560 | fileStatus = 1 |
|
559 | 561 | break |
|
560 | 562 | |
|
561 | 563 | fp.close() |
|
562 | 564 | |
|
563 | 565 | if bChangeDir: #si al buscar un file cambie de directorio ya no espero y salgo del bucle while |
|
564 | 566 | print "\tsearching next \"%s\" file ..." % filename |
|
565 | 567 | break |
|
566 | 568 | |
|
567 | 569 | print "\twaiting for block of \"%s\" file ..." % filename |
|
568 | 570 | time.sleep( self.__delay ) |
|
569 | 571 | |
|
570 | 572 | if fileStatus == 1: |
|
571 | 573 | break |
|
572 | 574 | |
|
573 | 575 | print "Skipping the file \"%s\" due to this files is empty" % filename |
|
574 | 576 | countFiles = 0 |
|
575 | 577 | |
|
576 | 578 | |
|
577 | 579 | if fileStatus == 1: |
|
578 | 580 | self.fileSize = fileSize |
|
579 | 581 | self.filename = file#name |
|
580 | 582 | self.__flagIsNewFile = 1 |
|
581 | 583 | self.__fp = fp |
|
582 | 584 | self.noMoreFiles = 0 |
|
583 | 585 | print 'Setting the file: %s' % file #name |
|
584 | 586 | else: |
|
585 | 587 | self.fileSize = 0 |
|
586 | 588 | self.filename = None |
|
587 | 589 | self.__fp = None |
|
588 | 590 | self.noMoreFiles = 1 |
|
589 | 591 | print 'No more Files' |
|
590 | 592 | |
|
591 | 593 | return fileStatus |
|
592 | 594 | |
|
593 | 595 | |
|
594 | 596 | def __setNextFileOffline( self ): |
|
595 | 597 | """ |
|
596 | 598 | Busca el siguiente file dentro de un folder que tenga suficiente data para ser leida |
|
597 | 599 | |
|
598 | 600 | Affected: |
|
599 | 601 | self.__flagIsNewFile |
|
600 | 602 | self.__idFile |
|
601 | 603 | self.filename |
|
602 | 604 | self.fileSize |
|
603 | 605 | self.__fp |
|
604 | 606 | |
|
605 | 607 | Return: |
|
606 | 608 | 0 : si un determinado file no puede ser abierto |
|
607 | 609 | 1 : si el file fue abierto con exito |
|
608 | 610 | |
|
609 | 611 | Excepciones: |
|
610 | 612 | Si un determinado file no puede ser abierto |
|
611 | 613 | """ |
|
612 | 614 | idFile = self.__idFile |
|
613 | 615 | self.__flagIsNewFile = 0 |
|
614 | 616 | |
|
615 | 617 | while(True): |
|
616 | 618 | idFile += 1 |
|
617 | 619 | |
|
618 | 620 | if not( idFile < len(self.filenameList) ): |
|
619 | 621 | self.noMoreFiles = 1 |
|
620 | 622 | return 0 |
|
621 | 623 | |
|
622 | 624 | filename = self.filenameList[idFile] |
|
623 | 625 | fileSize = os.path.getsize(filename) |
|
624 | 626 | |
|
625 | 627 | try: |
|
626 | 628 | fp = open( filename, 'rb' ) |
|
627 | 629 | except: |
|
628 | 630 | raise IOError, "The file %s can't be opened" %filename |
|
629 | 631 | |
|
630 | 632 | currentSize = fileSize - fp.tell() |
|
631 | 633 | neededSize = self.m_ProcessingHeader.blockSize + self.firstHeaderSize |
|
632 | 634 | |
|
633 | 635 | if (currentSize < neededSize): |
|
634 | 636 | print "Skipping the file %s due to it hasn't enough data" %filename |
|
635 | 637 | fp.close() |
|
636 | 638 | continue |
|
637 | 639 | |
|
638 | 640 | break |
|
639 | 641 | self.__flagIsNewFile = 1 |
|
640 | 642 | self.__idFile = idFile |
|
641 | 643 | self.filename = filename |
|
642 | 644 | self.fileSize = fileSize |
|
643 | 645 | self.__fp = fp |
|
644 | 646 | |
|
645 | 647 | print 'Setting the file: %s'%self.filename |
|
646 | 648 | |
|
647 | 649 | return 1 |
|
648 | 650 | |
|
649 | 651 | |
|
650 | 652 | def __setNextFile( self ): |
|
651 | 653 | """ |
|
652 | 654 | Determina el siguiente file a leer y si hay uno disponible lee el First Header |
|
653 | 655 | |
|
654 | 656 | Affected: |
|
655 | 657 | self.m_BasicHeader |
|
656 | 658 | self.m_SystemHeader |
|
657 | 659 | self.m_RadarControllerHeader |
|
658 | 660 | self.m_ProcessingHeader |
|
659 | 661 | self.firstHeaderSize |
|
660 | 662 | |
|
661 | 663 | Return: |
|
662 | 664 | 0 : Si no hay files disponibles |
|
663 | 665 | 1 : Si hay mas files disponibles |
|
664 | 666 | """ |
|
665 | 667 | if self.__fp != None: |
|
666 | 668 | self.__fp.close() |
|
667 | 669 | |
|
668 | 670 | if self.online: |
|
669 | 671 | newFile = self.__setNextFileOnline() |
|
670 | 672 | else: |
|
671 | 673 | newFile = self.__setNextFileOffline() |
|
672 | 674 | |
|
673 | 675 | if not(newFile): |
|
674 | 676 | return 0 |
|
675 | 677 | |
|
676 | 678 | self.__readFirstHeader() |
|
677 | 679 | |
|
678 | 680 | return 1 |
|
679 | 681 | |
|
680 | 682 | |
|
681 | 683 | def __setNewBlock( self ): |
|
682 | 684 | """ |
|
683 | 685 | Lee el Basic Header y posiciona le file pointer en la posicion inicial del bloque a leer |
|
684 | 686 | |
|
685 | 687 | Affected: |
|
686 | 688 | self.m_BasicHeader |
|
687 | 689 | self.flagResetProcessing |
|
688 | 690 | self.ns |
|
689 | 691 | |
|
690 | 692 | Return: |
|
691 | 693 | 0 : Si el file no tiene un Basic Header que pueda ser leido |
|
692 | 694 | 1 : Si se pudo leer el Basic Header |
|
693 | 695 | """ |
|
694 | 696 | if self.__fp == None: |
|
695 | 697 | return 0 |
|
696 | 698 | |
|
697 | 699 | if self.__flagIsNewFile: |
|
698 | 700 | return 1 |
|
699 | 701 | |
|
700 | 702 | currentSize = self.fileSize - self.__fp.tell() |
|
701 | 703 | neededSize = self.m_ProcessingHeader.blockSize + self.basicHeaderSize |
|
702 | 704 | |
|
703 | 705 | #If there is enough data setting new data block |
|
704 | 706 | if ( currentSize >= neededSize ): |
|
705 | 707 | self.__rdBasicHeader() |
|
706 | 708 | return 1 |
|
707 | 709 | elif self.online: |
|
708 | 710 | nTries = 0 |
|
709 | 711 | while( nTries < self.__nTries ): |
|
710 | 712 | nTries += 1 |
|
711 | 713 | print "Waiting for the next block, try %03d ..." % nTries |
|
712 | 714 | time.sleep( self.__delay ) |
|
713 | 715 | |
|
714 | 716 | fileSize = os.path.getsize(self.filename) |
|
715 | 717 | currentSize = fileSize - self.__fp.tell() |
|
716 | 718 | neededSize = self.m_ProcessingHeader.blockSize + self.basicHeaderSize |
|
717 | 719 | |
|
718 | 720 | if ( currentSize >= neededSize ): |
|
719 | 721 | self.__rdBasicHeader() |
|
720 | 722 | return 1 |
|
721 | 723 | |
|
722 | 724 | #Setting new file |
|
723 | 725 | if not( self.__setNextFile() ): |
|
724 | 726 | return 0 |
|
725 | 727 | |
|
726 | 728 | deltaTime = self.m_BasicHeader.utc - self.__lastUTTime # check this |
|
727 | 729 | |
|
728 | 730 | self.flagResetProcessing = 0 |
|
729 | 731 | |
|
730 | 732 | if deltaTime > self.__maxTimeStep: |
|
731 | 733 | self.flagResetProcessing = 1 |
|
732 | self.ns = 0 | |
|
734 | self.nReadBlocks = 0 | |
|
733 | 735 | |
|
734 | 736 | return 1 |
|
735 | 737 | |
|
736 | 738 | |
|
737 | 739 | def __readBlock(self): |
|
738 | 740 | """ |
|
739 | 741 | Lee el bloque de datos desde la posicion actual del puntero del archivo |
|
740 | 742 | (self.__fp) y actualiza todos los parametros relacionados al bloque de datos |
|
741 | 743 | (metadata + data). La data leida es almacenada en el buffer y el contador del buffer |
|
742 | 744 | es seteado a 0 |
|
743 | 745 | |
|
744 | 746 | Return: None |
|
745 | 747 | |
|
746 | 748 | Variables afectadas: |
|
747 | 749 | self.__datablockIndex |
|
748 | 750 | self.__flagIsNewFile |
|
749 | 751 | self.flagIsNewBlock |
|
750 | 752 | self.nReadBlocks |
|
751 | 753 | self.__data_spc |
|
752 | 754 | self.__data_cspc |
|
753 | 755 | self.__data_dc |
|
754 | 756 | """ |
|
755 | 757 | self.datablock_id = 0 |
|
756 | 758 | self.__flagIsNewFile = 0 |
|
757 | 759 | self.flagIsNewBlock = 1 |
|
758 | 760 | |
|
759 | 761 | fpointer = self.__fp.tell() |
|
760 | 762 | |
|
761 | 763 | spc = numpy.fromfile( self.__fp, self.__dataType[0], self.__pts2read_SelfSpectra ) |
|
762 | 764 | cspc = numpy.fromfile( self.__fp, self.__dataType, self.__pts2read_CrossSpectra ) |
|
763 | 765 | dc = numpy.fromfile( self.__fp, self.__dataType, self.__pts2read_DCchannels ) #int(self.m_ProcessingHeader.numHeights*self.m_SystemHeader.numChannels) ) |
|
764 | 766 | |
|
765 | 767 | if self.online: |
|
766 | 768 | if (spc.size + cspc.size + dc.size) != self.__blocksize: |
|
767 | 769 | nTries = 0 |
|
768 | 770 | while( nTries < self.__nTries ): |
|
769 | 771 | nTries += 1 |
|
770 | 772 | print "Waiting for the next block, try %03d ..." % nTries |
|
771 | 773 | time.sleep( self.__delay ) |
|
772 | 774 | self.__fp.seek( fpointer ) |
|
773 | 775 | fpointer = self.__fp.tell() |
|
774 | 776 | spc = numpy.fromfile( self.__fp, self.__dataType[0], self.__pts2read_SelfSpectra ) |
|
775 | 777 | cspc = numpy.fromfile( self.__fp, self.__dataType, self.__pts2read_CrossSpectra ) |
|
776 | 778 | dc = numpy.fromfile( self.__fp, self.__dataType, self.__pts2read_DCchannels ) #int(self.m_ProcessingHeader.numHeights*self.m_SystemHeader.numChannels) ) |
|
777 | 779 | if (spc.size + cspc.size + dc.size) == self.__blocksize: |
|
778 | 780 | nTries = 0 |
|
779 | 781 | break |
|
780 | 782 | if nTries > 0: |
|
781 | 783 | return |
|
782 | 784 | |
|
783 | 785 | spc = spc.reshape( (self.nChannels, self.m_ProcessingHeader.numHeights, self.m_ProcessingHeader.profilesPerBlock) ) #transforma a un arreglo 3D |
|
784 | 786 | |
|
785 | 787 | cspc = cspc.reshape( (self.nPairs, self.m_ProcessingHeader.numHeights, self.m_ProcessingHeader.profilesPerBlock) ) #transforma a un arreglo 3D |
|
786 | 788 | dc = dc.reshape( (self.m_SystemHeader.numChannels, self.m_ProcessingHeader.numHeights) ) #transforma a un arreglo 2D |
|
787 | 789 | |
|
788 | 790 | if not( self.m_ProcessingHeader.shif_fft ): |
|
789 | 791 | spc = numpy.roll( spc, self.m_ProcessingHeader.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones |
|
790 | 792 | cspc = numpy.roll( cspc, self.m_ProcessingHeader.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones |
|
791 | 793 | |
|
792 | 794 | spc = numpy.transpose( spc, (0,2,1) ) |
|
793 | 795 | cspc = numpy.transpose( cspc, (0,2,1) ) |
|
794 | 796 | #dc = numpy.transpose(dc, (0,2,1)) |
|
795 | 797 | |
|
796 | 798 | self.__data_spc = spc |
|
797 | 799 | self.__data_cspc = cspc['real'] + cspc['imag']*1j |
|
798 | 800 | self.__data_dc = dc['real'] + dc['imag']*1j |
|
799 | 801 | |
|
800 | 802 | self.__flagIsNewFile = 0 |
|
801 | 803 | |
|
802 | 804 | self.flagIsNewBlock = 1 |
|
803 | 805 | |
|
804 | 806 | self.nReadBlocks += 1 |
|
805 | 807 | self.datablock_id = 0 |
|
806 | 808 | |
|
807 | 809 | |
|
808 | 810 | |
|
809 | 811 | def __hasNotDataInBuffer( self ): |
|
810 | 812 | #if self.datablock_id >= self.m_ProcessingHeader.profilesPerBlock: |
|
811 | 813 | return 1 |
|
812 | 814 | |
|
813 | 815 | |
|
814 | 816 | def __searchFilesOnLine( self, path, startDateTime=None, ext = ".pdata" ): |
|
815 | 817 | """ |
|
816 | 818 | Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y |
|
817 | 819 | devuelve el archivo encontrado ademas de otros datos. |
|
818 | 820 | |
|
819 | 821 | Input: |
|
820 | 822 | path : carpeta donde estan contenidos los files que contiene data |
|
821 | 823 | startDateTime : punto especifico en el tiempo del cual se requiere la data |
|
822 | 824 | ext : extension de los files |
|
823 | 825 | |
|
824 | 826 | Return: |
|
825 | 827 | year : el anho |
|
826 | 828 | doy : el numero de dia del anho |
|
827 | 829 | set : el set del archivo |
|
828 | 830 | filename : el ultimo file de una determinada carpeta |
|
829 | 831 | directory : eL directorio donde esta el file encontrado |
|
830 | 832 | """ |
|
831 | 833 | |
|
832 | 834 | print "Searching files ..." |
|
833 | 835 | |
|
834 | 836 | dirList = [] |
|
835 | 837 | directory = None |
|
836 | 838 | |
|
837 | 839 | if startDateTime == None: |
|
838 | 840 | for thisPath in os.listdir(path): |
|
839 | 841 | if os.path.isdir( os.path.join(path,thisPath) ): |
|
840 | 842 | dirList.append( thisPath ) |
|
841 | 843 | |
|
842 | 844 | dirList = sorted( dirList, key=str.lower ) #para que quede ordenado al margen de si el nombre esta en mayusculas o minusculas, utilizo la funcion sorted |
|
843 | 845 | if len(dirList) > 0 : |
|
844 | 846 | directory = dirList[-1] |
|
845 | 847 | else: |
|
846 | 848 | year = startDateTime.timetuple().tm_year |
|
847 | 849 | doy = startDateTime.timetuple().tm_yday |
|
848 | 850 | |
|
849 | 851 | doyPath = "D%04d%03d" % (year,doy) #caso del nombre en mayusculas |
|
850 | 852 | if os.path.isdir( os.path.join(path,doyPath) ): |
|
851 | 853 | directory = doyPath |
|
852 | 854 | |
|
853 | 855 | doyPath = doyPath.lower() #caso del nombre en minusculas |
|
854 | 856 | if os.path.isdir( os.path.join(path,doyPath) ): |
|
855 | 857 | directory = doyPath |
|
856 | 858 | |
|
857 | 859 | if directory == None: |
|
858 | 860 | return 0, 0, 0, None, None |
|
859 | 861 | |
|
860 | 862 | filename = getlastFileFromPath( os.listdir( os.path.join(path,directory) ), ext ) |
|
861 | 863 | |
|
862 | 864 | if filename == None: |
|
863 | 865 | return 0, 0, 0, None, None |
|
864 | 866 | |
|
865 | 867 | year = int( directory[-7:-3] ) |
|
866 | 868 | doy = int( directory[-3:] ) |
|
867 | 869 | ln = len( ext ) |
|
868 | 870 | set = int( filename[-ln-3:-ln] ) |
|
869 | 871 | |
|
870 | 872 | return year, doy, set, filename, directory |
|
871 | 873 | |
|
872 | 874 | |
|
873 | 875 | def __searchFilesOffLine( self, path, startDateTime, endDateTime, set=None, expLabel = "", ext = ".pdata" ): |
|
874 | 876 | """ |
|
875 | 877 | Realiza una busqueda de los archivos que coincidan con los parametros |
|
876 | 878 | especificados y se encuentren ubicados en el path indicado. Para realizar una busqueda |
|
877 | 879 | correcta la estructura de directorios debe ser la siguiente: |
|
878 | 880 | |
|
879 | 881 | ...path/D[yyyy][ddd]/expLabel/D[yyyy][ddd][sss].ext |
|
880 | 882 | |
|
881 | 883 | [yyyy]: anio |
|
882 | 884 | [ddd] : dia del anio |
|
883 | 885 | [sss] : set del archivo |
|
884 | 886 | |
|
885 | 887 | Inputs: |
|
886 | 888 | path : Directorio de datos donde se realizara la busqueda. Todos los |
|
887 | 889 | ficheros que concidan con el criterio de busqueda seran |
|
888 | 890 | almacenados en una lista y luego retornados. |
|
889 | 891 | startDateTime : Fecha inicial. Rechaza todos los archivos donde |
|
890 | 892 | file end time < startDateTime (objeto datetime.datetime) |
|
891 | 893 | |
|
892 | 894 | endDateTime : Fecha final. Rechaza todos los archivos donde |
|
893 | 895 | file start time > endDateTime (obejto datetime.datetime) |
|
894 | 896 | |
|
895 | 897 | set : Set del primer archivo a leer. Por defecto None |
|
896 | 898 | |
|
897 | 899 | expLabel : Nombre del subdirectorio de datos. Por defecto "" |
|
898 | 900 | |
|
899 | 901 | ext : Extension de los archivos a leer. Por defecto .r |
|
900 | 902 | |
|
901 | 903 | Return: |
|
902 | 904 | |
|
903 | 905 | (pathList, filenameList) |
|
904 | 906 | |
|
905 | 907 | pathList : Lista de directorios donde se encontraron archivos dentro |
|
906 | 908 | de los parametros especificados |
|
907 | 909 | filenameList : Lista de archivos (ruta completa) que coincidieron con los |
|
908 | 910 | parametros especificados. |
|
909 | 911 | |
|
910 | 912 | Variables afectadas: |
|
911 | 913 | |
|
912 | 914 | self.filenameList: Lista de archivos (ruta completa) que la clase utiliza |
|
913 | 915 | como fuente para leer los bloque de datos, si se termina |
|
914 | 916 | de leer todos los bloques de datos de un determinado |
|
915 | 917 | archivo se pasa al siguiente archivo de la lista. |
|
916 | 918 | """ |
|
917 | 919 | |
|
918 | 920 | print "Searching files ..." |
|
919 | 921 | |
|
920 | 922 | dirList = [] |
|
921 | 923 | for thisPath in os.listdir(path): |
|
922 | 924 | if os.path.isdir(os.path.join(path,thisPath)): |
|
923 | 925 | dirList.append(thisPath) |
|
924 | 926 | |
|
925 | 927 | pathList = [] |
|
926 | 928 | |
|
927 | 929 | thisDateTime = startDateTime |
|
928 | 930 | |
|
929 | 931 | while(thisDateTime <= endDateTime): |
|
930 | 932 | year = thisDateTime.timetuple().tm_year |
|
931 | 933 | doy = thisDateTime.timetuple().tm_yday |
|
932 | 934 | |
|
933 | 935 | match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy)) |
|
934 | 936 | if len(match) == 0: |
|
935 | 937 | thisDateTime += datetime.timedelta(1) |
|
936 | 938 | continue |
|
937 | 939 | |
|
938 | 940 | pathList.append(os.path.join(path,match[0],expLabel)) |
|
939 | 941 | thisDateTime += datetime.timedelta(1) |
|
940 | 942 | |
|
941 | 943 | startUtSeconds = time.mktime(startDateTime.timetuple()) |
|
942 | 944 | endUtSeconds = time.mktime(endDateTime.timetuple()) |
|
943 | 945 | |
|
944 | 946 | filenameList = [] |
|
945 | 947 | for thisPath in pathList: |
|
946 | 948 | fileList = glob.glob1(thisPath, "*%s" %ext) |
|
947 | 949 | fileList.sort() |
|
948 | 950 | for file in fileList: |
|
949 | 951 | filename = os.path.join(thisPath,file) |
|
950 | 952 | if isThisFileinRange(filename, startUtSeconds, endUtSeconds): |
|
951 | 953 | filenameList.append(filename) |
|
952 | 954 | |
|
953 | 955 | self.filenameList = filenameList |
|
954 | 956 | |
|
955 | 957 | return pathList, filenameList |
|
956 | 958 | |
|
957 | 959 | |
|
958 | 960 | def __initFilesOnline( self, path, dirfilename, filename ): |
|
959 | 961 | """ |
|
960 | 962 | Verifica que el primer file tenga una data valida, para ello leo el 1er bloque |
|
961 | 963 | del file, si no es un file valido espera una cierta cantidad de tiempo a que |
|
962 | 964 | lo sea, si transcurrido el tiempo no logra validar el file entonces el metodo |
|
963 | 965 | devuelve 0 caso contrario devuelve 1 |
|
964 | 966 | |
|
965 | 967 | Affected: |
|
966 | 968 | m_BasicHeader |
|
967 | 969 | |
|
968 | 970 | Return: |
|
969 | 971 | 0 : file no valido para ser leido |
|
970 | 972 | 1 : file valido para ser leido |
|
971 | 973 | """ |
|
972 | 974 | m_BasicHeader = BasicHeader() |
|
973 | 975 | |
|
974 | 976 | file = os.path.join( path, dirfilename, filename ) |
|
975 | 977 | |
|
976 | 978 | nTries = 0 |
|
977 | 979 | while(True): |
|
978 | 980 | |
|
979 | 981 | nTries += 1 |
|
980 | 982 | if nTries > self.__nTries: |
|
981 | 983 | break |
|
982 | 984 | |
|
983 | 985 | try: |
|
984 | 986 | fp = open( file,'rb' ) #lectura binaria |
|
985 | 987 | except: |
|
986 | 988 | raise IOError, "The file %s can't be opened" %(file) |
|
987 | 989 | |
|
988 | 990 | try: |
|
989 | 991 | m_BasicHeader.read(fp) |
|
990 | 992 | except: |
|
991 | 993 | print "The file %s is empty" % filename |
|
992 | 994 | |
|
993 | 995 | fp.close() |
|
994 | 996 | |
|
995 | 997 | if m_BasicHeader.size > 24: |
|
996 | 998 | break |
|
997 | 999 | |
|
998 | 1000 | print 'waiting for new block: try %02d' % ( nTries ) |
|
999 | 1001 | time.sleep( self.__delay) |
|
1000 | 1002 | |
|
1001 | 1003 | if m_BasicHeader.size <= 24: |
|
1002 | 1004 | return 0 |
|
1003 | 1005 | |
|
1004 | 1006 | return 1 |
|
1005 | 1007 | |
|
1006 | 1008 | |
|
1007 | 1009 | def setup( self, path, startDateTime=None, endDateTime=None, set=None, expLabel = "", ext = ".pdata", online = 0 ): |
|
1008 | 1010 | """ |
|
1009 | 1011 | setup configura los parametros de lectura de la clase SpectraReader. |
|
1010 | 1012 | |
|
1011 | 1013 | Si el modo de lectura es offline, primero se realiza una busqueda de todos los archivos |
|
1012 | 1014 | que coincidan con los parametros especificados; esta lista de archivos son almacenados en |
|
1013 | 1015 | self.filenameList. |
|
1014 | 1016 | |
|
1015 | 1017 | Input: |
|
1016 | 1018 | path : Directorios donde se ubican los datos a leer. Dentro de este |
|
1017 | 1019 | directorio deberia de estar subdirectorios de la forma: |
|
1018 | 1020 | |
|
1019 | 1021 | path/D[yyyy][ddd]/expLabel/P[yyyy][ddd][sss][ext] |
|
1020 | 1022 | |
|
1021 | 1023 | startDateTime : Fecha inicial. Rechaza todos los archivos donde |
|
1022 | 1024 | file end time < startDatetime (objeto datetime.datetime) |
|
1023 | 1025 | |
|
1024 | 1026 | endDateTime : Fecha final. Si no es None, rechaza todos los archivos donde |
|
1025 | 1027 | file end time < startDatetime (objeto datetime.datetime) |
|
1026 | 1028 | |
|
1027 | 1029 | set : Set del primer archivo a leer. Por defecto None |
|
1028 | 1030 | |
|
1029 | 1031 | expLabel : Nombre del subdirectorio de datos. Por defecto "" |
|
1030 | 1032 | |
|
1031 | 1033 | ext : Extension de los archivos a leer. Por defecto .pdata |
|
1032 | 1034 | |
|
1033 | 1035 | online : Si es == a 0 entonces busca files que cumplan con las condiciones dadas |
|
1034 | 1036 | |
|
1035 | 1037 | Return: |
|
1036 | 1038 | 0 : Si no encuentra files que cumplan con las condiciones dadas |
|
1037 | 1039 | 1 : Si encuentra files que cumplan con las condiciones dadas |
|
1038 | 1040 | |
|
1039 | 1041 | Affected: |
|
1040 | 1042 | self.startUTCSeconds |
|
1041 | 1043 | self.endUTCSeconds |
|
1042 | 1044 | self.startYear |
|
1043 | 1045 | self.endYear |
|
1044 | 1046 | self.startDoy |
|
1045 | 1047 | self.endDoy |
|
1046 | 1048 | self.__pathList |
|
1047 | 1049 | self.filenameList |
|
1048 | 1050 | self.online |
|
1049 | 1051 | """ |
|
1050 | 1052 | |
|
1051 | 1053 | if online: |
|
1052 | 1054 | nTries = 0 |
|
1053 | 1055 | while( nTries < self.__nTries ): |
|
1054 | 1056 | nTries += 1 |
|
1055 | 1057 | subfolder = "D%04d%03d" % ( startDateTime.timetuple().tm_year,startDateTime.timetuple().tm_yday ) |
|
1056 | file = os.path.join( path, subfolder ) | |
|
1057 | print "Searching first file in \"%s\", try %03d ..." % ( file, nTries ) | |
|
1058 | 1058 | year, doy, set, filename, dirfilename = self.__searchFilesOnLine( path, startDateTime, ext ) |
|
1059 | 1059 | if filename == None: |
|
1060 | file = os.path.join( path, subfolder ) | |
|
1061 | print "Searching first file in \"%s\", try %03d ..." % ( file, nTries ) | |
|
1060 | 1062 | time.sleep( self.__delay ) |
|
1061 | 1063 | else: |
|
1062 | 1064 | break |
|
1063 | 1065 | |
|
1064 | 1066 | if filename == None: |
|
1065 | 1067 | print "No files On Line" |
|
1066 | 1068 | return 0 |
|
1067 | 1069 | |
|
1068 | 1070 | if self.__initFilesOnline( path, dirfilename, filename ) == 0: |
|
1069 | 1071 | print "The file %s hasn't enough data" |
|
1070 | 1072 | return 0 |
|
1071 | 1073 | |
|
1072 | 1074 | self.__year = year |
|
1073 | 1075 | self.__doy = doy |
|
1074 | 1076 | self.__set = set - 1 |
|
1075 | 1077 | self.__path = path |
|
1076 | 1078 | |
|
1077 | 1079 | else: |
|
1078 | 1080 | pathList, filenameList = self.__searchFilesOffLine( path, startDateTime, endDateTime, set, expLabel, ext ) |
|
1079 | 1081 | self.__idFile = -1 |
|
1080 | 1082 | self.__pathList = pathList |
|
1081 | 1083 | self.filenameList = filenameList |
|
1082 | 1084 | |
|
1083 | 1085 | self.online = online |
|
1084 | 1086 | self.__ext = ext |
|
1085 | 1087 | |
|
1086 | 1088 | if not(self.__setNextFile()): |
|
1087 | 1089 | if (startDateTime != None) and (endDateTime != None): |
|
1088 | 1090 | print "No files in range: %s - %s" %(startDateTime.ctime(), endDateTime.ctime()) |
|
1089 | 1091 | elif startDateTime != None: |
|
1090 | 1092 | print "No files in : %s" % startDateTime.ctime() |
|
1091 | 1093 | else: |
|
1092 | 1094 | print "No files" |
|
1093 | 1095 | return 0 |
|
1094 | 1096 | |
|
1095 | 1097 | if startDateTime != None: |
|
1096 | 1098 | self.startUTCSeconds = time.mktime(startDateTime.timetuple()) |
|
1097 | 1099 | self.startYear = startDateTime.timetuple().tm_year |
|
1098 | 1100 | self.startDoy = startDateTime.timetuple().tm_yday |
|
1099 | 1101 | |
|
1100 | 1102 | if endDateTime != None: |
|
1101 | 1103 | self.endUTCSeconds = time.mktime(endDateTime.timetuple()) |
|
1102 | 1104 | self.endYear = endDateTime.timetuple().tm_year |
|
1103 | 1105 | self.endDoy = endDateTime.timetuple().tm_yday |
|
1104 | 1106 | #call fillHeaderValues() - to Data Object |
|
1105 | 1107 | |
|
1106 | 1108 | self.m_Spectra.m_BasicHeader = self.m_BasicHeader.copy() |
|
1107 | 1109 | self.m_Spectra.m_ProcessingHeader = self.m_ProcessingHeader.copy() |
|
1108 | 1110 | self.m_Spectra.m_RadarControllerHeader = self.m_RadarControllerHeader.copy() |
|
1109 | 1111 | self.m_Spectra.m_SystemHeader = self.m_SystemHeader.copy() |
|
1110 | 1112 | self.m_Spectra.dataType = self.__dataType |
|
1111 | 1113 | |
|
1112 | 1114 | return 1 |
|
1113 | 1115 | |
|
1114 | 1116 | |
|
1115 | 1117 | def readNextBlock(self): |
|
1116 | 1118 | """ |
|
1117 | 1119 | Establece un nuevo bloque de datos a leer y los lee, si es que no existiese |
|
1118 | 1120 | mas bloques disponibles en el archivo actual salta al siguiente. |
|
1119 | 1121 | |
|
1120 | 1122 | Affected: |
|
1121 | 1123 | self.__lastUTTime |
|
1122 | 1124 | |
|
1123 | 1125 | Return: None |
|
1124 | 1126 | """ |
|
1125 | 1127 | |
|
1126 | 1128 | if not( self.__setNewBlock() ): |
|
1127 | 1129 | return 0 |
|
1128 | 1130 | |
|
1129 | 1131 | self.__readBlock() |
|
1130 | 1132 | |
|
1131 | 1133 | self.__lastUTTime = self.m_BasicHeader.utc |
|
1132 | 1134 | |
|
1133 | 1135 | return 1 |
|
1134 | 1136 | |
|
1135 | 1137 | |
|
1136 | 1138 | def getData(self): |
|
1137 | 1139 | """ |
|
1138 | 1140 | Copia el buffer de lectura a la clase "Spectra", |
|
1139 | 1141 | con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de |
|
1140 | 1142 | lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock" |
|
1141 | 1143 | |
|
1142 | 1144 | Return: |
|
1143 | 1145 | 0 : Si no hay mas archivos disponibles |
|
1144 | 1146 | 1 : Si hizo una buena copia del buffer |
|
1145 | 1147 | |
|
1146 |
|
|
|
1148 | Affected: | |
|
1147 | 1149 | self.m_Spectra |
|
1148 | 1150 | self.__datablockIndex |
|
1149 | 1151 | self.flagResetProcessing |
|
1150 | 1152 | self.flagIsNewBlock |
|
1151 | 1153 | """ |
|
1152 | 1154 | |
|
1153 | 1155 | self.flagResetProcessing = 0 |
|
1154 | 1156 | self.flagIsNewBlock = 0 |
|
1155 | 1157 | |
|
1156 | 1158 | if self.__hasNotDataInBuffer(): |
|
1157 | 1159 | |
|
1158 | 1160 | self.readNextBlock() |
|
1159 | 1161 | |
|
1160 | 1162 | self.m_Spectra.m_BasicHeader = self.m_BasicHeader.copy() |
|
1161 | 1163 | self.m_Spectra.m_ProcessingHeader = self.m_ProcessingHeader.copy() |
|
1162 | 1164 | self.m_Spectra.m_RadarControllerHeader = self.m_RadarControllerHeader.copy() |
|
1163 | 1165 | self.m_Spectra.m_SystemHeader = self.m_SystemHeader.copy() |
|
1164 | 1166 | self.m_Spectra.heights = self.__heights |
|
1165 | 1167 | self.m_Spectra.dataType = self.__dataType |
|
1166 | 1168 | |
|
1167 | 1169 | if self.noMoreFiles == 1: |
|
1168 | 1170 | print 'Process finished' |
|
1169 | 1171 | return 0 |
|
1170 | 1172 | |
|
1171 | 1173 | #data es un numpy array de 3 dmensiones (perfiles, alturas y canales) |
|
1172 | 1174 | |
|
1173 | 1175 | self.m_Spectra.flagNoData = False |
|
1174 | 1176 | self.m_Spectra.flagResetProcessing = self.flagResetProcessing |
|
1175 | 1177 | |
|
1176 | 1178 | self.m_Spectra.data_spc = self.__data_spc |
|
1177 | 1179 | self.m_Spectra.data_cspc = self.__data_cspc |
|
1178 | 1180 | self.m_Spectra.data_dc = self.__data_dc |
|
1179 | 1181 | |
|
1180 | 1182 | #call setData - to Data Object |
|
1181 | 1183 | #self.datablock_id += 1 |
|
1182 | 1184 | #self.idProfile += 1 |
|
1183 | 1185 | |
|
1184 | 1186 | return 1 |
|
1185 | 1187 | |
|
1186 | 1188 | |
|
1187 | 1189 | class SpectraWriter( DataWriter ): |
|
1188 | 1190 | """ |
|
1189 | 1191 | Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura |
|
1190 | 1192 | de los datos siempre se realiza por bloques. |
|
1191 | 1193 | """ |
|
1192 | 1194 | |
|
1193 | 1195 | def __init__( self, m_Spectra = None ): |
|
1194 | 1196 | """ |
|
1195 | 1197 | Inicializador de la clase SpectraWriter para la escritura de datos de espectros. |
|
1196 | 1198 | |
|
1197 | 1199 | Affected: |
|
1198 | 1200 | self.m_Spectra |
|
1199 | 1201 | self.m_BasicHeader |
|
1200 | 1202 | self.m_SystemHeader |
|
1201 | 1203 | self.m_RadarControllerHeader |
|
1202 | 1204 | self.m_ProcessingHeader |
|
1203 | 1205 | |
|
1204 | 1206 | Return: None |
|
1205 | 1207 | """ |
|
1206 | 1208 | |
|
1207 | 1209 | if m_Spectra == None: |
|
1208 | 1210 | m_Spectra = Spectra() |
|
1209 | 1211 | |
|
1210 | 1212 | self.m_Spectra = m_Spectra |
|
1211 | 1213 | |
|
1212 | 1214 | self.__path = None |
|
1213 | 1215 | |
|
1214 | 1216 | self.__fp = None |
|
1215 | 1217 | |
|
1216 | 1218 | self.__format = None |
|
1217 | 1219 | |
|
1218 | 1220 | self.__blocksCounter = 0 |
|
1219 | 1221 | |
|
1220 | 1222 | self.__setFile = None |
|
1221 | 1223 | |
|
1222 | 1224 | self.__flagIsNewFile = 1 |
|
1223 | 1225 | |
|
1224 | 1226 | self.__dataType = None |
|
1225 | 1227 | |
|
1226 | 1228 | self.__ext = None |
|
1227 | 1229 | |
|
1228 | 1230 | self.__shape_spc_Buffer = None |
|
1229 | 1231 | self.__shape_cspc_Buffer = None |
|
1230 | 1232 | self.__shape_dc_Buffer = None |
|
1231 | 1233 | |
|
1232 | 1234 | self.nWriteBlocks = 0 |
|
1233 | 1235 | |
|
1234 | 1236 | self.flagIsNewBlock = 0 |
|
1235 | 1237 | |
|
1236 | 1238 | self.noMoreFiles = 0 |
|
1237 | 1239 | |
|
1238 | 1240 | self.filename = None |
|
1239 | 1241 | |
|
1240 | 1242 | self.m_BasicHeader= BasicHeader() |
|
1241 | 1243 | |
|
1242 | 1244 | self.m_SystemHeader = SystemHeader() |
|
1243 | 1245 | |
|
1244 | 1246 | self.m_RadarControllerHeader = RadarControllerHeader() |
|
1245 | 1247 | |
|
1246 | 1248 | self.m_ProcessingHeader = ProcessingHeader() |
|
1247 | 1249 | |
|
1248 | 1250 | self.__data_spc = None |
|
1249 | 1251 | self.__data_cspc = None |
|
1250 | 1252 | self.__data_dc = None |
|
1251 | 1253 | |
|
1252 | 1254 | def __writeFirstHeader( self ): |
|
1253 | 1255 | """ |
|
1254 | 1256 | Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader) |
|
1255 | 1257 | |
|
1256 | 1258 | Affected: |
|
1257 | 1259 | __dataType |
|
1258 | 1260 | |
|
1259 | 1261 | Return: |
|
1260 | 1262 | None |
|
1261 | 1263 | """ |
|
1262 | 1264 | self.__writeBasicHeader() |
|
1263 | 1265 | self.__wrSystemHeader() |
|
1264 | 1266 | self.__wrRadarControllerHeader() |
|
1265 | 1267 | self.__wrProcessingHeader() |
|
1266 | 1268 | self.__dataType = self.m_Spectra.dataType |
|
1267 | 1269 | |
|
1268 | 1270 | def __writeBasicHeader( self, fp=None ): |
|
1269 | 1271 | """ |
|
1270 | 1272 | Escribe solo el Basic header en el file creado |
|
1271 | 1273 | |
|
1272 | 1274 | Return: |
|
1273 | 1275 | None |
|
1274 | 1276 | """ |
|
1275 | 1277 | if fp == None: |
|
1276 | 1278 | fp = self.__fp |
|
1277 | 1279 | |
|
1278 | 1280 | self.m_BasicHeader.write(fp) |
|
1279 | 1281 | |
|
1280 | 1282 | def __wrSystemHeader( self, fp=None ): |
|
1281 | 1283 | """ |
|
1282 | 1284 | Escribe solo el System header en el file creado |
|
1283 | 1285 | |
|
1284 | 1286 | Return: |
|
1285 | 1287 | None |
|
1286 | 1288 | """ |
|
1287 | 1289 | if fp == None: |
|
1288 | 1290 | fp = self.__fp |
|
1289 | 1291 | |
|
1290 | 1292 | self.m_SystemHeader.write(fp) |
|
1291 | 1293 | |
|
1292 | 1294 | def __wrRadarControllerHeader( self, fp=None ): |
|
1293 | 1295 | """ |
|
1294 | 1296 | Escribe solo el RadarController header en el file creado |
|
1295 | 1297 | |
|
1296 | 1298 | Return: |
|
1297 | 1299 | None |
|
1298 | 1300 | """ |
|
1299 | 1301 | if fp == None: |
|
1300 | 1302 | fp = self.__fp |
|
1301 | 1303 | |
|
1302 | 1304 | self.m_RadarControllerHeader.write(fp) |
|
1303 | 1305 | |
|
1304 | 1306 | def __wrProcessingHeader( self, fp=None ): |
|
1305 | 1307 | """ |
|
1306 | 1308 | Escribe solo el Processing header en el file creado |
|
1307 | 1309 | |
|
1308 | 1310 | Return: |
|
1309 | 1311 | None |
|
1310 | 1312 | """ |
|
1311 | 1313 | if fp == None: |
|
1312 | 1314 | fp = self.__fp |
|
1313 | 1315 | |
|
1314 | 1316 | self.m_ProcessingHeader.write(fp) |
|
1315 | 1317 | |
|
1316 | 1318 | def __setNextFile( self ): |
|
1317 | 1319 | """ |
|
1318 | 1320 | Determina el siguiente file que sera escrito |
|
1319 | 1321 | |
|
1320 | 1322 | Affected: |
|
1321 | 1323 | self.filename |
|
1322 | 1324 | self.__subfolder |
|
1323 | 1325 | self.__fp |
|
1324 | 1326 | self.__setFile |
|
1325 | 1327 | self.__flagIsNewFile |
|
1326 | 1328 | |
|
1327 | 1329 | Return: |
|
1328 | 1330 | 0 : Si el archivo no puede ser escrito |
|
1329 | 1331 | 1 : Si el archivo esta listo para ser escrito |
|
1330 | 1332 | """ |
|
1331 | 1333 | ext = self.__ext |
|
1332 | 1334 | path = self.__path |
|
1333 | 1335 | |
|
1334 | 1336 | if self.__fp != None: |
|
1335 | 1337 | self.__fp.close() |
|
1336 | 1338 | |
|
1337 | 1339 | if self.m_BasicHeader.size <= 24: return 0 #no existe la suficiente data para ser escrita |
|
1338 | 1340 | |
|
1339 | 1341 | timeTuple = time.localtime(self.m_Spectra.m_BasicHeader.utc) # utc from m_Spectra |
|
1340 | 1342 | subfolder = 'D%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) |
|
1341 | 1343 | |
|
1342 | 1344 | tmp = os.path.join( path, subfolder ) |
|
1343 | 1345 | if not( os.path.exists(tmp) ): |
|
1344 | 1346 | os.mkdir(tmp) |
|
1345 | 1347 | self.__setFile = -1 #inicializo mi contador de seteo |
|
1346 | 1348 | else: |
|
1347 | 1349 | filesList = os.listdir( tmp ) |
|
1348 | 1350 | if len( filesList ) > 0: |
|
1349 | 1351 | filesList = sorted( filesList, key=str.lower ) |
|
1350 | 1352 | filen = filesList[-1] |
|
1351 | 1353 | # el filename debera tener el siguiente formato |
|
1352 | 1354 | # 0 1234 567 89A BCDE (hex) |
|
1353 | 1355 | # P YYYY DDD SSS .ext |
|
1354 | 1356 | if isNumber( filen[8:11] ): |
|
1355 | 1357 | self.__setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file |
|
1356 | 1358 | else: |
|
1357 | 1359 | self.__setFile = -1 |
|
1358 | 1360 | else: |
|
1359 | 1361 | self.__setFile = -1 #inicializo mi contador de seteo |
|
1360 | 1362 | |
|
1361 | 1363 | setFile = self.__setFile |
|
1362 | 1364 | setFile += 1 |
|
1363 | 1365 | file = 'P%4.4d%3.3d%3.3d%s' % ( timeTuple.tm_year, timeTuple.tm_yday, setFile, ext ) |
|
1364 | 1366 | |
|
1365 | 1367 | filename = os.path.join( path, subfolder, file ) |
|
1366 | 1368 | |
|
1367 | 1369 | fp = open(filename,'wb') |
|
1368 | 1370 | |
|
1369 | 1371 | self.__blocksCounter = 0 |
|
1370 | 1372 | |
|
1371 | 1373 | #guardando atributos |
|
1372 | 1374 | self.filename = filename |
|
1373 | 1375 | self.__subfolder = subfolder |
|
1374 | 1376 | self.__fp = fp |
|
1375 | 1377 | self.__setFile = setFile |
|
1376 | 1378 | self.__flagIsNewFile = 1 |
|
1377 | 1379 | |
|
1378 | 1380 | print 'Writing the file: %s'%self.filename |
|
1379 | 1381 | |
|
1380 | 1382 | self.__writeFirstHeader() |
|
1381 | 1383 | |
|
1382 | 1384 | return 1 |
|
1383 | 1385 | |
|
1384 | 1386 | def __setNewBlock( self ): |
|
1385 | 1387 | """ |
|
1386 | 1388 | Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header |
|
1387 | 1389 | |
|
1388 | 1390 | Return: |
|
1389 | 1391 | 0 : si no pudo escribir nada |
|
1390 | 1392 | 1 : Si escribio el Basic el First Header |
|
1391 | 1393 | """ |
|
1392 | 1394 | if self.__fp == None: |
|
1393 | 1395 | self.__setNextFile() |
|
1394 | 1396 | |
|
1395 | 1397 | if self.__flagIsNewFile: |
|
1396 | 1398 | return 1 |
|
1397 | 1399 | |
|
1398 | 1400 | if self.__blocksCounter < self.m_ProcessingHeader.dataBlocksPerFile: |
|
1399 | 1401 | self.__writeBasicHeader() |
|
1400 | 1402 | return 1 |
|
1401 | 1403 | |
|
1402 | 1404 | if not(self.__setNextFile()): |
|
1403 | 1405 | return 0 |
|
1404 | 1406 | |
|
1405 | 1407 | return 1 |
|
1406 | 1408 | |
|
1407 | 1409 | def __writeBlock( self ): |
|
1408 | 1410 | """ |
|
1409 | 1411 | Escribe el buffer en el file designado |
|
1410 | 1412 | |
|
1411 | 1413 | Affected: |
|
1412 | 1414 | self.__data_spc |
|
1413 | 1415 | self.__data_cspc |
|
1414 | 1416 | self.__data_dc |
|
1415 | 1417 | self.__flagIsNewFile |
|
1416 | 1418 | self.flagIsNewBlock |
|
1417 | 1419 | self.nWriteBlocks |
|
1418 | 1420 | self.__blocksCounter |
|
1419 | 1421 | |
|
1420 | 1422 | Return: None |
|
1421 | 1423 | """ |
|
1422 | 1424 | spc = numpy.transpose( self.__data_spc, (0,2,1) ) |
|
1423 | 1425 | if not( self.m_ProcessingHeader.shif_fft ): |
|
1424 | 1426 | spc = numpy.roll( spc, self.m_ProcessingHeader.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones |
|
1425 | 1427 | data = spc.reshape((-1)) |
|
1426 | 1428 | data.tofile(self.__fp) |
|
1427 | 1429 | |
|
1428 | 1430 | data = numpy.zeros( self.__shape_cspc_Buffer, self.__dataType ) |
|
1429 | 1431 | cspc = numpy.transpose( self.__data_cspc, (0,2,1) ) |
|
1430 | 1432 | if not( self.m_ProcessingHeader.shif_fft ): |
|
1431 | 1433 | cspc = numpy.roll( cspc, self.m_ProcessingHeader.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones |
|
1432 | 1434 | data['real'] = cspc.real |
|
1433 | 1435 | data['imag'] = cspc.imag |
|
1434 | 1436 | data = data.reshape((-1)) |
|
1435 | 1437 | data.tofile(self.__fp) |
|
1436 | 1438 | |
|
1437 | 1439 | data = numpy.zeros( self.__shape_dc_Buffer, self.__dataType ) |
|
1438 | 1440 | dc = self.__data_dc |
|
1439 | 1441 | data['real'] = dc.real |
|
1440 | 1442 | data['imag'] = dc.imag |
|
1441 | 1443 | data = data.reshape((-1)) |
|
1442 | 1444 | data.tofile(self.__fp) |
|
1443 | 1445 | |
|
1444 | 1446 | self.__data_spc.fill(0) |
|
1445 | 1447 | self.__data_cspc.fill(0) |
|
1446 | 1448 | self.__data_dc.fill(0) |
|
1447 | 1449 | |
|
1448 | 1450 | self.__flagIsNewFile = 0 |
|
1449 | 1451 | |
|
1450 | 1452 | self.flagIsNewBlock = 1 |
|
1451 | 1453 | |
|
1452 | 1454 | self.nWriteBlocks += 1 |
|
1453 | 1455 | |
|
1454 | 1456 | self.__blocksCounter += 1 |
|
1455 | 1457 | |
|
1456 | 1458 | |
|
1457 | 1459 | def writeNextBlock( self ): |
|
1458 | 1460 | """ |
|
1459 | 1461 | Selecciona el bloque siguiente de datos y los escribe en un file |
|
1460 | 1462 | |
|
1461 | 1463 | Return: |
|
1462 | 1464 | 0 : Si no hizo pudo escribir el bloque de datos |
|
1463 | 1465 | 1 : Si no pudo escribir el bloque de datos |
|
1464 | 1466 | """ |
|
1465 | 1467 | if not( self.__setNewBlock() ): |
|
1466 | 1468 | return 0 |
|
1467 | 1469 | |
|
1468 | 1470 | self.__writeBlock() |
|
1469 | 1471 | |
|
1470 | 1472 | return 1 |
|
1471 | 1473 | |
|
1472 | 1474 | |
|
1473 | 1475 | def __hasAllDataInBuffer( self ): |
|
1474 | 1476 | return 1 |
|
1475 | 1477 | |
|
1476 | 1478 | |
|
1477 | 1479 | def putData( self ): |
|
1478 | 1480 | """ |
|
1479 | 1481 | Setea un bloque de datos y luego los escribe en un file |
|
1480 | 1482 | |
|
1481 | 1483 | Affected: |
|
1482 | 1484 | self.__data_spc |
|
1483 | 1485 | self.__data_cspc |
|
1484 | 1486 | self.__data_dc |
|
1485 | 1487 | |
|
1486 | 1488 | Return: |
|
1487 | 1489 | 0 : Si no hay data o no hay mas files que puedan escribirse |
|
1488 | 1490 | 1 : Si se escribio la data de un bloque en un file |
|
1489 | 1491 | """ |
|
1490 | 1492 | self.flagIsNewBlock = 0 |
|
1491 | 1493 | |
|
1492 | 1494 | if self.m_Spectra.flagNoData: |
|
1493 | 1495 | return 0 |
|
1494 | 1496 | |
|
1495 | 1497 | if self.m_Spectra.flagResetProcessing: |
|
1496 | 1498 | self.__data_spc.fill(0) |
|
1497 | 1499 | self.__data_cspc.fill(0) |
|
1498 | 1500 | self.__data_dc.fill(0) |
|
1499 | 1501 | self.__setNextFile() |
|
1500 | 1502 | |
|
1501 | 1503 | self.__data_spc = self.m_Spectra.data_spc |
|
1502 | 1504 | self.__data_cspc = self.m_Spectra.data_cspc |
|
1503 | 1505 | self.__data_dc = self.m_Spectra.data_dc |
|
1504 | 1506 | |
|
1505 | 1507 | if True: |
|
1506 | 1508 | #time.sleep( 3 ) |
|
1507 | 1509 | self.__getHeader() |
|
1508 | 1510 | self.writeNextBlock() |
|
1509 | 1511 | |
|
1510 | 1512 | if self.noMoreFiles: |
|
1511 | 1513 | #print 'Process finished' |
|
1512 | 1514 | return 0 |
|
1513 | 1515 | |
|
1514 | 1516 | return 1 |
|
1515 | 1517 | |
|
1516 | 1518 | def __getHeader( self ): |
|
1517 | 1519 | """ |
|
1518 | 1520 | Obtiene una copia del First Header |
|
1519 | 1521 | |
|
1520 | 1522 | Affected: |
|
1521 | 1523 | self.m_BasicHeader |
|
1522 | 1524 | self.m_SystemHeader |
|
1523 | 1525 | self.m_RadarControllerHeader |
|
1524 | 1526 | self.m_ProcessingHeader |
|
1525 | 1527 | self.__dataType |
|
1526 | 1528 | |
|
1527 | 1529 | Return: |
|
1528 | 1530 | None |
|
1529 | 1531 | """ |
|
1530 | 1532 | self.m_BasicHeader = self.m_Spectra.m_BasicHeader.copy() |
|
1531 | 1533 | self.m_SystemHeader = self.m_Spectra.m_SystemHeader.copy() |
|
1532 | 1534 | self.m_RadarControllerHeader = self.m_Spectra.m_RadarControllerHeader.copy() |
|
1533 | 1535 | self.m_ProcessingHeader = self.m_Spectra.m_ProcessingHeader.copy() |
|
1534 | 1536 | self.__dataType = self.m_Spectra.dataType |
|
1535 | 1537 | |
|
1538 | ||
|
1536 | 1539 | def __setHeaderByFile(self): |
|
1537 | 1540 | |
|
1538 | 1541 | format = self.__format |
|
1539 | 1542 | header = ['Basic','System','RadarController','Processing'] |
|
1540 | 1543 | |
|
1541 | 1544 | fmtFromFile = None |
|
1542 | 1545 | headerFromFile = None |
|
1543 | 1546 | |
|
1544 | 1547 | |
|
1545 | 1548 | fileTable = self.__configHeaderFile |
|
1546 | 1549 | |
|
1547 | 1550 | if os.access(fileTable, os.R_OK): |
|
1548 | 1551 | import re, string |
|
1549 | 1552 | |
|
1550 | 1553 | f = open(fileTable,'r') |
|
1551 | 1554 | lines = f.read() |
|
1552 | 1555 | f.close() |
|
1553 | 1556 | |
|
1554 | 1557 | #Delete comments into expConfig |
|
1555 | 1558 | while 1: |
|
1556 | 1559 | |
|
1557 | 1560 | startComment = string.find(lines.lower(),'#') |
|
1558 | 1561 | if startComment == -1: |
|
1559 | 1562 | break |
|
1560 | 1563 | endComment = string.find(lines.lower(),'\n',startComment) |
|
1561 | 1564 | lines = string.replace(lines,lines[startComment:endComment+1],'', 1) |
|
1562 | 1565 | |
|
1563 | 1566 | while expFromFile == None: |
|
1564 | 1567 | |
|
1565 | 1568 | currFmt = string.find(lines.lower(),'format="%s"' %(expName)) |
|
1566 | 1569 | nextFmt = string.find(lines.lower(),'format',currFmt+10) |
|
1567 | 1570 | |
|
1568 | 1571 | if currFmt == -1: |
|
1569 | 1572 | break |
|
1570 | 1573 | if nextFmt == -1: |
|
1571 | 1574 | nextFmt = len(lines)-1 |
|
1572 | 1575 | |
|
1573 | 1576 | fmtTable = lines[currFmt:nextFmt] |
|
1574 | 1577 | lines = lines[nextFmt:] |
|
1575 | 1578 | |
|
1576 | 1579 | fmtRead = self.__getValueFromArg(fmtTable,'format') |
|
1577 | 1580 | if fmtRead != format: |
|
1578 | 1581 | continue |
|
1579 | 1582 | fmtFromFile = fmtRead |
|
1580 | 1583 | |
|
1581 | 1584 | lines2 = fmtTable |
|
1582 | 1585 | |
|
1583 | 1586 | while headerFromFile == None: |
|
1584 | 1587 | |
|
1585 | 1588 | currHeader = string.find(lines2.lower(),'header="%s"' %(header)) |
|
1586 | 1589 | nextHeader = string.find(lines2.lower(),'header',currHeader+10) |
|
1587 | 1590 | |
|
1588 | 1591 | if currHeader == -1: |
|
1589 | 1592 | break |
|
1590 | 1593 | if nextHeader == -1: |
|
1591 | 1594 | nextHeader = len(lines2)-1 |
|
1592 | 1595 | |
|
1593 | 1596 | headerTable = lines2[currHeader:nextHeader] |
|
1594 | 1597 | lines2 = lines2[nextHeader:] |
|
1595 | 1598 | |
|
1596 | 1599 | headerRead = self.__getValueFromArg(headerTable,'site') |
|
1597 | 1600 | if not(headerRead in header): |
|
1598 | 1601 | continue |
|
1599 | 1602 | headerFromFile = headerRead |
|
1600 | 1603 | |
|
1601 | 1604 | if headerRead == 'Basic': |
|
1602 | 1605 | self.m_BasicHeader.size = self.__getValueFromArg(headerTable,'size',lower=False) |
|
1603 | 1606 | self.m_BasicHeader.version = self.__getValueFromArg(headerTable,'version',lower=False) |
|
1604 | 1607 | self.m_BasicHeader.dataBlock = self.__getValueFromArg(headerTable,'dataBlock',lower=False) |
|
1605 | 1608 | self.m_BasicHeader.utc = self.__getValueFromArg(headerTable,'utc',lower=False) |
|
1606 | 1609 | self.m_BasicHeader.miliSecond = self.__getValueFromArg(headerTable,'miliSecond',lower=False) |
|
1607 | 1610 | self.m_BasicHeader.timeZone = self.__getValueFromArg(headerTable,'timeZone',lower=False) |
|
1608 | 1611 | self.m_BasicHeader.dstFlag = self.__getValueFromArg(headerTable,'dstFlag',lower=False) |
|
1609 | 1612 | self.m_BasicHeader.errorCount = self.__getValueFromArg(headerTable,'errorCount',lower=False) |
|
1610 | 1613 | |
|
1611 | 1614 | else: |
|
1612 | 1615 | print "file access denied:%s"%fileTable |
|
1613 | 1616 | sys.exit(0) |
|
1614 | 1617 | |
|
1618 | ||
|
1615 | 1619 | def setup( self, path, format='pdata' ): |
|
1616 | 1620 | """ |
|
1617 | 1621 | Setea el tipo de formato en la cual sera guardada la data y escribe el First Header |
|
1618 | 1622 | |
|
1619 | 1623 | Inputs: |
|
1620 | 1624 | path : el path destino en el cual se escribiran los files a crear |
|
1621 | 1625 | format : formato en el cual sera salvado un file |
|
1622 | 1626 | |
|
1623 | 1627 | Return: |
|
1624 | 1628 | 0 : Si no realizo un buen seteo |
|
1625 | 1629 | 1 : Si realizo un buen seteo |
|
1626 | 1630 | """ |
|
1627 | 1631 | if format == 'hdf5': |
|
1628 | 1632 | ext = '.hdf5' |
|
1629 | 1633 | format = 'hdf5' |
|
1630 | 1634 | print 'call hdf5 library' |
|
1631 | 1635 | return 0 |
|
1632 | 1636 | |
|
1633 | 1637 | if format == 'rawdata': |
|
1634 | 1638 | ext = '.r' |
|
1635 | 1639 | format = 'Jicamarca' |
|
1636 | 1640 | |
|
1637 | 1641 | if format == 'pdata': |
|
1638 | 1642 | ext = '.pdata' |
|
1639 | 1643 | format = 'pdata' |
|
1640 | 1644 | |
|
1641 | 1645 | #call to config_headers |
|
1642 | 1646 | #self.__setHeaderByFile() |
|
1643 | 1647 | |
|
1644 | 1648 | self.__path = path |
|
1645 | 1649 | self.__setFile = -1 |
|
1646 | 1650 | self.__ext = ext |
|
1647 | 1651 | self.__format = format |
|
1648 | 1652 | |
|
1649 | 1653 | self.__getHeader() |
|
1650 | 1654 | |
|
1651 | 1655 | self.__shape_spc_Buffer = ( self.m_Spectra.nChannels, |
|
1652 | 1656 | self.m_ProcessingHeader.numHeights, |
|
1653 | 1657 | self.m_ProcessingHeader.profilesPerBlock |
|
1654 | 1658 | ) |
|
1655 | 1659 | |
|
1656 | 1660 | self.__shape_cspc_Buffer = ( self.m_Spectra.nPairs, |
|
1657 | 1661 | self.m_ProcessingHeader.numHeights, |
|
1658 | 1662 | self.m_ProcessingHeader.profilesPerBlock |
|
1659 | 1663 | ) |
|
1660 | 1664 | |
|
1661 | 1665 | self.__shape_dc_Buffer = ( self.m_SystemHeader.numChannels, |
|
1662 | 1666 | self.m_ProcessingHeader.numHeights |
|
1663 | 1667 | ) |
|
1664 | 1668 | |
|
1665 | 1669 | if not( self.__setNextFile() ): |
|
1666 | 1670 | print "There isn't a next file" #"zzzzzzzzzzzz" |
|
1667 | 1671 | return 0 |
|
1668 | 1672 | |
|
1669 | 1673 | return 1 No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now