@@ -1,660 +1,674 | |||||
1 | import os |
|
1 | import os | |
2 | import time |
|
2 | import time | |
3 | import datetime |
|
3 | import datetime | |
4 |
|
4 | |||
5 | import numpy |
|
5 | import numpy | |
6 | import h5py |
|
6 | import h5py | |
7 |
|
7 | |||
8 | import schainpy.admin |
|
8 | import schainpy.admin | |
9 | from schainpy.model.data.jrodata import * |
|
9 | from schainpy.model.data.jrodata import * | |
10 | from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator |
|
10 | from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator | |
11 | from schainpy.model.io.jroIO_base import * |
|
11 | from schainpy.model.io.jroIO_base import * | |
12 | from schainpy.utils import log |
|
12 | from schainpy.utils import log | |
13 |
|
13 | |||
14 |
|
14 | |||
15 | class HDFReader(Reader, ProcessingUnit): |
|
15 | class HDFReader(Reader, ProcessingUnit): | |
16 | """Processing unit to read HDF5 format files |
|
16 | """Processing unit to read HDF5 format files | |
17 |
|
17 | |||
18 | This unit reads HDF5 files created with `HDFWriter` operation contains |
|
18 | This unit reads HDF5 files created with `HDFWriter` operation contains | |
19 | by default two groups Data and Metadata all variables would be saved as `dataOut` |
|
19 | by default two groups Data and Metadata all variables would be saved as `dataOut` | |
20 | attributes. |
|
20 | attributes. | |
21 | It is possible to read any HDF5 file by given the structure in the `description` |
|
21 | It is possible to read any HDF5 file by given the structure in the `description` | |
22 | parameter, also you can add extra values to metadata with the parameter `extras`. |
|
22 | parameter, also you can add extra values to metadata with the parameter `extras`. | |
23 |
|
23 | |||
24 | Parameters: |
|
24 | Parameters: | |
25 | ----------- |
|
25 | ----------- | |
26 | path : str |
|
26 | path : str | |
27 | Path where files are located. |
|
27 | Path where files are located. | |
28 | startDate : date |
|
28 | startDate : date | |
29 | Start date of the files |
|
29 | Start date of the files | |
30 | endDate : list |
|
30 | endDate : list | |
31 | End date of the files |
|
31 | End date of the files | |
32 | startTime : time |
|
32 | startTime : time | |
33 | Start time of the files |
|
33 | Start time of the files | |
34 | endTime : time |
|
34 | endTime : time | |
35 | End time of the files |
|
35 | End time of the files | |
36 | description : dict, optional |
|
36 | description : dict, optional | |
37 | Dictionary with the description of the HDF5 file |
|
37 | Dictionary with the description of the HDF5 file | |
38 | extras : dict, optional |
|
38 | extras : dict, optional | |
39 | Dictionary with extra metadata to be be added to `dataOut` |
|
39 | Dictionary with extra metadata to be be added to `dataOut` | |
40 |
|
40 | |||
41 | Examples |
|
41 | Examples | |
42 | -------- |
|
42 | -------- | |
43 |
|
43 | |||
44 | desc = { |
|
44 | desc = { | |
45 | 'Data': { |
|
45 | 'Data': { | |
46 | 'data_output': ['u', 'v', 'w'], |
|
46 | 'data_output': ['u', 'v', 'w'], | |
47 | 'utctime': 'timestamps', |
|
47 | 'utctime': 'timestamps', | |
48 | } , |
|
48 | } , | |
49 | 'Metadata': { |
|
49 | 'Metadata': { | |
50 | 'heightList': 'heights' |
|
50 | 'heightList': 'heights' | |
51 | } |
|
51 | } | |
52 | } |
|
52 | } | |
53 |
|
53 | |||
54 | desc = { |
|
54 | desc = { | |
55 | 'Data': { |
|
55 | 'Data': { | |
56 | 'data_output': 'winds', |
|
56 | 'data_output': 'winds', | |
57 | 'utctime': 'timestamps' |
|
57 | 'utctime': 'timestamps' | |
58 | }, |
|
58 | }, | |
59 | 'Metadata': { |
|
59 | 'Metadata': { | |
60 | 'heightList': 'heights' |
|
60 | 'heightList': 'heights' | |
61 | } |
|
61 | } | |
62 | } |
|
62 | } | |
63 |
|
63 | |||
64 | extras = { |
|
64 | extras = { | |
65 | 'timeZone': 300 |
|
65 | 'timeZone': 300 | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | reader = project.addReadUnit( |
|
68 | reader = project.addReadUnit( | |
69 | name='HDFReader', |
|
69 | name='HDFReader', | |
70 | path='/path/to/files', |
|
70 | path='/path/to/files', | |
71 | startDate='2019/01/01', |
|
71 | startDate='2019/01/01', | |
72 | endDate='2019/01/31', |
|
72 | endDate='2019/01/31', | |
73 | startTime='00:00:00', |
|
73 | startTime='00:00:00', | |
74 | endTime='23:59:59', |
|
74 | endTime='23:59:59', | |
75 | # description=json.dumps(desc), |
|
75 | # description=json.dumps(desc), | |
76 | # extras=json.dumps(extras), |
|
76 | # extras=json.dumps(extras), | |
77 | ) |
|
77 | ) | |
78 |
|
78 | |||
79 | """ |
|
79 | """ | |
80 |
|
80 | |||
81 | __attrs__ = ['path', 'startDate', 'endDate', 'startTime', 'endTime', 'description', 'extras'] |
|
81 | __attrs__ = ['path', 'startDate', 'endDate', 'startTime', 'endTime', 'description', 'extras'] | |
82 |
|
82 | |||
83 | def __init__(self): |
|
83 | def __init__(self): | |
84 | ProcessingUnit.__init__(self) |
|
84 | ProcessingUnit.__init__(self) | |
85 | self.dataOut = Parameters() |
|
85 | self.dataOut = Parameters() | |
86 | self.ext = ".hdf5" |
|
86 | self.ext = ".hdf5" | |
87 | self.optchar = "D" |
|
87 | self.optchar = "D" | |
88 | self.meta = {} |
|
88 | self.meta = {} | |
89 | self.data = {} |
|
89 | self.data = {} | |
90 | self.open_file = h5py.File |
|
90 | self.open_file = h5py.File | |
91 | self.open_mode = 'r' |
|
91 | self.open_mode = 'r' | |
92 | self.description = {} |
|
92 | self.description = {} | |
93 | self.extras = {} |
|
93 | self.extras = {} | |
94 | self.filefmt = "*%Y%j***" |
|
94 | self.filefmt = "*%Y%j***" | |
95 | self.folderfmt = "*%Y%j" |
|
95 | self.folderfmt = "*%Y%j" | |
96 | self.utcoffset = 0 |
|
96 | self.utcoffset = 0 | |
97 |
|
97 | |||
98 | def setup(self, **kwargs): |
|
98 | def setup(self, **kwargs): | |
99 |
|
99 | |||
100 | self.set_kwargs(**kwargs) |
|
100 | self.set_kwargs(**kwargs) | |
101 | if not self.ext.startswith('.'): |
|
101 | if not self.ext.startswith('.'): | |
102 | self.ext = '.{}'.format(self.ext) |
|
102 | self.ext = '.{}'.format(self.ext) | |
103 |
|
103 | |||
104 | if self.online: |
|
104 | if self.online: | |
105 | log.log("Searching files in online mode...", self.name) |
|
105 | log.log("Searching files in online mode...", self.name) | |
106 |
|
106 | |||
107 | for nTries in range(self.nTries): |
|
107 | for nTries in range(self.nTries): | |
108 | fullpath = self.searchFilesOnLine(self.path, self.startDate, |
|
108 | fullpath = self.searchFilesOnLine(self.path, self.startDate, | |
109 | self.endDate, self.expLabel, self.ext, self.walk, |
|
109 | self.endDate, self.expLabel, self.ext, self.walk, | |
110 | self.filefmt, self.folderfmt) |
|
110 | self.filefmt, self.folderfmt) | |
111 | pathname, filename = os.path.split(fullpath) |
|
111 | pathname, filename = os.path.split(fullpath) | |
112 | #print(pathname,filename) |
|
112 | #print(pathname,filename) | |
113 | try: |
|
113 | try: | |
114 | fullpath = next(fullpath) |
|
114 | fullpath = next(fullpath) | |
115 |
|
115 | |||
116 | except: |
|
116 | except: | |
117 | fullpath = None |
|
117 | fullpath = None | |
118 |
|
118 | |||
119 | if fullpath: |
|
119 | if fullpath: | |
120 | break |
|
120 | break | |
121 |
|
121 | |||
122 | log.warning( |
|
122 | log.warning( | |
123 | 'Waiting {} sec for a valid file in {}: try {} ...'.format( |
|
123 | 'Waiting {} sec for a valid file in {}: try {} ...'.format( | |
124 | self.delay, self.path, nTries + 1), |
|
124 | self.delay, self.path, nTries + 1), | |
125 | self.name) |
|
125 | self.name) | |
126 | time.sleep(self.delay) |
|
126 | time.sleep(self.delay) | |
127 |
|
127 | |||
128 | if not(fullpath): |
|
128 | if not(fullpath): | |
129 | raise schainpy.admin.SchainError( |
|
129 | raise schainpy.admin.SchainError( | |
130 | 'There isn\'t any valid file in {}'.format(self.path)) |
|
130 | 'There isn\'t any valid file in {}'.format(self.path)) | |
131 |
|
131 | |||
132 | pathname, filename = os.path.split(fullpath) |
|
132 | pathname, filename = os.path.split(fullpath) | |
133 | self.year = int(filename[1:5]) |
|
133 | self.year = int(filename[1:5]) | |
134 | self.doy = int(filename[5:8]) |
|
134 | self.doy = int(filename[5:8]) | |
135 | self.set = int(filename[8:11]) - 1 |
|
135 | self.set = int(filename[8:11]) - 1 | |
136 | else: |
|
136 | else: | |
137 | log.log("Searching files in {}".format(self.path), self.name) |
|
137 | log.log("Searching files in {}".format(self.path), self.name) | |
138 | self.filenameList = self.searchFilesOffLine(self.path, self.startDate, |
|
138 | self.filenameList = self.searchFilesOffLine(self.path, self.startDate, | |
139 | self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt) |
|
139 | self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt) | |
140 |
|
140 | |||
141 | self.setNextFile() |
|
141 | self.setNextFile() | |
142 |
|
142 | |||
143 | return |
|
143 | return | |
144 |
|
144 | |||
145 |
|
145 | |||
146 | def readFirstHeader(self): |
|
146 | def readFirstHeader(self): | |
147 | '''Read metadata and data''' |
|
147 | '''Read metadata and data''' | |
148 |
|
148 | |||
149 | self.__readMetadata() |
|
149 | self.__readMetadata() | |
150 | self.__readData() |
|
150 | self.__readData() | |
151 | self.__setBlockList() |
|
151 | self.__setBlockList() | |
152 |
|
152 | |||
153 | if 'type' in self.meta: |
|
153 | if 'type' in self.meta: | |
|
154 | ##print("Creting dataOut...") | |||
154 | self.dataOut = eval(self.meta['type'])() |
|
155 | self.dataOut = eval(self.meta['type'])() | |
|
156 | ##print(vars(self.dataOut)) | |||
155 |
|
157 | |||
156 | for attr in self.meta: |
|
158 | for attr in self.meta: | |
157 | #print("attr: ", attr) |
|
159 | ##print("attr: ", attr) | |
|
160 | ##print(type(self.dataOut).__name__) | |||
158 | setattr(self.dataOut, attr, self.meta[attr]) |
|
161 | setattr(self.dataOut, attr, self.meta[attr]) | |
159 |
|
162 | |||
160 |
|
||||
161 | self.blockIndex = 0 |
|
163 | self.blockIndex = 0 | |
162 |
|
164 | |||
163 | return |
|
165 | return | |
164 |
|
166 | |||
165 | def __setBlockList(self): |
|
167 | def __setBlockList(self): | |
166 | ''' |
|
168 | ''' | |
167 | Selects the data within the times defined |
|
169 | Selects the data within the times defined | |
168 |
|
170 | |||
169 | self.fp |
|
171 | self.fp | |
170 | self.startTime |
|
172 | self.startTime | |
171 | self.endTime |
|
173 | self.endTime | |
172 | self.blockList |
|
174 | self.blockList | |
173 | self.blocksPerFile |
|
175 | self.blocksPerFile | |
174 |
|
176 | |||
175 | ''' |
|
177 | ''' | |
176 |
|
178 | |||
177 | startTime = self.startTime |
|
179 | startTime = self.startTime | |
178 | endTime = self.endTime |
|
180 | endTime = self.endTime | |
179 | thisUtcTime = self.data['utctime'] + self.utcoffset |
|
181 | thisUtcTime = self.data['utctime'] + self.utcoffset | |
180 | self.interval = numpy.min(thisUtcTime[1:] - thisUtcTime[:-1]) |
|
182 | self.interval = numpy.min(thisUtcTime[1:] - thisUtcTime[:-1]) | |
181 | thisDatetime = datetime.datetime.utcfromtimestamp(thisUtcTime[0]) |
|
183 | thisDatetime = datetime.datetime.utcfromtimestamp(thisUtcTime[0]) | |
182 | self.startFileDatetime = thisDatetime |
|
184 | self.startFileDatetime = thisDatetime | |
183 | thisDate = thisDatetime.date() |
|
185 | thisDate = thisDatetime.date() | |
184 | thisTime = thisDatetime.time() |
|
186 | thisTime = thisDatetime.time() | |
185 |
|
187 | |||
186 | startUtcTime = (datetime.datetime.combine(thisDate, startTime) - datetime.datetime(1970, 1, 1)).total_seconds() |
|
188 | startUtcTime = (datetime.datetime.combine(thisDate, startTime) - datetime.datetime(1970, 1, 1)).total_seconds() | |
187 | endUtcTime = (datetime.datetime.combine(thisDate, endTime) - datetime.datetime(1970, 1, 1)).total_seconds() |
|
189 | endUtcTime = (datetime.datetime.combine(thisDate, endTime) - datetime.datetime(1970, 1, 1)).total_seconds() | |
188 |
|
190 | |||
189 | ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0] |
|
191 | ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0] | |
190 |
|
192 | |||
191 | self.blockList = ind |
|
193 | self.blockList = ind | |
192 | self.blocksPerFile = len(ind) |
|
194 | self.blocksPerFile = len(ind) | |
193 | self.blocksPerFile = len(thisUtcTime) |
|
195 | self.blocksPerFile = len(thisUtcTime) | |
194 | return |
|
196 | return | |
195 |
|
197 | |||
196 | def __readMetadata(self): |
|
198 | def __readMetadata(self): | |
197 | ''' |
|
199 | ''' | |
198 | Reads Metadata |
|
200 | Reads Metadata | |
199 | ''' |
|
201 | ''' | |
200 |
|
202 | |||
201 | meta = {} |
|
203 | meta = {} | |
202 |
|
204 | |||
203 | if self.description: |
|
205 | if self.description: | |
204 | for key, value in self.description['Metadata'].items(): |
|
206 | for key, value in self.description['Metadata'].items(): | |
205 | meta[key] = self.fp[value][()] |
|
207 | meta[key] = self.fp[value][()] | |
206 | else: |
|
208 | else: | |
207 | grp = self.fp['Metadata'] |
|
209 | grp = self.fp['Metadata'] | |
208 | for name in grp: |
|
210 | for name in grp: | |
209 | meta[name] = grp[name][()] |
|
211 | meta[name] = grp[name][()] | |
210 |
|
212 | |||
211 | if self.extras: |
|
213 | if self.extras: | |
212 | for key, value in self.extras.items(): |
|
214 | for key, value in self.extras.items(): | |
213 | meta[key] = value |
|
215 | meta[key] = value | |
214 | self.meta = meta |
|
216 | self.meta = meta | |
215 |
|
217 | |||
216 | return |
|
218 | return | |
217 |
|
219 | |||
218 |
|
220 | |||
219 |
|
221 | |||
220 | def checkForRealPath(self, nextFile, nextDay): |
|
222 | def checkForRealPath(self, nextFile, nextDay): | |
221 |
|
223 | |||
222 | # print("check FRP") |
|
224 | # print("check FRP") | |
223 | # dt = self.startFileDatetime + datetime.timedelta(1) |
|
225 | # dt = self.startFileDatetime + datetime.timedelta(1) | |
224 | # filename = '{}.{}{}'.format(self.path, dt.strftime('%Y%m%d'), self.ext) |
|
226 | # filename = '{}.{}{}'.format(self.path, dt.strftime('%Y%m%d'), self.ext) | |
225 | # fullfilename = os.path.join(self.path, filename) |
|
227 | # fullfilename = os.path.join(self.path, filename) | |
226 | # print("check Path ",fullfilename,filename) |
|
228 | # print("check Path ",fullfilename,filename) | |
227 | # if os.path.exists(fullfilename): |
|
229 | # if os.path.exists(fullfilename): | |
228 | # return fullfilename, filename |
|
230 | # return fullfilename, filename | |
229 | # return None, filename |
|
231 | # return None, filename | |
230 | return None,None |
|
232 | return None,None | |
231 |
|
233 | |||
232 | def __readData(self): |
|
234 | def __readData(self): | |
233 |
|
235 | |||
234 | data = {} |
|
236 | data = {} | |
235 |
|
237 | |||
236 | if self.description: |
|
238 | if self.description: | |
237 | for key, value in self.description['Data'].items(): |
|
239 | for key, value in self.description['Data'].items(): | |
238 | if isinstance(value, str): |
|
240 | if isinstance(value, str): | |
239 | if isinstance(self.fp[value], h5py.Dataset): |
|
241 | if isinstance(self.fp[value], h5py.Dataset): | |
240 | data[key] = self.fp[value][()] |
|
242 | data[key] = self.fp[value][()] | |
241 | elif isinstance(self.fp[value], h5py.Group): |
|
243 | elif isinstance(self.fp[value], h5py.Group): | |
242 | array = [] |
|
244 | array = [] | |
243 | for ch in self.fp[value]: |
|
245 | for ch in self.fp[value]: | |
244 | array.append(self.fp[value][ch][()]) |
|
246 | array.append(self.fp[value][ch][()]) | |
245 | data[key] = numpy.array(array) |
|
247 | data[key] = numpy.array(array) | |
246 | elif isinstance(value, list): |
|
248 | elif isinstance(value, list): | |
247 | array = [] |
|
249 | array = [] | |
248 | for ch in value: |
|
250 | for ch in value: | |
249 | array.append(self.fp[ch][()]) |
|
251 | array.append(self.fp[ch][()]) | |
250 | data[key] = numpy.array(array) |
|
252 | data[key] = numpy.array(array) | |
251 | else: |
|
253 | else: | |
252 | grp = self.fp['Data'] |
|
254 | grp = self.fp['Data'] | |
253 | for name in grp: |
|
255 | for name in grp: | |
254 | if isinstance(grp[name], h5py.Dataset): |
|
256 | if isinstance(grp[name], h5py.Dataset): | |
255 | array = grp[name][()] |
|
257 | array = grp[name][()] | |
256 | elif isinstance(grp[name], h5py.Group): |
|
258 | elif isinstance(grp[name], h5py.Group): | |
257 | array = [] |
|
259 | array = [] | |
258 | for ch in grp[name]: |
|
260 | for ch in grp[name]: | |
259 | array.append(grp[name][ch][()]) |
|
261 | array.append(grp[name][ch][()]) | |
260 | array = numpy.array(array) |
|
262 | array = numpy.array(array) | |
261 | else: |
|
263 | else: | |
262 | log.warning('Unknown type: {}'.format(name)) |
|
264 | log.warning('Unknown type: {}'.format(name)) | |
263 |
|
265 | |||
264 | if name in self.description: |
|
266 | if name in self.description: | |
265 | key = self.description[name] |
|
267 | key = self.description[name] | |
266 | else: |
|
268 | else: | |
267 | key = name |
|
269 | key = name | |
268 | data[key] = array |
|
270 | data[key] = array | |
269 |
|
271 | |||
270 | self.data = data |
|
272 | self.data = data | |
271 | return |
|
273 | return | |
272 |
|
274 | |||
273 | def getData(self): |
|
275 | def getData(self): | |
274 | if not self.isDateTimeInRange(self.startFileDatetime, self.startDate, self.endDate, self.startTime, self.endTime): |
|
276 | if not self.isDateTimeInRange(self.startFileDatetime, self.startDate, self.endDate, self.startTime, self.endTime): | |
275 | self.dataOut.flagNoData = True |
|
277 | self.dataOut.flagNoData = True | |
276 | self.blockIndex = self.blocksPerFile |
|
278 | self.blockIndex = self.blocksPerFile | |
277 | #self.dataOut.error = True TERMINA EL PROGRAMA, removido |
|
279 | #self.dataOut.error = True TERMINA EL PROGRAMA, removido | |
278 | return |
|
280 | return | |
279 | for attr in self.data: |
|
281 | for attr in self.data: | |
|
282 | #print("attr ",attr) | |||
280 | if self.data[attr].ndim == 1: |
|
283 | if self.data[attr].ndim == 1: | |
281 | setattr(self.dataOut, attr, self.data[attr][self.blockIndex]) |
|
284 | setattr(self.dataOut, attr, self.data[attr][self.blockIndex]) | |
282 | else: |
|
285 | else: | |
283 | setattr(self.dataOut, attr, self.data[attr][:, self.blockIndex]) |
|
286 | setattr(self.dataOut, attr, self.data[attr][:, self.blockIndex]) | |
284 |
|
287 | |||
285 | self.dataOut.flagNoData = False |
|
288 | ||
286 | self.blockIndex += 1 |
|
289 | self.blockIndex += 1 | |
287 |
|
290 | |||
288 | if self.blockIndex == 1: |
|
291 | if self.blockIndex == 1: | |
289 | log.log("Block No. {}/{} -> {}".format( |
|
292 | log.log("Block No. {}/{} -> {}".format( | |
290 | self.blockIndex, |
|
293 | self.blockIndex, | |
291 | self.blocksPerFile, |
|
294 | self.blocksPerFile, | |
292 | self.dataOut.datatime.ctime()), self.name) |
|
295 | self.dataOut.datatime.ctime()), self.name) | |
293 | else: |
|
296 | else: | |
294 | log.log("Block No. {}/{} ".format( |
|
297 | log.log("Block No. {}/{} ".format( | |
295 | self.blockIndex, |
|
298 | self.blockIndex, | |
296 | self.blocksPerFile),self.name) |
|
299 | self.blocksPerFile),self.name) | |
297 |
|
300 | |||
298 |
|
301 | self.dataOut.flagNoData = False | ||
|
302 | self.dataOut.error = False | |||
299 | return |
|
303 | return | |
300 |
|
304 | |||
301 | def run(self, **kwargs): |
|
305 | def run(self, **kwargs): | |
302 |
|
306 | |||
303 | if not(self.isConfig): |
|
307 | if not(self.isConfig): | |
304 | self.setup(**kwargs) |
|
308 | self.setup(**kwargs) | |
305 | self.isConfig = True |
|
309 | self.isConfig = True | |
306 |
|
310 | |||
307 | if self.blockIndex == self.blocksPerFile: |
|
311 | if self.blockIndex == self.blocksPerFile: | |
308 | self.setNextFile() |
|
312 | self.setNextFile() | |
309 |
|
313 | |||
310 | self.getData() |
|
314 | self.getData() | |
311 |
|
315 | |||
312 | return |
|
316 | return | |
313 |
|
317 | |||
314 | @MPDecorator |
|
318 | @MPDecorator | |
315 | class HDFWriter(Operation): |
|
319 | class HDFWriter(Operation): | |
316 | """Operation to write HDF5 files. |
|
320 | """Operation to write HDF5 files. | |
317 |
|
321 | |||
318 | The HDF5 file contains by default two groups Data and Metadata where |
|
322 | The HDF5 file contains by default two groups Data and Metadata where | |
319 | you can save any `dataOut` attribute specified by `dataList` and `metadataList` |
|
323 | you can save any `dataOut` attribute specified by `dataList` and `metadataList` | |
320 | parameters, data attributes are normaly time dependent where the metadata |
|
324 | parameters, data attributes are normaly time dependent where the metadata | |
321 | are not. |
|
325 | are not. | |
322 | It is possible to customize the structure of the HDF5 file with the |
|
326 | It is possible to customize the structure of the HDF5 file with the | |
323 | optional description parameter see the examples. |
|
327 | optional description parameter see the examples. | |
324 |
|
328 | |||
325 | Parameters: |
|
329 | Parameters: | |
326 | ----------- |
|
330 | ----------- | |
327 | path : str |
|
331 | path : str | |
328 | Path where files will be saved. |
|
332 | Path where files will be saved. | |
329 | blocksPerFile : int |
|
333 | blocksPerFile : int | |
330 | Number of blocks per file |
|
334 | Number of blocks per file | |
331 | metadataList : list |
|
335 | metadataList : list | |
332 | List of the dataOut attributes that will be saved as metadata |
|
336 | List of the dataOut attributes that will be saved as metadata | |
333 | dataList : int |
|
337 | dataList : int | |
334 | List of the dataOut attributes that will be saved as data |
|
338 | List of the dataOut attributes that will be saved as data | |
335 | setType : bool |
|
339 | setType : bool | |
336 | If True the name of the files corresponds to the timestamp of the data |
|
340 | If True the name of the files corresponds to the timestamp of the data | |
337 | description : dict, optional |
|
341 | description : dict, optional | |
338 | Dictionary with the desired description of the HDF5 file |
|
342 | Dictionary with the desired description of the HDF5 file | |
339 |
|
343 | |||
340 | Examples |
|
344 | Examples | |
341 | -------- |
|
345 | -------- | |
342 |
|
346 | |||
343 | desc = { |
|
347 | desc = { | |
344 | 'data_output': {'winds': ['z', 'w', 'v']}, |
|
348 | 'data_output': {'winds': ['z', 'w', 'v']}, | |
345 | 'utctime': 'timestamps', |
|
349 | 'utctime': 'timestamps', | |
346 | 'heightList': 'heights' |
|
350 | 'heightList': 'heights' | |
347 | } |
|
351 | } | |
348 | desc = { |
|
352 | desc = { | |
349 | 'data_output': ['z', 'w', 'v'], |
|
353 | 'data_output': ['z', 'w', 'v'], | |
350 | 'utctime': 'timestamps', |
|
354 | 'utctime': 'timestamps', | |
351 | 'heightList': 'heights' |
|
355 | 'heightList': 'heights' | |
352 | } |
|
356 | } | |
353 | desc = { |
|
357 | desc = { | |
354 | 'Data': { |
|
358 | 'Data': { | |
355 | 'data_output': 'winds', |
|
359 | 'data_output': 'winds', | |
356 | 'utctime': 'timestamps' |
|
360 | 'utctime': 'timestamps' | |
357 | }, |
|
361 | }, | |
358 | 'Metadata': { |
|
362 | 'Metadata': { | |
359 | 'heightList': 'heights' |
|
363 | 'heightList': 'heights' | |
360 | } |
|
364 | } | |
361 | } |
|
365 | } | |
362 |
|
366 | |||
363 | writer = proc_unit.addOperation(name='HDFWriter') |
|
367 | writer = proc_unit.addOperation(name='HDFWriter') | |
364 | writer.addParameter(name='path', value='/path/to/file') |
|
368 | writer.addParameter(name='path', value='/path/to/file') | |
365 | writer.addParameter(name='blocksPerFile', value='32') |
|
369 | writer.addParameter(name='blocksPerFile', value='32') | |
366 | writer.addParameter(name='metadataList', value='heightList,timeZone') |
|
370 | writer.addParameter(name='metadataList', value='heightList,timeZone') | |
367 | writer.addParameter(name='dataList',value='data_output,utctime') |
|
371 | writer.addParameter(name='dataList',value='data_output,utctime') | |
368 | # writer.addParameter(name='description',value=json.dumps(desc)) |
|
372 | # writer.addParameter(name='description',value=json.dumps(desc)) | |
369 |
|
373 | |||
370 | """ |
|
374 | """ | |
371 |
|
375 | |||
372 | ext = ".hdf5" |
|
376 | ext = ".hdf5" | |
373 | optchar = "D" |
|
377 | optchar = "D" | |
374 | filename = None |
|
378 | filename = None | |
375 | path = None |
|
379 | path = None | |
376 | setFile = None |
|
380 | setFile = None | |
377 | fp = None |
|
381 | fp = None | |
378 | firsttime = True |
|
382 | firsttime = True | |
379 | #Configurations |
|
383 | #Configurations | |
380 | blocksPerFile = None |
|
384 | blocksPerFile = None | |
381 | blockIndex = None |
|
385 | blockIndex = None | |
382 | dataOut = None |
|
386 | dataOut = None | |
383 | #Data Arrays |
|
387 | #Data Arrays | |
384 | dataList = None |
|
388 | dataList = None | |
385 | metadataList = None |
|
389 | metadataList = None | |
386 | currentDay = None |
|
390 | currentDay = None | |
387 | lastTime = None |
|
391 | lastTime = None | |
|
392 | typeTime = "ut" | |||
|
393 | hourLimit = 3 | |||
|
394 | breakDays = True | |||
388 |
|
395 | |||
389 | def __init__(self): |
|
396 | def __init__(self): | |
390 |
|
397 | |||
391 | Operation.__init__(self) |
|
398 | Operation.__init__(self) | |
392 | return |
|
399 | return | |
393 |
|
400 | |||
394 |
def setup(self, path=None, blocksPerFile=10, metadataList=None, dataList=None, setType=None, |
|
401 | def setup(self, path=None, blocksPerFile=10, metadataList=None, dataList=None, setType=None, | |
|
402 | description=None,typeTime = "ut",hourLimit = 3, breakDays=True): | |||
395 | self.path = path |
|
403 | self.path = path | |
396 | self.blocksPerFile = blocksPerFile |
|
404 | self.blocksPerFile = blocksPerFile | |
397 | self.metadataList = metadataList |
|
405 | self.metadataList = metadataList | |
398 | self.dataList = [s.strip() for s in dataList] |
|
406 | self.dataList = [s.strip() for s in dataList] | |
399 | self.setType = setType |
|
407 | self.setType = setType | |
400 | self.description = description |
|
408 | self.description = description | |
|
409 | self.timeZone = typeTime | |||
|
410 | self.hourLimit = hourLimit | |||
|
411 | self.breakDays = breakDays | |||
401 |
|
412 | |||
402 | if self.metadataList is None: |
|
413 | if self.metadataList is None: | |
403 | self.metadataList = self.dataOut.metadata_list |
|
414 | self.metadataList = self.dataOut.metadata_list | |
404 |
|
415 | |||
405 | tableList = [] |
|
416 | tableList = [] | |
406 | dsList = [] |
|
417 | dsList = [] | |
407 |
|
418 | |||
408 | for i in range(len(self.dataList)): |
|
419 | for i in range(len(self.dataList)): | |
409 | dsDict = {} |
|
420 | dsDict = {} | |
410 | if hasattr(self.dataOut, self.dataList[i]): |
|
421 | if hasattr(self.dataOut, self.dataList[i]): | |
411 | dataAux = getattr(self.dataOut, self.dataList[i]) |
|
422 | dataAux = getattr(self.dataOut, self.dataList[i]) | |
412 | dsDict['variable'] = self.dataList[i] |
|
423 | dsDict['variable'] = self.dataList[i] | |
413 | else: |
|
424 | else: | |
414 | log.warning('Attribute {} not found in dataOut', self.name) |
|
425 | log.warning('Attribute {} not found in dataOut', self.name) | |
415 | continue |
|
426 | continue | |
416 |
|
427 | |||
417 | if dataAux is None: |
|
428 | if dataAux is None: | |
418 | continue |
|
429 | continue | |
419 | elif isinstance(dataAux, (int, float, numpy.integer, numpy.float)): |
|
430 | elif isinstance(dataAux, (int, float, numpy.integer, numpy.float)): | |
420 | dsDict['nDim'] = 0 |
|
431 | dsDict['nDim'] = 0 | |
421 | else: |
|
432 | else: | |
422 | dsDict['nDim'] = len(dataAux.shape) |
|
433 | dsDict['nDim'] = len(dataAux.shape) | |
423 | dsDict['shape'] = dataAux.shape |
|
434 | dsDict['shape'] = dataAux.shape | |
424 | dsDict['dsNumber'] = dataAux.shape[0] |
|
435 | dsDict['dsNumber'] = dataAux.shape[0] | |
425 | dsDict['dtype'] = dataAux.dtype |
|
436 | dsDict['dtype'] = dataAux.dtype | |
426 |
|
437 | |||
427 | dsList.append(dsDict) |
|
438 | dsList.append(dsDict) | |
428 |
|
439 | |||
429 | self.dsList = dsList |
|
440 | self.dsList = dsList | |
430 | self.currentDay = self.dataOut.datatime.date() |
|
441 | self.currentDay = self.dataOut.datatime.date() | |
431 |
|
442 | |||
432 | def timeFlag(self): |
|
443 | def timeFlag(self): | |
433 | currentTime = self.dataOut.utctime |
|
444 | currentTime = self.dataOut.utctime | |
434 | timeTuple = time.localtime(currentTime) |
|
445 | if self.timeZone == "lt": | |
|
446 | timeTuple = time.localtime(currentTime) | |||
|
447 | elif self.timeZone == "ut": | |||
|
448 | timeTuple = time.gmtime(currentTime) | |||
|
449 | ||||
435 | dataDay = timeTuple.tm_yday |
|
450 | dataDay = timeTuple.tm_yday | |
436 | #print("time UTC: ",currentTime, self.dataOut.datatime) |
|
451 | #print("time UTC: ",currentTime, self.dataOut.datatime) | |
437 | if self.lastTime is None: |
|
452 | if self.lastTime is None: | |
438 | self.lastTime = currentTime |
|
453 | self.lastTime = currentTime | |
439 | self.currentDay = dataDay |
|
454 | self.currentDay = dataDay | |
440 | return False |
|
455 | return False | |
441 |
|
456 | |||
442 | timeDiff = currentTime - self.lastTime |
|
457 | timeDiff = currentTime - self.lastTime | |
443 |
|
458 | |||
444 |
#Si el dia es diferente o si la diferencia entre un dato y otro supera |
|
459 | #Si el dia es diferente o si la diferencia entre un dato y otro supera self.hourLimit | |
445 | if dataDay != self.currentDay: |
|
460 | if (dataDay != self.currentDay) and self.breakDays: | |
446 | self.currentDay = dataDay |
|
461 | self.currentDay = dataDay | |
447 | return True |
|
462 | return True | |
448 |
elif timeDiff > |
|
463 | elif timeDiff > self.hourLimit*60*60: | |
449 | self.lastTime = currentTime |
|
464 | self.lastTime = currentTime | |
450 | return True |
|
465 | return True | |
451 | else: |
|
466 | else: | |
452 | self.lastTime = currentTime |
|
467 | self.lastTime = currentTime | |
453 | return False |
|
468 | return False | |
454 |
|
469 | |||
455 | def run(self, dataOut, path, blocksPerFile=10, metadataList=None, |
|
470 | def run(self, dataOut,**kwargs): | |
456 | dataList=[], setType=None, description={}): |
|
|||
457 |
|
471 | |||
458 | self.dataOut = dataOut |
|
472 | self.dataOut = dataOut | |
459 | if not(self.isConfig): |
|
473 | if not(self.isConfig): | |
460 | self.setup(path=path, blocksPerFile=blocksPerFile, |
|
474 | self.setup(**kwargs) | |
461 | metadataList=metadataList, dataList=dataList, |
|
|||
462 | setType=setType, description=description) |
|
|||
463 |
|
475 | |||
464 | self.isConfig = True |
|
476 | self.isConfig = True | |
465 | self.setNextFile() |
|
477 | self.setNextFile() | |
466 |
|
478 | |||
467 | self.putData() |
|
479 | self.putData() | |
468 | return |
|
480 | return | |
469 |
|
481 | |||
470 | def setNextFile(self): |
|
482 | def setNextFile(self): | |
471 |
|
483 | |||
472 | ext = self.ext |
|
484 | ext = self.ext | |
473 | path = self.path |
|
485 | path = self.path | |
474 | setFile = self.setFile |
|
486 | setFile = self.setFile | |
475 |
|
487 | if self.timeZone == "lt": | ||
476 |
timeTuple = time. |
|
488 | timeTuple = time.localtime(self.dataOut.utctime) | |
|
489 | elif self.timeZone == "ut": | |||
|
490 | timeTuple = time.gmtime(self.dataOut.utctime) | |||
477 | #print("path: ",timeTuple) |
|
491 | #print("path: ",timeTuple) | |
478 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) |
|
492 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) | |
479 | fullpath = os.path.join(path, subfolder) |
|
493 | fullpath = os.path.join(path, subfolder) | |
480 |
|
494 | |||
481 | if os.path.exists(fullpath): |
|
495 | if os.path.exists(fullpath): | |
482 | filesList = os.listdir(fullpath) |
|
496 | filesList = os.listdir(fullpath) | |
483 | filesList = [k for k in filesList if k.startswith(self.optchar)] |
|
497 | filesList = [k for k in filesList if k.startswith(self.optchar)] | |
484 | if len( filesList ) > 0: |
|
498 | if len( filesList ) > 0: | |
485 | filesList = sorted(filesList, key=str.lower) |
|
499 | filesList = sorted(filesList, key=str.lower) | |
486 | filen = filesList[-1] |
|
500 | filen = filesList[-1] | |
487 | # el filename debera tener el siguiente formato |
|
501 | # el filename debera tener el siguiente formato | |
488 | # 0 1234 567 89A BCDE (hex) |
|
502 | # 0 1234 567 89A BCDE (hex) | |
489 | # x YYYY DDD SSS .ext |
|
503 | # x YYYY DDD SSS .ext | |
490 | if isNumber(filen[8:11]): |
|
504 | if isNumber(filen[8:11]): | |
491 | setFile = int(filen[8:11]) #inicializo mi contador de seteo al seteo del ultimo file |
|
505 | setFile = int(filen[8:11]) #inicializo mi contador de seteo al seteo del ultimo file | |
492 | else: |
|
506 | else: | |
493 | setFile = -1 |
|
507 | setFile = -1 | |
494 | else: |
|
508 | else: | |
495 | setFile = -1 #inicializo mi contador de seteo |
|
509 | setFile = -1 #inicializo mi contador de seteo | |
496 | else: |
|
510 | else: | |
497 | os.makedirs(fullpath) |
|
511 | os.makedirs(fullpath) | |
498 | setFile = -1 #inicializo mi contador de seteo |
|
512 | setFile = -1 #inicializo mi contador de seteo | |
499 |
|
513 | |||
500 | if self.setType is None: |
|
514 | if self.setType is None: | |
501 | setFile += 1 |
|
515 | setFile += 1 | |
502 | file = '%s%4.4d%3.3d%03d%s' % (self.optchar, |
|
516 | file = '%s%4.4d%3.3d%03d%s' % (self.optchar, | |
503 | timeTuple.tm_year, |
|
517 | timeTuple.tm_year, | |
504 | timeTuple.tm_yday, |
|
518 | timeTuple.tm_yday, | |
505 | setFile, |
|
519 | setFile, | |
506 | ext ) |
|
520 | ext ) | |
507 | else: |
|
521 | else: | |
508 | setFile = timeTuple.tm_hour*60+timeTuple.tm_min |
|
522 | setFile = timeTuple.tm_hour*60+timeTuple.tm_min | |
509 | file = '%s%4.4d%3.3d%04d%s' % (self.optchar, |
|
523 | file = '%s%4.4d%3.3d%04d%s' % (self.optchar, | |
510 | timeTuple.tm_year, |
|
524 | timeTuple.tm_year, | |
511 | timeTuple.tm_yday, |
|
525 | timeTuple.tm_yday, | |
512 | setFile, |
|
526 | setFile, | |
513 | ext ) |
|
527 | ext ) | |
514 |
|
528 | |||
515 | self.filename = os.path.join( path, subfolder, file ) |
|
529 | self.filename = os.path.join( path, subfolder, file ) | |
516 |
|
530 | |||
517 | #Setting HDF5 File |
|
531 | #Setting HDF5 File | |
518 | self.fp = h5py.File(self.filename, 'w') |
|
532 | self.fp = h5py.File(self.filename, 'w') | |
519 | #write metadata |
|
533 | #write metadata | |
520 | self.writeMetadata(self.fp) |
|
534 | self.writeMetadata(self.fp) | |
521 | #Write data |
|
535 | #Write data | |
522 | self.writeData(self.fp) |
|
536 | self.writeData(self.fp) | |
523 |
|
537 | |||
524 | def getLabel(self, name, x=None): |
|
538 | def getLabel(self, name, x=None): | |
525 |
|
539 | |||
526 | if x is None: |
|
540 | if x is None: | |
527 | if 'Data' in self.description: |
|
541 | if 'Data' in self.description: | |
528 | data = self.description['Data'] |
|
542 | data = self.description['Data'] | |
529 | if 'Metadata' in self.description: |
|
543 | if 'Metadata' in self.description: | |
530 | data.update(self.description['Metadata']) |
|
544 | data.update(self.description['Metadata']) | |
531 | else: |
|
545 | else: | |
532 | data = self.description |
|
546 | data = self.description | |
533 | if name in data: |
|
547 | if name in data: | |
534 | if isinstance(data[name], str): |
|
548 | if isinstance(data[name], str): | |
535 | return data[name] |
|
549 | return data[name] | |
536 | elif isinstance(data[name], list): |
|
550 | elif isinstance(data[name], list): | |
537 | return None |
|
551 | return None | |
538 | elif isinstance(data[name], dict): |
|
552 | elif isinstance(data[name], dict): | |
539 | for key, value in data[name].items(): |
|
553 | for key, value in data[name].items(): | |
540 | return key |
|
554 | return key | |
541 | return name |
|
555 | return name | |
542 | else: |
|
556 | else: | |
543 | if 'Metadata' in self.description: |
|
557 | if 'Metadata' in self.description: | |
544 | meta = self.description['Metadata'] |
|
558 | meta = self.description['Metadata'] | |
545 | else: |
|
559 | else: | |
546 | meta = self.description |
|
560 | meta = self.description | |
547 | if name in meta: |
|
561 | if name in meta: | |
548 | if isinstance(meta[name], list): |
|
562 | if isinstance(meta[name], list): | |
549 | return meta[name][x] |
|
563 | return meta[name][x] | |
550 | elif isinstance(meta[name], dict): |
|
564 | elif isinstance(meta[name], dict): | |
551 | for key, value in meta[name].items(): |
|
565 | for key, value in meta[name].items(): | |
552 | return value[x] |
|
566 | return value[x] | |
553 | if 'cspc' in name: |
|
567 | if 'cspc' in name: | |
554 | return 'pair{:02d}'.format(x) |
|
568 | return 'pair{:02d}'.format(x) | |
555 | else: |
|
569 | else: | |
556 | return 'channel{:02d}'.format(x) |
|
570 | return 'channel{:02d}'.format(x) | |
557 |
|
571 | |||
558 | def writeMetadata(self, fp): |
|
572 | def writeMetadata(self, fp): | |
559 |
|
573 | |||
560 | if self.description: |
|
574 | if self.description: | |
561 | if 'Metadata' in self.description: |
|
575 | if 'Metadata' in self.description: | |
562 | grp = fp.create_group('Metadata') |
|
576 | grp = fp.create_group('Metadata') | |
563 | else: |
|
577 | else: | |
564 | grp = fp |
|
578 | grp = fp | |
565 | else: |
|
579 | else: | |
566 | grp = fp.create_group('Metadata') |
|
580 | grp = fp.create_group('Metadata') | |
567 |
|
581 | |||
568 | for i in range(len(self.metadataList)): |
|
582 | for i in range(len(self.metadataList)): | |
569 | if not hasattr(self.dataOut, self.metadataList[i]): |
|
583 | if not hasattr(self.dataOut, self.metadataList[i]): | |
570 | log.warning('Metadata: `{}` not found'.format(self.metadataList[i]), self.name) |
|
584 | log.warning('Metadata: `{}` not found'.format(self.metadataList[i]), self.name) | |
571 | continue |
|
585 | continue | |
572 | value = getattr(self.dataOut, self.metadataList[i]) |
|
586 | value = getattr(self.dataOut, self.metadataList[i]) | |
573 | if isinstance(value, bool): |
|
587 | if isinstance(value, bool): | |
574 | if value is True: |
|
588 | if value is True: | |
575 | value = 1 |
|
589 | value = 1 | |
576 | else: |
|
590 | else: | |
577 | value = 0 |
|
591 | value = 0 | |
578 | grp.create_dataset(self.getLabel(self.metadataList[i]), data=value) |
|
592 | grp.create_dataset(self.getLabel(self.metadataList[i]), data=value) | |
579 | return |
|
593 | return | |
580 |
|
594 | |||
581 | def writeData(self, fp): |
|
595 | def writeData(self, fp): | |
582 |
|
596 | |||
583 | if self.description: |
|
597 | if self.description: | |
584 | if 'Data' in self.description: |
|
598 | if 'Data' in self.description: | |
585 | grp = fp.create_group('Data') |
|
599 | grp = fp.create_group('Data') | |
586 | else: |
|
600 | else: | |
587 | grp = fp |
|
601 | grp = fp | |
588 | else: |
|
602 | else: | |
589 | grp = fp.create_group('Data') |
|
603 | grp = fp.create_group('Data') | |
590 |
|
604 | |||
591 | dtsets = [] |
|
605 | dtsets = [] | |
592 | data = [] |
|
606 | data = [] | |
593 |
|
607 | |||
594 | for dsInfo in self.dsList: |
|
608 | for dsInfo in self.dsList: | |
595 | if dsInfo['nDim'] == 0: |
|
609 | if dsInfo['nDim'] == 0: | |
596 | ds = grp.create_dataset( |
|
610 | ds = grp.create_dataset( | |
597 | self.getLabel(dsInfo['variable']), |
|
611 | self.getLabel(dsInfo['variable']), | |
598 | (self.blocksPerFile, ), |
|
612 | (self.blocksPerFile, ), | |
599 | chunks=True, |
|
613 | chunks=True, | |
600 | dtype=numpy.float64) |
|
614 | dtype=numpy.float64) | |
601 | dtsets.append(ds) |
|
615 | dtsets.append(ds) | |
602 | data.append((dsInfo['variable'], -1)) |
|
616 | data.append((dsInfo['variable'], -1)) | |
603 | else: |
|
617 | else: | |
604 | label = self.getLabel(dsInfo['variable']) |
|
618 | label = self.getLabel(dsInfo['variable']) | |
605 | if label is not None: |
|
619 | if label is not None: | |
606 | sgrp = grp.create_group(label) |
|
620 | sgrp = grp.create_group(label) | |
607 | else: |
|
621 | else: | |
608 | sgrp = grp |
|
622 | sgrp = grp | |
609 | for i in range(dsInfo['dsNumber']): |
|
623 | for i in range(dsInfo['dsNumber']): | |
610 | ds = sgrp.create_dataset( |
|
624 | ds = sgrp.create_dataset( | |
611 | self.getLabel(dsInfo['variable'], i), |
|
625 | self.getLabel(dsInfo['variable'], i), | |
612 | (self.blocksPerFile, ) + dsInfo['shape'][1:], |
|
626 | (self.blocksPerFile, ) + dsInfo['shape'][1:], | |
613 | chunks=True, |
|
627 | chunks=True, | |
614 | dtype=dsInfo['dtype']) |
|
628 | dtype=dsInfo['dtype']) | |
615 | dtsets.append(ds) |
|
629 | dtsets.append(ds) | |
616 | data.append((dsInfo['variable'], i)) |
|
630 | data.append((dsInfo['variable'], i)) | |
617 | fp.flush() |
|
631 | fp.flush() | |
618 |
|
632 | |||
619 | log.log('Creating file: {}'.format(fp.filename), self.name) |
|
633 | log.log('Creating file: {}'.format(fp.filename), self.name) | |
620 |
|
634 | |||
621 | self.ds = dtsets |
|
635 | self.ds = dtsets | |
622 | self.data = data |
|
636 | self.data = data | |
623 | self.firsttime = True |
|
637 | self.firsttime = True | |
624 | self.blockIndex = 0 |
|
638 | self.blockIndex = 0 | |
625 | return |
|
639 | return | |
626 |
|
640 | |||
627 | def putData(self): |
|
641 | def putData(self): | |
628 |
|
642 | |||
629 | if (self.blockIndex == self.blocksPerFile) or self.timeFlag(): |
|
643 | if (self.blockIndex == self.blocksPerFile) or self.timeFlag(): | |
630 | self.closeFile() |
|
644 | self.closeFile() | |
631 | self.setNextFile() |
|
645 | self.setNextFile() | |
632 |
|
646 | |||
633 | for i, ds in enumerate(self.ds): |
|
647 | for i, ds in enumerate(self.ds): | |
634 | attr, ch = self.data[i] |
|
648 | attr, ch = self.data[i] | |
635 | if ch == -1: |
|
649 | if ch == -1: | |
636 | ds[self.blockIndex] = getattr(self.dataOut, attr) |
|
650 | ds[self.blockIndex] = getattr(self.dataOut, attr) | |
637 | else: |
|
651 | else: | |
638 | ds[self.blockIndex] = getattr(self.dataOut, attr)[ch] |
|
652 | ds[self.blockIndex] = getattr(self.dataOut, attr)[ch] | |
639 |
|
653 | |||
640 | self.fp.flush() |
|
654 | self.fp.flush() | |
641 | self.blockIndex += 1 |
|
655 | self.blockIndex += 1 | |
642 | if self.blockIndex == 1: |
|
656 | if self.blockIndex == 1: | |
643 | log.log('Block No. {}/{} --> {}'.format(self.blockIndex, self.blocksPerFile,self.dataOut.datatime.ctime()), self.name) |
|
657 | log.log('Block No. {}/{} --> {}'.format(self.blockIndex, self.blocksPerFile,self.dataOut.datatime.ctime()), self.name) | |
644 | else: |
|
658 | else: | |
645 | log.log('Block No. {}/{}'.format(self.blockIndex, self.blocksPerFile), self.name) |
|
659 | log.log('Block No. {}/{}'.format(self.blockIndex, self.blocksPerFile), self.name) | |
646 | return |
|
660 | return | |
647 |
|
661 | |||
648 | def closeFile(self): |
|
662 | def closeFile(self): | |
649 |
|
663 | |||
650 | if self.blockIndex != self.blocksPerFile: |
|
664 | if self.blockIndex != self.blocksPerFile: | |
651 | for ds in self.ds: |
|
665 | for ds in self.ds: | |
652 | ds.resize(self.blockIndex, axis=0) |
|
666 | ds.resize(self.blockIndex, axis=0) | |
653 |
|
667 | |||
654 | if self.fp: |
|
668 | if self.fp: | |
655 | self.fp.flush() |
|
669 | self.fp.flush() | |
656 | self.fp.close() |
|
670 | self.fp.close() | |
657 |
|
671 | |||
658 | def close(self): |
|
672 | def close(self): | |
659 |
|
673 | |||
660 | self.closeFile() |
|
674 | self.closeFile() |
@@ -1,1683 +1,1683 | |||||
1 | # Copyright (c) 2012-2020 Jicamarca Radio Observatory |
|
1 | # Copyright (c) 2012-2020 Jicamarca Radio Observatory | |
2 | # All rights reserved. |
|
2 | # All rights reserved. | |
3 | # |
|
3 | # | |
4 | # Distributed under the terms of the BSD 3-clause license. |
|
4 | # Distributed under the terms of the BSD 3-clause license. | |
5 | """Spectra processing Unit and operations |
|
5 | """Spectra processing Unit and operations | |
6 |
|
6 | |||
7 | Here you will find the processing unit `SpectraProc` and several operations |
|
7 | Here you will find the processing unit `SpectraProc` and several operations | |
8 | to work with Spectra data type |
|
8 | to work with Spectra data type | |
9 | """ |
|
9 | """ | |
10 |
|
10 | |||
11 | import time |
|
11 | import time | |
12 | import itertools |
|
12 | import itertools | |
13 |
|
13 | |||
14 | import numpy |
|
14 | import numpy | |
15 | import math |
|
15 | import math | |
16 |
|
16 | |||
17 | from schainpy.model.proc.jroproc_base import ProcessingUnit, MPDecorator, Operation |
|
17 | from schainpy.model.proc.jroproc_base import ProcessingUnit, MPDecorator, Operation | |
18 | from schainpy.model.data.jrodata import Spectra |
|
18 | from schainpy.model.data.jrodata import Spectra | |
19 | from schainpy.model.data.jrodata import hildebrand_sekhon |
|
19 | from schainpy.model.data.jrodata import hildebrand_sekhon | |
20 | from schainpy.utils import log |
|
20 | from schainpy.utils import log | |
21 |
|
21 | |||
22 | from scipy.optimize import curve_fit |
|
22 | from scipy.optimize import curve_fit | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | class SpectraProc(ProcessingUnit): |
|
25 | class SpectraProc(ProcessingUnit): | |
26 |
|
26 | |||
27 | def __init__(self): |
|
27 | def __init__(self): | |
28 |
|
28 | |||
29 | ProcessingUnit.__init__(self) |
|
29 | ProcessingUnit.__init__(self) | |
30 |
|
30 | |||
31 | self.buffer = None |
|
31 | self.buffer = None | |
32 | self.firstdatatime = None |
|
32 | self.firstdatatime = None | |
33 | self.profIndex = 0 |
|
33 | self.profIndex = 0 | |
34 | self.dataOut = Spectra() |
|
34 | self.dataOut = Spectra() | |
35 | self.id_min = None |
|
35 | self.id_min = None | |
36 | self.id_max = None |
|
36 | self.id_max = None | |
37 | self.setupReq = False #Agregar a todas las unidades de proc |
|
37 | self.setupReq = False #Agregar a todas las unidades de proc | |
38 |
|
38 | |||
39 | def __updateSpecFromVoltage(self): |
|
39 | def __updateSpecFromVoltage(self): | |
40 |
|
40 | |||
41 | self.dataOut.timeZone = self.dataIn.timeZone |
|
41 | self.dataOut.timeZone = self.dataIn.timeZone | |
42 | self.dataOut.dstFlag = self.dataIn.dstFlag |
|
42 | self.dataOut.dstFlag = self.dataIn.dstFlag | |
43 | self.dataOut.errorCount = self.dataIn.errorCount |
|
43 | self.dataOut.errorCount = self.dataIn.errorCount | |
44 | self.dataOut.useLocalTime = self.dataIn.useLocalTime |
|
44 | self.dataOut.useLocalTime = self.dataIn.useLocalTime | |
45 | try: |
|
45 | try: | |
46 | self.dataOut.processingHeaderObj = self.dataIn.processingHeaderObj.copy() |
|
46 | self.dataOut.processingHeaderObj = self.dataIn.processingHeaderObj.copy() | |
47 | except: |
|
47 | except: | |
48 | pass |
|
48 | pass | |
49 | self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy() |
|
49 | self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy() | |
50 | self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy() |
|
50 | self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy() | |
51 | self.dataOut.channelList = self.dataIn.channelList |
|
51 | self.dataOut.channelList = self.dataIn.channelList | |
52 | self.dataOut.heightList = self.dataIn.heightList |
|
52 | self.dataOut.heightList = self.dataIn.heightList | |
53 | self.dataOut.dtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')]) |
|
53 | self.dataOut.dtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')]) | |
54 | self.dataOut.nProfiles = self.dataOut.nFFTPoints |
|
54 | self.dataOut.nProfiles = self.dataOut.nFFTPoints | |
55 | self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock |
|
55 | self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock | |
56 | self.dataOut.utctime = self.firstdatatime |
|
56 | self.dataOut.utctime = self.firstdatatime | |
57 | self.dataOut.flagDecodeData = self.dataIn.flagDecodeData |
|
57 | self.dataOut.flagDecodeData = self.dataIn.flagDecodeData | |
58 | self.dataOut.flagDeflipData = self.dataIn.flagDeflipData |
|
58 | self.dataOut.flagDeflipData = self.dataIn.flagDeflipData | |
59 | self.dataOut.flagShiftFFT = False |
|
59 | self.dataOut.flagShiftFFT = False | |
60 | self.dataOut.nCohInt = self.dataIn.nCohInt |
|
60 | self.dataOut.nCohInt = self.dataIn.nCohInt | |
61 | self.dataOut.nIncohInt = 1 |
|
61 | self.dataOut.nIncohInt = 1 | |
62 | self.dataOut.windowOfFilter = self.dataIn.windowOfFilter |
|
62 | self.dataOut.windowOfFilter = self.dataIn.windowOfFilter | |
63 | self.dataOut.frequency = self.dataIn.frequency |
|
63 | self.dataOut.frequency = self.dataIn.frequency | |
64 | self.dataOut.realtime = self.dataIn.realtime |
|
64 | self.dataOut.realtime = self.dataIn.realtime | |
65 | self.dataOut.azimuth = self.dataIn.azimuth |
|
65 | self.dataOut.azimuth = self.dataIn.azimuth | |
66 | self.dataOut.zenith = self.dataIn.zenith |
|
66 | self.dataOut.zenith = self.dataIn.zenith | |
67 | self.dataOut.codeList = self.dataIn.codeList |
|
67 | self.dataOut.codeList = self.dataIn.codeList | |
68 | self.dataOut.azimuthList = self.dataIn.azimuthList |
|
68 | self.dataOut.azimuthList = self.dataIn.azimuthList | |
69 | self.dataOut.elevationList = self.dataIn.elevationList |
|
69 | self.dataOut.elevationList = self.dataIn.elevationList | |
70 |
|
70 | |||
71 | def __getFft(self): |
|
71 | def __getFft(self): | |
72 | """ |
|
72 | """ | |
73 | Convierte valores de Voltaje a Spectra |
|
73 | Convierte valores de Voltaje a Spectra | |
74 |
|
74 | |||
75 | Affected: |
|
75 | Affected: | |
76 | self.dataOut.data_spc |
|
76 | self.dataOut.data_spc | |
77 | self.dataOut.data_cspc |
|
77 | self.dataOut.data_cspc | |
78 | self.dataOut.data_dc |
|
78 | self.dataOut.data_dc | |
79 | self.dataOut.heightList |
|
79 | self.dataOut.heightList | |
80 | self.profIndex |
|
80 | self.profIndex | |
81 | self.buffer |
|
81 | self.buffer | |
82 | self.dataOut.flagNoData |
|
82 | self.dataOut.flagNoData | |
83 | """ |
|
83 | """ | |
84 | fft_volt = numpy.fft.fft( |
|
84 | fft_volt = numpy.fft.fft( | |
85 | self.buffer, n=self.dataOut.nFFTPoints, axis=1) |
|
85 | self.buffer, n=self.dataOut.nFFTPoints, axis=1) | |
86 | fft_volt = fft_volt.astype(numpy.dtype('complex')) |
|
86 | fft_volt = fft_volt.astype(numpy.dtype('complex')) | |
87 | dc = fft_volt[:, 0, :] |
|
87 | dc = fft_volt[:, 0, :] | |
88 |
|
88 | |||
89 | # calculo de self-spectra |
|
89 | # calculo de self-spectra | |
90 | fft_volt = numpy.fft.fftshift(fft_volt, axes=(1,)) |
|
90 | fft_volt = numpy.fft.fftshift(fft_volt, axes=(1,)) | |
91 | spc = fft_volt * numpy.conjugate(fft_volt) |
|
91 | spc = fft_volt * numpy.conjugate(fft_volt) | |
92 | spc = spc.real |
|
92 | spc = spc.real | |
93 |
|
93 | |||
94 | blocksize = 0 |
|
94 | blocksize = 0 | |
95 | blocksize += dc.size |
|
95 | blocksize += dc.size | |
96 | blocksize += spc.size |
|
96 | blocksize += spc.size | |
97 |
|
97 | |||
98 | cspc = None |
|
98 | cspc = None | |
99 | pairIndex = 0 |
|
99 | pairIndex = 0 | |
100 | if self.dataOut.pairsList != None: |
|
100 | if self.dataOut.pairsList != None: | |
101 | # calculo de cross-spectra |
|
101 | # calculo de cross-spectra | |
102 | cspc = numpy.zeros( |
|
102 | cspc = numpy.zeros( | |
103 | (self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex') |
|
103 | (self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex') | |
104 | for pair in self.dataOut.pairsList: |
|
104 | for pair in self.dataOut.pairsList: | |
105 | if pair[0] not in self.dataOut.channelList: |
|
105 | if pair[0] not in self.dataOut.channelList: | |
106 | raise ValueError("Error getting CrossSpectra: pair 0 of %s is not in channelList = %s" % ( |
|
106 | raise ValueError("Error getting CrossSpectra: pair 0 of %s is not in channelList = %s" % ( | |
107 | str(pair), str(self.dataOut.channelList))) |
|
107 | str(pair), str(self.dataOut.channelList))) | |
108 | if pair[1] not in self.dataOut.channelList: |
|
108 | if pair[1] not in self.dataOut.channelList: | |
109 | raise ValueError("Error getting CrossSpectra: pair 1 of %s is not in channelList = %s" % ( |
|
109 | raise ValueError("Error getting CrossSpectra: pair 1 of %s is not in channelList = %s" % ( | |
110 | str(pair), str(self.dataOut.channelList))) |
|
110 | str(pair), str(self.dataOut.channelList))) | |
111 |
|
111 | |||
112 | cspc[pairIndex, :, :] = fft_volt[pair[0], :, :] * \ |
|
112 | cspc[pairIndex, :, :] = fft_volt[pair[0], :, :] * \ | |
113 | numpy.conjugate(fft_volt[pair[1], :, :]) |
|
113 | numpy.conjugate(fft_volt[pair[1], :, :]) | |
114 | pairIndex += 1 |
|
114 | pairIndex += 1 | |
115 | blocksize += cspc.size |
|
115 | blocksize += cspc.size | |
116 |
|
116 | |||
117 | self.dataOut.data_spc = spc |
|
117 | self.dataOut.data_spc = spc | |
118 | self.dataOut.data_cspc = cspc |
|
118 | self.dataOut.data_cspc = cspc | |
119 | self.dataOut.data_dc = dc |
|
119 | self.dataOut.data_dc = dc | |
120 | self.dataOut.blockSize = blocksize |
|
120 | self.dataOut.blockSize = blocksize | |
121 | self.dataOut.flagShiftFFT = False |
|
121 | self.dataOut.flagShiftFFT = False | |
122 |
|
122 | |||
123 | def run(self, nProfiles=None, nFFTPoints=None, pairsList=None, ippFactor=None, shift_fft=False): |
|
123 | def run(self, nProfiles=None, nFFTPoints=None, pairsList=None, ippFactor=None, shift_fft=False): | |
124 |
|
124 | |||
125 | if self.dataIn.type == "Spectra": |
|
125 | if self.dataIn.type == "Spectra": | |
126 | self.dataOut.copy(self.dataIn) |
|
126 | self.dataOut.copy(self.dataIn) | |
127 | if shift_fft: |
|
127 | if shift_fft: | |
128 | #desplaza a la derecha en el eje 2 determinadas posiciones |
|
128 | #desplaza a la derecha en el eje 2 determinadas posiciones | |
129 | shift = int(self.dataOut.nFFTPoints/2) |
|
129 | shift = int(self.dataOut.nFFTPoints/2) | |
130 | self.dataOut.data_spc = numpy.roll(self.dataOut.data_spc, shift , axis=1) |
|
130 | self.dataOut.data_spc = numpy.roll(self.dataOut.data_spc, shift , axis=1) | |
131 |
|
131 | |||
132 | if self.dataOut.data_cspc is not None: |
|
132 | if self.dataOut.data_cspc is not None: | |
133 | #desplaza a la derecha en el eje 2 determinadas posiciones |
|
133 | #desplaza a la derecha en el eje 2 determinadas posiciones | |
134 | self.dataOut.data_cspc = numpy.roll(self.dataOut.data_cspc, shift, axis=1) |
|
134 | self.dataOut.data_cspc = numpy.roll(self.dataOut.data_cspc, shift, axis=1) | |
135 | if pairsList: |
|
135 | if pairsList: | |
136 | self.__selectPairs(pairsList) |
|
136 | self.__selectPairs(pairsList) | |
137 |
|
137 | |||
138 | elif self.dataIn.type == "Voltage": |
|
138 | elif self.dataIn.type == "Voltage": | |
139 |
|
139 | |||
140 | self.dataOut.flagNoData = True |
|
140 | self.dataOut.flagNoData = True | |
141 |
|
141 | |||
142 | if nFFTPoints == None: |
|
142 | if nFFTPoints == None: | |
143 | raise ValueError("This SpectraProc.run() need nFFTPoints input variable") |
|
143 | raise ValueError("This SpectraProc.run() need nFFTPoints input variable") | |
144 |
|
144 | |||
145 | if nProfiles == None: |
|
145 | if nProfiles == None: | |
146 | nProfiles = nFFTPoints |
|
146 | nProfiles = nFFTPoints | |
147 |
|
147 | |||
148 | if ippFactor == None: |
|
148 | if ippFactor == None: | |
149 | self.dataOut.ippFactor = 1 |
|
149 | self.dataOut.ippFactor = 1 | |
150 |
|
150 | |||
151 | self.dataOut.nFFTPoints = nFFTPoints |
|
151 | self.dataOut.nFFTPoints = nFFTPoints | |
152 |
|
152 | |||
153 | if self.buffer is None: |
|
153 | if self.buffer is None: | |
154 | self.buffer = numpy.zeros((self.dataIn.nChannels, |
|
154 | self.buffer = numpy.zeros((self.dataIn.nChannels, | |
155 | nProfiles, |
|
155 | nProfiles, | |
156 | self.dataIn.nHeights), |
|
156 | self.dataIn.nHeights), | |
157 | dtype='complex') |
|
157 | dtype='complex') | |
158 |
|
158 | |||
159 | if self.dataIn.flagDataAsBlock: |
|
159 | if self.dataIn.flagDataAsBlock: | |
160 | nVoltProfiles = self.dataIn.data.shape[1] |
|
160 | nVoltProfiles = self.dataIn.data.shape[1] | |
161 |
|
161 | |||
162 | if nVoltProfiles == nProfiles: |
|
162 | if nVoltProfiles == nProfiles: | |
163 | self.buffer = self.dataIn.data.copy() |
|
163 | self.buffer = self.dataIn.data.copy() | |
164 | self.profIndex = nVoltProfiles |
|
164 | self.profIndex = nVoltProfiles | |
165 |
|
165 | |||
166 | elif nVoltProfiles < nProfiles: |
|
166 | elif nVoltProfiles < nProfiles: | |
167 |
|
167 | |||
168 | if self.profIndex == 0: |
|
168 | if self.profIndex == 0: | |
169 | self.id_min = 0 |
|
169 | self.id_min = 0 | |
170 | self.id_max = nVoltProfiles |
|
170 | self.id_max = nVoltProfiles | |
171 |
|
171 | |||
172 | self.buffer[:, self.id_min:self.id_max, |
|
172 | self.buffer[:, self.id_min:self.id_max, | |
173 | :] = self.dataIn.data |
|
173 | :] = self.dataIn.data | |
174 | self.profIndex += nVoltProfiles |
|
174 | self.profIndex += nVoltProfiles | |
175 | self.id_min += nVoltProfiles |
|
175 | self.id_min += nVoltProfiles | |
176 | self.id_max += nVoltProfiles |
|
176 | self.id_max += nVoltProfiles | |
177 | else: |
|
177 | else: | |
178 | raise ValueError("The type object %s has %d profiles, it should just has %d profiles" % ( |
|
178 | raise ValueError("The type object %s has %d profiles, it should just has %d profiles" % ( | |
179 | self.dataIn.type, self.dataIn.data.shape[1], nProfiles)) |
|
179 | self.dataIn.type, self.dataIn.data.shape[1], nProfiles)) | |
180 | self.dataOut.flagNoData = True |
|
180 | self.dataOut.flagNoData = True | |
181 | else: |
|
181 | else: | |
182 | self.buffer[:, self.profIndex, :] = self.dataIn.data.copy() |
|
182 | self.buffer[:, self.profIndex, :] = self.dataIn.data.copy() | |
183 | self.profIndex += 1 |
|
183 | self.profIndex += 1 | |
184 |
|
184 | |||
185 | if self.firstdatatime == None: |
|
185 | if self.firstdatatime == None: | |
186 | self.firstdatatime = self.dataIn.utctime |
|
186 | self.firstdatatime = self.dataIn.utctime | |
187 |
|
187 | |||
188 | if self.profIndex == nProfiles: |
|
188 | if self.profIndex == nProfiles: | |
189 | self.__updateSpecFromVoltage() |
|
189 | self.__updateSpecFromVoltage() | |
190 | if pairsList == None: |
|
190 | if pairsList == None: | |
191 | self.dataOut.pairsList = [pair for pair in itertools.combinations(self.dataOut.channelList, 2)] |
|
191 | self.dataOut.pairsList = [pair for pair in itertools.combinations(self.dataOut.channelList, 2)] | |
192 | else: |
|
192 | else: | |
193 | self.dataOut.pairsList = pairsList |
|
193 | self.dataOut.pairsList = pairsList | |
194 | self.__getFft() |
|
194 | self.__getFft() | |
195 | self.dataOut.flagNoData = False |
|
195 | self.dataOut.flagNoData = False | |
196 | self.firstdatatime = None |
|
196 | self.firstdatatime = None | |
197 | self.profIndex = 0 |
|
197 | self.profIndex = 0 | |
198 | else: |
|
198 | else: | |
199 | raise ValueError("The type of input object '%s' is not valid".format( |
|
199 | raise ValueError("The type of input object '%s' is not valid".format( | |
200 | self.dataIn.type)) |
|
200 | self.dataIn.type)) | |
201 |
|
201 | |||
202 | def __selectPairs(self, pairsList): |
|
202 | def __selectPairs(self, pairsList): | |
203 |
|
203 | |||
204 | if not pairsList: |
|
204 | if not pairsList: | |
205 | return |
|
205 | return | |
206 |
|
206 | |||
207 | pairs = [] |
|
207 | pairs = [] | |
208 | pairsIndex = [] |
|
208 | pairsIndex = [] | |
209 |
|
209 | |||
210 | for pair in pairsList: |
|
210 | for pair in pairsList: | |
211 | if pair[0] not in self.dataOut.channelList or pair[1] not in self.dataOut.channelList: |
|
211 | if pair[0] not in self.dataOut.channelList or pair[1] not in self.dataOut.channelList: | |
212 | continue |
|
212 | continue | |
213 | pairs.append(pair) |
|
213 | pairs.append(pair) | |
214 | pairsIndex.append(pairs.index(pair)) |
|
214 | pairsIndex.append(pairs.index(pair)) | |
215 |
|
215 | |||
216 | self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndex] |
|
216 | self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndex] | |
217 | self.dataOut.pairsList = pairs |
|
217 | self.dataOut.pairsList = pairs | |
218 |
|
218 | |||
219 | return |
|
219 | return | |
220 |
|
220 | |||
221 | def selectFFTs(self, minFFT, maxFFT ): |
|
221 | def selectFFTs(self, minFFT, maxFFT ): | |
222 | """ |
|
222 | """ | |
223 | Selecciona un bloque de datos en base a un grupo de valores de puntos FFTs segun el rango |
|
223 | Selecciona un bloque de datos en base a un grupo de valores de puntos FFTs segun el rango | |
224 | minFFT<= FFT <= maxFFT |
|
224 | minFFT<= FFT <= maxFFT | |
225 | """ |
|
225 | """ | |
226 |
|
226 | |||
227 | if (minFFT > maxFFT): |
|
227 | if (minFFT > maxFFT): | |
228 | raise ValueError("Error selecting heights: Height range (%d,%d) is not valid" % (minFFT, maxFFT)) |
|
228 | raise ValueError("Error selecting heights: Height range (%d,%d) is not valid" % (minFFT, maxFFT)) | |
229 |
|
229 | |||
230 | if (minFFT < self.dataOut.getFreqRange()[0]): |
|
230 | if (minFFT < self.dataOut.getFreqRange()[0]): | |
231 | minFFT = self.dataOut.getFreqRange()[0] |
|
231 | minFFT = self.dataOut.getFreqRange()[0] | |
232 |
|
232 | |||
233 | if (maxFFT > self.dataOut.getFreqRange()[-1]): |
|
233 | if (maxFFT > self.dataOut.getFreqRange()[-1]): | |
234 | maxFFT = self.dataOut.getFreqRange()[-1] |
|
234 | maxFFT = self.dataOut.getFreqRange()[-1] | |
235 |
|
235 | |||
236 | minIndex = 0 |
|
236 | minIndex = 0 | |
237 | maxIndex = 0 |
|
237 | maxIndex = 0 | |
238 | FFTs = self.dataOut.getFreqRange() |
|
238 | FFTs = self.dataOut.getFreqRange() | |
239 |
|
239 | |||
240 | inda = numpy.where(FFTs >= minFFT) |
|
240 | inda = numpy.where(FFTs >= minFFT) | |
241 | indb = numpy.where(FFTs <= maxFFT) |
|
241 | indb = numpy.where(FFTs <= maxFFT) | |
242 |
|
242 | |||
243 | try: |
|
243 | try: | |
244 | minIndex = inda[0][0] |
|
244 | minIndex = inda[0][0] | |
245 | except: |
|
245 | except: | |
246 | minIndex = 0 |
|
246 | minIndex = 0 | |
247 |
|
247 | |||
248 | try: |
|
248 | try: | |
249 | maxIndex = indb[0][-1] |
|
249 | maxIndex = indb[0][-1] | |
250 | except: |
|
250 | except: | |
251 | maxIndex = len(FFTs) |
|
251 | maxIndex = len(FFTs) | |
252 |
|
252 | |||
253 | self.selectFFTsByIndex(minIndex, maxIndex) |
|
253 | self.selectFFTsByIndex(minIndex, maxIndex) | |
254 |
|
254 | |||
255 | return 1 |
|
255 | return 1 | |
256 |
|
256 | |||
257 | def getBeaconSignal(self, tauindex=0, channelindex=0, hei_ref=None): |
|
257 | def getBeaconSignal(self, tauindex=0, channelindex=0, hei_ref=None): | |
258 | newheis = numpy.where( |
|
258 | newheis = numpy.where( | |
259 | self.dataOut.heightList > self.dataOut.radarControllerHeaderObj.Taus[tauindex]) |
|
259 | self.dataOut.heightList > self.dataOut.radarControllerHeaderObj.Taus[tauindex]) | |
260 |
|
260 | |||
261 | if hei_ref != None: |
|
261 | if hei_ref != None: | |
262 | newheis = numpy.where(self.dataOut.heightList > hei_ref) |
|
262 | newheis = numpy.where(self.dataOut.heightList > hei_ref) | |
263 |
|
263 | |||
264 | minIndex = min(newheis[0]) |
|
264 | minIndex = min(newheis[0]) | |
265 | maxIndex = max(newheis[0]) |
|
265 | maxIndex = max(newheis[0]) | |
266 | data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1] |
|
266 | data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1] | |
267 | heightList = self.dataOut.heightList[minIndex:maxIndex + 1] |
|
267 | heightList = self.dataOut.heightList[minIndex:maxIndex + 1] | |
268 |
|
268 | |||
269 | # determina indices |
|
269 | # determina indices | |
270 | nheis = int(self.dataOut.radarControllerHeaderObj.txB / |
|
270 | nheis = int(self.dataOut.radarControllerHeaderObj.txB / | |
271 | (self.dataOut.heightList[1] - self.dataOut.heightList[0])) |
|
271 | (self.dataOut.heightList[1] - self.dataOut.heightList[0])) | |
272 | avg_dB = 10 * \ |
|
272 | avg_dB = 10 * \ | |
273 | numpy.log10(numpy.sum(data_spc[channelindex, :, :], axis=0)) |
|
273 | numpy.log10(numpy.sum(data_spc[channelindex, :, :], axis=0)) | |
274 | beacon_dB = numpy.sort(avg_dB)[-nheis:] |
|
274 | beacon_dB = numpy.sort(avg_dB)[-nheis:] | |
275 | beacon_heiIndexList = [] |
|
275 | beacon_heiIndexList = [] | |
276 | for val in avg_dB.tolist(): |
|
276 | for val in avg_dB.tolist(): | |
277 | if val >= beacon_dB[0]: |
|
277 | if val >= beacon_dB[0]: | |
278 | beacon_heiIndexList.append(avg_dB.tolist().index(val)) |
|
278 | beacon_heiIndexList.append(avg_dB.tolist().index(val)) | |
279 |
|
279 | |||
280 | #data_spc = data_spc[:,:,beacon_heiIndexList] |
|
280 | #data_spc = data_spc[:,:,beacon_heiIndexList] | |
281 | data_cspc = None |
|
281 | data_cspc = None | |
282 | if self.dataOut.data_cspc is not None: |
|
282 | if self.dataOut.data_cspc is not None: | |
283 | data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1] |
|
283 | data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1] | |
284 | #data_cspc = data_cspc[:,:,beacon_heiIndexList] |
|
284 | #data_cspc = data_cspc[:,:,beacon_heiIndexList] | |
285 |
|
285 | |||
286 | data_dc = None |
|
286 | data_dc = None | |
287 | if self.dataOut.data_dc is not None: |
|
287 | if self.dataOut.data_dc is not None: | |
288 | data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1] |
|
288 | data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1] | |
289 | #data_dc = data_dc[:,beacon_heiIndexList] |
|
289 | #data_dc = data_dc[:,beacon_heiIndexList] | |
290 |
|
290 | |||
291 | self.dataOut.data_spc = data_spc |
|
291 | self.dataOut.data_spc = data_spc | |
292 | self.dataOut.data_cspc = data_cspc |
|
292 | self.dataOut.data_cspc = data_cspc | |
293 | self.dataOut.data_dc = data_dc |
|
293 | self.dataOut.data_dc = data_dc | |
294 | self.dataOut.heightList = heightList |
|
294 | self.dataOut.heightList = heightList | |
295 | self.dataOut.beacon_heiIndexList = beacon_heiIndexList |
|
295 | self.dataOut.beacon_heiIndexList = beacon_heiIndexList | |
296 |
|
296 | |||
297 | return 1 |
|
297 | return 1 | |
298 |
|
298 | |||
299 | def selectFFTsByIndex(self, minIndex, maxIndex): |
|
299 | def selectFFTsByIndex(self, minIndex, maxIndex): | |
300 | """ |
|
300 | """ | |
301 |
|
301 | |||
302 | """ |
|
302 | """ | |
303 |
|
303 | |||
304 | if (minIndex < 0) or (minIndex > maxIndex): |
|
304 | if (minIndex < 0) or (minIndex > maxIndex): | |
305 | raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % (minIndex, maxIndex)) |
|
305 | raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % (minIndex, maxIndex)) | |
306 |
|
306 | |||
307 | if (maxIndex >= self.dataOut.nProfiles): |
|
307 | if (maxIndex >= self.dataOut.nProfiles): | |
308 | maxIndex = self.dataOut.nProfiles-1 |
|
308 | maxIndex = self.dataOut.nProfiles-1 | |
309 |
|
309 | |||
310 | #Spectra |
|
310 | #Spectra | |
311 | data_spc = self.dataOut.data_spc[:,minIndex:maxIndex+1,:] |
|
311 | data_spc = self.dataOut.data_spc[:,minIndex:maxIndex+1,:] | |
312 |
|
312 | |||
313 | data_cspc = None |
|
313 | data_cspc = None | |
314 | if self.dataOut.data_cspc is not None: |
|
314 | if self.dataOut.data_cspc is not None: | |
315 | data_cspc = self.dataOut.data_cspc[:,minIndex:maxIndex+1,:] |
|
315 | data_cspc = self.dataOut.data_cspc[:,minIndex:maxIndex+1,:] | |
316 |
|
316 | |||
317 | data_dc = None |
|
317 | data_dc = None | |
318 | if self.dataOut.data_dc is not None: |
|
318 | if self.dataOut.data_dc is not None: | |
319 | data_dc = self.dataOut.data_dc[minIndex:maxIndex+1,:] |
|
319 | data_dc = self.dataOut.data_dc[minIndex:maxIndex+1,:] | |
320 |
|
320 | |||
321 | self.dataOut.data_spc = data_spc |
|
321 | self.dataOut.data_spc = data_spc | |
322 | self.dataOut.data_cspc = data_cspc |
|
322 | self.dataOut.data_cspc = data_cspc | |
323 | self.dataOut.data_dc = data_dc |
|
323 | self.dataOut.data_dc = data_dc | |
324 |
|
324 | |||
325 | self.dataOut.ippSeconds = self.dataOut.ippSeconds*(self.dataOut.nFFTPoints / numpy.shape(data_cspc)[1]) |
|
325 | self.dataOut.ippSeconds = self.dataOut.ippSeconds*(self.dataOut.nFFTPoints / numpy.shape(data_cspc)[1]) | |
326 | self.dataOut.nFFTPoints = numpy.shape(data_cspc)[1] |
|
326 | self.dataOut.nFFTPoints = numpy.shape(data_cspc)[1] | |
327 | self.dataOut.profilesPerBlock = numpy.shape(data_cspc)[1] |
|
327 | self.dataOut.profilesPerBlock = numpy.shape(data_cspc)[1] | |
328 |
|
328 | |||
329 | return 1 |
|
329 | return 1 | |
330 |
|
330 | |||
331 | def getNoise(self, minHei=None, maxHei=None, minVel=None, maxVel=None): |
|
331 | def getNoise(self, minHei=None, maxHei=None, minVel=None, maxVel=None): | |
332 | # validacion de rango |
|
332 | # validacion de rango | |
333 | if minHei == None: |
|
333 | if minHei == None: | |
334 | minHei = self.dataOut.heightList[0] |
|
334 | minHei = self.dataOut.heightList[0] | |
335 |
|
335 | |||
336 | if maxHei == None: |
|
336 | if maxHei == None: | |
337 | maxHei = self.dataOut.heightList[-1] |
|
337 | maxHei = self.dataOut.heightList[-1] | |
338 |
|
338 | |||
339 | if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei): |
|
339 | if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei): | |
340 | print('minHei: %.2f is out of the heights range' % (minHei)) |
|
340 | print('minHei: %.2f is out of the heights range' % (minHei)) | |
341 | print('minHei is setting to %.2f' % (self.dataOut.heightList[0])) |
|
341 | print('minHei is setting to %.2f' % (self.dataOut.heightList[0])) | |
342 | minHei = self.dataOut.heightList[0] |
|
342 | minHei = self.dataOut.heightList[0] | |
343 |
|
343 | |||
344 | if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei): |
|
344 | if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei): | |
345 | print('maxHei: %.2f is out of the heights range' % (maxHei)) |
|
345 | print('maxHei: %.2f is out of the heights range' % (maxHei)) | |
346 | print('maxHei is setting to %.2f' % (self.dataOut.heightList[-1])) |
|
346 | print('maxHei is setting to %.2f' % (self.dataOut.heightList[-1])) | |
347 | maxHei = self.dataOut.heightList[-1] |
|
347 | maxHei = self.dataOut.heightList[-1] | |
348 |
|
348 | |||
349 | # validacion de velocidades |
|
349 | # validacion de velocidades | |
350 | velrange = self.dataOut.getVelRange(1) |
|
350 | velrange = self.dataOut.getVelRange(1) | |
351 |
|
351 | |||
352 | if minVel == None: |
|
352 | if minVel == None: | |
353 | minVel = velrange[0] |
|
353 | minVel = velrange[0] | |
354 |
|
354 | |||
355 | if maxVel == None: |
|
355 | if maxVel == None: | |
356 | maxVel = velrange[-1] |
|
356 | maxVel = velrange[-1] | |
357 |
|
357 | |||
358 | if (minVel < velrange[0]) or (minVel > maxVel): |
|
358 | if (minVel < velrange[0]) or (minVel > maxVel): | |
359 | print('minVel: %.2f is out of the velocity range' % (minVel)) |
|
359 | print('minVel: %.2f is out of the velocity range' % (minVel)) | |
360 | print('minVel is setting to %.2f' % (velrange[0])) |
|
360 | print('minVel is setting to %.2f' % (velrange[0])) | |
361 | minVel = velrange[0] |
|
361 | minVel = velrange[0] | |
362 |
|
362 | |||
363 | if (maxVel > velrange[-1]) or (maxVel < minVel): |
|
363 | if (maxVel > velrange[-1]) or (maxVel < minVel): | |
364 | print('maxVel: %.2f is out of the velocity range' % (maxVel)) |
|
364 | print('maxVel: %.2f is out of the velocity range' % (maxVel)) | |
365 | print('maxVel is setting to %.2f' % (velrange[-1])) |
|
365 | print('maxVel is setting to %.2f' % (velrange[-1])) | |
366 | maxVel = velrange[-1] |
|
366 | maxVel = velrange[-1] | |
367 |
|
367 | |||
368 | # seleccion de indices para rango |
|
368 | # seleccion de indices para rango | |
369 | minIndex = 0 |
|
369 | minIndex = 0 | |
370 | maxIndex = 0 |
|
370 | maxIndex = 0 | |
371 | heights = self.dataOut.heightList |
|
371 | heights = self.dataOut.heightList | |
372 |
|
372 | |||
373 | inda = numpy.where(heights >= minHei) |
|
373 | inda = numpy.where(heights >= minHei) | |
374 | indb = numpy.where(heights <= maxHei) |
|
374 | indb = numpy.where(heights <= maxHei) | |
375 |
|
375 | |||
376 | try: |
|
376 | try: | |
377 | minIndex = inda[0][0] |
|
377 | minIndex = inda[0][0] | |
378 | except: |
|
378 | except: | |
379 | minIndex = 0 |
|
379 | minIndex = 0 | |
380 |
|
380 | |||
381 | try: |
|
381 | try: | |
382 | maxIndex = indb[0][-1] |
|
382 | maxIndex = indb[0][-1] | |
383 | except: |
|
383 | except: | |
384 | maxIndex = len(heights) |
|
384 | maxIndex = len(heights) | |
385 |
|
385 | |||
386 | if (minIndex < 0) or (minIndex > maxIndex): |
|
386 | if (minIndex < 0) or (minIndex > maxIndex): | |
387 | raise ValueError("some value in (%d,%d) is not valid" % ( |
|
387 | raise ValueError("some value in (%d,%d) is not valid" % ( | |
388 | minIndex, maxIndex)) |
|
388 | minIndex, maxIndex)) | |
389 |
|
389 | |||
390 | if (maxIndex >= self.dataOut.nHeights): |
|
390 | if (maxIndex >= self.dataOut.nHeights): | |
391 | maxIndex = self.dataOut.nHeights - 1 |
|
391 | maxIndex = self.dataOut.nHeights - 1 | |
392 |
|
392 | |||
393 | # seleccion de indices para velocidades |
|
393 | # seleccion de indices para velocidades | |
394 | indminvel = numpy.where(velrange >= minVel) |
|
394 | indminvel = numpy.where(velrange >= minVel) | |
395 | indmaxvel = numpy.where(velrange <= maxVel) |
|
395 | indmaxvel = numpy.where(velrange <= maxVel) | |
396 | try: |
|
396 | try: | |
397 | minIndexVel = indminvel[0][0] |
|
397 | minIndexVel = indminvel[0][0] | |
398 | except: |
|
398 | except: | |
399 | minIndexVel = 0 |
|
399 | minIndexVel = 0 | |
400 |
|
400 | |||
401 | try: |
|
401 | try: | |
402 | maxIndexVel = indmaxvel[0][-1] |
|
402 | maxIndexVel = indmaxvel[0][-1] | |
403 | except: |
|
403 | except: | |
404 | maxIndexVel = len(velrange) |
|
404 | maxIndexVel = len(velrange) | |
405 |
|
405 | |||
406 | # seleccion del espectro |
|
406 | # seleccion del espectro | |
407 | data_spc = self.dataOut.data_spc[:, |
|
407 | data_spc = self.dataOut.data_spc[:, | |
408 | minIndexVel:maxIndexVel + 1, minIndex:maxIndex + 1] |
|
408 | minIndexVel:maxIndexVel + 1, minIndex:maxIndex + 1] | |
409 | # estimacion de ruido |
|
409 | # estimacion de ruido | |
410 | noise = numpy.zeros(self.dataOut.nChannels) |
|
410 | noise = numpy.zeros(self.dataOut.nChannels) | |
411 |
|
411 | |||
412 | for channel in range(self.dataOut.nChannels): |
|
412 | for channel in range(self.dataOut.nChannels): | |
413 | daux = data_spc[channel, :, :] |
|
413 | daux = data_spc[channel, :, :] | |
414 | sortdata = numpy.sort(daux, axis=None) |
|
414 | sortdata = numpy.sort(daux, axis=None) | |
415 | noise[channel] = hildebrand_sekhon(sortdata, self.dataOut.nIncohInt) |
|
415 | noise[channel] = hildebrand_sekhon(sortdata, self.dataOut.nIncohInt) | |
416 |
|
416 | |||
417 | self.dataOut.noise_estimation = noise.copy() |
|
417 | self.dataOut.noise_estimation = noise.copy() | |
418 |
|
418 | |||
419 | return 1 |
|
419 | return 1 | |
420 |
|
420 | |||
421 | class removeDC(Operation): |
|
421 | class removeDC(Operation): | |
422 |
|
422 | |||
423 | def run(self, dataOut, mode=2): |
|
423 | def run(self, dataOut, mode=2): | |
424 | self.dataOut = dataOut |
|
424 | self.dataOut = dataOut | |
425 | jspectra = self.dataOut.data_spc |
|
425 | jspectra = self.dataOut.data_spc | |
426 | jcspectra = self.dataOut.data_cspc |
|
426 | jcspectra = self.dataOut.data_cspc | |
427 |
|
427 | |||
428 | num_chan = jspectra.shape[0] |
|
428 | num_chan = jspectra.shape[0] | |
429 | num_hei = jspectra.shape[2] |
|
429 | num_hei = jspectra.shape[2] | |
430 |
|
430 | |||
431 | if jcspectra is not None: |
|
431 | if jcspectra is not None: | |
432 | jcspectraExist = True |
|
432 | jcspectraExist = True | |
433 | num_pairs = jcspectra.shape[0] |
|
433 | num_pairs = jcspectra.shape[0] | |
434 | else: |
|
434 | else: | |
435 | jcspectraExist = False |
|
435 | jcspectraExist = False | |
436 |
|
436 | |||
437 | freq_dc = int(jspectra.shape[1] / 2) |
|
437 | freq_dc = int(jspectra.shape[1] / 2) | |
438 | ind_vel = numpy.array([-2, -1, 1, 2]) + freq_dc |
|
438 | ind_vel = numpy.array([-2, -1, 1, 2]) + freq_dc | |
439 | ind_vel = ind_vel.astype(int) |
|
439 | ind_vel = ind_vel.astype(int) | |
440 |
|
440 | |||
441 | if ind_vel[0] < 0: |
|
441 | if ind_vel[0] < 0: | |
442 | ind_vel[list(range(0, 1))] = ind_vel[list(range(0, 1))] + self.num_prof |
|
442 | ind_vel[list(range(0, 1))] = ind_vel[list(range(0, 1))] + self.num_prof | |
443 |
|
443 | |||
444 | if mode == 1: |
|
444 | if mode == 1: | |
445 | jspectra[:, freq_dc, :] = ( |
|
445 | jspectra[:, freq_dc, :] = ( | |
446 | jspectra[:, ind_vel[1], :] + jspectra[:, ind_vel[2], :]) / 2 # CORRECCION |
|
446 | jspectra[:, ind_vel[1], :] + jspectra[:, ind_vel[2], :]) / 2 # CORRECCION | |
447 |
|
447 | |||
448 | if jcspectraExist: |
|
448 | if jcspectraExist: | |
449 | jcspectra[:, freq_dc, :] = ( |
|
449 | jcspectra[:, freq_dc, :] = ( | |
450 | jcspectra[:, ind_vel[1], :] + jcspectra[:, ind_vel[2], :]) / 2 |
|
450 | jcspectra[:, ind_vel[1], :] + jcspectra[:, ind_vel[2], :]) / 2 | |
451 |
|
451 | |||
452 | if mode == 2: |
|
452 | if mode == 2: | |
453 |
|
453 | |||
454 | vel = numpy.array([-2, -1, 1, 2]) |
|
454 | vel = numpy.array([-2, -1, 1, 2]) | |
455 | xx = numpy.zeros([4, 4]) |
|
455 | xx = numpy.zeros([4, 4]) | |
456 |
|
456 | |||
457 | for fil in range(4): |
|
457 | for fil in range(4): | |
458 | xx[fil, :] = vel[fil]**numpy.asarray(list(range(4))) |
|
458 | xx[fil, :] = vel[fil]**numpy.asarray(list(range(4))) | |
459 |
|
459 | |||
460 | xx_inv = numpy.linalg.inv(xx) |
|
460 | xx_inv = numpy.linalg.inv(xx) | |
461 | xx_aux = xx_inv[0, :] |
|
461 | xx_aux = xx_inv[0, :] | |
462 |
|
462 | |||
463 | for ich in range(num_chan): |
|
463 | for ich in range(num_chan): | |
464 | yy = jspectra[ich, ind_vel, :] |
|
464 | yy = jspectra[ich, ind_vel, :] | |
465 | jspectra[ich, freq_dc, :] = numpy.dot(xx_aux, yy) |
|
465 | jspectra[ich, freq_dc, :] = numpy.dot(xx_aux, yy) | |
466 |
|
466 | |||
467 | junkid = jspectra[ich, freq_dc, :] <= 0 |
|
467 | junkid = jspectra[ich, freq_dc, :] <= 0 | |
468 | cjunkid = sum(junkid) |
|
468 | cjunkid = sum(junkid) | |
469 |
|
469 | |||
470 | if cjunkid.any(): |
|
470 | if cjunkid.any(): | |
471 | jspectra[ich, freq_dc, junkid.nonzero()] = ( |
|
471 | jspectra[ich, freq_dc, junkid.nonzero()] = ( | |
472 | jspectra[ich, ind_vel[1], junkid] + jspectra[ich, ind_vel[2], junkid]) / 2 |
|
472 | jspectra[ich, ind_vel[1], junkid] + jspectra[ich, ind_vel[2], junkid]) / 2 | |
473 |
|
473 | |||
474 | if jcspectraExist: |
|
474 | if jcspectraExist: | |
475 | for ip in range(num_pairs): |
|
475 | for ip in range(num_pairs): | |
476 | yy = jcspectra[ip, ind_vel, :] |
|
476 | yy = jcspectra[ip, ind_vel, :] | |
477 | jcspectra[ip, freq_dc, :] = numpy.dot(xx_aux, yy) |
|
477 | jcspectra[ip, freq_dc, :] = numpy.dot(xx_aux, yy) | |
478 |
|
478 | |||
479 | self.dataOut.data_spc = jspectra |
|
479 | self.dataOut.data_spc = jspectra | |
480 | self.dataOut.data_cspc = jcspectra |
|
480 | self.dataOut.data_cspc = jcspectra | |
481 |
|
481 | |||
482 | return self.dataOut |
|
482 | return self.dataOut | |
483 |
|
483 | |||
484 | # import matplotlib.pyplot as plt |
|
484 | # import matplotlib.pyplot as plt | |
485 |
|
485 | |||
486 | def fit_func( x, a0, a1, a2): #, a3, a4, a5): |
|
486 | def fit_func( x, a0, a1, a2): #, a3, a4, a5): | |
487 | z = (x - a1) / a2 |
|
487 | z = (x - a1) / a2 | |
488 | y = a0 * numpy.exp(-z**2 / a2) #+ a3 + a4 * x + a5 * x**2 |
|
488 | y = a0 * numpy.exp(-z**2 / a2) #+ a3 + a4 * x + a5 * x**2 | |
489 | return y |
|
489 | return y | |
490 |
|
490 | |||
491 |
|
491 | |||
492 | class CleanRayleigh(Operation): |
|
492 | class CleanRayleigh(Operation): | |
493 |
|
493 | |||
494 | def __init__(self): |
|
494 | def __init__(self): | |
495 |
|
495 | |||
496 | Operation.__init__(self) |
|
496 | Operation.__init__(self) | |
497 | self.i=0 |
|
497 | self.i=0 | |
498 | self.isConfig = False |
|
498 | self.isConfig = False | |
499 | self.__dataReady = False |
|
499 | self.__dataReady = False | |
500 | self.__profIndex = 0 |
|
500 | self.__profIndex = 0 | |
501 | self.byTime = False |
|
501 | self.byTime = False | |
502 | self.byProfiles = False |
|
502 | self.byProfiles = False | |
503 |
|
503 | |||
504 | self.bloques = None |
|
504 | self.bloques = None | |
505 | self.bloque0 = None |
|
505 | self.bloque0 = None | |
506 |
|
506 | |||
507 | self.index = 0 |
|
507 | self.index = 0 | |
508 |
|
508 | |||
509 | self.buffer = 0 |
|
509 | self.buffer = 0 | |
510 | self.buffer2 = 0 |
|
510 | self.buffer2 = 0 | |
511 | self.buffer3 = 0 |
|
511 | self.buffer3 = 0 | |
512 |
|
512 | |||
513 |
|
513 | |||
514 | def setup(self,dataOut,min_hei,max_hei,n, timeInterval,factor_stdv): |
|
514 | def setup(self,dataOut,min_hei,max_hei,n, timeInterval,factor_stdv): | |
515 |
|
515 | |||
516 | self.nChannels = dataOut.nChannels |
|
516 | self.nChannels = dataOut.nChannels | |
517 | self.nProf = dataOut.nProfiles |
|
517 | self.nProf = dataOut.nProfiles | |
518 | self.nPairs = dataOut.data_cspc.shape[0] |
|
518 | self.nPairs = dataOut.data_cspc.shape[0] | |
519 | self.pairsArray = numpy.array(dataOut.pairsList) |
|
519 | self.pairsArray = numpy.array(dataOut.pairsList) | |
520 | self.spectra = dataOut.data_spc |
|
520 | self.spectra = dataOut.data_spc | |
521 | self.cspectra = dataOut.data_cspc |
|
521 | self.cspectra = dataOut.data_cspc | |
522 | self.heights = dataOut.heightList #alturas totales |
|
522 | self.heights = dataOut.heightList #alturas totales | |
523 | self.nHeights = len(self.heights) |
|
523 | self.nHeights = len(self.heights) | |
524 | self.min_hei = min_hei |
|
524 | self.min_hei = min_hei | |
525 | self.max_hei = max_hei |
|
525 | self.max_hei = max_hei | |
526 | if (self.min_hei == None): |
|
526 | if (self.min_hei == None): | |
527 | self.min_hei = 0 |
|
527 | self.min_hei = 0 | |
528 | if (self.max_hei == None): |
|
528 | if (self.max_hei == None): | |
529 | self.max_hei = dataOut.heightList[-1] |
|
529 | self.max_hei = dataOut.heightList[-1] | |
530 | self.hval = ((self.max_hei>=self.heights) & (self.heights >= self.min_hei)).nonzero() |
|
530 | self.hval = ((self.max_hei>=self.heights) & (self.heights >= self.min_hei)).nonzero() | |
531 | self.heightsClean = self.heights[self.hval] #alturas filtradas |
|
531 | self.heightsClean = self.heights[self.hval] #alturas filtradas | |
532 | self.hval = self.hval[0] # forma (N,), an solo N elementos -> Indices de alturas |
|
532 | self.hval = self.hval[0] # forma (N,), an solo N elementos -> Indices de alturas | |
533 | self.nHeightsClean = len(self.heightsClean) |
|
533 | self.nHeightsClean = len(self.heightsClean) | |
534 | self.channels = dataOut.channelList |
|
534 | self.channels = dataOut.channelList | |
535 | self.nChan = len(self.channels) |
|
535 | self.nChan = len(self.channels) | |
536 | self.nIncohInt = dataOut.nIncohInt |
|
536 | self.nIncohInt = dataOut.nIncohInt | |
537 | self.__initime = dataOut.utctime |
|
537 | self.__initime = dataOut.utctime | |
538 | self.maxAltInd = self.hval[-1]+1 |
|
538 | self.maxAltInd = self.hval[-1]+1 | |
539 | self.minAltInd = self.hval[0] |
|
539 | self.minAltInd = self.hval[0] | |
540 |
|
540 | |||
541 | self.crosspairs = dataOut.pairsList |
|
541 | self.crosspairs = dataOut.pairsList | |
542 | self.nPairs = len(self.crosspairs) |
|
542 | self.nPairs = len(self.crosspairs) | |
543 | self.normFactor = dataOut.normFactor |
|
543 | self.normFactor = dataOut.normFactor | |
544 | self.nFFTPoints = dataOut.nFFTPoints |
|
544 | self.nFFTPoints = dataOut.nFFTPoints | |
545 | self.ippSeconds = dataOut.ippSeconds |
|
545 | self.ippSeconds = dataOut.ippSeconds | |
546 | self.currentTime = self.__initime |
|
546 | self.currentTime = self.__initime | |
547 | self.pairsArray = numpy.array(dataOut.pairsList) |
|
547 | self.pairsArray = numpy.array(dataOut.pairsList) | |
548 | self.factor_stdv = factor_stdv |
|
548 | self.factor_stdv = factor_stdv | |
549 | #print("CHANNELS: ",[x for x in self.channels]) |
|
549 | #print("CHANNELS: ",[x for x in self.channels]) | |
550 |
|
550 | |||
551 | if n != None : |
|
551 | if n != None : | |
552 | self.byProfiles = True |
|
552 | self.byProfiles = True | |
553 | self.nIntProfiles = n |
|
553 | self.nIntProfiles = n | |
554 | else: |
|
554 | else: | |
555 | self.__integrationtime = timeInterval |
|
555 | self.__integrationtime = timeInterval | |
556 |
|
556 | |||
557 | self.__dataReady = False |
|
557 | self.__dataReady = False | |
558 | self.isConfig = True |
|
558 | self.isConfig = True | |
559 |
|
559 | |||
560 |
|
560 | |||
561 |
|
561 | |||
562 | def run(self, dataOut,min_hei=None,max_hei=None, n=None, timeInterval=10,factor_stdv=2.5): |
|
562 | def run(self, dataOut,min_hei=None,max_hei=None, n=None, timeInterval=10,factor_stdv=2.5): | |
563 | #print (dataOut.utctime) |
|
563 | #print (dataOut.utctime) | |
564 | if not self.isConfig : |
|
564 | if not self.isConfig : | |
565 | #print("Setting config") |
|
565 | #print("Setting config") | |
566 | self.setup(dataOut, min_hei,max_hei,n,timeInterval,factor_stdv) |
|
566 | self.setup(dataOut, min_hei,max_hei,n,timeInterval,factor_stdv) | |
567 | #print("Config Done") |
|
567 | #print("Config Done") | |
568 | tini=dataOut.utctime |
|
568 | tini=dataOut.utctime | |
569 |
|
569 | |||
570 | if self.byProfiles: |
|
570 | if self.byProfiles: | |
571 | if self.__profIndex == self.nIntProfiles: |
|
571 | if self.__profIndex == self.nIntProfiles: | |
572 | self.__dataReady = True |
|
572 | self.__dataReady = True | |
573 | else: |
|
573 | else: | |
574 | if (tini - self.__initime) >= self.__integrationtime: |
|
574 | if (tini - self.__initime) >= self.__integrationtime: | |
575 | #print(tini - self.__initime,self.__profIndex) |
|
575 | #print(tini - self.__initime,self.__profIndex) | |
576 | self.__dataReady = True |
|
576 | self.__dataReady = True | |
577 | self.__initime = tini |
|
577 | self.__initime = tini | |
578 |
|
578 | |||
579 | #if (tini.tm_min % 2) == 0 and (tini.tm_sec < 5 and self.fint==0): |
|
579 | #if (tini.tm_min % 2) == 0 and (tini.tm_sec < 5 and self.fint==0): | |
580 |
|
580 | |||
581 | if self.__dataReady: |
|
581 | if self.__dataReady: | |
582 | #print("Data ready",self.__profIndex) |
|
582 | #print("Data ready",self.__profIndex) | |
583 | self.__profIndex = 0 |
|
583 | self.__profIndex = 0 | |
584 | jspc = self.buffer |
|
584 | jspc = self.buffer | |
585 | jcspc = self.buffer2 |
|
585 | jcspc = self.buffer2 | |
586 | #jnoise = self.buffer3 |
|
586 | #jnoise = self.buffer3 | |
587 | self.buffer = dataOut.data_spc |
|
587 | self.buffer = dataOut.data_spc | |
588 | self.buffer2 = dataOut.data_cspc |
|
588 | self.buffer2 = dataOut.data_cspc | |
589 | #self.buffer3 = dataOut.noise |
|
589 | #self.buffer3 = dataOut.noise | |
590 | self.currentTime = dataOut.utctime |
|
590 | self.currentTime = dataOut.utctime | |
591 | if numpy.any(jspc) : |
|
591 | if numpy.any(jspc) : | |
592 | #print( jspc.shape, jcspc.shape) |
|
592 | #print( jspc.shape, jcspc.shape) | |
593 | jspc = numpy.reshape(jspc,(int(len(jspc)/self.nChannels),self.nChannels,self.nFFTPoints,self.nHeights)) |
|
593 | jspc = numpy.reshape(jspc,(int(len(jspc)/self.nChannels),self.nChannels,self.nFFTPoints,self.nHeights)) | |
594 | jcspc= numpy.reshape(jcspc,(int(len(jcspc)/self.nPairs),self.nPairs,self.nFFTPoints,self.nHeights)) |
|
594 | jcspc= numpy.reshape(jcspc,(int(len(jcspc)/self.nPairs),self.nPairs,self.nFFTPoints,self.nHeights)) | |
595 | self.__dataReady = False |
|
595 | self.__dataReady = False | |
596 | #print( jspc.shape, jcspc.shape) |
|
596 | #print( jspc.shape, jcspc.shape) | |
597 | dataOut.flagNoData = False |
|
597 | dataOut.flagNoData = False | |
598 | else: |
|
598 | else: | |
599 | dataOut.flagNoData = True |
|
599 | dataOut.flagNoData = True | |
600 | self.__dataReady = False |
|
600 | self.__dataReady = False | |
601 | return dataOut |
|
601 | return dataOut | |
602 | else: |
|
602 | else: | |
603 | #print( len(self.buffer)) |
|
603 | #print( len(self.buffer)) | |
604 | if numpy.any(self.buffer): |
|
604 | if numpy.any(self.buffer): | |
605 | self.buffer = numpy.concatenate((self.buffer,dataOut.data_spc), axis=0) |
|
605 | self.buffer = numpy.concatenate((self.buffer,dataOut.data_spc), axis=0) | |
606 | self.buffer2 = numpy.concatenate((self.buffer2,dataOut.data_cspc), axis=0) |
|
606 | self.buffer2 = numpy.concatenate((self.buffer2,dataOut.data_cspc), axis=0) | |
607 | self.buffer3 += dataOut.data_dc |
|
607 | self.buffer3 += dataOut.data_dc | |
608 | else: |
|
608 | else: | |
609 | self.buffer = dataOut.data_spc |
|
609 | self.buffer = dataOut.data_spc | |
610 | self.buffer2 = dataOut.data_cspc |
|
610 | self.buffer2 = dataOut.data_cspc | |
611 | self.buffer3 = dataOut.data_dc |
|
611 | self.buffer3 = dataOut.data_dc | |
612 | #print self.index, self.fint |
|
612 | #print self.index, self.fint | |
613 | #print self.buffer2.shape |
|
613 | #print self.buffer2.shape | |
614 | dataOut.flagNoData = True ## NOTE: ?? revisar LUEGO |
|
614 | dataOut.flagNoData = True ## NOTE: ?? revisar LUEGO | |
615 | self.__profIndex += 1 |
|
615 | self.__profIndex += 1 | |
616 | return dataOut ## NOTE: REV |
|
616 | return dataOut ## NOTE: REV | |
617 |
|
617 | |||
618 |
|
618 | |||
619 | #index = tini.tm_hour*12+tini.tm_min/5 |
|
619 | #index = tini.tm_hour*12+tini.tm_min/5 | |
620 | '''REVISAR''' |
|
620 | '''REVISAR''' | |
621 | # jspc = jspc/self.nFFTPoints/self.normFactor |
|
621 | # jspc = jspc/self.nFFTPoints/self.normFactor | |
622 | # jcspc = jcspc/self.nFFTPoints/self.normFactor |
|
622 | # jcspc = jcspc/self.nFFTPoints/self.normFactor | |
623 |
|
623 | |||
624 |
|
624 | |||
625 |
|
625 | |||
626 | tmp_spectra,tmp_cspectra = self.cleanRayleigh(dataOut,jspc,jcspc,self.factor_stdv) |
|
626 | tmp_spectra,tmp_cspectra = self.cleanRayleigh(dataOut,jspc,jcspc,self.factor_stdv) | |
627 | dataOut.data_spc = tmp_spectra |
|
627 | dataOut.data_spc = tmp_spectra | |
628 | dataOut.data_cspc = tmp_cspectra |
|
628 | dataOut.data_cspc = tmp_cspectra | |
629 |
|
629 | |||
630 | #dataOut.data_spc,dataOut.data_cspc = self.cleanRayleigh(dataOut,jspc,jcspc,self.factor_stdv) |
|
630 | #dataOut.data_spc,dataOut.data_cspc = self.cleanRayleigh(dataOut,jspc,jcspc,self.factor_stdv) | |
631 |
|
631 | |||
632 | dataOut.data_dc = self.buffer3 |
|
632 | dataOut.data_dc = self.buffer3 | |
633 | dataOut.nIncohInt *= self.nIntProfiles |
|
633 | dataOut.nIncohInt *= self.nIntProfiles | |
634 | dataOut.utctime = self.currentTime #tiempo promediado |
|
634 | dataOut.utctime = self.currentTime #tiempo promediado | |
635 | #print("Time: ",time.localtime(dataOut.utctime)) |
|
635 | #print("Time: ",time.localtime(dataOut.utctime)) | |
636 | # dataOut.data_spc = sat_spectra |
|
636 | # dataOut.data_spc = sat_spectra | |
637 | # dataOut.data_cspc = sat_cspectra |
|
637 | # dataOut.data_cspc = sat_cspectra | |
638 | self.buffer = 0 |
|
638 | self.buffer = 0 | |
639 | self.buffer2 = 0 |
|
639 | self.buffer2 = 0 | |
640 | self.buffer3 = 0 |
|
640 | self.buffer3 = 0 | |
641 |
|
641 | |||
642 | return dataOut |
|
642 | return dataOut | |
643 |
|
643 | |||
644 | def cleanRayleigh(self,dataOut,spectra,cspectra,factor_stdv): |
|
644 | def cleanRayleigh(self,dataOut,spectra,cspectra,factor_stdv): | |
645 | #print("OP cleanRayleigh") |
|
645 | #print("OP cleanRayleigh") | |
646 | #import matplotlib.pyplot as plt |
|
646 | #import matplotlib.pyplot as plt | |
647 | #for k in range(149): |
|
647 | #for k in range(149): | |
648 | #channelsProcssd = [] |
|
648 | #channelsProcssd = [] | |
649 | #channelA_ok = False |
|
649 | #channelA_ok = False | |
650 | #rfunc = cspectra.copy() #self.bloques |
|
650 | #rfunc = cspectra.copy() #self.bloques | |
651 | rfunc = spectra.copy() |
|
651 | rfunc = spectra.copy() | |
652 | #rfunc = cspectra |
|
652 | #rfunc = cspectra | |
653 | #val_spc = spectra*0.0 #self.bloque0*0.0 |
|
653 | #val_spc = spectra*0.0 #self.bloque0*0.0 | |
654 | #val_cspc = cspectra*0.0 #self.bloques*0.0 |
|
654 | #val_cspc = cspectra*0.0 #self.bloques*0.0 | |
655 | #in_sat_spectra = spectra.copy() #self.bloque0 |
|
655 | #in_sat_spectra = spectra.copy() #self.bloque0 | |
656 | #in_sat_cspectra = cspectra.copy() #self.bloques |
|
656 | #in_sat_cspectra = cspectra.copy() #self.bloques | |
657 |
|
657 | |||
658 |
|
658 | |||
659 | ###ONLY FOR TEST: |
|
659 | ###ONLY FOR TEST: | |
660 | raxs = math.ceil(math.sqrt(self.nPairs)) |
|
660 | raxs = math.ceil(math.sqrt(self.nPairs)) | |
661 | caxs = math.ceil(self.nPairs/raxs) |
|
661 | caxs = math.ceil(self.nPairs/raxs) | |
662 | if self.nPairs <4: |
|
662 | if self.nPairs <4: | |
663 | raxs = 2 |
|
663 | raxs = 2 | |
664 | caxs = 2 |
|
664 | caxs = 2 | |
665 | #print(raxs, caxs) |
|
665 | #print(raxs, caxs) | |
666 | fft_rev = 14 #nFFT to plot |
|
666 | fft_rev = 14 #nFFT to plot | |
667 | hei_rev = ((self.heights >= 550) & (self.heights <= 551)).nonzero() #hei to plot |
|
667 | hei_rev = ((self.heights >= 550) & (self.heights <= 551)).nonzero() #hei to plot | |
668 | hei_rev = hei_rev[0] |
|
668 | hei_rev = hei_rev[0] | |
669 | #print(hei_rev) |
|
669 | #print(hei_rev) | |
670 |
|
670 | |||
671 | #print numpy.absolute(rfunc[:,0,0,14]) |
|
671 | #print numpy.absolute(rfunc[:,0,0,14]) | |
672 |
|
672 | |||
673 | gauss_fit, covariance = None, None |
|
673 | gauss_fit, covariance = None, None | |
674 | for ih in range(self.minAltInd,self.maxAltInd): |
|
674 | for ih in range(self.minAltInd,self.maxAltInd): | |
675 | for ifreq in range(self.nFFTPoints): |
|
675 | for ifreq in range(self.nFFTPoints): | |
676 | ''' |
|
676 | ''' | |
677 | ###ONLY FOR TEST: |
|
677 | ###ONLY FOR TEST: | |
678 | if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY |
|
678 | if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY | |
679 | fig, axs = plt.subplots(raxs, caxs) |
|
679 | fig, axs = plt.subplots(raxs, caxs) | |
680 | fig2, axs2 = plt.subplots(raxs, caxs) |
|
680 | fig2, axs2 = plt.subplots(raxs, caxs) | |
681 | col_ax = 0 |
|
681 | col_ax = 0 | |
682 | row_ax = 0 |
|
682 | row_ax = 0 | |
683 | ''' |
|
683 | ''' | |
684 | #print(self.nPairs) |
|
684 | #print(self.nPairs) | |
685 | for ii in range(self.nChan): #PARES DE CANALES SELF y CROSS |
|
685 | for ii in range(self.nChan): #PARES DE CANALES SELF y CROSS | |
686 | # if self.crosspairs[ii][1]-self.crosspairs[ii][0] > 1: # APLICAR SOLO EN PARES CONTIGUOS |
|
686 | # if self.crosspairs[ii][1]-self.crosspairs[ii][0] > 1: # APLICAR SOLO EN PARES CONTIGUOS | |
687 | # continue |
|
687 | # continue | |
688 | # if not self.crosspairs[ii][0] in channelsProcssd: |
|
688 | # if not self.crosspairs[ii][0] in channelsProcssd: | |
689 | # channelA_ok = True |
|
689 | # channelA_ok = True | |
690 | #print("pair: ",self.crosspairs[ii]) |
|
690 | #print("pair: ",self.crosspairs[ii]) | |
691 | ''' |
|
691 | ''' | |
692 | ###ONLY FOR TEST: |
|
692 | ###ONLY FOR TEST: | |
693 | if (col_ax%caxs==0 and col_ax!=0 and self.nPairs !=1): |
|
693 | if (col_ax%caxs==0 and col_ax!=0 and self.nPairs !=1): | |
694 | col_ax = 0 |
|
694 | col_ax = 0 | |
695 | row_ax += 1 |
|
695 | row_ax += 1 | |
696 | ''' |
|
696 | ''' | |
697 | func2clean = 10*numpy.log10(numpy.absolute(rfunc[:,ii,ifreq,ih])) #Potencia? |
|
697 | func2clean = 10*numpy.log10(numpy.absolute(rfunc[:,ii,ifreq,ih])) #Potencia? | |
698 | #print(func2clean.shape) |
|
698 | #print(func2clean.shape) | |
699 | val = (numpy.isfinite(func2clean)==True).nonzero() |
|
699 | val = (numpy.isfinite(func2clean)==True).nonzero() | |
700 |
|
700 | |||
701 | if len(val)>0: #limitador |
|
701 | if len(val)>0: #limitador | |
702 | min_val = numpy.around(numpy.amin(func2clean)-2) #> (-40) |
|
702 | min_val = numpy.around(numpy.amin(func2clean)-2) #> (-40) | |
703 | if min_val <= -40 : |
|
703 | if min_val <= -40 : | |
704 | min_val = -40 |
|
704 | min_val = -40 | |
705 | max_val = numpy.around(numpy.amax(func2clean)+2) #< 200 |
|
705 | max_val = numpy.around(numpy.amax(func2clean)+2) #< 200 | |
706 | if max_val >= 200 : |
|
706 | if max_val >= 200 : | |
707 | max_val = 200 |
|
707 | max_val = 200 | |
708 | #print min_val, max_val |
|
708 | #print min_val, max_val | |
709 | step = 1 |
|
709 | step = 1 | |
710 | #print("Getting bins and the histogram") |
|
710 | #print("Getting bins and the histogram") | |
711 | x_dist = min_val + numpy.arange(1 + ((max_val-(min_val))/step))*step |
|
711 | x_dist = min_val + numpy.arange(1 + ((max_val-(min_val))/step))*step | |
712 | y_dist,binstep = numpy.histogram(func2clean,bins=range(int(min_val),int(max_val+2),step)) |
|
712 | y_dist,binstep = numpy.histogram(func2clean,bins=range(int(min_val),int(max_val+2),step)) | |
713 | #print(len(y_dist),len(binstep[:-1])) |
|
713 | #print(len(y_dist),len(binstep[:-1])) | |
714 | #print(row_ax,col_ax, " ..") |
|
714 | #print(row_ax,col_ax, " ..") | |
715 | #print(self.pairsArray[ii][0],self.pairsArray[ii][1]) |
|
715 | #print(self.pairsArray[ii][0],self.pairsArray[ii][1]) | |
716 | mean = numpy.sum(x_dist * y_dist) / numpy.sum(y_dist) |
|
716 | mean = numpy.sum(x_dist * y_dist) / numpy.sum(y_dist) | |
717 | sigma = numpy.sqrt(numpy.sum(y_dist * (x_dist - mean)**2) / numpy.sum(y_dist)) |
|
717 | sigma = numpy.sqrt(numpy.sum(y_dist * (x_dist - mean)**2) / numpy.sum(y_dist)) | |
718 | parg = [numpy.amax(y_dist),mean,sigma] |
|
718 | parg = [numpy.amax(y_dist),mean,sigma] | |
719 |
|
719 | |||
720 | newY = None |
|
720 | newY = None | |
721 |
|
721 | |||
722 | try : |
|
722 | try : | |
723 | gauss_fit, covariance = curve_fit(fit_func, x_dist, y_dist,p0=parg) |
|
723 | gauss_fit, covariance = curve_fit(fit_func, x_dist, y_dist,p0=parg) | |
724 | mode = gauss_fit[1] |
|
724 | mode = gauss_fit[1] | |
725 | stdv = gauss_fit[2] |
|
725 | stdv = gauss_fit[2] | |
726 | #print(" FIT OK",gauss_fit) |
|
726 | #print(" FIT OK",gauss_fit) | |
727 | ''' |
|
727 | ''' | |
728 | ###ONLY FOR TEST: |
|
728 | ###ONLY FOR TEST: | |
729 | if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY |
|
729 | if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY | |
730 | newY = fit_func(x_dist,gauss_fit[0],gauss_fit[1],gauss_fit[2]) |
|
730 | newY = fit_func(x_dist,gauss_fit[0],gauss_fit[1],gauss_fit[2]) | |
731 | axs[row_ax,col_ax].plot(binstep[:-1],y_dist,color='green') |
|
731 | axs[row_ax,col_ax].plot(binstep[:-1],y_dist,color='green') | |
732 | axs[row_ax,col_ax].plot(binstep[:-1],newY,color='red') |
|
732 | axs[row_ax,col_ax].plot(binstep[:-1],newY,color='red') | |
733 | axs[row_ax,col_ax].set_title("CH "+str(self.channels[ii])) |
|
733 | axs[row_ax,col_ax].set_title("CH "+str(self.channels[ii])) | |
734 | ''' |
|
734 | ''' | |
735 | except: |
|
735 | except: | |
736 | mode = mean |
|
736 | mode = mean | |
737 | stdv = sigma |
|
737 | stdv = sigma | |
738 | #print("FIT FAIL") |
|
738 | #print("FIT FAIL") | |
739 | #continue |
|
739 | #continue | |
740 |
|
740 | |||
741 |
|
741 | |||
742 | #print(mode,stdv) |
|
742 | #print(mode,stdv) | |
743 | #Removing echoes greater than mode + std_factor*stdv |
|
743 | #Removing echoes greater than mode + std_factor*stdv | |
744 | noval = (abs(func2clean - mode)>=(factor_stdv*stdv)).nonzero() |
|
744 | noval = (abs(func2clean - mode)>=(factor_stdv*stdv)).nonzero() | |
745 | #noval tiene los indices que se van a remover |
|
745 | #noval tiene los indices que se van a remover | |
746 | #print("Chan ",ii," novals: ",len(noval[0])) |
|
746 | #print("Chan ",ii," novals: ",len(noval[0])) | |
747 | if len(noval[0]) > 0: #forma de array (N,) es igual a longitud (N) |
|
747 | if len(noval[0]) > 0: #forma de array (N,) es igual a longitud (N) | |
748 | novall = ((func2clean - mode) >= (factor_stdv*stdv)).nonzero() |
|
748 | novall = ((func2clean - mode) >= (factor_stdv*stdv)).nonzero() | |
749 | #print(novall) |
|
749 | #print(novall) | |
750 | #print(" ",self.pairsArray[ii]) |
|
750 | #print(" ",self.pairsArray[ii]) | |
751 | #cross_pairs = self.pairsArray[ii] |
|
751 | #cross_pairs = self.pairsArray[ii] | |
752 | #Getting coherent echoes which are removed. |
|
752 | #Getting coherent echoes which are removed. | |
753 | # if len(novall[0]) > 0: |
|
753 | # if len(novall[0]) > 0: | |
754 | # |
|
754 | # | |
755 | # val_spc[novall[0],cross_pairs[0],ifreq,ih] = 1 |
|
755 | # val_spc[novall[0],cross_pairs[0],ifreq,ih] = 1 | |
756 | # val_spc[novall[0],cross_pairs[1],ifreq,ih] = 1 |
|
756 | # val_spc[novall[0],cross_pairs[1],ifreq,ih] = 1 | |
757 | # val_cspc[novall[0],ii,ifreq,ih] = 1 |
|
757 | # val_cspc[novall[0],ii,ifreq,ih] = 1 | |
758 | #print("OUT NOVALL 1") |
|
758 | #print("OUT NOVALL 1") | |
759 | try: |
|
759 | try: | |
760 | pair = (self.channels[ii],self.channels[ii + 1]) |
|
760 | pair = (self.channels[ii],self.channels[ii + 1]) | |
761 | except: |
|
761 | except: | |
762 | pair = (99,99) |
|
762 | pair = (99,99) | |
763 | #print("par ", pair) |
|
763 | #print("par ", pair) | |
764 | if ( pair in self.crosspairs): |
|
764 | if ( pair in self.crosspairs): | |
765 | q = self.crosspairs.index(pair) |
|
765 | q = self.crosspairs.index(pair) | |
766 | #print("está aqui: ", q, (ii,ii + 1)) |
|
766 | #print("está aqui: ", q, (ii,ii + 1)) | |
767 | new_a = numpy.delete(cspectra[:,q,ifreq,ih], noval[0]) |
|
767 | new_a = numpy.delete(cspectra[:,q,ifreq,ih], noval[0]) | |
768 | cspectra[noval,q,ifreq,ih] = numpy.mean(new_a) #mean CrossSpectra |
|
768 | cspectra[noval,q,ifreq,ih] = numpy.mean(new_a) #mean CrossSpectra | |
769 |
|
769 | |||
770 | #if channelA_ok: |
|
770 | #if channelA_ok: | |
771 | #chA = self.channels.index(cross_pairs[0]) |
|
771 | #chA = self.channels.index(cross_pairs[0]) | |
772 | new_b = numpy.delete(spectra[:,ii,ifreq,ih], noval[0]) |
|
772 | new_b = numpy.delete(spectra[:,ii,ifreq,ih], noval[0]) | |
773 | spectra[noval,ii,ifreq,ih] = numpy.mean(new_b) #mean Spectra Pair A |
|
773 | spectra[noval,ii,ifreq,ih] = numpy.mean(new_b) #mean Spectra Pair A | |
774 | #channelA_ok = False |
|
774 | #channelA_ok = False | |
775 |
|
775 | |||
776 | # chB = self.channels.index(cross_pairs[1]) |
|
776 | # chB = self.channels.index(cross_pairs[1]) | |
777 | # new_c = numpy.delete(spectra[:,chB,ifreq,ih], noval[0]) |
|
777 | # new_c = numpy.delete(spectra[:,chB,ifreq,ih], noval[0]) | |
778 | # spectra[noval,chB,ifreq,ih] = numpy.mean(new_c) #mean Spectra Pair B |
|
778 | # spectra[noval,chB,ifreq,ih] = numpy.mean(new_c) #mean Spectra Pair B | |
779 | # |
|
779 | # | |
780 | # channelsProcssd.append(self.crosspairs[ii][0]) # save channel A |
|
780 | # channelsProcssd.append(self.crosspairs[ii][0]) # save channel A | |
781 | # channelsProcssd.append(self.crosspairs[ii][1]) # save channel B |
|
781 | # channelsProcssd.append(self.crosspairs[ii][1]) # save channel B | |
782 | ''' |
|
782 | ''' | |
783 | ###ONLY FOR TEST: |
|
783 | ###ONLY FOR TEST: | |
784 | if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY |
|
784 | if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY | |
785 | func2clean = 10*numpy.log10(numpy.absolute(spectra[:,ii,ifreq,ih])) |
|
785 | func2clean = 10*numpy.log10(numpy.absolute(spectra[:,ii,ifreq,ih])) | |
786 | y_dist,binstep = numpy.histogram(func2clean,bins=range(int(min_val),int(max_val+2),step)) |
|
786 | y_dist,binstep = numpy.histogram(func2clean,bins=range(int(min_val),int(max_val+2),step)) | |
787 | axs2[row_ax,col_ax].plot(binstep[:-1],newY,color='red') |
|
787 | axs2[row_ax,col_ax].plot(binstep[:-1],newY,color='red') | |
788 | axs2[row_ax,col_ax].plot(binstep[:-1],y_dist,color='green') |
|
788 | axs2[row_ax,col_ax].plot(binstep[:-1],y_dist,color='green') | |
789 | axs2[row_ax,col_ax].set_title("CH "+str(self.channels[ii])) |
|
789 | axs2[row_ax,col_ax].set_title("CH "+str(self.channels[ii])) | |
790 | ''' |
|
790 | ''' | |
791 | ''' |
|
791 | ''' | |
792 | ###ONLY FOR TEST: |
|
792 | ###ONLY FOR TEST: | |
793 | col_ax += 1 #contador de ploteo columnas |
|
793 | col_ax += 1 #contador de ploteo columnas | |
794 | ##print(col_ax) |
|
794 | ##print(col_ax) | |
795 | ###ONLY FOR TEST: |
|
795 | ###ONLY FOR TEST: | |
796 | if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY |
|
796 | if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY | |
797 | title = str(dataOut.datatime)+" nFFT: "+str(ifreq)+" Alt: "+str(self.heights[ih])+ " km" |
|
797 | title = str(dataOut.datatime)+" nFFT: "+str(ifreq)+" Alt: "+str(self.heights[ih])+ " km" | |
798 | title2 = str(dataOut.datatime)+" nFFT: "+str(ifreq)+" Alt: "+str(self.heights[ih])+ " km CLEANED" |
|
798 | title2 = str(dataOut.datatime)+" nFFT: "+str(ifreq)+" Alt: "+str(self.heights[ih])+ " km CLEANED" | |
799 | fig.suptitle(title) |
|
799 | fig.suptitle(title) | |
800 | fig2.suptitle(title2) |
|
800 | fig2.suptitle(title2) | |
801 | plt.show() |
|
801 | plt.show() | |
802 | ''' |
|
802 | ''' | |
803 | ################################################################################################## |
|
803 | ################################################################################################## | |
804 |
|
804 | |||
805 | #print("Getting average of the spectra and cross-spectra from incoherent echoes.") |
|
805 | #print("Getting average of the spectra and cross-spectra from incoherent echoes.") | |
806 | out_spectra = numpy.zeros([self.nChan,self.nFFTPoints,self.nHeights], dtype=float) #+numpy.nan |
|
806 | out_spectra = numpy.zeros([self.nChan,self.nFFTPoints,self.nHeights], dtype=float) #+numpy.nan | |
807 | out_cspectra = numpy.zeros([self.nPairs,self.nFFTPoints,self.nHeights], dtype=complex) #+numpy.nan |
|
807 | out_cspectra = numpy.zeros([self.nPairs,self.nFFTPoints,self.nHeights], dtype=complex) #+numpy.nan | |
808 | for ih in range(self.nHeights): |
|
808 | for ih in range(self.nHeights): | |
809 | for ifreq in range(self.nFFTPoints): |
|
809 | for ifreq in range(self.nFFTPoints): | |
810 | for ich in range(self.nChan): |
|
810 | for ich in range(self.nChan): | |
811 | tmp = spectra[:,ich,ifreq,ih] |
|
811 | tmp = spectra[:,ich,ifreq,ih] | |
812 | valid = (numpy.isfinite(tmp[:])==True).nonzero() |
|
812 | valid = (numpy.isfinite(tmp[:])==True).nonzero() | |
813 |
|
813 | |||
814 | if len(valid[0]) >0 : |
|
814 | if len(valid[0]) >0 : | |
815 | out_spectra[ich,ifreq,ih] = numpy.nansum(tmp)#/len(valid[0]) |
|
815 | out_spectra[ich,ifreq,ih] = numpy.nansum(tmp)#/len(valid[0]) | |
816 |
|
816 | |||
817 | for icr in range(self.nPairs): |
|
817 | for icr in range(self.nPairs): | |
818 | tmp = numpy.squeeze(cspectra[:,icr,ifreq,ih]) |
|
818 | tmp = numpy.squeeze(cspectra[:,icr,ifreq,ih]) | |
819 | valid = (numpy.isfinite(tmp)==True).nonzero() |
|
819 | valid = (numpy.isfinite(tmp)==True).nonzero() | |
820 | if len(valid[0]) > 0: |
|
820 | if len(valid[0]) > 0: | |
821 | out_cspectra[icr,ifreq,ih] = numpy.nansum(tmp)#/len(valid[0]) |
|
821 | out_cspectra[icr,ifreq,ih] = numpy.nansum(tmp)#/len(valid[0]) | |
822 |
|
822 | |||
823 | return out_spectra, out_cspectra |
|
823 | return out_spectra, out_cspectra | |
824 |
|
824 | |||
825 | def REM_ISOLATED_POINTS(self,array,rth): |
|
825 | def REM_ISOLATED_POINTS(self,array,rth): | |
826 | # import matplotlib.pyplot as plt |
|
826 | # import matplotlib.pyplot as plt | |
827 | if rth == None : |
|
827 | if rth == None : | |
828 | rth = 4 |
|
828 | rth = 4 | |
829 | #print("REM ISO") |
|
829 | #print("REM ISO") | |
830 | num_prof = len(array[0,:,0]) |
|
830 | num_prof = len(array[0,:,0]) | |
831 | num_hei = len(array[0,0,:]) |
|
831 | num_hei = len(array[0,0,:]) | |
832 | n2d = len(array[:,0,0]) |
|
832 | n2d = len(array[:,0,0]) | |
833 |
|
833 | |||
834 | for ii in range(n2d) : |
|
834 | for ii in range(n2d) : | |
835 | #print ii,n2d |
|
835 | #print ii,n2d | |
836 | tmp = array[ii,:,:] |
|
836 | tmp = array[ii,:,:] | |
837 | #print tmp.shape, array[ii,101,:],array[ii,102,:] |
|
837 | #print tmp.shape, array[ii,101,:],array[ii,102,:] | |
838 |
|
838 | |||
839 | # fig = plt.figure(figsize=(6,5)) |
|
839 | # fig = plt.figure(figsize=(6,5)) | |
840 | # left, bottom, width, height = 0.1, 0.1, 0.8, 0.8 |
|
840 | # left, bottom, width, height = 0.1, 0.1, 0.8, 0.8 | |
841 | # ax = fig.add_axes([left, bottom, width, height]) |
|
841 | # ax = fig.add_axes([left, bottom, width, height]) | |
842 | # x = range(num_prof) |
|
842 | # x = range(num_prof) | |
843 | # y = range(num_hei) |
|
843 | # y = range(num_hei) | |
844 | # cp = ax.contour(y,x,tmp) |
|
844 | # cp = ax.contour(y,x,tmp) | |
845 | # ax.clabel(cp, inline=True,fontsize=10) |
|
845 | # ax.clabel(cp, inline=True,fontsize=10) | |
846 | # plt.show() |
|
846 | # plt.show() | |
847 |
|
847 | |||
848 | #indxs = WHERE(FINITE(tmp) AND tmp GT 0,cindxs) |
|
848 | #indxs = WHERE(FINITE(tmp) AND tmp GT 0,cindxs) | |
849 | tmp = numpy.reshape(tmp,num_prof*num_hei) |
|
849 | tmp = numpy.reshape(tmp,num_prof*num_hei) | |
850 | indxs1 = (numpy.isfinite(tmp)==True).nonzero() |
|
850 | indxs1 = (numpy.isfinite(tmp)==True).nonzero() | |
851 | indxs2 = (tmp > 0).nonzero() |
|
851 | indxs2 = (tmp > 0).nonzero() | |
852 |
|
852 | |||
853 | indxs1 = (indxs1[0]) |
|
853 | indxs1 = (indxs1[0]) | |
854 | indxs2 = indxs2[0] |
|
854 | indxs2 = indxs2[0] | |
855 | #indxs1 = numpy.array(indxs1[0]) |
|
855 | #indxs1 = numpy.array(indxs1[0]) | |
856 | #indxs2 = numpy.array(indxs2[0]) |
|
856 | #indxs2 = numpy.array(indxs2[0]) | |
857 | indxs = None |
|
857 | indxs = None | |
858 | #print indxs1 , indxs2 |
|
858 | #print indxs1 , indxs2 | |
859 | for iv in range(len(indxs2)): |
|
859 | for iv in range(len(indxs2)): | |
860 | indv = numpy.array((indxs1 == indxs2[iv]).nonzero()) |
|
860 | indv = numpy.array((indxs1 == indxs2[iv]).nonzero()) | |
861 | #print len(indxs2), indv |
|
861 | #print len(indxs2), indv | |
862 | if len(indv[0]) > 0 : |
|
862 | if len(indv[0]) > 0 : | |
863 | indxs = numpy.concatenate((indxs,indxs2[iv]), axis=None) |
|
863 | indxs = numpy.concatenate((indxs,indxs2[iv]), axis=None) | |
864 | # print indxs |
|
864 | # print indxs | |
865 | indxs = indxs[1:] |
|
865 | indxs = indxs[1:] | |
866 | #print(indxs, len(indxs)) |
|
866 | #print(indxs, len(indxs)) | |
867 | if len(indxs) < 4 : |
|
867 | if len(indxs) < 4 : | |
868 | array[ii,:,:] = 0. |
|
868 | array[ii,:,:] = 0. | |
869 | return |
|
869 | return | |
870 |
|
870 | |||
871 | xpos = numpy.mod(indxs ,num_hei) |
|
871 | xpos = numpy.mod(indxs ,num_hei) | |
872 | ypos = (indxs / num_hei) |
|
872 | ypos = (indxs / num_hei) | |
873 | sx = numpy.argsort(xpos) # Ordering respect to "x" (time) |
|
873 | sx = numpy.argsort(xpos) # Ordering respect to "x" (time) | |
874 | #print sx |
|
874 | #print sx | |
875 | xpos = xpos[sx] |
|
875 | xpos = xpos[sx] | |
876 | ypos = ypos[sx] |
|
876 | ypos = ypos[sx] | |
877 |
|
877 | |||
878 | # *********************************** Cleaning isolated points ********************************** |
|
878 | # *********************************** Cleaning isolated points ********************************** | |
879 | ic = 0 |
|
879 | ic = 0 | |
880 | while True : |
|
880 | while True : | |
881 | r = numpy.sqrt(list(numpy.power((xpos[ic]-xpos),2)+ numpy.power((ypos[ic]-ypos),2))) |
|
881 | r = numpy.sqrt(list(numpy.power((xpos[ic]-xpos),2)+ numpy.power((ypos[ic]-ypos),2))) | |
882 | #no_coh = WHERE(FINITE(r) AND (r LE rth),cno_coh) |
|
882 | #no_coh = WHERE(FINITE(r) AND (r LE rth),cno_coh) | |
883 | #plt.plot(r) |
|
883 | #plt.plot(r) | |
884 | #plt.show() |
|
884 | #plt.show() | |
885 | no_coh1 = (numpy.isfinite(r)==True).nonzero() |
|
885 | no_coh1 = (numpy.isfinite(r)==True).nonzero() | |
886 | no_coh2 = (r <= rth).nonzero() |
|
886 | no_coh2 = (r <= rth).nonzero() | |
887 | #print r, no_coh1, no_coh2 |
|
887 | #print r, no_coh1, no_coh2 | |
888 | no_coh1 = numpy.array(no_coh1[0]) |
|
888 | no_coh1 = numpy.array(no_coh1[0]) | |
889 | no_coh2 = numpy.array(no_coh2[0]) |
|
889 | no_coh2 = numpy.array(no_coh2[0]) | |
890 | no_coh = None |
|
890 | no_coh = None | |
891 | #print valid1 , valid2 |
|
891 | #print valid1 , valid2 | |
892 | for iv in range(len(no_coh2)): |
|
892 | for iv in range(len(no_coh2)): | |
893 | indv = numpy.array((no_coh1 == no_coh2[iv]).nonzero()) |
|
893 | indv = numpy.array((no_coh1 == no_coh2[iv]).nonzero()) | |
894 | if len(indv[0]) > 0 : |
|
894 | if len(indv[0]) > 0 : | |
895 | no_coh = numpy.concatenate((no_coh,no_coh2[iv]), axis=None) |
|
895 | no_coh = numpy.concatenate((no_coh,no_coh2[iv]), axis=None) | |
896 | no_coh = no_coh[1:] |
|
896 | no_coh = no_coh[1:] | |
897 | #print len(no_coh), no_coh |
|
897 | #print len(no_coh), no_coh | |
898 | if len(no_coh) < 4 : |
|
898 | if len(no_coh) < 4 : | |
899 | #print xpos[ic], ypos[ic], ic |
|
899 | #print xpos[ic], ypos[ic], ic | |
900 | # plt.plot(r) |
|
900 | # plt.plot(r) | |
901 | # plt.show() |
|
901 | # plt.show() | |
902 | xpos[ic] = numpy.nan |
|
902 | xpos[ic] = numpy.nan | |
903 | ypos[ic] = numpy.nan |
|
903 | ypos[ic] = numpy.nan | |
904 |
|
904 | |||
905 | ic = ic + 1 |
|
905 | ic = ic + 1 | |
906 | if (ic == len(indxs)) : |
|
906 | if (ic == len(indxs)) : | |
907 | break |
|
907 | break | |
908 | #print( xpos, ypos) |
|
908 | #print( xpos, ypos) | |
909 |
|
909 | |||
910 | indxs = (numpy.isfinite(list(xpos))==True).nonzero() |
|
910 | indxs = (numpy.isfinite(list(xpos))==True).nonzero() | |
911 | #print indxs[0] |
|
911 | #print indxs[0] | |
912 | if len(indxs[0]) < 4 : |
|
912 | if len(indxs[0]) < 4 : | |
913 | array[ii,:,:] = 0. |
|
913 | array[ii,:,:] = 0. | |
914 | return |
|
914 | return | |
915 |
|
915 | |||
916 | xpos = xpos[indxs[0]] |
|
916 | xpos = xpos[indxs[0]] | |
917 | ypos = ypos[indxs[0]] |
|
917 | ypos = ypos[indxs[0]] | |
918 | for i in range(0,len(ypos)): |
|
918 | for i in range(0,len(ypos)): | |
919 | ypos[i]=int(ypos[i]) |
|
919 | ypos[i]=int(ypos[i]) | |
920 | junk = tmp |
|
920 | junk = tmp | |
921 | tmp = junk*0.0 |
|
921 | tmp = junk*0.0 | |
922 |
|
922 | |||
923 | tmp[list(xpos + (ypos*num_hei))] = junk[list(xpos + (ypos*num_hei))] |
|
923 | tmp[list(xpos + (ypos*num_hei))] = junk[list(xpos + (ypos*num_hei))] | |
924 | array[ii,:,:] = numpy.reshape(tmp,(num_prof,num_hei)) |
|
924 | array[ii,:,:] = numpy.reshape(tmp,(num_prof,num_hei)) | |
925 |
|
925 | |||
926 | #print array.shape |
|
926 | #print array.shape | |
927 | #tmp = numpy.reshape(tmp,(num_prof,num_hei)) |
|
927 | #tmp = numpy.reshape(tmp,(num_prof,num_hei)) | |
928 | #print tmp.shape |
|
928 | #print tmp.shape | |
929 |
|
929 | |||
930 | # fig = plt.figure(figsize=(6,5)) |
|
930 | # fig = plt.figure(figsize=(6,5)) | |
931 | # left, bottom, width, height = 0.1, 0.1, 0.8, 0.8 |
|
931 | # left, bottom, width, height = 0.1, 0.1, 0.8, 0.8 | |
932 | # ax = fig.add_axes([left, bottom, width, height]) |
|
932 | # ax = fig.add_axes([left, bottom, width, height]) | |
933 | # x = range(num_prof) |
|
933 | # x = range(num_prof) | |
934 | # y = range(num_hei) |
|
934 | # y = range(num_hei) | |
935 | # cp = ax.contour(y,x,array[ii,:,:]) |
|
935 | # cp = ax.contour(y,x,array[ii,:,:]) | |
936 | # ax.clabel(cp, inline=True,fontsize=10) |
|
936 | # ax.clabel(cp, inline=True,fontsize=10) | |
937 | # plt.show() |
|
937 | # plt.show() | |
938 | return array |
|
938 | return array | |
939 |
|
939 | |||
940 |
|
940 | |||
941 | class IntegrationFaradaySpectra(Operation): |
|
941 | class IntegrationFaradaySpectra(Operation): | |
942 |
|
942 | |||
943 | __profIndex = 0 |
|
943 | __profIndex = 0 | |
944 | __withOverapping = False |
|
944 | __withOverapping = False | |
945 |
|
945 | |||
946 | __byTime = False |
|
946 | __byTime = False | |
947 | __initime = None |
|
947 | __initime = None | |
948 | __lastdatatime = None |
|
948 | __lastdatatime = None | |
949 | __integrationtime = None |
|
949 | __integrationtime = None | |
950 |
|
950 | |||
951 | __buffer_spc = None |
|
951 | __buffer_spc = None | |
952 | __buffer_cspc = None |
|
952 | __buffer_cspc = None | |
953 | __buffer_dc = None |
|
953 | __buffer_dc = None | |
954 |
|
954 | |||
955 | __dataReady = False |
|
955 | __dataReady = False | |
956 |
|
956 | |||
957 | __timeInterval = None |
|
957 | __timeInterval = None | |
958 |
|
958 | |||
959 | n = None |
|
959 | n = None | |
960 |
|
960 | |||
961 | def __init__(self): |
|
961 | def __init__(self): | |
962 |
|
962 | |||
963 | Operation.__init__(self) |
|
963 | Operation.__init__(self) | |
964 |
|
964 | |||
965 | def setup(self, dataOut,n=None, timeInterval=None, overlapping=False, DPL=None): |
|
965 | def setup(self, dataOut,n=None, timeInterval=None, overlapping=False, DPL=None): | |
966 | """ |
|
966 | """ | |
967 | Set the parameters of the integration class. |
|
967 | Set the parameters of the integration class. | |
968 |
|
968 | |||
969 | Inputs: |
|
969 | Inputs: | |
970 |
|
970 | |||
971 | n : Number of coherent integrations |
|
971 | n : Number of coherent integrations | |
972 | timeInterval : Time of integration. If the parameter "n" is selected this one does not work |
|
972 | timeInterval : Time of integration. If the parameter "n" is selected this one does not work | |
973 | overlapping : |
|
973 | overlapping : | |
974 |
|
974 | |||
975 | """ |
|
975 | """ | |
976 |
|
976 | |||
977 | self.__initime = None |
|
977 | self.__initime = None | |
978 | self.__lastdatatime = 0 |
|
978 | self.__lastdatatime = 0 | |
979 |
|
979 | |||
980 | self.__buffer_spc = [] |
|
980 | self.__buffer_spc = [] | |
981 | self.__buffer_cspc = [] |
|
981 | self.__buffer_cspc = [] | |
982 | self.__buffer_dc = 0 |
|
982 | self.__buffer_dc = 0 | |
983 |
|
983 | |||
984 | self.__profIndex = 0 |
|
984 | self.__profIndex = 0 | |
985 | self.__dataReady = False |
|
985 | self.__dataReady = False | |
986 | self.__byTime = False |
|
986 | self.__byTime = False | |
987 |
|
987 | |||
988 | #self.ByLags = dataOut.ByLags ###REDEFINIR |
|
988 | #self.ByLags = dataOut.ByLags ###REDEFINIR | |
989 | self.ByLags = False |
|
989 | self.ByLags = False | |
990 |
|
990 | |||
991 | if DPL != None: |
|
991 | if DPL != None: | |
992 | self.DPL=DPL |
|
992 | self.DPL=DPL | |
993 | else: |
|
993 | else: | |
994 | #self.DPL=dataOut.DPL ###REDEFINIR |
|
994 | #self.DPL=dataOut.DPL ###REDEFINIR | |
995 | self.DPL=0 |
|
995 | self.DPL=0 | |
996 |
|
996 | |||
997 | if n is None and timeInterval is None: |
|
997 | if n is None and timeInterval is None: | |
998 | raise ValueError("n or timeInterval should be specified ...") |
|
998 | raise ValueError("n or timeInterval should be specified ...") | |
999 |
|
999 | |||
1000 | if n is not None: |
|
1000 | if n is not None: | |
1001 | self.n = int(n) |
|
1001 | self.n = int(n) | |
1002 | else: |
|
1002 | else: | |
1003 |
|
1003 | |||
1004 | self.__integrationtime = int(timeInterval) |
|
1004 | self.__integrationtime = int(timeInterval) | |
1005 | self.n = None |
|
1005 | self.n = None | |
1006 | self.__byTime = True |
|
1006 | self.__byTime = True | |
1007 |
|
1007 | |||
1008 | def putData(self, data_spc, data_cspc, data_dc): |
|
1008 | def putData(self, data_spc, data_cspc, data_dc): | |
1009 | """ |
|
1009 | """ | |
1010 | Add a profile to the __buffer_spc and increase in one the __profileIndex |
|
1010 | Add a profile to the __buffer_spc and increase in one the __profileIndex | |
1011 |
|
1011 | |||
1012 | """ |
|
1012 | """ | |
1013 |
|
1013 | |||
1014 | self.__buffer_spc.append(data_spc) |
|
1014 | self.__buffer_spc.append(data_spc) | |
1015 |
|
1015 | |||
1016 | if data_cspc is None: |
|
1016 | if data_cspc is None: | |
1017 | self.__buffer_cspc = None |
|
1017 | self.__buffer_cspc = None | |
1018 | else: |
|
1018 | else: | |
1019 | self.__buffer_cspc.append(data_cspc) |
|
1019 | self.__buffer_cspc.append(data_cspc) | |
1020 |
|
1020 | |||
1021 | if data_dc is None: |
|
1021 | if data_dc is None: | |
1022 | self.__buffer_dc = None |
|
1022 | self.__buffer_dc = None | |
1023 | else: |
|
1023 | else: | |
1024 | self.__buffer_dc += data_dc |
|
1024 | self.__buffer_dc += data_dc | |
1025 |
|
1025 | |||
1026 | self.__profIndex += 1 |
|
1026 | self.__profIndex += 1 | |
1027 |
|
1027 | |||
1028 | return |
|
1028 | return | |
1029 |
|
1029 | |||
1030 | def hildebrand_sekhon_Integration(self,data,navg): |
|
1030 | def hildebrand_sekhon_Integration(self,data,navg): | |
1031 |
|
1031 | |||
1032 | sortdata = numpy.sort(data, axis=None) |
|
1032 | sortdata = numpy.sort(data, axis=None) | |
1033 | sortID=data.argsort() |
|
1033 | sortID=data.argsort() | |
1034 | lenOfData = len(sortdata) |
|
1034 | lenOfData = len(sortdata) | |
1035 | nums_min = lenOfData*0.75 |
|
1035 | nums_min = lenOfData*0.75 | |
1036 | if nums_min <= 5: |
|
1036 | if nums_min <= 5: | |
1037 | nums_min = 5 |
|
1037 | nums_min = 5 | |
1038 | sump = 0. |
|
1038 | sump = 0. | |
1039 | sumq = 0. |
|
1039 | sumq = 0. | |
1040 | j = 0 |
|
1040 | j = 0 | |
1041 | cont = 1 |
|
1041 | cont = 1 | |
1042 | while((cont == 1)and(j < lenOfData)): |
|
1042 | while((cont == 1)and(j < lenOfData)): | |
1043 | sump += sortdata[j] |
|
1043 | sump += sortdata[j] | |
1044 | sumq += sortdata[j]**2 |
|
1044 | sumq += sortdata[j]**2 | |
1045 | if j > nums_min: |
|
1045 | if j > nums_min: | |
1046 | rtest = float(j)/(j-1) + 1.0/navg |
|
1046 | rtest = float(j)/(j-1) + 1.0/navg | |
1047 | if ((sumq*j) > (rtest*sump**2)): |
|
1047 | if ((sumq*j) > (rtest*sump**2)): | |
1048 | j = j - 1 |
|
1048 | j = j - 1 | |
1049 | sump = sump - sortdata[j] |
|
1049 | sump = sump - sortdata[j] | |
1050 | sumq = sumq - sortdata[j]**2 |
|
1050 | sumq = sumq - sortdata[j]**2 | |
1051 | cont = 0 |
|
1051 | cont = 0 | |
1052 | j += 1 |
|
1052 | j += 1 | |
1053 | #lnoise = sump / j |
|
1053 | #lnoise = sump / j | |
1054 |
|
1054 | |||
1055 | return j,sortID |
|
1055 | return j,sortID | |
1056 |
|
1056 | |||
1057 | def pushData(self): |
|
1057 | def pushData(self): | |
1058 | """ |
|
1058 | """ | |
1059 | Return the sum of the last profiles and the profiles used in the sum. |
|
1059 | Return the sum of the last profiles and the profiles used in the sum. | |
1060 |
|
1060 | |||
1061 | Affected: |
|
1061 | Affected: | |
1062 |
|
1062 | |||
1063 | self.__profileIndex |
|
1063 | self.__profileIndex | |
1064 |
|
1064 | |||
1065 | """ |
|
1065 | """ | |
1066 | bufferH=None |
|
1066 | bufferH=None | |
1067 | buffer=None |
|
1067 | buffer=None | |
1068 | buffer1=None |
|
1068 | buffer1=None | |
1069 | buffer_cspc=None |
|
1069 | buffer_cspc=None | |
1070 | self.__buffer_spc=numpy.array(self.__buffer_spc) |
|
1070 | self.__buffer_spc=numpy.array(self.__buffer_spc) | |
1071 | self.__buffer_cspc=numpy.array(self.__buffer_cspc) |
|
1071 | self.__buffer_cspc=numpy.array(self.__buffer_cspc) | |
1072 | freq_dc = int(self.__buffer_spc.shape[2] / 2) |
|
1072 | freq_dc = int(self.__buffer_spc.shape[2] / 2) | |
1073 | #print("FREQ_DC",freq_dc,self.__buffer_spc.shape,self.nHeights) |
|
1073 | #print("FREQ_DC",freq_dc,self.__buffer_spc.shape,self.nHeights) | |
1074 | for k in range(7,self.nHeights): |
|
1074 | for k in range(7,self.nHeights): | |
1075 | buffer_cspc=numpy.copy(self.__buffer_cspc[:,:,:,k]) |
|
1075 | buffer_cspc=numpy.copy(self.__buffer_cspc[:,:,:,k]) | |
1076 | outliers_IDs_cspc=[] |
|
1076 | outliers_IDs_cspc=[] | |
1077 | cspc_outliers_exist=False |
|
1077 | cspc_outliers_exist=False | |
1078 | #print("AQUIII") |
|
1078 | #print("AQUIII") | |
1079 | for i in range(self.nChannels):#dataOut.nChannels): |
|
1079 | for i in range(self.nChannels):#dataOut.nChannels): | |
1080 |
|
1080 | |||
1081 | buffer1=numpy.copy(self.__buffer_spc[:,i,:,k]) |
|
1081 | buffer1=numpy.copy(self.__buffer_spc[:,i,:,k]) | |
1082 | indexes=[] |
|
1082 | indexes=[] | |
1083 | #sortIDs=[] |
|
1083 | #sortIDs=[] | |
1084 | outliers_IDs=[] |
|
1084 | outliers_IDs=[] | |
1085 |
|
1085 | |||
1086 | for j in range(self.nProfiles): |
|
1086 | for j in range(self.nProfiles): | |
1087 | # if i==0 and j==freq_dc: #NOT CONSIDERING DC PROFILE AT CHANNEL 0 |
|
1087 | # if i==0 and j==freq_dc: #NOT CONSIDERING DC PROFILE AT CHANNEL 0 | |
1088 | # continue |
|
1088 | # continue | |
1089 | # if i==1 and j==0: #NOT CONSIDERING DC PROFILE AT CHANNEL 1 |
|
1089 | # if i==1 and j==0: #NOT CONSIDERING DC PROFILE AT CHANNEL 1 | |
1090 | # continue |
|
1090 | # continue | |
1091 | buffer=buffer1[:,j] |
|
1091 | buffer=buffer1[:,j] | |
1092 | index,sortID=self.hildebrand_sekhon_Integration(buffer,1) |
|
1092 | index,sortID=self.hildebrand_sekhon_Integration(buffer,1) | |
1093 |
|
1093 | |||
1094 | indexes.append(index) |
|
1094 | indexes.append(index) | |
1095 | #sortIDs.append(sortID) |
|
1095 | #sortIDs.append(sortID) | |
1096 | outliers_IDs=numpy.append(outliers_IDs,sortID[index:]) |
|
1096 | outliers_IDs=numpy.append(outliers_IDs,sortID[index:]) | |
1097 |
|
1097 | |||
1098 | outliers_IDs=numpy.array(outliers_IDs) |
|
1098 | outliers_IDs=numpy.array(outliers_IDs) | |
1099 | outliers_IDs=outliers_IDs.ravel() |
|
1099 | outliers_IDs=outliers_IDs.ravel() | |
1100 | outliers_IDs=numpy.unique(outliers_IDs) |
|
1100 | outliers_IDs=numpy.unique(outliers_IDs) | |
1101 | outliers_IDs=outliers_IDs.astype(numpy.dtype('int64')) |
|
1101 | outliers_IDs=outliers_IDs.astype(numpy.dtype('int64')) | |
1102 | indexes=numpy.array(indexes) |
|
1102 | indexes=numpy.array(indexes) | |
1103 | indexmin=numpy.min(indexes) |
|
1103 | indexmin=numpy.min(indexes) | |
1104 |
|
1104 | |||
1105 | if indexmin != buffer1.shape[0]: |
|
1105 | if indexmin != buffer1.shape[0]: | |
1106 | cspc_outliers_exist=True |
|
1106 | cspc_outliers_exist=True | |
1107 | ###sortdata=numpy.sort(buffer1,axis=0) |
|
1107 | ###sortdata=numpy.sort(buffer1,axis=0) | |
1108 | ###avg2=numpy.mean(sortdata[:indexmin,:],axis=0) |
|
1108 | ###avg2=numpy.mean(sortdata[:indexmin,:],axis=0) | |
1109 | lt=outliers_IDs |
|
1109 | lt=outliers_IDs | |
1110 | avg=numpy.mean(buffer1[[t for t in range(buffer1.shape[0]) if t not in lt],:],axis=0) |
|
1110 | avg=numpy.mean(buffer1[[t for t in range(buffer1.shape[0]) if t not in lt],:],axis=0) | |
1111 |
|
1111 | |||
1112 | for p in list(outliers_IDs): |
|
1112 | for p in list(outliers_IDs): | |
1113 | buffer1[p,:]=avg |
|
1113 | buffer1[p,:]=avg | |
1114 |
|
1114 | |||
1115 | self.__buffer_spc[:,i,:,k]=numpy.copy(buffer1) |
|
1115 | self.__buffer_spc[:,i,:,k]=numpy.copy(buffer1) | |
1116 | ###cspc IDs |
|
1116 | ###cspc IDs | |
1117 | #indexmin_cspc+=indexmin_cspc |
|
1117 | #indexmin_cspc+=indexmin_cspc | |
1118 | outliers_IDs_cspc=numpy.append(outliers_IDs_cspc,outliers_IDs) |
|
1118 | outliers_IDs_cspc=numpy.append(outliers_IDs_cspc,outliers_IDs) | |
1119 |
|
1119 | |||
1120 | #if not breakFlag: |
|
1120 | #if not breakFlag: | |
1121 | outliers_IDs_cspc=outliers_IDs_cspc.astype(numpy.dtype('int64')) |
|
1121 | outliers_IDs_cspc=outliers_IDs_cspc.astype(numpy.dtype('int64')) | |
1122 | if cspc_outliers_exist: |
|
1122 | if cspc_outliers_exist: | |
1123 | #sortdata=numpy.sort(buffer_cspc,axis=0) |
|
1123 | #sortdata=numpy.sort(buffer_cspc,axis=0) | |
1124 | #avg=numpy.mean(sortdata[:indexmin_cpsc,:],axis=0) |
|
1124 | #avg=numpy.mean(sortdata[:indexmin_cpsc,:],axis=0) | |
1125 | lt=outliers_IDs_cspc |
|
1125 | lt=outliers_IDs_cspc | |
1126 |
|
1126 | |||
1127 | avg=numpy.mean(buffer_cspc[[t for t in range(buffer_cspc.shape[0]) if t not in lt],:],axis=0) |
|
1127 | avg=numpy.mean(buffer_cspc[[t for t in range(buffer_cspc.shape[0]) if t not in lt],:],axis=0) | |
1128 | for p in list(outliers_IDs_cspc): |
|
1128 | for p in list(outliers_IDs_cspc): | |
1129 | buffer_cspc[p,:]=avg |
|
1129 | buffer_cspc[p,:]=avg | |
1130 |
|
1130 | |||
1131 | self.__buffer_cspc[:,:,:,k]=numpy.copy(buffer_cspc) |
|
1131 | self.__buffer_cspc[:,:,:,k]=numpy.copy(buffer_cspc) | |
1132 | #else: |
|
1132 | #else: | |
1133 | #break |
|
1133 | #break | |
1134 |
|
1134 | |||
1135 |
|
1135 | |||
1136 |
|
1136 | |||
1137 |
|
1137 | |||
1138 | buffer=None |
|
1138 | buffer=None | |
1139 | bufferH=None |
|
1139 | bufferH=None | |
1140 | buffer1=None |
|
1140 | buffer1=None | |
1141 | buffer_cspc=None |
|
1141 | buffer_cspc=None | |
1142 |
|
1142 | |||
1143 | #print("cpsc",self.__buffer_cspc[:,0,0,0,0]) |
|
1143 | #print("cpsc",self.__buffer_cspc[:,0,0,0,0]) | |
1144 | #print(self.__profIndex) |
|
1144 | #print(self.__profIndex) | |
1145 | #exit() |
|
1145 | #exit() | |
1146 |
|
1146 | |||
1147 | buffer=None |
|
1147 | buffer=None | |
1148 | #print(self.__buffer_spc[:,1,3,20,0]) |
|
1148 | #print(self.__buffer_spc[:,1,3,20,0]) | |
1149 | #print(self.__buffer_spc[:,1,5,37,0]) |
|
1149 | #print(self.__buffer_spc[:,1,5,37,0]) | |
1150 | data_spc = numpy.sum(self.__buffer_spc,axis=0) |
|
1150 | data_spc = numpy.sum(self.__buffer_spc,axis=0) | |
1151 | data_cspc = numpy.sum(self.__buffer_cspc,axis=0) |
|
1151 | data_cspc = numpy.sum(self.__buffer_cspc,axis=0) | |
1152 |
|
1152 | |||
1153 | #print(numpy.shape(data_spc)) |
|
1153 | #print(numpy.shape(data_spc)) | |
1154 | #data_spc[1,4,20,0]=numpy.nan |
|
1154 | #data_spc[1,4,20,0]=numpy.nan | |
1155 |
|
1155 | |||
1156 | #data_cspc = self.__buffer_cspc |
|
1156 | #data_cspc = self.__buffer_cspc | |
1157 | data_dc = self.__buffer_dc |
|
1157 | data_dc = self.__buffer_dc | |
1158 | n = self.__profIndex |
|
1158 | n = self.__profIndex | |
1159 |
|
1159 | |||
1160 | self.__buffer_spc = [] |
|
1160 | self.__buffer_spc = [] | |
1161 | self.__buffer_cspc = [] |
|
1161 | self.__buffer_cspc = [] | |
1162 | self.__buffer_dc = 0 |
|
1162 | self.__buffer_dc = 0 | |
1163 | self.__profIndex = 0 |
|
1163 | self.__profIndex = 0 | |
1164 |
|
1164 | |||
1165 | return data_spc, data_cspc, data_dc, n |
|
1165 | return data_spc, data_cspc, data_dc, n | |
1166 |
|
1166 | |||
1167 | def byProfiles(self, *args): |
|
1167 | def byProfiles(self, *args): | |
1168 |
|
1168 | |||
1169 | self.__dataReady = False |
|
1169 | self.__dataReady = False | |
1170 | avgdata_spc = None |
|
1170 | avgdata_spc = None | |
1171 | avgdata_cspc = None |
|
1171 | avgdata_cspc = None | |
1172 | avgdata_dc = None |
|
1172 | avgdata_dc = None | |
1173 |
|
1173 | |||
1174 | self.putData(*args) |
|
1174 | self.putData(*args) | |
1175 |
|
1175 | |||
1176 | if self.__profIndex == self.n: |
|
1176 | if self.__profIndex == self.n: | |
1177 |
|
1177 | |||
1178 | avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData() |
|
1178 | avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData() | |
1179 | self.n = n |
|
1179 | self.n = n | |
1180 | self.__dataReady = True |
|
1180 | self.__dataReady = True | |
1181 |
|
1181 | |||
1182 | return avgdata_spc, avgdata_cspc, avgdata_dc |
|
1182 | return avgdata_spc, avgdata_cspc, avgdata_dc | |
1183 |
|
1183 | |||
1184 | def byTime(self, datatime, *args): |
|
1184 | def byTime(self, datatime, *args): | |
1185 |
|
1185 | |||
1186 | self.__dataReady = False |
|
1186 | self.__dataReady = False | |
1187 | avgdata_spc = None |
|
1187 | avgdata_spc = None | |
1188 | avgdata_cspc = None |
|
1188 | avgdata_cspc = None | |
1189 | avgdata_dc = None |
|
1189 | avgdata_dc = None | |
1190 |
|
1190 | |||
1191 | self.putData(*args) |
|
1191 | self.putData(*args) | |
1192 |
|
1192 | |||
1193 | if (datatime - self.__initime) >= self.__integrationtime: |
|
1193 | if (datatime - self.__initime) >= self.__integrationtime: | |
1194 | avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData() |
|
1194 | avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData() | |
1195 | self.n = n |
|
1195 | self.n = n | |
1196 | self.__dataReady = True |
|
1196 | self.__dataReady = True | |
1197 |
|
1197 | |||
1198 | return avgdata_spc, avgdata_cspc, avgdata_dc |
|
1198 | return avgdata_spc, avgdata_cspc, avgdata_dc | |
1199 |
|
1199 | |||
1200 | def integrate(self, datatime, *args): |
|
1200 | def integrate(self, datatime, *args): | |
1201 |
|
1201 | |||
1202 | if self.__profIndex == 0: |
|
1202 | if self.__profIndex == 0: | |
1203 | self.__initime = datatime |
|
1203 | self.__initime = datatime | |
1204 |
|
1204 | |||
1205 | if self.__byTime: |
|
1205 | if self.__byTime: | |
1206 | avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime( |
|
1206 | avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime( | |
1207 | datatime, *args) |
|
1207 | datatime, *args) | |
1208 | else: |
|
1208 | else: | |
1209 | avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args) |
|
1209 | avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args) | |
1210 |
|
1210 | |||
1211 | if not self.__dataReady: |
|
1211 | if not self.__dataReady: | |
1212 | return None, None, None, None |
|
1212 | return None, None, None, None | |
1213 |
|
1213 | |||
1214 | return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc |
|
1214 | return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc | |
1215 |
|
1215 | |||
1216 | def run(self, dataOut, n=None, DPL = None,timeInterval=None, overlapping=False): |
|
1216 | def run(self, dataOut, n=None, DPL = None,timeInterval=None, overlapping=False): | |
1217 | if n == 1: |
|
1217 | if n == 1: | |
1218 | return dataOut |
|
1218 | return dataOut | |
1219 |
|
1219 | |||
1220 | dataOut.flagNoData = True |
|
1220 | dataOut.flagNoData = True | |
1221 |
|
1221 | |||
1222 | if not self.isConfig: |
|
1222 | if not self.isConfig: | |
1223 | self.setup(dataOut, n, timeInterval, overlapping,DPL ) |
|
1223 | self.setup(dataOut, n, timeInterval, overlapping,DPL ) | |
1224 | self.isConfig = True |
|
1224 | self.isConfig = True | |
1225 |
|
1225 | |||
1226 | if not self.ByLags: |
|
1226 | if not self.ByLags: | |
1227 | self.nProfiles=dataOut.nProfiles |
|
1227 | self.nProfiles=dataOut.nProfiles | |
1228 | self.nChannels=dataOut.nChannels |
|
1228 | self.nChannels=dataOut.nChannels | |
1229 | self.nHeights=dataOut.nHeights |
|
1229 | self.nHeights=dataOut.nHeights | |
1230 | avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime, |
|
1230 | avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime, | |
1231 | dataOut.data_spc, |
|
1231 | dataOut.data_spc, | |
1232 | dataOut.data_cspc, |
|
1232 | dataOut.data_cspc, | |
1233 | dataOut.data_dc) |
|
1233 | dataOut.data_dc) | |
1234 | else: |
|
1234 | else: | |
1235 | self.nProfiles=dataOut.nProfiles |
|
1235 | self.nProfiles=dataOut.nProfiles | |
1236 | self.nChannels=dataOut.nChannels |
|
1236 | self.nChannels=dataOut.nChannels | |
1237 | self.nHeights=dataOut.nHeights |
|
1237 | self.nHeights=dataOut.nHeights | |
1238 | avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime, |
|
1238 | avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime, | |
1239 | dataOut.dataLag_spc, |
|
1239 | dataOut.dataLag_spc, | |
1240 | dataOut.dataLag_cspc, |
|
1240 | dataOut.dataLag_cspc, | |
1241 | dataOut.dataLag_dc) |
|
1241 | dataOut.dataLag_dc) | |
1242 |
|
1242 | |||
1243 | if self.__dataReady: |
|
1243 | if self.__dataReady: | |
1244 |
|
1244 | |||
1245 | if not self.ByLags: |
|
1245 | if not self.ByLags: | |
1246 |
|
1246 | |||
1247 | dataOut.data_spc = numpy.squeeze(avgdata_spc) |
|
1247 | dataOut.data_spc = numpy.squeeze(avgdata_spc) | |
1248 | dataOut.data_cspc = numpy.squeeze(avgdata_cspc) |
|
1248 | dataOut.data_cspc = numpy.squeeze(avgdata_cspc) | |
1249 | dataOut.data_dc = avgdata_dc |
|
1249 | dataOut.data_dc = avgdata_dc | |
1250 | else: |
|
1250 | else: | |
1251 | dataOut.dataLag_spc = avgdata_spc |
|
1251 | dataOut.dataLag_spc = avgdata_spc | |
1252 | dataOut.dataLag_cspc = avgdata_cspc |
|
1252 | dataOut.dataLag_cspc = avgdata_cspc | |
1253 | dataOut.dataLag_dc = avgdata_dc |
|
1253 | dataOut.dataLag_dc = avgdata_dc | |
1254 |
|
1254 | |||
1255 | dataOut.data_spc=dataOut.dataLag_spc[:,:,:,dataOut.LagPlot] |
|
1255 | dataOut.data_spc=dataOut.dataLag_spc[:,:,:,dataOut.LagPlot] | |
1256 | dataOut.data_cspc=dataOut.dataLag_cspc[:,:,:,dataOut.LagPlot] |
|
1256 | dataOut.data_cspc=dataOut.dataLag_cspc[:,:,:,dataOut.LagPlot] | |
1257 | dataOut.data_dc=dataOut.dataLag_dc[:,:,dataOut.LagPlot] |
|
1257 | dataOut.data_dc=dataOut.dataLag_dc[:,:,dataOut.LagPlot] | |
1258 |
|
1258 | |||
1259 |
|
1259 | |||
1260 | dataOut.nIncohInt *= self.n |
|
1260 | dataOut.nIncohInt *= self.n | |
1261 | dataOut.utctime = avgdatatime |
|
1261 | dataOut.utctime = avgdatatime | |
1262 | dataOut.flagNoData = False |
|
1262 | dataOut.flagNoData = False | |
1263 |
|
1263 | |||
1264 | return dataOut |
|
1264 | return dataOut | |
1265 |
|
1265 | |||
1266 | class removeInterference(Operation): |
|
1266 | class removeInterference(Operation): | |
1267 |
|
1267 | |||
1268 | def removeInterference2(self): |
|
1268 | def removeInterference2(self): | |
1269 |
|
1269 | |||
1270 | cspc = self.dataOut.data_cspc |
|
1270 | cspc = self.dataOut.data_cspc | |
1271 | spc = self.dataOut.data_spc |
|
1271 | spc = self.dataOut.data_spc | |
1272 | Heights = numpy.arange(cspc.shape[2]) |
|
1272 | Heights = numpy.arange(cspc.shape[2]) | |
1273 | realCspc = numpy.abs(cspc) |
|
1273 | realCspc = numpy.abs(cspc) | |
1274 |
|
1274 | |||
1275 | for i in range(cspc.shape[0]): |
|
1275 | for i in range(cspc.shape[0]): | |
1276 | LinePower= numpy.sum(realCspc[i], axis=0) |
|
1276 | LinePower= numpy.sum(realCspc[i], axis=0) | |
1277 | Threshold = numpy.amax(LinePower)-numpy.sort(LinePower)[len(Heights)-int(len(Heights)*0.1)] |
|
1277 | Threshold = numpy.amax(LinePower)-numpy.sort(LinePower)[len(Heights)-int(len(Heights)*0.1)] | |
1278 | SelectedHeights = Heights[ numpy.where( LinePower < Threshold ) ] |
|
1278 | SelectedHeights = Heights[ numpy.where( LinePower < Threshold ) ] | |
1279 | InterferenceSum = numpy.sum( realCspc[i,:,SelectedHeights], axis=0 ) |
|
1279 | InterferenceSum = numpy.sum( realCspc[i,:,SelectedHeights], axis=0 ) | |
1280 | InterferenceThresholdMin = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.98)] |
|
1280 | InterferenceThresholdMin = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.98)] | |
1281 | InterferenceThresholdMax = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.99)] |
|
1281 | InterferenceThresholdMax = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.99)] | |
1282 |
|
1282 | |||
1283 |
|
1283 | |||
1284 | InterferenceRange = numpy.where( ([InterferenceSum > InterferenceThresholdMin]))# , InterferenceSum < InterferenceThresholdMax]) ) |
|
1284 | InterferenceRange = numpy.where( ([InterferenceSum > InterferenceThresholdMin]))# , InterferenceSum < InterferenceThresholdMax]) ) | |
1285 | #InterferenceRange = numpy.where( ([InterferenceRange < InterferenceThresholdMax])) |
|
1285 | #InterferenceRange = numpy.where( ([InterferenceRange < InterferenceThresholdMax])) | |
1286 | if len(InterferenceRange)<int(cspc.shape[1]*0.3): |
|
1286 | if len(InterferenceRange)<int(cspc.shape[1]*0.3): | |
1287 | cspc[i,InterferenceRange,:] = numpy.NaN |
|
1287 | cspc[i,InterferenceRange,:] = numpy.NaN | |
1288 |
|
1288 | |||
1289 | self.dataOut.data_cspc = cspc |
|
1289 | self.dataOut.data_cspc = cspc | |
1290 |
|
1290 | |||
1291 | def removeInterference(self, interf = 2, hei_interf = None, nhei_interf = None, offhei_interf = None): |
|
1291 | def removeInterference(self, interf = 2, hei_interf = None, nhei_interf = None, offhei_interf = None): | |
1292 |
|
1292 | |||
1293 | jspectra = self.dataOut.data_spc |
|
1293 | jspectra = self.dataOut.data_spc | |
1294 | jcspectra = self.dataOut.data_cspc |
|
1294 | jcspectra = self.dataOut.data_cspc | |
1295 | jnoise = self.dataOut.getNoise() |
|
1295 | jnoise = self.dataOut.getNoise() | |
1296 | num_incoh = self.dataOut.nIncohInt |
|
1296 | num_incoh = self.dataOut.nIncohInt | |
1297 |
|
1297 | |||
1298 | num_channel = jspectra.shape[0] |
|
1298 | num_channel = jspectra.shape[0] | |
1299 | num_prof = jspectra.shape[1] |
|
1299 | num_prof = jspectra.shape[1] | |
1300 | num_hei = jspectra.shape[2] |
|
1300 | num_hei = jspectra.shape[2] | |
1301 |
|
1301 | |||
1302 | # hei_interf |
|
1302 | # hei_interf | |
1303 | if hei_interf is None: |
|
1303 | if hei_interf is None: | |
1304 | count_hei = int(num_hei / 2) |
|
1304 | count_hei = int(num_hei / 2) | |
1305 | hei_interf = numpy.asmatrix(list(range(count_hei))) + num_hei - count_hei |
|
1305 | hei_interf = numpy.asmatrix(list(range(count_hei))) + num_hei - count_hei | |
1306 | hei_interf = numpy.asarray(hei_interf)[0] |
|
1306 | hei_interf = numpy.asarray(hei_interf)[0] | |
1307 | # nhei_interf |
|
1307 | # nhei_interf | |
1308 | if (nhei_interf == None): |
|
1308 | if (nhei_interf == None): | |
1309 | nhei_interf = 5 |
|
1309 | nhei_interf = 5 | |
1310 | if (nhei_interf < 1): |
|
1310 | if (nhei_interf < 1): | |
1311 | nhei_interf = 1 |
|
1311 | nhei_interf = 1 | |
1312 | if (nhei_interf > count_hei): |
|
1312 | if (nhei_interf > count_hei): | |
1313 | nhei_interf = count_hei |
|
1313 | nhei_interf = count_hei | |
1314 | if (offhei_interf == None): |
|
1314 | if (offhei_interf == None): | |
1315 | offhei_interf = 0 |
|
1315 | offhei_interf = 0 | |
1316 |
|
1316 | |||
1317 | ind_hei = list(range(num_hei)) |
|
1317 | ind_hei = list(range(num_hei)) | |
1318 | # mask_prof = numpy.asarray(range(num_prof - 2)) + 1 |
|
1318 | # mask_prof = numpy.asarray(range(num_prof - 2)) + 1 | |
1319 | # mask_prof[range(num_prof/2 - 1,len(mask_prof))] += 1 |
|
1319 | # mask_prof[range(num_prof/2 - 1,len(mask_prof))] += 1 | |
1320 | mask_prof = numpy.asarray(list(range(num_prof))) |
|
1320 | mask_prof = numpy.asarray(list(range(num_prof))) | |
1321 | num_mask_prof = mask_prof.size |
|
1321 | num_mask_prof = mask_prof.size | |
1322 | comp_mask_prof = [0, num_prof / 2] |
|
1322 | comp_mask_prof = [0, num_prof / 2] | |
1323 |
|
1323 | |||
1324 | # noise_exist: Determina si la variable jnoise ha sido definida y contiene la informacion del ruido de cada canal |
|
1324 | # noise_exist: Determina si la variable jnoise ha sido definida y contiene la informacion del ruido de cada canal | |
1325 | if (jnoise.size < num_channel or numpy.isnan(jnoise).any()): |
|
1325 | if (jnoise.size < num_channel or numpy.isnan(jnoise).any()): | |
1326 | jnoise = numpy.nan |
|
1326 | jnoise = numpy.nan | |
1327 | noise_exist = jnoise[0] < numpy.Inf |
|
1327 | noise_exist = jnoise[0] < numpy.Inf | |
1328 |
|
1328 | |||
1329 | # Subrutina de Remocion de la Interferencia |
|
1329 | # Subrutina de Remocion de la Interferencia | |
1330 | for ich in range(num_channel): |
|
1330 | for ich in range(num_channel): | |
1331 | # Se ordena los espectros segun su potencia (menor a mayor) |
|
1331 | # Se ordena los espectros segun su potencia (menor a mayor) | |
1332 | power = jspectra[ich, mask_prof, :] |
|
1332 | power = jspectra[ich, mask_prof, :] | |
1333 | power = power[:, hei_interf] |
|
1333 | power = power[:, hei_interf] | |
1334 | power = power.sum(axis=0) |
|
1334 | power = power.sum(axis=0) | |
1335 | psort = power.ravel().argsort() |
|
1335 | psort = power.ravel().argsort() | |
1336 |
|
1336 | |||
1337 | # Se estima la interferencia promedio en los Espectros de Potencia empleando |
|
1337 | # Se estima la interferencia promedio en los Espectros de Potencia empleando | |
1338 | junkspc_interf = jspectra[ich, :, hei_interf[psort[list(range( |
|
1338 | junkspc_interf = jspectra[ich, :, hei_interf[psort[list(range( | |
1339 | offhei_interf, nhei_interf + offhei_interf))]]] |
|
1339 | offhei_interf, nhei_interf + offhei_interf))]]] | |
1340 |
|
1340 | |||
1341 | if noise_exist: |
|
1341 | if noise_exist: | |
1342 | # tmp_noise = jnoise[ich] / num_prof |
|
1342 | # tmp_noise = jnoise[ich] / num_prof | |
1343 | tmp_noise = jnoise[ich] |
|
1343 | tmp_noise = jnoise[ich] | |
1344 | junkspc_interf = junkspc_interf - tmp_noise |
|
1344 | junkspc_interf = junkspc_interf - tmp_noise | |
1345 | #junkspc_interf[:,comp_mask_prof] = 0 |
|
1345 | #junkspc_interf[:,comp_mask_prof] = 0 | |
1346 |
|
1346 | |||
1347 | jspc_interf = junkspc_interf.sum(axis=0) / nhei_interf |
|
1347 | jspc_interf = junkspc_interf.sum(axis=0) / nhei_interf | |
1348 | jspc_interf = jspc_interf.transpose() |
|
1348 | jspc_interf = jspc_interf.transpose() | |
1349 | # Calculando el espectro de interferencia promedio |
|
1349 | # Calculando el espectro de interferencia promedio | |
1350 | noiseid = numpy.where( |
|
1350 | noiseid = numpy.where( | |
1351 | jspc_interf <= tmp_noise / numpy.sqrt(num_incoh)) |
|
1351 | jspc_interf <= tmp_noise / numpy.sqrt(num_incoh)) | |
1352 | noiseid = noiseid[0] |
|
1352 | noiseid = noiseid[0] | |
1353 | cnoiseid = noiseid.size |
|
1353 | cnoiseid = noiseid.size | |
1354 | interfid = numpy.where( |
|
1354 | interfid = numpy.where( | |
1355 | jspc_interf > tmp_noise / numpy.sqrt(num_incoh)) |
|
1355 | jspc_interf > tmp_noise / numpy.sqrt(num_incoh)) | |
1356 | interfid = interfid[0] |
|
1356 | interfid = interfid[0] | |
1357 | cinterfid = interfid.size |
|
1357 | cinterfid = interfid.size | |
1358 |
|
1358 | |||
1359 | if (cnoiseid > 0): |
|
1359 | if (cnoiseid > 0): | |
1360 | jspc_interf[noiseid] = 0 |
|
1360 | jspc_interf[noiseid] = 0 | |
1361 |
|
1361 | |||
1362 | # Expandiendo los perfiles a limpiar |
|
1362 | # Expandiendo los perfiles a limpiar | |
1363 | if (cinterfid > 0): |
|
1363 | if (cinterfid > 0): | |
1364 | new_interfid = ( |
|
1364 | new_interfid = ( | |
1365 | numpy.r_[interfid - 1, interfid, interfid + 1] + num_prof) % num_prof |
|
1365 | numpy.r_[interfid - 1, interfid, interfid + 1] + num_prof) % num_prof | |
1366 | new_interfid = numpy.asarray(new_interfid) |
|
1366 | new_interfid = numpy.asarray(new_interfid) | |
1367 | new_interfid = {x for x in new_interfid} |
|
1367 | new_interfid = {x for x in new_interfid} | |
1368 | new_interfid = numpy.array(list(new_interfid)) |
|
1368 | new_interfid = numpy.array(list(new_interfid)) | |
1369 | new_cinterfid = new_interfid.size |
|
1369 | new_cinterfid = new_interfid.size | |
1370 | else: |
|
1370 | else: | |
1371 | new_cinterfid = 0 |
|
1371 | new_cinterfid = 0 | |
1372 |
|
1372 | |||
1373 | for ip in range(new_cinterfid): |
|
1373 | for ip in range(new_cinterfid): | |
1374 | ind = junkspc_interf[:, new_interfid[ip]].ravel().argsort() |
|
1374 | ind = junkspc_interf[:, new_interfid[ip]].ravel().argsort() | |
1375 | jspc_interf[new_interfid[ip] |
|
1375 | jspc_interf[new_interfid[ip] | |
1376 | ] = junkspc_interf[ind[nhei_interf // 2], new_interfid[ip]] |
|
1376 | ] = junkspc_interf[ind[nhei_interf // 2], new_interfid[ip]] | |
1377 |
|
1377 | |||
1378 | jspectra[ich, :, ind_hei] = jspectra[ich, :, |
|
1378 | jspectra[ich, :, ind_hei] = jspectra[ich, :, | |
1379 | ind_hei] - jspc_interf # Corregir indices |
|
1379 | ind_hei] - jspc_interf # Corregir indices | |
1380 |
|
1380 | |||
1381 | # Removiendo la interferencia del punto de mayor interferencia |
|
1381 | # Removiendo la interferencia del punto de mayor interferencia | |
1382 | ListAux = jspc_interf[mask_prof].tolist() |
|
1382 | ListAux = jspc_interf[mask_prof].tolist() | |
1383 | maxid = ListAux.index(max(ListAux)) |
|
1383 | maxid = ListAux.index(max(ListAux)) | |
1384 |
|
1384 | |||
1385 | if cinterfid > 0: |
|
1385 | if cinterfid > 0: | |
1386 | for ip in range(cinterfid * (interf == 2) - 1): |
|
1386 | for ip in range(cinterfid * (interf == 2) - 1): | |
1387 | ind = (jspectra[ich, interfid[ip], :] < tmp_noise * |
|
1387 | ind = (jspectra[ich, interfid[ip], :] < tmp_noise * | |
1388 | (1 + 1 / numpy.sqrt(num_incoh))).nonzero() |
|
1388 | (1 + 1 / numpy.sqrt(num_incoh))).nonzero() | |
1389 | cind = len(ind) |
|
1389 | cind = len(ind) | |
1390 |
|
1390 | |||
1391 | if (cind > 0): |
|
1391 | if (cind > 0): | |
1392 | jspectra[ich, interfid[ip], ind] = tmp_noise * \ |
|
1392 | jspectra[ich, interfid[ip], ind] = tmp_noise * \ | |
1393 | (1 + (numpy.random.uniform(cind) - 0.5) / |
|
1393 | (1 + (numpy.random.uniform(cind) - 0.5) / | |
1394 | numpy.sqrt(num_incoh)) |
|
1394 | numpy.sqrt(num_incoh)) | |
1395 |
|
1395 | |||
1396 | ind = numpy.array([-2, -1, 1, 2]) |
|
1396 | ind = numpy.array([-2, -1, 1, 2]) | |
1397 | xx = numpy.zeros([4, 4]) |
|
1397 | xx = numpy.zeros([4, 4]) | |
1398 |
|
1398 | |||
1399 | for id1 in range(4): |
|
1399 | for id1 in range(4): | |
1400 | xx[:, id1] = ind[id1]**numpy.asarray(list(range(4))) |
|
1400 | xx[:, id1] = ind[id1]**numpy.asarray(list(range(4))) | |
1401 |
|
1401 | |||
1402 | xx_inv = numpy.linalg.inv(xx) |
|
1402 | xx_inv = numpy.linalg.inv(xx) | |
1403 | xx = xx_inv[:, 0] |
|
1403 | xx = xx_inv[:, 0] | |
1404 | ind = (ind + maxid + num_mask_prof) % num_mask_prof |
|
1404 | ind = (ind + maxid + num_mask_prof) % num_mask_prof | |
1405 | yy = jspectra[ich, mask_prof[ind], :] |
|
1405 | yy = jspectra[ich, mask_prof[ind], :] | |
1406 | jspectra[ich, mask_prof[maxid], :] = numpy.dot( |
|
1406 | jspectra[ich, mask_prof[maxid], :] = numpy.dot( | |
1407 | yy.transpose(), xx) |
|
1407 | yy.transpose(), xx) | |
1408 |
|
1408 | |||
1409 | indAux = (jspectra[ich, :, :] < tmp_noise * |
|
1409 | indAux = (jspectra[ich, :, :] < tmp_noise * | |
1410 | (1 - 1 / numpy.sqrt(num_incoh))).nonzero() |
|
1410 | (1 - 1 / numpy.sqrt(num_incoh))).nonzero() | |
1411 | jspectra[ich, indAux[0], indAux[1]] = tmp_noise * \ |
|
1411 | jspectra[ich, indAux[0], indAux[1]] = tmp_noise * \ | |
1412 | (1 - 1 / numpy.sqrt(num_incoh)) |
|
1412 | (1 - 1 / numpy.sqrt(num_incoh)) | |
1413 |
|
1413 | |||
1414 | # Remocion de Interferencia en el Cross Spectra |
|
1414 | # Remocion de Interferencia en el Cross Spectra | |
1415 | if jcspectra is None: |
|
1415 | if jcspectra is None: | |
1416 | return jspectra, jcspectra |
|
1416 | return jspectra, jcspectra | |
1417 | num_pairs = int(jcspectra.size / (num_prof * num_hei)) |
|
1417 | num_pairs = int(jcspectra.size / (num_prof * num_hei)) | |
1418 | jcspectra = jcspectra.reshape(num_pairs, num_prof, num_hei) |
|
1418 | jcspectra = jcspectra.reshape(num_pairs, num_prof, num_hei) | |
1419 |
|
1419 | |||
1420 | for ip in range(num_pairs): |
|
1420 | for ip in range(num_pairs): | |
1421 |
|
1421 | |||
1422 | #------------------------------------------- |
|
1422 | #------------------------------------------- | |
1423 |
|
1423 | |||
1424 | cspower = numpy.abs(jcspectra[ip, mask_prof, :]) |
|
1424 | cspower = numpy.abs(jcspectra[ip, mask_prof, :]) | |
1425 | cspower = cspower[:, hei_interf] |
|
1425 | cspower = cspower[:, hei_interf] | |
1426 | cspower = cspower.sum(axis=0) |
|
1426 | cspower = cspower.sum(axis=0) | |
1427 |
|
1427 | |||
1428 | cspsort = cspower.ravel().argsort() |
|
1428 | cspsort = cspower.ravel().argsort() | |
1429 | junkcspc_interf = jcspectra[ip, :, hei_interf[cspsort[list(range( |
|
1429 | junkcspc_interf = jcspectra[ip, :, hei_interf[cspsort[list(range( | |
1430 | offhei_interf, nhei_interf + offhei_interf))]]] |
|
1430 | offhei_interf, nhei_interf + offhei_interf))]]] | |
1431 | junkcspc_interf = junkcspc_interf.transpose() |
|
1431 | junkcspc_interf = junkcspc_interf.transpose() | |
1432 | jcspc_interf = junkcspc_interf.sum(axis=1) / nhei_interf |
|
1432 | jcspc_interf = junkcspc_interf.sum(axis=1) / nhei_interf | |
1433 |
|
1433 | |||
1434 | ind = numpy.abs(jcspc_interf[mask_prof]).ravel().argsort() |
|
1434 | ind = numpy.abs(jcspc_interf[mask_prof]).ravel().argsort() | |
1435 |
|
1435 | |||
1436 | median_real = int(numpy.median(numpy.real( |
|
1436 | median_real = int(numpy.median(numpy.real( | |
1437 | junkcspc_interf[mask_prof[ind[list(range(3 * num_prof // 4))]], :]))) |
|
1437 | junkcspc_interf[mask_prof[ind[list(range(3 * num_prof // 4))]], :]))) | |
1438 | median_imag = int(numpy.median(numpy.imag( |
|
1438 | median_imag = int(numpy.median(numpy.imag( | |
1439 | junkcspc_interf[mask_prof[ind[list(range(3 * num_prof // 4))]], :]))) |
|
1439 | junkcspc_interf[mask_prof[ind[list(range(3 * num_prof // 4))]], :]))) | |
1440 | comp_mask_prof = [int(e) for e in comp_mask_prof] |
|
1440 | comp_mask_prof = [int(e) for e in comp_mask_prof] | |
1441 | junkcspc_interf[comp_mask_prof, :] = numpy.complex( |
|
1441 | junkcspc_interf[comp_mask_prof, :] = numpy.complex( | |
1442 | median_real, median_imag) |
|
1442 | median_real, median_imag) | |
1443 |
|
1443 | |||
1444 | for iprof in range(num_prof): |
|
1444 | for iprof in range(num_prof): | |
1445 | ind = numpy.abs(junkcspc_interf[iprof, :]).ravel().argsort() |
|
1445 | ind = numpy.abs(junkcspc_interf[iprof, :]).ravel().argsort() | |
1446 | jcspc_interf[iprof] = junkcspc_interf[iprof, ind[nhei_interf // 2]] |
|
1446 | jcspc_interf[iprof] = junkcspc_interf[iprof, ind[nhei_interf // 2]] | |
1447 |
|
1447 | |||
1448 | # Removiendo la Interferencia |
|
1448 | # Removiendo la Interferencia | |
1449 | jcspectra[ip, :, ind_hei] = jcspectra[ip, |
|
1449 | jcspectra[ip, :, ind_hei] = jcspectra[ip, | |
1450 | :, ind_hei] - jcspc_interf |
|
1450 | :, ind_hei] - jcspc_interf | |
1451 |
|
1451 | |||
1452 | ListAux = numpy.abs(jcspc_interf[mask_prof]).tolist() |
|
1452 | ListAux = numpy.abs(jcspc_interf[mask_prof]).tolist() | |
1453 | maxid = ListAux.index(max(ListAux)) |
|
1453 | maxid = ListAux.index(max(ListAux)) | |
1454 |
|
1454 | |||
1455 | ind = numpy.array([-2, -1, 1, 2]) |
|
1455 | ind = numpy.array([-2, -1, 1, 2]) | |
1456 | xx = numpy.zeros([4, 4]) |
|
1456 | xx = numpy.zeros([4, 4]) | |
1457 |
|
1457 | |||
1458 | for id1 in range(4): |
|
1458 | for id1 in range(4): | |
1459 | xx[:, id1] = ind[id1]**numpy.asarray(list(range(4))) |
|
1459 | xx[:, id1] = ind[id1]**numpy.asarray(list(range(4))) | |
1460 |
|
1460 | |||
1461 | xx_inv = numpy.linalg.inv(xx) |
|
1461 | xx_inv = numpy.linalg.inv(xx) | |
1462 | xx = xx_inv[:, 0] |
|
1462 | xx = xx_inv[:, 0] | |
1463 |
|
1463 | |||
1464 | ind = (ind + maxid + num_mask_prof) % num_mask_prof |
|
1464 | ind = (ind + maxid + num_mask_prof) % num_mask_prof | |
1465 | yy = jcspectra[ip, mask_prof[ind], :] |
|
1465 | yy = jcspectra[ip, mask_prof[ind], :] | |
1466 | jcspectra[ip, mask_prof[maxid], :] = numpy.dot(yy.transpose(), xx) |
|
1466 | jcspectra[ip, mask_prof[maxid], :] = numpy.dot(yy.transpose(), xx) | |
1467 |
|
1467 | |||
1468 | # Guardar Resultados |
|
1468 | # Guardar Resultados | |
1469 | self.dataOut.data_spc = jspectra |
|
1469 | self.dataOut.data_spc = jspectra | |
1470 | self.dataOut.data_cspc = jcspectra |
|
1470 | self.dataOut.data_cspc = jcspectra | |
1471 |
|
1471 | |||
1472 | return 1 |
|
1472 | return 1 | |
1473 |
|
1473 | |||
1474 | def run(self, dataOut, interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None, mode=1): |
|
1474 | def run(self, dataOut, interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None, mode=1): | |
1475 |
|
1475 | |||
1476 | self.dataOut = dataOut |
|
1476 | self.dataOut = dataOut | |
1477 |
|
1477 | |||
1478 | if mode == 1: |
|
1478 | if mode == 1: | |
1479 | self.removeInterference(interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None) |
|
1479 | self.removeInterference(interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None) | |
1480 | elif mode == 2: |
|
1480 | elif mode == 2: | |
1481 | self.removeInterference2() |
|
1481 | self.removeInterference2() | |
1482 |
|
1482 | |||
1483 | return self.dataOut |
|
1483 | return self.dataOut | |
1484 |
|
1484 | |||
1485 |
|
1485 | |||
1486 | class IncohInt(Operation): |
|
1486 | class IncohInt(Operation): | |
1487 |
|
1487 | |||
1488 | __profIndex = 0 |
|
1488 | __profIndex = 0 | |
1489 | __withOverapping = False |
|
1489 | __withOverapping = False | |
1490 |
|
1490 | |||
1491 | __byTime = False |
|
1491 | __byTime = False | |
1492 | __initime = None |
|
1492 | __initime = None | |
1493 | __lastdatatime = None |
|
1493 | __lastdatatime = None | |
1494 | __integrationtime = None |
|
1494 | __integrationtime = None | |
1495 |
|
1495 | |||
1496 | __buffer_spc = None |
|
1496 | __buffer_spc = None | |
1497 | __buffer_cspc = None |
|
1497 | __buffer_cspc = None | |
1498 | __buffer_dc = None |
|
1498 | __buffer_dc = None | |
1499 |
|
1499 | |||
1500 | __dataReady = False |
|
1500 | __dataReady = False | |
1501 |
|
1501 | |||
1502 | __timeInterval = None |
|
1502 | __timeInterval = None | |
1503 |
|
1503 | |||
1504 | n = None |
|
1504 | n = None | |
1505 |
|
1505 | |||
1506 | def __init__(self): |
|
1506 | def __init__(self): | |
1507 |
|
1507 | |||
1508 | Operation.__init__(self) |
|
1508 | Operation.__init__(self) | |
1509 |
|
1509 | |||
1510 | def setup(self, n=None, timeInterval=None, overlapping=False): |
|
1510 | def setup(self, n=None, timeInterval=None, overlapping=False): | |
1511 | """ |
|
1511 | """ | |
1512 | Set the parameters of the integration class. |
|
1512 | Set the parameters of the integration class. | |
1513 |
|
1513 | |||
1514 | Inputs: |
|
1514 | Inputs: | |
1515 |
|
1515 | |||
1516 | n : Number of coherent integrations |
|
1516 | n : Number of coherent integrations | |
1517 | timeInterval : Time of integration. If the parameter "n" is selected this one does not work |
|
1517 | timeInterval : Time of integration. If the parameter "n" is selected this one does not work | |
1518 | overlapping : |
|
1518 | overlapping : | |
1519 |
|
1519 | |||
1520 | """ |
|
1520 | """ | |
1521 |
|
1521 | |||
1522 | self.__initime = None |
|
1522 | self.__initime = None | |
1523 | self.__lastdatatime = 0 |
|
1523 | self.__lastdatatime = 0 | |
1524 |
|
1524 | |||
1525 | self.__buffer_spc = 0 |
|
1525 | self.__buffer_spc = 0 | |
1526 | self.__buffer_cspc = 0 |
|
1526 | self.__buffer_cspc = 0 | |
1527 | self.__buffer_dc = 0 |
|
1527 | self.__buffer_dc = 0 | |
1528 |
|
1528 | |||
1529 | self.__profIndex = 0 |
|
1529 | self.__profIndex = 0 | |
1530 | self.__dataReady = False |
|
1530 | self.__dataReady = False | |
1531 | self.__byTime = False |
|
1531 | self.__byTime = False | |
1532 |
|
1532 | |||
1533 | if n is None and timeInterval is None: |
|
1533 | if n is None and timeInterval is None: | |
1534 | raise ValueError("n or timeInterval should be specified ...") |
|
1534 | raise ValueError("n or timeInterval should be specified ...") | |
1535 |
|
1535 | |||
1536 | if n is not None: |
|
1536 | if n is not None: | |
1537 | self.n = int(n) |
|
1537 | self.n = int(n) | |
1538 | else: |
|
1538 | else: | |
1539 |
|
1539 | |||
1540 | self.__integrationtime = int(timeInterval) |
|
1540 | self.__integrationtime = int(timeInterval) | |
1541 | self.n = None |
|
1541 | self.n = None | |
1542 | self.__byTime = True |
|
1542 | self.__byTime = True | |
1543 |
|
1543 | |||
1544 | def putData(self, data_spc, data_cspc, data_dc): |
|
1544 | def putData(self, data_spc, data_cspc, data_dc): | |
1545 | """ |
|
1545 | """ | |
1546 | Add a profile to the __buffer_spc and increase in one the __profileIndex |
|
1546 | Add a profile to the __buffer_spc and increase in one the __profileIndex | |
1547 |
|
1547 | |||
1548 | """ |
|
1548 | """ | |
1549 |
|
1549 | |||
1550 | self.__buffer_spc += data_spc |
|
1550 | self.__buffer_spc += data_spc | |
1551 |
|
1551 | |||
1552 | if data_cspc is None: |
|
1552 | if data_cspc is None: | |
1553 | self.__buffer_cspc = None |
|
1553 | self.__buffer_cspc = None | |
1554 | else: |
|
1554 | else: | |
1555 | self.__buffer_cspc += data_cspc |
|
1555 | self.__buffer_cspc += data_cspc | |
1556 |
|
1556 | |||
1557 | if data_dc is None: |
|
1557 | if data_dc is None: | |
1558 | self.__buffer_dc = None |
|
1558 | self.__buffer_dc = None | |
1559 | else: |
|
1559 | else: | |
1560 | self.__buffer_dc += data_dc |
|
1560 | self.__buffer_dc += data_dc | |
1561 |
|
1561 | |||
1562 | self.__profIndex += 1 |
|
1562 | self.__profIndex += 1 | |
1563 |
|
1563 | |||
1564 | return |
|
1564 | return | |
1565 |
|
1565 | |||
1566 | def pushData(self): |
|
1566 | def pushData(self): | |
1567 | """ |
|
1567 | """ | |
1568 | Return the sum of the last profiles and the profiles used in the sum. |
|
1568 | Return the sum of the last profiles and the profiles used in the sum. | |
1569 |
|
1569 | |||
1570 | Affected: |
|
1570 | Affected: | |
1571 |
|
1571 | |||
1572 | self.__profileIndex |
|
1572 | self.__profileIndex | |
1573 |
|
1573 | |||
1574 | """ |
|
1574 | """ | |
1575 |
|
1575 | |||
1576 | data_spc = self.__buffer_spc |
|
1576 | data_spc = self.__buffer_spc | |
1577 | data_cspc = self.__buffer_cspc |
|
1577 | data_cspc = self.__buffer_cspc | |
1578 | data_dc = self.__buffer_dc |
|
1578 | data_dc = self.__buffer_dc | |
1579 | n = self.__profIndex |
|
1579 | n = self.__profIndex | |
1580 |
|
1580 | |||
1581 | self.__buffer_spc = 0 |
|
1581 | self.__buffer_spc = 0 | |
1582 | self.__buffer_cspc = 0 |
|
1582 | self.__buffer_cspc = 0 | |
1583 | self.__buffer_dc = 0 |
|
1583 | self.__buffer_dc = 0 | |
1584 | self.__profIndex = 0 |
|
1584 | self.__profIndex = 0 | |
1585 |
|
1585 | |||
1586 | return data_spc, data_cspc, data_dc, n |
|
1586 | return data_spc, data_cspc, data_dc, n | |
1587 |
|
1587 | |||
1588 | def byProfiles(self, *args): |
|
1588 | def byProfiles(self, *args): | |
1589 |
|
1589 | |||
1590 | self.__dataReady = False |
|
1590 | self.__dataReady = False | |
1591 | avgdata_spc = None |
|
1591 | avgdata_spc = None | |
1592 | avgdata_cspc = None |
|
1592 | avgdata_cspc = None | |
1593 | avgdata_dc = None |
|
1593 | avgdata_dc = None | |
1594 |
|
1594 | |||
1595 | self.putData(*args) |
|
1595 | self.putData(*args) | |
1596 |
|
1596 | |||
1597 | if self.__profIndex == self.n: |
|
1597 | if self.__profIndex == self.n: | |
1598 |
|
1598 | |||
1599 | avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData() |
|
1599 | avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData() | |
1600 | self.n = n |
|
1600 | self.n = n | |
1601 | self.__dataReady = True |
|
1601 | self.__dataReady = True | |
1602 |
|
1602 | |||
1603 | return avgdata_spc, avgdata_cspc, avgdata_dc |
|
1603 | return avgdata_spc, avgdata_cspc, avgdata_dc | |
1604 |
|
1604 | |||
1605 | def byTime(self, datatime, *args): |
|
1605 | def byTime(self, datatime, *args): | |
1606 |
|
1606 | |||
1607 | self.__dataReady = False |
|
1607 | self.__dataReady = False | |
1608 | avgdata_spc = None |
|
1608 | avgdata_spc = None | |
1609 | avgdata_cspc = None |
|
1609 | avgdata_cspc = None | |
1610 | avgdata_dc = None |
|
1610 | avgdata_dc = None | |
1611 |
|
1611 | |||
1612 | self.putData(*args) |
|
1612 | self.putData(*args) | |
1613 |
|
1613 | |||
1614 | if (datatime - self.__initime) >= self.__integrationtime: |
|
1614 | if (datatime - self.__initime) >= self.__integrationtime: | |
1615 | avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData() |
|
1615 | avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData() | |
1616 | self.n = n |
|
1616 | self.n = n | |
1617 | self.__dataReady = True |
|
1617 | self.__dataReady = True | |
1618 |
|
1618 | |||
1619 | return avgdata_spc, avgdata_cspc, avgdata_dc |
|
1619 | return avgdata_spc, avgdata_cspc, avgdata_dc | |
1620 |
|
1620 | |||
1621 | def integrate(self, datatime, *args): |
|
1621 | def integrate(self, datatime, *args): | |
1622 |
|
1622 | |||
1623 | if self.__profIndex == 0: |
|
1623 | if self.__profIndex == 0: | |
1624 | self.__initime = datatime |
|
1624 | self.__initime = datatime | |
1625 |
|
1625 | |||
1626 | if self.__byTime: |
|
1626 | if self.__byTime: | |
1627 | avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime( |
|
1627 | avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime( | |
1628 | datatime, *args) |
|
1628 | datatime, *args) | |
1629 | else: |
|
1629 | else: | |
1630 | avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args) |
|
1630 | avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args) | |
1631 |
|
1631 | |||
1632 | if not self.__dataReady: |
|
1632 | if not self.__dataReady: | |
1633 | return None, None, None, None |
|
1633 | return None, None, None, None | |
1634 |
|
1634 | |||
1635 | return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc |
|
1635 | return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc | |
1636 |
|
1636 | |||
1637 | def run(self, dataOut, n=None, timeInterval=None, overlapping=False): |
|
1637 | def run(self, dataOut, n=None, timeInterval=None, overlapping=False): | |
1638 | if n == 1: |
|
1638 | if n == 1: | |
1639 | return dataOut |
|
1639 | return dataOut | |
1640 |
|
1640 | |||
1641 | dataOut.flagNoData = True |
|
1641 | dataOut.flagNoData = True | |
1642 |
|
1642 | |||
1643 | if not self.isConfig: |
|
1643 | if not self.isConfig: | |
1644 | self.setup(n, timeInterval, overlapping) |
|
1644 | self.setup(n, timeInterval, overlapping) | |
1645 | self.isConfig = True |
|
1645 | self.isConfig = True | |
1646 |
|
1646 | |||
1647 | avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime, |
|
1647 | avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime, | |
1648 | dataOut.data_spc, |
|
1648 | dataOut.data_spc, | |
1649 | dataOut.data_cspc, |
|
1649 | dataOut.data_cspc, | |
1650 | dataOut.data_dc) |
|
1650 | dataOut.data_dc) | |
1651 |
|
1651 | |||
1652 | if self.__dataReady: |
|
1652 | if self.__dataReady: | |
1653 |
|
1653 | |||
1654 | dataOut.data_spc = avgdata_spc |
|
1654 | dataOut.data_spc = avgdata_spc | |
1655 | dataOut.data_cspc = avgdata_cspc |
|
1655 | dataOut.data_cspc = avgdata_cspc | |
1656 | dataOut.data_dc = avgdata_dc |
|
1656 | dataOut.data_dc = avgdata_dc | |
1657 | dataOut.nIncohInt *= self.n |
|
1657 | dataOut.nIncohInt *= self.n | |
1658 | dataOut.utctime = avgdatatime |
|
1658 | dataOut.utctime = avgdatatime | |
1659 | dataOut.flagNoData = False |
|
1659 | dataOut.flagNoData = False | |
1660 |
|
1660 | |||
1661 | return dataOut |
|
1661 | return dataOut | |
1662 |
|
1662 | |||
1663 | class dopplerFlip(Operation): |
|
1663 | class dopplerFlip(Operation): | |
1664 |
|
1664 | |||
1665 | def run(self, dataOut): |
|
1665 | def run(self, dataOut): | |
1666 | # arreglo 1: (num_chan, num_profiles, num_heights) |
|
1666 | # arreglo 1: (num_chan, num_profiles, num_heights) | |
1667 | self.dataOut = dataOut |
|
1667 | self.dataOut = dataOut | |
1668 | # JULIA-oblicua, indice 2 |
|
1668 | # JULIA-oblicua, indice 2 | |
1669 | # arreglo 2: (num_profiles, num_heights) |
|
1669 | # arreglo 2: (num_profiles, num_heights) | |
1670 | jspectra = self.dataOut.data_spc[2] |
|
1670 | jspectra = self.dataOut.data_spc[2] | |
1671 | jspectra_tmp = numpy.zeros(jspectra.shape) |
|
1671 | jspectra_tmp = numpy.zeros(jspectra.shape) | |
1672 | num_profiles = jspectra.shape[0] |
|
1672 | num_profiles = jspectra.shape[0] | |
1673 | freq_dc = int(num_profiles / 2) |
|
1673 | freq_dc = int(num_profiles / 2) | |
1674 | # Flip con for |
|
1674 | # Flip con for | |
1675 | for j in range(num_profiles): |
|
1675 | for j in range(num_profiles): | |
1676 | jspectra_tmp[num_profiles-j-1]= jspectra[j] |
|
1676 | jspectra_tmp[num_profiles-j-1]= jspectra[j] | |
1677 | # Intercambio perfil de DC con perfil inmediato anterior |
|
1677 | # Intercambio perfil de DC con perfil inmediato anterior | |
1678 | jspectra_tmp[freq_dc-1]= jspectra[freq_dc-1] |
|
1678 | jspectra_tmp[freq_dc-1]= jspectra[freq_dc-1] | |
1679 | jspectra_tmp[freq_dc]= jspectra[freq_dc] |
|
1679 | jspectra_tmp[freq_dc]= jspectra[freq_dc] | |
1680 | # canal modificado es re-escrito en el arreglo de canales |
|
1680 | # canal modificado es re-escrito en el arreglo de canales | |
1681 | self.dataOut.data_spc[2] = jspectra_tmp |
|
1681 | self.dataOut.data_spc[2] = jspectra_tmp | |
1682 |
|
1682 | |||
1683 | return self.dataOut |
|
1683 | return self.dataOut |
General Comments 0
You need to be logged in to leave comments.
Login now