def _readKeyword(self, keyname): status, value, comment = cfitsio.fits_read_keyword(self._fptr, keyname) if status: if status == 202: # KEY_NO_EXIST return None else: msg = cfitsio.fits_get_errstatus(status) raise FitsError("CFITSIO: %s" % msg, status) status, unit = cfitsio.fits_read_key_unit(self._fptr, keyname) if status: msg = cfitsio.fits_get_errstatus(status) raise FitsError("CFITSIO: %s" % msg, status) status, datatype = cfitsio.fits_get_keytype(value) if status: msg = cfitsio.fits_get_errstatus(status) raise FitsError("CFITSIO: %s" % msg, status) # Rudimentary datatype conversion: if datatype == 'C': value = value[1:-1].strip() elif datatype == 'I': value = int(value) elif datatype == 'F': value = float(value) return _Keyword(keyname, value, unit, comment, datatype)
def close(self): status = cfitsio.fits_close_file(self._fptr) if status: msg = cfitsio.fits_get_errstatus(status) raise FitsError("CFITSIO: %s" % msg, status) self._fptr = None self._header = None
def createKeyword(self, keyname, value, unit="", comment=""): if keyname in self._keywords.keys(): raise FitsError("Keyword %s already present" % keyname) # Determination of datatype incomplete! # Boolean (datatype = 14) is missing! if type(value) == type(""): status = cfitsio.fits_write_key_str(self._fptr, \ keyname, \ value, \ comment) datatype = 'C' elif type(value) == type(0): status = cfitsio.fits_write_key_lng(self._fptr, \ keyname, \ value, \ comment) datatype = 'I' elif type(value) == type(0.): decimals = -7 status = cfitsio.fits_write_key_dbl(self._fptr, \ keyname, \ value, \ decimals, \ comment) datatype = 'F' else: msg = "Cannot determine datatype for value %s" % str(value) raise FitsError(msg) if status: msg = cfitsio.fits_get_errstatus(status) raise FitsError("CFITSIO: %s" % msg, status) if unit: status = cfitsio.fits_write_key_unit(self._fptr, \ keyname, \ unit) if status: msg = cfitsio.fits_get_errstatus(status) raise FitsError("CFITSIO: %s" % msg, status) self._keywords[keyname] = _Keyword(keyname, value, unit, comment, datatype) return self._keywords[keyname]
def create(self, filename): if self._fptr: raise FitsError("File %s already open" % filename) self._name = filename status, self._fptr = cfitsio.fits_create_file(filename) if status: msg = cfitsio.fits_get_errstatus(status) raise FitsError("CFITSIO: %s" % msg, status) if not self._fptr: raise FitsError("Failed to create file %s" % filename)
def createKeywordDate(self): keyname = "DATE" if keyname in self._keywords.keys(): raise FitsError("Keyword %s already present" % keyname) status = cfitsio.fits_write_date(self._fptr) if status: msg = cfitsio.fits_get_errstatus(status) raise FitsError("CFITSIO: %s" % msg, status) self._keywords[keyname] = self._readKeyword(keyname) return self._keywords[keyname]
def createImage(self, imageData): # Reopen the file to get a new file pointer to be used for the new HDU: status, fptrHdu = cfitsio.fits_reopen_file(self._fptr) if status: msg = cfitsio.fits_get_errstatus(status) raise FitsError("CFITSIO: %s" % msg, status) if not fptrHdu: raise FitsError("Failed to reopen file %s" % self._name) imageHdu = _ImageHdu(fptrHdu) imageHdu.create(imageData) self._hdus.append(imageHdu) return imageHdu
def writeImage(self): if not self._writeFunction: msg = "No writeFunction defined" raise FitsError(msg) imageData1d = Numeric.ravel(Numeric.transpose(self._imageData)) status = self._writeFunction(self._fptr, \ 0, \ 1, \ len(imageData1d), \ imageData1d) imageData1d = None if status: msg = cfitsio.fits_get_errstatus(status) raise FitsError("CFITSIO: %s" % msg, status)
def close(self): if not self._fptr: raise FitsError("Dataset is not open") if self._hdus: # If no HDU exists, fits_close_file will complain # because a valid FITS file has to contain at least # the primary HDU. # Hence, we cheat here and simply skip fits_close_file. for hdu in self._hdus: hdu.close() self._hdus = [] status = cfitsio.fits_close_file(self._fptr) if status: msg = cfitsio.fits_get_errstatus(status) raise FitsError("CFITSIO: %s" % msg, status) self._fptr = None self._name = None
def create(self, imageData): self._imageData = imageData typecode = imageData.typecode() if (typecode == Numeric.Float0 or \ typecode == Numeric.Float8 or \ typecode == Numeric.Float16 or \ typecode == Numeric.Float32): bitpix = -32 self._writeFunction = cfitsio.fits_write_img_flt elif (typecode == Numeric.Float or \ typecode == Numeric.Float64): bitpix = -64 self._writeFunction = cfitsio.fits_write_img_dbl elif (typecode == Numeric.Int0 or \ typecode == Numeric.Int8): bitpix = 8 self._writeFunction = cfitsio.fits_write_img_sht elif (typecode == Numeric.Int16): bitpix = 16 self._writeFunction = cfitsio.fits_write_img_int elif (typecode == Numeric.Int or \ typecode == Numeric.Int32): bitpix = 32 self._writeFunction = cfitsio.fits_write_img_lng else: msg = "Invalid typecode '%s' for image data" % imageData.typecode() raise FitsError(msg) naxes = list(imageData.shape) naxis = len(naxes) status = cfitsio.fits_create_img(self._fptr, \ bitpix, \ naxis, \ naxes) if status: msg = cfitsio.fits_get_errstatus(status) raise FitsError("CFITSIO: %s" % msg, status)