@@ -1,660 +1,660 | |||
|
1 | 1 | '''' |
|
2 | 2 | Created on Set 9, 2015 |
|
3 | 3 | |
|
4 | 4 | @author: roj-idl71 Karim Kuyeng |
|
5 | 5 | |
|
6 | 6 | @update: 2021, Joab Apaza |
|
7 | 7 | ''' |
|
8 | 8 | |
|
9 | 9 | import os |
|
10 | 10 | import sys |
|
11 | 11 | import glob |
|
12 | 12 | import fnmatch |
|
13 | 13 | import datetime |
|
14 | 14 | import time |
|
15 | 15 | import re |
|
16 | 16 | import h5py |
|
17 | 17 | import numpy |
|
18 | 18 | |
|
19 | 19 | try: |
|
20 | 20 | from gevent import sleep |
|
21 | 21 | except: |
|
22 | 22 | from time import sleep |
|
23 | 23 | |
|
24 | 24 | from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader |
|
25 | 25 | from schainpy.model.data.jrodata import Voltage |
|
26 | 26 | from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator |
|
27 | 27 | from numpy import imag |
|
28 | 28 | |
|
29 | 29 | |
|
30 | 30 | class AMISRReader(ProcessingUnit): |
|
31 | 31 | ''' |
|
32 | 32 | classdocs |
|
33 | 33 | ''' |
|
34 | 34 | |
|
35 | 35 | def __init__(self): |
|
36 | 36 | ''' |
|
37 | 37 | Constructor |
|
38 | 38 | ''' |
|
39 | 39 | |
|
40 | 40 | ProcessingUnit.__init__(self) |
|
41 | 41 | |
|
42 | 42 | self.set = None |
|
43 | 43 | self.subset = None |
|
44 | 44 | self.extension_file = '.h5' |
|
45 | 45 | self.dtc_str = 'dtc' |
|
46 | 46 | self.dtc_id = 0 |
|
47 | 47 | self.status = True |
|
48 | 48 | self.isConfig = False |
|
49 | 49 | self.dirnameList = [] |
|
50 | 50 | self.filenameList = [] |
|
51 | 51 | self.fileIndex = None |
|
52 | 52 | self.flagNoMoreFiles = False |
|
53 | 53 | self.flagIsNewFile = 0 |
|
54 | 54 | self.filename = '' |
|
55 | 55 | self.amisrFilePointer = None |
|
56 | 56 | self.realBeamCode = [] |
|
57 | 57 | self.beamCodeMap = None |
|
58 | 58 | self.azimuthList = [] |
|
59 | 59 | self.elevationList = [] |
|
60 | 60 | self.dataShape = None |
|
61 | 61 | self.flag_old_beams = False |
|
62 | 62 | |
|
63 | 63 | |
|
64 | 64 | self.profileIndex = 0 |
|
65 | 65 | |
|
66 | 66 | |
|
67 | 67 | self.beamCodeByFrame = None |
|
68 | 68 | self.radacTimeByFrame = None |
|
69 | 69 | |
|
70 | 70 | self.dataset = None |
|
71 | 71 | |
|
72 | 72 | self.__firstFile = True |
|
73 | 73 | |
|
74 | 74 | self.buffer = None |
|
75 | 75 | |
|
76 | 76 | self.timezone = 'ut' |
|
77 | 77 | |
|
78 | 78 | self.__waitForNewFile = 20 |
|
79 | 79 | self.__filename_online = None |
|
80 | 80 | #Is really necessary create the output object in the initializer |
|
81 | 81 | self.dataOut = Voltage() |
|
82 | 82 | self.dataOut.error=False |
|
83 | 83 | |
|
84 | 84 | |
|
85 | 85 | def setup(self,path=None, |
|
86 | 86 | startDate=None, |
|
87 | 87 | endDate=None, |
|
88 | 88 | startTime=None, |
|
89 | 89 | endTime=None, |
|
90 | 90 | walk=True, |
|
91 | 91 | timezone='ut', |
|
92 | 92 | all=0, |
|
93 | 93 | code = None, |
|
94 | 94 | nCode = 0, |
|
95 | 95 | nBaud = 0, |
|
96 | online=False | |
|
96 | online=False, | |
|
97 | 97 | old_beams=False): |
|
98 | 98 | |
|
99 | 99 | |
|
100 | 100 | |
|
101 | 101 | self.timezone = timezone |
|
102 | 102 | self.all = all |
|
103 | 103 | self.online = online |
|
104 | 104 | self.flag_old_beams = old_beams |
|
105 | 105 | self.code = code |
|
106 | 106 | self.nCode = int(nCode) |
|
107 | 107 | self.nBaud = int(nBaud) |
|
108 | 108 | |
|
109 | 109 | |
|
110 | 110 | |
|
111 | 111 | #self.findFiles() |
|
112 | 112 | if not(online): |
|
113 | 113 | #Busqueda de archivos offline |
|
114 | 114 | self.searchFilesOffLine(path, startDate, endDate, startTime, endTime, walk) |
|
115 | 115 | else: |
|
116 | 116 | self.searchFilesOnLine(path, startDate, endDate, startTime,endTime,walk) |
|
117 | 117 | |
|
118 | 118 | if not(self.filenameList): |
|
119 | 119 | raise schainpy.admin.SchainWarning("There is no files into the folder: %s"%(path)) |
|
120 | 120 | sys.exit() |
|
121 | 121 | |
|
122 | 122 | self.fileIndex = 0 |
|
123 | 123 | |
|
124 | 124 | self.readNextFile(online) |
|
125 | 125 | |
|
126 | 126 | ''' |
|
127 | 127 | Add code |
|
128 | 128 | ''' |
|
129 | 129 | self.isConfig = True |
|
130 | 130 | # print("Setup Done") |
|
131 | 131 | pass |
|
132 | 132 | |
|
133 | 133 | |
|
134 | 134 | def readAMISRHeader(self,fp): |
|
135 | 135 | |
|
136 | 136 | if self.isConfig and (not self.flagNoMoreFiles): |
|
137 | 137 | newShape = fp.get('Raw11/Data/Samples/Data').shape[1:] |
|
138 | 138 | if self.dataShape != newShape and newShape != None: |
|
139 | 139 | raise schainpy.admin.SchainError("NEW FILE HAS A DIFFERENT SHAPE: ") |
|
140 | 140 | print(self.dataShape,newShape,"\n") |
|
141 | 141 | return 0 |
|
142 | 142 | else: |
|
143 | 143 | self.dataShape = fp.get('Raw11/Data/Samples/Data').shape[1:] |
|
144 | 144 | |
|
145 | 145 | |
|
146 | 146 | header = 'Raw11/Data/RadacHeader' |
|
147 | 147 | self.beamCodeByPulse = fp.get(header+'/BeamCode') # LIST OF BEAMS PER PROFILE, TO BE USED ON REARRANGE |
|
148 | 148 | if (self.startDate> datetime.date(2021, 7, 15)) or self.flag_old_beams: #Se cambió la forma de extracción de Apuntes el 17 o forzar con flag de reorganización |
|
149 | 149 | self.beamcodeFile = fp['Setup/Beamcodefile'][()].decode() |
|
150 | 150 | self.trueBeams = self.beamcodeFile.split("\n") |
|
151 | 151 | self.trueBeams.pop()#remove last |
|
152 | 152 | [self.realBeamCode.append(x) for x in self.trueBeams if x not in self.realBeamCode] |
|
153 | 153 | self.beamCode = [int(x, 16) for x in self.realBeamCode] |
|
154 | 154 | else: |
|
155 | 155 | _beamCode= fp.get('Raw11/Data/Beamcodes') #se usa la manera previa al cambio de apuntes |
|
156 | 156 | self.beamCode = _beamCode[0,:] |
|
157 | 157 | |
|
158 | 158 | if self.beamCodeMap == None: |
|
159 | 159 | self.beamCodeMap = fp['Setup/BeamcodeMap'] |
|
160 | 160 | for beam in self.beamCode: |
|
161 | 161 | beamAziElev = numpy.where(self.beamCodeMap[:,0]==beam) |
|
162 | 162 | beamAziElev = beamAziElev[0].squeeze() |
|
163 | 163 | self.azimuthList.append(self.beamCodeMap[beamAziElev,1]) |
|
164 | 164 | self.elevationList.append(self.beamCodeMap[beamAziElev,2]) |
|
165 | 165 | #print("Beamssss: ",self.beamCodeMap[beamAziElev,1],self.beamCodeMap[beamAziElev,2]) |
|
166 | 166 | #print(self.beamCode) |
|
167 | 167 | #self.code = fp.get(header+'/Code') # NOT USE FOR THIS |
|
168 | 168 | self.frameCount = fp.get(header+'/FrameCount')# NOT USE FOR THIS |
|
169 | 169 | self.modeGroup = fp.get(header+'/ModeGroup')# NOT USE FOR THIS |
|
170 | 170 | self.nsamplesPulse = fp.get(header+'/NSamplesPulse')# TO GET NSA OR USING DATA FOR THAT |
|
171 | 171 | self.pulseCount = fp.get(header+'/PulseCount')# NOT USE FOR THIS |
|
172 | 172 | self.radacTime = fp.get(header+'/RadacTime')# 1st TIME ON FILE ANDE CALCULATE THE REST WITH IPP*nindexprofile |
|
173 | 173 | self.timeCount = fp.get(header+'/TimeCount')# NOT USE FOR THIS |
|
174 | 174 | self.timeStatus = fp.get(header+'/TimeStatus')# NOT USE FOR THIS |
|
175 | 175 | self.rangeFromFile = fp.get('Raw11/Data/Samples/Range') |
|
176 | 176 | self.frequency = fp.get('Rx/Frequency') |
|
177 | 177 | txAus = fp.get('Raw11/Data/Pulsewidth') |
|
178 | 178 | |
|
179 | 179 | |
|
180 | 180 | self.nblocks = self.pulseCount.shape[0] #nblocks |
|
181 | 181 | |
|
182 | 182 | self.nprofiles = self.pulseCount.shape[1] #nprofile |
|
183 | 183 | self.nsa = self.nsamplesPulse[0,0] #ngates |
|
184 | 184 | self.nchannels = len(self.beamCode) |
|
185 | 185 | self.ippSeconds = (self.radacTime[0][1] -self.radacTime[0][0]) #Ipp in seconds |
|
186 | 186 | #self.__waitForNewFile = self.nblocks # wait depending on the number of blocks since each block is 1 sec |
|
187 | 187 | self.__waitForNewFile = self.nblocks * self.nprofiles * self.ippSeconds # wait until new file is created |
|
188 | 188 | |
|
189 | 189 | #filling radar controller header parameters |
|
190 | 190 | self.__ippKm = self.ippSeconds *.15*1e6 # in km |
|
191 |
self.__txA = (txAus |
|
|
191 | self.__txA = (txAus[()])*.15 #(ipp[us]*.15km/1us) in km | |
|
192 | 192 | self.__txB = 0 |
|
193 | 193 | nWindows=1 |
|
194 | 194 | self.__nSamples = self.nsa |
|
195 | 195 | self.__firstHeight = self.rangeFromFile[0][0]/1000 #in km |
|
196 | 196 | self.__deltaHeight = (self.rangeFromFile[0][1] - self.rangeFromFile[0][0])/1000 |
|
197 | 197 | |
|
198 | 198 | #for now until understand why the code saved is different (code included even though code not in tuf file) |
|
199 | 199 | #self.__codeType = 0 |
|
200 | 200 | # self.__nCode = None |
|
201 | 201 | # self.__nBaud = None |
|
202 | 202 | self.__code = self.code |
|
203 | 203 | self.__codeType = 0 |
|
204 | 204 | if self.code != None: |
|
205 | 205 | self.__codeType = 1 |
|
206 | 206 | self.__nCode = self.nCode |
|
207 | 207 | self.__nBaud = self.nBaud |
|
208 | 208 | #self.__code = 0 |
|
209 | 209 | |
|
210 | 210 | #filling system header parameters |
|
211 | 211 | self.__nSamples = self.nsa |
|
212 | 212 | self.newProfiles = self.nprofiles/self.nchannels |
|
213 | 213 | self.__channelList = list(range(self.nchannels)) |
|
214 | 214 | |
|
215 | 215 | self.__frequency = self.frequency[0][0] |
|
216 | 216 | |
|
217 | 217 | |
|
218 | 218 | return 1 |
|
219 | 219 | |
|
220 | 220 | |
|
221 | 221 | def createBuffers(self): |
|
222 | 222 | |
|
223 | 223 | pass |
|
224 | 224 | |
|
225 | 225 | def __setParameters(self,path='', startDate='',endDate='',startTime='', endTime='', walk=''): |
|
226 | 226 | self.path = path |
|
227 | 227 | self.startDate = startDate |
|
228 | 228 | self.endDate = endDate |
|
229 | 229 | self.startTime = startTime |
|
230 | 230 | self.endTime = endTime |
|
231 | 231 | self.walk = walk |
|
232 | 232 | |
|
233 | 233 | def __checkPath(self): |
|
234 | 234 | if os.path.exists(self.path): |
|
235 | 235 | self.status = 1 |
|
236 | 236 | else: |
|
237 | 237 | self.status = 0 |
|
238 | 238 | print('Path:%s does not exists'%self.path) |
|
239 | 239 | |
|
240 | 240 | return |
|
241 | 241 | |
|
242 | 242 | |
|
243 | 243 | def __selDates(self, amisr_dirname_format): |
|
244 | 244 | try: |
|
245 | 245 | year = int(amisr_dirname_format[0:4]) |
|
246 | 246 | month = int(amisr_dirname_format[4:6]) |
|
247 | 247 | dom = int(amisr_dirname_format[6:8]) |
|
248 | 248 | thisDate = datetime.date(year,month,dom) |
|
249 | 249 | #margen de un día extra, igual luego se filtra for fecha y hora |
|
250 | 250 | if (thisDate>=(self.startDate - datetime.timedelta(days=1)) and thisDate <= (self.endDate)+ datetime.timedelta(days=1)): |
|
251 | 251 | return amisr_dirname_format |
|
252 | 252 | except: |
|
253 | 253 | return None |
|
254 | 254 | |
|
255 | 255 | |
|
256 | 256 | def __findDataForDates(self,online=False): |
|
257 | 257 | |
|
258 | 258 | if not(self.status): |
|
259 | 259 | return None |
|
260 | 260 | |
|
261 | 261 | pat = '\d+.\d+' |
|
262 | 262 | dirnameList = [re.search(pat,x) for x in os.listdir(self.path)] |
|
263 | 263 | dirnameList = [x for x in dirnameList if x!=None] |
|
264 | 264 | dirnameList = [x.string for x in dirnameList] |
|
265 | 265 | if not(online): |
|
266 | 266 | dirnameList = [self.__selDates(x) for x in dirnameList] |
|
267 | 267 | dirnameList = [x for x in dirnameList if x!=None] |
|
268 | 268 | if len(dirnameList)>0: |
|
269 | 269 | self.status = 1 |
|
270 | 270 | self.dirnameList = dirnameList |
|
271 | 271 | self.dirnameList.sort() |
|
272 | 272 | else: |
|
273 | 273 | self.status = 0 |
|
274 | 274 | return None |
|
275 | 275 | |
|
276 | 276 | def __getTimeFromData(self): |
|
277 | 277 | startDateTime_Reader = datetime.datetime.combine(self.startDate,self.startTime) |
|
278 | 278 | endDateTime_Reader = datetime.datetime.combine(self.endDate,self.endTime) |
|
279 | 279 | |
|
280 | 280 | print('Filtering Files from %s to %s'%(startDateTime_Reader, endDateTime_Reader)) |
|
281 | 281 | print('........................................') |
|
282 | 282 | filter_filenameList = [] |
|
283 | 283 | self.filenameList.sort() |
|
284 | 284 | #for i in range(len(self.filenameList)-1): |
|
285 | 285 | for i in range(len(self.filenameList)): |
|
286 | 286 | filename = self.filenameList[i] |
|
287 | 287 | fp = h5py.File(filename,'r') |
|
288 | 288 | time_str = fp.get('Time/RadacTimeString') |
|
289 | 289 | |
|
290 | 290 | startDateTimeStr_File = time_str[0][0].decode('UTF-8').split('.')[0] |
|
291 | 291 | #startDateTimeStr_File = "2019-12-16 09:21:11" |
|
292 | 292 | junk = time.strptime(startDateTimeStr_File, '%Y-%m-%d %H:%M:%S') |
|
293 | 293 | startDateTime_File = datetime.datetime(junk.tm_year,junk.tm_mon,junk.tm_mday,junk.tm_hour, junk.tm_min, junk.tm_sec) |
|
294 | 294 | |
|
295 | 295 | #endDateTimeStr_File = "2019-12-16 11:10:11" |
|
296 | 296 | endDateTimeStr_File = time_str[-1][-1].decode('UTF-8').split('.')[0] |
|
297 | 297 | junk = time.strptime(endDateTimeStr_File, '%Y-%m-%d %H:%M:%S') |
|
298 | 298 | endDateTime_File = datetime.datetime(junk.tm_year,junk.tm_mon,junk.tm_mday,junk.tm_hour, junk.tm_min, junk.tm_sec) |
|
299 | 299 | |
|
300 | 300 | fp.close() |
|
301 | 301 | |
|
302 | 302 | #print("check time", startDateTime_File) |
|
303 | 303 | if self.timezone == 'lt': |
|
304 | 304 | startDateTime_File = startDateTime_File - datetime.timedelta(minutes = 300) |
|
305 | 305 | endDateTime_File = endDateTime_File - datetime.timedelta(minutes = 300) |
|
306 | 306 | if (startDateTime_File >=startDateTime_Reader and endDateTime_File<=endDateTime_Reader): |
|
307 | 307 | filter_filenameList.append(filename) |
|
308 | 308 | |
|
309 | 309 | if (startDateTime_File>endDateTime_Reader): |
|
310 | 310 | break |
|
311 | 311 | |
|
312 | 312 | |
|
313 | 313 | filter_filenameList.sort() |
|
314 | 314 | self.filenameList = filter_filenameList |
|
315 | 315 | |
|
316 | 316 | return 1 |
|
317 | 317 | |
|
318 | 318 | def __filterByGlob1(self, dirName): |
|
319 | 319 | filter_files = glob.glob1(dirName, '*.*%s'%self.extension_file) |
|
320 | 320 | filter_files.sort() |
|
321 | 321 | filterDict = {} |
|
322 | 322 | filterDict.setdefault(dirName) |
|
323 | 323 | filterDict[dirName] = filter_files |
|
324 | 324 | return filterDict |
|
325 | 325 | |
|
326 | 326 | def __getFilenameList(self, fileListInKeys, dirList): |
|
327 | 327 | for value in fileListInKeys: |
|
328 | 328 | dirName = list(value.keys())[0] |
|
329 | 329 | for file in value[dirName]: |
|
330 | 330 | filename = os.path.join(dirName, file) |
|
331 | 331 | self.filenameList.append(filename) |
|
332 | 332 | |
|
333 | 333 | |
|
334 | 334 | def __selectDataForTimes(self, online=False): |
|
335 | 335 | #aun no esta implementado el filtro for tiempo |
|
336 | 336 | if not(self.status): |
|
337 | 337 | return None |
|
338 | 338 | |
|
339 | 339 | dirList = [os.path.join(self.path,x) for x in self.dirnameList] |
|
340 | 340 | fileListInKeys = [self.__filterByGlob1(x) for x in dirList] |
|
341 | 341 | self.__getFilenameList(fileListInKeys, dirList) |
|
342 | 342 | if not(online): |
|
343 | 343 | #filtro por tiempo |
|
344 | 344 | if not(self.all): |
|
345 | 345 | self.__getTimeFromData() |
|
346 | 346 | |
|
347 | 347 | if len(self.filenameList)>0: |
|
348 | 348 | self.status = 1 |
|
349 | 349 | self.filenameList.sort() |
|
350 | 350 | else: |
|
351 | 351 | self.status = 0 |
|
352 | 352 | return None |
|
353 | 353 | |
|
354 | 354 | else: |
|
355 | 355 | #get the last file - 1 |
|
356 | 356 | self.filenameList = [self.filenameList[-2]] |
|
357 | 357 | new_dirnameList = [] |
|
358 | 358 | for dirname in self.dirnameList: |
|
359 | 359 | junk = numpy.array([dirname in x for x in self.filenameList]) |
|
360 | 360 | junk_sum = junk.sum() |
|
361 | 361 | if junk_sum > 0: |
|
362 | 362 | new_dirnameList.append(dirname) |
|
363 | 363 | self.dirnameList = new_dirnameList |
|
364 | 364 | return 1 |
|
365 | 365 | |
|
366 | 366 | def searchFilesOnLine(self, path, startDate, endDate, startTime=datetime.time(0,0,0), |
|
367 | 367 | endTime=datetime.time(23,59,59),walk=True): |
|
368 | 368 | |
|
369 | 369 | if endDate ==None: |
|
370 | 370 | startDate = datetime.datetime.utcnow().date() |
|
371 | 371 | endDate = datetime.datetime.utcnow().date() |
|
372 | 372 | |
|
373 | 373 | self.__setParameters(path=path, startDate=startDate, endDate=endDate,startTime = startTime,endTime=endTime, walk=walk) |
|
374 | 374 | |
|
375 | 375 | self.__checkPath() |
|
376 | 376 | |
|
377 | 377 | self.__findDataForDates(online=True) |
|
378 | 378 | |
|
379 | 379 | self.dirnameList = [self.dirnameList[-1]] |
|
380 | 380 | |
|
381 | 381 | self.__selectDataForTimes(online=True) |
|
382 | 382 | |
|
383 | 383 | return |
|
384 | 384 | |
|
385 | 385 | |
|
386 | 386 | def searchFilesOffLine(self, |
|
387 | 387 | path, |
|
388 | 388 | startDate, |
|
389 | 389 | endDate, |
|
390 | 390 | startTime=datetime.time(0,0,0), |
|
391 | 391 | endTime=datetime.time(23,59,59), |
|
392 | 392 | walk=True): |
|
393 | 393 | |
|
394 | 394 | self.__setParameters(path, startDate, endDate, startTime, endTime, walk) |
|
395 | 395 | |
|
396 | 396 | self.__checkPath() |
|
397 | 397 | |
|
398 | 398 | self.__findDataForDates() |
|
399 | 399 | |
|
400 | 400 | self.__selectDataForTimes() |
|
401 | 401 | |
|
402 | 402 | for i in range(len(self.filenameList)): |
|
403 | 403 | print("%s" %(self.filenameList[i])) |
|
404 | 404 | |
|
405 | 405 | return |
|
406 | 406 | |
|
407 | 407 | def __setNextFileOffline(self): |
|
408 | 408 | |
|
409 | 409 | try: |
|
410 | 410 | self.filename = self.filenameList[self.fileIndex] |
|
411 | 411 | self.amisrFilePointer = h5py.File(self.filename,'r') |
|
412 | 412 | self.fileIndex += 1 |
|
413 | 413 | except: |
|
414 | 414 | self.flagNoMoreFiles = 1 |
|
415 | 415 | raise schainpy.admin.SchainError('No more files to read') |
|
416 | 416 | return 0 |
|
417 | 417 | |
|
418 | 418 | self.flagIsNewFile = 1 |
|
419 | 419 | print("Setting the file: %s"%self.filename) |
|
420 | 420 | |
|
421 | 421 | return 1 |
|
422 | 422 | |
|
423 | 423 | |
|
424 | 424 | def __setNextFileOnline(self): |
|
425 | 425 | filename = self.filenameList[0] |
|
426 | 426 | if self.__filename_online != None: |
|
427 | 427 | self.__selectDataForTimes(online=True) |
|
428 | 428 | filename = self.filenameList[0] |
|
429 | 429 | wait = 0 |
|
430 | 430 | self.__waitForNewFile=300 ## DEBUG: |
|
431 | 431 | while self.__filename_online == filename: |
|
432 | 432 | print('waiting %d seconds to get a new file...'%(self.__waitForNewFile)) |
|
433 | 433 | if wait == 5: |
|
434 | 434 | self.flagNoMoreFiles = 1 |
|
435 | 435 | return 0 |
|
436 | 436 | sleep(self.__waitForNewFile) |
|
437 | 437 | self.__selectDataForTimes(online=True) |
|
438 | 438 | filename = self.filenameList[0] |
|
439 | 439 | wait += 1 |
|
440 | 440 | |
|
441 | 441 | self.__filename_online = filename |
|
442 | 442 | |
|
443 | 443 | self.amisrFilePointer = h5py.File(filename,'r') |
|
444 | 444 | self.flagIsNewFile = 1 |
|
445 | 445 | self.filename = filename |
|
446 | 446 | print("Setting the file: %s"%self.filename) |
|
447 | 447 | return 1 |
|
448 | 448 | |
|
449 | 449 | |
|
450 | 450 | def readData(self): |
|
451 | 451 | buffer = self.amisrFilePointer.get('Raw11/Data/Samples/Data') |
|
452 | 452 | re = buffer[:,:,:,0] |
|
453 | 453 | im = buffer[:,:,:,1] |
|
454 | 454 | dataset = re + im*1j |
|
455 | 455 | |
|
456 | 456 | self.radacTime = self.amisrFilePointer.get('Raw11/Data/RadacHeader/RadacTime') |
|
457 | 457 | timeset = self.radacTime[:,0] |
|
458 | 458 | |
|
459 | 459 | return dataset,timeset |
|
460 | 460 | |
|
461 | 461 | def reshapeData(self): |
|
462 | 462 | #self.beamCodeByPulse, self.beamCode, self.nblocks, self.nprofiles, self.nsa, |
|
463 | 463 | channels = self.beamCodeByPulse[0,:] |
|
464 | 464 | nchan = self.nchannels |
|
465 | 465 | #self.newProfiles = self.nprofiles/nchan #must be defined on filljroheader |
|
466 | 466 | nblocks = self.nblocks |
|
467 | 467 | nsamples = self.nsa |
|
468 | 468 | |
|
469 | 469 | #Dimensions : nChannels, nProfiles, nSamples |
|
470 | 470 | new_block = numpy.empty((nblocks, nchan, numpy.int_(self.newProfiles), nsamples), dtype="complex64") |
|
471 | 471 | ############################################ |
|
472 | 472 | |
|
473 | 473 | for thisChannel in range(nchan): |
|
474 | 474 | new_block[:,thisChannel,:,:] = self.dataset[:,numpy.where(channels==self.beamCode[thisChannel])[0],:] |
|
475 | 475 | |
|
476 | 476 | |
|
477 | 477 | new_block = numpy.transpose(new_block, (1,0,2,3)) |
|
478 | 478 | new_block = numpy.reshape(new_block, (nchan,-1, nsamples)) |
|
479 | 479 | |
|
480 | 480 | return new_block |
|
481 | 481 | |
|
482 | 482 | def updateIndexes(self): |
|
483 | 483 | |
|
484 | 484 | pass |
|
485 | 485 | |
|
486 | 486 | def fillJROHeader(self): |
|
487 | 487 | |
|
488 | 488 | #fill radar controller header |
|
489 | 489 | self.dataOut.radarControllerHeaderObj = RadarControllerHeader(ipp=self.__ippKm, |
|
490 | 490 | txA=self.__txA, |
|
491 | 491 | txB=0, |
|
492 | 492 | nWindows=1, |
|
493 | 493 | nHeights=self.__nSamples, |
|
494 | 494 | firstHeight=self.__firstHeight, |
|
495 | 495 | deltaHeight=self.__deltaHeight, |
|
496 | 496 | codeType=self.__codeType, |
|
497 | 497 | nCode=self.__nCode, nBaud=self.__nBaud, |
|
498 | 498 | code = self.__code, |
|
499 | 499 | fClock=1) |
|
500 | 500 | |
|
501 | 501 | #fill system header |
|
502 | 502 | self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples, |
|
503 | 503 | nProfiles=self.newProfiles, |
|
504 | 504 | nChannels=len(self.__channelList), |
|
505 | 505 | adcResolution=14, |
|
506 | 506 | pciDioBusWidth=32) |
|
507 | 507 | |
|
508 | 508 | self.dataOut.type = "Voltage" |
|
509 | 509 | self.dataOut.data = None |
|
510 | 510 | self.dataOut.dtype = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
511 | 511 | # self.dataOut.nChannels = 0 |
|
512 | 512 | |
|
513 | 513 | # self.dataOut.nHeights = 0 |
|
514 | 514 | |
|
515 | 515 | self.dataOut.nProfiles = self.newProfiles*self.nblocks |
|
516 | 516 | #self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype = numpy.float)*self.__deltaHeigth |
|
517 |
ranges = numpy.reshape(self.rangeFromFile |
|
|
517 | ranges = numpy.reshape(self.rangeFromFile[()],(-1)) | |
|
518 | 518 | self.dataOut.heightList = ranges/1000.0 #km |
|
519 | 519 | self.dataOut.channelList = self.__channelList |
|
520 | 520 | self.dataOut.blocksize = self.dataOut.nChannels * self.dataOut.nHeights |
|
521 | 521 | |
|
522 | 522 | # self.dataOut.channelIndexList = None |
|
523 | 523 | |
|
524 | 524 | |
|
525 | 525 | self.dataOut.azimuthList = numpy.array(self.azimuthList) |
|
526 | 526 | self.dataOut.elevationList = numpy.array(self.elevationList) |
|
527 | 527 | self.dataOut.codeList = numpy.array(self.beamCode) |
|
528 | 528 | #print(self.dataOut.elevationList) |
|
529 | 529 | self.dataOut.flagNoData = True |
|
530 | 530 | |
|
531 | 531 | #Set to TRUE if the data is discontinuous |
|
532 | 532 | self.dataOut.flagDiscontinuousBlock = False |
|
533 | 533 | |
|
534 | 534 | self.dataOut.utctime = None |
|
535 | 535 | |
|
536 | 536 | #self.dataOut.timeZone = -5 #self.__timezone/60 #timezone like jroheader, difference in minutes between UTC and localtime |
|
537 | 537 | if self.timezone == 'lt': |
|
538 | 538 | self.dataOut.timeZone = time.timezone / 60. #get the timezone in minutes |
|
539 | 539 | else: |
|
540 | 540 | self.dataOut.timeZone = 0 #by default time is UTC |
|
541 | 541 | |
|
542 | 542 | self.dataOut.dstFlag = 0 |
|
543 | 543 | self.dataOut.errorCount = 0 |
|
544 | 544 | self.dataOut.nCohInt = 1 |
|
545 | 545 | self.dataOut.flagDecodeData = False #asumo que la data esta decodificada |
|
546 | 546 | self.dataOut.flagDeflipData = False #asumo que la data esta sin flip |
|
547 | 547 | self.dataOut.flagShiftFFT = False |
|
548 | 548 | self.dataOut.ippSeconds = self.ippSeconds |
|
549 | 549 | |
|
550 | 550 | #Time interval between profiles |
|
551 | 551 | #self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt |
|
552 | 552 | |
|
553 | 553 | self.dataOut.frequency = self.__frequency |
|
554 | 554 | self.dataOut.realtime = self.online |
|
555 | 555 | pass |
|
556 | 556 | |
|
557 | 557 | def readNextFile(self,online=False): |
|
558 | 558 | |
|
559 | 559 | if not(online): |
|
560 | 560 | newFile = self.__setNextFileOffline() |
|
561 | 561 | else: |
|
562 | 562 | newFile = self.__setNextFileOnline() |
|
563 | 563 | |
|
564 | 564 | if not(newFile): |
|
565 | 565 | self.dataOut.error = True |
|
566 | 566 | return 0 |
|
567 | 567 | |
|
568 | 568 | if not self.readAMISRHeader(self.amisrFilePointer): |
|
569 | 569 | self.dataOut.error = True |
|
570 | 570 | return 0 |
|
571 | 571 | |
|
572 | 572 | self.createBuffers() |
|
573 | 573 | self.fillJROHeader() |
|
574 | 574 | |
|
575 | 575 | #self.__firstFile = False |
|
576 | 576 | |
|
577 | 577 | |
|
578 | 578 | |
|
579 | 579 | self.dataset,self.timeset = self.readData() |
|
580 | 580 | |
|
581 | 581 | if self.endDate!=None: |
|
582 | 582 | endDateTime_Reader = datetime.datetime.combine(self.endDate,self.endTime) |
|
583 | 583 | time_str = self.amisrFilePointer.get('Time/RadacTimeString') |
|
584 | 584 | startDateTimeStr_File = time_str[0][0].decode('UTF-8').split('.')[0] |
|
585 | 585 | junk = time.strptime(startDateTimeStr_File, '%Y-%m-%d %H:%M:%S') |
|
586 | 586 | startDateTime_File = datetime.datetime(junk.tm_year,junk.tm_mon,junk.tm_mday,junk.tm_hour, junk.tm_min, junk.tm_sec) |
|
587 | 587 | if self.timezone == 'lt': |
|
588 | 588 | startDateTime_File = startDateTime_File - datetime.timedelta(minutes = 300) |
|
589 | 589 | if (startDateTime_File>endDateTime_Reader): |
|
590 | 590 | return 0 |
|
591 | 591 | |
|
592 | 592 | self.jrodataset = self.reshapeData() |
|
593 | 593 | #----self.updateIndexes() |
|
594 | 594 | self.profileIndex = 0 |
|
595 | 595 | |
|
596 | 596 | return 1 |
|
597 | 597 | |
|
598 | 598 | |
|
599 | 599 | def __hasNotDataInBuffer(self): |
|
600 | 600 | if self.profileIndex >= (self.newProfiles*self.nblocks): |
|
601 | 601 | return 1 |
|
602 | 602 | return 0 |
|
603 | 603 | |
|
604 | 604 | |
|
605 | 605 | def getData(self): |
|
606 | 606 | |
|
607 | 607 | if self.flagNoMoreFiles: |
|
608 | 608 | self.dataOut.flagNoData = True |
|
609 | 609 | return 0 |
|
610 | 610 | |
|
611 | 611 | if self.__hasNotDataInBuffer(): |
|
612 | 612 | if not (self.readNextFile(self.online)): |
|
613 | 613 | return 0 |
|
614 | 614 | |
|
615 | 615 | |
|
616 | 616 | if self.dataset is None: # setear esta condicion cuando no hayan datos por leer |
|
617 | 617 | self.dataOut.flagNoData = True |
|
618 | 618 | return 0 |
|
619 | 619 | |
|
620 | 620 | #self.dataOut.data = numpy.reshape(self.jrodataset[self.profileIndex,:],(1,-1)) |
|
621 | 621 | |
|
622 | 622 | self.dataOut.data = self.jrodataset[:,self.profileIndex,:] |
|
623 | 623 | |
|
624 | 624 | #print("R_t",self.timeset) |
|
625 | 625 | |
|
626 | 626 | #self.dataOut.utctime = self.jrotimeset[self.profileIndex] |
|
627 | 627 | #verificar basic header de jro data y ver si es compatible con este valor |
|
628 | 628 | #self.dataOut.utctime = self.timeset + (self.profileIndex * self.ippSeconds * self.nchannels) |
|
629 | 629 | indexprof = numpy.mod(self.profileIndex, self.newProfiles) |
|
630 | 630 | indexblock = self.profileIndex/self.newProfiles |
|
631 | 631 | #print (indexblock, indexprof) |
|
632 | 632 | diffUTC = 0 |
|
633 | 633 | t_comp = (indexprof * self.ippSeconds * self.nchannels) + diffUTC # |
|
634 | 634 | |
|
635 | 635 | #print("utc :",indexblock," __ ",t_comp) |
|
636 | 636 | #print(numpy.shape(self.timeset)) |
|
637 | 637 | self.dataOut.utctime = self.timeset[numpy.int_(indexblock)] + t_comp |
|
638 | 638 | #self.dataOut.utctime = self.timeset[self.profileIndex] + t_comp |
|
639 | 639 | |
|
640 | 640 | self.dataOut.profileIndex = self.profileIndex |
|
641 | 641 | #print("N profile:",self.profileIndex,self.newProfiles,self.nblocks,self.dataOut.utctime) |
|
642 | 642 | self.dataOut.flagNoData = False |
|
643 | 643 | # if indexprof == 0: |
|
644 | 644 | # print("kamisr: ",self.dataOut.utctime) |
|
645 | 645 | |
|
646 | 646 | self.profileIndex += 1 |
|
647 | 647 | |
|
648 | 648 | return self.dataOut.data #retorno necesario?? |
|
649 | 649 | |
|
650 | 650 | |
|
651 | 651 | def run(self, **kwargs): |
|
652 | 652 | ''' |
|
653 | 653 | This method will be called many times so here you should put all your code |
|
654 | 654 | ''' |
|
655 | 655 | #print("running kamisr") |
|
656 | 656 | if not self.isConfig: |
|
657 | 657 | self.setup(**kwargs) |
|
658 | 658 | self.isConfig = True |
|
659 | 659 | |
|
660 | 660 | self.getData() |
General Comments 0
You need to be logged in to leave comments.
Login now