@@ -1,1330 +1,1330 | |||
|
1 | 1 | import sys |
|
2 | 2 | import numpy |
|
3 | 3 | from scipy import interpolate |
|
4 |
from schainpy.model.proc.jroproc_base import ProcessingUnit,, |
|
|
4 | from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator | |
|
5 | 5 | from schainpy.model.data.jrodata import Voltage |
|
6 | 6 | from schainpy.utils import log |
|
7 | 7 | from time import time |
|
8 | 8 | |
|
9 | 9 | |
|
10 | 10 | @MPDecorator |
|
11 | 11 | class VoltageProc(ProcessingUnit): |
|
12 | 12 | |
|
13 | 13 | def __init__(self): |
|
14 | 14 | |
|
15 | 15 | ProcessingUnit.__init__(self) |
|
16 | 16 | |
|
17 | 17 | self.dataOut = Voltage() |
|
18 | 18 | self.flip = 1 |
|
19 | 19 | self.setupReq = False |
|
20 | 20 | |
|
21 | 21 | def run(self): |
|
22 | 22 | |
|
23 | 23 | if self.dataIn.type == 'AMISR': |
|
24 | 24 | self.__updateObjFromAmisrInput() |
|
25 | 25 | |
|
26 | 26 | if self.dataIn.type == 'Voltage': |
|
27 | 27 | self.dataOut.copy(self.dataIn) |
|
28 | 28 | |
|
29 | 29 | # self.dataOut.copy(self.dataIn) |
|
30 | 30 | |
|
31 | 31 | def __updateObjFromAmisrInput(self): |
|
32 | 32 | |
|
33 | 33 | self.dataOut.timeZone = self.dataIn.timeZone |
|
34 | 34 | self.dataOut.dstFlag = self.dataIn.dstFlag |
|
35 | 35 | self.dataOut.errorCount = self.dataIn.errorCount |
|
36 | 36 | self.dataOut.useLocalTime = self.dataIn.useLocalTime |
|
37 | 37 | |
|
38 | 38 | self.dataOut.flagNoData = self.dataIn.flagNoData |
|
39 | 39 | self.dataOut.data = self.dataIn.data |
|
40 | 40 | self.dataOut.utctime = self.dataIn.utctime |
|
41 | 41 | self.dataOut.channelList = self.dataIn.channelList |
|
42 | 42 | # self.dataOut.timeInterval = self.dataIn.timeInterval |
|
43 | 43 | self.dataOut.heightList = self.dataIn.heightList |
|
44 | 44 | self.dataOut.nProfiles = self.dataIn.nProfiles |
|
45 | 45 | |
|
46 | 46 | self.dataOut.nCohInt = self.dataIn.nCohInt |
|
47 | 47 | self.dataOut.ippSeconds = self.dataIn.ippSeconds |
|
48 | 48 | self.dataOut.frequency = self.dataIn.frequency |
|
49 | 49 | |
|
50 | 50 | self.dataOut.azimuth = self.dataIn.azimuth |
|
51 | 51 | self.dataOut.zenith = self.dataIn.zenith |
|
52 | 52 | |
|
53 | 53 | self.dataOut.beam.codeList = self.dataIn.beam.codeList |
|
54 | 54 | self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList |
|
55 | 55 | self.dataOut.beam.zenithList = self.dataIn.beam.zenithList |
|
56 | 56 | # |
|
57 | 57 | # pass# |
|
58 | 58 | # |
|
59 | 59 | # def init(self): |
|
60 | 60 | # |
|
61 | 61 | # |
|
62 | 62 | # if self.dataIn.type == 'AMISR': |
|
63 | 63 | # self.__updateObjFromAmisrInput() |
|
64 | 64 | # |
|
65 | 65 | # if self.dataIn.type == 'Voltage': |
|
66 | 66 | # self.dataOut.copy(self.dataIn) |
|
67 | 67 | # # No necesita copiar en cada init() los atributos de dataIn |
|
68 | 68 | # # la copia deberia hacerse por cada nuevo bloque de datos |
|
69 | 69 | |
|
70 | 70 | def selectChannels(self, channelList): |
|
71 | 71 | |
|
72 | 72 | channelIndexList = [] |
|
73 | 73 | |
|
74 | 74 | for channel in channelList: |
|
75 | 75 | if channel not in self.dataOut.channelList: |
|
76 | 76 | raise ValueError("Channel %d is not in %s" %(channel, str(self.dataOut.channelList))) |
|
77 | 77 | |
|
78 | 78 | index = self.dataOut.channelList.index(channel) |
|
79 | 79 | channelIndexList.append(index) |
|
80 | 80 | |
|
81 | 81 | self.selectChannelsByIndex(channelIndexList) |
|
82 | 82 | |
|
83 | 83 | def selectChannelsByIndex(self, channelIndexList): |
|
84 | 84 | """ |
|
85 | 85 | Selecciona un bloque de datos en base a canales segun el channelIndexList |
|
86 | 86 | |
|
87 | 87 | Input: |
|
88 | 88 | channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7] |
|
89 | 89 | |
|
90 | 90 | Affected: |
|
91 | 91 | self.dataOut.data |
|
92 | 92 | self.dataOut.channelIndexList |
|
93 | 93 | self.dataOut.nChannels |
|
94 | 94 | self.dataOut.m_ProcessingHeader.totalSpectra |
|
95 | 95 | self.dataOut.systemHeaderObj.numChannels |
|
96 | 96 | self.dataOut.m_ProcessingHeader.blockSize |
|
97 | 97 | |
|
98 | 98 | Return: |
|
99 | 99 | None |
|
100 | 100 | """ |
|
101 | 101 | |
|
102 | 102 | for channelIndex in channelIndexList: |
|
103 | 103 | if channelIndex not in self.dataOut.channelIndexList: |
|
104 | 104 | print(channelIndexList) |
|
105 | 105 | raise ValueError("The value %d in channelIndexList is not valid" %channelIndex) |
|
106 | 106 | |
|
107 | 107 | if self.dataOut.flagDataAsBlock: |
|
108 | 108 | """ |
|
109 | 109 | Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis] |
|
110 | 110 | """ |
|
111 | 111 | data = self.dataOut.data[channelIndexList,:,:] |
|
112 | 112 | else: |
|
113 | 113 | data = self.dataOut.data[channelIndexList,:] |
|
114 | 114 | |
|
115 | 115 | self.dataOut.data = data |
|
116 | 116 | self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList] |
|
117 | 117 | # self.dataOut.nChannels = nChannels |
|
118 | 118 | |
|
119 | 119 | return 1 |
|
120 | 120 | |
|
121 | 121 | def selectHeights(self, minHei=None, maxHei=None): |
|
122 | 122 | """ |
|
123 | 123 | Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango |
|
124 | 124 | minHei <= height <= maxHei |
|
125 | 125 | |
|
126 | 126 | Input: |
|
127 | 127 | minHei : valor minimo de altura a considerar |
|
128 | 128 | maxHei : valor maximo de altura a considerar |
|
129 | 129 | |
|
130 | 130 | Affected: |
|
131 | 131 | Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex |
|
132 | 132 | |
|
133 | 133 | Return: |
|
134 | 134 | 1 si el metodo se ejecuto con exito caso contrario devuelve 0 |
|
135 | 135 | """ |
|
136 | 136 | |
|
137 | 137 | if minHei == None: |
|
138 | 138 | minHei = self.dataOut.heightList[0] |
|
139 | 139 | |
|
140 | 140 | if maxHei == None: |
|
141 | 141 | maxHei = self.dataOut.heightList[-1] |
|
142 | 142 | |
|
143 | 143 | if (minHei < self.dataOut.heightList[0]): |
|
144 | 144 | minHei = self.dataOut.heightList[0] |
|
145 | 145 | |
|
146 | 146 | if (maxHei > self.dataOut.heightList[-1]): |
|
147 | 147 | maxHei = self.dataOut.heightList[-1] |
|
148 | 148 | |
|
149 | 149 | minIndex = 0 |
|
150 | 150 | maxIndex = 0 |
|
151 | 151 | heights = self.dataOut.heightList |
|
152 | 152 | |
|
153 | 153 | inda = numpy.where(heights >= minHei) |
|
154 | 154 | indb = numpy.where(heights <= maxHei) |
|
155 | 155 | |
|
156 | 156 | try: |
|
157 | 157 | minIndex = inda[0][0] |
|
158 | 158 | except: |
|
159 | 159 | minIndex = 0 |
|
160 | 160 | |
|
161 | 161 | try: |
|
162 | 162 | maxIndex = indb[0][-1] |
|
163 | 163 | except: |
|
164 | 164 | maxIndex = len(heights) |
|
165 | 165 | |
|
166 | 166 | self.selectHeightsByIndex(minIndex, maxIndex) |
|
167 | 167 | |
|
168 | 168 | return 1 |
|
169 | 169 | |
|
170 | 170 | |
|
171 | 171 | def selectHeightsByIndex(self, minIndex, maxIndex): |
|
172 | 172 | """ |
|
173 | 173 | Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango |
|
174 | 174 | minIndex <= index <= maxIndex |
|
175 | 175 | |
|
176 | 176 | Input: |
|
177 | 177 | minIndex : valor de indice minimo de altura a considerar |
|
178 | 178 | maxIndex : valor de indice maximo de altura a considerar |
|
179 | 179 | |
|
180 | 180 | Affected: |
|
181 | 181 | self.dataOut.data |
|
182 | 182 | self.dataOut.heightList |
|
183 | 183 | |
|
184 | 184 | Return: |
|
185 | 185 | 1 si el metodo se ejecuto con exito caso contrario devuelve 0 |
|
186 | 186 | """ |
|
187 | 187 | |
|
188 | 188 | if (minIndex < 0) or (minIndex > maxIndex): |
|
189 | 189 | raise ValueError("Height index range (%d,%d) is not valid" % (minIndex, maxIndex)) |
|
190 | 190 | |
|
191 | 191 | if (maxIndex >= self.dataOut.nHeights): |
|
192 | 192 | maxIndex = self.dataOut.nHeights |
|
193 | 193 | |
|
194 | 194 | #voltage |
|
195 | 195 | if self.dataOut.flagDataAsBlock: |
|
196 | 196 | """ |
|
197 | 197 | Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis] |
|
198 | 198 | """ |
|
199 | 199 | data = self.dataOut.data[:,:, minIndex:maxIndex] |
|
200 | 200 | else: |
|
201 | 201 | data = self.dataOut.data[:, minIndex:maxIndex] |
|
202 | 202 | |
|
203 | 203 | # firstHeight = self.dataOut.heightList[minIndex] |
|
204 | 204 | |
|
205 | 205 | self.dataOut.data = data |
|
206 | 206 | self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex] |
|
207 | 207 | |
|
208 | 208 | if self.dataOut.nHeights <= 1: |
|
209 | 209 | raise ValueError("selectHeights: Too few heights. Current number of heights is %d" %(self.dataOut.nHeights)) |
|
210 | 210 | |
|
211 | 211 | return 1 |
|
212 | 212 | |
|
213 | 213 | |
|
214 | 214 | def filterByHeights(self, window): |
|
215 | 215 | |
|
216 | 216 | deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] |
|
217 | 217 | |
|
218 | 218 | if window == None: |
|
219 | 219 | window = (self.dataOut.radarControllerHeaderObj.txA/self.dataOut.radarControllerHeaderObj.nBaud) / deltaHeight |
|
220 | 220 | |
|
221 | 221 | newdelta = deltaHeight * window |
|
222 | 222 | r = self.dataOut.nHeights % window |
|
223 | 223 | newheights = (self.dataOut.nHeights-r)/window |
|
224 | 224 | |
|
225 | 225 | if newheights <= 1: |
|
226 | 226 | raise ValueError("filterByHeights: Too few heights. Current number of heights is %d and window is %d" %(self.dataOut.nHeights, window)) |
|
227 | 227 | |
|
228 | 228 | if self.dataOut.flagDataAsBlock: |
|
229 | 229 | """ |
|
230 | 230 | Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis] |
|
231 | 231 | """ |
|
232 | 232 | buffer = self.dataOut.data[:, :, 0:self.dataOut.nHeights-r] |
|
233 | 233 | buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nProfiles,self.dataOut.nHeights/window,window) |
|
234 | 234 | buffer = numpy.sum(buffer,3) |
|
235 | 235 | |
|
236 | 236 | else: |
|
237 | 237 | buffer = self.dataOut.data[:,0:self.dataOut.nHeights-r] |
|
238 | 238 | buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nHeights/window,window) |
|
239 | 239 | buffer = numpy.sum(buffer,2) |
|
240 | 240 | |
|
241 | 241 | self.dataOut.data = buffer |
|
242 | 242 | self.dataOut.heightList = self.dataOut.heightList[0] + numpy.arange( newheights )*newdelta |
|
243 | 243 | self.dataOut.windowOfFilter = window |
|
244 | 244 | |
|
245 | 245 | def setH0(self, h0, deltaHeight = None): |
|
246 | 246 | |
|
247 | 247 | if not deltaHeight: |
|
248 | 248 | deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] |
|
249 | 249 | |
|
250 | 250 | nHeights = self.dataOut.nHeights |
|
251 | 251 | |
|
252 | 252 | newHeiRange = h0 + numpy.arange(nHeights)*deltaHeight |
|
253 | 253 | |
|
254 | 254 | self.dataOut.heightList = newHeiRange |
|
255 | 255 | |
|
256 | 256 | def deFlip(self, channelList = []): |
|
257 | 257 | |
|
258 | 258 | data = self.dataOut.data.copy() |
|
259 | 259 | |
|
260 | 260 | if self.dataOut.flagDataAsBlock: |
|
261 | 261 | flip = self.flip |
|
262 | 262 | profileList = list(range(self.dataOut.nProfiles)) |
|
263 | 263 | |
|
264 | 264 | if not channelList: |
|
265 | 265 | for thisProfile in profileList: |
|
266 | 266 | data[:,thisProfile,:] = data[:,thisProfile,:]*flip |
|
267 | 267 | flip *= -1.0 |
|
268 | 268 | else: |
|
269 | 269 | for thisChannel in channelList: |
|
270 | 270 | if thisChannel not in self.dataOut.channelList: |
|
271 | 271 | continue |
|
272 | 272 | |
|
273 | 273 | for thisProfile in profileList: |
|
274 | 274 | data[thisChannel,thisProfile,:] = data[thisChannel,thisProfile,:]*flip |
|
275 | 275 | flip *= -1.0 |
|
276 | 276 | |
|
277 | 277 | self.flip = flip |
|
278 | 278 | |
|
279 | 279 | else: |
|
280 | 280 | if not channelList: |
|
281 | 281 | data[:,:] = data[:,:]*self.flip |
|
282 | 282 | else: |
|
283 | 283 | for thisChannel in channelList: |
|
284 | 284 | if thisChannel not in self.dataOut.channelList: |
|
285 | 285 | continue |
|
286 | 286 | |
|
287 | 287 | data[thisChannel,:] = data[thisChannel,:]*self.flip |
|
288 | 288 | |
|
289 | 289 | self.flip *= -1. |
|
290 | 290 | |
|
291 | 291 | self.dataOut.data = data |
|
292 | 292 | |
|
293 | 293 | def setRadarFrequency(self, frequency=None): |
|
294 | 294 | |
|
295 | 295 | if frequency != None: |
|
296 | 296 | self.dataOut.frequency = frequency |
|
297 | 297 | |
|
298 | 298 | return 1 |
|
299 | 299 | |
|
300 | 300 | def interpolateHeights(self, topLim, botLim): |
|
301 | 301 | #69 al 72 para julia |
|
302 | 302 | #82-84 para meteoros |
|
303 | 303 | if len(numpy.shape(self.dataOut.data))==2: |
|
304 | 304 | sampInterp = (self.dataOut.data[:,botLim-1] + self.dataOut.data[:,topLim+1])/2 |
|
305 | 305 | sampInterp = numpy.transpose(numpy.tile(sampInterp,(topLim-botLim + 1,1))) |
|
306 | 306 | #self.dataOut.data[:,botLim:limSup+1] = sampInterp |
|
307 | 307 | self.dataOut.data[:,botLim:topLim+1] = sampInterp |
|
308 | 308 | else: |
|
309 | 309 | nHeights = self.dataOut.data.shape[2] |
|
310 | 310 | x = numpy.hstack((numpy.arange(botLim),numpy.arange(topLim+1,nHeights))) |
|
311 | 311 | y = self.dataOut.data[:,:,list(range(botLim))+list(range(topLim+1,nHeights))] |
|
312 | 312 | f = interpolate.interp1d(x, y, axis = 2) |
|
313 | 313 | xnew = numpy.arange(botLim,topLim+1) |
|
314 | 314 | ynew = f(xnew) |
|
315 | 315 | |
|
316 | 316 | self.dataOut.data[:,:,botLim:topLim+1] = ynew |
|
317 | 317 | |
|
318 | 318 | # import collections |
|
319 | 319 | |
|
320 | 320 | class CohInt(Operation): |
|
321 | 321 | |
|
322 | 322 | isConfig = False |
|
323 | 323 | __profIndex = 0 |
|
324 | 324 | __byTime = False |
|
325 | 325 | __initime = None |
|
326 | 326 | __lastdatatime = None |
|
327 | 327 | __integrationtime = None |
|
328 | 328 | __buffer = None |
|
329 | 329 | __bufferStride = [] |
|
330 | 330 | __dataReady = False |
|
331 | 331 | __profIndexStride = 0 |
|
332 | 332 | __dataToPutStride = False |
|
333 | 333 | n = None |
|
334 | 334 | |
|
335 | 335 | def __init__(self):#, **kwargs): |
|
336 | 336 | |
|
337 | 337 | Operation.__init__(self)#, **kwargs) |
|
338 | 338 | |
|
339 | 339 | # self.isConfig = False |
|
340 | 340 | |
|
341 | 341 | def setup(self, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False): |
|
342 | 342 | """ |
|
343 | 343 | Set the parameters of the integration class. |
|
344 | 344 | |
|
345 | 345 | Inputs: |
|
346 | 346 | |
|
347 | 347 | n : Number of coherent integrations |
|
348 | 348 | timeInterval : Time of integration. If the parameter "n" is selected this one does not work |
|
349 | 349 | overlapping : |
|
350 | 350 | """ |
|
351 | 351 | |
|
352 | 352 | self.__initime = None |
|
353 | 353 | self.__lastdatatime = 0 |
|
354 | 354 | self.__buffer = None |
|
355 | 355 | self.__dataReady = False |
|
356 | 356 | self.byblock = byblock |
|
357 | 357 | self.stride = stride |
|
358 | 358 | |
|
359 | 359 | if n == None and timeInterval == None: |
|
360 | 360 | raise ValueError("n or timeInterval should be specified ...") |
|
361 | 361 | |
|
362 | 362 | if n != None: |
|
363 | 363 | self.n = n |
|
364 | 364 | self.__byTime = False |
|
365 | 365 | else: |
|
366 | 366 | self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line |
|
367 | 367 | self.n = 9999 |
|
368 | 368 | self.__byTime = True |
|
369 | 369 | |
|
370 | 370 | if overlapping: |
|
371 | 371 | self.__withOverlapping = True |
|
372 | 372 | self.__buffer = None |
|
373 | 373 | else: |
|
374 | 374 | self.__withOverlapping = False |
|
375 | 375 | self.__buffer = 0 |
|
376 | 376 | |
|
377 | 377 | self.__profIndex = 0 |
|
378 | 378 | |
|
379 | 379 | def putData(self, data): |
|
380 | 380 | |
|
381 | 381 | """ |
|
382 | 382 | Add a profile to the __buffer and increase in one the __profileIndex |
|
383 | 383 | |
|
384 | 384 | """ |
|
385 | 385 | |
|
386 | 386 | if not self.__withOverlapping: |
|
387 | 387 | self.__buffer += data.copy() |
|
388 | 388 | self.__profIndex += 1 |
|
389 | 389 | return |
|
390 | 390 | |
|
391 | 391 | #Overlapping data |
|
392 | 392 | nChannels, nHeis = data.shape |
|
393 | 393 | data = numpy.reshape(data, (1, nChannels, nHeis)) |
|
394 | 394 | |
|
395 | 395 | #If the buffer is empty then it takes the data value |
|
396 | 396 | if self.__buffer is None: |
|
397 | 397 | self.__buffer = data |
|
398 | 398 | self.__profIndex += 1 |
|
399 | 399 | return |
|
400 | 400 | |
|
401 | 401 | #If the buffer length is lower than n then stakcing the data value |
|
402 | 402 | if self.__profIndex < self.n: |
|
403 | 403 | self.__buffer = numpy.vstack((self.__buffer, data)) |
|
404 | 404 | self.__profIndex += 1 |
|
405 | 405 | return |
|
406 | 406 | |
|
407 | 407 | #If the buffer length is equal to n then replacing the last buffer value with the data value |
|
408 | 408 | self.__buffer = numpy.roll(self.__buffer, -1, axis=0) |
|
409 | 409 | self.__buffer[self.n-1] = data |
|
410 | 410 | self.__profIndex = self.n |
|
411 | 411 | return |
|
412 | 412 | |
|
413 | 413 | |
|
414 | 414 | def pushData(self): |
|
415 | 415 | """ |
|
416 | 416 | Return the sum of the last profiles and the profiles used in the sum. |
|
417 | 417 | |
|
418 | 418 | Affected: |
|
419 | 419 | |
|
420 | 420 | self.__profileIndex |
|
421 | 421 | |
|
422 | 422 | """ |
|
423 | 423 | |
|
424 | 424 | if not self.__withOverlapping: |
|
425 | 425 | data = self.__buffer |
|
426 | 426 | n = self.__profIndex |
|
427 | 427 | |
|
428 | 428 | self.__buffer = 0 |
|
429 | 429 | self.__profIndex = 0 |
|
430 | 430 | |
|
431 | 431 | return data, n |
|
432 | 432 | |
|
433 | 433 | #Integration with Overlapping |
|
434 | 434 | data = numpy.sum(self.__buffer, axis=0) |
|
435 | 435 | # print data |
|
436 | 436 | # raise |
|
437 | 437 | n = self.__profIndex |
|
438 | 438 | |
|
439 | 439 | return data, n |
|
440 | 440 | |
|
441 | 441 | def byProfiles(self, data): |
|
442 | 442 | |
|
443 | 443 | self.__dataReady = False |
|
444 | 444 | avgdata = None |
|
445 | 445 | # n = None |
|
446 | 446 | # print data |
|
447 | 447 | # raise |
|
448 | 448 | self.putData(data) |
|
449 | 449 | |
|
450 | 450 | if self.__profIndex == self.n: |
|
451 | 451 | avgdata, n = self.pushData() |
|
452 | 452 | self.__dataReady = True |
|
453 | 453 | |
|
454 | 454 | return avgdata |
|
455 | 455 | |
|
456 | 456 | def byTime(self, data, datatime): |
|
457 | 457 | |
|
458 | 458 | self.__dataReady = False |
|
459 | 459 | avgdata = None |
|
460 | 460 | n = None |
|
461 | 461 | |
|
462 | 462 | self.putData(data) |
|
463 | 463 | |
|
464 | 464 | if (datatime - self.__initime) >= self.__integrationtime: |
|
465 | 465 | avgdata, n = self.pushData() |
|
466 | 466 | self.n = n |
|
467 | 467 | self.__dataReady = True |
|
468 | 468 | |
|
469 | 469 | return avgdata |
|
470 | 470 | |
|
471 | 471 | def integrateByStride(self, data, datatime): |
|
472 | 472 | # print data |
|
473 | 473 | if self.__profIndex == 0: |
|
474 | 474 | self.__buffer = [[data.copy(), datatime]] |
|
475 | 475 | else: |
|
476 | 476 | self.__buffer.append([data.copy(),datatime]) |
|
477 | 477 | self.__profIndex += 1 |
|
478 | 478 | self.__dataReady = False |
|
479 | 479 | |
|
480 | 480 | if self.__profIndex == self.n * self.stride : |
|
481 | 481 | self.__dataToPutStride = True |
|
482 | 482 | self.__profIndexStride = 0 |
|
483 | 483 | self.__profIndex = 0 |
|
484 | 484 | self.__bufferStride = [] |
|
485 | 485 | for i in range(self.stride): |
|
486 | 486 | current = self.__buffer[i::self.stride] |
|
487 | 487 | data = numpy.sum([t[0] for t in current], axis=0) |
|
488 | 488 | avgdatatime = numpy.average([t[1] for t in current]) |
|
489 | 489 | # print data |
|
490 | 490 | self.__bufferStride.append((data, avgdatatime)) |
|
491 | 491 | |
|
492 | 492 | if self.__dataToPutStride: |
|
493 | 493 | self.__dataReady = True |
|
494 | 494 | self.__profIndexStride += 1 |
|
495 | 495 | if self.__profIndexStride == self.stride: |
|
496 | 496 | self.__dataToPutStride = False |
|
497 | 497 | # print self.__bufferStride[self.__profIndexStride - 1] |
|
498 | 498 | # raise |
|
499 | 499 | return self.__bufferStride[self.__profIndexStride - 1] |
|
500 | 500 | |
|
501 | 501 | |
|
502 | 502 | return None, None |
|
503 | 503 | |
|
504 | 504 | def integrate(self, data, datatime=None): |
|
505 | 505 | |
|
506 | 506 | if self.__initime == None: |
|
507 | 507 | self.__initime = datatime |
|
508 | 508 | |
|
509 | 509 | if self.__byTime: |
|
510 | 510 | avgdata = self.byTime(data, datatime) |
|
511 | 511 | else: |
|
512 | 512 | avgdata = self.byProfiles(data) |
|
513 | 513 | |
|
514 | 514 | |
|
515 | 515 | self.__lastdatatime = datatime |
|
516 | 516 | |
|
517 | 517 | if avgdata is None: |
|
518 | 518 | return None, None |
|
519 | 519 | |
|
520 | 520 | avgdatatime = self.__initime |
|
521 | 521 | |
|
522 | 522 | deltatime = datatime - self.__lastdatatime |
|
523 | 523 | |
|
524 | 524 | if not self.__withOverlapping: |
|
525 | 525 | self.__initime = datatime |
|
526 | 526 | else: |
|
527 | 527 | self.__initime += deltatime |
|
528 | 528 | |
|
529 | 529 | return avgdata, avgdatatime |
|
530 | 530 | |
|
531 | 531 | def integrateByBlock(self, dataOut): |
|
532 | 532 | |
|
533 | 533 | times = int(dataOut.data.shape[1]/self.n) |
|
534 | 534 | avgdata = numpy.zeros((dataOut.nChannels, times, dataOut.nHeights), dtype=numpy.complex) |
|
535 | 535 | |
|
536 | 536 | id_min = 0 |
|
537 | 537 | id_max = self.n |
|
538 | 538 | |
|
539 | 539 | for i in range(times): |
|
540 | 540 | junk = dataOut.data[:,id_min:id_max,:] |
|
541 | 541 | avgdata[:,i,:] = junk.sum(axis=1) |
|
542 | 542 | id_min += self.n |
|
543 | 543 | id_max += self.n |
|
544 | 544 | |
|
545 | 545 | timeInterval = dataOut.ippSeconds*self.n |
|
546 | 546 | avgdatatime = (times - 1) * timeInterval + dataOut.utctime |
|
547 | 547 | self.__dataReady = True |
|
548 | 548 | return avgdata, avgdatatime |
|
549 | 549 | |
|
550 | 550 | def run(self, dataOut, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False, **kwargs): |
|
551 | 551 | |
|
552 | 552 | if not self.isConfig: |
|
553 | 553 | self.setup(n=n, stride=stride, timeInterval=timeInterval, overlapping=overlapping, byblock=byblock, **kwargs) |
|
554 | 554 | self.isConfig = True |
|
555 | 555 | |
|
556 | 556 | if dataOut.flagDataAsBlock: |
|
557 | 557 | """ |
|
558 | 558 | Si la data es leida por bloques, dimension = [nChannels, nProfiles, nHeis] |
|
559 | 559 | """ |
|
560 | 560 | avgdata, avgdatatime = self.integrateByBlock(dataOut) |
|
561 | 561 | dataOut.nProfiles /= self.n |
|
562 | 562 | else: |
|
563 | 563 | if stride is None: |
|
564 | 564 | avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime) |
|
565 | 565 | else: |
|
566 | 566 | avgdata, avgdatatime = self.integrateByStride(dataOut.data, dataOut.utctime) |
|
567 | 567 | |
|
568 | 568 | |
|
569 | 569 | # dataOut.timeInterval *= n |
|
570 | 570 | dataOut.flagNoData = True |
|
571 | 571 | |
|
572 | 572 | if self.__dataReady: |
|
573 | 573 | dataOut.data = avgdata |
|
574 | 574 | dataOut.nCohInt *= self.n |
|
575 | 575 | dataOut.utctime = avgdatatime |
|
576 | 576 | # print avgdata, avgdatatime |
|
577 | 577 | # raise |
|
578 | 578 | # dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt |
|
579 | 579 | dataOut.flagNoData = False |
|
580 | 580 | return dataOut |
|
581 | 581 | |
|
582 | 582 | class Decoder(Operation): |
|
583 | 583 | |
|
584 | 584 | isConfig = False |
|
585 | 585 | __profIndex = 0 |
|
586 | 586 | |
|
587 | 587 | code = None |
|
588 | 588 | |
|
589 | 589 | nCode = None |
|
590 | 590 | nBaud = None |
|
591 | 591 | |
|
592 | 592 | def __init__(self):#, **kwargs): |
|
593 | 593 | |
|
594 | 594 | Operation.__init__(self)#, **kwargs) |
|
595 | 595 | |
|
596 | 596 | self.times = None |
|
597 | 597 | self.osamp = None |
|
598 | 598 | # self.__setValues = False |
|
599 | 599 | # self.isConfig = False |
|
600 | 600 | self.setupReq = False |
|
601 | 601 | def setup(self, code, osamp, dataOut): |
|
602 | 602 | |
|
603 | 603 | self.__profIndex = 0 |
|
604 | 604 | |
|
605 | 605 | self.code = code |
|
606 | 606 | |
|
607 | 607 | self.nCode = len(code) |
|
608 | 608 | self.nBaud = len(code[0]) |
|
609 | 609 | |
|
610 | 610 | if (osamp != None) and (osamp >1): |
|
611 | 611 | self.osamp = osamp |
|
612 | 612 | self.code = numpy.repeat(code, repeats=self.osamp, axis=1) |
|
613 | 613 | self.nBaud = self.nBaud*self.osamp |
|
614 | 614 | |
|
615 | 615 | self.__nChannels = dataOut.nChannels |
|
616 | 616 | self.__nProfiles = dataOut.nProfiles |
|
617 | 617 | self.__nHeis = dataOut.nHeights |
|
618 | 618 | |
|
619 | 619 | if self.__nHeis < self.nBaud: |
|
620 | 620 | raise ValueError('Number of heights (%d) should be greater than number of bauds (%d)' %(self.__nHeis, self.nBaud)) |
|
621 | 621 | |
|
622 | 622 | #Frequency |
|
623 | 623 | __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex) |
|
624 | 624 | |
|
625 | 625 | __codeBuffer[:,0:self.nBaud] = self.code |
|
626 | 626 | |
|
627 | 627 | self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1)) |
|
628 | 628 | |
|
629 | 629 | if dataOut.flagDataAsBlock: |
|
630 | 630 | |
|
631 | 631 | self.ndatadec = self.__nHeis #- self.nBaud + 1 |
|
632 | 632 | |
|
633 | 633 | self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex) |
|
634 | 634 | |
|
635 | 635 | else: |
|
636 | 636 | |
|
637 | 637 | #Time |
|
638 | 638 | self.ndatadec = self.__nHeis #- self.nBaud + 1 |
|
639 | 639 | |
|
640 | 640 | self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex) |
|
641 | 641 | |
|
642 | 642 | def __convolutionInFreq(self, data): |
|
643 | 643 | |
|
644 | 644 | fft_code = self.fft_code[self.__profIndex].reshape(1,-1) |
|
645 | 645 | |
|
646 | 646 | fft_data = numpy.fft.fft(data, axis=1) |
|
647 | 647 | |
|
648 | 648 | conv = fft_data*fft_code |
|
649 | 649 | |
|
650 | 650 | data = numpy.fft.ifft(conv,axis=1) |
|
651 | 651 | |
|
652 | 652 | return data |
|
653 | 653 | |
|
654 | 654 | def __convolutionInFreqOpt(self, data): |
|
655 | 655 | |
|
656 | 656 | raise NotImplementedError |
|
657 | 657 | |
|
658 | 658 | def __convolutionInTime(self, data): |
|
659 | 659 | |
|
660 | 660 | code = self.code[self.__profIndex] |
|
661 | 661 | for i in range(self.__nChannels): |
|
662 | 662 | self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='full')[self.nBaud-1:] |
|
663 | 663 | |
|
664 | 664 | return self.datadecTime |
|
665 | 665 | |
|
666 | 666 | def __convolutionByBlockInTime(self, data): |
|
667 | 667 | |
|
668 | 668 | repetitions = self.__nProfiles / self.nCode |
|
669 | 669 | |
|
670 | 670 | junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize)) |
|
671 | 671 | junk = junk.flatten() |
|
672 | 672 | code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud)) |
|
673 | 673 | profilesList = range(self.__nProfiles) |
|
674 | 674 | |
|
675 | 675 | for i in range(self.__nChannels): |
|
676 | 676 | for j in profilesList: |
|
677 | 677 | self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:] |
|
678 | 678 | return self.datadecTime |
|
679 | 679 | |
|
680 | 680 | def __convolutionByBlockInFreq(self, data): |
|
681 | 681 | |
|
682 | 682 | raise NotImplementedError("Decoder by frequency fro Blocks not implemented") |
|
683 | 683 | |
|
684 | 684 | |
|
685 | 685 | fft_code = self.fft_code[self.__profIndex].reshape(1,-1) |
|
686 | 686 | |
|
687 | 687 | fft_data = numpy.fft.fft(data, axis=2) |
|
688 | 688 | |
|
689 | 689 | conv = fft_data*fft_code |
|
690 | 690 | |
|
691 | 691 | data = numpy.fft.ifft(conv,axis=2) |
|
692 | 692 | |
|
693 | 693 | return data |
|
694 | 694 | |
|
695 | 695 | |
|
696 | 696 | def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None): |
|
697 | 697 | |
|
698 | 698 | if dataOut.flagDecodeData: |
|
699 | 699 | print("This data is already decoded, recoding again ...") |
|
700 | 700 | |
|
701 | 701 | if not self.isConfig: |
|
702 | 702 | |
|
703 | 703 | if code is None: |
|
704 | 704 | if dataOut.code is None: |
|
705 | 705 | raise ValueError("Code could not be read from %s instance. Enter a value in Code parameter" %dataOut.type) |
|
706 | 706 | |
|
707 | 707 | code = dataOut.code |
|
708 | 708 | else: |
|
709 | 709 | code = numpy.array(code).reshape(nCode,nBaud) |
|
710 | 710 | self.setup(code, osamp, dataOut) |
|
711 | 711 | |
|
712 | 712 | self.isConfig = True |
|
713 | 713 | |
|
714 | 714 | if mode == 3: |
|
715 | 715 | sys.stderr.write("Decoder Warning: mode=%d is not valid, using mode=0\n" %mode) |
|
716 | 716 | |
|
717 | 717 | if times != None: |
|
718 | 718 | sys.stderr.write("Decoder Warning: Argument 'times' in not used anymore\n") |
|
719 | 719 | |
|
720 | 720 | if self.code is None: |
|
721 | 721 | print("Fail decoding: Code is not defined.") |
|
722 | 722 | return |
|
723 | 723 | |
|
724 | 724 | self.__nProfiles = dataOut.nProfiles |
|
725 | 725 | datadec = None |
|
726 | 726 | |
|
727 | 727 | if mode == 3: |
|
728 | 728 | mode = 0 |
|
729 | 729 | |
|
730 | 730 | if dataOut.flagDataAsBlock: |
|
731 | 731 | """ |
|
732 | 732 | Decoding when data have been read as block, |
|
733 | 733 | """ |
|
734 | 734 | |
|
735 | 735 | if mode == 0: |
|
736 | 736 | datadec = self.__convolutionByBlockInTime(dataOut.data) |
|
737 | 737 | if mode == 1: |
|
738 | 738 | datadec = self.__convolutionByBlockInFreq(dataOut.data) |
|
739 | 739 | else: |
|
740 | 740 | """ |
|
741 | 741 | Decoding when data have been read profile by profile |
|
742 | 742 | """ |
|
743 | 743 | if mode == 0: |
|
744 | 744 | datadec = self.__convolutionInTime(dataOut.data) |
|
745 | 745 | |
|
746 | 746 | if mode == 1: |
|
747 | 747 | datadec = self.__convolutionInFreq(dataOut.data) |
|
748 | 748 | |
|
749 | 749 | if mode == 2: |
|
750 | 750 | datadec = self.__convolutionInFreqOpt(dataOut.data) |
|
751 | 751 | |
|
752 | 752 | if datadec is None: |
|
753 | 753 | raise ValueError("Codification mode selected is not valid: mode=%d. Try selecting 0 or 1" %mode) |
|
754 | 754 | |
|
755 | 755 | dataOut.code = self.code |
|
756 | 756 | dataOut.nCode = self.nCode |
|
757 | 757 | dataOut.nBaud = self.nBaud |
|
758 | 758 | |
|
759 | 759 | dataOut.data = datadec |
|
760 | 760 | |
|
761 | 761 | dataOut.heightList = dataOut.heightList[0:datadec.shape[-1]] |
|
762 | 762 | |
|
763 | 763 | dataOut.flagDecodeData = True #asumo q la data esta decodificada |
|
764 | 764 | |
|
765 | 765 | if self.__profIndex == self.nCode-1: |
|
766 | 766 | self.__profIndex = 0 |
|
767 | 767 | return dataOut |
|
768 | 768 | |
|
769 | 769 | self.__profIndex += 1 |
|
770 | 770 | |
|
771 | 771 | return dataOut |
|
772 | 772 | # dataOut.flagDeflipData = True #asumo q la data no esta sin flip |
|
773 | 773 | |
|
774 | 774 | |
|
775 | 775 | class ProfileConcat(Operation): |
|
776 | 776 | |
|
777 | 777 | isConfig = False |
|
778 | 778 | buffer = None |
|
779 | 779 | |
|
780 | 780 | def __init__(self):#, **kwargs): |
|
781 | 781 | |
|
782 | 782 | Operation.__init__(self)#, **kwargs) |
|
783 | 783 | self.profileIndex = 0 |
|
784 | 784 | |
|
785 | 785 | def reset(self): |
|
786 | 786 | self.buffer = numpy.zeros_like(self.buffer) |
|
787 | 787 | self.start_index = 0 |
|
788 | 788 | self.times = 1 |
|
789 | 789 | |
|
790 | 790 | def setup(self, data, m, n=1): |
|
791 | 791 | self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0])) |
|
792 | 792 | self.nHeights = data.shape[1]#.nHeights |
|
793 | 793 | self.start_index = 0 |
|
794 | 794 | self.times = 1 |
|
795 | 795 | |
|
796 | 796 | def concat(self, data): |
|
797 | 797 | |
|
798 | 798 | self.buffer[:,self.start_index:self.nHeights*self.times] = data.copy() |
|
799 | 799 | self.start_index = self.start_index + self.nHeights |
|
800 | 800 | |
|
801 | 801 | def run(self, dataOut, m): |
|
802 | 802 | |
|
803 | 803 | dataOut.flagNoData = True |
|
804 | 804 | |
|
805 | 805 | if not self.isConfig: |
|
806 | 806 | self.setup(dataOut.data, m, 1) |
|
807 | 807 | self.isConfig = True |
|
808 | 808 | |
|
809 | 809 | if dataOut.flagDataAsBlock: |
|
810 | 810 | raise ValueError("ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False") |
|
811 | 811 | |
|
812 | 812 | else: |
|
813 | 813 | self.concat(dataOut.data) |
|
814 | 814 | self.times += 1 |
|
815 | 815 | if self.times > m: |
|
816 | 816 | dataOut.data = self.buffer |
|
817 | 817 | self.reset() |
|
818 | 818 | dataOut.flagNoData = False |
|
819 | 819 | # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas |
|
820 | 820 | deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] |
|
821 | 821 | xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * m |
|
822 | 822 | dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight) |
|
823 | 823 | dataOut.ippSeconds *= m |
|
824 | 824 | return dataOut |
|
825 | 825 | |
|
826 | 826 | class ProfileSelector(Operation): |
|
827 | 827 | |
|
828 | 828 | profileIndex = None |
|
829 | 829 | # Tamanho total de los perfiles |
|
830 | 830 | nProfiles = None |
|
831 | 831 | |
|
832 | 832 | def __init__(self):#, **kwargs): |
|
833 | 833 | |
|
834 | 834 | Operation.__init__(self)#, **kwargs) |
|
835 | 835 | self.profileIndex = 0 |
|
836 | 836 | |
|
837 | 837 | def incProfileIndex(self): |
|
838 | 838 | |
|
839 | 839 | self.profileIndex += 1 |
|
840 | 840 | |
|
841 | 841 | if self.profileIndex >= self.nProfiles: |
|
842 | 842 | self.profileIndex = 0 |
|
843 | 843 | |
|
844 | 844 | def isThisProfileInRange(self, profileIndex, minIndex, maxIndex): |
|
845 | 845 | |
|
846 | 846 | if profileIndex < minIndex: |
|
847 | 847 | return False |
|
848 | 848 | |
|
849 | 849 | if profileIndex > maxIndex: |
|
850 | 850 | return False |
|
851 | 851 | |
|
852 | 852 | return True |
|
853 | 853 | |
|
854 | 854 | def isThisProfileInList(self, profileIndex, profileList): |
|
855 | 855 | |
|
856 | 856 | if profileIndex not in profileList: |
|
857 | 857 | return False |
|
858 | 858 | |
|
859 | 859 | return True |
|
860 | 860 | |
|
861 | 861 | def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False, rangeList = None, nProfiles=None): |
|
862 | 862 | |
|
863 | 863 | """ |
|
864 | 864 | ProfileSelector: |
|
865 | 865 | |
|
866 | 866 | Inputs: |
|
867 | 867 | profileList : Index of profiles selected. Example: profileList = (0,1,2,7,8) |
|
868 | 868 | |
|
869 | 869 | profileRangeList : Minimum and maximum profile indexes. Example: profileRangeList = (4, 30) |
|
870 | 870 | |
|
871 | 871 | rangeList : List of profile ranges. Example: rangeList = ((4, 30), (32, 64), (128, 256)) |
|
872 | 872 | |
|
873 | 873 | """ |
|
874 | 874 | |
|
875 | 875 | if rangeList is not None: |
|
876 | 876 | if type(rangeList[0]) not in (tuple, list): |
|
877 | 877 | rangeList = [rangeList] |
|
878 | 878 | |
|
879 | 879 | dataOut.flagNoData = True |
|
880 | 880 | |
|
881 | 881 | if dataOut.flagDataAsBlock: |
|
882 | 882 | """ |
|
883 | 883 | data dimension = [nChannels, nProfiles, nHeis] |
|
884 | 884 | """ |
|
885 | 885 | if profileList != None: |
|
886 | 886 | dataOut.data = dataOut.data[:,profileList,:] |
|
887 | 887 | |
|
888 | 888 | if profileRangeList != None: |
|
889 | 889 | minIndex = profileRangeList[0] |
|
890 | 890 | maxIndex = profileRangeList[1] |
|
891 | 891 | profileList = list(range(minIndex, maxIndex+1)) |
|
892 | 892 | |
|
893 | 893 | dataOut.data = dataOut.data[:,minIndex:maxIndex+1,:] |
|
894 | 894 | |
|
895 | 895 | if rangeList != None: |
|
896 | 896 | |
|
897 | 897 | profileList = [] |
|
898 | 898 | |
|
899 | 899 | for thisRange in rangeList: |
|
900 | 900 | minIndex = thisRange[0] |
|
901 | 901 | maxIndex = thisRange[1] |
|
902 | 902 | |
|
903 | 903 | profileList.extend(list(range(minIndex, maxIndex+1))) |
|
904 | 904 | |
|
905 | 905 | dataOut.data = dataOut.data[:,profileList,:] |
|
906 | 906 | |
|
907 | 907 | dataOut.nProfiles = len(profileList) |
|
908 | 908 | dataOut.profileIndex = dataOut.nProfiles - 1 |
|
909 | 909 | dataOut.flagNoData = False |
|
910 | 910 | |
|
911 | 911 | return True |
|
912 | 912 | |
|
913 | 913 | """ |
|
914 | 914 | data dimension = [nChannels, nHeis] |
|
915 | 915 | """ |
|
916 | 916 | |
|
917 | 917 | if profileList != None: |
|
918 | 918 | |
|
919 | 919 | if self.isThisProfileInList(dataOut.profileIndex, profileList): |
|
920 | 920 | |
|
921 | 921 | self.nProfiles = len(profileList) |
|
922 | 922 | dataOut.nProfiles = self.nProfiles |
|
923 | 923 | dataOut.profileIndex = self.profileIndex |
|
924 | 924 | dataOut.flagNoData = False |
|
925 | 925 | |
|
926 | 926 | self.incProfileIndex() |
|
927 | 927 | return True |
|
928 | 928 | |
|
929 | 929 | if profileRangeList != None: |
|
930 | 930 | |
|
931 | 931 | minIndex = profileRangeList[0] |
|
932 | 932 | maxIndex = profileRangeList[1] |
|
933 | 933 | |
|
934 | 934 | if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex): |
|
935 | 935 | |
|
936 | 936 | self.nProfiles = maxIndex - minIndex + 1 |
|
937 | 937 | dataOut.nProfiles = self.nProfiles |
|
938 | 938 | dataOut.profileIndex = self.profileIndex |
|
939 | 939 | dataOut.flagNoData = False |
|
940 | 940 | |
|
941 | 941 | self.incProfileIndex() |
|
942 | 942 | return True |
|
943 | 943 | |
|
944 | 944 | if rangeList != None: |
|
945 | 945 | |
|
946 | 946 | nProfiles = 0 |
|
947 | 947 | |
|
948 | 948 | for thisRange in rangeList: |
|
949 | 949 | minIndex = thisRange[0] |
|
950 | 950 | maxIndex = thisRange[1] |
|
951 | 951 | |
|
952 | 952 | nProfiles += maxIndex - minIndex + 1 |
|
953 | 953 | |
|
954 | 954 | for thisRange in rangeList: |
|
955 | 955 | |
|
956 | 956 | minIndex = thisRange[0] |
|
957 | 957 | maxIndex = thisRange[1] |
|
958 | 958 | |
|
959 | 959 | if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex): |
|
960 | 960 | |
|
961 | 961 | self.nProfiles = nProfiles |
|
962 | 962 | dataOut.nProfiles = self.nProfiles |
|
963 | 963 | dataOut.profileIndex = self.profileIndex |
|
964 | 964 | dataOut.flagNoData = False |
|
965 | 965 | |
|
966 | 966 | self.incProfileIndex() |
|
967 | 967 | |
|
968 | 968 | break |
|
969 | 969 | |
|
970 | 970 | return True |
|
971 | 971 | |
|
972 | 972 | |
|
973 | 973 | if beam != None: #beam is only for AMISR data |
|
974 | 974 | if self.isThisProfileInList(dataOut.profileIndex, dataOut.beamRangeDict[beam]): |
|
975 | 975 | dataOut.flagNoData = False |
|
976 | 976 | dataOut.profileIndex = self.profileIndex |
|
977 | 977 | |
|
978 | 978 | self.incProfileIndex() |
|
979 | 979 | |
|
980 | 980 | return True |
|
981 | 981 | |
|
982 | 982 | raise ValueError("ProfileSelector needs profileList, profileRangeList or rangeList parameter") |
|
983 | 983 | |
|
984 | 984 | #return False |
|
985 | 985 | return dataOut |
|
986 | 986 | |
|
987 | 987 | class Reshaper(Operation): |
|
988 | 988 | |
|
989 | 989 | def __init__(self):#, **kwargs): |
|
990 | 990 | |
|
991 | 991 | Operation.__init__(self)#, **kwargs) |
|
992 | 992 | |
|
993 | 993 | self.__buffer = None |
|
994 | 994 | self.__nitems = 0 |
|
995 | 995 | |
|
996 | 996 | def __appendProfile(self, dataOut, nTxs): |
|
997 | 997 | |
|
998 | 998 | if self.__buffer is None: |
|
999 | 999 | shape = (dataOut.nChannels, int(dataOut.nHeights/nTxs) ) |
|
1000 | 1000 | self.__buffer = numpy.empty(shape, dtype = dataOut.data.dtype) |
|
1001 | 1001 | |
|
1002 | 1002 | ini = dataOut.nHeights * self.__nitems |
|
1003 | 1003 | end = ini + dataOut.nHeights |
|
1004 | 1004 | |
|
1005 | 1005 | self.__buffer[:, ini:end] = dataOut.data |
|
1006 | 1006 | |
|
1007 | 1007 | self.__nitems += 1 |
|
1008 | 1008 | |
|
1009 | 1009 | return int(self.__nitems*nTxs) |
|
1010 | 1010 | |
|
1011 | 1011 | def __getBuffer(self): |
|
1012 | 1012 | |
|
1013 | 1013 | if self.__nitems == int(1./self.__nTxs): |
|
1014 | 1014 | |
|
1015 | 1015 | self.__nitems = 0 |
|
1016 | 1016 | |
|
1017 | 1017 | return self.__buffer.copy() |
|
1018 | 1018 | |
|
1019 | 1019 | return None |
|
1020 | 1020 | |
|
1021 | 1021 | def __checkInputs(self, dataOut, shape, nTxs): |
|
1022 | 1022 | |
|
1023 | 1023 | if shape is None and nTxs is None: |
|
1024 | 1024 | raise ValueError("Reshaper: shape of factor should be defined") |
|
1025 | 1025 | |
|
1026 | 1026 | if nTxs: |
|
1027 | 1027 | if nTxs < 0: |
|
1028 | 1028 | raise ValueError("nTxs should be greater than 0") |
|
1029 | 1029 | |
|
1030 | 1030 | if nTxs < 1 and dataOut.nProfiles % (1./nTxs) != 0: |
|
1031 | 1031 | raise ValueError("nProfiles= %d is not divisibled by (1./nTxs) = %f" %(dataOut.nProfiles, (1./nTxs))) |
|
1032 | 1032 | |
|
1033 | 1033 | shape = [dataOut.nChannels, dataOut.nProfiles*nTxs, dataOut.nHeights/nTxs] |
|
1034 | 1034 | |
|
1035 | 1035 | return shape, nTxs |
|
1036 | 1036 | |
|
1037 | 1037 | if len(shape) != 2 and len(shape) != 3: |
|
1038 | 1038 | raise ValueError("shape dimension should be equal to 2 or 3. shape = (nProfiles, nHeis) or (nChannels, nProfiles, nHeis). Actually shape = (%d, %d, %d)" %(dataOut.nChannels, dataOut.nProfiles, dataOut.nHeights)) |
|
1039 | 1039 | |
|
1040 | 1040 | if len(shape) == 2: |
|
1041 | 1041 | shape_tuple = [dataOut.nChannels] |
|
1042 | 1042 | shape_tuple.extend(shape) |
|
1043 | 1043 | else: |
|
1044 | 1044 | shape_tuple = list(shape) |
|
1045 | 1045 | |
|
1046 | 1046 | nTxs = 1.0*shape_tuple[1]/dataOut.nProfiles |
|
1047 | 1047 | |
|
1048 | 1048 | return shape_tuple, nTxs |
|
1049 | 1049 | |
|
1050 | 1050 | def run(self, dataOut, shape=None, nTxs=None): |
|
1051 | 1051 | |
|
1052 | 1052 | shape_tuple, self.__nTxs = self.__checkInputs(dataOut, shape, nTxs) |
|
1053 | 1053 | |
|
1054 | 1054 | dataOut.flagNoData = True |
|
1055 | 1055 | profileIndex = None |
|
1056 | 1056 | |
|
1057 | 1057 | if dataOut.flagDataAsBlock: |
|
1058 | 1058 | |
|
1059 | 1059 | dataOut.data = numpy.reshape(dataOut.data, shape_tuple) |
|
1060 | 1060 | dataOut.flagNoData = False |
|
1061 | 1061 | |
|
1062 | 1062 | profileIndex = int(dataOut.nProfiles*self.__nTxs) - 1 |
|
1063 | 1063 | |
|
1064 | 1064 | else: |
|
1065 | 1065 | |
|
1066 | 1066 | if self.__nTxs < 1: |
|
1067 | 1067 | |
|
1068 | 1068 | self.__appendProfile(dataOut, self.__nTxs) |
|
1069 | 1069 | new_data = self.__getBuffer() |
|
1070 | 1070 | |
|
1071 | 1071 | if new_data is not None: |
|
1072 | 1072 | dataOut.data = new_data |
|
1073 | 1073 | dataOut.flagNoData = False |
|
1074 | 1074 | |
|
1075 | 1075 | profileIndex = dataOut.profileIndex*nTxs |
|
1076 | 1076 | |
|
1077 | 1077 | else: |
|
1078 | 1078 | raise ValueError("nTxs should be greater than 0 and lower than 1, or use VoltageReader(..., getblock=True)") |
|
1079 | 1079 | |
|
1080 | 1080 | deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] |
|
1081 | 1081 | |
|
1082 | 1082 | dataOut.heightList = numpy.arange(dataOut.nHeights/self.__nTxs) * deltaHeight + dataOut.heightList[0] |
|
1083 | 1083 | |
|
1084 | 1084 | dataOut.nProfiles = int(dataOut.nProfiles*self.__nTxs) |
|
1085 | 1085 | |
|
1086 | 1086 | dataOut.profileIndex = profileIndex |
|
1087 | 1087 | |
|
1088 | 1088 | dataOut.ippSeconds /= self.__nTxs |
|
1089 | 1089 | |
|
1090 | 1090 | return dataOut |
|
1091 | 1091 | |
|
1092 | 1092 | class SplitProfiles(Operation): |
|
1093 | 1093 | |
|
1094 | 1094 | def __init__(self):#, **kwargs): |
|
1095 | 1095 | |
|
1096 | 1096 | Operation.__init__(self)#, **kwargs) |
|
1097 | 1097 | |
|
1098 | 1098 | def run(self, dataOut, n): |
|
1099 | 1099 | |
|
1100 | 1100 | dataOut.flagNoData = True |
|
1101 | 1101 | profileIndex = None |
|
1102 | 1102 | |
|
1103 | 1103 | if dataOut.flagDataAsBlock: |
|
1104 | 1104 | |
|
1105 | 1105 | #nchannels, nprofiles, nsamples |
|
1106 | 1106 | shape = dataOut.data.shape |
|
1107 | 1107 | |
|
1108 | 1108 | if shape[2] % n != 0: |
|
1109 | 1109 | raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[2])) |
|
1110 | 1110 | |
|
1111 | 1111 | new_shape = shape[0], shape[1]*n, int(shape[2]/n) |
|
1112 | 1112 | |
|
1113 | 1113 | dataOut.data = numpy.reshape(dataOut.data, new_shape) |
|
1114 | 1114 | dataOut.flagNoData = False |
|
1115 | 1115 | |
|
1116 | 1116 | profileIndex = int(dataOut.nProfiles/n) - 1 |
|
1117 | 1117 | |
|
1118 | 1118 | else: |
|
1119 | 1119 | |
|
1120 | 1120 | raise ValueError("Could not split the data when is read Profile by Profile. Use VoltageReader(..., getblock=True)") |
|
1121 | 1121 | |
|
1122 | 1122 | deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] |
|
1123 | 1123 | |
|
1124 | 1124 | dataOut.heightList = numpy.arange(dataOut.nHeights/n) * deltaHeight + dataOut.heightList[0] |
|
1125 | 1125 | |
|
1126 | 1126 | dataOut.nProfiles = int(dataOut.nProfiles*n) |
|
1127 | 1127 | |
|
1128 | 1128 | dataOut.profileIndex = profileIndex |
|
1129 | 1129 | |
|
1130 | 1130 | dataOut.ippSeconds /= n |
|
1131 | 1131 | |
|
1132 | 1132 | return dataOut |
|
1133 | 1133 | |
|
1134 | 1134 | class CombineProfiles(Operation): |
|
1135 | 1135 | def __init__(self):#, **kwargs): |
|
1136 | 1136 | |
|
1137 | 1137 | Operation.__init__(self)#, **kwargs) |
|
1138 | 1138 | |
|
1139 | 1139 | self.__remData = None |
|
1140 | 1140 | self.__profileIndex = 0 |
|
1141 | 1141 | |
|
1142 | 1142 | def run(self, dataOut, n): |
|
1143 | 1143 | |
|
1144 | 1144 | dataOut.flagNoData = True |
|
1145 | 1145 | profileIndex = None |
|
1146 | 1146 | |
|
1147 | 1147 | if dataOut.flagDataAsBlock: |
|
1148 | 1148 | |
|
1149 | 1149 | #nchannels, nprofiles, nsamples |
|
1150 | 1150 | shape = dataOut.data.shape |
|
1151 | 1151 | new_shape = shape[0], shape[1]/n, shape[2]*n |
|
1152 | 1152 | |
|
1153 | 1153 | if shape[1] % n != 0: |
|
1154 | 1154 | raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[1])) |
|
1155 | 1155 | |
|
1156 | 1156 | dataOut.data = numpy.reshape(dataOut.data, new_shape) |
|
1157 | 1157 | dataOut.flagNoData = False |
|
1158 | 1158 | |
|
1159 | 1159 | profileIndex = int(dataOut.nProfiles*n) - 1 |
|
1160 | 1160 | |
|
1161 | 1161 | else: |
|
1162 | 1162 | |
|
1163 | 1163 | #nchannels, nsamples |
|
1164 | 1164 | if self.__remData is None: |
|
1165 | 1165 | newData = dataOut.data |
|
1166 | 1166 | else: |
|
1167 | 1167 | newData = numpy.concatenate((self.__remData, dataOut.data), axis=1) |
|
1168 | 1168 | |
|
1169 | 1169 | self.__profileIndex += 1 |
|
1170 | 1170 | |
|
1171 | 1171 | if self.__profileIndex < n: |
|
1172 | 1172 | self.__remData = newData |
|
1173 | 1173 | #continue |
|
1174 | 1174 | return |
|
1175 | 1175 | |
|
1176 | 1176 | self.__profileIndex = 0 |
|
1177 | 1177 | self.__remData = None |
|
1178 | 1178 | |
|
1179 | 1179 | dataOut.data = newData |
|
1180 | 1180 | dataOut.flagNoData = False |
|
1181 | 1181 | |
|
1182 | 1182 | profileIndex = dataOut.profileIndex/n |
|
1183 | 1183 | |
|
1184 | 1184 | |
|
1185 | 1185 | deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] |
|
1186 | 1186 | |
|
1187 | 1187 | dataOut.heightList = numpy.arange(dataOut.nHeights*n) * deltaHeight + dataOut.heightList[0] |
|
1188 | 1188 | |
|
1189 | 1189 | dataOut.nProfiles = int(dataOut.nProfiles/n) |
|
1190 | 1190 | |
|
1191 | 1191 | dataOut.profileIndex = profileIndex |
|
1192 | 1192 | |
|
1193 | 1193 | dataOut.ippSeconds *= n |
|
1194 | 1194 | |
|
1195 | 1195 | return dataOut |
|
1196 | 1196 | # import collections |
|
1197 | 1197 | # from scipy.stats import mode |
|
1198 | 1198 | # |
|
1199 | 1199 | # class Synchronize(Operation): |
|
1200 | 1200 | # |
|
1201 | 1201 | # isConfig = False |
|
1202 | 1202 | # __profIndex = 0 |
|
1203 | 1203 | # |
|
1204 | 1204 | # def __init__(self, **kwargs): |
|
1205 | 1205 | # |
|
1206 | 1206 | # Operation.__init__(self, **kwargs) |
|
1207 | 1207 | # # self.isConfig = False |
|
1208 | 1208 | # self.__powBuffer = None |
|
1209 | 1209 | # self.__startIndex = 0 |
|
1210 | 1210 | # self.__pulseFound = False |
|
1211 | 1211 | # |
|
1212 | 1212 | # def __findTxPulse(self, dataOut, channel=0, pulse_with = None): |
|
1213 | 1213 | # |
|
1214 | 1214 | # #Read data |
|
1215 | 1215 | # |
|
1216 | 1216 | # powerdB = dataOut.getPower(channel = channel) |
|
1217 | 1217 | # noisedB = dataOut.getNoise(channel = channel)[0] |
|
1218 | 1218 | # |
|
1219 | 1219 | # self.__powBuffer.extend(powerdB.flatten()) |
|
1220 | 1220 | # |
|
1221 | 1221 | # dataArray = numpy.array(self.__powBuffer) |
|
1222 | 1222 | # |
|
1223 | 1223 | # filteredPower = numpy.correlate(dataArray, dataArray[0:self.__nSamples], "same") |
|
1224 | 1224 | # |
|
1225 | 1225 | # maxValue = numpy.nanmax(filteredPower) |
|
1226 | 1226 | # |
|
1227 | 1227 | # if maxValue < noisedB + 10: |
|
1228 | 1228 | # #No se encuentra ningun pulso de transmision |
|
1229 | 1229 | # return None |
|
1230 | 1230 | # |
|
1231 | 1231 | # maxValuesIndex = numpy.where(filteredPower > maxValue - 0.1*abs(maxValue))[0] |
|
1232 | 1232 | # |
|
1233 | 1233 | # if len(maxValuesIndex) < 2: |
|
1234 | 1234 | # #Solo se encontro un solo pulso de transmision de un baudio, esperando por el siguiente TX |
|
1235 | 1235 | # return None |
|
1236 | 1236 | # |
|
1237 | 1237 | # phasedMaxValuesIndex = maxValuesIndex - self.__nSamples |
|
1238 | 1238 | # |
|
1239 | 1239 | # #Seleccionar solo valores con un espaciamiento de nSamples |
|
1240 | 1240 | # pulseIndex = numpy.intersect1d(maxValuesIndex, phasedMaxValuesIndex) |
|
1241 | 1241 | # |
|
1242 | 1242 | # if len(pulseIndex) < 2: |
|
1243 | 1243 | # #Solo se encontro un pulso de transmision con ancho mayor a 1 |
|
1244 | 1244 | # return None |
|
1245 | 1245 | # |
|
1246 | 1246 | # spacing = pulseIndex[1:] - pulseIndex[:-1] |
|
1247 | 1247 | # |
|
1248 | 1248 | # #remover senales que se distancien menos de 10 unidades o muestras |
|
1249 | 1249 | # #(No deberian existir IPP menor a 10 unidades) |
|
1250 | 1250 | # |
|
1251 | 1251 | # realIndex = numpy.where(spacing > 10 )[0] |
|
1252 | 1252 | # |
|
1253 | 1253 | # if len(realIndex) < 2: |
|
1254 | 1254 | # #Solo se encontro un pulso de transmision con ancho mayor a 1 |
|
1255 | 1255 | # return None |
|
1256 | 1256 | # |
|
1257 | 1257 | # #Eliminar pulsos anchos (deja solo la diferencia entre IPPs) |
|
1258 | 1258 | # realPulseIndex = pulseIndex[realIndex] |
|
1259 | 1259 | # |
|
1260 | 1260 | # period = mode(realPulseIndex[1:] - realPulseIndex[:-1])[0][0] |
|
1261 | 1261 | # |
|
1262 | 1262 | # print "IPP = %d samples" %period |
|
1263 | 1263 | # |
|
1264 | 1264 | # self.__newNSamples = dataOut.nHeights #int(period) |
|
1265 | 1265 | # self.__startIndex = int(realPulseIndex[0]) |
|
1266 | 1266 | # |
|
1267 | 1267 | # return 1 |
|
1268 | 1268 | # |
|
1269 | 1269 | # |
|
1270 | 1270 | # def setup(self, nSamples, nChannels, buffer_size = 4): |
|
1271 | 1271 | # |
|
1272 | 1272 | # self.__powBuffer = collections.deque(numpy.zeros( buffer_size*nSamples,dtype=numpy.float), |
|
1273 | 1273 | # maxlen = buffer_size*nSamples) |
|
1274 | 1274 | # |
|
1275 | 1275 | # bufferList = [] |
|
1276 | 1276 | # |
|
1277 | 1277 | # for i in range(nChannels): |
|
1278 | 1278 | # bufferByChannel = collections.deque(numpy.zeros( buffer_size*nSamples, dtype=numpy.complex) + numpy.NAN, |
|
1279 | 1279 | # maxlen = buffer_size*nSamples) |
|
1280 | 1280 | # |
|
1281 | 1281 | # bufferList.append(bufferByChannel) |
|
1282 | 1282 | # |
|
1283 | 1283 | # self.__nSamples = nSamples |
|
1284 | 1284 | # self.__nChannels = nChannels |
|
1285 | 1285 | # self.__bufferList = bufferList |
|
1286 | 1286 | # |
|
1287 | 1287 | # def run(self, dataOut, channel = 0): |
|
1288 | 1288 | # |
|
1289 | 1289 | # if not self.isConfig: |
|
1290 | 1290 | # nSamples = dataOut.nHeights |
|
1291 | 1291 | # nChannels = dataOut.nChannels |
|
1292 | 1292 | # self.setup(nSamples, nChannels) |
|
1293 | 1293 | # self.isConfig = True |
|
1294 | 1294 | # |
|
1295 | 1295 | # #Append new data to internal buffer |
|
1296 | 1296 | # for thisChannel in range(self.__nChannels): |
|
1297 | 1297 | # bufferByChannel = self.__bufferList[thisChannel] |
|
1298 | 1298 | # bufferByChannel.extend(dataOut.data[thisChannel]) |
|
1299 | 1299 | # |
|
1300 | 1300 | # if self.__pulseFound: |
|
1301 | 1301 | # self.__startIndex -= self.__nSamples |
|
1302 | 1302 | # |
|
1303 | 1303 | # #Finding Tx Pulse |
|
1304 | 1304 | # if not self.__pulseFound: |
|
1305 | 1305 | # indexFound = self.__findTxPulse(dataOut, channel) |
|
1306 | 1306 | # |
|
1307 | 1307 | # if indexFound == None: |
|
1308 | 1308 | # dataOut.flagNoData = True |
|
1309 | 1309 | # return |
|
1310 | 1310 | # |
|
1311 | 1311 | # self.__arrayBuffer = numpy.zeros((self.__nChannels, self.__newNSamples), dtype = numpy.complex) |
|
1312 | 1312 | # self.__pulseFound = True |
|
1313 | 1313 | # self.__startIndex = indexFound |
|
1314 | 1314 | # |
|
1315 | 1315 | # #If pulse was found ... |
|
1316 | 1316 | # for thisChannel in range(self.__nChannels): |
|
1317 | 1317 | # bufferByChannel = self.__bufferList[thisChannel] |
|
1318 | 1318 | # #print self.__startIndex |
|
1319 | 1319 | # x = numpy.array(bufferByChannel) |
|
1320 | 1320 | # self.__arrayBuffer[thisChannel] = x[self.__startIndex:self.__startIndex+self.__newNSamples] |
|
1321 | 1321 | # |
|
1322 | 1322 | # deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] |
|
1323 | 1323 | # dataOut.heightList = numpy.arange(self.__newNSamples)*deltaHeight |
|
1324 | 1324 | # # dataOut.ippSeconds = (self.__newNSamples / deltaHeight)/1e6 |
|
1325 | 1325 | # |
|
1326 | 1326 | # dataOut.data = self.__arrayBuffer |
|
1327 | 1327 | # |
|
1328 | 1328 | # self.__startIndex += self.__newNSamples |
|
1329 | 1329 | # |
|
1330 | 1330 | # return |
General Comments 0
You need to be logged in to leave comments.
Login now