jroproc_spectra_lags.py
739 lines
| 25.9 KiB
| text/x-python
|
PythonLexer
|
r768 | import numpy | ||
|
r1167 | from .jroproc_base import ProcessingUnit, Operation | ||
|
r768 | from schainpy.model.data.jrodata import Spectra | ||
from schainpy.model.data.jrodata import hildebrand_sekhon | ||||
class SpectraLagsProc(ProcessingUnit): | ||||
|
r897 | |||
def __init__(self, **kwargs): | ||||
ProcessingUnit.__init__(self, **kwargs) | ||||
|
r776 | self.__input_buffer = None | ||
|
r768 | self.firstdatatime = None | ||
self.profIndex = 0 | ||||
self.dataOut = Spectra() | ||||
self.id_min = None | ||||
self.id_max = None | ||||
|
r776 | self.__codeIndex = 0 | ||
|
r897 | |||
|
r776 | self.__lags_buffer = None | ||
|
r768 | |||
def __updateSpecFromVoltage(self): | ||||
|
r897 | |||
|
r776 | self.dataOut.plotting = "spectra_lags" | ||
|
r768 | self.dataOut.timeZone = self.dataIn.timeZone | ||
self.dataOut.dstFlag = self.dataIn.dstFlag | ||||
self.dataOut.errorCount = self.dataIn.errorCount | ||||
self.dataOut.useLocalTime = self.dataIn.useLocalTime | ||||
|
r897 | |||
|
r768 | self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy() | ||
self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy() | ||||
r1396 | self.dataOut.ippSeconds = self.dataIn.getDeltaH() * (10 ** -6) / 0.15 | |||
|
r897 | |||
|
r768 | self.dataOut.channelList = self.dataIn.channelList | ||
self.dataOut.heightList = self.dataIn.heightList | ||||
r1396 | self.dataOut.dtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')]) | |||
|
r897 | |||
|
r768 | self.dataOut.nBaud = self.dataIn.nBaud | ||
self.dataOut.nCode = self.dataIn.nCode | ||||
self.dataOut.code = self.dataIn.code | ||||
|
r770 | # self.dataOut.nProfiles = self.dataOut.nFFTPoints | ||
|
r897 | |||
|
r768 | self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock | ||
self.dataOut.utctime = self.firstdatatime | ||||
r1396 | self.dataOut.flagDecodeData = self.dataIn.flagDecodeData # asumo q la data esta decodificada | |||
self.dataOut.flagDeflipData = self.dataIn.flagDeflipData # asumo q la data esta sin flip | ||||
|
r768 | self.dataOut.flagShiftFFT = False | ||
|
r897 | |||
|
r768 | self.dataOut.nCohInt = self.dataIn.nCohInt | ||
self.dataOut.nIncohInt = 1 | ||||
|
r897 | |||
|
r768 | self.dataOut.windowOfFilter = self.dataIn.windowOfFilter | ||
|
r897 | |||
|
r768 | self.dataOut.frequency = self.dataIn.frequency | ||
self.dataOut.realtime = self.dataIn.realtime | ||||
|
r897 | |||
|
r768 | self.dataOut.azimuth = self.dataIn.azimuth | ||
self.dataOut.zenith = self.dataIn.zenith | ||||
|
r897 | |||
|
r768 | self.dataOut.beam.codeList = self.dataIn.beam.codeList | ||
self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList | ||||
self.dataOut.beam.zenithList = self.dataIn.beam.zenithList | ||||
|
r897 | |||
|
r776 | def __createLagsBlock(self, voltages): | ||
|
r897 | |||
|
r776 | if self.__lags_buffer is None: | ||
self.__lags_buffer = numpy.zeros((self.dataOut.nChannels, self.dataOut.nProfiles, self.dataOut.nHeights), dtype='complex') | ||||
|
r897 | |||
|
r776 | nsegments = self.dataOut.nHeights - self.dataOut.nProfiles | ||
|
r897 | |||
|
r776 | # codes = numpy.conjugate(self.__input_buffer[:,9:169])/10000 | ||
|
r897 | |||
|
r776 | for i in range(nsegments): | ||
r1396 | self.__lags_buffer[:, :, i] = voltages[:, i:i + self.dataOut.nProfiles] # *codes | |||
|
r897 | |||
|
r776 | return self.__lags_buffer | ||
|
r897 | |||
|
r776 | def __decodeData(self, volt_buffer, pulseIndex=None): | ||
|
r897 | |||
|
r776 | if pulseIndex is None: | ||
return volt_buffer | ||||
|
r897 | |||
r1396 | codes = numpy.conjugate(self.__input_buffer[:, pulseIndex[0]:pulseIndex[1]]) / 10000 | |||
|
r897 | |||
|
r776 | nsegments = self.dataOut.nHeights - self.dataOut.nProfiles | ||
|
r897 | |||
|
r776 | for i in range(nsegments): | ||
r1396 | volt_buffer[:, :, i] = volt_buffer[:, :, i] * codes | |||
|
r897 | |||
|
r776 | return volt_buffer | ||
|
r897 | |||
|
r776 | def __getFft(self, datablock): | ||
|
r768 | """ | ||
Convierte valores de Voltaje a Spectra | ||||
|
r897 | |||
|
r768 | Affected: | ||
self.dataOut.data_spc | ||||
self.dataOut.data_cspc | ||||
self.dataOut.data_dc | ||||
self.dataOut.heightList | ||||
|
r897 | self.profIndex | ||
|
r776 | self.__input_buffer | ||
|
r768 | self.dataOut.flagNoData | ||
""" | ||||
|
r897 | |||
|
r776 | fft_volt = numpy.fft.fft(datablock, n=self.dataOut.nFFTPoints, axis=1) | ||
|
r897 | |||
r1396 | dc = fft_volt[:, 0, :] | |||
|
r897 | |||
r1396 | # calculo de self-spectra | |||
|
r768 | fft_volt = numpy.fft.fftshift(fft_volt, axes=(1,)) | ||
spc = fft_volt * numpy.conjugate(fft_volt) | ||||
spc = spc.real | ||||
|
r897 | |||
|
r768 | blocksize = 0 | ||
|
r794 | blocksize += dc.size | ||
|
r768 | blocksize += spc.size | ||
|
r897 | |||
|
r768 | cspc = None | ||
pairIndex = 0 | ||||
|
r897 | |||
|
r794 | if self.dataOut.pairsList != []: | ||
r1396 | # calculo de cross-spectra | |||
|
r768 | cspc = numpy.zeros((self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex') | ||
for pair in self.dataOut.pairsList: | ||||
if pair[0] not in self.dataOut.channelList: | ||||
r1396 | raise ValueError("Error getting CrossSpectra: pair 0 of %s is not in channelList = %s" % (str(pair), str(self.dataOut.channelList))) | |||
|
r768 | if pair[1] not in self.dataOut.channelList: | ||
r1396 | raise ValueError("Error getting CrossSpectra: pair 1 of %s is not in channelList = %s" % (str(pair), str(self.dataOut.channelList))) | |||
|
r897 | |||
|
r768 | chan_index0 = self.dataOut.channelList.index(pair[0]) | ||
chan_index1 = self.dataOut.channelList.index(pair[1]) | ||||
|
r897 | |||
r1396 | cspc[pairIndex, :, :] = fft_volt[chan_index0, :, :] * numpy.conjugate(fft_volt[chan_index1, :, :]) | |||
|
r768 | pairIndex += 1 | ||
blocksize += cspc.size | ||||
|
r897 | |||
|
r768 | self.dataOut.data_spc = spc | ||
self.dataOut.data_cspc = cspc | ||||
|
r794 | self.dataOut.data_dc = dc | ||
|
r768 | self.dataOut.blockSize = blocksize | ||
self.dataOut.flagShiftFFT = True | ||||
|
r897 | |||
|
r776 | def run(self, nProfiles=None, nFFTPoints=None, pairsList=[], code=None, nCode=None, nBaud=None, codeFromHeader=False, pulseIndex=None): | ||
|
r897 | |||
|
r768 | self.dataOut.flagNoData = True | ||
|
r897 | |||
|
r776 | self.code = None | ||
|
r897 | |||
|
r776 | if codeFromHeader: | ||
if self.dataIn.code is not None: | ||||
self.code = self.dataIn.code | ||||
|
r897 | |||
|
r768 | if code is not None: | ||
r1396 | self.code = numpy.array(code).reshape(nCode, nBaud) | |||
|
r897 | |||
|
r768 | if self.dataIn.type == "Voltage": | ||
|
r897 | |||
|
r768 | if nFFTPoints == None: | ||
|
r1167 | raise ValueError("This SpectraProc.run() need nFFTPoints input variable") | ||
|
r897 | |||
|
r768 | if nProfiles == None: | ||
nProfiles = nFFTPoints | ||||
|
r897 | |||
|
r776 | self.profIndex == nProfiles | ||
self.firstdatatime = self.dataIn.utctime | ||||
|
r897 | |||
|
r776 | self.dataOut.ippFactor = 1 | ||
|
r768 | self.dataOut.nFFTPoints = nFFTPoints | ||
|
r770 | self.dataOut.nProfiles = nProfiles | ||
|
r768 | self.dataOut.pairsList = pairsList | ||
|
r897 | |||
|
r776 | self.__updateSpecFromVoltage() | ||
|
r897 | |||
|
r776 | if not self.dataIn.flagDataAsBlock: | ||
self.__input_buffer = self.dataIn.data.copy() | ||||
|
r897 | |||
|
r776 | lags_block = self.__createLagsBlock(self.__input_buffer) | ||
|
r897 | |||
|
r776 | lags_block = self.__decodeData(lags_block, pulseIndex) | ||
|
r897 | |||
|
r776 | else: | ||
self.__input_buffer = self.dataIn.data.copy() | ||||
|
r897 | |||
|
r776 | self.__getFft(lags_block) | ||
|
r897 | |||
|
r768 | self.dataOut.flagNoData = False | ||
|
r897 | |||
|
r768 | return True | ||
|
r897 | |||
r1396 | raise ValueError("The type of input object '%s' is not valid" % (self.dataIn.type)) | |||
|
r897 | |||
|
r768 | def __selectPairs(self, pairsList): | ||
|
r897 | |||
|
r768 | if channelList == None: | ||
return | ||||
|
r897 | |||
|
r768 | pairsIndexListSelected = [] | ||
|
r897 | |||
|
r768 | for thisPair in pairsList: | ||
|
r897 | |||
|
r768 | if thisPair not in self.dataOut.pairsList: | ||
continue | ||||
|
r897 | |||
|
r768 | pairIndex = self.dataOut.pairsList.index(thisPair) | ||
|
r897 | |||
|
r768 | pairsIndexListSelected.append(pairIndex) | ||
|
r897 | |||
|
r768 | if not pairsIndexListSelected: | ||
self.dataOut.data_cspc = None | ||||
self.dataOut.pairsList = [] | ||||
return | ||||
|
r897 | |||
|
r768 | self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndexListSelected] | ||
|
r897 | self.dataOut.pairsList = [self.dataOut.pairsList[i] for i in pairsIndexListSelected] | ||
|
r768 | return | ||
|
r897 | |||
|
r768 | def __selectPairsByChannel(self, channelList=None): | ||
|
r897 | |||
|
r768 | if channelList == None: | ||
return | ||||
|
r897 | |||
|
r768 | pairsIndexListSelected = [] | ||
for pairIndex in self.dataOut.pairsIndexList: | ||||
r1396 | # First pair | |||
|
r768 | if self.dataOut.pairsList[pairIndex][0] not in channelList: | ||
continue | ||||
r1396 | # Second pair | |||
|
r768 | if self.dataOut.pairsList[pairIndex][1] not in channelList: | ||
continue | ||||
|
r897 | |||
|
r768 | pairsIndexListSelected.append(pairIndex) | ||
|
r897 | |||
|
r768 | if not pairsIndexListSelected: | ||
self.dataOut.data_cspc = None | ||||
self.dataOut.pairsList = [] | ||||
return | ||||
|
r897 | |||
|
r768 | self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndexListSelected] | ||
|
r897 | self.dataOut.pairsList = [self.dataOut.pairsList[i] for i in pairsIndexListSelected] | ||
|
r768 | return | ||
|
r897 | |||
|
r768 | def selectChannels(self, channelList): | ||
|
r897 | |||
|
r768 | channelIndexList = [] | ||
|
r897 | |||
|
r768 | for channel in channelList: | ||
if channel not in self.dataOut.channelList: | ||||
r1396 | raise ValueError("Error selecting channels, Channel %d is not valid.\nAvailable channels = %s" % (channel, str(self.dataOut.channelList))) | |||
|
r897 | |||
|
r768 | index = self.dataOut.channelList.index(channel) | ||
channelIndexList.append(index) | ||||
|
r897 | |||
|
r768 | self.selectChannelsByIndex(channelIndexList) | ||
|
r897 | |||
|
r768 | def selectChannelsByIndex(self, channelIndexList): | ||
""" | ||||
|
r897 | Selecciona un bloque de datos en base a canales segun el channelIndexList | ||
|
r768 | Input: | ||
|
r897 | channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7] | ||
|
r768 | Affected: | ||
self.dataOut.data_spc | ||||
self.dataOut.channelIndexList | ||||
self.dataOut.nChannels | ||||
|
r897 | |||
|
r768 | Return: | ||
None | ||||
""" | ||||
for channelIndex in channelIndexList: | ||||
if channelIndex not in self.dataOut.channelIndexList: | ||||
r1396 | raise ValueError("Error selecting channels: The value %d in channelIndexList is not valid.\nAvailable channel indexes = " % (channelIndex, self.dataOut.channelIndexList)) | |||
|
r897 | |||
|
r768 | # nChannels = len(channelIndexList) | ||
|
r897 | |||
r1396 | data_spc = self.dataOut.data_spc[channelIndexList, :] | |||
data_dc = self.dataOut.data_dc[channelIndexList, :] | ||||
|
r897 | |||
|
r768 | self.dataOut.data_spc = data_spc | ||
self.dataOut.data_dc = data_dc | ||||
|
r897 | |||
|
r768 | self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList] | ||
# self.dataOut.nChannels = nChannels | ||||
|
r897 | |||
|
r768 | self.__selectPairsByChannel(self.dataOut.channelList) | ||
|
r897 | |||
|
r768 | return 1 | ||
def selectHeights(self, minHei, maxHei): | ||||
""" | ||||
Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango | ||||
minHei <= height <= maxHei | ||||
|
r897 | |||
|
r768 | Input: | ||
|
r897 | minHei : valor minimo de altura a considerar | ||
|
r768 | maxHei : valor maximo de altura a considerar | ||
|
r897 | |||
|
r768 | Affected: | ||
Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex | ||||
|
r897 | |||
|
r768 | Return: | ||
1 si el metodo se ejecuto con exito caso contrario devuelve 0 | ||||
""" | ||||
|
r897 | |||
|
r768 | if (minHei > maxHei): | ||
|
r1167 | raise ValueError("Error selecting heights: Height range (%d,%d) is not valid" % (minHei, maxHei)) | ||
|
r897 | |||
|
r768 | if (minHei < self.dataOut.heightList[0]): | ||
minHei = self.dataOut.heightList[0] | ||||
|
r897 | |||
|
r768 | if (maxHei > self.dataOut.heightList[-1]): | ||
maxHei = self.dataOut.heightList[-1] | ||||
minIndex = 0 | ||||
maxIndex = 0 | ||||
heights = self.dataOut.heightList | ||||
|
r897 | |||
|
r768 | inda = numpy.where(heights >= minHei) | ||
indb = numpy.where(heights <= maxHei) | ||||
|
r897 | |||
|
r768 | try: | ||
minIndex = inda[0][0] | ||||
except: | ||||
minIndex = 0 | ||||
|
r897 | |||
|
r768 | try: | ||
maxIndex = indb[0][-1] | ||||
except: | ||||
maxIndex = len(heights) | ||||
self.selectHeightsByIndex(minIndex, maxIndex) | ||||
|
r897 | |||
|
r768 | return 1 | ||
r1396 | def getBeaconSignal(self, tauindex=0, channelindex=0, hei_ref=None): | |||
newheis = numpy.where(self.dataOut.heightList > self.dataOut.radarControllerHeaderObj.Taus[tauindex]) | ||||
|
r897 | |||
|
r768 | if hei_ref != None: | ||
r1396 | newheis = numpy.where(self.dataOut.heightList > hei_ref) | |||
|
r897 | |||
|
r768 | minIndex = min(newheis[0]) | ||
maxIndex = max(newheis[0]) | ||||
r1396 | data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1] | |||
heightList = self.dataOut.heightList[minIndex:maxIndex + 1] | ||||
|
r897 | |||
|
r768 | # determina indices | ||
r1396 | nheis = int(self.dataOut.radarControllerHeaderObj.txB / (self.dataOut.heightList[1] - self.dataOut.heightList[0])) | |||
avg_dB = 10 * numpy.log10(numpy.sum(data_spc[channelindex, :, :], axis=0)) | ||||
|
r768 | beacon_dB = numpy.sort(avg_dB)[-nheis:] | ||
beacon_heiIndexList = [] | ||||
for val in avg_dB.tolist(): | ||||
if val >= beacon_dB[0]: | ||||
beacon_heiIndexList.append(avg_dB.tolist().index(val)) | ||||
|
r897 | |||
r1396 | # data_spc = data_spc[:,:,beacon_heiIndexList] | |||
|
r768 | data_cspc = None | ||
if self.dataOut.data_cspc is not None: | ||||
r1396 | data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1] | |||
# data_cspc = data_cspc[:,:,beacon_heiIndexList] | ||||
|
r897 | |||
|
r768 | data_dc = None | ||
if self.dataOut.data_dc is not None: | ||||
r1396 | data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1] | |||
# data_dc = data_dc[:,beacon_heiIndexList] | ||||
|
r897 | |||
|
r768 | self.dataOut.data_spc = data_spc | ||
self.dataOut.data_cspc = data_cspc | ||||
self.dataOut.data_dc = data_dc | ||||
self.dataOut.heightList = heightList | ||||
self.dataOut.beacon_heiIndexList = beacon_heiIndexList | ||||
|
r897 | |||
|
r768 | return 1 | ||
|
r897 | |||
|
r768 | def selectHeightsByIndex(self, minIndex, maxIndex): | ||
""" | ||||
Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango | ||||
minIndex <= index <= maxIndex | ||||
|
r897 | |||
|
r768 | Input: | ||
|
r897 | minIndex : valor de indice minimo de altura a considerar | ||
|
r768 | maxIndex : valor de indice maximo de altura a considerar | ||
|
r897 | |||
|
r768 | Affected: | ||
self.dataOut.data_spc | ||||
self.dataOut.data_cspc | ||||
self.dataOut.data_dc | ||||
self.dataOut.heightList | ||||
|
r897 | |||
|
r768 | Return: | ||
1 si el metodo se ejecuto con exito caso contrario devuelve 0 | ||||
""" | ||||
|
r897 | |||
|
r768 | if (minIndex < 0) or (minIndex > maxIndex): | ||
|
r1167 | raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % (minIndex, maxIndex)) | ||
|
r897 | |||
|
r768 | if (maxIndex >= self.dataOut.nHeights): | ||
r1396 | maxIndex = self.dataOut.nHeights - 1 | |||
|
r768 | |||
r1396 | # Spectra | |||
data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1] | ||||
|
r897 | |||
|
r768 | data_cspc = None | ||
if self.dataOut.data_cspc is not None: | ||||
r1396 | data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1] | |||
|
r897 | |||
|
r768 | data_dc = None | ||
if self.dataOut.data_dc is not None: | ||||
r1396 | data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1] | |||
|
r897 | |||
|
r768 | self.dataOut.data_spc = data_spc | ||
self.dataOut.data_cspc = data_cspc | ||||
self.dataOut.data_dc = data_dc | ||||
|
r897 | |||
r1396 | self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex + 1] | |||
|
r897 | |||
|
r768 | return 1 | ||
|
r897 | |||
r1396 | def removeDC(self, mode=2): | |||
|
r768 | jspectra = self.dataOut.data_spc | ||
jcspectra = self.dataOut.data_cspc | ||||
|
r897 | |||
|
r768 | num_chan = jspectra.shape[0] | ||
num_hei = jspectra.shape[2] | ||||
|
r897 | |||
|
r768 | if jcspectra is not None: | ||
jcspectraExist = True | ||||
num_pairs = jcspectra.shape[0] | ||||
else: jcspectraExist = False | ||||
|
r897 | |||
r1396 | freq_dc = jspectra.shape[1] / 2 | |||
ind_vel = numpy.array([-2, -1, 1, 2]) + freq_dc | ||||
|
r897 | |||
r1396 | if ind_vel[0] < 0: | |||
ind_vel[list(range(0, 1))] = ind_vel[list(range(0, 1))] + self.num_prof | ||||
|
r897 | |||
if mode == 1: | ||||
r1396 | jspectra[:, freq_dc, :] = (jspectra[:, ind_vel[1], :] + jspectra[:, ind_vel[2], :]) / 2 # CORRECCION | |||
|
r897 | |||
|
r768 | if jcspectraExist: | ||
r1396 | jcspectra[:, freq_dc, :] = (jcspectra[:, ind_vel[1], :] + jcspectra[:, ind_vel[2], :]) / 2 | |||
|
r897 | |||
|
r768 | if mode == 2: | ||
|
r897 | |||
r1396 | vel = numpy.array([-2, -1, 1, 2]) | |||
xx = numpy.zeros([4, 4]) | ||||
|
r897 | |||
|
r768 | for fil in range(4): | ||
r1396 | xx[fil, :] = vel[fil] ** numpy.asarray(list(range(4))) | |||
|
r897 | |||
|
r768 | xx_inv = numpy.linalg.inv(xx) | ||
r1396 | xx_aux = xx_inv[0, :] | |||
|
r897 | |||
|
r768 | for ich in range(num_chan): | ||
r1396 | yy = jspectra[ich, ind_vel, :] | |||
jspectra[ich, freq_dc, :] = numpy.dot(xx_aux, yy) | ||||
|
r768 | |||
r1396 | junkid = jspectra[ich, freq_dc, :] <= 0 | |||
|
r768 | cjunkid = sum(junkid) | ||
|
r897 | |||
|
r768 | if cjunkid.any(): | ||
r1396 | jspectra[ich, freq_dc, junkid.nonzero()] = (jspectra[ich, ind_vel[1], junkid] + jspectra[ich, ind_vel[2], junkid]) / 2 | |||
|
r897 | |||
|
r768 | if jcspectraExist: | ||
for ip in range(num_pairs): | ||||
r1396 | yy = jcspectra[ip, ind_vel, :] | |||
jcspectra[ip, freq_dc, :] = numpy.dot(xx_aux, yy) | ||||
|
r897 | |||
|
r768 | self.dataOut.data_spc = jspectra | ||
self.dataOut.data_cspc = jcspectra | ||||
|
r897 | |||
|
r768 | return 1 | ||
|
r897 | |||
r1396 | def removeInterference(self, interf=2, hei_interf=None, nhei_interf=None, offhei_interf=None): | |||
|
r897 | |||
|
r768 | jspectra = self.dataOut.data_spc | ||
jcspectra = self.dataOut.data_cspc | ||||
jnoise = self.dataOut.getNoise() | ||||
num_incoh = self.dataOut.nIncohInt | ||||
|
r897 | |||
r1396 | num_channel = jspectra.shape[0] | |||
num_prof = jspectra.shape[1] | ||||
num_hei = jspectra.shape[2] | ||||
|
r897 | |||
r1396 | # hei_interf | |||
|
r768 | if hei_interf is None: | ||
r1396 | count_hei = num_hei / 2 # Como es entero no importa | |||
|
r1167 | hei_interf = numpy.asmatrix(list(range(count_hei))) + num_hei - count_hei | ||
|
r768 | hei_interf = numpy.asarray(hei_interf)[0] | ||
r1396 | # nhei_interf | |||
|
r768 | if (nhei_interf == None): | ||
nhei_interf = 5 | ||||
if (nhei_interf < 1): | ||||
|
r897 | nhei_interf = 1 | ||
|
r768 | if (nhei_interf > count_hei): | ||
nhei_interf = count_hei | ||||
|
r897 | if (offhei_interf == None): | ||
|
r768 | offhei_interf = 0 | ||
|
r897 | |||
|
r1167 | ind_hei = list(range(num_hei)) | ||
|
r897 | # mask_prof = numpy.asarray(range(num_prof - 2)) + 1 | ||
|
r768 | # mask_prof[range(num_prof/2 - 1,len(mask_prof))] += 1 | ||
|
r1167 | mask_prof = numpy.asarray(list(range(num_prof))) | ||
|
r768 | num_mask_prof = mask_prof.size | ||
r1396 | comp_mask_prof = [0, num_prof / 2] | |||
|
r897 | |||
r1396 | # noise_exist: Determina si la variable jnoise ha sido definida y contiene la informacion del ruido de cada canal | |||
|
r768 | if (jnoise.size < num_channel or numpy.isnan(jnoise).any()): | ||
jnoise = numpy.nan | ||||
noise_exist = jnoise[0] < numpy.Inf | ||||
|
r897 | |||
r1396 | # Subrutina de Remocion de la Interferencia | |||
|
r768 | for ich in range(num_channel): | ||
r1396 | # Se ordena los espectros segun su potencia (menor a mayor) | |||
power = jspectra[ich, mask_prof, :] | ||||
power = power[:, hei_interf] | ||||
power = power.sum(axis=0) | ||||
|
r768 | psort = power.ravel().argsort() | ||
|
r897 | |||
r1396 | # Se estima la interferencia promedio en los Espectros de Potencia empleando | |||
junkspc_interf = jspectra[ich, :, hei_interf[psort[list(range(offhei_interf, nhei_interf + offhei_interf))]]] | ||||
|
r897 | |||
|
r768 | if noise_exist: | ||
# tmp_noise = jnoise[ich] / num_prof | ||||
tmp_noise = jnoise[ich] | ||||
junkspc_interf = junkspc_interf - tmp_noise | ||||
r1396 | # junkspc_interf[:,comp_mask_prof] = 0 | |||
|
r897 | |||
r1396 | jspc_interf = junkspc_interf.sum(axis=0) / nhei_interf | |||
|
r768 | jspc_interf = jspc_interf.transpose() | ||
r1396 | # Calculando el espectro de interferencia promedio | |||
noiseid = numpy.where(jspc_interf <= tmp_noise / numpy.sqrt(num_incoh)) | ||||
|
r768 | noiseid = noiseid[0] | ||
cnoiseid = noiseid.size | ||||
r1396 | interfid = numpy.where(jspc_interf > tmp_noise / numpy.sqrt(num_incoh)) | |||
|
r768 | interfid = interfid[0] | ||
cinterfid = interfid.size | ||||
|
r897 | |||
|
r768 | if (cnoiseid > 0): jspc_interf[noiseid] = 0 | ||
|
r897 | |||
r1396 | # Expandiendo los perfiles a limpiar | |||
|
r768 | if (cinterfid > 0): | ||
r1396 | new_interfid = (numpy.r_[interfid - 1, interfid, interfid + 1] + num_prof) % num_prof | |||
|
r897 | new_interfid = numpy.asarray(new_interfid) | ||
|
r768 | new_interfid = {x for x in new_interfid} | ||
new_interfid = numpy.array(list(new_interfid)) | ||||
new_cinterfid = new_interfid.size | ||||
else: new_cinterfid = 0 | ||||
|
r897 | |||
|
r768 | for ip in range(new_cinterfid): | ||
r1396 | ind = junkspc_interf[:, new_interfid[ip]].ravel().argsort() | |||
jspc_interf[new_interfid[ip]] = junkspc_interf[ind[nhei_interf / 2], new_interfid[ip]] | ||||
|
r897 | |||
r1396 | jspectra[ich, :, ind_hei] = jspectra[ich, :, ind_hei] - jspc_interf # Corregir indices | |||
|
r897 | |||
r1396 | # Removiendo la interferencia del punto de mayor interferencia | |||
|
r768 | ListAux = jspc_interf[mask_prof].tolist() | ||
maxid = ListAux.index(max(ListAux)) | ||||
|
r897 | |||
|
r768 | if cinterfid > 0: | ||
r1396 | for ip in range(cinterfid * (interf == 2) - 1): | |||
ind = (jspectra[ich, interfid[ip], :] < tmp_noise * (1 + 1 / numpy.sqrt(num_incoh))).nonzero() | ||||
|
r768 | cind = len(ind) | ||
|
r897 | |||
|
r768 | if (cind > 0): | ||
r1396 | jspectra[ich, interfid[ip], ind] = tmp_noise * (1 + (numpy.random.uniform(cind) - 0.5) / numpy.sqrt(num_incoh)) | |||
|
r897 | |||
r1396 | ind = numpy.array([-2, -1, 1, 2]) | |||
xx = numpy.zeros([4, 4]) | ||||
|
r897 | |||
|
r768 | for id1 in range(4): | ||
r1396 | xx[:, id1] = ind[id1] ** numpy.asarray(list(range(4))) | |||
|
r897 | |||
|
r768 | xx_inv = numpy.linalg.inv(xx) | ||
r1396 | xx = xx_inv[:, 0] | |||
ind = (ind + maxid + num_mask_prof) % num_mask_prof | ||||
yy = jspectra[ich, mask_prof[ind], :] | ||||
jspectra[ich, mask_prof[maxid], :] = numpy.dot(yy.transpose(), xx) | ||||
|
r897 | |||
r1396 | indAux = (jspectra[ich, :, :] < tmp_noise * (1 - 1 / numpy.sqrt(num_incoh))).nonzero() | |||
jspectra[ich, indAux[0], indAux[1]] = tmp_noise * (1 - 1 / numpy.sqrt(num_incoh)) | ||||
|
r897 | |||
r1396 | # Remocion de Interferencia en el Cross Spectra | |||
|
r768 | if jcspectra is None: return jspectra, jcspectra | ||
r1396 | num_pairs = jcspectra.size / (num_prof * num_hei) | |||
|
r768 | jcspectra = jcspectra.reshape(num_pairs, num_prof, num_hei) | ||
|
r897 | |||
|
r768 | for ip in range(num_pairs): | ||
|
r897 | |||
|
r768 | #------------------------------------------- | ||
|
r897 | |||
r1396 | cspower = numpy.abs(jcspectra[ip, mask_prof, :]) | |||
cspower = cspower[:, hei_interf] | ||||
cspower = cspower.sum(axis=0) | ||||
|
r897 | |||
|
r768 | cspsort = cspower.ravel().argsort() | ||
r1396 | junkcspc_interf = jcspectra[ip, :, hei_interf[cspsort[list(range(offhei_interf, nhei_interf + offhei_interf))]]] | |||
|
r768 | junkcspc_interf = junkcspc_interf.transpose() | ||
r1396 | jcspc_interf = junkcspc_interf.sum(axis=1) / nhei_interf | |||
|
r897 | |||
|
r768 | ind = numpy.abs(jcspc_interf[mask_prof]).ravel().argsort() | ||
|
r897 | |||
r1396 | median_real = numpy.median(numpy.real(junkcspc_interf[mask_prof[ind[list(range(3 * num_prof / 4))]], :])) | |||
median_imag = numpy.median(numpy.imag(junkcspc_interf[mask_prof[ind[list(range(3 * num_prof / 4))]], :])) | ||||
junkcspc_interf[comp_mask_prof, :] = numpy.complex(median_real, median_imag) | ||||
|
r897 | |||
|
r768 | for iprof in range(num_prof): | ||
r1396 | ind = numpy.abs(junkcspc_interf[iprof, :]).ravel().argsort() | |||
jcspc_interf[iprof] = junkcspc_interf[iprof, ind[nhei_interf / 2]] | ||||
|
r897 | |||
r1396 | # Removiendo la Interferencia | |||
jcspectra[ip, :, ind_hei] = jcspectra[ip, :, ind_hei] - jcspc_interf | ||||
|
r897 | |||
|
r768 | ListAux = numpy.abs(jcspc_interf[mask_prof]).tolist() | ||
maxid = ListAux.index(max(ListAux)) | ||||
|
r897 | |||
r1396 | ind = numpy.array([-2, -1, 1, 2]) | |||
xx = numpy.zeros([4, 4]) | ||||
|
r897 | |||
|
r768 | for id1 in range(4): | ||
r1396 | xx[:, id1] = ind[id1] ** numpy.asarray(list(range(4))) | |||
|
r897 | |||
|
r768 | xx_inv = numpy.linalg.inv(xx) | ||
r1396 | xx = xx_inv[:, 0] | |||
|
r897 | |||
r1396 | ind = (ind + maxid + num_mask_prof) % num_mask_prof | |||
yy = jcspectra[ip, mask_prof[ind], :] | ||||
jcspectra[ip, mask_prof[maxid], :] = numpy.dot(yy.transpose(), xx) | ||||
|
r897 | |||
r1396 | # Guardar Resultados | |||
|
r768 | self.dataOut.data_spc = jspectra | ||
self.dataOut.data_cspc = jcspectra | ||||
|
r897 | |||
|
r768 | return 1 | ||
|
r897 | |||
|
r768 | def setRadarFrequency(self, frequency=None): | ||
|
r897 | |||
|
r768 | if frequency != None: | ||
self.dataOut.frequency = frequency | ||||
|
r897 | |||
|
r768 | return 1 | ||
|
r897 | |||
def getNoise(self, minHei=None, maxHei=None, minVel=None, maxVel=None): | ||||
r1396 | # validacion de rango | |||
|
r768 | if minHei == None: | ||
minHei = self.dataOut.heightList[0] | ||||
|
r897 | |||
|
r768 | if maxHei == None: | ||
maxHei = self.dataOut.heightList[-1] | ||||
|
r897 | |||
|
r768 | if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei): | ||
r1396 | print('minHei: %.2f is out of the heights range' % (minHei)) | |||
print('minHei is setting to %.2f' % (self.dataOut.heightList[0])) | ||||
|
r768 | minHei = self.dataOut.heightList[0] | ||
|
r897 | |||
|
r768 | if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei): | ||
r1396 | print('maxHei: %.2f is out of the heights range' % (maxHei)) | |||
print('maxHei is setting to %.2f' % (self.dataOut.heightList[-1])) | ||||
|
r768 | maxHei = self.dataOut.heightList[-1] | ||
|
r897 | |||
|
r768 | # validacion de velocidades | ||
velrange = self.dataOut.getVelRange(1) | ||||
|
r897 | |||
|
r768 | if minVel == None: | ||
minVel = velrange[0] | ||||
|
r897 | |||
|
r768 | if maxVel == None: | ||
maxVel = velrange[-1] | ||||
|
r897 | |||
|
r768 | if (minVel < velrange[0]) or (minVel > maxVel): | ||
r1396 | print('minVel: %.2f is out of the velocity range' % (minVel)) | |||
print('minVel is setting to %.2f' % (velrange[0])) | ||||
|
r768 | minVel = velrange[0] | ||
|
r897 | |||
|
r768 | if (maxVel > velrange[-1]) or (maxVel < minVel): | ||
r1396 | print('maxVel: %.2f is out of the velocity range' % (maxVel)) | |||
print('maxVel is setting to %.2f' % (velrange[-1])) | ||||
|
r768 | maxVel = velrange[-1] | ||
|
r897 | |||
# seleccion de indices para rango | ||||
|
r768 | minIndex = 0 | ||
maxIndex = 0 | ||||
heights = self.dataOut.heightList | ||||
|
r897 | |||
|
r768 | inda = numpy.where(heights >= minHei) | ||
indb = numpy.where(heights <= maxHei) | ||||
|
r897 | |||
|
r768 | try: | ||
minIndex = inda[0][0] | ||||
except: | ||||
minIndex = 0 | ||||
|
r897 | |||
|
r768 | try: | ||
maxIndex = indb[0][-1] | ||||
except: | ||||
maxIndex = len(heights) | ||||
if (minIndex < 0) or (minIndex > maxIndex): | ||||
|
r1167 | raise ValueError("some value in (%d,%d) is not valid" % (minIndex, maxIndex)) | ||
|
r897 | |||
|
r768 | if (maxIndex >= self.dataOut.nHeights): | ||
r1396 | maxIndex = self.dataOut.nHeights - 1 | |||
|
r768 | |||
# seleccion de indices para velocidades | ||||
indminvel = numpy.where(velrange >= minVel) | ||||
indmaxvel = numpy.where(velrange <= maxVel) | ||||
try: | ||||
minIndexVel = indminvel[0][0] | ||||
except: | ||||
minIndexVel = 0 | ||||
|
r897 | |||
|
r768 | try: | ||
maxIndexVel = indmaxvel[0][-1] | ||||
|
r897 | except: | ||
|
r768 | maxIndexVel = len(velrange) | ||
|
r897 | |||
r1396 | # seleccion del espectro | |||
data_spc = self.dataOut.data_spc[:, minIndexVel:maxIndexVel + 1, minIndex:maxIndex + 1] | ||||
# estimacion de ruido | ||||
|
r768 | noise = numpy.zeros(self.dataOut.nChannels) | ||
|
r897 | |||
|
r768 | for channel in range(self.dataOut.nChannels): | ||
r1396 | daux = data_spc[channel, :, :] | |||
|
r768 | noise[channel] = hildebrand_sekhon(daux, self.dataOut.nIncohInt) | ||
|
r897 | |||
|
r768 | self.dataOut.noise_estimation = noise.copy() | ||
|
r897 | |||
r1377 | return 1 | |||