def loadFitsCube(self,name_dat, name_err=None, name_var=None, xscale=1, yscale=1, hdu=0): ### Open Fits file fits = pyfits.open(name_dat) if hdu==0: if fits[hdu].data is None: hdu=1 dat = fits[hdu].data dim = dat.shape ### Testing whether the data has 3 dimensions, if not raise error if len(dim)==3: ### Fill all instance variables self.dataCube = dat self.header = fits[hdu].header self.wDim = dim[0] self.yDim = dim[1] self.xDim = dim[2] self.xScale = xscale self.yScale = yscale self.crval3 = [self.header['crval3']] self.cdelt3 = [self.header['cdelt3']] self.naxis3 = [self.header['naxis3']] ### Create the wavelength array wave = numpy.zeros(self.wDim,dtype="float32") for i in range(len(self.naxis3)): if i == 0: wave[0:self.naxis3[i]] = numpy.arange(self.naxis3[i])*self.cdelt3[i]+self.crval3[i] elif i>0: wave[self.naxis3[i-1]:self.naxis3[i]] = numpy.arange(self.naxis3[i])*self.cdelt3[i]+self.crval3[i] self.wave = wave self.empty = False ### Reading corresponding variance frame when given if not name_err is None: fits = pyfits.open(name_err) err = fits[hdu].data dim_err = err.shape ### Test whether the cube has 3 Dimensions if len(dim)==3: self.errCube=err else: raise IFUcubeIOError("The Frame is not an IFU cube!") ### Reading corresponding variance frame when given if name_var is not None: fits = pyfits.open(name_var) var = fits[hdu].data dim_var = var.shape ### Test whether the cube has 3 Dimensions if len(dim)==3: self.errCube=numpy.sqrt(var) else: raise IFUcubeIOError("The Frame is not an IFU cube") else: raise IFUcubeIOError("The Frame is not an IFU cube" )
def extractImageMask(self, mask, mode='sum'): """Extract an image from the IFU cube in the given Mask. Available modes are 'sum','median','mean'.""" if mask.shape[0]!=self.wDim: raise IFUcubeIOError("Mask does not match wavelength dimension" ) if not self.empty: if mode == 'sum': image = numpy.sum(self.dataCube[mask, :, :], 0) elif mode == 'median': image = numpy.median(self.dataCube[mask, :, :], 0) elif mode == 'mean': image = numpy.mean(self.dataCube[mask, :, :],0) return image else: raise IFUcubeIOError("No data in IFU cube object" )
def extractSpecMask(self, mask, mode='sum'): if not self.empty and self.xDim == mask.shape[1] and self.yDim == mask.shape[0]: rss = numpy.reshape(self.dataCube, (self.wDim, self.yDim*self.xDim), 'C') ma_rss = numpy.reshape(mask, (self.yDim*self.xDim), 'c') if mode == 'sum': spec = numpy.sum(rss[:, ma_rss], 1) elif mode == 'median': spec = numpy.median(rss[:, ma_rss], 1) elif mode == 'mean': spec = numpy.mean(rss[:, ma_rss], 1) return spec elif not self.empty and (self.xDim != mask.shape[1] or self.yDim != mask.shape[0]): raise IFUcubeIOError("Cube Operation on a wrong shape") else: raise IFUcubeIOError("No data in IFU cube object" )
def loadFitsVarCube(self, name_var, hdu=0): fits = pyfits.open(name_var) dat = numpy.sqrt(fits[hdu].data) hdr = fits[0].header dim = dat.shape if dim!=(self.wDim, self.yDim, self.xDim) or [hdr['crval3']]!=self.crval3 or [hdr['cdelt3']]!=self.cdelt3: raise IFUcubeIOError("This is not a corresponding IFU Variance cube") else: self.errCube = dat
def extractImage(self, start_wave, end_wave, mode='sum'): """Extract an image from the IFU cube in the given wavelength range. Available modes are 'sum','median','mean'.""" if not self.empty: selectWave = numpy.logical_and(self.wave>=start_wave, self.wave<=end_wave) if mode == 'sum': image = numpy.sum(self.dataCube[selectWave, :, :], 0) elif mode == 'median': image = numpy.median(self.dataCube[selectWave, :, :], 0) elif mode == 'mean': image = numpy.mean(self.dataCube[selectWave, :, :], 0) return image else: raise IFUcubeIOError("No data in IFU cube object" )
def writeFitsErr(self, nameErr): if not self.emtpy and len(self.errCube.shape)==3: hdu = pyfits.PrimaryHDU(self.errCube) ## Update header if not self.header == []: hdu.header = self.header hdu.header.update('crval3', self.crval3[0]) hdu.header.update('cdelt3', self.cdelt3[0]) hdu.header.update('crval2', 0) hdu.header.update('cdelt2', self.yScale) hdu.header.update('crval1', 0) hdu.header.update('cdelt1', self.xScale) hdu.writeto(nameErr, clobber=True) else: raise IFUcubeIOError("The Frame is not an IFU cube" )
def writeFitsData(self, nameDat): """Write the current IFU data cube to a FITS file""" if not self.empty: hdu = pyfits.PrimaryHDU(self.dataCube) ## Update header if not self.header == []: hdu.header = self.header hdu.header['crval3'] = self.crval3[0] hdu.header['cdelt3'] = self.cdelt3[0] hdu.header['crval2'] = 0 hdu.header['cdelt2'] = self.yScale hdu.header['crval1'] = 0 hdu.header['cdelt1'] = self.xScale hdu.writeto(nameDat, clobber=True) else: raise IFUcubeIOError("The Frame is not an IFU cube" )