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