''' $Author: murco $ $Id: JROHeaderIO.py 151 2012-10-31 19:00:51Z murco $ ''' import sys import numpy import copy import datetime import inspect from schainpy.utils import log SPEED_OF_LIGHT = 299792458 SPEED_OF_LIGHT = 3e8 BASIC_STRUCTURE = numpy.dtype([ ('nSize', ' endFp: sys.stderr.write( "Warning %s: Size value read from System Header is lower than it has to be\n" % fp.name) return 0 if fp.tell() < endFp: sys.stderr.write( "Warning %s: Size value read from System Header size is greater than it has to be\n" % fp.name) return 0 self.length = header.nbytes return 1 def write(self, fp): headerTuple = (self.size, self.nSamples, self.nProfiles, self.nChannels, self.adcResolution, self.pciDioBusWidth) header = numpy.array(headerTuple, SYSTEM_STRUCTURE) header.tofile(fp) return 1 class RadarControllerHeader(Header): expType = None nTx = None ipp = None txA = None txB = None nWindows = None numTaus = None codeType = None line6Function = None line5Function = None fClock = None prePulseBefore = None prePulseAfter = None rangeIpp = None rangeTxA = None rangeTxB = None structure = RADAR_STRUCTURE __size = None def __init__(self, expType=2, nTx=1, ipp=None, txA=0, txB=0, nWindows=None, nHeights=None, firstHeight=None, deltaHeight=None, numTaus=0, line6Function=0, line5Function=0, fClock=None, prePulseBefore=0, prePulseAfter=0, codeType=0, nCode=0, nBaud=0, code=None, flip1=0, flip2=0): # self.size = 116 self.expType = expType self.nTx = nTx self.ipp = ipp self.txA = txA self.txB = txB self.rangeIpp = ipp self.rangeTxA = txA self.rangeTxB = txB self.nWindows = nWindows self.numTaus = numTaus self.codeType = codeType self.line6Function = line6Function self.line5Function = line5Function self.fClock = fClock self.prePulseBefore = prePulseBefore self.prePulseAfter = prePulseAfter self.nHeights = nHeights self.firstHeight = firstHeight self.deltaHeight = deltaHeight self.samplesWin = nHeights self.nCode = nCode self.nBaud = nBaud self.code = code self.flip1 = flip1 self.flip2 = flip2 self.code_size = int(numpy.ceil(self.nBaud / 32.)) * self.nCode * 4 # self.dynamic = numpy.array([],numpy.dtype('byte')) if self.fClock is None and self.deltaHeight is not None: self.fClock = 0.15 / (deltaHeight * 1e-6) # 0.15Km / (height * 1u) def read(self, fp): self.length = 0 try: startFp = fp.tell() except Exception as e: startFp = None pass try: if hasattr(fp, 'read'): header = numpy.fromfile(fp, RADAR_STRUCTURE, 1) else: header = numpy.fromstring(fp, RADAR_STRUCTURE, 1) self.length += header.nbytes except Exception as e: print("RadarControllerHeader: " + str(e)) return 0 size = int(header['nSize'][0]) self.expType = int(header['nExpType'][0]) self.nTx = int(header['nNTx'][0]) self.ipp = float(header['fIpp'][0]) self.txA = float(header['fTxA'][0]) self.txB = float(header['fTxB'][0]) self.nWindows = int(header['nNumWindows'][0]) self.numTaus = int(header['nNumTaus'][0]) self.codeType = int(header['nCodeType'][0]) self.line6Function = int(header['nLine6Function'][0]) self.line5Function = int(header['nLine5Function'][0]) self.fClock = float(header['fClock'][0]) self.prePulseBefore = int(header['nPrePulseBefore'][0]) self.prePulseAfter = int(header['nPrePulseAfter'][0]) self.rangeIpp = header['sRangeIPP'][0] self.rangeTxA = header['sRangeTxA'][0] self.rangeTxB = header['sRangeTxB'][0] try: if hasattr(fp, 'read'): samplingWindow = numpy.fromfile( fp, SAMPLING_STRUCTURE, self.nWindows) else: samplingWindow = numpy.fromstring( fp[self.length:], SAMPLING_STRUCTURE, self.nWindows) self.length += samplingWindow.nbytes except Exception as e: print("RadarControllerHeader: " + str(e)) return 0 self.nHeights = int(numpy.sum(samplingWindow['nsa'])) self.firstHeight = samplingWindow['h0'] self.deltaHeight = samplingWindow['dh'] self.samplesWin = samplingWindow['nsa'] try: if hasattr(fp, 'read'): self.Taus = numpy.fromfile(fp, ' endFp: sys.stderr.write( "Warning %s: Size value read from Radar Controller header is lower than it has to be\n" % fp.name) # return 0 if fp.tell() < endFp: sys.stderr.write( "Warning %s: Size value read from Radar Controller header is greater than it has to be\n" % fp.name) 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.prePulseAfter, self.rangeIpp, self.rangeTxA, self.rangeTxB) header = numpy.array(headerTuple, RADAR_STRUCTURE) header.tofile(fp) sampleWindowTuple = ( self.firstHeight, self.deltaHeight, self.samplesWin) samplingWindow = numpy.array(sampleWindowTuple, SAMPLING_STRUCTURE) samplingWindow.tofile(fp) if self.numTaus > 0: self.Taus.tofile(fp) if self.codeType != 0: nCode = numpy.array(self.nCode, ' 0: self.flag_cspc = True if startFp is not None: endFp = size + startFp if fp.tell() > endFp: sys.stderr.write( "Warning: Processing header size is lower than it has to be") return 0 if fp.tell() < endFp: sys.stderr.write( "Warning: Processing header size is greater than it is considered") return 1 def write(self, fp): # Clear DEFINE_PROCESS_CODE self.processFlags = self.processFlags & (~PROCFLAG.DEFINE_PROCESS_CODE) 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, PROCESSING_STRUCTURE) header.tofile(fp) if self.nWindows != 0: sampleWindowTuple = ( self.firstHeight, self.deltaHeight, self.samplesWin) samplingWindow = numpy.array(sampleWindowTuple, SAMPLING_STRUCTURE) 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('