@@ -1,1015 +1,1025 | |||
|
1 | 1 | ''' |
|
2 | 2 | |
|
3 | 3 | $Author: murco $ |
|
4 | 4 | $Id: JROData.py 173 2012-11-20 15:06:21Z murco $ |
|
5 | 5 | ''' |
|
6 | 6 | |
|
7 | 7 | import copy |
|
8 | 8 | import numpy |
|
9 | 9 | import datetime |
|
10 | 10 | |
|
11 | 11 | from jroheaderIO import SystemHeader, RadarControllerHeader |
|
12 | 12 | |
|
13 | 13 | def getNumpyDtype(dataTypeCode): |
|
14 | 14 | |
|
15 | 15 | if dataTypeCode == 0: |
|
16 | 16 | numpyDtype = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
17 | 17 | elif dataTypeCode == 1: |
|
18 | 18 | numpyDtype = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
19 | 19 | elif dataTypeCode == 2: |
|
20 | 20 | numpyDtype = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
21 | 21 | elif dataTypeCode == 3: |
|
22 | 22 | numpyDtype = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
23 | 23 | elif dataTypeCode == 4: |
|
24 | 24 | numpyDtype = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
25 | 25 | elif dataTypeCode == 5: |
|
26 | 26 | numpyDtype = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
27 | 27 | else: |
|
28 | 28 | raise ValueError, 'dataTypeCode was not defined' |
|
29 | 29 | |
|
30 | 30 | return numpyDtype |
|
31 | 31 | |
|
32 | 32 | def getDataTypeCode(numpyDtype): |
|
33 | 33 | |
|
34 | 34 | if numpyDtype == numpy.dtype([('real','<i1'),('imag','<i1')]): |
|
35 | 35 | datatype = 0 |
|
36 | 36 | elif numpyDtype == numpy.dtype([('real','<i2'),('imag','<i2')]): |
|
37 | 37 | datatype = 1 |
|
38 | 38 | elif numpyDtype == numpy.dtype([('real','<i4'),('imag','<i4')]): |
|
39 | 39 | datatype = 2 |
|
40 | 40 | elif numpyDtype == numpy.dtype([('real','<i8'),('imag','<i8')]): |
|
41 | 41 | datatype = 3 |
|
42 | 42 | elif numpyDtype == numpy.dtype([('real','<f4'),('imag','<f4')]): |
|
43 | 43 | datatype = 4 |
|
44 | 44 | elif numpyDtype == numpy.dtype([('real','<f8'),('imag','<f8')]): |
|
45 | 45 | datatype = 5 |
|
46 | 46 | else: |
|
47 | 47 | datatype = None |
|
48 | 48 | |
|
49 | 49 | return datatype |
|
50 | 50 | |
|
51 | 51 | def hildebrand_sekhon(data, navg): |
|
52 | 52 | |
|
53 | 53 | data = data.copy() |
|
54 | 54 | |
|
55 | 55 | sortdata = numpy.sort(data,axis=None) |
|
56 | 56 | lenOfData = len(sortdata) |
|
57 | 57 | nums_min = lenOfData/10 |
|
58 | 58 | |
|
59 | 59 | if (lenOfData/10) > 2: |
|
60 | 60 | nums_min = lenOfData/10 |
|
61 | 61 | else: |
|
62 | 62 | nums_min = 2 |
|
63 | 63 | |
|
64 | 64 | sump = 0. |
|
65 | 65 | |
|
66 | 66 | sumq = 0. |
|
67 | 67 | |
|
68 | 68 | j = 0 |
|
69 | 69 | |
|
70 | 70 | cont = 1 |
|
71 | 71 | |
|
72 | 72 | while((cont==1)and(j<lenOfData)): |
|
73 | 73 | |
|
74 | 74 | sump += sortdata[j] |
|
75 | 75 | |
|
76 | 76 | sumq += sortdata[j]**2 |
|
77 | 77 | |
|
78 | 78 | j += 1 |
|
79 | 79 | |
|
80 | 80 | if j > nums_min: |
|
81 | 81 | rtest = float(j)/(j-1) + 1.0/navg |
|
82 | 82 | if ((sumq*j) > (rtest*sump**2)): |
|
83 | 83 | j = j - 1 |
|
84 | 84 | sump = sump - sortdata[j] |
|
85 | 85 | sumq = sumq - sortdata[j]**2 |
|
86 | 86 | cont = 0 |
|
87 | 87 | |
|
88 | 88 | lnoise = sump /j |
|
89 | 89 | stdv = numpy.sqrt((sumq - lnoise**2)/(j - 1)) |
|
90 | 90 | return lnoise |
|
91 | 91 | |
|
92 | 92 | class Beam: |
|
93 | 93 | def __init__(self): |
|
94 | 94 | self.codeList = [] |
|
95 | 95 | self.azimuthList = [] |
|
96 | 96 | self.zenithList = [] |
|
97 | 97 | |
|
98 | 98 | class GenericData(object): |
|
99 | 99 | |
|
100 | 100 | flagNoData = True |
|
101 | 101 | |
|
102 | 102 | def __init__(self): |
|
103 | 103 | |
|
104 | 104 | raise ValueError, "This class has not been implemented" |
|
105 | 105 | |
|
106 | 106 | def copy(self, inputObj=None): |
|
107 | 107 | |
|
108 | 108 | if inputObj == None: |
|
109 | 109 | return copy.deepcopy(self) |
|
110 | 110 | |
|
111 | 111 | for key in inputObj.__dict__.keys(): |
|
112 | 112 | self.__dict__[key] = inputObj.__dict__[key] |
|
113 | 113 | |
|
114 | 114 | def deepcopy(self): |
|
115 | 115 | |
|
116 | 116 | return copy.deepcopy(self) |
|
117 | 117 | |
|
118 | 118 | def isEmpty(self): |
|
119 | 119 | |
|
120 | 120 | return self.flagNoData |
|
121 | 121 | |
|
122 | 122 | class JROData(GenericData): |
|
123 | 123 | |
|
124 | 124 | # m_BasicHeader = BasicHeader() |
|
125 | 125 | # m_ProcessingHeader = ProcessingHeader() |
|
126 | 126 | |
|
127 | 127 | systemHeaderObj = SystemHeader() |
|
128 | 128 | |
|
129 | 129 | radarControllerHeaderObj = RadarControllerHeader() |
|
130 | 130 | |
|
131 | 131 | # data = None |
|
132 | 132 | |
|
133 | 133 | type = None |
|
134 | 134 | |
|
135 | 135 | datatype = None #dtype but in string |
|
136 | 136 | |
|
137 | 137 | # dtype = None |
|
138 | 138 | |
|
139 | 139 | # nChannels = None |
|
140 | 140 | |
|
141 | 141 | # nHeights = None |
|
142 | 142 | |
|
143 | 143 | nProfiles = None |
|
144 | 144 | |
|
145 | 145 | heightList = None |
|
146 | 146 | |
|
147 | 147 | channelList = None |
|
148 | 148 | |
|
149 | 149 | flagTimeBlock = False |
|
150 | 150 | |
|
151 | 151 | useLocalTime = False |
|
152 | 152 | |
|
153 | 153 | utctime = None |
|
154 | 154 | |
|
155 | 155 | timeZone = None |
|
156 | 156 | |
|
157 | 157 | dstFlag = None |
|
158 | 158 | |
|
159 | 159 | errorCount = None |
|
160 | 160 | |
|
161 | 161 | blocksize = None |
|
162 | 162 | |
|
163 | 163 | nCode = None |
|
164 | 164 | |
|
165 | 165 | nBaud = None |
|
166 | 166 | |
|
167 | 167 | code = None |
|
168 | 168 | |
|
169 | 169 | flagDecodeData = False #asumo q la data no esta decodificada |
|
170 | 170 | |
|
171 | 171 | flagDeflipData = False #asumo q la data no esta sin flip |
|
172 | 172 | |
|
173 | 173 | flagShiftFFT = False |
|
174 | 174 | |
|
175 | 175 | # ippSeconds = None |
|
176 | 176 | |
|
177 | 177 | # timeInterval = None |
|
178 | 178 | |
|
179 | 179 | nCohInt = None |
|
180 | 180 | |
|
181 | 181 | noise = None |
|
182 | 182 | |
|
183 | 183 | windowOfFilter = 1 |
|
184 | 184 | |
|
185 | 185 | #Speed of ligth |
|
186 | 186 | C = 3e8 |
|
187 | 187 | |
|
188 | 188 | frequency = 49.92e6 |
|
189 | 189 | |
|
190 | 190 | realtime = False |
|
191 | 191 | |
|
192 | 192 | beacon_heiIndexList = None |
|
193 | 193 | |
|
194 | 194 | last_block = None |
|
195 | 195 | |
|
196 | 196 | blocknow = None |
|
197 | 197 | |
|
198 | 198 | azimuth = None |
|
199 | 199 | |
|
200 | 200 | zenith = None |
|
201 | 201 | |
|
202 | 202 | beam = Beam() |
|
203 | 203 | |
|
204 | profileIndex = None | |
|
205 | ||
|
204 | 206 | def __init__(self): |
|
205 | 207 | |
|
206 | 208 | raise ValueError, "This class has not been implemented" |
|
207 | 209 | |
|
208 | 210 | def getNoise(self): |
|
209 | 211 | |
|
210 | 212 | raise ValueError, "Not implemented" |
|
211 | 213 | |
|
212 | 214 | def getNChannels(self): |
|
213 | 215 | |
|
214 | 216 | return len(self.channelList) |
|
215 | 217 | |
|
216 | 218 | def getChannelIndexList(self): |
|
217 | 219 | |
|
218 | 220 | return range(self.nChannels) |
|
219 | 221 | |
|
220 | 222 | def getNHeights(self): |
|
221 | 223 | |
|
222 | 224 | return len(self.heightList) |
|
223 | 225 | |
|
224 | 226 | def getHeiRange(self, extrapoints=0): |
|
225 | 227 | |
|
226 | 228 | heis = self.heightList |
|
227 | 229 | # deltah = self.heightList[1] - self.heightList[0] |
|
228 | 230 | # |
|
229 | 231 | # heis.append(self.heightList[-1]) |
|
230 | 232 | |
|
231 | 233 | return heis |
|
232 | 234 | |
|
233 | 235 | def getltctime(self): |
|
234 | 236 | |
|
235 | 237 | if self.useLocalTime: |
|
236 | 238 | return self.utctime - self.timeZone*60 |
|
237 | 239 | |
|
238 | 240 | return self.utctime |
|
239 | 241 | |
|
240 | 242 | def getDatatime(self): |
|
241 | 243 | |
|
242 | 244 | datatimeValue = datetime.datetime.utcfromtimestamp(self.ltctime) |
|
243 | 245 | return datatimeValue |
|
244 | 246 | |
|
245 | 247 | def getTimeRange(self): |
|
246 | 248 | |
|
247 | 249 | datatime = [] |
|
248 | 250 | |
|
249 | 251 | datatime.append(self.ltctime) |
|
250 | 252 | datatime.append(self.ltctime + self.timeInterval) |
|
251 | 253 | |
|
252 | 254 | datatime = numpy.array(datatime) |
|
253 | 255 | |
|
254 | 256 | return datatime |
|
255 | 257 | |
|
256 | 258 | def getFmax(self): |
|
257 | 259 | |
|
258 | 260 | PRF = 1./(self.ippSeconds * self.nCohInt) |
|
259 | 261 | |
|
260 | 262 | fmax = PRF/2. |
|
261 | 263 | |
|
262 | 264 | return fmax |
|
263 | 265 | |
|
264 | 266 | def getVmax(self): |
|
265 | 267 | |
|
266 | 268 | _lambda = self.C/self.frequency |
|
267 | 269 | |
|
268 | 270 | vmax = self.getFmax() * _lambda |
|
269 | 271 | |
|
270 | 272 | return vmax |
|
271 | 273 | |
|
272 | 274 | def get_ippSeconds(self): |
|
273 | 275 | ''' |
|
274 | 276 | ''' |
|
275 | 277 | return self.radarControllerHeaderObj.ippSeconds |
|
276 | 278 | |
|
277 | 279 | def set_ippSeconds(self, ippSeconds): |
|
278 | 280 | ''' |
|
279 | 281 | ''' |
|
280 | 282 | |
|
281 | 283 | self.radarControllerHeaderObj.ippSeconds = ippSeconds |
|
282 | 284 | |
|
283 | 285 | return |
|
284 | 286 | |
|
285 | 287 | def get_dtype(self): |
|
286 | 288 | ''' |
|
287 | 289 | ''' |
|
288 | 290 | return getNumpyDtype(self.datatype) |
|
289 | 291 | |
|
290 | 292 | def set_dtype(self, numpyDtype): |
|
291 | 293 | ''' |
|
292 | 294 | ''' |
|
293 | 295 | |
|
294 | 296 | self.datatype = getDataTypeCode(numpyDtype) |
|
295 | 297 | |
|
296 | 298 | # def getTimeInterval(self): |
|
297 | 299 | # |
|
298 | 300 | # raise IOError, "This method should be implemented inside each Class" |
|
299 | 301 | |
|
300 | 302 | nChannels = property(getNChannels, "I'm the 'nChannel' property.") |
|
301 | 303 | channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.") |
|
302 | 304 | nHeights = property(getNHeights, "I'm the 'nHeights' property.") |
|
303 | 305 | #noise = property(getNoise, "I'm the 'nHeights' property.") |
|
304 | 306 | datatime = property(getDatatime, "I'm the 'datatime' property") |
|
305 | 307 | ltctime = property(getltctime, "I'm the 'ltctime' property") |
|
306 | 308 | ippSeconds = property(get_ippSeconds, set_ippSeconds) |
|
307 | 309 | dtype = property(get_dtype, set_dtype) |
|
308 | 310 | # timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property") |
|
309 | 311 | |
|
310 | 312 | class Voltage(JROData): |
|
311 | 313 | |
|
312 | 314 | #data es un numpy array de 2 dmensiones (canales, alturas) |
|
313 | 315 | data = None |
|
314 | 316 | |
|
315 | 317 | def __init__(self): |
|
316 | 318 | ''' |
|
317 | 319 | Constructor |
|
318 | 320 | ''' |
|
319 | 321 | |
|
320 | 322 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
321 | 323 | |
|
322 | 324 | self.systemHeaderObj = SystemHeader() |
|
323 | 325 | |
|
324 | 326 | self.type = "Voltage" |
|
325 | 327 | |
|
326 | 328 | self.data = None |
|
327 | 329 | |
|
328 | 330 | # self.dtype = None |
|
329 | 331 | |
|
330 | 332 | # self.nChannels = 0 |
|
331 | 333 | |
|
332 | 334 | # self.nHeights = 0 |
|
333 | 335 | |
|
334 | 336 | self.nProfiles = None |
|
335 | 337 | |
|
336 | 338 | self.heightList = None |
|
337 | 339 | |
|
338 | 340 | self.channelList = None |
|
339 | 341 | |
|
340 | 342 | # self.channelIndexList = None |
|
341 | 343 | |
|
342 | 344 | self.flagNoData = True |
|
343 | 345 | |
|
344 | 346 | self.flagTimeBlock = False |
|
345 | 347 | |
|
346 | 348 | self.utctime = None |
|
347 | 349 | |
|
348 | 350 | self.timeZone = None |
|
349 | 351 | |
|
350 | 352 | self.dstFlag = None |
|
351 | 353 | |
|
352 | 354 | self.errorCount = None |
|
353 | 355 | |
|
354 | 356 | self.nCohInt = None |
|
355 | 357 | |
|
356 | 358 | self.blocksize = None |
|
357 | 359 | |
|
358 | 360 | self.flagDecodeData = False #asumo q la data no esta decodificada |
|
359 | 361 | |
|
360 | 362 | self.flagDeflipData = False #asumo q la data no esta sin flip |
|
361 | 363 | |
|
362 | 364 | self.flagShiftFFT = False |
|
363 | 365 | |
|
364 | 366 | self.flagDataAsBlock = False #Asumo que la data es leida perfil a perfil |
|
365 | 367 | |
|
368 | self.profileIndex = 0 | |
|
369 | ||
|
366 | 370 | def getNoisebyHildebrand(self): |
|
367 | 371 | """ |
|
368 | 372 | Determino el nivel de ruido usando el metodo Hildebrand-Sekhon |
|
369 | 373 | |
|
370 | 374 | Return: |
|
371 | 375 | noiselevel |
|
372 | 376 | """ |
|
373 | 377 | |
|
374 | 378 | for channel in range(self.nChannels): |
|
375 | 379 | daux = self.data_spc[channel,:,:] |
|
376 | 380 | self.noise[channel] = hildebrand_sekhon(daux, self.nCohInt) |
|
377 | 381 | |
|
378 | 382 | return self.noise |
|
379 | 383 | |
|
380 | 384 | def getNoise(self, type = 1): |
|
381 | 385 | |
|
382 | 386 | self.noise = numpy.zeros(self.nChannels) |
|
383 | 387 | |
|
384 | 388 | if type == 1: |
|
385 | 389 | noise = self.getNoisebyHildebrand() |
|
386 | 390 | |
|
387 | 391 | return 10*numpy.log10(noise) |
|
388 | 392 | |
|
389 | 393 | def getTimeInterval(self): |
|
390 | 394 | |
|
391 | 395 | timeInterval = self.ippSeconds * self.nCohInt |
|
392 | 396 | |
|
393 | 397 | return timeInterval |
|
394 | 398 | |
|
395 | 399 | noise = property(getNoise, "I'm the 'nHeights' property.") |
|
396 | 400 | timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property") |
|
397 | 401 | |
|
398 | 402 | class Spectra(JROData): |
|
399 | 403 | |
|
400 | 404 | #data es un numpy array de 2 dmensiones (canales, perfiles, alturas) |
|
401 | 405 | data_spc = None |
|
402 | 406 | |
|
403 | 407 | #data es un numpy array de 2 dmensiones (canales, pares, alturas) |
|
404 | 408 | data_cspc = None |
|
405 | 409 | |
|
406 | 410 | #data es un numpy array de 2 dmensiones (canales, alturas) |
|
407 | 411 | data_dc = None |
|
408 | 412 | |
|
409 | 413 | nFFTPoints = None |
|
410 | 414 | |
|
411 | 415 | # nPairs = None |
|
412 | 416 | |
|
413 | 417 | pairsList = None |
|
414 | 418 | |
|
415 | 419 | nIncohInt = None |
|
416 | 420 | |
|
417 | 421 | wavelength = None #Necesario para cacular el rango de velocidad desde la frecuencia |
|
418 | 422 | |
|
419 | 423 | nCohInt = None #se requiere para determinar el valor de timeInterval |
|
420 | 424 | |
|
421 | 425 | ippFactor = None |
|
422 | 426 | |
|
427 | profileIndex = 0 | |
|
428 | ||
|
423 | 429 | def __init__(self): |
|
424 | 430 | ''' |
|
425 | 431 | Constructor |
|
426 | 432 | ''' |
|
427 | 433 | |
|
428 | 434 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
429 | 435 | |
|
430 | 436 | self.systemHeaderObj = SystemHeader() |
|
431 | 437 | |
|
432 | 438 | self.type = "Spectra" |
|
433 | 439 | |
|
434 | 440 | # self.data = None |
|
435 | 441 | |
|
436 | 442 | # self.dtype = None |
|
437 | 443 | |
|
438 | 444 | # self.nChannels = 0 |
|
439 | 445 | |
|
440 | 446 | # self.nHeights = 0 |
|
441 | 447 | |
|
442 | 448 | self.nProfiles = None |
|
443 | 449 | |
|
444 | 450 | self.heightList = None |
|
445 | 451 | |
|
446 | 452 | self.channelList = None |
|
447 | 453 | |
|
448 | 454 | # self.channelIndexList = None |
|
449 | 455 | |
|
450 | 456 | self.pairsList = None |
|
451 | 457 | |
|
452 | 458 | self.flagNoData = True |
|
453 | 459 | |
|
454 | 460 | self.flagTimeBlock = False |
|
455 | 461 | |
|
456 | 462 | self.utctime = None |
|
457 | 463 | |
|
458 | 464 | self.nCohInt = None |
|
459 | 465 | |
|
460 | 466 | self.nIncohInt = None |
|
461 | 467 | |
|
462 | 468 | self.blocksize = None |
|
463 | 469 | |
|
464 | 470 | self.nFFTPoints = None |
|
465 | 471 | |
|
466 | 472 | self.wavelength = None |
|
467 | 473 | |
|
468 | 474 | self.flagDecodeData = False #asumo q la data no esta decodificada |
|
469 | 475 | |
|
470 | 476 | self.flagDeflipData = False #asumo q la data no esta sin flip |
|
471 | 477 | |
|
472 | 478 | self.flagShiftFFT = False |
|
473 | 479 | |
|
474 | 480 | self.ippFactor = 1 |
|
475 | 481 | |
|
476 | 482 | #self.noise = None |
|
477 | 483 | |
|
478 | 484 | self.beacon_heiIndexList = [] |
|
479 | 485 | |
|
480 | 486 | self.noise_estimation = None |
|
481 | 487 | |
|
482 | 488 | |
|
483 | 489 | def getNoisebyHildebrand(self): |
|
484 | 490 | """ |
|
485 | 491 | Determino el nivel de ruido usando el metodo Hildebrand-Sekhon |
|
486 | 492 | |
|
487 | 493 | Return: |
|
488 | 494 | noiselevel |
|
489 | 495 | """ |
|
490 | 496 | |
|
491 | 497 | noise = numpy.zeros(self.nChannels) |
|
492 | 498 | for channel in range(self.nChannels): |
|
493 | 499 | daux = self.data_spc[channel,:,:] |
|
494 | 500 | noise[channel] = hildebrand_sekhon(daux, self.nIncohInt) |
|
495 | 501 | |
|
496 | 502 | return noise |
|
497 | 503 | |
|
498 | 504 | def getNoise(self): |
|
499 | 505 | if self.noise_estimation != None: |
|
500 | 506 | return self.noise_estimation #this was estimated by getNoise Operation defined in jroproc_spectra.py |
|
501 | 507 | else: |
|
502 | 508 | noise = self.getNoisebyHildebrand() |
|
503 | 509 | return noise |
|
504 | 510 | |
|
505 | 511 | |
|
506 | 512 | def getFreqRange(self, extrapoints=0): |
|
507 | 513 | |
|
508 | 514 | deltafreq = self.getFmax() / (self.nFFTPoints*self.ippFactor) |
|
509 | 515 | freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2 |
|
510 | 516 | |
|
511 | 517 | return freqrange |
|
512 | 518 | |
|
513 | 519 | def getVelRange(self, extrapoints=0): |
|
514 | 520 | |
|
515 | 521 | deltav = self.getVmax() / (self.nFFTPoints*self.ippFactor) |
|
516 | 522 | velrange = deltav*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltav/2 |
|
517 | 523 | |
|
518 | 524 | return velrange |
|
519 | 525 | |
|
520 | 526 | def getNPairs(self): |
|
521 | 527 | |
|
522 | 528 | return len(self.pairsList) |
|
523 | 529 | |
|
524 | 530 | def getPairsIndexList(self): |
|
525 | 531 | |
|
526 | 532 | return range(self.nPairs) |
|
527 | 533 | |
|
528 | 534 | def getNormFactor(self): |
|
529 | 535 | pwcode = 1 |
|
530 | 536 | if self.flagDecodeData: |
|
531 | 537 | pwcode = numpy.sum(self.code[0]**2) |
|
532 | 538 | #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter |
|
533 | 539 | normFactor = self.nProfiles*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter |
|
534 | 540 | |
|
535 | 541 | return normFactor |
|
536 | 542 | |
|
537 | 543 | def getFlagCspc(self): |
|
538 | 544 | |
|
539 | 545 | if self.data_cspc == None: |
|
540 | 546 | return True |
|
541 | 547 | |
|
542 | 548 | return False |
|
543 | 549 | |
|
544 | 550 | def getFlagDc(self): |
|
545 | 551 | |
|
546 | 552 | if self.data_dc == None: |
|
547 | 553 | return True |
|
548 | 554 | |
|
549 | 555 | return False |
|
550 | 556 | |
|
551 | 557 | def getTimeInterval(self): |
|
552 | 558 | |
|
553 | 559 | timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt * self.nProfiles |
|
554 | 560 | |
|
555 | 561 | return timeInterval |
|
556 | 562 | |
|
557 | 563 | nPairs = property(getNPairs, "I'm the 'nPairs' property.") |
|
558 | 564 | pairsIndexList = property(getPairsIndexList, "I'm the 'pairsIndexList' property.") |
|
559 | 565 | normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.") |
|
560 | 566 | flag_cspc = property(getFlagCspc) |
|
561 | 567 | flag_dc = property(getFlagDc) |
|
562 | 568 | noise = property(getNoise, "I'm the 'nHeights' property.") |
|
563 | 569 | timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property") |
|
564 | 570 | |
|
565 | 571 | class SpectraHeis(Spectra): |
|
566 | 572 | |
|
567 | 573 | data_spc = None |
|
568 | 574 | |
|
569 | 575 | data_cspc = None |
|
570 | 576 | |
|
571 | 577 | data_dc = None |
|
572 | 578 | |
|
573 | 579 | nFFTPoints = None |
|
574 | 580 | |
|
575 | 581 | # nPairs = None |
|
576 | 582 | |
|
577 | 583 | pairsList = None |
|
578 | 584 | |
|
579 | 585 | nIncohInt = None |
|
580 | 586 | |
|
581 | 587 | def __init__(self): |
|
582 | 588 | |
|
583 | 589 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
584 | 590 | |
|
585 | 591 | self.systemHeaderObj = SystemHeader() |
|
586 | 592 | |
|
587 | 593 | self.type = "SpectraHeis" |
|
588 | 594 | |
|
589 | 595 | # self.dtype = None |
|
590 | 596 | |
|
591 | 597 | # self.nChannels = 0 |
|
592 | 598 | |
|
593 | 599 | # self.nHeights = 0 |
|
594 | 600 | |
|
595 | 601 | self.nProfiles = None |
|
596 | 602 | |
|
597 | 603 | self.heightList = None |
|
598 | 604 | |
|
599 | 605 | self.channelList = None |
|
600 | 606 | |
|
601 | 607 | # self.channelIndexList = None |
|
602 | 608 | |
|
603 | 609 | self.flagNoData = True |
|
604 | 610 | |
|
605 | 611 | self.flagTimeBlock = False |
|
606 | 612 | |
|
607 | 613 | # self.nPairs = 0 |
|
608 | 614 | |
|
609 | 615 | self.utctime = None |
|
610 | 616 | |
|
611 | 617 | self.blocksize = None |
|
618 | ||
|
619 | self.profileIndex = 0 | |
|
612 | 620 | |
|
613 | 621 | def getNormFactor(self): |
|
614 | 622 | pwcode = 1 |
|
615 | 623 | if self.flagDecodeData: |
|
616 | 624 | pwcode = numpy.sum(self.code[0]**2) |
|
617 | 625 | |
|
618 | 626 | normFactor = self.nIncohInt*self.nCohInt*pwcode |
|
619 | 627 | |
|
620 | 628 | return normFactor |
|
621 | 629 | |
|
622 | 630 | def getTimeInterval(self): |
|
623 | 631 | |
|
624 | 632 | timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt |
|
625 | 633 | |
|
626 | 634 | return timeInterval |
|
627 | 635 | |
|
628 | 636 | normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.") |
|
629 | 637 | timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property") |
|
630 | 638 | |
|
631 | 639 | class Fits: |
|
632 | 640 | |
|
633 | 641 | heightList = None |
|
634 | 642 | |
|
635 | 643 | channelList = None |
|
636 | 644 | |
|
637 | 645 | flagNoData = True |
|
638 | 646 | |
|
639 | 647 | flagTimeBlock = False |
|
640 | 648 | |
|
641 | 649 | useLocalTime = False |
|
642 | 650 | |
|
643 | 651 | utctime = None |
|
644 | 652 | |
|
645 | 653 | timeZone = None |
|
646 | 654 | |
|
647 | 655 | # ippSeconds = None |
|
648 | 656 | |
|
649 | 657 | # timeInterval = None |
|
650 | 658 | |
|
651 | 659 | nCohInt = None |
|
652 | 660 | |
|
653 | 661 | nIncohInt = None |
|
654 | 662 | |
|
655 | 663 | noise = None |
|
656 | 664 | |
|
657 | 665 | windowOfFilter = 1 |
|
658 | 666 | |
|
659 | 667 | #Speed of ligth |
|
660 | 668 | C = 3e8 |
|
661 | 669 | |
|
662 | 670 | frequency = 49.92e6 |
|
663 | 671 | |
|
664 | 672 | realtime = False |
|
665 | 673 | |
|
666 | 674 | |
|
667 | 675 | def __init__(self): |
|
668 | 676 | |
|
669 | 677 | self.type = "Fits" |
|
670 | 678 | |
|
671 | 679 | self.nProfiles = None |
|
672 | 680 | |
|
673 | 681 | self.heightList = None |
|
674 | 682 | |
|
675 | 683 | self.channelList = None |
|
676 | 684 | |
|
677 | 685 | # self.channelIndexList = None |
|
678 | 686 | |
|
679 | 687 | self.flagNoData = True |
|
680 | 688 | |
|
681 | 689 | self.utctime = None |
|
682 | 690 | |
|
683 | 691 | self.nCohInt = None |
|
684 | 692 | |
|
685 | 693 | self.nIncohInt = None |
|
686 | 694 | |
|
687 | 695 | self.useLocalTime = True |
|
688 | 696 | |
|
697 | self.profileIndex = 0 | |
|
698 | ||
|
689 | 699 | # self.utctime = None |
|
690 | 700 | # self.timeZone = None |
|
691 | 701 | # self.ltctime = None |
|
692 | 702 | # self.timeInterval = None |
|
693 | 703 | # self.header = None |
|
694 | 704 | # self.data_header = None |
|
695 | 705 | # self.data = None |
|
696 | 706 | # self.datatime = None |
|
697 | 707 | # self.flagNoData = False |
|
698 | 708 | # self.expName = '' |
|
699 | 709 | # self.nChannels = None |
|
700 | 710 | # self.nSamples = None |
|
701 | 711 | # self.dataBlocksPerFile = None |
|
702 | 712 | # self.comments = '' |
|
703 | 713 | # |
|
704 | 714 | |
|
705 | 715 | |
|
706 | 716 | def getltctime(self): |
|
707 | 717 | |
|
708 | 718 | if self.useLocalTime: |
|
709 | 719 | return self.utctime - self.timeZone*60 |
|
710 | 720 | |
|
711 | 721 | return self.utctime |
|
712 | 722 | |
|
713 | 723 | def getDatatime(self): |
|
714 | 724 | |
|
715 | 725 | datatime = datetime.datetime.utcfromtimestamp(self.ltctime) |
|
716 | 726 | return datatime |
|
717 | 727 | |
|
718 | 728 | def getTimeRange(self): |
|
719 | 729 | |
|
720 | 730 | datatime = [] |
|
721 | 731 | |
|
722 | 732 | datatime.append(self.ltctime) |
|
723 | 733 | datatime.append(self.ltctime + self.timeInterval) |
|
724 | 734 | |
|
725 | 735 | datatime = numpy.array(datatime) |
|
726 | 736 | |
|
727 | 737 | return datatime |
|
728 | 738 | |
|
729 | 739 | def getHeiRange(self): |
|
730 | 740 | |
|
731 | 741 | heis = self.heightList |
|
732 | 742 | |
|
733 | 743 | return heis |
|
734 | 744 | |
|
735 | 745 | def isEmpty(self): |
|
736 | 746 | |
|
737 | 747 | return self.flagNoData |
|
738 | 748 | |
|
739 | 749 | def getNHeights(self): |
|
740 | 750 | |
|
741 | 751 | return len(self.heightList) |
|
742 | 752 | |
|
743 | 753 | def getNChannels(self): |
|
744 | 754 | |
|
745 | 755 | return len(self.channelList) |
|
746 | 756 | |
|
747 | 757 | def getChannelIndexList(self): |
|
748 | 758 | |
|
749 | 759 | return range(self.nChannels) |
|
750 | 760 | |
|
751 | 761 | def getNoise(self, type = 1): |
|
752 | 762 | |
|
753 | 763 | self.noise = numpy.zeros(self.nChannels) |
|
754 | 764 | |
|
755 | 765 | if type == 1: |
|
756 | 766 | noise = self.getNoisebyHildebrand() |
|
757 | 767 | |
|
758 | 768 | if type == 2: |
|
759 | 769 | noise = self.getNoisebySort() |
|
760 | 770 | |
|
761 | 771 | if type == 3: |
|
762 | 772 | noise = self.getNoisebyWindow() |
|
763 | 773 | |
|
764 | 774 | return noise |
|
765 | 775 | |
|
766 | 776 | def getTimeInterval(self): |
|
767 | 777 | |
|
768 | 778 | raise ValueError, "This method is not implemented yet" |
|
769 | 779 | |
|
770 | 780 | datatime = property(getDatatime, "I'm the 'datatime' property") |
|
771 | 781 | nHeights = property(getNHeights, "I'm the 'nHeights' property.") |
|
772 | 782 | nChannels = property(getNChannels, "I'm the 'nChannel' property.") |
|
773 | 783 | channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.") |
|
774 | 784 | noise = property(getNoise, "I'm the 'nHeights' property.") |
|
775 | 785 | datatime = property(getDatatime, "I'm the 'datatime' property") |
|
776 | 786 | ltctime = property(getltctime, "I'm the 'ltctime' property") |
|
777 | 787 | timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property") |
|
778 | 788 | |
|
779 | 789 | class Correlation(JROData): |
|
780 | 790 | |
|
781 | 791 | noise = None |
|
782 | 792 | |
|
783 | 793 | SNR = None |
|
784 | 794 | |
|
785 | 795 | pairsAutoCorr = None #Pairs of Autocorrelation |
|
786 | 796 | |
|
787 | 797 | #-------------------------------------------------- |
|
788 | 798 | |
|
789 | 799 | data_corr = None |
|
790 | 800 | |
|
791 | 801 | data_volt = None |
|
792 | 802 | |
|
793 | 803 | lagT = None # each element value is a profileIndex |
|
794 | 804 | |
|
795 | 805 | lagR = None # each element value is in km |
|
796 | 806 | |
|
797 | 807 | pairsList = None |
|
798 | 808 | |
|
799 | 809 | calculateVelocity = None |
|
800 | 810 | |
|
801 | 811 | nPoints = None |
|
802 | 812 | |
|
803 | 813 | nAvg = None |
|
804 | 814 | |
|
805 | 815 | bufferSize = None |
|
806 | 816 | |
|
807 | 817 | def __init__(self): |
|
808 | 818 | ''' |
|
809 | 819 | Constructor |
|
810 | 820 | ''' |
|
811 | 821 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
812 | 822 | |
|
813 | 823 | self.systemHeaderObj = SystemHeader() |
|
814 | 824 | |
|
815 | 825 | self.type = "Correlation" |
|
816 | 826 | |
|
817 | 827 | self.data = None |
|
818 | 828 | |
|
819 | 829 | self.dtype = None |
|
820 | 830 | |
|
821 | 831 | self.nProfiles = None |
|
822 | 832 | |
|
823 | 833 | self.heightList = None |
|
824 | 834 | |
|
825 | 835 | self.channelList = None |
|
826 | 836 | |
|
827 | 837 | self.flagNoData = True |
|
828 | 838 | |
|
829 | 839 | self.flagTimeBlock = False |
|
830 | 840 | |
|
831 | 841 | self.utctime = None |
|
832 | 842 | |
|
833 | 843 | self.timeZone = None |
|
834 | 844 | |
|
835 | 845 | self.dstFlag = None |
|
836 | 846 | |
|
837 | 847 | self.errorCount = None |
|
838 | 848 | |
|
839 | 849 | self.blocksize = None |
|
840 | 850 | |
|
841 | 851 | self.flagDecodeData = False #asumo q la data no esta decodificada |
|
842 | 852 | |
|
843 | 853 | self.flagDeflipData = False #asumo q la data no esta sin flip |
|
844 | 854 | |
|
845 | 855 | self.pairsList = None |
|
846 | 856 | |
|
847 | 857 | self.nPoints = None |
|
848 | 858 | |
|
849 | 859 | def getLagTRange(self, extrapoints=0): |
|
850 | 860 | |
|
851 | 861 | lagTRange = self.lagT |
|
852 | 862 | diff = lagTRange[1] - lagTRange[0] |
|
853 | 863 | extra = numpy.arange(1,extrapoints + 1)*diff + lagTRange[-1] |
|
854 | 864 | lagTRange = numpy.hstack((lagTRange, extra)) |
|
855 | 865 | |
|
856 | 866 | return lagTRange |
|
857 | 867 | |
|
858 | 868 | def getLagRRange(self, extrapoints=0): |
|
859 | 869 | |
|
860 | 870 | return self.lagR |
|
861 | 871 | |
|
862 | 872 | def getPairsList(self): |
|
863 | 873 | |
|
864 | 874 | return self.pairsList |
|
865 | 875 | |
|
866 | 876 | def getCalculateVelocity(self): |
|
867 | 877 | |
|
868 | 878 | return self.calculateVelocity |
|
869 | 879 | |
|
870 | 880 | def getNPoints(self): |
|
871 | 881 | |
|
872 | 882 | return self.nPoints |
|
873 | 883 | |
|
874 | 884 | def getNAvg(self): |
|
875 | 885 | |
|
876 | 886 | return self.nAvg |
|
877 | 887 | |
|
878 | 888 | def getBufferSize(self): |
|
879 | 889 | |
|
880 | 890 | return self.bufferSize |
|
881 | 891 | |
|
882 | 892 | def getPairsAutoCorr(self): |
|
883 | 893 | pairsList = self.pairsList |
|
884 | 894 | pairsAutoCorr = numpy.zeros(self.nChannels, dtype = 'int')*numpy.nan |
|
885 | 895 | |
|
886 | 896 | for l in range(len(pairsList)): |
|
887 | 897 | firstChannel = pairsList[l][0] |
|
888 | 898 | secondChannel = pairsList[l][1] |
|
889 | 899 | |
|
890 | 900 | #Obteniendo pares de Autocorrelacion |
|
891 | 901 | if firstChannel == secondChannel: |
|
892 | 902 | pairsAutoCorr[firstChannel] = int(l) |
|
893 | 903 | |
|
894 | 904 | pairsAutoCorr = pairsAutoCorr.astype(int) |
|
895 | 905 | |
|
896 | 906 | return pairsAutoCorr |
|
897 | 907 | |
|
898 | 908 | def getNoise(self, mode = 2): |
|
899 | 909 | |
|
900 | 910 | indR = numpy.where(self.lagR == 0)[0][0] |
|
901 | 911 | indT = numpy.where(self.lagT == 0)[0][0] |
|
902 | 912 | |
|
903 | 913 | jspectra0 = self.data_corr[:,:,indR,:] |
|
904 | 914 | jspectra = copy.copy(jspectra0) |
|
905 | 915 | |
|
906 | 916 | num_chan = jspectra.shape[0] |
|
907 | 917 | num_hei = jspectra.shape[2] |
|
908 | 918 | |
|
909 | 919 | freq_dc = jspectra.shape[1]/2 |
|
910 | 920 | ind_vel = numpy.array([-2,-1,1,2]) + freq_dc |
|
911 | 921 | |
|
912 | 922 | if ind_vel[0]<0: |
|
913 | 923 | ind_vel[range(0,1)] = ind_vel[range(0,1)] + self.num_prof |
|
914 | 924 | |
|
915 | 925 | if mode == 1: |
|
916 | 926 | jspectra[:,freq_dc,:] = (jspectra[:,ind_vel[1],:] + jspectra[:,ind_vel[2],:])/2 #CORRECCION |
|
917 | 927 | |
|
918 | 928 | if mode == 2: |
|
919 | 929 | |
|
920 | 930 | vel = numpy.array([-2,-1,1,2]) |
|
921 | 931 | xx = numpy.zeros([4,4]) |
|
922 | 932 | |
|
923 | 933 | for fil in range(4): |
|
924 | 934 | xx[fil,:] = vel[fil]**numpy.asarray(range(4)) |
|
925 | 935 | |
|
926 | 936 | xx_inv = numpy.linalg.inv(xx) |
|
927 | 937 | xx_aux = xx_inv[0,:] |
|
928 | 938 | |
|
929 | 939 | for ich in range(num_chan): |
|
930 | 940 | yy = jspectra[ich,ind_vel,:] |
|
931 | 941 | jspectra[ich,freq_dc,:] = numpy.dot(xx_aux,yy) |
|
932 | 942 | |
|
933 | 943 | junkid = jspectra[ich,freq_dc,:]<=0 |
|
934 | 944 | cjunkid = sum(junkid) |
|
935 | 945 | |
|
936 | 946 | if cjunkid.any(): |
|
937 | 947 | jspectra[ich,freq_dc,junkid.nonzero()] = (jspectra[ich,ind_vel[1],junkid] + jspectra[ich,ind_vel[2],junkid])/2 |
|
938 | 948 | |
|
939 | 949 | noise = jspectra0[:,freq_dc,:] - jspectra[:,freq_dc,:] |
|
940 | 950 | |
|
941 | 951 | return noise |
|
942 | 952 | |
|
943 | 953 | # pairsList = property(getPairsList, "I'm the 'pairsList' property.") |
|
944 | 954 | # nPoints = property(getNPoints, "I'm the 'nPoints' property.") |
|
945 | 955 | calculateVelocity = property(getCalculateVelocity, "I'm the 'calculateVelocity' property.") |
|
946 | 956 | nAvg = property(getNAvg, "I'm the 'nAvg' property.") |
|
947 | 957 | bufferSize = property(getBufferSize, "I'm the 'bufferSize' property.") |
|
948 | 958 | |
|
949 | 959 | |
|
950 | 960 | class Parameters(JROData): |
|
951 | 961 | |
|
952 | 962 | #Information from previous data |
|
953 | 963 | |
|
954 | 964 | inputUnit = None #Type of data to be processed |
|
955 | 965 | |
|
956 | 966 | operation = None #Type of operation to parametrize |
|
957 | 967 | |
|
958 | 968 | normFactor = None #Normalization Factor |
|
959 | 969 | |
|
960 | 970 | groupList = None #List of Pairs, Groups, etc |
|
961 | 971 | |
|
962 | 972 | #Parameters |
|
963 | 973 | |
|
964 | 974 | data_param = None #Parameters obtained |
|
965 | 975 | |
|
966 | 976 | data_pre = None #Data Pre Parametrization |
|
967 | 977 | |
|
968 | 978 | data_SNR = None #Signal to Noise Ratio |
|
969 | 979 | |
|
970 | 980 | heightRange = None #Heights |
|
971 | 981 | |
|
972 | 982 | abscissaRange = None #Abscissa, can be velocities, lags or time |
|
973 | 983 | |
|
974 | 984 | noise = None #Noise Potency |
|
975 | 985 | |
|
976 | 986 | initUtcTime = None #Initial UTC time |
|
977 | 987 | |
|
978 | 988 | paramInterval = None #Time interval to calculate Parameters in seconds |
|
979 | 989 | |
|
980 | 990 | #Fitting |
|
981 | 991 | |
|
982 | 992 | data_error = None #Error of the estimation |
|
983 | 993 | |
|
984 | 994 | constants = None |
|
985 | 995 | |
|
986 | 996 | library = None |
|
987 | 997 | |
|
988 | 998 | #Output signal |
|
989 | 999 | |
|
990 | 1000 | outputInterval = None #Time interval to calculate output signal in seconds |
|
991 | 1001 | |
|
992 | 1002 | data_output = None #Out signal |
|
993 | 1003 | |
|
994 | 1004 | |
|
995 | 1005 | |
|
996 | 1006 | def __init__(self): |
|
997 | 1007 | ''' |
|
998 | 1008 | Constructor |
|
999 | 1009 | ''' |
|
1000 | 1010 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
1001 | 1011 | |
|
1002 | 1012 | self.systemHeaderObj = SystemHeader() |
|
1003 | 1013 | |
|
1004 | 1014 | self.type = "Parameters" |
|
1005 | 1015 | |
|
1006 | 1016 | def getTimeRange1(self): |
|
1007 | 1017 | |
|
1008 | 1018 | datatime = [] |
|
1009 | 1019 | |
|
1010 | 1020 | datatime.append(self.initUtcTime) |
|
1011 | 1021 | datatime.append(self.initUtcTime + self.outputInterval - 1) |
|
1012 | 1022 | |
|
1013 | 1023 | datatime = numpy.array(datatime) |
|
1014 | 1024 | |
|
1015 | 1025 | return datatime |
@@ -1,1338 +1,1344 | |||
|
1 | 1 | ''' |
|
2 | 2 | |
|
3 | 3 | ''' |
|
4 | 4 | import os |
|
5 | 5 | import sys |
|
6 | 6 | import glob |
|
7 | 7 | import time |
|
8 | 8 | import numpy |
|
9 | 9 | import fnmatch |
|
10 | 10 | import time, datetime |
|
11 | 11 | #import h5py |
|
12 | 12 | import traceback |
|
13 | 13 | |
|
14 | 14 | #try: |
|
15 | 15 | # import pyfits |
|
16 | 16 | #except: |
|
17 | 17 | # print "pyfits module has not been imported, it should be installed to save files in fits format" |
|
18 | 18 | |
|
19 | 19 | #from jrodata import * |
|
20 | 20 | #from jroheaderIO import * |
|
21 | 21 | #from jroprocessing import * |
|
22 | 22 | |
|
23 | 23 | #import re |
|
24 | 24 | #from xml.etree.ElementTree import Element, SubElement, ElementTree |
|
25 | 25 | |
|
26 | 26 | |
|
27 | 27 | LOCALTIME = True #-18000 |
|
28 | 28 | |
|
29 | 29 | from model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader |
|
30 | 30 | |
|
31 | 31 | def isNumber(str): |
|
32 | 32 | """ |
|
33 | 33 | Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. |
|
34 | 34 | |
|
35 | 35 | Excepciones: |
|
36 | 36 | Si un determinado string no puede ser convertido a numero |
|
37 | 37 | Input: |
|
38 | 38 | str, string al cual se le analiza para determinar si convertible a un numero o no |
|
39 | 39 | |
|
40 | 40 | Return: |
|
41 | 41 | True : si el string es uno numerico |
|
42 | 42 | False : no es un string numerico |
|
43 | 43 | """ |
|
44 | 44 | try: |
|
45 | 45 | float( str ) |
|
46 | 46 | return True |
|
47 | 47 | except: |
|
48 | 48 | return False |
|
49 | 49 | |
|
50 | 50 | def isThisFileinRange(filename, startUTSeconds, endUTSeconds): |
|
51 | 51 | """ |
|
52 | 52 | Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado. |
|
53 | 53 | |
|
54 | 54 | Inputs: |
|
55 | 55 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
56 | 56 | |
|
57 | 57 | startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en |
|
58 | 58 | segundos contados desde 01/01/1970. |
|
59 | 59 | endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en |
|
60 | 60 | segundos contados desde 01/01/1970. |
|
61 | 61 | |
|
62 | 62 | Return: |
|
63 | 63 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
64 | 64 | fecha especificado, de lo contrario retorna False. |
|
65 | 65 | |
|
66 | 66 | Excepciones: |
|
67 | 67 | Si el archivo no existe o no puede ser abierto |
|
68 | 68 | Si la cabecera no puede ser leida. |
|
69 | 69 | |
|
70 | 70 | """ |
|
71 | 71 | basicHeaderObj = BasicHeader(LOCALTIME) |
|
72 | 72 | |
|
73 | 73 | try: |
|
74 | 74 | fp = open(filename,'rb') |
|
75 | 75 | except IOError: |
|
76 | 76 | traceback.print_exc() |
|
77 | 77 | raise IOError, "The file %s can't be opened" %(filename) |
|
78 | 78 | |
|
79 | 79 | sts = basicHeaderObj.read(fp) |
|
80 | 80 | fp.close() |
|
81 | 81 | |
|
82 | 82 | if not(sts): |
|
83 | 83 | print "Skipping the file %s because it has not a valid header" %(filename) |
|
84 | 84 | return 0 |
|
85 | 85 | |
|
86 | 86 | if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)): |
|
87 | 87 | return 0 |
|
88 | 88 | |
|
89 | 89 | return 1 |
|
90 | 90 | |
|
91 | 91 | def isFileinThisTime(filename, startTime, endTime): |
|
92 | 92 | """ |
|
93 | 93 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. |
|
94 | 94 | |
|
95 | 95 | Inputs: |
|
96 | 96 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
97 | 97 | |
|
98 | 98 | startTime : tiempo inicial del rango seleccionado en formato datetime.time |
|
99 | 99 | |
|
100 | 100 | endTime : tiempo final del rango seleccionado en formato datetime.time |
|
101 | 101 | |
|
102 | 102 | Return: |
|
103 | 103 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
104 | 104 | fecha especificado, de lo contrario retorna False. |
|
105 | 105 | |
|
106 | 106 | Excepciones: |
|
107 | 107 | Si el archivo no existe o no puede ser abierto |
|
108 | 108 | Si la cabecera no puede ser leida. |
|
109 | 109 | |
|
110 | 110 | """ |
|
111 | 111 | |
|
112 | 112 | |
|
113 | 113 | try: |
|
114 | 114 | fp = open(filename,'rb') |
|
115 | 115 | except IOError: |
|
116 | 116 | traceback.print_exc() |
|
117 | 117 | raise IOError, "The file %s can't be opened" %(filename) |
|
118 | 118 | |
|
119 | 119 | basicHeaderObj = BasicHeader(LOCALTIME) |
|
120 | 120 | sts = basicHeaderObj.read(fp) |
|
121 | 121 | fp.close() |
|
122 | 122 | |
|
123 | 123 | thisDatetime = basicHeaderObj.datatime |
|
124 | 124 | thisTime = thisDatetime.time() |
|
125 | 125 | |
|
126 | 126 | if not(sts): |
|
127 | 127 | print "Skipping the file %s because it has not a valid header" %(filename) |
|
128 | 128 | return None |
|
129 | 129 | |
|
130 | 130 | if not ((startTime <= thisTime) and (endTime > thisTime)): |
|
131 | 131 | return None |
|
132 | 132 | |
|
133 | 133 | return thisDatetime |
|
134 | 134 | |
|
135 | 135 | def getFileFromSet(path, ext, set): |
|
136 | 136 | validFilelist = [] |
|
137 | 137 | fileList = os.listdir(path) |
|
138 | 138 | |
|
139 | 139 | # 0 1234 567 89A BCDE |
|
140 | 140 | # H YYYY DDD SSS .ext |
|
141 | 141 | |
|
142 | 142 | for thisFile in fileList: |
|
143 | 143 | try: |
|
144 | 144 | year = int(thisFile[1:5]) |
|
145 | 145 | doy = int(thisFile[5:8]) |
|
146 | 146 | except: |
|
147 | 147 | continue |
|
148 | 148 | |
|
149 | 149 | if (os.path.splitext(thisFile)[-1].lower() != ext.lower()): |
|
150 | 150 | continue |
|
151 | 151 | |
|
152 | 152 | validFilelist.append(thisFile) |
|
153 | 153 | |
|
154 | 154 | myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set)) |
|
155 | 155 | |
|
156 | 156 | if len(myfile)!= 0: |
|
157 | 157 | return myfile[0] |
|
158 | 158 | else: |
|
159 | 159 | filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower()) |
|
160 | 160 | print 'the filename %s does not exist'%filename |
|
161 | 161 | print '...going to the last file: ' |
|
162 | 162 | |
|
163 | 163 | if validFilelist: |
|
164 | 164 | validFilelist = sorted( validFilelist, key=str.lower ) |
|
165 | 165 | return validFilelist[-1] |
|
166 | 166 | |
|
167 | 167 | return None |
|
168 | 168 | |
|
169 | 169 | def getlastFileFromPath(path, ext): |
|
170 | 170 | """ |
|
171 | 171 | Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext" |
|
172 | 172 | al final de la depuracion devuelve el ultimo file de la lista que quedo. |
|
173 | 173 | |
|
174 | 174 | Input: |
|
175 | 175 | fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta |
|
176 | 176 | ext : extension de los files contenidos en una carpeta |
|
177 | 177 | |
|
178 | 178 | Return: |
|
179 | 179 | El ultimo file de una determinada carpeta, no se considera el path. |
|
180 | 180 | """ |
|
181 | 181 | validFilelist = [] |
|
182 | 182 | fileList = os.listdir(path) |
|
183 | 183 | |
|
184 | 184 | # 0 1234 567 89A BCDE |
|
185 | 185 | # H YYYY DDD SSS .ext |
|
186 | 186 | |
|
187 | 187 | for thisFile in fileList: |
|
188 | 188 | |
|
189 | 189 | year = thisFile[1:5] |
|
190 | 190 | if not isNumber(year): |
|
191 | 191 | continue |
|
192 | 192 | |
|
193 | 193 | doy = thisFile[5:8] |
|
194 | 194 | if not isNumber(doy): |
|
195 | 195 | continue |
|
196 | 196 | |
|
197 | 197 | year = int(year) |
|
198 | 198 | doy = int(doy) |
|
199 | 199 | |
|
200 | 200 | if (os.path.splitext(thisFile)[-1].lower() != ext.lower()): |
|
201 | 201 | continue |
|
202 | 202 | |
|
203 | 203 | validFilelist.append(thisFile) |
|
204 | 204 | |
|
205 | 205 | if validFilelist: |
|
206 | 206 | validFilelist = sorted( validFilelist, key=str.lower ) |
|
207 | 207 | return validFilelist[-1] |
|
208 | 208 | |
|
209 | 209 | return None |
|
210 | 210 | |
|
211 | 211 | def checkForRealPath(path, foldercounter, year, doy, set, ext): |
|
212 | 212 | """ |
|
213 | 213 | Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path, |
|
214 | 214 | Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar |
|
215 | 215 | el path exacto de un determinado file. |
|
216 | 216 | |
|
217 | 217 | Example : |
|
218 | 218 | nombre correcto del file es .../.../D2009307/P2009307367.ext |
|
219 | 219 | |
|
220 | 220 | Entonces la funcion prueba con las siguientes combinaciones |
|
221 | 221 | .../.../y2009307367.ext |
|
222 | 222 | .../.../Y2009307367.ext |
|
223 | 223 | .../.../x2009307/y2009307367.ext |
|
224 | 224 | .../.../x2009307/Y2009307367.ext |
|
225 | 225 | .../.../X2009307/y2009307367.ext |
|
226 | 226 | .../.../X2009307/Y2009307367.ext |
|
227 | 227 | siendo para este caso, la ultima combinacion de letras, identica al file buscado |
|
228 | 228 | |
|
229 | 229 | Return: |
|
230 | 230 | Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file |
|
231 | 231 | caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas |
|
232 | 232 | para el filename |
|
233 | 233 | """ |
|
234 | 234 | fullfilename = None |
|
235 | 235 | find_flag = False |
|
236 | 236 | filename = None |
|
237 | 237 | |
|
238 | 238 | prefixDirList = [None,'d','D'] |
|
239 | 239 | if ext.lower() == ".r": #voltage |
|
240 | 240 | prefixFileList = ['d','D'] |
|
241 | 241 | elif ext.lower() == ".pdata": #spectra |
|
242 | 242 | prefixFileList = ['p','P'] |
|
243 | 243 | else: |
|
244 | 244 | return None, filename |
|
245 | 245 | |
|
246 | 246 | #barrido por las combinaciones posibles |
|
247 | 247 | for prefixDir in prefixDirList: |
|
248 | 248 | thispath = path |
|
249 | 249 | if prefixDir != None: |
|
250 | 250 | #formo el nombre del directorio xYYYYDDD (x=d o x=D) |
|
251 | 251 | if foldercounter == 0: |
|
252 | 252 | thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy )) |
|
253 | 253 | else: |
|
254 | 254 | thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter)) |
|
255 | 255 | for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D" |
|
256 | 256 | filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext |
|
257 | 257 | fullfilename = os.path.join( thispath, filename ) #formo el path completo |
|
258 | 258 | |
|
259 | 259 | if os.path.exists( fullfilename ): #verifico que exista |
|
260 | 260 | find_flag = True |
|
261 | 261 | break |
|
262 | 262 | if find_flag: |
|
263 | 263 | break |
|
264 | 264 | |
|
265 | 265 | if not(find_flag): |
|
266 | 266 | return None, filename |
|
267 | 267 | |
|
268 | 268 | return fullfilename, filename |
|
269 | 269 | |
|
270 | 270 | def isDoyFolder(folder): |
|
271 | 271 | try: |
|
272 | 272 | year = int(folder[1:5]) |
|
273 | 273 | except: |
|
274 | 274 | return 0 |
|
275 | 275 | |
|
276 | 276 | try: |
|
277 | 277 | doy = int(folder[5:8]) |
|
278 | 278 | except: |
|
279 | 279 | return 0 |
|
280 | 280 | |
|
281 | 281 | return 1 |
|
282 | 282 | |
|
283 | 283 | class JRODataIO: |
|
284 | 284 | |
|
285 | 285 | c = 3E8 |
|
286 | 286 | |
|
287 | 287 | isConfig = False |
|
288 | 288 | |
|
289 | 289 | basicHeaderObj = None |
|
290 | 290 | |
|
291 | 291 | systemHeaderObj = None |
|
292 | 292 | |
|
293 | 293 | radarControllerHeaderObj = None |
|
294 | 294 | |
|
295 | 295 | processingHeaderObj = None |
|
296 | 296 | |
|
297 | 297 | online = 0 |
|
298 | 298 | |
|
299 | 299 | dtype = None |
|
300 | 300 | |
|
301 | 301 | pathList = [] |
|
302 | 302 | |
|
303 | 303 | filenameList = [] |
|
304 | 304 | |
|
305 | 305 | filename = None |
|
306 | 306 | |
|
307 | 307 | ext = None |
|
308 | 308 | |
|
309 | 309 | flagIsNewFile = 1 |
|
310 | 310 | |
|
311 | 311 | flagTimeBlock = 0 |
|
312 | 312 | |
|
313 | 313 | flagIsNewBlock = 0 |
|
314 | 314 | |
|
315 | 315 | fp = None |
|
316 | 316 | |
|
317 | 317 | firstHeaderSize = 0 |
|
318 | 318 | |
|
319 | 319 | basicHeaderSize = 24 |
|
320 | 320 | |
|
321 | 321 | versionFile = 1103 |
|
322 | 322 | |
|
323 | 323 | fileSize = None |
|
324 | 324 | |
|
325 | 325 | # ippSeconds = None |
|
326 | 326 | |
|
327 | 327 | fileSizeByHeader = None |
|
328 | 328 | |
|
329 | 329 | fileIndex = None |
|
330 | 330 | |
|
331 | 331 | profileIndex = None |
|
332 | 332 | |
|
333 | 333 | blockIndex = None |
|
334 | 334 | |
|
335 | 335 | nTotalBlocks = None |
|
336 | 336 | |
|
337 | 337 | maxTimeStep = 30 |
|
338 | 338 | |
|
339 | 339 | lastUTTime = None |
|
340 | 340 | |
|
341 | 341 | datablock = None |
|
342 | 342 | |
|
343 | 343 | dataOut = None |
|
344 | 344 | |
|
345 | 345 | blocksize = None |
|
346 | 346 | |
|
347 | 347 | getByBlock = False |
|
348 | 348 | |
|
349 | 349 | def __init__(self): |
|
350 | 350 | |
|
351 | 351 | raise ValueError, "Not implemented" |
|
352 | 352 | |
|
353 | 353 | def run(self): |
|
354 | 354 | |
|
355 | 355 | raise ValueError, "Not implemented" |
|
356 | 356 | |
|
357 | 357 | class JRODataReader(JRODataIO): |
|
358 | 358 | |
|
359 | 359 | nReadBlocks = 0 |
|
360 | 360 | |
|
361 | 361 | delay = 10 #number of seconds waiting a new file |
|
362 | 362 | |
|
363 | 363 | nTries = 3 #quantity tries |
|
364 | 364 | |
|
365 | 365 | nFiles = 3 #number of files for searching |
|
366 | 366 | |
|
367 | 367 | path = None |
|
368 | 368 | |
|
369 | 369 | foldercounter = 0 |
|
370 | 370 | |
|
371 | 371 | flagNoMoreFiles = 0 |
|
372 | 372 | |
|
373 | 373 | datetimeList = [] |
|
374 | 374 | |
|
375 | 375 | __isFirstTimeOnline = 1 |
|
376 | 376 | |
|
377 | 377 | __printInfo = True |
|
378 | 378 | |
|
379 | 379 | profileIndex = None |
|
380 | 380 | |
|
381 | nTxs = 1 | |
|
382 | ||
|
383 | txIndex = None | |
|
384 | ||
|
381 | 385 | def __init__(self): |
|
382 | 386 | |
|
383 | 387 | """ |
|
384 | 388 | |
|
385 | 389 | """ |
|
386 | 390 | |
|
387 | 391 | raise ValueError, "This method has not been implemented" |
|
388 | 392 | |
|
389 | 393 | |
|
390 | 394 | def createObjByDefault(self): |
|
391 | 395 | """ |
|
392 | 396 | |
|
393 | 397 | """ |
|
394 | 398 | raise ValueError, "This method has not been implemented" |
|
395 | 399 | |
|
396 | 400 | def getBlockDimension(self): |
|
397 | 401 | |
|
398 | 402 | raise ValueError, "No implemented" |
|
399 | 403 | |
|
400 | 404 | def __searchFilesOffLine(self, |
|
401 | 405 | path, |
|
402 | 406 | startDate, |
|
403 | 407 | endDate, |
|
404 | 408 | startTime=datetime.time(0,0,0), |
|
405 | 409 | endTime=datetime.time(23,59,59), |
|
406 | 410 | set=None, |
|
407 | 411 | expLabel='', |
|
408 | 412 | ext='.r', |
|
409 | 413 | walk=True): |
|
410 | 414 | |
|
411 | 415 | pathList = [] |
|
412 | 416 | |
|
413 | 417 | if not walk: |
|
414 | 418 | #pathList.append(path) |
|
415 | 419 | multi_path = path.split(',') |
|
416 | 420 | for single_path in multi_path: |
|
417 | 421 | pathList.append(single_path) |
|
418 | 422 | |
|
419 | 423 | else: |
|
420 | 424 | #dirList = [] |
|
421 | 425 | multi_path = path.split(',') |
|
422 | 426 | for single_path in multi_path: |
|
423 | 427 | dirList = [] |
|
424 | 428 | for thisPath in os.listdir(single_path): |
|
425 | 429 | if not os.path.isdir(os.path.join(single_path,thisPath)): |
|
426 | 430 | continue |
|
427 | 431 | if not isDoyFolder(thisPath): |
|
428 | 432 | continue |
|
429 | 433 | |
|
430 | 434 | dirList.append(thisPath) |
|
431 | 435 | |
|
432 | 436 | if not(dirList): |
|
433 | 437 | return None, None |
|
434 | 438 | |
|
435 | 439 | thisDate = startDate |
|
436 | 440 | |
|
437 | 441 | while(thisDate <= endDate): |
|
438 | 442 | year = thisDate.timetuple().tm_year |
|
439 | 443 | doy = thisDate.timetuple().tm_yday |
|
440 | 444 | |
|
441 | 445 | matchlist = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy) + '*') |
|
442 | 446 | if len(matchlist) == 0: |
|
443 | 447 | thisDate += datetime.timedelta(1) |
|
444 | 448 | continue |
|
445 | 449 | for match in matchlist: |
|
446 | 450 | pathList.append(os.path.join(single_path,match,expLabel)) |
|
447 | 451 | |
|
448 | 452 | thisDate += datetime.timedelta(1) |
|
449 | 453 | |
|
450 | 454 | if pathList == []: |
|
451 | 455 | print "Any folder was found for the date range: %s-%s" %(startDate, endDate) |
|
452 | 456 | return None, None |
|
453 | 457 | |
|
454 | 458 | print "%d folder(s) was(were) found for the date range: %s - %s" %(len(pathList), startDate, endDate) |
|
455 | 459 | |
|
456 | 460 | filenameList = [] |
|
457 | 461 | datetimeList = [] |
|
458 | 462 | pathDict = {} |
|
459 | 463 | filenameList_to_sort = [] |
|
460 | 464 | |
|
461 | 465 | for i in range(len(pathList)): |
|
462 | 466 | |
|
463 | 467 | thisPath = pathList[i] |
|
464 | 468 | |
|
465 | 469 | fileList = glob.glob1(thisPath, "*%s" %ext) |
|
466 | 470 | fileList.sort() |
|
467 | 471 | pathDict.setdefault(fileList[0]) |
|
468 | 472 | pathDict[fileList[0]] = i |
|
469 | 473 | filenameList_to_sort.append(fileList[0]) |
|
470 | 474 | |
|
471 | 475 | filenameList_to_sort.sort() |
|
472 | 476 | |
|
473 | 477 | for file in filenameList_to_sort: |
|
474 | 478 | thisPath = pathList[pathDict[file]] |
|
475 | 479 | |
|
476 | 480 | fileList = glob.glob1(thisPath, "*%s" %ext) |
|
477 | 481 | fileList.sort() |
|
478 | 482 | |
|
479 | 483 | for file in fileList: |
|
480 | 484 | |
|
481 | 485 | filename = os.path.join(thisPath,file) |
|
482 | 486 | thisDatetime = isFileinThisTime(filename, startTime, endTime) |
|
483 | 487 | |
|
484 | 488 | if not(thisDatetime): |
|
485 | 489 | continue |
|
486 | 490 | |
|
487 | 491 | filenameList.append(filename) |
|
488 | 492 | datetimeList.append(thisDatetime) |
|
489 | 493 | |
|
490 | 494 | if not(filenameList): |
|
491 | 495 | print "Any file was found for the time range %s - %s" %(startTime, endTime) |
|
492 | 496 | return None, None |
|
493 | 497 | |
|
494 | 498 | print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime) |
|
495 | 499 | |
|
496 | 500 | |
|
497 | 501 | for i in range(len(filenameList)): |
|
498 | 502 | print "%s -> [%s]" %(filenameList[i], datetimeList[i].ctime()) |
|
499 | 503 | |
|
500 | 504 | self.filenameList = filenameList |
|
501 | 505 | self.datetimeList = datetimeList |
|
502 | 506 | |
|
503 | 507 | return pathList, filenameList |
|
504 | 508 | |
|
505 | 509 | def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None): |
|
506 | 510 | |
|
507 | 511 | """ |
|
508 | 512 | Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y |
|
509 | 513 | devuelve el archivo encontrado ademas de otros datos. |
|
510 | 514 | |
|
511 | 515 | Input: |
|
512 | 516 | path : carpeta donde estan contenidos los files que contiene data |
|
513 | 517 | |
|
514 | 518 | expLabel : Nombre del subexperimento (subfolder) |
|
515 | 519 | |
|
516 | 520 | ext : extension de los files |
|
517 | 521 | |
|
518 | 522 | walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath) |
|
519 | 523 | |
|
520 | 524 | Return: |
|
521 | 525 | directory : eL directorio donde esta el file encontrado |
|
522 | 526 | filename : el ultimo file de una determinada carpeta |
|
523 | 527 | year : el anho |
|
524 | 528 | doy : el numero de dia del anho |
|
525 | 529 | set : el set del archivo |
|
526 | 530 | |
|
527 | 531 | |
|
528 | 532 | """ |
|
529 | 533 | dirList = [] |
|
530 | 534 | |
|
531 | 535 | if not walk: |
|
532 | 536 | fullpath = path |
|
533 | 537 | foldercounter = 0 |
|
534 | 538 | else: |
|
535 | 539 | #Filtra solo los directorios |
|
536 | 540 | for thisPath in os.listdir(path): |
|
537 | 541 | if not os.path.isdir(os.path.join(path,thisPath)): |
|
538 | 542 | continue |
|
539 | 543 | if not isDoyFolder(thisPath): |
|
540 | 544 | continue |
|
541 | 545 | |
|
542 | 546 | dirList.append(thisPath) |
|
543 | 547 | |
|
544 | 548 | if not(dirList): |
|
545 | 549 | return None, None, None, None, None, None |
|
546 | 550 | |
|
547 | 551 | dirList = sorted( dirList, key=str.lower ) |
|
548 | 552 | |
|
549 | 553 | doypath = dirList[-1] |
|
550 | 554 | foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0 |
|
551 | 555 | fullpath = os.path.join(path, doypath, expLabel) |
|
552 | 556 | |
|
553 | 557 | |
|
554 | 558 | print "%s folder was found: " %(fullpath ) |
|
555 | 559 | |
|
556 | 560 | if set == None: |
|
557 | 561 | filename = getlastFileFromPath(fullpath, ext) |
|
558 | 562 | else: |
|
559 | 563 | filename = getFileFromSet(fullpath, ext, set) |
|
560 | 564 | |
|
561 | 565 | if not(filename): |
|
562 | 566 | return None, None, None, None, None, None |
|
563 | 567 | |
|
564 | 568 | print "%s file was found" %(filename) |
|
565 | 569 | |
|
566 | 570 | if not(self.__verifyFile(os.path.join(fullpath, filename))): |
|
567 | 571 | return None, None, None, None, None, None |
|
568 | 572 | |
|
569 | 573 | year = int( filename[1:5] ) |
|
570 | 574 | doy = int( filename[5:8] ) |
|
571 | 575 | set = int( filename[8:11] ) |
|
572 | 576 | |
|
573 | 577 | return fullpath, foldercounter, filename, year, doy, set |
|
574 | 578 | |
|
575 | 579 | def __setNextFileOffline(self): |
|
576 | 580 | |
|
577 | 581 | idFile = self.fileIndex |
|
578 | 582 | |
|
579 | 583 | while (True): |
|
580 | 584 | idFile += 1 |
|
581 | 585 | if not(idFile < len(self.filenameList)): |
|
582 | 586 | self.flagNoMoreFiles = 1 |
|
583 | 587 | print "No more Files" |
|
584 | 588 | return 0 |
|
585 | 589 | |
|
586 | 590 | filename = self.filenameList[idFile] |
|
587 | 591 | |
|
588 | 592 | if not(self.__verifyFile(filename)): |
|
589 | 593 | continue |
|
590 | 594 | |
|
591 | 595 | fileSize = os.path.getsize(filename) |
|
592 | 596 | fp = open(filename,'rb') |
|
593 | 597 | break |
|
594 | 598 | |
|
595 | 599 | self.flagIsNewFile = 1 |
|
596 | 600 | self.fileIndex = idFile |
|
597 | 601 | self.filename = filename |
|
598 | 602 | self.fileSize = fileSize |
|
599 | 603 | self.fp = fp |
|
600 | 604 | |
|
601 | 605 | print "Setting the file: %s"%self.filename |
|
602 | 606 | |
|
603 | 607 | return 1 |
|
604 | 608 | |
|
605 | 609 | def __setNextFileOnline(self): |
|
606 | 610 | """ |
|
607 | 611 | Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si |
|
608 | 612 | no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files |
|
609 | 613 | siguientes. |
|
610 | 614 | |
|
611 | 615 | Affected: |
|
612 | 616 | self.flagIsNewFile |
|
613 | 617 | self.filename |
|
614 | 618 | self.fileSize |
|
615 | 619 | self.fp |
|
616 | 620 | self.set |
|
617 | 621 | self.flagNoMoreFiles |
|
618 | 622 | |
|
619 | 623 | Return: |
|
620 | 624 | 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado |
|
621 | 625 | 1 : si el file fue abierto con exito y esta listo a ser leido |
|
622 | 626 | |
|
623 | 627 | Excepciones: |
|
624 | 628 | Si un determinado file no puede ser abierto |
|
625 | 629 | """ |
|
626 | 630 | nFiles = 0 |
|
627 | 631 | fileOk_flag = False |
|
628 | 632 | firstTime_flag = True |
|
629 | 633 | |
|
630 | 634 | self.set += 1 |
|
631 | 635 | |
|
632 | 636 | if self.set > 999: |
|
633 | 637 | self.set = 0 |
|
634 | 638 | self.foldercounter += 1 |
|
635 | 639 | |
|
636 | 640 | #busca el 1er file disponible |
|
637 | 641 | fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext ) |
|
638 | 642 | if fullfilename: |
|
639 | 643 | if self.__verifyFile(fullfilename, False): |
|
640 | 644 | fileOk_flag = True |
|
641 | 645 | |
|
642 | 646 | #si no encuentra un file entonces espera y vuelve a buscar |
|
643 | 647 | if not(fileOk_flag): |
|
644 | 648 | for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles |
|
645 | 649 | |
|
646 | 650 | if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces |
|
647 | 651 | tries = self.nTries |
|
648 | 652 | else: |
|
649 | 653 | tries = 1 #si no es la 1era vez entonces solo lo hace una vez |
|
650 | 654 | |
|
651 | 655 | for nTries in range( tries ): |
|
652 | 656 | if firstTime_flag: |
|
653 | 657 | print "\tWaiting %0.2f sec for the file \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 ) |
|
654 | 658 | time.sleep( self.delay ) |
|
655 | 659 | else: |
|
656 | 660 | print "\tSearching next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext) |
|
657 | 661 | |
|
658 | 662 | fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext ) |
|
659 | 663 | if fullfilename: |
|
660 | 664 | if self.__verifyFile(fullfilename): |
|
661 | 665 | fileOk_flag = True |
|
662 | 666 | break |
|
663 | 667 | |
|
664 | 668 | if fileOk_flag: |
|
665 | 669 | break |
|
666 | 670 | |
|
667 | 671 | firstTime_flag = False |
|
668 | 672 | |
|
669 | 673 | print "\tSkipping the file \"%s\" due to this file doesn't exist" % filename |
|
670 | 674 | self.set += 1 |
|
671 | 675 | |
|
672 | 676 | if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta |
|
673 | 677 | self.set = 0 |
|
674 | 678 | self.doy += 1 |
|
675 | 679 | self.foldercounter = 0 |
|
676 | 680 | |
|
677 | 681 | if fileOk_flag: |
|
678 | 682 | self.fileSize = os.path.getsize( fullfilename ) |
|
679 | 683 | self.filename = fullfilename |
|
680 | 684 | self.flagIsNewFile = 1 |
|
681 | 685 | if self.fp != None: self.fp.close() |
|
682 | 686 | self.fp = open(fullfilename, 'rb') |
|
683 | 687 | self.flagNoMoreFiles = 0 |
|
684 | 688 | print 'Setting the file: %s' % fullfilename |
|
685 | 689 | else: |
|
686 | 690 | self.fileSize = 0 |
|
687 | 691 | self.filename = None |
|
688 | 692 | self.flagIsNewFile = 0 |
|
689 | 693 | self.fp = None |
|
690 | 694 | self.flagNoMoreFiles = 1 |
|
691 | 695 | print 'No more Files' |
|
692 | 696 | |
|
693 | 697 | return fileOk_flag |
|
694 | 698 | |
|
695 | 699 | def setNextFile(self): |
|
696 | 700 | if self.fp != None: |
|
697 | 701 | self.fp.close() |
|
698 | 702 | |
|
699 | 703 | if self.online: |
|
700 | 704 | newFile = self.__setNextFileOnline() |
|
701 | 705 | else: |
|
702 | 706 | newFile = self.__setNextFileOffline() |
|
703 | 707 | |
|
704 | 708 | if not(newFile): |
|
705 | 709 | return 0 |
|
706 | 710 | |
|
707 | 711 | self.__readFirstHeader() |
|
708 | 712 | self.nReadBlocks = 0 |
|
709 | 713 | return 1 |
|
710 | 714 | |
|
711 | 715 | def __waitNewBlock(self): |
|
712 | 716 | """ |
|
713 | 717 | Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma. |
|
714 | 718 | |
|
715 | 719 | Si el modo de lectura es OffLine siempre retorn 0 |
|
716 | 720 | """ |
|
717 | 721 | if not self.online: |
|
718 | 722 | return 0 |
|
719 | 723 | |
|
720 | 724 | if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile): |
|
721 | 725 | return 0 |
|
722 | 726 | |
|
723 | 727 | currentPointer = self.fp.tell() |
|
724 | 728 | |
|
725 | 729 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
726 | 730 | |
|
727 | 731 | for nTries in range( self.nTries ): |
|
728 | 732 | |
|
729 | 733 | self.fp.close() |
|
730 | 734 | self.fp = open( self.filename, 'rb' ) |
|
731 | 735 | self.fp.seek( currentPointer ) |
|
732 | 736 | |
|
733 | 737 | self.fileSize = os.path.getsize( self.filename ) |
|
734 | 738 | currentSize = self.fileSize - currentPointer |
|
735 | 739 | |
|
736 | 740 | if ( currentSize >= neededSize ): |
|
737 | 741 | self.basicHeaderObj.read(self.fp) |
|
738 | 742 | return 1 |
|
739 | 743 | |
|
740 | 744 | if self.fileSize == self.fileSizeByHeader: |
|
741 | 745 | # self.flagEoF = True |
|
742 | 746 | return 0 |
|
743 | 747 | |
|
744 | 748 | print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1) |
|
745 | 749 | time.sleep( self.delay ) |
|
746 | 750 | |
|
747 | 751 | |
|
748 | 752 | return 0 |
|
749 | 753 | |
|
750 | 754 | def waitDataBlock(self,pointer_location): |
|
751 | 755 | |
|
752 | 756 | currentPointer = pointer_location |
|
753 | 757 | |
|
754 | 758 | neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize |
|
755 | 759 | |
|
756 | 760 | for nTries in range( self.nTries ): |
|
757 | 761 | self.fp.close() |
|
758 | 762 | self.fp = open( self.filename, 'rb' ) |
|
759 | 763 | self.fp.seek( currentPointer ) |
|
760 | 764 | |
|
761 | 765 | self.fileSize = os.path.getsize( self.filename ) |
|
762 | 766 | currentSize = self.fileSize - currentPointer |
|
763 | 767 | |
|
764 | 768 | if ( currentSize >= neededSize ): |
|
765 | 769 | return 1 |
|
766 | 770 | |
|
767 | 771 | print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1) |
|
768 | 772 | time.sleep( self.delay ) |
|
769 | 773 | |
|
770 | 774 | return 0 |
|
771 | 775 | |
|
772 | 776 | def __jumpToLastBlock(self): |
|
773 | 777 | |
|
774 | 778 | if not(self.__isFirstTimeOnline): |
|
775 | 779 | return |
|
776 | 780 | |
|
777 | 781 | csize = self.fileSize - self.fp.tell() |
|
778 | 782 | blocksize = self.processingHeaderObj.blockSize |
|
779 | 783 | |
|
780 | 784 | #salta el primer bloque de datos |
|
781 | 785 | if csize > self.processingHeaderObj.blockSize: |
|
782 | 786 | self.fp.seek(self.fp.tell() + blocksize) |
|
783 | 787 | else: |
|
784 | 788 | return |
|
785 | 789 | |
|
786 | 790 | csize = self.fileSize - self.fp.tell() |
|
787 | 791 | neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
788 | 792 | while True: |
|
789 | 793 | |
|
790 | 794 | if self.fp.tell()<self.fileSize: |
|
791 | 795 | self.fp.seek(self.fp.tell() + neededsize) |
|
792 | 796 | else: |
|
793 | 797 | self.fp.seek(self.fp.tell() - neededsize) |
|
794 | 798 | break |
|
795 | 799 | |
|
796 | 800 | # csize = self.fileSize - self.fp.tell() |
|
797 | 801 | # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
798 | 802 | # factor = int(csize/neededsize) |
|
799 | 803 | # if factor > 0: |
|
800 | 804 | # self.fp.seek(self.fp.tell() + factor*neededsize) |
|
801 | 805 | |
|
802 | 806 | self.flagIsNewFile = 0 |
|
803 | 807 | self.__isFirstTimeOnline = 0 |
|
804 | 808 | |
|
805 | 809 | def __setNewBlock(self): |
|
806 | 810 | |
|
807 | 811 | if self.fp == None: |
|
808 | 812 | return 0 |
|
809 | 813 | |
|
810 | 814 | if self.online: |
|
811 | 815 | self.__jumpToLastBlock() |
|
812 | 816 | |
|
813 | 817 | if self.flagIsNewFile: |
|
814 | 818 | return 1 |
|
815 | 819 | |
|
816 | 820 | self.lastUTTime = self.basicHeaderObj.utc |
|
817 | 821 | currentSize = self.fileSize - self.fp.tell() |
|
818 | 822 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
819 | 823 | |
|
820 | 824 | if (currentSize >= neededSize): |
|
821 | 825 | self.basicHeaderObj.read(self.fp) |
|
822 | 826 | return 1 |
|
823 | 827 | |
|
824 | 828 | if self.__waitNewBlock(): |
|
825 | 829 | return 1 |
|
826 | 830 | |
|
827 | 831 | if not(self.setNextFile()): |
|
828 | 832 | return 0 |
|
829 | 833 | |
|
830 | 834 | deltaTime = self.basicHeaderObj.utc - self.lastUTTime # |
|
831 | 835 | |
|
832 | 836 | self.flagTimeBlock = 0 |
|
833 | 837 | |
|
834 | 838 | if deltaTime > self.maxTimeStep: |
|
835 | 839 | self.flagTimeBlock = 1 |
|
836 | 840 | |
|
837 | 841 | return 1 |
|
838 | 842 | |
|
839 | 843 | def readNextBlock(self): |
|
840 | 844 | if not(self.__setNewBlock()): |
|
841 | 845 | return 0 |
|
842 | 846 | |
|
843 | 847 | if not(self.readBlock()): |
|
844 | 848 | return 0 |
|
845 | 849 | |
|
846 | 850 | return 1 |
|
847 | 851 | |
|
848 | 852 | def __readFirstHeader(self): |
|
849 | 853 | |
|
850 | 854 | self.basicHeaderObj.read(self.fp) |
|
851 | 855 | self.systemHeaderObj.read(self.fp) |
|
852 | 856 | self.radarControllerHeaderObj.read(self.fp) |
|
853 | 857 | self.processingHeaderObj.read(self.fp) |
|
854 | 858 | |
|
855 | 859 | self.firstHeaderSize = self.basicHeaderObj.size |
|
856 | 860 | |
|
857 | 861 | datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR)) |
|
858 | 862 | if datatype == 0: |
|
859 | 863 | datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
860 | 864 | elif datatype == 1: |
|
861 | 865 | datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
862 | 866 | elif datatype == 2: |
|
863 | 867 | datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
864 | 868 | elif datatype == 3: |
|
865 | 869 | datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
866 | 870 | elif datatype == 4: |
|
867 | 871 | datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
868 | 872 | elif datatype == 5: |
|
869 | 873 | datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
870 | 874 | else: |
|
871 | 875 | raise ValueError, 'Data type was not defined' |
|
872 | 876 | |
|
873 | 877 | self.dtype = datatype_str |
|
874 | 878 | #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c |
|
875 | 879 | self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1) |
|
876 | 880 | # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels) |
|
877 | 881 | # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels) |
|
878 | 882 | self.getBlockDimension() |
|
879 | 883 | |
|
880 | 884 | def __verifyFile(self, filename, msgFlag=True): |
|
881 | 885 | msg = None |
|
882 | 886 | try: |
|
883 | 887 | fp = open(filename, 'rb') |
|
884 | 888 | currentPosition = fp.tell() |
|
885 | 889 | except IOError: |
|
886 | 890 | traceback.print_exc() |
|
887 | 891 | if msgFlag: |
|
888 | 892 | print "The file %s can't be opened" % (filename) |
|
889 | 893 | return False |
|
890 | 894 | |
|
891 | 895 | neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize |
|
892 | 896 | |
|
893 | 897 | if neededSize == 0: |
|
894 | 898 | basicHeaderObj = BasicHeader(LOCALTIME) |
|
895 | 899 | systemHeaderObj = SystemHeader() |
|
896 | 900 | radarControllerHeaderObj = RadarControllerHeader() |
|
897 | 901 | processingHeaderObj = ProcessingHeader() |
|
898 | 902 | |
|
899 | 903 | try: |
|
900 | 904 | if not( basicHeaderObj.read(fp) ): raise IOError |
|
901 | 905 | if not( systemHeaderObj.read(fp) ): raise IOError |
|
902 | 906 | if not( radarControllerHeaderObj.read(fp) ): raise IOError |
|
903 | 907 | if not( processingHeaderObj.read(fp) ): raise IOError |
|
904 | 908 | # data_type = int(numpy.log2((processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR)) |
|
905 | 909 | |
|
906 | 910 | neededSize = processingHeaderObj.blockSize + basicHeaderObj.size |
|
907 | 911 | |
|
908 | 912 | except IOError: |
|
909 | 913 | traceback.print_exc() |
|
910 | 914 | if msgFlag: |
|
911 | 915 | print "\tThe file %s is empty or it hasn't enough data" % filename |
|
912 | 916 | |
|
913 | 917 | fp.close() |
|
914 | 918 | return False |
|
915 | 919 | else: |
|
916 | 920 | msg = "\tSkipping the file %s due to it hasn't enough data" %filename |
|
917 | 921 | |
|
918 | 922 | fp.close() |
|
919 | 923 | fileSize = os.path.getsize(filename) |
|
920 | 924 | currentSize = fileSize - currentPosition |
|
921 | 925 | if currentSize < neededSize: |
|
922 | 926 | if msgFlag and (msg != None): |
|
923 | 927 | print msg #print"\tSkipping the file %s due to it hasn't enough data" %filename |
|
924 | 928 | return False |
|
925 | 929 | |
|
926 | 930 | return True |
|
927 | 931 | |
|
928 | 932 | def setup(self, |
|
929 | 933 | path=None, |
|
930 | 934 | startDate=None, |
|
931 | 935 | endDate=None, |
|
932 | 936 | startTime=datetime.time(0,0,0), |
|
933 | 937 | endTime=datetime.time(23,59,59), |
|
934 | 938 | set=None, |
|
935 | 939 | expLabel = "", |
|
936 | 940 | ext = None, |
|
937 | 941 | online = False, |
|
938 | 942 | delay = 60, |
|
939 | 943 | walk = True, |
|
940 |
getblock = False |
|
|
944 | getblock = False, | |
|
945 | nTxs = 1): | |
|
941 | 946 | |
|
942 | 947 | if path == None: |
|
943 | 948 | raise ValueError, "The path is not valid" |
|
944 | 949 | |
|
945 | 950 | if ext == None: |
|
946 | 951 | ext = self.ext |
|
947 | 952 | |
|
948 | 953 | if online: |
|
949 | 954 | print "Searching files in online mode..." |
|
950 | 955 | |
|
951 | 956 | for nTries in range( self.nTries ): |
|
952 | 957 | fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set) |
|
953 | 958 | |
|
954 | 959 | if fullpath: |
|
955 | 960 | break |
|
956 | 961 | |
|
957 | 962 | print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1) |
|
958 | 963 | time.sleep( self.delay ) |
|
959 | 964 | |
|
960 | 965 | if not(fullpath): |
|
961 | 966 | print "There 'isn't valied files in %s" % path |
|
962 | 967 | return None |
|
963 | 968 | |
|
964 | 969 | self.year = year |
|
965 | 970 | self.doy = doy |
|
966 | 971 | self.set = set - 1 |
|
967 | 972 | self.path = path |
|
968 | 973 | self.foldercounter = foldercounter |
|
969 | 974 | last_set = None |
|
970 | 975 | |
|
971 | 976 | else: |
|
972 | 977 | print "Searching files in offline mode ..." |
|
973 | 978 | pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate, |
|
974 | 979 | startTime=startTime, endTime=endTime, |
|
975 | 980 | set=set, expLabel=expLabel, ext=ext, |
|
976 | 981 | walk=walk) |
|
977 | 982 | |
|
978 | 983 | if not(pathList): |
|
979 | 984 | print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path, |
|
980 | 985 | datetime.datetime.combine(startDate,startTime).ctime(), |
|
981 | 986 | datetime.datetime.combine(endDate,endTime).ctime()) |
|
982 | 987 | |
|
983 | 988 | sys.exit(-1) |
|
984 | 989 | |
|
985 | 990 | |
|
986 | 991 | self.fileIndex = -1 |
|
987 | 992 | self.pathList = pathList |
|
988 | 993 | self.filenameList = filenameList |
|
989 | 994 | file_name = os.path.basename(filenameList[-1]) |
|
990 | 995 | basename, ext = os.path.splitext(file_name) |
|
991 | 996 | last_set = int(basename[-3:]) |
|
992 | 997 | |
|
993 | 998 | self.online = online |
|
994 | 999 | self.delay = delay |
|
995 | 1000 | ext = ext.lower() |
|
996 | 1001 | self.ext = ext |
|
997 | 1002 | self.getByBlock = getblock |
|
1003 | self.nTxs = int(nTxs) | |
|
998 | 1004 | |
|
999 | 1005 | if not(self.setNextFile()): |
|
1000 | 1006 | if (startDate!=None) and (endDate!=None): |
|
1001 | 1007 | print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime()) |
|
1002 | 1008 | elif startDate != None: |
|
1003 | 1009 | print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime()) |
|
1004 | 1010 | else: |
|
1005 | 1011 | print "No files" |
|
1006 | 1012 | |
|
1007 | 1013 | sys.exit(-1) |
|
1008 | 1014 | |
|
1009 | 1015 | # self.updateDataHeader() |
|
1010 | 1016 | if last_set != None: |
|
1011 | 1017 | self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock |
|
1012 | 1018 | return |
|
1013 | 1019 | |
|
1014 | 1020 | def getBasicHeader(self): |
|
1015 | 1021 | |
|
1016 | 1022 | self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds |
|
1017 | 1023 | |
|
1018 | 1024 | self.dataOut.flagTimeBlock = self.flagTimeBlock |
|
1019 | 1025 | |
|
1020 | 1026 | self.dataOut.timeZone = self.basicHeaderObj.timeZone |
|
1021 | 1027 | |
|
1022 | 1028 | self.dataOut.dstFlag = self.basicHeaderObj.dstFlag |
|
1023 | 1029 | |
|
1024 | 1030 | self.dataOut.errorCount = self.basicHeaderObj.errorCount |
|
1025 | 1031 | |
|
1026 | 1032 | self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime |
|
1027 | 1033 | |
|
1028 | 1034 | def getFirstHeader(self): |
|
1029 | 1035 | |
|
1030 | 1036 | raise ValueError, "This method has not been implemented" |
|
1031 | 1037 | |
|
1032 | 1038 | def getData(self): |
|
1033 | 1039 | |
|
1034 | 1040 | raise ValueError, "This method has not been implemented" |
|
1035 | 1041 | |
|
1036 | 1042 | def hasNotDataInBuffer(self): |
|
1037 | 1043 | |
|
1038 | 1044 | raise ValueError, "This method has not been implemented" |
|
1039 | 1045 | |
|
1040 | 1046 | def readBlock(self): |
|
1041 | 1047 | |
|
1042 | 1048 | raise ValueError, "This method has not been implemented" |
|
1043 | 1049 | |
|
1044 | 1050 | def isEndProcess(self): |
|
1045 | 1051 | |
|
1046 | 1052 | return self.flagNoMoreFiles |
|
1047 | 1053 | |
|
1048 | 1054 | def printReadBlocks(self): |
|
1049 | 1055 | |
|
1050 | 1056 | print "Number of read blocks per file %04d" %self.nReadBlocks |
|
1051 | 1057 | |
|
1052 | 1058 | def printTotalBlocks(self): |
|
1053 | 1059 | |
|
1054 | 1060 | print "Number of read blocks %04d" %self.nTotalBlocks |
|
1055 | 1061 | |
|
1056 | 1062 | def printNumberOfBlock(self): |
|
1057 | 1063 | |
|
1058 | 1064 | if self.flagIsNewBlock: |
|
1059 | 1065 | print "Block No. %04d, Total blocks %04d -> %s" %(self.basicHeaderObj.dataBlock, self.nTotalBlocks, self.dataOut.datatime.ctime()) |
|
1060 | 1066 | self.dataOut.blocknow = self.basicHeaderObj.dataBlock |
|
1061 | 1067 | |
|
1062 | 1068 | def printInfo(self): |
|
1063 | 1069 | |
|
1064 | 1070 | if self.__printInfo == False: |
|
1065 | 1071 | return |
|
1066 | 1072 | |
|
1067 | 1073 | self.basicHeaderObj.printInfo() |
|
1068 | 1074 | self.systemHeaderObj.printInfo() |
|
1069 | 1075 | self.radarControllerHeaderObj.printInfo() |
|
1070 | 1076 | self.processingHeaderObj.printInfo() |
|
1071 | 1077 | |
|
1072 | 1078 | self.__printInfo = False |
|
1073 | 1079 | |
|
1074 | 1080 | |
|
1075 | 1081 | def run(self, **kwargs): |
|
1076 | 1082 | |
|
1077 | 1083 | if not(self.isConfig): |
|
1078 | 1084 | |
|
1079 | 1085 | # self.dataOut = dataOut |
|
1080 | 1086 | self.setup(**kwargs) |
|
1081 | 1087 | self.isConfig = True |
|
1082 | 1088 | |
|
1083 | 1089 | self.getData() |
|
1084 | 1090 | |
|
1085 | 1091 | class JRODataWriter(JRODataIO): |
|
1086 | 1092 | |
|
1087 | 1093 | """ |
|
1088 | 1094 | Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura |
|
1089 | 1095 | de los datos siempre se realiza por bloques. |
|
1090 | 1096 | """ |
|
1091 | 1097 | |
|
1092 | 1098 | blockIndex = 0 |
|
1093 | 1099 | |
|
1094 | 1100 | path = None |
|
1095 | 1101 | |
|
1096 | 1102 | setFile = None |
|
1097 | 1103 | |
|
1098 | 1104 | profilesPerBlock = None |
|
1099 | 1105 | |
|
1100 | 1106 | blocksPerFile = None |
|
1101 | 1107 | |
|
1102 | 1108 | nWriteBlocks = 0 |
|
1103 | 1109 | |
|
1104 | 1110 | def __init__(self, dataOut=None): |
|
1105 | 1111 | raise ValueError, "Not implemented" |
|
1106 | 1112 | |
|
1107 | 1113 | |
|
1108 | 1114 | def hasAllDataInBuffer(self): |
|
1109 | 1115 | raise ValueError, "Not implemented" |
|
1110 | 1116 | |
|
1111 | 1117 | |
|
1112 | 1118 | def setBlockDimension(self): |
|
1113 | 1119 | raise ValueError, "Not implemented" |
|
1114 | 1120 | |
|
1115 | 1121 | |
|
1116 | 1122 | def writeBlock(self): |
|
1117 | 1123 | raise ValueError, "No implemented" |
|
1118 | 1124 | |
|
1119 | 1125 | |
|
1120 | 1126 | def putData(self): |
|
1121 | 1127 | raise ValueError, "No implemented" |
|
1122 | 1128 | |
|
1123 | 1129 | |
|
1124 | 1130 | def setBasicHeader(self): |
|
1125 | 1131 | |
|
1126 | 1132 | self.basicHeaderObj.size = self.basicHeaderSize #bytes |
|
1127 | 1133 | self.basicHeaderObj.version = self.versionFile |
|
1128 | 1134 | self.basicHeaderObj.dataBlock = self.nTotalBlocks |
|
1129 | 1135 | |
|
1130 | 1136 | utc = numpy.floor(self.dataOut.utctime) |
|
1131 | 1137 | milisecond = (self.dataOut.utctime - utc)* 1000.0 |
|
1132 | 1138 | |
|
1133 | 1139 | self.basicHeaderObj.utc = utc |
|
1134 | 1140 | self.basicHeaderObj.miliSecond = milisecond |
|
1135 | 1141 | self.basicHeaderObj.timeZone = self.dataOut.timeZone |
|
1136 | 1142 | self.basicHeaderObj.dstFlag = self.dataOut.dstFlag |
|
1137 | 1143 | self.basicHeaderObj.errorCount = self.dataOut.errorCount |
|
1138 | 1144 | |
|
1139 | 1145 | def setFirstHeader(self): |
|
1140 | 1146 | """ |
|
1141 | 1147 | Obtiene una copia del First Header |
|
1142 | 1148 | |
|
1143 | 1149 | Affected: |
|
1144 | 1150 | |
|
1145 | 1151 | self.basicHeaderObj |
|
1146 | 1152 | self.systemHeaderObj |
|
1147 | 1153 | self.radarControllerHeaderObj |
|
1148 | 1154 | self.processingHeaderObj self. |
|
1149 | 1155 | |
|
1150 | 1156 | Return: |
|
1151 | 1157 | None |
|
1152 | 1158 | """ |
|
1153 | 1159 | |
|
1154 | 1160 | raise ValueError, "No implemented" |
|
1155 | 1161 | |
|
1156 | 1162 | def __writeFirstHeader(self): |
|
1157 | 1163 | """ |
|
1158 | 1164 | Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader) |
|
1159 | 1165 | |
|
1160 | 1166 | Affected: |
|
1161 | 1167 | __dataType |
|
1162 | 1168 | |
|
1163 | 1169 | Return: |
|
1164 | 1170 | None |
|
1165 | 1171 | """ |
|
1166 | 1172 | |
|
1167 | 1173 | # CALCULAR PARAMETROS |
|
1168 | 1174 | |
|
1169 | 1175 | sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size |
|
1170 | 1176 | self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader |
|
1171 | 1177 | |
|
1172 | 1178 | self.basicHeaderObj.write(self.fp) |
|
1173 | 1179 | self.systemHeaderObj.write(self.fp) |
|
1174 | 1180 | self.radarControllerHeaderObj.write(self.fp) |
|
1175 | 1181 | self.processingHeaderObj.write(self.fp) |
|
1176 | 1182 | |
|
1177 | 1183 | self.dtype = self.dataOut.dtype |
|
1178 | 1184 | |
|
1179 | 1185 | def __setNewBlock(self): |
|
1180 | 1186 | """ |
|
1181 | 1187 | Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header |
|
1182 | 1188 | |
|
1183 | 1189 | Return: |
|
1184 | 1190 | 0 : si no pudo escribir nada |
|
1185 | 1191 | 1 : Si escribio el Basic el First Header |
|
1186 | 1192 | """ |
|
1187 | 1193 | if self.fp == None: |
|
1188 | 1194 | self.setNextFile() |
|
1189 | 1195 | |
|
1190 | 1196 | if self.flagIsNewFile: |
|
1191 | 1197 | return 1 |
|
1192 | 1198 | |
|
1193 | 1199 | if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile: |
|
1194 | 1200 | self.basicHeaderObj.write(self.fp) |
|
1195 | 1201 | return 1 |
|
1196 | 1202 | |
|
1197 | 1203 | if not( self.setNextFile() ): |
|
1198 | 1204 | return 0 |
|
1199 | 1205 | |
|
1200 | 1206 | return 1 |
|
1201 | 1207 | |
|
1202 | 1208 | |
|
1203 | 1209 | def writeNextBlock(self): |
|
1204 | 1210 | """ |
|
1205 | 1211 | Selecciona el bloque siguiente de datos y los escribe en un file |
|
1206 | 1212 | |
|
1207 | 1213 | Return: |
|
1208 | 1214 | 0 : Si no hizo pudo escribir el bloque de datos |
|
1209 | 1215 | 1 : Si no pudo escribir el bloque de datos |
|
1210 | 1216 | """ |
|
1211 | 1217 | if not( self.__setNewBlock() ): |
|
1212 | 1218 | return 0 |
|
1213 | 1219 | |
|
1214 | 1220 | self.writeBlock() |
|
1215 | 1221 | |
|
1216 | 1222 | return 1 |
|
1217 | 1223 | |
|
1218 | 1224 | def setNextFile(self): |
|
1219 | 1225 | """ |
|
1220 | 1226 | Determina el siguiente file que sera escrito |
|
1221 | 1227 | |
|
1222 | 1228 | Affected: |
|
1223 | 1229 | self.filename |
|
1224 | 1230 | self.subfolder |
|
1225 | 1231 | self.fp |
|
1226 | 1232 | self.setFile |
|
1227 | 1233 | self.flagIsNewFile |
|
1228 | 1234 | |
|
1229 | 1235 | Return: |
|
1230 | 1236 | 0 : Si el archivo no puede ser escrito |
|
1231 | 1237 | 1 : Si el archivo esta listo para ser escrito |
|
1232 | 1238 | """ |
|
1233 | 1239 | ext = self.ext |
|
1234 | 1240 | path = self.path |
|
1235 | 1241 | |
|
1236 | 1242 | if self.fp != None: |
|
1237 | 1243 | self.fp.close() |
|
1238 | 1244 | |
|
1239 | 1245 | timeTuple = time.localtime( self.dataOut.utctime) |
|
1240 | 1246 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) |
|
1241 | 1247 | |
|
1242 | 1248 | fullpath = os.path.join( path, subfolder ) |
|
1243 | 1249 | if not( os.path.exists(fullpath) ): |
|
1244 | 1250 | os.mkdir(fullpath) |
|
1245 | 1251 | self.setFile = -1 #inicializo mi contador de seteo |
|
1246 | 1252 | else: |
|
1247 | 1253 | filesList = os.listdir( fullpath ) |
|
1248 | 1254 | if len( filesList ) > 0: |
|
1249 | 1255 | filesList = sorted( filesList, key=str.lower ) |
|
1250 | 1256 | filen = filesList[-1] |
|
1251 | 1257 | # el filename debera tener el siguiente formato |
|
1252 | 1258 | # 0 1234 567 89A BCDE (hex) |
|
1253 | 1259 | # x YYYY DDD SSS .ext |
|
1254 | 1260 | if isNumber( filen[8:11] ): |
|
1255 | 1261 | self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file |
|
1256 | 1262 | else: |
|
1257 | 1263 | self.setFile = -1 |
|
1258 | 1264 | else: |
|
1259 | 1265 | self.setFile = -1 #inicializo mi contador de seteo |
|
1260 | 1266 | |
|
1261 | 1267 | setFile = self.setFile |
|
1262 | 1268 | setFile += 1 |
|
1263 | 1269 | |
|
1264 | 1270 | file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, |
|
1265 | 1271 | timeTuple.tm_year, |
|
1266 | 1272 | timeTuple.tm_yday, |
|
1267 | 1273 | setFile, |
|
1268 | 1274 | ext ) |
|
1269 | 1275 | |
|
1270 | 1276 | filename = os.path.join( path, subfolder, file ) |
|
1271 | 1277 | |
|
1272 | 1278 | fp = open( filename,'wb' ) |
|
1273 | 1279 | |
|
1274 | 1280 | self.blockIndex = 0 |
|
1275 | 1281 | |
|
1276 | 1282 | #guardando atributos |
|
1277 | 1283 | self.filename = filename |
|
1278 | 1284 | self.subfolder = subfolder |
|
1279 | 1285 | self.fp = fp |
|
1280 | 1286 | self.setFile = setFile |
|
1281 | 1287 | self.flagIsNewFile = 1 |
|
1282 | 1288 | |
|
1283 | 1289 | self.setFirstHeader() |
|
1284 | 1290 | |
|
1285 | 1291 | print 'Writing the file: %s'%self.filename |
|
1286 | 1292 | |
|
1287 | 1293 | self.__writeFirstHeader() |
|
1288 | 1294 | |
|
1289 | 1295 | return 1 |
|
1290 | 1296 | |
|
1291 | 1297 | def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=0, ext=None): |
|
1292 | 1298 | """ |
|
1293 | 1299 | Setea el tipo de formato en la cual sera guardada la data y escribe el First Header |
|
1294 | 1300 | |
|
1295 | 1301 | Inputs: |
|
1296 | 1302 | path : el path destino en el cual se escribiran los files a crear |
|
1297 | 1303 | format : formato en el cual sera salvado un file |
|
1298 | 1304 | set : el setebo del file |
|
1299 | 1305 | |
|
1300 | 1306 | Return: |
|
1301 | 1307 | 0 : Si no realizo un buen seteo |
|
1302 | 1308 | 1 : Si realizo un buen seteo |
|
1303 | 1309 | """ |
|
1304 | 1310 | |
|
1305 | 1311 | if ext == None: |
|
1306 | 1312 | ext = self.ext |
|
1307 | 1313 | |
|
1308 | 1314 | ext = ext.lower() |
|
1309 | 1315 | |
|
1310 | 1316 | self.ext = ext |
|
1311 | 1317 | |
|
1312 | 1318 | self.path = path |
|
1313 | 1319 | |
|
1314 | 1320 | self.setFile = set - 1 |
|
1315 | 1321 | |
|
1316 | 1322 | self.blocksPerFile = blocksPerFile |
|
1317 | 1323 | |
|
1318 | 1324 | self.profilesPerBlock = profilesPerBlock |
|
1319 | 1325 | |
|
1320 | 1326 | self.dataOut = dataOut |
|
1321 | 1327 | |
|
1322 | 1328 | if not(self.setNextFile()): |
|
1323 | 1329 | print "There isn't a next file" |
|
1324 | 1330 | return 0 |
|
1325 | 1331 | |
|
1326 | 1332 | self.setBlockDimension() |
|
1327 | 1333 | |
|
1328 | 1334 | return 1 |
|
1329 | 1335 | |
|
1330 | 1336 | def run(self, dataOut, **kwargs): |
|
1331 | 1337 | |
|
1332 | 1338 | if not(self.isConfig): |
|
1333 | 1339 | |
|
1334 | 1340 | self.setup(dataOut, **kwargs) |
|
1335 | 1341 | self.isConfig = True |
|
1336 | 1342 | |
|
1337 | 1343 | self.putData() |
|
1338 | 1344 |
@@ -1,609 +1,650 | |||
|
1 | 1 | ''' |
|
2 | 2 | |
|
3 | 3 | ''' |
|
4 | 4 | import numpy |
|
5 | 5 | |
|
6 | 6 | from jroIO_base import LOCALTIME, JRODataReader, JRODataWriter |
|
7 | 7 | from model.proc.jroproc_base import ProcessingUnit, Operation |
|
8 | 8 | from model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader |
|
9 | 9 | from model.data.jrodata import Voltage |
|
10 | 10 | |
|
11 | 11 | class VoltageReader(JRODataReader, ProcessingUnit): |
|
12 | 12 | """ |
|
13 | 13 | Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura |
|
14 | 14 | de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones: |
|
15 | 15 | perfiles*alturas*canales) son almacenados en la variable "buffer". |
|
16 | 16 | |
|
17 | 17 | perfiles * alturas * canales |
|
18 | 18 | |
|
19 | 19 | Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader, |
|
20 | 20 | RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la |
|
21 | 21 | cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de |
|
22 | 22 | datos desde el "buffer" cada vez que se ejecute el metodo "getData". |
|
23 | 23 | |
|
24 | 24 | Example: |
|
25 | 25 | |
|
26 | 26 | dpath = "/home/myuser/data" |
|
27 | 27 | |
|
28 | 28 | startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0) |
|
29 | 29 | |
|
30 | 30 | endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0) |
|
31 | 31 | |
|
32 | 32 | readerObj = VoltageReader() |
|
33 | 33 | |
|
34 | 34 | readerObj.setup(dpath, startTime, endTime) |
|
35 | 35 | |
|
36 | 36 | while(True): |
|
37 | 37 | |
|
38 | 38 | #to get one profile |
|
39 | 39 | profile = readerObj.getData() |
|
40 | 40 | |
|
41 | 41 | #print the profile |
|
42 | 42 | print profile |
|
43 | 43 | |
|
44 | 44 | #If you want to see all datablock |
|
45 | 45 | print readerObj.datablock |
|
46 | 46 | |
|
47 | 47 | if readerObj.flagNoMoreFiles: |
|
48 | 48 | break |
|
49 | 49 | |
|
50 | 50 | """ |
|
51 | 51 | |
|
52 | 52 | ext = ".r" |
|
53 | 53 | |
|
54 | 54 | optchar = "D" |
|
55 | 55 | dataOut = None |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | def __init__(self): |
|
59 | 59 | """ |
|
60 | 60 | Inicializador de la clase VoltageReader para la lectura de datos de voltage. |
|
61 | 61 | |
|
62 | 62 | Input: |
|
63 | 63 | dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para |
|
64 | 64 | almacenar un perfil de datos cada vez que se haga un requerimiento |
|
65 | 65 | (getData). El perfil sera obtenido a partir del buffer de datos, |
|
66 | 66 | si el buffer esta vacio se hara un nuevo proceso de lectura de un |
|
67 | 67 | bloque de datos. |
|
68 | 68 | Si este parametro no es pasado se creara uno internamente. |
|
69 | 69 | |
|
70 | 70 | Variables afectadas: |
|
71 | 71 | self.dataOut |
|
72 | 72 | |
|
73 | 73 | Return: |
|
74 | 74 | None |
|
75 | 75 | """ |
|
76 | 76 | |
|
77 | 77 | ProcessingUnit.__init__(self) |
|
78 | 78 | |
|
79 | 79 | self.isConfig = False |
|
80 | 80 | |
|
81 | 81 | self.datablock = None |
|
82 | 82 | |
|
83 | 83 | self.utc = 0 |
|
84 | 84 | |
|
85 | 85 | self.ext = ".r" |
|
86 | 86 | |
|
87 | 87 | self.optchar = "D" |
|
88 | 88 | |
|
89 | 89 | self.basicHeaderObj = BasicHeader(LOCALTIME) |
|
90 | 90 | |
|
91 | 91 | self.systemHeaderObj = SystemHeader() |
|
92 | 92 | |
|
93 | 93 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
94 | 94 | |
|
95 | 95 | self.processingHeaderObj = ProcessingHeader() |
|
96 | 96 | |
|
97 | 97 | self.online = 0 |
|
98 | 98 | |
|
99 | 99 | self.fp = None |
|
100 | 100 | |
|
101 | 101 | self.idFile = None |
|
102 | 102 | |
|
103 | 103 | self.dtype = None |
|
104 | 104 | |
|
105 | 105 | self.fileSizeByHeader = None |
|
106 | 106 | |
|
107 | 107 | self.filenameList = [] |
|
108 | 108 | |
|
109 | 109 | self.filename = None |
|
110 | 110 | |
|
111 | 111 | self.fileSize = None |
|
112 | 112 | |
|
113 | 113 | self.firstHeaderSize = 0 |
|
114 | 114 | |
|
115 | 115 | self.basicHeaderSize = 24 |
|
116 | 116 | |
|
117 | 117 | self.pathList = [] |
|
118 | 118 | |
|
119 | 119 | self.filenameList = [] |
|
120 | 120 | |
|
121 | 121 | self.lastUTTime = 0 |
|
122 | 122 | |
|
123 | 123 | self.maxTimeStep = 30 |
|
124 | 124 | |
|
125 | 125 | self.flagNoMoreFiles = 0 |
|
126 | 126 | |
|
127 | 127 | self.set = 0 |
|
128 | 128 | |
|
129 | 129 | self.path = None |
|
130 | 130 | |
|
131 | 131 | self.profileIndex = 2**32-1 |
|
132 | 132 | |
|
133 | 133 | self.delay = 3 #seconds |
|
134 | 134 | |
|
135 | 135 | self.nTries = 3 #quantity tries |
|
136 | 136 | |
|
137 | 137 | self.nFiles = 3 #number of files for searching |
|
138 | 138 | |
|
139 | 139 | self.nReadBlocks = 0 |
|
140 | 140 | |
|
141 | 141 | self.flagIsNewFile = 1 |
|
142 | 142 | |
|
143 | 143 | self.__isFirstTimeOnline = 1 |
|
144 | 144 | |
|
145 | 145 | # self.ippSeconds = 0 |
|
146 | 146 | |
|
147 | 147 | self.flagTimeBlock = 0 |
|
148 | 148 | |
|
149 | 149 | self.flagIsNewBlock = 0 |
|
150 | 150 | |
|
151 | 151 | self.nTotalBlocks = 0 |
|
152 | 152 | |
|
153 | 153 | self.blocksize = 0 |
|
154 | 154 | |
|
155 | 155 | self.dataOut = self.createObjByDefault() |
|
156 | ||
|
157 | self.nTxs = 1 | |
|
158 | ||
|
159 | self.txIndex = 0 | |
|
156 | 160 | |
|
157 | 161 | def createObjByDefault(self): |
|
158 | 162 | |
|
159 | 163 | dataObj = Voltage() |
|
160 | 164 | |
|
161 | 165 | return dataObj |
|
162 | 166 | |
|
163 | 167 | def __hasNotDataInBuffer(self): |
|
168 | ||
|
164 | 169 | if self.profileIndex >= self.processingHeaderObj.profilesPerBlock: |
|
165 | 170 | return 1 |
|
171 | ||
|
166 | 172 | return 0 |
|
167 | 173 | |
|
168 | 174 | |
|
169 | 175 | def getBlockDimension(self): |
|
170 | 176 | """ |
|
171 | 177 | Obtiene la cantidad de puntos a leer por cada bloque de datos |
|
172 | 178 | |
|
173 | 179 | Affected: |
|
174 | 180 | self.blocksize |
|
175 | 181 | |
|
176 | 182 | Return: |
|
177 | 183 | None |
|
178 | 184 | """ |
|
179 | 185 | pts2read = self.processingHeaderObj.profilesPerBlock * self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels |
|
180 | 186 | self.blocksize = pts2read |
|
181 | 187 | |
|
182 | 188 | |
|
183 | 189 | def readBlock(self): |
|
184 | 190 | """ |
|
185 | 191 | readBlock lee el bloque de datos desde la posicion actual del puntero del archivo |
|
186 | 192 | (self.fp) y actualiza todos los parametros relacionados al bloque de datos |
|
187 | 193 | (metadata + data). La data leida es almacenada en el buffer y el contador del buffer |
|
188 | 194 | es seteado a 0 |
|
189 | 195 | |
|
190 | 196 | Inputs: |
|
191 | 197 | None |
|
192 | 198 | |
|
193 | 199 | Return: |
|
194 | 200 | None |
|
195 | 201 | |
|
196 | 202 | Affected: |
|
197 | 203 | self.profileIndex |
|
198 | 204 | self.datablock |
|
199 | 205 | self.flagIsNewFile |
|
200 | 206 | self.flagIsNewBlock |
|
201 | 207 | self.nTotalBlocks |
|
202 | 208 | |
|
203 | 209 | Exceptions: |
|
204 | 210 | Si un bloque leido no es un bloque valido |
|
205 | 211 | """ |
|
206 | 212 | current_pointer_location = self.fp.tell() |
|
207 | 213 | junk = numpy.fromfile( self.fp, self.dtype, self.blocksize ) |
|
208 | 214 | |
|
209 | 215 | try: |
|
210 | 216 | junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) ) |
|
211 | 217 | except: |
|
212 | 218 | #print "The read block (%3d) has not enough data" %self.nReadBlocks |
|
213 | 219 | |
|
214 | 220 | if self.waitDataBlock(pointer_location=current_pointer_location): |
|
215 | 221 | junk = numpy.fromfile( self.fp, self.dtype, self.blocksize ) |
|
216 | 222 | junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) ) |
|
217 | 223 | # return 0 |
|
218 | 224 | |
|
219 | 225 | junk = numpy.transpose(junk, (2,0,1)) |
|
220 | 226 | self.datablock = junk['real'] + junk['imag']*1j |
|
221 | 227 | |
|
222 | 228 | self.profileIndex = 0 |
|
223 | 229 | |
|
224 | 230 | self.flagIsNewFile = 0 |
|
225 | 231 | self.flagIsNewBlock = 1 |
|
226 | 232 | |
|
227 | 233 | self.nTotalBlocks += 1 |
|
228 | 234 | self.nReadBlocks += 1 |
|
229 | 235 | |
|
230 | 236 | return 1 |
|
231 | 237 | |
|
232 | 238 | def getFirstHeader(self): |
|
233 | 239 | |
|
234 | 240 | self.dataOut.systemHeaderObj = self.systemHeaderObj.copy() |
|
235 | 241 | |
|
236 | 242 | self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy() |
|
237 | 243 | |
|
238 | # self.dataOut.ippSeconds = self.ippSeconds | |
|
244 | if self.nTxs > 1: | |
|
245 | self.dataOut.radarControllerHeaderObj.ippSeconds /= self.nTxs | |
|
239 | 246 | |
|
240 | 247 | # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt |
|
241 | 248 | |
|
242 | 249 | if self.radarControllerHeaderObj.code != None: |
|
243 | 250 | |
|
244 | 251 | self.dataOut.nCode = self.radarControllerHeaderObj.nCode |
|
245 | 252 | |
|
246 | 253 | self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud |
|
247 | 254 | |
|
248 | 255 | self.dataOut.code = self.radarControllerHeaderObj.code |
|
249 | 256 | |
|
250 | 257 | self.dataOut.dtype = self.dtype |
|
251 | 258 | |
|
252 | self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock | |
|
259 | self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs | |
|
260 | ||
|
261 | if self.processingHeaderObj.nHeights % self.nTxs != 0: | |
|
262 | raise ValueError, "nTxs (%d) should be a multiple of nHeights (%d)" %(self.nTxs, self.processingHeaderObj.nHeights) | |
|
253 | 263 | |
|
254 | xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight | |
|
264 | xf = self.processingHeaderObj.firstHeight + int(self.processingHeaderObj.nHeights/self.nTxs)*self.processingHeaderObj.deltaHeight | |
|
255 | 265 | |
|
256 | 266 | self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight) |
|
257 | 267 | |
|
258 | 268 | self.dataOut.channelList = range(self.systemHeaderObj.nChannels) |
|
259 | 269 | |
|
260 | 270 | self.dataOut.nCohInt = self.processingHeaderObj.nCohInt |
|
261 | 271 | |
|
262 | 272 | self.dataOut.flagShiftFFT = False |
|
263 | 273 | |
|
264 | 274 | self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada |
|
265 | 275 | |
|
266 | 276 | self.dataOut.flagDeflipData = False #asumo q la data no esta sin flip |
|
267 | 277 | |
|
268 | 278 | self.dataOut.flagShiftFFT = False |
|
269 | 279 | |
|
270 | 280 | def getData(self): |
|
271 | 281 | """ |
|
272 | 282 | getData obtiene una unidad de datos del buffer de lectura, un perfil, y la copia al objeto self.dataOut |
|
273 | 283 | del tipo "Voltage" con todos los parametros asociados a este (metadata). cuando no hay datos |
|
274 | 284 | en el buffer de lectura es necesario hacer una nueva lectura de los bloques de datos usando |
|
275 | 285 | "readNextBlock" |
|
276 | 286 | |
|
277 | 287 | Ademas incrementa el contador del buffer "self.profileIndex" en 1. |
|
278 | 288 | |
|
279 | 289 | Return: |
|
280 | 290 | |
|
281 | 291 | Si el flag self.getByBlock ha sido seteado el bloque completo es copiado a self.dataOut y el self.profileIndex |
|
282 | 292 | es igual al total de perfiles leidos desde el archivo. |
|
283 | 293 | |
|
284 | 294 | Si self.getByBlock == False: |
|
285 | 295 | |
|
286 | 296 | self.dataOut.data = buffer[:, thisProfile, :] |
|
287 | 297 | |
|
288 | 298 | shape = [nChannels, nHeis] |
|
289 | 299 | |
|
290 | 300 | Si self.getByBlock == True: |
|
291 | 301 | |
|
292 | 302 | self.dataOut.data = buffer[:, :, :] |
|
293 | 303 | |
|
294 | 304 | shape = [nChannels, nProfiles, nHeis] |
|
295 | 305 | |
|
296 | 306 | Variables afectadas: |
|
297 | 307 | self.dataOut |
|
298 | 308 | self.profileIndex |
|
299 | 309 | |
|
300 | 310 | Affected: |
|
301 | 311 | self.dataOut |
|
302 | 312 | self.profileIndex |
|
303 | 313 | self.flagTimeBlock |
|
304 | 314 | self.flagIsNewBlock |
|
305 | 315 | """ |
|
306 | 316 | |
|
307 | 317 | if self.flagNoMoreFiles: |
|
308 | 318 | self.dataOut.flagNoData = True |
|
309 | 319 | print 'Process finished' |
|
310 | 320 | return 0 |
|
311 | 321 | |
|
312 | 322 | self.flagTimeBlock = 0 |
|
313 | 323 | self.flagIsNewBlock = 0 |
|
314 | 324 | |
|
315 | 325 | if self.__hasNotDataInBuffer(): |
|
316 | 326 | |
|
317 | 327 | if not( self.readNextBlock() ): |
|
318 | 328 | return 0 |
|
319 | 329 | |
|
320 | 330 | self.getFirstHeader() |
|
321 | 331 | |
|
322 | 332 | if self.datablock == None: |
|
323 | 333 | self.dataOut.flagNoData = True |
|
324 | 334 | return 0 |
|
325 | 335 | |
|
326 | if self.getByBlock: | |
|
336 | if not self.getByBlock: | |
|
337 | ||
|
338 | """ | |
|
339 | Return profile by profile | |
|
340 | ||
|
341 | If nTxs > 1 then one profile is divided by nTxs and number of total | |
|
342 | blocks is increased by nTxs (nProfiles *= nTxs) | |
|
343 | """ | |
|
344 | if self.nTxs == 1: | |
|
345 | self.dataOut.flagDataAsBlock = False | |
|
346 | self.dataOut.data = self.datablock[:,self.profileIndex,:] | |
|
347 | self.dataOut.profileIndex = self.profileIndex | |
|
348 | ||
|
349 | self.profileIndex += 1 | |
|
350 | ||
|
351 | else: | |
|
352 | self.dataOut.flagDataAsBlock = False | |
|
353 | ||
|
354 | iniHei_ForThisTx = (self.txIndex)*int(self.processingHeaderObj.nHeights/self.nTxs) | |
|
355 | endHei_ForThisTx = (self.txIndex+1)*int(self.processingHeaderObj.nHeights/self.nTxs) | |
|
356 | ||
|
357 | # print iniHei_ForThisTx, endHei_ForThisTx | |
|
358 | ||
|
359 | self.dataOut.data = self.datablock[:, self.profileIndex, iniHei_ForThisTx:endHei_ForThisTx] | |
|
360 | self.dataOut.profileIndex = self.profileIndex*self.nTxs + self.txIndex | |
|
361 | ||
|
362 | self.txIndex += 1 | |
|
363 | ||
|
364 | if self.txIndex == self.nTxs: | |
|
365 | self.txIndex = 0 | |
|
366 | self.profileIndex += 1 | |
|
367 | ||
|
368 | else: | |
|
369 | """ | |
|
370 | Return all block | |
|
371 | """ | |
|
327 | 372 | self.dataOut.flagDataAsBlock = True |
|
328 | 373 | self.dataOut.data = self.datablock |
|
329 | self.profileIndex = self.processingHeaderObj.profilesPerBlock | |
|
330 |
|
|
|
331 | self.dataOut.flagDataAsBlock = False | |
|
332 | self.dataOut.data = self.datablock[:,self.profileIndex,:] | |
|
333 | self.profileIndex += 1 | |
|
374 | self.dataOut.profileIndex = self.processingHeaderObj.profilesPerBlock - 1 | |
|
375 | ||
|
376 | self.profileIndex = self.processingHeaderObj.profilesPerBlock - 1 | |
|
334 | 377 | |
|
335 | 378 | self.dataOut.flagNoData = False |
|
336 | 379 | |
|
337 | 380 | self.getBasicHeader() |
|
338 | 381 | |
|
339 | ||
|
340 | ||
|
341 | 382 | self.dataOut.realtime = self.online |
|
342 | 383 | |
|
343 | 384 | return self.dataOut.data |
|
344 | 385 | |
|
345 | 386 | class VoltageWriter(JRODataWriter, Operation): |
|
346 | 387 | """ |
|
347 | 388 | Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura |
|
348 | 389 | de los datos siempre se realiza por bloques. |
|
349 | 390 | """ |
|
350 | 391 | |
|
351 | 392 | ext = ".r" |
|
352 | 393 | |
|
353 | 394 | optchar = "D" |
|
354 | 395 | |
|
355 | 396 | shapeBuffer = None |
|
356 | 397 | |
|
357 | 398 | |
|
358 | 399 | def __init__(self): |
|
359 | 400 | """ |
|
360 | 401 | Inicializador de la clase VoltageWriter para la escritura de datos de espectros. |
|
361 | 402 | |
|
362 | 403 | Affected: |
|
363 | 404 | self.dataOut |
|
364 | 405 | |
|
365 | 406 | Return: None |
|
366 | 407 | """ |
|
367 | 408 | Operation.__init__(self) |
|
368 | 409 | |
|
369 | 410 | self.nTotalBlocks = 0 |
|
370 | 411 | |
|
371 | 412 | self.profileIndex = 0 |
|
372 | 413 | |
|
373 | 414 | self.isConfig = False |
|
374 | 415 | |
|
375 | 416 | self.fp = None |
|
376 | 417 | |
|
377 | 418 | self.flagIsNewFile = 1 |
|
378 | 419 | |
|
379 | 420 | self.nTotalBlocks = 0 |
|
380 | 421 | |
|
381 | 422 | self.flagIsNewBlock = 0 |
|
382 | 423 | |
|
383 | 424 | self.setFile = None |
|
384 | 425 | |
|
385 | 426 | self.dtype = None |
|
386 | 427 | |
|
387 | 428 | self.path = None |
|
388 | 429 | |
|
389 | 430 | self.filename = None |
|
390 | 431 | |
|
391 | 432 | self.basicHeaderObj = BasicHeader(LOCALTIME) |
|
392 | 433 | |
|
393 | 434 | self.systemHeaderObj = SystemHeader() |
|
394 | 435 | |
|
395 | 436 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
396 | 437 | |
|
397 | 438 | self.processingHeaderObj = ProcessingHeader() |
|
398 | 439 | |
|
399 | 440 | def hasAllDataInBuffer(self): |
|
400 | 441 | if self.profileIndex >= self.processingHeaderObj.profilesPerBlock: |
|
401 | 442 | return 1 |
|
402 | 443 | return 0 |
|
403 | 444 | |
|
404 | 445 | |
|
405 | 446 | def setBlockDimension(self): |
|
406 | 447 | """ |
|
407 | 448 | Obtiene las formas dimensionales del los subbloques de datos que componen un bloque |
|
408 | 449 | |
|
409 | 450 | Affected: |
|
410 | 451 | self.shape_spc_Buffer |
|
411 | 452 | self.shape_cspc_Buffer |
|
412 | 453 | self.shape_dc_Buffer |
|
413 | 454 | |
|
414 | 455 | Return: None |
|
415 | 456 | """ |
|
416 | 457 | self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock, |
|
417 | 458 | self.processingHeaderObj.nHeights, |
|
418 | 459 | self.systemHeaderObj.nChannels) |
|
419 | 460 | |
|
420 | 461 | self.datablock = numpy.zeros((self.systemHeaderObj.nChannels, |
|
421 | 462 | self.processingHeaderObj.profilesPerBlock, |
|
422 | 463 | self.processingHeaderObj.nHeights), |
|
423 | 464 | dtype=numpy.dtype('complex64')) |
|
424 | 465 | |
|
425 | 466 | |
|
426 | 467 | def writeBlock(self): |
|
427 | 468 | """ |
|
428 | 469 | Escribe el buffer en el file designado |
|
429 | 470 | |
|
430 | 471 | Affected: |
|
431 | 472 | self.profileIndex |
|
432 | 473 | self.flagIsNewFile |
|
433 | 474 | self.flagIsNewBlock |
|
434 | 475 | self.nTotalBlocks |
|
435 | 476 | self.blockIndex |
|
436 | 477 | |
|
437 | 478 | Return: None |
|
438 | 479 | """ |
|
439 | 480 | data = numpy.zeros( self.shapeBuffer, self.dtype ) |
|
440 | 481 | |
|
441 | 482 | junk = numpy.transpose(self.datablock, (1,2,0)) |
|
442 | 483 | |
|
443 | 484 | data['real'] = junk.real |
|
444 | 485 | data['imag'] = junk.imag |
|
445 | 486 | |
|
446 | 487 | data = data.reshape( (-1) ) |
|
447 | 488 | |
|
448 | 489 | data.tofile( self.fp ) |
|
449 | 490 | |
|
450 | 491 | self.datablock.fill(0) |
|
451 | 492 | |
|
452 | 493 | self.profileIndex = 0 |
|
453 | 494 | self.flagIsNewFile = 0 |
|
454 | 495 | self.flagIsNewBlock = 1 |
|
455 | 496 | |
|
456 | 497 | self.blockIndex += 1 |
|
457 | 498 | self.nTotalBlocks += 1 |
|
458 | 499 | |
|
459 | 500 | def putData(self): |
|
460 | 501 | """ |
|
461 | 502 | Setea un bloque de datos y luego los escribe en un file |
|
462 | 503 | |
|
463 | 504 | Affected: |
|
464 | 505 | self.flagIsNewBlock |
|
465 | 506 | self.profileIndex |
|
466 | 507 | |
|
467 | 508 | Return: |
|
468 | 509 | 0 : Si no hay data o no hay mas files que puedan escribirse |
|
469 | 510 | 1 : Si se escribio la data de un bloque en un file |
|
470 | 511 | """ |
|
471 | 512 | if self.dataOut.flagNoData: |
|
472 | 513 | return 0 |
|
473 | 514 | |
|
474 | 515 | self.flagIsNewBlock = 0 |
|
475 | 516 | |
|
476 | 517 | if self.dataOut.flagTimeBlock: |
|
477 | 518 | |
|
478 | 519 | self.datablock.fill(0) |
|
479 | 520 | self.profileIndex = 0 |
|
480 | 521 | self.setNextFile() |
|
481 | 522 | |
|
482 | 523 | if self.profileIndex == 0: |
|
483 | 524 | self.setBasicHeader() |
|
484 | 525 | |
|
485 | 526 | self.datablock[:,self.profileIndex,:] = self.dataOut.data |
|
486 | 527 | |
|
487 | 528 | self.profileIndex += 1 |
|
488 | 529 | |
|
489 | 530 | if self.hasAllDataInBuffer(): |
|
490 | 531 | #if self.flagIsNewFile: |
|
491 | 532 | self.writeNextBlock() |
|
492 | 533 | # self.setFirstHeader() |
|
493 | 534 | |
|
494 | 535 | return 1 |
|
495 | 536 | |
|
496 | 537 | def __getProcessFlags(self): |
|
497 | 538 | |
|
498 | 539 | processFlags = 0 |
|
499 | 540 | |
|
500 | 541 | dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
501 | 542 | dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
502 | 543 | dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
503 | 544 | dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
504 | 545 | dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
505 | 546 | dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
506 | 547 | |
|
507 | 548 | dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5] |
|
508 | 549 | |
|
509 | 550 | |
|
510 | 551 | |
|
511 | 552 | datatypeValueList = [PROCFLAG.DATATYPE_CHAR, |
|
512 | 553 | PROCFLAG.DATATYPE_SHORT, |
|
513 | 554 | PROCFLAG.DATATYPE_LONG, |
|
514 | 555 | PROCFLAG.DATATYPE_INT64, |
|
515 | 556 | PROCFLAG.DATATYPE_FLOAT, |
|
516 | 557 | PROCFLAG.DATATYPE_DOUBLE] |
|
517 | 558 | |
|
518 | 559 | |
|
519 | 560 | for index in range(len(dtypeList)): |
|
520 | 561 | if self.dataOut.dtype == dtypeList[index]: |
|
521 | 562 | dtypeValue = datatypeValueList[index] |
|
522 | 563 | break |
|
523 | 564 | |
|
524 | 565 | processFlags += dtypeValue |
|
525 | 566 | |
|
526 | 567 | if self.dataOut.flagDecodeData: |
|
527 | 568 | processFlags += PROCFLAG.DECODE_DATA |
|
528 | 569 | |
|
529 | 570 | if self.dataOut.flagDeflipData: |
|
530 | 571 | processFlags += PROCFLAG.DEFLIP_DATA |
|
531 | 572 | |
|
532 | 573 | if self.dataOut.code != None: |
|
533 | 574 | processFlags += PROCFLAG.DEFINE_PROCESS_CODE |
|
534 | 575 | |
|
535 | 576 | if self.dataOut.nCohInt > 1: |
|
536 | 577 | processFlags += PROCFLAG.COHERENT_INTEGRATION |
|
537 | 578 | |
|
538 | 579 | return processFlags |
|
539 | 580 | |
|
540 | 581 | |
|
541 | 582 | def __getBlockSize(self): |
|
542 | 583 | ''' |
|
543 | 584 | Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage |
|
544 | 585 | ''' |
|
545 | 586 | |
|
546 | 587 | dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
547 | 588 | dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
548 | 589 | dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
549 | 590 | dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
550 | 591 | dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
551 | 592 | dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
552 | 593 | |
|
553 | 594 | dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5] |
|
554 | 595 | datatypeValueList = [1,2,4,8,4,8] |
|
555 | 596 | for index in range(len(dtypeList)): |
|
556 | 597 | if self.dataOut.dtype == dtypeList[index]: |
|
557 | 598 | datatypeValue = datatypeValueList[index] |
|
558 | 599 | break |
|
559 | 600 | |
|
560 | 601 | blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels * self.profilesPerBlock * datatypeValue * 2) |
|
561 | 602 | |
|
562 | 603 | return blocksize |
|
563 | 604 | |
|
564 | 605 | def setFirstHeader(self): |
|
565 | 606 | |
|
566 | 607 | """ |
|
567 | 608 | Obtiene una copia del First Header |
|
568 | 609 | |
|
569 | 610 | Affected: |
|
570 | 611 | self.systemHeaderObj |
|
571 | 612 | self.radarControllerHeaderObj |
|
572 | 613 | self.dtype |
|
573 | 614 | |
|
574 | 615 | Return: |
|
575 | 616 | None |
|
576 | 617 | """ |
|
577 | 618 | |
|
578 | 619 | self.systemHeaderObj = self.dataOut.systemHeaderObj.copy() |
|
579 | 620 | self.systemHeaderObj.nChannels = self.dataOut.nChannels |
|
580 | 621 | self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy() |
|
581 | 622 | |
|
582 | 623 | self.setBasicHeader() |
|
583 | 624 | |
|
584 | 625 | processingHeaderSize = 40 # bytes |
|
585 | 626 | self.processingHeaderObj.dtype = 0 # Voltage |
|
586 | 627 | self.processingHeaderObj.blockSize = self.__getBlockSize() |
|
587 | 628 | self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock |
|
588 | 629 | self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile |
|
589 | 630 | self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows |
|
590 | 631 | self.processingHeaderObj.processFlags = self.__getProcessFlags() |
|
591 | 632 | self.processingHeaderObj.nCohInt = self.dataOut.nCohInt |
|
592 | 633 | self.processingHeaderObj.nIncohInt = 1 # Cuando la data de origen es de tipo Voltage |
|
593 | 634 | self.processingHeaderObj.totalSpectra = 0 # Cuando la data de origen es de tipo Voltage |
|
594 | 635 | |
|
595 | 636 | # if self.dataOut.code != None: |
|
596 | 637 | # self.processingHeaderObj.code = self.dataOut.code |
|
597 | 638 | # self.processingHeaderObj.nCode = self.dataOut.nCode |
|
598 | 639 | # self.processingHeaderObj.nBaud = self.dataOut.nBaud |
|
599 | 640 | # codesize = int(8 + 4 * self.dataOut.nCode * self.dataOut.nBaud) |
|
600 | 641 | # processingHeaderSize += codesize |
|
601 | 642 | |
|
602 | 643 | if self.processingHeaderObj.nWindows != 0: |
|
603 | 644 | self.processingHeaderObj.firstHeight = self.dataOut.heightList[0] |
|
604 | 645 | self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] |
|
605 | 646 | self.processingHeaderObj.nHeights = self.dataOut.nHeights |
|
606 | 647 | self.processingHeaderObj.samplesWin = self.dataOut.nHeights |
|
607 | 648 | processingHeaderSize += 12 |
|
608 | 649 | |
|
609 | 650 | self.processingHeaderObj.size = processingHeaderSize No newline at end of file |
@@ -1,841 +1,890 | |||
|
1 | 1 | import numpy |
|
2 | 2 | |
|
3 | 3 | from jroproc_base import ProcessingUnit, Operation |
|
4 | 4 | from model.data.jrodata import Voltage |
|
5 | 5 | |
|
6 | 6 | |
|
7 | 7 | class VoltageProc(ProcessingUnit): |
|
8 | 8 | |
|
9 | 9 | |
|
10 | 10 | def __init__(self): |
|
11 | 11 | |
|
12 | 12 | ProcessingUnit.__init__(self) |
|
13 | 13 | |
|
14 | 14 | # self.objectDict = {} |
|
15 | 15 | self.dataOut = Voltage() |
|
16 | 16 | self.flip = 1 |
|
17 | 17 | |
|
18 | 18 | def run(self): |
|
19 | 19 | if self.dataIn.type == 'AMISR': |
|
20 | 20 | self.__updateObjFromAmisrInput() |
|
21 | 21 | |
|
22 | 22 | if self.dataIn.type == 'Voltage': |
|
23 | 23 | self.dataOut.copy(self.dataIn) |
|
24 | 24 | |
|
25 | 25 | # self.dataOut.copy(self.dataIn) |
|
26 | 26 | |
|
27 | 27 | def __updateObjFromAmisrInput(self): |
|
28 | 28 | |
|
29 | 29 | self.dataOut.timeZone = self.dataIn.timeZone |
|
30 | 30 | self.dataOut.dstFlag = self.dataIn.dstFlag |
|
31 | 31 | self.dataOut.errorCount = self.dataIn.errorCount |
|
32 | 32 | self.dataOut.useLocalTime = self.dataIn.useLocalTime |
|
33 | 33 | |
|
34 | 34 | self.dataOut.flagNoData = self.dataIn.flagNoData |
|
35 | 35 | self.dataOut.data = self.dataIn.data |
|
36 | 36 | self.dataOut.utctime = self.dataIn.utctime |
|
37 | 37 | self.dataOut.channelList = self.dataIn.channelList |
|
38 | 38 | self.dataOut.timeInterval = self.dataIn.timeInterval |
|
39 | 39 | self.dataOut.heightList = self.dataIn.heightList |
|
40 | 40 | self.dataOut.nProfiles = self.dataIn.nProfiles |
|
41 | 41 | |
|
42 | 42 | self.dataOut.nCohInt = self.dataIn.nCohInt |
|
43 | 43 | self.dataOut.ippSeconds = self.dataIn.ippSeconds |
|
44 | 44 | self.dataOut.frequency = self.dataIn.frequency |
|
45 | 45 | |
|
46 | 46 | self.dataOut.azimuth = self.dataIn.azimuth |
|
47 | 47 | self.dataOut.zenith = self.dataIn.zenith |
|
48 | 48 | |
|
49 | 49 | self.dataOut.beam.codeList = self.dataIn.beam.codeList |
|
50 | 50 | self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList |
|
51 | 51 | self.dataOut.beam.zenithList = self.dataIn.beam.zenithList |
|
52 | 52 | # |
|
53 | 53 | # pass# |
|
54 | 54 | # |
|
55 | 55 | # def init(self): |
|
56 | 56 | # |
|
57 | 57 | # |
|
58 | 58 | # if self.dataIn.type == 'AMISR': |
|
59 | 59 | # self.__updateObjFromAmisrInput() |
|
60 | 60 | # |
|
61 | 61 | # if self.dataIn.type == 'Voltage': |
|
62 | 62 | # self.dataOut.copy(self.dataIn) |
|
63 | 63 | # # No necesita copiar en cada init() los atributos de dataIn |
|
64 | 64 | # # la copia deberia hacerse por cada nuevo bloque de datos |
|
65 | 65 | |
|
66 | 66 | def selectChannels(self, channelList): |
|
67 | 67 | |
|
68 | 68 | channelIndexList = [] |
|
69 | 69 | |
|
70 | 70 | for channel in channelList: |
|
71 | 71 | index = self.dataOut.channelList.index(channel) |
|
72 | 72 | channelIndexList.append(index) |
|
73 | 73 | |
|
74 | 74 | self.selectChannelsByIndex(channelIndexList) |
|
75 | 75 | |
|
76 | 76 | def selectChannelsByIndex(self, channelIndexList): |
|
77 | 77 | """ |
|
78 | 78 | Selecciona un bloque de datos en base a canales segun el channelIndexList |
|
79 | 79 | |
|
80 | 80 | Input: |
|
81 | 81 | channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7] |
|
82 | 82 | |
|
83 | 83 | Affected: |
|
84 | 84 | self.dataOut.data |
|
85 | 85 | self.dataOut.channelIndexList |
|
86 | 86 | self.dataOut.nChannels |
|
87 | 87 | self.dataOut.m_ProcessingHeader.totalSpectra |
|
88 | 88 | self.dataOut.systemHeaderObj.numChannels |
|
89 | 89 | self.dataOut.m_ProcessingHeader.blockSize |
|
90 | 90 | |
|
91 | 91 | Return: |
|
92 | 92 | None |
|
93 | 93 | """ |
|
94 | 94 | |
|
95 | 95 | for channelIndex in channelIndexList: |
|
96 | 96 | if channelIndex not in self.dataOut.channelIndexList: |
|
97 | 97 | print channelIndexList |
|
98 | 98 | raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex |
|
99 | 99 | |
|
100 | 100 | # nChannels = len(channelIndexList) |
|
101 | 101 | if dataOut.flagDataAsBlock: |
|
102 | 102 | """ |
|
103 | 103 | Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis] |
|
104 | 104 | """ |
|
105 | 105 | data = self.dataOut.data[channelIndexList,:,:] |
|
106 | 106 | else: |
|
107 | 107 | data = self.dataOut.data[channelIndexList,:] |
|
108 | 108 | |
|
109 | 109 | self.dataOut.data = data |
|
110 | 110 | self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList] |
|
111 | 111 | # self.dataOut.nChannels = nChannels |
|
112 | 112 | |
|
113 | 113 | return 1 |
|
114 | 114 | |
|
115 | 115 | def selectHeights(self, minHei=None, maxHei=None): |
|
116 | 116 | """ |
|
117 | 117 | Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango |
|
118 | 118 | minHei <= height <= maxHei |
|
119 | 119 | |
|
120 | 120 | Input: |
|
121 | 121 | minHei : valor minimo de altura a considerar |
|
122 | 122 | maxHei : valor maximo de altura a considerar |
|
123 | 123 | |
|
124 | 124 | Affected: |
|
125 | 125 | Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex |
|
126 | 126 | |
|
127 | 127 | Return: |
|
128 | 128 | 1 si el metodo se ejecuto con exito caso contrario devuelve 0 |
|
129 | 129 | """ |
|
130 | 130 | |
|
131 | 131 | if minHei == None: |
|
132 | 132 | minHei = self.dataOut.heightList[0] |
|
133 | 133 | |
|
134 | 134 | if maxHei == None: |
|
135 | 135 | maxHei = self.dataOut.heightList[-1] |
|
136 | 136 | |
|
137 | 137 | if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei): |
|
138 | 138 | raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) |
|
139 | 139 | |
|
140 | 140 | |
|
141 | 141 | if (maxHei > self.dataOut.heightList[-1]): |
|
142 | 142 | maxHei = self.dataOut.heightList[-1] |
|
143 | 143 | # raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) |
|
144 | 144 | |
|
145 | 145 | minIndex = 0 |
|
146 | 146 | maxIndex = 0 |
|
147 | 147 | heights = self.dataOut.heightList |
|
148 | 148 | |
|
149 | 149 | inda = numpy.where(heights >= minHei) |
|
150 | 150 | indb = numpy.where(heights <= maxHei) |
|
151 | 151 | |
|
152 | 152 | try: |
|
153 | 153 | minIndex = inda[0][0] |
|
154 | 154 | except: |
|
155 | 155 | minIndex = 0 |
|
156 | 156 | |
|
157 | 157 | try: |
|
158 | 158 | maxIndex = indb[0][-1] |
|
159 | 159 | except: |
|
160 | 160 | maxIndex = len(heights) |
|
161 | 161 | |
|
162 | 162 | self.selectHeightsByIndex(minIndex, maxIndex) |
|
163 | 163 | |
|
164 | 164 | return 1 |
|
165 | 165 | |
|
166 | 166 | |
|
167 | 167 | def selectHeightsByIndex(self, minIndex, maxIndex): |
|
168 | 168 | """ |
|
169 | 169 | Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango |
|
170 | 170 | minIndex <= index <= maxIndex |
|
171 | 171 | |
|
172 | 172 | Input: |
|
173 | 173 | minIndex : valor de indice minimo de altura a considerar |
|
174 | 174 | maxIndex : valor de indice maximo de altura a considerar |
|
175 | 175 | |
|
176 | 176 | Affected: |
|
177 | 177 | self.dataOut.data |
|
178 | 178 | self.dataOut.heightList |
|
179 | 179 | |
|
180 | 180 | Return: |
|
181 | 181 | 1 si el metodo se ejecuto con exito caso contrario devuelve 0 |
|
182 | 182 | """ |
|
183 | 183 | |
|
184 | 184 | if (minIndex < 0) or (minIndex > maxIndex): |
|
185 | 185 | raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) |
|
186 | 186 | |
|
187 | 187 | if (maxIndex >= self.dataOut.nHeights): |
|
188 |
maxIndex = self.dataOut.nHeights |
|
|
188 | maxIndex = self.dataOut.nHeights | |
|
189 | 189 | # raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) |
|
190 | 190 | |
|
191 | 191 | # nHeights = maxIndex - minIndex + 1 |
|
192 | 192 | |
|
193 | 193 | #voltage |
|
194 | 194 | if self.dataOut.flagDataAsBlock: |
|
195 | 195 | """ |
|
196 | 196 | Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis] |
|
197 | 197 | """ |
|
198 |
data = self.dataOut.data[:,minIndex:maxIndex |
|
|
198 | data = self.dataOut.data[:,minIndex:maxIndex,:] | |
|
199 | 199 | else: |
|
200 |
data = self.dataOut.data[:,minIndex:maxIndex |
|
|
200 | data = self.dataOut.data[:,minIndex:maxIndex] | |
|
201 | 201 | |
|
202 | 202 | # firstHeight = self.dataOut.heightList[minIndex] |
|
203 | 203 | |
|
204 | 204 | self.dataOut.data = data |
|
205 |
self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex |
|
|
205 | self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex] | |
|
206 | 206 | |
|
207 | 207 | return 1 |
|
208 | 208 | |
|
209 | 209 | |
|
210 | 210 | def filterByHeights(self, window, axis=1): |
|
211 | 211 | |
|
212 | 212 | deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] |
|
213 | 213 | |
|
214 | 214 | if window == None: |
|
215 | 215 | window = (self.dataOut.radarControllerHeaderObj.txA/self.dataOut.radarControllerHeaderObj.nBaud) / deltaHeight |
|
216 | 216 | |
|
217 | 217 | newdelta = deltaHeight * window |
|
218 | 218 | r = self.dataOut.nHeights % window |
|
219 | 219 | |
|
220 | 220 | if dataOut.flagDataAsBlock: |
|
221 | 221 | """ |
|
222 | 222 | Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis] |
|
223 | 223 | """ |
|
224 | 224 | buffer = self.dataOut.data[:, :, 0:self.dataOut.nHeights-r] |
|
225 | 225 | buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nHeights,self.dataOut.nHeights/window,window) |
|
226 | 226 | buffer = numpy.sum(buffer,3) |
|
227 | 227 | |
|
228 | 228 | else: |
|
229 | 229 | buffer = self.dataOut.data[:,0:self.dataOut.nHeights-r] |
|
230 | 230 | buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nHeights/window,window) |
|
231 | 231 | buffer = numpy.sum(buffer,2) |
|
232 | 232 | |
|
233 | 233 | self.dataOut.data = buffer.copy() |
|
234 | 234 | self.dataOut.heightList = numpy.arange(self.dataOut.heightList[0],newdelta*(self.dataOut.nHeights-r)/window,newdelta) |
|
235 | 235 | self.dataOut.windowOfFilter = window |
|
236 | 236 | |
|
237 | 237 | return 1 |
|
238 | 238 | |
|
239 | 239 | def deFlip(self, channelList = []): |
|
240 | 240 | |
|
241 | 241 | data = self.dataOut.data.copy() |
|
242 | 242 | |
|
243 | 243 | if self.dataOut.flagDataAsBlock: |
|
244 | 244 | flip = self.flip |
|
245 | 245 | profileList = range(self.dataOut.nProfiles) |
|
246 | 246 | |
|
247 | 247 | if channelList == []: |
|
248 | 248 | for thisProfile in profileList: |
|
249 | 249 | data[:,thisProfile,:] = data[:,thisProfile,:]*flip |
|
250 | 250 | flip *= -1.0 |
|
251 | 251 | else: |
|
252 | 252 | for thisChannel in channelList: |
|
253 | 253 | for thisProfile in profileList: |
|
254 | 254 | data[thisChannel,thisProfile,:] = data[thisChannel,thisProfile,:]*flip |
|
255 | 255 | flip *= -1.0 |
|
256 | 256 | |
|
257 | 257 | self.flip = flip |
|
258 | 258 | |
|
259 | 259 | else: |
|
260 | 260 | if channelList == []: |
|
261 | 261 | data[:,:] = data[:,:]*self.flip |
|
262 | 262 | else: |
|
263 | 263 | for thisChannel in channelList: |
|
264 | 264 | data[thisChannel,:] = data[thisChannel,:]*self.flip |
|
265 | 265 | |
|
266 | 266 | self.flip *= -1. |
|
267 | 267 | |
|
268 | 268 | self.dataOut.data = data |
|
269 | 269 | |
|
270 | 270 | |
|
271 | 271 | |
|
272 | 272 | def setRadarFrequency(self, frequency=None): |
|
273 | 273 | |
|
274 | 274 | if frequency != None: |
|
275 | 275 | self.dataOut.frequency = frequency |
|
276 | 276 | |
|
277 | 277 | return 1 |
|
278 | 278 | |
|
279 | 279 | class CohInt(Operation): |
|
280 | 280 | |
|
281 | 281 | isConfig = False |
|
282 | 282 | |
|
283 | 283 | __profIndex = 0 |
|
284 | 284 | __withOverapping = False |
|
285 | 285 | |
|
286 | 286 | __byTime = False |
|
287 | 287 | __initime = None |
|
288 | 288 | __lastdatatime = None |
|
289 | 289 | __integrationtime = None |
|
290 | 290 | |
|
291 | 291 | __buffer = None |
|
292 | 292 | |
|
293 | 293 | __dataReady = False |
|
294 | 294 | |
|
295 | 295 | n = None |
|
296 | 296 | |
|
297 | 297 | |
|
298 | 298 | def __init__(self): |
|
299 | 299 | |
|
300 | 300 | Operation.__init__(self) |
|
301 | 301 | |
|
302 | 302 | # self.isConfig = False |
|
303 | 303 | |
|
304 | 304 | def setup(self, n=None, timeInterval=None, overlapping=False, byblock=False): |
|
305 | 305 | """ |
|
306 | 306 | Set the parameters of the integration class. |
|
307 | 307 | |
|
308 | 308 | Inputs: |
|
309 | 309 | |
|
310 | 310 | n : Number of coherent integrations |
|
311 | 311 | timeInterval : Time of integration. If the parameter "n" is selected this one does not work |
|
312 | 312 | overlapping : |
|
313 | 313 | |
|
314 | 314 | """ |
|
315 | 315 | |
|
316 | 316 | self.__initime = None |
|
317 | 317 | self.__lastdatatime = 0 |
|
318 | 318 | self.__buffer = None |
|
319 | 319 | self.__dataReady = False |
|
320 | 320 | self.byblock = byblock |
|
321 | 321 | |
|
322 | 322 | if n == None and timeInterval == None: |
|
323 | 323 | raise ValueError, "n or timeInterval should be specified ..." |
|
324 | 324 | |
|
325 | 325 | if n != None: |
|
326 | 326 | self.n = n |
|
327 | 327 | self.__byTime = False |
|
328 | 328 | else: |
|
329 | 329 | self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line |
|
330 | 330 | self.n = 9999 |
|
331 | 331 | self.__byTime = True |
|
332 | 332 | |
|
333 | 333 | if overlapping: |
|
334 | 334 | self.__withOverapping = True |
|
335 | 335 | self.__buffer = None |
|
336 | 336 | else: |
|
337 | 337 | self.__withOverapping = False |
|
338 | 338 | self.__buffer = 0 |
|
339 | 339 | |
|
340 | 340 | self.__profIndex = 0 |
|
341 | 341 | |
|
342 | 342 | def putData(self, data): |
|
343 | 343 | |
|
344 | 344 | """ |
|
345 | 345 | Add a profile to the __buffer and increase in one the __profileIndex |
|
346 | 346 | |
|
347 | 347 | """ |
|
348 | 348 | |
|
349 | 349 | if not self.__withOverapping: |
|
350 | 350 | self.__buffer += data.copy() |
|
351 | 351 | self.__profIndex += 1 |
|
352 | 352 | return |
|
353 | 353 | |
|
354 | 354 | #Overlapping data |
|
355 | 355 | nChannels, nHeis = data.shape |
|
356 | 356 | data = numpy.reshape(data, (1, nChannels, nHeis)) |
|
357 | 357 | |
|
358 | 358 | #If the buffer is empty then it takes the data value |
|
359 | 359 | if self.__buffer == None: |
|
360 | 360 | self.__buffer = data |
|
361 | 361 | self.__profIndex += 1 |
|
362 | 362 | return |
|
363 | 363 | |
|
364 | 364 | #If the buffer length is lower than n then stakcing the data value |
|
365 | 365 | if self.__profIndex < self.n: |
|
366 | 366 | self.__buffer = numpy.vstack((self.__buffer, data)) |
|
367 | 367 | self.__profIndex += 1 |
|
368 | 368 | return |
|
369 | 369 | |
|
370 | 370 | #If the buffer length is equal to n then replacing the last buffer value with the data value |
|
371 | 371 | self.__buffer = numpy.roll(self.__buffer, -1, axis=0) |
|
372 | 372 | self.__buffer[self.n-1] = data |
|
373 | 373 | self.__profIndex = self.n |
|
374 | 374 | return |
|
375 | 375 | |
|
376 | 376 | |
|
377 | 377 | def pushData(self): |
|
378 | 378 | """ |
|
379 | 379 | Return the sum of the last profiles and the profiles used in the sum. |
|
380 | 380 | |
|
381 | 381 | Affected: |
|
382 | 382 | |
|
383 | 383 | self.__profileIndex |
|
384 | 384 | |
|
385 | 385 | """ |
|
386 | 386 | |
|
387 | 387 | if not self.__withOverapping: |
|
388 | 388 | data = self.__buffer |
|
389 | 389 | n = self.__profIndex |
|
390 | 390 | |
|
391 | 391 | self.__buffer = 0 |
|
392 | 392 | self.__profIndex = 0 |
|
393 | 393 | |
|
394 | 394 | return data, n |
|
395 | 395 | |
|
396 | 396 | #Integration with Overlapping |
|
397 | 397 | data = numpy.sum(self.__buffer, axis=0) |
|
398 | 398 | n = self.__profIndex |
|
399 | 399 | |
|
400 | 400 | return data, n |
|
401 | 401 | |
|
402 | 402 | def byProfiles(self, data): |
|
403 | 403 | |
|
404 | 404 | self.__dataReady = False |
|
405 | 405 | avgdata = None |
|
406 | 406 | # n = None |
|
407 | 407 | |
|
408 | 408 | self.putData(data) |
|
409 | 409 | |
|
410 | 410 | if self.__profIndex == self.n: |
|
411 | 411 | |
|
412 | 412 | avgdata, n = self.pushData() |
|
413 | 413 | self.__dataReady = True |
|
414 | 414 | |
|
415 | 415 | return avgdata |
|
416 | 416 | |
|
417 | 417 | def byTime(self, data, datatime): |
|
418 | 418 | |
|
419 | 419 | self.__dataReady = False |
|
420 | 420 | avgdata = None |
|
421 | 421 | n = None |
|
422 | 422 | |
|
423 | 423 | self.putData(data) |
|
424 | 424 | |
|
425 | 425 | if (datatime - self.__initime) >= self.__integrationtime: |
|
426 | 426 | avgdata, n = self.pushData() |
|
427 | 427 | self.n = n |
|
428 | 428 | self.__dataReady = True |
|
429 | 429 | |
|
430 | 430 | return avgdata |
|
431 | 431 | |
|
432 | 432 | def integrate(self, data, datatime=None): |
|
433 | 433 | |
|
434 | 434 | if self.__initime == None: |
|
435 | 435 | self.__initime = datatime |
|
436 | 436 | |
|
437 | 437 | if self.__byTime: |
|
438 | 438 | avgdata = self.byTime(data, datatime) |
|
439 | 439 | else: |
|
440 | 440 | avgdata = self.byProfiles(data) |
|
441 | 441 | |
|
442 | 442 | |
|
443 | 443 | self.__lastdatatime = datatime |
|
444 | 444 | |
|
445 | 445 | if avgdata == None: |
|
446 | 446 | return None, None |
|
447 | 447 | |
|
448 | 448 | avgdatatime = self.__initime |
|
449 | 449 | |
|
450 | 450 | deltatime = datatime -self.__lastdatatime |
|
451 | 451 | |
|
452 | 452 | if not self.__withOverapping: |
|
453 | 453 | self.__initime = datatime |
|
454 | 454 | else: |
|
455 | 455 | self.__initime += deltatime |
|
456 | 456 | |
|
457 | 457 | return avgdata, avgdatatime |
|
458 | 458 | |
|
459 | 459 | def integrateByBlock(self, dataOut): |
|
460 | 460 | |
|
461 | 461 | times = int(dataOut.data.shape[1]/self.n) |
|
462 | 462 | avgdata = numpy.zeros((dataOut.nChannels, times, dataOut.nHeights), dtype=numpy.complex) |
|
463 | 463 | |
|
464 | 464 | id_min = 0 |
|
465 | 465 | id_max = self.n |
|
466 | 466 | |
|
467 | 467 | for i in range(times): |
|
468 | 468 | junk = dataOut.data[:,id_min:id_max,:] |
|
469 | 469 | avgdata[:,i,:] = junk.sum(axis=1) |
|
470 | 470 | id_min += self.n |
|
471 | 471 | id_max += self.n |
|
472 | 472 | |
|
473 | 473 | timeInterval = dataOut.ippSeconds*self.n |
|
474 | 474 | avgdatatime = (times - 1) * timeInterval + dataOut.utctime |
|
475 | 475 | self.__dataReady = True |
|
476 | 476 | return avgdata, avgdatatime |
|
477 | 477 | |
|
478 | 478 | def run(self, dataOut, **kwargs): |
|
479 | 479 | |
|
480 | 480 | if not self.isConfig: |
|
481 | 481 | self.setup(**kwargs) |
|
482 | 482 | self.isConfig = True |
|
483 | 483 | |
|
484 | 484 | if dataOut.flagDataAsBlock: |
|
485 | 485 | """ |
|
486 | 486 | Si la data es leida por bloques, dimension = [nChannels, nProfiles, nHeis] |
|
487 | 487 | """ |
|
488 | 488 | avgdata, avgdatatime = self.integrateByBlock(dataOut) |
|
489 | 489 | else: |
|
490 | 490 | avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime) |
|
491 | 491 | |
|
492 | 492 | # dataOut.timeInterval *= n |
|
493 | 493 | dataOut.flagNoData = True |
|
494 | 494 | |
|
495 | 495 | if self.__dataReady: |
|
496 | 496 | dataOut.data = avgdata |
|
497 | 497 | dataOut.nCohInt *= self.n |
|
498 | 498 | dataOut.utctime = avgdatatime |
|
499 | 499 | # dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt |
|
500 | 500 | dataOut.flagNoData = False |
|
501 | 501 | |
|
502 | 502 | class Decoder(Operation): |
|
503 | 503 | |
|
504 | 504 | isConfig = False |
|
505 | 505 | __profIndex = 0 |
|
506 | 506 | |
|
507 | 507 | code = None |
|
508 | 508 | |
|
509 | 509 | nCode = None |
|
510 | 510 | nBaud = None |
|
511 | 511 | |
|
512 | 512 | |
|
513 | 513 | def __init__(self): |
|
514 | 514 | |
|
515 | 515 | Operation.__init__(self) |
|
516 | 516 | |
|
517 | 517 | self.times = None |
|
518 | 518 | self.osamp = None |
|
519 | 519 | # self.__setValues = False |
|
520 | 520 | self.isConfig = False |
|
521 | 521 | |
|
522 | 522 | def setup(self, code, osamp, dataOut): |
|
523 | 523 | |
|
524 | 524 | self.__profIndex = 0 |
|
525 | 525 | |
|
526 | 526 | self.code = code |
|
527 | 527 | |
|
528 | 528 | self.nCode = len(code) |
|
529 | 529 | self.nBaud = len(code[0]) |
|
530 | 530 | |
|
531 | 531 | if (osamp != None) and (osamp >1): |
|
532 | 532 | self.osamp = osamp |
|
533 | 533 | self.code = numpy.repeat(code, repeats=self.osamp, axis=1) |
|
534 | 534 | self.nBaud = self.nBaud*self.osamp |
|
535 | 535 | |
|
536 | 536 | self.__nChannels = dataOut.nChannels |
|
537 | 537 | self.__nProfiles = dataOut.nProfiles |
|
538 | 538 | self.__nHeis = dataOut.nHeights |
|
539 | 539 | |
|
540 | 540 | if dataOut.flagDataAsBlock: |
|
541 | 541 | |
|
542 | self.ndatadec = self.__nHeis - self.nBaud + 1 | |
|
542 | self.ndatadec = self.__nHeis #- self.nBaud + 1 | |
|
543 | 543 | |
|
544 | 544 | self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex) |
|
545 | 545 | |
|
546 | 546 | else: |
|
547 | 547 | |
|
548 | 548 | __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex) |
|
549 | 549 | |
|
550 | 550 | __codeBuffer[:,0:self.nBaud] = self.code |
|
551 | 551 | |
|
552 | 552 | self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1)) |
|
553 | 553 | |
|
554 | self.ndatadec = self.__nHeis - self.nBaud + 1 | |
|
554 | self.ndatadec = self.__nHeis #- self.nBaud + 1 | |
|
555 | 555 | |
|
556 | 556 | self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex) |
|
557 | 557 | |
|
558 | 558 | def convolutionInFreq(self, data): |
|
559 | 559 | |
|
560 | 560 | fft_code = self.fft_code[self.__profIndex].reshape(1,-1) |
|
561 | 561 | |
|
562 | 562 | fft_data = numpy.fft.fft(data, axis=1) |
|
563 | 563 | |
|
564 | 564 | conv = fft_data*fft_code |
|
565 | 565 | |
|
566 | 566 | data = numpy.fft.ifft(conv,axis=1) |
|
567 | 567 | |
|
568 |
datadec = data[:,: |
|
|
568 | datadec = data#[:,:] | |
|
569 | 569 | |
|
570 | 570 | return datadec |
|
571 | 571 | |
|
572 | 572 | def convolutionInFreqOpt(self, data): |
|
573 | 573 | |
|
574 | 574 | fft_code = self.fft_code[self.__profIndex].reshape(1,-1) |
|
575 | 575 | |
|
576 | 576 | data = cfunctions.decoder(fft_code, data) |
|
577 | 577 | |
|
578 |
datadec = data[:,: |
|
|
578 | datadec = data#[:,:] | |
|
579 | 579 | |
|
580 | 580 | return datadec |
|
581 | 581 | |
|
582 | 582 | def convolutionInTime(self, data): |
|
583 | 583 | |
|
584 | 584 | code = self.code[self.__profIndex] |
|
585 | 585 | |
|
586 | 586 | for i in range(self.__nChannels): |
|
587 |
self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode=' |
|
|
587 | self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='same') | |
|
588 | 588 | |
|
589 | 589 | return self.datadecTime |
|
590 | 590 | |
|
591 | 591 | def convolutionByBlockInTime(self, data): |
|
592 | 592 | |
|
593 | 593 | repetitions = self.__nProfiles / self.nCode |
|
594 | 594 | |
|
595 | 595 | junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize)) |
|
596 | 596 | junk = junk.flatten() |
|
597 | 597 | code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud)) |
|
598 | 598 | |
|
599 | 599 | for i in range(self.__nChannels): |
|
600 | 600 | for j in range(self.__nProfiles): |
|
601 |
self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode=' |
|
|
601 | self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='same') | |
|
602 | 602 | |
|
603 | 603 | return self.datadecTime |
|
604 | 604 | |
|
605 | 605 | def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, times=None, osamp=None): |
|
606 | 606 | |
|
607 | 607 | if not self.isConfig: |
|
608 | 608 | |
|
609 | 609 | if code == None: |
|
610 | 610 | code = dataOut.code |
|
611 | 611 | else: |
|
612 | 612 | code = numpy.array(code).reshape(nCode,nBaud) |
|
613 | 613 | |
|
614 | 614 | self.setup(code, osamp, dataOut) |
|
615 | 615 | |
|
616 | 616 | self.isConfig = True |
|
617 | 617 | |
|
618 | 618 | if dataOut.flagDataAsBlock: |
|
619 | 619 | """ |
|
620 | 620 | Decoding when data have been read as block, |
|
621 | 621 | """ |
|
622 | 622 | datadec = self.convolutionByBlockInTime(dataOut.data) |
|
623 | 623 | |
|
624 | 624 | else: |
|
625 | 625 | """ |
|
626 | 626 | Decoding when data have been read profile by profile |
|
627 | 627 | """ |
|
628 | 628 | if mode == 0: |
|
629 | 629 | datadec = self.convolutionInTime(dataOut.data) |
|
630 | 630 | |
|
631 | 631 | if mode == 1: |
|
632 | 632 | datadec = self.convolutionInFreq(dataOut.data) |
|
633 | 633 | |
|
634 | 634 | if mode == 2: |
|
635 | 635 | datadec = self.convolutionInFreqOpt(dataOut.data) |
|
636 | 636 | |
|
637 | 637 | dataOut.code = code |
|
638 | 638 | dataOut.nCode = nCode |
|
639 | 639 | dataOut.nBaud = nBaud |
|
640 | 640 | dataOut.radarControllerHeaderObj.code = code |
|
641 | 641 | dataOut.radarControllerHeaderObj.nCode = nCode |
|
642 | 642 | dataOut.radarControllerHeaderObj.nBaud = nBaud |
|
643 | 643 | |
|
644 | 644 | dataOut.data = datadec |
|
645 | 645 | |
|
646 | 646 | dataOut.heightList = dataOut.heightList[0:self.ndatadec] |
|
647 | 647 | |
|
648 | 648 | dataOut.flagDecodeData = True #asumo q la data esta decodificada |
|
649 | 649 | |
|
650 | 650 | if self.__profIndex == self.nCode-1: |
|
651 | 651 | self.__profIndex = 0 |
|
652 | 652 | return 1 |
|
653 | 653 | |
|
654 | 654 | self.__profIndex += 1 |
|
655 | 655 | |
|
656 | 656 | return 1 |
|
657 | 657 | # dataOut.flagDeflipData = True #asumo q la data no esta sin flip |
|
658 | 658 | |
|
659 | 659 | |
|
660 | 660 | class ProfileConcat(Operation): |
|
661 | 661 | |
|
662 | 662 | isConfig = False |
|
663 | 663 | buffer = None |
|
664 | 664 | |
|
665 | 665 | def __init__(self): |
|
666 | 666 | |
|
667 | 667 | Operation.__init__(self) |
|
668 | 668 | self.profileIndex = 0 |
|
669 | 669 | |
|
670 | 670 | def reset(self): |
|
671 | 671 | self.buffer = numpy.zeros_like(self.buffer) |
|
672 | 672 | self.start_index = 0 |
|
673 | 673 | self.times = 1 |
|
674 | 674 | |
|
675 | 675 | def setup(self, data, m, n=1): |
|
676 | 676 | self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0])) |
|
677 |
self. |
|
|
677 | self.nHeights = data.nHeights | |
|
678 | 678 | self.start_index = 0 |
|
679 | 679 | self.times = 1 |
|
680 | 680 | |
|
681 | 681 | def concat(self, data): |
|
682 | 682 | |
|
683 | 683 | self.buffer[:,self.start_index:self.profiles*self.times] = data.copy() |
|
684 |
self.start_index = self.start_index + self. |
|
|
684 | self.start_index = self.start_index + self.nHeights | |
|
685 | 685 | |
|
686 | 686 | def run(self, dataOut, m): |
|
687 | 687 | |
|
688 | 688 | dataOut.flagNoData = True |
|
689 | 689 | |
|
690 | 690 | if not self.isConfig: |
|
691 | 691 | self.setup(dataOut.data, m, 1) |
|
692 | 692 | self.isConfig = True |
|
693 | 693 | |
|
694 | 694 | if dataOut.flagDataAsBlock: |
|
695 | 695 | |
|
696 |
raise ValueError, "ProfileConcat can only be used when voltage have been read profile by profi |
|
|
696 | raise ValueError, "ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False" | |
|
697 | 697 | |
|
698 | 698 | else: |
|
699 | 699 | self.concat(dataOut.data) |
|
700 | 700 | self.times += 1 |
|
701 | 701 | if self.times > m: |
|
702 | 702 | dataOut.data = self.buffer |
|
703 | 703 | self.reset() |
|
704 | 704 | dataOut.flagNoData = False |
|
705 | 705 | # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas |
|
706 | 706 | deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] |
|
707 |
xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * |
|
|
707 | xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * m | |
|
708 | 708 | dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight) |
|
709 | dataOut.ippSeconds *= m | |
|
709 | 710 | |
|
710 | 711 | class ProfileSelector(Operation): |
|
711 | 712 | |
|
712 | 713 | profileIndex = None |
|
713 | 714 | # Tamanho total de los perfiles |
|
714 | 715 | nProfiles = None |
|
715 | 716 | |
|
716 | 717 | def __init__(self): |
|
717 | 718 | |
|
718 | 719 | Operation.__init__(self) |
|
719 | 720 | self.profileIndex = 0 |
|
720 | 721 | |
|
721 | 722 | def incIndex(self): |
|
723 | ||
|
722 | 724 | self.profileIndex += 1 |
|
723 | 725 | |
|
724 | 726 | if self.profileIndex >= self.nProfiles: |
|
725 | 727 | self.profileIndex = 0 |
|
726 | 728 | |
|
727 | def isProfileInRange(self, minIndex, maxIndex): | |
|
729 | def isThisProfileInRange(self, profileIndex, minIndex, maxIndex): | |
|
728 | 730 | |
|
729 |
if |
|
|
731 | if profileIndex < minIndex: | |
|
730 | 732 | return False |
|
731 | 733 | |
|
732 |
if |
|
|
734 | if profileIndex > maxIndex: | |
|
733 | 735 | return False |
|
734 | 736 | |
|
735 | 737 | return True |
|
736 | 738 | |
|
737 | def isProfileInList(self, profileList): | |
|
739 | def isThisProfileInList(self, profileIndex, profileList): | |
|
738 | 740 | |
|
739 |
if |
|
|
741 | if profileIndex not in profileList: | |
|
740 | 742 | return False |
|
741 | 743 | |
|
742 | 744 | return True |
|
743 | 745 | |
|
744 | def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False): | |
|
746 | def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False, rangeList = None): | |
|
745 | 747 | |
|
746 | 748 | """ |
|
747 | 749 | ProfileSelector: |
|
748 | 750 | |
|
751 | Inputs: | |
|
752 | profileList : Index of profiles selected. Example: profileList = (0,1,2,7,8) | |
|
753 | ||
|
754 | profileRangeList : Minimum and maximum profile indexes. Example: profileRangeList = (4, 30) | |
|
755 | ||
|
756 | rangeList : List of profile ranges. Example: rangeList = ((4, 30), (32, 64), (128, 256)) | |
|
757 | ||
|
749 | 758 | """ |
|
750 | 759 | |
|
751 | 760 | dataOut.flagNoData = True |
|
752 | 761 | self.nProfiles = dataOut.nProfiles |
|
753 | 762 | |
|
754 | 763 | if dataOut.flagDataAsBlock: |
|
755 | 764 | """ |
|
756 | 765 | data dimension = [nChannels, nProfiles, nHeis] |
|
757 | 766 | """ |
|
758 | 767 | if profileList != None: |
|
759 | 768 | dataOut.data = dataOut.data[:,profileList,:] |
|
760 | 769 | dataOut.nProfiles = len(profileList) |
|
770 | dataOut.profileIndex = dataOut.nProfiles - 1 | |
|
761 | 771 | else: |
|
762 |
|
|
|
763 |
|
|
|
764 | dataOut.data = dataOut.data[:,pmin:pmax+1,:] | |
|
765 | dataOut.nProfiles = pmax - pmin + 1 | |
|
766 | ||
|
772 | minIndex = profileRangeList[0] | |
|
773 | maxIndex = profileRangeList[1] | |
|
774 | ||
|
775 | dataOut.data = dataOut.data[:,minIndex:maxIndex+1,:] | |
|
776 | dataOut.nProfiles = maxIndex - minIndex + 1 | |
|
777 | dataOut.profileIndex = dataOut.nProfiles - 1 | |
|
767 | 778 | |
|
768 | 779 | dataOut.flagNoData = False |
|
769 | self.profileIndex = 0 | |
|
770 | 780 | |
|
771 | 781 | return True |
|
772 | 782 | |
|
773 | 783 | else: |
|
774 | 784 | """ |
|
775 | 785 | data dimension = [nChannels, nHeis] |
|
776 | 786 | |
|
777 | 787 | """ |
|
778 | 788 | if profileList != None: |
|
779 | 789 | |
|
780 |
|
|
|
790 | dataOut.nProfiles = len(profileList) | |
|
791 | ||
|
792 | if self.isThisProfileInList(dataOut.profileIndex, profileList): | |
|
781 | 793 | dataOut.flagNoData = False |
|
794 | dataOut.profileIndex = self.profileIndex | |
|
782 | 795 | |
|
783 | self.incIndex() | |
|
784 |
return |
|
|
796 | self.incIndex() | |
|
797 | return True | |
|
785 | 798 | |
|
786 | 799 | |
|
787 | 800 | if profileRangeList != None: |
|
788 | 801 | |
|
789 | 802 | minIndex = profileRangeList[0] |
|
790 | 803 | maxIndex = profileRangeList[1] |
|
791 | if self.isProfileInRange(minIndex, maxIndex): | |
|
804 | ||
|
805 | dataOut.nProfiles = maxIndex - minIndex + 1 | |
|
806 | ||
|
807 | if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex): | |
|
792 | 808 | dataOut.flagNoData = False |
|
809 | dataOut.profileIndex = self.profileIndex | |
|
793 | 810 | |
|
794 | self.incIndex() | |
|
795 |
return |
|
|
811 | self.incIndex() | |
|
812 | return True | |
|
796 | 813 | |
|
814 | if rangeList != None: | |
|
815 | ||
|
816 | nProfiles = 0 | |
|
817 | ||
|
818 | for thisRange in rangeList: | |
|
819 | minIndex = thisRange[0] | |
|
820 | maxIndex = thisRange[1] | |
|
821 | ||
|
822 | nProfiles += maxIndex - minIndex + 1 | |
|
823 | ||
|
824 | dataOut.nProfiles = nProfiles | |
|
825 | ||
|
826 | for thisRange in rangeList: | |
|
827 | ||
|
828 | minIndex = thisRange[0] | |
|
829 | maxIndex = thisRange[1] | |
|
830 | ||
|
831 | if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex): | |
|
832 | ||
|
833 | # print "profileIndex = ", dataOut.profileIndex | |
|
834 | ||
|
835 | dataOut.flagNoData = False | |
|
836 | dataOut.profileIndex = self.profileIndex | |
|
837 | ||
|
838 | self.incIndex() | |
|
839 | break | |
|
840 | return True | |
|
841 | ||
|
842 | ||
|
797 | 843 | if beam != None: #beam is only for AMISR data |
|
798 | if self.isProfileInList(dataOut.beamRangeDict[beam]): | |
|
844 | if self.isThisProfileInList(dataOut.profileIndex, dataOut.beamRangeDict[beam]): | |
|
799 | 845 | dataOut.flagNoData = False |
|
846 | dataOut.profileIndex = self.profileIndex | |
|
800 | 847 | |
|
801 | self.incIndex() | |
|
848 | self.incIndex() | |
|
802 | 849 | return 1 |
|
803 | 850 | |
|
804 | 851 | raise ValueError, "ProfileSelector needs profileList or profileRangeList" |
|
805 | 852 | |
|
806 | 853 | return 0 |
|
807 | 854 | |
|
808 | 855 | |
|
809 | 856 | |
|
810 | 857 | class Reshaper(Operation): |
|
811 | 858 | |
|
812 | 859 | def __init__(self): |
|
813 | 860 | |
|
814 | 861 | Operation.__init__(self) |
|
815 | 862 | self.updateNewHeights = True |
|
816 | 863 | |
|
817 | 864 | def run(self, dataOut, shape): |
|
818 | 865 | |
|
819 | 866 | if not dataOut.flagDataAsBlock: |
|
820 | 867 | raise ValueError, "Reshaper can only be used when voltage have been read as Block, getBlock = True" |
|
821 | 868 | |
|
822 | 869 | if len(shape) != 3: |
|
823 | 870 | raise ValueError, "shape len should be equal to 3, (nChannels, nProfiles, nHeis)" |
|
824 | 871 | |
|
825 | 872 | shape_tuple = tuple(shape) |
|
826 | 873 | dataOut.data = numpy.reshape(dataOut.data, shape_tuple) |
|
827 | 874 | dataOut.flagNoData = False |
|
828 | 875 | |
|
829 | 876 | if self.updateNewHeights: |
|
830 | 877 | |
|
831 | 878 | old_nheights = dataOut.nHeights |
|
832 | 879 | new_nheights = dataOut.data.shape[2] |
|
833 | 880 | factor = 1.0*new_nheights / old_nheights |
|
834 | 881 | |
|
835 | 882 | deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] |
|
836 | 883 | |
|
837 | 884 | xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * factor |
|
838 | 885 | |
|
839 | 886 | dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight) |
|
840 | 887 | |
|
841 | dataOut.nProfiles = dataOut.data.shape[1] No newline at end of file | |
|
888 | dataOut.nProfiles = dataOut.data.shape[1] | |
|
889 | ||
|
890 | dataOut.ippSeconds *= factor No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now