@@ -1,819 +1,868 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on Jul 3, 2014 |
|
3 | 3 | |
|
4 | 4 | @author: roj-com0419 |
|
5 | 5 | ''' |
|
6 | 6 | |
|
7 | 7 | import os,sys |
|
8 | 8 | import time,datetime |
|
9 | 9 | import h5py |
|
10 | 10 | import numpy |
|
11 | 11 | import fnmatch |
|
12 | 12 | import re |
|
13 | 13 | import stuffr |
|
14 | 14 | |
|
15 | 15 | from model.data.jroheaderIO import RadarControllerHeader, SystemHeader |
|
16 | 16 | from model.data.jrodata import Voltage |
|
17 | 17 | from model.proc.jroproc_base import ProcessingUnit, Operation |
|
18 | 18 | |
|
19 | try: | |
|
20 | from gevent import sleep | |
|
21 | except: | |
|
22 | from time import sleep | |
|
23 | 19 | |
|
24 | 20 | def isNumber(str): |
|
25 | 21 | """ |
|
26 | 22 | Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. |
|
27 | 23 | |
|
28 | 24 | Excepciones: |
|
29 | 25 | Si un determinado string no puede ser convertido a numero |
|
30 | 26 | Input: |
|
31 | 27 | str, string al cual se le analiza para determinar si convertible a un numero o no |
|
32 | 28 | |
|
33 | 29 | Return: |
|
34 | 30 | True : si el string es uno numerico |
|
35 | 31 | False : no es un string numerico |
|
36 | 32 | """ |
|
37 | 33 | try: |
|
38 | 34 | float( str ) |
|
39 | 35 | return True |
|
40 | 36 | except: |
|
41 | 37 | return False |
|
42 | 38 | |
|
43 | def getFileFromSet(path, ext, set): | |
|
39 | def getFileFromSet(path, ext, set=None): | |
|
44 | 40 | validFilelist = [] |
|
45 | 41 | fileList = os.listdir(path) |
|
46 | ||
|
42 | ||
|
43 | ||
|
44 | if len(fileList) < 1: | |
|
45 | return None | |
|
46 | ||
|
47 | 47 | # 0 1234 567 89A BCDE |
|
48 | 48 | # H YYYY DDD SSS .ext |
|
49 | 49 | |
|
50 | 50 | for thisFile in fileList: |
|
51 | 51 | try: |
|
52 | 52 | number= int(thisFile[6:16]) |
|
53 | 53 | |
|
54 | 54 | # year = int(thisFile[1:5]) |
|
55 | 55 | # doy = int(thisFile[5:8]) |
|
56 | 56 | except: |
|
57 | 57 | continue |
|
58 | 58 | |
|
59 | 59 | if (os.path.splitext(thisFile)[-1].lower() != ext.lower()): |
|
60 | 60 | continue |
|
61 | 61 | |
|
62 | 62 | validFilelist.append(thisFile) |
|
63 | myfile = fnmatch.filter(validFilelist,'*%10.10d*'%(set)) | |
|
63 | ||
|
64 | if len(validFilelist) < 1: | |
|
65 | return None | |
|
66 | ||
|
67 | validFilelist = sorted( validFilelist, key=str.lower ) | |
|
68 | ||
|
69 | if set == None: | |
|
70 | return validFilelist[-1] | |
|
71 | ||
|
72 | print "set =" ,set | |
|
73 | for thisFile in validFilelist: | |
|
74 | if set <= int(thisFile[6:16]): | |
|
75 | print thisFile,int(thisFile[6:16]) | |
|
76 | return thisFile | |
|
77 | ||
|
78 | return validFilelist[-1] | |
|
79 | ||
|
80 | myfile = fnmatch.filter(validFilelist,'*%10d*'%(set)) | |
|
64 | 81 | #myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set)) |
|
65 | 82 | |
|
66 | 83 | if len(myfile)!= 0: |
|
67 | 84 | return myfile[0] |
|
68 | 85 | else: |
|
69 | 86 | filename = '*%10.10d%s'%(set,ext.lower()) |
|
70 | 87 | print 'the filename %s does not exist'%filename |
|
71 | 88 | print '...going to the last file: ' |
|
72 | 89 | |
|
73 | 90 | if validFilelist: |
|
74 | 91 | validFilelist = sorted( validFilelist, key=str.lower ) |
|
75 | 92 | return validFilelist[-1] |
|
76 | 93 | |
|
77 | 94 | return None |
|
78 | 95 | |
|
79 | 96 | def getlastFileFromPath(path, ext): |
|
80 | 97 | """ |
|
81 | 98 | Depura el fileList dejando solo los que cumplan el formato de "res-xxxxxx.ext" |
|
82 | 99 | al final de la depuracion devuelve el ultimo file de la lista que quedo. |
|
83 | 100 | |
|
84 | 101 | Input: |
|
85 | 102 | fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta |
|
86 | 103 | ext : extension de los files contenidos en una carpeta |
|
87 | 104 | |
|
88 | 105 | Return: |
|
89 | 106 | El ultimo file de una determinada carpeta, no se considera el path. |
|
90 | 107 | """ |
|
91 | 108 | validFilelist = [] |
|
92 | 109 | fileList = os.listdir(path) |
|
93 | 110 | |
|
94 | 111 | # 0 1234 567 89A BCDE |
|
95 | 112 | # H YYYY DDD SSS .ext |
|
96 | 113 | |
|
97 | 114 | for thisFile in fileList: |
|
98 | 115 | |
|
99 | 116 | try: |
|
100 | 117 | number= int(thisFile[6:16]) |
|
101 | 118 | except: |
|
102 | 119 | print "There is a file or folder with different format" |
|
103 | 120 | if not isNumber(number): |
|
104 | 121 | continue |
|
105 | 122 | |
|
106 | 123 | # year = thisFile[1:5] |
|
107 | 124 | # if not isNumber(year): |
|
108 | 125 | # continue |
|
109 | 126 | |
|
110 | 127 | # doy = thisFile[5:8] |
|
111 | 128 | # if not isNumber(doy): |
|
112 | 129 | # continue |
|
113 | 130 | |
|
114 | 131 | number= int(number) |
|
115 | 132 | # year = int(year) |
|
116 | 133 | # doy = int(doy) |
|
117 | 134 | |
|
118 | 135 | if (os.path.splitext(thisFile)[-1].lower() != ext.lower()): |
|
119 | 136 | continue |
|
120 | 137 | |
|
121 | 138 | |
|
122 | 139 | validFilelist.append(thisFile) |
|
123 | 140 | |
|
124 | 141 | |
|
125 | 142 | if validFilelist: |
|
126 | 143 | validFilelist = sorted( validFilelist, key=str.lower ) |
|
127 | 144 | return validFilelist[-1] |
|
128 | 145 | |
|
129 | 146 | return None |
|
130 | 147 | |
|
131 | 148 | |
|
132 | 149 | |
|
133 | 150 | class HFReader(ProcessingUnit): |
|
134 | 151 | ''' |
|
135 | 152 | classdocs |
|
136 | 153 | ''' |
|
137 | 154 | path = None |
|
138 | 155 | startDate= None |
|
139 | 156 | endDate = None |
|
140 | 157 | startTime= None |
|
141 | 158 | endTime = None |
|
142 | 159 | walk = None |
|
143 | 160 | isConfig = False |
|
144 | 161 | dataOut=None |
|
145 | 162 | nTries = 3 |
|
146 | 163 | ext = ".hdf5" |
|
147 | 164 | |
|
148 | 165 | def __init__(self): |
|
149 | 166 | ''' |
|
150 | 167 | Constructor |
|
151 | 168 | ''' |
|
152 | 169 | ProcessingUnit.__init__(self) |
|
153 | 170 | |
|
154 | 171 | self.isConfig =False |
|
155 | 172 | |
|
156 | 173 | self.datablock = None |
|
157 | 174 | |
|
158 | 175 | self.filename_current=None |
|
159 | 176 | |
|
160 | 177 | self.utc = 0 |
|
161 | 178 | |
|
162 | 179 | self.ext='.hdf5' |
|
163 | 180 | |
|
164 | 181 | self.flagIsNewFile = 1 |
|
165 | 182 | |
|
166 | 183 | #------------------------------------------------- |
|
167 | 184 | self.fileIndex=None |
|
168 | 185 | |
|
169 | 186 | self.profileIndex_offset=None |
|
170 | 187 | |
|
171 | 188 | self.filenameList=[] |
|
172 | 189 | |
|
173 | 190 | self.hfFilePointer= None |
|
174 | 191 | |
|
175 | 192 | self.filename_online = None |
|
176 | 193 | |
|
177 | 194 | self.status=True |
|
178 | 195 | |
|
179 | 196 | self.flagNoMoreFiles= False |
|
180 | 197 | |
|
181 |
self.__waitForNewFile = |
|
|
198 | self.__waitForNewFile = 20 | |
|
182 | 199 | |
|
183 | 200 | |
|
184 | 201 | #-------------------------------------------------- |
|
185 | 202 | |
|
186 | 203 | self.dataOut = self.createObjByDefault() |
|
187 | 204 | |
|
188 | 205 | |
|
189 | 206 | def createObjByDefault(self): |
|
190 | 207 | |
|
191 | 208 | dataObj = Voltage() |
|
192 | 209 | |
|
193 | 210 | return dataObj |
|
194 | 211 | |
|
195 | 212 | def setObjProperties(self): |
|
196 | 213 | |
|
197 | 214 | pass |
|
198 | 215 | |
|
199 | 216 | def getBlockDimension(self): |
|
200 | 217 | """ |
|
201 | 218 | Obtiene la cantidad de puntos a leer por cada bloque de datos |
|
202 | 219 | |
|
203 | 220 | Affected: |
|
204 | 221 | self.blocksize |
|
205 | 222 | |
|
206 | 223 | Return: |
|
207 | 224 | None |
|
208 | 225 | """ |
|
209 | 226 | pts2read =self.nChannels*self.nHeights*self.nProfiles |
|
210 | 227 | self.blocksize = pts2read |
|
211 | 228 | |
|
212 | 229 | def __readHeader(self): |
|
213 | 230 | |
|
214 | 231 | self.nProfiles = 100 |
|
215 | 232 | self.nHeights = 1000 |
|
216 | 233 | self.nChannels = 2 |
|
217 | 234 | self.__firstHeigth=0 |
|
218 | 235 | self.__nSamples=1000 |
|
219 | 236 | self.__deltaHeigth=1.5 |
|
220 | 237 | self.__sample_rate=1e5 |
|
221 | 238 | #self.__frequency=2.72e6 |
|
222 | 239 | #self.__frequency=3.64e6 |
|
223 | 240 | self.__frequency=None |
|
224 | 241 | self.__online = False |
|
225 | 242 | self.filename_next_set=None |
|
226 | 243 | |
|
227 | 244 | #print "Frequency of Operation:", self.__frequency |
|
228 | 245 | |
|
229 | 246 | |
|
230 | 247 | def __setParameters(self,path='', startDate='',endDate='',startTime='', endTime='', walk=''): |
|
231 | 248 | self.path = path |
|
232 | 249 | self.startDate = startDate |
|
233 | 250 | self.endDate = endDate |
|
234 | 251 | self.startTime = startTime |
|
235 | 252 | self.endTime = endTime |
|
236 | 253 | self.walk = walk |
|
237 | 254 | |
|
238 | 255 | def __checkPath(self): |
|
239 | 256 | if os.path.exists(self.path): |
|
240 | 257 | self.status=1 |
|
241 | 258 | else: |
|
242 | 259 | self.status=0 |
|
243 | 260 | print 'Path %s does not exits'%self.path |
|
244 | 261 | return |
|
245 | 262 | return |
|
246 | 263 | |
|
247 | 264 | def __selDates(self, hf_dirname_format): |
|
248 | 265 | try: |
|
249 | 266 | dir_hf_filename= self.path+"/"+hf_dirname_format |
|
250 | 267 | fp= h5py.File(dir_hf_filename,'r') |
|
251 | 268 | hipoc=fp['t'].value |
|
252 | 269 | fp.close() |
|
253 | 270 | date_time=stuffr.unix2datestr(hipoc) |
|
254 | 271 | year =int(date_time[0:4]) |
|
255 | 272 | month=int(date_time[5:7]) |
|
256 | 273 | dom =int(date_time[8:10]) |
|
257 | 274 | thisDate= datetime.date(year,month,dom) |
|
258 | 275 | if (thisDate>=self.startDate and thisDate <= self.endDate): |
|
259 | 276 | return hf_dirname_format |
|
260 | 277 | except: |
|
261 | 278 | return None |
|
262 | 279 | |
|
263 | 280 | def __findDataForDates(self,online=False): |
|
264 | 281 | if not(self.status): |
|
265 | 282 | return None |
|
266 | 283 | |
|
267 | 284 | pat = '\d+.\d+' |
|
268 | 285 | dirnameList = [re.search(pat,x) for x in os.listdir(self.path)] |
|
269 | 286 | dirnameList = filter(lambda x:x!=None,dirnameList) |
|
270 | 287 | dirnameList = [x.string for x in dirnameList] |
|
271 | 288 | if not(online): |
|
272 | 289 | |
|
273 | 290 | dirnameList = [self.__selDates(x) for x in dirnameList] |
|
274 | 291 | dirnameList = filter(lambda x:x!=None,dirnameList) |
|
275 | 292 | |
|
276 | 293 | if len(dirnameList)>0: |
|
277 | 294 | self.status = 1 |
|
278 | 295 | self.dirnameList = dirnameList |
|
279 | 296 | self.dirnameList.sort() |
|
280 | 297 | |
|
281 | 298 | else: |
|
282 | 299 | self.status = 0 |
|
283 | 300 | return None |
|
284 | 301 | |
|
285 | 302 | def __getTimeFromData(self): |
|
286 | 303 | startDateTime_Reader = datetime.datetime.combine(self.startDate,self.startTime) |
|
287 | 304 | endDateTime_Reader = datetime.datetime.combine(self.endDate,self.endTime) |
|
288 | 305 | print 'Filtering Files from %s to %s'%(startDateTime_Reader, endDateTime_Reader) |
|
289 | 306 | print '........................................' |
|
290 | 307 | filter_filenameList=[] |
|
291 | 308 | self.filenameList.sort() |
|
292 | 309 | for i in range(len(self.filenameList)-1): |
|
293 | 310 | filename=self.filenameList[i] |
|
294 | 311 | dir_hf_filename= filename |
|
295 | 312 | fp= h5py.File(dir_hf_filename,'r') |
|
296 | 313 | hipoc=fp['t'].value |
|
297 | 314 | hipoc=hipoc+self.timezone |
|
298 | 315 | date_time=stuffr.unix2datestr(hipoc) |
|
299 | 316 | fp.close() |
|
300 | 317 | year =int(date_time[0:4]) |
|
301 | 318 | month=int(date_time[5:7]) |
|
302 | 319 | dom =int(date_time[8:10]) |
|
303 | 320 | hour =int(date_time[11:13]) |
|
304 | 321 | min =int(date_time[14:16]) |
|
305 | 322 | sec =int(date_time[17:19]) |
|
306 | 323 | this_time=datetime.datetime(year,month,dom,hour,min,sec) |
|
307 | 324 | if (this_time>=startDateTime_Reader and this_time <= endDateTime_Reader): |
|
308 | 325 | filter_filenameList.append(filename) |
|
309 | 326 | filter_filenameList.sort() |
|
310 | 327 | self.filenameList = filter_filenameList |
|
311 | 328 | return 1 |
|
312 | 329 | |
|
313 | 330 | def __getFilenameList(self): |
|
314 | 331 | #print "hola" |
|
315 | 332 | #print self.dirnameList |
|
316 | 333 | dirList = [os.path.join(self.path,x) for x in self.dirnameList] |
|
317 | 334 | self.filenameList= dirList |
|
318 | 335 | #print self.filenameList |
|
319 | 336 | #print "pase",len(self.filenameList) |
|
320 | 337 | |
|
321 | 338 | def __selectDataForTimes(self, online=False): |
|
322 | 339 | |
|
323 | 340 | if not(self.status): |
|
324 | 341 | return None |
|
325 | 342 | #---------------- |
|
326 | 343 | self.__getFilenameList() |
|
327 | 344 | #---------------- |
|
328 | 345 | if not(online): |
|
329 | 346 | if not(self.all): |
|
330 | 347 | self.__getTimeFromData() |
|
331 | 348 | if len(self.filenameList)>0: |
|
332 | 349 | self.status=1 |
|
333 | 350 | self.filenameList.sort() |
|
334 | 351 | else: |
|
335 | 352 | self.status=0 |
|
336 | 353 | return None |
|
337 | 354 | else: |
|
338 |
if self.set |
|
|
339 | filename =getlastFileFromPath(self.path,self.ext) | |
|
340 | ||
|
355 | if self.set != None: | |
|
356 | ||
|
357 | filename=getFileFromSet(self.path,self.ext,self.set) | |
|
358 | ||
|
341 | 359 | if self.flag_nextfile==True: |
|
342 | 360 | self.dirnameList=[filename] |
|
343 | 361 | fullfilename=self.path+"/"+filename |
|
344 |
self.filenameList=[ |
|
|
362 | self.filenameList=[fullfilename] | |
|
345 | 363 | self.filename_next_set=int(filename[6:16])+10 |
|
346 | 364 | |
|
347 | 365 | self.flag_nextfile=False |
|
348 | 366 | else: |
|
349 | if self.filename_next_set== int(filename[6:16]): | |
|
350 | self.dirnameList=[filename] | |
|
351 |
|
|
|
352 | self.filenameList=[self.filenameList[-1]] | |
|
353 | self.filename_next_set=int(filename[6:16])+10 | |
|
354 |
|
|
|
367 | print filename | |
|
368 | print "PRIMERA CONDICION" | |
|
369 | #if self.filename_next_set== int(filename[6:16]): | |
|
370 | print "TODO BIEN" | |
|
371 | ||
|
372 | if filename == None: | |
|
373 | raise ValueError, "corregir" | |
|
374 | ||
|
375 | self.dirnameList=[filename] | |
|
376 | fullfilename=self.path+"/"+filename | |
|
377 | self.filenameList=[fullfilename] | |
|
378 | self.filename_next_set=int(filename[6:16])+10 | |
|
379 | print "Setting next file",self.filename_next_set | |
|
380 | self.set=int(filename[6:16]) | |
|
381 | if True: | |
|
382 | pass | |
|
355 | 383 | else: |
|
356 | set=self.filename_next_set | |
|
357 | filename=getFileFromSet(self.path,self.ext,set=set) | |
|
358 | self.filename_next_set=int(filename[6:16])+10 | |
|
359 | if filename==None: | |
|
360 | filename =getlastFileFromPath(self.path,self.ext) | |
|
361 | self.filename_next_set=int(filename[6:16]) | |
|
362 | self.dirnameList=[filename] | |
|
363 | fullfilename=self.path+"/"+filename | |
|
364 | self.filenameList=[self.filenameList[-1]] | |
|
384 | print "ESTOY AQUI PORQUE NO EXISTE EL SIGUIENTE ARCHIVO" | |
|
365 | 385 | |
|
366 | 386 | else: |
|
367 | try: | |
|
368 | filename=getFileFromSet(self.path,self.ext,self.set) | |
|
387 | filename =getlastFileFromPath(self.path,self.ext) | |
|
388 | ||
|
389 | if self.flag_nextfile==True: | |
|
369 | 390 | self.dirnameList=[filename] |
|
370 | 391 | fullfilename=self.path+"/"+filename |
|
371 | self.filenameList=[fullfilename] | |
|
372 | except: | |
|
373 | 392 | self.filenameList=[self.filenameList[-1]] |
|
393 | self.filename_next_set=int(filename[6:16])+10 | |
|
394 | ||
|
395 | self.flag_nextfile=False | |
|
396 | else: | |
|
397 | filename=getFileFromSet(self.path,self.ext,self.set) | |
|
398 | print filename | |
|
399 | print "PRIMERA CONDICION" | |
|
400 | #if self.filename_next_set== int(filename[6:16]): | |
|
401 | print "TODO BIEN" | |
|
402 | ||
|
403 | if filename == None: | |
|
404 | raise ValueError, "corregir" | |
|
405 | ||
|
406 | self.dirnameList=[filename] | |
|
407 | fullfilename=self.path+"/"+filename | |
|
408 | self.filenameList=[fullfilename] | |
|
409 | self.filename_next_set=int(filename[6:16])+10 | |
|
410 | print "Setting next file",self.filename_next_set | |
|
411 | self.set=int(filename[6:16]) | |
|
412 | if True: | |
|
413 | pass | |
|
414 | else: | |
|
415 | print "ESTOY AQUI PORQUE NO EXISTE EL SIGUIENTE ARCHIVO" | |
|
416 | ||
|
374 | 417 | |
|
375 | 418 | |
|
376 | 419 | def __searchFilesOffline(self, |
|
377 | 420 | path, |
|
378 | 421 | startDate, |
|
379 | 422 | endDate, |
|
380 | 423 | ext, |
|
381 | 424 | startTime=datetime.time(0,0,0), |
|
382 | 425 | endTime=datetime.time(23,59,59), |
|
383 | 426 | walk=True): |
|
384 | 427 | |
|
385 | 428 | self.__setParameters(path, startDate, endDate, startTime, endTime, walk) |
|
386 | 429 | |
|
387 | 430 | self.__checkPath() |
|
388 | 431 | |
|
389 | 432 | self.__findDataForDates() |
|
390 | 433 | #print self.dirnameList |
|
391 | 434 | |
|
392 | 435 | self.__selectDataForTimes() |
|
393 | 436 | |
|
394 | 437 | for i in range(len(self.filenameList)): |
|
395 | 438 | print "%s"% (self.filenameList[i]) |
|
396 | 439 | |
|
397 | 440 | return |
|
398 | 441 | |
|
399 | 442 | def __searchFilesOnline(self, |
|
400 | 443 | path, |
|
401 | 444 | expLabel= "", |
|
402 | 445 | ext=None, |
|
403 | 446 | startDate=None, |
|
404 | 447 | endDate=None, |
|
405 | 448 | walk=True, |
|
406 | 449 | set=None): |
|
407 | 450 | |
|
408 | 451 | |
|
409 | 452 | startDate = datetime.datetime.utcnow().date() |
|
410 | 453 | endDate = datetime.datetime.utcnow().date() |
|
411 | 454 | |
|
412 | 455 | self.__setParameters(path=path,startDate=startDate,endDate=endDate,walk=walk) |
|
413 | 456 | |
|
414 | 457 | self.__checkPath() |
|
415 | 458 | |
|
416 | 459 | fullpath=path |
|
417 | 460 | print "%s folder was found: " %(fullpath ) |
|
418 | 461 | |
|
419 | 462 | if set == None: |
|
420 | 463 | self.set=None |
|
421 | 464 | filename =getlastFileFromPath(fullpath,ext) |
|
422 | 465 | startDate= datetime.datetime.utcnow().date |
|
423 | 466 | endDate= datetime.datetime.utcnow().date() |
|
424 | 467 | # |
|
425 | 468 | else: |
|
426 | 469 | filename= getFileFromSet(fullpath,ext,set) |
|
427 | 470 | startDate=None |
|
428 | 471 | endDate=None |
|
429 | 472 | # |
|
430 | 473 | if not (filename): |
|
431 | 474 | return None,None,None,None,None |
|
432 | 475 | #print "%s file was found" %(filename) |
|
433 | 476 | |
|
434 | 477 | # |
|
435 | 478 | # dir_hf_filename= self.path+"/"+filename |
|
436 | 479 | # fp= h5py.File(dir_hf_filename,'r') |
|
437 | 480 | # hipoc=fp['t'].value |
|
438 | 481 | # fp.close() |
|
439 | 482 | # date_time=stuffr.unix2datestr(hipoc) |
|
440 | 483 | # |
|
441 | 484 | # year =int(date_time[0:4]) |
|
442 | 485 | # month=int(date_time[5:7]) |
|
443 | 486 | # dom =int(date_time[8:10]) |
|
444 | 487 | # set= int(filename[4:10]) |
|
445 | 488 | # self.set=set-1 |
|
446 | 489 | #self.dirnameList=[filename] |
|
447 | 490 | filenameList= fullpath+"/"+filename |
|
448 | 491 | self.dirnameList=[filename] |
|
449 | 492 | self.filenameList=[filenameList] |
|
450 | 493 | self.flag_nextfile=True |
|
451 | 494 | |
|
452 | 495 | #self.__findDataForDates(online=True) |
|
453 | 496 | #self.dirnameList=[self.dirnameList[-1]] |
|
454 | 497 | #print self.dirnameList |
|
455 | 498 | #self.__selectDataForTimes(online=True) |
|
456 | 499 | #return fullpath,filename,year,month,dom,set |
|
457 | 500 | return |
|
458 | 501 | |
|
459 | 502 | def __setNextFile(self,online=False): |
|
460 | 503 | """ |
|
461 | 504 | """ |
|
462 | 505 | if not(online): |
|
463 | 506 | newFile = self.__setNextFileOffline() |
|
464 | 507 | else: |
|
465 | 508 | newFile = self.__setNextFileOnline() |
|
466 | 509 | |
|
467 | 510 | if not(newFile): |
|
468 | 511 | return 0 |
|
469 | 512 | return 1 |
|
470 | 513 | |
|
471 | 514 | def __setNextFileOffline(self): |
|
472 | 515 | """ |
|
473 | 516 | """ |
|
474 | 517 | idFile= self.fileIndex |
|
475 | 518 | while(True): |
|
476 | 519 | idFile += 1 |
|
477 | 520 | if not (idFile < len(self.filenameList)): |
|
478 | 521 | self.flagNoMoreFiles = 1 |
|
479 | 522 | print "No more Files" |
|
480 | 523 | return 0 |
|
481 | 524 | filename = self.filenameList[idFile] |
|
482 | 525 | hfFilePointer =h5py.File(filename,'r') |
|
483 | 526 | |
|
484 | 527 | epoc=hfFilePointer['t'].value |
|
485 | 528 | #this_time=datetime.datetime(year,month,dom,hour,min,sec) |
|
486 | 529 | break |
|
487 | 530 | |
|
488 | 531 | self.flagIsNewFile = 1 |
|
489 | 532 | self.fileIndex = idFile |
|
490 | 533 | self.filename = filename |
|
491 | 534 | |
|
492 | 535 | self.hfFilePointer = hfFilePointer |
|
493 | 536 | hfFilePointer.close() |
|
494 | 537 | self.__t0=epoc |
|
495 | 538 | print "Setting the file: %s"%self.filename |
|
496 | 539 | |
|
497 | 540 | return 1 |
|
498 | 541 | |
|
499 | 542 | def __setNextFileOnline(self): |
|
500 | 543 | """ |
|
501 | 544 | """ |
|
545 | print "SOY NONE",self.set | |
|
502 | 546 | if self.set==None: |
|
503 | 547 | pass |
|
504 | 548 | else: |
|
505 | 549 | self.set +=10 |
|
506 | 550 | |
|
507 | 551 | filename = self.filenameList[0]#fullfilename |
|
508 | 552 | if self.filename_online != None: |
|
509 | 553 | self.__selectDataForTimes(online=True) |
|
510 | 554 | filename = self.filenameList[0] |
|
511 | 555 | while self.filename_online == filename: |
|
512 | 556 | print 'waiting %d seconds to get a new file...'%(self.__waitForNewFile) |
|
513 | sleep(self.__waitForNewFile) | |
|
557 | time.sleep(self.__waitForNewFile) | |
|
514 | 558 | #self.__findDataForDates(online=True) |
|
559 | self.set=self.filename_next_set | |
|
515 | 560 | self.__selectDataForTimes(online=True) |
|
516 | 561 | filename = self.filenameList[0] |
|
517 | 562 | sizeoffile=os.path.getsize(filename) |
|
518 | 563 | |
|
519 | 564 | #print filename |
|
520 | 565 | sizeoffile=os.path.getsize(filename) |
|
521 | 566 | if sizeoffile<1670240: |
|
522 | 567 | print "%s is not the rigth size"%filename |
|
523 |
delay= |
|
|
568 | delay=50 | |
|
524 | 569 | print 'waiting %d seconds for delay...'%(delay) |
|
525 | sleep(delay) | |
|
570 | time.sleep(delay) | |
|
571 | sizeoffile=os.path.getsize(filename) | |
|
572 | if sizeoffile<1670240: | |
|
573 | delay=50 | |
|
574 | print 'waiting %d more seconds for delay...'%(delay) | |
|
575 | time.sleep(delay) | |
|
576 | ||
|
526 | 577 | sizeoffile=os.path.getsize(filename) |
|
527 | 578 | if sizeoffile<1670240: |
|
528 | delay | |
|
579 | delay=50 | |
|
529 | 580 | print 'waiting %d more seconds for delay...'%(delay) |
|
530 | sleep(delay) | |
|
581 | time.sleep(delay) | |
|
531 | 582 | |
|
532 | ||
|
533 | ||
|
534 | 583 | try: |
|
535 | 584 | hfFilePointer=h5py.File(filename,'r') |
|
536 | 585 | |
|
537 | 586 | except: |
|
538 | 587 | print "Error reading file %s"%filename |
|
539 | 588 | |
|
540 | 589 | self.filename_online=filename |
|
541 | 590 | epoc=hfFilePointer['t'].value |
|
542 | 591 | |
|
543 | 592 | self.hfFilePointer=hfFilePointer |
|
544 | 593 | hfFilePointer.close() |
|
545 | 594 | self.__t0=epoc |
|
546 | 595 | |
|
547 | 596 | |
|
548 | 597 | self.flagIsNewFile = 1 |
|
549 | 598 | self.filename = filename |
|
550 | 599 | |
|
551 | 600 | print "Setting the file: %s"%self.filename |
|
552 | 601 | return 1 |
|
553 | 602 | |
|
554 | 603 | def __getExpParameters(self): |
|
555 | 604 | if not(self.status): |
|
556 | 605 | return None |
|
557 | 606 | |
|
558 | 607 | def setup(self, |
|
559 | 608 | path = None, |
|
560 | 609 | startDate = None, |
|
561 | 610 | endDate = None, |
|
562 | 611 | startTime = datetime.time(0,0,0), |
|
563 | 612 | endTime = datetime.time(23,59,59), |
|
564 | 613 | set = None, |
|
565 | 614 | expLabel = "", |
|
566 | 615 | ext = None, |
|
567 | 616 | all=0, |
|
568 | 617 | timezone=0, |
|
569 | 618 | online = False, |
|
570 | 619 | delay = 60, |
|
571 | 620 | walk = True): |
|
572 | 621 | ''' |
|
573 | 622 | In this method we should set all initial parameters. |
|
574 | 623 | |
|
575 | 624 | ''' |
|
576 | 625 | if path==None: |
|
577 | 626 | raise ValueError,"The path is not valid" |
|
578 | 627 | |
|
579 | 628 | if ext==None: |
|
580 | 629 | ext = self.ext |
|
581 | 630 | |
|
582 | 631 | self.timezone= timezone |
|
583 | 632 | self.online= online |
|
584 | 633 | self.all=all |
|
585 | 634 | #if set==None: |
|
586 | 635 | |
|
587 | 636 | #print set |
|
588 | 637 | if not(online): |
|
589 | 638 | print "Searching files in offline mode..." |
|
590 | 639 | |
|
591 | 640 | self.__searchFilesOffline(path, startDate, endDate, ext, startTime, endTime, walk) |
|
592 | 641 | else: |
|
593 | 642 | print "Searching files in online mode..." |
|
594 | 643 | self.__searchFilesOnline(path, walk,ext,set=set) |
|
595 | 644 | if set==None: |
|
596 | 645 | pass |
|
597 | 646 | else: |
|
598 | 647 | self.set=set-10 |
|
599 | 648 | |
|
600 | 649 | # for nTries in range(self.nTries): |
|
601 | 650 | # |
|
602 | 651 | # fullpath,file,year,month,day,set = self.__searchFilesOnline(path=path,expLabel=expLabel,ext=ext, walk=walk,set=set) |
|
603 | 652 | # |
|
604 | 653 | # if fullpath: |
|
605 | 654 | # break |
|
606 | 655 | # print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1) |
|
607 | 656 | # time.sleep(self.delay) |
|
608 | 657 | # if not(fullpath): |
|
609 | 658 | # print "There ins't valid files in %s" % path |
|
610 | 659 | # return None |
|
611 | 660 | |
|
612 | 661 | |
|
613 | 662 | if not(self.filenameList): |
|
614 | 663 | print "There is no files into the folder: %s"%(path) |
|
615 | 664 | sys.exit(-1) |
|
616 | 665 | |
|
617 | 666 | self.__getExpParameters() |
|
618 | 667 | |
|
619 | 668 | |
|
620 | 669 | self.fileIndex = -1 |
|
621 | 670 | |
|
622 | 671 | self.__setNextFile(online) |
|
623 | 672 | |
|
624 | 673 | self.__readMetadata() |
|
625 | 674 | |
|
626 | 675 | self.__setLocalVariables() |
|
627 | 676 | |
|
628 | 677 | self.__setHeaderDO() |
|
629 | 678 | #self.profileIndex_offset= 0 |
|
630 | 679 | |
|
631 | 680 | #self.profileIndex = self.profileIndex_offset |
|
632 | 681 | |
|
633 | 682 | self.isConfig = True |
|
634 | 683 | |
|
635 | 684 | def __readMetadata(self): |
|
636 | 685 | self.__readHeader() |
|
637 | 686 | |
|
638 | 687 | |
|
639 | 688 | def __setLocalVariables(self): |
|
640 | 689 | |
|
641 | 690 | self.datablock = numpy.zeros((self.nChannels, self.nHeights,self.nProfiles), dtype = numpy.complex) |
|
642 | 691 | # |
|
643 | 692 | |
|
644 | 693 | |
|
645 | 694 | |
|
646 | 695 | self.profileIndex = 9999 |
|
647 | 696 | |
|
648 | 697 | |
|
649 | 698 | def __setHeaderDO(self): |
|
650 | 699 | |
|
651 | 700 | |
|
652 | 701 | self.dataOut.radarControllerHeaderObj = RadarControllerHeader() |
|
653 | 702 | |
|
654 | 703 | self.dataOut.systemHeaderObj = SystemHeader() |
|
655 | 704 | |
|
656 | 705 | |
|
657 | 706 | #--------------------------------------------------------- |
|
658 | 707 | self.dataOut.systemHeaderObj.nProfiles=100 |
|
659 | 708 | self.dataOut.systemHeaderObj.nSamples=1000 |
|
660 | 709 | |
|
661 | 710 | |
|
662 | 711 | SAMPLING_STRUCTURE=[('h0', '<f4'), ('dh', '<f4'), ('nsa', '<u4')] |
|
663 | 712 | self.dataOut.radarControllerHeaderObj.samplingWindow=numpy.zeros((1,),SAMPLING_STRUCTURE) |
|
664 | 713 | self.dataOut.radarControllerHeaderObj.samplingWindow['h0']=0 |
|
665 | 714 | self.dataOut.radarControllerHeaderObj.samplingWindow['dh']=1.5 |
|
666 | 715 | self.dataOut.radarControllerHeaderObj.samplingWindow['nsa']=1000 |
|
667 | 716 | self.dataOut.radarControllerHeaderObj.nHeights=int(self.dataOut.radarControllerHeaderObj.samplingWindow['nsa']) |
|
668 | 717 | self.dataOut.radarControllerHeaderObj.firstHeight = self.dataOut.radarControllerHeaderObj.samplingWindow['h0'] |
|
669 | 718 | self.dataOut.radarControllerHeaderObj.deltaHeight = self.dataOut.radarControllerHeaderObj.samplingWindow['dh'] |
|
670 | 719 | self.dataOut.radarControllerHeaderObj.samplesWin = self.dataOut.radarControllerHeaderObj.samplingWindow['nsa'] |
|
671 | 720 | |
|
672 | 721 | self.dataOut.radarControllerHeaderObj.nWindows=1 |
|
673 | 722 | self.dataOut.radarControllerHeaderObj.codetype=0 |
|
674 | 723 | self.dataOut.radarControllerHeaderObj.numTaus=0 |
|
675 | 724 | #self.dataOut.radarControllerHeaderObj.Taus = numpy.zeros((1,),'<f4') |
|
676 | 725 | |
|
677 | 726 | |
|
678 | 727 | #self.dataOut.radarControllerHeaderObj.nCode=numpy.zeros((1,), '<u4') |
|
679 | 728 | #self.dataOut.radarControllerHeaderObj.nBaud=numpy.zeros((1,), '<u4') |
|
680 | 729 | #self.dataOut.radarControllerHeaderObj.code=numpy.zeros(0) |
|
681 | 730 | |
|
682 | 731 | self.dataOut.radarControllerHeaderObj.code_size=0 |
|
683 | 732 | self.dataOut.nBaud=0 |
|
684 | 733 | self.dataOut.nCode=0 |
|
685 | 734 | self.dataOut.nPairs=0 |
|
686 | 735 | |
|
687 | 736 | |
|
688 | 737 | #--------------------------------------------------------- |
|
689 | 738 | |
|
690 | 739 | self.dataOut.type = "Voltage" |
|
691 | 740 | |
|
692 | 741 | self.dataOut.data = None |
|
693 | 742 | |
|
694 | 743 | self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
695 | 744 | |
|
696 | 745 | self.dataOut.nProfiles = 1 |
|
697 | 746 | |
|
698 | 747 | self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype = numpy.float)*self.__deltaHeigth |
|
699 | 748 | |
|
700 | 749 | self.dataOut.channelList = range(self.nChannels) |
|
701 | 750 | |
|
702 | 751 | #self.dataOut.channelIndexList = None |
|
703 | 752 | |
|
704 | 753 | self.dataOut.flagNoData = True |
|
705 | 754 | |
|
706 | 755 | #Set to TRUE if the data is discontinuous |
|
707 | 756 | self.dataOut.flagDiscontinuousBlock = False |
|
708 | 757 | |
|
709 | 758 | self.dataOut.utctime = None |
|
710 | 759 | |
|
711 |
self.dataOut.timeZone = |
|
|
760 | self.dataOut.timeZone = self.timezone | |
|
712 | 761 | |
|
713 | 762 | self.dataOut.dstFlag = 0 |
|
714 | 763 | |
|
715 | 764 | self.dataOut.errorCount = 0 |
|
716 | 765 | |
|
717 | 766 | self.dataOut.nCohInt = 1 |
|
718 | 767 | |
|
719 | 768 | self.dataOut.blocksize = self.dataOut.getNChannels() * self.dataOut.getNHeights() |
|
720 | 769 | |
|
721 | 770 | self.dataOut.flagDecodeData = False #asumo que la data esta decodificada |
|
722 | 771 | |
|
723 | 772 | self.dataOut.flagDeflipData = False #asumo que la data esta sin flip |
|
724 | 773 | |
|
725 | 774 | self.dataOut.flagShiftFFT = False |
|
726 | 775 | |
|
727 | 776 | self.dataOut.ippSeconds = 1.0*self.__nSamples/self.__sample_rate |
|
728 | 777 | |
|
729 | 778 | #Time interval between profiles |
|
730 | 779 | #self.dataOut.timeInterval =self.dataOut.ippSeconds * self.dataOut.nCohInt |
|
731 | 780 | |
|
732 | 781 | |
|
733 | 782 | self.dataOut.frequency = self.__frequency |
|
734 | 783 | |
|
735 | 784 | self.dataOut.realtime = self.__online |
|
736 | 785 | |
|
737 | 786 | def __hasNotDataInBuffer(self): |
|
738 | 787 | |
|
739 | 788 | if self.profileIndex >= self.nProfiles: |
|
740 | 789 | return 1 |
|
741 | 790 | |
|
742 | 791 | return 0 |
|
743 | 792 | |
|
744 | 793 | def readNextBlock(self): |
|
745 | 794 | if not(self.__setNewBlock()): |
|
746 | 795 | return 0 |
|
747 | 796 | |
|
748 | 797 | if not(self.readBlock()): |
|
749 | 798 | return 0 |
|
750 | 799 | |
|
751 | 800 | return 1 |
|
752 | 801 | |
|
753 | 802 | def __setNewBlock(self): |
|
754 | 803 | |
|
755 | 804 | if self.hfFilePointer==None: |
|
756 | 805 | return 0 |
|
757 | 806 | |
|
758 | 807 | if self.flagIsNewFile: |
|
759 | 808 | return 1 |
|
760 | 809 | |
|
761 | 810 | if self.profileIndex < self.nProfiles: |
|
762 | 811 | return 1 |
|
763 | 812 | |
|
764 | 813 | self.__setNextFile(self.online) |
|
765 | 814 | |
|
766 | 815 | return 1 |
|
767 | 816 | |
|
768 | 817 | |
|
769 | 818 | |
|
770 | 819 | def readBlock(self): |
|
771 | 820 | fp=h5py.File(self.filename,'r') |
|
772 | 821 | #Puntero que apunta al archivo hdf5 |
|
773 | 822 | ch0=(fp['ch0']).value #Primer canal (100,1000)--(perfiles,alturas) |
|
774 | 823 | ch1=(fp['ch1']).value #Segundo canal (100,1000)--(perfiles,alturas) |
|
775 | 824 | fp.close() |
|
776 | 825 | ch0= ch0.swapaxes(0,1) #Primer canal (100,1000)--(alturas,perfiles) |
|
777 | 826 | ch1= ch1.swapaxes(0,1) #Segundo canal (100,1000)--(alturas,perfiles) |
|
778 | 827 | self.datablock = numpy.array([ch0,ch1]) |
|
779 | 828 | self.flagIsNewFile=0 |
|
780 | 829 | |
|
781 | 830 | self.profileIndex=0 |
|
782 | 831 | |
|
783 | 832 | return 1 |
|
784 | 833 | |
|
785 | 834 | def getData(self): |
|
786 | 835 | if self.flagNoMoreFiles: |
|
787 | 836 | self.dataOut.flagNoData = True |
|
788 | 837 | print 'Process finished' |
|
789 | 838 | return 0 |
|
790 | 839 | |
|
791 | 840 | if self.__hasNotDataInBuffer(): |
|
792 | 841 | if not(self.readNextBlock()): |
|
793 | 842 | self.dataOut.flagNodata=True |
|
794 | 843 | return 0 |
|
795 | 844 | |
|
796 | 845 | ############################## |
|
797 | 846 | ############################## |
|
798 | 847 | self.dataOut.data = self.datablock[:,:,self.profileIndex] |
|
799 |
self.dataOut.utctime= self.__t0 + self.dataOut.ippSeconds*self.profileIndex |
|
|
848 | self.dataOut.utctime = self.__t0 + self.dataOut.ippSeconds*self.profileIndex | |
|
800 | 849 | self.dataOut.profileIndex= self.profileIndex |
|
801 | 850 | self.dataOut.flagNoData=False |
|
802 | 851 | self.profileIndex +=1 |
|
803 | 852 | |
|
804 | 853 | return self.dataOut.data |
|
805 | 854 | |
|
806 | 855 | |
|
807 | 856 | def run(self, **kwargs): |
|
808 | 857 | ''' |
|
809 | 858 | This method will be called many times so here you should put all your code |
|
810 | 859 | ''' |
|
811 | 860 | |
|
812 | 861 | if not self.isConfig: |
|
813 | 862 | self.setup(**kwargs) |
|
814 | 863 | self.isConfig = True |
|
815 | 864 | self.getData() |
|
816 | 865 | |
|
817 | 866 | |
|
818 | 867 | |
|
819 | No newline at end of file | |
|
868 |
General Comments 0
You need to be logged in to leave comments.
Login now