@@ -1,408 +1,407 | |||||
1 | ''' |
|
1 | ''' | |
2 | Created on Nov 9, 2016 |
|
2 | Created on Nov 9, 2016 | |
3 |
|
3 | |||
4 | @author: roj- LouVD |
|
4 | @author: roj- LouVD | |
5 | ''' |
|
5 | ''' | |
6 |
|
6 | |||
7 |
|
7 | |||
8 | import os |
|
8 | import os | |
9 | import sys |
|
9 | import sys | |
10 | import time |
|
10 | import time | |
11 | import glob |
|
11 | import glob | |
12 | import datetime |
|
12 | import datetime | |
13 |
|
13 | |||
14 | import numpy |
|
14 | import numpy | |
15 |
|
15 | |||
16 | from schainpy.model.proc.jroproc_base import ProcessingUnit, MPDecorator |
|
16 | from schainpy.model.proc.jroproc_base import ProcessingUnit, MPDecorator | |
17 | from schainpy.model.data.jrodata import Parameters |
|
17 | from schainpy.model.data.jrodata import Parameters | |
18 | from schainpy.model.io.jroIO_base import JRODataReader, isNumber |
|
18 | from schainpy.model.io.jroIO_base import JRODataReader, isNumber | |
19 | from schainpy.utils import log |
|
19 | from schainpy.utils import log | |
20 |
|
20 | |||
21 | FILE_HEADER_STRUCTURE = numpy.dtype([ |
|
21 | FILE_HEADER_STRUCTURE = numpy.dtype([ | |
22 | ('FMN', '<u4'), |
|
22 | ('FMN', '<u4'), | |
23 | ('nrec', '<u4'), |
|
23 | ('nrec', '<u4'), | |
24 | ('fr_offset', '<u4'), |
|
24 | ('fr_offset', '<u4'), | |
25 | ('id', '<u4'), |
|
25 | ('id', '<u4'), | |
26 | ('site', 'u1', (32,)) |
|
26 | ('site', 'u1', (32,)) | |
27 | ]) |
|
27 | ]) | |
28 |
|
28 | |||
29 | REC_HEADER_STRUCTURE = numpy.dtype([ |
|
29 | REC_HEADER_STRUCTURE = numpy.dtype([ | |
30 | ('rmn', '<u4'), |
|
30 | ('rmn', '<u4'), | |
31 | ('rcounter', '<u4'), |
|
31 | ('rcounter', '<u4'), | |
32 | ('nr_offset', '<u4'), |
|
32 | ('nr_offset', '<u4'), | |
33 | ('tr_offset', '<u4'), |
|
33 | ('tr_offset', '<u4'), | |
34 | ('time', '<u4'), |
|
34 | ('time', '<u4'), | |
35 | ('time_msec', '<u4'), |
|
35 | ('time_msec', '<u4'), | |
36 | ('tag', 'u1', (32,)), |
|
36 | ('tag', 'u1', (32,)), | |
37 | ('comments', 'u1', (32,)), |
|
37 | ('comments', 'u1', (32,)), | |
38 | ('lat', '<f4'), |
|
38 | ('lat', '<f4'), | |
39 | ('lon', '<f4'), |
|
39 | ('lon', '<f4'), | |
40 | ('gps_status', '<u4'), |
|
40 | ('gps_status', '<u4'), | |
41 | ('freq', '<u4'), |
|
41 | ('freq', '<u4'), | |
42 | ('freq0', '<u4'), |
|
42 | ('freq0', '<u4'), | |
43 | ('nchan', '<u4'), |
|
43 | ('nchan', '<u4'), | |
44 | ('delta_r', '<u4'), |
|
44 | ('delta_r', '<u4'), | |
45 | ('nranges', '<u4'), |
|
45 | ('nranges', '<u4'), | |
46 | ('r0', '<u4'), |
|
46 | ('r0', '<u4'), | |
47 | ('prf', '<u4'), |
|
47 | ('prf', '<u4'), | |
48 | ('ncoh', '<u4'), |
|
48 | ('ncoh', '<u4'), | |
49 | ('npoints', '<u4'), |
|
49 | ('npoints', '<u4'), | |
50 | ('polarization', '<i4'), |
|
50 | ('polarization', '<i4'), | |
51 | ('rx_filter', '<u4'), |
|
51 | ('rx_filter', '<u4'), | |
52 | ('nmodes', '<u4'), |
|
52 | ('nmodes', '<u4'), | |
53 | ('dmode_index', '<u4'), |
|
53 | ('dmode_index', '<u4'), | |
54 | ('dmode_rngcorr', '<u4'), |
|
54 | ('dmode_rngcorr', '<u4'), | |
55 | ('nrxs', '<u4'), |
|
55 | ('nrxs', '<u4'), | |
56 | ('acf_length', '<u4'), |
|
56 | ('acf_length', '<u4'), | |
57 | ('acf_lags', '<u4'), |
|
57 | ('acf_lags', '<u4'), | |
58 | ('sea_to_atmos', '<f4'), |
|
58 | ('sea_to_atmos', '<f4'), | |
59 | ('sea_notch', '<u4'), |
|
59 | ('sea_notch', '<u4'), | |
60 | ('lh_sea', '<u4'), |
|
60 | ('lh_sea', '<u4'), | |
61 | ('hh_sea', '<u4'), |
|
61 | ('hh_sea', '<u4'), | |
62 | ('nbins_sea', '<u4'), |
|
62 | ('nbins_sea', '<u4'), | |
63 | ('min_snr', '<f4'), |
|
63 | ('min_snr', '<f4'), | |
64 | ('min_cc', '<f4'), |
|
64 | ('min_cc', '<f4'), | |
65 | ('max_time_diff', '<f4') |
|
65 | ('max_time_diff', '<f4') | |
66 | ]) |
|
66 | ]) | |
67 |
|
67 | |||
68 | DATA_STRUCTURE = numpy.dtype([ |
|
68 | DATA_STRUCTURE = numpy.dtype([ | |
69 | ('range', '<u4'), |
|
69 | ('range', '<u4'), | |
70 | ('status', '<u4'), |
|
70 | ('status', '<u4'), | |
71 | ('zonal', '<f4'), |
|
71 | ('zonal', '<f4'), | |
72 | ('meridional', '<f4'), |
|
72 | ('meridional', '<f4'), | |
73 | ('vertical', '<f4'), |
|
73 | ('vertical', '<f4'), | |
74 | ('zonal_a', '<f4'), |
|
74 | ('zonal_a', '<f4'), | |
75 | ('meridional_a', '<f4'), |
|
75 | ('meridional_a', '<f4'), | |
76 | ('corrected_fading', '<f4'), # seconds |
|
76 | ('corrected_fading', '<f4'), # seconds | |
77 | ('uncorrected_fading', '<f4'), # seconds |
|
77 | ('uncorrected_fading', '<f4'), # seconds | |
78 | ('time_diff', '<f4'), |
|
78 | ('time_diff', '<f4'), | |
79 | ('major_axis', '<f4'), |
|
79 | ('major_axis', '<f4'), | |
80 | ('axial_ratio', '<f4'), |
|
80 | ('axial_ratio', '<f4'), | |
81 | ('orientation', '<f4'), |
|
81 | ('orientation', '<f4'), | |
82 | ('sea_power', '<u4'), |
|
82 | ('sea_power', '<u4'), | |
83 | ('sea_algorithm', '<u4') |
|
83 | ('sea_algorithm', '<u4') | |
84 | ]) |
|
84 | ]) | |
85 |
|
85 | |||
86 | @MPDecorator |
|
86 | @MPDecorator | |
87 | class BLTRParamReader(JRODataReader, ProcessingUnit): |
|
87 | class BLTRParamReader(JRODataReader, ProcessingUnit): | |
88 | ''' |
|
88 | ''' | |
89 | Boundary Layer and Tropospheric Radar (BLTR) reader, Wind velocities and SNR |
|
89 | Boundary Layer and Tropospheric Radar (BLTR) reader, Wind velocities and SNR | |
90 | from *.sswma files |
|
90 | from *.sswma files | |
91 | ''' |
|
91 | ''' | |
92 |
|
92 | |||
93 | ext = '.sswma' |
|
93 | ext = '.sswma' | |
94 |
|
94 | |||
95 | def __init__(self): |
|
95 | def __init__(self): | |
96 |
|
96 | |||
97 | ProcessingUnit.__init__(self) |
|
97 | ProcessingUnit.__init__(self) | |
98 |
|
98 | |||
99 | self.dataOut = Parameters() |
|
99 | self.dataOut = Parameters() | |
100 | self.counter_records = 0 |
|
100 | self.counter_records = 0 | |
101 | self.flagNoMoreFiles = 0 |
|
101 | self.flagNoMoreFiles = 0 | |
102 | self.isConfig = False |
|
102 | self.isConfig = False | |
103 | self.filename = None |
|
103 | self.filename = None | |
104 |
|
104 | |||
105 | def setup(self, |
|
105 | def setup(self, | |
106 | path=None, |
|
106 | path=None, | |
107 | startDate=None, |
|
107 | startDate=None, | |
108 | endDate=None, |
|
108 | endDate=None, | |
109 | ext=None, |
|
109 | ext=None, | |
110 | startTime=datetime.time(0, 0, 0), |
|
110 | startTime=datetime.time(0, 0, 0), | |
111 | endTime=datetime.time(23, 59, 59), |
|
111 | endTime=datetime.time(23, 59, 59), | |
112 | timezone=0, |
|
112 | timezone=0, | |
113 | status_value=0, |
|
113 | status_value=0, | |
114 | **kwargs): |
|
114 | **kwargs): | |
115 | self.path = path |
|
115 | self.path = path | |
116 | self.startDate = startDate |
|
116 | self.startDate = startDate | |
117 | self.endDate = endDate |
|
117 | self.endDate = endDate | |
118 | self.startTime = startTime |
|
118 | self.startTime = startTime | |
119 | self.endTime = endTime |
|
119 | self.endTime = endTime | |
120 | self.status_value = status_value |
|
120 | self.status_value = status_value | |
121 | self.datatime = datetime.datetime(1900,1,1) |
|
121 | self.datatime = datetime.datetime(1900,1,1) | |
122 | self.delay = kwargs.get('delay', 10) |
|
122 | self.delay = kwargs.get('delay', 10) | |
123 | self.online = kwargs.get('online', False) |
|
123 | self.online = kwargs.get('online', False) | |
124 | self.nTries = kwargs.get('nTries', 3) |
|
124 | self.nTries = kwargs.get('nTries', 3) | |
125 |
|
125 | |||
126 | if self.path is None: |
|
126 | if self.path is None: | |
127 | raise ValueError("The path is not valid") |
|
127 | raise ValueError("The path is not valid") | |
128 |
|
128 | |||
129 | if ext is None: |
|
129 | if ext is None: | |
130 | ext = self.ext |
|
130 | ext = self.ext | |
131 |
|
131 | |||
132 | self.fileList = self.search_files(self.path, startDate, endDate, ext) |
|
132 | self.fileList = self.search_files(self.path, startDate, endDate, ext) | |
133 | self.timezone = timezone |
|
133 | self.timezone = timezone | |
134 | self.fileIndex = 0 |
|
134 | self.fileIndex = 0 | |
135 |
|
135 | |||
136 | if not self.fileList: |
|
136 | if not self.fileList: | |
137 | raise Warning("There is no files matching these date in the folder: %s. \n Check 'startDate' and 'endDate' " % ( |
|
137 | raise Warning("There is no files matching these date in the folder: %s. \n Check 'startDate' and 'endDate' " % ( | |
138 | path)) |
|
138 | path)) | |
139 |
|
139 | |||
140 | self.setNextFile() |
|
140 | self.setNextFile() | |
141 |
|
141 | |||
142 | def search_last_file(self): |
|
142 | def search_last_file(self): | |
143 | ''' |
|
143 | ''' | |
144 | Get last file and add it to the list |
|
144 | Get last file and add it to the list | |
145 | ''' |
|
145 | ''' | |
146 |
|
146 | |||
147 | for n in range(self.nTries+1): |
|
147 | for n in range(self.nTries+1): | |
148 | if n>0: |
|
148 | if n>0: | |
149 | log.warning( |
|
149 | log.warning( | |
150 | "Waiting %0.2f seconds for the next file, try %03d ..." % (self.delay, n+1), |
|
150 | "Waiting %0.2f seconds for the next file, try %03d ..." % (self.delay, n+1), | |
151 | self.name |
|
151 | self.name | |
152 | ) |
|
152 | ) | |
153 | time.sleep(self.delay) |
|
153 | time.sleep(self.delay) | |
154 | file_list = os.listdir(self.path) |
|
154 | file_list = os.listdir(self.path) | |
155 | file_list.sort() |
|
155 | file_list.sort() | |
156 | if file_list: |
|
156 | if file_list: | |
157 | if self.filename: |
|
157 | if self.filename: | |
158 | if file_list[-1] not in self.filename: |
|
158 | if file_list[-1] not in self.filename: | |
159 | return file_list[-1] |
|
159 | return file_list[-1] | |
160 | else: |
|
160 | else: | |
161 | continue |
|
161 | continue | |
162 | return file_list[-1] |
|
162 | return file_list[-1] | |
163 | return 0 |
|
163 | return 0 | |
164 |
|
164 | |||
165 | def search_files(self, path, startDate, endDate, ext): |
|
165 | def search_files(self, path, startDate, endDate, ext): | |
166 | ''' |
|
166 | ''' | |
167 | Searching for BLTR rawdata file in path |
|
167 | Searching for BLTR rawdata file in path | |
168 | Creating a list of file to proces included in [startDate,endDate] |
|
168 | Creating a list of file to proces included in [startDate,endDate] | |
169 |
|
169 | |||
170 | Input: |
|
170 | Input: | |
171 | path - Path to find BLTR rawdata files |
|
171 | path - Path to find BLTR rawdata files | |
172 | startDate - Select file from this date |
|
172 | startDate - Select file from this date | |
173 | enDate - Select file until this date |
|
173 | enDate - Select file until this date | |
174 | ext - Extension of the file to read |
|
174 | ext - Extension of the file to read | |
175 | ''' |
|
175 | ''' | |
176 |
|
176 | |||
177 | log.success('Searching files in {} '.format(path), 'BLTRParamReader') |
|
177 | log.success('Searching files in {} '.format(path), 'BLTRParamReader') | |
178 | foldercounter = 0 |
|
178 | foldercounter = 0 | |
179 | fileList0 = glob.glob1(path, "*%s" % ext) |
|
179 | fileList0 = glob.glob1(path, "*%s" % ext) | |
180 | fileList0.sort() |
|
180 | fileList0.sort() | |
181 |
|
181 | |||
182 | for thisFile in fileList0: |
|
182 | for thisFile in fileList0: | |
183 | year = thisFile[-14:-10] |
|
183 | year = thisFile[-14:-10] | |
184 | if not isNumber(year): |
|
184 | if not isNumber(year): | |
185 | continue |
|
185 | continue | |
186 |
|
186 | |||
187 | month = thisFile[-10:-8] |
|
187 | month = thisFile[-10:-8] | |
188 | if not isNumber(month): |
|
188 | if not isNumber(month): | |
189 | continue |
|
189 | continue | |
190 |
|
190 | |||
191 | day = thisFile[-8:-6] |
|
191 | day = thisFile[-8:-6] | |
192 | if not isNumber(day): |
|
192 | if not isNumber(day): | |
193 | continue |
|
193 | continue | |
194 |
|
194 | |||
195 | year, month, day = int(year), int(month), int(day) |
|
195 | year, month, day = int(year), int(month), int(day) | |
196 | dateFile = datetime.date(year, month, day) |
|
196 | dateFile = datetime.date(year, month, day) | |
197 |
|
197 | |||
198 | if (startDate > dateFile) or (endDate < dateFile): |
|
198 | if (startDate > dateFile) or (endDate < dateFile): | |
199 | continue |
|
199 | continue | |
200 |
|
200 | |||
201 | yield thisFile |
|
201 | yield thisFile | |
202 |
|
202 | |||
203 | return |
|
203 | return | |
204 |
|
204 | |||
205 | def setNextFile(self): |
|
205 | def setNextFile(self): | |
206 |
|
206 | |||
207 | if self.online: |
|
207 | if self.online: | |
208 | filename = self.search_last_file() |
|
208 | filename = self.search_last_file() | |
209 | if not filename: |
|
209 | if not filename: | |
210 | self.flagNoMoreFiles = 1 |
|
210 | self.flagNoMoreFiles = 1 | |
211 | return 0 |
|
211 | return 0 | |
212 | else: |
|
212 | else: | |
213 | try: |
|
213 | try: | |
214 | filename = next(self.fileList) |
|
214 | filename = next(self.fileList) | |
215 | except StopIteration: |
|
215 | except StopIteration: | |
216 | self.flagNoMoreFiles = 1 |
|
216 | self.flagNoMoreFiles = 1 | |
217 | return 0 |
|
217 | return 0 | |
218 |
|
218 | |||
219 | log.success('Opening {}'.format(filename), 'BLTRParamReader') |
|
219 | log.success('Opening {}'.format(filename), 'BLTRParamReader') | |
220 |
|
220 | |||
221 | dirname, name = os.path.split(filename) |
|
221 | dirname, name = os.path.split(filename) | |
222 | # 'peru2' ---> Piura - 'peru1' ---> Huancayo or Porcuya |
|
222 | # 'peru2' ---> Piura - 'peru1' ---> Huancayo or Porcuya | |
223 | self.siteFile = filename.split('.')[0] |
|
223 | self.siteFile = filename.split('.')[0] | |
224 | if self.filename is not None: |
|
224 | if self.filename is not None: | |
225 | self.fp.close() |
|
225 | self.fp.close() | |
226 | self.filename = os.path.join(self.path, filename) |
|
226 | self.filename = os.path.join(self.path, filename) | |
227 | self.fp = open(self.filename, 'rb') |
|
227 | self.fp = open(self.filename, 'rb') | |
228 | self.header_file = numpy.fromfile(self.fp, FILE_HEADER_STRUCTURE, 1) |
|
228 | self.header_file = numpy.fromfile(self.fp, FILE_HEADER_STRUCTURE, 1) | |
229 | self.nrecords = self.header_file['nrec'][0] |
|
229 | self.nrecords = self.header_file['nrec'][0] | |
230 | self.sizeOfFile = os.path.getsize(self.filename) |
|
230 | self.sizeOfFile = os.path.getsize(self.filename) | |
231 | self.counter_records = 0 |
|
231 | self.counter_records = 0 | |
232 | self.flagIsNewFile = 0 |
|
232 | self.flagIsNewFile = 0 | |
233 | self.fileIndex += 1 |
|
233 | self.fileIndex += 1 | |
234 | time.sleep(2) |
|
|||
235 |
|
234 | |||
236 | return 1 |
|
235 | return 1 | |
237 |
|
236 | |||
238 | def readNextBlock(self): |
|
237 | def readNextBlock(self): | |
239 |
|
238 | |||
240 | while True: |
|
239 | while True: | |
241 | if not self.online and self.counter_records == self.nrecords: |
|
240 | if not self.online and self.counter_records == self.nrecords: | |
242 | self.flagIsNewFile = 1 |
|
241 | self.flagIsNewFile = 1 | |
243 | if not self.setNextFile(): |
|
242 | if not self.setNextFile(): | |
244 | return 0 |
|
243 | return 0 | |
245 |
|
244 | |||
246 | try: |
|
245 | try: | |
247 | pointer = self.fp.tell() |
|
246 | pointer = self.fp.tell() | |
248 | self.readBlock() |
|
247 | self.readBlock() | |
249 | except: |
|
248 | except: | |
250 | if self.online and self.waitDataBlock(pointer, 38512) == 1: |
|
249 | if self.online and self.waitDataBlock(pointer, 38512) == 1: | |
251 | continue |
|
250 | continue | |
252 | else: |
|
251 | else: | |
253 | if not self.setNextFile(): |
|
252 | if not self.setNextFile(): | |
254 | return 0 |
|
253 | return 0 | |
255 |
|
254 | |||
256 | if (self.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or \ |
|
255 | if (self.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or \ | |
257 | (self.datatime > datetime.datetime.combine(self.endDate, self.endTime)): |
|
256 | (self.datatime > datetime.datetime.combine(self.endDate, self.endTime)): | |
258 | log.warning( |
|
257 | log.warning( | |
259 | 'Reading Record No. {}/{} -> {} [Skipping]'.format( |
|
258 | 'Reading Record No. {}/{} -> {} [Skipping]'.format( | |
260 | self.counter_records, |
|
259 | self.counter_records, | |
261 | self.nrecords, |
|
260 | self.nrecords, | |
262 | self.datatime.ctime()), |
|
261 | self.datatime.ctime()), | |
263 | 'BLTRParamReader') |
|
262 | 'BLTRParamReader') | |
264 | continue |
|
263 | continue | |
265 | break |
|
264 | break | |
266 |
|
265 | |||
267 | log.log('Reading Record No. {} -> {}'.format( |
|
266 | log.log('Reading Record No. {} -> {}'.format( | |
268 | self.counter_records, |
|
267 | self.counter_records, | |
269 | # self.nrecords, |
|
268 | # self.nrecords, | |
270 | self.datatime.ctime()), 'BLTRParamReader') |
|
269 | self.datatime.ctime()), 'BLTRParamReader') | |
271 |
|
270 | |||
272 | return 1 |
|
271 | return 1 | |
273 |
|
272 | |||
274 | def readBlock(self): |
|
273 | def readBlock(self): | |
275 |
|
274 | |||
276 | pointer = self.fp.tell() |
|
275 | pointer = self.fp.tell() | |
277 | header_rec = numpy.fromfile(self.fp, REC_HEADER_STRUCTURE, 1) |
|
276 | header_rec = numpy.fromfile(self.fp, REC_HEADER_STRUCTURE, 1) | |
278 | self.nchannels = int(header_rec['nchan'][0] / 2) |
|
277 | self.nchannels = int(header_rec['nchan'][0] / 2) | |
279 | self.kchan = header_rec['nrxs'][0] |
|
278 | self.kchan = header_rec['nrxs'][0] | |
280 | self.nmodes = header_rec['nmodes'][0] |
|
279 | self.nmodes = header_rec['nmodes'][0] | |
281 | self.nranges = header_rec['nranges'][0] |
|
280 | self.nranges = header_rec['nranges'][0] | |
282 | self.fp.seek(pointer) |
|
281 | self.fp.seek(pointer) | |
283 | self.height = numpy.empty((self.nmodes, self.nranges)) |
|
282 | self.height = numpy.empty((self.nmodes, self.nranges)) | |
284 | self.snr = numpy.empty((self.nmodes, int(self.nchannels), self.nranges)) |
|
283 | self.snr = numpy.empty((self.nmodes, int(self.nchannels), self.nranges)) | |
285 | self.buffer = numpy.empty((self.nmodes, 3, self.nranges)) |
|
284 | self.buffer = numpy.empty((self.nmodes, 3, self.nranges)) | |
286 | self.flagDiscontinuousBlock = 0 |
|
285 | self.flagDiscontinuousBlock = 0 | |
287 |
|
286 | |||
288 | for mode in range(self.nmodes): |
|
287 | for mode in range(self.nmodes): | |
289 | self.readHeader() |
|
288 | self.readHeader() | |
290 | data = self.readData() |
|
289 | data = self.readData() | |
291 | self.height[mode] = (data[0] - self.correction) / 1000. |
|
290 | self.height[mode] = (data[0] - self.correction) / 1000. | |
292 | self.buffer[mode] = data[1] |
|
291 | self.buffer[mode] = data[1] | |
293 | self.snr[mode] = data[2] |
|
292 | self.snr[mode] = data[2] | |
294 |
|
293 | |||
295 | self.counter_records = self.counter_records + self.nmodes |
|
294 | self.counter_records = self.counter_records + self.nmodes | |
296 |
|
295 | |||
297 | return |
|
296 | return | |
298 |
|
297 | |||
299 | def readHeader(self): |
|
298 | def readHeader(self): | |
300 | ''' |
|
299 | ''' | |
301 | RecordHeader of BLTR rawdata file |
|
300 | RecordHeader of BLTR rawdata file | |
302 | ''' |
|
301 | ''' | |
303 |
|
302 | |||
304 | header_structure = numpy.dtype( |
|
303 | header_structure = numpy.dtype( | |
305 | REC_HEADER_STRUCTURE.descr + [ |
|
304 | REC_HEADER_STRUCTURE.descr + [ | |
306 | ('antenna_coord', 'f4', (2, int(self.nchannels))), |
|
305 | ('antenna_coord', 'f4', (2, int(self.nchannels))), | |
307 | ('rx_gains', 'u4', (int(self.nchannels),)), |
|
306 | ('rx_gains', 'u4', (int(self.nchannels),)), | |
308 | ('rx_analysis', 'u4', (int(self.nchannels),)) |
|
307 | ('rx_analysis', 'u4', (int(self.nchannels),)) | |
309 | ] |
|
308 | ] | |
310 | ) |
|
309 | ) | |
311 |
|
310 | |||
312 | self.header_rec = numpy.fromfile(self.fp, header_structure, 1) |
|
311 | self.header_rec = numpy.fromfile(self.fp, header_structure, 1) | |
313 | self.lat = self.header_rec['lat'][0] |
|
312 | self.lat = self.header_rec['lat'][0] | |
314 | self.lon = self.header_rec['lon'][0] |
|
313 | self.lon = self.header_rec['lon'][0] | |
315 | self.delta = self.header_rec['delta_r'][0] |
|
314 | self.delta = self.header_rec['delta_r'][0] | |
316 | self.correction = self.header_rec['dmode_rngcorr'][0] |
|
315 | self.correction = self.header_rec['dmode_rngcorr'][0] | |
317 | self.imode = self.header_rec['dmode_index'][0] |
|
316 | self.imode = self.header_rec['dmode_index'][0] | |
318 | self.antenna = self.header_rec['antenna_coord'] |
|
317 | self.antenna = self.header_rec['antenna_coord'] | |
319 | self.rx_gains = self.header_rec['rx_gains'] |
|
318 | self.rx_gains = self.header_rec['rx_gains'] | |
320 | self.time = self.header_rec['time'][0] |
|
319 | self.time = self.header_rec['time'][0] | |
321 | dt = datetime.datetime.utcfromtimestamp(self.time) |
|
320 | dt = datetime.datetime.utcfromtimestamp(self.time) | |
322 | if dt.date()>self.datatime.date(): |
|
321 | if dt.date()>self.datatime.date(): | |
323 | self.flagDiscontinuousBlock = 1 |
|
322 | self.flagDiscontinuousBlock = 1 | |
324 | self.datatime = dt |
|
323 | self.datatime = dt | |
325 |
|
324 | |||
326 | def readData(self): |
|
325 | def readData(self): | |
327 | ''' |
|
326 | ''' | |
328 | Reading and filtering data block record of BLTR rawdata file, |
|
327 | Reading and filtering data block record of BLTR rawdata file, | |
329 | filtering is according to status_value. |
|
328 | filtering is according to status_value. | |
330 |
|
329 | |||
331 | Input: |
|
330 | Input: | |
332 | status_value - Array data is set to NAN for values that are not |
|
331 | status_value - Array data is set to NAN for values that are not | |
333 | equal to status_value |
|
332 | equal to status_value | |
334 |
|
333 | |||
335 | ''' |
|
334 | ''' | |
336 | self.nchannels = int(self.nchannels) |
|
335 | self.nchannels = int(self.nchannels) | |
337 |
|
336 | |||
338 | data_structure = numpy.dtype( |
|
337 | data_structure = numpy.dtype( | |
339 | DATA_STRUCTURE.descr + [ |
|
338 | DATA_STRUCTURE.descr + [ | |
340 | ('rx_saturation', 'u4', (self.nchannels,)), |
|
339 | ('rx_saturation', 'u4', (self.nchannels,)), | |
341 | ('chan_offset', 'u4', (2 * self.nchannels,)), |
|
340 | ('chan_offset', 'u4', (2 * self.nchannels,)), | |
342 | ('rx_amp', 'u4', (self.nchannels,)), |
|
341 | ('rx_amp', 'u4', (self.nchannels,)), | |
343 | ('rx_snr', 'f4', (self.nchannels,)), |
|
342 | ('rx_snr', 'f4', (self.nchannels,)), | |
344 | ('cross_snr', 'f4', (self.kchan,)), |
|
343 | ('cross_snr', 'f4', (self.kchan,)), | |
345 | ('sea_power_relative', 'f4', (self.kchan,))] |
|
344 | ('sea_power_relative', 'f4', (self.kchan,))] | |
346 | ) |
|
345 | ) | |
347 |
|
346 | |||
348 | data = numpy.fromfile(self.fp, data_structure, self.nranges) |
|
347 | data = numpy.fromfile(self.fp, data_structure, self.nranges) | |
349 |
|
348 | |||
350 | height = data['range'] |
|
349 | height = data['range'] | |
351 | winds = numpy.array( |
|
350 | winds = numpy.array( | |
352 | (data['zonal'], data['meridional'], data['vertical'])) |
|
351 | (data['zonal'], data['meridional'], data['vertical'])) | |
353 | snr = data['rx_snr'].T |
|
352 | snr = data['rx_snr'].T | |
354 |
|
353 | |||
355 | winds[numpy.where(winds == -9999.)] = numpy.nan |
|
354 | winds[numpy.where(winds == -9999.)] = numpy.nan | |
356 | winds[:, numpy.where(data['status'] != self.status_value)] = numpy.nan |
|
355 | winds[:, numpy.where(data['status'] != self.status_value)] = numpy.nan | |
357 | snr[numpy.where(snr == -9999.)] = numpy.nan |
|
356 | snr[numpy.where(snr == -9999.)] = numpy.nan | |
358 | snr[:, numpy.where(data['status'] != self.status_value)] = numpy.nan |
|
357 | snr[:, numpy.where(data['status'] != self.status_value)] = numpy.nan | |
359 | snr = numpy.power(10, snr / 10) |
|
358 | snr = numpy.power(10, snr / 10) | |
360 |
|
359 | |||
361 | return height, winds, snr |
|
360 | return height, winds, snr | |
362 |
|
361 | |||
363 | def set_output(self): |
|
362 | def set_output(self): | |
364 | ''' |
|
363 | ''' | |
365 | Storing data from databuffer to dataOut object |
|
364 | Storing data from databuffer to dataOut object | |
366 | ''' |
|
365 | ''' | |
367 |
|
366 | |||
368 | self.dataOut.data_SNR = self.snr |
|
367 | self.dataOut.data_SNR = self.snr | |
369 | self.dataOut.height = self.height |
|
368 | self.dataOut.height = self.height | |
370 | self.dataOut.data = self.buffer |
|
369 | self.dataOut.data = self.buffer | |
371 | self.dataOut.utctimeInit = self.time |
|
370 | self.dataOut.utctimeInit = self.time | |
372 | self.dataOut.utctime = self.dataOut.utctimeInit |
|
371 | self.dataOut.utctime = self.dataOut.utctimeInit | |
373 | self.dataOut.useLocalTime = False |
|
372 | self.dataOut.useLocalTime = False | |
374 | self.dataOut.paramInterval = 157 |
|
373 | self.dataOut.paramInterval = 157 | |
375 | self.dataOut.timezone = self.timezone |
|
374 | self.dataOut.timezone = self.timezone | |
376 | self.dataOut.site = self.siteFile |
|
375 | self.dataOut.site = self.siteFile | |
377 | self.dataOut.nrecords = self.nrecords / self.nmodes |
|
376 | self.dataOut.nrecords = self.nrecords / self.nmodes | |
378 | self.dataOut.sizeOfFile = self.sizeOfFile |
|
377 | self.dataOut.sizeOfFile = self.sizeOfFile | |
379 | self.dataOut.lat = self.lat |
|
378 | self.dataOut.lat = self.lat | |
380 | self.dataOut.lon = self.lon |
|
379 | self.dataOut.lon = self.lon | |
381 | self.dataOut.channelList = list(range(self.nchannels)) |
|
380 | self.dataOut.channelList = list(range(self.nchannels)) | |
382 | self.dataOut.kchan = self.kchan |
|
381 | self.dataOut.kchan = self.kchan | |
383 | self.dataOut.delta = self.delta |
|
382 | self.dataOut.delta = self.delta | |
384 | self.dataOut.correction = self.correction |
|
383 | self.dataOut.correction = self.correction | |
385 | self.dataOut.nmodes = self.nmodes |
|
384 | self.dataOut.nmodes = self.nmodes | |
386 | self.dataOut.imode = self.imode |
|
385 | self.dataOut.imode = self.imode | |
387 | self.dataOut.antenna = self.antenna |
|
386 | self.dataOut.antenna = self.antenna | |
388 | self.dataOut.rx_gains = self.rx_gains |
|
387 | self.dataOut.rx_gains = self.rx_gains | |
389 | self.dataOut.flagNoData = False |
|
388 | self.dataOut.flagNoData = False | |
390 | self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock |
|
389 | self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock | |
391 |
|
390 | |||
392 | def getData(self): |
|
391 | def getData(self): | |
393 | ''' |
|
392 | ''' | |
394 | Storing data from databuffer to dataOut object |
|
393 | Storing data from databuffer to dataOut object | |
395 | ''' |
|
394 | ''' | |
396 | if self.flagNoMoreFiles: |
|
395 | if self.flagNoMoreFiles: | |
397 | self.dataOut.flagNoData = True |
|
396 | self.dataOut.flagNoData = True | |
398 | self.dataOut.error = 'No More files to read' |
|
397 | self.dataOut.error = 'No More files to read' | |
399 | return |
|
398 | return | |
400 |
|
399 | |||
401 | if not self.readNextBlock(): |
|
400 | if not self.readNextBlock(): | |
402 | self.dataOut.flagNoData = True |
|
401 | self.dataOut.flagNoData = True | |
403 | self.dataOut.error = 'Time for wait new file reach!!!' |
|
402 | self.dataOut.error = 'Time for wait new file reach!!!' | |
404 |
|
403 | |||
405 | self.set_output() |
|
404 | self.set_output() | |
406 |
|
405 | |||
407 | return 1 |
|
406 | return 1 | |
408 | No newline at end of file |
|
407 |
@@ -1,1833 +1,1831 | |||||
1 | ''' |
|
1 | ''' | |
2 | Created on Jul 2, 2014 |
|
2 | Created on Jul 2, 2014 | |
3 |
|
3 | |||
4 | @author: roj-idl71 |
|
4 | @author: roj-idl71 | |
5 | ''' |
|
5 | ''' | |
6 | import os |
|
6 | import os | |
7 | import sys |
|
7 | import sys | |
8 | import glob |
|
8 | import glob | |
9 | import time |
|
9 | import time | |
10 | import numpy |
|
10 | import numpy | |
11 | import fnmatch |
|
11 | import fnmatch | |
12 | import inspect |
|
12 | import inspect | |
13 | import time |
|
13 | import time | |
14 | import datetime |
|
14 | import datetime | |
15 | import traceback |
|
15 | import traceback | |
16 | import zmq |
|
16 | import zmq | |
17 |
|
17 | |||
18 | try: |
|
18 | try: | |
19 | from gevent import sleep |
|
19 | from gevent import sleep | |
20 | except: |
|
20 | except: | |
21 | from time import sleep |
|
21 | from time import sleep | |
22 |
|
22 | |||
23 | from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader |
|
23 | from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader | |
24 | from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width |
|
24 | from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width | |
25 | from schainpy.utils import log |
|
25 | from schainpy.utils import log | |
26 | import schainpy.admin |
|
26 | import schainpy.admin | |
27 |
|
27 | |||
28 | LOCALTIME = True |
|
28 | LOCALTIME = True | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | def isNumber(cad): |
|
31 | def isNumber(cad): | |
32 | """ |
|
32 | """ | |
33 | Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. |
|
33 | Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. | |
34 |
|
34 | |||
35 | Excepciones: |
|
35 | Excepciones: | |
36 | Si un determinado string no puede ser convertido a numero |
|
36 | Si un determinado string no puede ser convertido a numero | |
37 | Input: |
|
37 | Input: | |
38 | str, string al cual se le analiza para determinar si convertible a un numero o no |
|
38 | str, string al cual se le analiza para determinar si convertible a un numero o no | |
39 |
|
39 | |||
40 | Return: |
|
40 | Return: | |
41 | True : si el string es uno numerico |
|
41 | True : si el string es uno numerico | |
42 | False : no es un string numerico |
|
42 | False : no es un string numerico | |
43 | """ |
|
43 | """ | |
44 | try: |
|
44 | try: | |
45 | float(cad) |
|
45 | float(cad) | |
46 | return True |
|
46 | return True | |
47 | except: |
|
47 | except: | |
48 | return False |
|
48 | return False | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | def isFileInEpoch(filename, startUTSeconds, endUTSeconds): |
|
51 | def isFileInEpoch(filename, startUTSeconds, endUTSeconds): | |
52 | """ |
|
52 | """ | |
53 | Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado. |
|
53 | Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado. | |
54 |
|
54 | |||
55 | Inputs: |
|
55 | Inputs: | |
56 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
56 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) | |
57 |
|
57 | |||
58 | startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en |
|
58 | startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en | |
59 | segundos contados desde 01/01/1970. |
|
59 | segundos contados desde 01/01/1970. | |
60 | endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en |
|
60 | endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en | |
61 | segundos contados desde 01/01/1970. |
|
61 | segundos contados desde 01/01/1970. | |
62 |
|
62 | |||
63 | Return: |
|
63 | Return: | |
64 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
64 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de | |
65 | fecha especificado, de lo contrario retorna False. |
|
65 | fecha especificado, de lo contrario retorna False. | |
66 |
|
66 | |||
67 | Excepciones: |
|
67 | Excepciones: | |
68 | Si el archivo no existe o no puede ser abierto |
|
68 | Si el archivo no existe o no puede ser abierto | |
69 | Si la cabecera no puede ser leida. |
|
69 | Si la cabecera no puede ser leida. | |
70 |
|
70 | |||
71 | """ |
|
71 | """ | |
72 | basicHeaderObj = BasicHeader(LOCALTIME) |
|
72 | basicHeaderObj = BasicHeader(LOCALTIME) | |
73 |
|
73 | |||
74 | try: |
|
74 | try: | |
75 | fp = open(filename, 'rb') |
|
75 | fp = open(filename, 'rb') | |
76 | except IOError: |
|
76 | except IOError: | |
77 | print("The file %s can't be opened" % (filename)) |
|
77 | print("The file %s can't be opened" % (filename)) | |
78 | return 0 |
|
78 | return 0 | |
79 |
|
79 | |||
80 | sts = basicHeaderObj.read(fp) |
|
80 | sts = basicHeaderObj.read(fp) | |
81 | fp.close() |
|
81 | fp.close() | |
82 |
|
82 | |||
83 | if not(sts): |
|
83 | if not(sts): | |
84 | print("Skipping the file %s because it has not a valid header" % (filename)) |
|
84 | print("Skipping the file %s because it has not a valid header" % (filename)) | |
85 | return 0 |
|
85 | return 0 | |
86 |
|
86 | |||
87 | if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)): |
|
87 | if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)): | |
88 | return 0 |
|
88 | return 0 | |
89 |
|
89 | |||
90 | return 1 |
|
90 | return 1 | |
91 |
|
91 | |||
92 |
|
92 | |||
93 | def isTimeInRange(thisTime, startTime, endTime): |
|
93 | def isTimeInRange(thisTime, startTime, endTime): | |
94 | if endTime >= startTime: |
|
94 | if endTime >= startTime: | |
95 | if (thisTime < startTime) or (thisTime > endTime): |
|
95 | if (thisTime < startTime) or (thisTime > endTime): | |
96 | return 0 |
|
96 | return 0 | |
97 | return 1 |
|
97 | return 1 | |
98 | else: |
|
98 | else: | |
99 | if (thisTime < startTime) and (thisTime > endTime): |
|
99 | if (thisTime < startTime) and (thisTime > endTime): | |
100 | return 0 |
|
100 | return 0 | |
101 | return 1 |
|
101 | return 1 | |
102 |
|
102 | |||
103 |
|
103 | |||
104 | def isFileInTimeRange(filename, startDate, endDate, startTime, endTime): |
|
104 | def isFileInTimeRange(filename, startDate, endDate, startTime, endTime): | |
105 | """ |
|
105 | """ | |
106 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. |
|
106 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. | |
107 |
|
107 | |||
108 | Inputs: |
|
108 | Inputs: | |
109 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
109 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) | |
110 |
|
110 | |||
111 | startDate : fecha inicial del rango seleccionado en formato datetime.date |
|
111 | startDate : fecha inicial del rango seleccionado en formato datetime.date | |
112 |
|
112 | |||
113 | endDate : fecha final del rango seleccionado en formato datetime.date |
|
113 | endDate : fecha final del rango seleccionado en formato datetime.date | |
114 |
|
114 | |||
115 | startTime : tiempo inicial del rango seleccionado en formato datetime.time |
|
115 | startTime : tiempo inicial del rango seleccionado en formato datetime.time | |
116 |
|
116 | |||
117 | endTime : tiempo final del rango seleccionado en formato datetime.time |
|
117 | endTime : tiempo final del rango seleccionado en formato datetime.time | |
118 |
|
118 | |||
119 | Return: |
|
119 | Return: | |
120 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
120 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de | |
121 | fecha especificado, de lo contrario retorna False. |
|
121 | fecha especificado, de lo contrario retorna False. | |
122 |
|
122 | |||
123 | Excepciones: |
|
123 | Excepciones: | |
124 | Si el archivo no existe o no puede ser abierto |
|
124 | Si el archivo no existe o no puede ser abierto | |
125 | Si la cabecera no puede ser leida. |
|
125 | Si la cabecera no puede ser leida. | |
126 |
|
126 | |||
127 | """ |
|
127 | """ | |
128 |
|
128 | |||
129 | try: |
|
129 | try: | |
130 | fp = open(filename, 'rb') |
|
130 | fp = open(filename, 'rb') | |
131 | except IOError: |
|
131 | except IOError: | |
132 | print("The file %s can't be opened" % (filename)) |
|
132 | print("The file %s can't be opened" % (filename)) | |
133 | return None |
|
133 | return None | |
134 |
|
134 | |||
135 | firstBasicHeaderObj = BasicHeader(LOCALTIME) |
|
135 | firstBasicHeaderObj = BasicHeader(LOCALTIME) | |
136 | systemHeaderObj = SystemHeader() |
|
136 | systemHeaderObj = SystemHeader() | |
137 | radarControllerHeaderObj = RadarControllerHeader() |
|
137 | radarControllerHeaderObj = RadarControllerHeader() | |
138 | processingHeaderObj = ProcessingHeader() |
|
138 | processingHeaderObj = ProcessingHeader() | |
139 |
|
139 | |||
140 | lastBasicHeaderObj = BasicHeader(LOCALTIME) |
|
140 | lastBasicHeaderObj = BasicHeader(LOCALTIME) | |
141 |
|
141 | |||
142 | sts = firstBasicHeaderObj.read(fp) |
|
142 | sts = firstBasicHeaderObj.read(fp) | |
143 |
|
143 | |||
144 | if not(sts): |
|
144 | if not(sts): | |
145 | print("[Reading] Skipping the file %s because it has not a valid header" % (filename)) |
|
145 | print("[Reading] Skipping the file %s because it has not a valid header" % (filename)) | |
146 | return None |
|
146 | return None | |
147 |
|
147 | |||
148 | if not systemHeaderObj.read(fp): |
|
148 | if not systemHeaderObj.read(fp): | |
149 | return None |
|
149 | return None | |
150 |
|
150 | |||
151 | if not radarControllerHeaderObj.read(fp): |
|
151 | if not radarControllerHeaderObj.read(fp): | |
152 | return None |
|
152 | return None | |
153 |
|
153 | |||
154 | if not processingHeaderObj.read(fp): |
|
154 | if not processingHeaderObj.read(fp): | |
155 | return None |
|
155 | return None | |
156 |
|
156 | |||
157 | filesize = os.path.getsize(filename) |
|
157 | filesize = os.path.getsize(filename) | |
158 |
|
158 | |||
159 | offset = processingHeaderObj.blockSize + 24 # header size |
|
159 | offset = processingHeaderObj.blockSize + 24 # header size | |
160 |
|
160 | |||
161 | if filesize <= offset: |
|
161 | if filesize <= offset: | |
162 | print("[Reading] %s: This file has not enough data" % filename) |
|
162 | print("[Reading] %s: This file has not enough data" % filename) | |
163 | return None |
|
163 | return None | |
164 |
|
164 | |||
165 | fp.seek(-offset, 2) |
|
165 | fp.seek(-offset, 2) | |
166 |
|
166 | |||
167 | sts = lastBasicHeaderObj.read(fp) |
|
167 | sts = lastBasicHeaderObj.read(fp) | |
168 |
|
168 | |||
169 | fp.close() |
|
169 | fp.close() | |
170 |
|
170 | |||
171 | thisDatetime = lastBasicHeaderObj.datatime |
|
171 | thisDatetime = lastBasicHeaderObj.datatime | |
172 | thisTime_last_block = thisDatetime.time() |
|
172 | thisTime_last_block = thisDatetime.time() | |
173 |
|
173 | |||
174 | thisDatetime = firstBasicHeaderObj.datatime |
|
174 | thisDatetime = firstBasicHeaderObj.datatime | |
175 | thisDate = thisDatetime.date() |
|
175 | thisDate = thisDatetime.date() | |
176 | thisTime_first_block = thisDatetime.time() |
|
176 | thisTime_first_block = thisDatetime.time() | |
177 |
|
177 | |||
178 | # General case |
|
178 | # General case | |
179 | # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o |
|
179 | # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o | |
180 | #-----------o----------------------------o----------- |
|
180 | #-----------o----------------------------o----------- | |
181 | # startTime endTime |
|
181 | # startTime endTime | |
182 |
|
182 | |||
183 | if endTime >= startTime: |
|
183 | if endTime >= startTime: | |
184 | if (thisTime_last_block < startTime) or (thisTime_first_block > endTime): |
|
184 | if (thisTime_last_block < startTime) or (thisTime_first_block > endTime): | |
185 | return None |
|
185 | return None | |
186 |
|
186 | |||
187 | return thisDatetime |
|
187 | return thisDatetime | |
188 |
|
188 | |||
189 | # If endTime < startTime then endTime belongs to the next day |
|
189 | # If endTime < startTime then endTime belongs to the next day | |
190 |
|
190 | |||
191 | #<<<<<<<<<<<o o>>>>>>>>>>> |
|
191 | #<<<<<<<<<<<o o>>>>>>>>>>> | |
192 | #-----------o----------------------------o----------- |
|
192 | #-----------o----------------------------o----------- | |
193 | # endTime startTime |
|
193 | # endTime startTime | |
194 |
|
194 | |||
195 | if (thisDate == startDate) and (thisTime_last_block < startTime): |
|
195 | if (thisDate == startDate) and (thisTime_last_block < startTime): | |
196 | return None |
|
196 | return None | |
197 |
|
197 | |||
198 | if (thisDate == endDate) and (thisTime_first_block > endTime): |
|
198 | if (thisDate == endDate) and (thisTime_first_block > endTime): | |
199 | return None |
|
199 | return None | |
200 |
|
200 | |||
201 | if (thisTime_last_block < startTime) and (thisTime_first_block > endTime): |
|
201 | if (thisTime_last_block < startTime) and (thisTime_first_block > endTime): | |
202 | return None |
|
202 | return None | |
203 |
|
203 | |||
204 | return thisDatetime |
|
204 | return thisDatetime | |
205 |
|
205 | |||
206 |
|
206 | |||
207 | def isFolderInDateRange(folder, startDate=None, endDate=None): |
|
207 | def isFolderInDateRange(folder, startDate=None, endDate=None): | |
208 | """ |
|
208 | """ | |
209 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. |
|
209 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. | |
210 |
|
210 | |||
211 | Inputs: |
|
211 | Inputs: | |
212 | folder : nombre completo del directorio. |
|
212 | folder : nombre completo del directorio. | |
213 | Su formato deberia ser "/path_root/?YYYYDDD" |
|
213 | Su formato deberia ser "/path_root/?YYYYDDD" | |
214 |
|
214 | |||
215 | siendo: |
|
215 | siendo: | |
216 | YYYY : Anio (ejemplo 2015) |
|
216 | YYYY : Anio (ejemplo 2015) | |
217 | DDD : Dia del anio (ejemplo 305) |
|
217 | DDD : Dia del anio (ejemplo 305) | |
218 |
|
218 | |||
219 | startDate : fecha inicial del rango seleccionado en formato datetime.date |
|
219 | startDate : fecha inicial del rango seleccionado en formato datetime.date | |
220 |
|
220 | |||
221 | endDate : fecha final del rango seleccionado en formato datetime.date |
|
221 | endDate : fecha final del rango seleccionado en formato datetime.date | |
222 |
|
222 | |||
223 | Return: |
|
223 | Return: | |
224 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
224 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de | |
225 | fecha especificado, de lo contrario retorna False. |
|
225 | fecha especificado, de lo contrario retorna False. | |
226 | Excepciones: |
|
226 | Excepciones: | |
227 | Si el directorio no tiene el formato adecuado |
|
227 | Si el directorio no tiene el formato adecuado | |
228 | """ |
|
228 | """ | |
229 |
|
229 | |||
230 | basename = os.path.basename(folder) |
|
230 | basename = os.path.basename(folder) | |
231 |
|
231 | |||
232 | if not isRadarFolder(basename): |
|
232 | if not isRadarFolder(basename): | |
233 | print("The folder %s has not the rigth format" % folder) |
|
233 | print("The folder %s has not the rigth format" % folder) | |
234 | return 0 |
|
234 | return 0 | |
235 |
|
235 | |||
236 | if startDate and endDate: |
|
236 | if startDate and endDate: | |
237 | thisDate = getDateFromRadarFolder(basename) |
|
237 | thisDate = getDateFromRadarFolder(basename) | |
238 |
|
238 | |||
239 | if thisDate < startDate: |
|
239 | if thisDate < startDate: | |
240 | return 0 |
|
240 | return 0 | |
241 |
|
241 | |||
242 | if thisDate > endDate: |
|
242 | if thisDate > endDate: | |
243 | return 0 |
|
243 | return 0 | |
244 |
|
244 | |||
245 | return 1 |
|
245 | return 1 | |
246 |
|
246 | |||
247 |
|
247 | |||
248 | def isFileInDateRange(filename, startDate=None, endDate=None): |
|
248 | def isFileInDateRange(filename, startDate=None, endDate=None): | |
249 | """ |
|
249 | """ | |
250 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. |
|
250 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. | |
251 |
|
251 | |||
252 | Inputs: |
|
252 | Inputs: | |
253 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
253 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) | |
254 |
|
254 | |||
255 | Su formato deberia ser "?YYYYDDDsss" |
|
255 | Su formato deberia ser "?YYYYDDDsss" | |
256 |
|
256 | |||
257 | siendo: |
|
257 | siendo: | |
258 | YYYY : Anio (ejemplo 2015) |
|
258 | YYYY : Anio (ejemplo 2015) | |
259 | DDD : Dia del anio (ejemplo 305) |
|
259 | DDD : Dia del anio (ejemplo 305) | |
260 | sss : set |
|
260 | sss : set | |
261 |
|
261 | |||
262 | startDate : fecha inicial del rango seleccionado en formato datetime.date |
|
262 | startDate : fecha inicial del rango seleccionado en formato datetime.date | |
263 |
|
263 | |||
264 | endDate : fecha final del rango seleccionado en formato datetime.date |
|
264 | endDate : fecha final del rango seleccionado en formato datetime.date | |
265 |
|
265 | |||
266 | Return: |
|
266 | Return: | |
267 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
267 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de | |
268 | fecha especificado, de lo contrario retorna False. |
|
268 | fecha especificado, de lo contrario retorna False. | |
269 | Excepciones: |
|
269 | Excepciones: | |
270 | Si el archivo no tiene el formato adecuado |
|
270 | Si el archivo no tiene el formato adecuado | |
271 | """ |
|
271 | """ | |
272 |
|
272 | |||
273 | basename = os.path.basename(filename) |
|
273 | basename = os.path.basename(filename) | |
274 |
|
274 | |||
275 | if not isRadarFile(basename): |
|
275 | if not isRadarFile(basename): | |
276 | print("The filename %s has not the rigth format" % filename) |
|
276 | print("The filename %s has not the rigth format" % filename) | |
277 | return 0 |
|
277 | return 0 | |
278 |
|
278 | |||
279 | if startDate and endDate: |
|
279 | if startDate and endDate: | |
280 | thisDate = getDateFromRadarFile(basename) |
|
280 | thisDate = getDateFromRadarFile(basename) | |
281 |
|
281 | |||
282 | if thisDate < startDate: |
|
282 | if thisDate < startDate: | |
283 | return 0 |
|
283 | return 0 | |
284 |
|
284 | |||
285 | if thisDate > endDate: |
|
285 | if thisDate > endDate: | |
286 | return 0 |
|
286 | return 0 | |
287 |
|
287 | |||
288 | return 1 |
|
288 | return 1 | |
289 |
|
289 | |||
290 |
|
290 | |||
291 | def getFileFromSet(path, ext, set): |
|
291 | def getFileFromSet(path, ext, set): | |
292 | validFilelist = [] |
|
292 | validFilelist = [] | |
293 | fileList = os.listdir(path) |
|
293 | fileList = os.listdir(path) | |
294 |
|
294 | |||
295 | # 0 1234 567 89A BCDE |
|
295 | # 0 1234 567 89A BCDE | |
296 | # H YYYY DDD SSS .ext |
|
296 | # H YYYY DDD SSS .ext | |
297 |
|
297 | |||
298 | for thisFile in fileList: |
|
298 | for thisFile in fileList: | |
299 | try: |
|
299 | try: | |
300 | year = int(thisFile[1:5]) |
|
300 | year = int(thisFile[1:5]) | |
301 | doy = int(thisFile[5:8]) |
|
301 | doy = int(thisFile[5:8]) | |
302 | except: |
|
302 | except: | |
303 | continue |
|
303 | continue | |
304 |
|
304 | |||
305 | if (os.path.splitext(thisFile)[-1].lower() != ext.lower()): |
|
305 | if (os.path.splitext(thisFile)[-1].lower() != ext.lower()): | |
306 | continue |
|
306 | continue | |
307 |
|
307 | |||
308 | validFilelist.append(thisFile) |
|
308 | validFilelist.append(thisFile) | |
309 |
|
309 | |||
310 | myfile = fnmatch.filter( |
|
310 | myfile = fnmatch.filter( | |
311 | validFilelist, '*%4.4d%3.3d%3.3d*' % (year, doy, set)) |
|
311 | validFilelist, '*%4.4d%3.3d%3.3d*' % (year, doy, set)) | |
312 |
|
312 | |||
313 | if len(myfile) != 0: |
|
313 | if len(myfile) != 0: | |
314 | return myfile[0] |
|
314 | return myfile[0] | |
315 | else: |
|
315 | else: | |
316 | filename = '*%4.4d%3.3d%3.3d%s' % (year, doy, set, ext.lower()) |
|
316 | filename = '*%4.4d%3.3d%3.3d%s' % (year, doy, set, ext.lower()) | |
317 | print('the filename %s does not exist' % filename) |
|
317 | print('the filename %s does not exist' % filename) | |
318 | print('...going to the last file: ') |
|
318 | print('...going to the last file: ') | |
319 |
|
319 | |||
320 | if validFilelist: |
|
320 | if validFilelist: | |
321 | validFilelist = sorted(validFilelist, key=str.lower) |
|
321 | validFilelist = sorted(validFilelist, key=str.lower) | |
322 | return validFilelist[-1] |
|
322 | return validFilelist[-1] | |
323 |
|
323 | |||
324 | return None |
|
324 | return None | |
325 |
|
325 | |||
326 |
|
326 | |||
327 | def getlastFileFromPath(path, ext): |
|
327 | def getlastFileFromPath(path, ext): | |
328 | """ |
|
328 | """ | |
329 | Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext" |
|
329 | Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext" | |
330 | al final de la depuracion devuelve el ultimo file de la lista que quedo. |
|
330 | al final de la depuracion devuelve el ultimo file de la lista que quedo. | |
331 |
|
331 | |||
332 | Input: |
|
332 | Input: | |
333 | fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta |
|
333 | fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta | |
334 | ext : extension de los files contenidos en una carpeta |
|
334 | ext : extension de los files contenidos en una carpeta | |
335 |
|
335 | |||
336 | Return: |
|
336 | Return: | |
337 | El ultimo file de una determinada carpeta, no se considera el path. |
|
337 | El ultimo file de una determinada carpeta, no se considera el path. | |
338 | """ |
|
338 | """ | |
339 | validFilelist = [] |
|
339 | validFilelist = [] | |
340 | fileList = os.listdir(path) |
|
340 | fileList = os.listdir(path) | |
341 |
|
341 | |||
342 | # 0 1234 567 89A BCDE |
|
342 | # 0 1234 567 89A BCDE | |
343 | # H YYYY DDD SSS .ext |
|
343 | # H YYYY DDD SSS .ext | |
344 |
|
344 | |||
345 | for thisFile in fileList: |
|
345 | for thisFile in fileList: | |
346 |
|
346 | |||
347 | year = thisFile[1:5] |
|
347 | year = thisFile[1:5] | |
348 | if not isNumber(year): |
|
348 | if not isNumber(year): | |
349 | continue |
|
349 | continue | |
350 |
|
350 | |||
351 | doy = thisFile[5:8] |
|
351 | doy = thisFile[5:8] | |
352 | if not isNumber(doy): |
|
352 | if not isNumber(doy): | |
353 | continue |
|
353 | continue | |
354 |
|
354 | |||
355 | year = int(year) |
|
355 | year = int(year) | |
356 | doy = int(doy) |
|
356 | doy = int(doy) | |
357 |
|
357 | |||
358 | if (os.path.splitext(thisFile)[-1].lower() != ext.lower()): |
|
358 | if (os.path.splitext(thisFile)[-1].lower() != ext.lower()): | |
359 | continue |
|
359 | continue | |
360 |
|
360 | |||
361 | validFilelist.append(thisFile) |
|
361 | validFilelist.append(thisFile) | |
362 |
|
362 | |||
363 | if validFilelist: |
|
363 | if validFilelist: | |
364 | validFilelist = sorted(validFilelist, key=str.lower) |
|
364 | validFilelist = sorted(validFilelist, key=str.lower) | |
365 | return validFilelist[-1] |
|
365 | return validFilelist[-1] | |
366 |
|
366 | |||
367 | return None |
|
367 | return None | |
368 |
|
368 | |||
369 |
|
369 | |||
370 | def checkForRealPath(path, foldercounter, year, doy, set, ext): |
|
370 | def checkForRealPath(path, foldercounter, year, doy, set, ext): | |
371 | """ |
|
371 | """ | |
372 | Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path, |
|
372 | Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path, | |
373 | Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar |
|
373 | Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar | |
374 | el path exacto de un determinado file. |
|
374 | el path exacto de un determinado file. | |
375 |
|
375 | |||
376 | Example : |
|
376 | Example : | |
377 | nombre correcto del file es .../.../D2009307/P2009307367.ext |
|
377 | nombre correcto del file es .../.../D2009307/P2009307367.ext | |
378 |
|
378 | |||
379 | Entonces la funcion prueba con las siguientes combinaciones |
|
379 | Entonces la funcion prueba con las siguientes combinaciones | |
380 | .../.../y2009307367.ext |
|
380 | .../.../y2009307367.ext | |
381 | .../.../Y2009307367.ext |
|
381 | .../.../Y2009307367.ext | |
382 | .../.../x2009307/y2009307367.ext |
|
382 | .../.../x2009307/y2009307367.ext | |
383 | .../.../x2009307/Y2009307367.ext |
|
383 | .../.../x2009307/Y2009307367.ext | |
384 | .../.../X2009307/y2009307367.ext |
|
384 | .../.../X2009307/y2009307367.ext | |
385 | .../.../X2009307/Y2009307367.ext |
|
385 | .../.../X2009307/Y2009307367.ext | |
386 | siendo para este caso, la ultima combinacion de letras, identica al file buscado |
|
386 | siendo para este caso, la ultima combinacion de letras, identica al file buscado | |
387 |
|
387 | |||
388 | Return: |
|
388 | Return: | |
389 | Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file |
|
389 | Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file | |
390 | caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas |
|
390 | caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas | |
391 | para el filename |
|
391 | para el filename | |
392 | """ |
|
392 | """ | |
393 | fullfilename = None |
|
393 | fullfilename = None | |
394 | find_flag = False |
|
394 | find_flag = False | |
395 | filename = None |
|
395 | filename = None | |
396 |
|
396 | |||
397 | prefixDirList = [None, 'd', 'D'] |
|
397 | prefixDirList = [None, 'd', 'D'] | |
398 | if ext.lower() == ".r": # voltage |
|
398 | if ext.lower() == ".r": # voltage | |
399 | prefixFileList = ['d', 'D'] |
|
399 | prefixFileList = ['d', 'D'] | |
400 | elif ext.lower() == ".pdata": # spectra |
|
400 | elif ext.lower() == ".pdata": # spectra | |
401 | prefixFileList = ['p', 'P'] |
|
401 | prefixFileList = ['p', 'P'] | |
402 | else: |
|
402 | else: | |
403 | return None, filename |
|
403 | return None, filename | |
404 |
|
404 | |||
405 | # barrido por las combinaciones posibles |
|
405 | # barrido por las combinaciones posibles | |
406 | for prefixDir in prefixDirList: |
|
406 | for prefixDir in prefixDirList: | |
407 | thispath = path |
|
407 | thispath = path | |
408 | if prefixDir != None: |
|
408 | if prefixDir != None: | |
409 | # formo el nombre del directorio xYYYYDDD (x=d o x=D) |
|
409 | # formo el nombre del directorio xYYYYDDD (x=d o x=D) | |
410 | if foldercounter == 0: |
|
410 | if foldercounter == 0: | |
411 | thispath = os.path.join(path, "%s%04d%03d" % |
|
411 | thispath = os.path.join(path, "%s%04d%03d" % | |
412 | (prefixDir, year, doy)) |
|
412 | (prefixDir, year, doy)) | |
413 | else: |
|
413 | else: | |
414 | thispath = os.path.join(path, "%s%04d%03d_%02d" % ( |
|
414 | thispath = os.path.join(path, "%s%04d%03d_%02d" % ( | |
415 | prefixDir, year, doy, foldercounter)) |
|
415 | prefixDir, year, doy, foldercounter)) | |
416 | for prefixFile in prefixFileList: # barrido por las dos combinaciones posibles de "D" |
|
416 | for prefixFile in prefixFileList: # barrido por las dos combinaciones posibles de "D" | |
417 | # formo el nombre del file xYYYYDDDSSS.ext |
|
417 | # formo el nombre del file xYYYYDDDSSS.ext | |
418 | filename = "%s%04d%03d%03d%s" % (prefixFile, year, doy, set, ext) |
|
418 | filename = "%s%04d%03d%03d%s" % (prefixFile, year, doy, set, ext) | |
419 | fullfilename = os.path.join( |
|
419 | fullfilename = os.path.join( | |
420 | thispath, filename) # formo el path completo |
|
420 | thispath, filename) # formo el path completo | |
421 |
|
421 | |||
422 | if os.path.exists(fullfilename): # verifico que exista |
|
422 | if os.path.exists(fullfilename): # verifico que exista | |
423 | find_flag = True |
|
423 | find_flag = True | |
424 | break |
|
424 | break | |
425 | if find_flag: |
|
425 | if find_flag: | |
426 | break |
|
426 | break | |
427 |
|
427 | |||
428 | if not(find_flag): |
|
428 | if not(find_flag): | |
429 | return None, filename |
|
429 | return None, filename | |
430 |
|
430 | |||
431 | return fullfilename, filename |
|
431 | return fullfilename, filename | |
432 |
|
432 | |||
433 |
|
433 | |||
434 | def isRadarFolder(folder): |
|
434 | def isRadarFolder(folder): | |
435 | try: |
|
435 | try: | |
436 | year = int(folder[1:5]) |
|
436 | year = int(folder[1:5]) | |
437 | doy = int(folder[5:8]) |
|
437 | doy = int(folder[5:8]) | |
438 | except: |
|
438 | except: | |
439 | return 0 |
|
439 | return 0 | |
440 |
|
440 | |||
441 | return 1 |
|
441 | return 1 | |
442 |
|
442 | |||
443 |
|
443 | |||
444 | def isRadarFile(file): |
|
444 | def isRadarFile(file): | |
445 | try: |
|
445 | try: | |
446 | year = int(file[1:5]) |
|
446 | year = int(file[1:5]) | |
447 | doy = int(file[5:8]) |
|
447 | doy = int(file[5:8]) | |
448 | set = int(file[8:11]) |
|
448 | set = int(file[8:11]) | |
449 | except: |
|
449 | except: | |
450 | return 0 |
|
450 | return 0 | |
451 |
|
451 | |||
452 | return 1 |
|
452 | return 1 | |
453 |
|
453 | |||
454 |
|
454 | |||
455 | def getDateFromRadarFile(file): |
|
455 | def getDateFromRadarFile(file): | |
456 | try: |
|
456 | try: | |
457 | year = int(file[1:5]) |
|
457 | year = int(file[1:5]) | |
458 | doy = int(file[5:8]) |
|
458 | doy = int(file[5:8]) | |
459 | set = int(file[8:11]) |
|
459 | set = int(file[8:11]) | |
460 | except: |
|
460 | except: | |
461 | return None |
|
461 | return None | |
462 |
|
462 | |||
463 | thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1) |
|
463 | thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1) | |
464 | return thisDate |
|
464 | return thisDate | |
465 |
|
465 | |||
466 |
|
466 | |||
467 | def getDateFromRadarFolder(folder): |
|
467 | def getDateFromRadarFolder(folder): | |
468 | try: |
|
468 | try: | |
469 | year = int(folder[1:5]) |
|
469 | year = int(folder[1:5]) | |
470 | doy = int(folder[5:8]) |
|
470 | doy = int(folder[5:8]) | |
471 | except: |
|
471 | except: | |
472 | return None |
|
472 | return None | |
473 |
|
473 | |||
474 | thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1) |
|
474 | thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1) | |
475 | return thisDate |
|
475 | return thisDate | |
476 |
|
476 | |||
477 |
|
477 | |||
478 | class JRODataIO: |
|
478 | class JRODataIO: | |
479 |
|
479 | |||
480 | c = 3E8 |
|
480 | c = 3E8 | |
481 |
|
481 | |||
482 | isConfig = False |
|
482 | isConfig = False | |
483 |
|
483 | |||
484 | basicHeaderObj = None |
|
484 | basicHeaderObj = None | |
485 |
|
485 | |||
486 | systemHeaderObj = None |
|
486 | systemHeaderObj = None | |
487 |
|
487 | |||
488 | radarControllerHeaderObj = None |
|
488 | radarControllerHeaderObj = None | |
489 |
|
489 | |||
490 | processingHeaderObj = None |
|
490 | processingHeaderObj = None | |
491 |
|
491 | |||
492 | dtype = None |
|
492 | dtype = None | |
493 |
|
493 | |||
494 | pathList = [] |
|
494 | pathList = [] | |
495 |
|
495 | |||
496 | filenameList = [] |
|
496 | filenameList = [] | |
497 |
|
497 | |||
498 | filename = None |
|
498 | filename = None | |
499 |
|
499 | |||
500 | ext = None |
|
500 | ext = None | |
501 |
|
501 | |||
502 | flagIsNewFile = 1 |
|
502 | flagIsNewFile = 1 | |
503 |
|
503 | |||
504 | flagDiscontinuousBlock = 0 |
|
504 | flagDiscontinuousBlock = 0 | |
505 |
|
505 | |||
506 | flagIsNewBlock = 0 |
|
506 | flagIsNewBlock = 0 | |
507 |
|
507 | |||
508 | fp = None |
|
508 | fp = None | |
509 |
|
509 | |||
510 | firstHeaderSize = 0 |
|
510 | firstHeaderSize = 0 | |
511 |
|
511 | |||
512 | basicHeaderSize = 24 |
|
512 | basicHeaderSize = 24 | |
513 |
|
513 | |||
514 | versionFile = 1103 |
|
514 | versionFile = 1103 | |
515 |
|
515 | |||
516 | fileSize = None |
|
516 | fileSize = None | |
517 |
|
517 | |||
518 | # ippSeconds = None |
|
518 | # ippSeconds = None | |
519 |
|
519 | |||
520 | fileSizeByHeader = None |
|
520 | fileSizeByHeader = None | |
521 |
|
521 | |||
522 | fileIndex = None |
|
522 | fileIndex = None | |
523 |
|
523 | |||
524 | profileIndex = None |
|
524 | profileIndex = None | |
525 |
|
525 | |||
526 | blockIndex = None |
|
526 | blockIndex = None | |
527 |
|
527 | |||
528 | nTotalBlocks = None |
|
528 | nTotalBlocks = None | |
529 |
|
529 | |||
530 | maxTimeStep = 30 |
|
530 | maxTimeStep = 30 | |
531 |
|
531 | |||
532 | lastUTTime = None |
|
532 | lastUTTime = None | |
533 |
|
533 | |||
534 | datablock = None |
|
534 | datablock = None | |
535 |
|
535 | |||
536 | dataOut = None |
|
536 | dataOut = None | |
537 |
|
537 | |||
538 | blocksize = None |
|
538 | blocksize = None | |
539 |
|
539 | |||
540 | getByBlock = False |
|
540 | getByBlock = False | |
541 |
|
541 | |||
542 | def __init__(self): |
|
542 | def __init__(self): | |
543 |
|
543 | |||
544 | raise NotImplementedError |
|
544 | raise NotImplementedError | |
545 |
|
545 | |||
546 | def run(self): |
|
546 | def run(self): | |
547 |
|
547 | |||
548 | raise NotImplementedError |
|
548 | raise NotImplementedError | |
549 |
|
549 | |||
550 | def getDtypeWidth(self): |
|
550 | def getDtypeWidth(self): | |
551 |
|
551 | |||
552 | dtype_index = get_dtype_index(self.dtype) |
|
552 | dtype_index = get_dtype_index(self.dtype) | |
553 | dtype_width = get_dtype_width(dtype_index) |
|
553 | dtype_width = get_dtype_width(dtype_index) | |
554 |
|
554 | |||
555 | return dtype_width |
|
555 | return dtype_width | |
556 |
|
556 | |||
557 | def getAllowedArgs(self): |
|
557 | def getAllowedArgs(self): | |
558 | if hasattr(self, '__attrs__'): |
|
558 | if hasattr(self, '__attrs__'): | |
559 | return self.__attrs__ |
|
559 | return self.__attrs__ | |
560 | else: |
|
560 | else: | |
561 | return inspect.getargspec(self.run).args |
|
561 | return inspect.getargspec(self.run).args | |
562 |
|
562 | |||
563 |
|
563 | |||
564 | class JRODataReader(JRODataIO): |
|
564 | class JRODataReader(JRODataIO): | |
565 |
|
565 | |||
566 | online = 0 |
|
566 | online = 0 | |
567 |
|
567 | |||
568 | realtime = 0 |
|
568 | realtime = 0 | |
569 |
|
569 | |||
570 | nReadBlocks = 0 |
|
570 | nReadBlocks = 0 | |
571 |
|
571 | |||
572 | delay = 10 # number of seconds waiting a new file |
|
572 | delay = 10 # number of seconds waiting a new file | |
573 |
|
573 | |||
574 | nTries = 3 # quantity tries |
|
574 | nTries = 3 # quantity tries | |
575 |
|
575 | |||
576 | nFiles = 3 # number of files for searching |
|
576 | nFiles = 3 # number of files for searching | |
577 |
|
577 | |||
578 | path = None |
|
578 | path = None | |
579 |
|
579 | |||
580 | foldercounter = 0 |
|
580 | foldercounter = 0 | |
581 |
|
581 | |||
582 | flagNoMoreFiles = 0 |
|
582 | flagNoMoreFiles = 0 | |
583 |
|
583 | |||
584 | datetimeList = [] |
|
584 | datetimeList = [] | |
585 |
|
585 | |||
586 | __isFirstTimeOnline = 1 |
|
586 | __isFirstTimeOnline = 1 | |
587 |
|
587 | |||
588 | __printInfo = True |
|
588 | __printInfo = True | |
589 |
|
589 | |||
590 | profileIndex = None |
|
590 | profileIndex = None | |
591 |
|
591 | |||
592 | nTxs = 1 |
|
592 | nTxs = 1 | |
593 |
|
593 | |||
594 | txIndex = None |
|
594 | txIndex = None | |
595 |
|
595 | |||
596 | # Added-------------------- |
|
596 | # Added-------------------- | |
597 |
|
597 | |||
598 | selBlocksize = None |
|
598 | selBlocksize = None | |
599 |
|
599 | |||
600 | selBlocktime = None |
|
600 | selBlocktime = None | |
601 |
|
601 | |||
602 | def __init__(self): |
|
602 | def __init__(self): | |
603 | """ |
|
603 | """ | |
604 | This class is used to find data files |
|
604 | This class is used to find data files | |
605 |
|
605 | |||
606 | Example: |
|
606 | Example: | |
607 | reader = JRODataReader() |
|
607 | reader = JRODataReader() | |
608 | fileList = reader.findDataFiles() |
|
608 | fileList = reader.findDataFiles() | |
609 |
|
609 | |||
610 | """ |
|
610 | """ | |
611 | pass |
|
611 | pass | |
612 |
|
612 | |||
613 | def createObjByDefault(self): |
|
613 | def createObjByDefault(self): | |
614 | """ |
|
614 | """ | |
615 |
|
615 | |||
616 | """ |
|
616 | """ | |
617 | raise NotImplementedError |
|
617 | raise NotImplementedError | |
618 |
|
618 | |||
619 | def getBlockDimension(self): |
|
619 | def getBlockDimension(self): | |
620 |
|
620 | |||
621 | raise NotImplementedError |
|
621 | raise NotImplementedError | |
622 |
|
622 | |||
623 | def searchFilesOffLine(self, |
|
623 | def searchFilesOffLine(self, | |
624 | path, |
|
624 | path, | |
625 | startDate=None, |
|
625 | startDate=None, | |
626 | endDate=None, |
|
626 | endDate=None, | |
627 | startTime=datetime.time(0, 0, 0), |
|
627 | startTime=datetime.time(0, 0, 0), | |
628 | endTime=datetime.time(23, 59, 59), |
|
628 | endTime=datetime.time(23, 59, 59), | |
629 | set=None, |
|
629 | set=None, | |
630 | expLabel='', |
|
630 | expLabel='', | |
631 | ext='.r', |
|
631 | ext='.r', | |
632 | cursor=None, |
|
632 | cursor=None, | |
633 | skip=None, |
|
633 | skip=None, | |
634 | walk=True): |
|
634 | walk=True): | |
635 |
|
635 | |||
636 | self.filenameList = [] |
|
636 | self.filenameList = [] | |
637 | self.datetimeList = [] |
|
637 | self.datetimeList = [] | |
638 |
|
638 | |||
639 | pathList = [] |
|
639 | pathList = [] | |
640 |
|
640 | |||
641 | dateList, pathList = self.findDatafiles( |
|
641 | dateList, pathList = self.findDatafiles( | |
642 | path, startDate, endDate, expLabel, ext, walk, include_path=True) |
|
642 | path, startDate, endDate, expLabel, ext, walk, include_path=True) | |
643 |
|
643 | |||
644 | if dateList == []: |
|
644 | if dateList == []: | |
645 | return [], [] |
|
645 | return [], [] | |
646 |
|
646 | |||
647 | if len(dateList) > 1: |
|
647 | if len(dateList) > 1: | |
648 | print("[Reading] Data found for date range [%s - %s]: total days = %d" % (startDate, endDate, len(dateList))) |
|
648 | print("[Reading] Data found for date range [%s - %s]: total days = %d" % (startDate, endDate, len(dateList))) | |
649 | else: |
|
649 | else: | |
650 | print("[Reading] Data found for date range [%s - %s]: date = %s" % (startDate, endDate, dateList[0])) |
|
650 | print("[Reading] Data found for date range [%s - %s]: date = %s" % (startDate, endDate, dateList[0])) | |
651 |
|
651 | |||
652 | filenameList = [] |
|
652 | filenameList = [] | |
653 | datetimeList = [] |
|
653 | datetimeList = [] | |
654 |
|
654 | |||
655 | for thisPath in pathList: |
|
655 | for thisPath in pathList: | |
656 |
|
656 | |||
657 | fileList = glob.glob1(thisPath, "*%s" % ext) |
|
657 | fileList = glob.glob1(thisPath, "*%s" % ext) | |
658 | fileList.sort() |
|
658 | fileList.sort() | |
659 |
|
659 | |||
660 | for file in fileList: |
|
660 | for file in fileList: | |
661 |
|
661 | |||
662 | filename = os.path.join(thisPath, file) |
|
662 | filename = os.path.join(thisPath, file) | |
663 |
|
663 | |||
664 | if not isFileInDateRange(filename, startDate, endDate): |
|
664 | if not isFileInDateRange(filename, startDate, endDate): | |
665 | continue |
|
665 | continue | |
666 |
|
666 | |||
667 | thisDatetime = isFileInTimeRange( |
|
667 | thisDatetime = isFileInTimeRange( | |
668 | filename, startDate, endDate, startTime, endTime) |
|
668 | filename, startDate, endDate, startTime, endTime) | |
669 |
|
669 | |||
670 | if not(thisDatetime): |
|
670 | if not(thisDatetime): | |
671 | continue |
|
671 | continue | |
672 |
|
672 | |||
673 | filenameList.append(filename) |
|
673 | filenameList.append(filename) | |
674 | datetimeList.append(thisDatetime) |
|
674 | datetimeList.append(thisDatetime) | |
675 |
|
675 | |||
676 | if cursor is not None and skip is not None: |
|
676 | if cursor is not None and skip is not None: | |
677 | filenameList = filenameList[cursor * skip:cursor * skip + skip] |
|
677 | filenameList = filenameList[cursor * skip:cursor * skip + skip] | |
678 | datetimeList = datetimeList[cursor * skip:cursor * skip + skip] |
|
678 | datetimeList = datetimeList[cursor * skip:cursor * skip + skip] | |
679 |
|
679 | |||
680 | if not(filenameList): |
|
680 | if not(filenameList): | |
681 | print("[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" % (startTime, endTime, ext, path)) |
|
681 | print("[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" % (startTime, endTime, ext, path)) | |
682 | return [], [] |
|
682 | return [], [] | |
683 |
|
683 | |||
684 | print("[Reading] %d file(s) was(were) found in time range: %s - %s" % (len(filenameList), startTime, endTime)) |
|
684 | print("[Reading] %d file(s) was(were) found in time range: %s - %s" % (len(filenameList), startTime, endTime)) | |
685 |
|
685 | |||
686 | # for i in range(len(filenameList)): |
|
686 | # for i in range(len(filenameList)): | |
687 | # print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime()) |
|
687 | # print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime()) | |
688 |
|
688 | |||
689 | self.filenameList = filenameList |
|
689 | self.filenameList = filenameList | |
690 | self.datetimeList = datetimeList |
|
690 | self.datetimeList = datetimeList | |
691 |
|
691 | |||
692 | return pathList, filenameList |
|
692 | return pathList, filenameList | |
693 |
|
693 | |||
694 | def __searchFilesOnLine(self, path, expLabel="", ext=None, walk=True, set=None): |
|
694 | def __searchFilesOnLine(self, path, expLabel="", ext=None, walk=True, set=None): | |
695 | """ |
|
695 | """ | |
696 | Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y |
|
696 | Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y | |
697 | devuelve el archivo encontrado ademas de otros datos. |
|
697 | devuelve el archivo encontrado ademas de otros datos. | |
698 |
|
698 | |||
699 | Input: |
|
699 | Input: | |
700 | path : carpeta donde estan contenidos los files que contiene data |
|
700 | path : carpeta donde estan contenidos los files que contiene data | |
701 |
|
701 | |||
702 | expLabel : Nombre del subexperimento (subfolder) |
|
702 | expLabel : Nombre del subexperimento (subfolder) | |
703 |
|
703 | |||
704 | ext : extension de los files |
|
704 | ext : extension de los files | |
705 |
|
705 | |||
706 | walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath) |
|
706 | walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath) | |
707 |
|
707 | |||
708 | Return: |
|
708 | Return: | |
709 | directory : eL directorio donde esta el file encontrado |
|
709 | directory : eL directorio donde esta el file encontrado | |
710 | filename : el ultimo file de una determinada carpeta |
|
710 | filename : el ultimo file de una determinada carpeta | |
711 | year : el anho |
|
711 | year : el anho | |
712 | doy : el numero de dia del anho |
|
712 | doy : el numero de dia del anho | |
713 | set : el set del archivo |
|
713 | set : el set del archivo | |
714 |
|
714 | |||
715 |
|
715 | |||
716 | """ |
|
716 | """ | |
717 | if not os.path.isdir(path): |
|
717 | if not os.path.isdir(path): | |
718 | return None, None, None, None, None, None |
|
718 | return None, None, None, None, None, None | |
719 |
|
719 | |||
720 | dirList = [] |
|
720 | dirList = [] | |
721 |
|
721 | |||
722 | if not walk: |
|
722 | if not walk: | |
723 | fullpath = path |
|
723 | fullpath = path | |
724 | foldercounter = 0 |
|
724 | foldercounter = 0 | |
725 | else: |
|
725 | else: | |
726 | # Filtra solo los directorios |
|
726 | # Filtra solo los directorios | |
727 | for thisPath in os.listdir(path): |
|
727 | for thisPath in os.listdir(path): | |
728 | if not os.path.isdir(os.path.join(path, thisPath)): |
|
728 | if not os.path.isdir(os.path.join(path, thisPath)): | |
729 | continue |
|
729 | continue | |
730 | if not isRadarFolder(thisPath): |
|
730 | if not isRadarFolder(thisPath): | |
731 | continue |
|
731 | continue | |
732 |
|
732 | |||
733 | dirList.append(thisPath) |
|
733 | dirList.append(thisPath) | |
734 |
|
734 | |||
735 | if not(dirList): |
|
735 | if not(dirList): | |
736 | return None, None, None, None, None, None |
|
736 | return None, None, None, None, None, None | |
737 |
|
737 | |||
738 | dirList = sorted(dirList, key=str.lower) |
|
738 | dirList = sorted(dirList, key=str.lower) | |
739 |
|
739 | |||
740 | doypath = dirList[-1] |
|
740 | doypath = dirList[-1] | |
741 | foldercounter = int(doypath.split('_')[1]) if len( |
|
741 | foldercounter = int(doypath.split('_')[1]) if len( | |
742 | doypath.split('_')) > 1 else 0 |
|
742 | doypath.split('_')) > 1 else 0 | |
743 | fullpath = os.path.join(path, doypath, expLabel) |
|
743 | fullpath = os.path.join(path, doypath, expLabel) | |
744 |
|
744 | |||
745 | print("[Reading] %s folder was found: " % (fullpath)) |
|
745 | print("[Reading] %s folder was found: " % (fullpath)) | |
746 |
|
746 | |||
747 | if set == None: |
|
747 | if set == None: | |
748 | filename = getlastFileFromPath(fullpath, ext) |
|
748 | filename = getlastFileFromPath(fullpath, ext) | |
749 | else: |
|
749 | else: | |
750 | filename = getFileFromSet(fullpath, ext, set) |
|
750 | filename = getFileFromSet(fullpath, ext, set) | |
751 |
|
751 | |||
752 | if not(filename): |
|
752 | if not(filename): | |
753 | return None, None, None, None, None, None |
|
753 | return None, None, None, None, None, None | |
754 |
|
754 | |||
755 | print("[Reading] %s file was found" % (filename)) |
|
755 | print("[Reading] %s file was found" % (filename)) | |
756 |
|
756 | |||
757 | if not(self.__verifyFile(os.path.join(fullpath, filename))): |
|
757 | if not(self.__verifyFile(os.path.join(fullpath, filename))): | |
758 | return None, None, None, None, None, None |
|
758 | return None, None, None, None, None, None | |
759 |
|
759 | |||
760 | year = int(filename[1:5]) |
|
760 | year = int(filename[1:5]) | |
761 | doy = int(filename[5:8]) |
|
761 | doy = int(filename[5:8]) | |
762 | set = int(filename[8:11]) |
|
762 | set = int(filename[8:11]) | |
763 |
|
763 | |||
764 | return fullpath, foldercounter, filename, year, doy, set |
|
764 | return fullpath, foldercounter, filename, year, doy, set | |
765 |
|
765 | |||
766 | def __setNextFileOffline(self): |
|
766 | def __setNextFileOffline(self): | |
767 |
|
767 | |||
768 | idFile = self.fileIndex |
|
768 | idFile = self.fileIndex | |
769 |
|
769 | |||
770 | while (True): |
|
770 | while (True): | |
771 | idFile += 1 |
|
771 | idFile += 1 | |
772 | if not(idFile < len(self.filenameList)): |
|
772 | if not(idFile < len(self.filenameList)): | |
773 | self.flagNoMoreFiles = 1 |
|
773 | self.flagNoMoreFiles = 1 | |
774 | # print "[Reading] No more Files" |
|
774 | # print "[Reading] No more Files" | |
775 | return 0 |
|
775 | return 0 | |
776 |
|
776 | |||
777 | filename = self.filenameList[idFile] |
|
777 | filename = self.filenameList[idFile] | |
778 |
|
778 | |||
779 | if not(self.__verifyFile(filename)): |
|
779 | if not(self.__verifyFile(filename)): | |
780 | continue |
|
780 | continue | |
781 |
|
781 | |||
782 | fileSize = os.path.getsize(filename) |
|
782 | fileSize = os.path.getsize(filename) | |
783 | fp = open(filename, 'rb') |
|
783 | fp = open(filename, 'rb') | |
784 | break |
|
784 | break | |
785 |
|
785 | |||
786 | self.flagIsNewFile = 1 |
|
786 | self.flagIsNewFile = 1 | |
787 | self.fileIndex = idFile |
|
787 | self.fileIndex = idFile | |
788 | self.filename = filename |
|
788 | self.filename = filename | |
789 | self.fileSize = fileSize |
|
789 | self.fileSize = fileSize | |
790 | self.fp = fp |
|
790 | self.fp = fp | |
791 |
|
791 | |||
792 | # print "[Reading] Setting the file: %s"%self.filename |
|
792 | # print "[Reading] Setting the file: %s"%self.filename | |
793 |
|
793 | |||
794 | return 1 |
|
794 | return 1 | |
795 |
|
795 | |||
796 | def __setNextFileOnline(self): |
|
796 | def __setNextFileOnline(self): | |
797 | """ |
|
797 | """ | |
798 | Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si |
|
798 | Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si | |
799 | no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files |
|
799 | no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files | |
800 | siguientes. |
|
800 | siguientes. | |
801 |
|
801 | |||
802 | Affected: |
|
802 | Affected: | |
803 | self.flagIsNewFile |
|
803 | self.flagIsNewFile | |
804 | self.filename |
|
804 | self.filename | |
805 | self.fileSize |
|
805 | self.fileSize | |
806 | self.fp |
|
806 | self.fp | |
807 | self.set |
|
807 | self.set | |
808 | self.flagNoMoreFiles |
|
808 | self.flagNoMoreFiles | |
809 |
|
809 | |||
810 | Return: |
|
810 | Return: | |
811 | 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado |
|
811 | 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado | |
812 | 1 : si el file fue abierto con exito y esta listo a ser leido |
|
812 | 1 : si el file fue abierto con exito y esta listo a ser leido | |
813 |
|
813 | |||
814 | Excepciones: |
|
814 | Excepciones: | |
815 | Si un determinado file no puede ser abierto |
|
815 | Si un determinado file no puede ser abierto | |
816 | """ |
|
816 | """ | |
817 | nFiles = 0 |
|
817 | nFiles = 0 | |
818 | fileOk_flag = False |
|
818 | fileOk_flag = False | |
819 | firstTime_flag = True |
|
819 | firstTime_flag = True | |
820 |
|
820 | |||
821 | self.set += 1 |
|
821 | self.set += 1 | |
822 |
|
822 | |||
823 | if self.set > 999: |
|
823 | if self.set > 999: | |
824 | self.set = 0 |
|
824 | self.set = 0 | |
825 | self.foldercounter += 1 |
|
825 | self.foldercounter += 1 | |
826 |
|
826 | |||
827 | # busca el 1er file disponible |
|
827 | # busca el 1er file disponible | |
828 | fullfilename, filename = checkForRealPath( |
|
828 | fullfilename, filename = checkForRealPath( | |
829 | self.path, self.foldercounter, self.year, self.doy, self.set, self.ext) |
|
829 | self.path, self.foldercounter, self.year, self.doy, self.set, self.ext) | |
830 | if fullfilename: |
|
830 | if fullfilename: | |
831 | if self.__verifyFile(fullfilename, False): |
|
831 | if self.__verifyFile(fullfilename, False): | |
832 | fileOk_flag = True |
|
832 | fileOk_flag = True | |
833 |
|
833 | |||
834 | # si no encuentra un file entonces espera y vuelve a buscar |
|
834 | # si no encuentra un file entonces espera y vuelve a buscar | |
835 | if not(fileOk_flag): |
|
835 | if not(fileOk_flag): | |
836 | # busco en los siguientes self.nFiles+1 files posibles |
|
836 | # busco en los siguientes self.nFiles+1 files posibles | |
837 | for nFiles in range(self.nFiles + 1): |
|
837 | for nFiles in range(self.nFiles + 1): | |
838 |
|
838 | |||
839 | if firstTime_flag: # si es la 1era vez entonces hace el for self.nTries veces |
|
839 | if firstTime_flag: # si es la 1era vez entonces hace el for self.nTries veces | |
840 | tries = self.nTries |
|
840 | tries = self.nTries | |
841 | else: |
|
841 | else: | |
842 | tries = 1 # si no es la 1era vez entonces solo lo hace una vez |
|
842 | tries = 1 # si no es la 1era vez entonces solo lo hace una vez | |
843 |
|
843 | |||
844 | for nTries in range(tries): |
|
844 | for nTries in range(tries): | |
845 | if firstTime_flag: |
|
845 | if firstTime_flag: | |
846 | print("\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % (self.delay, filename, nTries + 1)) |
|
846 | print("\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % (self.delay, filename, nTries + 1)) | |
847 | sleep(self.delay) |
|
847 | sleep(self.delay) | |
848 | else: |
|
848 | else: | |
849 | print("\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)) |
|
849 | print("\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)) | |
850 |
|
850 | |||
851 | fullfilename, filename = checkForRealPath( |
|
851 | fullfilename, filename = checkForRealPath( | |
852 | self.path, self.foldercounter, self.year, self.doy, self.set, self.ext) |
|
852 | self.path, self.foldercounter, self.year, self.doy, self.set, self.ext) | |
853 | if fullfilename: |
|
853 | if fullfilename: | |
854 | if self.__verifyFile(fullfilename): |
|
854 | if self.__verifyFile(fullfilename): | |
855 | fileOk_flag = True |
|
855 | fileOk_flag = True | |
856 | break |
|
856 | break | |
857 |
|
857 | |||
858 | if fileOk_flag: |
|
858 | if fileOk_flag: | |
859 | break |
|
859 | break | |
860 |
|
860 | |||
861 | firstTime_flag = False |
|
861 | firstTime_flag = False | |
862 |
|
862 | |||
863 | log.warning('Skipping the file {} due to this file doesn\'t exist'.format(filename)) |
|
863 | log.warning('Skipping the file {} due to this file doesn\'t exist'.format(filename)) | |
864 | self.set += 1 |
|
864 | self.set += 1 | |
865 |
|
865 | |||
866 | # si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta |
|
866 | # si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta | |
867 | if nFiles == (self.nFiles - 1): |
|
867 | if nFiles == (self.nFiles - 1): | |
868 | self.set = 0 |
|
868 | self.set = 0 | |
869 | self.doy += 1 |
|
869 | self.doy += 1 | |
870 | self.foldercounter = 0 |
|
870 | self.foldercounter = 0 | |
871 |
|
871 | |||
872 | if fileOk_flag: |
|
872 | if fileOk_flag: | |
873 | self.fileSize = os.path.getsize(fullfilename) |
|
873 | self.fileSize = os.path.getsize(fullfilename) | |
874 | self.filename = fullfilename |
|
874 | self.filename = fullfilename | |
875 | self.flagIsNewFile = 1 |
|
875 | self.flagIsNewFile = 1 | |
876 | if self.fp != None: |
|
876 | if self.fp != None: | |
877 | self.fp.close() |
|
877 | self.fp.close() | |
878 | self.fp = open(fullfilename, 'rb') |
|
878 | self.fp = open(fullfilename, 'rb') | |
879 | self.flagNoMoreFiles = 0 |
|
879 | self.flagNoMoreFiles = 0 | |
880 | # print '[Reading] Setting the file: %s' % fullfilename |
|
880 | # print '[Reading] Setting the file: %s' % fullfilename | |
881 | else: |
|
881 | else: | |
882 | self.fileSize = 0 |
|
882 | self.fileSize = 0 | |
883 | self.filename = None |
|
883 | self.filename = None | |
884 | self.flagIsNewFile = 0 |
|
884 | self.flagIsNewFile = 0 | |
885 | self.fp = None |
|
885 | self.fp = None | |
886 | self.flagNoMoreFiles = 1 |
|
886 | self.flagNoMoreFiles = 1 | |
887 | # print '[Reading] No more files to read' |
|
887 | # print '[Reading] No more files to read' | |
888 |
|
888 | |||
889 | return fileOk_flag |
|
889 | return fileOk_flag | |
890 |
|
890 | |||
891 | def setNextFile(self): |
|
891 | def setNextFile(self): | |
892 | if self.fp != None: |
|
892 | if self.fp != None: | |
893 | self.fp.close() |
|
893 | self.fp.close() | |
894 |
|
894 | |||
895 | if self.online: |
|
895 | if self.online: | |
896 | newFile = self.__setNextFileOnline() |
|
896 | newFile = self.__setNextFileOnline() | |
897 | else: |
|
897 | else: | |
898 | newFile = self.__setNextFileOffline() |
|
898 | newFile = self.__setNextFileOffline() | |
899 |
|
899 | |||
900 | if not(newFile): |
|
900 | if not(newFile): | |
901 | self.dataOut.error = 'No more files to read' |
|
901 | self.dataOut.error = 'No more files to read' | |
902 | return 0 |
|
902 | return 0 | |
903 |
|
903 | |||
904 | if self.verbose: |
|
904 | if self.verbose: | |
905 | print('[Reading] Setting the file: %s' % self.filename) |
|
905 | print('[Reading] Setting the file: %s' % self.filename) | |
906 |
|
906 | |||
907 | self.__readFirstHeader() |
|
907 | self.__readFirstHeader() | |
908 | self.nReadBlocks = 0 |
|
908 | self.nReadBlocks = 0 | |
909 | return 1 |
|
909 | return 1 | |
910 |
|
910 | |||
911 | def __waitNewBlock(self): |
|
911 | def __waitNewBlock(self): | |
912 | """ |
|
912 | """ | |
913 | Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma. |
|
913 | Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma. | |
914 |
|
914 | |||
915 | Si el modo de lectura es OffLine siempre retorn 0 |
|
915 | Si el modo de lectura es OffLine siempre retorn 0 | |
916 | """ |
|
916 | """ | |
917 | if not self.online: |
|
917 | if not self.online: | |
918 | return 0 |
|
918 | return 0 | |
919 |
|
919 | |||
920 | if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile): |
|
920 | if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile): | |
921 | return 0 |
|
921 | return 0 | |
922 |
|
922 | |||
923 | currentPointer = self.fp.tell() |
|
923 | currentPointer = self.fp.tell() | |
924 |
|
924 | |||
925 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
925 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize | |
926 |
|
926 | |||
927 | for nTries in range(self.nTries): |
|
927 | for nTries in range(self.nTries): | |
928 |
|
928 | |||
929 | self.fp.close() |
|
929 | self.fp.close() | |
930 | self.fp = open(self.filename, 'rb') |
|
930 | self.fp = open(self.filename, 'rb') | |
931 | self.fp.seek(currentPointer) |
|
931 | self.fp.seek(currentPointer) | |
932 |
|
932 | |||
933 | self.fileSize = os.path.getsize(self.filename) |
|
933 | self.fileSize = os.path.getsize(self.filename) | |
934 | currentSize = self.fileSize - currentPointer |
|
934 | currentSize = self.fileSize - currentPointer | |
935 |
|
935 | |||
936 | if (currentSize >= neededSize): |
|
936 | if (currentSize >= neededSize): | |
937 | self.basicHeaderObj.read(self.fp) |
|
937 | self.basicHeaderObj.read(self.fp) | |
938 | return 1 |
|
938 | return 1 | |
939 |
|
939 | |||
940 | if self.fileSize == self.fileSizeByHeader: |
|
940 | if self.fileSize == self.fileSizeByHeader: | |
941 | # self.flagEoF = True |
|
941 | # self.flagEoF = True | |
942 | return 0 |
|
942 | return 0 | |
943 |
|
943 | |||
944 | print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1)) |
|
944 | print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1)) | |
945 | sleep(self.delay) |
|
945 | sleep(self.delay) | |
946 |
|
946 | |||
947 | return 0 |
|
947 | return 0 | |
948 |
|
948 | |||
949 | def waitDataBlock(self, pointer_location, blocksize=None): |
|
949 | def waitDataBlock(self, pointer_location, blocksize=None): | |
950 |
|
950 | |||
951 | currentPointer = pointer_location |
|
951 | currentPointer = pointer_location | |
952 | if blocksize is None: |
|
952 | if blocksize is None: | |
953 | neededSize = self.processingHeaderObj.blockSize # + self.basicHeaderSize |
|
953 | neededSize = self.processingHeaderObj.blockSize # + self.basicHeaderSize | |
954 | else: |
|
954 | else: | |
955 | neededSize = blocksize |
|
955 | neededSize = blocksize | |
956 |
|
956 | |||
957 | for nTries in range(self.nTries): |
|
957 | for nTries in range(self.nTries): | |
958 | self.fp.close() |
|
958 | self.fp.close() | |
959 | self.fp = open(self.filename, 'rb') |
|
959 | self.fp = open(self.filename, 'rb') | |
960 | self.fp.seek(currentPointer) |
|
960 | self.fp.seek(currentPointer) | |
961 |
|
961 | |||
962 | self.fileSize = os.path.getsize(self.filename) |
|
962 | self.fileSize = os.path.getsize(self.filename) | |
963 | currentSize = self.fileSize - currentPointer |
|
963 | currentSize = self.fileSize - currentPointer | |
964 |
|
964 | |||
965 | if (currentSize >= neededSize): |
|
965 | if (currentSize >= neededSize): | |
966 | return 1 |
|
966 | return 1 | |
967 |
|
967 | |||
968 | log.warning( |
|
968 | log.warning( | |
969 | "Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1), |
|
969 | "Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1), | |
970 | self.name |
|
970 | self.name | |
971 | ) |
|
971 | ) | |
972 | sleep(self.delay) |
|
972 | sleep(self.delay) | |
973 |
|
973 | |||
974 | return 0 |
|
974 | return 0 | |
975 |
|
975 | |||
976 | def __jumpToLastBlock(self): |
|
976 | def __jumpToLastBlock(self): | |
977 |
|
977 | |||
978 | if not(self.__isFirstTimeOnline): |
|
978 | if not(self.__isFirstTimeOnline): | |
979 | return |
|
979 | return | |
980 |
|
980 | |||
981 | csize = self.fileSize - self.fp.tell() |
|
981 | csize = self.fileSize - self.fp.tell() | |
982 | blocksize = self.processingHeaderObj.blockSize |
|
982 | blocksize = self.processingHeaderObj.blockSize | |
983 |
|
983 | |||
984 | # salta el primer bloque de datos |
|
984 | # salta el primer bloque de datos | |
985 | if csize > self.processingHeaderObj.blockSize: |
|
985 | if csize > self.processingHeaderObj.blockSize: | |
986 | self.fp.seek(self.fp.tell() + blocksize) |
|
986 | self.fp.seek(self.fp.tell() + blocksize) | |
987 | else: |
|
987 | else: | |
988 | return |
|
988 | return | |
989 |
|
989 | |||
990 | csize = self.fileSize - self.fp.tell() |
|
990 | csize = self.fileSize - self.fp.tell() | |
991 | neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
991 | neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize | |
992 | while True: |
|
992 | while True: | |
993 |
|
993 | |||
994 | if self.fp.tell() < self.fileSize: |
|
994 | if self.fp.tell() < self.fileSize: | |
995 | self.fp.seek(self.fp.tell() + neededsize) |
|
995 | self.fp.seek(self.fp.tell() + neededsize) | |
996 | else: |
|
996 | else: | |
997 | self.fp.seek(self.fp.tell() - neededsize) |
|
997 | self.fp.seek(self.fp.tell() - neededsize) | |
998 | break |
|
998 | break | |
999 |
|
999 | |||
1000 | # csize = self.fileSize - self.fp.tell() |
|
1000 | # csize = self.fileSize - self.fp.tell() | |
1001 | # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
1001 | # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize | |
1002 | # factor = int(csize/neededsize) |
|
1002 | # factor = int(csize/neededsize) | |
1003 | # if factor > 0: |
|
1003 | # if factor > 0: | |
1004 | # self.fp.seek(self.fp.tell() + factor*neededsize) |
|
1004 | # self.fp.seek(self.fp.tell() + factor*neededsize) | |
1005 |
|
1005 | |||
1006 | self.flagIsNewFile = 0 |
|
1006 | self.flagIsNewFile = 0 | |
1007 | self.__isFirstTimeOnline = 0 |
|
1007 | self.__isFirstTimeOnline = 0 | |
1008 |
|
1008 | |||
1009 | def __setNewBlock(self): |
|
1009 | def __setNewBlock(self): | |
1010 | # if self.server is None: |
|
1010 | # if self.server is None: | |
1011 | if self.fp == None: |
|
1011 | if self.fp == None: | |
1012 | return 0 |
|
1012 | return 0 | |
1013 |
|
1013 | |||
1014 | # if self.online: |
|
1014 | # if self.online: | |
1015 | # self.__jumpToLastBlock() |
|
1015 | # self.__jumpToLastBlock() | |
1016 |
|
1016 | |||
1017 | if self.flagIsNewFile: |
|
1017 | if self.flagIsNewFile: | |
1018 | self.lastUTTime = self.basicHeaderObj.utc |
|
1018 | self.lastUTTime = self.basicHeaderObj.utc | |
1019 | return 1 |
|
1019 | return 1 | |
1020 |
|
1020 | |||
1021 | if self.realtime: |
|
1021 | if self.realtime: | |
1022 | self.flagDiscontinuousBlock = 1 |
|
1022 | self.flagDiscontinuousBlock = 1 | |
1023 | if not(self.setNextFile()): |
|
1023 | if not(self.setNextFile()): | |
1024 | return 0 |
|
1024 | return 0 | |
1025 | else: |
|
1025 | else: | |
1026 | return 1 |
|
1026 | return 1 | |
1027 | # if self.server is None: |
|
1027 | # if self.server is None: | |
1028 | currentSize = self.fileSize - self.fp.tell() |
|
1028 | currentSize = self.fileSize - self.fp.tell() | |
1029 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
1029 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize | |
1030 | if (currentSize >= neededSize): |
|
1030 | if (currentSize >= neededSize): | |
1031 | self.basicHeaderObj.read(self.fp) |
|
1031 | self.basicHeaderObj.read(self.fp) | |
1032 | self.lastUTTime = self.basicHeaderObj.utc |
|
1032 | self.lastUTTime = self.basicHeaderObj.utc | |
1033 | return 1 |
|
1033 | return 1 | |
1034 | # else: |
|
1034 | # else: | |
1035 | # self.basicHeaderObj.read(self.zHeader) |
|
1035 | # self.basicHeaderObj.read(self.zHeader) | |
1036 | # self.lastUTTime = self.basicHeaderObj.utc |
|
1036 | # self.lastUTTime = self.basicHeaderObj.utc | |
1037 | # return 1 |
|
1037 | # return 1 | |
1038 | if self.__waitNewBlock(): |
|
1038 | if self.__waitNewBlock(): | |
1039 | self.lastUTTime = self.basicHeaderObj.utc |
|
1039 | self.lastUTTime = self.basicHeaderObj.utc | |
1040 | return 1 |
|
1040 | return 1 | |
1041 | # if self.server is None: |
|
1041 | # if self.server is None: | |
1042 | if not(self.setNextFile()): |
|
1042 | if not(self.setNextFile()): | |
1043 | return 0 |
|
1043 | return 0 | |
1044 |
|
1044 | |||
1045 | deltaTime = self.basicHeaderObj.utc - self.lastUTTime |
|
1045 | deltaTime = self.basicHeaderObj.utc - self.lastUTTime | |
1046 | self.lastUTTime = self.basicHeaderObj.utc |
|
1046 | self.lastUTTime = self.basicHeaderObj.utc | |
1047 |
|
1047 | |||
1048 | self.flagDiscontinuousBlock = 0 |
|
1048 | self.flagDiscontinuousBlock = 0 | |
1049 |
|
1049 | |||
1050 | if deltaTime > self.maxTimeStep: |
|
1050 | if deltaTime > self.maxTimeStep: | |
1051 | self.flagDiscontinuousBlock = 1 |
|
1051 | self.flagDiscontinuousBlock = 1 | |
1052 |
|
1052 | |||
1053 | return 1 |
|
1053 | return 1 | |
1054 |
|
1054 | |||
1055 | def readNextBlock(self): |
|
1055 | def readNextBlock(self): | |
1056 |
|
1056 | |||
1057 | # Skip block out of startTime and endTime |
|
1057 | # Skip block out of startTime and endTime | |
1058 | while True: |
|
1058 | while True: | |
1059 | if not(self.__setNewBlock()): |
|
1059 | if not(self.__setNewBlock()): | |
1060 | self.dataOut.error = 'No more files to read' |
|
1060 | self.dataOut.error = 'No more files to read' | |
1061 | return 0 |
|
1061 | return 0 | |
1062 |
|
1062 | |||
1063 | if not(self.readBlock()): |
|
1063 | if not(self.readBlock()): | |
1064 | return 0 |
|
1064 | return 0 | |
1065 |
|
1065 | |||
1066 | self.getBasicHeader() |
|
1066 | self.getBasicHeader() | |
1067 | if (self.dataOut.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or (self.dataOut.datatime > datetime.datetime.combine(self.endDate, self.endTime)): |
|
1067 | if (self.dataOut.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or (self.dataOut.datatime > datetime.datetime.combine(self.endDate, self.endTime)): | |
1068 | print("[Reading] Block No. %d/%d -> %s [Skipping]" % (self.nReadBlocks, |
|
1068 | print("[Reading] Block No. %d/%d -> %s [Skipping]" % (self.nReadBlocks, | |
1069 | self.processingHeaderObj.dataBlocksPerFile, |
|
1069 | self.processingHeaderObj.dataBlocksPerFile, | |
1070 | self.dataOut.datatime.ctime())) |
|
1070 | self.dataOut.datatime.ctime())) | |
1071 | continue |
|
1071 | continue | |
1072 |
|
1072 | |||
1073 | break |
|
1073 | break | |
1074 |
|
1074 | |||
1075 | if self.verbose: |
|
1075 | if self.verbose: | |
1076 | print("[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks, |
|
1076 | print("[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks, | |
1077 | self.processingHeaderObj.dataBlocksPerFile, |
|
1077 | self.processingHeaderObj.dataBlocksPerFile, | |
1078 | self.dataOut.datatime.ctime())) |
|
1078 | self.dataOut.datatime.ctime())) | |
1079 | return 1 |
|
1079 | return 1 | |
1080 |
|
1080 | |||
1081 | def __readFirstHeader(self): |
|
1081 | def __readFirstHeader(self): | |
1082 |
|
1082 | |||
1083 | self.basicHeaderObj.read(self.fp) |
|
1083 | self.basicHeaderObj.read(self.fp) | |
1084 | self.systemHeaderObj.read(self.fp) |
|
1084 | self.systemHeaderObj.read(self.fp) | |
1085 | self.radarControllerHeaderObj.read(self.fp) |
|
1085 | self.radarControllerHeaderObj.read(self.fp) | |
1086 | self.processingHeaderObj.read(self.fp) |
|
1086 | self.processingHeaderObj.read(self.fp) | |
1087 |
|
1087 | |||
1088 | self.firstHeaderSize = self.basicHeaderObj.size |
|
1088 | self.firstHeaderSize = self.basicHeaderObj.size | |
1089 |
|
1089 | |||
1090 | datatype = int(numpy.log2((self.processingHeaderObj.processFlags & |
|
1090 | datatype = int(numpy.log2((self.processingHeaderObj.processFlags & | |
1091 | PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR)) |
|
1091 | PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR)) | |
1092 | if datatype == 0: |
|
1092 | if datatype == 0: | |
1093 | datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')]) |
|
1093 | datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')]) | |
1094 | elif datatype == 1: |
|
1094 | elif datatype == 1: | |
1095 | datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')]) |
|
1095 | datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')]) | |
1096 | elif datatype == 2: |
|
1096 | elif datatype == 2: | |
1097 | datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')]) |
|
1097 | datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')]) | |
1098 | elif datatype == 3: |
|
1098 | elif datatype == 3: | |
1099 | datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')]) |
|
1099 | datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')]) | |
1100 | elif datatype == 4: |
|
1100 | elif datatype == 4: | |
1101 | datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')]) |
|
1101 | datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')]) | |
1102 | elif datatype == 5: |
|
1102 | elif datatype == 5: | |
1103 | datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')]) |
|
1103 | datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')]) | |
1104 | else: |
|
1104 | else: | |
1105 | raise ValueError('Data type was not defined') |
|
1105 | raise ValueError('Data type was not defined') | |
1106 |
|
1106 | |||
1107 | self.dtype = datatype_str |
|
1107 | self.dtype = datatype_str | |
1108 | #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c |
|
1108 | #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c | |
1109 | self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \ |
|
1109 | self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \ | |
1110 | self.firstHeaderSize + self.basicHeaderSize * \ |
|
1110 | self.firstHeaderSize + self.basicHeaderSize * \ | |
1111 | (self.processingHeaderObj.dataBlocksPerFile - 1) |
|
1111 | (self.processingHeaderObj.dataBlocksPerFile - 1) | |
1112 | # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels) |
|
1112 | # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels) | |
1113 | # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels) |
|
1113 | # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels) | |
1114 | self.getBlockDimension() |
|
1114 | self.getBlockDimension() | |
1115 |
|
1115 | |||
1116 | def __verifyFile(self, filename, msgFlag=True): |
|
1116 | def __verifyFile(self, filename, msgFlag=True): | |
1117 |
|
1117 | |||
1118 | msg = None |
|
1118 | msg = None | |
1119 |
|
1119 | |||
1120 | try: |
|
1120 | try: | |
1121 | fp = open(filename, 'rb') |
|
1121 | fp = open(filename, 'rb') | |
1122 | except IOError: |
|
1122 | except IOError: | |
1123 |
|
1123 | |||
1124 | if msgFlag: |
|
1124 | if msgFlag: | |
1125 | print("[Reading] File %s can't be opened" % (filename)) |
|
1125 | print("[Reading] File %s can't be opened" % (filename)) | |
1126 |
|
1126 | |||
1127 | return False |
|
1127 | return False | |
1128 |
|
1128 | |||
1129 | currentPosition = fp.tell() |
|
1129 | currentPosition = fp.tell() | |
1130 | neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize |
|
1130 | neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize | |
1131 |
|
1131 | |||
1132 | if neededSize == 0: |
|
1132 | if neededSize == 0: | |
1133 | basicHeaderObj = BasicHeader(LOCALTIME) |
|
1133 | basicHeaderObj = BasicHeader(LOCALTIME) | |
1134 | systemHeaderObj = SystemHeader() |
|
1134 | systemHeaderObj = SystemHeader() | |
1135 | radarControllerHeaderObj = RadarControllerHeader() |
|
1135 | radarControllerHeaderObj = RadarControllerHeader() | |
1136 | processingHeaderObj = ProcessingHeader() |
|
1136 | processingHeaderObj = ProcessingHeader() | |
1137 |
|
1137 | |||
1138 | if not(basicHeaderObj.read(fp)): |
|
1138 | if not(basicHeaderObj.read(fp)): | |
1139 | fp.close() |
|
1139 | fp.close() | |
1140 | return False |
|
1140 | return False | |
1141 |
|
1141 | |||
1142 | if not(systemHeaderObj.read(fp)): |
|
1142 | if not(systemHeaderObj.read(fp)): | |
1143 | fp.close() |
|
1143 | fp.close() | |
1144 | return False |
|
1144 | return False | |
1145 |
|
1145 | |||
1146 | if not(radarControllerHeaderObj.read(fp)): |
|
1146 | if not(radarControllerHeaderObj.read(fp)): | |
1147 | fp.close() |
|
1147 | fp.close() | |
1148 | return False |
|
1148 | return False | |
1149 |
|
1149 | |||
1150 | if not(processingHeaderObj.read(fp)): |
|
1150 | if not(processingHeaderObj.read(fp)): | |
1151 | fp.close() |
|
1151 | fp.close() | |
1152 | return False |
|
1152 | return False | |
1153 |
|
1153 | |||
1154 | neededSize = processingHeaderObj.blockSize + basicHeaderObj.size |
|
1154 | neededSize = processingHeaderObj.blockSize + basicHeaderObj.size | |
1155 | else: |
|
1155 | else: | |
1156 | msg = "[Reading] Skipping the file %s due to it hasn't enough data" % filename |
|
1156 | msg = "[Reading] Skipping the file %s due to it hasn't enough data" % filename | |
1157 |
|
1157 | |||
1158 | fp.close() |
|
1158 | fp.close() | |
1159 |
|
1159 | |||
1160 | fileSize = os.path.getsize(filename) |
|
1160 | fileSize = os.path.getsize(filename) | |
1161 | currentSize = fileSize - currentPosition |
|
1161 | currentSize = fileSize - currentPosition | |
1162 |
|
1162 | |||
1163 | if currentSize < neededSize: |
|
1163 | if currentSize < neededSize: | |
1164 | if msgFlag and (msg != None): |
|
1164 | if msgFlag and (msg != None): | |
1165 | print(msg) |
|
1165 | print(msg) | |
1166 | return False |
|
1166 | return False | |
1167 |
|
1167 | |||
1168 | return True |
|
1168 | return True | |
1169 |
|
1169 | |||
1170 | def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False): |
|
1170 | def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False): | |
1171 |
|
1171 | |||
1172 | path_empty = True |
|
1172 | path_empty = True | |
1173 |
|
1173 | |||
1174 | dateList = [] |
|
1174 | dateList = [] | |
1175 | pathList = [] |
|
1175 | pathList = [] | |
1176 |
|
1176 | |||
1177 | multi_path = path.split(',') |
|
1177 | multi_path = path.split(',') | |
1178 |
|
1178 | |||
1179 | if not walk: |
|
1179 | if not walk: | |
1180 |
|
1180 | |||
1181 | for single_path in multi_path: |
|
1181 | for single_path in multi_path: | |
1182 |
|
1182 | |||
1183 | if not os.path.isdir(single_path): |
|
1183 | if not os.path.isdir(single_path): | |
1184 | continue |
|
1184 | continue | |
1185 |
|
1185 | |||
1186 | fileList = glob.glob1(single_path, "*" + ext) |
|
1186 | fileList = glob.glob1(single_path, "*" + ext) | |
1187 |
|
1187 | |||
1188 | if not fileList: |
|
1188 | if not fileList: | |
1189 | continue |
|
1189 | continue | |
1190 |
|
1190 | |||
1191 | path_empty = False |
|
1191 | path_empty = False | |
1192 |
|
1192 | |||
1193 | fileList.sort() |
|
1193 | fileList.sort() | |
1194 |
|
1194 | |||
1195 | for thisFile in fileList: |
|
1195 | for thisFile in fileList: | |
1196 |
|
1196 | |||
1197 | if not os.path.isfile(os.path.join(single_path, thisFile)): |
|
1197 | if not os.path.isfile(os.path.join(single_path, thisFile)): | |
1198 | continue |
|
1198 | continue | |
1199 |
|
1199 | |||
1200 | if not isRadarFile(thisFile): |
|
1200 | if not isRadarFile(thisFile): | |
1201 | continue |
|
1201 | continue | |
1202 |
|
1202 | |||
1203 | if not isFileInDateRange(thisFile, startDate, endDate): |
|
1203 | if not isFileInDateRange(thisFile, startDate, endDate): | |
1204 | continue |
|
1204 | continue | |
1205 |
|
1205 | |||
1206 | thisDate = getDateFromRadarFile(thisFile) |
|
1206 | thisDate = getDateFromRadarFile(thisFile) | |
1207 |
|
1207 | |||
1208 | if thisDate in dateList or single_path in pathList: |
|
1208 | if thisDate in dateList or single_path in pathList: | |
1209 | continue |
|
1209 | continue | |
1210 |
|
1210 | |||
1211 | dateList.append(thisDate) |
|
1211 | dateList.append(thisDate) | |
1212 | pathList.append(single_path) |
|
1212 | pathList.append(single_path) | |
1213 |
|
1213 | |||
1214 | else: |
|
1214 | else: | |
1215 | for single_path in multi_path: |
|
1215 | for single_path in multi_path: | |
1216 |
|
1216 | |||
1217 | if not os.path.isdir(single_path): |
|
1217 | if not os.path.isdir(single_path): | |
1218 | continue |
|
1218 | continue | |
1219 |
|
1219 | |||
1220 | dirList = [] |
|
1220 | dirList = [] | |
1221 |
|
1221 | |||
1222 | for thisPath in os.listdir(single_path): |
|
1222 | for thisPath in os.listdir(single_path): | |
1223 |
|
1223 | |||
1224 | if not os.path.isdir(os.path.join(single_path, thisPath)): |
|
1224 | if not os.path.isdir(os.path.join(single_path, thisPath)): | |
1225 | continue |
|
1225 | continue | |
1226 |
|
1226 | |||
1227 | if not isRadarFolder(thisPath): |
|
1227 | if not isRadarFolder(thisPath): | |
1228 | continue |
|
1228 | continue | |
1229 |
|
1229 | |||
1230 | if not isFolderInDateRange(thisPath, startDate, endDate): |
|
1230 | if not isFolderInDateRange(thisPath, startDate, endDate): | |
1231 | continue |
|
1231 | continue | |
1232 |
|
1232 | |||
1233 | dirList.append(thisPath) |
|
1233 | dirList.append(thisPath) | |
1234 |
|
1234 | |||
1235 | if not dirList: |
|
1235 | if not dirList: | |
1236 | continue |
|
1236 | continue | |
1237 |
|
1237 | |||
1238 | dirList.sort() |
|
1238 | dirList.sort() | |
1239 |
|
1239 | |||
1240 | for thisDir in dirList: |
|
1240 | for thisDir in dirList: | |
1241 |
|
1241 | |||
1242 | datapath = os.path.join(single_path, thisDir, expLabel) |
|
1242 | datapath = os.path.join(single_path, thisDir, expLabel) | |
1243 | fileList = glob.glob1(datapath, "*" + ext) |
|
1243 | fileList = glob.glob1(datapath, "*" + ext) | |
1244 |
|
1244 | |||
1245 | if not fileList: |
|
1245 | if not fileList: | |
1246 | continue |
|
1246 | continue | |
1247 |
|
1247 | |||
1248 | path_empty = False |
|
1248 | path_empty = False | |
1249 |
|
1249 | |||
1250 | thisDate = getDateFromRadarFolder(thisDir) |
|
1250 | thisDate = getDateFromRadarFolder(thisDir) | |
1251 |
|
1251 | |||
1252 | pathList.append(datapath) |
|
1252 | pathList.append(datapath) | |
1253 | dateList.append(thisDate) |
|
1253 | dateList.append(thisDate) | |
1254 |
|
1254 | |||
1255 | dateList.sort() |
|
1255 | dateList.sort() | |
1256 |
|
1256 | |||
1257 | if walk: |
|
1257 | if walk: | |
1258 | pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel) |
|
1258 | pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel) | |
1259 | else: |
|
1259 | else: | |
1260 | pattern_path = multi_path[0] |
|
1260 | pattern_path = multi_path[0] | |
1261 |
|
1261 | |||
1262 | if path_empty: |
|
1262 | if path_empty: | |
1263 | print("[Reading] No *%s files in %s for %s to %s" % (ext, pattern_path, startDate, endDate)) |
|
1263 | print("[Reading] No *%s files in %s for %s to %s" % (ext, pattern_path, startDate, endDate)) | |
1264 | else: |
|
1264 | else: | |
1265 | if not dateList: |
|
1265 | if not dateList: | |
1266 | print("[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" % (startDate, endDate, ext, path)) |
|
1266 | print("[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" % (startDate, endDate, ext, path)) | |
1267 |
|
1267 | |||
1268 | if include_path: |
|
1268 | if include_path: | |
1269 | return dateList, pathList |
|
1269 | return dateList, pathList | |
1270 |
|
1270 | |||
1271 | return dateList |
|
1271 | return dateList | |
1272 |
|
1272 | |||
1273 | def setup(self, |
|
1273 | def setup(self, | |
1274 | path=None, |
|
1274 | path=None, | |
1275 | startDate=None, |
|
1275 | startDate=None, | |
1276 | endDate=None, |
|
1276 | endDate=None, | |
1277 | startTime=datetime.time(0, 0, 0), |
|
1277 | startTime=datetime.time(0, 0, 0), | |
1278 | endTime=datetime.time(23, 59, 59), |
|
1278 | endTime=datetime.time(23, 59, 59), | |
1279 | set=None, |
|
1279 | set=None, | |
1280 | expLabel="", |
|
1280 | expLabel="", | |
1281 | ext=None, |
|
1281 | ext=None, | |
1282 | online=False, |
|
1282 | online=False, | |
1283 | delay=60, |
|
1283 | delay=60, | |
1284 | walk=True, |
|
1284 | walk=True, | |
1285 | getblock=False, |
|
1285 | getblock=False, | |
1286 | nTxs=1, |
|
1286 | nTxs=1, | |
1287 | realtime=False, |
|
1287 | realtime=False, | |
1288 | blocksize=None, |
|
1288 | blocksize=None, | |
1289 | blocktime=None, |
|
1289 | blocktime=None, | |
1290 | skip=None, |
|
1290 | skip=None, | |
1291 | cursor=None, |
|
1291 | cursor=None, | |
1292 | warnings=True, |
|
1292 | warnings=True, | |
1293 | verbose=True, |
|
1293 | verbose=True, | |
1294 | server=None, |
|
1294 | server=None, | |
1295 | format=None, |
|
1295 | format=None, | |
1296 | oneDDict=None, |
|
1296 | oneDDict=None, | |
1297 | twoDDict=None, |
|
1297 | twoDDict=None, | |
1298 | independentParam=None): |
|
1298 | independentParam=None): | |
1299 | if server is not None: |
|
1299 | if server is not None: | |
1300 | if 'tcp://' in server: |
|
1300 | if 'tcp://' in server: | |
1301 | address = server |
|
1301 | address = server | |
1302 | else: |
|
1302 | else: | |
1303 | address = 'ipc:///tmp/%s' % server |
|
1303 | address = 'ipc:///tmp/%s' % server | |
1304 | self.server = address |
|
1304 | self.server = address | |
1305 | self.context = zmq.Context() |
|
1305 | self.context = zmq.Context() | |
1306 | self.receiver = self.context.socket(zmq.PULL) |
|
1306 | self.receiver = self.context.socket(zmq.PULL) | |
1307 | self.receiver.connect(self.server) |
|
1307 | self.receiver.connect(self.server) | |
1308 | time.sleep(0.5) |
|
1308 | time.sleep(0.5) | |
1309 | print('[Starting] ReceiverData from {}'.format(self.server)) |
|
1309 | print('[Starting] ReceiverData from {}'.format(self.server)) | |
1310 | else: |
|
1310 | else: | |
1311 | self.server = None |
|
1311 | self.server = None | |
1312 | if path == None: |
|
1312 | if path == None: | |
1313 | raise ValueError("[Reading] The path is not valid") |
|
1313 | raise ValueError("[Reading] The path is not valid") | |
1314 |
|
1314 | |||
1315 | if ext == None: |
|
1315 | if ext == None: | |
1316 | ext = self.ext |
|
1316 | ext = self.ext | |
1317 |
|
1317 | |||
1318 | if online: |
|
1318 | if online: | |
1319 | print("[Reading] Searching files in online mode...") |
|
1319 | print("[Reading] Searching files in online mode...") | |
1320 |
|
1320 | |||
1321 | for nTries in range(self.nTries): |
|
1321 | for nTries in range(self.nTries): | |
1322 | fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine( |
|
1322 | fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine( | |
1323 | path=path, expLabel=expLabel, ext=ext, walk=walk, set=set) |
|
1323 | path=path, expLabel=expLabel, ext=ext, walk=walk, set=set) | |
1324 |
|
1324 | |||
1325 | if fullpath: |
|
1325 | if fullpath: | |
1326 | break |
|
1326 | break | |
1327 |
|
1327 | |||
1328 | print('[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries + 1)) |
|
1328 | print('[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries + 1)) | |
1329 | sleep(self.delay) |
|
1329 | sleep(self.delay) | |
1330 |
|
1330 | |||
1331 | if not(fullpath): |
|
1331 | if not(fullpath): | |
1332 | self.dataOut.error = 'There isn\'t any valid file in {}'.format(path) |
|
1332 | self.dataOut.error = 'There isn\'t any valid file in {}'.format(path) | |
1333 | return |
|
1333 | return | |
1334 |
|
1334 | |||
1335 | self.year = year |
|
1335 | self.year = year | |
1336 | self.doy = doy |
|
1336 | self.doy = doy | |
1337 | self.set = set - 1 |
|
1337 | self.set = set - 1 | |
1338 | self.path = path |
|
1338 | self.path = path | |
1339 | self.foldercounter = foldercounter |
|
1339 | self.foldercounter = foldercounter | |
1340 | last_set = None |
|
1340 | last_set = None | |
1341 | else: |
|
1341 | else: | |
1342 | print("[Reading] Searching files in offline mode ...") |
|
1342 | print("[Reading] Searching files in offline mode ...") | |
1343 | pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate, |
|
1343 | pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate, | |
1344 | startTime=startTime, endTime=endTime, |
|
1344 | startTime=startTime, endTime=endTime, | |
1345 | set=set, expLabel=expLabel, ext=ext, |
|
1345 | set=set, expLabel=expLabel, ext=ext, | |
1346 | walk=walk, cursor=cursor, |
|
1346 | walk=walk, cursor=cursor, | |
1347 | skip=skip) |
|
1347 | skip=skip) | |
1348 |
|
1348 | |||
1349 | if not(pathList): |
|
1349 | if not(pathList): | |
1350 | self.fileIndex = -1 |
|
1350 | self.fileIndex = -1 | |
1351 | self.pathList = [] |
|
1351 | self.pathList = [] | |
1352 | self.filenameList = [] |
|
1352 | self.filenameList = [] | |
1353 | return |
|
1353 | return | |
1354 |
|
1354 | |||
1355 | self.fileIndex = -1 |
|
1355 | self.fileIndex = -1 | |
1356 | self.pathList = pathList |
|
1356 | self.pathList = pathList | |
1357 | self.filenameList = filenameList |
|
1357 | self.filenameList = filenameList | |
1358 | file_name = os.path.basename(filenameList[-1]) |
|
1358 | file_name = os.path.basename(filenameList[-1]) | |
1359 | basename, ext = os.path.splitext(file_name) |
|
1359 | basename, ext = os.path.splitext(file_name) | |
1360 | last_set = int(basename[-3:]) |
|
1360 | last_set = int(basename[-3:]) | |
1361 |
|
1361 | |||
1362 | self.online = online |
|
1362 | self.online = online | |
1363 | self.realtime = realtime |
|
1363 | self.realtime = realtime | |
1364 | self.delay = delay |
|
1364 | self.delay = delay | |
1365 | ext = ext.lower() |
|
1365 | ext = ext.lower() | |
1366 | self.ext = ext |
|
1366 | self.ext = ext | |
1367 | self.getByBlock = getblock |
|
1367 | self.getByBlock = getblock | |
1368 | self.nTxs = nTxs |
|
1368 | self.nTxs = nTxs | |
1369 | self.startTime = startTime |
|
1369 | self.startTime = startTime | |
1370 | self.endTime = endTime |
|
1370 | self.endTime = endTime | |
1371 | self.endDate = endDate |
|
1371 | self.endDate = endDate | |
1372 | self.startDate = startDate |
|
1372 | self.startDate = startDate | |
1373 | # Added----------------- |
|
1373 | # Added----------------- | |
1374 | self.selBlocksize = blocksize |
|
1374 | self.selBlocksize = blocksize | |
1375 | self.selBlocktime = blocktime |
|
1375 | self.selBlocktime = blocktime | |
1376 |
|
1376 | |||
1377 | # Verbose----------- |
|
1377 | # Verbose----------- | |
1378 | self.verbose = verbose |
|
1378 | self.verbose = verbose | |
1379 | self.warnings = warnings |
|
1379 | self.warnings = warnings | |
1380 |
|
1380 | |||
1381 | if not(self.setNextFile()): |
|
1381 | if not(self.setNextFile()): | |
1382 | if (startDate != None) and (endDate != None): |
|
1382 | if (startDate != None) and (endDate != None): | |
1383 | print("[Reading] No files in range: %s - %s" % (datetime.datetime.combine(startDate, startTime).ctime(), datetime.datetime.combine(endDate, endTime).ctime())) |
|
1383 | print("[Reading] No files in range: %s - %s" % (datetime.datetime.combine(startDate, startTime).ctime(), datetime.datetime.combine(endDate, endTime).ctime())) | |
1384 | elif startDate != None: |
|
1384 | elif startDate != None: | |
1385 | print("[Reading] No files in range: %s" % (datetime.datetime.combine(startDate, startTime).ctime())) |
|
1385 | print("[Reading] No files in range: %s" % (datetime.datetime.combine(startDate, startTime).ctime())) | |
1386 | else: |
|
1386 | else: | |
1387 | print("[Reading] No files") |
|
1387 | print("[Reading] No files") | |
1388 |
|
1388 | |||
1389 | self.fileIndex = -1 |
|
1389 | self.fileIndex = -1 | |
1390 | self.pathList = [] |
|
1390 | self.pathList = [] | |
1391 | self.filenameList = [] |
|
1391 | self.filenameList = [] | |
1392 | return |
|
1392 | return | |
1393 |
|
1393 | |||
1394 | # self.getBasicHeader() |
|
1394 | # self.getBasicHeader() | |
1395 |
|
1395 | |||
1396 | if last_set != None: |
|
1396 | if last_set != None: | |
1397 | self.dataOut.last_block = last_set * \ |
|
1397 | self.dataOut.last_block = last_set * \ | |
1398 | self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock |
|
1398 | self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock | |
1399 | return |
|
1399 | return | |
1400 |
|
1400 | |||
1401 | def getBasicHeader(self): |
|
1401 | def getBasicHeader(self): | |
1402 |
|
1402 | |||
1403 | self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond / \ |
|
1403 | self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond / \ | |
1404 | 1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds |
|
1404 | 1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds | |
1405 |
|
1405 | |||
1406 | self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock |
|
1406 | self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock | |
1407 |
|
1407 | |||
1408 | self.dataOut.timeZone = self.basicHeaderObj.timeZone |
|
1408 | self.dataOut.timeZone = self.basicHeaderObj.timeZone | |
1409 |
|
1409 | |||
1410 | self.dataOut.dstFlag = self.basicHeaderObj.dstFlag |
|
1410 | self.dataOut.dstFlag = self.basicHeaderObj.dstFlag | |
1411 |
|
1411 | |||
1412 | self.dataOut.errorCount = self.basicHeaderObj.errorCount |
|
1412 | self.dataOut.errorCount = self.basicHeaderObj.errorCount | |
1413 |
|
1413 | |||
1414 | self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime |
|
1414 | self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime | |
1415 |
|
1415 | |||
1416 | self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs |
|
1416 | self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs | |
1417 |
|
1417 | |||
1418 | # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs |
|
1418 | # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs | |
1419 |
|
1419 | |||
1420 | def getFirstHeader(self): |
|
1420 | def getFirstHeader(self): | |
1421 |
|
1421 | |||
1422 | raise NotImplementedError |
|
1422 | raise NotImplementedError | |
1423 |
|
1423 | |||
1424 | def getData(self): |
|
1424 | def getData(self): | |
1425 |
|
1425 | |||
1426 | raise NotImplementedError |
|
1426 | raise NotImplementedError | |
1427 |
|
1427 | |||
1428 | def hasNotDataInBuffer(self): |
|
1428 | def hasNotDataInBuffer(self): | |
1429 |
|
1429 | |||
1430 | raise NotImplementedError |
|
1430 | raise NotImplementedError | |
1431 |
|
1431 | |||
1432 | def readBlock(self): |
|
1432 | def readBlock(self): | |
1433 |
|
1433 | |||
1434 | raise NotImplementedError |
|
1434 | raise NotImplementedError | |
1435 |
|
1435 | |||
1436 | def isEndProcess(self): |
|
1436 | def isEndProcess(self): | |
1437 |
|
1437 | |||
1438 | return self.flagNoMoreFiles |
|
1438 | return self.flagNoMoreFiles | |
1439 |
|
1439 | |||
1440 | def printReadBlocks(self): |
|
1440 | def printReadBlocks(self): | |
1441 |
|
1441 | |||
1442 | print("[Reading] Number of read blocks per file %04d" % self.nReadBlocks) |
|
1442 | print("[Reading] Number of read blocks per file %04d" % self.nReadBlocks) | |
1443 |
|
1443 | |||
1444 | def printTotalBlocks(self): |
|
1444 | def printTotalBlocks(self): | |
1445 |
|
1445 | |||
1446 | print("[Reading] Number of read blocks %04d" % self.nTotalBlocks) |
|
1446 | print("[Reading] Number of read blocks %04d" % self.nTotalBlocks) | |
1447 |
|
1447 | |||
1448 | def printNumberOfBlock(self): |
|
1448 | def printNumberOfBlock(self): | |
1449 | 'SPAM!' |
|
1449 | 'SPAM!' | |
1450 |
|
1450 | |||
1451 | # if self.flagIsNewBlock: |
|
1451 | # if self.flagIsNewBlock: | |
1452 | # print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks, |
|
1452 | # print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks, | |
1453 | # self.processingHeaderObj.dataBlocksPerFile, |
|
1453 | # self.processingHeaderObj.dataBlocksPerFile, | |
1454 | # self.dataOut.datatime.ctime()) |
|
1454 | # self.dataOut.datatime.ctime()) | |
1455 |
|
1455 | |||
1456 | def printInfo(self): |
|
1456 | def printInfo(self): | |
1457 |
|
1457 | |||
1458 | if self.__printInfo == False: |
|
1458 | if self.__printInfo == False: | |
1459 | return |
|
1459 | return | |
1460 |
|
1460 | |||
1461 | self.basicHeaderObj.printInfo() |
|
1461 | self.basicHeaderObj.printInfo() | |
1462 | self.systemHeaderObj.printInfo() |
|
1462 | self.systemHeaderObj.printInfo() | |
1463 | self.radarControllerHeaderObj.printInfo() |
|
1463 | self.radarControllerHeaderObj.printInfo() | |
1464 | self.processingHeaderObj.printInfo() |
|
1464 | self.processingHeaderObj.printInfo() | |
1465 |
|
1465 | |||
1466 | self.__printInfo = False |
|
1466 | self.__printInfo = False | |
1467 |
|
1467 | |||
1468 | def run(self, |
|
1468 | def run(self, | |
1469 | path=None, |
|
1469 | path=None, | |
1470 | startDate=None, |
|
1470 | startDate=None, | |
1471 | endDate=None, |
|
1471 | endDate=None, | |
1472 | startTime=datetime.time(0, 0, 0), |
|
1472 | startTime=datetime.time(0, 0, 0), | |
1473 | endTime=datetime.time(23, 59, 59), |
|
1473 | endTime=datetime.time(23, 59, 59), | |
1474 | set=None, |
|
1474 | set=None, | |
1475 | expLabel="", |
|
1475 | expLabel="", | |
1476 | ext=None, |
|
1476 | ext=None, | |
1477 | online=False, |
|
1477 | online=False, | |
1478 | delay=60, |
|
1478 | delay=60, | |
1479 | walk=True, |
|
1479 | walk=True, | |
1480 | getblock=False, |
|
1480 | getblock=False, | |
1481 | nTxs=1, |
|
1481 | nTxs=1, | |
1482 | realtime=False, |
|
1482 | realtime=False, | |
1483 | blocksize=None, |
|
1483 | blocksize=None, | |
1484 | blocktime=None, |
|
1484 | blocktime=None, | |
1485 | skip=None, |
|
1485 | skip=None, | |
1486 | cursor=None, |
|
1486 | cursor=None, | |
1487 | warnings=True, |
|
1487 | warnings=True, | |
1488 | server=None, |
|
1488 | server=None, | |
1489 | verbose=True, |
|
1489 | verbose=True, | |
1490 | format=None, |
|
1490 | format=None, | |
1491 | oneDDict=None, |
|
1491 | oneDDict=None, | |
1492 | twoDDict=None, |
|
1492 | twoDDict=None, | |
1493 | independentParam=None, **kwargs): |
|
1493 | independentParam=None, **kwargs): | |
1494 |
|
1494 | |||
1495 | if not(self.isConfig): |
|
1495 | if not(self.isConfig): | |
1496 | self.setup(path=path, |
|
1496 | self.setup(path=path, | |
1497 | startDate=startDate, |
|
1497 | startDate=startDate, | |
1498 | endDate=endDate, |
|
1498 | endDate=endDate, | |
1499 | startTime=startTime, |
|
1499 | startTime=startTime, | |
1500 | endTime=endTime, |
|
1500 | endTime=endTime, | |
1501 | set=set, |
|
1501 | set=set, | |
1502 | expLabel=expLabel, |
|
1502 | expLabel=expLabel, | |
1503 | ext=ext, |
|
1503 | ext=ext, | |
1504 | online=online, |
|
1504 | online=online, | |
1505 | delay=delay, |
|
1505 | delay=delay, | |
1506 | walk=walk, |
|
1506 | walk=walk, | |
1507 | getblock=getblock, |
|
1507 | getblock=getblock, | |
1508 | nTxs=nTxs, |
|
1508 | nTxs=nTxs, | |
1509 | realtime=realtime, |
|
1509 | realtime=realtime, | |
1510 | blocksize=blocksize, |
|
1510 | blocksize=blocksize, | |
1511 | blocktime=blocktime, |
|
1511 | blocktime=blocktime, | |
1512 | skip=skip, |
|
1512 | skip=skip, | |
1513 | cursor=cursor, |
|
1513 | cursor=cursor, | |
1514 | warnings=warnings, |
|
1514 | warnings=warnings, | |
1515 | server=server, |
|
1515 | server=server, | |
1516 | verbose=verbose, |
|
1516 | verbose=verbose, | |
1517 | format=format, |
|
1517 | format=format, | |
1518 | oneDDict=oneDDict, |
|
1518 | oneDDict=oneDDict, | |
1519 | twoDDict=twoDDict, |
|
1519 | twoDDict=twoDDict, | |
1520 | independentParam=independentParam) |
|
1520 | independentParam=independentParam) | |
1521 | self.isConfig = True |
|
1521 | self.isConfig = True | |
1522 | if server is None: |
|
1522 | if server is None: | |
1523 | self.getData() |
|
1523 | self.getData() | |
1524 | else: |
|
1524 | else: | |
1525 | self.getFromServer() |
|
1525 | self.getFromServer() | |
1526 |
|
1526 | |||
1527 |
|
1527 | |||
1528 | class JRODataWriter(JRODataIO): |
|
1528 | class JRODataWriter(JRODataIO): | |
1529 |
|
1529 | |||
1530 | """ |
|
1530 | """ | |
1531 | Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura |
|
1531 | Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura | |
1532 | de los datos siempre se realiza por bloques. |
|
1532 | de los datos siempre se realiza por bloques. | |
1533 | """ |
|
1533 | """ | |
1534 |
|
1534 | |||
1535 | blockIndex = 0 |
|
1535 | blockIndex = 0 | |
1536 |
|
1536 | |||
1537 | path = None |
|
1537 | path = None | |
1538 |
|
1538 | |||
1539 | setFile = None |
|
1539 | setFile = None | |
1540 |
|
1540 | |||
1541 | profilesPerBlock = None |
|
1541 | profilesPerBlock = None | |
1542 |
|
1542 | |||
1543 | blocksPerFile = None |
|
1543 | blocksPerFile = None | |
1544 |
|
1544 | |||
1545 | nWriteBlocks = 0 |
|
1545 | nWriteBlocks = 0 | |
1546 |
|
1546 | |||
1547 | fileDate = None |
|
1547 | fileDate = None | |
1548 |
|
1548 | |||
1549 | def __init__(self, dataOut=None): |
|
1549 | def __init__(self, dataOut=None): | |
1550 | raise NotImplementedError |
|
1550 | raise NotImplementedError | |
1551 |
|
1551 | |||
1552 | def hasAllDataInBuffer(self): |
|
1552 | def hasAllDataInBuffer(self): | |
1553 | raise NotImplementedError |
|
1553 | raise NotImplementedError | |
1554 |
|
1554 | |||
1555 | def setBlockDimension(self): |
|
1555 | def setBlockDimension(self): | |
1556 | raise NotImplementedError |
|
1556 | raise NotImplementedError | |
1557 |
|
1557 | |||
1558 | def writeBlock(self): |
|
1558 | def writeBlock(self): | |
1559 | raise NotImplementedError |
|
1559 | raise NotImplementedError | |
1560 |
|
1560 | |||
1561 | def putData(self): |
|
1561 | def putData(self): | |
1562 | raise NotImplementedError |
|
1562 | raise NotImplementedError | |
1563 |
|
1563 | |||
1564 | def getProcessFlags(self): |
|
1564 | def getProcessFlags(self): | |
1565 |
|
1565 | |||
1566 | processFlags = 0 |
|
1566 | processFlags = 0 | |
1567 |
|
1567 | |||
1568 | dtype_index = get_dtype_index(self.dtype) |
|
1568 | dtype_index = get_dtype_index(self.dtype) | |
1569 | procflag_dtype = get_procflag_dtype(dtype_index) |
|
1569 | procflag_dtype = get_procflag_dtype(dtype_index) | |
1570 |
|
1570 | |||
1571 | processFlags += procflag_dtype |
|
1571 | processFlags += procflag_dtype | |
1572 |
|
1572 | |||
1573 | if self.dataOut.flagDecodeData: |
|
1573 | if self.dataOut.flagDecodeData: | |
1574 | processFlags += PROCFLAG.DECODE_DATA |
|
1574 | processFlags += PROCFLAG.DECODE_DATA | |
1575 |
|
1575 | |||
1576 | if self.dataOut.flagDeflipData: |
|
1576 | if self.dataOut.flagDeflipData: | |
1577 | processFlags += PROCFLAG.DEFLIP_DATA |
|
1577 | processFlags += PROCFLAG.DEFLIP_DATA | |
1578 |
|
1578 | |||
1579 | if self.dataOut.code is not None: |
|
1579 | if self.dataOut.code is not None: | |
1580 | processFlags += PROCFLAG.DEFINE_PROCESS_CODE |
|
1580 | processFlags += PROCFLAG.DEFINE_PROCESS_CODE | |
1581 |
|
1581 | |||
1582 | if self.dataOut.nCohInt > 1: |
|
1582 | if self.dataOut.nCohInt > 1: | |
1583 | processFlags += PROCFLAG.COHERENT_INTEGRATION |
|
1583 | processFlags += PROCFLAG.COHERENT_INTEGRATION | |
1584 |
|
1584 | |||
1585 | if self.dataOut.type == "Spectra": |
|
1585 | if self.dataOut.type == "Spectra": | |
1586 | if self.dataOut.nIncohInt > 1: |
|
1586 | if self.dataOut.nIncohInt > 1: | |
1587 | processFlags += PROCFLAG.INCOHERENT_INTEGRATION |
|
1587 | processFlags += PROCFLAG.INCOHERENT_INTEGRATION | |
1588 |
|
1588 | |||
1589 | if self.dataOut.data_dc is not None: |
|
1589 | if self.dataOut.data_dc is not None: | |
1590 | processFlags += PROCFLAG.SAVE_CHANNELS_DC |
|
1590 | processFlags += PROCFLAG.SAVE_CHANNELS_DC | |
1591 |
|
1591 | |||
1592 | if self.dataOut.flagShiftFFT: |
|
1592 | if self.dataOut.flagShiftFFT: | |
1593 | processFlags += PROCFLAG.SHIFT_FFT_DATA |
|
1593 | processFlags += PROCFLAG.SHIFT_FFT_DATA | |
1594 |
|
1594 | |||
1595 | return processFlags |
|
1595 | return processFlags | |
1596 |
|
1596 | |||
1597 | def setBasicHeader(self): |
|
1597 | def setBasicHeader(self): | |
1598 |
|
1598 | |||
1599 | self.basicHeaderObj.size = self.basicHeaderSize # bytes |
|
1599 | self.basicHeaderObj.size = self.basicHeaderSize # bytes | |
1600 | self.basicHeaderObj.version = self.versionFile |
|
1600 | self.basicHeaderObj.version = self.versionFile | |
1601 | self.basicHeaderObj.dataBlock = self.nTotalBlocks |
|
1601 | self.basicHeaderObj.dataBlock = self.nTotalBlocks | |
1602 | log.warning(datetime.datetime.fromtimestamp(self.dataOut.utctime)) |
|
|||
1603 | utc = numpy.floor(self.dataOut.utctime) |
|
1602 | utc = numpy.floor(self.dataOut.utctime) | |
1604 | milisecond = (self.dataOut.utctime - utc) * 1000.0 |
|
1603 | milisecond = (self.dataOut.utctime - utc) * 1000.0 | |
1605 | log.warning(milisecond) |
|
|||
1606 | self.basicHeaderObj.utc = utc |
|
1604 | self.basicHeaderObj.utc = utc | |
1607 | self.basicHeaderObj.miliSecond = milisecond |
|
1605 | self.basicHeaderObj.miliSecond = milisecond | |
1608 | self.basicHeaderObj.timeZone = self.dataOut.timeZone |
|
1606 | self.basicHeaderObj.timeZone = self.dataOut.timeZone | |
1609 | self.basicHeaderObj.dstFlag = self.dataOut.dstFlag |
|
1607 | self.basicHeaderObj.dstFlag = self.dataOut.dstFlag | |
1610 | self.basicHeaderObj.errorCount = self.dataOut.errorCount |
|
1608 | self.basicHeaderObj.errorCount = self.dataOut.errorCount | |
1611 |
|
1609 | |||
1612 | def setFirstHeader(self): |
|
1610 | def setFirstHeader(self): | |
1613 | """ |
|
1611 | """ | |
1614 | Obtiene una copia del First Header |
|
1612 | Obtiene una copia del First Header | |
1615 |
|
1613 | |||
1616 | Affected: |
|
1614 | Affected: | |
1617 |
|
1615 | |||
1618 | self.basicHeaderObj |
|
1616 | self.basicHeaderObj | |
1619 | self.systemHeaderObj |
|
1617 | self.systemHeaderObj | |
1620 | self.radarControllerHeaderObj |
|
1618 | self.radarControllerHeaderObj | |
1621 | self.processingHeaderObj self. |
|
1619 | self.processingHeaderObj self. | |
1622 |
|
1620 | |||
1623 | Return: |
|
1621 | Return: | |
1624 | None |
|
1622 | None | |
1625 | """ |
|
1623 | """ | |
1626 |
|
1624 | |||
1627 | raise NotImplementedError |
|
1625 | raise NotImplementedError | |
1628 |
|
1626 | |||
1629 | def __writeFirstHeader(self): |
|
1627 | def __writeFirstHeader(self): | |
1630 | """ |
|
1628 | """ | |
1631 | Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader) |
|
1629 | Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader) | |
1632 |
|
1630 | |||
1633 | Affected: |
|
1631 | Affected: | |
1634 | __dataType |
|
1632 | __dataType | |
1635 |
|
1633 | |||
1636 | Return: |
|
1634 | Return: | |
1637 | None |
|
1635 | None | |
1638 | """ |
|
1636 | """ | |
1639 |
|
1637 | |||
1640 | # CALCULAR PARAMETROS |
|
1638 | # CALCULAR PARAMETROS | |
1641 |
|
1639 | |||
1642 | sizeLongHeader = self.systemHeaderObj.size + \ |
|
1640 | sizeLongHeader = self.systemHeaderObj.size + \ | |
1643 | self.radarControllerHeaderObj.size + self.processingHeaderObj.size |
|
1641 | self.radarControllerHeaderObj.size + self.processingHeaderObj.size | |
1644 | self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader |
|
1642 | self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader | |
1645 |
|
1643 | |||
1646 | self.basicHeaderObj.write(self.fp) |
|
1644 | self.basicHeaderObj.write(self.fp) | |
1647 | self.systemHeaderObj.write(self.fp) |
|
1645 | self.systemHeaderObj.write(self.fp) | |
1648 | self.radarControllerHeaderObj.write(self.fp) |
|
1646 | self.radarControllerHeaderObj.write(self.fp) | |
1649 | self.processingHeaderObj.write(self.fp) |
|
1647 | self.processingHeaderObj.write(self.fp) | |
1650 |
|
1648 | |||
1651 | def __setNewBlock(self): |
|
1649 | def __setNewBlock(self): | |
1652 | """ |
|
1650 | """ | |
1653 | Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header |
|
1651 | Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header | |
1654 |
|
1652 | |||
1655 | Return: |
|
1653 | Return: | |
1656 | 0 : si no pudo escribir nada |
|
1654 | 0 : si no pudo escribir nada | |
1657 | 1 : Si escribio el Basic el First Header |
|
1655 | 1 : Si escribio el Basic el First Header | |
1658 | """ |
|
1656 | """ | |
1659 | if self.fp == None: |
|
1657 | if self.fp == None: | |
1660 | self.setNextFile() |
|
1658 | self.setNextFile() | |
1661 |
|
1659 | |||
1662 | if self.flagIsNewFile: |
|
1660 | if self.flagIsNewFile: | |
1663 | return 1 |
|
1661 | return 1 | |
1664 |
|
1662 | |||
1665 | if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile: |
|
1663 | if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile: | |
1666 | self.basicHeaderObj.write(self.fp) |
|
1664 | self.basicHeaderObj.write(self.fp) | |
1667 | return 1 |
|
1665 | return 1 | |
1668 |
|
1666 | |||
1669 | if not(self.setNextFile()): |
|
1667 | if not(self.setNextFile()): | |
1670 | return 0 |
|
1668 | return 0 | |
1671 |
|
1669 | |||
1672 | return 1 |
|
1670 | return 1 | |
1673 |
|
1671 | |||
1674 | def writeNextBlock(self): |
|
1672 | def writeNextBlock(self): | |
1675 | """ |
|
1673 | """ | |
1676 | Selecciona el bloque siguiente de datos y los escribe en un file |
|
1674 | Selecciona el bloque siguiente de datos y los escribe en un file | |
1677 |
|
1675 | |||
1678 | Return: |
|
1676 | Return: | |
1679 | 0 : Si no hizo pudo escribir el bloque de datos |
|
1677 | 0 : Si no hizo pudo escribir el bloque de datos | |
1680 | 1 : Si no pudo escribir el bloque de datos |
|
1678 | 1 : Si no pudo escribir el bloque de datos | |
1681 | """ |
|
1679 | """ | |
1682 | if not(self.__setNewBlock()): |
|
1680 | if not(self.__setNewBlock()): | |
1683 | return 0 |
|
1681 | return 0 | |
1684 |
|
1682 | |||
1685 | self.writeBlock() |
|
1683 | self.writeBlock() | |
1686 |
|
1684 | |||
1687 | print("[Writing] Block No. %d/%d" % (self.blockIndex, |
|
1685 | print("[Writing] Block No. %d/%d" % (self.blockIndex, | |
1688 | self.processingHeaderObj.dataBlocksPerFile)) |
|
1686 | self.processingHeaderObj.dataBlocksPerFile)) | |
1689 |
|
1687 | |||
1690 | return 1 |
|
1688 | return 1 | |
1691 |
|
1689 | |||
1692 | def setNextFile(self): |
|
1690 | def setNextFile(self): | |
1693 | """ |
|
1691 | """ | |
1694 | Determina el siguiente file que sera escrito |
|
1692 | Determina el siguiente file que sera escrito | |
1695 |
|
1693 | |||
1696 | Affected: |
|
1694 | Affected: | |
1697 | self.filename |
|
1695 | self.filename | |
1698 | self.subfolder |
|
1696 | self.subfolder | |
1699 | self.fp |
|
1697 | self.fp | |
1700 | self.setFile |
|
1698 | self.setFile | |
1701 | self.flagIsNewFile |
|
1699 | self.flagIsNewFile | |
1702 |
|
1700 | |||
1703 | Return: |
|
1701 | Return: | |
1704 | 0 : Si el archivo no puede ser escrito |
|
1702 | 0 : Si el archivo no puede ser escrito | |
1705 | 1 : Si el archivo esta listo para ser escrito |
|
1703 | 1 : Si el archivo esta listo para ser escrito | |
1706 | """ |
|
1704 | """ | |
1707 | ext = self.ext |
|
1705 | ext = self.ext | |
1708 | path = self.path |
|
1706 | path = self.path | |
1709 |
|
1707 | |||
1710 | if self.fp != None: |
|
1708 | if self.fp != None: | |
1711 | self.fp.close() |
|
1709 | self.fp.close() | |
1712 |
|
1710 | |||
1713 | timeTuple = time.localtime(self.dataOut.utctime) |
|
1711 | timeTuple = time.localtime(self.dataOut.utctime) | |
1714 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday) |
|
1712 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday) | |
1715 |
|
1713 | |||
1716 | fullpath = os.path.join(path, subfolder) |
|
1714 | fullpath = os.path.join(path, subfolder) | |
1717 | setFile = self.setFile |
|
1715 | setFile = self.setFile | |
1718 |
|
1716 | |||
1719 | if not(os.path.exists(fullpath)): |
|
1717 | if not(os.path.exists(fullpath)): | |
1720 | os.mkdir(fullpath) |
|
1718 | os.mkdir(fullpath) | |
1721 | setFile = -1 # inicializo mi contador de seteo |
|
1719 | setFile = -1 # inicializo mi contador de seteo | |
1722 | else: |
|
1720 | else: | |
1723 | filesList = os.listdir(fullpath) |
|
1721 | filesList = os.listdir(fullpath) | |
1724 | if len(filesList) > 0: |
|
1722 | if len(filesList) > 0: | |
1725 | filesList = sorted(filesList, key=str.lower) |
|
1723 | filesList = sorted(filesList, key=str.lower) | |
1726 | filen = filesList[-1] |
|
1724 | filen = filesList[-1] | |
1727 | # el filename debera tener el siguiente formato |
|
1725 | # el filename debera tener el siguiente formato | |
1728 | # 0 1234 567 89A BCDE (hex) |
|
1726 | # 0 1234 567 89A BCDE (hex) | |
1729 | # x YYYY DDD SSS .ext |
|
1727 | # x YYYY DDD SSS .ext | |
1730 | if isNumber(filen[8:11]): |
|
1728 | if isNumber(filen[8:11]): | |
1731 | # inicializo mi contador de seteo al seteo del ultimo file |
|
1729 | # inicializo mi contador de seteo al seteo del ultimo file | |
1732 | setFile = int(filen[8:11]) |
|
1730 | setFile = int(filen[8:11]) | |
1733 | else: |
|
1731 | else: | |
1734 | setFile = -1 |
|
1732 | setFile = -1 | |
1735 | else: |
|
1733 | else: | |
1736 | setFile = -1 # inicializo mi contador de seteo |
|
1734 | setFile = -1 # inicializo mi contador de seteo | |
1737 |
|
1735 | |||
1738 | setFile += 1 |
|
1736 | setFile += 1 | |
1739 |
|
1737 | |||
1740 | # If this is a new day it resets some values |
|
1738 | # If this is a new day it resets some values | |
1741 | if self.dataOut.datatime.date() > self.fileDate: |
|
1739 | if self.dataOut.datatime.date() > self.fileDate: | |
1742 | setFile = 0 |
|
1740 | setFile = 0 | |
1743 | self.nTotalBlocks = 0 |
|
1741 | self.nTotalBlocks = 0 | |
1744 |
|
1742 | |||
1745 | filen = '{}{:04d}{:03d}{:03d}{}'.format( |
|
1743 | filen = '{}{:04d}{:03d}{:03d}{}'.format( | |
1746 | self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext) |
|
1744 | self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext) | |
1747 |
|
1745 | |||
1748 | filename = os.path.join(path, subfolder, filen) |
|
1746 | filename = os.path.join(path, subfolder, filen) | |
1749 |
|
1747 | |||
1750 | fp = open(filename, 'wb') |
|
1748 | fp = open(filename, 'wb') | |
1751 |
|
1749 | |||
1752 | self.blockIndex = 0 |
|
1750 | self.blockIndex = 0 | |
1753 |
|
1751 | |||
1754 | # guardando atributos |
|
1752 | # guardando atributos | |
1755 | self.filename = filename |
|
1753 | self.filename = filename | |
1756 | self.subfolder = subfolder |
|
1754 | self.subfolder = subfolder | |
1757 | self.fp = fp |
|
1755 | self.fp = fp | |
1758 | self.setFile = setFile |
|
1756 | self.setFile = setFile | |
1759 | self.flagIsNewFile = 1 |
|
1757 | self.flagIsNewFile = 1 | |
1760 | self.fileDate = self.dataOut.datatime.date() |
|
1758 | self.fileDate = self.dataOut.datatime.date() | |
1761 |
|
1759 | |||
1762 | self.setFirstHeader() |
|
1760 | self.setFirstHeader() | |
1763 |
|
1761 | |||
1764 | print('[Writing] Opening file: %s' % self.filename) |
|
1762 | print('[Writing] Opening file: %s' % self.filename) | |
1765 |
|
1763 | |||
1766 | self.__writeFirstHeader() |
|
1764 | self.__writeFirstHeader() | |
1767 |
|
1765 | |||
1768 | return 1 |
|
1766 | return 1 | |
1769 |
|
1767 | |||
1770 | def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4): |
|
1768 | def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4): | |
1771 | """ |
|
1769 | """ | |
1772 | Setea el tipo de formato en la cual sera guardada la data y escribe el First Header |
|
1770 | Setea el tipo de formato en la cual sera guardada la data y escribe el First Header | |
1773 |
|
1771 | |||
1774 | Inputs: |
|
1772 | Inputs: | |
1775 | path : directory where data will be saved |
|
1773 | path : directory where data will be saved | |
1776 | profilesPerBlock : number of profiles per block |
|
1774 | profilesPerBlock : number of profiles per block | |
1777 | set : initial file set |
|
1775 | set : initial file set | |
1778 | datatype : An integer number that defines data type: |
|
1776 | datatype : An integer number that defines data type: | |
1779 | 0 : int8 (1 byte) |
|
1777 | 0 : int8 (1 byte) | |
1780 | 1 : int16 (2 bytes) |
|
1778 | 1 : int16 (2 bytes) | |
1781 | 2 : int32 (4 bytes) |
|
1779 | 2 : int32 (4 bytes) | |
1782 | 3 : int64 (8 bytes) |
|
1780 | 3 : int64 (8 bytes) | |
1783 | 4 : float32 (4 bytes) |
|
1781 | 4 : float32 (4 bytes) | |
1784 | 5 : double64 (8 bytes) |
|
1782 | 5 : double64 (8 bytes) | |
1785 |
|
1783 | |||
1786 | Return: |
|
1784 | Return: | |
1787 | 0 : Si no realizo un buen seteo |
|
1785 | 0 : Si no realizo un buen seteo | |
1788 | 1 : Si realizo un buen seteo |
|
1786 | 1 : Si realizo un buen seteo | |
1789 | """ |
|
1787 | """ | |
1790 |
|
1788 | |||
1791 | if ext == None: |
|
1789 | if ext == None: | |
1792 | ext = self.ext |
|
1790 | ext = self.ext | |
1793 |
|
1791 | |||
1794 | self.ext = ext.lower() |
|
1792 | self.ext = ext.lower() | |
1795 |
|
1793 | |||
1796 | self.path = path |
|
1794 | self.path = path | |
1797 |
|
1795 | |||
1798 | if set is None: |
|
1796 | if set is None: | |
1799 | self.setFile = -1 |
|
1797 | self.setFile = -1 | |
1800 | else: |
|
1798 | else: | |
1801 | self.setFile = set - 1 |
|
1799 | self.setFile = set - 1 | |
1802 |
|
1800 | |||
1803 | self.blocksPerFile = blocksPerFile |
|
1801 | self.blocksPerFile = blocksPerFile | |
1804 |
|
1802 | |||
1805 | self.profilesPerBlock = profilesPerBlock |
|
1803 | self.profilesPerBlock = profilesPerBlock | |
1806 |
|
1804 | |||
1807 | self.dataOut = dataOut |
|
1805 | self.dataOut = dataOut | |
1808 | self.fileDate = self.dataOut.datatime.date() |
|
1806 | self.fileDate = self.dataOut.datatime.date() | |
1809 | # By default |
|
1807 | # By default | |
1810 | self.dtype = self.dataOut.dtype |
|
1808 | self.dtype = self.dataOut.dtype | |
1811 |
|
1809 | |||
1812 | if datatype is not None: |
|
1810 | if datatype is not None: | |
1813 | self.dtype = get_numpy_dtype(datatype) |
|
1811 | self.dtype = get_numpy_dtype(datatype) | |
1814 |
|
1812 | |||
1815 | if not(self.setNextFile()): |
|
1813 | if not(self.setNextFile()): | |
1816 | print("[Writing] There isn't a next file") |
|
1814 | print("[Writing] There isn't a next file") | |
1817 | return 0 |
|
1815 | return 0 | |
1818 |
|
1816 | |||
1819 | self.setBlockDimension() |
|
1817 | self.setBlockDimension() | |
1820 |
|
1818 | |||
1821 | return 1 |
|
1819 | return 1 | |
1822 |
|
1820 | |||
1823 | def run(self, dataOut, path, blocksPerFile=100, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs): |
|
1821 | def run(self, dataOut, path, blocksPerFile=100, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs): | |
1824 |
|
1822 | |||
1825 | if not(self.isConfig): |
|
1823 | if not(self.isConfig): | |
1826 |
|
1824 | |||
1827 | self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock, |
|
1825 | self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock, | |
1828 | set=set, ext=ext, datatype=datatype, **kwargs) |
|
1826 | set=set, ext=ext, datatype=datatype, **kwargs) | |
1829 | self.isConfig = True |
|
1827 | self.isConfig = True | |
1830 |
|
1828 | |||
1831 | self.dataOut = dataOut |
|
1829 | self.dataOut = dataOut | |
1832 | self.putData() |
|
1830 | self.putData() | |
1833 | return self.dataOut No newline at end of file |
|
1831 | return self.dataOut |
@@ -1,1543 +1,1544 | |||||
1 | import numpy |
|
1 | import numpy | |
2 | import time |
|
2 | import time | |
3 | import os |
|
3 | import os | |
4 | import h5py |
|
4 | import h5py | |
5 | import re |
|
5 | import re | |
6 | import datetime |
|
6 | import datetime | |
7 |
|
7 | |||
8 | from schainpy.model.data.jrodata import * |
|
8 | from schainpy.model.data.jrodata import * | |
9 | from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator |
|
9 | from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator | |
10 | from schainpy.model.io.jroIO_base import * |
|
10 | from schainpy.model.io.jroIO_base import * | |
11 | from schainpy.utils import log |
|
11 | from schainpy.utils import log | |
12 |
|
12 | |||
13 | @MPDecorator |
|
13 | @MPDecorator | |
14 | class ParamReader(JRODataReader,ProcessingUnit): |
|
14 | class ParamReader(JRODataReader,ProcessingUnit): | |
15 | ''' |
|
15 | ''' | |
16 | Reads HDF5 format files |
|
16 | Reads HDF5 format files | |
17 | path |
|
17 | path | |
18 | startDate |
|
18 | startDate | |
19 | endDate |
|
19 | endDate | |
20 | startTime |
|
20 | startTime | |
21 | endTime |
|
21 | endTime | |
22 | ''' |
|
22 | ''' | |
23 |
|
23 | |||
24 | ext = ".hdf5" |
|
24 | ext = ".hdf5" | |
25 | optchar = "D" |
|
25 | optchar = "D" | |
26 | timezone = None |
|
26 | timezone = None | |
27 | startTime = None |
|
27 | startTime = None | |
28 | endTime = None |
|
28 | endTime = None | |
29 | fileIndex = None |
|
29 | fileIndex = None | |
30 | utcList = None #To select data in the utctime list |
|
30 | utcList = None #To select data in the utctime list | |
31 | blockList = None #List to blocks to be read from the file |
|
31 | blockList = None #List to blocks to be read from the file | |
32 | blocksPerFile = None #Number of blocks to be read |
|
32 | blocksPerFile = None #Number of blocks to be read | |
33 | blockIndex = None |
|
33 | blockIndex = None | |
34 | path = None |
|
34 | path = None | |
35 | #List of Files |
|
35 | #List of Files | |
36 | filenameList = None |
|
36 | filenameList = None | |
37 | datetimeList = None |
|
37 | datetimeList = None | |
38 | #Hdf5 File |
|
38 | #Hdf5 File | |
39 | listMetaname = None |
|
39 | listMetaname = None | |
40 | listMeta = None |
|
40 | listMeta = None | |
41 | listDataname = None |
|
41 | listDataname = None | |
42 | listData = None |
|
42 | listData = None | |
43 | listShapes = None |
|
43 | listShapes = None | |
44 | fp = None |
|
44 | fp = None | |
45 | #dataOut reconstruction |
|
45 | #dataOut reconstruction | |
46 | dataOut = None |
|
46 | dataOut = None | |
47 |
|
47 | |||
48 | def __init__(self):#, **kwargs): |
|
48 | def __init__(self):#, **kwargs): | |
49 | ProcessingUnit.__init__(self) #, **kwargs) |
|
49 | ProcessingUnit.__init__(self) #, **kwargs) | |
50 | self.dataOut = Parameters() |
|
50 | self.dataOut = Parameters() | |
51 | return |
|
51 | return | |
52 |
|
52 | |||
53 | def setup(self, **kwargs): |
|
53 | def setup(self, **kwargs): | |
54 |
|
54 | |||
55 | path = kwargs['path'] |
|
55 | path = kwargs['path'] | |
56 | startDate = kwargs['startDate'] |
|
56 | startDate = kwargs['startDate'] | |
57 | endDate = kwargs['endDate'] |
|
57 | endDate = kwargs['endDate'] | |
58 | startTime = kwargs['startTime'] |
|
58 | startTime = kwargs['startTime'] | |
59 | endTime = kwargs['endTime'] |
|
59 | endTime = kwargs['endTime'] | |
60 | walk = kwargs['walk'] |
|
60 | walk = kwargs['walk'] | |
61 | if 'ext' in kwargs: |
|
61 | if 'ext' in kwargs: | |
62 | ext = kwargs['ext'] |
|
62 | ext = kwargs['ext'] | |
63 | else: |
|
63 | else: | |
64 | ext = '.hdf5' |
|
64 | ext = '.hdf5' | |
65 | if 'timezone' in kwargs: |
|
65 | if 'timezone' in kwargs: | |
66 | self.timezone = kwargs['timezone'] |
|
66 | self.timezone = kwargs['timezone'] | |
67 | else: |
|
67 | else: | |
68 | self.timezone = 'lt' |
|
68 | self.timezone = 'lt' | |
69 |
|
69 | |||
70 | print("[Reading] Searching files in offline mode ...") |
|
70 | print("[Reading] Searching files in offline mode ...") | |
71 | pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate, |
|
71 | pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate, | |
72 | startTime=startTime, endTime=endTime, |
|
72 | startTime=startTime, endTime=endTime, | |
73 | ext=ext, walk=walk) |
|
73 | ext=ext, walk=walk) | |
74 |
|
74 | |||
75 | if not(filenameList): |
|
75 | if not(filenameList): | |
76 | print("There is no files into the folder: %s"%(path)) |
|
76 | print("There is no files into the folder: %s"%(path)) | |
77 | sys.exit(-1) |
|
77 | sys.exit(-1) | |
78 |
|
78 | |||
79 | self.fileIndex = -1 |
|
79 | self.fileIndex = -1 | |
80 | self.startTime = startTime |
|
80 | self.startTime = startTime | |
81 | self.endTime = endTime |
|
81 | self.endTime = endTime | |
82 |
|
82 | |||
83 | self.__readMetadata() |
|
83 | self.__readMetadata() | |
84 |
|
84 | |||
85 | self.__setNextFileOffline() |
|
85 | self.__setNextFileOffline() | |
86 |
|
86 | |||
87 | return |
|
87 | return | |
88 |
|
88 | |||
89 | def searchFilesOffLine(self, |
|
89 | def searchFilesOffLine(self, | |
90 | path, |
|
90 | path, | |
91 | startDate=None, |
|
91 | startDate=None, | |
92 | endDate=None, |
|
92 | endDate=None, | |
93 | startTime=datetime.time(0,0,0), |
|
93 | startTime=datetime.time(0,0,0), | |
94 | endTime=datetime.time(23,59,59), |
|
94 | endTime=datetime.time(23,59,59), | |
95 | ext='.hdf5', |
|
95 | ext='.hdf5', | |
96 | walk=True): |
|
96 | walk=True): | |
97 |
|
97 | |||
98 | expLabel = '' |
|
98 | expLabel = '' | |
99 | self.filenameList = [] |
|
99 | self.filenameList = [] | |
100 | self.datetimeList = [] |
|
100 | self.datetimeList = [] | |
101 |
|
101 | |||
102 | pathList = [] |
|
102 | pathList = [] | |
103 |
|
103 | |||
104 | JRODataObj = JRODataReader() |
|
104 | JRODataObj = JRODataReader() | |
105 | dateList, pathList = JRODataObj.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True) |
|
105 | dateList, pathList = JRODataObj.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True) | |
106 |
|
106 | |||
107 | if dateList == []: |
|
107 | if dateList == []: | |
108 | print("[Reading] No *%s files in %s from %s to %s)"%(ext, path, |
|
108 | print("[Reading] No *%s files in %s from %s to %s)"%(ext, path, | |
109 | datetime.datetime.combine(startDate,startTime).ctime(), |
|
109 | datetime.datetime.combine(startDate,startTime).ctime(), | |
110 | datetime.datetime.combine(endDate,endTime).ctime())) |
|
110 | datetime.datetime.combine(endDate,endTime).ctime())) | |
111 |
|
111 | |||
112 | return None, None |
|
112 | return None, None | |
113 |
|
113 | |||
114 | if len(dateList) > 1: |
|
114 | if len(dateList) > 1: | |
115 | print("[Reading] %d days were found in date range: %s - %s" %(len(dateList), startDate, endDate)) |
|
115 | print("[Reading] %d days were found in date range: %s - %s" %(len(dateList), startDate, endDate)) | |
116 | else: |
|
116 | else: | |
117 | print("[Reading] data was found for the date %s" %(dateList[0])) |
|
117 | print("[Reading] data was found for the date %s" %(dateList[0])) | |
118 |
|
118 | |||
119 | filenameList = [] |
|
119 | filenameList = [] | |
120 | datetimeList = [] |
|
120 | datetimeList = [] | |
121 |
|
121 | |||
122 | #---------------------------------------------------------------------------------- |
|
122 | #---------------------------------------------------------------------------------- | |
123 |
|
123 | |||
124 | for thisPath in pathList: |
|
124 | for thisPath in pathList: | |
125 |
|
125 | |||
126 | fileList = glob.glob1(thisPath, "*%s" %ext) |
|
126 | fileList = glob.glob1(thisPath, "*%s" %ext) | |
127 | fileList.sort() |
|
127 | fileList.sort() | |
128 |
|
128 | |||
129 | for file in fileList: |
|
129 | for file in fileList: | |
130 |
|
130 | |||
131 | filename = os.path.join(thisPath,file) |
|
131 | filename = os.path.join(thisPath,file) | |
132 |
|
132 | |||
133 | if not isFileInDateRange(filename, startDate, endDate): |
|
133 | if not isFileInDateRange(filename, startDate, endDate): | |
134 | continue |
|
134 | continue | |
135 |
|
135 | |||
136 | thisDatetime = self.__isFileInTimeRange(filename, startDate, endDate, startTime, endTime) |
|
136 | thisDatetime = self.__isFileInTimeRange(filename, startDate, endDate, startTime, endTime) | |
137 |
|
137 | |||
138 | if not(thisDatetime): |
|
138 | if not(thisDatetime): | |
139 | continue |
|
139 | continue | |
140 |
|
140 | |||
141 | filenameList.append(filename) |
|
141 | filenameList.append(filename) | |
142 | datetimeList.append(thisDatetime) |
|
142 | datetimeList.append(thisDatetime) | |
143 |
|
143 | |||
144 | if not(filenameList): |
|
144 | if not(filenameList): | |
145 | print("[Reading] Any file was found int time range %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())) |
|
145 | print("[Reading] Any file was found int time range %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())) | |
146 | return None, None |
|
146 | return None, None | |
147 |
|
147 | |||
148 | print("[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)) |
|
148 | print("[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)) | |
149 | print() |
|
149 | print() | |
150 |
|
150 | |||
151 | self.filenameList = filenameList |
|
151 | self.filenameList = filenameList | |
152 | self.datetimeList = datetimeList |
|
152 | self.datetimeList = datetimeList | |
153 |
|
153 | |||
154 | return pathList, filenameList |
|
154 | return pathList, filenameList | |
155 |
|
155 | |||
156 | def __isFileInTimeRange(self,filename, startDate, endDate, startTime, endTime): |
|
156 | def __isFileInTimeRange(self,filename, startDate, endDate, startTime, endTime): | |
157 |
|
157 | |||
158 | """ |
|
158 | """ | |
159 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. |
|
159 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. | |
160 |
|
160 | |||
161 | Inputs: |
|
161 | Inputs: | |
162 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
162 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) | |
163 | startDate : fecha inicial del rango seleccionado en formato datetime.date |
|
163 | startDate : fecha inicial del rango seleccionado en formato datetime.date | |
164 | endDate : fecha final del rango seleccionado en formato datetime.date |
|
164 | endDate : fecha final del rango seleccionado en formato datetime.date | |
165 | startTime : tiempo inicial del rango seleccionado en formato datetime.time |
|
165 | startTime : tiempo inicial del rango seleccionado en formato datetime.time | |
166 | endTime : tiempo final del rango seleccionado en formato datetime.time |
|
166 | endTime : tiempo final del rango seleccionado en formato datetime.time | |
167 |
|
167 | |||
168 | Return: |
|
168 | Return: | |
169 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
169 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de | |
170 | fecha especificado, de lo contrario retorna False. |
|
170 | fecha especificado, de lo contrario retorna False. | |
171 |
|
171 | |||
172 | Excepciones: |
|
172 | Excepciones: | |
173 | Si el archivo no existe o no puede ser abierto |
|
173 | Si el archivo no existe o no puede ser abierto | |
174 | Si la cabecera no puede ser leida. |
|
174 | Si la cabecera no puede ser leida. | |
175 |
|
175 | |||
176 | """ |
|
176 | """ | |
177 |
|
177 | |||
178 | try: |
|
178 | try: | |
179 | fp = h5py.File(filename,'r') |
|
179 | fp = h5py.File(filename,'r') | |
180 | grp1 = fp['Data'] |
|
180 | grp1 = fp['Data'] | |
181 |
|
181 | |||
182 | except IOError: |
|
182 | except IOError: | |
183 | traceback.print_exc() |
|
183 | traceback.print_exc() | |
184 | raise IOError("The file %s can't be opened" %(filename)) |
|
184 | raise IOError("The file %s can't be opened" %(filename)) | |
185 |
|
185 | |||
186 | #In case has utctime attribute |
|
186 | #In case has utctime attribute | |
187 | grp2 = grp1['utctime'] |
|
187 | grp2 = grp1['utctime'] | |
188 | # thisUtcTime = grp2.value[0] - 5*3600 #To convert to local time |
|
188 | # thisUtcTime = grp2.value[0] - 5*3600 #To convert to local time | |
189 | thisUtcTime = grp2.value[0] |
|
189 | thisUtcTime = grp2.value[0] | |
190 |
|
190 | |||
191 | fp.close() |
|
191 | fp.close() | |
192 |
|
192 | |||
193 | if self.timezone == 'lt': |
|
193 | if self.timezone == 'lt': | |
194 | thisUtcTime -= 5*3600 |
|
194 | thisUtcTime -= 5*3600 | |
195 |
|
195 | |||
196 | thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0] + 5*3600) |
|
196 | thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0] + 5*3600) | |
197 | thisDate = thisDatetime.date() |
|
197 | thisDate = thisDatetime.date() | |
198 | thisTime = thisDatetime.time() |
|
198 | thisTime = thisDatetime.time() | |
199 |
|
199 | |||
200 | startUtcTime = (datetime.datetime.combine(thisDate,startTime)- datetime.datetime(1970, 1, 1)).total_seconds() |
|
200 | startUtcTime = (datetime.datetime.combine(thisDate,startTime)- datetime.datetime(1970, 1, 1)).total_seconds() | |
201 | endUtcTime = (datetime.datetime.combine(thisDate,endTime)- datetime.datetime(1970, 1, 1)).total_seconds() |
|
201 | endUtcTime = (datetime.datetime.combine(thisDate,endTime)- datetime.datetime(1970, 1, 1)).total_seconds() | |
202 |
|
202 | |||
203 | #General case |
|
203 | #General case | |
204 | # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o |
|
204 | # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o | |
205 | #-----------o----------------------------o----------- |
|
205 | #-----------o----------------------------o----------- | |
206 | # startTime endTime |
|
206 | # startTime endTime | |
207 |
|
207 | |||
208 | if endTime >= startTime: |
|
208 | if endTime >= startTime: | |
209 | thisUtcLog = numpy.logical_and(thisUtcTime > startUtcTime, thisUtcTime < endUtcTime) |
|
209 | thisUtcLog = numpy.logical_and(thisUtcTime > startUtcTime, thisUtcTime < endUtcTime) | |
210 | if numpy.any(thisUtcLog): #If there is one block between the hours mentioned |
|
210 | if numpy.any(thisUtcLog): #If there is one block between the hours mentioned | |
211 | return thisDatetime |
|
211 | return thisDatetime | |
212 | return None |
|
212 | return None | |
213 |
|
213 | |||
214 | #If endTime < startTime then endTime belongs to the next day |
|
214 | #If endTime < startTime then endTime belongs to the next day | |
215 | #<<<<<<<<<<<o o>>>>>>>>>>> |
|
215 | #<<<<<<<<<<<o o>>>>>>>>>>> | |
216 | #-----------o----------------------------o----------- |
|
216 | #-----------o----------------------------o----------- | |
217 | # endTime startTime |
|
217 | # endTime startTime | |
218 |
|
218 | |||
219 | if (thisDate == startDate) and numpy.all(thisUtcTime < startUtcTime): |
|
219 | if (thisDate == startDate) and numpy.all(thisUtcTime < startUtcTime): | |
220 | return None |
|
220 | return None | |
221 |
|
221 | |||
222 | if (thisDate == endDate) and numpy.all(thisUtcTime > endUtcTime): |
|
222 | if (thisDate == endDate) and numpy.all(thisUtcTime > endUtcTime): | |
223 | return None |
|
223 | return None | |
224 |
|
224 | |||
225 | if numpy.all(thisUtcTime < startUtcTime) and numpy.all(thisUtcTime > endUtcTime): |
|
225 | if numpy.all(thisUtcTime < startUtcTime) and numpy.all(thisUtcTime > endUtcTime): | |
226 | return None |
|
226 | return None | |
227 |
|
227 | |||
228 | return thisDatetime |
|
228 | return thisDatetime | |
229 |
|
229 | |||
230 | def __setNextFileOffline(self): |
|
230 | def __setNextFileOffline(self): | |
231 |
|
231 | |||
232 | self.fileIndex += 1 |
|
232 | self.fileIndex += 1 | |
233 | idFile = self.fileIndex |
|
233 | idFile = self.fileIndex | |
234 |
|
234 | |||
235 | if not(idFile < len(self.filenameList)): |
|
235 | if not(idFile < len(self.filenameList)): | |
236 | self.dataOut.error = "No more Files" |
|
236 | self.dataOut.error = "No more Files" | |
237 | return 0 |
|
237 | return 0 | |
238 |
|
238 | |||
239 | filename = self.filenameList[idFile] |
|
239 | filename = self.filenameList[idFile] | |
240 | filePointer = h5py.File(filename,'r') |
|
240 | filePointer = h5py.File(filename,'r') | |
241 | self.filename = filename |
|
241 | self.filename = filename | |
242 | self.fp = filePointer |
|
242 | self.fp = filePointer | |
243 |
|
243 | |||
244 | print("Setting the file: %s"%self.filename) |
|
244 | print("Setting the file: %s"%self.filename) | |
245 |
|
245 | |||
246 | self.__setBlockList() |
|
246 | self.__setBlockList() | |
247 | self.__readData() |
|
247 | self.__readData() | |
248 | self.blockIndex = 0 |
|
248 | self.blockIndex = 0 | |
249 | return 1 |
|
249 | return 1 | |
250 |
|
250 | |||
251 | def __setBlockList(self): |
|
251 | def __setBlockList(self): | |
252 | ''' |
|
252 | ''' | |
253 | Selects the data within the times defined |
|
253 | Selects the data within the times defined | |
254 |
|
254 | |||
255 | self.fp |
|
255 | self.fp | |
256 | self.startTime |
|
256 | self.startTime | |
257 | self.endTime |
|
257 | self.endTime | |
258 |
|
258 | |||
259 | self.blockList |
|
259 | self.blockList | |
260 | self.blocksPerFile |
|
260 | self.blocksPerFile | |
261 |
|
261 | |||
262 | ''' |
|
262 | ''' | |
263 | fp = self.fp |
|
263 | fp = self.fp | |
264 | startTime = self.startTime |
|
264 | startTime = self.startTime | |
265 | endTime = self.endTime |
|
265 | endTime = self.endTime | |
266 |
|
266 | |||
267 | grp = fp['Data'] |
|
267 | grp = fp['Data'] | |
268 | thisUtcTime = grp['utctime'].value.astype(numpy.float)[0] |
|
268 | thisUtcTime = grp['utctime'].value.astype(numpy.float)[0] | |
269 |
|
269 | |||
270 | #ERROOOOR |
|
270 | #ERROOOOR | |
271 | if self.timezone == 'lt': |
|
271 | if self.timezone == 'lt': | |
272 | thisUtcTime -= 5*3600 |
|
272 | thisUtcTime -= 5*3600 | |
273 |
|
273 | |||
274 | thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0] + 5*3600) |
|
274 | thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0] + 5*3600) | |
275 |
|
275 | |||
276 | thisDate = thisDatetime.date() |
|
276 | thisDate = thisDatetime.date() | |
277 | thisTime = thisDatetime.time() |
|
277 | thisTime = thisDatetime.time() | |
278 |
|
278 | |||
279 | startUtcTime = (datetime.datetime.combine(thisDate,startTime) - datetime.datetime(1970, 1, 1)).total_seconds() |
|
279 | startUtcTime = (datetime.datetime.combine(thisDate,startTime) - datetime.datetime(1970, 1, 1)).total_seconds() | |
280 | endUtcTime = (datetime.datetime.combine(thisDate,endTime) - datetime.datetime(1970, 1, 1)).total_seconds() |
|
280 | endUtcTime = (datetime.datetime.combine(thisDate,endTime) - datetime.datetime(1970, 1, 1)).total_seconds() | |
281 |
|
281 | |||
282 | ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0] |
|
282 | ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0] | |
283 |
|
283 | |||
284 | self.blockList = ind |
|
284 | self.blockList = ind | |
285 | self.blocksPerFile = len(ind) |
|
285 | self.blocksPerFile = len(ind) | |
286 |
|
286 | |||
287 | return |
|
287 | return | |
288 |
|
288 | |||
289 | def __readMetadata(self): |
|
289 | def __readMetadata(self): | |
290 | ''' |
|
290 | ''' | |
291 | Reads Metadata |
|
291 | Reads Metadata | |
292 |
|
292 | |||
293 | self.pathMeta |
|
293 | self.pathMeta | |
294 | self.listShapes |
|
294 | self.listShapes | |
295 | self.listMetaname |
|
295 | self.listMetaname | |
296 | self.listMeta |
|
296 | self.listMeta | |
297 |
|
297 | |||
298 | ''' |
|
298 | ''' | |
299 |
|
299 | |||
300 | filename = self.filenameList[0] |
|
300 | filename = self.filenameList[0] | |
301 | fp = h5py.File(filename,'r') |
|
301 | fp = h5py.File(filename,'r') | |
302 | gp = fp['Metadata'] |
|
302 | gp = fp['Metadata'] | |
303 |
|
303 | |||
304 | listMetaname = [] |
|
304 | listMetaname = [] | |
305 | listMetadata = [] |
|
305 | listMetadata = [] | |
306 | for item in list(gp.items()): |
|
306 | for item in list(gp.items()): | |
307 | name = item[0] |
|
307 | name = item[0] | |
308 |
|
308 | |||
309 | if name=='array dimensions': |
|
309 | if name=='array dimensions': | |
310 | table = gp[name][:] |
|
310 | table = gp[name][:] | |
311 | listShapes = {} |
|
311 | listShapes = {} | |
312 | for shapes in table: |
|
312 | for shapes in table: | |
313 | listShapes[shapes[0]] = numpy.array([shapes[1],shapes[2],shapes[3],shapes[4],shapes[5]]) |
|
313 | listShapes[shapes[0]] = numpy.array([shapes[1],shapes[2],shapes[3],shapes[4],shapes[5]]) | |
314 | else: |
|
314 | else: | |
315 | data = gp[name].value |
|
315 | data = gp[name].value | |
316 | listMetaname.append(name) |
|
316 | listMetaname.append(name) | |
317 | listMetadata.append(data) |
|
317 | listMetadata.append(data) | |
318 |
|
318 | |||
319 | self.listShapes = listShapes |
|
319 | self.listShapes = listShapes | |
320 | self.listMetaname = listMetaname |
|
320 | self.listMetaname = listMetaname | |
321 | self.listMeta = listMetadata |
|
321 | self.listMeta = listMetadata | |
322 |
|
322 | |||
323 | fp.close() |
|
323 | fp.close() | |
324 | return |
|
324 | return | |
325 |
|
325 | |||
326 | def __readData(self): |
|
326 | def __readData(self): | |
327 | grp = self.fp['Data'] |
|
327 | grp = self.fp['Data'] | |
328 | listdataname = [] |
|
328 | listdataname = [] | |
329 | listdata = [] |
|
329 | listdata = [] | |
330 |
|
330 | |||
331 | for item in list(grp.items()): |
|
331 | for item in list(grp.items()): | |
332 | name = item[0] |
|
332 | name = item[0] | |
333 | listdataname.append(name) |
|
333 | listdataname.append(name) | |
334 |
|
334 | |||
335 | array = self.__setDataArray(grp[name],self.listShapes[name]) |
|
335 | array = self.__setDataArray(grp[name],self.listShapes[name]) | |
336 | listdata.append(array) |
|
336 | listdata.append(array) | |
337 |
|
337 | |||
338 | self.listDataname = listdataname |
|
338 | self.listDataname = listdataname | |
339 | self.listData = listdata |
|
339 | self.listData = listdata | |
340 | return |
|
340 | return | |
341 |
|
341 | |||
342 | def __setDataArray(self, dataset, shapes): |
|
342 | def __setDataArray(self, dataset, shapes): | |
343 |
|
343 | |||
344 | nDims = shapes[0] |
|
344 | nDims = shapes[0] | |
345 | nDim2 = shapes[1] #Dimension 0 |
|
345 | nDim2 = shapes[1] #Dimension 0 | |
346 | nDim1 = shapes[2] #Dimension 1, number of Points or Parameters |
|
346 | nDim1 = shapes[2] #Dimension 1, number of Points or Parameters | |
347 | nDim0 = shapes[3] #Dimension 2, number of samples or ranges |
|
347 | nDim0 = shapes[3] #Dimension 2, number of samples or ranges | |
348 | mode = shapes[4] #Mode of storing |
|
348 | mode = shapes[4] #Mode of storing | |
349 | blockList = self.blockList |
|
349 | blockList = self.blockList | |
350 | blocksPerFile = self.blocksPerFile |
|
350 | blocksPerFile = self.blocksPerFile | |
351 |
|
351 | |||
352 | #Depending on what mode the data was stored |
|
352 | #Depending on what mode the data was stored | |
353 | if mode == 0: #Divided in channels |
|
353 | if mode == 0: #Divided in channels | |
354 | arrayData = dataset.value.astype(numpy.float)[0][blockList] |
|
354 | arrayData = dataset.value.astype(numpy.float)[0][blockList] | |
355 | if mode == 1: #Divided in parameter |
|
355 | if mode == 1: #Divided in parameter | |
356 | strds = 'table' |
|
356 | strds = 'table' | |
357 | nDatas = nDim1 |
|
357 | nDatas = nDim1 | |
358 | newShapes = (blocksPerFile,nDim2,nDim0) |
|
358 | newShapes = (blocksPerFile,nDim2,nDim0) | |
359 | elif mode==2: #Concatenated in a table |
|
359 | elif mode==2: #Concatenated in a table | |
360 | strds = 'table0' |
|
360 | strds = 'table0' | |
361 | arrayData = dataset[strds].value |
|
361 | arrayData = dataset[strds].value | |
362 | #Selecting part of the dataset |
|
362 | #Selecting part of the dataset | |
363 | utctime = arrayData[:,0] |
|
363 | utctime = arrayData[:,0] | |
364 | u, indices = numpy.unique(utctime, return_index=True) |
|
364 | u, indices = numpy.unique(utctime, return_index=True) | |
365 |
|
365 | |||
366 | if blockList.size != indices.size: |
|
366 | if blockList.size != indices.size: | |
367 | indMin = indices[blockList[0]] |
|
367 | indMin = indices[blockList[0]] | |
368 | if blockList[1] + 1 >= indices.size: |
|
368 | if blockList[1] + 1 >= indices.size: | |
369 | arrayData = arrayData[indMin:,:] |
|
369 | arrayData = arrayData[indMin:,:] | |
370 | else: |
|
370 | else: | |
371 | indMax = indices[blockList[1] + 1] |
|
371 | indMax = indices[blockList[1] + 1] | |
372 | arrayData = arrayData[indMin:indMax,:] |
|
372 | arrayData = arrayData[indMin:indMax,:] | |
373 | return arrayData |
|
373 | return arrayData | |
374 |
|
374 | |||
375 | # One dimension |
|
375 | # One dimension | |
376 | if nDims == 0: |
|
376 | if nDims == 0: | |
377 | arrayData = dataset.value.astype(numpy.float)[0][blockList] |
|
377 | arrayData = dataset.value.astype(numpy.float)[0][blockList] | |
378 |
|
378 | |||
379 | # Two dimensions |
|
379 | # Two dimensions | |
380 | elif nDims == 2: |
|
380 | elif nDims == 2: | |
381 | arrayData = numpy.zeros((blocksPerFile,nDim1,nDim0)) |
|
381 | arrayData = numpy.zeros((blocksPerFile,nDim1,nDim0)) | |
382 | newShapes = (blocksPerFile,nDim0) |
|
382 | newShapes = (blocksPerFile,nDim0) | |
383 | nDatas = nDim1 |
|
383 | nDatas = nDim1 | |
384 |
|
384 | |||
385 | for i in range(nDatas): |
|
385 | for i in range(nDatas): | |
386 | data = dataset[strds + str(i)].value |
|
386 | data = dataset[strds + str(i)].value | |
387 | arrayData[:,i,:] = data[blockList,:] |
|
387 | arrayData[:,i,:] = data[blockList,:] | |
388 |
|
388 | |||
389 | # Three dimensions |
|
389 | # Three dimensions | |
390 | else: |
|
390 | else: | |
391 | arrayData = numpy.zeros((blocksPerFile,nDim2,nDim1,nDim0)) |
|
391 | arrayData = numpy.zeros((blocksPerFile,nDim2,nDim1,nDim0)) | |
392 | for i in range(nDatas): |
|
392 | for i in range(nDatas): | |
393 |
|
393 | |||
394 | data = dataset[strds + str(i)].value |
|
394 | data = dataset[strds + str(i)].value | |
395 |
|
395 | |||
396 | for b in range(blockList.size): |
|
396 | for b in range(blockList.size): | |
397 | arrayData[b,:,i,:] = data[:,:,blockList[b]] |
|
397 | arrayData[b,:,i,:] = data[:,:,blockList[b]] | |
398 |
|
398 | |||
399 | return arrayData |
|
399 | return arrayData | |
400 |
|
400 | |||
401 | def __setDataOut(self): |
|
401 | def __setDataOut(self): | |
402 | listMeta = self.listMeta |
|
402 | listMeta = self.listMeta | |
403 | listMetaname = self.listMetaname |
|
403 | listMetaname = self.listMetaname | |
404 | listDataname = self.listDataname |
|
404 | listDataname = self.listDataname | |
405 | listData = self.listData |
|
405 | listData = self.listData | |
406 | listShapes = self.listShapes |
|
406 | listShapes = self.listShapes | |
407 |
|
407 | |||
408 | blockIndex = self.blockIndex |
|
408 | blockIndex = self.blockIndex | |
409 | # blockList = self.blockList |
|
409 | # blockList = self.blockList | |
410 |
|
410 | |||
411 | for i in range(len(listMeta)): |
|
411 | for i in range(len(listMeta)): | |
412 | setattr(self.dataOut,listMetaname[i],listMeta[i]) |
|
412 | setattr(self.dataOut,listMetaname[i],listMeta[i]) | |
413 |
|
413 | |||
414 | for j in range(len(listData)): |
|
414 | for j in range(len(listData)): | |
415 | nShapes = listShapes[listDataname[j]][0] |
|
415 | nShapes = listShapes[listDataname[j]][0] | |
416 | mode = listShapes[listDataname[j]][4] |
|
416 | mode = listShapes[listDataname[j]][4] | |
417 | if nShapes == 1: |
|
417 | if nShapes == 1: | |
418 | setattr(self.dataOut,listDataname[j],listData[j][blockIndex]) |
|
418 | setattr(self.dataOut,listDataname[j],listData[j][blockIndex]) | |
419 | elif nShapes > 1: |
|
419 | elif nShapes > 1: | |
420 | setattr(self.dataOut,listDataname[j],listData[j][blockIndex,:]) |
|
420 | setattr(self.dataOut,listDataname[j],listData[j][blockIndex,:]) | |
421 | elif mode==0: |
|
421 | elif mode==0: | |
422 | setattr(self.dataOut,listDataname[j],listData[j][blockIndex]) |
|
422 | setattr(self.dataOut,listDataname[j],listData[j][blockIndex]) | |
423 | #Mode Meteors |
|
423 | #Mode Meteors | |
424 | elif mode ==2: |
|
424 | elif mode ==2: | |
425 | selectedData = self.__selectDataMode2(listData[j], blockIndex) |
|
425 | selectedData = self.__selectDataMode2(listData[j], blockIndex) | |
426 | setattr(self.dataOut, listDataname[j], selectedData) |
|
426 | setattr(self.dataOut, listDataname[j], selectedData) | |
427 | return |
|
427 | return | |
428 |
|
428 | |||
429 | def __selectDataMode2(self, data, blockIndex): |
|
429 | def __selectDataMode2(self, data, blockIndex): | |
430 | utctime = data[:,0] |
|
430 | utctime = data[:,0] | |
431 | aux, indices = numpy.unique(utctime, return_inverse=True) |
|
431 | aux, indices = numpy.unique(utctime, return_inverse=True) | |
432 | selInd = numpy.where(indices == blockIndex)[0] |
|
432 | selInd = numpy.where(indices == blockIndex)[0] | |
433 | selData = data[selInd,:] |
|
433 | selData = data[selInd,:] | |
434 |
|
434 | |||
435 | return selData |
|
435 | return selData | |
436 |
|
436 | |||
437 | def getData(self): |
|
437 | def getData(self): | |
438 |
|
438 | |||
439 | if self.blockIndex==self.blocksPerFile: |
|
439 | if self.blockIndex==self.blocksPerFile: | |
440 | if not( self.__setNextFileOffline() ): |
|
440 | if not( self.__setNextFileOffline() ): | |
441 | self.dataOut.flagNoData = True |
|
441 | self.dataOut.flagNoData = True | |
442 | return 0 |
|
442 | return 0 | |
443 |
|
443 | |||
444 | self.__setDataOut() |
|
444 | self.__setDataOut() | |
445 | self.dataOut.flagNoData = False |
|
445 | self.dataOut.flagNoData = False | |
446 |
|
446 | |||
447 | self.blockIndex += 1 |
|
447 | self.blockIndex += 1 | |
448 |
|
448 | |||
449 | return |
|
449 | return | |
450 |
|
450 | |||
451 | def run(self, **kwargs): |
|
451 | def run(self, **kwargs): | |
452 |
|
452 | |||
453 | if not(self.isConfig): |
|
453 | if not(self.isConfig): | |
454 | self.setup(**kwargs) |
|
454 | self.setup(**kwargs) | |
455 | self.isConfig = True |
|
455 | self.isConfig = True | |
456 |
|
456 | |||
457 | self.getData() |
|
457 | self.getData() | |
458 |
|
458 | |||
459 | return |
|
459 | return | |
460 |
|
460 | |||
461 | @MPDecorator |
|
461 | @MPDecorator | |
462 | class ParamWriter(Operation): |
|
462 | class ParamWriter(Operation): | |
463 | ''' |
|
463 | ''' | |
464 | HDF5 Writer, stores parameters data in HDF5 format files |
|
464 | HDF5 Writer, stores parameters data in HDF5 format files | |
465 |
|
465 | |||
466 | path: path where the files will be stored |
|
466 | path: path where the files will be stored | |
467 | blocksPerFile: number of blocks that will be saved in per HDF5 format file |
|
467 | blocksPerFile: number of blocks that will be saved in per HDF5 format file | |
468 | mode: selects the data stacking mode: '0' channels, '1' parameters, '3' table (for meteors) |
|
468 | mode: selects the data stacking mode: '0' channels, '1' parameters, '3' table (for meteors) | |
469 | metadataList: list of attributes that will be stored as metadata |
|
469 | metadataList: list of attributes that will be stored as metadata | |
470 | dataList: list of attributes that will be stores as data |
|
470 | dataList: list of attributes that will be stores as data | |
471 | ''' |
|
471 | ''' | |
472 |
|
472 | |||
473 | ext = ".hdf5" |
|
473 | ext = ".hdf5" | |
474 | optchar = "D" |
|
474 | optchar = "D" | |
475 | metaoptchar = "M" |
|
475 | metaoptchar = "M" | |
476 | metaFile = None |
|
476 | metaFile = None | |
477 | filename = None |
|
477 | filename = None | |
478 | path = None |
|
478 | path = None | |
479 | setFile = None |
|
479 | setFile = None | |
480 | fp = None |
|
480 | fp = None | |
481 | grp = None |
|
481 | grp = None | |
482 | ds = None |
|
482 | ds = None | |
483 | firsttime = True |
|
483 | firsttime = True | |
484 | #Configurations |
|
484 | #Configurations | |
485 | blocksPerFile = None |
|
485 | blocksPerFile = None | |
486 | blockIndex = None |
|
486 | blockIndex = None | |
487 | dataOut = None |
|
487 | dataOut = None | |
488 | #Data Arrays |
|
488 | #Data Arrays | |
489 | dataList = None |
|
489 | dataList = None | |
490 | metadataList = None |
|
490 | metadataList = None | |
491 | dsList = None #List of dictionaries with dataset properties |
|
491 | dsList = None #List of dictionaries with dataset properties | |
492 | tableDim = None |
|
492 | tableDim = None | |
493 | dtype = [('arrayName', 'S20'),('nDimensions', 'i'), ('dim2', 'i'), ('dim1', 'i'),('dim0', 'i'),('mode', 'b')] |
|
493 | dtype = [('arrayName', 'S20'),('nDimensions', 'i'), ('dim2', 'i'), ('dim1', 'i'),('dim0', 'i'),('mode', 'b')] | |
494 | currentDay = None |
|
494 | currentDay = None | |
495 | lastTime = None |
|
495 | lastTime = None | |
496 | setType = None |
|
496 | setType = None | |
497 |
|
497 | |||
498 | def __init__(self): |
|
498 | def __init__(self): | |
499 |
|
499 | |||
500 | Operation.__init__(self) |
|
500 | Operation.__init__(self) | |
501 | return |
|
501 | return | |
502 |
|
502 | |||
503 | def setup(self, dataOut, path=None, blocksPerFile=10, metadataList=None, dataList=None, mode=None, setType=None): |
|
503 | def setup(self, dataOut, path=None, blocksPerFile=10, metadataList=None, dataList=None, mode=None, setType=None): | |
504 | self.path = path |
|
504 | self.path = path | |
505 | self.blocksPerFile = blocksPerFile |
|
505 | self.blocksPerFile = blocksPerFile | |
506 | self.metadataList = metadataList |
|
506 | self.metadataList = metadataList | |
507 | self.dataList = dataList |
|
507 | self.dataList = dataList | |
508 | self.dataOut = dataOut |
|
508 | self.dataOut = dataOut | |
509 | self.mode = mode |
|
509 | self.mode = mode | |
510 | if self.mode is not None: |
|
510 | if self.mode is not None: | |
511 | self.mode = numpy.zeros(len(self.dataList)) + mode |
|
511 | self.mode = numpy.zeros(len(self.dataList)) + mode | |
512 | else: |
|
512 | else: | |
513 | self.mode = numpy.ones(len(self.dataList)) |
|
513 | self.mode = numpy.ones(len(self.dataList)) | |
514 |
|
514 | |||
515 | self.setType = setType |
|
515 | self.setType = setType | |
516 |
|
516 | |||
517 | arrayDim = numpy.zeros((len(self.dataList),5)) |
|
517 | arrayDim = numpy.zeros((len(self.dataList),5)) | |
518 |
|
518 | |||
519 | #Table dimensions |
|
519 | #Table dimensions | |
520 | dtype0 = self.dtype |
|
520 | dtype0 = self.dtype | |
521 | tableList = [] |
|
521 | tableList = [] | |
522 |
|
522 | |||
523 | #Dictionary and list of tables |
|
523 | #Dictionary and list of tables | |
524 | dsList = [] |
|
524 | dsList = [] | |
525 |
|
525 | |||
526 | for i in range(len(self.dataList)): |
|
526 | for i in range(len(self.dataList)): | |
527 | dsDict = {} |
|
527 | dsDict = {} | |
528 | dataAux = getattr(self.dataOut, self.dataList[i]) |
|
528 | dataAux = getattr(self.dataOut, self.dataList[i]) | |
529 | dsDict['variable'] = self.dataList[i] |
|
529 | dsDict['variable'] = self.dataList[i] | |
530 | #--------------------- Conditionals ------------------------ |
|
530 | #--------------------- Conditionals ------------------------ | |
531 | #There is no data |
|
531 | #There is no data | |
532 |
|
532 | |||
533 | if dataAux is None: |
|
533 | if dataAux is None: | |
534 |
|
534 | |||
535 | return 0 |
|
535 | return 0 | |
536 |
|
536 | |||
537 | if isinstance(dataAux, (int, float, numpy.integer, numpy.float)): |
|
537 | if isinstance(dataAux, (int, float, numpy.integer, numpy.float)): | |
538 | dsDict['mode'] = 0 |
|
538 | dsDict['mode'] = 0 | |
539 | dsDict['nDim'] = 0 |
|
539 | dsDict['nDim'] = 0 | |
540 | arrayDim[i,0] = 0 |
|
540 | arrayDim[i,0] = 0 | |
541 | dsList.append(dsDict) |
|
541 | dsList.append(dsDict) | |
542 |
|
542 | |||
543 | #Mode 2: meteors |
|
543 | #Mode 2: meteors | |
544 | elif self.mode[i] == 2: |
|
544 | elif self.mode[i] == 2: | |
545 | dsDict['dsName'] = 'table0' |
|
545 | dsDict['dsName'] = 'table0' | |
546 | dsDict['mode'] = 2 # Mode meteors |
|
546 | dsDict['mode'] = 2 # Mode meteors | |
547 | dsDict['shape'] = dataAux.shape[-1] |
|
547 | dsDict['shape'] = dataAux.shape[-1] | |
548 | dsDict['nDim'] = 0 |
|
548 | dsDict['nDim'] = 0 | |
549 | dsDict['dsNumber'] = 1 |
|
549 | dsDict['dsNumber'] = 1 | |
550 | arrayDim[i,3] = dataAux.shape[-1] |
|
550 | arrayDim[i,3] = dataAux.shape[-1] | |
551 | arrayDim[i,4] = self.mode[i] #Mode the data was stored |
|
551 | arrayDim[i,4] = self.mode[i] #Mode the data was stored | |
552 | dsList.append(dsDict) |
|
552 | dsList.append(dsDict) | |
553 |
|
553 | |||
554 | #Mode 1 |
|
554 | #Mode 1 | |
555 | else: |
|
555 | else: | |
556 | arrayDim0 = dataAux.shape #Data dimensions |
|
556 | arrayDim0 = dataAux.shape #Data dimensions | |
557 | arrayDim[i,0] = len(arrayDim0) #Number of array dimensions |
|
557 | arrayDim[i,0] = len(arrayDim0) #Number of array dimensions | |
558 | arrayDim[i,4] = self.mode[i] #Mode the data was stored |
|
558 | arrayDim[i,4] = self.mode[i] #Mode the data was stored | |
559 | strtable = 'table' |
|
559 | strtable = 'table' | |
560 | dsDict['mode'] = 1 # Mode parameters |
|
560 | dsDict['mode'] = 1 # Mode parameters | |
561 |
|
561 | |||
562 | # Three-dimension arrays |
|
562 | # Three-dimension arrays | |
563 | if len(arrayDim0) == 3: |
|
563 | if len(arrayDim0) == 3: | |
564 | arrayDim[i,1:-1] = numpy.array(arrayDim0) |
|
564 | arrayDim[i,1:-1] = numpy.array(arrayDim0) | |
565 | nTables = int(arrayDim[i,2]) |
|
565 | nTables = int(arrayDim[i,2]) | |
566 | dsDict['dsNumber'] = nTables |
|
566 | dsDict['dsNumber'] = nTables | |
567 | dsDict['shape'] = arrayDim[i,2:4] |
|
567 | dsDict['shape'] = arrayDim[i,2:4] | |
568 | dsDict['nDim'] = 3 |
|
568 | dsDict['nDim'] = 3 | |
569 |
|
569 | |||
570 | for j in range(nTables): |
|
570 | for j in range(nTables): | |
571 | dsDict = dsDict.copy() |
|
571 | dsDict = dsDict.copy() | |
572 | dsDict['dsName'] = strtable + str(j) |
|
572 | dsDict['dsName'] = strtable + str(j) | |
573 | dsList.append(dsDict) |
|
573 | dsList.append(dsDict) | |
574 |
|
574 | |||
575 | # Two-dimension arrays |
|
575 | # Two-dimension arrays | |
576 | elif len(arrayDim0) == 2: |
|
576 | elif len(arrayDim0) == 2: | |
577 | arrayDim[i,2:-1] = numpy.array(arrayDim0) |
|
577 | arrayDim[i,2:-1] = numpy.array(arrayDim0) | |
578 | nTables = int(arrayDim[i,2]) |
|
578 | nTables = int(arrayDim[i,2]) | |
579 | dsDict['dsNumber'] = nTables |
|
579 | dsDict['dsNumber'] = nTables | |
580 | dsDict['shape'] = arrayDim[i,3] |
|
580 | dsDict['shape'] = arrayDim[i,3] | |
581 | dsDict['nDim'] = 2 |
|
581 | dsDict['nDim'] = 2 | |
582 |
|
582 | |||
583 | for j in range(nTables): |
|
583 | for j in range(nTables): | |
584 | dsDict = dsDict.copy() |
|
584 | dsDict = dsDict.copy() | |
585 | dsDict['dsName'] = strtable + str(j) |
|
585 | dsDict['dsName'] = strtable + str(j) | |
586 | dsList.append(dsDict) |
|
586 | dsList.append(dsDict) | |
587 |
|
587 | |||
588 | # One-dimension arrays |
|
588 | # One-dimension arrays | |
589 | elif len(arrayDim0) == 1: |
|
589 | elif len(arrayDim0) == 1: | |
590 | arrayDim[i,3] = arrayDim0[0] |
|
590 | arrayDim[i,3] = arrayDim0[0] | |
591 | dsDict['shape'] = arrayDim0[0] |
|
591 | dsDict['shape'] = arrayDim0[0] | |
592 | dsDict['dsNumber'] = 1 |
|
592 | dsDict['dsNumber'] = 1 | |
593 | dsDict['dsName'] = strtable + str(0) |
|
593 | dsDict['dsName'] = strtable + str(0) | |
594 | dsDict['nDim'] = 1 |
|
594 | dsDict['nDim'] = 1 | |
595 | dsList.append(dsDict) |
|
595 | dsList.append(dsDict) | |
596 |
|
596 | |||
597 | table = numpy.array((self.dataList[i],) + tuple(arrayDim[i,:]),dtype = dtype0) |
|
597 | table = numpy.array((self.dataList[i],) + tuple(arrayDim[i,:]),dtype = dtype0) | |
598 | tableList.append(table) |
|
598 | tableList.append(table) | |
599 |
|
599 | |||
600 | self.dsList = dsList |
|
600 | self.dsList = dsList | |
601 | self.tableDim = numpy.array(tableList, dtype = dtype0) |
|
601 | self.tableDim = numpy.array(tableList, dtype = dtype0) | |
602 | self.blockIndex = 0 |
|
602 | self.blockIndex = 0 | |
603 | timeTuple = time.localtime(dataOut.utctime) |
|
603 | timeTuple = time.localtime(dataOut.utctime) | |
604 | self.currentDay = timeTuple.tm_yday |
|
604 | self.currentDay = timeTuple.tm_yday | |
605 |
|
605 | |||
606 | def putMetadata(self): |
|
606 | def putMetadata(self): | |
607 |
|
607 | |||
608 | fp = self.createMetadataFile() |
|
608 | fp = self.createMetadataFile() | |
609 | self.writeMetadata(fp) |
|
609 | self.writeMetadata(fp) | |
610 | fp.close() |
|
610 | fp.close() | |
611 | return |
|
611 | return | |
612 |
|
612 | |||
613 | def createMetadataFile(self): |
|
613 | def createMetadataFile(self): | |
614 | ext = self.ext |
|
614 | ext = self.ext | |
615 | path = self.path |
|
615 | path = self.path | |
616 | setFile = self.setFile |
|
616 | setFile = self.setFile | |
617 |
|
617 | |||
618 | timeTuple = time.localtime(self.dataOut.utctime) |
|
618 | timeTuple = time.localtime(self.dataOut.utctime) | |
619 |
|
619 | |||
620 | subfolder = '' |
|
620 | subfolder = '' | |
621 | fullpath = os.path.join( path, subfolder ) |
|
621 | fullpath = os.path.join( path, subfolder ) | |
622 |
|
622 | |||
623 | if not( os.path.exists(fullpath) ): |
|
623 | if not( os.path.exists(fullpath) ): | |
624 | os.mkdir(fullpath) |
|
624 | os.mkdir(fullpath) | |
625 | setFile = -1 #inicializo mi contador de seteo |
|
625 | setFile = -1 #inicializo mi contador de seteo | |
626 |
|
626 | |||
627 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) |
|
627 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) | |
628 | fullpath = os.path.join( path, subfolder ) |
|
628 | fullpath = os.path.join( path, subfolder ) | |
629 |
|
629 | |||
630 | if not( os.path.exists(fullpath) ): |
|
630 | if not( os.path.exists(fullpath) ): | |
631 | os.mkdir(fullpath) |
|
631 | os.mkdir(fullpath) | |
632 | setFile = -1 #inicializo mi contador de seteo |
|
632 | setFile = -1 #inicializo mi contador de seteo | |
633 |
|
633 | |||
634 | else: |
|
634 | else: | |
635 | filesList = os.listdir( fullpath ) |
|
635 | filesList = os.listdir( fullpath ) | |
636 | filesList = sorted( filesList, key=str.lower ) |
|
636 | filesList = sorted( filesList, key=str.lower ) | |
637 | if len( filesList ) > 0: |
|
637 | if len( filesList ) > 0: | |
638 | filesList = [k for k in filesList if k.startswith(self.metaoptchar)] |
|
638 | filesList = [k for k in filesList if k.startswith(self.metaoptchar)] | |
639 | filen = filesList[-1] |
|
639 | filen = filesList[-1] | |
640 | # el filename debera tener el siguiente formato |
|
640 | # el filename debera tener el siguiente formato | |
641 | # 0 1234 567 89A BCDE (hex) |
|
641 | # 0 1234 567 89A BCDE (hex) | |
642 | # x YYYY DDD SSS .ext |
|
642 | # x YYYY DDD SSS .ext | |
643 | if isNumber( filen[8:11] ): |
|
643 | if isNumber( filen[8:11] ): | |
644 | setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file |
|
644 | setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file | |
645 | else: |
|
645 | else: | |
646 | setFile = -1 |
|
646 | setFile = -1 | |
647 | else: |
|
647 | else: | |
648 | setFile = -1 #inicializo mi contador de seteo |
|
648 | setFile = -1 #inicializo mi contador de seteo | |
649 |
|
649 | |||
650 | if self.setType is None: |
|
650 | if self.setType is None: | |
651 | setFile += 1 |
|
651 | setFile += 1 | |
652 | file = '%s%4.4d%3.3d%03d%s' % (self.metaoptchar, |
|
652 | file = '%s%4.4d%3.3d%03d%s' % (self.metaoptchar, | |
653 | timeTuple.tm_year, |
|
653 | timeTuple.tm_year, | |
654 | timeTuple.tm_yday, |
|
654 | timeTuple.tm_yday, | |
655 | setFile, |
|
655 | setFile, | |
656 | ext ) |
|
656 | ext ) | |
657 | else: |
|
657 | else: | |
658 | setFile = timeTuple.tm_hour*60+timeTuple.tm_min |
|
658 | setFile = timeTuple.tm_hour*60+timeTuple.tm_min | |
659 | file = '%s%4.4d%3.3d%04d%s' % (self.metaoptchar, |
|
659 | file = '%s%4.4d%3.3d%04d%s' % (self.metaoptchar, | |
660 | timeTuple.tm_year, |
|
660 | timeTuple.tm_year, | |
661 | timeTuple.tm_yday, |
|
661 | timeTuple.tm_yday, | |
662 | setFile, |
|
662 | setFile, | |
663 | ext ) |
|
663 | ext ) | |
664 |
|
664 | |||
665 | filename = os.path.join( path, subfolder, file ) |
|
665 | filename = os.path.join( path, subfolder, file ) | |
666 | self.metaFile = file |
|
666 | self.metaFile = file | |
667 | #Setting HDF5 File |
|
667 | #Setting HDF5 File | |
668 | fp = h5py.File(filename,'w') |
|
668 | fp = h5py.File(filename,'w') | |
669 |
|
669 | |||
670 | return fp |
|
670 | return fp | |
671 |
|
671 | |||
672 | def writeMetadata(self, fp): |
|
672 | def writeMetadata(self, fp): | |
673 |
|
673 | |||
674 | grp = fp.create_group("Metadata") |
|
674 | grp = fp.create_group("Metadata") | |
675 | grp.create_dataset('array dimensions', data = self.tableDim, dtype = self.dtype) |
|
675 | grp.create_dataset('array dimensions', data = self.tableDim, dtype = self.dtype) | |
676 |
|
676 | |||
677 | for i in range(len(self.metadataList)): |
|
677 | for i in range(len(self.metadataList)): | |
678 | grp.create_dataset(self.metadataList[i], data=getattr(self.dataOut, self.metadataList[i])) |
|
678 | grp.create_dataset(self.metadataList[i], data=getattr(self.dataOut, self.metadataList[i])) | |
679 | return |
|
679 | return | |
680 |
|
680 | |||
681 | def timeFlag(self): |
|
681 | def timeFlag(self): | |
682 | currentTime = self.dataOut.utctime |
|
682 | currentTime = self.dataOut.utctime | |
683 |
|
683 | |||
684 | if self.lastTime is None: |
|
684 | if self.lastTime is None: | |
685 | self.lastTime = currentTime |
|
685 | self.lastTime = currentTime | |
686 |
|
686 | |||
687 | #Day |
|
687 | #Day | |
688 | timeTuple = time.localtime(currentTime) |
|
688 | timeTuple = time.localtime(currentTime) | |
689 | dataDay = timeTuple.tm_yday |
|
689 | dataDay = timeTuple.tm_yday | |
690 |
|
690 | |||
691 | #Time |
|
691 | #Time | |
692 | timeDiff = currentTime - self.lastTime |
|
692 | timeDiff = currentTime - self.lastTime | |
693 |
|
693 | |||
694 | #Si el dia es diferente o si la diferencia entre un dato y otro supera la hora |
|
694 | #Si el dia es diferente o si la diferencia entre un dato y otro supera la hora | |
695 | if dataDay != self.currentDay: |
|
695 | if dataDay != self.currentDay: | |
696 | self.currentDay = dataDay |
|
696 | self.currentDay = dataDay | |
697 | return True |
|
697 | return True | |
698 | elif timeDiff > 3*60*60: |
|
698 | elif timeDiff > 3*60*60: | |
699 | self.lastTime = currentTime |
|
699 | self.lastTime = currentTime | |
700 | return True |
|
700 | return True | |
701 | else: |
|
701 | else: | |
702 | self.lastTime = currentTime |
|
702 | self.lastTime = currentTime | |
703 | return False |
|
703 | return False | |
704 |
|
704 | |||
705 | def setNextFile(self): |
|
705 | def setNextFile(self): | |
706 |
|
706 | |||
707 | ext = self.ext |
|
707 | ext = self.ext | |
708 | path = self.path |
|
708 | path = self.path | |
709 | setFile = self.setFile |
|
709 | setFile = self.setFile | |
710 | mode = self.mode |
|
710 | mode = self.mode | |
711 |
|
711 | |||
712 | timeTuple = time.localtime(self.dataOut.utctime) |
|
712 | timeTuple = time.localtime(self.dataOut.utctime) | |
713 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) |
|
713 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) | |
714 |
|
714 | |||
715 | fullpath = os.path.join( path, subfolder ) |
|
715 | fullpath = os.path.join( path, subfolder ) | |
716 |
|
716 | |||
717 | if os.path.exists(fullpath): |
|
717 | if os.path.exists(fullpath): | |
718 | filesList = os.listdir( fullpath ) |
|
718 | filesList = os.listdir( fullpath ) | |
719 | filesList = [k for k in filesList if 'M' in k] |
|
719 | filesList = [k for k in filesList if 'M' in k] | |
720 | if len( filesList ) > 0: |
|
720 | if len( filesList ) > 0: | |
721 | filesList = sorted( filesList, key=str.lower ) |
|
721 | filesList = sorted( filesList, key=str.lower ) | |
722 | filen = filesList[-1] |
|
722 | filen = filesList[-1] | |
723 | # el filename debera tener el siguiente formato |
|
723 | # el filename debera tener el siguiente formato | |
724 | # 0 1234 567 89A BCDE (hex) |
|
724 | # 0 1234 567 89A BCDE (hex) | |
725 | # x YYYY DDD SSS .ext |
|
725 | # x YYYY DDD SSS .ext | |
726 | if isNumber( filen[8:11] ): |
|
726 | if isNumber( filen[8:11] ): | |
727 | setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file |
|
727 | setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file | |
728 | else: |
|
728 | else: | |
729 | setFile = -1 |
|
729 | setFile = -1 | |
730 | else: |
|
730 | else: | |
731 | setFile = -1 #inicializo mi contador de seteo |
|
731 | setFile = -1 #inicializo mi contador de seteo | |
732 | else: |
|
732 | else: | |
733 | os.makedirs(fullpath) |
|
733 | os.makedirs(fullpath) | |
734 | setFile = -1 #inicializo mi contador de seteo |
|
734 | setFile = -1 #inicializo mi contador de seteo | |
735 |
|
735 | |||
736 | if self.setType is None: |
|
736 | if self.setType is None: | |
737 | setFile += 1 |
|
737 | setFile += 1 | |
738 | file = '%s%4.4d%3.3d%03d%s' % (self.optchar, |
|
738 | file = '%s%4.4d%3.3d%03d%s' % (self.optchar, | |
739 | timeTuple.tm_year, |
|
739 | timeTuple.tm_year, | |
740 | timeTuple.tm_yday, |
|
740 | timeTuple.tm_yday, | |
741 | setFile, |
|
741 | setFile, | |
742 | ext ) |
|
742 | ext ) | |
743 | else: |
|
743 | else: | |
744 | setFile = timeTuple.tm_hour*60+timeTuple.tm_min |
|
744 | setFile = timeTuple.tm_hour*60+timeTuple.tm_min | |
745 | file = '%s%4.4d%3.3d%04d%s' % (self.optchar, |
|
745 | file = '%s%4.4d%3.3d%04d%s' % (self.optchar, | |
746 | timeTuple.tm_year, |
|
746 | timeTuple.tm_year, | |
747 | timeTuple.tm_yday, |
|
747 | timeTuple.tm_yday, | |
748 | setFile, |
|
748 | setFile, | |
749 | ext ) |
|
749 | ext ) | |
750 |
|
750 | |||
751 | filename = os.path.join( path, subfolder, file ) |
|
751 | filename = os.path.join( path, subfolder, file ) | |
752 |
|
752 | |||
753 | #Setting HDF5 File |
|
753 | #Setting HDF5 File | |
754 | fp = h5py.File(filename,'w') |
|
754 | fp = h5py.File(filename,'w') | |
755 | #write metadata |
|
755 | #write metadata | |
756 | self.writeMetadata(fp) |
|
756 | self.writeMetadata(fp) | |
757 | #Write data |
|
757 | #Write data | |
758 | grp = fp.create_group("Data") |
|
758 | grp = fp.create_group("Data") | |
759 | ds = [] |
|
759 | ds = [] | |
760 | data = [] |
|
760 | data = [] | |
761 | dsList = self.dsList |
|
761 | dsList = self.dsList | |
762 | i = 0 |
|
762 | i = 0 | |
763 | while i < len(dsList): |
|
763 | while i < len(dsList): | |
764 | dsInfo = dsList[i] |
|
764 | dsInfo = dsList[i] | |
765 | #One-dimension data |
|
765 | #One-dimension data | |
766 | if dsInfo['mode'] == 0: |
|
766 | if dsInfo['mode'] == 0: | |
767 | ds0 = grp.create_dataset(dsInfo['variable'], (1,1), maxshape=(1,self.blocksPerFile) , chunks = True, dtype=numpy.float64) |
|
767 | ds0 = grp.create_dataset(dsInfo['variable'], (1,1), maxshape=(1,self.blocksPerFile) , chunks = True, dtype=numpy.float64) | |
768 | ds.append(ds0) |
|
768 | ds.append(ds0) | |
769 | data.append([]) |
|
769 | data.append([]) | |
770 | i += 1 |
|
770 | i += 1 | |
771 | continue |
|
771 | continue | |
772 |
|
772 | |||
773 | elif dsInfo['mode'] == 2: |
|
773 | elif dsInfo['mode'] == 2: | |
774 | grp0 = grp.create_group(dsInfo['variable']) |
|
774 | grp0 = grp.create_group(dsInfo['variable']) | |
775 | ds0 = grp0.create_dataset(dsInfo['dsName'], (1,dsInfo['shape']), data = numpy.zeros((1,dsInfo['shape'])) , maxshape=(None,dsInfo['shape']), chunks=True) |
|
775 | ds0 = grp0.create_dataset(dsInfo['dsName'], (1,dsInfo['shape']), data = numpy.zeros((1,dsInfo['shape'])) , maxshape=(None,dsInfo['shape']), chunks=True) | |
776 | ds.append(ds0) |
|
776 | ds.append(ds0) | |
777 | data.append([]) |
|
777 | data.append([]) | |
778 | i += 1 |
|
778 | i += 1 | |
779 | continue |
|
779 | continue | |
780 |
|
780 | |||
781 | elif dsInfo['mode'] == 1: |
|
781 | elif dsInfo['mode'] == 1: | |
782 | grp0 = grp.create_group(dsInfo['variable']) |
|
782 | grp0 = grp.create_group(dsInfo['variable']) | |
783 |
|
783 | |||
784 | for j in range(dsInfo['dsNumber']): |
|
784 | for j in range(dsInfo['dsNumber']): | |
785 | dsInfo = dsList[i] |
|
785 | dsInfo = dsList[i] | |
786 | tableName = dsInfo['dsName'] |
|
786 | tableName = dsInfo['dsName'] | |
787 |
|
787 | |||
788 |
|
788 | |||
789 | if dsInfo['nDim'] == 3: |
|
789 | if dsInfo['nDim'] == 3: | |
790 | shape = dsInfo['shape'].astype(int) |
|
790 | shape = dsInfo['shape'].astype(int) | |
791 | ds0 = grp0.create_dataset(tableName, (shape[0],shape[1],1) , data = numpy.zeros((shape[0],shape[1],1)), maxshape = (None,shape[1],None), chunks=True) |
|
791 | ds0 = grp0.create_dataset(tableName, (shape[0],shape[1],1) , data = numpy.zeros((shape[0],shape[1],1)), maxshape = (None,shape[1],None), chunks=True) | |
792 | else: |
|
792 | else: | |
793 | shape = int(dsInfo['shape']) |
|
793 | shape = int(dsInfo['shape']) | |
794 | ds0 = grp0.create_dataset(tableName, (1,shape), data = numpy.zeros((1,shape)) , maxshape=(None,shape), chunks=True) |
|
794 | ds0 = grp0.create_dataset(tableName, (1,shape), data = numpy.zeros((1,shape)) , maxshape=(None,shape), chunks=True) | |
795 |
|
795 | |||
796 | ds.append(ds0) |
|
796 | ds.append(ds0) | |
797 | data.append([]) |
|
797 | data.append([]) | |
798 | i += 1 |
|
798 | i += 1 | |
799 |
|
799 | |||
800 | fp.flush() |
|
800 | fp.flush() | |
801 | fp.close() |
|
801 | fp.close() | |
802 |
|
802 | |||
803 | log.log('creating file: {}'.format(filename), 'Writing') |
|
803 | log.log('creating file: {}'.format(filename), 'Writing') | |
804 | self.filename = filename |
|
804 | self.filename = filename | |
805 | self.ds = ds |
|
805 | self.ds = ds | |
806 | self.data = data |
|
806 | self.data = data | |
807 | self.firsttime = True |
|
807 | self.firsttime = True | |
808 | self.blockIndex = 0 |
|
808 | self.blockIndex = 0 | |
809 | return |
|
809 | return | |
810 |
|
810 | |||
811 | def putData(self): |
|
811 | def putData(self): | |
812 |
|
812 | |||
813 | if self.blockIndex == self.blocksPerFile or self.timeFlag(): |
|
813 | if self.blockIndex == self.blocksPerFile or self.timeFlag(): | |
814 | self.setNextFile() |
|
814 | self.setNextFile() | |
815 |
|
815 | |||
816 | self.readBlock() |
|
816 | self.readBlock() | |
817 | self.setBlock() #Prepare data to be written |
|
817 | self.setBlock() #Prepare data to be written | |
818 | self.writeBlock() #Write data |
|
818 | self.writeBlock() #Write data | |
819 |
|
819 | |||
820 | return |
|
820 | return | |
821 |
|
821 | |||
822 | def readBlock(self): |
|
822 | def readBlock(self): | |
823 |
|
823 | |||
824 | ''' |
|
824 | ''' | |
825 | data Array configured |
|
825 | data Array configured | |
826 |
|
826 | |||
827 |
|
827 | |||
828 | self.data |
|
828 | self.data | |
829 | ''' |
|
829 | ''' | |
830 | dsList = self.dsList |
|
830 | dsList = self.dsList | |
831 | ds = self.ds |
|
831 | ds = self.ds | |
832 | #Setting HDF5 File |
|
832 | #Setting HDF5 File | |
833 | fp = h5py.File(self.filename,'r+') |
|
833 | fp = h5py.File(self.filename,'r+') | |
834 | grp = fp["Data"] |
|
834 | grp = fp["Data"] | |
835 | ind = 0 |
|
835 | ind = 0 | |
836 |
|
836 | |||
837 | while ind < len(dsList): |
|
837 | while ind < len(dsList): | |
838 | dsInfo = dsList[ind] |
|
838 | dsInfo = dsList[ind] | |
839 |
|
839 | |||
840 | if dsInfo['mode'] == 0: |
|
840 | if dsInfo['mode'] == 0: | |
841 | ds0 = grp[dsInfo['variable']] |
|
841 | ds0 = grp[dsInfo['variable']] | |
842 | ds[ind] = ds0 |
|
842 | ds[ind] = ds0 | |
843 | ind += 1 |
|
843 | ind += 1 | |
844 | else: |
|
844 | else: | |
845 |
|
845 | |||
846 | grp0 = grp[dsInfo['variable']] |
|
846 | grp0 = grp[dsInfo['variable']] | |
847 |
|
847 | |||
848 | for j in range(dsInfo['dsNumber']): |
|
848 | for j in range(dsInfo['dsNumber']): | |
849 | dsInfo = dsList[ind] |
|
849 | dsInfo = dsList[ind] | |
850 | ds0 = grp0[dsInfo['dsName']] |
|
850 | ds0 = grp0[dsInfo['dsName']] | |
851 | ds[ind] = ds0 |
|
851 | ds[ind] = ds0 | |
852 | ind += 1 |
|
852 | ind += 1 | |
853 |
|
853 | |||
854 | self.fp = fp |
|
854 | self.fp = fp | |
855 | self.grp = grp |
|
855 | self.grp = grp | |
856 | self.ds = ds |
|
856 | self.ds = ds | |
857 |
|
857 | |||
858 | return |
|
858 | return | |
859 |
|
859 | |||
860 | def setBlock(self): |
|
860 | def setBlock(self): | |
861 | ''' |
|
861 | ''' | |
862 | data Array configured |
|
862 | data Array configured | |
863 |
|
863 | |||
864 |
|
864 | |||
865 | self.data |
|
865 | self.data | |
866 | ''' |
|
866 | ''' | |
867 | #Creating Arrays |
|
867 | #Creating Arrays | |
868 | dsList = self.dsList |
|
868 | dsList = self.dsList | |
869 | data = self.data |
|
869 | data = self.data | |
870 | ind = 0 |
|
870 | ind = 0 | |
871 |
|
871 | |||
872 | while ind < len(dsList): |
|
872 | while ind < len(dsList): | |
873 | dsInfo = dsList[ind] |
|
873 | dsInfo = dsList[ind] | |
874 | dataAux = getattr(self.dataOut, dsInfo['variable']) |
|
874 | dataAux = getattr(self.dataOut, dsInfo['variable']) | |
875 |
|
875 | |||
876 | mode = dsInfo['mode'] |
|
876 | mode = dsInfo['mode'] | |
877 | nDim = dsInfo['nDim'] |
|
877 | nDim = dsInfo['nDim'] | |
878 |
|
878 | |||
879 | if mode == 0 or mode == 2 or nDim == 1: |
|
879 | if mode == 0 or mode == 2 or nDim == 1: | |
880 | data[ind] = dataAux |
|
880 | data[ind] = dataAux | |
881 | ind += 1 |
|
881 | ind += 1 | |
882 | # elif nDim == 1: |
|
882 | # elif nDim == 1: | |
883 | # data[ind] = numpy.reshape(dataAux,(numpy.size(dataAux),1)) |
|
883 | # data[ind] = numpy.reshape(dataAux,(numpy.size(dataAux),1)) | |
884 | # ind += 1 |
|
884 | # ind += 1 | |
885 | elif nDim == 2: |
|
885 | elif nDim == 2: | |
886 | for j in range(dsInfo['dsNumber']): |
|
886 | for j in range(dsInfo['dsNumber']): | |
887 | data[ind] = dataAux[j,:] |
|
887 | data[ind] = dataAux[j,:] | |
888 | ind += 1 |
|
888 | ind += 1 | |
889 | elif nDim == 3: |
|
889 | elif nDim == 3: | |
890 | for j in range(dsInfo['dsNumber']): |
|
890 | for j in range(dsInfo['dsNumber']): | |
891 | data[ind] = dataAux[:,j,:] |
|
891 | data[ind] = dataAux[:,j,:] | |
892 | ind += 1 |
|
892 | ind += 1 | |
893 |
|
893 | |||
894 | self.data = data |
|
894 | self.data = data | |
895 | return |
|
895 | return | |
896 |
|
896 | |||
897 | def writeBlock(self): |
|
897 | def writeBlock(self): | |
898 | ''' |
|
898 | ''' | |
899 | Saves the block in the HDF5 file |
|
899 | Saves the block in the HDF5 file | |
900 | ''' |
|
900 | ''' | |
901 | dsList = self.dsList |
|
901 | dsList = self.dsList | |
902 |
|
902 | |||
903 | for i in range(len(self.ds)): |
|
903 | for i in range(len(self.ds)): | |
904 | dsInfo = dsList[i] |
|
904 | dsInfo = dsList[i] | |
905 | nDim = dsInfo['nDim'] |
|
905 | nDim = dsInfo['nDim'] | |
906 | mode = dsInfo['mode'] |
|
906 | mode = dsInfo['mode'] | |
907 |
|
907 | |||
908 | # First time |
|
908 | # First time | |
909 | if self.firsttime: |
|
909 | if self.firsttime: | |
910 | if type(self.data[i]) == numpy.ndarray: |
|
910 | if type(self.data[i]) == numpy.ndarray: | |
911 |
|
911 | |||
912 | if nDim == 3: |
|
912 | if nDim == 3: | |
913 | self.data[i] = self.data[i].reshape((self.data[i].shape[0],self.data[i].shape[1],1)) |
|
913 | self.data[i] = self.data[i].reshape((self.data[i].shape[0],self.data[i].shape[1],1)) | |
914 | self.ds[i].resize(self.data[i].shape) |
|
914 | self.ds[i].resize(self.data[i].shape) | |
915 | if mode == 2: |
|
915 | if mode == 2: | |
916 | self.ds[i].resize(self.data[i].shape) |
|
916 | self.ds[i].resize(self.data[i].shape) | |
917 | self.ds[i][:] = self.data[i] |
|
917 | self.ds[i][:] = self.data[i] | |
918 | else: |
|
918 | else: | |
919 |
|
919 | |||
920 | # From second time |
|
920 | # From second time | |
921 | # Meteors! |
|
921 | # Meteors! | |
922 | if mode == 2: |
|
922 | if mode == 2: | |
923 | dataShape = self.data[i].shape |
|
923 | dataShape = self.data[i].shape | |
924 | dsShape = self.ds[i].shape |
|
924 | dsShape = self.ds[i].shape | |
925 | self.ds[i].resize((self.ds[i].shape[0] + dataShape[0],self.ds[i].shape[1])) |
|
925 | self.ds[i].resize((self.ds[i].shape[0] + dataShape[0],self.ds[i].shape[1])) | |
926 | self.ds[i][dsShape[0]:,:] = self.data[i] |
|
926 | self.ds[i][dsShape[0]:,:] = self.data[i] | |
927 | # No dimension |
|
927 | # No dimension | |
928 | elif mode == 0: |
|
928 | elif mode == 0: | |
929 | self.ds[i].resize((self.ds[i].shape[0], self.ds[i].shape[1] + 1)) |
|
929 | self.ds[i].resize((self.ds[i].shape[0], self.ds[i].shape[1] + 1)) | |
930 | self.ds[i][0,-1] = self.data[i] |
|
930 | self.ds[i][0,-1] = self.data[i] | |
931 | # One dimension |
|
931 | # One dimension | |
932 | elif nDim == 1: |
|
932 | elif nDim == 1: | |
933 | self.ds[i].resize((self.ds[i].shape[0] + 1, self.ds[i].shape[1])) |
|
933 | self.ds[i].resize((self.ds[i].shape[0] + 1, self.ds[i].shape[1])) | |
934 | self.ds[i][-1,:] = self.data[i] |
|
934 | self.ds[i][-1,:] = self.data[i] | |
935 | # Two dimension |
|
935 | # Two dimension | |
936 | elif nDim == 2: |
|
936 | elif nDim == 2: | |
937 | self.ds[i].resize((self.ds[i].shape[0] + 1,self.ds[i].shape[1])) |
|
937 | self.ds[i].resize((self.ds[i].shape[0] + 1,self.ds[i].shape[1])) | |
938 | self.ds[i][self.blockIndex,:] = self.data[i] |
|
938 | self.ds[i][self.blockIndex,:] = self.data[i] | |
939 | # Three dimensions |
|
939 | # Three dimensions | |
940 | elif nDim == 3: |
|
940 | elif nDim == 3: | |
941 | self.ds[i].resize((self.ds[i].shape[0],self.ds[i].shape[1],self.ds[i].shape[2]+1)) |
|
941 | self.ds[i].resize((self.ds[i].shape[0],self.ds[i].shape[1],self.ds[i].shape[2]+1)) | |
942 | self.ds[i][:,:,-1] = self.data[i] |
|
942 | self.ds[i][:,:,-1] = self.data[i] | |
943 |
|
943 | |||
944 | self.firsttime = False |
|
944 | self.firsttime = False | |
945 | self.blockIndex += 1 |
|
945 | self.blockIndex += 1 | |
946 |
|
946 | |||
947 | #Close to save changes |
|
947 | #Close to save changes | |
948 | self.fp.flush() |
|
948 | self.fp.flush() | |
949 | self.fp.close() |
|
949 | self.fp.close() | |
950 | return |
|
950 | return | |
951 |
|
951 | |||
952 | def run(self, dataOut, path, blocksPerFile=10, metadataList=None, dataList=None, mode=None, setType=None): |
|
952 | def run(self, dataOut, path, blocksPerFile=10, metadataList=None, dataList=None, mode=None, setType=None): | |
953 |
|
953 | |||
954 | self.dataOut = dataOut |
|
954 | self.dataOut = dataOut | |
955 | if not(self.isConfig): |
|
955 | if not(self.isConfig): | |
956 | self.setup(dataOut, path=path, blocksPerFile=blocksPerFile, |
|
956 | self.setup(dataOut, path=path, blocksPerFile=blocksPerFile, | |
957 | metadataList=metadataList, dataList=dataList, mode=mode, |
|
957 | metadataList=metadataList, dataList=dataList, mode=mode, | |
958 | setType=setType) |
|
958 | setType=setType) | |
959 |
|
959 | |||
960 | self.isConfig = True |
|
960 | self.isConfig = True | |
961 | self.setNextFile() |
|
961 | self.setNextFile() | |
962 |
|
962 | |||
963 | self.putData() |
|
963 | self.putData() | |
964 | return |
|
964 | return | |
965 |
|
965 | |||
966 |
|
966 | |||
967 | @MPDecorator |
|
967 | @MPDecorator | |
968 | class ParameterReader(JRODataReader,ProcessingUnit): |
|
968 | class ParameterReader(JRODataReader,ProcessingUnit): | |
969 | ''' |
|
969 | ''' | |
970 | Reads HDF5 format files |
|
970 | Reads HDF5 format files | |
971 | ''' |
|
971 | ''' | |
972 |
|
972 | |||
973 | ext = ".hdf5" |
|
973 | ext = ".hdf5" | |
974 | optchar = "D" |
|
974 | optchar = "D" | |
975 | timezone = None |
|
975 | timezone = None | |
976 | startTime = None |
|
976 | startTime = None | |
977 | endTime = None |
|
977 | endTime = None | |
978 | fileIndex = None |
|
978 | fileIndex = None | |
979 | blockList = None #List to blocks to be read from the file |
|
979 | blockList = None #List to blocks to be read from the file | |
980 | blocksPerFile = None #Number of blocks to be read |
|
980 | blocksPerFile = None #Number of blocks to be read | |
981 | blockIndex = None |
|
981 | blockIndex = None | |
982 | path = None |
|
982 | path = None | |
983 | #List of Files |
|
983 | #List of Files | |
984 | filenameList = None |
|
984 | filenameList = None | |
985 | datetimeList = None |
|
985 | datetimeList = None | |
986 | #Hdf5 File |
|
986 | #Hdf5 File | |
987 | listMetaname = None |
|
987 | listMetaname = None | |
988 | listMeta = None |
|
988 | listMeta = None | |
989 | listDataname = None |
|
989 | listDataname = None | |
990 | listData = None |
|
990 | listData = None | |
991 | listShapes = None |
|
991 | listShapes = None | |
992 | fp = None |
|
992 | fp = None | |
993 | #dataOut reconstruction |
|
993 | #dataOut reconstruction | |
994 | dataOut = None |
|
994 | dataOut = None | |
995 |
|
995 | |||
996 | def __init__(self): |
|
996 | def __init__(self): | |
997 | ProcessingUnit.__init__(self) |
|
997 | ProcessingUnit.__init__(self) | |
998 | self.dataOut = Parameters() |
|
998 | self.dataOut = Parameters() | |
999 | return |
|
999 | return | |
1000 |
|
1000 | |||
1001 | def setup(self, **kwargs): |
|
1001 | def setup(self, **kwargs): | |
1002 |
|
1002 | |||
1003 | path = kwargs['path'] |
|
1003 | path = kwargs['path'] | |
1004 | startDate = kwargs['startDate'] |
|
1004 | startDate = kwargs['startDate'] | |
1005 | endDate = kwargs['endDate'] |
|
1005 | endDate = kwargs['endDate'] | |
1006 | startTime = kwargs['startTime'] |
|
1006 | startTime = kwargs['startTime'] | |
1007 | endTime = kwargs['endTime'] |
|
1007 | endTime = kwargs['endTime'] | |
1008 | walk = kwargs['walk'] |
|
1008 | walk = kwargs['walk'] | |
1009 | if 'ext' in kwargs: |
|
1009 | if 'ext' in kwargs: | |
1010 | ext = kwargs['ext'] |
|
1010 | ext = kwargs['ext'] | |
1011 | else: |
|
1011 | else: | |
1012 | ext = '.hdf5' |
|
1012 | ext = '.hdf5' | |
1013 | if 'timezone' in kwargs: |
|
1013 | if 'timezone' in kwargs: | |
1014 | self.timezone = kwargs['timezone'] |
|
1014 | self.timezone = kwargs['timezone'] | |
1015 | else: |
|
1015 | else: | |
1016 | self.timezone = 'lt' |
|
1016 | self.timezone = 'lt' | |
1017 |
|
1017 | |||
1018 | print("[Reading] Searching files in offline mode ...") |
|
1018 | print("[Reading] Searching files in offline mode ...") | |
1019 | pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate, |
|
1019 | pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate, | |
1020 | startTime=startTime, endTime=endTime, |
|
1020 | startTime=startTime, endTime=endTime, | |
1021 | ext=ext, walk=walk) |
|
1021 | ext=ext, walk=walk) | |
1022 |
|
1022 | |||
1023 | if not(filenameList): |
|
1023 | if not(filenameList): | |
1024 | print("There is no files into the folder: %s"%(path)) |
|
1024 | print("There is no files into the folder: %s"%(path)) | |
1025 | sys.exit(-1) |
|
1025 | sys.exit(-1) | |
1026 |
|
1026 | |||
1027 | self.fileIndex = -1 |
|
1027 | self.fileIndex = -1 | |
1028 | self.startTime = startTime |
|
1028 | self.startTime = startTime | |
1029 | self.endTime = endTime |
|
1029 | self.endTime = endTime | |
1030 | self.__readMetadata() |
|
1030 | self.__readMetadata() | |
1031 | self.__setNextFileOffline() |
|
1031 | self.__setNextFileOffline() | |
1032 |
|
1032 | |||
1033 | return |
|
1033 | return | |
1034 |
|
1034 | |||
1035 | def searchFilesOffLine(self, path, startDate=None, endDate=None, startTime=datetime.time(0,0,0), endTime=datetime.time(23,59,59), ext='.hdf5', walk=True): |
|
1035 | def searchFilesOffLine(self, path, startDate=None, endDate=None, startTime=datetime.time(0,0,0), endTime=datetime.time(23,59,59), ext='.hdf5', walk=True): | |
1036 |
|
1036 | |||
1037 | expLabel = '' |
|
1037 | expLabel = '' | |
1038 | self.filenameList = [] |
|
1038 | self.filenameList = [] | |
1039 | self.datetimeList = [] |
|
1039 | self.datetimeList = [] | |
1040 | pathList = [] |
|
1040 | pathList = [] | |
1041 | dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True) |
|
1041 | dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True) | |
1042 |
|
1042 | |||
1043 | if dateList == []: |
|
1043 | if dateList == []: | |
1044 | print("[Reading] No *%s files in %s from %s to %s)"%(ext, path, |
|
1044 | print("[Reading] No *%s files in %s from %s to %s)"%(ext, path, | |
1045 | datetime.datetime.combine(startDate,startTime).ctime(), |
|
1045 | datetime.datetime.combine(startDate,startTime).ctime(), | |
1046 | datetime.datetime.combine(endDate,endTime).ctime())) |
|
1046 | datetime.datetime.combine(endDate,endTime).ctime())) | |
1047 |
|
1047 | |||
1048 | return None, None |
|
1048 | return None, None | |
1049 |
|
1049 | |||
1050 | if len(dateList) > 1: |
|
1050 | if len(dateList) > 1: | |
1051 | print("[Reading] %d days were found in date range: %s - %s" %(len(dateList), startDate, endDate)) |
|
1051 | print("[Reading] %d days were found in date range: %s - %s" %(len(dateList), startDate, endDate)) | |
1052 | else: |
|
1052 | else: | |
1053 | print("[Reading] data was found for the date %s" %(dateList[0])) |
|
1053 | print("[Reading] data was found for the date %s" %(dateList[0])) | |
1054 |
|
1054 | |||
1055 | filenameList = [] |
|
1055 | filenameList = [] | |
1056 | datetimeList = [] |
|
1056 | datetimeList = [] | |
1057 |
|
1057 | |||
1058 | for thisPath in pathList: |
|
1058 | for thisPath in pathList: | |
1059 |
|
1059 | |||
1060 | fileList = glob.glob1(thisPath, "*%s" %ext) |
|
1060 | fileList = glob.glob1(thisPath, "*%s" %ext) | |
1061 | fileList.sort() |
|
1061 | fileList.sort() | |
1062 |
|
1062 | |||
1063 | for file in fileList: |
|
1063 | for file in fileList: | |
1064 |
|
1064 | |||
1065 | filename = os.path.join(thisPath,file) |
|
1065 | filename = os.path.join(thisPath,file) | |
1066 |
|
1066 | |||
1067 | if not isFileInDateRange(filename, startDate, endDate): |
|
1067 | if not isFileInDateRange(filename, startDate, endDate): | |
1068 | continue |
|
1068 | continue | |
1069 |
|
1069 | |||
1070 | thisDatetime = self.__isFileInTimeRange(filename, startDate, endDate, startTime, endTime) |
|
1070 | thisDatetime = self.__isFileInTimeRange(filename, startDate, endDate, startTime, endTime) | |
1071 |
|
1071 | |||
1072 | if not(thisDatetime): |
|
1072 | if not(thisDatetime): | |
1073 | continue |
|
1073 | continue | |
1074 |
|
1074 | |||
1075 | filenameList.append(filename) |
|
1075 | filenameList.append(filename) | |
1076 | datetimeList.append(thisDatetime) |
|
1076 | datetimeList.append(thisDatetime) | |
1077 |
|
1077 | |||
1078 | if not(filenameList): |
|
1078 | if not(filenameList): | |
1079 | print("[Reading] Any file was found int time range %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())) |
|
1079 | print("[Reading] Any file was found int time range %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())) | |
1080 | return None, None |
|
1080 | return None, None | |
1081 |
|
1081 | |||
1082 | print("[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)) |
|
1082 | print("[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)) | |
1083 | print() |
|
1083 | print() | |
1084 |
|
1084 | |||
1085 | self.filenameList = filenameList |
|
1085 | self.filenameList = filenameList | |
1086 | self.datetimeList = datetimeList |
|
1086 | self.datetimeList = datetimeList | |
1087 |
|
1087 | |||
1088 | return pathList, filenameList |
|
1088 | return pathList, filenameList | |
1089 |
|
1089 | |||
1090 | def __isFileInTimeRange(self,filename, startDate, endDate, startTime, endTime): |
|
1090 | def __isFileInTimeRange(self,filename, startDate, endDate, startTime, endTime): | |
1091 |
|
1091 | |||
1092 | """ |
|
1092 | """ | |
1093 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. |
|
1093 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. | |
1094 |
|
1094 | |||
1095 | Inputs: |
|
1095 | Inputs: | |
1096 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
1096 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) | |
1097 | startDate : fecha inicial del rango seleccionado en formato datetime.date |
|
1097 | startDate : fecha inicial del rango seleccionado en formato datetime.date | |
1098 | endDate : fecha final del rango seleccionado en formato datetime.date |
|
1098 | endDate : fecha final del rango seleccionado en formato datetime.date | |
1099 | startTime : tiempo inicial del rango seleccionado en formato datetime.time |
|
1099 | startTime : tiempo inicial del rango seleccionado en formato datetime.time | |
1100 | endTime : tiempo final del rango seleccionado en formato datetime.time |
|
1100 | endTime : tiempo final del rango seleccionado en formato datetime.time | |
1101 |
|
1101 | |||
1102 | Return: |
|
1102 | Return: | |
1103 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
1103 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de | |
1104 | fecha especificado, de lo contrario retorna False. |
|
1104 | fecha especificado, de lo contrario retorna False. | |
1105 |
|
1105 | |||
1106 | Excepciones: |
|
1106 | Excepciones: | |
1107 | Si el archivo no existe o no puede ser abierto |
|
1107 | Si el archivo no existe o no puede ser abierto | |
1108 | Si la cabecera no puede ser leida. |
|
1108 | Si la cabecera no puede ser leida. | |
1109 |
|
1109 | |||
1110 | """ |
|
1110 | """ | |
1111 |
|
1111 | |||
1112 | try: |
|
1112 | try: | |
1113 | fp = h5py.File(filename, 'r') |
|
1113 | fp = h5py.File(filename, 'r') | |
1114 | grp1 = fp['Data'] |
|
1114 | grp1 = fp['Data'] | |
1115 |
|
1115 | |||
1116 | except IOError: |
|
1116 | except IOError: | |
1117 | traceback.print_exc() |
|
1117 | traceback.print_exc() | |
1118 | raise IOError("The file %s can't be opened" %(filename)) |
|
1118 | raise IOError("The file %s can't be opened" %(filename)) | |
1119 | #In case has utctime attribute |
|
1119 | #In case has utctime attribute | |
1120 | grp2 = grp1['utctime'] |
|
1120 | grp2 = grp1['utctime'] | |
1121 | thisUtcTime = grp2.value[0] |
|
1121 | thisUtcTime = grp2.value[0] | |
1122 |
|
1122 | |||
1123 | fp.close() |
|
1123 | fp.close() | |
1124 |
|
1124 | |||
1125 | if self.timezone == 'lt': |
|
1125 | if self.timezone == 'lt': | |
1126 | thisUtcTime -= 5*3600 |
|
1126 | thisUtcTime -= 5*3600 | |
1127 |
|
1127 | |||
1128 | thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime + 5*3600) |
|
1128 | thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime + 5*3600) | |
1129 | thisDate = thisDatetime.date() |
|
1129 | thisDate = thisDatetime.date() | |
1130 | thisTime = thisDatetime.time() |
|
1130 | thisTime = thisDatetime.time() | |
1131 |
|
1131 | |||
1132 | startUtcTime = (datetime.datetime.combine(thisDate,startTime)- datetime.datetime(1970, 1, 1)).total_seconds() |
|
1132 | startUtcTime = (datetime.datetime.combine(thisDate,startTime)- datetime.datetime(1970, 1, 1)).total_seconds() | |
1133 | endUtcTime = (datetime.datetime.combine(thisDate,endTime)- datetime.datetime(1970, 1, 1)).total_seconds() |
|
1133 | endUtcTime = (datetime.datetime.combine(thisDate,endTime)- datetime.datetime(1970, 1, 1)).total_seconds() | |
1134 |
|
1134 | |||
1135 | #General case |
|
1135 | #General case | |
1136 | # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o |
|
1136 | # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o | |
1137 | #-----------o----------------------------o----------- |
|
1137 | #-----------o----------------------------o----------- | |
1138 | # startTime endTime |
|
1138 | # startTime endTime | |
1139 |
|
1139 | |||
1140 | if endTime >= startTime: |
|
1140 | if endTime >= startTime: | |
1141 | thisUtcLog = numpy.logical_and(thisUtcTime > startUtcTime, thisUtcTime < endUtcTime) |
|
1141 | thisUtcLog = numpy.logical_and(thisUtcTime > startUtcTime, thisUtcTime < endUtcTime) | |
1142 | if numpy.any(thisUtcLog): #If there is one block between the hours mentioned |
|
1142 | if numpy.any(thisUtcLog): #If there is one block between the hours mentioned | |
1143 | return thisDatetime |
|
1143 | return thisDatetime | |
1144 | return None |
|
1144 | return None | |
1145 |
|
1145 | |||
1146 | #If endTime < startTime then endTime belongs to the next day |
|
1146 | #If endTime < startTime then endTime belongs to the next day | |
1147 | #<<<<<<<<<<<o o>>>>>>>>>>> |
|
1147 | #<<<<<<<<<<<o o>>>>>>>>>>> | |
1148 | #-----------o----------------------------o----------- |
|
1148 | #-----------o----------------------------o----------- | |
1149 | # endTime startTime |
|
1149 | # endTime startTime | |
1150 |
|
1150 | |||
1151 | if (thisDate == startDate) and numpy.all(thisUtcTime < startUtcTime): |
|
1151 | if (thisDate == startDate) and numpy.all(thisUtcTime < startUtcTime): | |
1152 | return None |
|
1152 | return None | |
1153 |
|
1153 | |||
1154 | if (thisDate == endDate) and numpy.all(thisUtcTime > endUtcTime): |
|
1154 | if (thisDate == endDate) and numpy.all(thisUtcTime > endUtcTime): | |
1155 | return None |
|
1155 | return None | |
1156 |
|
1156 | |||
1157 | if numpy.all(thisUtcTime < startUtcTime) and numpy.all(thisUtcTime > endUtcTime): |
|
1157 | if numpy.all(thisUtcTime < startUtcTime) and numpy.all(thisUtcTime > endUtcTime): | |
1158 | return None |
|
1158 | return None | |
1159 |
|
1159 | |||
1160 | return thisDatetime |
|
1160 | return thisDatetime | |
1161 |
|
1161 | |||
1162 | def __setNextFileOffline(self): |
|
1162 | def __setNextFileOffline(self): | |
1163 |
|
1163 | |||
1164 | self.fileIndex += 1 |
|
1164 | self.fileIndex += 1 | |
1165 | idFile = self.fileIndex |
|
1165 | idFile = self.fileIndex | |
1166 |
|
1166 | |||
1167 | if not(idFile < len(self.filenameList)): |
|
1167 | if not(idFile < len(self.filenameList)): | |
1168 | self.dataOut.error = 'No more files' |
|
1168 | self.dataOut.error = 'No more files' | |
1169 | return 0 |
|
1169 | return 0 | |
1170 |
|
1170 | |||
1171 | filename = self.filenameList[idFile] |
|
1171 | filename = self.filenameList[idFile] | |
1172 | self.fp = h5py.File(filename, 'r') |
|
1172 | self.fp = h5py.File(filename, 'r') | |
1173 | self.filename = filename |
|
1173 | self.filename = filename | |
1174 |
|
1174 | |||
1175 | print("Setting the file: %s"%self.filename) |
|
1175 | print("Setting the file: %s"%self.filename) | |
1176 |
|
1176 | |||
1177 | self.__setBlockList() |
|
1177 | self.__setBlockList() | |
1178 | self.__readData() |
|
1178 | self.__readData() | |
1179 | self.blockIndex = 0 |
|
1179 | self.blockIndex = 0 | |
1180 | return 1 |
|
1180 | return 1 | |
1181 |
|
1181 | |||
1182 | def __setBlockList(self): |
|
1182 | def __setBlockList(self): | |
1183 | ''' |
|
1183 | ''' | |
1184 | Selects the data within the times defined |
|
1184 | Selects the data within the times defined | |
1185 |
|
1185 | |||
1186 | self.fp |
|
1186 | self.fp | |
1187 | self.startTime |
|
1187 | self.startTime | |
1188 | self.endTime |
|
1188 | self.endTime | |
1189 | self.blockList |
|
1189 | self.blockList | |
1190 | self.blocksPerFile |
|
1190 | self.blocksPerFile | |
1191 |
|
1191 | |||
1192 | ''' |
|
1192 | ''' | |
1193 | fp = self.fp |
|
1193 | fp = self.fp | |
1194 | startTime = self.startTime |
|
1194 | startTime = self.startTime | |
1195 | endTime = self.endTime |
|
1195 | endTime = self.endTime | |
1196 |
|
1196 | |||
1197 | grp = fp['Data'] |
|
1197 | grp = fp['Data'] | |
1198 | thisUtcTime = grp['utctime'].value |
|
1198 | thisUtcTime = grp['utctime'].value | |
1199 |
|
1199 | |||
1200 | if self.timezone == 'lt': |
|
1200 | if self.timezone == 'lt': | |
1201 | thisUtcTime -= 5*3600 |
|
1201 | thisUtcTime -= 5*3600 | |
1202 |
|
1202 | |||
1203 | thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0] + 5*3600) |
|
1203 | thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0] + 5*3600) | |
1204 |
|
1204 | |||
1205 | thisDate = thisDatetime.date() |
|
1205 | thisDate = thisDatetime.date() | |
1206 | thisTime = thisDatetime.time() |
|
1206 | thisTime = thisDatetime.time() | |
1207 |
|
1207 | |||
1208 | startUtcTime = (datetime.datetime.combine(thisDate,startTime) - datetime.datetime(1970, 1, 1)).total_seconds() |
|
1208 | startUtcTime = (datetime.datetime.combine(thisDate,startTime) - datetime.datetime(1970, 1, 1)).total_seconds() | |
1209 | endUtcTime = (datetime.datetime.combine(thisDate,endTime) - datetime.datetime(1970, 1, 1)).total_seconds() |
|
1209 | endUtcTime = (datetime.datetime.combine(thisDate,endTime) - datetime.datetime(1970, 1, 1)).total_seconds() | |
1210 |
|
1210 | |||
1211 | ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0] |
|
1211 | ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0] | |
1212 |
|
1212 | |||
1213 | self.blockList = ind |
|
1213 | self.blockList = ind | |
1214 | self.blocksPerFile = len(ind) |
|
1214 | self.blocksPerFile = len(ind) | |
1215 | return |
|
1215 | return | |
1216 |
|
1216 | |||
1217 | def __readMetadata(self): |
|
1217 | def __readMetadata(self): | |
1218 | ''' |
|
1218 | ''' | |
1219 | Reads Metadata |
|
1219 | Reads Metadata | |
1220 | ''' |
|
1220 | ''' | |
1221 |
|
1221 | |||
1222 | filename = self.filenameList[0] |
|
1222 | filename = self.filenameList[0] | |
1223 | fp = h5py.File(filename, 'r') |
|
1223 | fp = h5py.File(filename, 'r') | |
1224 | gp = fp['Metadata'] |
|
1224 | gp = fp['Metadata'] | |
1225 | listMetaname = [] |
|
1225 | listMetaname = [] | |
1226 | listMetadata = [] |
|
1226 | listMetadata = [] | |
1227 |
|
1227 | |||
1228 | for item in list(gp.items()): |
|
1228 | for item in list(gp.items()): | |
1229 | name = item[0] |
|
1229 | name = item[0] | |
1230 |
|
1230 | |||
1231 | if name=='variables': |
|
1231 | if name=='variables': | |
1232 | table = gp[name][:] |
|
1232 | table = gp[name][:] | |
1233 | listShapes = {} |
|
1233 | listShapes = {} | |
1234 | for shapes in table: |
|
1234 | for shapes in table: | |
1235 | listShapes[shapes[0].decode()] = numpy.array([shapes[1]]) |
|
1235 | listShapes[shapes[0].decode()] = numpy.array([shapes[1]]) | |
1236 | else: |
|
1236 | else: | |
1237 | data = gp[name].value |
|
1237 | data = gp[name].value | |
1238 | listMetaname.append(name) |
|
1238 | listMetaname.append(name) | |
1239 | listMetadata.append(data) |
|
1239 | listMetadata.append(data) | |
1240 |
|
1240 | |||
1241 | self.listShapes = listShapes |
|
1241 | self.listShapes = listShapes | |
1242 | self.listMetaname = listMetaname |
|
1242 | self.listMetaname = listMetaname | |
1243 | self.listMeta = listMetadata |
|
1243 | self.listMeta = listMetadata | |
1244 |
|
1244 | |||
1245 | fp.close() |
|
1245 | fp.close() | |
1246 | return |
|
1246 | return | |
1247 |
|
1247 | |||
1248 | def __readData(self): |
|
1248 | def __readData(self): | |
1249 |
|
1249 | |||
1250 | grp = self.fp['Data'] |
|
1250 | grp = self.fp['Data'] | |
1251 | listdataname = [] |
|
1251 | listdataname = [] | |
1252 | listdata = [] |
|
1252 | listdata = [] | |
1253 |
|
1253 | |||
1254 | for item in list(grp.items()): |
|
1254 | for item in list(grp.items()): | |
1255 | name = item[0] |
|
1255 | name = item[0] | |
1256 | listdataname.append(name) |
|
1256 | listdataname.append(name) | |
1257 | dim = self.listShapes[name][0] |
|
1257 | dim = self.listShapes[name][0] | |
1258 | if dim == 0: |
|
1258 | if dim == 0: | |
1259 | array = grp[name].value |
|
1259 | array = grp[name].value | |
1260 | else: |
|
1260 | else: | |
1261 | array = [] |
|
1261 | array = [] | |
1262 | for i in range(dim): |
|
1262 | for i in range(dim): | |
1263 | array.append(grp[name]['table{:02d}'.format(i)].value) |
|
1263 | array.append(grp[name]['table{:02d}'.format(i)].value) | |
1264 | array = numpy.array(array) |
|
1264 | array = numpy.array(array) | |
1265 |
|
1265 | |||
1266 | listdata.append(array) |
|
1266 | listdata.append(array) | |
1267 |
|
1267 | |||
1268 | self.listDataname = listdataname |
|
1268 | self.listDataname = listdataname | |
1269 | self.listData = listdata |
|
1269 | self.listData = listdata | |
1270 | return |
|
1270 | return | |
1271 |
|
1271 | |||
1272 | def getData(self): |
|
1272 | def getData(self): | |
1273 |
|
1273 | |||
1274 | for i in range(len(self.listMeta)): |
|
1274 | for i in range(len(self.listMeta)): | |
1275 | setattr(self.dataOut, self.listMetaname[i], self.listMeta[i]) |
|
1275 | setattr(self.dataOut, self.listMetaname[i], self.listMeta[i]) | |
1276 |
|
1276 | |||
1277 | for j in range(len(self.listData)): |
|
1277 | for j in range(len(self.listData)): | |
1278 | dim = self.listShapes[self.listDataname[j]][0] |
|
1278 | dim = self.listShapes[self.listDataname[j]][0] | |
1279 | if dim == 0: |
|
1279 | if dim == 0: | |
1280 | setattr(self.dataOut, self.listDataname[j], self.listData[j][self.blockIndex]) |
|
1280 | setattr(self.dataOut, self.listDataname[j], self.listData[j][self.blockIndex]) | |
1281 | else: |
|
1281 | else: | |
1282 | setattr(self.dataOut, self.listDataname[j], self.listData[j][:,self.blockIndex]) |
|
1282 | setattr(self.dataOut, self.listDataname[j], self.listData[j][:,self.blockIndex]) | |
1283 |
|
1283 | |||
1284 | self.dataOut.flagNoData = False |
|
1284 | self.dataOut.flagNoData = False | |
1285 | self.blockIndex += 1 |
|
1285 | self.blockIndex += 1 | |
1286 |
|
1286 | |||
1287 | return |
|
1287 | return | |
1288 |
|
1288 | |||
1289 | def run(self, **kwargs): |
|
1289 | def run(self, **kwargs): | |
1290 |
|
1290 | |||
1291 | if not(self.isConfig): |
|
1291 | if not(self.isConfig): | |
1292 | self.setup(**kwargs) |
|
1292 | self.setup(**kwargs) | |
1293 | self.isConfig = True |
|
1293 | self.isConfig = True | |
1294 |
|
1294 | |||
1295 | if self.blockIndex == self.blocksPerFile: |
|
1295 | if self.blockIndex == self.blocksPerFile: | |
1296 | if not(self.__setNextFileOffline()): |
|
1296 | if not(self.__setNextFileOffline()): | |
1297 | self.dataOut.flagNoData = True |
|
1297 | self.dataOut.flagNoData = True | |
1298 | return 0 |
|
1298 | return 0 | |
1299 |
|
1299 | |||
1300 | self.getData() |
|
1300 | self.getData() | |
1301 |
|
1301 | |||
1302 | return |
|
1302 | return | |
1303 |
|
1303 | |||
1304 | @MPDecorator |
|
1304 | @MPDecorator | |
1305 | class ParameterWriter(Operation): |
|
1305 | class ParameterWriter(Operation): | |
1306 | ''' |
|
1306 | ''' | |
1307 | HDF5 Writer, stores parameters data in HDF5 format files |
|
1307 | HDF5 Writer, stores parameters data in HDF5 format files | |
1308 |
|
1308 | |||
1309 | path: path where the files will be stored |
|
1309 | path: path where the files will be stored | |
1310 | blocksPerFile: number of blocks that will be saved in per HDF5 format file |
|
1310 | blocksPerFile: number of blocks that will be saved in per HDF5 format file | |
1311 | mode: selects the data stacking mode: '0' channels, '1' parameters, '3' table (for meteors) |
|
1311 | mode: selects the data stacking mode: '0' channels, '1' parameters, '3' table (for meteors) | |
1312 | metadataList: list of attributes that will be stored as metadata |
|
1312 | metadataList: list of attributes that will be stored as metadata | |
1313 | dataList: list of attributes that will be stores as data |
|
1313 | dataList: list of attributes that will be stores as data | |
1314 | ''' |
|
1314 | ''' | |
1315 |
|
1315 | |||
1316 |
|
1316 | |||
1317 | ext = ".hdf5" |
|
1317 | ext = ".hdf5" | |
1318 | optchar = "D" |
|
1318 | optchar = "D" | |
1319 | metaoptchar = "M" |
|
1319 | metaoptchar = "M" | |
1320 | metaFile = None |
|
1320 | metaFile = None | |
1321 | filename = None |
|
1321 | filename = None | |
1322 | path = None |
|
1322 | path = None | |
1323 | setFile = None |
|
1323 | setFile = None | |
1324 | fp = None |
|
1324 | fp = None | |
1325 | grp = None |
|
1325 | grp = None | |
1326 | ds = None |
|
1326 | ds = None | |
1327 | firsttime = True |
|
1327 | firsttime = True | |
1328 | #Configurations |
|
1328 | #Configurations | |
1329 | blocksPerFile = None |
|
1329 | blocksPerFile = None | |
1330 | blockIndex = None |
|
1330 | blockIndex = None | |
1331 | dataOut = None |
|
1331 | dataOut = None | |
1332 | #Data Arrays |
|
1332 | #Data Arrays | |
1333 | dataList = None |
|
1333 | dataList = None | |
1334 | metadataList = None |
|
1334 | metadataList = None | |
1335 | dsList = None #List of dictionaries with dataset properties |
|
1335 | dsList = None #List of dictionaries with dataset properties | |
1336 | tableDim = None |
|
1336 | tableDim = None | |
1337 | dtype = [('name', 'S20'),('nDim', 'i')] |
|
1337 | dtype = [('name', 'S20'),('nDim', 'i')] | |
1338 | currentDay = None |
|
1338 | currentDay = None | |
1339 | lastTime = None |
|
1339 | lastTime = None | |
1340 |
|
1340 | |||
1341 | def __init__(self): |
|
1341 | def __init__(self): | |
1342 |
|
1342 | |||
1343 | Operation.__init__(self) |
|
1343 | Operation.__init__(self) | |
1344 | return |
|
1344 | return | |
1345 |
|
1345 | |||
1346 | def setup(self, path=None, blocksPerFile=10, metadataList=None, dataList=None, setType=None): |
|
1346 | def setup(self, path=None, blocksPerFile=10, metadataList=None, dataList=None, setType=None): | |
1347 | self.path = path |
|
1347 | self.path = path | |
1348 | self.blocksPerFile = blocksPerFile |
|
1348 | self.blocksPerFile = blocksPerFile | |
1349 | self.metadataList = metadataList |
|
1349 | self.metadataList = metadataList | |
1350 | self.dataList = dataList |
|
1350 | self.dataList = dataList | |
1351 | self.setType = setType |
|
1351 | self.setType = setType | |
1352 |
|
1352 | |||
1353 | tableList = [] |
|
1353 | tableList = [] | |
1354 | dsList = [] |
|
1354 | dsList = [] | |
1355 |
|
1355 | |||
1356 | for i in range(len(self.dataList)): |
|
1356 | for i in range(len(self.dataList)): | |
1357 | dsDict = {} |
|
1357 | dsDict = {} | |
1358 | dataAux = getattr(self.dataOut, self.dataList[i]) |
|
1358 | dataAux = getattr(self.dataOut, self.dataList[i]) | |
1359 | dsDict['variable'] = self.dataList[i] |
|
1359 | dsDict['variable'] = self.dataList[i] | |
1360 |
|
1360 | |||
1361 | if dataAux is None: |
|
1361 | if dataAux is None: | |
1362 | continue |
|
1362 | continue | |
1363 | elif isinstance(dataAux, (int, float, numpy.integer, numpy.float)): |
|
1363 | elif isinstance(dataAux, (int, float, numpy.integer, numpy.float)): | |
1364 | dsDict['nDim'] = 0 |
|
1364 | dsDict['nDim'] = 0 | |
1365 | else: |
|
1365 | else: | |
1366 | dsDict['nDim'] = len(dataAux.shape) |
|
1366 | dsDict['nDim'] = len(dataAux.shape) | |
1367 | dsDict['shape'] = dataAux.shape |
|
1367 | dsDict['shape'] = dataAux.shape | |
1368 | dsDict['dsNumber'] = dataAux.shape[0] |
|
1368 | dsDict['dsNumber'] = dataAux.shape[0] | |
1369 |
|
1369 | |||
1370 | dsList.append(dsDict) |
|
1370 | dsList.append(dsDict) | |
1371 | tableList.append((self.dataList[i], dsDict['nDim'])) |
|
1371 | tableList.append((self.dataList[i], dsDict['nDim'])) | |
1372 |
|
1372 | |||
1373 | self.dsList = dsList |
|
1373 | self.dsList = dsList | |
1374 | self.tableDim = numpy.array(tableList, dtype=self.dtype) |
|
1374 | self.tableDim = numpy.array(tableList, dtype=self.dtype) | |
1375 | self.currentDay = self.dataOut.datatime.date() |
|
1375 | self.currentDay = self.dataOut.datatime.date() | |
1376 |
|
1376 | |||
1377 | def timeFlag(self): |
|
1377 | def timeFlag(self): | |
1378 | currentTime = self.dataOut.utctime |
|
1378 | currentTime = self.dataOut.utctime | |
1379 | timeTuple = time.localtime(currentTime) |
|
1379 | timeTuple = time.localtime(currentTime) | |
1380 | dataDay = timeTuple.tm_yday |
|
1380 | dataDay = timeTuple.tm_yday | |
1381 |
|
1381 | |||
1382 | if self.lastTime is None: |
|
1382 | if self.lastTime is None: | |
1383 | self.lastTime = currentTime |
|
1383 | self.lastTime = currentTime | |
1384 | self.currentDay = dataDay |
|
1384 | self.currentDay = dataDay | |
1385 | return False |
|
1385 | return False | |
1386 |
|
1386 | |||
1387 | timeDiff = currentTime - self.lastTime |
|
1387 | timeDiff = currentTime - self.lastTime | |
1388 |
|
1388 | |||
1389 | #Si el dia es diferente o si la diferencia entre un dato y otro supera la hora |
|
1389 | #Si el dia es diferente o si la diferencia entre un dato y otro supera la hora | |
1390 | if dataDay != self.currentDay: |
|
1390 | if dataDay != self.currentDay: | |
1391 | self.currentDay = dataDay |
|
1391 | self.currentDay = dataDay | |
1392 | return True |
|
1392 | return True | |
1393 | elif timeDiff > 3*60*60: |
|
1393 | elif timeDiff > 3*60*60: | |
1394 | self.lastTime = currentTime |
|
1394 | self.lastTime = currentTime | |
1395 | return True |
|
1395 | return True | |
1396 | else: |
|
1396 | else: | |
1397 | self.lastTime = currentTime |
|
1397 | self.lastTime = currentTime | |
1398 | return False |
|
1398 | return False | |
1399 |
|
1399 | |||
1400 | def run(self, dataOut, path, blocksPerFile=10, metadataList=None, dataList=None, setType=None): |
|
1400 | def run(self, dataOut, path, blocksPerFile=10, metadataList=None, dataList=None, setType=None): | |
1401 |
|
1401 | |||
1402 | self.dataOut = dataOut |
|
1402 | self.dataOut = dataOut | |
1403 | if not(self.isConfig): |
|
1403 | if not(self.isConfig): | |
1404 | self.setup(path=path, blocksPerFile=blocksPerFile, |
|
1404 | self.setup(path=path, blocksPerFile=blocksPerFile, | |
1405 | metadataList=metadataList, dataList=dataList, |
|
1405 | metadataList=metadataList, dataList=dataList, | |
1406 | setType=setType) |
|
1406 | setType=setType) | |
1407 |
|
1407 | |||
1408 | self.isConfig = True |
|
1408 | self.isConfig = True | |
1409 | self.setNextFile() |
|
1409 | self.setNextFile() | |
1410 |
|
1410 | |||
1411 | self.putData() |
|
1411 | self.putData() | |
1412 | return |
|
1412 | return | |
1413 |
|
1413 | |||
1414 | def setNextFile(self): |
|
1414 | def setNextFile(self): | |
1415 |
|
1415 | |||
1416 | ext = self.ext |
|
1416 | ext = self.ext | |
1417 | path = self.path |
|
1417 | path = self.path | |
1418 | setFile = self.setFile |
|
1418 | setFile = self.setFile | |
1419 |
|
1419 | |||
1420 | timeTuple = time.localtime(self.dataOut.utctime) |
|
1420 | timeTuple = time.localtime(self.dataOut.utctime) | |
1421 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) |
|
1421 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) | |
1422 | fullpath = os.path.join(path, subfolder) |
|
1422 | fullpath = os.path.join(path, subfolder) | |
1423 |
|
1423 | |||
1424 | if os.path.exists(fullpath): |
|
1424 | if os.path.exists(fullpath): | |
1425 | filesList = os.listdir(fullpath) |
|
1425 | filesList = os.listdir(fullpath) | |
1426 | filesList = [k for k in filesList if k.startswith(self.optchar)] |
|
1426 | filesList = [k for k in filesList if k.startswith(self.optchar)] | |
1427 | if len( filesList ) > 0: |
|
1427 | if len( filesList ) > 0: | |
1428 | filesList = sorted(filesList, key=str.lower) |
|
1428 | filesList = sorted(filesList, key=str.lower) | |
1429 | filen = filesList[-1] |
|
1429 | filen = filesList[-1] | |
1430 | # el filename debera tener el siguiente formato |
|
1430 | # el filename debera tener el siguiente formato | |
1431 | # 0 1234 567 89A BCDE (hex) |
|
1431 | # 0 1234 567 89A BCDE (hex) | |
1432 | # x YYYY DDD SSS .ext |
|
1432 | # x YYYY DDD SSS .ext | |
1433 | if isNumber(filen[8:11]): |
|
1433 | if isNumber(filen[8:11]): | |
1434 | setFile = int(filen[8:11]) #inicializo mi contador de seteo al seteo del ultimo file |
|
1434 | setFile = int(filen[8:11]) #inicializo mi contador de seteo al seteo del ultimo file | |
1435 | else: |
|
1435 | else: | |
1436 | setFile = -1 |
|
1436 | setFile = -1 | |
1437 | else: |
|
1437 | else: | |
1438 | setFile = -1 #inicializo mi contador de seteo |
|
1438 | setFile = -1 #inicializo mi contador de seteo | |
1439 | else: |
|
1439 | else: | |
1440 | os.makedirs(fullpath) |
|
1440 | os.makedirs(fullpath) | |
1441 | setFile = -1 #inicializo mi contador de seteo |
|
1441 | setFile = -1 #inicializo mi contador de seteo | |
1442 |
|
1442 | |||
1443 | if self.setType is None: |
|
1443 | if self.setType is None: | |
1444 | setFile += 1 |
|
1444 | setFile += 1 | |
1445 | file = '%s%4.4d%3.3d%03d%s' % (self.optchar, |
|
1445 | file = '%s%4.4d%3.3d%03d%s' % (self.optchar, | |
1446 | timeTuple.tm_year, |
|
1446 | timeTuple.tm_year, | |
1447 | timeTuple.tm_yday, |
|
1447 | timeTuple.tm_yday, | |
1448 | setFile, |
|
1448 | setFile, | |
1449 | ext ) |
|
1449 | ext ) | |
1450 | else: |
|
1450 | else: | |
1451 | setFile = timeTuple.tm_hour*60+timeTuple.tm_min |
|
1451 | setFile = timeTuple.tm_hour*60+timeTuple.tm_min | |
1452 | file = '%s%4.4d%3.3d%04d%s' % (self.optchar, |
|
1452 | file = '%s%4.4d%3.3d%04d%s' % (self.optchar, | |
1453 | timeTuple.tm_year, |
|
1453 | timeTuple.tm_year, | |
1454 | timeTuple.tm_yday, |
|
1454 | timeTuple.tm_yday, | |
1455 | setFile, |
|
1455 | setFile, | |
1456 | ext ) |
|
1456 | ext ) | |
1457 |
|
1457 | |||
1458 | self.filename = os.path.join( path, subfolder, file ) |
|
1458 | self.filename = os.path.join( path, subfolder, file ) | |
1459 |
|
1459 | |||
1460 | #Setting HDF5 File |
|
1460 | #Setting HDF5 File | |
1461 | self.fp = h5py.File(self.filename, 'w') |
|
1461 | self.fp = h5py.File(self.filename, 'w') | |
1462 | #write metadata |
|
1462 | #write metadata | |
1463 | self.writeMetadata(self.fp) |
|
1463 | self.writeMetadata(self.fp) | |
1464 | #Write data |
|
1464 | #Write data | |
1465 | self.writeData(self.fp) |
|
1465 | self.writeData(self.fp) | |
1466 |
|
1466 | |||
1467 | def writeMetadata(self, fp): |
|
1467 | def writeMetadata(self, fp): | |
1468 |
|
1468 | |||
1469 | grp = fp.create_group("Metadata") |
|
1469 | grp = fp.create_group("Metadata") | |
1470 | grp.create_dataset('variables', data=self.tableDim, dtype=self.dtype) |
|
1470 | grp.create_dataset('variables', data=self.tableDim, dtype=self.dtype) | |
1471 |
|
1471 | |||
1472 | for i in range(len(self.metadataList)): |
|
1472 | for i in range(len(self.metadataList)): | |
1473 | if not hasattr(self.dataOut, self.metadataList[i]): |
|
1473 | if not hasattr(self.dataOut, self.metadataList[i]): | |
1474 | log.warning('Metadata: `{}` not found'.format(self.metadataList[i]), self.name) |
|
1474 | log.warning('Metadata: `{}` not found'.format(self.metadataList[i]), self.name) | |
1475 | continue |
|
1475 | continue | |
1476 | value = getattr(self.dataOut, self.metadataList[i]) |
|
1476 | value = getattr(self.dataOut, self.metadataList[i]) | |
1477 | grp.create_dataset(self.metadataList[i], data=value) |
|
1477 | grp.create_dataset(self.metadataList[i], data=value) | |
1478 | return |
|
1478 | return | |
1479 |
|
1479 | |||
1480 | def writeData(self, fp): |
|
1480 | def writeData(self, fp): | |
1481 |
|
1481 | |||
1482 | grp = fp.create_group("Data") |
|
1482 | grp = fp.create_group("Data") | |
1483 | dtsets = [] |
|
1483 | dtsets = [] | |
1484 | data = [] |
|
1484 | data = [] | |
1485 |
|
1485 | |||
1486 | for dsInfo in self.dsList: |
|
1486 | for dsInfo in self.dsList: | |
1487 | if dsInfo['nDim'] == 0: |
|
1487 | if dsInfo['nDim'] == 0: | |
1488 | ds = grp.create_dataset( |
|
1488 | ds = grp.create_dataset( | |
1489 | dsInfo['variable'], |
|
1489 | dsInfo['variable'], | |
1490 | (self.blocksPerFile, ), |
|
1490 | (self.blocksPerFile, ), | |
1491 | chunks=True, |
|
1491 | chunks=True, | |
1492 | dtype=numpy.float64) |
|
1492 | dtype=numpy.float64) | |
1493 | dtsets.append(ds) |
|
1493 | dtsets.append(ds) | |
1494 | data.append((dsInfo['variable'], -1)) |
|
1494 | data.append((dsInfo['variable'], -1)) | |
1495 | else: |
|
1495 | else: | |
1496 | sgrp = grp.create_group(dsInfo['variable']) |
|
1496 | sgrp = grp.create_group(dsInfo['variable']) | |
1497 | for i in range(dsInfo['dsNumber']): |
|
1497 | for i in range(dsInfo['dsNumber']): | |
1498 | ds = sgrp.create_dataset( |
|
1498 | ds = sgrp.create_dataset( | |
1499 | 'table{:02d}'.format(i), |
|
1499 | 'table{:02d}'.format(i), | |
1500 | (self.blocksPerFile, ) + dsInfo['shape'][1:], |
|
1500 | (self.blocksPerFile, ) + dsInfo['shape'][1:], | |
1501 | chunks=True) |
|
1501 | chunks=True) | |
1502 | dtsets.append(ds) |
|
1502 | dtsets.append(ds) | |
1503 | data.append((dsInfo['variable'], i)) |
|
1503 | data.append((dsInfo['variable'], i)) | |
1504 | fp.flush() |
|
1504 | fp.flush() | |
1505 |
|
1505 | |||
1506 |
log.log(' |
|
1506 | log.log('Creating file: {}'.format(fp.filename), self.name) | |
1507 |
|
1507 | |||
1508 | self.ds = dtsets |
|
1508 | self.ds = dtsets | |
1509 | self.data = data |
|
1509 | self.data = data | |
1510 | self.firsttime = True |
|
1510 | self.firsttime = True | |
1511 | self.blockIndex = 0 |
|
1511 | self.blockIndex = 0 | |
1512 | return |
|
1512 | return | |
1513 |
|
1513 | |||
1514 | def putData(self): |
|
1514 | def putData(self): | |
1515 |
|
1515 | |||
1516 | if (self.blockIndex == self.blocksPerFile) or self.timeFlag(): |
|
1516 | if (self.blockIndex == self.blocksPerFile) or self.timeFlag(): | |
1517 | self.closeFile() |
|
1517 | self.closeFile() | |
1518 | self.setNextFile() |
|
1518 | self.setNextFile() | |
1519 |
|
1519 | |||
1520 | for i, ds in enumerate(self.ds): |
|
1520 | for i, ds in enumerate(self.ds): | |
1521 | attr, ch = self.data[i] |
|
1521 | attr, ch = self.data[i] | |
1522 | if ch == -1: |
|
1522 | if ch == -1: | |
1523 | ds[self.blockIndex] = getattr(self.dataOut, attr) |
|
1523 | ds[self.blockIndex] = getattr(self.dataOut, attr) | |
1524 | else: |
|
1524 | else: | |
1525 | ds[self.blockIndex] = getattr(self.dataOut, attr)[ch] |
|
1525 | ds[self.blockIndex] = getattr(self.dataOut, attr)[ch] | |
1526 |
|
1526 | |||
1527 | self.fp.flush() |
|
1527 | self.fp.flush() | |
1528 | self.blockIndex += 1 |
|
1528 | self.blockIndex += 1 | |
|
1529 | log.log('Block No. {}/{}'.format(self.blockIndex, self.blocksPerFile), self.name) | |||
1529 |
|
1530 | |||
1530 | return |
|
1531 | return | |
1531 |
|
1532 | |||
1532 | def closeFile(self): |
|
1533 | def closeFile(self): | |
1533 |
|
1534 | |||
1534 | if self.blockIndex != self.blocksPerFile: |
|
1535 | if self.blockIndex != self.blocksPerFile: | |
1535 | for ds in self.ds: |
|
1536 | for ds in self.ds: | |
1536 | ds.resize(self.blockIndex, axis=0) |
|
1537 | ds.resize(self.blockIndex, axis=0) | |
1537 |
|
1538 | |||
1538 | self.fp.flush() |
|
1539 | self.fp.flush() | |
1539 | self.fp.close() |
|
1540 | self.fp.close() | |
1540 |
|
1541 | |||
1541 | def close(self): |
|
1542 | def close(self): | |
1542 |
|
1543 | |||
1543 | self.closeFile() |
|
1544 | self.closeFile() |
@@ -1,404 +1,424 | |||||
1 | ''' |
|
1 | ''' | |
2 | Updated for multiprocessing |
|
2 | Updated for multiprocessing | |
3 | Author : Sergio Cortez |
|
3 | Author : Sergio Cortez | |
4 | Jan 2018 |
|
4 | Jan 2018 | |
5 | Abstract: |
|
5 | Abstract: | |
6 | Base class for processing units and operations. A decorator provides multiprocessing features and interconnect the processes created. |
|
6 | Base class for processing units and operations. A decorator provides multiprocessing features and interconnect the processes created. | |
7 | The argument (kwargs) sent from the controller is parsed and filtered via the decorator for each processing unit or operation instantiated. |
|
7 | The argument (kwargs) sent from the controller is parsed and filtered via the decorator for each processing unit or operation instantiated. | |
8 | The decorator handle also the methods inside the processing unit to be called from the main script (not as operations) (OPERATION -> type ='self'). |
|
8 | The decorator handle also the methods inside the processing unit to be called from the main script (not as operations) (OPERATION -> type ='self'). | |
9 |
|
9 | |||
10 | Based on: |
|
10 | Based on: | |
11 | $Author: murco $ |
|
11 | $Author: murco $ | |
12 | $Id: jroproc_base.py 1 2012-11-12 18:56:07Z murco $ |
|
12 | $Id: jroproc_base.py 1 2012-11-12 18:56:07Z murco $ | |
13 | ''' |
|
13 | ''' | |
14 |
|
14 | |||
15 | import os |
|
15 | import os | |
16 | import inspect |
|
16 | import inspect | |
17 | import zmq |
|
17 | import zmq | |
18 | import time |
|
18 | import time | |
19 | import pickle |
|
19 | import pickle | |
|
20 | from queue import Queue | |||
|
21 | from threading import Thread | |||
20 | from multiprocessing import Process |
|
22 | from multiprocessing import Process | |
21 | from zmq.utils.monitor import recv_monitor_message |
|
23 | from zmq.utils.monitor import recv_monitor_message | |
22 |
|
24 | |||
23 | from schainpy.utils import log |
|
25 | from schainpy.utils import log | |
24 |
|
26 | |||
25 |
|
27 | |||
26 | class ProcessingUnit(object): |
|
28 | class ProcessingUnit(object): | |
27 |
|
29 | |||
28 | """ |
|
30 | """ | |
29 | Update - Jan 2018 - MULTIPROCESSING |
|
31 | Update - Jan 2018 - MULTIPROCESSING | |
30 | All the "call" methods present in the previous base were removed. |
|
32 | All the "call" methods present in the previous base were removed. | |
31 | The majority of operations are independant processes, thus |
|
33 | The majority of operations are independant processes, thus | |
32 | the decorator is in charge of communicate the operation processes |
|
34 | the decorator is in charge of communicate the operation processes | |
33 | with the proccessing unit via IPC. |
|
35 | with the proccessing unit via IPC. | |
34 |
|
36 | |||
35 | The constructor does not receive any argument. The remaining methods |
|
37 | The constructor does not receive any argument. The remaining methods | |
36 | are related with the operations to execute. |
|
38 | are related with the operations to execute. | |
37 |
|
39 | |||
38 |
|
40 | |||
39 | """ |
|
41 | """ | |
40 |
|
42 | |||
41 | def __init__(self): |
|
43 | def __init__(self): | |
42 |
|
44 | |||
43 | self.dataIn = None |
|
45 | self.dataIn = None | |
44 | self.dataOut = None |
|
46 | self.dataOut = None | |
45 | self.isConfig = False |
|
47 | self.isConfig = False | |
46 | self.operations = [] |
|
48 | self.operations = [] | |
47 | self.plots = [] |
|
49 | self.plots = [] | |
48 |
|
50 | |||
49 | def getAllowedArgs(self): |
|
51 | def getAllowedArgs(self): | |
50 | if hasattr(self, '__attrs__'): |
|
52 | if hasattr(self, '__attrs__'): | |
51 | return self.__attrs__ |
|
53 | return self.__attrs__ | |
52 | else: |
|
54 | else: | |
53 | return inspect.getargspec(self.run).args |
|
55 | return inspect.getargspec(self.run).args | |
54 |
|
56 | |||
55 | def addOperation(self, conf, operation): |
|
57 | def addOperation(self, conf, operation): | |
56 | """ |
|
58 | """ | |
57 | This method is used in the controller, and update the dictionary containing the operations to execute. The dict |
|
59 | This method is used in the controller, and update the dictionary containing the operations to execute. The dict | |
58 | posses the id of the operation process (IPC purposes) |
|
60 | posses the id of the operation process (IPC purposes) | |
59 |
|
61 | |||
60 | Agrega un objeto del tipo "Operation" (opObj) a la lista de objetos "self.objectList" y retorna el |
|
62 | Agrega un objeto del tipo "Operation" (opObj) a la lista de objetos "self.objectList" y retorna el | |
61 | identificador asociado a este objeto. |
|
63 | identificador asociado a este objeto. | |
62 |
|
64 | |||
63 | Input: |
|
65 | Input: | |
64 |
|
66 | |||
65 | object : objeto de la clase "Operation" |
|
67 | object : objeto de la clase "Operation" | |
66 |
|
68 | |||
67 | Return: |
|
69 | Return: | |
68 |
|
70 | |||
69 | objId : identificador del objeto, necesario para comunicar con master(procUnit) |
|
71 | objId : identificador del objeto, necesario para comunicar con master(procUnit) | |
70 | """ |
|
72 | """ | |
71 |
|
73 | |||
72 | self.operations.append( |
|
74 | self.operations.append( | |
73 | (operation, conf.type, conf.id, conf.getKwargs())) |
|
75 | (operation, conf.type, conf.id, conf.getKwargs())) | |
74 |
|
76 | |||
75 | if 'plot' in self.name.lower(): |
|
77 | if 'plot' in self.name.lower(): | |
76 | self.plots.append(operation.CODE) |
|
78 | self.plots.append(operation.CODE) | |
77 |
|
79 | |||
78 | def getOperationObj(self, objId): |
|
80 | def getOperationObj(self, objId): | |
79 |
|
81 | |||
80 | if objId not in list(self.operations.keys()): |
|
82 | if objId not in list(self.operations.keys()): | |
81 | return None |
|
83 | return None | |
82 |
|
84 | |||
83 | return self.operations[objId] |
|
85 | return self.operations[objId] | |
84 |
|
86 | |||
85 | def operation(self, **kwargs): |
|
87 | def operation(self, **kwargs): | |
86 | """ |
|
88 | """ | |
87 | Operacion directa sobre la data (dataOut.data). Es necesario actualizar los valores de los |
|
89 | Operacion directa sobre la data (dataOut.data). Es necesario actualizar los valores de los | |
88 | atributos del objeto dataOut |
|
90 | atributos del objeto dataOut | |
89 |
|
91 | |||
90 | Input: |
|
92 | Input: | |
91 |
|
93 | |||
92 | **kwargs : Diccionario de argumentos de la funcion a ejecutar |
|
94 | **kwargs : Diccionario de argumentos de la funcion a ejecutar | |
93 | """ |
|
95 | """ | |
94 |
|
96 | |||
95 | raise NotImplementedError |
|
97 | raise NotImplementedError | |
96 |
|
98 | |||
97 | def setup(self): |
|
99 | def setup(self): | |
98 |
|
100 | |||
99 | raise NotImplementedError |
|
101 | raise NotImplementedError | |
100 |
|
102 | |||
101 | def run(self): |
|
103 | def run(self): | |
102 |
|
104 | |||
103 | raise NotImplementedError |
|
105 | raise NotImplementedError | |
104 |
|
106 | |||
105 | def close(self): |
|
107 | def close(self): | |
106 |
|
108 | |||
107 | return |
|
109 | return | |
108 |
|
110 | |||
109 |
|
111 | |||
110 | class Operation(object): |
|
112 | class Operation(object): | |
111 |
|
113 | |||
112 | """ |
|
114 | """ | |
113 | Update - Jan 2018 - MULTIPROCESSING |
|
115 | Update - Jan 2018 - MULTIPROCESSING | |
114 |
|
116 | |||
115 | Most of the methods remained the same. The decorator parse the arguments and executed the run() method for each process. |
|
117 | Most of the methods remained the same. The decorator parse the arguments and executed the run() method for each process. | |
116 | The constructor doe snot receive any argument, neither the baseclass. |
|
118 | The constructor doe snot receive any argument, neither the baseclass. | |
117 |
|
119 | |||
118 |
|
120 | |||
119 | Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit |
|
121 | Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit | |
120 | y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de |
|
122 | y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de | |
121 | acumulacion dentro de esta clase |
|
123 | acumulacion dentro de esta clase | |
122 |
|
124 | |||
123 | Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer) |
|
125 | Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer) | |
124 |
|
126 | |||
125 | """ |
|
127 | """ | |
126 |
|
128 | |||
127 | def __init__(self): |
|
129 | def __init__(self): | |
128 |
|
130 | |||
129 | self.id = None |
|
131 | self.id = None | |
130 | self.isConfig = False |
|
132 | self.isConfig = False | |
131 |
|
133 | |||
132 | if not hasattr(self, 'name'): |
|
134 | if not hasattr(self, 'name'): | |
133 | self.name = self.__class__.__name__ |
|
135 | self.name = self.__class__.__name__ | |
134 |
|
136 | |||
135 | def getAllowedArgs(self): |
|
137 | def getAllowedArgs(self): | |
136 | if hasattr(self, '__attrs__'): |
|
138 | if hasattr(self, '__attrs__'): | |
137 | return self.__attrs__ |
|
139 | return self.__attrs__ | |
138 | else: |
|
140 | else: | |
139 | return inspect.getargspec(self.run).args |
|
141 | return inspect.getargspec(self.run).args | |
140 |
|
142 | |||
141 | def setup(self): |
|
143 | def setup(self): | |
142 |
|
144 | |||
143 | self.isConfig = True |
|
145 | self.isConfig = True | |
144 |
|
146 | |||
145 | raise NotImplementedError |
|
147 | raise NotImplementedError | |
146 |
|
148 | |||
147 | def run(self, dataIn, **kwargs): |
|
149 | def run(self, dataIn, **kwargs): | |
148 | """ |
|
150 | """ | |
149 | Realiza las operaciones necesarias sobre la dataIn.data y actualiza los |
|
151 | Realiza las operaciones necesarias sobre la dataIn.data y actualiza los | |
150 | atributos del objeto dataIn. |
|
152 | atributos del objeto dataIn. | |
151 |
|
153 | |||
152 | Input: |
|
154 | Input: | |
153 |
|
155 | |||
154 | dataIn : objeto del tipo JROData |
|
156 | dataIn : objeto del tipo JROData | |
155 |
|
157 | |||
156 | Return: |
|
158 | Return: | |
157 |
|
159 | |||
158 | None |
|
160 | None | |
159 |
|
161 | |||
160 | Affected: |
|
162 | Affected: | |
161 | __buffer : buffer de recepcion de datos. |
|
163 | __buffer : buffer de recepcion de datos. | |
162 |
|
164 | |||
163 | """ |
|
165 | """ | |
164 | if not self.isConfig: |
|
166 | if not self.isConfig: | |
165 | self.setup(**kwargs) |
|
167 | self.setup(**kwargs) | |
166 |
|
168 | |||
167 | raise NotImplementedError |
|
169 | raise NotImplementedError | |
168 |
|
170 | |||
169 | def close(self): |
|
171 | def close(self): | |
170 |
|
172 | |||
171 | return |
|
173 | return | |
172 |
|
174 | |||
|
175 | class InputQueue(Thread): | |||
|
176 | ''' | |||
|
177 | Class to hold input data for Proccessing Units and external Operations, | |||
|
178 | ''' | |||
|
179 | ||||
|
180 | def __init__(self, project_id, inputId): | |||
|
181 | Thread.__init__(self) | |||
|
182 | self.queue = Queue() | |||
|
183 | self.project_id = project_id | |||
|
184 | self.inputId = inputId | |||
|
185 | ||||
|
186 | def run(self): | |||
|
187 | ||||
|
188 | c = zmq.Context() | |||
|
189 | self.receiver = c.socket(zmq.SUB) | |||
|
190 | self.receiver.connect( | |||
|
191 | 'ipc:///tmp/schain/{}_pub'.format(self.project_id)) | |||
|
192 | self.receiver.setsockopt(zmq.SUBSCRIBE, self.inputId.encode()) | |||
|
193 | ||||
|
194 | while True: | |||
|
195 | self.queue.put(self.receiver.recv_multipart()[1]) | |||
|
196 | ||||
|
197 | def get(self): | |||
|
198 | ||||
|
199 | return pickle.loads(self.queue.get()) | |||
|
200 | ||||
173 |
|
201 | |||
174 | def MPDecorator(BaseClass): |
|
202 | def MPDecorator(BaseClass): | |
175 | """ |
|
203 | """ | |
176 | Multiprocessing class decorator |
|
204 | Multiprocessing class decorator | |
177 |
|
205 | |||
178 | This function add multiprocessing features to a BaseClass. Also, it handle |
|
206 | This function add multiprocessing features to a BaseClass. Also, it handle | |
179 | the communication beetween processes (readers, procUnits and operations). |
|
207 | the communication beetween processes (readers, procUnits and operations). | |
180 | """ |
|
208 | """ | |
181 |
|
209 | |||
182 | class MPClass(BaseClass, Process): |
|
210 | class MPClass(BaseClass, Process): | |
183 |
|
211 | |||
184 | def __init__(self, *args, **kwargs): |
|
212 | def __init__(self, *args, **kwargs): | |
185 | super(MPClass, self).__init__() |
|
213 | super(MPClass, self).__init__() | |
186 | Process.__init__(self) |
|
214 | Process.__init__(self) | |
187 | self.operationKwargs = {} |
|
215 | self.operationKwargs = {} | |
188 | self.args = args |
|
216 | self.args = args | |
189 | self.kwargs = kwargs |
|
217 | self.kwargs = kwargs | |
190 | self.sender = None |
|
218 | self.sender = None | |
191 | self.receiver = None |
|
219 | self.receiver = None | |
192 |
self.i |
|
220 | self.i = 0 | |
193 | self.name = BaseClass.__name__ |
|
221 | self.name = BaseClass.__name__ | |
194 | if 'plot' in self.name.lower() and not self.name.endswith('_'): |
|
222 | if 'plot' in self.name.lower() and not self.name.endswith('_'): | |
195 | self.name = '{}{}'.format(self.CODE.upper(), 'Plot') |
|
223 | self.name = '{}{}'.format(self.CODE.upper(), 'Plot') | |
196 | self.start_time = time.time() |
|
224 | self.start_time = time.time() | |
197 |
|
225 | |||
198 | if len(self.args) is 3: |
|
226 | if len(self.args) is 3: | |
199 | self.typeProc = "ProcUnit" |
|
227 | self.typeProc = "ProcUnit" | |
200 | self.id = args[0] |
|
228 | self.id = args[0] | |
201 | self.inputId = args[1] |
|
229 | self.inputId = args[1] | |
202 | self.project_id = args[2] |
|
230 | self.project_id = args[2] | |
203 | elif len(self.args) is 2: |
|
231 | elif len(self.args) is 2: | |
204 | self.id = args[0] |
|
232 | self.id = args[0] | |
205 | self.inputId = args[0] |
|
233 | self.inputId = args[0] | |
206 | self.project_id = args[1] |
|
234 | self.project_id = args[1] | |
207 | self.typeProc = "Operation" |
|
235 | self.typeProc = "Operation" | |
208 |
|
|
236 | ||
209 | def fix_publish(self,valor,multiple1): |
|
237 | self.queue = InputQueue(self.project_id, self.inputId) | |
210 | return True if valor%multiple1 ==0 else False |
|
|||
211 |
|
238 | |||
212 | def subscribe(self): |
|
239 | def subscribe(self): | |
213 | ''' |
|
240 | ''' | |
214 | This function create a socket to receive objects from the |
|
241 | This function start the input queue. | |
215 | topic `inputId`. |
|
|||
216 | ''' |
|
242 | ''' | |
217 |
|
243 | |||
218 |
|
|
244 | self.queue.start() | |
219 | self.receiver = c.socket(zmq.SUB) |
|
245 | ||
220 | self.receiver.connect( |
|
|||
221 | 'ipc:///tmp/schain/{}_pub'.format(self.project_id)) |
|
|||
222 | self.receiver.setsockopt(zmq.SUBSCRIBE, self.inputId.encode()) |
|
|||
223 |
|
246 | |||
224 | def listen(self): |
|
247 | def listen(self): | |
225 | ''' |
|
248 | ''' | |
226 |
This function waits for objects |
|
249 | This function waits for objects | |
227 | ''' |
|
250 | ''' | |
228 | try: |
|
251 | ||
229 | data = pickle.loads(self.receiver.recv_multipart()[1]) |
|
252 | return self.queue.get() | |
230 | except zmq.ZMQError as e: |
|
|||
231 | if e.errno == zmq.ETERM: |
|
|||
232 | print (e.errno) |
|
|||
233 |
|
||||
234 | return data |
|
|||
235 |
|
253 | |||
236 | def set_publisher(self): |
|
254 | def set_publisher(self): | |
237 | ''' |
|
255 | ''' | |
238 | This function create a socket for publishing purposes. |
|
256 | This function create a socket for publishing purposes. | |
239 | ''' |
|
257 | ''' | |
240 |
|
258 | |||
241 | time.sleep(1) |
|
259 | time.sleep(1) | |
242 | c = zmq.Context() |
|
260 | c = zmq.Context() | |
243 | self.sender = c.socket(zmq.PUB) |
|
261 | self.sender = c.socket(zmq.PUB) | |
244 | self.sender.connect( |
|
262 | self.sender.connect( | |
245 | 'ipc:///tmp/schain/{}_sub'.format(self.project_id)) |
|
263 | 'ipc:///tmp/schain/{}_sub'.format(self.project_id)) | |
246 |
|
264 | |||
247 | def publish(self, data, id): |
|
265 | def publish(self, data, id): | |
248 | ''' |
|
266 | ''' | |
249 | This function publish an object, to a specific topic. |
|
267 | This function publish an object, to a specific topic. | |
250 | The fix method only affect inputId None which is Read Unit |
|
268 | For Read Units (inputId == None) adds a little delay | |
251 | Use value between 64 80, you should notice a little retard in processing |
|
269 | to avoid data loss | |
252 | ''' |
|
270 | ''' | |
|
271 | ||||
253 | if self.inputId is None: |
|
272 | if self.inputId is None: | |
254 | self.i+=1 |
|
273 | self.i += 1 | |
255 |
if self. |
|
274 | if self.i % 100 == 0: | |
256 |
|
|
275 | self.i = 0 | |
|
276 | time.sleep(0.01) | |||
257 |
|
277 | |||
258 | self.sender.send_multipart([str(id).encode(), pickle.dumps(data)]) |
|
278 | self.sender.send_multipart([str(id).encode(), pickle.dumps(data)]) | |
259 |
|
279 | |||
260 | def runReader(self): |
|
280 | def runReader(self): | |
261 | ''' |
|
281 | ''' | |
262 | Run fuction for read units |
|
282 | Run fuction for read units | |
263 | ''' |
|
283 | ''' | |
264 | while True: |
|
284 | while True: | |
265 |
|
285 | |||
266 | BaseClass.run(self, **self.kwargs) |
|
286 | BaseClass.run(self, **self.kwargs) | |
267 |
|
287 | |||
268 | for op, optype, opId, kwargs in self.operations: |
|
288 | for op, optype, opId, kwargs in self.operations: | |
269 | if optype == 'self' and not self.dataOut.flagNoData: |
|
289 | if optype == 'self' and not self.dataOut.flagNoData: | |
270 | op(**kwargs) |
|
290 | op(**kwargs) | |
271 | elif optype == 'other' and not self.dataOut.flagNoData: |
|
291 | elif optype == 'other' and not self.dataOut.flagNoData: | |
272 | self.dataOut = op.run(self.dataOut, **self.kwargs) |
|
292 | self.dataOut = op.run(self.dataOut, **self.kwargs) | |
273 | elif optype == 'external': |
|
293 | elif optype == 'external': | |
274 | self.publish(self.dataOut, opId) |
|
294 | self.publish(self.dataOut, opId) | |
275 |
|
295 | |||
276 | if self.dataOut.flagNoData and not self.dataOut.error: |
|
296 | if self.dataOut.flagNoData and not self.dataOut.error: | |
277 | continue |
|
297 | continue | |
278 |
|
298 | |||
279 | self.publish(self.dataOut, self.id) |
|
299 | self.publish(self.dataOut, self.id) | |
280 |
|
300 | |||
281 | if self.dataOut.error: |
|
301 | if self.dataOut.error: | |
282 | log.error(self.dataOut.error, self.name) |
|
302 | log.error(self.dataOut.error, self.name) | |
283 | # self.sender.send_multipart([str(self.project_id).encode(), 'end'.encode()]) |
|
303 | # self.sender.send_multipart([str(self.project_id).encode(), 'end'.encode()]) | |
284 | break |
|
304 | break | |
285 |
|
305 | |||
286 | time.sleep(1) |
|
306 | time.sleep(1) | |
287 |
|
307 | |||
288 | def runProc(self): |
|
308 | def runProc(self): | |
289 | ''' |
|
309 | ''' | |
290 | Run function for proccessing units |
|
310 | Run function for proccessing units | |
291 | ''' |
|
311 | ''' | |
292 |
|
312 | |||
293 | while True: |
|
313 | while True: | |
294 | self.dataIn = self.listen() |
|
314 | self.dataIn = self.listen() | |
295 |
|
315 | |||
296 | if self.dataIn.flagNoData and self.dataIn.error is None: |
|
316 | if self.dataIn.flagNoData and self.dataIn.error is None: | |
297 | continue |
|
317 | continue | |
298 |
|
318 | |||
299 | BaseClass.run(self, **self.kwargs) |
|
319 | BaseClass.run(self, **self.kwargs) | |
300 |
|
320 | |||
301 | if self.dataIn.error: |
|
321 | if self.dataIn.error: | |
302 | self.dataOut.error = self.dataIn.error |
|
322 | self.dataOut.error = self.dataIn.error | |
303 | self.dataOut.flagNoData = True |
|
323 | self.dataOut.flagNoData = True | |
304 |
|
324 | |||
305 | for op, optype, opId, kwargs in self.operations: |
|
325 | for op, optype, opId, kwargs in self.operations: | |
306 | if optype == 'self' and not self.dataOut.flagNoData: |
|
326 | if optype == 'self' and not self.dataOut.flagNoData: | |
307 | op(**kwargs) |
|
327 | op(**kwargs) | |
308 | elif optype == 'other' and not self.dataOut.flagNoData: |
|
328 | elif optype == 'other' and not self.dataOut.flagNoData: | |
309 | self.dataOut = op.run(self.dataOut, **kwargs) |
|
329 | self.dataOut = op.run(self.dataOut, **kwargs) | |
310 | elif optype == 'external' and not self.dataOut.flagNoData: |
|
330 | elif optype == 'external' and not self.dataOut.flagNoData: | |
311 | self.publish(self.dataOut, opId) |
|
331 | self.publish(self.dataOut, opId) | |
312 |
|
332 | |||
313 | if not self.dataOut.flagNoData or self.dataOut.error: |
|
333 | if not self.dataOut.flagNoData or self.dataOut.error: | |
314 | self.publish(self.dataOut, self.id) |
|
334 | self.publish(self.dataOut, self.id) | |
315 | for op, optype, opId, kwargs in self.operations: |
|
335 | for op, optype, opId, kwargs in self.operations: | |
316 | if optype == 'self' and self.dataOut.error: |
|
336 | if optype == 'self' and self.dataOut.error: | |
317 | op(**kwargs) |
|
337 | op(**kwargs) | |
318 | elif optype == 'other' and self.dataOut.error: |
|
338 | elif optype == 'other' and self.dataOut.error: | |
319 | self.dataOut = op.run(self.dataOut, **kwargs) |
|
339 | self.dataOut = op.run(self.dataOut, **kwargs) | |
320 | elif optype == 'external' and self.dataOut.error: |
|
340 | elif optype == 'external' and self.dataOut.error: | |
321 | self.publish(self.dataOut, opId) |
|
341 | self.publish(self.dataOut, opId) | |
322 |
|
342 | |||
323 | if self.dataIn.error: |
|
343 | if self.dataIn.error: | |
324 | break |
|
344 | break | |
325 |
|
345 | |||
326 | time.sleep(1) |
|
346 | time.sleep(1) | |
327 |
|
347 | |||
328 | def runOp(self): |
|
348 | def runOp(self): | |
329 | ''' |
|
349 | ''' | |
330 | Run function for external operations (this operations just receive data |
|
350 | Run function for external operations (this operations just receive data | |
331 | ex: plots, writers, publishers) |
|
351 | ex: plots, writers, publishers) | |
332 | ''' |
|
352 | ''' | |
333 |
|
353 | |||
334 | while True: |
|
354 | while True: | |
335 |
|
355 | |||
336 | dataOut = self.listen() |
|
356 | dataOut = self.listen() | |
337 |
|
357 | |||
338 | BaseClass.run(self, dataOut, **self.kwargs) |
|
358 | BaseClass.run(self, dataOut, **self.kwargs) | |
339 |
|
359 | |||
340 | if dataOut.error: |
|
360 | if dataOut.error: | |
341 | break |
|
361 | break | |
342 |
|
362 | |||
343 | time.sleep(1) |
|
363 | time.sleep(1) | |
344 |
|
364 | |||
345 | def run(self): |
|
365 | def run(self): | |
346 | if self.typeProc is "ProcUnit": |
|
366 | if self.typeProc is "ProcUnit": | |
347 |
|
367 | |||
348 | if self.inputId is not None: |
|
368 | if self.inputId is not None: | |
349 |
|
369 | |||
350 | self.subscribe() |
|
370 | self.subscribe() | |
351 |
|
371 | |||
352 | self.set_publisher() |
|
372 | self.set_publisher() | |
353 |
|
373 | |||
354 | if 'Reader' not in BaseClass.__name__: |
|
374 | if 'Reader' not in BaseClass.__name__: | |
355 | self.runProc() |
|
375 | self.runProc() | |
356 | else: |
|
376 | else: | |
357 | self.runReader() |
|
377 | self.runReader() | |
358 |
|
378 | |||
359 | elif self.typeProc is "Operation": |
|
379 | elif self.typeProc is "Operation": | |
360 |
|
380 | |||
361 | self.subscribe() |
|
381 | self.subscribe() | |
362 | self.runOp() |
|
382 | self.runOp() | |
363 |
|
383 | |||
364 | else: |
|
384 | else: | |
365 | raise ValueError("Unknown type") |
|
385 | raise ValueError("Unknown type") | |
366 |
|
386 | |||
367 | self.close() |
|
387 | self.close() | |
368 |
|
388 | |||
369 | def event_monitor(self, monitor): |
|
389 | def event_monitor(self, monitor): | |
370 |
|
390 | |||
371 | events = {} |
|
391 | events = {} | |
372 |
|
392 | |||
373 | for name in dir(zmq): |
|
393 | for name in dir(zmq): | |
374 | if name.startswith('EVENT_'): |
|
394 | if name.startswith('EVENT_'): | |
375 | value = getattr(zmq, name) |
|
395 | value = getattr(zmq, name) | |
376 | events[value] = name |
|
396 | events[value] = name | |
377 |
|
397 | |||
378 | while monitor.poll(): |
|
398 | while monitor.poll(): | |
379 | evt = recv_monitor_message(monitor) |
|
399 | evt = recv_monitor_message(monitor) | |
380 | if evt['event'] == 32: |
|
400 | if evt['event'] == 32: | |
381 | self.connections += 1 |
|
401 | self.connections += 1 | |
382 | if evt['event'] == 512: |
|
402 | if evt['event'] == 512: | |
383 | pass |
|
403 | pass | |
384 |
|
404 | |||
385 | evt.update({'description': events[evt['event']]}) |
|
405 | evt.update({'description': events[evt['event']]}) | |
386 |
|
406 | |||
387 | if evt['event'] == zmq.EVENT_MONITOR_STOPPED: |
|
407 | if evt['event'] == zmq.EVENT_MONITOR_STOPPED: | |
388 | break |
|
408 | break | |
389 | monitor.close() |
|
409 | monitor.close() | |
390 | print('event monitor thread done!') |
|
410 | print('event monitor thread done!') | |
391 |
|
411 | |||
392 | def close(self): |
|
412 | def close(self): | |
393 |
|
413 | |||
394 | BaseClass.close(self) |
|
414 | BaseClass.close(self) | |
395 |
|
415 | |||
396 | if self.sender: |
|
416 | if self.sender: | |
397 | self.sender.close() |
|
417 | self.sender.close() | |
398 |
|
418 | |||
399 | if self.receiver: |
|
419 | if self.receiver: | |
400 | self.receiver.close() |
|
420 | self.receiver.close() | |
401 |
|
421 | |||
402 | log.success('Done...(Time:{:4.2f} secs)'.format(time.time()-self.start_time), self.name) |
|
422 | log.success('Done...(Time:{:4.2f} secs)'.format(time.time()-self.start_time), self.name) | |
403 |
|
423 | |||
404 | return MPClass |
|
424 | return MPClass |
General Comments 0
You need to be logged in to leave comments.
Login now