##// END OF EJS Templates
para pruebas em PC AMISR
joabAM -
r1541:77e2d8d2b2ef
parent child
Show More
@@ -1,666 +1,667
1 ''''
1 ''''
2 Created on Set 9, 2015
2 Created on Set 9, 2015
3
3
4 @author: roj-idl71 Karim Kuyeng
4 @author: roj-idl71 Karim Kuyeng
5
5
6 @update: 2021, Joab Apaza
6 @update: 2021, Joab Apaza
7 '''
7 '''
8
8
9 import os
9 import os
10 import sys
10 import sys
11 import glob
11 import glob
12 import fnmatch
12 import fnmatch
13 import datetime
13 import datetime
14 import time
14 import time
15 import re
15 import re
16 import h5py
16 import h5py
17 import numpy
17 import numpy
18
18
19 try:
19 try:
20 from gevent import sleep
20 from gevent import sleep
21 except:
21 except:
22 from time import sleep
22 from time import sleep
23
23
24 from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader
24 from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader
25 from schainpy.model.data.jrodata import Voltage
25 from schainpy.model.data.jrodata import Voltage
26 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
26 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
27 from numpy import imag
27 from numpy import imag
28 from schainpy.utils import log
28 from schainpy.utils import log
29
29
30
30
31 class AMISRReader(ProcessingUnit):
31 class AMISRReader(ProcessingUnit):
32 '''
32 '''
33 classdocs
33 classdocs
34 '''
34 '''
35
35
36 def __init__(self):
36 def __init__(self):
37 '''
37 '''
38 Constructor
38 Constructor
39 '''
39 '''
40
40
41 ProcessingUnit.__init__(self)
41 ProcessingUnit.__init__(self)
42
42
43 self.set = None
43 self.set = None
44 self.subset = None
44 self.subset = None
45 self.extension_file = '.h5'
45 self.extension_file = '.h5'
46 self.dtc_str = 'dtc'
46 self.dtc_str = 'dtc'
47 self.dtc_id = 0
47 self.dtc_id = 0
48 self.status = True
48 self.status = True
49 self.isConfig = False
49 self.isConfig = False
50 self.dirnameList = []
50 self.dirnameList = []
51 self.filenameList = []
51 self.filenameList = []
52 self.fileIndex = None
52 self.fileIndex = None
53 self.flagNoMoreFiles = False
53 self.flagNoMoreFiles = False
54 self.flagIsNewFile = 0
54 self.flagIsNewFile = 0
55 self.filename = ''
55 self.filename = ''
56 self.amisrFilePointer = None
56 self.amisrFilePointer = None
57 self.realBeamCode = []
57 self.realBeamCode = []
58 self.beamCodeMap = None
58 self.beamCodeMap = None
59 self.azimuthList = []
59 self.azimuthList = []
60 self.elevationList = []
60 self.elevationList = []
61 self.dataShape = None
61 self.dataShape = None
62 self.flag_old_beams = False
62 self.flag_old_beams = False
63
63
64
64
65 self.profileIndex = 0
65 self.profileIndex = 0
66
66
67
67
68 self.beamCodeByFrame = None
68 self.beamCodeByFrame = None
69 self.radacTimeByFrame = None
69 self.radacTimeByFrame = None
70
70
71 self.dataset = None
71 self.dataset = None
72
72
73 self.__firstFile = True
73 self.__firstFile = True
74
74
75 self.buffer = None
75 self.buffer = None
76
76
77 self.timezone = 'ut'
77 self.timezone = 'ut'
78
78
79 self.__waitForNewFile = 20
79 self.__waitForNewFile = 20
80 self.__filename_online = None
80 self.__filename_online = None
81 #Is really necessary create the output object in the initializer
81 #Is really necessary create the output object in the initializer
82 self.dataOut = Voltage()
82 self.dataOut = Voltage()
83 self.dataOut.error=False
83 self.dataOut.error=False
84 self.margin_days = 1
84 self.margin_days = 1
85
85
86 def setup(self,path=None,
86 def setup(self,path=None,
87 startDate=None,
87 startDate=None,
88 endDate=None,
88 endDate=None,
89 startTime=None,
89 startTime=None,
90 endTime=None,
90 endTime=None,
91 walk=True,
91 walk=True,
92 timezone='ut',
92 timezone='ut',
93 all=0,
93 all=0,
94 code = None,
94 code = None,
95 nCode = 0,
95 nCode = 0,
96 nBaud = 0,
96 nBaud = 0,
97 online=False,
97 online=False,
98 old_beams=False,
98 old_beams=False,
99 margin_days=1):
99 margin_days=1):
100
100
101
101
102
102
103 self.timezone = timezone
103 self.timezone = timezone
104 self.all = all
104 self.all = all
105 self.online = online
105 self.online = online
106 self.flag_old_beams = old_beams
106 self.flag_old_beams = old_beams
107 self.code = code
107 self.code = code
108 self.nCode = int(nCode)
108 self.nCode = int(nCode)
109 self.nBaud = int(nBaud)
109 self.nBaud = int(nBaud)
110 self.margin_days = margin_days
110 self.margin_days = margin_days
111
111
112
112
113 #self.findFiles()
113 #self.findFiles()
114 if not(online):
114 if not(online):
115 #Busqueda de archivos offline
115 #Busqueda de archivos offline
116 self.searchFilesOffLine(path, startDate, endDate, startTime, endTime, walk)
116 self.searchFilesOffLine(path, startDate, endDate, startTime, endTime, walk)
117 else:
117 else:
118 self.searchFilesOnLine(path, startDate, endDate, startTime,endTime,walk)
118 self.searchFilesOnLine(path, startDate, endDate, startTime,endTime,walk)
119
119
120 if not(self.filenameList):
120 if not(self.filenameList):
121 raise schainpy.admin.SchainWarning("There is no files into the folder: %s"%(path))
121 raise schainpy.admin.SchainWarning("There is no files into the folder: %s"%(path))
122 sys.exit()
122 sys.exit()
123
123
124 self.fileIndex = 0
124 self.fileIndex = 0
125
125
126 self.readNextFile(online)
126 self.readNextFile(online)
127
127
128 '''
128 '''
129 Add code
129 Add code
130 '''
130 '''
131 self.isConfig = True
131 self.isConfig = True
132 # print("Setup Done")
132 # print("Setup Done")
133 pass
133 pass
134
134
135
135
136 def readAMISRHeader(self,fp):
136 def readAMISRHeader(self,fp):
137
137
138 if self.isConfig and (not self.flagNoMoreFiles):
138 if self.isConfig and (not self.flagNoMoreFiles):
139 newShape = fp.get('Raw11/Data/Samples/Data').shape[1:]
139 newShape = fp.get('Raw11/Data/Samples/Data').shape[1:]
140 if self.dataShape != newShape and newShape != None:
140 if self.dataShape != newShape and newShape != None:
141 raise schainpy.admin.SchainError("NEW FILE HAS A DIFFERENT SHAPE: ")
141 raise schainpy.admin.SchainError("NEW FILE HAS A DIFFERENT SHAPE: ")
142 print(self.dataShape,newShape,"\n")
142 print(self.dataShape,newShape,"\n")
143 return 0
143 return 0
144 else:
144 else:
145 self.dataShape = fp.get('Raw11/Data/Samples/Data').shape[1:]
145 self.dataShape = fp.get('Raw11/Data/Samples/Data').shape[1:]
146
146
147
147
148 header = 'Raw11/Data/RadacHeader'
148 header = 'Raw11/Data/RadacHeader'
149 self.beamCodeByPulse = fp.get(header+'/BeamCode') # LIST OF BEAMS PER PROFILE, TO BE USED ON REARRANGE
149 self.beamCodeByPulse = fp.get(header+'/BeamCode') # LIST OF BEAMS PER PROFILE, TO BE USED ON REARRANGE
150 if (self.startDate> datetime.date(2021, 7, 15)) or self.flag_old_beams: #Se cambiΓ³ la forma de extracciΓ³n de Apuntes el 17 o forzar con flag de reorganizaciΓ³n
150 if (self.startDate> datetime.date(2021, 7, 15)) or self.flag_old_beams: #Se cambiΓ³ la forma de extracciΓ³n de Apuntes el 17 o forzar con flag de reorganizaciΓ³n
151 self.beamcodeFile = fp['Setup/Beamcodefile'][()].decode()
151 self.beamcodeFile = fp['Setup/Beamcodefile'][()].decode()
152 self.trueBeams = self.beamcodeFile.split("\n")
152 self.trueBeams = self.beamcodeFile.split("\n")
153 self.trueBeams.pop()#remove last
153 self.trueBeams.pop()#remove last
154 [self.realBeamCode.append(x) for x in self.trueBeams if x not in self.realBeamCode]
154 [self.realBeamCode.append(x) for x in self.trueBeams if x not in self.realBeamCode]
155 self.beamCode = [int(x, 16) for x in self.realBeamCode]
155 self.beamCode = [int(x, 16) for x in self.realBeamCode]
156 else:
156 else:
157 _beamCode= fp.get('Raw11/Data/Beamcodes') #se usa la manera previa al cambio de apuntes
157 _beamCode= fp.get('Raw11/Data/Beamcodes') #se usa la manera previa al cambio de apuntes
158 self.beamCode = _beamCode[0,:]
158 self.beamCode = _beamCode[0,:]
159
159
160 if self.beamCodeMap == None:
160 if self.beamCodeMap == None:
161 self.beamCodeMap = fp['Setup/BeamcodeMap']
161 self.beamCodeMap = fp['Setup/BeamcodeMap']
162 for beam in self.beamCode:
162 for beam in self.beamCode:
163 beamAziElev = numpy.where(self.beamCodeMap[:,0]==beam)
163 beamAziElev = numpy.where(self.beamCodeMap[:,0]==beam)
164 beamAziElev = beamAziElev[0].squeeze()
164 beamAziElev = beamAziElev[0].squeeze()
165 self.azimuthList.append(self.beamCodeMap[beamAziElev,1])
165 self.azimuthList.append(self.beamCodeMap[beamAziElev,1])
166 self.elevationList.append(self.beamCodeMap[beamAziElev,2])
166 self.elevationList.append(self.beamCodeMap[beamAziElev,2])
167 #print("Beamssss: ",self.beamCodeMap[beamAziElev,1],self.beamCodeMap[beamAziElev,2])
167 #print("Beamssss: ",self.beamCodeMap[beamAziElev,1],self.beamCodeMap[beamAziElev,2])
168 #print(self.beamCode)
168 #print(self.beamCode)
169 #self.code = fp.get(header+'/Code') # NOT USE FOR THIS
169 #self.code = fp.get(header+'/Code') # NOT USE FOR THIS
170 self.frameCount = fp.get(header+'/FrameCount')# NOT USE FOR THIS
170 self.frameCount = fp.get(header+'/FrameCount')# NOT USE FOR THIS
171 self.modeGroup = fp.get(header+'/ModeGroup')# NOT USE FOR THIS
171 self.modeGroup = fp.get(header+'/ModeGroup')# NOT USE FOR THIS
172 self.nsamplesPulse = fp.get(header+'/NSamplesPulse')# TO GET NSA OR USING DATA FOR THAT
172 self.nsamplesPulse = fp.get(header+'/NSamplesPulse')# TO GET NSA OR USING DATA FOR THAT
173 self.pulseCount = fp.get(header+'/PulseCount')# NOT USE FOR THIS
173 self.pulseCount = fp.get(header+'/PulseCount')# NOT USE FOR THIS
174 self.radacTime = fp.get(header+'/RadacTime')# 1st TIME ON FILE ANDE CALCULATE THE REST WITH IPP*nindexprofile
174 self.radacTime = fp.get(header+'/RadacTime')# 1st TIME ON FILE ANDE CALCULATE THE REST WITH IPP*nindexprofile
175 self.timeCount = fp.get(header+'/TimeCount')# NOT USE FOR THIS
175 self.timeCount = fp.get(header+'/TimeCount')# NOT USE FOR THIS
176 self.timeStatus = fp.get(header+'/TimeStatus')# NOT USE FOR THIS
176 self.timeStatus = fp.get(header+'/TimeStatus')# NOT USE FOR THIS
177 self.rangeFromFile = fp.get('Raw11/Data/Samples/Range')
177 self.rangeFromFile = fp.get('Raw11/Data/Samples/Range')
178 self.frequency = fp.get('Rx/Frequency')
178 self.frequency = fp.get('Rx/Frequency')
179 txAus = fp.get('Raw11/Data/Pulsewidth')
179 txAus = fp.get('Raw11/Data/Pulsewidth')
180
180
181
181
182 self.nblocks = self.pulseCount.shape[0] #nblocks
182 self.nblocks = self.pulseCount.shape[0] #nblocks
183
183
184 self.nprofiles = self.pulseCount.shape[1] #nprofile
184 self.nprofiles = self.pulseCount.shape[1] #nprofile
185 self.nsa = self.nsamplesPulse[0,0] #ngates
185 self.nsa = self.nsamplesPulse[0,0] #ngates
186 self.nchannels = len(self.beamCode)
186 self.nchannels = len(self.beamCode)
187 self.ippSeconds = (self.radacTime[0][1] -self.radacTime[0][0]) #Ipp in seconds
187 self.ippSeconds = (self.radacTime[0][1] -self.radacTime[0][0]) #Ipp in seconds
188 #print("IPPS secs: ",self.ippSeconds)
188 #print("IPPS secs: ",self.ippSeconds)
189 #self.__waitForNewFile = self.nblocks # wait depending on the number of blocks since each block is 1 sec
189 #self.__waitForNewFile = self.nblocks # wait depending on the number of blocks since each block is 1 sec
190 self.__waitForNewFile = self.nblocks * self.nprofiles * self.ippSeconds # wait until new file is created
190 self.__waitForNewFile = self.nblocks * self.nprofiles * self.ippSeconds # wait until new file is created
191
191
192 #filling radar controller header parameters
192 #filling radar controller header parameters
193 self.__ippKm = self.ippSeconds *.15*1e6 # in km
193 self.__ippKm = self.ippSeconds *.15*1e6 # in km
194 self.__txA = (txAus[()])*.15 #(ipp[us]*.15km/1us) in km
194 self.__txA = (txAus[()])*.15 #(ipp[us]*.15km/1us) in km
195 self.__txB = 0
195 self.__txB = 0
196 nWindows=1
196 nWindows=1
197 self.__nSamples = self.nsa
197 self.__nSamples = self.nsa
198 self.__firstHeight = self.rangeFromFile[0][0]/1000 #in km
198 self.__firstHeight = self.rangeFromFile[0][0]/1000 #in km
199 self.__deltaHeight = (self.rangeFromFile[0][1] - self.rangeFromFile[0][0])/1000
199 self.__deltaHeight = (self.rangeFromFile[0][1] - self.rangeFromFile[0][0])/1000
200 #print("amisr-ipp:",self.ippSeconds, self.__ippKm)
200 #print("amisr-ipp:",self.ippSeconds, self.__ippKm)
201 #for now until understand why the code saved is different (code included even though code not in tuf file)
201 #for now until understand why the code saved is different (code included even though code not in tuf file)
202 #self.__codeType = 0
202 #self.__codeType = 0
203 # self.__nCode = None
203 # self.__nCode = None
204 # self.__nBaud = None
204 # self.__nBaud = None
205 self.__code = self.code
205 self.__code = self.code
206 self.__codeType = 0
206 self.__codeType = 0
207 if self.code != None:
207 if self.code != None:
208 self.__codeType = 1
208 self.__codeType = 1
209 self.__nCode = self.nCode
209 self.__nCode = self.nCode
210 self.__nBaud = self.nBaud
210 self.__nBaud = self.nBaud
211 #self.__code = 0
211 #self.__code = 0
212
212
213 #filling system header parameters
213 #filling system header parameters
214 self.__nSamples = self.nsa
214 self.__nSamples = self.nsa
215 self.newProfiles = self.nprofiles/self.nchannels
215 self.newProfiles = self.nprofiles/self.nchannels
216 self.__channelList = list(range(self.nchannels))
216 self.__channelList = list(range(self.nchannels))
217
217
218 self.__frequency = self.frequency[0][0]
218 self.__frequency = self.frequency[0][0]
219
219
220
220
221 return 1
221 return 1
222
222
223
223
224 def createBuffers(self):
224 def createBuffers(self):
225
225
226 pass
226 pass
227
227
228 def __setParameters(self,path='', startDate='',endDate='',startTime='', endTime='', walk=''):
228 def __setParameters(self,path='', startDate='',endDate='',startTime='', endTime='', walk=''):
229 self.path = path
229 self.path = path
230 self.startDate = startDate
230 self.startDate = startDate
231 self.endDate = endDate
231 self.endDate = endDate
232 self.startTime = startTime
232 self.startTime = startTime
233 self.endTime = endTime
233 self.endTime = endTime
234 self.walk = walk
234 self.walk = walk
235
235
236 def __checkPath(self):
236 def __checkPath(self):
237 if os.path.exists(self.path):
237 if os.path.exists(self.path):
238 self.status = 1
238 self.status = 1
239 else:
239 else:
240 self.status = 0
240 self.status = 0
241 print('Path:%s does not exists'%self.path)
241 print('Path:%s does not exists'%self.path)
242
242
243 return
243 return
244
244
245
245
246 def __selDates(self, amisr_dirname_format):
246 def __selDates(self, amisr_dirname_format):
247 try:
247 try:
248 year = int(amisr_dirname_format[0:4])
248 year = int(amisr_dirname_format[0:4])
249 month = int(amisr_dirname_format[4:6])
249 month = int(amisr_dirname_format[4:6])
250 dom = int(amisr_dirname_format[6:8])
250 dom = int(amisr_dirname_format[6:8])
251 thisDate = datetime.date(year,month,dom)
251 thisDate = datetime.date(year,month,dom)
252 #margen de un dΓ­a extra, igual luego se filtra for fecha y hora
252 #margen de un dΓ­a extra, igual luego se filtra for fecha y hora
253 if (thisDate>=(self.startDate - datetime.timedelta(days=self.margin_days)) and thisDate <= (self.endDate)+ datetime.timedelta(days=1)):
253 if (thisDate>=(self.startDate - datetime.timedelta(days=self.margin_days)) and thisDate <= (self.endDate)+ datetime.timedelta(days=1)):
254 return amisr_dirname_format
254 return amisr_dirname_format
255 except:
255 except:
256 return None
256 return None
257
257
258
258
259 def __findDataForDates(self,online=False):
259 def __findDataForDates(self,online=False):
260
260
261 if not(self.status):
261 if not(self.status):
262 return None
262 return None
263
263
264 pat = '\d+.\d+'
264 pat = '\d+.\d+'
265 dirnameList = [re.search(pat,x) for x in os.listdir(self.path)]
265 dirnameList = [re.search(pat,x) for x in os.listdir(self.path)]
266 dirnameList = [x for x in dirnameList if x!=None]
266 dirnameList = [x for x in dirnameList if x!=None]
267 dirnameList = [x.string for x in dirnameList]
267 dirnameList = [x.string for x in dirnameList]
268 if not(online):
268 if not(online):
269 dirnameList = [self.__selDates(x) for x in dirnameList]
269 dirnameList = [self.__selDates(x) for x in dirnameList]
270 dirnameList = [x for x in dirnameList if x!=None]
270 dirnameList = [x for x in dirnameList if x!=None]
271 if len(dirnameList)>0:
271 if len(dirnameList)>0:
272 self.status = 1
272 self.status = 1
273 self.dirnameList = dirnameList
273 self.dirnameList = dirnameList
274 self.dirnameList.sort()
274 self.dirnameList.sort()
275 else:
275 else:
276 self.status = 0
276 self.status = 0
277 return None
277 return None
278
278
279 def __getTimeFromData(self):
279 def __getTimeFromData(self):
280 startDateTime_Reader = datetime.datetime.combine(self.startDate,self.startTime)
280 startDateTime_Reader = datetime.datetime.combine(self.startDate,self.startTime)
281 endDateTime_Reader = datetime.datetime.combine(self.endDate,self.endTime)
281 endDateTime_Reader = datetime.datetime.combine(self.endDate,self.endTime)
282
282
283 print('Filtering Files from %s to %s'%(startDateTime_Reader, endDateTime_Reader))
283 print('Filtering Files from %s to %s'%(startDateTime_Reader, endDateTime_Reader))
284 print('........................................')
284 print('........................................')
285 filter_filenameList = []
285 filter_filenameList = []
286 self.filenameList.sort()
286 self.filenameList.sort()
287 total_files = len(self.filenameList)
287 total_files = len(self.filenameList)
288 #for i in range(len(self.filenameList)-1):
288 #for i in range(len(self.filenameList)-1):
289 for i in range(total_files):
289 for i in range(total_files):
290 filename = self.filenameList[i]
290 filename = self.filenameList[i]
291 #print("file-> ",filename)
291 #print("file-> ",filename)
292 try:
292 try:
293 fp = h5py.File(filename,'r')
293 fp = h5py.File(filename,'r')
294 time_str = fp.get('Time/RadacTimeString')
294 time_str = fp.get('Time/RadacTimeString')
295
295
296 startDateTimeStr_File = time_str[0][0].decode('UTF-8').split('.')[0]
296 startDateTimeStr_File = time_str[0][0].decode('UTF-8').split('.')[0]
297 #startDateTimeStr_File = "2019-12-16 09:21:11"
297 #startDateTimeStr_File = "2019-12-16 09:21:11"
298 junk = time.strptime(startDateTimeStr_File, '%Y-%m-%d %H:%M:%S')
298 junk = time.strptime(startDateTimeStr_File, '%Y-%m-%d %H:%M:%S')
299 startDateTime_File = datetime.datetime(junk.tm_year,junk.tm_mon,junk.tm_mday,junk.tm_hour, junk.tm_min, junk.tm_sec)
299 startDateTime_File = datetime.datetime(junk.tm_year,junk.tm_mon,junk.tm_mday,junk.tm_hour, junk.tm_min, junk.tm_sec)
300
300
301 #endDateTimeStr_File = "2019-12-16 11:10:11"
301 #endDateTimeStr_File = "2019-12-16 11:10:11"
302 endDateTimeStr_File = time_str[-1][-1].decode('UTF-8').split('.')[0]
302 endDateTimeStr_File = time_str[-1][-1].decode('UTF-8').split('.')[0]
303 junk = time.strptime(endDateTimeStr_File, '%Y-%m-%d %H:%M:%S')
303 junk = time.strptime(endDateTimeStr_File, '%Y-%m-%d %H:%M:%S')
304 endDateTime_File = datetime.datetime(junk.tm_year,junk.tm_mon,junk.tm_mday,junk.tm_hour, junk.tm_min, junk.tm_sec)
304 endDateTime_File = datetime.datetime(junk.tm_year,junk.tm_mon,junk.tm_mday,junk.tm_hour, junk.tm_min, junk.tm_sec)
305
305
306 fp.close()
306 fp.close()
307
307
308 #print("check time", startDateTime_File)
308 #print("check time", startDateTime_File)
309 if self.timezone == 'lt':
309 if self.timezone == 'lt':
310 startDateTime_File = startDateTime_File - datetime.timedelta(minutes = 300)
310 startDateTime_File = startDateTime_File - datetime.timedelta(minutes = 300)
311 endDateTime_File = endDateTime_File - datetime.timedelta(minutes = 300)
311 endDateTime_File = endDateTime_File - datetime.timedelta(minutes = 300)
312 if (startDateTime_File >=startDateTime_Reader and endDateTime_File<=endDateTime_Reader):
312 if (startDateTime_File >=startDateTime_Reader and endDateTime_File<=endDateTime_Reader):
313 filter_filenameList.append(filename)
313 filter_filenameList.append(filename)
314
314
315 if (startDateTime_File>endDateTime_Reader):
315 if (startDateTime_File>endDateTime_Reader):
316 break
316 break
317 except Exception as e:
317 except Exception as e:
318 log.warning("Error opening file {} -> {}".format(os.path.split(filename)[1],e))
318 log.warning("Error opening file {} -> {}".format(os.path.split(filename)[1],e))
319
319
320 filter_filenameList.sort()
320 filter_filenameList.sort()
321 self.filenameList = filter_filenameList
321 self.filenameList = filter_filenameList
322
322
323 return 1
323 return 1
324
324
325 def __filterByGlob1(self, dirName):
325 def __filterByGlob1(self, dirName):
326 filter_files = glob.glob1(dirName, '*.*%s'%self.extension_file)
326 filter_files = glob.glob1(dirName, '*.*%s'%self.extension_file)
327 filter_files.sort()
327 filter_files.sort()
328 filterDict = {}
328 filterDict = {}
329 filterDict.setdefault(dirName)
329 filterDict.setdefault(dirName)
330 filterDict[dirName] = filter_files
330 filterDict[dirName] = filter_files
331 return filterDict
331 return filterDict
332
332
333 def __getFilenameList(self, fileListInKeys, dirList):
333 def __getFilenameList(self, fileListInKeys, dirList):
334 for value in fileListInKeys:
334 for value in fileListInKeys:
335 dirName = list(value.keys())[0]
335 dirName = list(value.keys())[0]
336 for file in value[dirName]:
336 for file in value[dirName]:
337 filename = os.path.join(dirName, file)
337 filename = os.path.join(dirName, file)
338 self.filenameList.append(filename)
338 self.filenameList.append(filename)
339
339
340
340
341 def __selectDataForTimes(self, online=False):
341 def __selectDataForTimes(self, online=False):
342 #aun no esta implementado el filtro for tiempo
342 #aun no esta implementado el filtro for tiempo
343 if not(self.status):
343 if not(self.status):
344 return None
344 return None
345
345
346 dirList = [os.path.join(self.path,x) for x in self.dirnameList]
346 dirList = [os.path.join(self.path,x) for x in self.dirnameList]
347 fileListInKeys = [self.__filterByGlob1(x) for x in dirList]
347 fileListInKeys = [self.__filterByGlob1(x) for x in dirList]
348 self.__getFilenameList(fileListInKeys, dirList)
348 self.__getFilenameList(fileListInKeys, dirList)
349 if not(online):
349 if not(online):
350 #filtro por tiempo
350 #filtro por tiempo
351 if not(self.all):
351 if not(self.all):
352 self.__getTimeFromData()
352 self.__getTimeFromData()
353
353
354 if len(self.filenameList)>0:
354 if len(self.filenameList)>0:
355 self.status = 1
355 self.status = 1
356 self.filenameList.sort()
356 self.filenameList.sort()
357 else:
357 else:
358 self.status = 0
358 self.status = 0
359 return None
359 return None
360
360
361 else:
361 else:
362 #get the last file - 1
362 #get the last file - 1
363 self.filenameList = [self.filenameList[-2]]
363 self.filenameList = [self.filenameList[-2]]
364 new_dirnameList = []
364 new_dirnameList = []
365 for dirname in self.dirnameList:
365 for dirname in self.dirnameList:
366 junk = numpy.array([dirname in x for x in self.filenameList])
366 junk = numpy.array([dirname in x for x in self.filenameList])
367 junk_sum = junk.sum()
367 junk_sum = junk.sum()
368 if junk_sum > 0:
368 if junk_sum > 0:
369 new_dirnameList.append(dirname)
369 new_dirnameList.append(dirname)
370 self.dirnameList = new_dirnameList
370 self.dirnameList = new_dirnameList
371 return 1
371 return 1
372
372
373 def searchFilesOnLine(self, path, startDate, endDate, startTime=datetime.time(0,0,0),
373 def searchFilesOnLine(self, path, startDate, endDate, startTime=datetime.time(0,0,0),
374 endTime=datetime.time(23,59,59),walk=True):
374 endTime=datetime.time(23,59,59),walk=True):
375
375
376 if endDate ==None:
376 if endDate ==None:
377 startDate = datetime.datetime.utcnow().date()
377 startDate = datetime.datetime.utcnow().date()
378 endDate = datetime.datetime.utcnow().date()
378 endDate = datetime.datetime.utcnow().date()
379
379
380 self.__setParameters(path=path, startDate=startDate, endDate=endDate,startTime = startTime,endTime=endTime, walk=walk)
380 self.__setParameters(path=path, startDate=startDate, endDate=endDate,startTime = startTime,endTime=endTime, walk=walk)
381
381
382 self.__checkPath()
382 self.__checkPath()
383
383
384 self.__findDataForDates(online=True)
384 self.__findDataForDates(online=True)
385
385
386 self.dirnameList = [self.dirnameList[-1]]
386 self.dirnameList = [self.dirnameList[-1]]
387
387
388 self.__selectDataForTimes(online=True)
388 self.__selectDataForTimes(online=True)
389
389
390 return
390 return
391
391
392
392
393 def searchFilesOffLine(self,
393 def searchFilesOffLine(self,
394 path,
394 path,
395 startDate,
395 startDate,
396 endDate,
396 endDate,
397 startTime=datetime.time(0,0,0),
397 startTime=datetime.time(0,0,0),
398 endTime=datetime.time(23,59,59),
398 endTime=datetime.time(23,59,59),
399 walk=True):
399 walk=True):
400
400
401 self.__setParameters(path, startDate, endDate, startTime, endTime, walk)
401 self.__setParameters(path, startDate, endDate, startTime, endTime, walk)
402
402
403 self.__checkPath()
403 self.__checkPath()
404
404
405 self.__findDataForDates()
405 self.__findDataForDates()
406
406
407 self.__selectDataForTimes()
407 self.__selectDataForTimes()
408
408
409 for i in range(len(self.filenameList)):
409 for i in range(len(self.filenameList)):
410 print("%s" %(self.filenameList[i]))
410 print("%s" %(self.filenameList[i]))
411
411
412 return
412 return
413
413
414 def __setNextFileOffline(self):
414 def __setNextFileOffline(self):
415
415
416 try:
416 try:
417 self.filename = self.filenameList[self.fileIndex]
417 self.filename = self.filenameList[self.fileIndex]
418 self.amisrFilePointer = h5py.File(self.filename,'r')
418 self.amisrFilePointer = h5py.File(self.filename,'r')
419 self.fileIndex += 1
419 self.fileIndex += 1
420 except:
420 except:
421 self.flagNoMoreFiles = 1
421 self.flagNoMoreFiles = 1
422 raise schainpy.admin.SchainError('No more files to read')
422 raise schainpy.admin.SchainError('No more files to read')
423 return 0
423 return 0
424
424
425 self.flagIsNewFile = 1
425 self.flagIsNewFile = 1
426 print("Setting the file: %s"%self.filename)
426 print("Setting the file: %s"%self.filename)
427
427
428 return 1
428 return 1
429
429
430
430
431 def __setNextFileOnline(self):
431 def __setNextFileOnline(self):
432 filename = self.filenameList[0]
432 filename = self.filenameList[0]
433 if self.__filename_online != None:
433 if self.__filename_online != None:
434 self.__selectDataForTimes(online=True)
434 self.__selectDataForTimes(online=True)
435 filename = self.filenameList[0]
435 filename = self.filenameList[0]
436 wait = 0
436 wait = 0
437 self.__waitForNewFile=300 ## DEBUG:
437 self.__waitForNewFile=300 ## DEBUG:
438 while self.__filename_online == filename:
438 while self.__filename_online == filename:
439 print('waiting %d seconds to get a new file...'%(self.__waitForNewFile))
439 print('waiting %d seconds to get a new file...'%(self.__waitForNewFile))
440 if wait == 5:
440 if wait == 5:
441 self.flagNoMoreFiles = 1
441 self.flagNoMoreFiles = 1
442 return 0
442 return 0
443 sleep(self.__waitForNewFile)
443 sleep(self.__waitForNewFile)
444 self.__selectDataForTimes(online=True)
444 self.__selectDataForTimes(online=True)
445 filename = self.filenameList[0]
445 filename = self.filenameList[0]
446 wait += 1
446 wait += 1
447
447
448 self.__filename_online = filename
448 self.__filename_online = filename
449
449
450 self.amisrFilePointer = h5py.File(filename,'r')
450 self.amisrFilePointer = h5py.File(filename,'r')
451 self.flagIsNewFile = 1
451 self.flagIsNewFile = 1
452 self.filename = filename
452 self.filename = filename
453 print("Setting the file: %s"%self.filename)
453 print("Setting the file: %s"%self.filename)
454 return 1
454 return 1
455
455
456
456
457 def readData(self):
457 def readData(self):
458 buffer = self.amisrFilePointer.get('Raw11/Data/Samples/Data')
458 buffer = self.amisrFilePointer.get('Raw11/Data/Samples/Data')
459 re = buffer[:,:,:,0]
459 re = buffer[:,:,:,0]
460 im = buffer[:,:,:,1]
460 im = buffer[:,:,:,1]
461 dataset = re + im*1j
461 dataset = re + im*1j
462
462
463 self.radacTime = self.amisrFilePointer.get('Raw11/Data/RadacHeader/RadacTime')
463 self.radacTime = self.amisrFilePointer.get('Raw11/Data/RadacHeader/RadacTime')
464 timeset = self.radacTime[:,0]
464 timeset = self.radacTime[:,0]
465
465
466 return dataset,timeset
466 return dataset,timeset
467
467
468 def reshapeData(self):
468 def reshapeData(self):
469 #self.beamCodeByPulse, self.beamCode, self.nblocks, self.nprofiles, self.nsa,
469 #self.beamCodeByPulse, self.beamCode, self.nblocks, self.nprofiles, self.nsa,
470 channels = self.beamCodeByPulse[0,:]
470 channels = self.beamCodeByPulse[0,:]
471 nchan = self.nchannels
471 nchan = self.nchannels
472 #self.newProfiles = self.nprofiles/nchan #must be defined on filljroheader
472 #self.newProfiles = self.nprofiles/nchan #must be defined on filljroheader
473 nblocks = self.nblocks
473 nblocks = self.nblocks
474 nsamples = self.nsa
474 nsamples = self.nsa
475
475
476 #Dimensions : nChannels, nProfiles, nSamples
476 #Dimensions : nChannels, nProfiles, nSamples
477 new_block = numpy.empty((nblocks, nchan, numpy.int_(self.newProfiles), nsamples), dtype="complex64")
477 new_block = numpy.empty((nblocks, nchan, numpy.int_(self.newProfiles), nsamples), dtype="complex64")
478 ############################################
478 ############################################
479
479
480 for thisChannel in range(nchan):
480 for thisChannel in range(nchan):
481 new_block[:,thisChannel,:,:] = self.dataset[:,numpy.where(channels==self.beamCode[thisChannel])[0],:]
481 new_block[:,thisChannel,:,:] = self.dataset[:,numpy.where(channels==self.beamCode[thisChannel])[0],:]
482
482
483
483
484 new_block = numpy.transpose(new_block, (1,0,2,3))
484 new_block = numpy.transpose(new_block, (1,0,2,3))
485 new_block = numpy.reshape(new_block, (nchan,-1, nsamples))
485 new_block = numpy.reshape(new_block, (nchan,-1, nsamples))
486
486
487 return new_block
487 return new_block
488
488
489 def updateIndexes(self):
489 def updateIndexes(self):
490
490
491 pass
491 pass
492
492
493 def fillJROHeader(self):
493 def fillJROHeader(self):
494
494
495 #fill radar controller header
495 #fill radar controller header
496 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(ipp=self.__ippKm,
496 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(ipp=self.__ippKm,
497 txA=self.__txA,
497 txA=self.__txA,
498 txB=0,
498 txB=0,
499 nWindows=1,
499 nWindows=1,
500 nHeights=self.__nSamples,
500 nHeights=self.__nSamples,
501 firstHeight=self.__firstHeight,
501 firstHeight=self.__firstHeight,
502 deltaHeight=self.__deltaHeight,
502 deltaHeight=self.__deltaHeight,
503 codeType=self.__codeType,
503 codeType=self.__codeType,
504 nCode=self.__nCode, nBaud=self.__nBaud,
504 nCode=self.__nCode, nBaud=self.__nBaud,
505 code = self.__code,
505 code = self.__code,
506 fClock=1)
506 fClock=1)
507 #fill system header
507 #fill system header
508 self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples,
508 self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples,
509 nProfiles=self.newProfiles,
509 nProfiles=self.newProfiles,
510 nChannels=len(self.__channelList),
510 nChannels=len(self.__channelList),
511 adcResolution=14,
511 adcResolution=14,
512 pciDioBusWidth=32)
512 pciDioBusWidth=32)
513
513
514 self.dataOut.type = "Voltage"
514 self.dataOut.type = "Voltage"
515 self.dataOut.data = None
515 self.dataOut.data = None
516 self.dataOut.dtype = numpy.dtype([('real','<i8'),('imag','<i8')])
516 self.dataOut.dtype = numpy.dtype([('real','<i8'),('imag','<i8')])
517 # self.dataOut.nChannels = 0
517 # self.dataOut.nChannels = 0
518
518
519 # self.dataOut.nHeights = 0
519 # self.dataOut.nHeights = 0
520
520
521 self.dataOut.nProfiles = self.newProfiles*self.nblocks
521 self.dataOut.nProfiles = self.newProfiles*self.nblocks
522 #self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype = numpy.float)*self.__deltaHeigth
522 #self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype = numpy.float)*self.__deltaHeigth
523 ranges = numpy.reshape(self.rangeFromFile[()],(-1))
523 ranges = numpy.reshape(self.rangeFromFile[()],(-1))
524 self.dataOut.heightList = ranges/1000.0 #km
524 self.dataOut.heightList = ranges/1000.0 #km
525 self.dataOut.channelList = self.__channelList
525 self.dataOut.channelList = self.__channelList
526 self.dataOut.blocksize = self.dataOut.nChannels * self.dataOut.nHeights
526 self.dataOut.blocksize = self.dataOut.nChannels * self.dataOut.nHeights
527
527
528 # self.dataOut.channelIndexList = None
528 # self.dataOut.channelIndexList = None
529
529
530
530
531 self.dataOut.azimuthList = numpy.array(self.azimuthList)
531 self.dataOut.azimuthList = numpy.array(self.azimuthList)
532 self.dataOut.elevationList = numpy.array(self.elevationList)
532 self.dataOut.elevationList = numpy.array(self.elevationList)
533 self.dataOut.codeList = numpy.array(self.beamCode)
533 self.dataOut.codeList = numpy.array(self.beamCode)
534 #print(self.dataOut.elevationList)
534 #print(self.dataOut.elevationList)
535 self.dataOut.flagNoData = True
535 self.dataOut.flagNoData = True
536
536
537 #Set to TRUE if the data is discontinuous
537 #Set to TRUE if the data is discontinuous
538 self.dataOut.flagDiscontinuousBlock = False
538 self.dataOut.flagDiscontinuousBlock = False
539
539
540 self.dataOut.utctime = None
540 self.dataOut.utctime = None
541
541
542 #self.dataOut.timeZone = -5 #self.__timezone/60 #timezone like jroheader, difference in minutes between UTC and localtime
542 #self.dataOut.timeZone = -5 #self.__timezone/60 #timezone like jroheader, difference in minutes between UTC and localtime
543 if self.timezone == 'lt':
543 if self.timezone == 'lt':
544 self.dataOut.timeZone = time.timezone / 60. #get the timezone in minutes
544 self.dataOut.timeZone = time.timezone / 60. #get the timezone in minutes
545 else:
545 else:
546 self.dataOut.timeZone = 0 #by default time is UTC
546 self.dataOut.timeZone = 0 #by default time is UTC
547
547
548 self.dataOut.dstFlag = 0
548 self.dataOut.dstFlag = 0
549 self.dataOut.errorCount = 0
549 self.dataOut.errorCount = 0
550 self.dataOut.nCohInt = 1
550 self.dataOut.nCohInt = 1
551 self.dataOut.flagDecodeData = False #asumo que la data esta decodificada
551 self.dataOut.flagDecodeData = False #asumo que la data esta decodificada
552 self.dataOut.flagDeflipData = False #asumo que la data esta sin flip
552 self.dataOut.flagDeflipData = False #asumo que la data esta sin flip
553 self.dataOut.flagShiftFFT = False
553 self.dataOut.flagShiftFFT = False
554 self.dataOut.ippSeconds = self.ippSeconds
554 self.dataOut.ippSeconds = self.ippSeconds
555
555
556 #Time interval between profiles
556 #Time interval between profiles
557 #self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt
557 #self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt
558
558
559 self.dataOut.frequency = self.__frequency
559 self.dataOut.frequency = self.__frequency
560 self.dataOut.realtime = self.online
560 self.dataOut.realtime = self.online
561 pass
561 pass
562
562
563 def readNextFile(self,online=False):
563 def readNextFile(self,online=False):
564
564
565 if not(online):
565 if not(online):
566 newFile = self.__setNextFileOffline()
566 newFile = self.__setNextFileOffline()
567 else:
567 else:
568 newFile = self.__setNextFileOnline()
568 newFile = self.__setNextFileOnline()
569
569
570 if not(newFile):
570 if not(newFile):
571 self.dataOut.error = True
571 self.dataOut.error = True
572 return 0
572 return 0
573
573
574 if not self.readAMISRHeader(self.amisrFilePointer):
574 if not self.readAMISRHeader(self.amisrFilePointer):
575 self.dataOut.error = True
575 self.dataOut.error = True
576 return 0
576 return 0
577
577
578 self.createBuffers()
578 self.createBuffers()
579 self.fillJROHeader()
579 self.fillJROHeader()
580
580
581 #self.__firstFile = False
581 #self.__firstFile = False
582
582
583
583
584
584
585 self.dataset,self.timeset = self.readData()
585 self.dataset,self.timeset = self.readData()
586
586
587 if self.endDate!=None:
587 if self.endDate!=None:
588 endDateTime_Reader = datetime.datetime.combine(self.endDate,self.endTime)
588 endDateTime_Reader = datetime.datetime.combine(self.endDate,self.endTime)
589 time_str = self.amisrFilePointer.get('Time/RadacTimeString')
589 time_str = self.amisrFilePointer.get('Time/RadacTimeString')
590 startDateTimeStr_File = time_str[0][0].decode('UTF-8').split('.')[0]
590 startDateTimeStr_File = time_str[0][0].decode('UTF-8').split('.')[0]
591 junk = time.strptime(startDateTimeStr_File, '%Y-%m-%d %H:%M:%S')
591 junk = time.strptime(startDateTimeStr_File, '%Y-%m-%d %H:%M:%S')
592 startDateTime_File = datetime.datetime(junk.tm_year,junk.tm_mon,junk.tm_mday,junk.tm_hour, junk.tm_min, junk.tm_sec)
592 startDateTime_File = datetime.datetime(junk.tm_year,junk.tm_mon,junk.tm_mday,junk.tm_hour, junk.tm_min, junk.tm_sec)
593 if self.timezone == 'lt':
593 if self.timezone == 'lt':
594 startDateTime_File = startDateTime_File - datetime.timedelta(minutes = 300)
594 startDateTime_File = startDateTime_File - datetime.timedelta(minutes = 300)
595 if (startDateTime_File>endDateTime_Reader):
595 if (startDateTime_File>endDateTime_Reader):
596 return 0
596 return 0
597
597
598 self.jrodataset = self.reshapeData()
598 self.jrodataset = self.reshapeData()
599 #----self.updateIndexes()
599 #----self.updateIndexes()
600 self.profileIndex = 0
600 self.profileIndex = 0
601
601
602 return 1
602 return 1
603
603
604
604
605 def __hasNotDataInBuffer(self):
605 def __hasNotDataInBuffer(self):
606 if self.profileIndex >= (self.newProfiles*self.nblocks):
606 if self.profileIndex >= (self.newProfiles*self.nblocks):
607 return 1
607 return 1
608 return 0
608 return 0
609
609
610
610
611 def getData(self):
611 def getData(self):
612
612
613 if self.flagNoMoreFiles:
613 if self.flagNoMoreFiles:
614 self.dataOut.flagNoData = True
614 self.dataOut.flagNoData = True
615 return 0
615 return 0
616
616
617 if self.__hasNotDataInBuffer():
617 if self.profileIndex >= (self.newProfiles*self.nblocks): #
618 #if self.__hasNotDataInBuffer():
618 if not (self.readNextFile(self.online)):
619 if not (self.readNextFile(self.online)):
619 return 0
620 return 0
620
621
621
622
622 if self.dataset is None: # setear esta condicion cuando no hayan datos por leer
623 if self.dataset is None: # setear esta condicion cuando no hayan datos por leer
623 self.dataOut.flagNoData = True
624 self.dataOut.flagNoData = True
624 return 0
625 return 0
625
626
626 #self.dataOut.data = numpy.reshape(self.jrodataset[self.profileIndex,:],(1,-1))
627 #self.dataOut.data = numpy.reshape(self.jrodataset[self.profileIndex,:],(1,-1))
627
628
628 self.dataOut.data = self.jrodataset[:,self.profileIndex,:]
629 self.dataOut.data = self.jrodataset[:,self.profileIndex,:]
629
630
630 #print("R_t",self.timeset)
631 #print("R_t",self.timeset)
631
632
632 #self.dataOut.utctime = self.jrotimeset[self.profileIndex]
633 #self.dataOut.utctime = self.jrotimeset[self.profileIndex]
633 #verificar basic header de jro data y ver si es compatible con este valor
634 #verificar basic header de jro data y ver si es compatible con este valor
634 #self.dataOut.utctime = self.timeset + (self.profileIndex * self.ippSeconds * self.nchannels)
635 #self.dataOut.utctime = self.timeset + (self.profileIndex * self.ippSeconds * self.nchannels)
635 indexprof = numpy.mod(self.profileIndex, self.newProfiles)
636 indexprof = numpy.mod(self.profileIndex, self.newProfiles)
636 indexblock = self.profileIndex/self.newProfiles
637 indexblock = self.profileIndex/self.newProfiles
637 #print (indexblock, indexprof)
638 #print (indexblock, indexprof)
638 diffUTC = 0
639 diffUTC = 0
639 t_comp = (indexprof * self.ippSeconds * self.nchannels) + diffUTC #
640 t_comp = (indexprof * self.ippSeconds * self.nchannels) + diffUTC #
640
641
641 #print("utc :",indexblock," __ ",t_comp)
642 #print("utc :",indexblock," __ ",t_comp)
642 #print(numpy.shape(self.timeset))
643 #print(numpy.shape(self.timeset))
643 self.dataOut.utctime = self.timeset[numpy.int_(indexblock)] + t_comp
644 self.dataOut.utctime = self.timeset[numpy.int_(indexblock)] + t_comp
644 #self.dataOut.utctime = self.timeset[self.profileIndex] + t_comp
645 #self.dataOut.utctime = self.timeset[self.profileIndex] + t_comp
645
646
646 self.dataOut.profileIndex = self.profileIndex
647 self.dataOut.profileIndex = self.profileIndex
647 #print("N profile:",self.profileIndex,self.newProfiles,self.nblocks,self.dataOut.utctime)
648 #print("N profile:",self.profileIndex,self.newProfiles,self.nblocks,self.dataOut.utctime)
648 self.dataOut.flagNoData = False
649 self.dataOut.flagNoData = False
649 # if indexprof == 0:
650 # if indexprof == 0:
650 # print("kamisr: ",self.dataOut.utctime)
651 # print("kamisr: ",self.dataOut.utctime)
651
652
652 self.profileIndex += 1
653 self.profileIndex += 1
653
654
654 return self.dataOut.data #retorno necesario??
655 return self.dataOut.data #retorno necesario??
655
656
656
657
657 def run(self, **kwargs):
658 def run(self, **kwargs):
658 '''
659 '''
659 This method will be called many times so here you should put all your code
660 This method will be called many times so here you should put all your code
660 '''
661 '''
661 #print("running kamisr")
662 #print("running kamisr")
662 if not self.isConfig:
663 if not self.isConfig:
663 self.setup(**kwargs)
664 self.setup(**kwargs)
664 self.isConfig = True
665 self.isConfig = True
665
666
666 self.getData()
667 self.getData()
@@ -1,2076 +1,2086
1 # Copyright (c) 2012-2020 Jicamarca Radio Observatory
1 # Copyright (c) 2012-2020 Jicamarca Radio Observatory
2 # All rights reserved.
2 # All rights reserved.
3 #
3 #
4 # Distributed under the terms of the BSD 3-clause license.
4 # Distributed under the terms of the BSD 3-clause license.
5 """Spectra processing Unit and operations
5 """Spectra processing Unit and operations
6
6
7 Here you will find the processing unit `SpectraProc` and several operations
7 Here you will find the processing unit `SpectraProc` and several operations
8 to work with Spectra data type
8 to work with Spectra data type
9 """
9 """
10
10
11 import time
11 import time
12 import itertools
12 import itertools
13
13
14 import numpy
14 import numpy
15 import math
15 import math
16
16
17 from schainpy.model.proc.jroproc_base import ProcessingUnit, MPDecorator, Operation
17 from schainpy.model.proc.jroproc_base import ProcessingUnit, MPDecorator, Operation
18 from schainpy.model.data.jrodata import Spectra
18 from schainpy.model.data.jrodata import Spectra
19 from schainpy.model.data.jrodata import hildebrand_sekhon
19 from schainpy.model.data.jrodata import hildebrand_sekhon
20 from schainpy.model.data import _noise
20 from schainpy.model.data import _noise
21
21
22 from schainpy.utils import log
22 from schainpy.utils import log
23 import matplotlib.pyplot as plt
23 import matplotlib.pyplot as plt
24 #from scipy.optimize import curve_fit
24 #from scipy.optimize import curve_fit
25
25
26 class SpectraProc(ProcessingUnit):
26 class SpectraProc(ProcessingUnit):
27
27
28 def __init__(self):
28 def __init__(self):
29
29
30 ProcessingUnit.__init__(self)
30 ProcessingUnit.__init__(self)
31
31
32 self.buffer = None
32 self.buffer = None
33 self.firstdatatime = None
33 self.firstdatatime = None
34 self.profIndex = 0
34 self.profIndex = 0
35 self.dataOut = Spectra()
35 self.dataOut = Spectra()
36 self.id_min = None
36 self.id_min = None
37 self.id_max = None
37 self.id_max = None
38 self.setupReq = False #Agregar a todas las unidades de proc
38 self.setupReq = False #Agregar a todas las unidades de proc
39
39
40 def __updateSpecFromVoltage(self):
40 def __updateSpecFromVoltage(self):
41
41
42
42
43
43
44 self.dataOut.timeZone = self.dataIn.timeZone
44 self.dataOut.timeZone = self.dataIn.timeZone
45 self.dataOut.dstFlag = self.dataIn.dstFlag
45 self.dataOut.dstFlag = self.dataIn.dstFlag
46 self.dataOut.errorCount = self.dataIn.errorCount
46 self.dataOut.errorCount = self.dataIn.errorCount
47 self.dataOut.useLocalTime = self.dataIn.useLocalTime
47 self.dataOut.useLocalTime = self.dataIn.useLocalTime
48 try:
48 try:
49 self.dataOut.processingHeaderObj = self.dataIn.processingHeaderObj.copy()
49 self.dataOut.processingHeaderObj = self.dataIn.processingHeaderObj.copy()
50 except:
50 except:
51 pass
51 pass
52 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
52 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
53 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
53 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
54 self.dataOut.channelList = self.dataIn.channelList
54 self.dataOut.channelList = self.dataIn.channelList
55 self.dataOut.heightList = self.dataIn.heightList
55 self.dataOut.heightList = self.dataIn.heightList
56 self.dataOut.dtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
56 self.dataOut.dtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
57 self.dataOut.nProfiles = self.dataOut.nFFTPoints
57 self.dataOut.nProfiles = self.dataOut.nFFTPoints
58 self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock
58 self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock
59 self.dataOut.utctime = self.firstdatatime
59 self.dataOut.utctime = self.firstdatatime
60 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData
60 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData
61 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData
61 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData
62 self.dataOut.flagShiftFFT = False
62 self.dataOut.flagShiftFFT = False
63 self.dataOut.nCohInt = self.dataIn.nCohInt
63 self.dataOut.nCohInt = self.dataIn.nCohInt
64 self.dataOut.nIncohInt = 1
64 self.dataOut.nIncohInt = 1
65 self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
65 self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
66 self.dataOut.frequency = self.dataIn.frequency
66 self.dataOut.frequency = self.dataIn.frequency
67 self.dataOut.realtime = self.dataIn.realtime
67 self.dataOut.realtime = self.dataIn.realtime
68 self.dataOut.azimuth = self.dataIn.azimuth
68 self.dataOut.azimuth = self.dataIn.azimuth
69 self.dataOut.zenith = self.dataIn.zenith
69 self.dataOut.zenith = self.dataIn.zenith
70 self.dataOut.codeList = self.dataIn.codeList
70 self.dataOut.codeList = self.dataIn.codeList
71 self.dataOut.azimuthList = self.dataIn.azimuthList
71 self.dataOut.azimuthList = self.dataIn.azimuthList
72 self.dataOut.elevationList = self.dataIn.elevationList
72 self.dataOut.elevationList = self.dataIn.elevationList
73
73
74
74
75 def __getFft(self):
75 def __getFft(self):
76 """
76 """
77 Convierte valores de Voltaje a Spectra
77 Convierte valores de Voltaje a Spectra
78
78
79 Affected:
79 Affected:
80 self.dataOut.data_spc
80 self.dataOut.data_spc
81 self.dataOut.data_cspc
81 self.dataOut.data_cspc
82 self.dataOut.data_dc
82 self.dataOut.data_dc
83 self.dataOut.heightList
83 self.dataOut.heightList
84 self.profIndex
84 self.profIndex
85 self.buffer
85 self.buffer
86 self.dataOut.flagNoData
86 self.dataOut.flagNoData
87 """
87 """
88 fft_volt = numpy.fft.fft(
88 fft_volt = numpy.fft.fft(
89 self.buffer, n=self.dataOut.nFFTPoints, axis=1)
89 self.buffer, n=self.dataOut.nFFTPoints, axis=1)
90 fft_volt = fft_volt.astype(numpy.dtype('complex'))
90 fft_volt = fft_volt.astype(numpy.dtype('complex'))
91 dc = fft_volt[:, 0, :]
91 dc = fft_volt[:, 0, :]
92
92
93 # calculo de self-spectra
93 # calculo de self-spectra
94 fft_volt = numpy.fft.fftshift(fft_volt, axes=(1,))
94 fft_volt = numpy.fft.fftshift(fft_volt, axes=(1,))
95 spc = fft_volt * numpy.conjugate(fft_volt)
95 spc = fft_volt * numpy.conjugate(fft_volt)
96 spc = spc.real
96 spc = spc.real
97
97
98 blocksize = 0
98 blocksize = 0
99 blocksize += dc.size
99 blocksize += dc.size
100 blocksize += spc.size
100 blocksize += spc.size
101
101
102 cspc = None
102 cspc = None
103 pairIndex = 0
103 pairIndex = 0
104 if self.dataOut.pairsList != None:
104 if self.dataOut.pairsList != None:
105 # calculo de cross-spectra
105 # calculo de cross-spectra
106 cspc = numpy.zeros(
106 cspc = numpy.zeros(
107 (self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex')
107 (self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex')
108 for pair in self.dataOut.pairsList:
108 for pair in self.dataOut.pairsList:
109 if pair[0] not in self.dataOut.channelList:
109 if pair[0] not in self.dataOut.channelList:
110 raise ValueError("Error getting CrossSpectra: pair 0 of %s is not in channelList = %s" % (
110 raise ValueError("Error getting CrossSpectra: pair 0 of %s is not in channelList = %s" % (
111 str(pair), str(self.dataOut.channelList)))
111 str(pair), str(self.dataOut.channelList)))
112 if pair[1] not in self.dataOut.channelList:
112 if pair[1] not in self.dataOut.channelList:
113 raise ValueError("Error getting CrossSpectra: pair 1 of %s is not in channelList = %s" % (
113 raise ValueError("Error getting CrossSpectra: pair 1 of %s is not in channelList = %s" % (
114 str(pair), str(self.dataOut.channelList)))
114 str(pair), str(self.dataOut.channelList)))
115
115
116 cspc[pairIndex, :, :] = fft_volt[pair[0], :, :] * \
116 cspc[pairIndex, :, :] = fft_volt[pair[0], :, :] * \
117 numpy.conjugate(fft_volt[pair[1], :, :])
117 numpy.conjugate(fft_volt[pair[1], :, :])
118 pairIndex += 1
118 pairIndex += 1
119 blocksize += cspc.size
119 blocksize += cspc.size
120
120
121 self.dataOut.data_spc = spc
121 self.dataOut.data_spc = spc
122 self.dataOut.data_cspc = cspc
122 self.dataOut.data_cspc = cspc
123 self.dataOut.data_dc = dc
123 self.dataOut.data_dc = dc
124 self.dataOut.blockSize = blocksize
124 self.dataOut.blockSize = blocksize
125 self.dataOut.flagShiftFFT = False
125 self.dataOut.flagShiftFFT = False
126
126
127 def run(self, nProfiles=None, nFFTPoints=None, pairsList=None, ippFactor=None, shift_fft=False):
127 def run(self, nProfiles=None, nFFTPoints=None, pairsList=None, ippFactor=None, shift_fft=False):
128 #print("run spc proc")
128 #print("run spc proc")
129 try:
129 try:
130 type = self.dataIn.type.decode("utf-8")
130 type = self.dataIn.type.decode("utf-8")
131 self.dataIn.type = type
131 self.dataIn.type = type
132 except:
132 except:
133 pass
133 pass
134 if self.dataIn.type == "Spectra":
134 if self.dataIn.type == "Spectra":
135
135
136 try:
136 try:
137 self.dataOut.copy(self.dataIn)
137 self.dataOut.copy(self.dataIn)
138
138
139 except Exception as e:
139 except Exception as e:
140 print("Error dataIn ",e)
140 print("Error dataIn ",e)
141
141
142 if shift_fft:
142 if shift_fft:
143 #desplaza a la derecha en el eje 2 determinadas posiciones
143 #desplaza a la derecha en el eje 2 determinadas posiciones
144 shift = int(self.dataOut.nFFTPoints/2)
144 shift = int(self.dataOut.nFFTPoints/2)
145 self.dataOut.data_spc = numpy.roll(self.dataOut.data_spc, shift , axis=1)
145 self.dataOut.data_spc = numpy.roll(self.dataOut.data_spc, shift , axis=1)
146
146
147 if self.dataOut.data_cspc is not None:
147 if self.dataOut.data_cspc is not None:
148 #desplaza a la derecha en el eje 2 determinadas posiciones
148 #desplaza a la derecha en el eje 2 determinadas posiciones
149 self.dataOut.data_cspc = numpy.roll(self.dataOut.data_cspc, shift, axis=1)
149 self.dataOut.data_cspc = numpy.roll(self.dataOut.data_cspc, shift, axis=1)
150 if pairsList:
150 if pairsList:
151 self.__selectPairs(pairsList)
151 self.__selectPairs(pairsList)
152
152
153
153
154 elif self.dataIn.type == "Voltage":
154 elif self.dataIn.type == "Voltage":
155
155
156 self.dataOut.flagNoData = True
156 self.dataOut.flagNoData = True
157
157
158 if nFFTPoints == None:
158 if nFFTPoints == None:
159 raise ValueError("This SpectraProc.run() need nFFTPoints input variable")
159 raise ValueError("This SpectraProc.run() need nFFTPoints input variable")
160
160
161 if nProfiles == None:
161 if nProfiles == None:
162 nProfiles = nFFTPoints
162 nProfiles = nFFTPoints
163
163
164 if ippFactor == None:
164 if ippFactor == None:
165 self.dataOut.ippFactor = 1
165 self.dataOut.ippFactor = 1
166
166
167 self.dataOut.nFFTPoints = nFFTPoints
167 self.dataOut.nFFTPoints = nFFTPoints
168 #print(" volts ch,prof, h: ", self.dataIn.data.shape)
168 #print(" volts ch,prof, h: ", self.dataIn.data.shape)
169 if self.buffer is None:
169 if self.buffer is None:
170 self.buffer = numpy.zeros((self.dataIn.nChannels,
170 self.buffer = numpy.zeros((self.dataIn.nChannels,
171 nProfiles,
171 nProfiles,
172 self.dataIn.nHeights),
172 self.dataIn.nHeights),
173 dtype='complex')
173 dtype='complex')
174
174
175 if self.dataIn.flagDataAsBlock:
175 if self.dataIn.flagDataAsBlock:
176 nVoltProfiles = self.dataIn.data.shape[1]
176 nVoltProfiles = self.dataIn.data.shape[1]
177
177
178 if nVoltProfiles == nProfiles:
178 if nVoltProfiles == nProfiles:
179 self.buffer = self.dataIn.data.copy()
179 self.buffer = self.dataIn.data.copy()
180 self.profIndex = nVoltProfiles
180 self.profIndex = nVoltProfiles
181
181
182 elif nVoltProfiles < nProfiles:
182 elif nVoltProfiles < nProfiles:
183
183
184 if self.profIndex == 0:
184 if self.profIndex == 0:
185 self.id_min = 0
185 self.id_min = 0
186 self.id_max = nVoltProfiles
186 self.id_max = nVoltProfiles
187
187
188 self.buffer[:, self.id_min:self.id_max,
188 self.buffer[:, self.id_min:self.id_max,
189 :] = self.dataIn.data
189 :] = self.dataIn.data
190 self.profIndex += nVoltProfiles
190 self.profIndex += nVoltProfiles
191 self.id_min += nVoltProfiles
191 self.id_min += nVoltProfiles
192 self.id_max += nVoltProfiles
192 self.id_max += nVoltProfiles
193 else:
193 else:
194 raise ValueError("The type object %s has %d profiles, it should just has %d profiles" % (
194 raise ValueError("The type object %s has %d profiles, it should just has %d profiles" % (
195 self.dataIn.type, self.dataIn.data.shape[1], nProfiles))
195 self.dataIn.type, self.dataIn.data.shape[1], nProfiles))
196 self.dataOut.flagNoData = True
196 self.dataOut.flagNoData = True
197 else:
197 else:
198 self.buffer[:, self.profIndex, :] = self.dataIn.data.copy()
198 self.buffer[:, self.profIndex, :] = self.dataIn.data.copy()
199 self.profIndex += 1
199 self.profIndex += 1
200
200
201 if self.firstdatatime == None:
201 if self.firstdatatime == None:
202 self.firstdatatime = self.dataIn.utctime
202 self.firstdatatime = self.dataIn.utctime
203
203
204 if self.profIndex == nProfiles:
204 if self.profIndex == nProfiles:
205
205
206 self.__updateSpecFromVoltage()
206 self.__updateSpecFromVoltage()
207
207
208 if pairsList == None:
208 if pairsList == None:
209 self.dataOut.pairsList = [pair for pair in itertools.combinations(self.dataOut.channelList, 2)]
209 self.dataOut.pairsList = [pair for pair in itertools.combinations(self.dataOut.channelList, 2)]
210 else:
210 else:
211 self.dataOut.pairsList = pairsList
211 self.dataOut.pairsList = pairsList
212 self.__getFft()
212 self.__getFft()
213 self.dataOut.flagNoData = False
213 self.dataOut.flagNoData = False
214 self.firstdatatime = None
214 self.firstdatatime = None
215 self.profIndex = 0
215 self.profIndex = 0
216
216
217 elif self.dataIn.type == "Parameters":
217 elif self.dataIn.type == "Parameters":
218
218
219 self.dataOut.data_spc = self.dataIn.data_spc
219 self.dataOut.data_spc = self.dataIn.data_spc
220 self.dataOut.data_cspc = self.dataIn.data_cspc
220 self.dataOut.data_cspc = self.dataIn.data_cspc
221 self.dataOut.data_outlier = self.dataIn.data_outlier
221 self.dataOut.data_outlier = self.dataIn.data_outlier
222 self.dataOut.nProfiles = self.dataIn.nProfiles
222 self.dataOut.nProfiles = self.dataIn.nProfiles
223 self.dataOut.nIncohInt = self.dataIn.nIncohInt
223 self.dataOut.nIncohInt = self.dataIn.nIncohInt
224 self.dataOut.nFFTPoints = self.dataIn.nFFTPoints
224 self.dataOut.nFFTPoints = self.dataIn.nFFTPoints
225 self.dataOut.ippFactor = self.dataIn.ippFactor
225 self.dataOut.ippFactor = self.dataIn.ippFactor
226 self.dataOut.max_nIncohInt = self.dataIn.max_nIncohInt
226 self.dataOut.max_nIncohInt = self.dataIn.max_nIncohInt
227 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
227 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
228 self.dataOut.ipp = self.dataIn.ipp
228 self.dataOut.ipp = self.dataIn.ipp
229 #self.dataOut.abscissaList = self.dataIn.getVelRange(1)
229 #self.dataOut.abscissaList = self.dataIn.getVelRange(1)
230 #self.dataOut.spc_noise = self.dataIn.getNoise()
230 #self.dataOut.spc_noise = self.dataIn.getNoise()
231 #self.dataOut.spc_range = (self.dataIn.getFreqRange(1) , self.dataIn.getAcfRange(1) , self.dataIn.getVelRange(1))
231 #self.dataOut.spc_range = (self.dataIn.getFreqRange(1) , self.dataIn.getAcfRange(1) , self.dataIn.getVelRange(1))
232 # self.dataOut.normFactor = self.dataIn.normFactor
232 # self.dataOut.normFactor = self.dataIn.normFactor
233 if hasattr(self.dataIn, 'channelList'):
233 if hasattr(self.dataIn, 'channelList'):
234 self.dataOut.channelList = self.dataIn.channelList
234 self.dataOut.channelList = self.dataIn.channelList
235 if hasattr(self.dataIn, 'pairsList'):
235 if hasattr(self.dataIn, 'pairsList'):
236 self.dataOut.pairsList = self.dataIn.pairsList
236 self.dataOut.pairsList = self.dataIn.pairsList
237 self.dataOut.groupList = self.dataIn.pairsList
237 self.dataOut.groupList = self.dataIn.pairsList
238
238
239 self.dataOut.flagNoData = False
239 self.dataOut.flagNoData = False
240
240
241 if hasattr(self.dataIn, 'ChanDist'): #Distances of receiver channels
241 if hasattr(self.dataIn, 'ChanDist'): #Distances of receiver channels
242 self.dataOut.ChanDist = self.dataIn.ChanDist
242 self.dataOut.ChanDist = self.dataIn.ChanDist
243 else: self.dataOut.ChanDist = None
243 else: self.dataOut.ChanDist = None
244
244
245 #if hasattr(self.dataIn, 'VelRange'): #Velocities range
245 #if hasattr(self.dataIn, 'VelRange'): #Velocities range
246 # self.dataOut.VelRange = self.dataIn.VelRange
246 # self.dataOut.VelRange = self.dataIn.VelRange
247 #else: self.dataOut.VelRange = None
247 #else: self.dataOut.VelRange = None
248
248
249
249
250
250
251 else:
251 else:
252 raise ValueError("The type of input object {} is not valid".format(
252 raise ValueError("The type of input object {} is not valid".format(
253 self.dataIn.type))
253 self.dataIn.type))
254
254
255
255
256 def __selectPairs(self, pairsList):
256 def __selectPairs(self, pairsList):
257
257
258 if not pairsList:
258 if not pairsList:
259 return
259 return
260
260
261 pairs = []
261 pairs = []
262 pairsIndex = []
262 pairsIndex = []
263
263
264 for pair in pairsList:
264 for pair in pairsList:
265 if pair[0] not in self.dataOut.channelList or pair[1] not in self.dataOut.channelList:
265 if pair[0] not in self.dataOut.channelList or pair[1] not in self.dataOut.channelList:
266 continue
266 continue
267 pairs.append(pair)
267 pairs.append(pair)
268 pairsIndex.append(pairs.index(pair))
268 pairsIndex.append(pairs.index(pair))
269
269
270 self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndex]
270 self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndex]
271 self.dataOut.pairsList = pairs
271 self.dataOut.pairsList = pairs
272
272
273 return
273 return
274
274
275 def selectFFTs(self, minFFT, maxFFT ):
275 def selectFFTs(self, minFFT, maxFFT ):
276 """
276 """
277 Selecciona un bloque de datos en base a un grupo de valores de puntos FFTs segun el rango
277 Selecciona un bloque de datos en base a un grupo de valores de puntos FFTs segun el rango
278 minFFT<= FFT <= maxFFT
278 minFFT<= FFT <= maxFFT
279 """
279 """
280
280
281 if (minFFT > maxFFT):
281 if (minFFT > maxFFT):
282 raise ValueError("Error selecting heights: Height range (%d,%d) is not valid" % (minFFT, maxFFT))
282 raise ValueError("Error selecting heights: Height range (%d,%d) is not valid" % (minFFT, maxFFT))
283
283
284 if (minFFT < self.dataOut.getFreqRange()[0]):
284 if (minFFT < self.dataOut.getFreqRange()[0]):
285 minFFT = self.dataOut.getFreqRange()[0]
285 minFFT = self.dataOut.getFreqRange()[0]
286
286
287 if (maxFFT > self.dataOut.getFreqRange()[-1]):
287 if (maxFFT > self.dataOut.getFreqRange()[-1]):
288 maxFFT = self.dataOut.getFreqRange()[-1]
288 maxFFT = self.dataOut.getFreqRange()[-1]
289
289
290 minIndex = 0
290 minIndex = 0
291 maxIndex = 0
291 maxIndex = 0
292 FFTs = self.dataOut.getFreqRange()
292 FFTs = self.dataOut.getFreqRange()
293
293
294 inda = numpy.where(FFTs >= minFFT)
294 inda = numpy.where(FFTs >= minFFT)
295 indb = numpy.where(FFTs <= maxFFT)
295 indb = numpy.where(FFTs <= maxFFT)
296
296
297 try:
297 try:
298 minIndex = inda[0][0]
298 minIndex = inda[0][0]
299 except:
299 except:
300 minIndex = 0
300 minIndex = 0
301
301
302 try:
302 try:
303 maxIndex = indb[0][-1]
303 maxIndex = indb[0][-1]
304 except:
304 except:
305 maxIndex = len(FFTs)
305 maxIndex = len(FFTs)
306
306
307 self.selectFFTsByIndex(minIndex, maxIndex)
307 self.selectFFTsByIndex(minIndex, maxIndex)
308
308
309 return 1
309 return 1
310
310
311 def getBeaconSignal(self, tauindex=0, channelindex=0, hei_ref=None):
311 def getBeaconSignal(self, tauindex=0, channelindex=0, hei_ref=None):
312 newheis = numpy.where(
312 newheis = numpy.where(
313 self.dataOut.heightList > self.dataOut.radarControllerHeaderObj.Taus[tauindex])
313 self.dataOut.heightList > self.dataOut.radarControllerHeaderObj.Taus[tauindex])
314
314
315 if hei_ref != None:
315 if hei_ref != None:
316 newheis = numpy.where(self.dataOut.heightList > hei_ref)
316 newheis = numpy.where(self.dataOut.heightList > hei_ref)
317
317
318 minIndex = min(newheis[0])
318 minIndex = min(newheis[0])
319 maxIndex = max(newheis[0])
319 maxIndex = max(newheis[0])
320 data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1]
320 data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1]
321 heightList = self.dataOut.heightList[minIndex:maxIndex + 1]
321 heightList = self.dataOut.heightList[minIndex:maxIndex + 1]
322
322
323 # determina indices
323 # determina indices
324 nheis = int(self.dataOut.radarControllerHeaderObj.txB /
324 nheis = int(self.dataOut.radarControllerHeaderObj.txB /
325 (self.dataOut.heightList[1] - self.dataOut.heightList[0]))
325 (self.dataOut.heightList[1] - self.dataOut.heightList[0]))
326 avg_dB = 10 * \
326 avg_dB = 10 * \
327 numpy.log10(numpy.sum(data_spc[channelindex, :, :], axis=0))
327 numpy.log10(numpy.sum(data_spc[channelindex, :, :], axis=0))
328 beacon_dB = numpy.sort(avg_dB)[-nheis:]
328 beacon_dB = numpy.sort(avg_dB)[-nheis:]
329 beacon_heiIndexList = []
329 beacon_heiIndexList = []
330 for val in avg_dB.tolist():
330 for val in avg_dB.tolist():
331 if val >= beacon_dB[0]:
331 if val >= beacon_dB[0]:
332 beacon_heiIndexList.append(avg_dB.tolist().index(val))
332 beacon_heiIndexList.append(avg_dB.tolist().index(val))
333
333
334 #data_spc = data_spc[:,:,beacon_heiIndexList]
334 #data_spc = data_spc[:,:,beacon_heiIndexList]
335 data_cspc = None
335 data_cspc = None
336 if self.dataOut.data_cspc is not None:
336 if self.dataOut.data_cspc is not None:
337 data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1]
337 data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1]
338 #data_cspc = data_cspc[:,:,beacon_heiIndexList]
338 #data_cspc = data_cspc[:,:,beacon_heiIndexList]
339
339
340 data_dc = None
340 data_dc = None
341 if self.dataOut.data_dc is not None:
341 if self.dataOut.data_dc is not None:
342 data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1]
342 data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1]
343 #data_dc = data_dc[:,beacon_heiIndexList]
343 #data_dc = data_dc[:,beacon_heiIndexList]
344
344
345 self.dataOut.data_spc = data_spc
345 self.dataOut.data_spc = data_spc
346 self.dataOut.data_cspc = data_cspc
346 self.dataOut.data_cspc = data_cspc
347 self.dataOut.data_dc = data_dc
347 self.dataOut.data_dc = data_dc
348 self.dataOut.heightList = heightList
348 self.dataOut.heightList = heightList
349 self.dataOut.beacon_heiIndexList = beacon_heiIndexList
349 self.dataOut.beacon_heiIndexList = beacon_heiIndexList
350
350
351 return 1
351 return 1
352
352
353 def selectFFTsByIndex(self, minIndex, maxIndex):
353 def selectFFTsByIndex(self, minIndex, maxIndex):
354 """
354 """
355
355
356 """
356 """
357
357
358 if (minIndex < 0) or (minIndex > maxIndex):
358 if (minIndex < 0) or (minIndex > maxIndex):
359 raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % (minIndex, maxIndex))
359 raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % (minIndex, maxIndex))
360
360
361 if (maxIndex >= self.dataOut.nProfiles):
361 if (maxIndex >= self.dataOut.nProfiles):
362 maxIndex = self.dataOut.nProfiles-1
362 maxIndex = self.dataOut.nProfiles-1
363
363
364 #Spectra
364 #Spectra
365 data_spc = self.dataOut.data_spc[:,minIndex:maxIndex+1,:]
365 data_spc = self.dataOut.data_spc[:,minIndex:maxIndex+1,:]
366
366
367 data_cspc = None
367 data_cspc = None
368 if self.dataOut.data_cspc is not None:
368 if self.dataOut.data_cspc is not None:
369 data_cspc = self.dataOut.data_cspc[:,minIndex:maxIndex+1,:]
369 data_cspc = self.dataOut.data_cspc[:,minIndex:maxIndex+1,:]
370
370
371 data_dc = None
371 data_dc = None
372 if self.dataOut.data_dc is not None:
372 if self.dataOut.data_dc is not None:
373 data_dc = self.dataOut.data_dc[minIndex:maxIndex+1,:]
373 data_dc = self.dataOut.data_dc[minIndex:maxIndex+1,:]
374
374
375 self.dataOut.data_spc = data_spc
375 self.dataOut.data_spc = data_spc
376 self.dataOut.data_cspc = data_cspc
376 self.dataOut.data_cspc = data_cspc
377 self.dataOut.data_dc = data_dc
377 self.dataOut.data_dc = data_dc
378
378
379 self.dataOut.ippSeconds = self.dataOut.ippSeconds*(self.dataOut.nFFTPoints / numpy.shape(data_cspc)[1])
379 self.dataOut.ippSeconds = self.dataOut.ippSeconds*(self.dataOut.nFFTPoints / numpy.shape(data_cspc)[1])
380 self.dataOut.nFFTPoints = numpy.shape(data_cspc)[1]
380 self.dataOut.nFFTPoints = numpy.shape(data_cspc)[1]
381 self.dataOut.profilesPerBlock = numpy.shape(data_cspc)[1]
381 self.dataOut.profilesPerBlock = numpy.shape(data_cspc)[1]
382
382
383 return 1
383 return 1
384
384
385 def getNoise(self, minHei=None, maxHei=None, minVel=None, maxVel=None):
385 def getNoise(self, minHei=None, maxHei=None, minVel=None, maxVel=None):
386 # validacion de rango
386 # validacion de rango
387 if minHei == None:
387 if minHei == None:
388 minHei = self.dataOut.heightList[0]
388 minHei = self.dataOut.heightList[0]
389
389
390 if maxHei == None:
390 if maxHei == None:
391 maxHei = self.dataOut.heightList[-1]
391 maxHei = self.dataOut.heightList[-1]
392
392
393 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
393 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
394 print('minHei: %.2f is out of the heights range' % (minHei))
394 print('minHei: %.2f is out of the heights range' % (minHei))
395 print('minHei is setting to %.2f' % (self.dataOut.heightList[0]))
395 print('minHei is setting to %.2f' % (self.dataOut.heightList[0]))
396 minHei = self.dataOut.heightList[0]
396 minHei = self.dataOut.heightList[0]
397
397
398 if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei):
398 if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei):
399 print('maxHei: %.2f is out of the heights range' % (maxHei))
399 print('maxHei: %.2f is out of the heights range' % (maxHei))
400 print('maxHei is setting to %.2f' % (self.dataOut.heightList[-1]))
400 print('maxHei is setting to %.2f' % (self.dataOut.heightList[-1]))
401 maxHei = self.dataOut.heightList[-1]
401 maxHei = self.dataOut.heightList[-1]
402
402
403 # validacion de velocidades
403 # validacion de velocidades
404 velrange = self.dataOut.getVelRange(1)
404 velrange = self.dataOut.getVelRange(1)
405
405
406 if minVel == None:
406 if minVel == None:
407 minVel = velrange[0]
407 minVel = velrange[0]
408
408
409 if maxVel == None:
409 if maxVel == None:
410 maxVel = velrange[-1]
410 maxVel = velrange[-1]
411
411
412 if (minVel < velrange[0]) or (minVel > maxVel):
412 if (minVel < velrange[0]) or (minVel > maxVel):
413 print('minVel: %.2f is out of the velocity range' % (minVel))
413 print('minVel: %.2f is out of the velocity range' % (minVel))
414 print('minVel is setting to %.2f' % (velrange[0]))
414 print('minVel is setting to %.2f' % (velrange[0]))
415 minVel = velrange[0]
415 minVel = velrange[0]
416
416
417 if (maxVel > velrange[-1]) or (maxVel < minVel):
417 if (maxVel > velrange[-1]) or (maxVel < minVel):
418 print('maxVel: %.2f is out of the velocity range' % (maxVel))
418 print('maxVel: %.2f is out of the velocity range' % (maxVel))
419 print('maxVel is setting to %.2f' % (velrange[-1]))
419 print('maxVel is setting to %.2f' % (velrange[-1]))
420 maxVel = velrange[-1]
420 maxVel = velrange[-1]
421
421
422 # seleccion de indices para rango
422 # seleccion de indices para rango
423 minIndex = 0
423 minIndex = 0
424 maxIndex = 0
424 maxIndex = 0
425 heights = self.dataOut.heightList
425 heights = self.dataOut.heightList
426
426
427 inda = numpy.where(heights >= minHei)
427 inda = numpy.where(heights >= minHei)
428 indb = numpy.where(heights <= maxHei)
428 indb = numpy.where(heights <= maxHei)
429
429
430 try:
430 try:
431 minIndex = inda[0][0]
431 minIndex = inda[0][0]
432 except:
432 except:
433 minIndex = 0
433 minIndex = 0
434
434
435 try:
435 try:
436 maxIndex = indb[0][-1]
436 maxIndex = indb[0][-1]
437 except:
437 except:
438 maxIndex = len(heights)
438 maxIndex = len(heights)
439
439
440 if (minIndex < 0) or (minIndex > maxIndex):
440 if (minIndex < 0) or (minIndex > maxIndex):
441 raise ValueError("some value in (%d,%d) is not valid" % (
441 raise ValueError("some value in (%d,%d) is not valid" % (
442 minIndex, maxIndex))
442 minIndex, maxIndex))
443
443
444 if (maxIndex >= self.dataOut.nHeights):
444 if (maxIndex >= self.dataOut.nHeights):
445 maxIndex = self.dataOut.nHeights - 1
445 maxIndex = self.dataOut.nHeights - 1
446
446
447 # seleccion de indices para velocidades
447 # seleccion de indices para velocidades
448 indminvel = numpy.where(velrange >= minVel)
448 indminvel = numpy.where(velrange >= minVel)
449 indmaxvel = numpy.where(velrange <= maxVel)
449 indmaxvel = numpy.where(velrange <= maxVel)
450 try:
450 try:
451 minIndexVel = indminvel[0][0]
451 minIndexVel = indminvel[0][0]
452 except:
452 except:
453 minIndexVel = 0
453 minIndexVel = 0
454
454
455 try:
455 try:
456 maxIndexVel = indmaxvel[0][-1]
456 maxIndexVel = indmaxvel[0][-1]
457 except:
457 except:
458 maxIndexVel = len(velrange)
458 maxIndexVel = len(velrange)
459
459
460 # seleccion del espectro
460 # seleccion del espectro
461 data_spc = self.dataOut.data_spc[:,
461 data_spc = self.dataOut.data_spc[:,
462 minIndexVel:maxIndexVel + 1, minIndex:maxIndex + 1]
462 minIndexVel:maxIndexVel + 1, minIndex:maxIndex + 1]
463 # estimacion de ruido
463 # estimacion de ruido
464 noise = numpy.zeros(self.dataOut.nChannels)
464 noise = numpy.zeros(self.dataOut.nChannels)
465
465
466 for channel in range(self.dataOut.nChannels):
466 for channel in range(self.dataOut.nChannels):
467 daux = data_spc[channel, :, :]
467 daux = data_spc[channel, :, :]
468 sortdata = numpy.sort(daux, axis=None)
468 sortdata = numpy.sort(daux, axis=None)
469 noise[channel] = hildebrand_sekhon(sortdata, self.dataOut.nIncohInt)
469 noise[channel] = hildebrand_sekhon(sortdata, self.dataOut.nIncohInt)
470
470
471 self.dataOut.noise_estimation = noise.copy()
471 self.dataOut.noise_estimation = noise.copy()
472
472
473 return 1
473 return 1
474
474
475 class removeDC(Operation):
475 class removeDC(Operation):
476
476
477 def run(self, dataOut, mode=2):
477 def run(self, dataOut, mode=2):
478 self.dataOut = dataOut
478 self.dataOut = dataOut
479 jspectra = self.dataOut.data_spc
479 jspectra = self.dataOut.data_spc
480 jcspectra = self.dataOut.data_cspc
480 jcspectra = self.dataOut.data_cspc
481
481
482 num_chan = jspectra.shape[0]
482 num_chan = jspectra.shape[0]
483 num_hei = jspectra.shape[2]
483 num_hei = jspectra.shape[2]
484
484
485 if jcspectra is not None:
485 if jcspectra is not None:
486 jcspectraExist = True
486 jcspectraExist = True
487 num_pairs = jcspectra.shape[0]
487 num_pairs = jcspectra.shape[0]
488 else:
488 else:
489 jcspectraExist = False
489 jcspectraExist = False
490
490
491 freq_dc = int(jspectra.shape[1] / 2)
491 freq_dc = int(jspectra.shape[1] / 2)
492 ind_vel = numpy.array([-2, -1, 1, 2]) + freq_dc
492 ind_vel = numpy.array([-2, -1, 1, 2]) + freq_dc
493 ind_vel = ind_vel.astype(int)
493 ind_vel = ind_vel.astype(int)
494
494
495 if ind_vel[0] < 0:
495 if ind_vel[0] < 0:
496 ind_vel[list(range(0, 1))] = ind_vel[list(range(0, 1))] + self.num_prof
496 ind_vel[list(range(0, 1))] = ind_vel[list(range(0, 1))] + self.num_prof
497
497
498 if mode == 1:
498 if mode == 1:
499 jspectra[:, freq_dc, :] = (
499 jspectra[:, freq_dc, :] = (
500 jspectra[:, ind_vel[1], :] + jspectra[:, ind_vel[2], :]) / 2 # CORRECCION
500 jspectra[:, ind_vel[1], :] + jspectra[:, ind_vel[2], :]) / 2 # CORRECCION
501
501
502 if jcspectraExist:
502 if jcspectraExist:
503 jcspectra[:, freq_dc, :] = (
503 jcspectra[:, freq_dc, :] = (
504 jcspectra[:, ind_vel[1], :] + jcspectra[:, ind_vel[2], :]) / 2
504 jcspectra[:, ind_vel[1], :] + jcspectra[:, ind_vel[2], :]) / 2
505
505
506 if mode == 2:
506 if mode == 2:
507
507
508 vel = numpy.array([-2, -1, 1, 2])
508 vel = numpy.array([-2, -1, 1, 2])
509 xx = numpy.zeros([4, 4])
509 xx = numpy.zeros([4, 4])
510
510
511 for fil in range(4):
511 for fil in range(4):
512 xx[fil, :] = vel[fil]**numpy.asarray(list(range(4)))
512 xx[fil, :] = vel[fil]**numpy.asarray(list(range(4)))
513
513
514 xx_inv = numpy.linalg.inv(xx)
514 xx_inv = numpy.linalg.inv(xx)
515 xx_aux = xx_inv[0, :]
515 xx_aux = xx_inv[0, :]
516
516
517 for ich in range(num_chan):
517 for ich in range(num_chan):
518 yy = jspectra[ich, ind_vel, :]
518 yy = jspectra[ich, ind_vel, :]
519 jspectra[ich, freq_dc, :] = numpy.dot(xx_aux, yy)
519 jspectra[ich, freq_dc, :] = numpy.dot(xx_aux, yy)
520
520
521 junkid = jspectra[ich, freq_dc, :] <= 0
521 junkid = jspectra[ich, freq_dc, :] <= 0
522 cjunkid = sum(junkid)
522 cjunkid = sum(junkid)
523
523
524 if cjunkid.any():
524 if cjunkid.any():
525 jspectra[ich, freq_dc, junkid.nonzero()] = (
525 jspectra[ich, freq_dc, junkid.nonzero()] = (
526 jspectra[ich, ind_vel[1], junkid] + jspectra[ich, ind_vel[2], junkid]) / 2
526 jspectra[ich, ind_vel[1], junkid] + jspectra[ich, ind_vel[2], junkid]) / 2
527
527
528 if jcspectraExist:
528 if jcspectraExist:
529 for ip in range(num_pairs):
529 for ip in range(num_pairs):
530 yy = jcspectra[ip, ind_vel, :]
530 yy = jcspectra[ip, ind_vel, :]
531 jcspectra[ip, freq_dc, :] = numpy.dot(xx_aux, yy)
531 jcspectra[ip, freq_dc, :] = numpy.dot(xx_aux, yy)
532
532
533 self.dataOut.data_spc = jspectra
533 self.dataOut.data_spc = jspectra
534 self.dataOut.data_cspc = jcspectra
534 self.dataOut.data_cspc = jcspectra
535
535
536 return self.dataOut
536 return self.dataOut
537
537
538 class getNoiseB(Operation):
538 class getNoiseB(Operation):
539
539
540 __slots__ =('offset','warnings', 'isConfig', 'minIndex','maxIndex','minIndexFFT','maxIndexFFT')
540 __slots__ =('offset','warnings', 'isConfig', 'minIndex','maxIndex','minIndexFFT','maxIndexFFT')
541 def __init__(self):
541 def __init__(self):
542
542
543 Operation.__init__(self)
543 Operation.__init__(self)
544 self.isConfig = False
544 self.isConfig = False
545
545
546 def setup(self, offset=None, minHei=None, maxHei=None,minVel=None, maxVel=None, minFreq= None, maxFreq=None, warnings=False):
546 def setup(self, offset=None, minHei=None, maxHei=None,minVel=None, maxVel=None, minFreq= None, maxFreq=None, warnings=False):
547
547
548 self.warnings = warnings
548 self.warnings = warnings
549 if minHei == None:
549 if minHei == None:
550 minHei = self.dataOut.heightList[0]
550 minHei = self.dataOut.heightList[0]
551
551
552 if maxHei == None:
552 if maxHei == None:
553 maxHei = self.dataOut.heightList[-1]
553 maxHei = self.dataOut.heightList[-1]
554
554
555 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
555 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
556 if self.warnings:
556 if self.warnings:
557 print('minHei: %.2f is out of the heights range' % (minHei))
557 print('minHei: %.2f is out of the heights range' % (minHei))
558 print('minHei is setting to %.2f' % (self.dataOut.heightList[0]))
558 print('minHei is setting to %.2f' % (self.dataOut.heightList[0]))
559 minHei = self.dataOut.heightList[0]
559 minHei = self.dataOut.heightList[0]
560
560
561 if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei):
561 if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei):
562 if self.warnings:
562 if self.warnings:
563 print('maxHei: %.2f is out of the heights range' % (maxHei))
563 print('maxHei: %.2f is out of the heights range' % (maxHei))
564 print('maxHei is setting to %.2f' % (self.dataOut.heightList[-1]))
564 print('maxHei is setting to %.2f' % (self.dataOut.heightList[-1]))
565 maxHei = self.dataOut.heightList[-1]
565 maxHei = self.dataOut.heightList[-1]
566
566
567
567
568 #indices relativos a los puntos de fft, puede ser de acuerdo a velocidad o frecuencia
568 #indices relativos a los puntos de fft, puede ser de acuerdo a velocidad o frecuencia
569 minIndexFFT = 0
569 minIndexFFT = 0
570 maxIndexFFT = 0
570 maxIndexFFT = 0
571 # validacion de velocidades
571 # validacion de velocidades
572 indminPoint = None
572 indminPoint = None
573 indmaxPoint = None
573 indmaxPoint = None
574 if self.dataOut.type == 'Spectra':
574 if self.dataOut.type == 'Spectra':
575 if minVel == None and maxVel == None :
575 if minVel == None and maxVel == None :
576
576
577 freqrange = self.dataOut.getFreqRange(1)
577 freqrange = self.dataOut.getFreqRange(1)
578
578
579 if minFreq == None:
579 if minFreq == None:
580 minFreq = freqrange[0]
580 minFreq = freqrange[0]
581
581
582 if maxFreq == None:
582 if maxFreq == None:
583 maxFreq = freqrange[-1]
583 maxFreq = freqrange[-1]
584
584
585 if (minFreq < freqrange[0]) or (minFreq > maxFreq):
585 if (minFreq < freqrange[0]) or (minFreq > maxFreq):
586 if self.warnings:
586 if self.warnings:
587 print('minFreq: %.2f is out of the frequency range' % (minFreq))
587 print('minFreq: %.2f is out of the frequency range' % (minFreq))
588 print('minFreq is setting to %.2f' % (freqrange[0]))
588 print('minFreq is setting to %.2f' % (freqrange[0]))
589 minFreq = freqrange[0]
589 minFreq = freqrange[0]
590
590
591 if (maxFreq > freqrange[-1]) or (maxFreq < minFreq):
591 if (maxFreq > freqrange[-1]) or (maxFreq < minFreq):
592 if self.warnings:
592 if self.warnings:
593 print('maxFreq: %.2f is out of the frequency range' % (maxFreq))
593 print('maxFreq: %.2f is out of the frequency range' % (maxFreq))
594 print('maxFreq is setting to %.2f' % (freqrange[-1]))
594 print('maxFreq is setting to %.2f' % (freqrange[-1]))
595 maxFreq = freqrange[-1]
595 maxFreq = freqrange[-1]
596
596
597 indminPoint = numpy.where(freqrange >= minFreq)
597 indminPoint = numpy.where(freqrange >= minFreq)
598 indmaxPoint = numpy.where(freqrange <= maxFreq)
598 indmaxPoint = numpy.where(freqrange <= maxFreq)
599
599
600 else:
600 else:
601
601
602 velrange = self.dataOut.getVelRange(1)
602 velrange = self.dataOut.getVelRange(1)
603
603
604 if minVel == None:
604 if minVel == None:
605 minVel = velrange[0]
605 minVel = velrange[0]
606
606
607 if maxVel == None:
607 if maxVel == None:
608 maxVel = velrange[-1]
608 maxVel = velrange[-1]
609
609
610 if (minVel < velrange[0]) or (minVel > maxVel):
610 if (minVel < velrange[0]) or (minVel > maxVel):
611 if self.warnings:
611 if self.warnings:
612 print('minVel: %.2f is out of the velocity range' % (minVel))
612 print('minVel: %.2f is out of the velocity range' % (minVel))
613 print('minVel is setting to %.2f' % (velrange[0]))
613 print('minVel is setting to %.2f' % (velrange[0]))
614 minVel = velrange[0]
614 minVel = velrange[0]
615
615
616 if (maxVel > velrange[-1]) or (maxVel < minVel):
616 if (maxVel > velrange[-1]) or (maxVel < minVel):
617 if self.warnings:
617 if self.warnings:
618 print('maxVel: %.2f is out of the velocity range' % (maxVel))
618 print('maxVel: %.2f is out of the velocity range' % (maxVel))
619 print('maxVel is setting to %.2f' % (velrange[-1]))
619 print('maxVel is setting to %.2f' % (velrange[-1]))
620 maxVel = velrange[-1]
620 maxVel = velrange[-1]
621
621
622 indminPoint = numpy.where(velrange >= minVel)
622 indminPoint = numpy.where(velrange >= minVel)
623 indmaxPoint = numpy.where(velrange <= maxVel)
623 indmaxPoint = numpy.where(velrange <= maxVel)
624
624
625
625
626 # seleccion de indices para rango
626 # seleccion de indices para rango
627 minIndex = 0
627 minIndex = 0
628 maxIndex = 0
628 maxIndex = 0
629 heights = self.dataOut.heightList
629 heights = self.dataOut.heightList
630
630
631 inda = numpy.where(heights >= minHei)
631 inda = numpy.where(heights >= minHei)
632 indb = numpy.where(heights <= maxHei)
632 indb = numpy.where(heights <= maxHei)
633
633
634 try:
634 try:
635 minIndex = inda[0][0]
635 minIndex = inda[0][0]
636 except:
636 except:
637 minIndex = 0
637 minIndex = 0
638
638
639 try:
639 try:
640 maxIndex = indb[0][-1]
640 maxIndex = indb[0][-1]
641 except:
641 except:
642 maxIndex = len(heights)
642 maxIndex = len(heights)
643
643
644 if (minIndex < 0) or (minIndex > maxIndex):
644 if (minIndex < 0) or (minIndex > maxIndex):
645 raise ValueError("some value in (%d,%d) is not valid" % (
645 raise ValueError("some value in (%d,%d) is not valid" % (
646 minIndex, maxIndex))
646 minIndex, maxIndex))
647
647
648 if (maxIndex >= self.dataOut.nHeights):
648 if (maxIndex >= self.dataOut.nHeights):
649 maxIndex = self.dataOut.nHeights - 1
649 maxIndex = self.dataOut.nHeights - 1
650 #############################################################3
650 #############################################################3
651 # seleccion de indices para velocidades
651 # seleccion de indices para velocidades
652 if self.dataOut.type == 'Spectra':
652 if self.dataOut.type == 'Spectra':
653 try:
653 try:
654 minIndexFFT = indminPoint[0][0]
654 minIndexFFT = indminPoint[0][0]
655 except:
655 except:
656 minIndexFFT = 0
656 minIndexFFT = 0
657
657
658 try:
658 try:
659 maxIndexFFT = indmaxPoint[0][-1]
659 maxIndexFFT = indmaxPoint[0][-1]
660 except:
660 except:
661 maxIndexFFT = len( self.dataOut.getFreqRange(1))
661 maxIndexFFT = len( self.dataOut.getFreqRange(1))
662
662
663 self.minIndex, self.maxIndex, self.minIndexFFT, self.maxIndexFFT = minIndex, maxIndex, minIndexFFT, maxIndexFFT
663 self.minIndex, self.maxIndex, self.minIndexFFT, self.maxIndexFFT = minIndex, maxIndex, minIndexFFT, maxIndexFFT
664 self.isConfig = True
664 self.isConfig = True
665 if offset!=None:
665 if offset!=None:
666 self.offset = 10**(offset/10)
666 self.offset = 10**(offset/10)
667 #print("config getNoise Done")
667 #print("config getNoiseB Done")
668
668
669 def run(self, dataOut, offset=None, minHei=None, maxHei=None,minVel=None, maxVel=None, minFreq= None, maxFreq=None, warnings=False):
669 def run(self, dataOut, offset=None, minHei=None, maxHei=None,minVel=None, maxVel=None, minFreq= None, maxFreq=None, warnings=False):
670 self.dataOut = dataOut
670 self.dataOut = dataOut
671
671
672 if not self.isConfig:
672 if not self.isConfig:
673 self.setup(offset, minHei, maxHei,minVel, maxVel, minFreq, maxFreq, warnings)
673 self.setup(offset, minHei, maxHei,minVel, maxVel, minFreq, maxFreq, warnings)
674
674
675 self.dataOut.noise_estimation = None
675 self.dataOut.noise_estimation = None
676 noise = None
676 noise = None
677 if self.dataOut.type == 'Voltage':
677 if self.dataOut.type == 'Voltage':
678 noise = self.dataOut.getNoise(ymin_index=self.minIndex, ymax_index=self.maxIndex)
678 noise = self.dataOut.getNoise(ymin_index=self.minIndex, ymax_index=self.maxIndex)
679 #print(minIndex, maxIndex,minIndexVel, maxIndexVel)
679 #print(minIndex, maxIndex,minIndexVel, maxIndexVel)
680 elif self.dataOut.type == 'Spectra':
680 elif self.dataOut.type == 'Spectra':
681
681 #print(self.minIndex, self.maxIndex,self.minIndexFFT, self.maxIndexFFT, self.dataOut.nIncohInt)
682 noise = numpy.zeros( self.dataOut.nChannels)
682 noise = numpy.zeros( self.dataOut.nChannels)
683 norm = 1
683 for channel in range( self.dataOut.nChannels):
684 for channel in range( self.dataOut.nChannels):
684 norm = self.dataOut.max_nIncohInt/self.dataOut.nIncohInt[channel, self.minIndex:self.maxIndex]
685 if not hasattr(self.dataOut.nIncohInt,'__len__'):
685 #print("norm nIncoh: ", norm )
686 norm = 1
687 else:
688 norm = self.dataOut.max_nIncohInt/self.dataOut.nIncohInt[channel, self.minIndex:self.maxIndex]
689 #print("norm nIncoh: ", norm ,self.dataOut.data_spc.shape)
686 daux = self.dataOut.data_spc[channel,self.minIndexFFT:self.maxIndexFFT, self.minIndex:self.maxIndex]
690 daux = self.dataOut.data_spc[channel,self.minIndexFFT:self.maxIndexFFT, self.minIndex:self.maxIndex]
687 daux = numpy.multiply(daux, norm)
691 daux = numpy.multiply(daux, norm)
688 #print("offset: ", self.offset, 10*numpy.log10(self.offset))
692 #print("offset: ", self.offset, 10*numpy.log10(self.offset))
689 #noise[channel] = self.getNoiseByMean(daux)/self.offset
693 #noise[channel] = self.getNoiseByMean(daux)/self.offset
694 #print(daux.shape, daux)
690 noise[channel] = self.getNoiseByHS(daux, self.dataOut.max_nIncohInt)/self.offset
695 noise[channel] = self.getNoiseByHS(daux, self.dataOut.max_nIncohInt)/self.offset
691
696
697 # data = numpy.mean(daux,axis=1)
698 # sortdata = numpy.sort(data, axis=None)
699 # noise[channel] = _noise.hildebrand_sekhon(sortdata, self.dataOut.max_nIncohInt)/self.offset
700
692 #noise = self.dataOut.getNoise(xmin_index=self.minIndexFFT, xmax_index=self.maxIndexFFT, ymin_index=self.minIndex, ymax_index=self.maxIndex)
701 #noise = self.dataOut.getNoise(xmin_index=self.minIndexFFT, xmax_index=self.maxIndexFFT, ymin_index=self.minIndex, ymax_index=self.maxIndex)
693 else:
702 else:
694 noise = self.dataOut.getNoise(xmin_index=self.minIndexFFT, xmax_index=self.maxIndexFFT, ymin_index=self.minIndex, ymax_index=self.maxIndex)
703 noise = self.dataOut.getNoise(xmin_index=self.minIndexFFT, xmax_index=self.maxIndexFFT, ymin_index=self.minIndex, ymax_index=self.maxIndex)
695 self.dataOut.noise_estimation = noise.copy() # dataOut.noise
704 self.dataOut.noise_estimation = noise.copy() # dataOut.noise
696 #print("2: ",10*numpy.log10(self.dataOut.noise_estimation/64))
705 #print("2: ",10*numpy.log10(self.dataOut.noise_estimation/64))
697
706
698 #print(self.dataOut.flagNoData)
707 #print(self.dataOut.flagNoData)
708 print("getNoise Done")
699 return self.dataOut
709 return self.dataOut
700
710
701 def getNoiseByMean(self,data):
711 def getNoiseByMean(self,data):
702 #data debe estar ordenado
712 #data debe estar ordenado
703 data = numpy.mean(data,axis=1)
713 data = numpy.mean(data,axis=1)
704 sortdata = numpy.sort(data, axis=None)
714 sortdata = numpy.sort(data, axis=None)
705 #sortID=data.argsort()
715 #sortID=data.argsort()
706 #print(data.shape)
716 #print(data.shape)
707
717
708 pnoise = None
718 pnoise = None
709 j = 0
719 j = 0
710
720
711 mean = numpy.mean(sortdata)
721 mean = numpy.mean(sortdata)
712 min = numpy.min(sortdata)
722 min = numpy.min(sortdata)
713 delta = mean - min
723 delta = mean - min
714 indexes = numpy.where(sortdata > (mean+delta))[0] #only array of indexes
724 indexes = numpy.where(sortdata > (mean+delta))[0] #only array of indexes
715 #print(len(indexes))
725 #print(len(indexes))
716 if len(indexes)==0:
726 if len(indexes)==0:
717 pnoise = numpy.mean(sortdata)
727 pnoise = numpy.mean(sortdata)
718 else:
728 else:
719 j = indexes[0]
729 j = indexes[0]
720 pnoise = numpy.mean(sortdata[0:j])
730 pnoise = numpy.mean(sortdata[0:j])
721
731
722 # from matplotlib import pyplot as plt
732 # from matplotlib import pyplot as plt
723 # plt.plot(sortdata)
733 # plt.plot(sortdata)
724 # plt.vlines(j,(pnoise-delta),(pnoise+delta), color='r')
734 # plt.vlines(j,(pnoise-delta),(pnoise+delta), color='r')
725 # plt.show()
735 # plt.show()
726 #print("noise: ", 10*numpy.log10(pnoise))
736 #print("noise: ", 10*numpy.log10(pnoise))
727 return pnoise
737 return pnoise
728
738
729 def getNoiseByHS(self,data, navg):
739 def getNoiseByHS(self,data, navg):
730 #data debe estar ordenado
740 #data debe estar ordenado
731 #data = numpy.mean(data,axis=1)
741 #data = numpy.mean(data,axis=1)
732 sortdata = numpy.sort(data, axis=None)
742 sortdata = numpy.sort(data, axis=None)
733
743
734 lenOfData = len(sortdata)
744 lenOfData = len(sortdata)
735 nums_min = lenOfData*0.05
745 nums_min = lenOfData*0.05
736
746
737 if nums_min <= 5:
747 if nums_min <= 5:
738
748
739 nums_min = 5
749 nums_min = 5
740
750
741 sump = 0.
751 sump = 0.
742 sumq = 0.
752 sumq = 0.
743
753
744 j = 0
754 j = 0
745 cont = 1
755 cont = 1
746
756
747 while((cont == 1)and(j < lenOfData)):
757 while((cont == 1)and(j < lenOfData)):
748
758
749 sump += sortdata[j]
759 sump += sortdata[j]
750 sumq += sortdata[j]**2
760 sumq += sortdata[j]**2
751 #sumq -= sump**2
761 #sumq -= sump**2
752 if j > nums_min:
762 if j > nums_min:
753 rtest = float(j)/(j-1) + 1.0/0.1
763 rtest = float(j)/(j-1) + 1.0/0.1
754 #if ((sumq*j) > (sump**2)):
764 #if ((sumq*j) > (sump**2)):
755 if ((sumq*j) > (rtest*sump**2)):
765 if ((sumq*j) > (rtest*sump**2)):
756 j = j - 1
766 j = j - 1
757 sump = sump - sortdata[j]
767 sump = sump - sortdata[j]
758 sumq = sumq - sortdata[j]**2
768 sumq = sumq - sortdata[j]**2
759 cont = 0
769 cont = 0
760
770
761 j += 1
771 j += 1
762
772
763 lnoise = sump / j
773 lnoise = sump / j
764
774
765 return lnoise
775 return lnoise
766
776
767
777
768
778
769 def fit_func( x, a0, a1, a2): #, a3, a4, a5):
779 def fit_func( x, a0, a1, a2): #, a3, a4, a5):
770 z = (x - a1) / a2
780 z = (x - a1) / a2
771 y = a0 * numpy.exp(-z**2 / a2) #+ a3 + a4 * x + a5 * x**2
781 y = a0 * numpy.exp(-z**2 / a2) #+ a3 + a4 * x + a5 * x**2
772 return y
782 return y
773
783
774
784
775 class CleanRayleigh(Operation):
785 class CleanRayleigh(Operation):
776
786
777 def __init__(self):
787 def __init__(self):
778
788
779 Operation.__init__(self)
789 Operation.__init__(self)
780 self.i=0
790 self.i=0
781 self.isConfig = False
791 self.isConfig = False
782 self.__dataReady = False
792 self.__dataReady = False
783 self.__profIndex = 0
793 self.__profIndex = 0
784 self.byTime = False
794 self.byTime = False
785 self.byProfiles = False
795 self.byProfiles = False
786
796
787 self.bloques = None
797 self.bloques = None
788 self.bloque0 = None
798 self.bloque0 = None
789
799
790 self.index = 0
800 self.index = 0
791
801
792 self.buffer = 0
802 self.buffer = 0
793 self.buffer2 = 0
803 self.buffer2 = 0
794 self.buffer3 = 0
804 self.buffer3 = 0
795
805
796
806
797 def setup(self,dataOut,min_hei,max_hei,n, timeInterval,factor_stdv):
807 def setup(self,dataOut,min_hei,max_hei,n, timeInterval,factor_stdv):
798
808
799 self.nChannels = dataOut.nChannels
809 self.nChannels = dataOut.nChannels
800 self.nProf = dataOut.nProfiles
810 self.nProf = dataOut.nProfiles
801 self.nPairs = dataOut.data_cspc.shape[0]
811 self.nPairs = dataOut.data_cspc.shape[0]
802 self.pairsArray = numpy.array(dataOut.pairsList)
812 self.pairsArray = numpy.array(dataOut.pairsList)
803 self.spectra = dataOut.data_spc
813 self.spectra = dataOut.data_spc
804 self.cspectra = dataOut.data_cspc
814 self.cspectra = dataOut.data_cspc
805 self.heights = dataOut.heightList #alturas totales
815 self.heights = dataOut.heightList #alturas totales
806 self.nHeights = len(self.heights)
816 self.nHeights = len(self.heights)
807 self.min_hei = min_hei
817 self.min_hei = min_hei
808 self.max_hei = max_hei
818 self.max_hei = max_hei
809 if (self.min_hei == None):
819 if (self.min_hei == None):
810 self.min_hei = 0
820 self.min_hei = 0
811 if (self.max_hei == None):
821 if (self.max_hei == None):
812 self.max_hei = dataOut.heightList[-1]
822 self.max_hei = dataOut.heightList[-1]
813 self.hval = ((self.max_hei>=self.heights) & (self.heights >= self.min_hei)).nonzero()
823 self.hval = ((self.max_hei>=self.heights) & (self.heights >= self.min_hei)).nonzero()
814 self.heightsClean = self.heights[self.hval] #alturas filtradas
824 self.heightsClean = self.heights[self.hval] #alturas filtradas
815 self.hval = self.hval[0] # forma (N,), an solo N elementos -> Indices de alturas
825 self.hval = self.hval[0] # forma (N,), an solo N elementos -> Indices de alturas
816 self.nHeightsClean = len(self.heightsClean)
826 self.nHeightsClean = len(self.heightsClean)
817 self.channels = dataOut.channelList
827 self.channels = dataOut.channelList
818 self.nChan = len(self.channels)
828 self.nChan = len(self.channels)
819 self.nIncohInt = dataOut.nIncohInt
829 self.nIncohInt = dataOut.nIncohInt
820 self.__initime = dataOut.utctime
830 self.__initime = dataOut.utctime
821 self.maxAltInd = self.hval[-1]+1
831 self.maxAltInd = self.hval[-1]+1
822 self.minAltInd = self.hval[0]
832 self.minAltInd = self.hval[0]
823
833
824 self.crosspairs = dataOut.pairsList
834 self.crosspairs = dataOut.pairsList
825 self.nPairs = len(self.crosspairs)
835 self.nPairs = len(self.crosspairs)
826 self.normFactor = dataOut.normFactor
836 self.normFactor = dataOut.normFactor
827 self.nFFTPoints = dataOut.nFFTPoints
837 self.nFFTPoints = dataOut.nFFTPoints
828 self.ippSeconds = dataOut.ippSeconds
838 self.ippSeconds = dataOut.ippSeconds
829 self.currentTime = self.__initime
839 self.currentTime = self.__initime
830 self.pairsArray = numpy.array(dataOut.pairsList)
840 self.pairsArray = numpy.array(dataOut.pairsList)
831 self.factor_stdv = factor_stdv
841 self.factor_stdv = factor_stdv
832
842
833 if n != None :
843 if n != None :
834 self.byProfiles = True
844 self.byProfiles = True
835 self.nIntProfiles = n
845 self.nIntProfiles = n
836 else:
846 else:
837 self.__integrationtime = timeInterval
847 self.__integrationtime = timeInterval
838
848
839 self.__dataReady = False
849 self.__dataReady = False
840 self.isConfig = True
850 self.isConfig = True
841
851
842
852
843
853
844 def run(self, dataOut,min_hei=None,max_hei=None, n=None, timeInterval=10,factor_stdv=2.5):
854 def run(self, dataOut,min_hei=None,max_hei=None, n=None, timeInterval=10,factor_stdv=2.5):
845 #print("runing cleanRayleigh")
855 #print("runing cleanRayleigh")
846 if not self.isConfig :
856 if not self.isConfig :
847
857
848 self.setup(dataOut, min_hei,max_hei,n,timeInterval,factor_stdv)
858 self.setup(dataOut, min_hei,max_hei,n,timeInterval,factor_stdv)
849
859
850 tini=dataOut.utctime
860 tini=dataOut.utctime
851
861
852 if self.byProfiles:
862 if self.byProfiles:
853 if self.__profIndex == self.nIntProfiles:
863 if self.__profIndex == self.nIntProfiles:
854 self.__dataReady = True
864 self.__dataReady = True
855 else:
865 else:
856 if (tini - self.__initime) >= self.__integrationtime:
866 if (tini - self.__initime) >= self.__integrationtime:
857
867
858 self.__dataReady = True
868 self.__dataReady = True
859 self.__initime = tini
869 self.__initime = tini
860
870
861 #if (tini.tm_min % 2) == 0 and (tini.tm_sec < 5 and self.fint==0):
871 #if (tini.tm_min % 2) == 0 and (tini.tm_sec < 5 and self.fint==0):
862
872
863 if self.__dataReady:
873 if self.__dataReady:
864
874
865 self.__profIndex = 0
875 self.__profIndex = 0
866 jspc = self.buffer
876 jspc = self.buffer
867 jcspc = self.buffer2
877 jcspc = self.buffer2
868 #jnoise = self.buffer3
878 #jnoise = self.buffer3
869 self.buffer = dataOut.data_spc
879 self.buffer = dataOut.data_spc
870 self.buffer2 = dataOut.data_cspc
880 self.buffer2 = dataOut.data_cspc
871 #self.buffer3 = dataOut.noise
881 #self.buffer3 = dataOut.noise
872 self.currentTime = dataOut.utctime
882 self.currentTime = dataOut.utctime
873 if numpy.any(jspc) :
883 if numpy.any(jspc) :
874 #print( jspc.shape, jcspc.shape)
884 #print( jspc.shape, jcspc.shape)
875 jspc = numpy.reshape(jspc,(int(len(jspc)/self.nChannels),self.nChannels,self.nFFTPoints,self.nHeights))
885 jspc = numpy.reshape(jspc,(int(len(jspc)/self.nChannels),self.nChannels,self.nFFTPoints,self.nHeights))
876 try:
886 try:
877 jcspc= numpy.reshape(jcspc,(int(len(jcspc)/self.nPairs),self.nPairs,self.nFFTPoints,self.nHeights))
887 jcspc= numpy.reshape(jcspc,(int(len(jcspc)/self.nPairs),self.nPairs,self.nFFTPoints,self.nHeights))
878 except:
888 except:
879 print("no cspc")
889 print("no cspc")
880 self.__dataReady = False
890 self.__dataReady = False
881 #print( jspc.shape, jcspc.shape)
891 #print( jspc.shape, jcspc.shape)
882 dataOut.flagNoData = False
892 dataOut.flagNoData = False
883 else:
893 else:
884 dataOut.flagNoData = True
894 dataOut.flagNoData = True
885 self.__dataReady = False
895 self.__dataReady = False
886 return dataOut
896 return dataOut
887 else:
897 else:
888 #print( len(self.buffer))
898 #print( len(self.buffer))
889 if numpy.any(self.buffer):
899 if numpy.any(self.buffer):
890 self.buffer = numpy.concatenate((self.buffer,dataOut.data_spc), axis=0)
900 self.buffer = numpy.concatenate((self.buffer,dataOut.data_spc), axis=0)
891 try:
901 try:
892 self.buffer2 = numpy.concatenate((self.buffer2,dataOut.data_cspc), axis=0)
902 self.buffer2 = numpy.concatenate((self.buffer2,dataOut.data_cspc), axis=0)
893 self.buffer3 += dataOut.data_dc
903 self.buffer3 += dataOut.data_dc
894 except:
904 except:
895 pass
905 pass
896 else:
906 else:
897 self.buffer = dataOut.data_spc
907 self.buffer = dataOut.data_spc
898 self.buffer2 = dataOut.data_cspc
908 self.buffer2 = dataOut.data_cspc
899 self.buffer3 = dataOut.data_dc
909 self.buffer3 = dataOut.data_dc
900 #print self.index, self.fint
910 #print self.index, self.fint
901 #print self.buffer2.shape
911 #print self.buffer2.shape
902 dataOut.flagNoData = True ## NOTE: ?? revisar LUEGO
912 dataOut.flagNoData = True ## NOTE: ?? revisar LUEGO
903 self.__profIndex += 1
913 self.__profIndex += 1
904 return dataOut ## NOTE: REV
914 return dataOut ## NOTE: REV
905
915
906
916
907 #index = tini.tm_hour*12+tini.tm_min/5
917 #index = tini.tm_hour*12+tini.tm_min/5
908 '''
918 '''
909 #REVISAR
919 #REVISAR
910 '''
920 '''
911 # jspc = jspc/self.nFFTPoints/self.normFactor
921 # jspc = jspc/self.nFFTPoints/self.normFactor
912 # jcspc = jcspc/self.nFFTPoints/self.normFactor
922 # jcspc = jcspc/self.nFFTPoints/self.normFactor
913
923
914
924
915
925
916 tmp_spectra,tmp_cspectra = self.cleanRayleigh(dataOut,jspc,jcspc,self.factor_stdv)
926 tmp_spectra,tmp_cspectra = self.cleanRayleigh(dataOut,jspc,jcspc,self.factor_stdv)
917 dataOut.data_spc = tmp_spectra
927 dataOut.data_spc = tmp_spectra
918 dataOut.data_cspc = tmp_cspectra
928 dataOut.data_cspc = tmp_cspectra
919
929
920 #dataOut.data_spc,dataOut.data_cspc = self.cleanRayleigh(dataOut,jspc,jcspc,self.factor_stdv)
930 #dataOut.data_spc,dataOut.data_cspc = self.cleanRayleigh(dataOut,jspc,jcspc,self.factor_stdv)
921
931
922 dataOut.data_dc = self.buffer3
932 dataOut.data_dc = self.buffer3
923 dataOut.nIncohInt *= self.nIntProfiles
933 dataOut.nIncohInt *= self.nIntProfiles
924 dataOut.max_nIncohInt = self.nIntProfiles
934 dataOut.max_nIncohInt = self.nIntProfiles
925 dataOut.utctime = self.currentTime #tiempo promediado
935 dataOut.utctime = self.currentTime #tiempo promediado
926 #print("Time: ",time.localtime(dataOut.utctime))
936 #print("Time: ",time.localtime(dataOut.utctime))
927 # dataOut.data_spc = sat_spectra
937 # dataOut.data_spc = sat_spectra
928 # dataOut.data_cspc = sat_cspectra
938 # dataOut.data_cspc = sat_cspectra
929 self.buffer = 0
939 self.buffer = 0
930 self.buffer2 = 0
940 self.buffer2 = 0
931 self.buffer3 = 0
941 self.buffer3 = 0
932
942
933 return dataOut
943 return dataOut
934
944
935 def cleanRayleigh(self,dataOut,spectra,cspectra,factor_stdv):
945 def cleanRayleigh(self,dataOut,spectra,cspectra,factor_stdv):
936 print("OP cleanRayleigh")
946 print("OP cleanRayleigh")
937 #import matplotlib.pyplot as plt
947 #import matplotlib.pyplot as plt
938 #for k in range(149):
948 #for k in range(149):
939 #channelsProcssd = []
949 #channelsProcssd = []
940 #channelA_ok = False
950 #channelA_ok = False
941 #rfunc = cspectra.copy() #self.bloques
951 #rfunc = cspectra.copy() #self.bloques
942 rfunc = spectra.copy()
952 rfunc = spectra.copy()
943 #rfunc = cspectra
953 #rfunc = cspectra
944 #val_spc = spectra*0.0 #self.bloque0*0.0
954 #val_spc = spectra*0.0 #self.bloque0*0.0
945 #val_cspc = cspectra*0.0 #self.bloques*0.0
955 #val_cspc = cspectra*0.0 #self.bloques*0.0
946 #in_sat_spectra = spectra.copy() #self.bloque0
956 #in_sat_spectra = spectra.copy() #self.bloque0
947 #in_sat_cspectra = cspectra.copy() #self.bloques
957 #in_sat_cspectra = cspectra.copy() #self.bloques
948
958
949
959
950 ###ONLY FOR TEST:
960 ###ONLY FOR TEST:
951 raxs = math.ceil(math.sqrt(self.nPairs))
961 raxs = math.ceil(math.sqrt(self.nPairs))
952 if raxs == 0:
962 if raxs == 0:
953 raxs = 1
963 raxs = 1
954 caxs = math.ceil(self.nPairs/raxs)
964 caxs = math.ceil(self.nPairs/raxs)
955 if self.nPairs <4:
965 if self.nPairs <4:
956 raxs = 2
966 raxs = 2
957 caxs = 2
967 caxs = 2
958 #print(raxs, caxs)
968 #print(raxs, caxs)
959 fft_rev = 14 #nFFT to plot
969 fft_rev = 14 #nFFT to plot
960 hei_rev = ((self.heights >= 550) & (self.heights <= 551)).nonzero() #hei to plot
970 hei_rev = ((self.heights >= 550) & (self.heights <= 551)).nonzero() #hei to plot
961 hei_rev = hei_rev[0]
971 hei_rev = hei_rev[0]
962 #print(hei_rev)
972 #print(hei_rev)
963
973
964 #print numpy.absolute(rfunc[:,0,0,14])
974 #print numpy.absolute(rfunc[:,0,0,14])
965
975
966 gauss_fit, covariance = None, None
976 gauss_fit, covariance = None, None
967 for ih in range(self.minAltInd,self.maxAltInd):
977 for ih in range(self.minAltInd,self.maxAltInd):
968 for ifreq in range(self.nFFTPoints):
978 for ifreq in range(self.nFFTPoints):
969 '''
979 '''
970 ###ONLY FOR TEST:
980 ###ONLY FOR TEST:
971 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
981 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
972 fig, axs = plt.subplots(raxs, caxs)
982 fig, axs = plt.subplots(raxs, caxs)
973 fig2, axs2 = plt.subplots(raxs, caxs)
983 fig2, axs2 = plt.subplots(raxs, caxs)
974 col_ax = 0
984 col_ax = 0
975 row_ax = 0
985 row_ax = 0
976 '''
986 '''
977 #print(self.nPairs)
987 #print(self.nPairs)
978 for ii in range(self.nChan): #PARES DE CANALES SELF y CROSS
988 for ii in range(self.nChan): #PARES DE CANALES SELF y CROSS
979 # if self.crosspairs[ii][1]-self.crosspairs[ii][0] > 1: # APLICAR SOLO EN PARES CONTIGUOS
989 # if self.crosspairs[ii][1]-self.crosspairs[ii][0] > 1: # APLICAR SOLO EN PARES CONTIGUOS
980 # continue
990 # continue
981 # if not self.crosspairs[ii][0] in channelsProcssd:
991 # if not self.crosspairs[ii][0] in channelsProcssd:
982 # channelA_ok = True
992 # channelA_ok = True
983 #print("pair: ",self.crosspairs[ii])
993 #print("pair: ",self.crosspairs[ii])
984 '''
994 '''
985 ###ONLY FOR TEST:
995 ###ONLY FOR TEST:
986 if (col_ax%caxs==0 and col_ax!=0 and self.nPairs !=1):
996 if (col_ax%caxs==0 and col_ax!=0 and self.nPairs !=1):
987 col_ax = 0
997 col_ax = 0
988 row_ax += 1
998 row_ax += 1
989 '''
999 '''
990 func2clean = 10*numpy.log10(numpy.absolute(rfunc[:,ii,ifreq,ih])) #Potencia?
1000 func2clean = 10*numpy.log10(numpy.absolute(rfunc[:,ii,ifreq,ih])) #Potencia?
991 #print(func2clean.shape)
1001 #print(func2clean.shape)
992 val = (numpy.isfinite(func2clean)==True).nonzero()
1002 val = (numpy.isfinite(func2clean)==True).nonzero()
993
1003
994 if len(val)>0: #limitador
1004 if len(val)>0: #limitador
995 min_val = numpy.around(numpy.amin(func2clean)-2) #> (-40)
1005 min_val = numpy.around(numpy.amin(func2clean)-2) #> (-40)
996 if min_val <= -40 :
1006 if min_val <= -40 :
997 min_val = -40
1007 min_val = -40
998 max_val = numpy.around(numpy.amax(func2clean)+2) #< 200
1008 max_val = numpy.around(numpy.amax(func2clean)+2) #< 200
999 if max_val >= 200 :
1009 if max_val >= 200 :
1000 max_val = 200
1010 max_val = 200
1001 #print min_val, max_val
1011 #print min_val, max_val
1002 step = 1
1012 step = 1
1003 #print("Getting bins and the histogram")
1013 #print("Getting bins and the histogram")
1004 x_dist = min_val + numpy.arange(1 + ((max_val-(min_val))/step))*step
1014 x_dist = min_val + numpy.arange(1 + ((max_val-(min_val))/step))*step
1005 y_dist,binstep = numpy.histogram(func2clean,bins=range(int(min_val),int(max_val+2),step))
1015 y_dist,binstep = numpy.histogram(func2clean,bins=range(int(min_val),int(max_val+2),step))
1006 #print(len(y_dist),len(binstep[:-1]))
1016 #print(len(y_dist),len(binstep[:-1]))
1007 #print(row_ax,col_ax, " ..")
1017 #print(row_ax,col_ax, " ..")
1008 #print(self.pairsArray[ii][0],self.pairsArray[ii][1])
1018 #print(self.pairsArray[ii][0],self.pairsArray[ii][1])
1009 mean = numpy.sum(x_dist * y_dist) / numpy.sum(y_dist)
1019 mean = numpy.sum(x_dist * y_dist) / numpy.sum(y_dist)
1010 sigma = numpy.sqrt(numpy.sum(y_dist * (x_dist - mean)**2) / numpy.sum(y_dist))
1020 sigma = numpy.sqrt(numpy.sum(y_dist * (x_dist - mean)**2) / numpy.sum(y_dist))
1011 parg = [numpy.amax(y_dist),mean,sigma]
1021 parg = [numpy.amax(y_dist),mean,sigma]
1012
1022
1013 newY = None
1023 newY = None
1014
1024
1015 try :
1025 try :
1016 gauss_fit, covariance = curve_fit(fit_func, x_dist, y_dist,p0=parg)
1026 gauss_fit, covariance = curve_fit(fit_func, x_dist, y_dist,p0=parg)
1017 mode = gauss_fit[1]
1027 mode = gauss_fit[1]
1018 stdv = gauss_fit[2]
1028 stdv = gauss_fit[2]
1019 #print(" FIT OK",gauss_fit)
1029 #print(" FIT OK",gauss_fit)
1020 '''
1030 '''
1021 ###ONLY FOR TEST:
1031 ###ONLY FOR TEST:
1022 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
1032 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
1023 newY = fit_func(x_dist,gauss_fit[0],gauss_fit[1],gauss_fit[2])
1033 newY = fit_func(x_dist,gauss_fit[0],gauss_fit[1],gauss_fit[2])
1024 axs[row_ax,col_ax].plot(binstep[:-1],y_dist,color='green')
1034 axs[row_ax,col_ax].plot(binstep[:-1],y_dist,color='green')
1025 axs[row_ax,col_ax].plot(binstep[:-1],newY,color='red')
1035 axs[row_ax,col_ax].plot(binstep[:-1],newY,color='red')
1026 axs[row_ax,col_ax].set_title("CH "+str(self.channels[ii]))
1036 axs[row_ax,col_ax].set_title("CH "+str(self.channels[ii]))
1027 '''
1037 '''
1028 except:
1038 except:
1029 mode = mean
1039 mode = mean
1030 stdv = sigma
1040 stdv = sigma
1031 #print("FIT FAIL")
1041 #print("FIT FAIL")
1032 #continue
1042 #continue
1033
1043
1034
1044
1035 #print(mode,stdv)
1045 #print(mode,stdv)
1036 #Removing echoes greater than mode + std_factor*stdv
1046 #Removing echoes greater than mode + std_factor*stdv
1037 noval = (abs(func2clean - mode)>=(factor_stdv*stdv)).nonzero()
1047 noval = (abs(func2clean - mode)>=(factor_stdv*stdv)).nonzero()
1038 #noval tiene los indices que se van a remover
1048 #noval tiene los indices que se van a remover
1039 #print("Chan ",ii," novals: ",len(noval[0]))
1049 #print("Chan ",ii," novals: ",len(noval[0]))
1040 if len(noval[0]) > 0: #forma de array (N,) es igual a longitud (N)
1050 if len(noval[0]) > 0: #forma de array (N,) es igual a longitud (N)
1041 novall = ((func2clean - mode) >= (factor_stdv*stdv)).nonzero()
1051 novall = ((func2clean - mode) >= (factor_stdv*stdv)).nonzero()
1042 #print(novall)
1052 #print(novall)
1043 #print(" ",self.pairsArray[ii])
1053 #print(" ",self.pairsArray[ii])
1044 #cross_pairs = self.pairsArray[ii]
1054 #cross_pairs = self.pairsArray[ii]
1045 #Getting coherent echoes which are removed.
1055 #Getting coherent echoes which are removed.
1046 # if len(novall[0]) > 0:
1056 # if len(novall[0]) > 0:
1047 #
1057 #
1048 # val_spc[novall[0],cross_pairs[0],ifreq,ih] = 1
1058 # val_spc[novall[0],cross_pairs[0],ifreq,ih] = 1
1049 # val_spc[novall[0],cross_pairs[1],ifreq,ih] = 1
1059 # val_spc[novall[0],cross_pairs[1],ifreq,ih] = 1
1050 # val_cspc[novall[0],ii,ifreq,ih] = 1
1060 # val_cspc[novall[0],ii,ifreq,ih] = 1
1051 #print("OUT NOVALL 1")
1061 #print("OUT NOVALL 1")
1052 try:
1062 try:
1053 pair = (self.channels[ii],self.channels[ii + 1])
1063 pair = (self.channels[ii],self.channels[ii + 1])
1054 except:
1064 except:
1055 pair = (99,99)
1065 pair = (99,99)
1056 #print("par ", pair)
1066 #print("par ", pair)
1057 if ( pair in self.crosspairs):
1067 if ( pair in self.crosspairs):
1058 q = self.crosspairs.index(pair)
1068 q = self.crosspairs.index(pair)
1059 #print("estΓ‘ aqui: ", q, (ii,ii + 1))
1069 #print("estΓ‘ aqui: ", q, (ii,ii + 1))
1060 new_a = numpy.delete(cspectra[:,q,ifreq,ih], noval[0])
1070 new_a = numpy.delete(cspectra[:,q,ifreq,ih], noval[0])
1061 cspectra[noval,q,ifreq,ih] = numpy.mean(new_a) #mean CrossSpectra
1071 cspectra[noval,q,ifreq,ih] = numpy.mean(new_a) #mean CrossSpectra
1062
1072
1063 #if channelA_ok:
1073 #if channelA_ok:
1064 #chA = self.channels.index(cross_pairs[0])
1074 #chA = self.channels.index(cross_pairs[0])
1065 new_b = numpy.delete(spectra[:,ii,ifreq,ih], noval[0])
1075 new_b = numpy.delete(spectra[:,ii,ifreq,ih], noval[0])
1066 spectra[noval,ii,ifreq,ih] = numpy.mean(new_b) #mean Spectra Pair A
1076 spectra[noval,ii,ifreq,ih] = numpy.mean(new_b) #mean Spectra Pair A
1067 #channelA_ok = False
1077 #channelA_ok = False
1068
1078
1069 # chB = self.channels.index(cross_pairs[1])
1079 # chB = self.channels.index(cross_pairs[1])
1070 # new_c = numpy.delete(spectra[:,chB,ifreq,ih], noval[0])
1080 # new_c = numpy.delete(spectra[:,chB,ifreq,ih], noval[0])
1071 # spectra[noval,chB,ifreq,ih] = numpy.mean(new_c) #mean Spectra Pair B
1081 # spectra[noval,chB,ifreq,ih] = numpy.mean(new_c) #mean Spectra Pair B
1072 #
1082 #
1073 # channelsProcssd.append(self.crosspairs[ii][0]) # save channel A
1083 # channelsProcssd.append(self.crosspairs[ii][0]) # save channel A
1074 # channelsProcssd.append(self.crosspairs[ii][1]) # save channel B
1084 # channelsProcssd.append(self.crosspairs[ii][1]) # save channel B
1075 '''
1085 '''
1076 ###ONLY FOR TEST:
1086 ###ONLY FOR TEST:
1077 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
1087 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
1078 func2clean = 10*numpy.log10(numpy.absolute(spectra[:,ii,ifreq,ih]))
1088 func2clean = 10*numpy.log10(numpy.absolute(spectra[:,ii,ifreq,ih]))
1079 y_dist,binstep = numpy.histogram(func2clean,bins=range(int(min_val),int(max_val+2),step))
1089 y_dist,binstep = numpy.histogram(func2clean,bins=range(int(min_val),int(max_val+2),step))
1080 axs2[row_ax,col_ax].plot(binstep[:-1],newY,color='red')
1090 axs2[row_ax,col_ax].plot(binstep[:-1],newY,color='red')
1081 axs2[row_ax,col_ax].plot(binstep[:-1],y_dist,color='green')
1091 axs2[row_ax,col_ax].plot(binstep[:-1],y_dist,color='green')
1082 axs2[row_ax,col_ax].set_title("CH "+str(self.channels[ii]))
1092 axs2[row_ax,col_ax].set_title("CH "+str(self.channels[ii]))
1083 '''
1093 '''
1084 '''
1094 '''
1085 ###ONLY FOR TEST:
1095 ###ONLY FOR TEST:
1086 col_ax += 1 #contador de ploteo columnas
1096 col_ax += 1 #contador de ploteo columnas
1087 ##print(col_ax)
1097 ##print(col_ax)
1088 ###ONLY FOR TEST:
1098 ###ONLY FOR TEST:
1089 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
1099 if ifreq ==fft_rev and ih==hei_rev: #TO VIEW A SIGNLE FREQUENCY
1090 title = str(dataOut.datatime)+" nFFT: "+str(ifreq)+" Alt: "+str(self.heights[ih])+ " km"
1100 title = str(dataOut.datatime)+" nFFT: "+str(ifreq)+" Alt: "+str(self.heights[ih])+ " km"
1091 title2 = str(dataOut.datatime)+" nFFT: "+str(ifreq)+" Alt: "+str(self.heights[ih])+ " km CLEANED"
1101 title2 = str(dataOut.datatime)+" nFFT: "+str(ifreq)+" Alt: "+str(self.heights[ih])+ " km CLEANED"
1092 fig.suptitle(title)
1102 fig.suptitle(title)
1093 fig2.suptitle(title2)
1103 fig2.suptitle(title2)
1094 plt.show()
1104 plt.show()
1095 '''
1105 '''
1096 ##################################################################################################
1106 ##################################################################################################
1097
1107
1098 #print("Getting average of the spectra and cross-spectra from incoherent echoes.")
1108 #print("Getting average of the spectra and cross-spectra from incoherent echoes.")
1099 out_spectra = numpy.zeros([self.nChan,self.nFFTPoints,self.nHeights], dtype=float) #+numpy.nan
1109 out_spectra = numpy.zeros([self.nChan,self.nFFTPoints,self.nHeights], dtype=float) #+numpy.nan
1100 out_cspectra = numpy.zeros([self.nPairs,self.nFFTPoints,self.nHeights], dtype=complex) #+numpy.nan
1110 out_cspectra = numpy.zeros([self.nPairs,self.nFFTPoints,self.nHeights], dtype=complex) #+numpy.nan
1101 for ih in range(self.nHeights):
1111 for ih in range(self.nHeights):
1102 for ifreq in range(self.nFFTPoints):
1112 for ifreq in range(self.nFFTPoints):
1103 for ich in range(self.nChan):
1113 for ich in range(self.nChan):
1104 tmp = spectra[:,ich,ifreq,ih]
1114 tmp = spectra[:,ich,ifreq,ih]
1105 valid = (numpy.isfinite(tmp[:])==True).nonzero()
1115 valid = (numpy.isfinite(tmp[:])==True).nonzero()
1106
1116
1107 if len(valid[0]) >0 :
1117 if len(valid[0]) >0 :
1108 out_spectra[ich,ifreq,ih] = numpy.nansum(tmp)#/len(valid[0])
1118 out_spectra[ich,ifreq,ih] = numpy.nansum(tmp)#/len(valid[0])
1109
1119
1110 for icr in range(self.nPairs):
1120 for icr in range(self.nPairs):
1111 tmp = numpy.squeeze(cspectra[:,icr,ifreq,ih])
1121 tmp = numpy.squeeze(cspectra[:,icr,ifreq,ih])
1112 valid = (numpy.isfinite(tmp)==True).nonzero()
1122 valid = (numpy.isfinite(tmp)==True).nonzero()
1113 if len(valid[0]) > 0:
1123 if len(valid[0]) > 0:
1114 out_cspectra[icr,ifreq,ih] = numpy.nansum(tmp)#/len(valid[0])
1124 out_cspectra[icr,ifreq,ih] = numpy.nansum(tmp)#/len(valid[0])
1115
1125
1116 return out_spectra, out_cspectra
1126 return out_spectra, out_cspectra
1117
1127
1118 def REM_ISOLATED_POINTS(self,array,rth):
1128 def REM_ISOLATED_POINTS(self,array,rth):
1119 # import matplotlib.pyplot as plt
1129 # import matplotlib.pyplot as plt
1120 if rth == None :
1130 if rth == None :
1121 rth = 4
1131 rth = 4
1122 #print("REM ISO")
1132 #print("REM ISO")
1123 num_prof = len(array[0,:,0])
1133 num_prof = len(array[0,:,0])
1124 num_hei = len(array[0,0,:])
1134 num_hei = len(array[0,0,:])
1125 n2d = len(array[:,0,0])
1135 n2d = len(array[:,0,0])
1126
1136
1127 for ii in range(n2d) :
1137 for ii in range(n2d) :
1128 #print ii,n2d
1138 #print ii,n2d
1129 tmp = array[ii,:,:]
1139 tmp = array[ii,:,:]
1130 #print tmp.shape, array[ii,101,:],array[ii,102,:]
1140 #print tmp.shape, array[ii,101,:],array[ii,102,:]
1131
1141
1132 # fig = plt.figure(figsize=(6,5))
1142 # fig = plt.figure(figsize=(6,5))
1133 # left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
1143 # left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
1134 # ax = fig.add_axes([left, bottom, width, height])
1144 # ax = fig.add_axes([left, bottom, width, height])
1135 # x = range(num_prof)
1145 # x = range(num_prof)
1136 # y = range(num_hei)
1146 # y = range(num_hei)
1137 # cp = ax.contour(y,x,tmp)
1147 # cp = ax.contour(y,x,tmp)
1138 # ax.clabel(cp, inline=True,fontsize=10)
1148 # ax.clabel(cp, inline=True,fontsize=10)
1139 # plt.show()
1149 # plt.show()
1140
1150
1141 #indxs = WHERE(FINITE(tmp) AND tmp GT 0,cindxs)
1151 #indxs = WHERE(FINITE(tmp) AND tmp GT 0,cindxs)
1142 tmp = numpy.reshape(tmp,num_prof*num_hei)
1152 tmp = numpy.reshape(tmp,num_prof*num_hei)
1143 indxs1 = (numpy.isfinite(tmp)==True).nonzero()
1153 indxs1 = (numpy.isfinite(tmp)==True).nonzero()
1144 indxs2 = (tmp > 0).nonzero()
1154 indxs2 = (tmp > 0).nonzero()
1145
1155
1146 indxs1 = (indxs1[0])
1156 indxs1 = (indxs1[0])
1147 indxs2 = indxs2[0]
1157 indxs2 = indxs2[0]
1148 #indxs1 = numpy.array(indxs1[0])
1158 #indxs1 = numpy.array(indxs1[0])
1149 #indxs2 = numpy.array(indxs2[0])
1159 #indxs2 = numpy.array(indxs2[0])
1150 indxs = None
1160 indxs = None
1151 #print indxs1 , indxs2
1161 #print indxs1 , indxs2
1152 for iv in range(len(indxs2)):
1162 for iv in range(len(indxs2)):
1153 indv = numpy.array((indxs1 == indxs2[iv]).nonzero())
1163 indv = numpy.array((indxs1 == indxs2[iv]).nonzero())
1154 #print len(indxs2), indv
1164 #print len(indxs2), indv
1155 if len(indv[0]) > 0 :
1165 if len(indv[0]) > 0 :
1156 indxs = numpy.concatenate((indxs,indxs2[iv]), axis=None)
1166 indxs = numpy.concatenate((indxs,indxs2[iv]), axis=None)
1157 # print indxs
1167 # print indxs
1158 indxs = indxs[1:]
1168 indxs = indxs[1:]
1159 #print(indxs, len(indxs))
1169 #print(indxs, len(indxs))
1160 if len(indxs) < 4 :
1170 if len(indxs) < 4 :
1161 array[ii,:,:] = 0.
1171 array[ii,:,:] = 0.
1162 return
1172 return
1163
1173
1164 xpos = numpy.mod(indxs ,num_hei)
1174 xpos = numpy.mod(indxs ,num_hei)
1165 ypos = (indxs / num_hei)
1175 ypos = (indxs / num_hei)
1166 sx = numpy.argsort(xpos) # Ordering respect to "x" (time)
1176 sx = numpy.argsort(xpos) # Ordering respect to "x" (time)
1167 #print sx
1177 #print sx
1168 xpos = xpos[sx]
1178 xpos = xpos[sx]
1169 ypos = ypos[sx]
1179 ypos = ypos[sx]
1170
1180
1171 # *********************************** Cleaning isolated points **********************************
1181 # *********************************** Cleaning isolated points **********************************
1172 ic = 0
1182 ic = 0
1173 while True :
1183 while True :
1174 r = numpy.sqrt(list(numpy.power((xpos[ic]-xpos),2)+ numpy.power((ypos[ic]-ypos),2)))
1184 r = numpy.sqrt(list(numpy.power((xpos[ic]-xpos),2)+ numpy.power((ypos[ic]-ypos),2)))
1175 #no_coh = WHERE(FINITE(r) AND (r LE rth),cno_coh)
1185 #no_coh = WHERE(FINITE(r) AND (r LE rth),cno_coh)
1176 #plt.plot(r)
1186 #plt.plot(r)
1177 #plt.show()
1187 #plt.show()
1178 no_coh1 = (numpy.isfinite(r)==True).nonzero()
1188 no_coh1 = (numpy.isfinite(r)==True).nonzero()
1179 no_coh2 = (r <= rth).nonzero()
1189 no_coh2 = (r <= rth).nonzero()
1180 #print r, no_coh1, no_coh2
1190 #print r, no_coh1, no_coh2
1181 no_coh1 = numpy.array(no_coh1[0])
1191 no_coh1 = numpy.array(no_coh1[0])
1182 no_coh2 = numpy.array(no_coh2[0])
1192 no_coh2 = numpy.array(no_coh2[0])
1183 no_coh = None
1193 no_coh = None
1184 #print valid1 , valid2
1194 #print valid1 , valid2
1185 for iv in range(len(no_coh2)):
1195 for iv in range(len(no_coh2)):
1186 indv = numpy.array((no_coh1 == no_coh2[iv]).nonzero())
1196 indv = numpy.array((no_coh1 == no_coh2[iv]).nonzero())
1187 if len(indv[0]) > 0 :
1197 if len(indv[0]) > 0 :
1188 no_coh = numpy.concatenate((no_coh,no_coh2[iv]), axis=None)
1198 no_coh = numpy.concatenate((no_coh,no_coh2[iv]), axis=None)
1189 no_coh = no_coh[1:]
1199 no_coh = no_coh[1:]
1190 #print len(no_coh), no_coh
1200 #print len(no_coh), no_coh
1191 if len(no_coh) < 4 :
1201 if len(no_coh) < 4 :
1192 #print xpos[ic], ypos[ic], ic
1202 #print xpos[ic], ypos[ic], ic
1193 # plt.plot(r)
1203 # plt.plot(r)
1194 # plt.show()
1204 # plt.show()
1195 xpos[ic] = numpy.nan
1205 xpos[ic] = numpy.nan
1196 ypos[ic] = numpy.nan
1206 ypos[ic] = numpy.nan
1197
1207
1198 ic = ic + 1
1208 ic = ic + 1
1199 if (ic == len(indxs)) :
1209 if (ic == len(indxs)) :
1200 break
1210 break
1201 #print( xpos, ypos)
1211 #print( xpos, ypos)
1202
1212
1203 indxs = (numpy.isfinite(list(xpos))==True).nonzero()
1213 indxs = (numpy.isfinite(list(xpos))==True).nonzero()
1204 #print indxs[0]
1214 #print indxs[0]
1205 if len(indxs[0]) < 4 :
1215 if len(indxs[0]) < 4 :
1206 array[ii,:,:] = 0.
1216 array[ii,:,:] = 0.
1207 return
1217 return
1208
1218
1209 xpos = xpos[indxs[0]]
1219 xpos = xpos[indxs[0]]
1210 ypos = ypos[indxs[0]]
1220 ypos = ypos[indxs[0]]
1211 for i in range(0,len(ypos)):
1221 for i in range(0,len(ypos)):
1212 ypos[i]=int(ypos[i])
1222 ypos[i]=int(ypos[i])
1213 junk = tmp
1223 junk = tmp
1214 tmp = junk*0.0
1224 tmp = junk*0.0
1215
1225
1216 tmp[list(xpos + (ypos*num_hei))] = junk[list(xpos + (ypos*num_hei))]
1226 tmp[list(xpos + (ypos*num_hei))] = junk[list(xpos + (ypos*num_hei))]
1217 array[ii,:,:] = numpy.reshape(tmp,(num_prof,num_hei))
1227 array[ii,:,:] = numpy.reshape(tmp,(num_prof,num_hei))
1218
1228
1219 #print array.shape
1229 #print array.shape
1220 #tmp = numpy.reshape(tmp,(num_prof,num_hei))
1230 #tmp = numpy.reshape(tmp,(num_prof,num_hei))
1221 #print tmp.shape
1231 #print tmp.shape
1222
1232
1223 # fig = plt.figure(figsize=(6,5))
1233 # fig = plt.figure(figsize=(6,5))
1224 # left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
1234 # left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
1225 # ax = fig.add_axes([left, bottom, width, height])
1235 # ax = fig.add_axes([left, bottom, width, height])
1226 # x = range(num_prof)
1236 # x = range(num_prof)
1227 # y = range(num_hei)
1237 # y = range(num_hei)
1228 # cp = ax.contour(y,x,array[ii,:,:])
1238 # cp = ax.contour(y,x,array[ii,:,:])
1229 # ax.clabel(cp, inline=True,fontsize=10)
1239 # ax.clabel(cp, inline=True,fontsize=10)
1230 # plt.show()
1240 # plt.show()
1231 return array
1241 return array
1232
1242
1233
1243
1234 class IntegrationFaradaySpectra(Operation):
1244 class IntegrationFaradaySpectra(Operation):
1235
1245
1236 __profIndex = 0
1246 __profIndex = 0
1237 __withOverapping = False
1247 __withOverapping = False
1238
1248
1239 __byTime = False
1249 __byTime = False
1240 __initime = None
1250 __initime = None
1241 __lastdatatime = None
1251 __lastdatatime = None
1242 __integrationtime = None
1252 __integrationtime = None
1243
1253
1244 __buffer_spc = None
1254 __buffer_spc = None
1245 __buffer_cspc = None
1255 __buffer_cspc = None
1246 __buffer_dc = None
1256 __buffer_dc = None
1247
1257
1248 __dataReady = False
1258 __dataReady = False
1249
1259
1250 __timeInterval = None
1260 __timeInterval = None
1251 n_ints = None #matriz de numero de integracions (CH,HEI)
1261 n_ints = None #matriz de numero de integracions (CH,HEI)
1252 n = None
1262 n = None
1253 minHei_ind = None
1263 minHei_ind = None
1254 maxHei_ind = None
1264 maxHei_ind = None
1255 navg = 1.0
1265 navg = 1.0
1256 factor = 0.0
1266 factor = 0.0
1257 dataoutliers = None # (CHANNELS, HEIGHTS)
1267 dataoutliers = None # (CHANNELS, HEIGHTS)
1258
1268
1259 def __init__(self):
1269 def __init__(self):
1260
1270
1261 Operation.__init__(self)
1271 Operation.__init__(self)
1262
1272
1263 def setup(self, dataOut,n=None, timeInterval=None, overlapping=False, DPL=None, minHei=None, maxHei=None, avg=1,factor=0.75):
1273 def setup(self, dataOut,n=None, timeInterval=None, overlapping=False, DPL=None, minHei=None, maxHei=None, avg=1,factor=0.75):
1264 """
1274 """
1265 Set the parameters of the integration class.
1275 Set the parameters of the integration class.
1266
1276
1267 Inputs:
1277 Inputs:
1268
1278
1269 n : Number of coherent integrations
1279 n : Number of coherent integrations
1270 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
1280 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
1271 overlapping :
1281 overlapping :
1272
1282
1273 """
1283 """
1274
1284
1275 self.__initime = None
1285 self.__initime = None
1276 self.__lastdatatime = 0
1286 self.__lastdatatime = 0
1277
1287
1278 self.__buffer_spc = []
1288 self.__buffer_spc = []
1279 self.__buffer_cspc = []
1289 self.__buffer_cspc = []
1280 self.__buffer_dc = 0
1290 self.__buffer_dc = 0
1281
1291
1282 self.__profIndex = 0
1292 self.__profIndex = 0
1283 self.__dataReady = False
1293 self.__dataReady = False
1284 self.__byTime = False
1294 self.__byTime = False
1285
1295
1286 self.factor = factor
1296 self.factor = factor
1287 self.navg = avg
1297 self.navg = avg
1288 #self.ByLags = dataOut.ByLags ###REDEFINIR
1298 #self.ByLags = dataOut.ByLags ###REDEFINIR
1289 self.ByLags = False
1299 self.ByLags = False
1290 self.maxProfilesInt = 1
1300 self.maxProfilesInt = 1
1291
1301
1292 if DPL != None:
1302 if DPL != None:
1293 self.DPL=DPL
1303 self.DPL=DPL
1294 else:
1304 else:
1295 #self.DPL=dataOut.DPL ###REDEFINIR
1305 #self.DPL=dataOut.DPL ###REDEFINIR
1296 self.DPL=0
1306 self.DPL=0
1297
1307
1298 if n is None and timeInterval is None:
1308 if n is None and timeInterval is None:
1299 raise ValueError("n or timeInterval should be specified ...")
1309 raise ValueError("n or timeInterval should be specified ...")
1300
1310
1301 if n is not None:
1311 if n is not None:
1302 self.n = int(n)
1312 self.n = int(n)
1303 else:
1313 else:
1304 self.__integrationtime = int(timeInterval)
1314 self.__integrationtime = int(timeInterval)
1305 self.n = None
1315 self.n = None
1306 self.__byTime = True
1316 self.__byTime = True
1307
1317
1308 if minHei == None:
1318 if minHei == None:
1309 minHei = self.dataOut.heightList[0]
1319 minHei = self.dataOut.heightList[0]
1310
1320
1311 if maxHei == None:
1321 if maxHei == None:
1312 maxHei = self.dataOut.heightList[-1]
1322 maxHei = self.dataOut.heightList[-1]
1313
1323
1314 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
1324 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
1315 print('minHei: %.2f is out of the heights range' % (minHei))
1325 print('minHei: %.2f is out of the heights range' % (minHei))
1316 print('minHei is setting to %.2f' % (self.dataOut.heightList[0]))
1326 print('minHei is setting to %.2f' % (self.dataOut.heightList[0]))
1317 minHei = self.dataOut.heightList[0]
1327 minHei = self.dataOut.heightList[0]
1318
1328
1319 if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei):
1329 if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei):
1320 print('maxHei: %.2f is out of the heights range' % (maxHei))
1330 print('maxHei: %.2f is out of the heights range' % (maxHei))
1321 print('maxHei is setting to %.2f' % (self.dataOut.heightList[-1]))
1331 print('maxHei is setting to %.2f' % (self.dataOut.heightList[-1]))
1322 maxHei = self.dataOut.heightList[-1]
1332 maxHei = self.dataOut.heightList[-1]
1323
1333
1324 ind_list1 = numpy.where(self.dataOut.heightList >= minHei)
1334 ind_list1 = numpy.where(self.dataOut.heightList >= minHei)
1325 ind_list2 = numpy.where(self.dataOut.heightList <= maxHei)
1335 ind_list2 = numpy.where(self.dataOut.heightList <= maxHei)
1326 self.minHei_ind = ind_list1[0][0]
1336 self.minHei_ind = ind_list1[0][0]
1327 self.maxHei_ind = ind_list2[0][-1]
1337 self.maxHei_ind = ind_list2[0][-1]
1328
1338
1329 def putData(self, data_spc, data_cspc, data_dc):
1339 def putData(self, data_spc, data_cspc, data_dc):
1330 """
1340 """
1331 Add a profile to the __buffer_spc and increase in one the __profileIndex
1341 Add a profile to the __buffer_spc and increase in one the __profileIndex
1332
1342
1333 """
1343 """
1334
1344
1335 self.__buffer_spc.append(data_spc)
1345 self.__buffer_spc.append(data_spc)
1336
1346
1337 if data_cspc is None:
1347 if data_cspc is None:
1338 self.__buffer_cspc = None
1348 self.__buffer_cspc = None
1339 else:
1349 else:
1340 self.__buffer_cspc.append(data_cspc)
1350 self.__buffer_cspc.append(data_cspc)
1341
1351
1342 if data_dc is None:
1352 if data_dc is None:
1343 self.__buffer_dc = None
1353 self.__buffer_dc = None
1344 else:
1354 else:
1345 self.__buffer_dc += data_dc
1355 self.__buffer_dc += data_dc
1346
1356
1347 self.__profIndex += 1
1357 self.__profIndex += 1
1348
1358
1349 return
1359 return
1350
1360
1351 def hildebrand_sekhon_Integration(self,sortdata,navg, factor):
1361 def hildebrand_sekhon_Integration(self,sortdata,navg, factor):
1352 #data debe estar ordenado
1362 #data debe estar ordenado
1353 #sortdata = numpy.sort(data, axis=None)
1363 #sortdata = numpy.sort(data, axis=None)
1354 #sortID=data.argsort()
1364 #sortID=data.argsort()
1355 lenOfData = len(sortdata)
1365 lenOfData = len(sortdata)
1356 nums_min = lenOfData*factor
1366 nums_min = lenOfData*factor
1357 if nums_min <= 5:
1367 if nums_min <= 5:
1358 nums_min = 5
1368 nums_min = 5
1359 sump = 0.
1369 sump = 0.
1360 sumq = 0.
1370 sumq = 0.
1361 j = 0
1371 j = 0
1362 cont = 1
1372 cont = 1
1363 while((cont == 1)and(j < lenOfData)):
1373 while((cont == 1)and(j < lenOfData)):
1364 sump += sortdata[j]
1374 sump += sortdata[j]
1365 sumq += sortdata[j]**2
1375 sumq += sortdata[j]**2
1366 if j > nums_min:
1376 if j > nums_min:
1367 rtest = float(j)/(j-1) + 1.0/navg
1377 rtest = float(j)/(j-1) + 1.0/navg
1368 if ((sumq*j) > (rtest*sump**2)):
1378 if ((sumq*j) > (rtest*sump**2)):
1369 j = j - 1
1379 j = j - 1
1370 sump = sump - sortdata[j]
1380 sump = sump - sortdata[j]
1371 sumq = sumq - sortdata[j]**2
1381 sumq = sumq - sortdata[j]**2
1372 cont = 0
1382 cont = 0
1373 j += 1
1383 j += 1
1374 #lnoise = sump / j
1384 #lnoise = sump / j
1375 #print("H S done")
1385 #print("H S done")
1376 #return j,sortID
1386 #return j,sortID
1377 return j
1387 return j
1378
1388
1379
1389
1380 def pushData(self):
1390 def pushData(self):
1381 """
1391 """
1382 Return the sum of the last profiles and the profiles used in the sum.
1392 Return the sum of the last profiles and the profiles used in the sum.
1383
1393
1384 Affected:
1394 Affected:
1385
1395
1386 self.__profileIndex
1396 self.__profileIndex
1387
1397
1388 """
1398 """
1389 bufferH=None
1399 bufferH=None
1390 buffer=None
1400 buffer=None
1391 buffer1=None
1401 buffer1=None
1392 buffer_cspc=None
1402 buffer_cspc=None
1393 self.__buffer_spc=numpy.array(self.__buffer_spc)
1403 self.__buffer_spc=numpy.array(self.__buffer_spc)
1394 try:
1404 try:
1395 self.__buffer_cspc=numpy.array(self.__buffer_cspc)
1405 self.__buffer_cspc=numpy.array(self.__buffer_cspc)
1396 except :
1406 except :
1397 #print("No cpsc",e)
1407 #print("No cpsc",e)
1398 pass
1408 pass
1399 #print("FREQ_DC",self.__buffer_spc.shape,self.__buffer_cspc.shape)
1409 #print("FREQ_DC",self.__buffer_spc.shape,self.__buffer_cspc.shape)
1400
1410
1401 freq_dc = int(self.__buffer_spc.shape[2] / 2)
1411 freq_dc = int(self.__buffer_spc.shape[2] / 2)
1402 #print("FREQ_DC",freq_dc,self.__buffer_spc.shape,self.nHeights)
1412 #print("FREQ_DC",freq_dc,self.__buffer_spc.shape,self.nHeights)
1403
1413
1404 self.dataOutliers = numpy.zeros((self.nChannels,self.nHeights)) # --> almacen de outliers
1414 self.dataOutliers = numpy.zeros((self.nChannels,self.nHeights)) # --> almacen de outliers
1405
1415
1406 for k in range(self.minHei_ind,self.maxHei_ind):
1416 for k in range(self.minHei_ind,self.maxHei_ind):
1407 try:
1417 try:
1408 buffer_cspc=numpy.copy(self.__buffer_cspc[:,:,:,k])
1418 buffer_cspc=numpy.copy(self.__buffer_cspc[:,:,:,k])
1409 except:
1419 except:
1410 #print("No cpsc",e)
1420 #print("No cpsc",e)
1411 pass
1421 pass
1412 outliers_IDs_cspc=[]
1422 outliers_IDs_cspc=[]
1413 cspc_outliers_exist=False
1423 cspc_outliers_exist=False
1414 for i in range(self.nChannels):#dataOut.nChannels):
1424 for i in range(self.nChannels):#dataOut.nChannels):
1415
1425
1416 buffer1=numpy.copy(self.__buffer_spc[:,i,:,k])
1426 buffer1=numpy.copy(self.__buffer_spc[:,i,:,k])
1417 indexes=[]
1427 indexes=[]
1418 #sortIDs=[]
1428 #sortIDs=[]
1419 outliers_IDs=[]
1429 outliers_IDs=[]
1420
1430
1421 for j in range(self.nProfiles): #frecuencias en el tiempo
1431 for j in range(self.nProfiles): #frecuencias en el tiempo
1422 # if i==0 and j==freq_dc: #NOT CONSIDERING DC PROFILE AT CHANNEL 0
1432 # if i==0 and j==freq_dc: #NOT CONSIDERING DC PROFILE AT CHANNEL 0
1423 # continue
1433 # continue
1424 # if i==1 and j==0: #NOT CONSIDERING DC PROFILE AT CHANNEL 1
1434 # if i==1 and j==0: #NOT CONSIDERING DC PROFILE AT CHANNEL 1
1425 # continue
1435 # continue
1426 buffer=buffer1[:,j]
1436 buffer=buffer1[:,j]
1427 sortdata = numpy.sort(buffer, axis=None)
1437 sortdata = numpy.sort(buffer, axis=None)
1428
1438
1429 sortID=buffer.argsort()
1439 sortID=buffer.argsort()
1430 index = _noise.hildebrand_sekhon2(sortdata,self.navg)
1440 index = _noise.hildebrand_sekhon2(sortdata,self.navg)
1431
1441
1432 #index,sortID=self.hildebrand_sekhon_Integration(buffer,1,self.factor)
1442 #index,sortID=self.hildebrand_sekhon_Integration(buffer,1,self.factor)
1433
1443
1434 # fig,ax = plt.subplots()
1444 # fig,ax = plt.subplots()
1435 # ax.set_title(str(k)+" "+str(j))
1445 # ax.set_title(str(k)+" "+str(j))
1436 # x=range(len(sortdata))
1446 # x=range(len(sortdata))
1437 # ax.scatter(x,sortdata)
1447 # ax.scatter(x,sortdata)
1438 # ax.axvline(index)
1448 # ax.axvline(index)
1439 # plt.show()
1449 # plt.show()
1440
1450
1441 indexes.append(index)
1451 indexes.append(index)
1442 #sortIDs.append(sortID)
1452 #sortIDs.append(sortID)
1443 outliers_IDs=numpy.append(outliers_IDs,sortID[index:])
1453 outliers_IDs=numpy.append(outliers_IDs,sortID[index:])
1444
1454
1445 #print("Outliers: ",outliers_IDs)
1455 #print("Outliers: ",outliers_IDs)
1446 outliers_IDs=numpy.array(outliers_IDs)
1456 outliers_IDs=numpy.array(outliers_IDs)
1447 outliers_IDs=outliers_IDs.ravel()
1457 outliers_IDs=outliers_IDs.ravel()
1448 outliers_IDs=numpy.unique(outliers_IDs)
1458 outliers_IDs=numpy.unique(outliers_IDs)
1449 outliers_IDs=outliers_IDs.astype(numpy.dtype('int64'))
1459 outliers_IDs=outliers_IDs.astype(numpy.dtype('int64'))
1450 indexes=numpy.array(indexes)
1460 indexes=numpy.array(indexes)
1451 indexmin=numpy.min(indexes)
1461 indexmin=numpy.min(indexes)
1452
1462
1453
1463
1454 #print(indexmin,buffer1.shape[0], k)
1464 #print(indexmin,buffer1.shape[0], k)
1455
1465
1456 # fig,ax = plt.subplots()
1466 # fig,ax = plt.subplots()
1457 # ax.plot(sortdata)
1467 # ax.plot(sortdata)
1458 # ax2 = ax.twinx()
1468 # ax2 = ax.twinx()
1459 # x=range(len(indexes))
1469 # x=range(len(indexes))
1460 # #plt.scatter(x,indexes)
1470 # #plt.scatter(x,indexes)
1461 # ax2.scatter(x,indexes)
1471 # ax2.scatter(x,indexes)
1462 # plt.show()
1472 # plt.show()
1463
1473
1464 if indexmin != buffer1.shape[0]:
1474 if indexmin != buffer1.shape[0]:
1465 if self.nChannels > 1:
1475 if self.nChannels > 1:
1466 cspc_outliers_exist= True
1476 cspc_outliers_exist= True
1467
1477
1468 lt=outliers_IDs
1478 lt=outliers_IDs
1469 #avg=numpy.mean(buffer1[[t for t in range(buffer1.shape[0]) if t not in lt],:],axis=0)
1479 #avg=numpy.mean(buffer1[[t for t in range(buffer1.shape[0]) if t not in lt],:],axis=0)
1470
1480
1471 for p in list(outliers_IDs):
1481 for p in list(outliers_IDs):
1472 #buffer1[p,:]=avg
1482 #buffer1[p,:]=avg
1473 buffer1[p,:] = numpy.NaN
1483 buffer1[p,:] = numpy.NaN
1474
1484
1475 self.dataOutliers[i,k] = len(outliers_IDs)
1485 self.dataOutliers[i,k] = len(outliers_IDs)
1476
1486
1477 self.__buffer_spc[:,i,:,k]=numpy.copy(buffer1)
1487 self.__buffer_spc[:,i,:,k]=numpy.copy(buffer1)
1478
1488
1479 outliers_IDs_cspc=numpy.append(outliers_IDs_cspc,outliers_IDs)
1489 outliers_IDs_cspc=numpy.append(outliers_IDs_cspc,outliers_IDs)
1480
1490
1481
1491
1482
1492
1483 outliers_IDs_cspc=outliers_IDs_cspc.astype(numpy.dtype('int64'))
1493 outliers_IDs_cspc=outliers_IDs_cspc.astype(numpy.dtype('int64'))
1484 if cspc_outliers_exist :
1494 if cspc_outliers_exist :
1485
1495
1486 lt=outliers_IDs_cspc
1496 lt=outliers_IDs_cspc
1487
1497
1488 #avg=numpy.mean(buffer_cspc[[t for t in range(buffer_cspc.shape[0]) if t not in lt],:],axis=0)
1498 #avg=numpy.mean(buffer_cspc[[t for t in range(buffer_cspc.shape[0]) if t not in lt],:],axis=0)
1489 for p in list(outliers_IDs_cspc):
1499 for p in list(outliers_IDs_cspc):
1490 #buffer_cspc[p,:]=avg
1500 #buffer_cspc[p,:]=avg
1491 buffer_cspc[p,:] = numpy.NaN
1501 buffer_cspc[p,:] = numpy.NaN
1492
1502
1493 try:
1503 try:
1494 self.__buffer_cspc[:,:,:,k]=numpy.copy(buffer_cspc)
1504 self.__buffer_cspc[:,:,:,k]=numpy.copy(buffer_cspc)
1495 except:
1505 except:
1496 #print("No cpsc",e)
1506 #print("No cpsc",e)
1497 pass
1507 pass
1498 #else:
1508 #else:
1499 #break
1509 #break
1500
1510
1501
1511
1502
1512
1503 nOutliers = len(outliers_IDs)
1513 nOutliers = len(outliers_IDs)
1504 #print("Outliers n: ",self.dataOutliers,nOutliers)
1514 #print("Outliers n: ",self.dataOutliers,nOutliers)
1505 buffer=None
1515 buffer=None
1506 bufferH=None
1516 bufferH=None
1507 buffer1=None
1517 buffer1=None
1508 buffer_cspc=None
1518 buffer_cspc=None
1509
1519
1510
1520
1511 buffer=None
1521 buffer=None
1512
1522
1513 #data_spc = numpy.sum(self.__buffer_spc,axis=0)
1523 #data_spc = numpy.sum(self.__buffer_spc,axis=0)
1514 data_spc = numpy.nansum(self.__buffer_spc,axis=0)
1524 data_spc = numpy.nansum(self.__buffer_spc,axis=0)
1515 try:
1525 try:
1516 #data_cspc = numpy.sum(self.__buffer_cspc,axis=0)
1526 #data_cspc = numpy.sum(self.__buffer_cspc,axis=0)
1517 data_cspc = numpy.nansum(self.__buffer_cspc,axis=0)
1527 data_cspc = numpy.nansum(self.__buffer_cspc,axis=0)
1518 except:
1528 except:
1519 #print("No cpsc",e)
1529 #print("No cpsc",e)
1520 pass
1530 pass
1521
1531
1522
1532
1523 data_dc = self.__buffer_dc
1533 data_dc = self.__buffer_dc
1524 #(CH, HEIGH)
1534 #(CH, HEIGH)
1525 self.maxProfilesInt = self.__profIndex
1535 self.maxProfilesInt = self.__profIndex
1526 n = self.__profIndex - self.dataOutliers # n becomes a matrix
1536 n = self.__profIndex - self.dataOutliers # n becomes a matrix
1527
1537
1528 self.__buffer_spc = []
1538 self.__buffer_spc = []
1529 self.__buffer_cspc = []
1539 self.__buffer_cspc = []
1530 self.__buffer_dc = 0
1540 self.__buffer_dc = 0
1531 self.__profIndex = 0
1541 self.__profIndex = 0
1532
1542
1533 return data_spc, data_cspc, data_dc, n
1543 return data_spc, data_cspc, data_dc, n
1534
1544
1535 def byProfiles(self, *args):
1545 def byProfiles(self, *args):
1536
1546
1537 self.__dataReady = False
1547 self.__dataReady = False
1538 avgdata_spc = None
1548 avgdata_spc = None
1539 avgdata_cspc = None
1549 avgdata_cspc = None
1540 avgdata_dc = None
1550 avgdata_dc = None
1541
1551
1542 self.putData(*args)
1552 self.putData(*args)
1543
1553
1544 if self.__profIndex == self.n:
1554 if self.__profIndex == self.n:
1545
1555
1546 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1556 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1547 self.n_ints = n
1557 self.n_ints = n
1548 self.__dataReady = True
1558 self.__dataReady = True
1549
1559
1550 return avgdata_spc, avgdata_cspc, avgdata_dc
1560 return avgdata_spc, avgdata_cspc, avgdata_dc
1551
1561
1552 def byTime(self, datatime, *args):
1562 def byTime(self, datatime, *args):
1553
1563
1554 self.__dataReady = False
1564 self.__dataReady = False
1555 avgdata_spc = None
1565 avgdata_spc = None
1556 avgdata_cspc = None
1566 avgdata_cspc = None
1557 avgdata_dc = None
1567 avgdata_dc = None
1558
1568
1559 self.putData(*args)
1569 self.putData(*args)
1560
1570
1561 if (datatime - self.__initime) >= self.__integrationtime:
1571 if (datatime - self.__initime) >= self.__integrationtime:
1562 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1572 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1563 self.n_ints = n
1573 self.n_ints = n
1564 self.__dataReady = True
1574 self.__dataReady = True
1565
1575
1566 return avgdata_spc, avgdata_cspc, avgdata_dc
1576 return avgdata_spc, avgdata_cspc, avgdata_dc
1567
1577
1568 def integrate(self, datatime, *args):
1578 def integrate(self, datatime, *args):
1569
1579
1570 if self.__profIndex == 0:
1580 if self.__profIndex == 0:
1571 self.__initime = datatime
1581 self.__initime = datatime
1572
1582
1573 if self.__byTime:
1583 if self.__byTime:
1574 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(
1584 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(
1575 datatime, *args)
1585 datatime, *args)
1576 else:
1586 else:
1577 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
1587 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
1578
1588
1579 if not self.__dataReady:
1589 if not self.__dataReady:
1580 return None, None, None, None
1590 return None, None, None, None
1581
1591
1582 return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc
1592 return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc
1583
1593
1584 def run(self, dataOut, n=None, DPL = None,timeInterval=None, overlapping=False, minHei=None, maxHei=None, avg=1, factor=0.75):
1594 def run(self, dataOut, n=None, DPL = None,timeInterval=None, overlapping=False, minHei=None, maxHei=None, avg=1, factor=0.75):
1585 self.dataOut = dataOut
1595 self.dataOut = dataOut
1586 if n == 1:
1596 if n == 1:
1587 return self.dataOut
1597 return self.dataOut
1588
1598
1589
1599
1590 if self.dataOut.nChannels == 1:
1600 if self.dataOut.nChannels == 1:
1591 self.dataOut.data_cspc = None #si es un solo canal no vale la pena acumular DATOS
1601 self.dataOut.data_cspc = None #si es un solo canal no vale la pena acumular DATOS
1592 #print(self.dataOut.data_spc.shape, self.dataOut.data_cspc)
1602 #print(self.dataOut.data_spc.shape, self.dataOut.data_cspc)
1593 if not self.isConfig:
1603 if not self.isConfig:
1594 self.setup(self.dataOut, n, timeInterval, overlapping,DPL ,minHei, maxHei, avg, factor)
1604 self.setup(self.dataOut, n, timeInterval, overlapping,DPL ,minHei, maxHei, avg, factor)
1595 self.isConfig = True
1605 self.isConfig = True
1596
1606
1597 if not self.ByLags:
1607 if not self.ByLags:
1598 self.nProfiles=self.dataOut.nProfiles
1608 self.nProfiles=self.dataOut.nProfiles
1599 self.nChannels=self.dataOut.nChannels
1609 self.nChannels=self.dataOut.nChannels
1600 self.nHeights=self.dataOut.nHeights
1610 self.nHeights=self.dataOut.nHeights
1601 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(self.dataOut.utctime,
1611 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(self.dataOut.utctime,
1602 self.dataOut.data_spc,
1612 self.dataOut.data_spc,
1603 self.dataOut.data_cspc,
1613 self.dataOut.data_cspc,
1604 self.dataOut.data_dc)
1614 self.dataOut.data_dc)
1605 else:
1615 else:
1606 self.nProfiles=self.dataOut.nProfiles
1616 self.nProfiles=self.dataOut.nProfiles
1607 self.nChannels=self.dataOut.nChannels
1617 self.nChannels=self.dataOut.nChannels
1608 self.nHeights=self.dataOut.nHeights
1618 self.nHeights=self.dataOut.nHeights
1609 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(self.dataOut.utctime,
1619 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(self.dataOut.utctime,
1610 self.dataOut.dataLag_spc,
1620 self.dataOut.dataLag_spc,
1611 self.dataOut.dataLag_cspc,
1621 self.dataOut.dataLag_cspc,
1612 self.dataOut.dataLag_dc)
1622 self.dataOut.dataLag_dc)
1613 self.dataOut.flagNoData = True
1623 self.dataOut.flagNoData = True
1614 if self.__dataReady:
1624 if self.__dataReady:
1615
1625
1616 if not self.ByLags:
1626 if not self.ByLags:
1617 if self.nChannels == 1:
1627 if self.nChannels == 1:
1618 #print("f int", avgdata_spc.shape)
1628 #print("f int", avgdata_spc.shape)
1619 self.dataOut.data_spc = avgdata_spc
1629 self.dataOut.data_spc = avgdata_spc
1620 self.dataOut.data_cspc = avgdata_spc
1630 self.dataOut.data_cspc = avgdata_spc
1621 else:
1631 else:
1622 self.dataOut.data_spc = numpy.squeeze(avgdata_spc)
1632 self.dataOut.data_spc = numpy.squeeze(avgdata_spc)
1623 self.dataOut.data_cspc = numpy.squeeze(avgdata_cspc)
1633 self.dataOut.data_cspc = numpy.squeeze(avgdata_cspc)
1624 self.dataOut.data_dc = avgdata_dc
1634 self.dataOut.data_dc = avgdata_dc
1625 self.dataOut.data_outlier = self.dataOutliers
1635 self.dataOut.data_outlier = self.dataOutliers
1626
1636
1627 else:
1637 else:
1628 self.dataOut.dataLag_spc = avgdata_spc
1638 self.dataOut.dataLag_spc = avgdata_spc
1629 self.dataOut.dataLag_cspc = avgdata_cspc
1639 self.dataOut.dataLag_cspc = avgdata_cspc
1630 self.dataOut.dataLag_dc = avgdata_dc
1640 self.dataOut.dataLag_dc = avgdata_dc
1631
1641
1632 self.dataOut.data_spc=self.dataOut.dataLag_spc[:,:,:,self.dataOut.LagPlot]
1642 self.dataOut.data_spc=self.dataOut.dataLag_spc[:,:,:,self.dataOut.LagPlot]
1633 self.dataOut.data_cspc=self.dataOut.dataLag_cspc[:,:,:,self.dataOut.LagPlot]
1643 self.dataOut.data_cspc=self.dataOut.dataLag_cspc[:,:,:,self.dataOut.LagPlot]
1634 self.dataOut.data_dc=self.dataOut.dataLag_dc[:,:,self.dataOut.LagPlot]
1644 self.dataOut.data_dc=self.dataOut.dataLag_dc[:,:,self.dataOut.LagPlot]
1635
1645
1636
1646
1637 self.dataOut.nIncohInt *= self.n_ints
1647 self.dataOut.nIncohInt *= self.n_ints
1638 self.dataOut.max_nIncohInt = self.maxProfilesInt
1648 self.dataOut.max_nIncohInt = self.maxProfilesInt
1639 #print(self.dataOut.max_nIncohInt)
1649 #print(self.dataOut.max_nIncohInt)
1640 self.dataOut.utctime = avgdatatime
1650 self.dataOut.utctime = avgdatatime
1641 self.dataOut.flagNoData = False
1651 self.dataOut.flagNoData = False
1642 #print("Faraday Integration DONE...")
1652 #print("Faraday Integration DONE...")
1643 #print(self.dataOut.flagNoData)
1653 #print(self.dataOut.flagNoData)
1644 return self.dataOut
1654 return self.dataOut
1645
1655
1646 class removeInterference(Operation):
1656 class removeInterference(Operation):
1647
1657
1648 def removeInterference2(self):
1658 def removeInterference2(self):
1649
1659
1650 cspc = self.dataOut.data_cspc
1660 cspc = self.dataOut.data_cspc
1651 spc = self.dataOut.data_spc
1661 spc = self.dataOut.data_spc
1652 Heights = numpy.arange(cspc.shape[2])
1662 Heights = numpy.arange(cspc.shape[2])
1653 realCspc = numpy.abs(cspc)
1663 realCspc = numpy.abs(cspc)
1654
1664
1655 for i in range(cspc.shape[0]):
1665 for i in range(cspc.shape[0]):
1656 LinePower= numpy.sum(realCspc[i], axis=0)
1666 LinePower= numpy.sum(realCspc[i], axis=0)
1657 Threshold = numpy.amax(LinePower)-numpy.sort(LinePower)[len(Heights)-int(len(Heights)*0.1)]
1667 Threshold = numpy.amax(LinePower)-numpy.sort(LinePower)[len(Heights)-int(len(Heights)*0.1)]
1658 SelectedHeights = Heights[ numpy.where( LinePower < Threshold ) ]
1668 SelectedHeights = Heights[ numpy.where( LinePower < Threshold ) ]
1659 InterferenceSum = numpy.sum( realCspc[i,:,SelectedHeights], axis=0 )
1669 InterferenceSum = numpy.sum( realCspc[i,:,SelectedHeights], axis=0 )
1660 InterferenceThresholdMin = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.98)]
1670 InterferenceThresholdMin = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.98)]
1661 InterferenceThresholdMax = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.99)]
1671 InterferenceThresholdMax = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.99)]
1662
1672
1663
1673
1664 InterferenceRange = numpy.where( ([InterferenceSum > InterferenceThresholdMin]))# , InterferenceSum < InterferenceThresholdMax]) )
1674 InterferenceRange = numpy.where( ([InterferenceSum > InterferenceThresholdMin]))# , InterferenceSum < InterferenceThresholdMax]) )
1665 #InterferenceRange = numpy.where( ([InterferenceRange < InterferenceThresholdMax]))
1675 #InterferenceRange = numpy.where( ([InterferenceRange < InterferenceThresholdMax]))
1666 if len(InterferenceRange)<int(cspc.shape[1]*0.3):
1676 if len(InterferenceRange)<int(cspc.shape[1]*0.3):
1667 cspc[i,InterferenceRange,:] = numpy.NaN
1677 cspc[i,InterferenceRange,:] = numpy.NaN
1668
1678
1669 self.dataOut.data_cspc = cspc
1679 self.dataOut.data_cspc = cspc
1670
1680
1671 def removeInterference(self, interf = 2, hei_interf = None, nhei_interf = None, offhei_interf = None):
1681 def removeInterference(self, interf = 2, hei_interf = None, nhei_interf = None, offhei_interf = None):
1672
1682
1673 jspectra = self.dataOut.data_spc
1683 jspectra = self.dataOut.data_spc
1674 jcspectra = self.dataOut.data_cspc
1684 jcspectra = self.dataOut.data_cspc
1675 jnoise = self.dataOut.getNoise()
1685 jnoise = self.dataOut.getNoise()
1676 num_incoh = self.dataOut.nIncohInt
1686 num_incoh = self.dataOut.nIncohInt
1677
1687
1678 num_channel = jspectra.shape[0]
1688 num_channel = jspectra.shape[0]
1679 num_prof = jspectra.shape[1]
1689 num_prof = jspectra.shape[1]
1680 num_hei = jspectra.shape[2]
1690 num_hei = jspectra.shape[2]
1681
1691
1682 # hei_interf
1692 # hei_interf
1683 if hei_interf is None:
1693 if hei_interf is None:
1684 count_hei = int(num_hei / 2)
1694 count_hei = int(num_hei / 2)
1685 hei_interf = numpy.asmatrix(list(range(count_hei))) + num_hei - count_hei
1695 hei_interf = numpy.asmatrix(list(range(count_hei))) + num_hei - count_hei
1686 hei_interf = numpy.asarray(hei_interf)[0]
1696 hei_interf = numpy.asarray(hei_interf)[0]
1687 # nhei_interf
1697 # nhei_interf
1688 if (nhei_interf == None):
1698 if (nhei_interf == None):
1689 nhei_interf = 5
1699 nhei_interf = 5
1690 if (nhei_interf < 1):
1700 if (nhei_interf < 1):
1691 nhei_interf = 1
1701 nhei_interf = 1
1692 if (nhei_interf > count_hei):
1702 if (nhei_interf > count_hei):
1693 nhei_interf = count_hei
1703 nhei_interf = count_hei
1694 if (offhei_interf == None):
1704 if (offhei_interf == None):
1695 offhei_interf = 0
1705 offhei_interf = 0
1696
1706
1697 ind_hei = list(range(num_hei))
1707 ind_hei = list(range(num_hei))
1698 # mask_prof = numpy.asarray(range(num_prof - 2)) + 1
1708 # mask_prof = numpy.asarray(range(num_prof - 2)) + 1
1699 # mask_prof[range(num_prof/2 - 1,len(mask_prof))] += 1
1709 # mask_prof[range(num_prof/2 - 1,len(mask_prof))] += 1
1700 mask_prof = numpy.asarray(list(range(num_prof)))
1710 mask_prof = numpy.asarray(list(range(num_prof)))
1701 num_mask_prof = mask_prof.size
1711 num_mask_prof = mask_prof.size
1702 comp_mask_prof = [0, num_prof / 2]
1712 comp_mask_prof = [0, num_prof / 2]
1703
1713
1704 # noise_exist: Determina si la variable jnoise ha sido definida y contiene la informacion del ruido de cada canal
1714 # noise_exist: Determina si la variable jnoise ha sido definida y contiene la informacion del ruido de cada canal
1705 if (jnoise.size < num_channel or numpy.isnan(jnoise).any()):
1715 if (jnoise.size < num_channel or numpy.isnan(jnoise).any()):
1706 jnoise = numpy.nan
1716 jnoise = numpy.nan
1707 noise_exist = jnoise[0] < numpy.Inf
1717 noise_exist = jnoise[0] < numpy.Inf
1708
1718
1709 # Subrutina de Remocion de la Interferencia
1719 # Subrutina de Remocion de la Interferencia
1710 for ich in range(num_channel):
1720 for ich in range(num_channel):
1711 # Se ordena los espectros segun su potencia (menor a mayor)
1721 # Se ordena los espectros segun su potencia (menor a mayor)
1712 power = jspectra[ich, mask_prof, :]
1722 power = jspectra[ich, mask_prof, :]
1713 power = power[:, hei_interf]
1723 power = power[:, hei_interf]
1714 power = power.sum(axis=0)
1724 power = power.sum(axis=0)
1715 psort = power.ravel().argsort()
1725 psort = power.ravel().argsort()
1716
1726
1717 # Se estima la interferencia promedio en los Espectros de Potencia empleando
1727 # Se estima la interferencia promedio en los Espectros de Potencia empleando
1718 junkspc_interf = jspectra[ich, :, hei_interf[psort[list(range(
1728 junkspc_interf = jspectra[ich, :, hei_interf[psort[list(range(
1719 offhei_interf, nhei_interf + offhei_interf))]]]
1729 offhei_interf, nhei_interf + offhei_interf))]]]
1720
1730
1721 if noise_exist:
1731 if noise_exist:
1722 # tmp_noise = jnoise[ich] / num_prof
1732 # tmp_noise = jnoise[ich] / num_prof
1723 tmp_noise = jnoise[ich]
1733 tmp_noise = jnoise[ich]
1724 junkspc_interf = junkspc_interf - tmp_noise
1734 junkspc_interf = junkspc_interf - tmp_noise
1725 #junkspc_interf[:,comp_mask_prof] = 0
1735 #junkspc_interf[:,comp_mask_prof] = 0
1726
1736
1727 jspc_interf = junkspc_interf.sum(axis=0) / nhei_interf
1737 jspc_interf = junkspc_interf.sum(axis=0) / nhei_interf
1728 jspc_interf = jspc_interf.transpose()
1738 jspc_interf = jspc_interf.transpose()
1729 # Calculando el espectro de interferencia promedio
1739 # Calculando el espectro de interferencia promedio
1730 noiseid = numpy.where(
1740 noiseid = numpy.where(
1731 jspc_interf <= tmp_noise / numpy.sqrt(num_incoh))
1741 jspc_interf <= tmp_noise / numpy.sqrt(num_incoh))
1732 noiseid = noiseid[0]
1742 noiseid = noiseid[0]
1733 cnoiseid = noiseid.size
1743 cnoiseid = noiseid.size
1734 interfid = numpy.where(
1744 interfid = numpy.where(
1735 jspc_interf > tmp_noise / numpy.sqrt(num_incoh))
1745 jspc_interf > tmp_noise / numpy.sqrt(num_incoh))
1736 interfid = interfid[0]
1746 interfid = interfid[0]
1737 cinterfid = interfid.size
1747 cinterfid = interfid.size
1738
1748
1739 if (cnoiseid > 0):
1749 if (cnoiseid > 0):
1740 jspc_interf[noiseid] = 0
1750 jspc_interf[noiseid] = 0
1741
1751
1742 # Expandiendo los perfiles a limpiar
1752 # Expandiendo los perfiles a limpiar
1743 if (cinterfid > 0):
1753 if (cinterfid > 0):
1744 new_interfid = (
1754 new_interfid = (
1745 numpy.r_[interfid - 1, interfid, interfid + 1] + num_prof) % num_prof
1755 numpy.r_[interfid - 1, interfid, interfid + 1] + num_prof) % num_prof
1746 new_interfid = numpy.asarray(new_interfid)
1756 new_interfid = numpy.asarray(new_interfid)
1747 new_interfid = {x for x in new_interfid}
1757 new_interfid = {x for x in new_interfid}
1748 new_interfid = numpy.array(list(new_interfid))
1758 new_interfid = numpy.array(list(new_interfid))
1749 new_cinterfid = new_interfid.size
1759 new_cinterfid = new_interfid.size
1750 else:
1760 else:
1751 new_cinterfid = 0
1761 new_cinterfid = 0
1752
1762
1753 for ip in range(new_cinterfid):
1763 for ip in range(new_cinterfid):
1754 ind = junkspc_interf[:, new_interfid[ip]].ravel().argsort()
1764 ind = junkspc_interf[:, new_interfid[ip]].ravel().argsort()
1755 jspc_interf[new_interfid[ip]
1765 jspc_interf[new_interfid[ip]
1756 ] = junkspc_interf[ind[nhei_interf // 2], new_interfid[ip]]
1766 ] = junkspc_interf[ind[nhei_interf // 2], new_interfid[ip]]
1757
1767
1758 jspectra[ich, :, ind_hei] = jspectra[ich, :,
1768 jspectra[ich, :, ind_hei] = jspectra[ich, :,
1759 ind_hei] - jspc_interf # Corregir indices
1769 ind_hei] - jspc_interf # Corregir indices
1760
1770
1761 # Removiendo la interferencia del punto de mayor interferencia
1771 # Removiendo la interferencia del punto de mayor interferencia
1762 ListAux = jspc_interf[mask_prof].tolist()
1772 ListAux = jspc_interf[mask_prof].tolist()
1763 maxid = ListAux.index(max(ListAux))
1773 maxid = ListAux.index(max(ListAux))
1764
1774
1765 if cinterfid > 0:
1775 if cinterfid > 0:
1766 for ip in range(cinterfid * (interf == 2) - 1):
1776 for ip in range(cinterfid * (interf == 2) - 1):
1767 ind = (jspectra[ich, interfid[ip], :] < tmp_noise *
1777 ind = (jspectra[ich, interfid[ip], :] < tmp_noise *
1768 (1 + 1 / numpy.sqrt(num_incoh))).nonzero()
1778 (1 + 1 / numpy.sqrt(num_incoh))).nonzero()
1769 cind = len(ind)
1779 cind = len(ind)
1770
1780
1771 if (cind > 0):
1781 if (cind > 0):
1772 jspectra[ich, interfid[ip], ind] = tmp_noise * \
1782 jspectra[ich, interfid[ip], ind] = tmp_noise * \
1773 (1 + (numpy.random.uniform(cind) - 0.5) /
1783 (1 + (numpy.random.uniform(cind) - 0.5) /
1774 numpy.sqrt(num_incoh))
1784 numpy.sqrt(num_incoh))
1775
1785
1776 ind = numpy.array([-2, -1, 1, 2])
1786 ind = numpy.array([-2, -1, 1, 2])
1777 xx = numpy.zeros([4, 4])
1787 xx = numpy.zeros([4, 4])
1778
1788
1779 for id1 in range(4):
1789 for id1 in range(4):
1780 xx[:, id1] = ind[id1]**numpy.asarray(list(range(4)))
1790 xx[:, id1] = ind[id1]**numpy.asarray(list(range(4)))
1781
1791
1782 xx_inv = numpy.linalg.inv(xx)
1792 xx_inv = numpy.linalg.inv(xx)
1783 xx = xx_inv[:, 0]
1793 xx = xx_inv[:, 0]
1784 ind = (ind + maxid + num_mask_prof) % num_mask_prof
1794 ind = (ind + maxid + num_mask_prof) % num_mask_prof
1785 yy = jspectra[ich, mask_prof[ind], :]
1795 yy = jspectra[ich, mask_prof[ind], :]
1786 jspectra[ich, mask_prof[maxid], :] = numpy.dot(
1796 jspectra[ich, mask_prof[maxid], :] = numpy.dot(
1787 yy.transpose(), xx)
1797 yy.transpose(), xx)
1788
1798
1789 indAux = (jspectra[ich, :, :] < tmp_noise *
1799 indAux = (jspectra[ich, :, :] < tmp_noise *
1790 (1 - 1 / numpy.sqrt(num_incoh))).nonzero()
1800 (1 - 1 / numpy.sqrt(num_incoh))).nonzero()
1791 jspectra[ich, indAux[0], indAux[1]] = tmp_noise * \
1801 jspectra[ich, indAux[0], indAux[1]] = tmp_noise * \
1792 (1 - 1 / numpy.sqrt(num_incoh))
1802 (1 - 1 / numpy.sqrt(num_incoh))
1793
1803
1794 # Remocion de Interferencia en el Cross Spectra
1804 # Remocion de Interferencia en el Cross Spectra
1795 if jcspectra is None:
1805 if jcspectra is None:
1796 return jspectra, jcspectra
1806 return jspectra, jcspectra
1797 num_pairs = int(jcspectra.size / (num_prof * num_hei))
1807 num_pairs = int(jcspectra.size / (num_prof * num_hei))
1798 jcspectra = jcspectra.reshape(num_pairs, num_prof, num_hei)
1808 jcspectra = jcspectra.reshape(num_pairs, num_prof, num_hei)
1799
1809
1800 for ip in range(num_pairs):
1810 for ip in range(num_pairs):
1801
1811
1802 #-------------------------------------------
1812 #-------------------------------------------
1803
1813
1804 cspower = numpy.abs(jcspectra[ip, mask_prof, :])
1814 cspower = numpy.abs(jcspectra[ip, mask_prof, :])
1805 cspower = cspower[:, hei_interf]
1815 cspower = cspower[:, hei_interf]
1806 cspower = cspower.sum(axis=0)
1816 cspower = cspower.sum(axis=0)
1807
1817
1808 cspsort = cspower.ravel().argsort()
1818 cspsort = cspower.ravel().argsort()
1809 junkcspc_interf = jcspectra[ip, :, hei_interf[cspsort[list(range(
1819 junkcspc_interf = jcspectra[ip, :, hei_interf[cspsort[list(range(
1810 offhei_interf, nhei_interf + offhei_interf))]]]
1820 offhei_interf, nhei_interf + offhei_interf))]]]
1811 junkcspc_interf = junkcspc_interf.transpose()
1821 junkcspc_interf = junkcspc_interf.transpose()
1812 jcspc_interf = junkcspc_interf.sum(axis=1) / nhei_interf
1822 jcspc_interf = junkcspc_interf.sum(axis=1) / nhei_interf
1813
1823
1814 ind = numpy.abs(jcspc_interf[mask_prof]).ravel().argsort()
1824 ind = numpy.abs(jcspc_interf[mask_prof]).ravel().argsort()
1815
1825
1816 median_real = int(numpy.median(numpy.real(
1826 median_real = int(numpy.median(numpy.real(
1817 junkcspc_interf[mask_prof[ind[list(range(3 * num_prof // 4))]], :])))
1827 junkcspc_interf[mask_prof[ind[list(range(3 * num_prof // 4))]], :])))
1818 median_imag = int(numpy.median(numpy.imag(
1828 median_imag = int(numpy.median(numpy.imag(
1819 junkcspc_interf[mask_prof[ind[list(range(3 * num_prof // 4))]], :])))
1829 junkcspc_interf[mask_prof[ind[list(range(3 * num_prof // 4))]], :])))
1820 comp_mask_prof = [int(e) for e in comp_mask_prof]
1830 comp_mask_prof = [int(e) for e in comp_mask_prof]
1821 junkcspc_interf[comp_mask_prof, :] = numpy.complex(
1831 junkcspc_interf[comp_mask_prof, :] = numpy.complex(
1822 median_real, median_imag)
1832 median_real, median_imag)
1823
1833
1824 for iprof in range(num_prof):
1834 for iprof in range(num_prof):
1825 ind = numpy.abs(junkcspc_interf[iprof, :]).ravel().argsort()
1835 ind = numpy.abs(junkcspc_interf[iprof, :]).ravel().argsort()
1826 jcspc_interf[iprof] = junkcspc_interf[iprof, ind[nhei_interf // 2]]
1836 jcspc_interf[iprof] = junkcspc_interf[iprof, ind[nhei_interf // 2]]
1827
1837
1828 # Removiendo la Interferencia
1838 # Removiendo la Interferencia
1829 jcspectra[ip, :, ind_hei] = jcspectra[ip,
1839 jcspectra[ip, :, ind_hei] = jcspectra[ip,
1830 :, ind_hei] - jcspc_interf
1840 :, ind_hei] - jcspc_interf
1831
1841
1832 ListAux = numpy.abs(jcspc_interf[mask_prof]).tolist()
1842 ListAux = numpy.abs(jcspc_interf[mask_prof]).tolist()
1833 maxid = ListAux.index(max(ListAux))
1843 maxid = ListAux.index(max(ListAux))
1834
1844
1835 ind = numpy.array([-2, -1, 1, 2])
1845 ind = numpy.array([-2, -1, 1, 2])
1836 xx = numpy.zeros([4, 4])
1846 xx = numpy.zeros([4, 4])
1837
1847
1838 for id1 in range(4):
1848 for id1 in range(4):
1839 xx[:, id1] = ind[id1]**numpy.asarray(list(range(4)))
1849 xx[:, id1] = ind[id1]**numpy.asarray(list(range(4)))
1840
1850
1841 xx_inv = numpy.linalg.inv(xx)
1851 xx_inv = numpy.linalg.inv(xx)
1842 xx = xx_inv[:, 0]
1852 xx = xx_inv[:, 0]
1843
1853
1844 ind = (ind + maxid + num_mask_prof) % num_mask_prof
1854 ind = (ind + maxid + num_mask_prof) % num_mask_prof
1845 yy = jcspectra[ip, mask_prof[ind], :]
1855 yy = jcspectra[ip, mask_prof[ind], :]
1846 jcspectra[ip, mask_prof[maxid], :] = numpy.dot(yy.transpose(), xx)
1856 jcspectra[ip, mask_prof[maxid], :] = numpy.dot(yy.transpose(), xx)
1847
1857
1848 # Guardar Resultados
1858 # Guardar Resultados
1849 self.dataOut.data_spc = jspectra
1859 self.dataOut.data_spc = jspectra
1850 self.dataOut.data_cspc = jcspectra
1860 self.dataOut.data_cspc = jcspectra
1851
1861
1852 return 1
1862 return 1
1853
1863
1854 def run(self, dataOut, interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None, mode=1):
1864 def run(self, dataOut, interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None, mode=1):
1855
1865
1856 self.dataOut = dataOut
1866 self.dataOut = dataOut
1857
1867
1858 if mode == 1:
1868 if mode == 1:
1859 self.removeInterference(interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None)
1869 self.removeInterference(interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None)
1860 elif mode == 2:
1870 elif mode == 2:
1861 self.removeInterference2()
1871 self.removeInterference2()
1862
1872
1863 return self.dataOut
1873 return self.dataOut
1864
1874
1865
1875
1866 class IncohInt(Operation):
1876 class IncohInt(Operation):
1867
1877
1868 __profIndex = 0
1878 __profIndex = 0
1869 __withOverapping = False
1879 __withOverapping = False
1870
1880
1871 __byTime = False
1881 __byTime = False
1872 __initime = None
1882 __initime = None
1873 __lastdatatime = None
1883 __lastdatatime = None
1874 __integrationtime = None
1884 __integrationtime = None
1875
1885
1876 __buffer_spc = None
1886 __buffer_spc = None
1877 __buffer_cspc = None
1887 __buffer_cspc = None
1878 __buffer_dc = None
1888 __buffer_dc = None
1879
1889
1880 __dataReady = False
1890 __dataReady = False
1881
1891
1882 __timeInterval = None
1892 __timeInterval = None
1883 incohInt = 0
1893 incohInt = 0
1884 nOutliers = 0
1894 nOutliers = 0
1885 n = None
1895 n = None
1886
1896
1887 def __init__(self):
1897 def __init__(self):
1888
1898
1889 Operation.__init__(self)
1899 Operation.__init__(self)
1890
1900
1891 def setup(self, n=None, timeInterval=None, overlapping=False):
1901 def setup(self, n=None, timeInterval=None, overlapping=False):
1892 """
1902 """
1893 Set the parameters of the integration class.
1903 Set the parameters of the integration class.
1894
1904
1895 Inputs:
1905 Inputs:
1896
1906
1897 n : Number of coherent integrations
1907 n : Number of coherent integrations
1898 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
1908 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
1899 overlapping :
1909 overlapping :
1900
1910
1901 """
1911 """
1902
1912
1903 self.__initime = None
1913 self.__initime = None
1904 self.__lastdatatime = 0
1914 self.__lastdatatime = 0
1905
1915
1906 self.__buffer_spc = 0
1916 self.__buffer_spc = 0
1907 self.__buffer_cspc = 0
1917 self.__buffer_cspc = 0
1908 self.__buffer_dc = 0
1918 self.__buffer_dc = 0
1909
1919
1910 self.__profIndex = 0
1920 self.__profIndex = 0
1911 self.__dataReady = False
1921 self.__dataReady = False
1912 self.__byTime = False
1922 self.__byTime = False
1913 self.incohInt = 0
1923 self.incohInt = 0
1914 self.nOutliers = 0
1924 self.nOutliers = 0
1915 if n is None and timeInterval is None:
1925 if n is None and timeInterval is None:
1916 raise ValueError("n or timeInterval should be specified ...")
1926 raise ValueError("n or timeInterval should be specified ...")
1917
1927
1918 if n is not None:
1928 if n is not None:
1919 self.n = int(n)
1929 self.n = int(n)
1920 else:
1930 else:
1921
1931
1922 self.__integrationtime = int(timeInterval)
1932 self.__integrationtime = int(timeInterval)
1923 self.n = None
1933 self.n = None
1924 self.__byTime = True
1934 self.__byTime = True
1925
1935
1926 def putData(self, data_spc, data_cspc, data_dc):
1936 def putData(self, data_spc, data_cspc, data_dc):
1927 """
1937 """
1928 Add a profile to the __buffer_spc and increase in one the __profileIndex
1938 Add a profile to the __buffer_spc and increase in one the __profileIndex
1929
1939
1930 """
1940 """
1931 if data_spc.all() == numpy.nan :
1941 if data_spc.all() == numpy.nan :
1932 print("nan ")
1942 print("nan ")
1933 return
1943 return
1934 self.__buffer_spc += data_spc
1944 self.__buffer_spc += data_spc
1935
1945
1936 if data_cspc is None:
1946 if data_cspc is None:
1937 self.__buffer_cspc = None
1947 self.__buffer_cspc = None
1938 else:
1948 else:
1939 self.__buffer_cspc += data_cspc
1949 self.__buffer_cspc += data_cspc
1940
1950
1941 if data_dc is None:
1951 if data_dc is None:
1942 self.__buffer_dc = None
1952 self.__buffer_dc = None
1943 else:
1953 else:
1944 self.__buffer_dc += data_dc
1954 self.__buffer_dc += data_dc
1945
1955
1946 self.__profIndex += 1
1956 self.__profIndex += 1
1947
1957
1948 return
1958 return
1949
1959
1950 def pushData(self):
1960 def pushData(self):
1951 """
1961 """
1952 Return the sum of the last profiles and the profiles used in the sum.
1962 Return the sum of the last profiles and the profiles used in the sum.
1953
1963
1954 Affected:
1964 Affected:
1955
1965
1956 self.__profileIndex
1966 self.__profileIndex
1957
1967
1958 """
1968 """
1959
1969
1960 data_spc = self.__buffer_spc
1970 data_spc = self.__buffer_spc
1961 data_cspc = self.__buffer_cspc
1971 data_cspc = self.__buffer_cspc
1962 data_dc = self.__buffer_dc
1972 data_dc = self.__buffer_dc
1963 n = self.__profIndex
1973 n = self.__profIndex
1964
1974
1965 self.__buffer_spc = 0
1975 self.__buffer_spc = 0
1966 self.__buffer_cspc = 0
1976 self.__buffer_cspc = 0
1967 self.__buffer_dc = 0
1977 self.__buffer_dc = 0
1968
1978
1969
1979
1970 return data_spc, data_cspc, data_dc, n
1980 return data_spc, data_cspc, data_dc, n
1971
1981
1972 def byProfiles(self, *args):
1982 def byProfiles(self, *args):
1973
1983
1974 self.__dataReady = False
1984 self.__dataReady = False
1975 avgdata_spc = None
1985 avgdata_spc = None
1976 avgdata_cspc = None
1986 avgdata_cspc = None
1977 avgdata_dc = None
1987 avgdata_dc = None
1978
1988
1979 self.putData(*args)
1989 self.putData(*args)
1980
1990
1981 if self.__profIndex == self.n:
1991 if self.__profIndex == self.n:
1982
1992
1983 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1993 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1984 self.n = n
1994 self.n = n
1985 self.__dataReady = True
1995 self.__dataReady = True
1986
1996
1987 return avgdata_spc, avgdata_cspc, avgdata_dc
1997 return avgdata_spc, avgdata_cspc, avgdata_dc
1988
1998
1989 def byTime(self, datatime, *args):
1999 def byTime(self, datatime, *args):
1990
2000
1991 self.__dataReady = False
2001 self.__dataReady = False
1992 avgdata_spc = None
2002 avgdata_spc = None
1993 avgdata_cspc = None
2003 avgdata_cspc = None
1994 avgdata_dc = None
2004 avgdata_dc = None
1995
2005
1996 self.putData(*args)
2006 self.putData(*args)
1997
2007
1998 if (datatime - self.__initime) >= self.__integrationtime:
2008 if (datatime - self.__initime) >= self.__integrationtime:
1999 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
2009 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
2000 self.n = n
2010 self.n = n
2001 self.__dataReady = True
2011 self.__dataReady = True
2002
2012
2003 return avgdata_spc, avgdata_cspc, avgdata_dc
2013 return avgdata_spc, avgdata_cspc, avgdata_dc
2004
2014
2005 def integrate(self, datatime, *args):
2015 def integrate(self, datatime, *args):
2006
2016
2007 if self.__profIndex == 0:
2017 if self.__profIndex == 0:
2008 self.__initime = datatime
2018 self.__initime = datatime
2009
2019
2010 if self.__byTime:
2020 if self.__byTime:
2011 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(
2021 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(
2012 datatime, *args)
2022 datatime, *args)
2013 else:
2023 else:
2014 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
2024 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
2015
2025
2016 if not self.__dataReady:
2026 if not self.__dataReady:
2017 return None, None, None, None
2027 return None, None, None, None
2018
2028
2019 return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc
2029 return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc
2020
2030
2021 def run(self, dataOut, n=None, timeInterval=None, overlapping=False):
2031 def run(self, dataOut, n=None, timeInterval=None, overlapping=False):
2022 if n == 1:
2032 if n == 1:
2023 return dataOut
2033 return dataOut
2024
2034
2025 if dataOut.flagNoData == True:
2035 if dataOut.flagNoData == True:
2026 return dataOut
2036 return dataOut
2027
2037
2028 dataOut.flagNoData = True
2038 dataOut.flagNoData = True
2029
2039
2030 if not self.isConfig:
2040 if not self.isConfig:
2031 self.setup(n, timeInterval, overlapping)
2041 self.setup(n, timeInterval, overlapping)
2032 self.isConfig = True
2042 self.isConfig = True
2033
2043
2034 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime,
2044 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime,
2035 dataOut.data_spc,
2045 dataOut.data_spc,
2036 dataOut.data_cspc,
2046 dataOut.data_cspc,
2037 dataOut.data_dc)
2047 dataOut.data_dc)
2038 self.incohInt += dataOut.nIncohInt
2048 self.incohInt += dataOut.nIncohInt
2039 self.nOutliers += dataOut.data_outlier
2049 self.nOutliers += dataOut.data_outlier
2040 if self.__dataReady:
2050 if self.__dataReady:
2041 #print("prof: ",dataOut.max_nIncohInt,self.__profIndex)
2051 #print("prof: ",dataOut.max_nIncohInt,self.__profIndex)
2042 dataOut.data_spc = avgdata_spc
2052 dataOut.data_spc = avgdata_spc
2043 dataOut.data_cspc = avgdata_cspc
2053 dataOut.data_cspc = avgdata_cspc
2044 dataOut.data_dc = avgdata_dc
2054 dataOut.data_dc = avgdata_dc
2045 dataOut.nIncohInt = self.incohInt
2055 dataOut.nIncohInt = self.incohInt
2046 dataOut.data_outlier = self.nOutliers
2056 dataOut.data_outlier = self.nOutliers
2047 dataOut.utctime = avgdatatime
2057 dataOut.utctime = avgdatatime
2048 dataOut.flagNoData = False
2058 dataOut.flagNoData = False
2049 dataOut.max_nIncohInt += self.__profIndex
2059 dataOut.max_nIncohInt += self.__profIndex
2050 self.incohInt = 0
2060 self.incohInt = 0
2051 self.nOutliers = 0
2061 self.nOutliers = 0
2052 self.__profIndex = 0
2062 self.__profIndex = 0
2053
2063 #print("IncohInt Done")
2054 return dataOut
2064 return dataOut
2055
2065
2056 class dopplerFlip(Operation):
2066 class dopplerFlip(Operation):
2057
2067
2058 def run(self, dataOut):
2068 def run(self, dataOut):
2059 # arreglo 1: (num_chan, num_profiles, num_heights)
2069 # arreglo 1: (num_chan, num_profiles, num_heights)
2060 self.dataOut = dataOut
2070 self.dataOut = dataOut
2061 # JULIA-oblicua, indice 2
2071 # JULIA-oblicua, indice 2
2062 # arreglo 2: (num_profiles, num_heights)
2072 # arreglo 2: (num_profiles, num_heights)
2063 jspectra = self.dataOut.data_spc[2]
2073 jspectra = self.dataOut.data_spc[2]
2064 jspectra_tmp = numpy.zeros(jspectra.shape)
2074 jspectra_tmp = numpy.zeros(jspectra.shape)
2065 num_profiles = jspectra.shape[0]
2075 num_profiles = jspectra.shape[0]
2066 freq_dc = int(num_profiles / 2)
2076 freq_dc = int(num_profiles / 2)
2067 # Flip con for
2077 # Flip con for
2068 for j in range(num_profiles):
2078 for j in range(num_profiles):
2069 jspectra_tmp[num_profiles-j-1]= jspectra[j]
2079 jspectra_tmp[num_profiles-j-1]= jspectra[j]
2070 # Intercambio perfil de DC con perfil inmediato anterior
2080 # Intercambio perfil de DC con perfil inmediato anterior
2071 jspectra_tmp[freq_dc-1]= jspectra[freq_dc-1]
2081 jspectra_tmp[freq_dc-1]= jspectra[freq_dc-1]
2072 jspectra_tmp[freq_dc]= jspectra[freq_dc]
2082 jspectra_tmp[freq_dc]= jspectra[freq_dc]
2073 # canal modificado es re-escrito en el arreglo de canales
2083 # canal modificado es re-escrito en el arreglo de canales
2074 self.dataOut.data_spc[2] = jspectra_tmp
2084 self.dataOut.data_spc[2] = jspectra_tmp
2075
2085
2076 return self.dataOut
2086 return self.dataOut
@@ -1,2369 +1,2361
1 import sys
1 import sys
2 import numpy,math
2 import numpy,math
3 from scipy import interpolate
3 from scipy import interpolate
4 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
4 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
5 from schainpy.model.data.jrodata import Voltage,hildebrand_sekhon
5 from schainpy.model.data.jrodata import Voltage,hildebrand_sekhon
6 from schainpy.utils import log
6 from schainpy.utils import log
7 from schainpy.model.io.utils import getHei_index
7 from schainpy.model.io.utils import getHei_index
8 from time import time
8 from time import time
9 #import datetime
9 #import datetime
10 import numpy
10 import numpy
11 #import copy
11 #import copy
12 from schainpy.model.data import _noise
12 from schainpy.model.data import _noise
13
13
14 class VoltageProc(ProcessingUnit):
14 class VoltageProc(ProcessingUnit):
15
15
16 def __init__(self):
16 def __init__(self):
17
17
18 ProcessingUnit.__init__(self)
18 ProcessingUnit.__init__(self)
19
19
20 self.dataOut = Voltage()
20 self.dataOut = Voltage()
21 self.flip = 1
21 self.flip = 1
22 self.setupReq = False
22 self.setupReq = False
23
23
24 def run(self):
24 def run(self):
25 #print("running volt proc")
25 #print("running volt proc")
26 if self.dataIn.type == 'AMISR':
26 if self.dataIn.type == 'AMISR':
27 self.__updateObjFromAmisrInput()
27 self.__updateObjFromAmisrInput()
28
28
29 if self.dataOut.buffer_empty:
29 if self.dataOut.buffer_empty:
30 if self.dataIn.type == 'Voltage':
30 if self.dataIn.type == 'Voltage':
31 self.dataOut.copy(self.dataIn)
31 self.dataOut.copy(self.dataIn)
32 #print("new volts reading")
32 #print("new volts reading")
33
33
34
34
35 def __updateObjFromAmisrInput(self):
35 def __updateObjFromAmisrInput(self):
36
36
37 self.dataOut.timeZone = self.dataIn.timeZone
37 self.dataOut.timeZone = self.dataIn.timeZone
38 self.dataOut.dstFlag = self.dataIn.dstFlag
38 self.dataOut.dstFlag = self.dataIn.dstFlag
39 self.dataOut.errorCount = self.dataIn.errorCount
39 self.dataOut.errorCount = self.dataIn.errorCount
40 self.dataOut.useLocalTime = self.dataIn.useLocalTime
40 self.dataOut.useLocalTime = self.dataIn.useLocalTime
41
41
42 self.dataOut.flagNoData = self.dataIn.flagNoData
42 self.dataOut.flagNoData = self.dataIn.flagNoData
43 self.dataOut.data = self.dataIn.data
43 self.dataOut.data = self.dataIn.data
44 self.dataOut.utctime = self.dataIn.utctime
44 self.dataOut.utctime = self.dataIn.utctime
45 self.dataOut.channelList = self.dataIn.channelList
45 self.dataOut.channelList = self.dataIn.channelList
46 #self.dataOut.timeInterval = self.dataIn.timeInterval
46 #self.dataOut.timeInterval = self.dataIn.timeInterval
47 self.dataOut.heightList = self.dataIn.heightList
47 self.dataOut.heightList = self.dataIn.heightList
48 self.dataOut.nProfiles = self.dataIn.nProfiles
48 self.dataOut.nProfiles = self.dataIn.nProfiles
49
49
50 self.dataOut.nCohInt = self.dataIn.nCohInt
50 self.dataOut.nCohInt = self.dataIn.nCohInt
51 self.dataOut.ippSeconds = self.dataIn.ippSeconds
51 self.dataOut.ippSeconds = self.dataIn.ippSeconds
52 self.dataOut.frequency = self.dataIn.frequency
52 self.dataOut.frequency = self.dataIn.frequency
53
53
54 self.dataOut.azimuth = self.dataIn.azimuth
54 self.dataOut.azimuth = self.dataIn.azimuth
55 self.dataOut.zenith = self.dataIn.zenith
55 self.dataOut.zenith = self.dataIn.zenith
56
56
57 self.dataOut.beam.codeList = self.dataIn.beam.codeList
57 self.dataOut.beam.codeList = self.dataIn.beam.codeList
58 self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList
58 self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList
59 self.dataOut.beam.zenithList = self.dataIn.beam.zenithList
59 self.dataOut.beam.zenithList = self.dataIn.beam.zenithList
60
60
61
61
62 class selectChannels(Operation):
62 class selectChannels(Operation):
63
63
64 def run(self, dataOut, channelList=None):
64 def run(self, dataOut, channelList=None):
65 self.channelList = channelList
65 self.channelList = channelList
66 if self.channelList == None:
66 if self.channelList == None:
67 print("Missing channelList")
67 print("Missing channelList")
68 return dataOut
68 return dataOut
69 channelIndexList = []
69 channelIndexList = []
70
70
71 if type(dataOut.channelList) is not list: #leer array desde HDF5
71 if type(dataOut.channelList) is not list: #leer array desde HDF5
72 try:
72 try:
73 dataOut.channelList = dataOut.channelList.tolist()
73 dataOut.channelList = dataOut.channelList.tolist()
74 except Exception as e:
74 except Exception as e:
75 print("Select Channels: ",e)
75 print("Select Channels: ",e)
76 for channel in self.channelList:
76 for channel in self.channelList:
77 if channel not in dataOut.channelList:
77 if channel not in dataOut.channelList:
78 raise ValueError("Channel %d is not in %s" %(channel, str(dataOut.channelList)))
78 raise ValueError("Channel %d is not in %s" %(channel, str(dataOut.channelList)))
79
79
80 index = dataOut.channelList.index(channel)
80 index = dataOut.channelList.index(channel)
81 channelIndexList.append(index)
81 channelIndexList.append(index)
82 dataOut = self.selectChannelsByIndex(dataOut,channelIndexList)
82 dataOut = self.selectChannelsByIndex(dataOut,channelIndexList)
83 return dataOut
83 return dataOut
84
84
85 def selectChannelsByIndex(self, dataOut, channelIndexList):
85 def selectChannelsByIndex(self, dataOut, channelIndexList):
86 """
86 """
87 Selecciona un bloque de datos en base a canales segun el channelIndexList
87 Selecciona un bloque de datos en base a canales segun el channelIndexList
88
88
89 Input:
89 Input:
90 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
90 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
91
91
92 Affected:
92 Affected:
93 dataOut.data
93 dataOut.data
94 dataOut.channelIndexList
94 dataOut.channelIndexList
95 dataOut.nChannels
95 dataOut.nChannels
96 dataOut.m_ProcessingHeader.totalSpectra
96 dataOut.m_ProcessingHeader.totalSpectra
97 dataOut.systemHeaderObj.numChannels
97 dataOut.systemHeaderObj.numChannels
98 dataOut.m_ProcessingHeader.blockSize
98 dataOut.m_ProcessingHeader.blockSize
99
99
100 Return:
100 Return:
101 None
101 None
102 """
102 """
103 #print("selectChannelsByIndex")
103 #print("selectChannelsByIndex")
104 # for channelIndex in channelIndexList:
104 # for channelIndex in channelIndexList:
105 # if channelIndex not in dataOut.channelIndexList:
105 # if channelIndex not in dataOut.channelIndexList:
106 # raise ValueError("The value %d in channelIndexList is not valid" %channelIndex)
106 # raise ValueError("The value %d in channelIndexList is not valid" %channelIndex)
107
107
108 if dataOut.type == 'Voltage':
108 if dataOut.type == 'Voltage':
109 if dataOut.flagDataAsBlock:
109 if dataOut.flagDataAsBlock:
110 """
110 """
111 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
111 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
112 """
112 """
113 data = dataOut.data[channelIndexList,:,:]
113 data = dataOut.data[channelIndexList,:,:]
114 else:
114 else:
115 data = dataOut.data[channelIndexList,:]
115 data = dataOut.data[channelIndexList,:]
116
116
117 dataOut.data = data
117 dataOut.data = data
118 # dataOut.channelList = [dataOut.channelList[i] for i in channelIndexList]
118 # dataOut.channelList = [dataOut.channelList[i] for i in channelIndexList]
119 dataOut.channelList = range(len(channelIndexList))
119 dataOut.channelList = range(len(channelIndexList))
120
120
121 elif dataOut.type == 'Spectra':
121 elif dataOut.type == 'Spectra':
122 if hasattr(dataOut, 'data_spc'):
122 if hasattr(dataOut, 'data_spc'):
123 if dataOut.data_spc is None:
123 if dataOut.data_spc is None:
124 raise ValueError("data_spc is None")
124 raise ValueError("data_spc is None")
125 return dataOut
125 return dataOut
126 else:
126 else:
127 data_spc = dataOut.data_spc[channelIndexList, :]
127 data_spc = dataOut.data_spc[channelIndexList, :]
128 dataOut.data_spc = data_spc
128 dataOut.data_spc = data_spc
129
129
130 # if hasattr(dataOut, 'data_dc') :# and
130 # if hasattr(dataOut, 'data_dc') :# and
131 # if dataOut.data_dc is None:
131 # if dataOut.data_dc is None:
132 # raise ValueError("data_dc is None")
132 # raise ValueError("data_dc is None")
133 # return dataOut
133 # return dataOut
134 # else:
134 # else:
135 # data_dc = dataOut.data_dc[channelIndexList, :]
135 # data_dc = dataOut.data_dc[channelIndexList, :]
136 # dataOut.data_dc = data_dc
136 # dataOut.data_dc = data_dc
137 # dataOut.channelList = [dataOut.channelList[i] for i in channelIndexList]
137 # dataOut.channelList = [dataOut.channelList[i] for i in channelIndexList]
138 dataOut.channelList = channelIndexList
138 dataOut.channelList = channelIndexList
139 dataOut = self.__selectPairsByChannel(dataOut,channelIndexList)
139 dataOut = self.__selectPairsByChannel(dataOut,channelIndexList)
140
140
141 return dataOut
141 return dataOut
142
142
143 def __selectPairsByChannel(self, dataOut, channelList=None):
143 def __selectPairsByChannel(self, dataOut, channelList=None):
144 #print("__selectPairsByChannel")
144 #print("__selectPairsByChannel")
145 if channelList == None:
145 if channelList == None:
146 return
146 return
147
147
148 pairsIndexListSelected = []
148 pairsIndexListSelected = []
149 for pairIndex in dataOut.pairsIndexList:
149 for pairIndex in dataOut.pairsIndexList:
150 # First pair
150 # First pair
151 if dataOut.pairsList[pairIndex][0] not in channelList:
151 if dataOut.pairsList[pairIndex][0] not in channelList:
152 continue
152 continue
153 # Second pair
153 # Second pair
154 if dataOut.pairsList[pairIndex][1] not in channelList:
154 if dataOut.pairsList[pairIndex][1] not in channelList:
155 continue
155 continue
156
156
157 pairsIndexListSelected.append(pairIndex)
157 pairsIndexListSelected.append(pairIndex)
158 if not pairsIndexListSelected:
158 if not pairsIndexListSelected:
159 dataOut.data_cspc = None
159 dataOut.data_cspc = None
160 dataOut.pairsList = []
160 dataOut.pairsList = []
161 return
161 return
162
162
163 dataOut.data_cspc = dataOut.data_cspc[pairsIndexListSelected]
163 dataOut.data_cspc = dataOut.data_cspc[pairsIndexListSelected]
164 dataOut.pairsList = [dataOut.pairsList[i]
164 dataOut.pairsList = [dataOut.pairsList[i]
165 for i in pairsIndexListSelected]
165 for i in pairsIndexListSelected]
166
166
167 return dataOut
167 return dataOut
168
168
169 class selectHeights(Operation):
169 class selectHeights(Operation):
170
170
171 def run(self, dataOut, minHei=None, maxHei=None, minIndex=None, maxIndex=None):
171 def run(self, dataOut, minHei=None, maxHei=None, minIndex=None, maxIndex=None):
172 """
172 """
173 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
173 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
174 minHei <= height <= maxHei
174 minHei <= height <= maxHei
175
175
176 Input:
176 Input:
177 minHei : valor minimo de altura a considerar
177 minHei : valor minimo de altura a considerar
178 maxHei : valor maximo de altura a considerar
178 maxHei : valor maximo de altura a considerar
179
179
180 Affected:
180 Affected:
181 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
181 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
182
182
183 Return:
183 Return:
184 1 si el metodo se ejecuto con exito caso contrario devuelve 0
184 1 si el metodo se ejecuto con exito caso contrario devuelve 0
185 """
185 """
186
186
187 self.dataOut = dataOut
187 self.dataOut = dataOut
188
188
189 if minHei and maxHei:
189 if minHei and maxHei:
190
190
191 if (minHei < dataOut.heightList[0]):
191 if (minHei < dataOut.heightList[0]):
192 minHei = dataOut.heightList[0]
192 minHei = dataOut.heightList[0]
193
193
194 if (maxHei > dataOut.heightList[-1]):
194 if (maxHei > dataOut.heightList[-1]):
195 maxHei = dataOut.heightList[-1]
195 maxHei = dataOut.heightList[-1]
196
196
197 minIndex = 0
197 minIndex = 0
198 maxIndex = 0
198 maxIndex = 0
199 heights = dataOut.heightList
199 heights = dataOut.heightList
200
200
201 inda = numpy.where(heights >= minHei)
201 inda = numpy.where(heights >= minHei)
202 indb = numpy.where(heights <= maxHei)
202 indb = numpy.where(heights <= maxHei)
203
203
204 try:
204 try:
205 minIndex = inda[0][0]
205 minIndex = inda[0][0]
206 except:
206 except:
207 minIndex = 0
207 minIndex = 0
208
208
209 try:
209 try:
210 maxIndex = indb[0][-1]
210 maxIndex = indb[0][-1]
211 except:
211 except:
212 maxIndex = len(heights)
212 maxIndex = len(heights)
213
213
214 self.selectHeightsByIndex(minIndex, maxIndex)
214 self.selectHeightsByIndex(minIndex, maxIndex)
215
215
216 return dataOut
216 return dataOut
217
217
218 def selectHeightsByIndex(self, minIndex, maxIndex):
218 def selectHeightsByIndex(self, minIndex, maxIndex):
219 """
219 """
220 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
220 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
221 minIndex <= index <= maxIndex
221 minIndex <= index <= maxIndex
222
222
223 Input:
223 Input:
224 minIndex : valor de indice minimo de altura a considerar
224 minIndex : valor de indice minimo de altura a considerar
225 maxIndex : valor de indice maximo de altura a considerar
225 maxIndex : valor de indice maximo de altura a considerar
226
226
227 Affected:
227 Affected:
228 self.dataOut.data
228 self.dataOut.data
229 self.dataOut.heightList
229 self.dataOut.heightList
230
230
231 Return:
231 Return:
232 1 si el metodo se ejecuto con exito caso contrario devuelve 0
232 1 si el metodo se ejecuto con exito caso contrario devuelve 0
233 """
233 """
234
234
235 if self.dataOut.type == 'Voltage':
235 if self.dataOut.type == 'Voltage':
236 if (minIndex < 0) or (minIndex > maxIndex):
236 if (minIndex < 0) or (minIndex > maxIndex):
237 raise ValueError("Height index range (%d,%d) is not valid" % (minIndex, maxIndex))
237 raise ValueError("Height index range (%d,%d) is not valid" % (minIndex, maxIndex))
238
238
239 if (maxIndex >= self.dataOut.nHeights):
239 if (maxIndex >= self.dataOut.nHeights):
240 maxIndex = self.dataOut.nHeights
240 maxIndex = self.dataOut.nHeights
241
241
242 #voltage
242 #voltage
243 if self.dataOut.flagDataAsBlock:
243 if self.dataOut.flagDataAsBlock:
244 """
244 """
245 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
245 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
246 """
246 """
247 data = self.dataOut.data[:,:, minIndex:maxIndex]
247 data = self.dataOut.data[:,:, minIndex:maxIndex]
248 else:
248 else:
249 data = self.dataOut.data[:, minIndex:maxIndex]
249 data = self.dataOut.data[:, minIndex:maxIndex]
250
250
251 # firstHeight = self.dataOut.heightList[minIndex]
251 # firstHeight = self.dataOut.heightList[minIndex]
252
252
253 self.dataOut.data = data
253 self.dataOut.data = data
254 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex]
254 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex]
255
255
256 if self.dataOut.nHeights <= 1:
256 if self.dataOut.nHeights <= 1:
257 raise ValueError("selectHeights: Too few heights. Current number of heights is %d" %(self.dataOut.nHeights))
257 raise ValueError("selectHeights: Too few heights. Current number of heights is %d" %(self.dataOut.nHeights))
258 elif self.dataOut.type == 'Spectra':
258 elif self.dataOut.type == 'Spectra':
259 if (minIndex < 0) or (minIndex > maxIndex):
259 if (minIndex < 0) or (minIndex > maxIndex):
260 raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % (
260 raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % (
261 minIndex, maxIndex))
261 minIndex, maxIndex))
262
262
263 if (maxIndex >= self.dataOut.nHeights):
263 if (maxIndex >= self.dataOut.nHeights):
264 maxIndex = self.dataOut.nHeights - 1
264 maxIndex = self.dataOut.nHeights - 1
265
265
266 # Spectra
266 # Spectra
267 data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1]
267 data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1]
268
268
269 data_cspc = None
269 data_cspc = None
270 if self.dataOut.data_cspc is not None:
270 if self.dataOut.data_cspc is not None:
271 data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1]
271 data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1]
272
272
273 data_dc = None
273 data_dc = None
274 if self.dataOut.data_dc is not None:
274 if self.dataOut.data_dc is not None:
275 data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1]
275 data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1]
276
276
277 self.dataOut.data_spc = data_spc
277 self.dataOut.data_spc = data_spc
278 self.dataOut.data_cspc = data_cspc
278 self.dataOut.data_cspc = data_cspc
279 self.dataOut.data_dc = data_dc
279 self.dataOut.data_dc = data_dc
280
280
281 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex + 1]
281 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex + 1]
282
282
283 return 1
283 return 1
284
284
285
285
286 class filterByHeights(Operation):
286 class filterByHeights(Operation):
287
287
288 def run(self, dataOut, window):
288 def run(self, dataOut, window):
289
289
290 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
290 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
291
291
292 if window == None:
292 if window == None:
293 window = (dataOut.radarControllerHeaderObj.txA/dataOut.radarControllerHeaderObj.nBaud) / deltaHeight
293 window = (dataOut.radarControllerHeaderObj.txA/dataOut.radarControllerHeaderObj.nBaud) / deltaHeight
294
294
295 newdelta = deltaHeight * window
295 newdelta = deltaHeight * window
296 r = dataOut.nHeights % window
296 r = dataOut.nHeights % window
297 newheights = (dataOut.nHeights-r)/window
297 newheights = (dataOut.nHeights-r)/window
298
298
299 if newheights <= 1:
299 if newheights <= 1:
300 raise ValueError("filterByHeights: Too few heights. Current number of heights is %d and window is %d" %(dataOut.nHeights, window))
300 raise ValueError("filterByHeights: Too few heights. Current number of heights is %d and window is %d" %(dataOut.nHeights, window))
301
301
302 if dataOut.flagDataAsBlock:
302 if dataOut.flagDataAsBlock:
303 """
303 """
304 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
304 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
305 """
305 """
306 buffer = dataOut.data[:, :, 0:int(dataOut.nHeights-r)]
306 buffer = dataOut.data[:, :, 0:int(dataOut.nHeights-r)]
307 buffer = buffer.reshape(dataOut.nChannels, dataOut.nProfiles, int(dataOut.nHeights/window), window)
307 buffer = buffer.reshape(dataOut.nChannels, dataOut.nProfiles, int(dataOut.nHeights/window), window)
308 buffer = numpy.sum(buffer,3)
308 buffer = numpy.sum(buffer,3)
309
309
310 else:
310 else:
311 buffer = dataOut.data[:,0:int(dataOut.nHeights-r)]
311 buffer = dataOut.data[:,0:int(dataOut.nHeights-r)]
312 buffer = buffer.reshape(dataOut.nChannels,int(dataOut.nHeights/window),int(window))
312 buffer = buffer.reshape(dataOut.nChannels,int(dataOut.nHeights/window),int(window))
313 buffer = numpy.sum(buffer,2)
313 buffer = numpy.sum(buffer,2)
314
314
315 dataOut.data = buffer
315 dataOut.data = buffer
316 dataOut.heightList = dataOut.heightList[0] + numpy.arange( newheights )*newdelta
316 dataOut.heightList = dataOut.heightList[0] + numpy.arange( newheights )*newdelta
317 dataOut.windowOfFilter = window
317 dataOut.windowOfFilter = window
318
318
319 return dataOut
319 return dataOut
320
320
321
321
322 class setH0(Operation):
322 class setH0(Operation):
323
323
324 def run(self, dataOut, h0, deltaHeight = None):
324 def run(self, dataOut, h0, deltaHeight = None):
325
325
326 if not deltaHeight:
326 if not deltaHeight:
327 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
327 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
328
328
329 nHeights = dataOut.nHeights
329 nHeights = dataOut.nHeights
330
330
331 newHeiRange = h0 + numpy.arange(nHeights)*deltaHeight
331 newHeiRange = h0 + numpy.arange(nHeights)*deltaHeight
332
332
333 dataOut.heightList = newHeiRange
333 dataOut.heightList = newHeiRange
334
334
335 return dataOut
335 return dataOut
336
336
337
337
338 class deFlip(Operation):
338 class deFlip(Operation):
339
339
340 def run(self, dataOut, channelList = []):
340 def run(self, dataOut, channelList = []):
341
341
342 data = dataOut.data.copy()
342 data = dataOut.data.copy()
343
343
344 if dataOut.flagDataAsBlock:
344 if dataOut.flagDataAsBlock:
345 flip = self.flip
345 flip = self.flip
346 profileList = list(range(dataOut.nProfiles))
346 profileList = list(range(dataOut.nProfiles))
347
347
348 if not channelList:
348 if not channelList:
349 for thisProfile in profileList:
349 for thisProfile in profileList:
350 data[:,thisProfile,:] = data[:,thisProfile,:]*flip
350 data[:,thisProfile,:] = data[:,thisProfile,:]*flip
351 flip *= -1.0
351 flip *= -1.0
352 else:
352 else:
353 for thisChannel in channelList:
353 for thisChannel in channelList:
354 if thisChannel not in dataOut.channelList:
354 if thisChannel not in dataOut.channelList:
355 continue
355 continue
356
356
357 for thisProfile in profileList:
357 for thisProfile in profileList:
358 data[thisChannel,thisProfile,:] = data[thisChannel,thisProfile,:]*flip
358 data[thisChannel,thisProfile,:] = data[thisChannel,thisProfile,:]*flip
359 flip *= -1.0
359 flip *= -1.0
360
360
361 self.flip = flip
361 self.flip = flip
362
362
363 else:
363 else:
364 if not channelList:
364 if not channelList:
365 data[:,:] = data[:,:]*self.flip
365 data[:,:] = data[:,:]*self.flip
366 else:
366 else:
367 for thisChannel in channelList:
367 for thisChannel in channelList:
368 if thisChannel not in dataOut.channelList:
368 if thisChannel not in dataOut.channelList:
369 continue
369 continue
370
370
371 data[thisChannel,:] = data[thisChannel,:]*self.flip
371 data[thisChannel,:] = data[thisChannel,:]*self.flip
372
372
373 self.flip *= -1.
373 self.flip *= -1.
374
374
375 dataOut.data = data
375 dataOut.data = data
376
376
377 return dataOut
377 return dataOut
378
378
379
379
380 class setAttribute(Operation):
380 class setAttribute(Operation):
381 '''
381 '''
382 Set an arbitrary attribute(s) to dataOut
382 Set an arbitrary attribute(s) to dataOut
383 '''
383 '''
384
384
385 def __init__(self):
385 def __init__(self):
386
386
387 Operation.__init__(self)
387 Operation.__init__(self)
388 self._ready = False
388 self._ready = False
389
389
390 def run(self, dataOut, **kwargs):
390 def run(self, dataOut, **kwargs):
391
391
392 for key, value in kwargs.items():
392 for key, value in kwargs.items():
393 setattr(dataOut, key, value)
393 setattr(dataOut, key, value)
394
394
395 return dataOut
395 return dataOut
396
396
397
397
398 @MPDecorator
398 @MPDecorator
399 class printAttribute(Operation):
399 class printAttribute(Operation):
400 '''
400 '''
401 Print an arbitrary attribute of dataOut
401 Print an arbitrary attribute of dataOut
402 '''
402 '''
403
403
404 def __init__(self):
404 def __init__(self):
405
405
406 Operation.__init__(self)
406 Operation.__init__(self)
407
407
408 def run(self, dataOut, attributes):
408 def run(self, dataOut, attributes):
409
409
410 if isinstance(attributes, str):
410 if isinstance(attributes, str):
411 attributes = [attributes]
411 attributes = [attributes]
412 for attr in attributes:
412 for attr in attributes:
413 if hasattr(dataOut, attr):
413 if hasattr(dataOut, attr):
414 log.log(getattr(dataOut, attr), attr)
414 log.log(getattr(dataOut, attr), attr)
415
415
416
416
417 class interpolateHeights(Operation):
417 class interpolateHeights(Operation):
418
418
419 def run(self, dataOut, topLim, botLim):
419 def run(self, dataOut, topLim, botLim):
420 #69 al 72 para julia
420 #69 al 72 para julia
421 #82-84 para meteoros
421 #82-84 para meteoros
422 if len(numpy.shape(dataOut.data))==2:
422 if len(numpy.shape(dataOut.data))==2:
423 sampInterp = (dataOut.data[:,botLim-1] + dataOut.data[:,topLim+1])/2
423 sampInterp = (dataOut.data[:,botLim-1] + dataOut.data[:,topLim+1])/2
424 sampInterp = numpy.transpose(numpy.tile(sampInterp,(topLim-botLim + 1,1)))
424 sampInterp = numpy.transpose(numpy.tile(sampInterp,(topLim-botLim + 1,1)))
425 #dataOut.data[:,botLim:limSup+1] = sampInterp
425 #dataOut.data[:,botLim:limSup+1] = sampInterp
426 dataOut.data[:,botLim:topLim+1] = sampInterp
426 dataOut.data[:,botLim:topLim+1] = sampInterp
427 else:
427 else:
428 nHeights = dataOut.data.shape[2]
428 nHeights = dataOut.data.shape[2]
429 x = numpy.hstack((numpy.arange(botLim),numpy.arange(topLim+1,nHeights)))
429 x = numpy.hstack((numpy.arange(botLim),numpy.arange(topLim+1,nHeights)))
430 y = dataOut.data[:,:,list(range(botLim))+list(range(topLim+1,nHeights))]
430 y = dataOut.data[:,:,list(range(botLim))+list(range(topLim+1,nHeights))]
431 f = interpolate.interp1d(x, y, axis = 2)
431 f = interpolate.interp1d(x, y, axis = 2)
432 xnew = numpy.arange(botLim,topLim+1)
432 xnew = numpy.arange(botLim,topLim+1)
433 ynew = f(xnew)
433 ynew = f(xnew)
434 dataOut.data[:,:,botLim:topLim+1] = ynew
434 dataOut.data[:,:,botLim:topLim+1] = ynew
435
435
436 return dataOut
436 return dataOut
437
437
438
438
439 class CohInt(Operation):
439 class CohInt(Operation):
440
440
441 isConfig = False
441 isConfig = False
442 __profIndex = 0
442 __profIndex = 0
443 __byTime = False
443 __byTime = False
444 __initime = None
444 __initime = None
445 __lastdatatime = None
445 __lastdatatime = None
446 __integrationtime = None
446 __integrationtime = None
447 __buffer = None
447 __buffer = None
448 __bufferStride = []
448 __bufferStride = []
449 __dataReady = False
449 __dataReady = False
450 __profIndexStride = 0
450 __profIndexStride = 0
451 __dataToPutStride = False
451 __dataToPutStride = False
452 n = None
452 n = None
453
453
454 def __init__(self, **kwargs):
454 def __init__(self, **kwargs):
455
455
456 Operation.__init__(self, **kwargs)
456 Operation.__init__(self, **kwargs)
457
457
458 def setup(self, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False):
458 def setup(self, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False):
459 """
459 """
460 Set the parameters of the integration class.
460 Set the parameters of the integration class.
461
461
462 Inputs:
462 Inputs:
463
463
464 n : Number of coherent integrations
464 n : Number of coherent integrations
465 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
465 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
466 overlapping :
466 overlapping :
467 """
467 """
468
468
469 self.__initime = None
469 self.__initime = None
470 self.__lastdatatime = 0
470 self.__lastdatatime = 0
471 self.__buffer = None
471 self.__buffer = None
472 self.__dataReady = False
472 self.__dataReady = False
473 self.byblock = byblock
473 self.byblock = byblock
474 self.stride = stride
474 self.stride = stride
475
475
476 if n == None and timeInterval == None:
476 if n == None and timeInterval == None:
477 raise ValueError("n or timeInterval should be specified ...")
477 raise ValueError("n or timeInterval should be specified ...")
478
478
479 if n != None:
479 if n != None:
480 self.n = n
480 self.n = n
481 self.__byTime = False
481 self.__byTime = False
482 else:
482 else:
483 self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line
483 self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line
484 self.n = 9999
484 self.n = 9999
485 self.__byTime = True
485 self.__byTime = True
486
486
487 if overlapping:
487 if overlapping:
488 self.__withOverlapping = True
488 self.__withOverlapping = True
489 self.__buffer = None
489 self.__buffer = None
490 else:
490 else:
491 self.__withOverlapping = False
491 self.__withOverlapping = False
492 self.__buffer = 0
492 self.__buffer = 0
493
493
494 self.__profIndex = 0
494 self.__profIndex = 0
495
495
496 def putData(self, data):
496 def putData(self, data):
497
497
498 """
498 """
499 Add a profile to the __buffer and increase in one the __profileIndex
499 Add a profile to the __buffer and increase in one the __profileIndex
500
500
501 """
501 """
502
502
503 if not self.__withOverlapping:
503 if not self.__withOverlapping:
504 self.__buffer += data.copy()
504 self.__buffer += data.copy()
505 self.__profIndex += 1
505 self.__profIndex += 1
506 return
506 return
507
507
508 #Overlapping data
508 #Overlapping data
509 nChannels, nHeis = data.shape
509 nChannels, nHeis = data.shape
510 data = numpy.reshape(data, (1, nChannels, nHeis))
510 data = numpy.reshape(data, (1, nChannels, nHeis))
511
511
512 #If the buffer is empty then it takes the data value
512 #If the buffer is empty then it takes the data value
513 if self.__buffer is None:
513 if self.__buffer is None:
514 self.__buffer = data
514 self.__buffer = data
515 self.__profIndex += 1
515 self.__profIndex += 1
516 return
516 return
517
517
518 #If the buffer length is lower than n then stakcing the data value
518 #If the buffer length is lower than n then stakcing the data value
519 if self.__profIndex < self.n:
519 if self.__profIndex < self.n:
520 self.__buffer = numpy.vstack((self.__buffer, data))
520 self.__buffer = numpy.vstack((self.__buffer, data))
521 self.__profIndex += 1
521 self.__profIndex += 1
522 return
522 return
523
523
524 #If the buffer length is equal to n then replacing the last buffer value with the data value
524 #If the buffer length is equal to n then replacing the last buffer value with the data value
525 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
525 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
526 self.__buffer[self.n-1] = data
526 self.__buffer[self.n-1] = data
527 self.__profIndex = self.n
527 self.__profIndex = self.n
528 return
528 return
529
529
530
530
531 def pushData(self):
531 def pushData(self):
532 """
532 """
533 Return the sum of the last profiles and the profiles used in the sum.
533 Return the sum of the last profiles and the profiles used in the sum.
534
534
535 Affected:
535 Affected:
536
536
537 self.__profileIndex
537 self.__profileIndex
538
538
539 """
539 """
540
540
541 if not self.__withOverlapping:
541 if not self.__withOverlapping:
542 data = self.__buffer
542 data = self.__buffer
543 n = self.__profIndex
543 n = self.__profIndex
544
544
545 self.__buffer = 0
545 self.__buffer = 0
546 self.__profIndex = 0
546 self.__profIndex = 0
547
547
548 return data, n
548 return data, n
549
549
550 #Integration with Overlapping
550 #Integration with Overlapping
551 data = numpy.sum(self.__buffer, axis=0)
551 data = numpy.sum(self.__buffer, axis=0)
552 # print data
552 # print data
553 # raise
553 # raise
554 n = self.__profIndex
554 n = self.__profIndex
555
555
556 return data, n
556 return data, n
557
557
558 def byProfiles(self, data):
558 def byProfiles(self, data):
559
559
560 self.__dataReady = False
560 self.__dataReady = False
561 avgdata = None
561 avgdata = None
562 # n = None
562 # n = None
563 # print data
563 # print data
564 # raise
564 # raise
565 self.putData(data)
565 self.putData(data)
566
566
567 if self.__profIndex == self.n:
567 if self.__profIndex == self.n:
568 avgdata, n = self.pushData()
568 avgdata, n = self.pushData()
569 self.__dataReady = True
569 self.__dataReady = True
570
570
571 return avgdata
571 return avgdata
572
572
573 def byTime(self, data, datatime):
573 def byTime(self, data, datatime):
574
574
575 self.__dataReady = False
575 self.__dataReady = False
576 avgdata = None
576 avgdata = None
577 n = None
577 n = None
578
578
579 self.putData(data)
579 self.putData(data)
580
580
581 if (datatime - self.__initime) >= self.__integrationtime:
581 if (datatime - self.__initime) >= self.__integrationtime:
582 avgdata, n = self.pushData()
582 avgdata, n = self.pushData()
583 self.n = n
583 self.n = n
584 self.__dataReady = True
584 self.__dataReady = True
585
585
586 return avgdata
586 return avgdata
587
587
588 def integrateByStride(self, data, datatime):
588 def integrateByStride(self, data, datatime):
589 # print data
589 # print data
590 if self.__profIndex == 0:
590 if self.__profIndex == 0:
591 self.__buffer = [[data.copy(), datatime]]
591 self.__buffer = [[data.copy(), datatime]]
592 else:
592 else:
593 self.__buffer.append([data.copy(),datatime])
593 self.__buffer.append([data.copy(),datatime])
594 self.__profIndex += 1
594 self.__profIndex += 1
595 self.__dataReady = False
595 self.__dataReady = False
596
596
597 if self.__profIndex == self.n * self.stride :
597 if self.__profIndex == self.n * self.stride :
598 self.__dataToPutStride = True
598 self.__dataToPutStride = True
599 self.__profIndexStride = 0
599 self.__profIndexStride = 0
600 self.__profIndex = 0
600 self.__profIndex = 0
601 self.__bufferStride = []
601 self.__bufferStride = []
602 for i in range(self.stride):
602 for i in range(self.stride):
603 current = self.__buffer[i::self.stride]
603 current = self.__buffer[i::self.stride]
604 data = numpy.sum([t[0] for t in current], axis=0)
604 data = numpy.sum([t[0] for t in current], axis=0)
605 avgdatatime = numpy.average([t[1] for t in current])
605 avgdatatime = numpy.average([t[1] for t in current])
606 # print data
606 # print data
607 self.__bufferStride.append((data, avgdatatime))
607 self.__bufferStride.append((data, avgdatatime))
608
608
609 if self.__dataToPutStride:
609 if self.__dataToPutStride:
610 self.__dataReady = True
610 self.__dataReady = True
611 self.__profIndexStride += 1
611 self.__profIndexStride += 1
612 if self.__profIndexStride == self.stride:
612 if self.__profIndexStride == self.stride:
613 self.__dataToPutStride = False
613 self.__dataToPutStride = False
614 # print self.__bufferStride[self.__profIndexStride - 1]
614 # print self.__bufferStride[self.__profIndexStride - 1]
615 # raise
615 # raise
616 return self.__bufferStride[self.__profIndexStride - 1]
616 return self.__bufferStride[self.__profIndexStride - 1]
617
617
618
618
619 return None, None
619 return None, None
620
620
621 def integrate(self, data, datatime=None):
621 def integrate(self, data, datatime=None):
622
622
623 if self.__initime == None:
623 if self.__initime == None:
624 self.__initime = datatime
624 self.__initime = datatime
625
625
626 if self.__byTime:
626 if self.__byTime:
627 avgdata = self.byTime(data, datatime)
627 avgdata = self.byTime(data, datatime)
628 else:
628 else:
629 avgdata = self.byProfiles(data)
629 avgdata = self.byProfiles(data)
630
630
631
631
632 self.__lastdatatime = datatime
632 self.__lastdatatime = datatime
633
633
634 if avgdata is None:
634 if avgdata is None:
635 return None, None
635 return None, None
636
636
637 avgdatatime = self.__initime
637 avgdatatime = self.__initime
638
638
639 deltatime = datatime - self.__lastdatatime
639 deltatime = datatime - self.__lastdatatime
640
640
641 if not self.__withOverlapping:
641 if not self.__withOverlapping:
642 self.__initime = datatime
642 self.__initime = datatime
643 else:
643 else:
644 self.__initime += deltatime
644 self.__initime += deltatime
645
645
646 return avgdata, avgdatatime
646 return avgdata, avgdatatime
647
647
648 def integrateByBlock(self, dataOut):
648 def integrateByBlock(self, dataOut):
649
649
650 times = int(dataOut.data.shape[1]/self.n)
650 times = int(dataOut.data.shape[1]/self.n)
651 avgdata = numpy.zeros((dataOut.nChannels, times, dataOut.nHeights), dtype=numpy.complex)
651 avgdata = numpy.zeros((dataOut.nChannels, times, dataOut.nHeights), dtype=numpy.complex)
652
652
653 id_min = 0
653 id_min = 0
654 id_max = self.n
654 id_max = self.n
655
655
656 for i in range(times):
656 for i in range(times):
657 junk = dataOut.data[:,id_min:id_max,:]
657 junk = dataOut.data[:,id_min:id_max,:]
658 avgdata[:,i,:] = junk.sum(axis=1)
658 avgdata[:,i,:] = junk.sum(axis=1)
659 id_min += self.n
659 id_min += self.n
660 id_max += self.n
660 id_max += self.n
661
661
662 timeInterval = dataOut.ippSeconds*self.n
662 timeInterval = dataOut.ippSeconds*self.n
663 avgdatatime = (times - 1) * timeInterval + dataOut.utctime
663 avgdatatime = (times - 1) * timeInterval + dataOut.utctime
664 self.__dataReady = True
664 self.__dataReady = True
665 return avgdata, avgdatatime
665 return avgdata, avgdatatime
666
666
667 def run(self, dataOut, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False, **kwargs):
667 def run(self, dataOut, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False, **kwargs):
668
668
669 if not self.isConfig:
669 if not self.isConfig:
670 self.setup(n=n, stride=stride, timeInterval=timeInterval, overlapping=overlapping, byblock=byblock, **kwargs)
670 self.setup(n=n, stride=stride, timeInterval=timeInterval, overlapping=overlapping, byblock=byblock, **kwargs)
671 self.isConfig = True
671 self.isConfig = True
672
672
673 if dataOut.flagDataAsBlock:
673 if dataOut.flagDataAsBlock:
674 """
674 """
675 Si la data es leida por bloques, dimension = [nChannels, nProfiles, nHeis]
675 Si la data es leida por bloques, dimension = [nChannels, nProfiles, nHeis]
676 """
676 """
677 avgdata, avgdatatime = self.integrateByBlock(dataOut)
677 avgdata, avgdatatime = self.integrateByBlock(dataOut)
678 dataOut.nProfiles /= self.n
678 dataOut.nProfiles /= self.n
679 else:
679 else:
680 if stride is None:
680 if stride is None:
681 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
681 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
682 else:
682 else:
683 avgdata, avgdatatime = self.integrateByStride(dataOut.data, dataOut.utctime)
683 avgdata, avgdatatime = self.integrateByStride(dataOut.data, dataOut.utctime)
684
684
685
685
686 # dataOut.timeInterval *= n
686 # dataOut.timeInterval *= n
687 dataOut.flagNoData = True
687 dataOut.flagNoData = True
688
688
689 if self.__dataReady:
689 if self.__dataReady:
690 dataOut.data = avgdata
690 dataOut.data = avgdata
691 if not dataOut.flagCohInt:
691 if not dataOut.flagCohInt:
692 dataOut.nCohInt *= self.n
692 dataOut.nCohInt *= self.n
693 dataOut.flagCohInt = True
693 dataOut.flagCohInt = True
694 dataOut.utctime = avgdatatime
694 dataOut.utctime = avgdatatime
695 # print avgdata, avgdatatime
695 # print avgdata, avgdatatime
696 # raise
696 # raise
697 # dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
697 # dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
698 dataOut.flagNoData = False
698 dataOut.flagNoData = False
699 return dataOut
699 return dataOut
700
700
701 class Decoder(Operation):
701 class Decoder(Operation):
702
702
703 isConfig = False
703 isConfig = False
704 __profIndex = 0
704 __profIndex = 0
705
705
706 code = None
706 code = None
707
707
708 nCode = None
708 nCode = None
709 nBaud = None
709 nBaud = None
710
710
711 def __init__(self, **kwargs):
711 def __init__(self, **kwargs):
712
712
713 Operation.__init__(self, **kwargs)
713 Operation.__init__(self, **kwargs)
714
714
715 self.times = None
715 self.times = None
716 self.osamp = None
716 self.osamp = None
717 # self.__setValues = False
717 # self.__setValues = False
718 self.isConfig = False
718 self.isConfig = False
719 self.setupReq = False
719 self.setupReq = False
720 def setup(self, code, osamp, dataOut):
720 def setup(self, code, osamp, dataOut):
721
721
722 self.__profIndex = 0
722 self.__profIndex = 0
723
723
724 self.code = code
724 self.code = code
725
725
726 self.nCode = len(code)
726 self.nCode = len(code)
727 self.nBaud = len(code[0])
727 self.nBaud = len(code[0])
728 if (osamp != None) and (osamp >1):
728 if (osamp != None) and (osamp >1):
729 self.osamp = osamp
729 self.osamp = osamp
730 self.code = numpy.repeat(code, repeats=self.osamp, axis=1)
730 self.code = numpy.repeat(code, repeats=self.osamp, axis=1)
731 self.nBaud = self.nBaud*self.osamp
731 self.nBaud = self.nBaud*self.osamp
732
732
733 self.__nChannels = dataOut.nChannels
733 self.__nChannels = dataOut.nChannels
734 self.__nProfiles = dataOut.nProfiles
734 self.__nProfiles = dataOut.nProfiles
735 self.__nHeis = dataOut.nHeights
735 self.__nHeis = dataOut.nHeights
736
736
737 if self.__nHeis < self.nBaud:
737 if self.__nHeis < self.nBaud:
738 raise ValueError('Number of heights (%d) should be greater than number of bauds (%d)' %(self.__nHeis, self.nBaud))
738 raise ValueError('Number of heights (%d) should be greater than number of bauds (%d)' %(self.__nHeis, self.nBaud))
739
739
740 #Frequency
740 #Frequency
741 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
741 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
742
742
743 __codeBuffer[:,0:self.nBaud] = self.code
743 __codeBuffer[:,0:self.nBaud] = self.code
744
744
745 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
745 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
746
746
747 if dataOut.flagDataAsBlock:
747 if dataOut.flagDataAsBlock:
748
748
749 self.ndatadec = self.__nHeis #- self.nBaud + 1
749 self.ndatadec = self.__nHeis #- self.nBaud + 1
750
750
751 self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex)
751 self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex)
752
752
753 else:
753 else:
754
754
755 #Time
755 #Time
756 self.ndatadec = self.__nHeis #- self.nBaud + 1
756 self.ndatadec = self.__nHeis #- self.nBaud + 1
757
757
758 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
758 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
759
759
760 def __convolutionInFreq(self, data):
760 def __convolutionInFreq(self, data):
761
761
762 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
762 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
763
763
764 fft_data = numpy.fft.fft(data, axis=1)
764 fft_data = numpy.fft.fft(data, axis=1)
765
765
766 conv = fft_data*fft_code
766 conv = fft_data*fft_code
767
767
768 data = numpy.fft.ifft(conv,axis=1)
768 data = numpy.fft.ifft(conv,axis=1)
769
769
770 return data
770 return data
771
771
772 def __convolutionInFreqOpt(self, data):
772 def __convolutionInFreqOpt(self, data):
773
773
774 raise NotImplementedError
774 raise NotImplementedError
775
775
776 def __convolutionInTime(self, data):
776 def __convolutionInTime(self, data):
777
777
778 code = self.code[self.__profIndex]
778 code = self.code[self.__profIndex]
779 for i in range(self.__nChannels):
779 for i in range(self.__nChannels):
780 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='full')[self.nBaud-1:]
780 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='full')[self.nBaud-1:]
781
781
782 return self.datadecTime
782 return self.datadecTime
783
783
784 def __convolutionByBlockInTime(self, data):
784 def __convolutionByBlockInTime(self, data):
785
785
786 repetitions = int(self.__nProfiles / self.nCode)
786 repetitions = int(self.__nProfiles / self.nCode)
787 junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize))
787 junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize))
788 junk = junk.flatten()
788 junk = junk.flatten()
789 code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud))
789 code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud))
790 profilesList = range(self.__nProfiles)
790 profilesList = range(self.__nProfiles)
791
791
792 for i in range(self.__nChannels):
792 for i in range(self.__nChannels):
793 for j in profilesList:
793 for j in profilesList:
794 self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:]
794 self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:]
795 return self.datadecTime
795 return self.datadecTime
796
796
797 def __convolutionByBlockInFreq(self, data):
797 def __convolutionByBlockInFreq(self, data):
798
798
799 raise NotImplementedError("Decoder by frequency fro Blocks not implemented")
799 raise NotImplementedError("Decoder by frequency fro Blocks not implemented")
800
800
801
801
802 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
802 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
803
803
804 fft_data = numpy.fft.fft(data, axis=2)
804 fft_data = numpy.fft.fft(data, axis=2)
805
805
806 conv = fft_data*fft_code
806 conv = fft_data*fft_code
807
807
808 data = numpy.fft.ifft(conv,axis=2)
808 data = numpy.fft.ifft(conv,axis=2)
809
809
810 return data
810 return data
811
811
812
812
813 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None):
813 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None):
814
814
815 if dataOut.flagDecodeData:
815 if dataOut.flagDecodeData:
816 print("This data is already decoded, recoding again ...")
816 print("This data is already decoded, recoding again ...")
817
817
818 if not self.isConfig:
818 if not self.isConfig:
819
819
820 if code is None:
820 if code is None:
821 if dataOut.code is None:
821 if dataOut.code is None:
822 raise ValueError("Code could not be read from %s instance. Enter a value in Code parameter" %dataOut.type)
822 raise ValueError("Code could not be read from %s instance. Enter a value in Code parameter" %dataOut.type)
823
823
824 code = dataOut.code
824 code = dataOut.code
825 else:
825 else:
826 code = numpy.array(code).reshape(nCode,nBaud)
826 code = numpy.array(code).reshape(nCode,nBaud)
827 self.setup(code, osamp, dataOut)
827 self.setup(code, osamp, dataOut)
828
828
829 self.isConfig = True
829 self.isConfig = True
830
830
831 if mode == 3:
831 if mode == 3:
832 sys.stderr.write("Decoder Warning: mode=%d is not valid, using mode=0\n" %mode)
832 sys.stderr.write("Decoder Warning: mode=%d is not valid, using mode=0\n" %mode)
833
833
834 if times != None:
834 if times != None:
835 sys.stderr.write("Decoder Warning: Argument 'times' in not used anymore\n")
835 sys.stderr.write("Decoder Warning: Argument 'times' in not used anymore\n")
836
836
837 if self.code is None:
837 if self.code is None:
838 print("Fail decoding: Code is not defined.")
838 print("Fail decoding: Code is not defined.")
839 return
839 return
840
840
841 self.__nProfiles = dataOut.nProfiles
841 self.__nProfiles = dataOut.nProfiles
842 datadec = None
842 datadec = None
843
843
844 if mode == 3:
844 if mode == 3:
845 mode = 0
845 mode = 0
846
846
847 if dataOut.flagDataAsBlock:
847 if dataOut.flagDataAsBlock:
848 """
848 """
849 Decoding when data have been read as block,
849 Decoding when data have been read as block,
850 """
850 """
851
851
852 if mode == 0:
852 if mode == 0:
853 datadec = self.__convolutionByBlockInTime(dataOut.data)
853 datadec = self.__convolutionByBlockInTime(dataOut.data)
854 if mode == 1:
854 if mode == 1:
855 datadec = self.__convolutionByBlockInFreq(dataOut.data)
855 datadec = self.__convolutionByBlockInFreq(dataOut.data)
856 else:
856 else:
857 """
857 """
858 Decoding when data have been read profile by profile
858 Decoding when data have been read profile by profile
859 """
859 """
860 if mode == 0:
860 if mode == 0:
861 datadec = self.__convolutionInTime(dataOut.data)
861 datadec = self.__convolutionInTime(dataOut.data)
862
862
863 if mode == 1:
863 if mode == 1:
864 datadec = self.__convolutionInFreq(dataOut.data)
864 datadec = self.__convolutionInFreq(dataOut.data)
865
865
866 if mode == 2:
866 if mode == 2:
867 datadec = self.__convolutionInFreqOpt(dataOut.data)
867 datadec = self.__convolutionInFreqOpt(dataOut.data)
868
868
869 if datadec is None:
869 if datadec is None:
870 raise ValueError("Codification mode selected is not valid: mode=%d. Try selecting 0 or 1" %mode)
870 raise ValueError("Codification mode selected is not valid: mode=%d. Try selecting 0 or 1" %mode)
871
871
872 dataOut.code = self.code
872 dataOut.code = self.code
873 dataOut.nCode = self.nCode
873 dataOut.nCode = self.nCode
874 dataOut.nBaud = self.nBaud
874 dataOut.nBaud = self.nBaud
875
875
876 dataOut.data = datadec
876 dataOut.data = datadec
877
877
878 dataOut.heightList = dataOut.heightList[0:datadec.shape[-1]]
878 dataOut.heightList = dataOut.heightList[0:datadec.shape[-1]]
879
879
880 dataOut.flagDecodeData = True #asumo q la data esta decodificada
880 dataOut.flagDecodeData = True #asumo q la data esta decodificada
881
881
882 if self.__profIndex == self.nCode-1:
882 if self.__profIndex == self.nCode-1:
883 self.__profIndex = 0
883 self.__profIndex = 0
884 return dataOut
884 return dataOut
885
885
886 self.__profIndex += 1
886 self.__profIndex += 1
887
887
888 return dataOut
888 return dataOut
889 # dataOut.flagDeflipData = True #asumo q la data no esta sin flip
889 # dataOut.flagDeflipData = True #asumo q la data no esta sin flip
890
890
891
891
892 class ProfileConcat(Operation):
892 class ProfileConcat(Operation):
893
893
894 isConfig = False
894 isConfig = False
895 buffer = None
895 buffer = None
896
896
897 def __init__(self, **kwargs):
897 def __init__(self, **kwargs):
898
898
899 Operation.__init__(self, **kwargs)
899 Operation.__init__(self, **kwargs)
900 self.profileIndex = 0
900 self.profileIndex = 0
901
901
902 def reset(self):
902 def reset(self):
903 self.buffer = numpy.zeros_like(self.buffer)
903 self.buffer = numpy.zeros_like(self.buffer)
904 self.start_index = 0
904 self.start_index = 0
905 self.times = 1
905 self.times = 1
906
906
907 def setup(self, data, m, n=1):
907 def setup(self, data, m, n=1):
908 self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0]))
908 self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0]))
909 self.nHeights = data.shape[1]#.nHeights
909 self.nHeights = data.shape[1]#.nHeights
910 self.start_index = 0
910 self.start_index = 0
911 self.times = 1
911 self.times = 1
912
912
913 def concat(self, data):
913 def concat(self, data):
914
914
915 self.buffer[:,self.start_index:self.nHeights*self.times] = data.copy()
915 self.buffer[:,self.start_index:self.nHeights*self.times] = data.copy()
916 self.start_index = self.start_index + self.nHeights
916 self.start_index = self.start_index + self.nHeights
917
917
918 def run(self, dataOut, m):
918 def run(self, dataOut, m):
919 dataOut.flagNoData = True
919 dataOut.flagNoData = True
920
920
921 if not self.isConfig:
921 if not self.isConfig:
922 self.setup(dataOut.data, m, 1)
922 self.setup(dataOut.data, m, 1)
923 self.isConfig = True
923 self.isConfig = True
924
924
925 if dataOut.flagDataAsBlock:
925 if dataOut.flagDataAsBlock:
926 raise ValueError("ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False")
926 raise ValueError("ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False")
927
927
928 else:
928 else:
929 self.concat(dataOut.data)
929 self.concat(dataOut.data)
930 self.times += 1
930 self.times += 1
931 if self.times > m:
931 if self.times > m:
932 dataOut.data = self.buffer
932 dataOut.data = self.buffer
933 self.reset()
933 self.reset()
934 dataOut.flagNoData = False
934 dataOut.flagNoData = False
935 # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas
935 # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas
936 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
936 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
937 xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * m
937 xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * m
938 dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight)
938 dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight)
939 dataOut.ippSeconds *= m
939 dataOut.ippSeconds *= m
940 return dataOut
940 return dataOut
941
941
942 class ProfileSelector(Operation):
942 class ProfileSelector(Operation):
943
943
944 profileIndex = None
944 profileIndex = None
945 # Tamanho total de los perfiles
945 # Tamanho total de los perfiles
946 nProfiles = None
946 nProfiles = None
947
947
948 def __init__(self, **kwargs):
948 def __init__(self, **kwargs):
949
949
950 Operation.__init__(self, **kwargs)
950 Operation.__init__(self, **kwargs)
951 self.profileIndex = 0
951 self.profileIndex = 0
952
952
953 def incProfileIndex(self):
953 def incProfileIndex(self):
954
954
955 self.profileIndex += 1
955 self.profileIndex += 1
956
956
957 if self.profileIndex >= self.nProfiles:
957 if self.profileIndex >= self.nProfiles:
958 self.profileIndex = 0
958 self.profileIndex = 0
959
959
960 def isThisProfileInRange(self, profileIndex, minIndex, maxIndex):
960 def isThisProfileInRange(self, profileIndex, minIndex, maxIndex):
961
961
962 if profileIndex < minIndex:
962 if profileIndex < minIndex:
963 return False
963 return False
964
964
965 if profileIndex > maxIndex:
965 if profileIndex > maxIndex:
966 return False
966 return False
967
967
968 return True
968 return True
969
969
970 def isThisProfileInList(self, profileIndex, profileList):
970 def isThisProfileInList(self, profileIndex, profileList):
971
971
972 if profileIndex not in profileList:
972 if profileIndex not in profileList:
973 return False
973 return False
974
974
975 return True
975 return True
976
976
977 def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False, rangeList = None, nProfiles=None):
977 def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False, rangeList = None, nProfiles=None):
978
978
979 """
979 """
980 ProfileSelector:
980 ProfileSelector:
981
981
982 Inputs:
982 Inputs:
983 profileList : Index of profiles selected. Example: profileList = (0,1,2,7,8)
983 profileList : Index of profiles selected. Example: profileList = (0,1,2,7,8)
984
984
985 profileRangeList : Minimum and maximum profile indexes. Example: profileRangeList = (4, 30)
985 profileRangeList : Minimum and maximum profile indexes. Example: profileRangeList = (4, 30)
986
986
987 rangeList : List of profile ranges. Example: rangeList = ((4, 30), (32, 64), (128, 256))
987 rangeList : List of profile ranges. Example: rangeList = ((4, 30), (32, 64), (128, 256))
988
988
989 """
989 """
990
990
991 if rangeList is not None:
991 if rangeList is not None:
992 if type(rangeList[0]) not in (tuple, list):
992 if type(rangeList[0]) not in (tuple, list):
993 rangeList = [rangeList]
993 rangeList = [rangeList]
994
994
995 dataOut.flagNoData = True
995 dataOut.flagNoData = True
996
996
997 if dataOut.flagDataAsBlock:
997 if dataOut.flagDataAsBlock:
998 """
998 """
999 data dimension = [nChannels, nProfiles, nHeis]
999 data dimension = [nChannels, nProfiles, nHeis]
1000 """
1000 """
1001 if profileList != None:
1001 if profileList != None:
1002 dataOut.data = dataOut.data[:,profileList,:]
1002 dataOut.data = dataOut.data[:,profileList,:]
1003
1003
1004 if profileRangeList != None:
1004 if profileRangeList != None:
1005 minIndex = profileRangeList[0]
1005 minIndex = profileRangeList[0]
1006 maxIndex = profileRangeList[1]
1006 maxIndex = profileRangeList[1]
1007 profileList = list(range(minIndex, maxIndex+1))
1007 profileList = list(range(minIndex, maxIndex+1))
1008
1008
1009 dataOut.data = dataOut.data[:,minIndex:maxIndex+1,:]
1009 dataOut.data = dataOut.data[:,minIndex:maxIndex+1,:]
1010
1010
1011 if rangeList != None:
1011 if rangeList != None:
1012
1012
1013 profileList = []
1013 profileList = []
1014
1014
1015 for thisRange in rangeList:
1015 for thisRange in rangeList:
1016 minIndex = thisRange[0]
1016 minIndex = thisRange[0]
1017 maxIndex = thisRange[1]
1017 maxIndex = thisRange[1]
1018
1018
1019 profileList.extend(list(range(minIndex, maxIndex+1)))
1019 profileList.extend(list(range(minIndex, maxIndex+1)))
1020
1020
1021 dataOut.data = dataOut.data[:,profileList,:]
1021 dataOut.data = dataOut.data[:,profileList,:]
1022
1022
1023 dataOut.nProfiles = len(profileList)
1023 dataOut.nProfiles = len(profileList)
1024 dataOut.profileIndex = dataOut.nProfiles - 1
1024 dataOut.profileIndex = dataOut.nProfiles - 1
1025 dataOut.flagNoData = False
1025 dataOut.flagNoData = False
1026
1026
1027 return dataOut
1027 return dataOut
1028
1028
1029 """
1029 """
1030 data dimension = [nChannels, nHeis]
1030 data dimension = [nChannels, nHeis]
1031 """
1031 """
1032
1032
1033 if profileList != None:
1033 if profileList != None:
1034
1034
1035 if self.isThisProfileInList(dataOut.profileIndex, profileList):
1035 if self.isThisProfileInList(dataOut.profileIndex, profileList):
1036
1036
1037 self.nProfiles = len(profileList)
1037 self.nProfiles = len(profileList)
1038 dataOut.nProfiles = self.nProfiles
1038 dataOut.nProfiles = self.nProfiles
1039 dataOut.profileIndex = self.profileIndex
1039 dataOut.profileIndex = self.profileIndex
1040 dataOut.flagNoData = False
1040 dataOut.flagNoData = False
1041
1041
1042 self.incProfileIndex()
1042 self.incProfileIndex()
1043 return dataOut
1043 return dataOut
1044
1044
1045 if profileRangeList != None:
1045 if profileRangeList != None:
1046
1046
1047 minIndex = profileRangeList[0]
1047 minIndex = profileRangeList[0]
1048 maxIndex = profileRangeList[1]
1048 maxIndex = profileRangeList[1]
1049
1049
1050 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
1050 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
1051
1051
1052 self.nProfiles = maxIndex - minIndex + 1
1052 self.nProfiles = maxIndex - minIndex + 1
1053 dataOut.nProfiles = self.nProfiles
1053 dataOut.nProfiles = self.nProfiles
1054 dataOut.profileIndex = self.profileIndex
1054 dataOut.profileIndex = self.profileIndex
1055 dataOut.flagNoData = False
1055 dataOut.flagNoData = False
1056
1056
1057 self.incProfileIndex()
1057 self.incProfileIndex()
1058 return dataOut
1058 return dataOut
1059
1059
1060 if rangeList != None:
1060 if rangeList != None:
1061
1061
1062 nProfiles = 0
1062 nProfiles = 0
1063
1063
1064 for thisRange in rangeList:
1064 for thisRange in rangeList:
1065 minIndex = thisRange[0]
1065 minIndex = thisRange[0]
1066 maxIndex = thisRange[1]
1066 maxIndex = thisRange[1]
1067
1067
1068 nProfiles += maxIndex - minIndex + 1
1068 nProfiles += maxIndex - minIndex + 1
1069
1069
1070 for thisRange in rangeList:
1070 for thisRange in rangeList:
1071
1071
1072 minIndex = thisRange[0]
1072 minIndex = thisRange[0]
1073 maxIndex = thisRange[1]
1073 maxIndex = thisRange[1]
1074
1074
1075 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
1075 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
1076
1076
1077 self.nProfiles = nProfiles
1077 self.nProfiles = nProfiles
1078 dataOut.nProfiles = self.nProfiles
1078 dataOut.nProfiles = self.nProfiles
1079 dataOut.profileIndex = self.profileIndex
1079 dataOut.profileIndex = self.profileIndex
1080 dataOut.flagNoData = False
1080 dataOut.flagNoData = False
1081
1081
1082 self.incProfileIndex()
1082 self.incProfileIndex()
1083
1083
1084 break
1084 break
1085
1085
1086 return dataOut
1086 return dataOut
1087
1087
1088
1088
1089 if beam != None: #beam is only for AMISR data
1089 if beam != None: #beam is only for AMISR data
1090 if self.isThisProfileInList(dataOut.profileIndex, dataOut.beamRangeDict[beam]):
1090 if self.isThisProfileInList(dataOut.profileIndex, dataOut.beamRangeDict[beam]):
1091 dataOut.flagNoData = False
1091 dataOut.flagNoData = False
1092 dataOut.profileIndex = self.profileIndex
1092 dataOut.profileIndex = self.profileIndex
1093
1093
1094 self.incProfileIndex()
1094 self.incProfileIndex()
1095
1095
1096 return dataOut
1096 return dataOut
1097
1097
1098 raise ValueError("ProfileSelector needs profileList, profileRangeList or rangeList parameter")
1098 raise ValueError("ProfileSelector needs profileList, profileRangeList or rangeList parameter")
1099
1099
1100
1100
1101 class Reshaper(Operation):
1101 class Reshaper(Operation):
1102
1102
1103 def __init__(self, **kwargs):
1103 def __init__(self, **kwargs):
1104
1104
1105 Operation.__init__(self, **kwargs)
1105 Operation.__init__(self, **kwargs)
1106
1106
1107 self.__buffer = None
1107 self.__buffer = None
1108 self.__nitems = 0
1108 self.__nitems = 0
1109
1109
1110 def __appendProfile(self, dataOut, nTxs):
1110 def __appendProfile(self, dataOut, nTxs):
1111
1111
1112 if self.__buffer is None:
1112 if self.__buffer is None:
1113 shape = (dataOut.nChannels, int(dataOut.nHeights/nTxs) )
1113 shape = (dataOut.nChannels, int(dataOut.nHeights/nTxs) )
1114 self.__buffer = numpy.empty(shape, dtype = dataOut.data.dtype)
1114 self.__buffer = numpy.empty(shape, dtype = dataOut.data.dtype)
1115
1115
1116 ini = dataOut.nHeights * self.__nitems
1116 ini = dataOut.nHeights * self.__nitems
1117 end = ini + dataOut.nHeights
1117 end = ini + dataOut.nHeights
1118
1118
1119 self.__buffer[:, ini:end] = dataOut.data
1119 self.__buffer[:, ini:end] = dataOut.data
1120
1120
1121 self.__nitems += 1
1121 self.__nitems += 1
1122
1122
1123 return int(self.__nitems*nTxs)
1123 return int(self.__nitems*nTxs)
1124
1124
1125 def __getBuffer(self):
1125 def __getBuffer(self):
1126
1126
1127 if self.__nitems == int(1./self.__nTxs):
1127 if self.__nitems == int(1./self.__nTxs):
1128
1128
1129 self.__nitems = 0
1129 self.__nitems = 0
1130
1130
1131 return self.__buffer.copy()
1131 return self.__buffer.copy()
1132
1132
1133 return None
1133 return None
1134
1134
1135 def __checkInputs(self, dataOut, shape, nTxs):
1135 def __checkInputs(self, dataOut, shape, nTxs):
1136
1136
1137 if shape is None and nTxs is None:
1137 if shape is None and nTxs is None:
1138 raise ValueError("Reshaper: shape of factor should be defined")
1138 raise ValueError("Reshaper: shape of factor should be defined")
1139
1139
1140 if nTxs:
1140 if nTxs:
1141 if nTxs < 0:
1141 if nTxs < 0:
1142 raise ValueError("nTxs should be greater than 0")
1142 raise ValueError("nTxs should be greater than 0")
1143
1143
1144 if nTxs < 1 and dataOut.nProfiles % (1./nTxs) != 0:
1144 if nTxs < 1 and dataOut.nProfiles % (1./nTxs) != 0:
1145 raise ValueError("nProfiles= %d is not divisibled by (1./nTxs) = %f" %(dataOut.nProfiles, (1./nTxs)))
1145 raise ValueError("nProfiles= %d is not divisibled by (1./nTxs) = %f" %(dataOut.nProfiles, (1./nTxs)))
1146
1146
1147 shape = [dataOut.nChannels, dataOut.nProfiles*nTxs, dataOut.nHeights/nTxs]
1147 shape = [dataOut.nChannels, dataOut.nProfiles*nTxs, dataOut.nHeights/nTxs]
1148
1148
1149 return shape, nTxs
1149 return shape, nTxs
1150
1150
1151 if len(shape) != 2 and len(shape) != 3:
1151 if len(shape) != 2 and len(shape) != 3:
1152 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))
1152 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))
1153
1153
1154 if len(shape) == 2:
1154 if len(shape) == 2:
1155 shape_tuple = [dataOut.nChannels]
1155 shape_tuple = [dataOut.nChannels]
1156 shape_tuple.extend(shape)
1156 shape_tuple.extend(shape)
1157 else:
1157 else:
1158 shape_tuple = list(shape)
1158 shape_tuple = list(shape)
1159
1159
1160 nTxs = 1.0*shape_tuple[1]/dataOut.nProfiles
1160 nTxs = 1.0*shape_tuple[1]/dataOut.nProfiles
1161
1161
1162 return shape_tuple, nTxs
1162 return shape_tuple, nTxs
1163
1163
1164 def run(self, dataOut, shape=None, nTxs=None):
1164 def run(self, dataOut, shape=None, nTxs=None):
1165
1165
1166 shape_tuple, self.__nTxs = self.__checkInputs(dataOut, shape, nTxs)
1166 shape_tuple, self.__nTxs = self.__checkInputs(dataOut, shape, nTxs)
1167
1167
1168 dataOut.flagNoData = True
1168 dataOut.flagNoData = True
1169 profileIndex = None
1169 profileIndex = None
1170
1170
1171 if dataOut.flagDataAsBlock:
1171 if dataOut.flagDataAsBlock:
1172
1172
1173 dataOut.data = numpy.reshape(dataOut.data, shape_tuple)
1173 dataOut.data = numpy.reshape(dataOut.data, shape_tuple)
1174 dataOut.flagNoData = False
1174 dataOut.flagNoData = False
1175
1175
1176 profileIndex = int(dataOut.nProfiles*self.__nTxs) - 1
1176 profileIndex = int(dataOut.nProfiles*self.__nTxs) - 1
1177
1177
1178 else:
1178 else:
1179
1179
1180 if self.__nTxs < 1:
1180 if self.__nTxs < 1:
1181
1181
1182 self.__appendProfile(dataOut, self.__nTxs)
1182 self.__appendProfile(dataOut, self.__nTxs)
1183 new_data = self.__getBuffer()
1183 new_data = self.__getBuffer()
1184
1184
1185 if new_data is not None:
1185 if new_data is not None:
1186 dataOut.data = new_data
1186 dataOut.data = new_data
1187 dataOut.flagNoData = False
1187 dataOut.flagNoData = False
1188
1188
1189 profileIndex = dataOut.profileIndex*nTxs
1189 profileIndex = dataOut.profileIndex*nTxs
1190
1190
1191 else:
1191 else:
1192 raise ValueError("nTxs should be greater than 0 and lower than 1, or use VoltageReader(..., getblock=True)")
1192 raise ValueError("nTxs should be greater than 0 and lower than 1, or use VoltageReader(..., getblock=True)")
1193
1193
1194 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1194 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1195
1195
1196 dataOut.heightList = numpy.arange(dataOut.nHeights/self.__nTxs) * deltaHeight + dataOut.heightList[0]
1196 dataOut.heightList = numpy.arange(dataOut.nHeights/self.__nTxs) * deltaHeight + dataOut.heightList[0]
1197
1197
1198 dataOut.nProfiles = int(dataOut.nProfiles*self.__nTxs)
1198 dataOut.nProfiles = int(dataOut.nProfiles*self.__nTxs)
1199
1199
1200 dataOut.profileIndex = profileIndex
1200 dataOut.profileIndex = profileIndex
1201
1201
1202 dataOut.ippSeconds /= self.__nTxs
1202 dataOut.ippSeconds /= self.__nTxs
1203
1203
1204 return dataOut
1204 return dataOut
1205
1205
1206 class SplitProfiles(Operation):
1206 class SplitProfiles(Operation):
1207
1207
1208 def __init__(self, **kwargs):
1208 def __init__(self, **kwargs):
1209
1209
1210 Operation.__init__(self, **kwargs)
1210 Operation.__init__(self, **kwargs)
1211
1211
1212 def run(self, dataOut, n):
1212 def run(self, dataOut, n):
1213
1213
1214 dataOut.flagNoData = True
1214 dataOut.flagNoData = True
1215 profileIndex = None
1215 profileIndex = None
1216
1216
1217 if dataOut.flagDataAsBlock:
1217 if dataOut.flagDataAsBlock:
1218
1218
1219 #nchannels, nprofiles, nsamples
1219 #nchannels, nprofiles, nsamples
1220 shape = dataOut.data.shape
1220 shape = dataOut.data.shape
1221
1221
1222 if shape[2] % n != 0:
1222 if shape[2] % n != 0:
1223 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[2]))
1223 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[2]))
1224
1224
1225 new_shape = shape[0], shape[1]*n, int(shape[2]/n)
1225 new_shape = shape[0], shape[1]*n, int(shape[2]/n)
1226
1226
1227 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1227 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1228 dataOut.flagNoData = False
1228 dataOut.flagNoData = False
1229
1229
1230 profileIndex = int(dataOut.nProfiles/n) - 1
1230 profileIndex = int(dataOut.nProfiles/n) - 1
1231
1231
1232 else:
1232 else:
1233
1233
1234 raise ValueError("Could not split the data when is read Profile by Profile. Use VoltageReader(..., getblock=True)")
1234 raise ValueError("Could not split the data when is read Profile by Profile. Use VoltageReader(..., getblock=True)")
1235
1235
1236 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1236 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1237
1237
1238 dataOut.heightList = numpy.arange(dataOut.nHeights/n) * deltaHeight + dataOut.heightList[0]
1238 dataOut.heightList = numpy.arange(dataOut.nHeights/n) * deltaHeight + dataOut.heightList[0]
1239
1239
1240 dataOut.nProfiles = int(dataOut.nProfiles*n)
1240 dataOut.nProfiles = int(dataOut.nProfiles*n)
1241
1241
1242 dataOut.profileIndex = profileIndex
1242 dataOut.profileIndex = profileIndex
1243
1243
1244 dataOut.ippSeconds /= n
1244 dataOut.ippSeconds /= n
1245
1245
1246 return dataOut
1246 return dataOut
1247
1247
1248 class CombineProfiles(Operation):
1248 class CombineProfiles(Operation):
1249 def __init__(self, **kwargs):
1249 def __init__(self, **kwargs):
1250
1250
1251 Operation.__init__(self, **kwargs)
1251 Operation.__init__(self, **kwargs)
1252
1252
1253 self.__remData = None
1253 self.__remData = None
1254 self.__profileIndex = 0
1254 self.__profileIndex = 0
1255
1255
1256 def run(self, dataOut, n):
1256 def run(self, dataOut, n):
1257
1257
1258 dataOut.flagNoData = True
1258 dataOut.flagNoData = True
1259 profileIndex = None
1259 profileIndex = None
1260
1260
1261 if dataOut.flagDataAsBlock:
1261 if dataOut.flagDataAsBlock:
1262
1262
1263 #nchannels, nprofiles, nsamples
1263 #nchannels, nprofiles, nsamples
1264 shape = dataOut.data.shape
1264 shape = dataOut.data.shape
1265 new_shape = shape[0], shape[1]/n, shape[2]*n
1265 new_shape = shape[0], shape[1]/n, shape[2]*n
1266
1266
1267 if shape[1] % n != 0:
1267 if shape[1] % n != 0:
1268 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[1]))
1268 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[1]))
1269
1269
1270 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1270 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1271 dataOut.flagNoData = False
1271 dataOut.flagNoData = False
1272
1272
1273 profileIndex = int(dataOut.nProfiles*n) - 1
1273 profileIndex = int(dataOut.nProfiles*n) - 1
1274
1274
1275 else:
1275 else:
1276
1276
1277 #nchannels, nsamples
1277 #nchannels, nsamples
1278 if self.__remData is None:
1278 if self.__remData is None:
1279 newData = dataOut.data
1279 newData = dataOut.data
1280 else:
1280 else:
1281 newData = numpy.concatenate((self.__remData, dataOut.data), axis=1)
1281 newData = numpy.concatenate((self.__remData, dataOut.data), axis=1)
1282
1282
1283 self.__profileIndex += 1
1283 self.__profileIndex += 1
1284
1284
1285 if self.__profileIndex < n:
1285 if self.__profileIndex < n:
1286 self.__remData = newData
1286 self.__remData = newData
1287 #continue
1287 #continue
1288 return
1288 return
1289
1289
1290 self.__profileIndex = 0
1290 self.__profileIndex = 0
1291 self.__remData = None
1291 self.__remData = None
1292
1292
1293 dataOut.data = newData
1293 dataOut.data = newData
1294 dataOut.flagNoData = False
1294 dataOut.flagNoData = False
1295
1295
1296 profileIndex = dataOut.profileIndex/n
1296 profileIndex = dataOut.profileIndex/n
1297
1297
1298
1298
1299 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1299 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1300
1300
1301 dataOut.heightList = numpy.arange(dataOut.nHeights*n) * deltaHeight + dataOut.heightList[0]
1301 dataOut.heightList = numpy.arange(dataOut.nHeights*n) * deltaHeight + dataOut.heightList[0]
1302
1302
1303 dataOut.nProfiles = int(dataOut.nProfiles/n)
1303 dataOut.nProfiles = int(dataOut.nProfiles/n)
1304
1304
1305 dataOut.profileIndex = profileIndex
1305 dataOut.profileIndex = profileIndex
1306
1306
1307 dataOut.ippSeconds *= n
1307 dataOut.ippSeconds *= n
1308
1308
1309 return dataOut
1309 return dataOut
1310
1310
1311 class PulsePairVoltage(Operation):
1311 class PulsePairVoltage(Operation):
1312 '''
1312 '''
1313 Function PulsePair(Signal Power, Velocity)
1313 Function PulsePair(Signal Power, Velocity)
1314 The real component of Lag[0] provides Intensity Information
1314 The real component of Lag[0] provides Intensity Information
1315 The imag component of Lag[1] Phase provides Velocity Information
1315 The imag component of Lag[1] Phase provides Velocity Information
1316
1316
1317 Configuration Parameters:
1317 Configuration Parameters:
1318 nPRF = Number of Several PRF
1318 nPRF = Number of Several PRF
1319 theta = Degree Azimuth angel Boundaries
1319 theta = Degree Azimuth angel Boundaries
1320
1320
1321 Input:
1321 Input:
1322 self.dataOut
1322 self.dataOut
1323 lag[N]
1323 lag[N]
1324 Affected:
1324 Affected:
1325 self.dataOut.spc
1325 self.dataOut.spc
1326 '''
1326 '''
1327 isConfig = False
1327 isConfig = False
1328 __profIndex = 0
1328 __profIndex = 0
1329 __initime = None
1329 __initime = None
1330 __lastdatatime = None
1330 __lastdatatime = None
1331 __buffer = None
1331 __buffer = None
1332 noise = None
1332 noise = None
1333 __dataReady = False
1333 __dataReady = False
1334 n = None
1334 n = None
1335 __nch = 0
1335 __nch = 0
1336 __nHeis = 0
1336 __nHeis = 0
1337 removeDC = False
1337 removeDC = False
1338 ipp = None
1338 ipp = None
1339 lambda_ = 0
1339 lambda_ = 0
1340
1340
1341 def __init__(self,**kwargs):
1341 def __init__(self,**kwargs):
1342 Operation.__init__(self,**kwargs)
1342 Operation.__init__(self,**kwargs)
1343
1343
1344 def setup(self, dataOut, n = None, removeDC=False):
1344 def setup(self, dataOut, n = None, removeDC=False):
1345 '''
1345 '''
1346 n= Numero de PRF's de entrada
1346 n= Numero de PRF's de entrada
1347 '''
1347 '''
1348 self.__initime = None
1348 self.__initime = None
1349 self.__lastdatatime = 0
1349 self.__lastdatatime = 0
1350 self.__dataReady = False
1350 self.__dataReady = False
1351 self.__buffer = 0
1351 self.__buffer = 0
1352 self.__profIndex = 0
1352 self.__profIndex = 0
1353 self.noise = None
1353 self.noise = None
1354 self.__nch = dataOut.nChannels
1354 self.__nch = dataOut.nChannels
1355 self.__nHeis = dataOut.nHeights
1355 self.__nHeis = dataOut.nHeights
1356 self.removeDC = removeDC
1356 self.removeDC = removeDC
1357 self.lambda_ = 3.0e8/(9345.0e6)
1357 self.lambda_ = 3.0e8/(9345.0e6)
1358 self.ippSec = dataOut.ippSeconds
1358 self.ippSec = dataOut.ippSeconds
1359 self.nCohInt = dataOut.nCohInt
1359 self.nCohInt = dataOut.nCohInt
1360
1360
1361 if n == None:
1361 if n == None:
1362 raise ValueError("n should be specified.")
1362 raise ValueError("n should be specified.")
1363
1363
1364 if n != None:
1364 if n != None:
1365 if n<2:
1365 if n<2:
1366 raise ValueError("n should be greater than 2")
1366 raise ValueError("n should be greater than 2")
1367
1367
1368 self.n = n
1368 self.n = n
1369 self.__nProf = n
1369 self.__nProf = n
1370
1370
1371 self.__buffer = numpy.zeros((dataOut.nChannels,
1371 self.__buffer = numpy.zeros((dataOut.nChannels,
1372 n,
1372 n,
1373 dataOut.nHeights),
1373 dataOut.nHeights),
1374 dtype='complex')
1374 dtype='complex')
1375
1375
1376 def putData(self,data):
1376 def putData(self,data):
1377 '''
1377 '''
1378 Add a profile to he __buffer and increase in one the __profiel Index
1378 Add a profile to he __buffer and increase in one the __profiel Index
1379 '''
1379 '''
1380 self.__buffer[:,self.__profIndex,:]= data
1380 self.__buffer[:,self.__profIndex,:]= data
1381 self.__profIndex += 1
1381 self.__profIndex += 1
1382 return
1382 return
1383
1383
1384 def pushData(self,dataOut):
1384 def pushData(self,dataOut):
1385 '''
1385 '''
1386 Return the PULSEPAIR and the profiles used in the operation
1386 Return the PULSEPAIR and the profiles used in the operation
1387 Affected : self.__profileIndex
1387 Affected : self.__profileIndex
1388 '''
1388 '''
1389 #----------------- Remove DC-----------------------------------
1389 #----------------- Remove DC-----------------------------------
1390 if self.removeDC==True:
1390 if self.removeDC==True:
1391 mean = numpy.mean(self.__buffer,1)
1391 mean = numpy.mean(self.__buffer,1)
1392 tmp = mean.reshape(self.__nch,1,self.__nHeis)
1392 tmp = mean.reshape(self.__nch,1,self.__nHeis)
1393 dc= numpy.tile(tmp,[1,self.__nProf,1])
1393 dc= numpy.tile(tmp,[1,self.__nProf,1])
1394 self.__buffer = self.__buffer - dc
1394 self.__buffer = self.__buffer - dc
1395 #------------------Calculo de Potencia ------------------------
1395 #------------------Calculo de Potencia ------------------------
1396 pair0 = self.__buffer*numpy.conj(self.__buffer)
1396 pair0 = self.__buffer*numpy.conj(self.__buffer)
1397 pair0 = pair0.real
1397 pair0 = pair0.real
1398 lag_0 = numpy.sum(pair0,1)
1398 lag_0 = numpy.sum(pair0,1)
1399 #------------------Calculo de Ruido x canal--------------------
1399 #------------------Calculo de Ruido x canal--------------------
1400 self.noise = numpy.zeros(self.__nch)
1400 self.noise = numpy.zeros(self.__nch)
1401 for i in range(self.__nch):
1401 for i in range(self.__nch):
1402 daux = numpy.sort(pair0[i,:,:],axis= None)
1402 daux = numpy.sort(pair0[i,:,:],axis= None)
1403 self.noise[i]=hildebrand_sekhon( daux ,self.nCohInt)
1403 self.noise[i]=hildebrand_sekhon( daux ,self.nCohInt)
1404
1404
1405 self.noise = self.noise.reshape(self.__nch,1)
1405 self.noise = self.noise.reshape(self.__nch,1)
1406 self.noise = numpy.tile(self.noise,[1,self.__nHeis])
1406 self.noise = numpy.tile(self.noise,[1,self.__nHeis])
1407 noise_buffer = self.noise.reshape(self.__nch,1,self.__nHeis)
1407 noise_buffer = self.noise.reshape(self.__nch,1,self.__nHeis)
1408 noise_buffer = numpy.tile(noise_buffer,[1,self.__nProf,1])
1408 noise_buffer = numpy.tile(noise_buffer,[1,self.__nProf,1])
1409 #------------------ Potencia recibida= P , Potencia senal = S , Ruido= N--
1409 #------------------ Potencia recibida= P , Potencia senal = S , Ruido= N--
1410 #------------------ P= S+N ,P=lag_0/N ---------------------------------
1410 #------------------ P= S+N ,P=lag_0/N ---------------------------------
1411 #-------------------- Power --------------------------------------------------
1411 #-------------------- Power --------------------------------------------------
1412 data_power = lag_0/(self.n*self.nCohInt)
1412 data_power = lag_0/(self.n*self.nCohInt)
1413 #------------------ Senal ---------------------------------------------------
1413 #------------------ Senal ---------------------------------------------------
1414 data_intensity = pair0 - noise_buffer
1414 data_intensity = pair0 - noise_buffer
1415 data_intensity = numpy.sum(data_intensity,axis=1)*(self.n*self.nCohInt)#*self.nCohInt)
1415 data_intensity = numpy.sum(data_intensity,axis=1)*(self.n*self.nCohInt)#*self.nCohInt)
1416 #data_intensity = (lag_0-self.noise*self.n)*(self.n*self.nCohInt)
1416 #data_intensity = (lag_0-self.noise*self.n)*(self.n*self.nCohInt)
1417 for i in range(self.__nch):
1417 for i in range(self.__nch):
1418 for j in range(self.__nHeis):
1418 for j in range(self.__nHeis):
1419 if data_intensity[i][j] < 0:
1419 if data_intensity[i][j] < 0:
1420 data_intensity[i][j] = numpy.min(numpy.absolute(data_intensity[i][j]))
1420 data_intensity[i][j] = numpy.min(numpy.absolute(data_intensity[i][j]))
1421
1421
1422 #----------------- Calculo de Frecuencia y Velocidad doppler--------
1422 #----------------- Calculo de Frecuencia y Velocidad doppler--------
1423 pair1 = self.__buffer[:,:-1,:]*numpy.conjugate(self.__buffer[:,1:,:])
1423 pair1 = self.__buffer[:,:-1,:]*numpy.conjugate(self.__buffer[:,1:,:])
1424 lag_1 = numpy.sum(pair1,1)
1424 lag_1 = numpy.sum(pair1,1)
1425 data_freq = (-1/(2.0*math.pi*self.ippSec*self.nCohInt))*numpy.angle(lag_1)
1425 data_freq = (-1/(2.0*math.pi*self.ippSec*self.nCohInt))*numpy.angle(lag_1)
1426 data_velocity = (self.lambda_/2.0)*data_freq
1426 data_velocity = (self.lambda_/2.0)*data_freq
1427
1427
1428 #---------------- Potencia promedio estimada de la Senal-----------
1428 #---------------- Potencia promedio estimada de la Senal-----------
1429 lag_0 = lag_0/self.n
1429 lag_0 = lag_0/self.n
1430 S = lag_0-self.noise
1430 S = lag_0-self.noise
1431
1431
1432 #---------------- Frecuencia Doppler promedio ---------------------
1432 #---------------- Frecuencia Doppler promedio ---------------------
1433 lag_1 = lag_1/(self.n-1)
1433 lag_1 = lag_1/(self.n-1)
1434 R1 = numpy.abs(lag_1)
1434 R1 = numpy.abs(lag_1)
1435
1435
1436 #---------------- Calculo del SNR----------------------------------
1436 #---------------- Calculo del SNR----------------------------------
1437 data_snrPP = S/self.noise
1437 data_snrPP = S/self.noise
1438 for i in range(self.__nch):
1438 for i in range(self.__nch):
1439 for j in range(self.__nHeis):
1439 for j in range(self.__nHeis):
1440 if data_snrPP[i][j] < 1.e-20:
1440 if data_snrPP[i][j] < 1.e-20:
1441 data_snrPP[i][j] = 1.e-20
1441 data_snrPP[i][j] = 1.e-20
1442
1442
1443 #----------------- Calculo del ancho espectral ----------------------
1443 #----------------- Calculo del ancho espectral ----------------------
1444 L = S/R1
1444 L = S/R1
1445 L = numpy.where(L<0,1,L)
1445 L = numpy.where(L<0,1,L)
1446 L = numpy.log(L)
1446 L = numpy.log(L)
1447 tmp = numpy.sqrt(numpy.absolute(L))
1447 tmp = numpy.sqrt(numpy.absolute(L))
1448 data_specwidth = (self.lambda_/(2*math.sqrt(2)*math.pi*self.ippSec*self.nCohInt))*tmp*numpy.sign(L)
1448 data_specwidth = (self.lambda_/(2*math.sqrt(2)*math.pi*self.ippSec*self.nCohInt))*tmp*numpy.sign(L)
1449 n = self.__profIndex
1449 n = self.__profIndex
1450
1450
1451 self.__buffer = numpy.zeros((self.__nch, self.__nProf,self.__nHeis), dtype='complex')
1451 self.__buffer = numpy.zeros((self.__nch, self.__nProf,self.__nHeis), dtype='complex')
1452 self.__profIndex = 0
1452 self.__profIndex = 0
1453 return data_power,data_intensity,data_velocity,data_snrPP,data_specwidth,n
1453 return data_power,data_intensity,data_velocity,data_snrPP,data_specwidth,n
1454
1454
1455
1455
1456 def pulsePairbyProfiles(self,dataOut):
1456 def pulsePairbyProfiles(self,dataOut):
1457
1457
1458 self.__dataReady = False
1458 self.__dataReady = False
1459 data_power = None
1459 data_power = None
1460 data_intensity = None
1460 data_intensity = None
1461 data_velocity = None
1461 data_velocity = None
1462 data_specwidth = None
1462 data_specwidth = None
1463 data_snrPP = None
1463 data_snrPP = None
1464 self.putData(data=dataOut.data)
1464 self.putData(data=dataOut.data)
1465 if self.__profIndex == self.n:
1465 if self.__profIndex == self.n:
1466 data_power,data_intensity, data_velocity,data_snrPP,data_specwidth, n = self.pushData(dataOut=dataOut)
1466 data_power,data_intensity, data_velocity,data_snrPP,data_specwidth, n = self.pushData(dataOut=dataOut)
1467 self.__dataReady = True
1467 self.__dataReady = True
1468
1468
1469 return data_power, data_intensity, data_velocity, data_snrPP, data_specwidth
1469 return data_power, data_intensity, data_velocity, data_snrPP, data_specwidth
1470
1470
1471
1471
1472 def pulsePairOp(self, dataOut, datatime= None):
1472 def pulsePairOp(self, dataOut, datatime= None):
1473
1473
1474 if self.__initime == None:
1474 if self.__initime == None:
1475 self.__initime = datatime
1475 self.__initime = datatime
1476 data_power, data_intensity, data_velocity, data_snrPP, data_specwidth = self.pulsePairbyProfiles(dataOut)
1476 data_power, data_intensity, data_velocity, data_snrPP, data_specwidth = self.pulsePairbyProfiles(dataOut)
1477 self.__lastdatatime = datatime
1477 self.__lastdatatime = datatime
1478
1478
1479 if data_power is None:
1479 if data_power is None:
1480 return None, None, None,None,None,None
1480 return None, None, None,None,None,None
1481
1481
1482 avgdatatime = self.__initime
1482 avgdatatime = self.__initime
1483 deltatime = datatime - self.__lastdatatime
1483 deltatime = datatime - self.__lastdatatime
1484 self.__initime = datatime
1484 self.__initime = datatime
1485
1485
1486 return data_power, data_intensity, data_velocity, data_snrPP, data_specwidth, avgdatatime
1486 return data_power, data_intensity, data_velocity, data_snrPP, data_specwidth, avgdatatime
1487
1487
1488 def run(self, dataOut,n = None,removeDC= False, overlapping= False,**kwargs):
1488 def run(self, dataOut,n = None,removeDC= False, overlapping= False,**kwargs):
1489
1489
1490 if not self.isConfig:
1490 if not self.isConfig:
1491 self.setup(dataOut = dataOut, n = n , removeDC=removeDC , **kwargs)
1491 self.setup(dataOut = dataOut, n = n , removeDC=removeDC , **kwargs)
1492 self.isConfig = True
1492 self.isConfig = True
1493 data_power, data_intensity, data_velocity,data_snrPP,data_specwidth, avgdatatime = self.pulsePairOp(dataOut, dataOut.utctime)
1493 data_power, data_intensity, data_velocity,data_snrPP,data_specwidth, avgdatatime = self.pulsePairOp(dataOut, dataOut.utctime)
1494 dataOut.flagNoData = True
1494 dataOut.flagNoData = True
1495
1495
1496 if self.__dataReady:
1496 if self.__dataReady:
1497 dataOut.nCohInt *= self.n
1497 dataOut.nCohInt *= self.n
1498 dataOut.dataPP_POW = data_intensity # S
1498 dataOut.dataPP_POW = data_intensity # S
1499 dataOut.dataPP_POWER = data_power # P
1499 dataOut.dataPP_POWER = data_power # P
1500 dataOut.dataPP_DOP = data_velocity
1500 dataOut.dataPP_DOP = data_velocity
1501 dataOut.dataPP_SNR = data_snrPP
1501 dataOut.dataPP_SNR = data_snrPP
1502 dataOut.dataPP_WIDTH = data_specwidth
1502 dataOut.dataPP_WIDTH = data_specwidth
1503 dataOut.PRFbyAngle = self.n #numero de PRF*cada angulo rotado que equivale a un tiempo.
1503 dataOut.PRFbyAngle = self.n #numero de PRF*cada angulo rotado que equivale a un tiempo.
1504 dataOut.utctime = avgdatatime
1504 dataOut.utctime = avgdatatime
1505 dataOut.flagNoData = False
1505 dataOut.flagNoData = False
1506 return dataOut
1506 return dataOut
1507
1507
1508
1508
1509
1509
1510 # import collections
1510 # import collections
1511 # from scipy.stats import mode
1511 # from scipy.stats import mode
1512 #
1512 #
1513 # class Synchronize(Operation):
1513 # class Synchronize(Operation):
1514 #
1514 #
1515 # isConfig = False
1515 # isConfig = False
1516 # __profIndex = 0
1516 # __profIndex = 0
1517 #
1517 #
1518 # def __init__(self, **kwargs):
1518 # def __init__(self, **kwargs):
1519 #
1519 #
1520 # Operation.__init__(self, **kwargs)
1520 # Operation.__init__(self, **kwargs)
1521 # # self.isConfig = False
1521 # # self.isConfig = False
1522 # self.__powBuffer = None
1522 # self.__powBuffer = None
1523 # self.__startIndex = 0
1523 # self.__startIndex = 0
1524 # self.__pulseFound = False
1524 # self.__pulseFound = False
1525 #
1525 #
1526 # def __findTxPulse(self, dataOut, channel=0, pulse_with = None):
1526 # def __findTxPulse(self, dataOut, channel=0, pulse_with = None):
1527 #
1527 #
1528 # #Read data
1528 # #Read data
1529 #
1529 #
1530 # powerdB = dataOut.getPower(channel = channel)
1530 # powerdB = dataOut.getPower(channel = channel)
1531 # noisedB = dataOut.getNoise(channel = channel)[0]
1531 # noisedB = dataOut.getNoise(channel = channel)[0]
1532 #
1532 #
1533 # self.__powBuffer.extend(powerdB.flatten())
1533 # self.__powBuffer.extend(powerdB.flatten())
1534 #
1534 #
1535 # dataArray = numpy.array(self.__powBuffer)
1535 # dataArray = numpy.array(self.__powBuffer)
1536 #
1536 #
1537 # filteredPower = numpy.correlate(dataArray, dataArray[0:self.__nSamples], "same")
1537 # filteredPower = numpy.correlate(dataArray, dataArray[0:self.__nSamples], "same")
1538 #
1538 #
1539 # maxValue = numpy.nanmax(filteredPower)
1539 # maxValue = numpy.nanmax(filteredPower)
1540 #
1540 #
1541 # if maxValue < noisedB + 10:
1541 # if maxValue < noisedB + 10:
1542 # #No se encuentra ningun pulso de transmision
1542 # #No se encuentra ningun pulso de transmision
1543 # return None
1543 # return None
1544 #
1544 #
1545 # maxValuesIndex = numpy.where(filteredPower > maxValue - 0.1*abs(maxValue))[0]
1545 # maxValuesIndex = numpy.where(filteredPower > maxValue - 0.1*abs(maxValue))[0]
1546 #
1546 #
1547 # if len(maxValuesIndex) < 2:
1547 # if len(maxValuesIndex) < 2:
1548 # #Solo se encontro un solo pulso de transmision de un baudio, esperando por el siguiente TX
1548 # #Solo se encontro un solo pulso de transmision de un baudio, esperando por el siguiente TX
1549 # return None
1549 # return None
1550 #
1550 #
1551 # phasedMaxValuesIndex = maxValuesIndex - self.__nSamples
1551 # phasedMaxValuesIndex = maxValuesIndex - self.__nSamples
1552 #
1552 #
1553 # #Seleccionar solo valores con un espaciamiento de nSamples
1553 # #Seleccionar solo valores con un espaciamiento de nSamples
1554 # pulseIndex = numpy.intersect1d(maxValuesIndex, phasedMaxValuesIndex)
1554 # pulseIndex = numpy.intersect1d(maxValuesIndex, phasedMaxValuesIndex)
1555 #
1555 #
1556 # if len(pulseIndex) < 2:
1556 # if len(pulseIndex) < 2:
1557 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1557 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1558 # return None
1558 # return None
1559 #
1559 #
1560 # spacing = pulseIndex[1:] - pulseIndex[:-1]
1560 # spacing = pulseIndex[1:] - pulseIndex[:-1]
1561 #
1561 #
1562 # #remover senales que se distancien menos de 10 unidades o muestras
1562 # #remover senales que se distancien menos de 10 unidades o muestras
1563 # #(No deberian existir IPP menor a 10 unidades)
1563 # #(No deberian existir IPP menor a 10 unidades)
1564 #
1564 #
1565 # realIndex = numpy.where(spacing > 10 )[0]
1565 # realIndex = numpy.where(spacing > 10 )[0]
1566 #
1566 #
1567 # if len(realIndex) < 2:
1567 # if len(realIndex) < 2:
1568 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1568 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1569 # return None
1569 # return None
1570 #
1570 #
1571 # #Eliminar pulsos anchos (deja solo la diferencia entre IPPs)
1571 # #Eliminar pulsos anchos (deja solo la diferencia entre IPPs)
1572 # realPulseIndex = pulseIndex[realIndex]
1572 # realPulseIndex = pulseIndex[realIndex]
1573 #
1573 #
1574 # period = mode(realPulseIndex[1:] - realPulseIndex[:-1])[0][0]
1574 # period = mode(realPulseIndex[1:] - realPulseIndex[:-1])[0][0]
1575 #
1575 #
1576 # print "IPP = %d samples" %period
1576 # print "IPP = %d samples" %period
1577 #
1577 #
1578 # self.__newNSamples = dataOut.nHeights #int(period)
1578 # self.__newNSamples = dataOut.nHeights #int(period)
1579 # self.__startIndex = int(realPulseIndex[0])
1579 # self.__startIndex = int(realPulseIndex[0])
1580 #
1580 #
1581 # return 1
1581 # return 1
1582 #
1582 #
1583 #
1583 #
1584 # def setup(self, nSamples, nChannels, buffer_size = 4):
1584 # def setup(self, nSamples, nChannels, buffer_size = 4):
1585 #
1585 #
1586 # self.__powBuffer = collections.deque(numpy.zeros( buffer_size*nSamples,dtype=numpy.float),
1586 # self.__powBuffer = collections.deque(numpy.zeros( buffer_size*nSamples,dtype=numpy.float),
1587 # maxlen = buffer_size*nSamples)
1587 # maxlen = buffer_size*nSamples)
1588 #
1588 #
1589 # bufferList = []
1589 # bufferList = []
1590 #
1590 #
1591 # for i in range(nChannels):
1591 # for i in range(nChannels):
1592 # bufferByChannel = collections.deque(numpy.zeros( buffer_size*nSamples, dtype=numpy.complex) + numpy.NAN,
1592 # bufferByChannel = collections.deque(numpy.zeros( buffer_size*nSamples, dtype=numpy.complex) + numpy.NAN,
1593 # maxlen = buffer_size*nSamples)
1593 # maxlen = buffer_size*nSamples)
1594 #
1594 #
1595 # bufferList.append(bufferByChannel)
1595 # bufferList.append(bufferByChannel)
1596 #
1596 #
1597 # self.__nSamples = nSamples
1597 # self.__nSamples = nSamples
1598 # self.__nChannels = nChannels
1598 # self.__nChannels = nChannels
1599 # self.__bufferList = bufferList
1599 # self.__bufferList = bufferList
1600 #
1600 #
1601 # def run(self, dataOut, channel = 0):
1601 # def run(self, dataOut, channel = 0):
1602 #
1602 #
1603 # if not self.isConfig:
1603 # if not self.isConfig:
1604 # nSamples = dataOut.nHeights
1604 # nSamples = dataOut.nHeights
1605 # nChannels = dataOut.nChannels
1605 # nChannels = dataOut.nChannels
1606 # self.setup(nSamples, nChannels)
1606 # self.setup(nSamples, nChannels)
1607 # self.isConfig = True
1607 # self.isConfig = True
1608 #
1608 #
1609 # #Append new data to internal buffer
1609 # #Append new data to internal buffer
1610 # for thisChannel in range(self.__nChannels):
1610 # for thisChannel in range(self.__nChannels):
1611 # bufferByChannel = self.__bufferList[thisChannel]
1611 # bufferByChannel = self.__bufferList[thisChannel]
1612 # bufferByChannel.extend(dataOut.data[thisChannel])
1612 # bufferByChannel.extend(dataOut.data[thisChannel])
1613 #
1613 #
1614 # if self.__pulseFound:
1614 # if self.__pulseFound:
1615 # self.__startIndex -= self.__nSamples
1615 # self.__startIndex -= self.__nSamples
1616 #
1616 #
1617 # #Finding Tx Pulse
1617 # #Finding Tx Pulse
1618 # if not self.__pulseFound:
1618 # if not self.__pulseFound:
1619 # indexFound = self.__findTxPulse(dataOut, channel)
1619 # indexFound = self.__findTxPulse(dataOut, channel)
1620 #
1620 #
1621 # if indexFound == None:
1621 # if indexFound == None:
1622 # dataOut.flagNoData = True
1622 # dataOut.flagNoData = True
1623 # return
1623 # return
1624 #
1624 #
1625 # self.__arrayBuffer = numpy.zeros((self.__nChannels, self.__newNSamples), dtype = numpy.complex)
1625 # self.__arrayBuffer = numpy.zeros((self.__nChannels, self.__newNSamples), dtype = numpy.complex)
1626 # self.__pulseFound = True
1626 # self.__pulseFound = True
1627 # self.__startIndex = indexFound
1627 # self.__startIndex = indexFound
1628 #
1628 #
1629 # #If pulse was found ...
1629 # #If pulse was found ...
1630 # for thisChannel in range(self.__nChannels):
1630 # for thisChannel in range(self.__nChannels):
1631 # bufferByChannel = self.__bufferList[thisChannel]
1631 # bufferByChannel = self.__bufferList[thisChannel]
1632 # #print self.__startIndex
1632 # #print self.__startIndex
1633 # x = numpy.array(bufferByChannel)
1633 # x = numpy.array(bufferByChannel)
1634 # self.__arrayBuffer[thisChannel] = x[self.__startIndex:self.__startIndex+self.__newNSamples]
1634 # self.__arrayBuffer[thisChannel] = x[self.__startIndex:self.__startIndex+self.__newNSamples]
1635 #
1635 #
1636 # deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1636 # deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1637 # dataOut.heightList = numpy.arange(self.__newNSamples)*deltaHeight
1637 # dataOut.heightList = numpy.arange(self.__newNSamples)*deltaHeight
1638 # # dataOut.ippSeconds = (self.__newNSamples / deltaHeight)/1e6
1638 # # dataOut.ippSeconds = (self.__newNSamples / deltaHeight)/1e6
1639 #
1639 #
1640 # dataOut.data = self.__arrayBuffer
1640 # dataOut.data = self.__arrayBuffer
1641 #
1641 #
1642 # self.__startIndex += self.__newNSamples
1642 # self.__startIndex += self.__newNSamples
1643 #
1643 #
1644 # return
1644 # return
1645 class SSheightProfiles(Operation):
1645 class SSheightProfiles(Operation):
1646
1646
1647 step = None
1647 step = None
1648 nsamples = None
1648 nsamples = None
1649 bufferShape = None
1649 bufferShape = None
1650 profileShape = None
1650 profileShape = None
1651 sshProfiles = None
1651 sshProfiles = None
1652 profileIndex = None
1652 profileIndex = None
1653
1653
1654 def __init__(self, **kwargs):
1654 def __init__(self, **kwargs):
1655
1655
1656 Operation.__init__(self, **kwargs)
1656 Operation.__init__(self, **kwargs)
1657 self.isConfig = False
1657 self.isConfig = False
1658
1658
1659 def setup(self,dataOut ,step = None , nsamples = None):
1659 def setup(self,dataOut ,step = None , nsamples = None):
1660
1660
1661 if step == None and nsamples == None:
1661 if step == None and nsamples == None:
1662 raise ValueError("step or nheights should be specified ...")
1662 raise ValueError("step or nheights should be specified ...")
1663
1663
1664 self.step = step
1664 self.step = step
1665 self.nsamples = nsamples
1665 self.nsamples = nsamples
1666 self.__nChannels = dataOut.nChannels
1666 self.__nChannels = dataOut.nChannels
1667 self.__nProfiles = dataOut.nProfiles
1667 self.__nProfiles = dataOut.nProfiles
1668 self.__nHeis = dataOut.nHeights
1668 self.__nHeis = dataOut.nHeights
1669 shape = dataOut.data.shape #nchannels, nprofiles, nsamples
1669 shape = dataOut.data.shape #nchannels, nprofiles, nsamples
1670
1670
1671 residue = (shape[1] - self.nsamples) % self.step
1671 residue = (shape[1] - self.nsamples) % self.step
1672 if residue != 0:
1672 if residue != 0:
1673 print("The residue is %d, step=%d should be multiple of %d to avoid loss of %d samples"%(residue,step,shape[1] - self.nsamples,residue))
1673 print("The residue is %d, step=%d should be multiple of %d to avoid loss of %d samples"%(residue,step,shape[1] - self.nsamples,residue))
1674
1674
1675 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1675 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1676 numberProfile = self.nsamples
1676 numberProfile = self.nsamples
1677 numberSamples = (shape[1] - self.nsamples)/self.step
1677 numberSamples = (shape[1] - self.nsamples)/self.step
1678
1678
1679 self.bufferShape = int(shape[0]), int(numberSamples), int(numberProfile) # nchannels, nsamples , nprofiles
1679 self.bufferShape = int(shape[0]), int(numberSamples), int(numberProfile) # nchannels, nsamples , nprofiles
1680 self.profileShape = int(shape[0]), int(numberProfile), int(numberSamples) # nchannels, nprofiles, nsamples
1680 self.profileShape = int(shape[0]), int(numberProfile), int(numberSamples) # nchannels, nprofiles, nsamples
1681
1681
1682 self.buffer = numpy.zeros(self.bufferShape , dtype=numpy.complex)
1682 self.buffer = numpy.zeros(self.bufferShape , dtype=numpy.complex)
1683 self.sshProfiles = numpy.zeros(self.profileShape, dtype=numpy.complex)
1683 self.sshProfiles = numpy.zeros(self.profileShape, dtype=numpy.complex)
1684
1684
1685 def run(self, dataOut, step, nsamples, code = None, repeat = None):
1685 def run(self, dataOut, step, nsamples, code = None, repeat = None):
1686 dataOut.flagNoData = True
1686 dataOut.flagNoData = True
1687
1687
1688 profileIndex = None
1688 profileIndex = None
1689 #print("nProfiles, nHeights ",dataOut.nProfiles, dataOut.nHeights)
1689 #print("nProfiles, nHeights ",dataOut.nProfiles, dataOut.nHeights)
1690 #print(dataOut.getFreqRange(1)/1000.)
1690 #print(dataOut.getFreqRange(1)/1000.)
1691 #exit(1)
1691 #exit(1)
1692 if dataOut.flagDataAsBlock:
1692 if dataOut.flagDataAsBlock:
1693 dataOut.data = numpy.average(dataOut.data,axis=1)
1693 dataOut.data = numpy.average(dataOut.data,axis=1)
1694 #print("jee")
1694 #print("jee")
1695 dataOut.flagDataAsBlock = False
1695 dataOut.flagDataAsBlock = False
1696 if not self.isConfig:
1696 if not self.isConfig:
1697 self.setup(dataOut, step=step , nsamples=nsamples)
1697 self.setup(dataOut, step=step , nsamples=nsamples)
1698 #print("Setup done")
1698 #print("Setup done")
1699 self.isConfig = True
1699 self.isConfig = True
1700
1700
1701
1701
1702 if code is not None:
1702 if code is not None:
1703 code = numpy.array(code)
1703 code = numpy.array(code)
1704 code_block = code
1704 code_block = code
1705
1705
1706 if repeat is not None:
1706 if repeat is not None:
1707 code_block = numpy.repeat(code_block, repeats=repeat, axis=1)
1707 code_block = numpy.repeat(code_block, repeats=repeat, axis=1)
1708 #print(code_block.shape)
1708 #print(code_block.shape)
1709 for i in range(self.buffer.shape[1]):
1709 for i in range(self.buffer.shape[1]):
1710
1710
1711 if code is not None:
1711 if code is not None:
1712 self.buffer[:,i] = dataOut.data[:,i*self.step:i*self.step + self.nsamples]*code_block
1712 self.buffer[:,i] = dataOut.data[:,i*self.step:i*self.step + self.nsamples]*code_block
1713
1713
1714 else:
1714 else:
1715
1715
1716 self.buffer[:,i] = dataOut.data[:,i*self.step:i*self.step + self.nsamples]#*code[dataOut.profileIndex,:]
1716 self.buffer[:,i] = dataOut.data[:,i*self.step:i*self.step + self.nsamples]#*code[dataOut.profileIndex,:]
1717
1717
1718 #self.buffer[:,j,self.__nHeis-j*self.step - self.nheights:self.__nHeis-j*self.step] = numpy.flip(dataOut.data[:,j*self.step:j*self.step + self.nheights])
1718 #self.buffer[:,j,self.__nHeis-j*self.step - self.nheights:self.__nHeis-j*self.step] = numpy.flip(dataOut.data[:,j*self.step:j*self.step + self.nheights])
1719
1719
1720 for j in range(self.buffer.shape[0]):
1720 for j in range(self.buffer.shape[0]):
1721 self.sshProfiles[j] = numpy.transpose(self.buffer[j])
1721 self.sshProfiles[j] = numpy.transpose(self.buffer[j])
1722
1722
1723 profileIndex = self.nsamples
1723 profileIndex = self.nsamples
1724 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1724 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1725 ippSeconds = (deltaHeight*1.0e-6)/(0.15)
1725 ippSeconds = (deltaHeight*1.0e-6)/(0.15)
1726 #print("ippSeconds, dH: ",ippSeconds,deltaHeight)
1726 #print("ippSeconds, dH: ",ippSeconds,deltaHeight)
1727 try:
1727 try:
1728 if dataOut.concat_m is not None:
1728 if dataOut.concat_m is not None:
1729 ippSeconds= ippSeconds/float(dataOut.concat_m)
1729 ippSeconds= ippSeconds/float(dataOut.concat_m)
1730 #print "Profile concat %d"%dataOut.concat_m
1730 #print "Profile concat %d"%dataOut.concat_m
1731 except:
1731 except:
1732 pass
1732 pass
1733
1733
1734 dataOut.data = self.sshProfiles
1734 dataOut.data = self.sshProfiles
1735 dataOut.flagNoData = False
1735 dataOut.flagNoData = False
1736 dataOut.heightList = numpy.arange(self.buffer.shape[1]) *self.step*deltaHeight + dataOut.heightList[0]
1736 dataOut.heightList = numpy.arange(self.buffer.shape[1]) *self.step*deltaHeight + dataOut.heightList[0]
1737 dataOut.nProfiles = int(dataOut.nProfiles*self.nsamples)
1737 dataOut.nProfiles = int(dataOut.nProfiles*self.nsamples)
1738
1738
1739 dataOut.profileIndex = profileIndex
1739 dataOut.profileIndex = profileIndex
1740 dataOut.flagDataAsBlock = True
1740 dataOut.flagDataAsBlock = True
1741 dataOut.ippSeconds = ippSeconds
1741 dataOut.ippSeconds = ippSeconds
1742 dataOut.step = self.step
1742 dataOut.step = self.step
1743 #print(numpy.shape(dataOut.data))
1743 #print(numpy.shape(dataOut.data))
1744 #exit(1)
1744 #exit(1)
1745 #print("new data shape and time:", dataOut.data.shape, dataOut.utctime)
1745 #print("new data shape and time:", dataOut.data.shape, dataOut.utctime)
1746
1746
1747 return dataOut
1747 return dataOut
1748 ################################################################################3############################3
1748 ################################################################################3############################3
1749 ################################################################################3############################3
1749 ################################################################################3############################3
1750 ################################################################################3############################3
1750 ################################################################################3############################3
1751 ################################################################################3############################3
1751 ################################################################################3############################3
1752
1752
1753 class SSheightProfiles2(Operation):
1753 class SSheightProfiles2(Operation):
1754 '''
1754 '''
1755 Procesa por perfiles y por bloques
1755 Procesa por perfiles y por bloques
1756 '''
1756 '''
1757
1757
1758
1758
1759 bufferShape = None
1759 bufferShape = None
1760 profileShape = None
1760 profileShape = None
1761 sshProfiles = None
1761 sshProfiles = None
1762 profileIndex = None
1762 profileIndex = None
1763 #nsamples = None
1763 #nsamples = None
1764 #step = None
1764 #step = None
1765 #deltaHeight = None
1765 #deltaHeight = None
1766 #init_range = None
1766 #init_range = None
1767 __slots__ = ('step', 'nsamples', 'deltaHeight', 'init_range', 'isConfig', '__nChannels',
1767 __slots__ = ('step', 'nsamples', 'deltaHeight', 'init_range', 'isConfig', '__nChannels',
1768 '__nProfiles', '__nHeis', 'deltaHeight', 'new_nHeights')
1768 '__nProfiles', '__nHeis', 'deltaHeight', 'new_nHeights')
1769
1769
1770 def __init__(self, **kwargs):
1770 def __init__(self, **kwargs):
1771
1771
1772 Operation.__init__(self, **kwargs)
1772 Operation.__init__(self, **kwargs)
1773 self.isConfig = False
1773 self.isConfig = False
1774
1774
1775 def setup(self,dataOut ,step = None , nsamples = None):
1775 def setup(self,dataOut ,step = None , nsamples = None):
1776
1776
1777 if step == None and nsamples == None:
1777 if step == None and nsamples == None:
1778 raise ValueError("step or nheights should be specified ...")
1778 raise ValueError("step or nheights should be specified ...")
1779
1779
1780 self.step = step
1780 self.step = step
1781 self.nsamples = nsamples
1781 self.nsamples = nsamples
1782 self.__nChannels = int(dataOut.nChannels)
1782 self.__nChannels = int(dataOut.nChannels)
1783 self.__nProfiles = int(dataOut.nProfiles)
1783 self.__nProfiles = int(dataOut.nProfiles)
1784 self.__nHeis = int(dataOut.nHeights)
1784 self.__nHeis = int(dataOut.nHeights)
1785
1785
1786 residue = (self.__nHeis - self.nsamples) % self.step
1786 residue = (self.__nHeis - self.nsamples) % self.step
1787 if residue != 0:
1787 if residue != 0:
1788 print("The residue is %d, step=%d should be multiple of %d to avoid loss of %d samples"%(residue,step,shape[1] - self.nsamples,residue))
1788 print("The residue is %d, step=%d should be multiple of %d to avoid loss of %d samples"%(residue,step,shape[1] - self.nsamples,residue))
1789
1789
1790 self.deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1790 self.deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1791 self.init_range = dataOut.heightList[0]
1791 self.init_range = dataOut.heightList[0]
1792 #numberProfile = self.nsamples
1792 #numberProfile = self.nsamples
1793 numberSamples = (self.__nHeis - self.nsamples)/self.step
1793 numberSamples = (self.__nHeis - self.nsamples)/self.step
1794
1794
1795 self.new_nHeights = numberSamples
1795 self.new_nHeights = numberSamples
1796
1796
1797 self.bufferShape = int(self.__nChannels), int(numberSamples), int(self.nsamples) # nchannels, nsamples , nprofiles
1797 self.bufferShape = int(self.__nChannels), int(numberSamples), int(self.nsamples) # nchannels, nsamples , nprofiles
1798 self.profileShape = int(self.__nChannels), int(self.nsamples), int(numberSamples) # nchannels, nprofiles, nsamples
1798 self.profileShape = int(self.__nChannels), int(self.nsamples), int(numberSamples) # nchannels, nprofiles, nsamples
1799
1799
1800 self.buffer = numpy.zeros(self.bufferShape , dtype=numpy.complex)
1800 self.buffer = numpy.zeros(self.bufferShape , dtype=numpy.complex)
1801 self.sshProfiles = numpy.zeros(self.profileShape, dtype=numpy.complex)
1801 self.sshProfiles = numpy.zeros(self.profileShape, dtype=numpy.complex)
1802
1802
1803 def getNewProfiles(self, data, code=None, repeat=None):
1803 def getNewProfiles(self, data, code=None, repeat=None):
1804
1804
1805 if code is not None:
1805 if code is not None:
1806 code = numpy.array(code)
1806 code = numpy.array(code)
1807 code_block = code
1807 code_block = code
1808
1808
1809 if repeat is not None:
1809 if repeat is not None:
1810 code_block = numpy.repeat(code_block, repeats=repeat, axis=1)
1810 code_block = numpy.repeat(code_block, repeats=repeat, axis=1)
1811 if data.ndim == 2:
1811 if data.ndim == 2:
1812 data = data.reshape(1,1,self.__nHeis )
1812 data = data.reshape(1,1,self.__nHeis )
1813 #print("buff, data, :",self.buffer.shape, data.shape,self.sshProfiles.shape)
1813 #print("buff, data, :",self.buffer.shape, data.shape,self.sshProfiles.shape)
1814 for i in range(int(self.new_nHeights)): #nuevas alturas
1814 for i in range(int(self.new_nHeights)): #nuevas alturas
1815 if code is not None:
1815 if code is not None:
1816 self.buffer[:,i,:] = data[:,:,i*self.step:i*self.step + self.nsamples]*code_block
1816 self.buffer[:,i,:] = data[:,:,i*self.step:i*self.step + self.nsamples]*code_block
1817 else:
1817 else:
1818 self.buffer[:,i,:] = data[:,:,i*self.step:i*self.step + self.nsamples]#*code[dataOut.profileIndex,:]
1818 self.buffer[:,i,:] = data[:,:,i*self.step:i*self.step + self.nsamples]#*code[dataOut.profileIndex,:]
1819
1819
1820 for j in range(self.__nChannels): #en los cananles
1820 for j in range(self.__nChannels): #en los cananles
1821 self.sshProfiles[j,:,:] = numpy.transpose(self.buffer[j,:,:])
1821 self.sshProfiles[j,:,:] = numpy.transpose(self.buffer[j,:,:])
1822 #print("new profs Done")
1822 #print("new profs Done")
1823
1823
1824
1824
1825
1825
1826 def run(self, dataOut, step, nsamples, code = None, repeat = None):
1826 def run(self, dataOut, step, nsamples, code = None, repeat = None):
1827
1827
1828 if dataOut.flagNoData == True:
1828 if dataOut.flagNoData == True:
1829 return dataOut
1829 return dataOut
1830 dataOut.flagNoData = True
1830 dataOut.flagNoData = True
1831 #print("init data shape:", dataOut.data.shape)
1831 #print("init data shape:", dataOut.data.shape)
1832 #print("ch: {} prof: {} hs: {}".format(int(dataOut.nChannels),
1832 #print("ch: {} prof: {} hs: {}".format(int(dataOut.nChannels),
1833 # int(dataOut.nProfiles),int(dataOut.nHeights)))
1833 # int(dataOut.nProfiles),int(dataOut.nHeights)))
1834
1834
1835 profileIndex = None
1835 profileIndex = None
1836 # if not dataOut.flagDataAsBlock:
1836 # if not dataOut.flagDataAsBlock:
1837 # dataOut.nProfiles = 1
1837 # dataOut.nProfiles = 1
1838
1838
1839 if not self.isConfig:
1839 if not self.isConfig:
1840 self.setup(dataOut, step=step , nsamples=nsamples)
1840 self.setup(dataOut, step=step , nsamples=nsamples)
1841 #print("Setup done")
1841 #print("Setup done")
1842 self.isConfig = True
1842 self.isConfig = True
1843
1843
1844 dataBlock = None
1844 dataBlock = None
1845
1845
1846 nprof = 1
1846 nprof = 1
1847 if dataOut.flagDataAsBlock:
1847 if dataOut.flagDataAsBlock:
1848 nprof = int(dataOut.nProfiles)
1848 nprof = int(dataOut.nProfiles)
1849
1849
1850 #print("dataOut nProfiles:", dataOut.nProfiles)
1850 #print("dataOut nProfiles:", dataOut.nProfiles)
1851 for profile in range(nprof):
1851 for profile in range(nprof):
1852 if dataOut.flagDataAsBlock:
1852 if dataOut.flagDataAsBlock:
1853 #print("read blocks")
1853 #print("read blocks")
1854 self.getNewProfiles(dataOut.data[:,profile,:], code=code, repeat=repeat)
1854 self.getNewProfiles(dataOut.data[:,profile,:], code=code, repeat=repeat)
1855 else:
1855 else:
1856 #print("read profiles")
1856 #print("read profiles")
1857 self.getNewProfiles(dataOut.data, code=code, repeat=repeat) #only one channe
1857 self.getNewProfiles(dataOut.data, code=code, repeat=repeat) #only one channe
1858 if profile == 0:
1858 if profile == 0:
1859 dataBlock = self.sshProfiles.copy()
1859 dataBlock = self.sshProfiles.copy()
1860 else: #by blocks
1860 else: #by blocks
1861 dataBlock = numpy.concatenate((dataBlock,self.sshProfiles), axis=1) #profile axis
1861 dataBlock = numpy.concatenate((dataBlock,self.sshProfiles), axis=1) #profile axis
1862 #print("by blocks: ",dataBlock.shape, self.sshProfiles.shape)
1862 #print("by blocks: ",dataBlock.shape, self.sshProfiles.shape)
1863
1863
1864 profileIndex = self.nsamples
1864 profileIndex = self.nsamples
1865 #deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1865 #deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1866 ippSeconds = (self.deltaHeight*1.0e-6)/(0.15)
1866 ippSeconds = (self.deltaHeight*1.0e-6)/(0.15)
1867
1867
1868
1868
1869 dataOut.data = dataBlock
1869 dataOut.data = dataBlock
1870 #print("show me: ",self.step,self.deltaHeight, dataOut.heightList, self.new_nHeights)
1870 #print("show me: ",self.step,self.deltaHeight, dataOut.heightList, self.new_nHeights)
1871 dataOut.heightList = numpy.arange(int(self.new_nHeights)) *self.step*self.deltaHeight + self.init_range
1871 dataOut.heightList = numpy.arange(int(self.new_nHeights)) *self.step*self.deltaHeight + self.init_range
1872
1872
1873 dataOut.ippSeconds = ippSeconds
1873 dataOut.ippSeconds = ippSeconds
1874 dataOut.step = self.step
1874 dataOut.step = self.step
1875 dataOut.flagNoData = False
1875 dataOut.flagNoData = False
1876 if dataOut.flagDataAsBlock:
1876 if dataOut.flagDataAsBlock:
1877 dataOut.nProfiles = int(dataOut.nProfiles*self.nsamples)
1877 dataOut.nProfiles = int(dataOut.nProfiles*self.nsamples)
1878
1878
1879 else:
1879 else:
1880 dataOut.nProfiles = int(self.nsamples)
1880 dataOut.nProfiles = int(self.nsamples)
1881 dataOut.profileIndex = dataOut.nProfiles
1881 dataOut.profileIndex = dataOut.nProfiles
1882 dataOut.flagDataAsBlock = True
1882 dataOut.flagDataAsBlock = True
1883
1883
1884 dataBlock = None
1884 dataBlock = None
1885
1885
1886 #print("new data shape:", dataOut.data.shape, dataOut.utctime)
1886 #print("new data shape:", dataOut.data.shape, dataOut.utctime)
1887
1887
1888 return dataOut
1888 return dataOut
1889
1889
1890
1890
1891
1891
1892
1892
1893 #import skimage.color
1893 #import skimage.color
1894 #import skimage.io
1894 #import skimage.io
1895 #import matplotlib.pyplot as plt
1895 #import matplotlib.pyplot as plt
1896
1896
1897 class removeProfileByFaradayHS(Operation):
1897 class removeProfileByFaradayHS(Operation):
1898 '''
1898 '''
1899
1899
1900 '''
1900 '''
1901 #isConfig = False
1902 #n = None
1903
1901
1904 #__dataReady = False
1905 __buffer_data = []
1902 __buffer_data = []
1906 __buffer_times = []
1903 __buffer_times = []
1907 #__initime = None
1908 #__count_exec = 0
1909 #__profIndex = 0
1910 buffer = None
1911 #lenProfileOut = 1
1912
1904
1913 #init_prof = 0
1905 buffer = None
1914 #end_prof = 0
1915
1906
1916 #first_utcBlock = None
1917 outliers_IDs_list = []
1907 outliers_IDs_list = []
1918 #__dh = 0
1908
1919
1909
1920 __slots__ = ('n','navg','profileMargin','thHistOutlier','minHei_idx','maxHei_idx','nHeights',
1910 __slots__ = ('n','navg','profileMargin','thHistOutlier','minHei_idx','maxHei_idx','nHeights',
1921 '__dh','first_utcBlock','__profIndex','init_prof','end_prof','lenProfileOut','nChannels',
1911 '__dh','first_utcBlock','__profIndex','init_prof','end_prof','lenProfileOut','nChannels',
1922 '__count_exec','__initime','__dataReady','__ipp')
1912 '__count_exec','__initime','__dataReady','__ipp')
1923 def __init__(self, **kwargs):
1913 def __init__(self, **kwargs):
1924
1914
1925 Operation.__init__(self, **kwargs)
1915 Operation.__init__(self, **kwargs)
1926 self.isConfig = False
1916 self.isConfig = False
1927
1917
1928 def setup(self,dataOut, n=None , navg=0.8, profileMargin=50,thHistOutlier=3, minHei=None, maxHei=None):
1918 def setup(self,dataOut, n=None , navg=0.8, profileMargin=50,thHistOutlier=3, minHei=None, maxHei=None):
1929
1919
1930 if n == None and timeInterval == None:
1920 if n == None and timeInterval == None:
1931 raise ValueError("nprofiles or timeInterval should be specified ...")
1921 raise ValueError("nprofiles or timeInterval should be specified ...")
1932
1922
1933 if n != None:
1923 if n != None:
1934 self.n = n
1924 self.n = n
1935
1925
1936 self.navg = navg
1926 self.navg = navg
1937 self.profileMargin = profileMargin
1927 self.profileMargin = profileMargin
1938 self.thHistOutlier = thHistOutlier
1928 self.thHistOutlier = thHistOutlier
1939 self.__profIndex = 0
1929 self.__profIndex = 0
1940 self.buffer = None
1930 self.buffer = None
1941 self._ipp = dataOut.ippSeconds
1931 self._ipp = dataOut.ippSeconds
1942 self.n_prof_released = 0
1932 self.n_prof_released = 0
1943 self.heightList = dataOut.heightList
1933 self.heightList = dataOut.heightList
1944 self.init_prof = 0
1934 self.init_prof = 0
1945 self.end_prof = 0
1935 self.end_prof = 0
1946 self.__count_exec = 0
1936 self.__count_exec = 0
1947 self.__profIndex = 0
1937 self.__profIndex = 0
1948 self.first_utcBlock = None
1938 self.first_utcBlock = None
1949 self.__dh = dataOut.heightList[1] - dataOut.heightList[0]
1939 self.__dh = dataOut.heightList[1] - dataOut.heightList[0]
1950 minHei = minHei
1940 minHei = minHei
1951 maxHei = maxHei
1941 maxHei = maxHei
1952 if minHei==None :
1942 if minHei==None :
1953 minHei = dataOut.heightList[0]
1943 minHei = dataOut.heightList[0]
1954 if maxHei==None :
1944 if maxHei==None :
1955 maxHei = dataOut.heightList[-1]
1945 maxHei = dataOut.heightList[-1]
1956 self.minHei_idx,self.maxHei_idx = getHei_index(minHei, maxHei, dataOut.heightList)
1946 self.minHei_idx,self.maxHei_idx = getHei_index(minHei, maxHei, dataOut.heightList)
1957
1947
1958 self.nChannels = dataOut.nChannels
1948 self.nChannels = dataOut.nChannels
1959 self.nHeights = dataOut.nHeights
1949 self.nHeights = dataOut.nHeights
1960
1950
1961 def filterSatsProfiles(self):
1951 def filterSatsProfiles(self):
1962 data = self.__buffer_data
1952 data = self.__buffer_data
1963 #print(data.shape)
1953 #print(data.shape)
1964 nChannels, profiles, heights = data.shape
1954 nChannels, profiles, heights = data.shape
1965 indexes=[]
1955 indexes=[]
1966 outliers_IDs=[]
1956 outliers_IDs=[]
1967 for c in range(nChannels):
1957 for c in range(nChannels):
1968 for h in range(self.minHei_idx, self.maxHei_idx):
1958 for h in range(self.minHei_idx, self.maxHei_idx):
1969 power = data[c,:,h] * numpy.conjugate(data[c,:,h])
1959 power = data[c,:,h] * numpy.conjugate(data[c,:,h])
1970 power = power.real
1960 power = power.real
1971 #power = (numpy.abs(data[c,:,h].real))
1961 #power = (numpy.abs(data[c,:,h].real))
1972 sortdata = numpy.sort(power, axis=None)
1962 sortdata = numpy.sort(power, axis=None)
1973 sortID=power.argsort()
1963 sortID=power.argsort()
1974 index = _noise.hildebrand_sekhon2(sortdata,self.navg) #0.75-> buen valor
1964 index = _noise.hildebrand_sekhon2(sortdata,self.navg) #0.75-> buen valor
1975
1965
1976 indexes.append(index)
1966 indexes.append(index)
1977 outliers_IDs=numpy.append(outliers_IDs,sortID[index:])
1967 outliers_IDs=numpy.append(outliers_IDs,sortID[index:])
1978 # print(outliers_IDs)
1968 # print(outliers_IDs)
1979 # fig,ax = plt.subplots()
1969 # fig,ax = plt.subplots()
1980 # #ax.set_title(str(k)+" "+str(j))
1970 # #ax.set_title(str(k)+" "+str(j))
1981 # x=range(len(sortdata))
1971 # x=range(len(sortdata))
1982 # ax.scatter(x,sortdata)
1972 # ax.scatter(x,sortdata)
1983 # ax.axvline(index)
1973 # ax.axvline(index)
1984 # plt.grid()
1974 # plt.grid()
1985 # plt.show()
1975 # plt.show()
1986
1976
1977
1987 outliers_IDs = outliers_IDs.astype(numpy.dtype('int64'))
1978 outliers_IDs = outliers_IDs.astype(numpy.dtype('int64'))
1988 outliers_IDs = numpy.unique(outliers_IDs)
1979 outliers_IDs = numpy.unique(outliers_IDs)
1989 outs_lines = numpy.sort(outliers_IDs)
1980 outs_lines = numpy.sort(outliers_IDs)
1990 # #print("outliers Ids: ", outs_lines, outs_lines.shape)
1981 # #print("outliers Ids: ", outs_lines, outs_lines.shape)
1991 #hist, bin_edges = numpy.histogram(outs_lines, bins=10, density=True)
1982 #hist, bin_edges = numpy.histogram(outs_lines, bins=10, density=True)
1992
1983
1993
1984
1994 #Agrupando el histograma de outliers,
1985 #Agrupando el histograma de outliers,
1986 #my_bins = numpy.linspace(0,int(profiles), int(profiles/100), endpoint=False)
1995 my_bins = numpy.linspace(0,9600, 96, endpoint=False)
1987 my_bins = numpy.linspace(0,9600, 96, endpoint=False)
1996
1988
1997
1998 hist, bins = numpy.histogram(outs_lines,bins=my_bins)
1989 hist, bins = numpy.histogram(outs_lines,bins=my_bins)
1999 hist_outliers_indexes = numpy.where(hist > self.thHistOutlier) #es outlier
1990 hist_outliers_indexes = numpy.where(hist > self.thHistOutlier) #es outlier
2000 #print(hist_outliers_indexes[0])
1991 #print(hist_outliers_indexes[0])
2001 bins_outliers_indexes = [int(i) for i in bins[hist_outliers_indexes]] #
1992 bins_outliers_indexes = [int(i) for i in bins[hist_outliers_indexes]] #
2002 #print(bins_outliers_indexes)
1993 #print(bins_outliers_indexes)
2003 outlier_loc_index = []
1994 outlier_loc_index = []
2004
1995
2005 #outlier_loc_index = [k for k in range(bins_outliers_indexes[n]-50,bins_outliers_indexes[n+1]+50) for n in range(len(bins_outliers_indexes)-1) ]
1996
2006 for n in range(len(bins_outliers_indexes)-1):
1997 # for n in range(len(bins_outliers_indexes)-1):
2007 #outlier_loc_index = [k for k in range(bins_outliers_indexes[n]-50,bins_outliers_indexes[n+1]+50)]
1998 # for k in range(bins_outliers_indexes[n]-self.profileMargin,bins_outliers_indexes[n+1]+self.profileMargin):
2008 for k in range(bins_outliers_indexes[n]-self.profileMargin,bins_outliers_indexes[n+1]+self.profileMargin):
1999 # outlier_loc_index.append(k)
2009 outlier_loc_index.append(k)
2000
2001 outlier_loc_index = [e for n in range(len(bins_outliers_indexes)-1) for e in range(bins_outliers_indexes[n]-self.profileMargin,bins_outliers_indexes[n+1]+self.profileMargin) ]
2010
2002
2011 outlier_loc_index = numpy.asarray(outlier_loc_index)
2003 outlier_loc_index = numpy.asarray(outlier_loc_index)
2012 #print(numpy.unique(outlier_loc_index))
2004 #print(len(numpy.unique(outlier_loc_index)), numpy.unique(outlier_loc_index))
2013
2005
2014
2006
2015
2007
2016 # x, y = numpy.meshgrid(numpy.arange(profiles), self.heightList)
2008 # x, y = numpy.meshgrid(numpy.arange(profiles), self.heightList)
2017 # fig, ax = plt.subplots(1,2,figsize=(8, 6))
2009 # fig, ax = plt.subplots(1,2,figsize=(8, 6))
2018 #
2010 #
2019 # dat = data[0,:,:].real
2011 # dat = data[0,:,:].real
2020 # m = numpy.nanmean(dat)
2012 # m = numpy.nanmean(dat)
2021 # o = numpy.nanstd(dat)
2013 # o = numpy.nanstd(dat)
2022 # #print(m, o, x.shape, y.shape)
2014 # #print(m, o, x.shape, y.shape)
2023 # c = ax[0].pcolormesh(x, y, dat.T, cmap ='YlGnBu', vmin = (m-2*o), vmax = (m+2*o))
2015 # c = ax[0].pcolormesh(x, y, dat.T, cmap ='YlGnBu', vmin = (m-2*o), vmax = (m+2*o))
2024 # ax[0].vlines(outs_lines,200,600, linestyles='dashed', label = 'outs', color='w')
2016 # ax[0].vlines(outs_lines,200,600, linestyles='dashed', label = 'outs', color='w')
2025 # fig.colorbar(c)
2017 # fig.colorbar(c)
2026 # ax[0].vlines(outlier_loc_index,650,750, linestyles='dashed', label = 'outs', color='r')
2018 # ax[0].vlines(outlier_loc_index,650,750, linestyles='dashed', label = 'outs', color='r')
2027 # ax[1].hist(outs_lines,bins=my_bins)
2019 # ax[1].hist(outs_lines,bins=my_bins)
2028 # plt.show()
2020 # plt.show()
2029
2021
2030
2022
2031 self.outliers_IDs_list = numpy.unique(outlier_loc_index)
2023 self.outliers_IDs_list = numpy.unique(outlier_loc_index)
2032 return data
2024 return data
2033
2025
2034 def cleanOutliersByBlock(self):
2026 def cleanOutliersByBlock(self):
2035 #print(self.__buffer_data[0].shape)
2027 #print(self.__buffer_data[0].shape)
2036 data = self.__buffer_data#.copy()
2028 data = self.__buffer_data#.copy()
2037 #print("cleaning shape inpt: ",data.shape)
2029 #print("cleaning shape inpt: ",data.shape)
2038 '''
2030 '''
2039 self.__buffer_data = []
2031 self.__buffer_data = []
2040
2032
2041 spectrum = numpy.fft.fft2(data, axes=(0,2))
2033 spectrum = numpy.fft.fft2(data, axes=(0,2))
2042 #print("spc : ",spectrum.shape)
2034 #print("spc : ",spectrum.shape)
2043 (nch,nsamples, nh) = spectrum.shape
2035 (nch,nsamples, nh) = spectrum.shape
2044 data2 = None
2036 data2 = None
2045 #print(data.shape)
2037 #print(data.shape)
2046 spectrum2 = spectrum.copy()
2038 spectrum2 = spectrum.copy()
2047 for ch in range(nch):
2039 for ch in range(nch):
2048 dh = self.__dh
2040 dh = self.__dh
2049 dt1 = (dh*1.0e-6)/(0.15)
2041 dt1 = (dh*1.0e-6)/(0.15)
2050 dt2 = self.__buffer_times[1]-self.__buffer_times[0]
2042 dt2 = self.__buffer_times[1]-self.__buffer_times[0]
2051 freqv = numpy.fft.fftfreq(nh, d=dt1)
2043 freqv = numpy.fft.fftfreq(nh, d=dt1)
2052 freqh = numpy.fft.fftfreq(self.n, d=dt2)
2044 freqh = numpy.fft.fftfreq(self.n, d=dt2)
2053 #print("spc loop: ")
2045 #print("spc loop: ")
2054
2046
2055
2047
2056
2048
2057 x, y = numpy.meshgrid(numpy.sort(freqh),numpy.sort(freqv))
2049 x, y = numpy.meshgrid(numpy.sort(freqh),numpy.sort(freqv))
2058 z = numpy.abs(spectrum[ch,:,:])
2050 z = numpy.abs(spectrum[ch,:,:])
2059 # Find all peaks higher than the 98th percentile
2051 # Find all peaks higher than the 98th percentile
2060 peaks = z < numpy.percentile(z, 98)
2052 peaks = z < numpy.percentile(z, 98)
2061 #print(peaks)
2053 #print(peaks)
2062 # Set those peak coefficients to zero
2054 # Set those peak coefficients to zero
2063 spectrum2 = spectrum2 * peaks.astype(int)
2055 spectrum2 = spectrum2 * peaks.astype(int)
2064 data2 = numpy.fft.ifft2(spectrum2)
2056 data2 = numpy.fft.ifft2(spectrum2)
2065
2057
2066 dat = numpy.log10(z.T)
2058 dat = numpy.log10(z.T)
2067 dat2 = numpy.log10(spectrum2.T)
2059 dat2 = numpy.log10(spectrum2.T)
2068
2060
2069 # m = numpy.mean(dat)
2061 # m = numpy.mean(dat)
2070 # o = numpy.std(dat)
2062 # o = numpy.std(dat)
2071 # fig, ax = plt.subplots(2,1,figsize=(8, 6))
2063 # fig, ax = plt.subplots(2,1,figsize=(8, 6))
2072 #
2064 #
2073 # c = ax[0].pcolormesh(x, y, dat, cmap ='YlGnBu', vmin = (m-2*o), vmax = (m+2*o))
2065 # c = ax[0].pcolormesh(x, y, dat, cmap ='YlGnBu', vmin = (m-2*o), vmax = (m+2*o))
2074 # #c = ax.pcolor( z.T , cmap ='gray', vmin = (m-2*o), vmax = (m+2*o))
2066 # #c = ax.pcolor( z.T , cmap ='gray', vmin = (m-2*o), vmax = (m+2*o))
2075 # date_time = datetime.datetime.fromtimestamp(self.__buffer_times[0]).strftime('%Y-%m-%d %H:%M:%S.%f')
2067 # date_time = datetime.datetime.fromtimestamp(self.__buffer_times[0]).strftime('%Y-%m-%d %H:%M:%S.%f')
2076 # #strftime('%Y-%m-%d %H:%M:%S')
2068 # #strftime('%Y-%m-%d %H:%M:%S')
2077 # ax[0].set_title('Spectrum magnitude '+date_time)
2069 # ax[0].set_title('Spectrum magnitude '+date_time)
2078 # fig.canvas.set_window_title('Spectrum magnitude {} '.format(self.n)+date_time)
2070 # fig.canvas.set_window_title('Spectrum magnitude {} '.format(self.n)+date_time)
2079 #
2071 #
2080 #
2072 #
2081 # c = ax[1].pcolormesh(x, y, dat, cmap ='YlGnBu', vmin = (m-2*o), vmax = (m+2*o))
2073 # c = ax[1].pcolormesh(x, y, dat, cmap ='YlGnBu', vmin = (m-2*o), vmax = (m+2*o))
2082 # fig.colorbar(c)
2074 # fig.colorbar(c)
2083 # plt.show()
2075 # plt.show()
2084
2076
2085 #print(data2.shape)
2077 #print(data2.shape)
2086
2078
2087 data = data2
2079 data = data2
2088
2080
2089 #cleanBlock = numpy.fft.ifft2(spectrum, axes=(0,2)).reshape()
2081 #cleanBlock = numpy.fft.ifft2(spectrum, axes=(0,2)).reshape()
2090 '''
2082 '''
2091 #print("cleanOutliersByBlock Done")
2083 #print("cleanOutliersByBlock Done")
2092
2084
2093 return self.filterSatsProfiles()
2085 return self.filterSatsProfiles()
2094
2086
2095
2087
2096
2088
2097 def fillBuffer(self, data, datatime):
2089 def fillBuffer(self, data, datatime):
2098
2090
2099 if self.__profIndex == 0:
2091 if self.__profIndex == 0:
2100 self.__buffer_data = data.copy()
2092 self.__buffer_data = data.copy()
2101
2093
2102 else:
2094 else:
2103 self.__buffer_data = numpy.concatenate((self.__buffer_data,data), axis=1)#en perfiles
2095 self.__buffer_data = numpy.concatenate((self.__buffer_data,data), axis=1)#en perfiles
2104 self.__profIndex += 1
2096 self.__profIndex += 1
2105 #self.__buffer_times.append(datatime)
2097 #self.__buffer_times.append(datatime)
2106
2098
2107 def getData(self, data, datatime=None):
2099 def getData(self, data, datatime=None):
2108
2100
2109 if self.__profIndex == 0:
2101 if self.__profIndex == 0:
2110 self.__initime = datatime
2102 self.__initime = datatime
2111
2103
2112
2104
2113 self.__dataReady = False
2105 self.__dataReady = False
2114
2106
2115 self.fillBuffer(data, datatime)
2107 self.fillBuffer(data, datatime)
2116 dataBlock = None
2108 dataBlock = None
2117
2109
2118 if self.__profIndex == self.n:
2110 if self.__profIndex == self.n:
2119 #print("apnd : ",data)
2111 #print("apnd : ",data)
2120 #dataBlock = self.cleanOutliersByBlock()
2112 #dataBlock = self.cleanOutliersByBlock()
2121 dataBlock = self.filterSatsProfiles()
2113 dataBlock = self.filterSatsProfiles()
2122 self.__dataReady = True
2114 self.__dataReady = True
2123
2115
2124 return dataBlock
2116 return dataBlock
2125
2117
2126 if dataBlock is None:
2118 if dataBlock is None:
2127 return None, None
2119 return None, None
2128
2120
2129
2121
2130
2122
2131 return dataBlock
2123 return dataBlock
2132
2124
2133 def releaseBlock(self):
2125 def releaseBlock(self):
2134
2126
2135 if self.n % self.lenProfileOut != 0:
2127 if self.n % self.lenProfileOut != 0:
2136 raise ValueError("lenProfileOut %d must be submultiple of nProfiles %d" %(self.lenProfileOut, self.n))
2128 raise ValueError("lenProfileOut %d must be submultiple of nProfiles %d" %(self.lenProfileOut, self.n))
2137 return None
2129 return None
2138
2130
2139 data = self.buffer[:,self.init_prof:self.end_prof:,:] #ch, prof, alt
2131 data = self.buffer[:,self.init_prof:self.end_prof:,:] #ch, prof, alt
2140
2132
2141 self.init_prof = self.end_prof
2133 self.init_prof = self.end_prof
2142 self.end_prof += self.lenProfileOut
2134 self.end_prof += self.lenProfileOut
2143 #print("data release shape: ",dataOut.data.shape, self.end_prof)
2135 #print("data release shape: ",dataOut.data.shape, self.end_prof)
2144 self.n_prof_released += 1
2136 self.n_prof_released += 1
2145
2137
2146
2138
2147 #print("f_no_data ", dataOut.flagNoData)
2139 #print("f_no_data ", dataOut.flagNoData)
2148 return data
2140 return data
2149
2141
2150 def run(self, dataOut, n=None, navg=0.8, nProfilesOut=1, profile_margin=50,th_hist_outlier=3,minHei=None, maxHei=None):
2142 def run(self, dataOut, n=None, navg=0.8, nProfilesOut=1, profile_margin=50,th_hist_outlier=3,minHei=None, maxHei=None):
2151 #print("run op buffer 2D",dataOut.ippSeconds)
2143 #print("run op buffer 2D",dataOut.ippSeconds)
2152 # self.nChannels = dataOut.nChannels
2144 # self.nChannels = dataOut.nChannels
2153 # self.nHeights = dataOut.nHeights
2145 # self.nHeights = dataOut.nHeights
2154
2146
2155 if not self.isConfig:
2147 if not self.isConfig:
2156 #print("init p idx: ", dataOut.profileIndex )
2148 #print("init p idx: ", dataOut.profileIndex )
2157 self.setup(dataOut,n=n, navg=navg,profileMargin=profile_margin,
2149 self.setup(dataOut,n=n, navg=navg,profileMargin=profile_margin,
2158 thHistOutlier=th_hist_outlier,minHei=minHei, maxHei=maxHei)
2150 thHistOutlier=th_hist_outlier,minHei=minHei, maxHei=maxHei)
2159 self.isConfig = True
2151 self.isConfig = True
2160
2152
2161 dataBlock = None
2153 dataBlock = None
2162
2154
2163 if not dataOut.buffer_empty: #hay datos acumulados
2155 if not dataOut.buffer_empty: #hay datos acumulados
2164
2156
2165 if self.init_prof == 0:
2157 if self.init_prof == 0:
2166 self.n_prof_released = 0
2158 self.n_prof_released = 0
2167 self.lenProfileOut = nProfilesOut
2159 self.lenProfileOut = nProfilesOut
2168 dataOut.flagNoData = False
2160 dataOut.flagNoData = False
2169 #print("tp 2 ",dataOut.data.shape)
2161 #print("tp 2 ",dataOut.data.shape)
2170
2162
2171 self.init_prof = 0
2163 self.init_prof = 0
2172 self.end_prof = self.lenProfileOut
2164 self.end_prof = self.lenProfileOut
2173
2165
2174 dataOut.nProfiles = self.lenProfileOut
2166 dataOut.nProfiles = self.lenProfileOut
2175 if nProfilesOut == 1:
2167 if nProfilesOut == 1:
2176 dataOut.flagDataAsBlock = False
2168 dataOut.flagDataAsBlock = False
2177 else:
2169 else:
2178 dataOut.flagDataAsBlock = True
2170 dataOut.flagDataAsBlock = True
2179 #print("prof: ",self.init_prof)
2171 #print("prof: ",self.init_prof)
2180 dataOut.flagNoData = False
2172 dataOut.flagNoData = False
2181 if numpy.isin(self.n_prof_released, self.outliers_IDs_list):
2173 if numpy.isin(self.n_prof_released, self.outliers_IDs_list):
2182 #print("omitting: ", self.n_prof_released)
2174 #print("omitting: ", self.n_prof_released)
2183 dataOut.flagNoData = True
2175 dataOut.flagNoData = True
2184 dataOut.ippSeconds = self._ipp
2176 dataOut.ippSeconds = self._ipp
2185 dataOut.utctime = self.first_utcBlock + self.init_prof*self._ipp
2177 dataOut.utctime = self.first_utcBlock + self.init_prof*self._ipp
2186 # print("time: ", dataOut.utctime, self.first_utcBlock, self.init_prof,self._ipp,dataOut.ippSeconds)
2178 # print("time: ", dataOut.utctime, self.first_utcBlock, self.init_prof,self._ipp,dataOut.ippSeconds)
2187 #dataOut.data = self.releaseBlock()
2179 #dataOut.data = self.releaseBlock()
2188 #########################################################3
2180 #########################################################3
2189 if self.n % self.lenProfileOut != 0:
2181 if self.n % self.lenProfileOut != 0:
2190 raise ValueError("lenProfileOut %d must be submultiple of nProfiles %d" %(self.lenProfileOut, self.n))
2182 raise ValueError("lenProfileOut %d must be submultiple of nProfiles %d" %(self.lenProfileOut, self.n))
2191 return None
2183 return None
2192
2184
2193 dataOut.data = self.buffer[:,self.init_prof:self.end_prof:,:] #ch, prof, alt
2185 dataOut.data = self.buffer[:,self.init_prof:self.end_prof:,:] #ch, prof, alt
2194
2186
2195 self.init_prof = self.end_prof
2187 self.init_prof = self.end_prof
2196 self.end_prof += self.lenProfileOut
2188 self.end_prof += self.lenProfileOut
2197 #print("data release shape: ",dataOut.data.shape, self.end_prof)
2189 #print("data release shape: ",dataOut.data.shape, self.end_prof)
2198 self.n_prof_released += 1
2190 self.n_prof_released += 1
2199
2191
2200 if self.end_prof >= (self.n +self.lenProfileOut):
2192 if self.end_prof >= (self.n +self.lenProfileOut):
2201
2193
2202 self.init_prof = 0
2194 self.init_prof = 0
2203 self.__profIndex = 0
2195 self.__profIndex = 0
2204 self.buffer = None
2196 self.buffer = None
2205 dataOut.buffer_empty = True
2197 dataOut.buffer_empty = True
2206 self.outliers_IDs_list = []
2198 self.outliers_IDs_list = []
2207 self.n_prof_released = 0
2199 self.n_prof_released = 0
2208 dataOut.flagNoData = False #enviar ultimo aunque sea outlier :(
2200 dataOut.flagNoData = False #enviar ultimo aunque sea outlier :(
2209 #print("cleaning...", dataOut.buffer_empty)
2201 #print("cleaning...", dataOut.buffer_empty)
2210 dataOut.profileIndex = 0 #self.lenProfileOut
2202 dataOut.profileIndex = 0 #self.lenProfileOut
2211 ####################################################################
2203 ####################################################################
2212 return dataOut
2204 return dataOut
2213
2205
2214
2206
2215 #print("tp 223 ",dataOut.data.shape)
2207 #print("tp 223 ",dataOut.data.shape)
2216 dataOut.flagNoData = True
2208 dataOut.flagNoData = True
2217
2209
2218
2210
2219
2211
2220 try:
2212 try:
2221 #dataBlock = self.getData(dataOut.data.reshape(self.nChannels,1,self.nHeights), dataOut.utctime)
2213 #dataBlock = self.getData(dataOut.data.reshape(self.nChannels,1,self.nHeights), dataOut.utctime)
2222 dataBlock = self.getData(numpy.reshape(dataOut.data,(self.nChannels,1,self.nHeights)), dataOut.utctime)
2214 dataBlock = self.getData(numpy.reshape(dataOut.data,(self.nChannels,1,self.nHeights)), dataOut.utctime)
2223 self.__count_exec +=1
2215 self.__count_exec +=1
2224 except Exception as e:
2216 except Exception as e:
2225 print("Error getting profiles data",self.__count_exec )
2217 print("Error getting profiles data",self.__count_exec )
2226 print(e)
2218 print(e)
2227 sys.exit()
2219 sys.exit()
2228
2220
2229 if self.__dataReady:
2221 if self.__dataReady:
2230 #print("omitting: ", len(self.outliers_IDs_list))
2222 #print("omitting: ", len(self.outliers_IDs_list))
2231 self.__count_exec = 0
2223 self.__count_exec = 0
2232 #dataOut.data =
2224 #dataOut.data =
2233 #self.buffer = numpy.flip(dataBlock, axis=1)
2225 #self.buffer = numpy.flip(dataBlock, axis=1)
2234 self.buffer = dataBlock
2226 self.buffer = dataBlock
2235 self.first_utcBlock = self.__initime
2227 self.first_utcBlock = self.__initime
2236 dataOut.utctime = self.__initime
2228 dataOut.utctime = self.__initime
2237 dataOut.nProfiles = self.__profIndex
2229 dataOut.nProfiles = self.__profIndex
2238 #dataOut.flagNoData = False
2230 #dataOut.flagNoData = False
2239 self.init_prof = 0
2231 self.init_prof = 0
2240 self.__profIndex = 0
2232 self.__profIndex = 0
2241 self.__initime = None
2233 self.__initime = None
2242 dataBlock = None
2234 dataBlock = None
2243 self.__buffer_times = []
2235 self.__buffer_times = []
2244 dataOut.error = False
2236 dataOut.error = False
2245 dataOut.useInputBuffer = True
2237 dataOut.useInputBuffer = True
2246 dataOut.buffer_empty = False
2238 dataOut.buffer_empty = False
2247 #print("1 ch: {} prof: {} hs: {}".format(int(dataOut.nChannels),int(dataOut.nProfiles),int(dataOut.nHeights)))
2239 #print("1 ch: {} prof: {} hs: {}".format(int(dataOut.nChannels),int(dataOut.nProfiles),int(dataOut.nHeights)))
2248
2240
2249
2241
2250
2242
2251 #print(self.__count_exec)
2243 #print(self.__count_exec)
2252
2244
2253 return dataOut
2245 return dataOut
2254
2246
2255 class RemoveProfileSats(Operation):
2247 class RemoveProfileSats(Operation):
2256 '''
2248 '''
2257 Omite los perfiles contaminados con seΓ±al de satelites,
2249 Omite los perfiles contaminados con seΓ±al de satelites,
2258 In: minHei = min_sat_range
2250 In: minHei = min_sat_range
2259 max_sat_range
2251 max_sat_range
2260 min_hei_ref
2252 min_hei_ref
2261 max_hei_ref
2253 max_hei_ref
2262 th = diference between profiles mean, ref and sats
2254 th = diference between profiles mean, ref and sats
2263 Out:
2255 Out:
2264 profile clean
2256 profile clean
2265 '''
2257 '''
2266
2258
2267 isConfig = False
2259 isConfig = False
2268 min_sats = 0
2260 min_sats = 0
2269 max_sats = 999999999
2261 max_sats = 999999999
2270 min_ref= 0
2262 min_ref= 0
2271 max_ref= 9999999999
2263 max_ref= 9999999999
2272 needReshape = False
2264 needReshape = False
2273 count = 0
2265 count = 0
2274 thdB = 0
2266 thdB = 0
2275 byRanges = False
2267 byRanges = False
2276 min_sats = None
2268 min_sats = None
2277 max_sats = None
2269 max_sats = None
2278 noise = 0
2270 noise = 0
2279
2271
2280 def __init__(self, **kwargs):
2272 def __init__(self, **kwargs):
2281
2273
2282 Operation.__init__(self, **kwargs)
2274 Operation.__init__(self, **kwargs)
2283 self.isConfig = False
2275 self.isConfig = False
2284
2276
2285
2277
2286 def setup(self, dataOut, minHei, maxHei, minRef, maxRef, th, thdB, rangeHeiList):
2278 def setup(self, dataOut, minHei, maxHei, minRef, maxRef, th, thdB, rangeHeiList):
2287
2279
2288 if rangeHeiList!=None:
2280 if rangeHeiList!=None:
2289 self.byRanges = True
2281 self.byRanges = True
2290 else:
2282 else:
2291 if minHei==None or maxHei==None :
2283 if minHei==None or maxHei==None :
2292 raise ValueError("Parameters heights are required")
2284 raise ValueError("Parameters heights are required")
2293 if minRef==None or maxRef==None:
2285 if minRef==None or maxRef==None:
2294 raise ValueError("Parameters heights are required")
2286 raise ValueError("Parameters heights are required")
2295
2287
2296 if self.byRanges:
2288 if self.byRanges:
2297 self.min_sats = []
2289 self.min_sats = []
2298 self.max_sats = []
2290 self.max_sats = []
2299 for min,max in rangeHeiList:
2291 for min,max in rangeHeiList:
2300 a,b = getHei_index(min, max, dataOut.heightList)
2292 a,b = getHei_index(min, max, dataOut.heightList)
2301 self.min_sats.append(a)
2293 self.min_sats.append(a)
2302 self.max_sats.append(b)
2294 self.max_sats.append(b)
2303 else:
2295 else:
2304 self.min_sats, self.max_sats = getHei_index(minHei, maxHei, dataOut.heightList)
2296 self.min_sats, self.max_sats = getHei_index(minHei, maxHei, dataOut.heightList)
2305 self.min_ref, self.max_ref = getHei_index(minRef, maxRef, dataOut.heightList)
2297 self.min_ref, self.max_ref = getHei_index(minRef, maxRef, dataOut.heightList)
2306 self.th = th
2298 self.th = th
2307 self.thdB = thdB
2299 self.thdB = thdB
2308 self.isConfig = True
2300 self.isConfig = True
2309
2301
2310
2302
2311 def compareRanges(self,data, minHei,maxHei):
2303 def compareRanges(self,data, minHei,maxHei):
2312
2304
2313 # ref = data[0,self.min_ref:self.max_ref] * numpy.conjugate(data[0,self.min_ref:self.max_ref])
2305 # ref = data[0,self.min_ref:self.max_ref] * numpy.conjugate(data[0,self.min_ref:self.max_ref])
2314 # p_ref = 10*numpy.log10(ref.real)
2306 # p_ref = 10*numpy.log10(ref.real)
2315 # m_ref = numpy.mean(p_ref)
2307 # m_ref = numpy.mean(p_ref)
2316
2308
2317 m_ref = self.noise
2309 m_ref = self.noise
2318
2310
2319 sats = data[0,minHei:maxHei] * numpy.conjugate(data[0,minHei:maxHei])
2311 sats = data[0,minHei:maxHei] * numpy.conjugate(data[0,minHei:maxHei])
2320 p_sats = 10*numpy.log10(sats.real)
2312 p_sats = 10*numpy.log10(sats.real)
2321 m_sats = numpy.mean(p_sats)
2313 m_sats = numpy.mean(p_sats)
2322
2314
2323 if m_sats > (m_ref + self.th): #and (m_sats > self.thdB):
2315 if m_sats > (m_ref + self.th): #and (m_sats > self.thdB):
2324 #print("msats: ",m_sats," \tmRef: ", m_ref, "\t",(m_sats - m_ref))
2316 #print("msats: ",m_sats," \tmRef: ", m_ref, "\t",(m_sats - m_ref))
2325 #print("Removing profiles...")
2317 #print("Removing profiles...")
2326 return False
2318 return False
2327
2319
2328 return True
2320 return True
2329
2321
2330 def isProfileClean(self, data):
2322 def isProfileClean(self, data):
2331 '''
2323 '''
2332 Analiza solo 1 canal, y descarta todos...
2324 Analiza solo 1 canal, y descarta todos...
2333 '''
2325 '''
2334
2326
2335 clean = True
2327 clean = True
2336
2328
2337 if self.byRanges:
2329 if self.byRanges:
2338
2330
2339 for n in range(len(self.min_sats)):
2331 for n in range(len(self.min_sats)):
2340 c = self.compareRanges(data,self.min_sats[n],self.max_sats[n])
2332 c = self.compareRanges(data,self.min_sats[n],self.max_sats[n])
2341 clean = clean and c
2333 clean = clean and c
2342 else:
2334 else:
2343
2335
2344 clean = (self.compareRanges(data, self.min_sats,self.max_sats))
2336 clean = (self.compareRanges(data, self.min_sats,self.max_sats))
2345
2337
2346 return clean
2338 return clean
2347
2339
2348
2340
2349
2341
2350 def run(self, dataOut, minHei=None, maxHei=None, minRef=None, maxRef=None, th=5, thdB=65, rangeHeiList=None):
2342 def run(self, dataOut, minHei=None, maxHei=None, minRef=None, maxRef=None, th=5, thdB=65, rangeHeiList=None):
2351 dataOut.flagNoData = True
2343 dataOut.flagNoData = True
2352
2344
2353 if not self.isConfig:
2345 if not self.isConfig:
2354 self.setup(dataOut, minHei, maxHei, minRef, maxRef, th, thdB, rangeHeiList)
2346 self.setup(dataOut, minHei, maxHei, minRef, maxRef, th, thdB, rangeHeiList)
2355 self.isConfig = True
2347 self.isConfig = True
2356 #print(self.min_sats,self.max_sats)
2348 #print(self.min_sats,self.max_sats)
2357 if dataOut.flagDataAsBlock:
2349 if dataOut.flagDataAsBlock:
2358 raise ValueError("ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False")
2350 raise ValueError("ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False")
2359
2351
2360 else:
2352 else:
2361 self.noise =10*numpy.log10(dataOut.getNoisebyHildebrand(ymin_index=self.min_ref, ymax_index=self.max_ref))
2353 self.noise =10*numpy.log10(dataOut.getNoisebyHildebrand(ymin_index=self.min_ref, ymax_index=self.max_ref))
2362 if not self.isProfileClean(dataOut.data):
2354 if not self.isProfileClean(dataOut.data):
2363 return dataOut
2355 return dataOut
2364 #dataOut.data = numpy.full((dataOut.nChannels,dataOut.nHeights),numpy.NAN)
2356 #dataOut.data = numpy.full((dataOut.nChannels,dataOut.nHeights),numpy.NAN)
2365 #self.count += 1
2357 #self.count += 1
2366
2358
2367 dataOut.flagNoData = False
2359 dataOut.flagNoData = False
2368
2360
2369 return dataOut
2361 return dataOut
General Comments 0
You need to be logged in to leave comments. Login now