jroIO_heispectra.py
850 lines
| 25.0 KiB
| text/x-python
|
PythonLexer
|
r487 | ''' | ||
|
r568 | Created on Jul 3, 2014 | ||
|
r487 | |||
|
r568 | @author: roj-idl71 | ||
|
r487 | ''' | ||
import os, sys | ||||
import time, datetime | ||||
import numpy | ||||
import fnmatch | ||||
import glob | ||||
|
r664 | from time import sleep | ||
|
r568 | |||
|
r487 | try: | ||
import pyfits | ||||
|
r1167 | except ImportError as e: | ||
|
r1284 | pass | ||
|
r897 | |||
|
r487 | from xml.etree.ElementTree import ElementTree | ||
|
r1167 | from .jroIO_base import isRadarFolder, isNumber | ||
|
r664 | from schainpy.model.data.jrodata import Fits | ||
|
r1191 | from schainpy.model.proc.jroproc_base import Operation, ProcessingUnit, MPDecorator | ||
from schainpy.utils import log | ||||
|
r897 | |||
class PyFits(object): | ||||
|
r1383 | name = None | ||
format = None | ||||
array = None | ||||
data = None | ||||
thdulist = None | ||||
prihdr = None | ||||
hdu = None | ||||
|
r897 | |||
|
r568 | def __init__(self): | ||
|
r897 | |||
|
r568 | pass | ||
|
r897 | |||
|
r1383 | def setColF(self, name, format, array): | ||
self.name = name | ||||
self.format = format | ||||
self.array = array | ||||
a1 = numpy.array([self.array], dtype=numpy.float32) | ||||
|
r568 | self.col1 = pyfits.Column(name=self.name, format=self.format, array=a1) | ||
return self.col1 | ||||
|
r897 | |||
|
r568 | # def setColP(self,name,format,data): | ||
# self.name=name | ||||
# self.format=format | ||||
# self.data=data | ||||
# a2=numpy.array([self.data],dtype=numpy.float32) | ||||
# self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2) | ||||
# return self.col2 | ||||
|
r897 | |||
|
r568 | |||
|
r1383 | def writeData(self, name, format, data): | ||
self.name = name | ||||
self.format = format | ||||
self.data = data | ||||
a2 = numpy.array([self.data], dtype=numpy.float32) | ||||
|
r568 | self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2) | ||
return self.col2 | ||||
|
r897 | |||
|
r1383 | 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) | ||||
|
r897 | return self.hdu | ||
|
r568 | |||
|
r1383 | def Ctable(self, colList): | ||
self.cols = pyfits.ColDefs(colList) | ||||
|
r568 | self.tbhdu = pyfits.new_table(self.cols) | ||
return self.tbhdu | ||||
|
r897 | |||
|
r1383 | def CFile(self, hdu, tbhdu): | ||
self.thdulist = pyfits.HDUList([hdu, tbhdu]) | ||||
|
r897 | |||
|
r1383 | def wFile(self, filename): | ||
|
r568 | if os.path.isfile(filename): | ||
os.remove(filename) | ||||
|
r897 | self.thdulist.writeto(filename) | ||
|
r568 | |||
|
r487 | |||
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 | ||||
|
r664 | class Metadata(object): | ||
|
r897 | |||
|
r487 | def __init__(self, filename): | ||
self.parmConfObjList = [] | ||||
self.readXml(filename) | ||||
|
r897 | |||
|
r487 | def readXml(self, filename): | ||
self.projectElement = None | ||||
self.procUnitConfObjDict = {} | ||||
self.projectElement = ElementTree().parse(filename) | ||||
|
r897 | self.project = self.projectElement.tag | ||
|
r487 | |||
parmElementList = self.projectElement.getiterator(ParameterConf().getElementName()) | ||||
|
r897 | |||
|
r487 | for parmElement in parmElementList: | ||
parmConfObj = ParameterConf() | ||||
parmConfObj.readXml(parmElement) | ||||
self.parmConfObjList.append(parmConfObj) | ||||
|
r1287 | @MPDecorator | ||
|
r487 | class FitsWriter(Operation): | ||
|
r897 | def __init__(self, **kwargs): | ||
Operation.__init__(self, **kwargs) | ||||
|
r487 | self.isConfig = False | ||
self.dataBlocksPerFile = None | ||||
self.blockIndex = 0 | ||||
self.flagIsNewFile = 1 | ||||
self.fitsObj = None | ||||
self.optchar = 'P' | ||||
self.ext = '.fits' | ||||
self.setFile = 0 | ||||
|
r897 | |||
|
r664 | def setFitsHeader(self, dataOut, metadatafile=None): | ||
|
r897 | |||
|
r487 | header_data = pyfits.PrimaryHDU() | ||
|
r897 | |||
|
r664 | header_data.header['EXPNAME'] = "RADAR DATA" | ||
header_data.header['DATATYPE'] = "SPECTRA" | ||||
header_data.header['COMMENT'] = "" | ||||
|
r897 | |||
|
r664 | if metadatafile: | ||
|
r897 | |||
|
r664 | metadata4fits = Metadata(metadatafile) | ||
|
r897 | |||
|
r664 | for parameter in metadata4fits.parmConfObjList: | ||
parm_name = parameter.name | ||||
parm_value = parameter.value | ||||
|
r897 | |||
|
r664 | header_data.header[parm_name] = parm_value | ||
|
r897 | |||
|
r487 | 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 | ||||
|
r1383 | # header_data.header['HEIGHTS'] = dataOut.heightList | ||
|
r487 | header_data.header['NHEIGHTS'] = dataOut.nHeights | ||
|
r897 | |||
|
r487 | 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 | ||||
|
r897 | |||
|
r487 | header_data.writeto(self.filename) | ||
|
r897 | |||
|
r1383 | self.addExtension(dataOut.heightList, 'HEIGHTLIST') | ||
|
r897 | |||
|
r664 | def setup(self, dataOut, path, dataBlocksPerFile=100, metadatafile=None): | ||
|
r897 | |||
|
r487 | self.path = path | ||
self.dataOut = dataOut | ||||
self.metadatafile = metadatafile | ||||
self.dataBlocksPerFile = dataBlocksPerFile | ||||
|
r897 | |||
|
r487 | def open(self): | ||
self.fitsObj = pyfits.open(self.filename, mode='update') | ||||
|
r897 | |||
|
r487 | def addExtension(self, data, tagname): | ||
self.open() | ||||
extension = pyfits.ImageHDU(data=data, name=tagname) | ||||
|
r1383 | # extension.header['TAG'] = tagname | ||
|
r487 | self.fitsObj.append(extension) | ||
self.write() | ||||
|
r897 | |||
|
r487 | 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): | ||||
|
r897 | |||
|
r487 | self.fitsObj.flush(verbose=True) | ||
self.fitsObj.close() | ||||
|
r897 | |||
|
r487 | def setNextFile(self): | ||
ext = self.ext | ||||
path = self.path | ||||
|
r897 | |||
|
r1383 | timeTuple = time.localtime(self.dataOut.utctime) | ||
subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday) | ||||
|
r487 | |||
|
r1383 | fullpath = os.path.join(path, subfolder) | ||
if not(os.path.exists(fullpath)): | ||||
|
r487 | os.mkdir(fullpath) | ||
|
r1383 | self.setFile = -1 # inicializo mi contador de seteo | ||
|
r487 | else: | ||
|
r1383 | filesList = os.listdir(fullpath) | ||
if len(filesList) > 0: | ||||
filesList = sorted(filesList, key=str.lower) | ||||
|
r487 | filen = filesList[-1] | ||
|
r897 | |||
|
r1383 | if isNumber(filen[8:11]): | ||
self.setFile = int(filen[8:11]) # inicializo mi contador de seteo al seteo del ultimo file | ||||
|
r897 | else: | ||
|
r487 | self.setFile = -1 | ||
else: | ||||
|
r1383 | self.setFile = -1 # inicializo mi contador de seteo | ||
|
r897 | |||
|
r487 | setFile = self.setFile | ||
setFile += 1 | ||||
|
r897 | |||
|
r568 | thisFile = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, | ||
|
r487 | timeTuple.tm_year, | ||
timeTuple.tm_yday, | ||||
setFile, | ||||
|
r1383 | ext) | ||
|
r487 | |||
|
r1383 | filename = os.path.join(path, subfolder, thisFile) | ||
|
r897 | |||
|
r487 | self.blockIndex = 0 | ||
self.filename = filename | ||||
self.setFile = setFile | ||||
self.flagIsNewFile = 1 | ||||
|
r1383 | print('Writing the file: %s' % self.filename) | ||
|
r897 | |||
|
r487 | self.setFitsHeader(self.dataOut, self.metadatafile) | ||
|
r897 | |||
|
r487 | return 1 | ||
def writeBlock(self): | ||||
|
r897 | self.addData(self.dataOut.data_spc) | ||
|
r487 | self.flagIsNewFile = 0 | ||
|
r897 | |||
|
r487 | def __setNewBlock(self): | ||
if self.flagIsNewFile: | ||||
return 1 | ||||
|
r897 | |||
|
r487 | if self.blockIndex < self.dataBlocksPerFile: | ||
return 1 | ||||
|
r897 | |||
|
r1383 | if not(self.setNextFile()): | ||
|
r487 | return 0 | ||
|
r897 | |||
|
r487 | return 1 | ||
def writeNextBlock(self): | ||||
|
r1383 | if not(self.__setNewBlock()): | ||
|
r487 | return 0 | ||
self.writeBlock() | ||||
|
r897 | return 1 | ||
def putData(self): | ||||
|
r487 | if self.flagIsNewFile: | ||
self.setNextFile() | ||||
self.writeNextBlock() | ||||
|
r897 | |||
|
r954 | def run(self, dataOut, path, dataBlocksPerFile=100, metadatafile=None, **kwargs): | ||
|
r487 | if not(self.isConfig): | ||
|
r954 | self.setup(dataOut, path, dataBlocksPerFile=dataBlocksPerFile, metadatafile=metadatafile, **kwargs) | ||
|
r487 | self.isConfig = True | ||
self.putData() | ||||
|
r897 | |||
|
r1287 | |||
|
r487 | class FitsReader(ProcessingUnit): | ||
|
r897 | |||
|
r487 | # __TIMEZONE = time.timezone | ||
|
r897 | |||
|
r487 | expName = None | ||
datetimestr = None | ||||
utc = None | ||||
nChannels = None | ||||
nSamples = None | ||||
dataBlocksPerFile = None | ||||
|
r897 | comments = None | ||
|
r487 | lastUTTime = None | ||
header_dict = None | ||||
data = None | ||||
data_header_dict = None | ||||
|
r897 | |||
|
r1383 | def __init__(self): # , **kwargs): | ||
ProcessingUnit.__init__(self) # , **kwargs) | ||||
|
r487 | self.isConfig = False | ||
self.ext = '.fits' | ||||
self.setFile = 0 | ||||
self.flagNoMoreFiles = 0 | ||||
self.flagIsNewFile = 1 | ||||
|
r568 | self.flagDiscontinuousBlock = None | ||
|
r487 | 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() | ||||
|
r1383 | self.maxTimeStep = 10 # deberia ser definido por el usuario usando el metodo setup() | ||
|
r897 | self.blockIndex = 1 | ||
|
r487 | def createObjByDefault(self): | ||
|
r897 | |||
|
r487 | dataObj = Fits() | ||
|
r897 | |||
|
r487 | return dataObj | ||
|
r897 | |||
|
r487 | def isFileinThisTime(self, filename, startTime, endTime, useLocalTime=False): | ||
try: | ||||
|
r1383 | fitsObj = pyfits.open(filename, 'readonly') | ||
|
r487 | except: | ||
|
r1383 | print("File %s can't be opened" % (filename)) | ||
|
r684 | return None | ||
|
r897 | |||
|
r487 | header = fitsObj[0].header | ||
struct_time = time.strptime(header['DATETIME'], "%b %d %Y %H:%M:%S") | ||||
|
r1383 | utc = time.mktime(struct_time) - time.timezone # TIMEZONE debe ser un parametro del header FITS | ||
|
r897 | |||
|
r487 | ltc = utc | ||
if useLocalTime: | ||||
ltc -= time.timezone | ||||
thisDatetime = datetime.datetime.utcfromtimestamp(ltc) | ||||
thisTime = thisDatetime.time() | ||||
|
r897 | |||
|
r487 | if not ((startTime <= thisTime) and (endTime > thisTime)): | ||
return None | ||||
|
r897 | |||
|
r487 | return thisDatetime | ||
|
r897 | |||
|
r487 | def __setNextFileOnline(self): | ||
|
r684 | raise NotImplementedError | ||
|
r897 | |||
|
r487 | def __setNextFileOffline(self): | ||
idFile = self.fileIndex | ||||
while (True): | ||||
idFile += 1 | ||||
if not(idFile < len(self.filenameList)): | ||||
self.flagNoMoreFiles = 1 | ||||
|
r1167 | print("No more Files") | ||
|
r487 | return 0 | ||
filename = self.filenameList[idFile] | ||||
# if not(self.__verifyFile(filename)): | ||||
# continue | ||||
fileSize = os.path.getsize(filename) | ||||
|
r1383 | fitsObj = pyfits.open(filename, 'readonly') | ||
|
r487 | break | ||
self.flagIsNewFile = 1 | ||||
self.fileIndex = idFile | ||||
self.filename = filename | ||||
self.fileSize = fileSize | ||||
self.fitsObj = fitsObj | ||||
self.blockIndex = 0 | ||||
|
r1383 | print("Setting the file: %s" % self.filename) | ||
|
r487 | |||
return 1 | ||||
|
r897 | |||
|
r587 | def __setValuesFromHeader(self): | ||
|
r897 | |||
self.dataOut.header = self.header_dict | ||||
|
r587 | self.dataOut.expName = self.expName | ||
|
r897 | |||
|
r587 | self.dataOut.timeZone = self.timeZone | ||
self.dataOut.dataBlocksPerFile = self.dataBlocksPerFile | ||||
|
r897 | self.dataOut.comments = self.comments | ||
|
r587 | # self.dataOut.timeInterval = self.timeInterval | ||
self.dataOut.channelList = self.channelList | ||||
self.dataOut.heightList = self.heightList | ||||
|
r897 | |||
|
r587 | self.dataOut.nCohInt = self.nCohInt | ||
self.dataOut.nIncohInt = self.nIncohInt | ||||
|
r1191 | self.dataOut.ipp_sec = self.ippSeconds | ||
|
r897 | |||
|
r487 | def readHeader(self): | ||
headerObj = self.fitsObj[0] | ||||
|
r897 | |||
|
r487 | self.header_dict = headerObj.header | ||
|
r1167 | if 'EXPNAME' in list(headerObj.header.keys()): | ||
|
r487 | self.expName = headerObj.header['EXPNAME'] | ||
|
r897 | |||
|
r1167 | if 'DATATYPE' in list(headerObj.header.keys()): | ||
|
r487 | self.dataType = headerObj.header['DATATYPE'] | ||
|
r897 | |||
|
r487 | 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'] | ||||
|
r897 | |||
|
r527 | # self.timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt | ||
|
r897 | |||
|
r1167 | if 'COMMENT' in list(headerObj.header.keys()): | ||
|
r487 | self.comments = headerObj.header['COMMENT'] | ||
|
r897 | |||
|
r487 | self.readHeightList() | ||
|
r897 | |||
|
r487 | def readHeightList(self): | ||
self.blockIndex = self.blockIndex + 1 | ||||
obj = self.fitsObj[self.blockIndex] | ||||
self.heightList = obj.data | ||||
self.blockIndex = self.blockIndex + 1 | ||||
|
r897 | |||
|
r487 | def readExtension(self): | ||
obj = self.fitsObj[self.blockIndex] | ||||
self.heightList = obj.data | ||||
self.blockIndex = self.blockIndex + 1 | ||||
|
r897 | |||
|
r487 | def setNextFile(self): | ||
if self.online: | ||||
newFile = self.__setNextFileOnline() | ||||
else: | ||||
newFile = self.__setNextFileOffline() | ||||
if not(newFile): | ||||
return 0 | ||||
|
r897 | |||
|
r487 | self.readHeader() | ||
|
r587 | self.__setValuesFromHeader() | ||
|
r487 | self.nReadBlocks = 0 | ||
# self.blockIndex = 1 | ||||
return 1 | ||||
|
r1052 | def searchFilesOffLine(self, | ||
|
r487 | path, | ||
startDate, | ||||
endDate, | ||||
|
r1383 | startTime=datetime.time(0, 0, 0), | ||
endTime=datetime.time(23, 59, 59), | ||||
|
r487 | set=None, | ||
expLabel='', | ||||
ext='.fits', | ||||
walk=True): | ||||
|
r897 | |||
|
r487 | pathList = [] | ||
|
r897 | |||
|
r487 | if not walk: | ||
pathList.append(path) | ||||
|
r897 | |||
|
r487 | else: | ||
dirList = [] | ||||
for thisPath in os.listdir(path): | ||||
|
r1383 | if not os.path.isdir(os.path.join(path, thisPath)): | ||
|
r487 | continue | ||
|
r589 | if not isRadarFolder(thisPath): | ||
|
r487 | continue | ||
|
r897 | |||
|
r487 | dirList.append(thisPath) | ||
|
r897 | |||
|
r487 | if not(dirList): | ||
return None, None | ||||
|
r897 | |||
|
r487 | thisDate = startDate | ||
|
r897 | |||
|
r487 | while(thisDate <= endDate): | ||
year = thisDate.timetuple().tm_year | ||||
doy = thisDate.timetuple().tm_yday | ||||
|
r897 | |||
|
r1383 | matchlist = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year, doy) + '*') | ||
|
r487 | if len(matchlist) == 0: | ||
thisDate += datetime.timedelta(1) | ||||
continue | ||||
for match in matchlist: | ||||
|
r1383 | pathList.append(os.path.join(path, match, expLabel)) | ||
|
r897 | |||
|
r487 | thisDate += datetime.timedelta(1) | ||
|
r897 | |||
|
r487 | if pathList == []: | ||
|
r1383 | print("Any folder was found for the date range: %s-%s" % (startDate, endDate)) | ||
|
r487 | return None, None | ||
|
r897 | |||
|
r1383 | print("%d folder(s) was(were) found for the date range: %s - %s" % (len(pathList), startDate, endDate)) | ||
|
r897 | |||
|
r487 | filenameList = [] | ||
datetimeList = [] | ||||
|
r897 | |||
|
r487 | for i in range(len(pathList)): | ||
|
r897 | |||
|
r487 | thisPath = pathList[i] | ||
|
r897 | |||
|
r1383 | fileList = glob.glob1(thisPath, "*%s" % ext) | ||
|
r487 | fileList.sort() | ||
|
r897 | |||
|
r568 | for thisFile in fileList: | ||
|
r897 | |||
|
r1383 | filename = os.path.join(thisPath, thisFile) | ||
|
r487 | thisDatetime = self.isFileinThisTime(filename, startTime, endTime) | ||
|
r897 | |||
|
r487 | if not(thisDatetime): | ||
continue | ||||
|
r897 | |||
|
r487 | filenameList.append(filename) | ||
datetimeList.append(thisDatetime) | ||||
|
r897 | |||
|
r487 | if not(filenameList): | ||
|
r1383 | print("Any file was found for the time range %s - %s" % (startTime, endTime)) | ||
|
r487 | return None, None | ||
|
r897 | |||
|
r1383 | print("%d file(s) was(were) found for the time range: %s - %s" % (len(filenameList), startTime, endTime)) | ||
|
r1167 | print() | ||
|
r897 | |||
|
r487 | for i in range(len(filenameList)): | ||
|
r1383 | print("%s -> [%s]" % (filenameList[i], datetimeList[i].ctime())) | ||
|
r487 | |||
self.filenameList = filenameList | ||||
self.datetimeList = datetimeList | ||||
|
r897 | |||
|
r487 | return pathList, filenameList | ||
|
r897 | |||
|
r487 | def setup(self, path=None, | ||
|
r897 | startDate=None, | ||
endDate=None, | ||||
|
r1383 | startTime=datetime.time(0, 0, 0), | ||
endTime=datetime.time(23, 59, 59), | ||||
|
r897 | set=0, | ||
|
r1383 | expLabel="", | ||
ext=None, | ||||
online=False, | ||||
delay=60, | ||||
walk=True): | ||||
|
r897 | |||
|
r487 | if path == None: | ||
|
r1167 | raise ValueError("The path is not valid") | ||
|
r487 | |||
if ext == None: | ||||
ext = self.ext | ||||
|
r897 | |||
|
r487 | if not(online): | ||
|
r1167 | print("Searching files in offline mode ...") | ||
|
r1052 | pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate, | ||
|
r487 | startTime=startTime, endTime=endTime, | ||
set=set, expLabel=expLabel, ext=ext, | ||||
walk=walk) | ||||
|
r897 | |||
|
r487 | if not(pathList): | ||
|
r1383 | 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())) | ||||
|
r897 | |||
|
r487 | sys.exit(-1) | ||
self.fileIndex = -1 | ||||
self.pathList = pathList | ||||
self.filenameList = filenameList | ||||
|
r897 | |||
|
r487 | self.online = online | ||
self.delay = delay | ||||
ext = ext.lower() | ||||
self.ext = ext | ||||
if not(self.setNextFile()): | ||||
|
r1383 | 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())) | ||||
|
r487 | elif startDate != None: | ||
|
r1383 | print("No files in range: %s" % (datetime.datetime.combine(startDate, startTime).ctime())) | ||
|
r487 | else: | ||
|
r1167 | print("No files") | ||
|
r487 | |||
sys.exit(-1) | ||||
|
r897 | |||
|
r487 | 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'] | ||||
|
r897 | |||
|
r487 | self.flagIsNewFile = 0 | ||
self.blockIndex += 1 | ||||
self.nTotalBlocks += 1 | ||||
self.nReadBlocks += 1 | ||||
|
r897 | |||
|
r487 | return 1 | ||
|
r897 | |||
|
r487 | def __jumpToLastBlock(self): | ||
|
r684 | raise NotImplementedError | ||
|
r487 | |||
def __waitNewBlock(self): | ||||
""" | ||||
|
r897 | Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma. | ||
|
r487 | Si el modo de lectura es OffLine siempre retorn 0 | ||
""" | ||||
if not self.online: | ||||
return 0 | ||||
|
r897 | |||
|
r487 | if (self.nReadBlocks >= self.dataBlocksPerFile): | ||
return 0 | ||||
|
r897 | |||
|
r487 | currentPointer = self.fp.tell() | ||
|
r897 | |||
|
r487 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize | ||
|
r897 | |||
|
r1383 | for nTries in range(self.nTries): | ||
|
r897 | |||
|
r487 | self.fp.close() | ||
|
r1383 | self.fp = open(self.filename, 'rb') | ||
self.fp.seek(currentPointer) | ||||
|
r487 | |||
|
r1383 | self.fileSize = os.path.getsize(self.filename) | ||
|
r487 | currentSize = self.fileSize - currentPointer | ||
|
r1383 | if (currentSize >= neededSize): | ||
|
r487 | self.__rdBasicHeader() | ||
return 1 | ||||
|
r897 | |||
|
r1383 | print("\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1)) | ||
sleep(self.delay) | ||||
|
r897 | |||
|
r487 | return 0 | ||
|
r897 | |||
|
r487 | def __setNewBlock(self): | ||
if self.online: | ||||
self.__jumpToLastBlock() | ||||
|
r897 | |||
|
r487 | if self.flagIsNewFile: | ||
return 1 | ||||
|
r897 | |||
|
r487 | self.lastUTTime = self.utc | ||
if self.online: | ||||
if self.__waitNewBlock(): | ||||
return 1 | ||||
|
r897 | |||
|
r487 | if self.nReadBlocks < self.dataBlocksPerFile: | ||
return 1 | ||||
|
r897 | |||
|
r487 | if not(self.setNextFile()): | ||
return 0 | ||||
|
r897 | |||
deltaTime = self.utc - self.lastUTTime | ||||
|
r568 | self.flagDiscontinuousBlock = 0 | ||
|
r487 | |||
if deltaTime > self.maxTimeStep: | ||||
|
r568 | self.flagDiscontinuousBlock = 1 | ||
|
r487 | |||
return 1 | ||||
|
r897 | |||
|
r487 | def readNextBlock(self): | ||
if not(self.__setNewBlock()): | ||||
return 0 | ||||
if not(self.readBlock()): | ||||
return 0 | ||||
return 1 | ||||
|
r897 | |||
|
r664 | def printInfo(self): | ||
|
r897 | |||
|
r664 | pass | ||
|
r897 | |||
|
r487 | def getData(self): | ||
|
r897 | |||
|
r487 | if self.flagNoMoreFiles: | ||
self.dataOut.flagNoData = True | ||||
|
r1191 | return (0, 'No more files') | ||
|
r897 | |||
|
r568 | self.flagDiscontinuousBlock = 0 | ||
|
r487 | self.flagIsNewBlock = 0 | ||
if not(self.readNextBlock()): | ||||
|
r1191 | return (1, 'Error reading data') | ||
|
r897 | |||
|
r664 | if self.data is None: | ||
|
r487 | self.dataOut.flagNoData = True | ||
|
r1191 | return (0, 'No more data') | ||
|
r897 | |||
|
r487 | self.dataOut.data = self.data | ||
self.dataOut.data_header = self.data_header_dict | ||||
self.dataOut.utctime = self.utc | ||||
|
r897 | |||
# self.dataOut.header = self.header_dict | ||||
|
r587 | # self.dataOut.expName = self.expName | ||
# self.dataOut.nChannels = self.nChannels | ||||
# self.dataOut.timeZone = self.timeZone | ||||
# self.dataOut.dataBlocksPerFile = self.dataBlocksPerFile | ||||
|
r897 | # self.dataOut.comments = self.comments | ||
|
r587 | # # self.dataOut.timeInterval = self.timeInterval | ||
# self.dataOut.channelList = self.channelList | ||||
# self.dataOut.heightList = self.heightList | ||||
|
r487 | self.dataOut.flagNoData = False | ||
|
r1191 | # return self.dataOut.data | ||
|
r897 | |||
|
r487 | def run(self, **kwargs): | ||
|
r897 | |||
|
r487 | if not(self.isConfig): | ||
self.setup(**kwargs) | ||||
self.isConfig = True | ||||
|
r897 | |||
|
r487 | self.getData() | ||
|
r1191 | @MPDecorator | ||
|
r897 | class SpectraHeisWriter(Operation): | ||
|
r487 | # set = None | ||
setFile = None | ||||
idblock = None | ||||
doypath = None | ||||
subfolder = None | ||||
|
r897 | |||
|
r1383 | def __init__(self): # , **kwargs): | ||
Operation.__init__(self) # , **kwargs) | ||||
|
r664 | self.wrObj = PyFits() | ||
|
r897 | # self.dataOut = dataOut | ||
|
r1383 | self.nTotalBlocks = 0 | ||
|
r487 | # self.set = None | ||
self.setFile = None | ||||
self.idblock = 0 | ||||
self.wrpath = None | ||||
self.doypath = None | ||||
self.subfolder = None | ||||
|
r897 | self.isConfig = False | ||
|
r487 | def isNumber(str): | ||
""" | ||||
Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. | ||||
|
r897 | Excepciones: | ||
|
r487 | 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 | ||||
|
r897 | |||
|
r487 | Return: | ||
True : si el string es uno numerico | ||||
False : no es un string numerico | ||||
""" | ||||
try: | ||||
|
r1383 | float(str) | ||
|
r487 | return True | ||
except: | ||||
|
r897 | return False | ||
|
r487 | def setup(self, dataOut, wrpath): | ||
if not(os.path.exists(wrpath)): | ||||
os.mkdir(wrpath) | ||||
|
r897 | |||
|
r487 | self.wrpath = wrpath | ||
# self.setFile = 0 | ||||
self.dataOut = dataOut | ||||
def putData(self): | ||||
|
r1383 | name = time.localtime(self.dataOut.utctime) | ||
ext = ".fits" | ||||
|
r897 | |||
|
r487 | if self.doypath == None: | ||
|
r1383 | 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) | ||||
|
r487 | os.mkdir(self.doypath) | ||
|
r897 | |||
|
r487 | if self.setFile == None: | ||
# self.set = self.dataOut.set | ||||
self.setFile = 0 | ||||
# if self.set != self.dataOut.set: | ||||
|
r1383 | # # self.set = self.dataOut.set | ||
|
r487 | # self.setFile = 0 | ||
|
r897 | |||
|
r1383 | # make the filename | ||
thisFile = 'D%4.4d%3.3d_%3.3d%s' % (name.tm_year, name.tm_yday, self.setFile, ext) | ||||
|
r897 | |||
|
r1383 | filename = os.path.join(self.wrpath, self.subfolder, thisFile) | ||
|
r487 | |||
|
r1383 | idblock = numpy.array([self.idblock], dtype="int64") | ||
header = self.wrObj.cFImage(idblock=idblock, | ||||
|
r487 | year=time.gmtime(self.dataOut.utctime).tm_year, | ||
month=time.gmtime(self.dataOut.utctime).tm_mon, | ||||
|
r897 | day=time.gmtime(self.dataOut.utctime).tm_mday, | ||
|
r487 | hour=time.gmtime(self.dataOut.utctime).tm_hour, | ||
|
r897 | minute=time.gmtime(self.dataOut.utctime).tm_min, | ||
|
r487 | second=time.gmtime(self.dataOut.utctime).tm_sec) | ||
|
r897 | |||
|
r1383 | c = 3E8 | ||
|
r487 | deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] | ||
|
r1383 | freq = numpy.arange(-1 * self.dataOut.nHeights / 2., self.dataOut.nHeights / 2.) * (c / (2 * deltaHeight * 1000)) | ||
|
r897 | |||
|
r487 | colList = [] | ||
|
r897 | |||
|
r1383 | colFreq = self.wrObj.setColF(name="freq", format=str(self.dataOut.nFFTPoints) + 'E', array=freq) | ||
|
r897 | |||
|
r487 | colList.append(colFreq) | ||
|
r1383 | nchannel = self.dataOut.nChannels | ||
|
r897 | |||
|
r487 | for i in range(nchannel): | ||
|
r1383 | col = self.wrObj.writeData(name="PCh" + str(i + 1), | ||
format=str(self.dataOut.nFFTPoints) + 'E', | ||||
data=10 * numpy.log10(self.dataOut.data_spc[i, :])) | ||||
|
r897 | |||
|
r487 | colList.append(col) | ||
|
r897 | |||
|
r1383 | data = self.wrObj.Ctable(colList=colList) | ||
|
r897 | |||
|
r1383 | self.wrObj.CFile(header, data) | ||
|
r897 | |||
self.wrObj.wFile(filename) | ||||
|
r1383 | # update the setFile | ||
|
r487 | self.setFile += 1 | ||
self.idblock += 1 | ||||
|
r897 | |||
|
r487 | return 1 | ||
|
r897 | |||
|
r487 | def run(self, dataOut, **kwargs): | ||
|
r897 | |||
|
r487 | if not(self.isConfig): | ||
|
r897 | |||
|
r487 | self.setup(dataOut, **kwargs) | ||
self.isConfig = True | ||||
|
r897 | |||
|
r1191 | self.putData() | ||
|
r1383 | return dataOut | ||