From 967f28151148425f38ceb63f8f7acc67cfdb8dfc 2013-02-08 16:01:13 From: Daniel Valdez Date: 2013-02-08 16:01:13 Subject: [PATCH] Modificaciones para escribir datos en formato FITS --- diff --git a/schainpy/model/jrodataIO.py b/schainpy/model/jrodataIO.py index ae1c6bb..5eebbfe 100644 --- a/schainpy/model/jrodataIO.py +++ b/schainpy/model/jrodataIO.py @@ -10,6 +10,7 @@ import time import numpy import fnmatch import time, datetime +import pyfits from jrodata import * from jroheaderIO import * @@ -2533,14 +2534,24 @@ class SpectraWriter(JRODataWriter): self.processingHeaderObj.size = processingHeaderSize -class SpectraHeisWriter(): - - i=0 +class SpectraHeisWriter(Operation): +# set = None + setFile = None + idblock = None + doypath = None + subfolder = None - def __init__(self, dataOut): - + def __init__(self): self.wrObj = FITS() - self.dataOut = dataOut +# 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): """ @@ -2559,59 +2570,96 @@ class SpectraHeisWriter(): float( str ) return True except: - return False + return False - def setup(self, wrpath,): + def setup(self, dataOut, wrpath): if not(os.path.exists(wrpath)): os.mkdir(wrpath) self.wrpath = wrpath - self.setFile = 0 +# self.setFile = 0 + self.dataOut = dataOut def putData(self): - # self.wrObj.writeHeader(nChannels=self.dataOut.nChannels, nFFTPoints=self.dataOut.nFFTPoints) - #name = self.dataOut.utctime name= time.localtime( self.dataOut.utctime) - ext=".fits" - #folder='D%4.4d%3.3d'%(name.tm_year,name.tm_yday) - subfolder = 'D%4.4d%3.3d' % (name.tm_year,name.tm_yday) + 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) - fullpath = os.path.join( self.wrpath, subfolder ) - if not( os.path.exists(fullpath) ): - os.mkdir(fullpath) + 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 - file = 'D%4.4d%3.3d%3.3d%s' % (name.tm_year,name.tm_yday,self.setFile,ext) - - filename = os.path.join(self.wrpath,subfolder, file) - - # print self.dataOut.ippSeconds - freq=numpy.arange(-1*self.dataOut.nHeights/2.,self.dataOut.nHeights/2.)/(2*self.dataOut.ippSeconds) - - col1=self.wrObj.setColF(name="freq", format=str(self.dataOut.nFFTPoints)+'E', array=freq) - col2=self.wrObj.writeData(name="P_Ch1",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[0,:])) - col3=self.wrObj.writeData(name="P_Ch2",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[1,:])) - col4=self.wrObj.writeData(name="P_Ch3",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[2,:])) - col5=self.wrObj.writeData(name="P_Ch4",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[3,:])) - col6=self.wrObj.writeData(name="P_Ch5",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[4,:])) - col7=self.wrObj.writeData(name="P_Ch6",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[5,:])) - col8=self.wrObj.writeData(name="P_Ch7",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[6,:])) - col9=self.wrObj.writeData(name="P_Ch8",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[7,:])) - #n=numpy.arange((100)) - n=self.dataOut.data_spc[6,:] - a=self.wrObj.cFImage(n) - b=self.wrObj.Ctable(col1,col2,col3,col4,col5,col6,col7,col8,col9) - self.wrObj.CFile(a,b) - self.wrObj.wFile(filename) + self.idblock += 1 + return 1 + + def run(self, dataOut, **kwargs): + + if not(self.isConfig): + + self.setup(dataOut, **kwargs) + self.isConfig = True + + self.putData() + class FITS: - name=None format=None array =None data =None thdulist=None + prihdr=None + hdu=None def __init__(self): @@ -2633,9 +2681,7 @@ class FITS: # self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2) # return self.col2 - def writeHeader(self,): - pass - + def writeData(self,name,format,data): self.name=name self.format=format @@ -2644,17 +2690,27 @@ class FITS: self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2) return self.col2 - def cFImage(self,n): - self.hdu= pyfits.PrimaryHDU(n) - return self.hdu - - def Ctable(self,col1,col2,col3,col4,col5,col6,col7,col8,col9): - self.cols=pyfits.ColDefs( [col1,col2,col3,col4,col5,col6,col7,col8,col9]) + def cFImage(self,idblock,year,month,day,hour,minute,second): + self.hdu= pyfits.PrimaryHDU(idblock) + self.hdu.header.set("Year",year) + self.hdu.header.set("Month",month) + self.hdu.header.set("Day",day) + self.hdu.header.set("Hour",hour) + self.hdu.header.set("Minute",minute) + self.hdu.header.set("Second",second) + return self.hdu + + + def Ctable(self,colList): + self.cols=pyfits.ColDefs(colList) self.tbhdu = pyfits.new_table(self.cols) return self.tbhdu + def CFile(self,hdu,tbhdu): self.thdulist=pyfits.HDUList([hdu,tbhdu]) - + def wFile(self,filename): - self.thdulist.writeto(filename) \ No newline at end of file + if os.path.isfile(filename): + os.remove(filename) + self.thdulist.writeto(filename) diff --git a/schainpy/model/jroprocessing.py b/schainpy/model/jroprocessing.py index 71230d5..dae5727 100644 --- a/schainpy/model/jroprocessing.py +++ b/schainpy/model/jroprocessing.py @@ -1613,4 +1613,9 @@ class IncohInt4SpectraHeis(Operation): dataOut.utctime = avgdatatime dataOut.timeInterval = dataOut.ippSeconds * dataOut.nIncohInt # dataOut.timeInterval = self.__timeInterval*self.n - dataOut.flagNoData = False \ No newline at end of file + dataOut.flagNoData = False + + + + + \ No newline at end of file diff --git a/schainpy/test/SunExperiment.py b/schainpy/test/SunExperiment.py index 311ebc9..e438a29 100644 --- a/schainpy/test/SunExperiment.py +++ b/schainpy/test/SunExperiment.py @@ -59,6 +59,10 @@ opObj11.addParameter(name='figpath', value='/Users/dsuarez/Pictures/sun_pics', f opObj11.addParameter(name='ftp', value='1', format='int') opObj11.addParameter(name='ftpratio', value='10', format='int') +opObj11 = procUnitConfObj1.addOperation(name='SpectraHeisWriter', optype='other') +opObj11.addParameter(name='wrpath', value='/Users/dsuarez/Remote', format='str') +#opObj11.addParameter(name='blocksPerFile', value='200', format='int') + print "Escribiendo el archivo XML" controllerObj.writeXml(filename)