From 6d8233fe37fd92a40ac6ff755125fc30c676babe 2014-09-23 17:39:50 From: Daniel Valdez Date: 2014-09-23 17:39:50 Subject: [PATCH] these files are not necessary for the new organization --- diff --git a/schainpy/model/jrodata.py b/schainpy/model/jrodata.py deleted file mode 100644 index 1c446fb..0000000 --- a/schainpy/model/jrodata.py +++ /dev/null @@ -1,704 +0,0 @@ -''' - -$Author: murco $ -$Id: JROData.py 173 2012-11-20 15:06:21Z murco $ -''' - -import os, sys -import copy -import numpy -import datetime -import time -from jroheaderIO import SystemHeader, RadarControllerHeader - - -def hildebrand_sekhon(data, navg): - - data = data.copy() - - sortdata = numpy.sort(data,axis=None) - lenOfData = len(sortdata) - nums_min = lenOfData/10 - - if (lenOfData/10) > 2: - nums_min = lenOfData/10 - else: - nums_min = 2 - - sump = 0. - - sumq = 0. - - j = 0 - - cont = 1 - - while((cont==1)and(j nums_min: - rtest = float(j)/(j-1) + 1.0/navg - if ((sumq*j) > (rtest*sump**2)): - j = j - 1 - sump = sump - sortdata[j] - sumq = sumq - sortdata[j]**2 - cont = 0 - - lnoise = sump /j - stdv = numpy.sqrt((sumq - lnoise**2)/(j - 1)) - return lnoise - -class JROData: - -# m_BasicHeader = BasicHeader() -# m_ProcessingHeader = ProcessingHeader() - - systemHeaderObj = SystemHeader() - - radarControllerHeaderObj = RadarControllerHeader() - -# data = None - - type = None - - dtype = None - -# nChannels = None - -# nHeights = None - - nProfiles = None - - heightList = None - - channelList = None - - flagNoData = True - - flagTimeBlock = False - - useLocalTime = False - - utctime = None - - timeZone = None - - dstFlag = None - - errorCount = None - - blocksize = None - - nCode = None - - nBaud = None - - code = None - - flagDecodeData = False #asumo q la data no esta decodificada - - flagDeflipData = False #asumo q la data no esta sin flip - - flagShiftFFT = False - - ippSeconds = None - - timeInterval = None - - nCohInt = None - - noise = None - - windowOfFilter = 1 - - #Speed of ligth - C = 3e8 - - frequency = 49.92e6 - - realtime = False - - beacon_heiIndexList = None - - last_block = None - - blocknow = None - - def __init__(self): - - raise ValueError, "This class has not been implemented" - - def copy(self, inputObj=None): - - if inputObj == None: - return copy.deepcopy(self) - - for key in inputObj.__dict__.keys(): - self.__dict__[key] = inputObj.__dict__[key] - - def deepcopy(self): - - return copy.deepcopy(self) - - def isEmpty(self): - - return self.flagNoData - - def getNoise(self): - - raise ValueError, "Not implemented" - - def getNChannels(self): - - return len(self.channelList) - - def getChannelIndexList(self): - - return range(self.nChannels) - - def getNHeights(self): - - return len(self.heightList) - - def getHeiRange(self, extrapoints=0): - - heis = self.heightList -# deltah = self.heightList[1] - self.heightList[0] -# -# heis.append(self.heightList[-1]) - - return heis - - def getltctime(self): - - if self.useLocalTime: - return self.utctime - self.timeZone*60 - - return self.utctime - - def getDatatime(self): - - datatime = datetime.datetime.utcfromtimestamp(self.ltctime) - return datatime - - def getTimeRange(self): - - datatime = [] - - datatime.append(self.ltctime) - datatime.append(self.ltctime + self.timeInterval) - - datatime = numpy.array(datatime) - - return datatime - - def getFmax(self): - - PRF = 1./(self.ippSeconds * self.nCohInt) - - fmax = PRF/2. - - return fmax - - def getVmax(self): - - _lambda = self.C/self.frequency - - vmax = self.getFmax() * _lambda - - return vmax - - nChannels = property(getNChannels, "I'm the 'nChannel' property.") - channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.") - nHeights = property(getNHeights, "I'm the 'nHeights' property.") - noise = property(getNoise, "I'm the 'nHeights' property.") - datatime = property(getDatatime, "I'm the 'datatime' property") - ltctime = property(getltctime, "I'm the 'ltctime' property") - -class Voltage(JROData): - - #data es un numpy array de 2 dmensiones (canales, alturas) - data = None - - def __init__(self): - ''' - Constructor - ''' - - self.radarControllerHeaderObj = RadarControllerHeader() - - self.systemHeaderObj = SystemHeader() - - self.type = "Voltage" - - self.data = None - - self.dtype = None - -# self.nChannels = 0 - -# self.nHeights = 0 - - self.nProfiles = None - - self.heightList = None - - self.channelList = None - -# self.channelIndexList = None - - self.flagNoData = True - - self.flagTimeBlock = False - - self.utctime = None - - self.timeZone = None - - self.dstFlag = None - - self.errorCount = None - - self.nCohInt = None - - self.blocksize = None - - self.flagDecodeData = False #asumo q la data no esta decodificada - - self.flagDeflipData = False #asumo q la data no esta sin flip - - self.flagShiftFFT = False - - - def getNoisebyHildebrand(self): - """ - Determino el nivel de ruido usando el metodo Hildebrand-Sekhon - - Return: - noiselevel - """ - - for channel in range(self.nChannels): - daux = self.data_spc[channel,:,:] - self.noise[channel] = hildebrand_sekhon(daux, self.nCohInt) - - return self.noise - - def getNoise(self, type = 1): - - self.noise = numpy.zeros(self.nChannels) - - if type == 1: - noise = self.getNoisebyHildebrand() - - return 10*numpy.log10(noise) - -class Spectra(JROData): - - #data es un numpy array de 2 dmensiones (canales, perfiles, alturas) - data_spc = None - - #data es un numpy array de 2 dmensiones (canales, pares, alturas) - data_cspc = None - - #data es un numpy array de 2 dmensiones (canales, alturas) - data_dc = None - - nFFTPoints = None - - nPairs = None - - pairsList = None - - nIncohInt = None - - wavelength = None #Necesario para cacular el rango de velocidad desde la frecuencia - - nCohInt = None #se requiere para determinar el valor de timeInterval - - ippFactor = None - - def __init__(self): - ''' - Constructor - ''' - - self.radarControllerHeaderObj = RadarControllerHeader() - - self.systemHeaderObj = SystemHeader() - - self.type = "Spectra" - -# self.data = None - - self.dtype = None - -# self.nChannels = 0 - -# self.nHeights = 0 - - self.nProfiles = None - - self.heightList = None - - self.channelList = None - -# self.channelIndexList = None - - self.flagNoData = True - - self.flagTimeBlock = False - - self.utctime = None - - self.nCohInt = None - - self.nIncohInt = None - - self.blocksize = None - - self.nFFTPoints = None - - self.wavelength = None - - self.flagDecodeData = False #asumo q la data no esta decodificada - - self.flagDeflipData = False #asumo q la data no esta sin flip - - self.flagShiftFFT = False - - self.ippFactor = 1 - - self.noise = None - - self.beacon_heiIndexList = [] - - - def getNoisebyHildebrand(self): - """ - Determino el nivel de ruido usando el metodo Hildebrand-Sekhon - - Return: - noiselevel - """ - noise = numpy.zeros(self.nChannels) - for channel in range(self.nChannels): - daux = self.data_spc[channel,:,:] - noise[channel] = hildebrand_sekhon(daux, self.nIncohInt) - - return noise - - def getNoise(self): - if self.noise != None: - return self.noise - else: - noise = self.getNoisebyHildebrand() - return noise - - - def getFreqRange(self, extrapoints=0): - - deltafreq = self.getFmax() / (self.nFFTPoints*self.ippFactor) - freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2 - - return freqrange - - def getVelRange(self, extrapoints=0): - - deltav = self.getVmax() / (self.nFFTPoints*self.ippFactor) - velrange = deltav*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltav/2 - - return velrange - - def getNPairs(self): - - return len(self.pairsList) - - def getPairsIndexList(self): - - return range(self.nPairs) - - def getNormFactor(self): - pwcode = 1 - if self.flagDecodeData: - pwcode = numpy.sum(self.code[0]**2) - #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter - normFactor = self.nProfiles*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter - - return normFactor - - def getFlagCspc(self): - - if self.data_cspc == None: - return True - - return False - - def getFlagDc(self): - - if self.data_dc == None: - return True - - return False - - nPairs = property(getNPairs, "I'm the 'nPairs' property.") - pairsIndexList = property(getPairsIndexList, "I'm the 'pairsIndexList' property.") - normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.") - flag_cspc = property(getFlagCspc) - flag_dc = property(getFlagDc) - -class SpectraHeis(JROData): - - data_spc = None - - data_cspc = None - - data_dc = None - - nFFTPoints = None - - nPairs = None - - pairsList = None - - nIncohInt = None - - def __init__(self): - - self.radarControllerHeaderObj = RadarControllerHeader() - - self.systemHeaderObj = SystemHeader() - - self.type = "SpectraHeis" - - self.dtype = None - -# self.nChannels = 0 - -# self.nHeights = 0 - - self.nProfiles = None - - self.heightList = None - - self.channelList = None - -# self.channelIndexList = None - - self.flagNoData = True - - self.flagTimeBlock = False - - self.nPairs = 0 - - self.utctime = None - - self.blocksize = None - -class Fits: - - heightList = None - - channelList = None - - flagNoData = True - - flagTimeBlock = False - - useLocalTime = False - - utctime = None - - timeZone = None - - ippSeconds = None - - timeInterval = None - - nCohInt = None - - nIncohInt = None - - noise = None - - windowOfFilter = 1 - - #Speed of ligth - C = 3e8 - - frequency = 49.92e6 - - realtime = False - - - def __init__(self): - - self.type = "Fits" - - self.nProfiles = None - - self.heightList = None - - self.channelList = None - -# self.channelIndexList = None - - self.flagNoData = True - - self.utctime = None - - self.nCohInt = None - - self.nIncohInt = None - - self.useLocalTime = True - -# self.utctime = None -# self.timeZone = None -# self.ltctime = None -# self.timeInterval = None -# self.header = None -# self.data_header = None -# self.data = None -# self.datatime = None -# self.flagNoData = False -# self.expName = '' -# self.nChannels = None -# self.nSamples = None -# self.dataBlocksPerFile = None -# self.comments = '' -# - - - def getltctime(self): - - if self.useLocalTime: - return self.utctime - self.timeZone*60 - - return self.utctime - - def getDatatime(self): - - datatime = datetime.datetime.utcfromtimestamp(self.ltctime) - return datatime - - def getTimeRange(self): - - datatime = [] - - datatime.append(self.ltctime) - datatime.append(self.ltctime + self.timeInterval) - - datatime = numpy.array(datatime) - - return datatime - - def getHeiRange(self): - - heis = self.heightList - - return heis - - def isEmpty(self): - - return self.flagNoData - - def getNHeights(self): - - return len(self.heightList) - - def getNChannels(self): - - return len(self.channelList) - - def getChannelIndexList(self): - - return range(self.nChannels) - - def getNoise(self, type = 1): - - self.noise = numpy.zeros(self.nChannels) - - if type == 1: - noise = self.getNoisebyHildebrand() - - if type == 2: - noise = self.getNoisebySort() - - if type == 3: - noise = self.getNoisebyWindow() - - return noise - - datatime = property(getDatatime, "I'm the 'datatime' property") - nHeights = property(getNHeights, "I'm the 'nHeights' property.") - nChannels = property(getNChannels, "I'm the 'nChannel' property.") - channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.") - noise = property(getNoise, "I'm the 'nHeights' property.") - datatime = property(getDatatime, "I'm the 'datatime' property") - ltctime = property(getltctime, "I'm the 'ltctime' property") - - ltctime = property(getltctime, "I'm the 'ltctime' property") - -class AMISR: - def __init__(self): - self.flagNoData = True - self.data = None - self.utctime = None - self.type = "AMISR" - - #propiedades para compatibilidad con Voltages - self.timeZone = 300#timezone like jroheader, difference in minutes between UTC and localtime - self.dstFlag = 0#self.dataIn.dstFlag - self.errorCount = 0#self.dataIn.errorCount - self.useLocalTime = True#self.dataIn.useLocalTime - - self.radarControllerHeaderObj = None#self.dataIn.radarControllerHeaderObj.copy() - self.systemHeaderObj = None#self.dataIn.systemHeaderObj.copy() - self.channelList = [0]#self.dataIn.channelList esto solo aplica para el caso de AMISR - self.dtype = numpy.dtype([('real',' basicHeaderObj.utc)): - return 0 - - return 1 - -def isFileinThisTime(filename, startTime, endTime): - """ - Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. - - Inputs: - filename : nombre completo del archivo de datos en formato Jicamarca (.r) - - startTime : tiempo inicial del rango seleccionado en formato datetime.time - - endTime : tiempo final del rango seleccionado en formato datetime.time - - Return: - Boolean : Retorna True si el archivo de datos contiene datos en el rango de - fecha especificado, de lo contrario retorna False. - - Excepciones: - Si el archivo no existe o no puede ser abierto - Si la cabecera no puede ser leida. - - """ - - - try: - fp = open(filename,'rb') - except: - raise IOError, "The file %s can't be opened" %(filename) - - basicHeaderObj = BasicHeader(LOCALTIME) - sts = basicHeaderObj.read(fp) - fp.close() - - thisDatetime = basicHeaderObj.datatime - thisTime = basicHeaderObj.datatime.time() - - if not(sts): - print "Skipping the file %s because it has not a valid header" %(filename) - return None - - if not ((startTime <= thisTime) and (endTime > thisTime)): - return None - - return thisDatetime - -def getFileFromSet(path,ext,set): - validFilelist = [] - fileList = os.listdir(path) - - # 0 1234 567 89A BCDE - # H YYYY DDD SSS .ext - - for file in fileList: - try: - year = int(file[1:5]) - doy = int(file[5:8]) - - - except: - continue - - if (os.path.splitext(file)[-1].lower() != ext.lower()): - continue - - validFilelist.append(file) - - myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set)) - - if len(myfile)!= 0: - return myfile[0] - else: - filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower()) - print 'the filename %s does not exist'%filename - print '...going to the last file: ' - - if validFilelist: - validFilelist = sorted( validFilelist, key=str.lower ) - return validFilelist[-1] - - return None - - -def getlastFileFromPath(path, ext): - """ - Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext" - al final de la depuracion devuelve el ultimo file de la lista que quedo. - - Input: - fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta - ext : extension de los files contenidos en una carpeta - - Return: - El ultimo file de una determinada carpeta, no se considera el path. - """ - validFilelist = [] - fileList = os.listdir(path) - - # 0 1234 567 89A BCDE - # H YYYY DDD SSS .ext - - for file in fileList: - try: - year = int(file[1:5]) - doy = int(file[5:8]) - - - except: - continue - - if (os.path.splitext(file)[-1].lower() != ext.lower()): - continue - - validFilelist.append(file) - - if validFilelist: - validFilelist = sorted( validFilelist, key=str.lower ) - return validFilelist[-1] - - return None - -def checkForRealPath(path, foldercounter, year, doy, set, ext): - """ - Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path, - Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar - el path exacto de un determinado file. - - Example : - nombre correcto del file es .../.../D2009307/P2009307367.ext - - Entonces la funcion prueba con las siguientes combinaciones - .../.../y2009307367.ext - .../.../Y2009307367.ext - .../.../x2009307/y2009307367.ext - .../.../x2009307/Y2009307367.ext - .../.../X2009307/y2009307367.ext - .../.../X2009307/Y2009307367.ext - siendo para este caso, la ultima combinacion de letras, identica al file buscado - - Return: - Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file - caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas - para el filename - """ - fullfilename = None - find_flag = False - filename = None - - prefixDirList = [None,'d','D'] - if ext.lower() == ".r": #voltage - prefixFileList = ['d','D'] - elif ext.lower() == ".pdata": #spectra - prefixFileList = ['p','P'] - else: - return None, filename - - #barrido por las combinaciones posibles - for prefixDir in prefixDirList: - thispath = path - if prefixDir != None: - #formo el nombre del directorio xYYYYDDD (x=d o x=D) - if foldercounter == 0: - thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy )) - else: - thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter)) - for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D" - filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext - fullfilename = os.path.join( thispath, filename ) #formo el path completo - - if os.path.exists( fullfilename ): #verifico que exista - find_flag = True - break - if find_flag: - break - - if not(find_flag): - return None, filename - - return fullfilename, filename - -def isDoyFolder(folder): - try: - year = int(folder[1:5]) - except: - return 0 - - try: - doy = int(folder[5:8]) - except: - return 0 - - return 1 - -class JRODataIO: - - c = 3E8 - - isConfig = False - - basicHeaderObj = BasicHeader(LOCALTIME) - - systemHeaderObj = SystemHeader() - - radarControllerHeaderObj = RadarControllerHeader() - - processingHeaderObj = ProcessingHeader() - - online = 0 - - dtype = None - - pathList = [] - - filenameList = [] - - filename = None - - ext = None - - flagIsNewFile = 1 - - flagTimeBlock = 0 - - flagIsNewBlock = 0 - - fp = None - - firstHeaderSize = 0 - - basicHeaderSize = 24 - - versionFile = 1103 - - fileSize = None - - ippSeconds = None - - fileSizeByHeader = None - - fileIndex = None - - profileIndex = None - - blockIndex = None - - nTotalBlocks = None - - maxTimeStep = 30 - - lastUTTime = None - - datablock = None - - dataOut = None - - blocksize = None - - def __init__(self): - - raise ValueError, "Not implemented" - - def run(self): - - raise ValueError, "Not implemented" - - def getOutput(self): - - return self.dataOut - -class JRODataReader(JRODataIO, ProcessingUnit): - - nReadBlocks = 0 - - delay = 10 #number of seconds waiting a new file - - nTries = 3 #quantity tries - - nFiles = 3 #number of files for searching - - path = None - - foldercounter = 0 - - flagNoMoreFiles = 0 - - datetimeList = [] - - __isFirstTimeOnline = 1 - - __printInfo = True - - profileIndex = None - - def __init__(self): - - """ - - """ - - raise ValueError, "This method has not been implemented" - - - def createObjByDefault(self): - """ - - """ - raise ValueError, "This method has not been implemented" - - def getBlockDimension(self): - - raise ValueError, "No implemented" - - def __searchFilesOffLine(self, - path, - startDate, - endDate, - startTime=datetime.time(0,0,0), - endTime=datetime.time(23,59,59), - set=None, - expLabel='', - ext='.r', - walk=True): - - pathList = [] - - if not walk: - #pathList.append(path) - multi_path = path.split(',') - for single_path in multi_path: - pathList.append(single_path) - - else: - #dirList = [] - multi_path = path.split(',') - for single_path in multi_path: - dirList = [] - for thisPath in os.listdir(single_path): - if not os.path.isdir(os.path.join(single_path,thisPath)): - continue - if not isDoyFolder(thisPath): - continue - - dirList.append(thisPath) - - if not(dirList): - return None, None - - thisDate = startDate - - while(thisDate <= endDate): - year = thisDate.timetuple().tm_year - doy = thisDate.timetuple().tm_yday - - matchlist = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy) + '*') - if len(matchlist) == 0: - thisDate += datetime.timedelta(1) - continue - for match in matchlist: - pathList.append(os.path.join(single_path,match,expLabel)) - - thisDate += datetime.timedelta(1) - - if pathList == []: - print "Any folder was found for the date range: %s-%s" %(startDate, endDate) - return None, None - - print "%d folder(s) was(were) found for the date range: %s - %s" %(len(pathList), startDate, endDate) - - filenameList = [] - datetimeList = [] - pathDict = {} - filenameList_to_sort = [] - - for i in range(len(pathList)): - - thisPath = pathList[i] - - fileList = glob.glob1(thisPath, "*%s" %ext) - fileList.sort() - pathDict.setdefault(fileList[0]) - pathDict[fileList[0]] = i - filenameList_to_sort.append(fileList[0]) - - filenameList_to_sort.sort() - - for file in filenameList_to_sort: - thisPath = pathList[pathDict[file]] - - fileList = glob.glob1(thisPath, "*%s" %ext) - fileList.sort() - - for file in fileList: - - filename = os.path.join(thisPath,file) - thisDatetime = isFileinThisTime(filename, startTime, endTime) - - if not(thisDatetime): - continue - - filenameList.append(filename) - datetimeList.append(thisDatetime) - - if not(filenameList): - print "Any file was found for the time range %s - %s" %(startTime, endTime) - return None, None - - print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime) - print - - for i in range(len(filenameList)): - print "%s -> [%s]" %(filenameList[i], datetimeList[i].ctime()) - - self.filenameList = filenameList - self.datetimeList = datetimeList - - return pathList, filenameList - - def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None): - - """ - Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y - devuelve el archivo encontrado ademas de otros datos. - - Input: - path : carpeta donde estan contenidos los files que contiene data - - expLabel : Nombre del subexperimento (subfolder) - - ext : extension de los files - - walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath) - - Return: - directory : eL directorio donde esta el file encontrado - filename : el ultimo file de una determinada carpeta - year : el anho - doy : el numero de dia del anho - set : el set del archivo - - - """ - dirList = [] - - if not walk: - fullpath = path - foldercounter = 0 - else: - #Filtra solo los directorios - for thisPath in os.listdir(path): - if not os.path.isdir(os.path.join(path,thisPath)): - continue - if not isDoyFolder(thisPath): - continue - - dirList.append(thisPath) - - if not(dirList): - return None, None, None, None, None, None - - dirList = sorted( dirList, key=str.lower ) - - doypath = dirList[-1] - foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0 - fullpath = os.path.join(path, doypath, expLabel) - - - print "%s folder was found: " %(fullpath ) - - if set == None: - filename = getlastFileFromPath(fullpath, ext) - else: - filename = getFileFromSet(fullpath, ext, set) - - if not(filename): - return None, None, None, None, None, None - - print "%s file was found" %(filename) - - if not(self.__verifyFile(os.path.join(fullpath, filename))): - return None, None, None, None, None, None - - year = int( filename[1:5] ) - doy = int( filename[5:8] ) - set = int( filename[8:11] ) - - return fullpath, foldercounter, filename, year, doy, set - - def __setNextFileOffline(self): - - idFile = self.fileIndex - - while (True): - idFile += 1 - if not(idFile < len(self.filenameList)): - self.flagNoMoreFiles = 1 - print "No more Files" - return 0 - - filename = self.filenameList[idFile] - - if not(self.__verifyFile(filename)): - continue - - fileSize = os.path.getsize(filename) - fp = open(filename,'rb') - break - - self.flagIsNewFile = 1 - self.fileIndex = idFile - self.filename = filename - self.fileSize = fileSize - self.fp = fp - - print "Setting the file: %s"%self.filename - - return 1 - - def __setNextFileOnline(self): - """ - Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si - no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files - siguientes. - - Affected: - self.flagIsNewFile - self.filename - self.fileSize - self.fp - self.set - self.flagNoMoreFiles - - Return: - 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado - 1 : si el file fue abierto con exito y esta listo a ser leido - - Excepciones: - Si un determinado file no puede ser abierto - """ - nFiles = 0 - fileOk_flag = False - firstTime_flag = True - - self.set += 1 - - if self.set > 999: - self.set = 0 - self.foldercounter += 1 - - #busca el 1er file disponible - fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext ) - if fullfilename: - if self.__verifyFile(fullfilename, False): - fileOk_flag = True - - #si no encuentra un file entonces espera y vuelve a buscar - if not(fileOk_flag): - for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles - - if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces - tries = self.nTries - else: - tries = 1 #si no es la 1era vez entonces solo lo hace una vez - - for nTries in range( tries ): - if firstTime_flag: - print "\tWaiting %0.2f sec for the file \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 ) - time.sleep( self.delay ) - else: - print "\tSearching next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext) - - fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext ) - if fullfilename: - if self.__verifyFile(fullfilename): - fileOk_flag = True - break - - if fileOk_flag: - break - - firstTime_flag = False - - print "\tSkipping the file \"%s\" due to this file doesn't exist" % filename - self.set += 1 - - if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta - self.set = 0 - self.doy += 1 - self.foldercounter = 0 - - if fileOk_flag: - self.fileSize = os.path.getsize( fullfilename ) - self.filename = fullfilename - self.flagIsNewFile = 1 - if self.fp != None: self.fp.close() - self.fp = open(fullfilename, 'rb') - self.flagNoMoreFiles = 0 - print 'Setting the file: %s' % fullfilename - else: - self.fileSize = 0 - self.filename = None - self.flagIsNewFile = 0 - self.fp = None - self.flagNoMoreFiles = 1 - print 'No more Files' - - return fileOk_flag - - - def setNextFile(self): - if self.fp != None: - self.fp.close() - - if self.online: - newFile = self.__setNextFileOnline() - else: - newFile = self.__setNextFileOffline() - - if not(newFile): - return 0 - - self.__readFirstHeader() - self.nReadBlocks = 0 - return 1 - - def __waitNewBlock(self): - """ - Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma. - - Si el modo de lectura es OffLine siempre retorn 0 - """ - if not self.online: - return 0 - - if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile): - return 0 - - currentPointer = self.fp.tell() - - neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize - - for nTries in range( self.nTries ): - - self.fp.close() - self.fp = open( self.filename, 'rb' ) - self.fp.seek( currentPointer ) - - self.fileSize = os.path.getsize( self.filename ) - currentSize = self.fileSize - currentPointer - - if ( currentSize >= neededSize ): - self.__rdBasicHeader() - return 1 - - if self.fileSize == self.fileSizeByHeader: -# self.flagEoF = True - return 0 - - print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1) - time.sleep( self.delay ) - - - return 0 - - def waitDataBlock(self,pointer_location): - - currentPointer = pointer_location - - neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize - - for nTries in range( self.nTries ): - self.fp.close() - self.fp = open( self.filename, 'rb' ) - self.fp.seek( currentPointer ) - - self.fileSize = os.path.getsize( self.filename ) - currentSize = self.fileSize - currentPointer - - if ( currentSize >= neededSize ): - return 1 - - print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1) - time.sleep( self.delay ) - - return 0 - - - def __jumpToLastBlock(self): - - if not(self.__isFirstTimeOnline): - return - - csize = self.fileSize - self.fp.tell() - blocksize = self.processingHeaderObj.blockSize - - #salta el primer bloque de datos - if csize > self.processingHeaderObj.blockSize: - self.fp.seek(self.fp.tell() + blocksize) - else: - return - - csize = self.fileSize - self.fp.tell() - neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize - while True: - - if self.fp.tell() 0: -# self.fp.seek(self.fp.tell() + factor*neededsize) - - self.flagIsNewFile = 0 - self.__isFirstTimeOnline = 0 - - - def __setNewBlock(self): - - if self.fp == None: - return 0 - - if self.online: - self.__jumpToLastBlock() - - if self.flagIsNewFile: - return 1 - - self.lastUTTime = self.basicHeaderObj.utc - currentSize = self.fileSize - self.fp.tell() - neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize - - if (currentSize >= neededSize): - self.__rdBasicHeader() - return 1 - - if self.__waitNewBlock(): - return 1 - - if not(self.setNextFile()): - return 0 - - deltaTime = self.basicHeaderObj.utc - self.lastUTTime # - - self.flagTimeBlock = 0 - - if deltaTime > self.maxTimeStep: - self.flagTimeBlock = 1 - - return 1 - - - def readNextBlock(self): - if not(self.__setNewBlock()): - return 0 - - if not(self.readBlock()): - return 0 - - return 1 - - def __rdProcessingHeader(self, fp=None): - if fp == None: - fp = self.fp - - self.processingHeaderObj.read(fp) - - def __rdRadarControllerHeader(self, fp=None): - if fp == None: - fp = self.fp - - self.radarControllerHeaderObj.read(fp) - - def __rdSystemHeader(self, fp=None): - if fp == None: - fp = self.fp - - self.systemHeaderObj.read(fp) - - def __rdBasicHeader(self, fp=None): - if fp == None: - fp = self.fp - - self.basicHeaderObj.read(fp) - - - def __readFirstHeader(self): - self.__rdBasicHeader() - self.__rdSystemHeader() - self.__rdRadarControllerHeader() - self.__rdProcessingHeader() - - self.firstHeaderSize = self.basicHeaderObj.size - - datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR)) - if datatype == 0: - datatype_str = numpy.dtype([('real',' %s" %(self.basicHeaderObj.dataBlock, self.nTotalBlocks, self.dataOut.datatime.ctime()) - self.dataOut.blocknow = self.basicHeaderObj.dataBlock - def printInfo(self): - - if self.__printInfo == False: - return - - self.basicHeaderObj.printInfo() - self.systemHeaderObj.printInfo() - self.radarControllerHeaderObj.printInfo() - self.processingHeaderObj.printInfo() - - self.__printInfo = False - - - def run(self, **kwargs): - - if not(self.isConfig): - -# self.dataOut = dataOut - self.setup(**kwargs) - self.isConfig = True - - self.getData() - -class JRODataWriter(JRODataIO, Operation): - - """ - Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura - de los datos siempre se realiza por bloques. - """ - - blockIndex = 0 - - path = None - - setFile = None - - profilesPerBlock = None - - blocksPerFile = None - - nWriteBlocks = 0 - - def __init__(self, dataOut=None): - raise ValueError, "Not implemented" - - - def hasAllDataInBuffer(self): - raise ValueError, "Not implemented" - - - def setBlockDimension(self): - raise ValueError, "Not implemented" - - - def writeBlock(self): - raise ValueError, "No implemented" - - - def putData(self): - raise ValueError, "No implemented" - - - def setBasicHeader(self): - - self.basicHeaderObj.size = self.basicHeaderSize #bytes - self.basicHeaderObj.version = self.versionFile - self.basicHeaderObj.dataBlock = self.nTotalBlocks - - utc = numpy.floor(self.dataOut.utctime) - milisecond = (self.dataOut.utctime - utc)* 1000.0 - - self.basicHeaderObj.utc = utc - self.basicHeaderObj.miliSecond = milisecond - self.basicHeaderObj.timeZone = self.dataOut.timeZone - self.basicHeaderObj.dstFlag = self.dataOut.dstFlag - self.basicHeaderObj.errorCount = self.dataOut.errorCount - - def setFirstHeader(self): - """ - Obtiene una copia del First Header - - Affected: - - self.basicHeaderObj - self.systemHeaderObj - self.radarControllerHeaderObj - self.processingHeaderObj self. - - Return: - None - """ - - raise ValueError, "No implemented" - - def __writeFirstHeader(self): - """ - Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader) - - Affected: - __dataType - - Return: - None - """ - -# CALCULAR PARAMETROS - - sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size - self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader - - self.basicHeaderObj.write(self.fp) - self.systemHeaderObj.write(self.fp) - self.radarControllerHeaderObj.write(self.fp) - self.processingHeaderObj.write(self.fp) - - self.dtype = self.dataOut.dtype - - def __setNewBlock(self): - """ - Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header - - Return: - 0 : si no pudo escribir nada - 1 : Si escribio el Basic el First Header - """ - if self.fp == None: - self.setNextFile() - - if self.flagIsNewFile: - return 1 - - if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile: - self.basicHeaderObj.write(self.fp) - return 1 - - if not( self.setNextFile() ): - return 0 - - return 1 - - - def writeNextBlock(self): - """ - Selecciona el bloque siguiente de datos y los escribe en un file - - Return: - 0 : Si no hizo pudo escribir el bloque de datos - 1 : Si no pudo escribir el bloque de datos - """ - if not( self.__setNewBlock() ): - return 0 - - self.writeBlock() - - return 1 - - def setNextFile(self): - """ - Determina el siguiente file que sera escrito - - Affected: - self.filename - self.subfolder - self.fp - self.setFile - self.flagIsNewFile - - Return: - 0 : Si el archivo no puede ser escrito - 1 : Si el archivo esta listo para ser escrito - """ - ext = self.ext - path = self.path - - if self.fp != None: - self.fp.close() - - timeTuple = time.localtime( self.dataOut.utctime) - subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) - - fullpath = os.path.join( path, subfolder ) - if not( os.path.exists(fullpath) ): - os.mkdir(fullpath) - self.setFile = -1 #inicializo mi contador de seteo - else: - filesList = os.listdir( fullpath ) - if len( filesList ) > 0: - filesList = sorted( filesList, key=str.lower ) - filen = filesList[-1] - # el filename debera tener el siguiente formato - # 0 1234 567 89A BCDE (hex) - # x YYYY DDD SSS .ext - if isNumber( filen[8:11] ): - self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file - else: - self.setFile = -1 - else: - self.setFile = -1 #inicializo mi contador de seteo - - setFile = self.setFile - setFile += 1 - - file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, - timeTuple.tm_year, - timeTuple.tm_yday, - setFile, - ext ) - - filename = os.path.join( path, subfolder, file ) - - fp = open( filename,'wb' ) - - self.blockIndex = 0 - - #guardando atributos - self.filename = filename - self.subfolder = subfolder - self.fp = fp - self.setFile = setFile - self.flagIsNewFile = 1 - - self.setFirstHeader() - - print 'Writing the file: %s'%self.filename - - self.__writeFirstHeader() - - return 1 - - def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=0, ext=None): - """ - Setea el tipo de formato en la cual sera guardada la data y escribe el First Header - - Inputs: - path : el path destino en el cual se escribiran los files a crear - format : formato en el cual sera salvado un file - set : el setebo del file - - Return: - 0 : Si no realizo un buen seteo - 1 : Si realizo un buen seteo - """ - - if ext == None: - ext = self.ext - - ext = ext.lower() - - self.ext = ext - - self.path = path - - self.setFile = set - 1 - - self.blocksPerFile = blocksPerFile - - self.profilesPerBlock = profilesPerBlock - - self.dataOut = dataOut - - if not(self.setNextFile()): - print "There isn't a next file" - return 0 - - self.setBlockDimension() - - return 1 - - def run(self, dataOut, **kwargs): - - if not(self.isConfig): - - self.setup(dataOut, **kwargs) - self.isConfig = True - - self.putData() - -class VoltageReader(JRODataReader): - """ - Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura - de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones: - perfiles*alturas*canales) son almacenados en la variable "buffer". - - perfiles * alturas * canales - - Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader, - RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la - cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de - datos desde el "buffer" cada vez que se ejecute el metodo "getData". - - Example: - - dpath = "/home/myuser/data" - - startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0) - - endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0) - - readerObj = VoltageReader() - - readerObj.setup(dpath, startTime, endTime) - - while(True): - - #to get one profile - profile = readerObj.getData() - - #print the profile - print profile - - #If you want to see all datablock - print readerObj.datablock - - if readerObj.flagNoMoreFiles: - break - - """ - - ext = ".r" - - optchar = "D" - dataOut = None - - - def __init__(self): - """ - Inicializador de la clase VoltageReader para la lectura de datos de voltage. - - Input: - dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para - almacenar un perfil de datos cada vez que se haga un requerimiento - (getData). El perfil sera obtenido a partir del buffer de datos, - si el buffer esta vacio se hara un nuevo proceso de lectura de un - bloque de datos. - Si este parametro no es pasado se creara uno internamente. - - Variables afectadas: - self.dataOut - - Return: - None - """ - - self.isConfig = False - - self.datablock = None - - self.utc = 0 - - self.ext = ".r" - - self.optchar = "D" - - self.basicHeaderObj = BasicHeader(LOCALTIME) - - self.systemHeaderObj = SystemHeader() - - self.radarControllerHeaderObj = RadarControllerHeader() - - self.processingHeaderObj = ProcessingHeader() - - self.online = 0 - - self.fp = None - - self.idFile = None - - self.dtype = None - - self.fileSizeByHeader = None - - self.filenameList = [] - - self.filename = None - - self.fileSize = None - - self.firstHeaderSize = 0 - - self.basicHeaderSize = 24 - - self.pathList = [] - - self.filenameList = [] - - self.lastUTTime = 0 - - self.maxTimeStep = 30 - - self.flagNoMoreFiles = 0 - - self.set = 0 - - self.path = None - - self.profileIndex = 2**32-1 - - self.delay = 3 #seconds - - self.nTries = 3 #quantity tries - - self.nFiles = 3 #number of files for searching - - self.nReadBlocks = 0 - - self.flagIsNewFile = 1 - - self.__isFirstTimeOnline = 1 - - self.ippSeconds = 0 - - self.flagTimeBlock = 0 - - self.flagIsNewBlock = 0 - - self.nTotalBlocks = 0 - - self.blocksize = 0 - - self.dataOut = self.createObjByDefault() - - def createObjByDefault(self): - - dataObj = Voltage() - - return dataObj - - def __hasNotDataInBuffer(self): - if self.profileIndex >= self.processingHeaderObj.profilesPerBlock: - return 1 - return 0 - - - def getBlockDimension(self): - """ - Obtiene la cantidad de puntos a leer por cada bloque de datos - - Affected: - self.blocksize - - Return: - None - """ - pts2read = self.processingHeaderObj.profilesPerBlock * self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels - self.blocksize = pts2read - - - def readBlock(self): - """ - readBlock lee el bloque de datos desde la posicion actual del puntero del archivo - (self.fp) y actualiza todos los parametros relacionados al bloque de datos - (metadata + data). La data leida es almacenada en el buffer y el contador del buffer - es seteado a 0 - - Inputs: - None - - Return: - None - - Affected: - self.profileIndex - self.datablock - self.flagIsNewFile - self.flagIsNewBlock - self.nTotalBlocks - - Exceptions: - Si un bloque leido no es un bloque valido - """ - current_pointer_location = self.fp.tell() - junk = numpy.fromfile( self.fp, self.dtype, self.blocksize ) - - try: - junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) ) - except: - #print "The read block (%3d) has not enough data" %self.nReadBlocks - - if self.waitDataBlock(pointer_location=current_pointer_location): - junk = numpy.fromfile( self.fp, self.dtype, self.blocksize ) - junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) ) -# return 0 - - junk = numpy.transpose(junk, (2,0,1)) - self.datablock = junk['real'] + junk['imag']*1j - - self.profileIndex = 0 - - self.flagIsNewFile = 0 - self.flagIsNewBlock = 1 - - self.nTotalBlocks += 1 - self.nReadBlocks += 1 - - return 1 - - def getFirstHeader(self): - - self.dataOut.dtype = self.dtype - - self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock - - xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight - - self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight) - - self.dataOut.channelList = range(self.systemHeaderObj.nChannels) - - self.dataOut.ippSeconds = self.ippSeconds - - self.dataOut.timeInterval = self.ippSeconds * self.processingHeaderObj.nCohInt - - self.dataOut.nCohInt = self.processingHeaderObj.nCohInt - - self.dataOut.flagShiftFFT = False - - if self.radarControllerHeaderObj.code != None: - - self.dataOut.nCode = self.radarControllerHeaderObj.nCode - - self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud - - self.dataOut.code = self.radarControllerHeaderObj.code - - self.dataOut.systemHeaderObj = self.systemHeaderObj.copy() - - self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy() - - self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada - - self.dataOut.flagDeflipData = False #asumo q la data no esta sin flip - - self.dataOut.flagShiftFFT = False - - def getData(self): - """ - getData obtiene una unidad de datos del buffer de lectura y la copia a la clase "Voltage" - con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de - lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock" - - Ademas incrementa el contador del buffer en 1. - - Return: - data : retorna un perfil de voltages (alturas * canales) copiados desde el - buffer. Si no hay mas archivos a leer retorna None. - - Variables afectadas: - self.dataOut - self.profileIndex - - Affected: - self.dataOut - self.profileIndex - self.flagTimeBlock - self.flagIsNewBlock - """ - - if self.flagNoMoreFiles: - self.dataOut.flagNoData = True - print 'Process finished' - return 0 - - self.flagTimeBlock = 0 - self.flagIsNewBlock = 0 - - if self.__hasNotDataInBuffer(): - - if not( self.readNextBlock() ): - return 0 - - self.getFirstHeader() - - if self.datablock == None: - self.dataOut.flagNoData = True - return 0 - - self.dataOut.data = self.datablock[:,self.profileIndex,:] - - self.dataOut.flagNoData = False - - self.getBasicHeader() - - self.profileIndex += 1 - - self.dataOut.realtime = self.online - - return self.dataOut.data - - -class VoltageWriter(JRODataWriter): - """ - Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura - de los datos siempre se realiza por bloques. - """ - - ext = ".r" - - optchar = "D" - - shapeBuffer = None - - - def __init__(self): - """ - Inicializador de la clase VoltageWriter para la escritura de datos de espectros. - - Affected: - self.dataOut - - Return: None - """ - - self.nTotalBlocks = 0 - - self.profileIndex = 0 - - self.isConfig = False - - self.fp = None - - self.flagIsNewFile = 1 - - self.nTotalBlocks = 0 - - self.flagIsNewBlock = 0 - - self.setFile = None - - self.dtype = None - - self.path = None - - self.filename = None - - self.basicHeaderObj = BasicHeader(LOCALTIME) - - self.systemHeaderObj = SystemHeader() - - self.radarControllerHeaderObj = RadarControllerHeader() - - self.processingHeaderObj = ProcessingHeader() - - def hasAllDataInBuffer(self): - if self.profileIndex >= self.processingHeaderObj.profilesPerBlock: - return 1 - return 0 - - - def setBlockDimension(self): - """ - Obtiene las formas dimensionales del los subbloques de datos que componen un bloque - - Affected: - self.shape_spc_Buffer - self.shape_cspc_Buffer - self.shape_dc_Buffer - - Return: None - """ - self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock, - self.processingHeaderObj.nHeights, - self.systemHeaderObj.nChannels) - - self.datablock = numpy.zeros((self.systemHeaderObj.nChannels, - self.processingHeaderObj.profilesPerBlock, - self.processingHeaderObj.nHeights), - dtype=numpy.dtype('complex64')) - - - def writeBlock(self): - """ - Escribe el buffer en el file designado - - Affected: - self.profileIndex - self.flagIsNewFile - self.flagIsNewBlock - self.nTotalBlocks - self.blockIndex - - Return: None - """ - data = numpy.zeros( self.shapeBuffer, self.dtype ) - - junk = numpy.transpose(self.datablock, (1,2,0)) - - data['real'] = junk.real - data['imag'] = junk.imag - - data = data.reshape( (-1) ) - - data.tofile( self.fp ) - - self.datablock.fill(0) - - self.profileIndex = 0 - self.flagIsNewFile = 0 - self.flagIsNewBlock = 1 - - self.blockIndex += 1 - self.nTotalBlocks += 1 - - def putData(self): - """ - Setea un bloque de datos y luego los escribe en un file - - Affected: - self.flagIsNewBlock - self.profileIndex - - Return: - 0 : Si no hay data o no hay mas files que puedan escribirse - 1 : Si se escribio la data de un bloque en un file - """ - if self.dataOut.flagNoData: - return 0 - - self.flagIsNewBlock = 0 - - if self.dataOut.flagTimeBlock: - - self.datablock.fill(0) - self.profileIndex = 0 - self.setNextFile() - - if self.profileIndex == 0: - self.setBasicHeader() - - self.datablock[:,self.profileIndex,:] = self.dataOut.data - - self.profileIndex += 1 - - if self.hasAllDataInBuffer(): - #if self.flagIsNewFile: - self.writeNextBlock() -# self.setFirstHeader() - - return 1 - - def __getProcessFlags(self): - - processFlags = 0 - - dtype0 = numpy.dtype([('real',' 1: - processFlags += PROCFLAG.COHERENT_INTEGRATION - - return processFlags - - - def __getBlockSize(self): - ''' - Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage - ''' - - dtype0 = numpy.dtype([('real',' 1: - processFlags += PROCFLAG.INCOHERENT_INTEGRATION - - if self.dataOut.data_dc != None: - processFlags += PROCFLAG.SAVE_CHANNELS_DC - - return processFlags - - - def __getBlockSize(self): - ''' - Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra - ''' - - dtype0 = numpy.dtype([('real',' 0: - channelList = [] - for channel in range(self.dataOut.nChannels): - channelList.append(channel) - channelList.append(channel) - - pairsList = [] - if self.dataOut.nPairs > 0: - for pair in self.dataOut.pairsList: - pairsList.append(pair[0]) - pairsList.append(pair[1]) - - spectraComb = channelList + pairsList - spectraComb = numpy.array(spectraComb,dtype="u1") - self.processingHeaderObj.spectraComb = spectraComb - sizeOfSpcComb = len(spectraComb) - processingHeaderSize += sizeOfSpcComb - -# The processing header should not have information about code -# if self.dataOut.code != None: -# self.processingHeaderObj.code = self.dataOut.code -# self.processingHeaderObj.nCode = self.dataOut.nCode -# self.processingHeaderObj.nBaud = self.dataOut.nBaud -# nCodeSize = 4 # bytes -# nBaudSize = 4 # bytes -# codeSize = 4 # bytes -# sizeOfCode = int(nCodeSize + nBaudSize + codeSize * self.dataOut.nCode * self.dataOut.nBaud) -# processingHeaderSize += sizeOfCode - - if self.processingHeaderObj.nWindows != 0: - self.processingHeaderObj.firstHeight = self.dataOut.heightList[0] - self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] - self.processingHeaderObj.nHeights = self.dataOut.nHeights - self.processingHeaderObj.samplesWin = self.dataOut.nHeights - sizeOfFirstHeight = 4 - sizeOfdeltaHeight = 4 - sizeOfnHeights = 4 - sizeOfWindows = (sizeOfFirstHeight + sizeOfdeltaHeight + sizeOfnHeights)*self.processingHeaderObj.nWindows - processingHeaderSize += sizeOfWindows - - self.processingHeaderObj.size = processingHeaderSize - -class SpectraHeisWriter(Operation): -# set = None - setFile = None - idblock = None - doypath = None - subfolder = None - - def __init__(self): - self.wrObj = FITS() -# self.dataOut = dataOut - self.nTotalBlocks=0 -# self.set = None - self.setFile = None - self.idblock = 0 - self.wrpath = None - self.doypath = None - self.subfolder = None - self.isConfig = False - - def isNumber(str): - """ - Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. - - Excepciones: - Si un determinado string no puede ser convertido a numero - Input: - str, string al cual se le analiza para determinar si convertible a un numero o no - - Return: - True : si el string es uno numerico - False : no es un string numerico - """ - try: - float( str ) - return True - except: - return False - - def setup(self, dataOut, wrpath): - - if not(os.path.exists(wrpath)): - os.mkdir(wrpath) - - self.wrpath = wrpath -# self.setFile = 0 - self.dataOut = dataOut - - def putData(self): - name= time.localtime( self.dataOut.utctime) - ext=".fits" - - if self.doypath == None: - self.subfolder = 'F%4.4d%3.3d_%d' % (name.tm_year,name.tm_yday,time.mktime(datetime.datetime.now().timetuple())) - self.doypath = os.path.join( self.wrpath, self.subfolder ) - os.mkdir(self.doypath) - - if self.setFile == None: -# self.set = self.dataOut.set - self.setFile = 0 -# if self.set != self.dataOut.set: -## self.set = self.dataOut.set -# self.setFile = 0 - - #make the filename - file = 'D%4.4d%3.3d_%3.3d%s' % (name.tm_year,name.tm_yday,self.setFile,ext) - - filename = os.path.join(self.wrpath,self.subfolder, file) - - idblock = numpy.array([self.idblock],dtype="int64") - header=self.wrObj.cFImage(idblock=idblock, - year=time.gmtime(self.dataOut.utctime).tm_year, - month=time.gmtime(self.dataOut.utctime).tm_mon, - day=time.gmtime(self.dataOut.utctime).tm_mday, - hour=time.gmtime(self.dataOut.utctime).tm_hour, - minute=time.gmtime(self.dataOut.utctime).tm_min, - second=time.gmtime(self.dataOut.utctime).tm_sec) - - c=3E8 - deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] - freq=numpy.arange(-1*self.dataOut.nHeights/2.,self.dataOut.nHeights/2.)*(c/(2*deltaHeight*1000)) - - colList = [] - - colFreq=self.wrObj.setColF(name="freq", format=str(self.dataOut.nFFTPoints)+'E', array=freq) - - colList.append(colFreq) - - nchannel=self.dataOut.nChannels - - for i in range(nchannel): - col = self.wrObj.writeData(name="PCh"+str(i+1), - format=str(self.dataOut.nFFTPoints)+'E', - data=10*numpy.log10(self.dataOut.data_spc[i,:])) - - colList.append(col) - - data=self.wrObj.Ctable(colList=colList) - - self.wrObj.CFile(header,data) - - self.wrObj.wFile(filename) - - #update the setFile - self.setFile += 1 - self.idblock += 1 - - return 1 - - def run(self, dataOut, **kwargs): - - if not(self.isConfig): - - self.setup(dataOut, **kwargs) - self.isConfig = True - - self.putData() - - - -class ParameterConf: - ELEMENTNAME = 'Parameter' - def __init__(self): - self.name = '' - self.value = '' - - def readXml(self, parmElement): - self.name = parmElement.get('name') - self.value = parmElement.get('value') - - def getElementName(self): - return self.ELEMENTNAME - -class Metadata: - - def __init__(self, filename): - self.parmConfObjList = [] - self.readXml(filename) - - def readXml(self, filename): - self.projectElement = None - self.procUnitConfObjDict = {} - self.projectElement = ElementTree().parse(filename) - self.project = self.projectElement.tag - - parmElementList = self.projectElement.getiterator(ParameterConf().getElementName()) - - for parmElement in parmElementList: - parmConfObj = ParameterConf() - parmConfObj.readXml(parmElement) - self.parmConfObjList.append(parmConfObj) - -class FitsWriter(Operation): - - def __init__(self): - self.isConfig = False - self.dataBlocksPerFile = None - self.blockIndex = 0 - self.flagIsNewFile = 1 - self.fitsObj = None - self.optchar = 'P' - self.ext = '.fits' - self.setFile = 0 - - def setFitsHeader(self, dataOut, metadatafile): - - header_data = pyfits.PrimaryHDU() - - metadata4fits = Metadata(metadatafile) - for parameter in metadata4fits.parmConfObjList: - parm_name = parameter.name - parm_value = parameter.value - -# if parm_value == 'fromdatadatetime': -# value = time.strftime("%b %d %Y %H:%M:%S", dataOut.datatime.timetuple()) -# elif parm_value == 'fromdataheights': -# value = dataOut.nHeights -# elif parm_value == 'fromdatachannel': -# value = dataOut.nChannels -# elif parm_value == 'fromdatasamples': -# value = dataOut.nFFTPoints -# else: -# value = parm_value - - header_data.header[parm_name] = parm_value - - - header_data.header['DATETIME'] = time.strftime("%b %d %Y %H:%M:%S", dataOut.datatime.timetuple()) - header_data.header['CHANNELLIST'] = str(dataOut.channelList) - header_data.header['NCHANNELS'] = dataOut.nChannels - #header_data.header['HEIGHTS'] = dataOut.heightList - header_data.header['NHEIGHTS'] = dataOut.nHeights - - header_data.header['IPPSECONDS'] = dataOut.ippSeconds - header_data.header['NCOHINT'] = dataOut.nCohInt - header_data.header['NINCOHINT'] = dataOut.nIncohInt - header_data.header['TIMEZONE'] = dataOut.timeZone - header_data.header['NBLOCK'] = self.blockIndex - - header_data.writeto(self.filename) - - self.addExtension(dataOut.heightList,'HEIGHTLIST') - - - def setup(self, dataOut, path, dataBlocksPerFile, metadatafile): - - self.path = path - self.dataOut = dataOut - self.metadatafile = metadatafile - self.dataBlocksPerFile = dataBlocksPerFile - - def open(self): - self.fitsObj = pyfits.open(self.filename, mode='update') - - - def addExtension(self, data, tagname): - self.open() - extension = pyfits.ImageHDU(data=data, name=tagname) - #extension.header['TAG'] = tagname - self.fitsObj.append(extension) - self.write() - - def addData(self, data): - self.open() - extension = pyfits.ImageHDU(data=data, name=self.fitsObj[0].header['DATATYPE']) - extension.header['UTCTIME'] = self.dataOut.utctime - self.fitsObj.append(extension) - self.blockIndex += 1 - self.fitsObj[0].header['NBLOCK'] = self.blockIndex - - self.write() - - def write(self): - - self.fitsObj.flush(verbose=True) - self.fitsObj.close() - - - def setNextFile(self): - - ext = self.ext - path = self.path - - timeTuple = time.localtime( self.dataOut.utctime) - subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) - - fullpath = os.path.join( path, subfolder ) - if not( os.path.exists(fullpath) ): - os.mkdir(fullpath) - self.setFile = -1 #inicializo mi contador de seteo - else: - filesList = os.listdir( fullpath ) - if len( filesList ) > 0: - filesList = sorted( filesList, key=str.lower ) - filen = filesList[-1] - - if isNumber( filen[8:11] ): - self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file - else: - self.setFile = -1 - else: - self.setFile = -1 #inicializo mi contador de seteo - - setFile = self.setFile - setFile += 1 - - file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, - timeTuple.tm_year, - timeTuple.tm_yday, - setFile, - ext ) - - filename = os.path.join( path, subfolder, file ) - - self.blockIndex = 0 - self.filename = filename - self.setFile = setFile - self.flagIsNewFile = 1 - - print 'Writing the file: %s'%self.filename - - self.setFitsHeader(self.dataOut, self.metadatafile) - - return 1 - - def writeBlock(self): - self.addData(self.dataOut.data_spc) - self.flagIsNewFile = 0 - - - def __setNewBlock(self): - - if self.flagIsNewFile: - return 1 - - if self.blockIndex < self.dataBlocksPerFile: - return 1 - - if not( self.setNextFile() ): - return 0 - - return 1 - - def writeNextBlock(self): - if not( self.__setNewBlock() ): - return 0 - self.writeBlock() - return 1 - - def putData(self): - if self.flagIsNewFile: - self.setNextFile() - self.writeNextBlock() - - def run(self, dataOut, **kwargs): - if not(self.isConfig): - self.setup(dataOut, **kwargs) - self.isConfig = True - self.putData() - - -class FitsReader(ProcessingUnit): - -# __TIMEZONE = time.timezone - - expName = None - datetimestr = None - utc = None - nChannels = None - nSamples = None - dataBlocksPerFile = None - comments = None - lastUTTime = None - header_dict = None - data = None - data_header_dict = None - - def __init__(self): - self.isConfig = False - self.ext = '.fits' - self.setFile = 0 - self.flagNoMoreFiles = 0 - self.flagIsNewFile = 1 - self.flagTimeBlock = None - self.fileIndex = None - self.filename = None - self.fileSize = None - self.fitsObj = None - self.timeZone = None - self.nReadBlocks = 0 - self.nTotalBlocks = 0 - self.dataOut = self.createObjByDefault() - self.maxTimeStep = 10# deberia ser definido por el usuario usando el metodo setup() - self.blockIndex = 1 - - def createObjByDefault(self): - - dataObj = Fits() - - return dataObj - - def isFileinThisTime(self, filename, startTime, endTime, useLocalTime=False): - try: - fitsObj = pyfits.open(filename,'readonly') - except: - raise IOError, "The file %s can't be opened" %(filename) - - header = fitsObj[0].header - struct_time = time.strptime(header['DATETIME'], "%b %d %Y %H:%M:%S") - utc = time.mktime(struct_time) - time.timezone #TIMEZONE debe ser un parametro del header FITS - - ltc = utc - if useLocalTime: - ltc -= time.timezone - thisDatetime = datetime.datetime.utcfromtimestamp(ltc) - thisTime = thisDatetime.time() - - if not ((startTime <= thisTime) and (endTime > thisTime)): - return None - - return thisDatetime - - def __setNextFileOnline(self): - raise ValueError, "No implemented" - - def __setNextFileOffline(self): - idFile = self.fileIndex - - while (True): - idFile += 1 - if not(idFile < len(self.filenameList)): - self.flagNoMoreFiles = 1 - print "No more Files" - return 0 - - filename = self.filenameList[idFile] - -# if not(self.__verifyFile(filename)): -# continue - - fileSize = os.path.getsize(filename) - fitsObj = pyfits.open(filename,'readonly') - break - - self.flagIsNewFile = 1 - self.fileIndex = idFile - self.filename = filename - self.fileSize = fileSize - self.fitsObj = fitsObj - self.blockIndex = 0 - print "Setting the file: %s"%self.filename - - return 1 - - def readHeader(self): - headerObj = self.fitsObj[0] - - self.header_dict = headerObj.header - if 'EXPNAME' in headerObj.header.keys(): - self.expName = headerObj.header['EXPNAME'] - - if 'DATATYPE' in headerObj.header.keys(): - self.dataType = headerObj.header['DATATYPE'] - - self.datetimestr = headerObj.header['DATETIME'] - channelList = headerObj.header['CHANNELLIST'] - channelList = channelList.split('[') - channelList = channelList[1].split(']') - channelList = channelList[0].split(',') - channelList = [int(ch) for ch in channelList] - self.channelList = channelList - self.nChannels = headerObj.header['NCHANNELS'] - self.nHeights = headerObj.header['NHEIGHTS'] - self.ippSeconds = headerObj.header['IPPSECONDS'] - self.nCohInt = headerObj.header['NCOHINT'] - self.nIncohInt = headerObj.header['NINCOHINT'] - self.dataBlocksPerFile = headerObj.header['NBLOCK'] - self.timeZone = headerObj.header['TIMEZONE'] - - self.timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt - - if 'COMMENT' in headerObj.header.keys(): - self.comments = headerObj.header['COMMENT'] - - self.readHeightList() - - def readHeightList(self): - self.blockIndex = self.blockIndex + 1 - obj = self.fitsObj[self.blockIndex] - self.heightList = obj.data - self.blockIndex = self.blockIndex + 1 - - def readExtension(self): - obj = self.fitsObj[self.blockIndex] - self.heightList = obj.data - self.blockIndex = self.blockIndex + 1 - - def setNextFile(self): - - if self.online: - newFile = self.__setNextFileOnline() - else: - newFile = self.__setNextFileOffline() - - if not(newFile): - return 0 - - self.readHeader() - - self.nReadBlocks = 0 -# self.blockIndex = 1 - return 1 - - def __searchFilesOffLine(self, - path, - startDate, - endDate, - startTime=datetime.time(0,0,0), - endTime=datetime.time(23,59,59), - set=None, - expLabel='', - ext='.fits', - walk=True): - - pathList = [] - - if not walk: - pathList.append(path) - - else: - dirList = [] - for thisPath in os.listdir(path): - if not os.path.isdir(os.path.join(path,thisPath)): - continue - if not isDoyFolder(thisPath): - continue - - dirList.append(thisPath) - - if not(dirList): - return None, None - - thisDate = startDate - - while(thisDate <= endDate): - year = thisDate.timetuple().tm_year - doy = thisDate.timetuple().tm_yday - - matchlist = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy) + '*') - if len(matchlist) == 0: - thisDate += datetime.timedelta(1) - continue - for match in matchlist: - pathList.append(os.path.join(path,match,expLabel)) - - thisDate += datetime.timedelta(1) - - if pathList == []: - print "Any folder was found for the date range: %s-%s" %(startDate, endDate) - return None, None - - print "%d folder(s) was(were) found for the date range: %s - %s" %(len(pathList), startDate, endDate) - - filenameList = [] - datetimeList = [] - - for i in range(len(pathList)): - - thisPath = pathList[i] - - fileList = glob.glob1(thisPath, "*%s" %ext) - fileList.sort() - - for file in fileList: - - filename = os.path.join(thisPath,file) - thisDatetime = self.isFileinThisTime(filename, startTime, endTime) - - if not(thisDatetime): - continue - - filenameList.append(filename) - datetimeList.append(thisDatetime) - - if not(filenameList): - print "Any file was found for the time range %s - %s" %(startTime, endTime) - return None, None - - print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime) - print - - for i in range(len(filenameList)): - print "%s -> [%s]" %(filenameList[i], datetimeList[i].ctime()) - - self.filenameList = filenameList - self.datetimeList = datetimeList - - return pathList, filenameList - - def setup(self, path=None, - startDate=None, - endDate=None, - startTime=datetime.time(0,0,0), - endTime=datetime.time(23,59,59), - set=0, - expLabel = "", - ext = None, - online = False, - delay = 60, - walk = True): - - if path == None: - raise ValueError, "The path is not valid" - - if ext == None: - ext = self.ext - - if not(online): - print "Searching files in offline mode ..." - pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate, - startTime=startTime, endTime=endTime, - set=set, expLabel=expLabel, ext=ext, - walk=walk) - - if not(pathList): - print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path, - datetime.datetime.combine(startDate,startTime).ctime(), - datetime.datetime.combine(endDate,endTime).ctime()) - - sys.exit(-1) - - self.fileIndex = -1 - self.pathList = pathList - self.filenameList = filenameList - - self.online = online - self.delay = delay - ext = ext.lower() - self.ext = ext - - if not(self.setNextFile()): - if (startDate!=None) and (endDate!=None): - print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime()) - elif startDate != None: - print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime()) - else: - print "No files" - - sys.exit(-1) - - - - def readBlock(self): - dataObj = self.fitsObj[self.blockIndex] - - self.data = dataObj.data - self.data_header_dict = dataObj.header - self.utc = self.data_header_dict['UTCTIME'] - - self.flagIsNewFile = 0 - self.blockIndex += 1 - self.nTotalBlocks += 1 - self.nReadBlocks += 1 - - return 1 - - def __jumpToLastBlock(self): - raise ValueError, "No implemented" - - def __waitNewBlock(self): - """ - Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma. - - Si el modo de lectura es OffLine siempre retorn 0 - """ - if not self.online: - return 0 - - if (self.nReadBlocks >= self.dataBlocksPerFile): - return 0 - - currentPointer = self.fp.tell() - - neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize - - for nTries in range( self.nTries ): - - self.fp.close() - self.fp = open( self.filename, 'rb' ) - self.fp.seek( currentPointer ) - - self.fileSize = os.path.getsize( self.filename ) - currentSize = self.fileSize - currentPointer - - if ( currentSize >= neededSize ): - self.__rdBasicHeader() - return 1 - - print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1) - time.sleep( self.delay ) - - - return 0 - - def __setNewBlock(self): - - if self.online: - self.__jumpToLastBlock() - - if self.flagIsNewFile: - return 1 - - self.lastUTTime = self.utc - - if self.online: - if self.__waitNewBlock(): - return 1 - - if self.nReadBlocks < self.dataBlocksPerFile: - return 1 - - if not(self.setNextFile()): - return 0 - - deltaTime = self.utc - self.lastUTTime - - self.flagTimeBlock = 0 - - if deltaTime > self.maxTimeStep: - self.flagTimeBlock = 1 - - return 1 - - - def readNextBlock(self): - if not(self.__setNewBlock()): - return 0 - - if not(self.readBlock()): - return 0 - - return 1 - - - def getData(self): - - if self.flagNoMoreFiles: - self.dataOut.flagNoData = True - print 'Process finished' - return 0 - - self.flagTimeBlock = 0 - self.flagIsNewBlock = 0 - - if not(self.readNextBlock()): - return 0 - - if self.data == None: - self.dataOut.flagNoData = True - return 0 - - self.dataOut.data = self.data - self.dataOut.data_header = self.data_header_dict - self.dataOut.utctime = self.utc - - self.dataOut.header = self.header_dict - self.dataOut.expName = self.expName - self.dataOut.nChannels = self.nChannels - self.dataOut.timeZone = self.timeZone - self.dataOut.dataBlocksPerFile = self.dataBlocksPerFile - self.dataOut.comments = self.comments - self.dataOut.timeInterval = self.timeInterval - self.dataOut.channelList = self.channelList - self.dataOut.heightList = self.heightList - self.dataOut.flagNoData = False - - return self.dataOut.data - - def run(self, **kwargs): - - if not(self.isConfig): - self.setup(**kwargs) - self.isConfig = True - - self.getData() - - -class RadacHeader(): - def __init__(self, fp): - header = 'Raw11/Data/RadacHeader' - self.beamCodeByPulse = fp.get(header+'/BeamCode') - self.beamCode = fp.get('Raw11/Data/Beamcodes') - self.code = fp.get(header+'/Code') - self.frameCount = fp.get(header+'/FrameCount') - self.modeGroup = fp.get(header+'/ModeGroup') - self.nsamplesPulse = fp.get(header+'/NSamplesPulse') - self.pulseCount = fp.get(header+'/PulseCount') - self.radacTime = fp.get(header+'/RadacTime') - self.timeCount = fp.get(header+'/TimeCount') - self.timeStatus = fp.get(header+'/TimeStatus') - - self.nrecords = self.pulseCount.shape[0] #numero de bloques - self.npulses = self.pulseCount.shape[1] #numero de perfiles - self.nsamples = self.nsamplesPulse[0,0] #numero de alturas - self.nbeams = self.beamCode.shape[1] #numero de beams - - - def getIndexRangeToPulse(self, idrecord=0): - indexToZero = numpy.where(self.pulseCount.value[idrecord,:]==0) - startPulseCountId = indexToZero[0][0] - endPulseCountId = startPulseCountId - 1 - range1 = numpy.arange(startPulseCountId,self.npulses,1) - range2 = numpy.arange(0,startPulseCountId,1) - return range1, range2 - - -class AMISRReader(ProcessingUnit): - - path = None - startDate = None - endDate = None - startTime = None - endTime = None - walk = None - isConfig = False - - def __init__(self): - self.set = None - self.subset = None - self.extension_file = '.h5' - self.dtc_str = 'dtc' - self.dtc_id = 0 - self.status = True - self.isConfig = False - self.dirnameList = [] - self.filenameList = [] - self.fileIndex = None - self.flagNoMoreFiles = False - self.flagIsNewFile = 0 - self.filename = '' - self.amisrFilePointer = None - self.radacHeaderObj = None - self.dataOut = self.__createObjByDefault() - self.datablock = None - self.rest_datablock = None - self.range = None - self.idrecord_count = 0 - self.profileIndex = 0 - self.idpulse_range1 = None - self.idpulse_range2 = None - self.beamCodeByFrame = None - self.radacTimeByFrame = None - #atributos originales tal y como esta en el archivo de datos - self.beamCodesFromFile = None - self.radacTimeFromFile = None - self.rangeFromFile = None - self.dataByFrame = None - self.dataset = None - - self.beamCodeDict = {} - self.beamRangeDict = {} - - #experiment cgf file - self.npulsesint_fromfile = None - self.recordsperfile_fromfile = None - self.nbeamcodes_fromfile = None - self.ngates_fromfile = None - self.ippSeconds_fromfile = None - self.frequency_h5file = None - - - self.__firstFile = True - self.buffer_radactime = None - - def __createObjByDefault(self): - - dataObj = AMISR() - - return dataObj - - def __setParameters(self,path,startDate,endDate,startTime,endTime,walk): - self.path = path - self.startDate = startDate - self.endDate = endDate - self.startTime = startTime - self.endTime = endTime - self.walk = walk - - def __checkPath(self): - if os.path.exists(self.path): - self.status = 1 - else: - self.status = 0 - print 'Path:%s does not exists'%self.path - - return - - def __selDates(self, amisr_dirname_format): - try: - year = int(amisr_dirname_format[0:4]) - month = int(amisr_dirname_format[4:6]) - dom = int(amisr_dirname_format[6:8]) - thisDate = datetime.date(year,month,dom) - - if (thisDate>=self.startDate and thisDate <= self.endDate): - return amisr_dirname_format - except: - return None - - def __findDataForDates(self): - - - - if not(self.status): - return None - - pat = '\d+.\d+' - dirnameList = [re.search(pat,x) for x in os.listdir(self.path)] - dirnameList = filter(lambda x:x!=None,dirnameList) - dirnameList = [x.string for x in dirnameList] - dirnameList = [self.__selDates(x) for x in dirnameList] - dirnameList = filter(lambda x:x!=None,dirnameList) - if len(dirnameList)>0: - self.status = 1 - self.dirnameList = dirnameList - self.dirnameList.sort() - else: - self.status = 0 - return None - - def __getTimeFromData(self): - pass - - def __filterByGlob1(self, dirName): - filter_files = glob.glob1(dirName, '*.*%s'%self.extension_file) - filterDict = {} - filterDict.setdefault(dirName) - filterDict[dirName] = filter_files - return filterDict - - def __getFilenameList(self, fileListInKeys, dirList): - for value in fileListInKeys: - dirName = value.keys()[0] - for file in value[dirName]: - filename = os.path.join(dirName, file) - self.filenameList.append(filename) - - - def __selectDataForTimes(self): - #aun no esta implementado el filtro for tiempo - if not(self.status): - return None - - dirList = [os.path.join(self.path,x) for x in self.dirnameList] - - fileListInKeys = [self.__filterByGlob1(x) for x in dirList] - - self.__getFilenameList(fileListInKeys, dirList) - - if len(self.filenameList)>0: - self.status = 1 - self.filenameList.sort() - else: - self.status = 0 - return None - - - def __searchFilesOffline(self, - path, - startDate, - endDate, - startTime=datetime.time(0,0,0), - endTime=datetime.time(23,59,59), - walk=True): - - self.__setParameters(path, startDate, endDate, startTime, endTime, walk) - - self.__checkPath() - - self.__findDataForDates() - - self.__selectDataForTimes() - - for i in range(len(self.filenameList)): - print "%s" %(self.filenameList[i]) - - return - - def __setNextFileOffline(self): - idFile = self.fileIndex - - while (True): - idFile += 1 - if not(idFile < len(self.filenameList)): - self.flagNoMoreFiles = 1 - print "No more Files" - return 0 - - filename = self.filenameList[idFile] - - amisrFilePointer = h5py.File(filename,'r') - - break - - self.flagIsNewFile = 1 - self.fileIndex = idFile - self.filename = filename - - self.amisrFilePointer = amisrFilePointer - - print "Setting the file: %s"%self.filename - - return 1 - - def __readHeader(self): - self.radacHeaderObj = RadacHeader(self.amisrFilePointer) - - #update values from experiment cfg file - if self.radacHeaderObj.nrecords == self.recordsperfile_fromfile: - self.radacHeaderObj.nrecords = self.recordsperfile_fromfile - self.radacHeaderObj.nbeams = self.nbeamcodes_fromfile - self.radacHeaderObj.npulses = self.npulsesint_fromfile - self.radacHeaderObj.nsamples = self.ngates_fromfile - - #get tuning frequency - frequency_h5file_dataset = self.amisrFilePointer.get('Rx'+'/TuningFrequency') - self.frequency_h5file = frequency_h5file_dataset[0,0] - - self.flagIsNewFile = 1 - - def __getBeamCode(self): - self.beamCodeDict = {} - self.beamRangeDict = {} - - for i in range(len(self.radacHeaderObj.beamCode[0,:])): - self.beamCodeDict.setdefault(i) - self.beamRangeDict.setdefault(i) - self.beamCodeDict[i] = self.radacHeaderObj.beamCode[0,i] - - - just4record0 = self.radacHeaderObj.beamCodeByPulse[0,:] - - for i in range(len(self.beamCodeDict.values())): - xx = numpy.where(just4record0==self.beamCodeDict.values()[i]) - self.beamRangeDict[i] = xx[0] - - def __getExpParameters(self): - if not(self.status): - return None - - experimentCfgPath = os.path.join(self.path, self.dirnameList[0], 'Setup') - - expFinder = glob.glob1(experimentCfgPath,'*.exp') - if len(expFinder)== 0: - self.status = 0 - return None - - experimentFilename = os.path.join(experimentCfgPath,expFinder[0]) - - f = open(experimentFilename) - lines = f.readlines() - f.close() - - parmsList = ['npulsesint*','recordsperfile*','nbeamcodes*','ngates*'] - filterList = [fnmatch.filter(lines, x) for x in parmsList] - - - values = [re.sub(r'\D',"",x[0]) for x in filterList] - - self.npulsesint_fromfile = int(values[0]) - self.recordsperfile_fromfile = int(values[1]) - self.nbeamcodes_fromfile = int(values[2]) - self.ngates_fromfile = int(values[3]) - - tufileFinder = fnmatch.filter(lines, 'tufile=*') - tufile = tufileFinder[0].split('=')[1].split('\n')[0] - tufilename = os.path.join(experimentCfgPath,tufile) - - f = open(tufilename) - lines = f.readlines() - f.close() - self.ippSeconds_fromfile = float(lines[1].split()[2])/1E6 - - - self.status = 1 - - def __setIdsAndArrays(self): - self.dataByFrame = self.__setDataByFrame() - self.beamCodeByFrame = self.amisrFilePointer.get('Raw11/Data/RadacHeader/BeamCode').value[0, :] - self.readRanges() - self.idpulse_range1, self.idpulse_range2 = self.radacHeaderObj.getIndexRangeToPulse(0) - self.radacTimeByFrame = numpy.zeros(self.radacHeaderObj.npulses) - self.buffer_radactime = numpy.zeros_like(self.radacTimeByFrame) - - - def __setNextFile(self): - - newFile = self.__setNextFileOffline() - - if not(newFile): - return 0 - - self.__readHeader() - - if self.__firstFile: - self.__setIdsAndArrays() - self.__firstFile = False - - self.__getBeamCode() - self.readDataBlock() - - - def setup(self,path=None, - startDate=None, - endDate=None, - startTime=datetime.time(0,0,0), - endTime=datetime.time(23,59,59), - walk=True): - - #Busqueda de archivos offline - self.__searchFilesOffline(path, startDate, endDate, startTime, endTime, walk) - - if not(self.filenameList): - print "There is no files into the folder: %s"%(path) - - sys.exit(-1) - - self.__getExpParameters() - - self.fileIndex = -1 - - self.__setNextFile() - - def readRanges(self): - dataset = self.amisrFilePointer.get('Raw11/Data/Samples/Range') - #self.rangeFromFile = dataset.value - self.rangeFromFile = numpy.reshape(dataset.value,(-1)) - return range - - - def readRadacTime(self,idrecord, range1, range2): - self.radacTimeFromFile = self.radacHeaderObj.radacTime.value - - radacTimeByFrame = numpy.zeros((self.radacHeaderObj.npulses)) - #radacTimeByFrame = dataset[idrecord - 1,range1] - #radacTimeByFrame = dataset[idrecord,range2] - - return radacTimeByFrame - - def readBeamCode(self, idrecord, range1, range2): - dataset = self.amisrFilePointer.get('Raw11/Data/RadacHeader/BeamCode') - beamcodeByFrame = numpy.zeros((self.radacHeaderObj.npulses)) - self.beamCodesFromFile = dataset.value - - #beamcodeByFrame[range1] = dataset[idrecord - 1, range1] - #beamcodeByFrame[range2] = dataset[idrecord, range2] - beamcodeByFrame[range1] = dataset[idrecord, range1] - beamcodeByFrame[range2] = dataset[idrecord, range2] - - return beamcodeByFrame - - - def __setDataByFrame(self): - ndata = 2 # porque es complejo - dataByFrame = numpy.zeros((self.radacHeaderObj.npulses, self.radacHeaderObj.nsamples, ndata)) - return dataByFrame - - def __readDataSet(self): - dataset = self.amisrFilePointer.get('Raw11/Data/Samples/Data') - return dataset - - def __setDataBlock(self,): - real = self.dataByFrame[:,:,0] #asumo que 0 es real - imag = self.dataByFrame[:,:,1] #asumo que 1 es imaginario - datablock = real + imag*1j #armo el complejo - return datablock - - def readSamples_version1(self,idrecord): - #estas tres primeras lineas solo se deben ejecutar una vez - if self.flagIsNewFile: - #reading dataset - self.dataset = self.__readDataSet() - self.flagIsNewFile = 0 - - if idrecord == 0: - #if self.buffer_last_record == None: - selectorById = self.radacHeaderObj.pulseCount[0,self.idpulse_range2] - - self.dataByFrame[selectorById,:,:] = self.dataset[0, self.idpulse_range2,:,:] - - self.radacTimeByFrame[selectorById] = self.radacHeaderObj.radacTime[0, self.idpulse_range2] - - selectorById = self.radacHeaderObj.pulseCount[0,self.idpulse_range1] - - self.radacTimeByFrame[selectorById] = self.buffer_radactime[selectorById] - - datablock = self.__setDataBlock() - - return datablock - - selectorById = self.radacHeaderObj.pulseCount[idrecord-1,self.idpulse_range1] - self.dataByFrame[selectorById,:,:] = self.dataset[idrecord-1, self.idpulse_range1, :, :] - self.radacTimeByFrame[selectorById] = self.radacHeaderObj.radacTime[idrecord-1, self.idpulse_range1] - - selectorById = self.radacHeaderObj.pulseCount[idrecord,self.idpulse_range2]#data incompleta ultimo archivo de carpeta, verifica el record real segun la dimension del arreglo de datos - self.dataByFrame[selectorById,:,:] = self.dataset[idrecord, self.idpulse_range2, :, :] - self.radacTimeByFrame[selectorById] = self.radacHeaderObj.radacTime[idrecord, self.idpulse_range2] - - datablock = self.__setDataBlock() - - selectorById = self.radacHeaderObj.pulseCount[idrecord,self.idpulse_range1] - self.dataByFrame[selectorById,:,:] = self.dataset[idrecord, self.idpulse_range1, :, :] - self.buffer_radactime[selectorById] = self.radacHeaderObj.radacTime[idrecord, self.idpulse_range1] - - return datablock - - - def readSamples(self,idrecord): - if self.flagIsNewFile: - self.dataByFrame = self.__setDataByFrame() - self.beamCodeByFrame = self.amisrFilePointer.get('Raw11/Data/RadacHeader/BeamCode').value[idrecord, :] - - #reading ranges - self.readRanges() - #reading dataset - self.dataset = self.__readDataSet() - - self.flagIsNewFile = 0 - self.radacTimeByFrame = self.radacHeaderObj.radacTime.value[idrecord, :] - self.dataByFrame = self.dataset[idrecord, :, :, :] - datablock = self.__setDataBlock() - return datablock - - - def readDataBlock(self): - - self.datablock = self.readSamples_version1(self.idrecord_count) - #self.datablock = self.readSamples(self.idrecord_count) - #print 'record:', self.idrecord_count - - self.idrecord_count += 1 - self.profileIndex = 0 - - if self.idrecord_count >= self.radacHeaderObj.nrecords: - self.idrecord_count = 0 - self.flagIsNewFile = 1 - - def readNextBlock(self): - - self.readDataBlock() - - if self.flagIsNewFile: - self.__setNextFile() - pass - - def __hasNotDataInBuffer(self): - #self.radacHeaderObj.npulses debe ser otra variable para considerar el numero de pulsos a tomar en el primer y ultimo record - if self.profileIndex >= self.radacHeaderObj.npulses: - return 1 - return 0 - - def printUTC(self): - print self.dataOut.utctime - print '' - - def setObjProperties(self): - self.dataOut.heightList = self.rangeFromFile/1000.0 #km - self.dataOut.nProfiles = self.radacHeaderObj.npulses - self.dataOut.nRecords = self.radacHeaderObj.nrecords - self.dataOut.nBeams = self.radacHeaderObj.nbeams - self.dataOut.ippSeconds = self.ippSeconds_fromfile - self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt - self.dataOut.frequency = self.frequency_h5file - self.dataOut.nBaud = None - self.dataOut.nCode = None - self.dataOut.code = None - - self.dataOut.beamCodeDict = self.beamCodeDict - self.dataOut.beamRangeDict = self.beamRangeDict - - def getData(self): - - if self.flagNoMoreFiles: - self.dataOut.flagNoData = True - print 'Process finished' - return 0 - - if self.__hasNotDataInBuffer(): - self.readNextBlock() - - - if self.datablock == None: # setear esta condicion cuando no hayan datos por leers - self.dataOut.flagNoData = True - return 0 - - self.dataOut.data = numpy.reshape(self.datablock[self.profileIndex,:],(1,-1)) - - self.dataOut.utctime = self.radacTimeByFrame[self.profileIndex] - - self.dataOut.flagNoData = False - - self.profileIndex += 1 - - return self.dataOut.data - - - def run(self, **kwargs): - if not(self.isConfig): - self.setup(**kwargs) - self.setObjProperties() - self.isConfig = True - - self.getData() diff --git a/schainpy/model/jroheaderIO.py b/schainpy/model/jroheaderIO.py deleted file mode 100644 index e51de80..0000000 --- a/schainpy/model/jroheaderIO.py +++ /dev/null @@ -1,569 +0,0 @@ -''' - -$Author: murco $ -$Id: JROHeaderIO.py 151 2012-10-31 19:00:51Z murco $ -''' -import sys -import numpy -import copy -import datetime - -class Header: - - def __init__(self): - raise - - def copy(self): - return copy.deepcopy(self) - - def read(): - pass - - def write(): - pass - - def printInfo(self): - - print "#"*100 - print self.__class__.__name__.upper() - print "#"*100 - for key in self.__dict__.keys(): - print "%s = %s" %(key, self.__dict__[key]) - -class BasicHeader(Header): - - size = None - version = None - dataBlock = None - utc = None - ltc = None - miliSecond = None - timeZone = None - dstFlag = None - errorCount = None - struct = None - datatime = None - - __LOCALTIME = None - - def __init__(self, useLocalTime=True): - - self.size = 0 - self.version = 0 - self.dataBlock = 0 - self.utc = 0 - self.miliSecond = 0 - self.timeZone = 0 - self.dstFlag = 0 - self.errorCount = 0 - self.struct = numpy.dtype([ - ('nSize',' 0: - fp.seek(jumpFp) - - except Exception, e: - print "RadarControllerHeader: " + e - return 0 - - return 1 - - def write(self, fp): - headerTuple = (self.size, - self.expType, - self.nTx, - self.ipp, - self.txA, - self.txB, - self.nWindows, - self.numTaus, - self.codeType, - self.line6Function, - self.line5Function, - self.fClock, - self.prePulseBefore, - self.prePulserAfter, - self.rangeIpp, - self.rangeTxA, - self.rangeTxB) - - header = numpy.array(headerTuple,self.struct) - header.tofile(fp) - - #dynamic = self.dynamic - #dynamic.tofile(fp) - - samplingWindow = self.samplingWindow - samplingWindow.tofile(fp) - - if self.numTaus > 0: - self.Taus.tofile(fp) - - nCode = numpy.array(self.nCode, ' 0: - self.flag_cspc = True - -# except Exception, e: -# print "Error ProcessingHeader: " -# return 0 - - return 1 - - def write(self, fp): - headerTuple = (self.size, - self.dtype, - self.blockSize, - self.profilesPerBlock, - self.dataBlocksPerFile, - self.nWindows, - self.processFlags, - self.nCohInt, - self.nIncohInt, - self.totalSpectra) - - header = numpy.array(headerTuple,self.struct) - header.tofile(fp) - - if self.nWindows != 0: - sampleWindowTuple = (self.firstHeight,self.deltaHeight,self.samplesWin) - samplingWindow = numpy.array(sampleWindowTuple,self.structSamplingWindow) - samplingWindow.tofile(fp) - - - if self.totalSpectra != 0: - spectraComb = numpy.array([],numpy.dtype('u1')) - spectraComb = self.spectraComb - spectraComb.tofile(fp) - -# if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE: -# nCode = numpy.array([self.nCode], numpy.dtype('u4')) #Probar con un dato que almacene codigo, hasta el momento no se hizo la prueba -# nCode.tofile(fp) -# -# nBaud = numpy.array([self.nBaud], numpy.dtype('u4')) -# nBaud.tofile(fp) -# -# code = self.code.reshape(self.nCode*self.nBaud) -# code = code.astype(numpy.dtype('= 30.: - return False - return True - -class CrossSpectraPlot(Figure): - - __isConfig = None - __nsubplots = None - - WIDTH = None - HEIGHT = None - WIDTHPROF = None - HEIGHTPROF = None - PREFIX = 'cspc' - - def __init__(self): - - self.__isConfig = False - self.__nsubplots = 4 - self.counter_imagwr = 0 - self.WIDTH = 250 - self.HEIGHT = 250 - self.WIDTHPROF = 0 - self.HEIGHTPROF = 0 - - self.PLOT_CODE = 1 - self.FTP_WEI = None - self.EXP_CODE = None - self.SUB_EXP_CODE = None - self.PLOT_POS = None - - def getSubplots(self): - - ncol = 4 - nrow = self.nplots - - return nrow, ncol - - def setup(self, id, nplots, wintitle, showprofile=True, show=True): - - self.__showprofile = showprofile - self.nplots = nplots - - ncolspan = 1 - colspan = 1 - - self.createFigure(id = id, - wintitle = wintitle, - widthplot = self.WIDTH + self.WIDTHPROF, - heightplot = self.HEIGHT + self.HEIGHTPROF, - show=True) - - nrow, ncol = self.getSubplots() - - counter = 0 - for y in range(nrow): - for x in range(ncol): - self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) - - counter += 1 - - def run(self, dataOut, id, wintitle="", pairsList=None, - xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, - save=False, figpath='./', figfile=None, ftp=False, wr_period=1, - power_cmap='jet', coherence_cmap='jet', phase_cmap='RdBu_r', show=True, - server=None, folder=None, username=None, password=None, - ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): - - """ - - Input: - dataOut : - id : - wintitle : - channelList : - showProfile : - xmin : None, - xmax : None, - ymin : None, - ymax : None, - zmin : None, - zmax : None - """ - - if pairsList == None: - pairsIndexList = dataOut.pairsIndexList - else: - pairsIndexList = [] - for pair in pairsList: - if pair not in dataOut.pairsList: - raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair) - pairsIndexList.append(dataOut.pairsList.index(pair)) - - if pairsIndexList == []: - return - - if len(pairsIndexList) > 4: - pairsIndexList = pairsIndexList[0:4] - factor = dataOut.normFactor - x = dataOut.getVelRange(1) - y = dataOut.getHeiRange() - z = dataOut.data_spc[:,:,:]/factor -# z = numpy.where(numpy.isfinite(z), z, numpy.NAN) - avg = numpy.abs(numpy.average(z, axis=1)) - noise = dataOut.getNoise()/factor - - zdB = 10*numpy.log10(z) - avgdB = 10*numpy.log10(avg) - noisedB = 10*numpy.log10(noise) - - - #thisDatetime = dataOut.datatime - thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) - title = wintitle + " Cross-Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) - xlabel = "Velocity (m/s)" - ylabel = "Range (Km)" - - if not self.__isConfig: - - nplots = len(pairsIndexList) - - self.setup(id=id, - nplots=nplots, - wintitle=wintitle, - showprofile=False, - show=show) - - if xmin == None: xmin = numpy.nanmin(x) - if xmax == None: xmax = numpy.nanmax(x) - if ymin == None: ymin = numpy.nanmin(y) - if ymax == None: ymax = numpy.nanmax(y) - if zmin == None: zmin = numpy.nanmin(avgdB)*0.9 - if zmax == None: zmax = numpy.nanmax(avgdB)*0.9 - - self.FTP_WEI = ftp_wei - self.EXP_CODE = exp_code - self.SUB_EXP_CODE = sub_exp_code - self.PLOT_POS = plot_pos - - self.__isConfig = True - - self.setWinTitle(title) - - for i in range(self.nplots): - pair = dataOut.pairsList[pairsIndexList[i]] - str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S")) - title = "Ch%d: %4.2fdB: %s" %(pair[0], noisedB[pair[0]], str_datetime) - zdB = 10.*numpy.log10(dataOut.data_spc[pair[0],:,:]) - axes0 = self.axesList[i*self.__nsubplots] - axes0.pcolor(x, y, zdB, - xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, - xlabel=xlabel, ylabel=ylabel, title=title, - ticksize=9, colormap=power_cmap, cblabel='') - - title = "Ch%d: %4.2fdB: %s" %(pair[1], noisedB[pair[1]], str_datetime) - zdB = 10.*numpy.log10(dataOut.data_spc[pair[1],:,:]) - axes0 = self.axesList[i*self.__nsubplots+1] - axes0.pcolor(x, y, zdB, - xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, - xlabel=xlabel, ylabel=ylabel, title=title, - ticksize=9, colormap=power_cmap, cblabel='') - - coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[pair[0],:,:]*dataOut.data_spc[pair[1],:,:]) - coherence = numpy.abs(coherenceComplex) -# phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi - phase = numpy.arctan2(coherenceComplex.imag, coherenceComplex.real)*180/numpy.pi - - title = "Coherence %d%d" %(pair[0], pair[1]) - axes0 = self.axesList[i*self.__nsubplots+2] - axes0.pcolor(x, y, coherence, - xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=0, zmax=1, - xlabel=xlabel, ylabel=ylabel, title=title, - ticksize=9, colormap=coherence_cmap, cblabel='') - - title = "Phase %d%d" %(pair[0], pair[1]) - axes0 = self.axesList[i*self.__nsubplots+3] - axes0.pcolor(x, y, phase, - xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=-180, zmax=180, - xlabel=xlabel, ylabel=ylabel, title=title, - ticksize=9, colormap=phase_cmap, cblabel='') - - - - self.draw() - - if save: - - self.counter_imagwr += 1 - if (self.counter_imagwr==wr_period): - if figfile == None: - str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S") - figfile = self.getFilename(name = str_datetime) - - self.saveFigure(figpath, figfile) - - if ftp: - #provisionalmente envia archivos en el formato de la web en tiempo real - name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) - path = '%s%03d' %(self.PREFIX, self.id) - ftp_file = os.path.join(path,'ftp','%s.png'%name) - self.saveFigure(figpath, ftp_file) - ftp_filename = os.path.join(figpath,ftp_file) - - self.sendByFTP_Thread(ftp_filename, server, folder, username, password) - self.counter_imagwr = 0 - - self.counter_imagwr = 0 - -class SNRPlot(Figure): - - __isConfig = None - __nsubplots = None - - WIDTHPROF = None - HEIGHTPROF = None - PREFIX = 'snr' - - def __init__(self): - - self.timerange = 2*60*60 - self.__isConfig = False - self.__nsubplots = 1 - - self.WIDTH = 800 - self.HEIGHT = 150 - self.WIDTHPROF = 120 - self.HEIGHTPROF = 0 - self.counter_imagwr = 0 - - self.PLOT_CODE = 0 - self.FTP_WEI = None - self.EXP_CODE = None - self.SUB_EXP_CODE = None - self.PLOT_POS = None - - def getSubplots(self): - - ncol = 1 - nrow = self.nplots - - return nrow, ncol - - def setup(self, id, nplots, wintitle, showprofile=True, show=True): - - self.__showprofile = showprofile - self.nplots = nplots - - ncolspan = 1 - colspan = 1 - if showprofile: - ncolspan = 7 - colspan = 6 - self.__nsubplots = 2 - - self.createFigure(id = id, - wintitle = wintitle, - widthplot = self.WIDTH + self.WIDTHPROF, - heightplot = self.HEIGHT + self.HEIGHTPROF, - show=show) - - nrow, ncol = self.getSubplots() - - counter = 0 - for y in range(nrow): - for x in range(ncol): - - if counter >= self.nplots: - break - - self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) - - if showprofile: - self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) - - counter += 1 - - def run(self, dataOut, id, wintitle="", channelList=None, showprofile=False, - xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, - timerange=None, - save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True, - server=None, folder=None, username=None, password=None, - ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): - - """ - - Input: - dataOut : - id : - wintitle : - channelList : - showProfile : - xmin : None, - xmax : None, - ymin : None, - ymax : None, - zmin : None, - zmax : None - """ - - if channelList == None: - channelIndexList = dataOut.channelIndexList - else: - channelIndexList = [] - for channel in channelList: - if channel not in dataOut.channelList: - raise ValueError, "Channel %d is not in dataOut.channelList" - channelIndexList.append(dataOut.channelList.index(channel)) - - if timerange != None: - self.timerange = timerange - - tmin = None - tmax = None - factor = dataOut.normFactor - x = dataOut.getTimeRange() - y = dataOut.getHeiRange() - - z = dataOut.data_spc[channelIndexList,:,:]/factor - z = numpy.where(numpy.isfinite(z), z, numpy.NAN) - avg = numpy.average(z, axis=1) - - avgdB = 10.*numpy.log10(avg) - - noise = dataOut.getNoise()/factor - noisedB = 10.*numpy.log10(noise) - - SNR = numpy.transpose(numpy.divide(avg.T,noise)) - - SNR_dB = 10.*numpy.log10(SNR) - - #SNR_dB = numpy.transpose(numpy.subtract(avgdB.T, noisedB)) - -# thisDatetime = dataOut.datatime - thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) - title = wintitle + " RTI" #: %s" %(thisDatetime.strftime("%d-%b-%Y")) - xlabel = "" - ylabel = "Range (Km)" - - if not self.__isConfig: - - nplots = len(channelIndexList) - - self.setup(id=id, - nplots=nplots, - wintitle=wintitle, - showprofile=showprofile, - show=show) - - tmin, tmax = self.getTimeLim(x, xmin, xmax) - if ymin == None: ymin = numpy.nanmin(y) - if ymax == None: ymax = numpy.nanmax(y) - if zmin == None: zmin = numpy.nanmin(avgdB)*0.9 - if zmax == None: zmax = numpy.nanmax(avgdB)*0.9 - - self.FTP_WEI = ftp_wei - self.EXP_CODE = exp_code - self.SUB_EXP_CODE = sub_exp_code - self.PLOT_POS = plot_pos - - self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") - self.__isConfig = True - - - self.setWinTitle(title) - - for i in range(self.nplots): - title = "Channel %d: %s" %(dataOut.channelList[i]+1, thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) - axes = self.axesList[i*self.__nsubplots] - zdB = SNR_dB[i].reshape((1,-1)) - axes.pcolorbuffer(x, y, zdB, - xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, - xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, - ticksize=9, cblabel='', cbsize="1%") - -# if self.__showprofile: -# axes = self.axesList[i*self.__nsubplots +1] -# axes.pline(avgdB[i], y, -# xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, -# xlabel='dB', ylabel='', title='', -# ytick_visible=False, -# grid='x') -# - self.draw() - - if lastone: - if dataOut.blocknow >= dataOut.last_block: - if figfile == None: - figfile = self.getFilename(name = self.name) - self.saveFigure(figpath, figfile) - - if (save and not(lastone)): - - self.counter_imagwr += 1 - if (self.counter_imagwr==wr_period): - if figfile == None: - figfile = self.getFilename(name = self.name) - self.saveFigure(figpath, figfile) - - if ftp: - #provisionalmente envia archivos en el formato de la web en tiempo real - name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) - path = '%s%03d' %(self.PREFIX, self.id) - ftp_file = os.path.join(path,'ftp','%s.png'%name) - self.saveFigure(figpath, ftp_file) - ftp_filename = os.path.join(figpath,ftp_file) - self.sendByFTP_Thread(ftp_filename, server, folder, username, password) - self.counter_imagwr = 0 - - self.counter_imagwr = 0 - - if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: - - self.__isConfig = False - - if lastone: - if figfile == None: - figfile = self.getFilename(name = self.name) - self.saveFigure(figpath, figfile) - - if ftp: - #provisionalmente envia archivos en el formato de la web en tiempo real - name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) - path = '%s%03d' %(self.PREFIX, self.id) - ftp_file = os.path.join(path,'ftp','%s.png'%name) - self.saveFigure(figpath, ftp_file) - ftp_filename = os.path.join(figpath,ftp_file) - self.sendByFTP_Thread(ftp_filename, server, folder, username, password) - - -class RTIPlot(Figure): - - __isConfig = None - __nsubplots = None - - WIDTHPROF = None - HEIGHTPROF = None - PREFIX = 'rti' - - def __init__(self): - - self.timerange = 2*60*60 - self.__isConfig = False - self.__nsubplots = 1 - - self.WIDTH = 800 - self.HEIGHT = 150 - self.WIDTHPROF = 120 - self.HEIGHTPROF = 0 - self.counter_imagwr = 0 - - self.PLOT_CODE = 0 - self.FTP_WEI = None - self.EXP_CODE = None - self.SUB_EXP_CODE = None - self.PLOT_POS = None - self.tmin = None - self.tmax = None - - self.xmin = None - self.xmax = None - - def getSubplots(self): - - ncol = 1 - nrow = self.nplots - - return nrow, ncol - - def setup(self, id, nplots, wintitle, showprofile=True, show=True): - - self.__showprofile = showprofile - self.nplots = nplots - - ncolspan = 1 - colspan = 1 - if showprofile: - ncolspan = 7 - colspan = 6 - self.__nsubplots = 2 - - self.createFigure(id = id, - wintitle = wintitle, - widthplot = self.WIDTH + self.WIDTHPROF, - heightplot = self.HEIGHT + self.HEIGHTPROF, - show=show) - - nrow, ncol = self.getSubplots() - - counter = 0 - for y in range(nrow): - for x in range(ncol): - - if counter >= self.nplots: - break - - self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) - - if showprofile: - self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) - - counter += 1 - - def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True', - xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, - timerange=None, - save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True, - server=None, folder=None, username=None, password=None, - ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): - - """ - - Input: - dataOut : - id : - wintitle : - channelList : - showProfile : - xmin : None, - xmax : None, - ymin : None, - ymax : None, - zmin : None, - zmax : None - """ - - if channelList == None: - channelIndexList = dataOut.channelIndexList - else: - channelIndexList = [] - for channel in channelList: - if channel not in dataOut.channelList: - raise ValueError, "Channel %d is not in dataOut.channelList" - channelIndexList.append(dataOut.channelList.index(channel)) - - if timerange != None: - self.timerange = timerange - - #tmin = None - #tmax = None - factor = dataOut.normFactor - x = dataOut.getTimeRange() - y = dataOut.getHeiRange() - - z = dataOut.data_spc[channelIndexList,:,:]/factor - z = numpy.where(numpy.isfinite(z), z, numpy.NAN) - avg = numpy.average(z, axis=1) - - avgdB = 10.*numpy.log10(avg) - - -# thisDatetime = dataOut.datatime - thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) - title = wintitle + " RTI" #: %s" %(thisDatetime.strftime("%d-%b-%Y")) - xlabel = "" - ylabel = "Range (Km)" - - if not self.__isConfig: - - nplots = len(channelIndexList) - - self.setup(id=id, - nplots=nplots, - wintitle=wintitle, - showprofile=showprofile, - show=show) - - self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange) - -# if timerange != None: -# self.timerange = timerange -# self.xmin, self.tmax = self.getTimeLim(x, xmin, xmax, timerange) - - - - if ymin == None: ymin = numpy.nanmin(y) - if ymax == None: ymax = numpy.nanmax(y) - if zmin == None: zmin = numpy.nanmin(avgdB)*0.9 - if zmax == None: zmax = numpy.nanmax(avgdB)*0.9 - - self.FTP_WEI = ftp_wei - self.EXP_CODE = exp_code - self.SUB_EXP_CODE = sub_exp_code - self.PLOT_POS = plot_pos - - self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") - self.__isConfig = True - - - self.setWinTitle(title) - - if ((self.xmax - x[1]) < (x[1]-x[0])): - x[1] = self.xmax - - for i in range(self.nplots): - title = "Channel %d: %s" %(dataOut.channelList[i]+1, thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) - axes = self.axesList[i*self.__nsubplots] - zdB = avgdB[i].reshape((1,-1)) - axes.pcolorbuffer(x, y, zdB, - xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, - xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, - ticksize=9, cblabel='', cbsize="1%") - - if self.__showprofile: - axes = self.axesList[i*self.__nsubplots +1] - axes.pline(avgdB[i], y, - xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, - xlabel='dB', ylabel='', title='', - ytick_visible=False, - grid='x') - - self.draw() - -# if lastone: -# if dataOut.blocknow >= dataOut.last_block: -# if figfile == None: -# figfile = self.getFilename(name = self.name) -# self.saveFigure(figpath, figfile) -# -# if (save and not(lastone)): -# -# self.counter_imagwr += 1 -# if (self.counter_imagwr==wr_period): -# if figfile == None: -# figfile = self.getFilename(name = self.name) -# self.saveFigure(figpath, figfile) -# -# if ftp: -# #provisionalmente envia archivos en el formato de la web en tiempo real -# name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) -# path = '%s%03d' %(self.PREFIX, self.id) -# ftp_file = os.path.join(path,'ftp','%s.png'%name) -# self.saveFigure(figpath, ftp_file) -# ftp_filename = os.path.join(figpath,ftp_file) -# self.sendByFTP_Thread(ftp_filename, server, folder, username, password) -# self.counter_imagwr = 0 -# -# self.counter_imagwr = 0 - - #if ((dataOut.utctime-time.timezone) >= self.axesList[0].xmax): - self.saveFigure(figpath, figfile) - if x[1] >= self.axesList[0].xmax: - self.saveFigure(figpath, figfile) - self.__isConfig = False - -# if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: -# -# self.__isConfig = False - -# if lastone: -# if figfile == None: -# figfile = self.getFilename(name = self.name) -# self.saveFigure(figpath, figfile) -# -# if ftp: -# #provisionalmente envia archivos en el formato de la web en tiempo real -# name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) -# path = '%s%03d' %(self.PREFIX, self.id) -# ftp_file = os.path.join(path,'ftp','%s.png'%name) -# self.saveFigure(figpath, ftp_file) -# ftp_filename = os.path.join(figpath,ftp_file) -# self.sendByFTP_Thread(ftp_filename, server, folder, username, password) - - -class SpectraPlot(Figure): - - __isConfig = None - __nsubplots = None - - WIDTHPROF = None - HEIGHTPROF = None - PREFIX = 'spc' - - def __init__(self): - - self.__isConfig = False - self.__nsubplots = 1 - - self.WIDTH = 280 - self.HEIGHT = 250 - self.WIDTHPROF = 120 - self.HEIGHTPROF = 0 - self.counter_imagwr = 0 - - self.PLOT_CODE = 1 - self.FTP_WEI = None - self.EXP_CODE = None - self.SUB_EXP_CODE = None - self.PLOT_POS = None - - def getSubplots(self): - - ncol = int(numpy.sqrt(self.nplots)+0.9) - nrow = int(self.nplots*1./ncol + 0.9) - - return nrow, ncol - - def setup(self, id, nplots, wintitle, showprofile=True, show=True): - - self.__showprofile = showprofile - self.nplots = nplots - - ncolspan = 1 - colspan = 1 - if showprofile: - ncolspan = 3 - colspan = 2 - self.__nsubplots = 2 - - self.createFigure(id = id, - wintitle = wintitle, - widthplot = self.WIDTH + self.WIDTHPROF, - heightplot = self.HEIGHT + self.HEIGHTPROF, - show=show) - - nrow, ncol = self.getSubplots() - - counter = 0 - for y in range(nrow): - for x in range(ncol): - - if counter >= self.nplots: - break - - self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) - - if showprofile: - self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) - - counter += 1 - - def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True, - xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, - save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1, - server=None, folder=None, username=None, password=None, - ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False): - - """ - - Input: - dataOut : - id : - wintitle : - channelList : - showProfile : - xmin : None, - xmax : None, - ymin : None, - ymax : None, - zmin : None, - zmax : None - """ - - if dataOut.flagNoData: - return None - - if realtime: - if not(isRealtime(utcdatatime = dataOut.utctime)): - print 'Skipping this plot function' - return - - if channelList == None: - channelIndexList = dataOut.channelIndexList - else: - channelIndexList = [] - for channel in channelList: - if channel not in dataOut.channelList: - raise ValueError, "Channel %d is not in dataOut.channelList" - channelIndexList.append(dataOut.channelList.index(channel)) - factor = dataOut.normFactor - x = dataOut.getVelRange(1) - y = dataOut.getHeiRange() - - z = dataOut.data_spc[channelIndexList,:,:]/factor - z = numpy.where(numpy.isfinite(z), z, numpy.NAN) - avg = numpy.average(z, axis=1) - noise = dataOut.getNoise()/factor - - zdB = 10*numpy.log10(z) - avgdB = 10*numpy.log10(avg) - noisedB = 10*numpy.log10(noise) - - #thisDatetime = dataOut.datatime - thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) - title = wintitle + " Spectra" - xlabel = "Velocity (m/s)" - ylabel = "Range (Km)" - - if not self.__isConfig: - - nplots = len(channelIndexList) - - self.setup(id=id, - nplots=nplots, - wintitle=wintitle, - showprofile=showprofile, - show=show) - - if xmin == None: xmin = numpy.nanmin(x) - if xmax == None: xmax = numpy.nanmax(x) - if ymin == None: ymin = numpy.nanmin(y) - if ymax == None: ymax = numpy.nanmax(y) - if zmin == None: zmin = numpy.nanmin(avgdB)*0.9 - if zmax == None: zmax = numpy.nanmax(avgdB)*0.9 - - self.FTP_WEI = ftp_wei - self.EXP_CODE = exp_code - self.SUB_EXP_CODE = sub_exp_code - self.PLOT_POS = plot_pos - - self.__isConfig = True - - self.setWinTitle(title) - - for i in range(self.nplots): - str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S")) - title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[i]+1, noisedB[i], str_datetime) - axes = self.axesList[i*self.__nsubplots] - axes.pcolor(x, y, zdB[i,:,:], - xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, - xlabel=xlabel, ylabel=ylabel, title=title, - ticksize=9, cblabel='') - - if self.__showprofile: - axes = self.axesList[i*self.__nsubplots +1] - axes.pline(avgdB[i], y, - xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, - xlabel='dB', ylabel='', title='', - ytick_visible=False, - grid='x') - - noiseline = numpy.repeat(noisedB[i], len(y)) - axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2) - - self.draw() - - if save: - - self.counter_imagwr += 1 - if (self.counter_imagwr==wr_period): - if figfile == None: - str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S") - figfile = self.getFilename(name = str_datetime) - - self.saveFigure(figpath, figfile) - - if ftp: - #provisionalmente envia archivos en el formato de la web en tiempo real - name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) - path = '%s%03d' %(self.PREFIX, self.id) - ftp_file = os.path.join(path,'ftp','%s.png'%name) - self.saveFigure(figpath, ftp_file) - ftp_filename = os.path.join(figpath,ftp_file) - self.sendByFTP_Thread(ftp_filename, server, folder, username, password) - self.counter_imagwr = 0 - - - self.counter_imagwr = 0 - - -class Scope(Figure): - - __isConfig = None - - def __init__(self): - - self.__isConfig = False - self.WIDTH = 300 - self.HEIGHT = 200 - self.counter_imagwr = 0 - - def getSubplots(self): - - nrow = self.nplots - ncol = 3 - return nrow, ncol - - def setup(self, id, nplots, wintitle, show): - - self.nplots = nplots - - self.createFigure(id=id, - wintitle=wintitle, - show=show) - - nrow,ncol = self.getSubplots() - colspan = 3 - rowspan = 1 - - for i in range(nplots): - self.addAxes(nrow, ncol, i, 0, colspan, rowspan) - - def plot_iq(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax): - yreal = y[channelIndexList,:].real - yimag = y[channelIndexList,:].imag - - title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) - xlabel = "Range (Km)" - ylabel = "Intensity - IQ" - - if not self.__isConfig: - nplots = len(channelIndexList) - - self.setup(id=id, - nplots=nplots, - wintitle='', - show=show) - - if xmin == None: xmin = numpy.nanmin(x) - if xmax == None: xmax = numpy.nanmax(x) - if ymin == None: ymin = min(numpy.nanmin(yreal),numpy.nanmin(yimag)) - if ymax == None: ymax = max(numpy.nanmax(yreal),numpy.nanmax(yimag)) - - self.__isConfig = True - - self.setWinTitle(title) - - for i in range(len(self.axesList)): - title = "Channel %d" %(i) - axes = self.axesList[i] - - axes.pline(x, yreal[i,:], - xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, - xlabel=xlabel, ylabel=ylabel, title=title) - - axes.addpline(x, yimag[i,:], idline=1, color="red", linestyle="solid", lw=2) - - def plot_power(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax): - y = y[channelIndexList,:] * numpy.conjugate(y[channelIndexList,:]) - yreal = y.real - - title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) - xlabel = "Range (Km)" - ylabel = "Intensity" - - if not self.__isConfig: - nplots = len(channelIndexList) - - self.setup(id=id, - nplots=nplots, - wintitle='', - show=show) - - if xmin == None: xmin = numpy.nanmin(x) - if xmax == None: xmax = numpy.nanmax(x) - if ymin == None: ymin = numpy.nanmin(yreal) - if ymax == None: ymax = numpy.nanmax(yreal) - - self.__isConfig = True - - self.setWinTitle(title) - - for i in range(len(self.axesList)): - title = "Channel %d" %(i) - axes = self.axesList[i] - ychannel = yreal[i,:] - axes.pline(x, ychannel, - xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, - xlabel=xlabel, ylabel=ylabel, title=title) - - - def run(self, dataOut, id, wintitle="", channelList=None, - xmin=None, xmax=None, ymin=None, ymax=None, save=False, - figpath='./', figfile=None, show=True, wr_period=1, - server=None, folder=None, username=None, password=None, type='power'): - - """ - - Input: - dataOut : - id : - wintitle : - channelList : - xmin : None, - xmax : None, - ymin : None, - ymax : None, - """ - if dataOut.flagNoData: - return None - - if channelList == None: - channelIndexList = dataOut.channelIndexList - else: - channelIndexList = [] - for channel in channelList: - if channel not in dataOut.channelList: - raise ValueError, "Channel %d is not in dataOut.channelList" - channelIndexList.append(dataOut.channelList.index(channel)) - - x = dataOut.heightList - y = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:]) - y = y.real - - thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) - - if type == "power": - self.plot_power(dataOut.heightList, - dataOut.data, - id, - channelIndexList, - thisDatetime, - wintitle, - show, - xmin, - xmax, - ymin, - ymax) - - if type == "iq": - self.plot_iq(dataOut.heightList, - dataOut.data, - id, - channelIndexList, - thisDatetime, - wintitle, - show, - xmin, - xmax, - ymin, - ymax) - - - self.draw() - - if save: - date = thisDatetime.strftime("%Y%m%d_%H%M%S") - if figfile == None: - figfile = self.getFilename(name = date) - - self.saveFigure(figpath, figfile) - - self.counter_imagwr += 1 - if (ftp and (self.counter_imagwr==wr_period)): - ftp_filename = os.path.join(figpath,figfile) - self.sendByFTP_Thread(ftp_filename, server, folder, username, password) - self.counter_imagwr = 0 - -class PowerProfile(Figure): - __isConfig = None - __nsubplots = None - - WIDTHPROF = None - HEIGHTPROF = None - PREFIX = 'spcprofile' - - def __init__(self): - self.__isConfig = False - self.__nsubplots = 1 - - self.WIDTH = 300 - self.HEIGHT = 500 - self.counter_imagwr = 0 - - def getSubplots(self): - ncol = 1 - nrow = 1 - - return nrow, ncol - - def setup(self, id, nplots, wintitle, show): - - self.nplots = nplots - - ncolspan = 1 - colspan = 1 - - self.createFigure(id = id, - wintitle = wintitle, - widthplot = self.WIDTH, - heightplot = self.HEIGHT, - show=show) - - nrow, ncol = self.getSubplots() - - counter = 0 - for y in range(nrow): - for x in range(ncol): - self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) - - def run(self, dataOut, id, wintitle="", channelList=None, - xmin=None, xmax=None, ymin=None, ymax=None, - save=False, figpath='./', figfile=None, show=True, wr_period=1, - server=None, folder=None, username=None, password=None,): - - if dataOut.flagNoData: - return None - - if channelList == None: - channelIndexList = dataOut.channelIndexList - channelList = dataOut.channelList - else: - channelIndexList = [] - for channel in channelList: - if channel not in dataOut.channelList: - raise ValueError, "Channel %d is not in dataOut.channelList" - channelIndexList.append(dataOut.channelList.index(channel)) - - try: - factor = dataOut.normFactor - except: - factor = 1 - - y = dataOut.getHeiRange() - - #for voltage - if dataOut.type == 'Voltage': - x = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:]) - x = x.real - x = numpy.where(numpy.isfinite(x), x, numpy.NAN) - - #for spectra - if dataOut.type == 'Spectra': - x = dataOut.data_spc[channelIndexList,:,:]/factor - x = numpy.where(numpy.isfinite(x), x, numpy.NAN) - x = numpy.average(x, axis=1) - - - xdB = 10*numpy.log10(x) - - thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) - title = wintitle + " Power Profile %s" %(thisDatetime.strftime("%d-%b-%Y")) - xlabel = "dB" - ylabel = "Range (Km)" - - if not self.__isConfig: - - nplots = 1 - - self.setup(id=id, - nplots=nplots, - wintitle=wintitle, - show=show) - - if ymin == None: ymin = numpy.nanmin(y) - if ymax == None: ymax = numpy.nanmax(y) - if xmin == None: xmin = numpy.nanmin(xdB)*0.9 - if xmax == None: xmax = numpy.nanmax(xdB)*0.9 - - self.__isConfig = True - - self.setWinTitle(title) - - title = "Power Profile: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) - axes = self.axesList[0] - - legendlabels = ["channel %d"%x for x in channelList] - axes.pmultiline(xdB, y, - xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, - xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, - ytick_visible=True, nxticks=5, - grid='x') - - self.draw() - - if save: - date = thisDatetime.strftime("%Y%m%d") - if figfile == None: - figfile = self.getFilename(name = date) - - self.saveFigure(figpath, figfile) - - self.counter_imagwr += 1 - if (ftp and (self.counter_imagwr==wr_period)): - ftp_filename = os.path.join(figpath,figfile) - self.sendByFTP_Thread(ftp_filename, server, folder, username, password) - self.counter_imagwr = 0 - -class CoherenceMap(Figure): - __isConfig = None - __nsubplots = None - - WIDTHPROF = None - HEIGHTPROF = None - PREFIX = 'cmap' - - def __init__(self): - self.timerange = 2*60*60 - self.__isConfig = False - self.__nsubplots = 1 - - self.WIDTH = 800 - self.HEIGHT = 150 - self.WIDTHPROF = 120 - self.HEIGHTPROF = 0 - self.counter_imagwr = 0 - - self.PLOT_CODE = 3 - self.FTP_WEI = None - self.EXP_CODE = None - self.SUB_EXP_CODE = None - self.PLOT_POS = None - self.counter_imagwr = 0 - - self.xmin = None - self.xmax = None - - def getSubplots(self): - ncol = 1 - nrow = self.nplots*2 - - return nrow, ncol - - def setup(self, id, nplots, wintitle, showprofile=True, show=True): - self.__showprofile = showprofile - self.nplots = nplots - - ncolspan = 1 - colspan = 1 - if showprofile: - ncolspan = 7 - colspan = 6 - self.__nsubplots = 2 - - self.createFigure(id = id, - wintitle = wintitle, - widthplot = self.WIDTH + self.WIDTHPROF, - heightplot = self.HEIGHT + self.HEIGHTPROF, - show=True) - - nrow, ncol = self.getSubplots() - - for y in range(nrow): - for x in range(ncol): - - self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) - - if showprofile: - self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) - - def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True', - xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, - timerange=None, - save=False, figpath='./', figfile=None, ftp=False, wr_period=1, - coherence_cmap='jet', phase_cmap='RdBu_r', show=True, - server=None, folder=None, username=None, password=None, - ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): - - if pairsList == None: - pairsIndexList = dataOut.pairsIndexList - else: - pairsIndexList = [] - for pair in pairsList: - if pair not in dataOut.pairsList: - raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair) - pairsIndexList.append(dataOut.pairsList.index(pair)) - - if timerange != None: - self.timerange = timerange - - if pairsIndexList == []: - return - - if len(pairsIndexList) > 4: - pairsIndexList = pairsIndexList[0:4] - -# tmin = None -# tmax = None - x = dataOut.getTimeRange() - y = dataOut.getHeiRange() - - #thisDatetime = dataOut.datatime - thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) - title = wintitle + " CoherenceMap" #: %s" %(thisDatetime.strftime("%d-%b-%Y")) - xlabel = "" - ylabel = "Range (Km)" - - if not self.__isConfig: - nplots = len(pairsIndexList) - self.setup(id=id, - nplots=nplots, - wintitle=wintitle, - showprofile=showprofile, - show=show) - - #tmin, tmax = self.getTimeLim(x, xmin, xmax) - - self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange) - - if ymin == None: ymin = numpy.nanmin(y) - if ymax == None: ymax = numpy.nanmax(y) - if zmin == None: zmin = 0. - if zmax == None: zmax = 1. - - self.FTP_WEI = ftp_wei - self.EXP_CODE = exp_code - self.SUB_EXP_CODE = sub_exp_code - self.PLOT_POS = plot_pos - - self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") - - self.__isConfig = True - - self.setWinTitle(title) - - if ((self.xmax - x[1]) < (x[1]-x[0])): - x[1] = self.xmax - - for i in range(self.nplots): - - pair = dataOut.pairsList[pairsIndexList[i]] -# coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[pair[0],:,:]*dataOut.data_spc[pair[1],:,:]) -# avgcoherenceComplex = numpy.average(coherenceComplex, axis=0) -# coherence = numpy.abs(avgcoherenceComplex) - -## coherence = numpy.abs(coherenceComplex) -## avg = numpy.average(coherence, axis=0) - - ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i],:,:],axis=0) - powa = numpy.average(dataOut.data_spc[pair[0],:,:],axis=0) - powb = numpy.average(dataOut.data_spc[pair[1],:,:],axis=0) - - - avgcoherenceComplex = ccf/numpy.sqrt(powa*powb) - coherence = numpy.abs(avgcoherenceComplex) - - z = coherence.reshape((1,-1)) - - counter = 0 - - title = "Coherence %d%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) - axes = self.axesList[i*self.__nsubplots*2] - axes.pcolorbuffer(x, y, z, - xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, - xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, - ticksize=9, cblabel='', colormap=coherence_cmap, cbsize="1%") - - if self.__showprofile: - counter += 1 - axes = self.axesList[i*self.__nsubplots*2 + counter] - axes.pline(coherence, y, - xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, - xlabel='', ylabel='', title='', ticksize=7, - ytick_visible=False, nxticks=5, - grid='x') - - counter += 1 -# phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi - phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi -# avg = numpy.average(phase, axis=0) - z = phase.reshape((1,-1)) - - title = "Phase %d%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) - axes = self.axesList[i*self.__nsubplots*2 + counter] - axes.pcolorbuffer(x, y, z, - xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=-180, zmax=180, - xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, - ticksize=9, cblabel='', colormap=phase_cmap, cbsize="1%") - - if self.__showprofile: - counter += 1 - axes = self.axesList[i*self.__nsubplots*2 + counter] - axes.pline(phase, y, - xmin=-180, xmax=180, ymin=ymin, ymax=ymax, - xlabel='', ylabel='', title='', ticksize=7, - ytick_visible=False, nxticks=4, - grid='x') - - self.draw() - - if x[1] >= self.axesList[0].xmax: - self.saveFigure(figpath, figfile) - self.__isConfig = False - -# if save: -# -# self.counter_imagwr += 1 -# if (self.counter_imagwr==wr_period): -# if figfile == None: -# figfile = self.getFilename(name = self.name) -# self.saveFigure(figpath, figfile) -# -# if ftp: -# #provisionalmente envia archivos en el formato de la web en tiempo real -# name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) -# path = '%s%03d' %(self.PREFIX, self.id) -# ftp_file = os.path.join(path,'ftp','%s.png'%name) -# self.saveFigure(figpath, ftp_file) -# ftp_filename = os.path.join(figpath,ftp_file) -# self.sendByFTP_Thread(ftp_filename, server, folder, username, password) -# self.counter_imagwr = 0 -# -# self.counter_imagwr = 0 -# -# -# if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: -# self.__isConfig = False - -class BeaconPhase(Figure): - - __isConfig = None - __nsubplots = None - - PREFIX = 'beacon_phase' - - def __init__(self): - - self.timerange = 24*60*60 - self.__isConfig = False - self.__nsubplots = 1 - self.counter_imagwr = 0 - self.WIDTH = 600 - self.HEIGHT = 300 - self.WIDTHPROF = 120 - self.HEIGHTPROF = 0 - self.xdata = None - self.ydata = None - - self.PLOT_CODE = 18 - self.FTP_WEI = None - self.EXP_CODE = None - self.SUB_EXP_CODE = None - self.PLOT_POS = None - - self.filename_phase = None - - def getSubplots(self): - - ncol = 1 - nrow = 1 - - return nrow, ncol - - def setup(self, id, nplots, wintitle, showprofile=True, show=True): - - self.__showprofile = showprofile - self.nplots = nplots - - ncolspan = 7 - colspan = 6 - self.__nsubplots = 2 - - self.createFigure(id = id, - wintitle = wintitle, - widthplot = self.WIDTH+self.WIDTHPROF, - heightplot = self.HEIGHT+self.HEIGHTPROF, - show=show) - - nrow, ncol = self.getSubplots() - - self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1) - - def save_phase(self, filename_phase): - f = open(filename_phase,'w+') - f.write('\n\n') - f.write('JICAMARCA RADIO OBSERVATORY - Beacon Phase \n') - f.write('DD MM YYYY HH MM SS pair(2,0) pair(2,1) pair(2,3) pair(2,4)\n\n' ) - f.close() - - def save_data(self, filename_phase, data, data_datetime): - f=open(filename_phase,'a') - timetuple_data = data_datetime.timetuple() - day = str(timetuple_data.tm_mday) - month = str(timetuple_data.tm_mon) - year = str(timetuple_data.tm_year) - hour = str(timetuple_data.tm_hour) - minute = str(timetuple_data.tm_min) - second = str(timetuple_data.tm_sec) - f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' '+str(data[0])+' '+str(data[1])+' '+str(data[2])+' '+str(data[3])+'\n') - f.close() - - - def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True', - xmin=None, xmax=None, ymin=None, ymax=None, - timerange=None, - save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1, - server=None, folder=None, username=None, password=None, - ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): - - if pairsList == None: - pairsIndexList = dataOut.pairsIndexList - else: - pairsIndexList = [] - for pair in pairsList: - if pair not in dataOut.pairsList: - raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair) - pairsIndexList.append(dataOut.pairsList.index(pair)) - - if pairsIndexList == []: - return - -# if len(pairsIndexList) > 4: -# pairsIndexList = pairsIndexList[0:4] - - if timerange != None: - self.timerange = timerange - - tmin = None - tmax = None - x = dataOut.getTimeRange() - y = dataOut.getHeiRange() - - - #thisDatetime = dataOut.datatime - thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) - title = wintitle + " Phase of Beacon Signal" # : %s" %(thisDatetime.strftime("%d-%b-%Y")) - xlabel = "Local Time" - ylabel = "Phase" - - nplots = len(pairsIndexList) - #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList))) - phase_beacon = numpy.zeros(len(pairsIndexList)) - for i in range(nplots): - pair = dataOut.pairsList[pairsIndexList[i]] - ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i],:,:],axis=0) - powa = numpy.average(dataOut.data_spc[pair[0],:,:],axis=0) - powb = numpy.average(dataOut.data_spc[pair[1],:,:],axis=0) - avgcoherenceComplex = ccf/numpy.sqrt(powa*powb) - phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi - - #print "Phase %d%d" %(pair[0], pair[1]) - #print phase[dataOut.beacon_heiIndexList] - - phase_beacon[i] = numpy.average(phase[dataOut.beacon_heiIndexList]) - - if not self.__isConfig: - - nplots = len(pairsIndexList) - - self.setup(id=id, - nplots=nplots, - wintitle=wintitle, - showprofile=showprofile, - show=show) - - tmin, tmax = self.getTimeLim(x, xmin, xmax) - if ymin == None: ymin = numpy.nanmin(phase_beacon) - 10.0 - if ymax == None: ymax = numpy.nanmax(phase_beacon) + 10.0 - - self.FTP_WEI = ftp_wei - self.EXP_CODE = exp_code - self.SUB_EXP_CODE = sub_exp_code - self.PLOT_POS = plot_pos - - self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") - self.__isConfig = True - - self.xdata = numpy.array([]) - self.ydata = numpy.array([]) - - #open file beacon phase - path = '%s%03d' %(self.PREFIX, self.id) - beacon_file = os.path.join(path,'%s.txt'%self.name) - self.filename_phase = os.path.join(figpath,beacon_file) - #self.save_phase(self.filename_phase) - - - #store data beacon phase - #self.save_data(self.filename_phase, phase_beacon, thisDatetime) - - self.setWinTitle(title) - - - title = "Beacon Signal %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) - - legendlabels = ["pairs %d%d"%(pair[0], pair[1]) for pair in dataOut.pairsList] - - axes = self.axesList[0] - - self.xdata = numpy.hstack((self.xdata, x[0:1])) - - if len(self.ydata)==0: - self.ydata = phase_beacon.reshape(-1,1) - else: - self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1))) - - - axes.pmultilineyaxis(x=self.xdata, y=self.ydata, - xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, - xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid", - XAxisAsTime=True, grid='both' - ) - - self.draw() - - if save: - - self.counter_imagwr += 1 - if (self.counter_imagwr==wr_period): - if figfile == None: - figfile = self.getFilename(name = self.name) - self.saveFigure(figpath, figfile) - - if ftp: - #provisionalmente envia archivos en el formato de la web en tiempo real - name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) - path = '%s%03d' %(self.PREFIX, self.id) - ftp_file = os.path.join(path,'ftp','%s.png'%name) - self.saveFigure(figpath, ftp_file) - ftp_filename = os.path.join(figpath,ftp_file) - self.sendByFTP_Thread(ftp_filename, server, folder, username, password) - - self.counter_imagwr = 0 - - if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: - self.__isConfig = False - del self.xdata - del self.ydata - - - - -class Noise(Figure): - - __isConfig = None - __nsubplots = None - - PREFIX = 'noise' - - def __init__(self): - - self.timerange = 24*60*60 - self.__isConfig = False - self.__nsubplots = 1 - self.counter_imagwr = 0 - self.WIDTH = 600 - self.HEIGHT = 300 - self.WIDTHPROF = 120 - self.HEIGHTPROF = 0 - self.xdata = None - self.ydata = None - - self.PLOT_CODE = 77 - self.FTP_WEI = None - self.EXP_CODE = None - self.SUB_EXP_CODE = None - self.PLOT_POS = None - - def getSubplots(self): - - ncol = 1 - nrow = 1 - - return nrow, ncol - - def openfile(self, filename): - f = open(filename,'w+') - f.write('\n\n') - f.write('JICAMARCA RADIO OBSERVATORY - Noise \n') - f.write('DD MM YYYY HH MM SS Channel0 Channel1 Channel2 Channel3\n\n' ) - f.close() - - def save_data(self, filename_phase, data, data_datetime): - f=open(filename_phase,'a') - timetuple_data = data_datetime.timetuple() - day = str(timetuple_data.tm_mday) - month = str(timetuple_data.tm_mon) - year = str(timetuple_data.tm_year) - hour = str(timetuple_data.tm_hour) - minute = str(timetuple_data.tm_min) - second = str(timetuple_data.tm_sec) - f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' '+str(data[0])+' '+str(data[1])+' '+str(data[2])+' '+str(data[3])+'\n') - f.close() - - - def setup(self, id, nplots, wintitle, showprofile=True, show=True): - - self.__showprofile = showprofile - self.nplots = nplots - - ncolspan = 7 - colspan = 6 - self.__nsubplots = 2 - - self.createFigure(id = id, - wintitle = wintitle, - widthplot = self.WIDTH+self.WIDTHPROF, - heightplot = self.HEIGHT+self.HEIGHTPROF, - show=show) - - nrow, ncol = self.getSubplots() - - self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1) - - - def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True', - xmin=None, xmax=None, ymin=None, ymax=None, - timerange=None, - save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1, - server=None, folder=None, username=None, password=None, - ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): - - if channelList == None: - channelIndexList = dataOut.channelIndexList - channelList = dataOut.channelList - else: - channelIndexList = [] - for channel in channelList: - if channel not in dataOut.channelList: - raise ValueError, "Channel %d is not in dataOut.channelList" - channelIndexList.append(dataOut.channelList.index(channel)) - - if timerange != None: - self.timerange = timerange - - tmin = None - tmax = None - x = dataOut.getTimeRange() - y = dataOut.getHeiRange() - factor = dataOut.normFactor - noise = dataOut.getNoise()/factor - noisedB = 10*numpy.log10(noise) - - #thisDatetime = dataOut.datatime - thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) - title = wintitle + " Noise" # : %s" %(thisDatetime.strftime("%d-%b-%Y")) - xlabel = "" - ylabel = "Intensity (dB)" - - if not self.__isConfig: - - nplots = 1 - - self.setup(id=id, - nplots=nplots, - wintitle=wintitle, - showprofile=showprofile, - show=show) - - tmin, tmax = self.getTimeLim(x, xmin, xmax) - if ymin == None: ymin = numpy.nanmin(noisedB) - 10.0 - if ymax == None: ymax = numpy.nanmax(noisedB) + 10.0 - - self.FTP_WEI = ftp_wei - self.EXP_CODE = exp_code - self.SUB_EXP_CODE = sub_exp_code - self.PLOT_POS = plot_pos - - - self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") - self.__isConfig = True - - self.xdata = numpy.array([]) - self.ydata = numpy.array([]) - - #open file beacon phase - path = '%s%03d' %(self.PREFIX, self.id) - noise_file = os.path.join(path,'%s.txt'%self.name) - self.filename_noise = os.path.join(figpath,noise_file) - self.openfile(self.filename_noise) - - - #store data beacon phase - self.save_data(self.filename_noise, noisedB, thisDatetime) - - - self.setWinTitle(title) - - - title = "Noise %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) - - legendlabels = ["channel %d"%(idchannel+1) for idchannel in channelList] - axes = self.axesList[0] - - self.xdata = numpy.hstack((self.xdata, x[0:1])) - - if len(self.ydata)==0: - self.ydata = noisedB[channelIndexList].reshape(-1,1) - else: - self.ydata = numpy.hstack((self.ydata, noisedB[channelIndexList].reshape(-1,1))) - - - axes.pmultilineyaxis(x=self.xdata, y=self.ydata, - xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, - xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid", - XAxisAsTime=True, grid='both' - ) - - self.draw() - -# if save: -# -# if figfile == None: -# figfile = self.getFilename(name = self.name) -# -# self.saveFigure(figpath, figfile) - - if save: - - self.counter_imagwr += 1 - if (self.counter_imagwr==wr_period): - if figfile == None: - figfile = self.getFilename(name = self.name) - self.saveFigure(figpath, figfile) - - if ftp: - #provisionalmente envia archivos en el formato de la web en tiempo real - name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) - path = '%s%03d' %(self.PREFIX, self.id) - ftp_file = os.path.join(path,'ftp','%s.png'%name) - self.saveFigure(figpath, ftp_file) - ftp_filename = os.path.join(figpath,ftp_file) - self.sendByFTP_Thread(ftp_filename, server, folder, username, password) - self.counter_imagwr = 0 - - self.counter_imagwr = 0 - - if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: - self.__isConfig = False - del self.xdata - del self.ydata - - -class SpectraHeisScope(Figure): - - - __isConfig = None - __nsubplots = None - - WIDTHPROF = None - HEIGHTPROF = None - PREFIX = 'spc' - - def __init__(self): - - self.__isConfig = False - self.__nsubplots = 1 - - self.WIDTH = 230 - self.HEIGHT = 250 - self.WIDTHPROF = 120 - self.HEIGHTPROF = 0 - self.counter_imagwr = 0 - - def getSubplots(self): - - ncol = int(numpy.sqrt(self.nplots)+0.9) - nrow = int(self.nplots*1./ncol + 0.9) - - return nrow, ncol - - def setup(self, id, nplots, wintitle, show): - - showprofile = False - self.__showprofile = showprofile - self.nplots = nplots - - ncolspan = 1 - colspan = 1 - if showprofile: - ncolspan = 3 - colspan = 2 - self.__nsubplots = 2 - - self.createFigure(id = id, - wintitle = wintitle, - widthplot = self.WIDTH + self.WIDTHPROF, - heightplot = self.HEIGHT + self.HEIGHTPROF, - show = show) - - nrow, ncol = self.getSubplots() - - counter = 0 - for y in range(nrow): - for x in range(ncol): - - if counter >= self.nplots: - break - - self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) - - if showprofile: - self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) - - counter += 1 - - - def run(self, dataOut, id, wintitle="", channelList=None, - xmin=None, xmax=None, ymin=None, ymax=None, save=False, - figpath='./', figfile=None, ftp=False, wr_period=1, show=True, - server=None, folder=None, username=None, password=None): - - """ - - Input: - dataOut : - id : - wintitle : - channelList : - xmin : None, - xmax : None, - ymin : None, - ymax : None, - """ - - if dataOut.realtime: - if not(isRealtime(utcdatatime = dataOut.utctime)): - print 'Skipping this plot function' - return - - if channelList == None: - channelIndexList = dataOut.channelIndexList - else: - channelIndexList = [] - for channel in channelList: - if channel not in dataOut.channelList: - raise ValueError, "Channel %d is not in dataOut.channelList" - channelIndexList.append(dataOut.channelList.index(channel)) - -# x = dataOut.heightList - c = 3E8 - deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] - #deberia cambiar para el caso de 1Mhz y 100KHz - x = numpy.arange(-1*dataOut.nHeights/2.,dataOut.nHeights/2.)*(c/(2*deltaHeight*dataOut.nHeights*1000)) - #para 1Mhz descomentar la siguiente linea - #x= x/(10000.0) -# y = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:]) -# y = y.real - datadB = 10.*numpy.log10(dataOut.data_spc) - y = datadB - - #thisDatetime = dataOut.datatime - thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) - title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) - xlabel = "" - #para 1Mhz descomentar la siguiente linea - #xlabel = "Frequency x 10000" - ylabel = "Intensity (dB)" - - if not self.__isConfig: - nplots = len(channelIndexList) - - self.setup(id=id, - nplots=nplots, - wintitle=wintitle, - show=show) - - if xmin == None: xmin = numpy.nanmin(x) - if xmax == None: xmax = numpy.nanmax(x) - if ymin == None: ymin = numpy.nanmin(y) - if ymax == None: ymax = numpy.nanmax(y) - - self.__isConfig = True - - self.setWinTitle(title) - - for i in range(len(self.axesList)): - ychannel = y[i,:] - str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S")) - title = "Channel %d: %4.2fdB: %s" %(i, numpy.max(ychannel), str_datetime) - axes = self.axesList[i] - axes.pline(x, ychannel, - xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, - xlabel=xlabel, ylabel=ylabel, title=title, grid='both') - - - self.draw() - - if save: - date = thisDatetime.strftime("%Y%m%d_%H%M%S") - if figfile == None: - figfile = self.getFilename(name = date) - - self.saveFigure(figpath, figfile) - - self.counter_imagwr += 1 - if (ftp and (self.counter_imagwr==wr_period)): - ftp_filename = os.path.join(figpath,figfile) - self.sendByFTP_Thread(ftp_filename, server, folder, username, password) - self.counter_imagwr = 0 - - -class RTIfromSpectraHeis(Figure): - - __isConfig = None - __nsubplots = None - - PREFIX = 'rtinoise' - - def __init__(self): - - self.timerange = 24*60*60 - self.__isConfig = False - self.__nsubplots = 1 - - self.WIDTH = 820 - self.HEIGHT = 200 - self.WIDTHPROF = 120 - self.HEIGHTPROF = 0 - self.counter_imagwr = 0 - self.xdata = None - self.ydata = None - - def getSubplots(self): - - ncol = 1 - nrow = 1 - - return nrow, ncol - - def setup(self, id, nplots, wintitle, showprofile=True, show=True): - - self.__showprofile = showprofile - self.nplots = nplots - - ncolspan = 7 - colspan = 6 - self.__nsubplots = 2 - - self.createFigure(id = id, - wintitle = wintitle, - widthplot = self.WIDTH+self.WIDTHPROF, - heightplot = self.HEIGHT+self.HEIGHTPROF, - show = show) - - nrow, ncol = self.getSubplots() - - self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1) - - - def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True', - xmin=None, xmax=None, ymin=None, ymax=None, - timerange=None, - save=False, figpath='./', figfile=None, ftp=False, wr_period=1, show=True, - server=None, folder=None, username=None, password=None): - - if channelList == None: - channelIndexList = dataOut.channelIndexList - channelList = dataOut.channelList - else: - channelIndexList = [] - for channel in channelList: - if channel not in dataOut.channelList: - raise ValueError, "Channel %d is not in dataOut.channelList" - channelIndexList.append(dataOut.channelList.index(channel)) - - if timerange != None: - self.timerange = timerange - - tmin = None - tmax = None - x = dataOut.getTimeRange() - y = dataOut.getHeiRange() - - #factor = 1 - data = dataOut.data_spc#/factor - data = numpy.average(data,axis=1) - datadB = 10*numpy.log10(data) - -# factor = dataOut.normFactor -# noise = dataOut.getNoise()/factor -# noisedB = 10*numpy.log10(noise) - - #thisDatetime = dataOut.datatime - thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) - title = wintitle + " RTI: %s" %(thisDatetime.strftime("%d-%b-%Y")) - xlabel = "Local Time" - ylabel = "Intensity (dB)" - - if not self.__isConfig: - - nplots = 1 - - self.setup(id=id, - nplots=nplots, - wintitle=wintitle, - showprofile=showprofile, - show=show) - - tmin, tmax = self.getTimeLim(x, xmin, xmax) - if ymin == None: ymin = numpy.nanmin(datadB) - if ymax == None: ymax = numpy.nanmax(datadB) - - self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") - self.__isConfig = True - - self.xdata = numpy.array([]) - self.ydata = numpy.array([]) - - self.setWinTitle(title) - - -# title = "RTI %s" %(thisDatetime.strftime("%d-%b-%Y")) - title = "RTI - %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) - - legendlabels = ["channel %d"%idchannel for idchannel in channelList] - axes = self.axesList[0] - - self.xdata = numpy.hstack((self.xdata, x[0:1])) - - if len(self.ydata)==0: - self.ydata = datadB[channelIndexList].reshape(-1,1) - else: - self.ydata = numpy.hstack((self.ydata, datadB[channelIndexList].reshape(-1,1))) - - - axes.pmultilineyaxis(x=self.xdata, y=self.ydata, - xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, - xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='.', markersize=8, linestyle="solid", grid='both', - XAxisAsTime=True - ) - - self.draw() - - if save: - - if figfile == None: - figfile = self.getFilename(name = self.name) - - self.saveFigure(figpath, figfile) - - self.counter_imagwr += 1 - if (ftp and (self.counter_imagwr==wr_period)): - ftp_filename = os.path.join(figpath,figfile) - self.sendByFTP_Thread(ftp_filename, server, folder, username, password) - self.counter_imagwr = 0 - - if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: - self.__isConfig = False - del self.xdata - del self.ydata - - - \ No newline at end of file diff --git a/schainpy/model/jroprocessing.py b/schainpy/model/jroprocessing.py deleted file mode 100644 index ec36258..0000000 --- a/schainpy/model/jroprocessing.py +++ /dev/null @@ -1,2150 +0,0 @@ -''' - -$Author: dsuarez $ -$Id: Processor.py 1 2012-11-12 18:56:07Z dsuarez $ -''' -import os -import numpy -import datetime -import time -import math -from jrodata import * -from jrodataIO import * -from jroplot import * - -try: - import cfunctions -except: - pass - -class ProcessingUnit: - - """ - Esta es la clase base para el procesamiento de datos. - - Contiene el metodo "call" para llamar operaciones. Las operaciones pueden ser: - - Metodos internos (callMethod) - - Objetos del tipo Operation (callObject). Antes de ser llamados, estos objetos - tienen que ser agreagados con el metodo "add". - - """ - # objeto de datos de entrada (Voltage, Spectra o Correlation) - dataIn = None - - # objeto de datos de entrada (Voltage, Spectra o Correlation) - dataOut = None - - - objectDict = None - - def __init__(self): - - self.objectDict = {} - - def init(self): - - raise ValueError, "Not implemented" - - def addOperation(self, object, objId): - - """ - Agrega el objeto "object" a la lista de objetos "self.objectList" y retorna el - identificador asociado a este objeto. - - Input: - - object : objeto de la clase "Operation" - - Return: - - objId : identificador del objeto, necesario para ejecutar la operacion - """ - - self.objectDict[objId] = object - - return objId - - def operation(self, **kwargs): - - """ - Operacion directa sobre la data (dataOut.data). Es necesario actualizar los valores de los - atributos del objeto dataOut - - Input: - - **kwargs : Diccionario de argumentos de la funcion a ejecutar - """ - - raise ValueError, "ImplementedError" - - def callMethod(self, name, **kwargs): - - """ - Ejecuta el metodo con el nombre "name" y con argumentos **kwargs de la propia clase. - - Input: - name : nombre del metodo a ejecutar - - **kwargs : diccionario con los nombres y valores de la funcion a ejecutar. - - """ - if name != 'run': - - if name == 'init' and self.dataIn.isEmpty(): - self.dataOut.flagNoData = True - return False - - if name != 'init' and self.dataOut.isEmpty(): - return False - - methodToCall = getattr(self, name) - - methodToCall(**kwargs) - - if name != 'run': - return True - - if self.dataOut.isEmpty(): - return False - - return True - - def callObject(self, objId, **kwargs): - - """ - Ejecuta la operacion asociada al identificador del objeto "objId" - - Input: - - objId : identificador del objeto a ejecutar - - **kwargs : diccionario con los nombres y valores de la funcion a ejecutar. - - Return: - - None - """ - - if self.dataOut.isEmpty(): - return False - - object = self.objectDict[objId] - - object.run(self.dataOut, **kwargs) - - return True - - def call(self, operationConf, **kwargs): - - """ - Return True si ejecuta la operacion "operationConf.name" con los - argumentos "**kwargs". False si la operacion no se ha ejecutado. - La operacion puede ser de dos tipos: - - 1. Un metodo propio de esta clase: - - operation.type = "self" - - 2. El metodo "run" de un objeto del tipo Operation o de un derivado de ella: - operation.type = "other". - - Este objeto de tipo Operation debe de haber sido agregado antes con el metodo: - "addOperation" e identificado con el operation.id - - - con el id de la operacion. - - Input: - - Operation : Objeto del tipo operacion con los atributos: name, type y id. - - """ - - if operationConf.type == 'self': - sts = self.callMethod(operationConf.name, **kwargs) - - if operationConf.type == 'other': - sts = self.callObject(operationConf.id, **kwargs) - - return sts - - def setInput(self, dataIn): - - self.dataIn = dataIn - - def getOutput(self): - - return self.dataOut - -class Operation(): - - """ - Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit - y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de - acumulacion dentro de esta clase - - Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer) - - """ - - __buffer = None - __isConfig = False - - def __init__(self): - - pass - - def run(self, dataIn, **kwargs): - - """ - Realiza las operaciones necesarias sobre la dataIn.data y actualiza los atributos del objeto dataIn. - - Input: - - dataIn : objeto del tipo JROData - - Return: - - None - - Affected: - __buffer : buffer de recepcion de datos. - - """ - - raise ValueError, "ImplementedError" - -class VoltageProc(ProcessingUnit): - - - def __init__(self): - - self.objectDict = {} - self.dataOut = Voltage() - self.flip = 1 - - def __updateObjFromAmisrInput(self): - - self.dataOut.timeZone = self.dataIn.timeZone - self.dataOut.dstFlag = self.dataIn.dstFlag - self.dataOut.errorCount = self.dataIn.errorCount - self.dataOut.useLocalTime = self.dataIn.useLocalTime - - self.dataOut.flagNoData = self.dataIn.flagNoData - self.dataOut.data = self.dataIn.data - self.dataOut.utctime = self.dataIn.utctime - self.dataOut.channelList = self.dataIn.channelList - self.dataOut.timeInterval = self.dataIn.timeInterval - self.dataOut.heightList = self.dataIn.heightList - self.dataOut.nProfiles = self.dataIn.nProfiles - - self.dataOut.nCohInt = self.dataIn.nCohInt - self.dataOut.ippSeconds = self.dataIn.ippSeconds - self.dataOut.frequency = self.dataIn.frequency - - pass - - def init(self): - - - if self.dataIn.type == 'AMISR': - self.__updateObjFromAmisrInput() - - if self.dataIn.type == 'Voltage': - self.dataOut.copy(self.dataIn) - # No necesita copiar en cada init() los atributos de dataIn - # la copia deberia hacerse por cada nuevo bloque de datos - - def selectChannels(self, channelList): - - channelIndexList = [] - - for channel in channelList: - index = self.dataOut.channelList.index(channel) - channelIndexList.append(index) - - self.selectChannelsByIndex(channelIndexList) - - def selectChannelsByIndex(self, channelIndexList): - """ - Selecciona un bloque de datos en base a canales segun el channelIndexList - - Input: - channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7] - - Affected: - self.dataOut.data - self.dataOut.channelIndexList - self.dataOut.nChannels - self.dataOut.m_ProcessingHeader.totalSpectra - self.dataOut.systemHeaderObj.numChannels - self.dataOut.m_ProcessingHeader.blockSize - - Return: - None - """ - - for channelIndex in channelIndexList: - if channelIndex not in self.dataOut.channelIndexList: - print channelIndexList - raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex - - nChannels = len(channelIndexList) - - data = self.dataOut.data[channelIndexList,:] - - self.dataOut.data = data - self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList] -# self.dataOut.nChannels = nChannels - - return 1 - - def selectHeights(self, minHei=None, maxHei=None): - """ - Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango - minHei <= height <= maxHei - - Input: - minHei : valor minimo de altura a considerar - maxHei : valor maximo de altura a considerar - - Affected: - Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex - - Return: - 1 si el metodo se ejecuto con exito caso contrario devuelve 0 - """ - - if minHei == None: - minHei = self.dataOut.heightList[0] - - if maxHei == None: - maxHei = self.dataOut.heightList[-1] - - if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei): - raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) - - - if (maxHei > self.dataOut.heightList[-1]): - maxHei = self.dataOut.heightList[-1] -# raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) - - minIndex = 0 - maxIndex = 0 - heights = self.dataOut.heightList - - inda = numpy.where(heights >= minHei) - indb = numpy.where(heights <= maxHei) - - try: - minIndex = inda[0][0] - except: - minIndex = 0 - - try: - maxIndex = indb[0][-1] - except: - maxIndex = len(heights) - - self.selectHeightsByIndex(minIndex, maxIndex) - - return 1 - - - def selectHeightsByIndex(self, minIndex, maxIndex): - """ - Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango - minIndex <= index <= maxIndex - - Input: - minIndex : valor de indice minimo de altura a considerar - maxIndex : valor de indice maximo de altura a considerar - - Affected: - self.dataOut.data - self.dataOut.heightList - - Return: - 1 si el metodo se ejecuto con exito caso contrario devuelve 0 - """ - - if (minIndex < 0) or (minIndex > maxIndex): - raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) - - if (maxIndex >= self.dataOut.nHeights): - maxIndex = self.dataOut.nHeights-1 -# raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) - - nHeights = maxIndex - minIndex + 1 - - #voltage - data = self.dataOut.data[:,minIndex:maxIndex+1] - - firstHeight = self.dataOut.heightList[minIndex] - - self.dataOut.data = data - self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex+1] - - return 1 - - - def filterByHeights(self, window): - deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] - - if window == None: - window = (self.dataOut.radarControllerHeaderObj.txA/self.dataOut.radarControllerHeaderObj.nBaud) / deltaHeight - - newdelta = deltaHeight * window - r = self.dataOut.data.shape[1] % window - buffer = self.dataOut.data[:,0:self.dataOut.data.shape[1]-r] - buffer = buffer.reshape(self.dataOut.data.shape[0],self.dataOut.data.shape[1]/window,window) - buffer = numpy.sum(buffer,2) - self.dataOut.data = buffer - self.dataOut.heightList = numpy.arange(self.dataOut.heightList[0],newdelta*(self.dataOut.nHeights-r)/window,newdelta) - self.dataOut.windowOfFilter = window - - def deFlip(self): - self.dataOut.data *= self.flip - self.flip *= -1. - - def setRadarFrequency(self, frequency=None): - if frequency != None: - self.dataOut.frequency = frequency - - return 1 - -class CohInt(Operation): - - __isConfig = False - - __profIndex = 0 - __withOverapping = False - - __byTime = False - __initime = None - __lastdatatime = None - __integrationtime = None - - __buffer = None - - __dataReady = False - - n = None - - - def __init__(self): - - self.__isConfig = False - - def setup(self, n=None, timeInterval=None, overlapping=False): - """ - Set the parameters of the integration class. - - Inputs: - - n : Number of coherent integrations - timeInterval : Time of integration. If the parameter "n" is selected this one does not work - overlapping : - - """ - - self.__initime = None - self.__lastdatatime = 0 - self.__buffer = None - self.__dataReady = False - - - if n == None and timeInterval == None: - raise ValueError, "n or timeInterval should be specified ..." - - if n != None: - self.n = n - self.__byTime = False - else: - self.__integrationtime = timeInterval * 60. #if (type(timeInterval)!=integer) -> change this line - self.n = 9999 - self.__byTime = True - - if overlapping: - self.__withOverapping = True - self.__buffer = None - else: - self.__withOverapping = False - self.__buffer = 0 - - self.__profIndex = 0 - - def putData(self, data): - - """ - Add a profile to the __buffer and increase in one the __profileIndex - - """ - - if not self.__withOverapping: - self.__buffer += data.copy() - self.__profIndex += 1 - return - - #Overlapping data - nChannels, nHeis = data.shape - data = numpy.reshape(data, (1, nChannels, nHeis)) - - #If the buffer is empty then it takes the data value - if self.__buffer == None: - self.__buffer = data - self.__profIndex += 1 - return - - #If the buffer length is lower than n then stakcing the data value - if self.__profIndex < self.n: - self.__buffer = numpy.vstack((self.__buffer, data)) - self.__profIndex += 1 - return - - #If the buffer length is equal to n then replacing the last buffer value with the data value - self.__buffer = numpy.roll(self.__buffer, -1, axis=0) - self.__buffer[self.n-1] = data - self.__profIndex = self.n - return - - - def pushData(self): - """ - Return the sum of the last profiles and the profiles used in the sum. - - Affected: - - self.__profileIndex - - """ - - if not self.__withOverapping: - data = self.__buffer - n = self.__profIndex - - self.__buffer = 0 - self.__profIndex = 0 - - return data, n - - #Integration with Overlapping - data = numpy.sum(self.__buffer, axis=0) - n = self.__profIndex - - return data, n - - def byProfiles(self, data): - - self.__dataReady = False - avgdata = None - n = None - - self.putData(data) - - if self.__profIndex == self.n: - - avgdata, n = self.pushData() - self.__dataReady = True - - return avgdata - - def byTime(self, data, datatime): - - self.__dataReady = False - avgdata = None - n = None - - self.putData(data) - - if (datatime - self.__initime) >= self.__integrationtime: - avgdata, n = self.pushData() - self.n = n - self.__dataReady = True - - return avgdata - - def integrate(self, data, datatime=None): - - if self.__initime == None: - self.__initime = datatime - - if self.__byTime: - avgdata = self.byTime(data, datatime) - else: - avgdata = self.byProfiles(data) - - - self.__lastdatatime = datatime - - if avgdata == None: - return None, None - - avgdatatime = self.__initime - - deltatime = datatime -self.__lastdatatime - - if not self.__withOverapping: - self.__initime = datatime - else: - self.__initime += deltatime - - return avgdata, avgdatatime - - def run(self, dataOut, **kwargs): - - if not self.__isConfig: - self.setup(**kwargs) - self.__isConfig = True - - avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime) - -# dataOut.timeInterval *= n - dataOut.flagNoData = True - - if self.__dataReady: - dataOut.data = avgdata - dataOut.nCohInt *= self.n - dataOut.utctime = avgdatatime - dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt - dataOut.flagNoData = False - - -class Decoder(Operation): - - __isConfig = False - __profIndex = 0 - - code = None - - nCode = None - nBaud = None - - def __init__(self): - - self.__isConfig = False - - def setup(self, code, shape): - - self.__profIndex = 0 - - self.code = code - - self.nCode = len(code) - self.nBaud = len(code[0]) - - self.__nChannels, self.__nHeis = shape - - __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex) - - __codeBuffer[:,0:self.nBaud] = self.code - - self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1)) - - self.ndatadec = self.__nHeis - self.nBaud + 1 - - self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex) - - def convolutionInFreq(self, data): - - fft_code = self.fft_code[self.__profIndex].reshape(1,-1) - - fft_data = numpy.fft.fft(data, axis=1) - - conv = fft_data*fft_code - - data = numpy.fft.ifft(conv,axis=1) - - datadec = data[:,:-self.nBaud+1] - - return datadec - - def convolutionInFreqOpt(self, data): - - fft_code = self.fft_code[self.__profIndex].reshape(1,-1) - - data = cfunctions.decoder(fft_code, data) - - datadec = data[:,:-self.nBaud+1] - - return datadec - - def convolutionInTime(self, data): - - code = self.code[self.__profIndex] - - for i in range(self.__nChannels): - self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='valid') - - return self.datadecTime - - def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0): - - if code == None: - code = dataOut.code - else: - code = numpy.array(code).reshape(nCode,nBaud) - dataOut.code = code - dataOut.nCode = nCode - dataOut.nBaud = nBaud - dataOut.radarControllerHeaderObj.code = code - dataOut.radarControllerHeaderObj.nCode = nCode - dataOut.radarControllerHeaderObj.nBaud = nBaud - - - if not self.__isConfig: - - self.setup(code, dataOut.data.shape) - self.__isConfig = True - - if mode == 0: - datadec = self.convolutionInTime(dataOut.data) - - if mode == 1: - datadec = self.convolutionInFreq(dataOut.data) - - if mode == 2: - datadec = self.convolutionInFreqOpt(dataOut.data) - - dataOut.data = datadec - - dataOut.heightList = dataOut.heightList[0:self.ndatadec] - - dataOut.flagDecodeData = True #asumo q la data no esta decodificada - - if self.__profIndex == self.nCode-1: - self.__profIndex = 0 - return 1 - - self.__profIndex += 1 - - return 1 -# dataOut.flagDeflipData = True #asumo q la data no esta sin flip - - - -class SpectraProc(ProcessingUnit): - - def __init__(self): - - self.objectDict = {} - self.buffer = None - self.firstdatatime = None - self.profIndex = 0 - self.dataOut = Spectra() - - def __updateObjFromInput(self): - - self.dataOut.timeZone = self.dataIn.timeZone - self.dataOut.dstFlag = self.dataIn.dstFlag - self.dataOut.errorCount = self.dataIn.errorCount - self.dataOut.useLocalTime = self.dataIn.useLocalTime - - self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy() - self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy() - self.dataOut.channelList = self.dataIn.channelList - self.dataOut.heightList = self.dataIn.heightList - self.dataOut.dtype = numpy.dtype([('real',' maxHei): - raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) - - if (maxHei > self.dataOut.heightList[-1]): - maxHei = self.dataOut.heightList[-1] -# raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) - - minIndex = 0 - maxIndex = 0 - heights = self.dataOut.heightList - - inda = numpy.where(heights >= minHei) - indb = numpy.where(heights <= maxHei) - - try: - minIndex = inda[0][0] - except: - minIndex = 0 - - try: - maxIndex = indb[0][-1] - except: - maxIndex = len(heights) - - self.selectHeightsByIndex(minIndex, maxIndex) - - return 1 - - def getBeaconSignal(self, tauindex = 0, channelindex = 0, hei_ref=None): - newheis = numpy.where(self.dataOut.heightList>self.dataOut.radarControllerHeaderObj.Taus[tauindex]) - - if hei_ref != None: - newheis = numpy.where(self.dataOut.heightList>hei_ref) - - minIndex = min(newheis[0]) - maxIndex = max(newheis[0]) - data_spc = self.dataOut.data_spc[:,:,minIndex:maxIndex+1] - heightList = self.dataOut.heightList[minIndex:maxIndex+1] - - # determina indices - 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)) - 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)) - - #data_spc = data_spc[:,:,beacon_heiIndexList] - data_cspc = None - if self.dataOut.data_cspc != None: - data_cspc = self.dataOut.data_cspc[:,:,minIndex:maxIndex+1] - #data_cspc = data_cspc[:,:,beacon_heiIndexList] - - data_dc = None - if self.dataOut.data_dc != None: - data_dc = self.dataOut.data_dc[:,minIndex:maxIndex+1] - #data_dc = data_dc[:,beacon_heiIndexList] - - 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 - - return 1 - - - def selectHeightsByIndex(self, minIndex, maxIndex): - """ - Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango - minIndex <= index <= maxIndex - - Input: - minIndex : valor de indice minimo de altura a considerar - maxIndex : valor de indice maximo de altura a considerar - - Affected: - self.dataOut.data_spc - self.dataOut.data_cspc - self.dataOut.data_dc - self.dataOut.heightList - - Return: - 1 si el metodo se ejecuto con exito caso contrario devuelve 0 - """ - - if (minIndex < 0) or (minIndex > maxIndex): - raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) - - if (maxIndex >= self.dataOut.nHeights): - maxIndex = self.dataOut.nHeights-1 -# raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) - - nHeights = maxIndex - minIndex + 1 - - #Spectra - data_spc = self.dataOut.data_spc[:,:,minIndex:maxIndex+1] - - data_cspc = None - if self.dataOut.data_cspc != None: - data_cspc = self.dataOut.data_cspc[:,:,minIndex:maxIndex+1] - - data_dc = None - if self.dataOut.data_dc != None: - data_dc = self.dataOut.data_dc[:,minIndex:maxIndex+1] - - self.dataOut.data_spc = data_spc - self.dataOut.data_cspc = data_cspc - self.dataOut.data_dc = data_dc - - self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex+1] - - return 1 - - def removeDC(self, mode = 2): - jspectra = self.dataOut.data_spc - jcspectra = self.dataOut.data_cspc - - - num_chan = jspectra.shape[0] - num_hei = jspectra.shape[2] - - if jcspectra != None: - jcspectraExist = True - num_pairs = jcspectra.shape[0] - else: jcspectraExist = False - - freq_dc = jspectra.shape[1]/2 - ind_vel = numpy.array([-2,-1,1,2]) + freq_dc - - if ind_vel[0]<0: - ind_vel[range(0,1)] = ind_vel[range(0,1)] + self.num_prof - - if mode == 1: - jspectra[:,freq_dc,:] = (jspectra[:,ind_vel[1],:] + jspectra[:,ind_vel[2],:])/2 #CORRECCION - - if jcspectraExist: - jcspectra[:,freq_dc,:] = (jcspectra[:,ind_vel[1],:] + jcspectra[:,ind_vel[2],:])/2 - - if mode == 2: - - vel = numpy.array([-2,-1,1,2]) - xx = numpy.zeros([4,4]) - - for fil in range(4): - xx[fil,:] = vel[fil]**numpy.asarray(range(4)) - - xx_inv = numpy.linalg.inv(xx) - xx_aux = xx_inv[0,:] - - for ich in range(num_chan): - yy = jspectra[ich,ind_vel,:] - jspectra[ich,freq_dc,:] = numpy.dot(xx_aux,yy) - - junkid = jspectra[ich,freq_dc,:]<=0 - cjunkid = sum(junkid) - - if cjunkid.any(): - jspectra[ich,freq_dc,junkid.nonzero()] = (jspectra[ich,ind_vel[1],junkid] + jspectra[ich,ind_vel[2],junkid])/2 - - if jcspectraExist: - for ip in range(num_pairs): - yy = jcspectra[ip,ind_vel,:] - jcspectra[ip,freq_dc,:] = numpy.dot(xx_aux,yy) - - - self.dataOut.data_spc = jspectra - self.dataOut.data_cspc = jcspectra - - return 1 - - def removeInterference(self, interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None): - - jspectra = self.dataOut.data_spc - jcspectra = self.dataOut.data_cspc - jnoise = self.dataOut.getNoise() - num_incoh = self.dataOut.nIncohInt - - num_channel = jspectra.shape[0] - num_prof = jspectra.shape[1] - num_hei = jspectra.shape[2] - - #hei_interf - if hei_interf == None: - count_hei = num_hei/2 #Como es entero no importa - hei_interf = numpy.asmatrix(range(count_hei)) + num_hei - count_hei - hei_interf = numpy.asarray(hei_interf)[0] - #nhei_interf - if (nhei_interf == None): - nhei_interf = 5 - if (nhei_interf < 1): - nhei_interf = 1 - if (nhei_interf > count_hei): - nhei_interf = count_hei - if (offhei_interf == None): - offhei_interf = 0 - - ind_hei = range(num_hei) -# mask_prof = numpy.asarray(range(num_prof - 2)) + 1 -# mask_prof[range(num_prof/2 - 1,len(mask_prof))] += 1 - mask_prof = numpy.asarray(range(num_prof)) - num_mask_prof = mask_prof.size - comp_mask_prof = [0, num_prof/2] - - - #noise_exist: Determina si la variable jnoise ha sido definida y contiene la informacion del ruido de cada canal - if (jnoise.size < num_channel or numpy.isnan(jnoise).any()): - jnoise = numpy.nan - noise_exist = jnoise[0] < numpy.Inf - - #Subrutina de Remocion de la Interferencia - for ich in range(num_channel): - #Se ordena los espectros segun su potencia (menor a mayor) - power = jspectra[ich,mask_prof,:] - power = power[:,hei_interf] - power = power.sum(axis = 0) - psort = power.ravel().argsort() - - #Se estima la interferencia promedio en los Espectros de Potencia empleando - junkspc_interf = jspectra[ich,:,hei_interf[psort[range(offhei_interf, nhei_interf + offhei_interf)]]] - - if noise_exist: - # tmp_noise = jnoise[ich] / num_prof - tmp_noise = jnoise[ich] - junkspc_interf = junkspc_interf - tmp_noise - #junkspc_interf[:,comp_mask_prof] = 0 - - jspc_interf = junkspc_interf.sum(axis = 0) / nhei_interf - jspc_interf = jspc_interf.transpose() - #Calculando el espectro de interferencia promedio - noiseid = numpy.where(jspc_interf <= tmp_noise/ math.sqrt(num_incoh)) - noiseid = noiseid[0] - cnoiseid = noiseid.size - interfid = numpy.where(jspc_interf > tmp_noise/ math.sqrt(num_incoh)) - interfid = interfid[0] - cinterfid = interfid.size - - if (cnoiseid > 0): jspc_interf[noiseid] = 0 - - #Expandiendo los perfiles a limpiar - if (cinterfid > 0): - new_interfid = (numpy.r_[interfid - 1, interfid, interfid + 1] + num_prof)%num_prof - new_interfid = numpy.asarray(new_interfid) - new_interfid = {x for x in new_interfid} - new_interfid = numpy.array(list(new_interfid)) - new_cinterfid = new_interfid.size - else: new_cinterfid = 0 - - for ip in range(new_cinterfid): - ind = junkspc_interf[:,new_interfid[ip]].ravel().argsort() - jspc_interf[new_interfid[ip]] = junkspc_interf[ind[nhei_interf/2],new_interfid[ip]] - - - jspectra[ich,:,ind_hei] = jspectra[ich,:,ind_hei] - jspc_interf #Corregir indices - - #Removiendo la interferencia del punto de mayor interferencia - ListAux = jspc_interf[mask_prof].tolist() - maxid = ListAux.index(max(ListAux)) - - - if cinterfid > 0: - for ip in range(cinterfid*(interf == 2) - 1): - ind = (jspectra[ich,interfid[ip],:] < tmp_noise*(1 + 1/math.sqrt(num_incoh))).nonzero() - cind = len(ind) - - if (cind > 0): - jspectra[ich,interfid[ip],ind] = tmp_noise*(1 + (numpy.random.uniform(cind) - 0.5)/math.sqrt(num_incoh)) - - ind = numpy.array([-2,-1,1,2]) - xx = numpy.zeros([4,4]) - - for id1 in range(4): - xx[:,id1] = ind[id1]**numpy.asarray(range(4)) - - xx_inv = numpy.linalg.inv(xx) - 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) - - - indAux = (jspectra[ich,:,:] < tmp_noise*(1-1/math.sqrt(num_incoh))).nonzero() - jspectra[ich,indAux[0],indAux[1]] = tmp_noise * (1 - 1/math.sqrt(num_incoh)) - - #Remocion de Interferencia en el Cross Spectra - if jcspectra == None: return jspectra, jcspectra - num_pairs = jcspectra.size/(num_prof*num_hei) - jcspectra = jcspectra.reshape(num_pairs, num_prof, num_hei) - - for ip in range(num_pairs): - - #------------------------------------------- - - cspower = numpy.abs(jcspectra[ip,mask_prof,:]) - cspower = cspower[:,hei_interf] - cspower = cspower.sum(axis = 0) - - cspsort = cspower.ravel().argsort() - junkcspc_interf = jcspectra[ip,:,hei_interf[cspsort[range(offhei_interf, nhei_interf + offhei_interf)]]] - junkcspc_interf = junkcspc_interf.transpose() - jcspc_interf = junkcspc_interf.sum(axis = 1)/nhei_interf - - ind = numpy.abs(jcspc_interf[mask_prof]).ravel().argsort() - - median_real = numpy.median(numpy.real(junkcspc_interf[mask_prof[ind[range(3*num_prof/4)]],:])) - median_imag = numpy.median(numpy.imag(junkcspc_interf[mask_prof[ind[range(3*num_prof/4)]],:])) - junkcspc_interf[comp_mask_prof,:] = numpy.complex(median_real, median_imag) - - for iprof in range(num_prof): - ind = numpy.abs(junkcspc_interf[iprof,:]).ravel().argsort() - jcspc_interf[iprof] = junkcspc_interf[iprof, ind[nhei_interf/2]] - - #Removiendo la Interferencia - jcspectra[ip,:,ind_hei] = jcspectra[ip,:,ind_hei] - jcspc_interf - - ListAux = numpy.abs(jcspc_interf[mask_prof]).tolist() - maxid = ListAux.index(max(ListAux)) - - ind = numpy.array([-2,-1,1,2]) - xx = numpy.zeros([4,4]) - - for id1 in range(4): - xx[:,id1] = ind[id1]**numpy.asarray(range(4)) - - xx_inv = numpy.linalg.inv(xx) - xx = xx_inv[:,0] - - 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) - - #Guardar Resultados - self.dataOut.data_spc = jspectra - self.dataOut.data_cspc = jcspectra - - return 1 - - def setRadarFrequency(self, frequency=None): - if frequency != None: - self.dataOut.frequency = frequency - - return 1 - - def getNoise(self, minHei=None, maxHei=None, minVel=None, maxVel=None): - #validacion de rango - if minHei == None: - minHei = self.dataOut.heightList[0] - - if maxHei == None: - maxHei = self.dataOut.heightList[-1] - - if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei): - print 'minHei: %.2f is out of the heights range'%(minHei) - print 'minHei is setting to %.2f'%(self.dataOut.heightList[0]) - minHei = self.dataOut.heightList[0] - - if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei): - print 'maxHei: %.2f is out of the heights range'%(maxHei) - print 'maxHei is setting to %.2f'%(self.dataOut.heightList[-1]) - maxHei = self.dataOut.heightList[-1] - - # validacion de velocidades - velrange = self.dataOut.getVelRange(1) - - if minVel == None: - minVel = velrange[0] - - if maxVel == None: - maxVel = velrange[-1] - - if (minVel < velrange[0]) or (minVel > maxVel): - print 'minVel: %.2f is out of the velocity range'%(minVel) - print 'minVel is setting to %.2f'%(velrange[0]) - minVel = velrange[0] - - if (maxVel > velrange[-1]) or (maxVel < minVel): - print 'maxVel: %.2f is out of the velocity range'%(maxVel) - print 'maxVel is setting to %.2f'%(velrange[-1]) - maxVel = velrange[-1] - - # seleccion de indices para rango - minIndex = 0 - maxIndex = 0 - heights = self.dataOut.heightList - - inda = numpy.where(heights >= minHei) - indb = numpy.where(heights <= maxHei) - - try: - minIndex = inda[0][0] - except: - minIndex = 0 - - try: - maxIndex = indb[0][-1] - except: - maxIndex = len(heights) - - if (minIndex < 0) or (minIndex > maxIndex): - raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) - - if (maxIndex >= self.dataOut.nHeights): - maxIndex = self.dataOut.nHeights-1 - - # seleccion de indices para velocidades - indminvel = numpy.where(velrange >= minVel) - indmaxvel = numpy.where(velrange <= maxVel) - try: - minIndexVel = indminvel[0][0] - except: - minIndexVel = 0 - - try: - maxIndexVel = indmaxvel[0][-1] - except: - maxIndexVel = len(velrange) - - #seleccion del espectro - data_spc = self.dataOut.data_spc[:,minIndexVel:maxIndexVel+1,minIndex:maxIndex+1] - #estimacion de ruido - noise = numpy.zeros(self.dataOut.nChannels) - - for channel in range(self.dataOut.nChannels): - daux = data_spc[channel,:,:] - noise[channel] = hildebrand_sekhon(daux, self.dataOut.nIncohInt) - - self.dataOut.noise = noise.copy() - - return 1 - - -class IncohInt(Operation): - - - __profIndex = 0 - __withOverapping = False - - __byTime = False - __initime = None - __lastdatatime = None - __integrationtime = None - - __buffer_spc = None - __buffer_cspc = None - __buffer_dc = None - - __dataReady = False - - __timeInterval = None - - n = None - - - - def __init__(self): - - self.__isConfig = False - - def setup(self, n=None, timeInterval=None, overlapping=False): - """ - Set the parameters of the integration class. - - Inputs: - - n : Number of coherent integrations - timeInterval : Time of integration. If the parameter "n" is selected this one does not work - overlapping : - - """ - - self.__initime = None - self.__lastdatatime = 0 - self.__buffer_spc = None - self.__buffer_cspc = None - self.__buffer_dc = None - self.__dataReady = False - - - if n == None and timeInterval == None: - raise ValueError, "n or timeInterval should be specified ..." - - if n != None: - self.n = n - self.__byTime = False - else: - self.__integrationtime = timeInterval #if (type(timeInterval)!=integer) -> change this line - self.n = 9999 - self.__byTime = True - - if overlapping: - self.__withOverapping = True - else: - self.__withOverapping = False - self.__buffer_spc = 0 - self.__buffer_cspc = 0 - self.__buffer_dc = 0 - - self.__profIndex = 0 - - def putData(self, data_spc, data_cspc, data_dc): - - """ - Add a profile to the __buffer_spc and increase in one the __profileIndex - - """ - - if not self.__withOverapping: - self.__buffer_spc += data_spc - - if data_cspc == None: - self.__buffer_cspc = None - else: - self.__buffer_cspc += data_cspc - - if data_dc == None: - self.__buffer_dc = None - else: - self.__buffer_dc += data_dc - - self.__profIndex += 1 - return - - #Overlapping data - nChannels, nFFTPoints, nHeis = data_spc.shape - data_spc = numpy.reshape(data_spc, (1, nChannels, nFFTPoints, nHeis)) - if data_cspc != None: - data_cspc = numpy.reshape(data_cspc, (1, -1, nFFTPoints, nHeis)) - if data_dc != None: - data_dc = numpy.reshape(data_dc, (1, -1, nHeis)) - - #If the buffer is empty then it takes the data value - if self.__buffer_spc == None: - self.__buffer_spc = data_spc - - if data_cspc == None: - self.__buffer_cspc = None - else: - self.__buffer_cspc += data_cspc - - if data_dc == None: - self.__buffer_dc = None - else: - self.__buffer_dc += data_dc - - self.__profIndex += 1 - return - - #If the buffer length is lower than n then stakcing the data value - if self.__profIndex < self.n: - self.__buffer_spc = numpy.vstack((self.__buffer_spc, data_spc)) - - if data_cspc != None: - self.__buffer_cspc = numpy.vstack((self.__buffer_cspc, data_cspc)) - - if data_dc != None: - self.__buffer_dc = numpy.vstack((self.__buffer_dc, data_dc)) - - self.__profIndex += 1 - return - - #If the buffer length is equal to n then replacing the last buffer value with the data value - self.__buffer_spc = numpy.roll(self.__buffer_spc, -1, axis=0) - self.__buffer_spc[self.n-1] = data_spc - - if data_cspc != None: - self.__buffer_cspc = numpy.roll(self.__buffer_cspc, -1, axis=0) - self.__buffer_cspc[self.n-1] = data_cspc - - if data_dc != None: - self.__buffer_dc = numpy.roll(self.__buffer_dc, -1, axis=0) - self.__buffer_dc[self.n-1] = data_dc - - self.__profIndex = self.n - return - - - def pushData(self): - """ - Return the sum of the last profiles and the profiles used in the sum. - - Affected: - - self.__profileIndex - - """ - data_spc = None - data_cspc = None - data_dc = None - - if not self.__withOverapping: - data_spc = self.__buffer_spc - data_cspc = self.__buffer_cspc - data_dc = self.__buffer_dc - - n = self.__profIndex - - self.__buffer_spc = 0 - self.__buffer_cspc = 0 - self.__buffer_dc = 0 - self.__profIndex = 0 - - return data_spc, data_cspc, data_dc, n - - #Integration with Overlapping - data_spc = numpy.sum(self.__buffer_spc, axis=0) - - if self.__buffer_cspc != None: - data_cspc = numpy.sum(self.__buffer_cspc, axis=0) - - if self.__buffer_dc != None: - data_dc = numpy.sum(self.__buffer_dc, axis=0) - - n = self.__profIndex - - return data_spc, data_cspc, data_dc, n - - def byProfiles(self, *args): - - self.__dataReady = False - avgdata_spc = None - avgdata_cspc = None - avgdata_dc = None - n = None - - self.putData(*args) - - if self.__profIndex == self.n: - - avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData() - self.__dataReady = True - - return avgdata_spc, avgdata_cspc, avgdata_dc - - def byTime(self, datatime, *args): - - self.__dataReady = False - avgdata_spc = None - avgdata_cspc = None - avgdata_dc = None - n = None - - self.putData(*args) - - if (datatime - self.__initime) >= self.__integrationtime: - avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData() - self.n = n - self.__dataReady = True - - return avgdata_spc, avgdata_cspc, avgdata_dc - - def integrate(self, datatime, *args): - - if self.__initime == None: - self.__initime = datatime - - if self.__byTime: - avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(datatime, *args) - else: - avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args) - - self.__lastdatatime = datatime - - if avgdata_spc == None: - return None, None, None, None - - avgdatatime = self.__initime - try: - self.__timeInterval = (self.__lastdatatime - self.__initime)/(self.n - 1) - except: - self.__timeInterval = self.__lastdatatime - self.__initime - - deltatime = datatime -self.__lastdatatime - - if not self.__withOverapping: - self.__initime = datatime - else: - self.__initime += deltatime - - return avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc - - def run(self, dataOut, n=None, timeInterval=None, overlapping=False): - - if n==1: - dataOut.flagNoData = False - return - - if not self.__isConfig: - self.setup(n, timeInterval, overlapping) - self.__isConfig = True - - avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime, - dataOut.data_spc, - dataOut.data_cspc, - dataOut.data_dc) - -# dataOut.timeInterval *= n - dataOut.flagNoData = True - - if self.__dataReady: - - dataOut.data_spc = avgdata_spc - dataOut.data_cspc = avgdata_cspc - dataOut.data_dc = avgdata_dc - - dataOut.nIncohInt *= self.n - dataOut.utctime = avgdatatime - #dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt * dataOut.nIncohInt * dataOut.nFFTPoints - dataOut.timeInterval = self.__timeInterval*self.n - dataOut.flagNoData = False - -class ProfileConcat(Operation): - - __isConfig = False - buffer = None - - def __init__(self): - - self.profileIndex = 0 - - def reset(self): - self.buffer = numpy.zeros_like(self.buffer) - self.start_index = 0 - self.times = 1 - - def setup(self, data, m, n=1): - self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0])) - self.profiles = data.shape[1] - self.start_index = 0 - self.times = 1 - - def concat(self, data): - - self.buffer[:,self.start_index:self.profiles*self.times] = data.copy() - self.start_index = self.start_index + self.profiles - - def run(self, dataOut, m): - - dataOut.flagNoData = True - - if not self.__isConfig: - self.setup(dataOut.data, m, 1) - self.__isConfig = True - - self.concat(dataOut.data) - self.times += 1 - if self.times > m: - dataOut.data = self.buffer - self.reset() - dataOut.flagNoData = False - # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas - deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] - xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * 5 - dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight) - - - -class ProfileSelector(Operation): - - profileIndex = None - # Tamanho total de los perfiles - nProfiles = None - - def __init__(self): - - self.profileIndex = 0 - - def incIndex(self): - self.profileIndex += 1 - - if self.profileIndex >= self.nProfiles: - self.profileIndex = 0 - - def isProfileInRange(self, minIndex, maxIndex): - - if self.profileIndex < minIndex: - return False - - if self.profileIndex > maxIndex: - return False - - return True - - def isProfileInList(self, profileList): - - if self.profileIndex not in profileList: - return False - - return True - - def run(self, dataOut, profileList=None, profileRangeList=None, beam=None): - - dataOut.flagNoData = True - self.nProfiles = dataOut.nProfiles - - if profileList != None: - if self.isProfileInList(profileList): - dataOut.flagNoData = False - - self.incIndex() - return 1 - - - elif profileRangeList != None: - minIndex = profileRangeList[0] - maxIndex = profileRangeList[1] - if self.isProfileInRange(minIndex, maxIndex): - dataOut.flagNoData = False - - self.incIndex() - return 1 - elif beam != None: - if self.isProfileInList(dataOut.beamRangeDict[beam]): - dataOut.flagNoData = False - - self.incIndex() - return 1 - - else: - raise ValueError, "ProfileSelector needs profileList or profileRangeList" - - return 0 - -class SpectraHeisProc(ProcessingUnit): - def __init__(self): - self.objectDict = {} -# self.buffer = None -# self.firstdatatime = None -# self.profIndex = 0 - self.dataOut = SpectraHeis() - - def __updateObjFromInput(self): - self.dataOut.timeZone = self.dataIn.timeZone - self.dataOut.dstFlag = self.dataIn.dstFlag - self.dataOut.errorCount = self.dataIn.errorCount - self.dataOut.useLocalTime = self.dataIn.useLocalTime - - self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()# - self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()# - self.dataOut.channelList = self.dataIn.channelList - self.dataOut.heightList = self.dataIn.heightList -# self.dataOut.dtype = self.dataIn.dtype - self.dataOut.dtype = numpy.dtype([('real',' change this line - self.n = 9999 - self.__byTime = True - - if overlapping: - self.__withOverapping = True - self.__buffer = None - else: - self.__withOverapping = False - self.__buffer = 0 - - self.__profIndex = 0 - - def putData(self, data): - - """ - Add a profile to the __buffer and increase in one the __profileIndex - - """ - - if not self.__withOverapping: - self.__buffer += data.copy() - self.__profIndex += 1 - return - - #Overlapping data - nChannels, nHeis = data.shape - data = numpy.reshape(data, (1, nChannels, nHeis)) - - #If the buffer is empty then it takes the data value - if self.__buffer == None: - self.__buffer = data - self.__profIndex += 1 - return - - #If the buffer length is lower than n then stakcing the data value - if self.__profIndex < self.n: - self.__buffer = numpy.vstack((self.__buffer, data)) - self.__profIndex += 1 - return - - #If the buffer length is equal to n then replacing the last buffer value with the data value - self.__buffer = numpy.roll(self.__buffer, -1, axis=0) - self.__buffer[self.n-1] = data - self.__profIndex = self.n - return - - - def pushData(self): - """ - Return the sum of the last profiles and the profiles used in the sum. - - Affected: - - self.__profileIndex - - """ - - if not self.__withOverapping: - data = self.__buffer - n = self.__profIndex - - self.__buffer = 0 - self.__profIndex = 0 - - return data, n - - #Integration with Overlapping - data = numpy.sum(self.__buffer, axis=0) - n = self.__profIndex - - return data, n - - def byProfiles(self, data): - - self.__dataReady = False - avgdata = None - n = None - - self.putData(data) - - if self.__profIndex == self.n: - - avgdata, n = self.pushData() - self.__dataReady = True - - return avgdata - - def byTime(self, data, datatime): - - self.__dataReady = False - avgdata = None - n = None - - self.putData(data) - - if (datatime - self.__initime) >= self.__integrationtime: - avgdata, n = self.pushData() - self.n = n - self.__dataReady = True - - return avgdata - - def integrate(self, data, datatime=None): - - if self.__initime == None: - self.__initime = datatime - - if self.__byTime: - avgdata = self.byTime(data, datatime) - else: - avgdata = self.byProfiles(data) - - - self.__lastdatatime = datatime - - if avgdata == None: - return None, None - - avgdatatime = self.__initime - - deltatime = datatime -self.__lastdatatime - - if not self.__withOverapping: - self.__initime = datatime - else: - self.__initime += deltatime - - return avgdata, avgdatatime - - def run(self, dataOut, **kwargs): - - if not self.__isConfig: - self.setup(**kwargs) - self.__isConfig = True - - avgdata, avgdatatime = self.integrate(dataOut.data_spc, dataOut.utctime) - -# dataOut.timeInterval *= n - dataOut.flagNoData = True - - if self.__dataReady: - dataOut.data_spc = avgdata - dataOut.nIncohInt *= self.n -# dataOut.nCohInt *= self.n - dataOut.utctime = avgdatatime - dataOut.timeInterval = dataOut.ippSeconds * dataOut.nIncohInt -# dataOut.timeInterval = self.__timeInterval*self.n - dataOut.flagNoData = False - - - - -class AMISRProc(ProcessingUnit): - def __init__(self): - self.objectDict = {} - self.dataOut = AMISR() - - def init(self): - if self.dataIn.type == 'AMISR': - self.dataOut.copy(self.dataIn) - - -class PrintInfo(Operation): - def __init__(self): - pass - - def run(self, dataOut): - - print 'Number of Records by File: %d'%dataOut.nRecords - print 'Number of Pulses: %d'%dataOut.nProfiles - print 'Number of Samples by Pulse: %d'%len(dataOut.heightList) - print 'Ipp Seconds: %f'%dataOut.ippSeconds - print 'Number of Beams: %d'%dataOut.nBeams - print 'BeamCodes:' - beamStrList = ['Beam %d -> Code %d'%(k,v) for k,v in dataOut.beamCodeDict.items()] - for b in beamStrList: - print b - - -class BeamSelector(Operation): - profileIndex = None - # Tamanho total de los perfiles - nProfiles = None - - def __init__(self): - - self.profileIndex = 0 - - def incIndex(self): - self.profileIndex += 1 - - if self.profileIndex >= self.nProfiles: - self.profileIndex = 0 - - def isProfileInRange(self, minIndex, maxIndex): - - if self.profileIndex < minIndex: - return False - - if self.profileIndex > maxIndex: - return False - - return True - - def isProfileInList(self, profileList): - - if self.profileIndex not in profileList: - return False - - return True - - def run(self, dataOut, beam=None): - - dataOut.flagNoData = True - self.nProfiles = dataOut.nProfiles - - if beam != None: - if self.isProfileInList(dataOut.beamRangeDict[beam]): - dataOut.flagNoData = False - - self.incIndex() - return 1 - - else: - raise ValueError, "BeamSelector needs beam value" - - return 0 \ No newline at end of file