def recognize(self, retries=5): ###log.info("Recognizing file %s" %self.filename) # Check the file exists if not os.path.exists( self.pathname ): log.error('Cannot find frame : "%s"' % self.pathname) raise Exception("File %s not found"%self.pathname) # check FITS-file integrity nTry = 0 while True: try: fits_simple_verify(self.pathname) except Exception,e: if nTry<retries: nTry +=1 time.sleep(nTry*0.5) log.warning("Trying to re-read integrity of FITS-file : %s\n %s"%(self.pathname, str(e))) else: log.error("Could not read with integrity FITS-file: %s\n %s"%(self.pathname, str(e))) log.error("File discarded : %s"%self.pathname) raise e else: break
def addHistory(self, string_history): """ Add a history keyword to the header NOTE: The update is only done in memory-header(my_header). To flush to disk, we should to write/update to a new file. STILL NOT USED !!!! """ log.warning("Header not updated nicely !!") try: #t=pyfits.open(self.pathname,'update') self.my_header.add_history(string_history) #t.close(output_verify='ignore') log.warning('History added') except: log.error('Error while adding history to header')
def fits_simple_verify(fitsfile): """ Performs 2 simple checks on the input fitsfile, which is a string containing a path to a FITS file. First, it checks that the first card is SIMPLE, and second it checks that the file 2880 byte aligned. This function is useful for performing quick verification of FITS files. Raises: ValueError: if either of the 2 checks fails IOError: if fitsfile doesn't exist """ if not os.path.exists(fitsfile): raise IOError("file '%s' doesn't exist" % fitsfile) f = open(fitsfile,"readonly") FITS_BLOCK_SIZE = 2880 try: # check first card name card = f.read(len("SIMPLE")) if card != "SIMPLE": raise ValueError("input file is not a FITS file") # check file size stat_result = os.stat(fitsfile) file_size = stat_result.st_size # check that file_size>fits_block_size*10 to be sure all the header/s content can be read if file_size % FITS_BLOCK_SIZE != 0 or file_size<FITS_BLOCK_SIZE*10: log.warning("FITS file is not 2880 byte aligned (corrupted?) or file_size too small") raise ValueError("FITS file is not 2880 byte aligned (corrupted?) or file_size too small") finally: f.close()
else: self._shape = (self.naxis2, self.naxis1) # pointer to the primary-main header self.my_header = myfits[0].header # INSTRUMENT try: if 'INSTRUME' in myfits[0].header: self.instrument = myfits[0].header['INSTRUME'].lower() else: self.instrument = "Unknown" except Exception,e: log.warning("INSTRUME keyword not found") self.instrument = "Unknown" # Find out the how data file were"observed" if 'OBS_TOOL' in myfits[0].header or self.instrument=='hawki': self.obs_tool = True else: self.obs_tool = False # Software Version (GEIRS Version): # Old versions of GEIRS used 'SOFTWARE' if 'SOFTWARE' in myfits[0].header: self._softwareVer = myfits[0].header['SOFTWARE'] # New versions of GEIRS moved to CREATOR keyword for software version if 'CREATOR' in myfits[0].header: self._softwareVer = myfits[0].header['CREATOR']