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