def rebin(self, reference_wcs, exact=True, parallel=True): """ This function ... :param reference_wcs: :param exact: :param parallel: :return: """ # Calculate rebinned data and footprint of the original image if exact: new_data, footprint = reproject_exact( (self._data, self.wcs), reference_wcs, shape_out=reference_wcs.shape, parallel=parallel) else: new_data, footprint = reproject_interp( (self._data, self.wcs), reference_wcs, shape_out=reference_wcs.shape) # Replace the data and WCS self._data = new_data self._wcs = reference_wcs # Return the footprint return Frame(footprint)
def alt_decals_sdss_match_func(set_ra, set_dec, set_z, decals_file, sdss_file, out_file): Ns = len(set_z) for kk in range( Ns ): ra_g, dec_g, z_g = set_ra[kk], set_dec[kk], set_z[kk] ### decals imgs desi_data = fits.open( decals_file % (ra_g, dec_g, z_g),) Head_0 = desi_data[0].header desi_img = desi_data[0].data ### sdss imgs sdss_data = fits.open( sdss_file % (ra_g, dec_g, z_g),) Head_1 = sdss_data[0].header relign_img = reproject_exact(desi_data[1], Head_1,)[0] ### flux unit conversion relign_img = relign_img * 10**(-3) ### save the reproject imgs hdu = fits.PrimaryHDU() hdu.data = relign_img hdu.header = Head_1 hdu.writeto( out_file % (ra_g, dec_g, z_g), overwrite = True) return
def _reproject_to_wcs(self, geom, mode='interp', order=1): from reproject import reproject_interp, reproject_exact data = np.empty(geom.data_shape) for img, idx in self.iter_by_image(): # TODO: Create WCS object for image plane if # multi-resolution geom shape_out = geom.get_image_shape(idx)[::-1] if self.geom.projection == 'CAR' and self.geom.is_allsky: vals, footprint = reproject_car_to_wcs((img, self.geom.wcs), geom.wcs, shape_out=shape_out) elif mode == 'interp': vals, footprint = reproject_interp((img, self.geom.wcs), geom.wcs, shape_out=shape_out) elif mode == 'exact': vals, footprint = reproject_exact((img, self.geom.wcs), geom.wcs, shape_out=shape_out) else: raise TypeError( "Invalid reprojection mode, either choose 'interp' or 'exact'") data[idx] = vals return self._init_copy(geom=geom, data=data)
def _reproject_to_wcs(self, geom, mode="interp", order=1): from reproject import reproject_interp, reproject_exact data = np.empty(geom.data_shape) for img, idx in self.iter_by_image(): # TODO: Create WCS object for image plane if # multi-resolution geom shape_out = geom.get_image_shape(idx)[::-1] if self.geom.projection == "CAR" and self.geom.is_allsky: vals, footprint = reproject_car_to_wcs( (img, self.geom.wcs), geom.wcs, shape_out=shape_out ) elif mode == "interp": vals, footprint = reproject_interp( (img, self.geom.wcs), geom.wcs, shape_out=shape_out ) elif mode == "exact": vals, footprint = reproject_exact( (img, self.geom.wcs), geom.wcs, shape_out=shape_out ) else: raise TypeError( "mode must be 'interp' or 'exact'. Got: {!r}".format(mode) ) data[idx] = vals return self._init_copy(geom=geom, data=data)
def getReference(image_in, header_in, reference_fits_file, useExactReproj=False): """Return the reference image aligned with the input image. getReference accepts a numpy array (masked or not) and a header with WCS information and a master reference fits file and return a reprojected reference image array for the same portion of the sky. Return reference_image""" from reproject import reproject_interp, reproject_exact refHasWCS = _headerHasWCS(fits.getheader(reference_fits_file)) refhdulist = fits.open(reference_fits_file) ref_mask = np.zeros(refhdulist[0].data.shape, dtype='bool') for anhdu in refhdulist[1:]: #Combine all the 'bad' and 'mask' masks into a single one, if any if any(s in anhdu.name for s in ["bad", "mask"]): ref_mask = ref_mask | anhdu.data #Check if there is a header available if (header_in is not None) and _headerHasWCS(header_in) and refHasWCS: #reproject with reproject here... if useExactReproj: ref_reproj_data, __ = reproject_exact(refhdulist[0], header_in) else: ref_reproj_data, __ = reproject_interp(refhdulist[0], header_in) ref_reproj_mask, __ = reproject_interp((ref_mask, refhdulist[0].header), header_in) gold_master = np.ma.array(data=ref_reproj_data, mask=ref_reproj_mask) else: #Here do the no WCS method gold_master = _no_wcs_available(image_in, np.ma.array(refhdulist[0].data, mask=ref_mask)) return gold_master
def regrid(self, basename, slavename): outName = string.split(slavename, '.fits')[0] outName = outName + '_rg.fits' bdata, bheader = fint.openFile(basename) sdata, sheader = fint.openFile(slavename) slave = fits.open(slavename)[0] bheader['BMIN'] = sheader['BMIN'] bheader['BMAJ'] = sheader['BMAJ'] if 'FREQ' in slave.header: bheader['FREQ'] = sheader['FREQ'] elif 'CRVAL3' in sheader: bheader['FREQ'] = sheader['CRVAL3'] #print basename #for i in base.header.keys(): # print i,'\t',base.header[i] #print slavename #for i in slave.header.keys(): # print i,'\t',slave.header[i] newslave, footprint = reproject_exact(slave, bheader) fits.writeto(outName, newslave, bheader, clobber=True) return outName
def scatter_plot(obsx, obsy, name=''): if name == '': name = "dummy" plt.clf() hdux = fits.open(obsx.filename)[0] hduy = fits.open(obsy.filename)[0] if obsx.xyscale > obsy.xyscale: reprox = hdux.data reproy, dummy = reproject_exact(hduy, hdux.header) else: reproy = hduy.data reprox, dummy = reproject_exact(hdux, hduy.header) plt.scatter(reprox, reproy, s=1) plt.savefig("scatter_" + name + ".png")
def reproject(self, reference, mode='interp', *args, **kwargs): """ Reproject image to given reference. Parameters ---------- reference : `~astropy.io.fits.Header`, or `~gammapy.image.SkyImage` Reference image specification to reproject the data on. mode : {'interp', 'exact'} Interpolation mode. *args : list Arguments passed to `~reproject.reproject_interp` or `~reproject.reproject_exact`. **kwargs : dict Keyword arguments passed to `~reproject.reproject_interp` or `~reproject.reproject_exact`. Returns ------- image : `~gammapy.image.SkyImage` Image reprojected onto ``reference``. """ from reproject import reproject_interp, reproject_exact if isinstance(reference, SkyImage): wcs_reference = reference.wcs shape_out = reference.data.shape elif isinstance(reference, fits.Header): wcs_reference = WCS(reference) shape_out = (reference['NAXIS2'], reference['NAXIS1']) else: raise TypeError("Invalid reference image. Must be either instance" "of `Header`, `WCS` or `SkyImage`.") if mode == 'interp': out = reproject_interp((self.data, self.wcs), wcs_reference, shape_out=shape_out, *args, **kwargs) elif mode == 'exact': out = reproject_exact((self.data, self.wcs), wcs_reference, shape_out=shape_out, *args, **kwargs) else: raise TypeError( "Invalid reprojection mode, either choose 'interp' or 'exact'") return self.__class__( name=self.name, data=out[0], wcs=wcs_reference, unit=self.unit, meta=self.meta, )
def FitsCutout(pathname, ra, dec, rad_arcsec, pix_width_arcsec=None, exten=0, reproj=False, variable=False, outfile=False, parallel=True, fast=True): # Open input fits and extract data if isinstance(pathname,basestring): in_fitsdata = astropy.io.fits.open(pathname) elif isinstance(pathname,astropy.io.fits.HDUList): in_fitsdata = pathname in_map = in_fitsdata[exten].data in_header = in_fitsdata[exten].header in_wcs = astropy.wcs.WCS(in_header) # If reprojection not requesed, pass input parameters to astropy cutout function if reproj==False: pos = astropy.coordinates.SkyCoord(ra, dec, unit='deg') size = astropy.units.Quantity(2.0*rad_arcsec, astropy.units.arcsec) cutout_obj = astropy.nddata.utils.Cutout2D(in_map, pos, size, wcs=in_wcs, mode='partial', fill_value=np.NaN) # Extract outputs of interest out_map = cutout_obj.data out_wcs = cutout_obj.wcs out_header = out_wcs.to_header() # If reporjection requested, pass input parameters to reprojection function (fast or thorough, as specified) if reproj==True: import reproject width_deg = ( 2.0 * float(rad_arcsec) ) / 3600.0 if pix_width_arcsec == None: pix_width_arcsec = 3600.0*np.mean(np.abs(np.diagonal(in_wcs.pixel_scale_matrix))) cutout_header = FitsHeader(ra, dec, width_deg, pix_width_arcsec) cutout_shape = ( cutout_header['NAXIS1'], cutout_header['NAXIS1'] ) try: if fast==False: cutout_tuple = reproject.reproject_exact(in_fitsdata, cutout_header, shape_out=cutout_shape, hdu_in=exten, parallel=parallel) elif fast==True: cutout_tuple = reproject.reproject_interp(in_fitsdata, cutout_header, shape_out=cutout_shape, hdu_in=exten) except Exception as exception: print(exception.message) # Extract outputs of interest try: out_map = cutout_tuple[0] except: pdb.set_trace() out_wcs = astropy.wcs.WCS(cutout_header) out_header = cutout_header # Save, tidy, and return; all to taste if outfile!=False: out_hdu = astropy.io.fits.PrimaryHDU(data=out_map, header=out_header) out_hdulist = astropy.io.fits.HDUList([out_hdu]) out_hdulist.writeto(outfile, overwrite=True) if isinstance(pathname,basestring): in_fitsdata.close() if variable==True: out_hdu = astropy.io.fits.PrimaryHDU(data=out_map, header=out_header) out_hdulist = astropy.io.fits.HDUList([out_hdu]) return out_hdulist
def regrid_exact(input_string, output_string): projection = fits.open(output_string)[0].header data_arr = fits.open(input_string)[0].data data_hdr = fits.open(input_string)[0].header array, fp = reproject_exact((data_arr, data_hdr), projection, hdu_in=0) return array
def reproject_convolved_to_original(self): if hasattr(self, 'beamConvolvedList'): self.convolvedOriginalList = [] for f in range(len(self.beamConvolvedList)): currentHead = self.beamConvolvedList[f].header currentData = self.beamConvolvedList[f].data newHead = self.fitsList[f].header newData = reproject_exact((currentData, currentHead), newHead, parallel = False)[0] HDUList = fits.HDUList(fits.ImageHDU(newData, newHead))[0] self.convolvedOriginalList.append(HDUList) elif hasattr(self, 'beamReprojectedImage'): currentHead = self.beamConvolvedImage.header currentData = self.beamConvolvedImage.data newHead = self.fitsSingle.header newData = reproject_exact((currentData, currentHead), newHead, parallel = False)[0] HDUList = fits.HDUList(fits.ImageHDU(newData, newHead))[0] self.convolvedOriginalSingle.append(HDUList) else: raise AttributeError('No convolved images detected. Unable to reproject.')
def regrid_to_beam(self, beampixel = 1): if hasattr(self, 'fitsList'): self.beamReprojectedList = [] for f in self.fitsList: oldHead = f.header oldData = f.data newHead = self.generate_new_header(oldHead, beampixel) newData = reproject_exact((oldData, oldHead), newHead, parallel = False)[0] HDUList = fits.HDUList(fits.ImageHDU(newData, newHead))[0] self.beamReprojectedList.append(HDUList) elif hasattr(self, 'fitsSingle'): oldHead = self.fitsSingle.header oldData = self.fitsSingle.data newHead = self.generate_new_header(oldHead, beampixel) newData = reproject_exact((oldData, oldHead), newHead, parallel = False)[0] HDUList = fits.HDUList(fits.ImageHDU(newData, newHead))[0] self.beamReprojectedImage = HDUList else: raise AttributeError('No images detected. Unable to reproject to beam grid.')
def regrid(self, regrid_hdr, parallel=True): """ Regrid image to new header """ logging.debug('%s: regridding' % (self.imagefile)) self.img_data, __footprint = reproject_exact( (self.img_data, self.img_hdr), regrid_hdr, parallel=parallel) beam = self.get_beam() freq = find_freq(self.img_hdr) self.img_hdr = regrid_hdr self.img_hdr['FREQ'] = freq self.set_beam(beam) # retain beam info if not present in regrd_hdr self.get_degperpixel() # update
def GALEX_Reproject(id_string, band, out_hdr, list_file, gal_dir, swarp_dir): # To make sure we go in order and catch problems, we skip weight files in the first instance if '.wgt.fits' in list_file: return # First, reproject the actual integration map; if that fails, just move on with life print('Reprojecting map '+list_file) os.chdir(os.path.join(gal_dir, band+'_Reproject_Temp')) try: out_img = reproject.reproject_exact(os.path.join(gal_dir,band+'_Reproject_Temp',list_file), out_hdr, parallel=False)[0] astropy.io.fits.writeto(os.path.join(swarp_dir,list_file), data=out_img, header=out_hdr) except: return # Next, reproject the weight map; if that fails, delete the corresponding integration file try: out_img = reproject.reproject_exact(os.path.join(gal_dir,band+'_Reproject_Temp',list_file.replace('.fits','.wgt.fits')), out_hdr, parallel=False)[0] astropy.io.fits.writeto(os.path.join(swarp_dir,list_file.replace('.fits','.wgt.fits')), data=out_img, header=out_hdr) except: if ('-int.fits' in list_file) and (os.path.exists(os.path.join(gal_dir,band+'_Reproject_Temp',list_file.replace('.fits','.wgt.fits')))): os.remove(os.path.join(gal_dir,band+'_Reproject_Temp',list_file.replace('.fits','.wgt.fits'))) if ('.wgt.fits' in list_file) and (os.path.exists(os.path.join(gal_dir,band+'_Reproject_Temp',list_file.replace('.wgt.fits','.fits')))): os.remove(os.path.join(gal_dir,band+'_Reproject_Temp',list_file.replace('.wgt.fits','.fits')))
def rebin(self, reference_wcs, exact=False, parallel=True, threshold=0.5, dilate=False, rank=2, connectivity=1, iterations=2): """ This function ... :param reference_wcs: :param exact: :param parallel: :param threshold: :param dilate: :param rank: :param connectivity: :param iterations: :return: """ from .frame import Frame # Check whether the frame has a WCS if not self.has_wcs: raise RuntimeError("Cannot rebin a mask without coordinate system") # Check whether the WCS is the same if self.wcs == reference_wcs: return Frame.ones_like(self) #from ..tools import plotting #print(self._data.shape) #plotting.plot_box(self._data.astype(int), title="before") # Calculate rebinned data and footprint of the original image if exact: new_data, footprint = reproject_exact((self._data.astype(int), self.wcs), reference_wcs, shape_out=reference_wcs.shape, parallel=parallel) else: new_data, footprint = reproject_interp((self._data.astype(int), self.wcs), reference_wcs, shape_out=reference_wcs.shape) #from ..tools import plotting #print(new_data.shape) #plotting.plot_box(new_data, title="after") # Get binary mask data mask_data = np.logical_or(new_data > threshold, np.isnan(new_data)) # Replace the data and WCS self._data = mask_data self._wcs = reference_wcs.copy() # Dilate? if dilate: self.dilate_rc(rank, connectivity=connectivity, iterations=iterations) # 1 also worked in test # Return the footprint return Frame(footprint, wcs=reference_wcs.copy())
def reproject(self, reference, mode='interp', *args, **kwargs): """ Reproject image to given reference. Parameters ---------- reference : `~astropy.io.fits.Header`, or `~gammapy.image.SkyImage` Reference image specification to reproject the data on. mode : {'interp', 'exact'} Interpolation mode. *args : list Arguments passed to `~reproject.reproject_interp` or `~reproject.reproject_exact`. **kwargs : dict Keyword arguments passed to `~reproject.reproject_interp` or `~reproject.reproject_exact`. Returns ------- image : `~gammapy.image.SkyImage` Image reprojected onto ``reference``. """ from reproject import reproject_interp, reproject_exact if isinstance(reference, SkyImage): wcs_reference = reference.wcs shape_out = reference.data.shape elif isinstance(reference, fits.Header): wcs_reference = WCS(reference) shape_out = (reference['NAXIS2'], reference['NAXIS1']) else: raise TypeError("Invalid reference image. Must be either instance" "of `Header`, `WCS` or `SkyImage`.") if mode == 'interp': out = reproject_interp((self.data, self.wcs), wcs_reference, shape_out=shape_out, *args, **kwargs) elif mode == 'exact': out = reproject_exact((self.data, self.wcs), wcs_reference, shape_out=shape_out, *args, **kwargs) else: raise TypeError("Invalid reprojection mode, either choose 'interp' or 'exact'") return self.__class__( name=self.name, data=out[0], wcs=wcs_reference, unit=self.unit, meta=self.meta, )
def _reproject_wcs(self, geom, mode='interp', order=1): from reproject import reproject_interp, reproject_exact map_out = WcsMapND(geom) axes_eq = np.all( [ax0 == ax1 for ax0, ax1 in zip(geom.axes, self.geom.axes)]) for vals, idx in map_out.iter_by_image(): if self.geom.ndim == 2 or axes_eq: img = self.data[idx[::-1]] else: coords = axes_pix_to_coord(geom.axes, idx) img = self.interp_image(coords, order=order).data # FIXME: This is a temporary solution for handling maps # with undefined pixels if np.any(~np.isfinite(img)): img = img.copy() img[~np.isfinite(img)] = 0.0 # TODO: Create WCS object for image plane if # multi-resolution geom shape_out = geom.get_image_shape(idx)[::-1] if self.geom.projection == 'CAR' and self.geom.allsky: data, footprint = reproject_car_to_wcs((img, self.geom.wcs), geom.wcs, shape_out=shape_out) elif mode == 'interp': data, footprint = reproject_interp((img, self.geom.wcs), geom.wcs, shape_out=shape_out) elif mode == 'exact': data, footprint = reproject_exact((img, self.geom.wcs), geom.wcs, shape_out=shape_out) else: raise TypeError( "Invalid reprojection mode, either choose 'interp' or 'exact'" ) vals[...] = data return map_out
def _transform(self,userepr=False): from reproject import reproject_interp,reproject_exact if self.hdu[0].header == self.reproject: return if userepr: to_glob = os.path.splitext(self.filename)[0] reprfile = ''.join([to_glob,'.repr.fits']) print 'Using repr for reproj' self._rotate_WORKING() if self.exact: array,_= reproject_exact(self.hdu, self.reproject,hdu_in=0) else: array,_= reproject_interp(self.hdu, self.reproject,order='biquadratic') self.hdu[0].data = array self.w = WCS(self.reproject) self.data = self.hdu[0].data
def _reproject_wcs(self, geom, mode='interp', order=1): from reproject import reproject_interp, reproject_exact map_out = WcsNDMap(geom) axes_eq = np.all([ax0 == ax1 for ax0, ax1 in zip(geom.axes, self.geom.axes)]) for vals, idx in map_out.iter_by_image(): if self.geom.ndim == 2 or axes_eq: img = self.data[idx[::-1]] else: coords = axes_pix_to_coord(geom.axes, idx) img = self.interp_image(coords, order=order).data # FIXME: This is a temporary solution for handling maps # with undefined pixels if np.any(~np.isfinite(img)): img = img.copy() img[~np.isfinite(img)] = 0.0 # TODO: Create WCS object for image plane if # multi-resolution geom shape_out = geom.get_image_shape(idx)[::-1] if self.geom.projection == 'CAR' and self.geom.is_allsky: data, footprint = reproject_car_to_wcs((img, self.geom.wcs), geom.wcs, shape_out=shape_out) elif mode == 'interp': data, footprint = reproject_interp((img, self.geom.wcs), geom.wcs, shape_out=shape_out) elif mode == 'exact': data, footprint = reproject_exact((img, self.geom.wcs), geom.wcs, shape_out=shape_out) else: raise TypeError( "Invalid reprojection mode, either choose 'interp' or 'exact'") vals[...] = data return map_out
def rebin(self, reference_wcs, exact=False, parallel=True, threshold=0.5): """ This function ... :param reference_wcs: :param exact: :param parallel: :param threshold: :return: """ # Check whether the frame has a WCS if not self.has_wcs: raise RuntimeError("Cannot rebin a mask without coordinate system") # Calculate rebinned data and footprint of the original image if exact: new_data, footprint = reproject_exact( (self._data.astype(int), self.wcs), reference_wcs, shape_out=reference_wcs.shape, parallel=parallel) else: new_data, footprint = reproject_interp( (self._data.astype(int), self.wcs), reference_wcs, shape_out=reference_wcs.shape) #print(new_data) #print(np.sum(np.isnan(new_data))) #print(new_data > threshold) #print(np.isnan(new_data)) mask_data = np.logical_or(new_data > threshold, np.isnan(new_data)) #print(mask_data) # Replace the data and WCS self._data = mask_data self._wcs = reference_wcs.copy() # Return the footprint from .frame import Frame return Frame(footprint, wcs=reference_wcs.copy())
def rebin(self, reference_wcs, exact=True, parallel=True): """ This function ... :param reference_wcs: :param exact: :param parallel: :return: """ # Calculate rebinned data and footprint of the original image if exact: new_data, footprint = reproject_exact((self._data, self.wcs), reference_wcs, shape_out=reference_wcs.shape, parallel=parallel) else: new_data, footprint = reproject_interp((self._data, self.wcs), reference_wcs, shape_out=reference_wcs.shape) # Replace the data and WCS self._data = new_data self._wcs = reference_wcs # Return the footprint return Frame(footprint)
def getReference(image_in, header_in, reference_fits_file, useExactReproj=False): """Return the reference image aligned with the input image. getReference accepts a numpy array (masked or not) and a header with WCS information and a master reference fits file and return a reprojected reference image array for the same portion of the sky. Return reference_image""" from reproject import reproject_interp, reproject_exact refHasWCS = _headerHasWCS(fits.getheader(reference_fits_file)) refhdulist = fits.open(reference_fits_file) ref_mask = np.zeros(refhdulist[0].data.shape, dtype='bool') for anhdu in refhdulist[1:]: #Combine all the 'bad' and 'mask' masks into a single one, if any if any(s in anhdu.name for s in ["bad", "mask"]): ref_mask = ref_mask | anhdu.data #Check if there is a header available if (header_in is not None) and _headerHasWCS(header_in) and refHasWCS: #reproject with reproject here... if useExactReproj: ref_reproj_data, __ = reproject_exact(refhdulist[0], header_in) else: ref_reproj_data, __ = reproject_interp(refhdulist[0], header_in) ref_reproj_mask, __ = reproject_interp( (ref_mask, refhdulist[0].header), header_in) gold_master = np.ma.array(data=ref_reproj_data, mask=ref_reproj_mask) else: #Here do the no WCS method gold_master = _no_wcs_available( image_in, np.ma.array(refhdulist[0].data, mask=ref_mask)) return gold_master
def grid_resample(src_path, new_wcs, exact=False, **kwargs): with pf.open(src_path) as hdulist: if (len(hdulist) > 1): sys.stderr.write("Unexpected HDU count: %d\n" % len(hdulist)) raise src_hdu = hdulist[0] new_hdr = hdulist[0].header.copy(strip=True)[:120] # trash old WCS if exact: new_img, footprint = reproject_exact(src_hdu, new_wcs, shape_out=new_wcs.pixel_shape, parallel=4) else: new_img, footprint = reproject_interp( src_hdu, new_wcs, shape_out=new_wcs.pixel_shape, order='bilinear') #shape_out=new_wcs.pixel_shape, order='biquadratic') #shape_out=new_wcs.pixel_shape, order='bicubic') new_hdr.update(new_wcs.to_header()) return new_img, new_hdr, footprint
def decals_sdss_match_func(set_ra, set_dec, set_z, decals_file, sdss_file, out_file): Ns = len(set_z) for kk in range(Ns): ra_g, dec_g, z_g = set_ra[kk], set_dec[kk], set_z[kk] ### decals imgs desi_data = fits.open(decals_file % (ra_g, dec_g, z_g), ) Head_0 = desi_data[0].header desi_img = desi_data[0].data cen_x = np.int(Head_0['CRPIX1']) cen_y = np.int(Head_0['CRPIX2']) coord_0 = awc.WCS(Head_0) ### sdss imgs sdss_data = fits.open(sdss_file % (ra_g, dec_g, z_g), ) Head_1 = sdss_data[0].header sdss_img = sdss_data[0].data CPx = np.int(Head_1['CRPIX1']) CPy = np.int(Head_1['CRPIX2']) coord_1 = awc.WCS(Head_1) relign_img = reproject_exact( desi_data, Head_1, )[0] ### save the reproject imgs hdu = fits.PrimaryHDU() hdu.data = relign_img hdu.header = Head_1 hdu.writeto(out_file % (ra_g, dec_g, z_g), overwrite=True) return
def reproject_to(self, target_wcs, algorithm='interpolation', shape_out=None, order='bilinear', output_array=None, parallel=False, return_footprint=False): """ Reprojects this NDCube to the coordinates described by another WCS object. Parameters ---------- algorithm: `str` The algorithm to use for reprojecting. This can be any of: 'interpolation', 'adaptive', and 'exact'. target_wcs : `astropy.wcs.wcsapi.BaseHighLevelWCS`, `astropy.wcs.wcsapi.BaseLowLevelWCS`, or `astropy.io.fits.Header` The WCS object to which the ``NDCube`` is to be reprojected. shape_out: `tuple`, optional The shape of the output data array. The ordering of the dimensions must follow NumPy ordering and not the WCS pixel shape. If not specified, `~astropy.wcs.wcsapi.BaseLowLevelWCS.array_shape` attribute (if available) from the low level API of the ``target_wcs`` is used. order: `int` or `str` The order of the interpolation (used only when the 'interpolation' or 'adaptive' algorithm is selected). For 'interpolation' algorithm, this can be any of: 'nearest-neighbor', 'bilinear', 'biquadratic', and 'bicubic'. For 'adaptive' algorithm, this can be either 'nearest-neighbor' or 'bilinear'. output_array: `numpy.ndarray`, optional An array in which to store the reprojected data. This can be any numpy array including a memory map, which may be helpful when dealing with extremely large files. parallel: `bool` or `int` Flag for parallel implementation (used only when the 'exact' algorithm is selected). If ``True``, a parallel implementation is chosen and the number of processes is selected automatically as the number of logical CPUs detected on the machine. If ``False``, a serial implementation is chosen. If the flag is a positive integer n greater than one, a parallel implementation using n processes is chosen. return_footprint: `bool` Whether to return the footprint in addition to the output NDCube. Returns ------- resampled_cube : `ndcube.NDCube` A new resultant NDCube object, the supplied ``target_wcs`` will be the ``.wcs`` attribute of the output ``NDCube``. footprint: `numpy.ndarray` Footprint of the input array in the output array. Values of 0 indicate no coverage or valid values in the input image, while values of 1 indicate valid values. Notes ----- This method doesn't support handling of the ``mask``, ``extra_coords``, and ``uncertainty`` attributes yet. However, ``meta`` and ``global_coords`` are copied to the output ``NDCube``. """ try: from reproject import reproject_adaptive, reproject_exact, reproject_interp from reproject.wcs_utils import has_celestial except ModuleNotFoundError: raise ImportError( "The NDCube.reproject_to method requires the optional package `reproject`." ) if isinstance(target_wcs, Mapping): target_wcs = WCS(header=target_wcs) low_level_target_wcs = utils.wcs.get_low_level_wcs( target_wcs, 'target_wcs') # 'adaptive' and 'exact' algorithms work only on 2D celestial WCS. if algorithm == 'adaptive' or algorithm == 'exact': if low_level_target_wcs.pixel_n_dim != 2 or low_level_target_wcs.world_n_dim != 2: raise ValueError( 'For adaptive and exact algorithms, target_wcs must be 2D.' ) if not has_celestial(target_wcs): raise ValueError( 'For adaptive and exact algorithms, ' 'target_wcs must contain celestial axes only.') if not utils.wcs.compare_wcs_physical_types(self.wcs, target_wcs): raise ValueError( 'Given target_wcs is not compatible with this NDCube, the physical types do not match.' ) # If shape_out is not specified explicity, try to extract it from the low level WCS if not shape_out: if hasattr(low_level_target_wcs, 'array_shape' ) and low_level_target_wcs.array_shape is not None: shape_out = low_level_target_wcs.array_shape else: raise ValueError( "shape_out must be specified if target_wcs does not have the array_shape attribute." ) self._validate_algorithm_and_order(algorithm, order) if algorithm == 'interpolation': data = reproject_interp(self, output_projection=target_wcs, shape_out=shape_out, order=order, output_array=output_array, return_footprint=return_footprint) elif algorithm == 'adaptive': data = reproject_adaptive(self, output_projection=target_wcs, shape_out=shape_out, order=order, return_footprint=return_footprint) elif algorithm == 'exact': data = reproject_exact(self, output_projection=target_wcs, shape_out=shape_out, parallel=parallel, return_footprint=return_footprint) if return_footprint: data, footprint = data resampled_cube = type(self)(data, wcs=target_wcs, meta=deepcopy(self.meta)) resampled_cube._global_coords = deepcopy(self.global_coords) if return_footprint: return resampled_cube, footprint return resampled_cube
# Load newly Astronmety.net processed Science fits file # In[23]: sci_frame_adn = fits.open(adn_sci_image_name) # **Reproject the Reference Frame into the Science Frame WCS coordinates** # In[24]: refproj_out = reproject_exact(ref_frame_adn[0], sci_frame_adn[0].header) # **Image Registration** # # Register (i.e. interpolate) the reference frame into the science frame pixel coordiantes # In[25]: # Determine the magnitude of the shfit in X and Y pixel coordinates xshift, yshift, xshifterr, yshifterr = chi2_shift(refproj_out[0], sci_frame_adn[0].data) # Perform the shift in X and Y pixel coordinates refproj_imReg_out = fft_tools.shift2d(refproj_out[0], xshift, yshift)
def plot_composites(pdata,idx_plot,outfolder,contours,contour_colors=True,calibration_plot=True): ### open figure #fig, ax = plt.subplots(5,2, figsize=(7, 15)) #fig, ax = plt.subplots(1,1, figsize=(7, 7),subplot_kw={'projection': ccrs.PlateCarree()}) #ax = np.ravel(ax) ### image qualities fs = 10 maxlim = 0.05 ### filters #filters = ['SDSS u','SDSS g','SDSS i'] #fcolors = ['Blues','Greens','Reds'] #ftext = ['blue','green','red'] filters = ['SDSS i'] fcolors = ['Greys'] ftext = ['black'] ### contour color limits (customized for W1-W2) color_limits = [-1.0,2.6] kernel = None ### begin loop for ii,idx in enumerate(idx_plot): ### load object information objname = pdata['objname'][idx] fagn = pdata['pars']['fagn']['q50'][idx] ra, dec = load_coordinates(objname) phot_size = load_structure(objname,long_axis=True) # in arcseconds ### set up figure fig, ax = None, None xs, ys, dely = 0.05,0.9, 0.07 for kk,filt in enumerate(filters): hdu = load_image(objname,filt) #### if it's the first filter, #### set up WCS using this information if fig == None: ### grab WCS information, create figure + axis wcs = WCS(hdu.header) fig, ax = plt.subplots(2,3, figsize=(18, 18)) plt.subplots_adjust(top=0.95,bottom=0.33) sedax = fig.add_axes([0.3,0.05,0.4,0.25]) ax = np.ravel(ax) ### translate object location into pixels using WCS coordinates pix_center = wcs.all_world2pix([[ra[0],dec[0]]],1) size = calc_dist(wcs, pix_center, phot_size, hdu.data.shape) hdu_original = copy.deepcopy(hdu.header) data_to_plot = hdu.data ### build image extents # first calculate pixel location of image left, image bottom center_pix = np.atleast_2d([(size[0]+size[1])/2.,(size[2]+size[3])/2.]) center_left_pix = np.atleast_2d([size[0],center_pix[0][1]]) center_bottom_pix = np.atleast_2d([center_pix[0][0],size[2]]) # now wcs location center_left_wcs = wcs.all_pix2world(center_left_pix,0) center_bottom_wcs = wcs.all_pix2world(center_bottom_pix,0) center_wcs = wcs.all_pix2world(center_pix,0) # now calculate distance center = SkyCoord(ra=center_wcs[0][0]*u.degree,dec=center_wcs[0][1]*u.degree) center_left = SkyCoord(ra=center_left_wcs[0][0]*u.degree,dec=center_left_wcs[0][1]*u.degree) center_bottom = SkyCoord(ra=center_bottom_wcs[0][0]*u.degree,dec=center_bottom_wcs[0][1]*u.degree) ydist = center.separation(center_bottom).arcsec xdist = center.separation(center_left).arcsec extent = [-xdist,xdist,-ydist,ydist] #### if it's not the first filter, #### project into WCS of first filter # see reprojection https://reproject.readthedocs.io/en/stable/ else: data_to_plot, footprint = reproject_exact(hdu, hdu_original) plot_image(ax[5],data_to_plot[size[2]:size[3],size[0]:size[1]],size,cmap=fcolors[kk],extent=extent) ax[5].text(xs, ys, filters[kk]+'-band',color=ftext[kk],transform=ax[5].transAxes) ys -= dely ### draw 6" line wise_psf = 6 # in arcseconds start = -0.85*xdist ax[5].plot([start,start+wise_psf],[start,start],lw=2,color='k') ax[5].text(start+wise_psf/2.,start+1, '6"', ha='center') ax[5].set_xlim(-xdist,xdist) # reset plot limits b/c of text stuff ax[5].set_ylim(-ydist,ydist) ax[5].set_xlabel('arcseconds') ax[5].set_ylabel('arcseconds') #### load up HDU, subtract background and convert to physical units # also convolve to W2 resolution hdu = load_image(objname,contours[0]) hdu.data *= 1.9350E-06 ### convert from DN to flux in Janskies, from this table: http://wise2.ipac.caltech.edu/docs/release/allsky/expsup/sec2_3f.html hdu.data -= np.median(hdu.data) ### subtract background as median data1_noconv, footprint = reproject_exact(hdu, hdu_original) data_convolved, kernel = match_resolution(hdu.data,contours[0],contours[1],kernel=kernel,data1_res=hdu.header['PXSCAL1']) # convolve to W2 resolution hdu.data = data_convolved data1, footprint = reproject_exact(hdu, hdu_original) ### load up HDU2, subtract background, convert to physical units hdu = load_image(objname,contours[1]) hdu.data -= np.median(hdu.data) ### subtract background as median hdu.data *= 2.7048E-06 ### convert from DN to flux in Janskies, from this table: http://wise2.ipac.caltech.edu/docs/release/allsky/expsup/sec2_3f.html #### put onto same scale data2, footprint = reproject_exact(hdu, hdu_original) ### plot the main result data1_slice = data1[size[2]:size[3],size[0]:size[1]] data2_slice = data2[size[2]:size[3],size[0]:size[1]] plot_color_contour(ax[5],data1_slice, data2_slice, contours[0],contours[1], maxlim=maxlim, color_limits=color_limits) #ax[5].text(xs, ys, 'contours:' +contours[0]+'-'+contours[1],transform=ax[5].transAxes) ys -= dely ### labels and limits ax[5].text(0.98,0.93,objname,transform=ax[5].transAxes,ha='right') ax[5].text(0.98,0.88,r'f$_{\mathrm{AGN,MIR}}$='+"{:.2f}".format(fagn),transform=ax[5].transAxes, ha='right') ax[5].set_title('WISE colors on\nSDSS imaging') #### CALIBRATION PLOT flux_color = convert_to_color(data1_slice, data2_slice,contours[0],contours[1],minflux=1e-10) img = ax[0].imshow(data1_noconv[size[2]:size[3],size[0]:size[1]], origin='lower',extent=extent) cbar = fig.colorbar(img, ax=ax[0]) cbar.formatter.set_powerlimits((0, 0)) cbar.update_ticks() ax[0].set_title(contours[0]+', \n raw') img = ax[1].imshow(data1_slice, origin='lower',extent=extent) cbar = fig.colorbar(img, ax=ax[1]) cbar.formatter.set_powerlimits((0, 0)) cbar.update_ticks() ax[1].set_title(contours[0]+', \n convolved to W2 PSF') img = ax[2].imshow(data2_slice, origin='lower',extent=extent) cbar = fig.colorbar(img, ax=ax[2]) cbar.formatter.set_powerlimits((0, 0)) cbar.update_ticks() ax[2].set_title(contours[1]+', \n raw') img = ax[3].imshow(flux_color, origin='lower',extent=extent,vmin=color_limits[0],vmax=color_limits[1]) cbar = fig.colorbar(img, ax=ax[3]) ax[3].set_title(contours[0]+'-'+contours[1]+', \n raw') ### don't trust anything less than X times the max! max1 = np.nanmax(data1_slice) max2 = np.nanmax(data2_slice) background = (data1_slice < max1*maxlim) | (data2_slice < max2*maxlim) flux_color[background] = np.nan img = ax[4].imshow(flux_color, origin='lower',extent=extent,vmin=color_limits[0],vmax=color_limits[1]) cbar = fig.colorbar(img, ax=ax[4]) cbar.formatter.set_powerlimits((0, 0)) cbar.update_ticks() ax[4].set_title(contours[0]+'-'+contours[1]+', \n background removed') ax[4].plot([start,start+wise_psf],[start,start],lw=2,color='k') ax[4].text(start+wise_psf/2.,start+1, '6"', ha='center') ax[4].set_xlim(-xdist,xdist) # reset plot limits b/c of text stuff ax[4].set_ylim(-ydist,ydist) #### now plot the SED agn_color, noagn_color = '#FF3D0D', '#1C86EE' wavlims = (1,30) wav_idx = (pdata['observables']['wave'][ii]/1e4 > wavlims[0]) & (pdata['observables']['wave'][ii]/1e4 < wavlims[1]) sedax.plot(pdata['observables']['wave'][ii][wav_idx]/1e4,pdata['observables']['agn_on_spec'][ii][wav_idx], lw=2.5, alpha=0.5, color=agn_color) sedax.plot(pdata['observables']['wave'][ii][wav_idx]/1e4,pdata['observables']['agn_off_spec'][ii][wav_idx], lw=2.5, alpha=0.5, color=noagn_color) if type(pdata['observables']['spit_lam'][ii]) is np.ndarray: wav_idx = (pdata['observables']['spit_lam'][ii]/1e4 > wavlims[0]) & (pdata['observables']['spit_lam'][ii]/1e4 < wavlims[1]) sedax.plot(pdata['observables']['spit_lam'][ii][wav_idx]/1e4,pdata['observables']['spit_flux'][ii][wav_idx], lw=2.5, alpha=0.5, color='black') if type(pdata['observables']['ak_lam'][ii]) is np.ndarray: wav_idx = (pdata['observables']['ak_lam'][ii]/1e4 > wavlims[0]) & (pdata['observables']['ak_lam'][ii]/1e4 < wavlims[1]) sedax.plot(pdata['observables']['ak_lam'][ii][wav_idx]/1e4,pdata['observables']['ak_flux'][ii][wav_idx], lw=2.5, alpha=0.5, color='black') ### write down Vega colors sedax.text(0.95,0.1,'W1-W2(AGN ON)='+'{:.2f}'.format(pdata['observables']['agn_on_mag'][ii]),transform=sedax.transAxes,color=agn_color,ha='right') sedax.text(0.95,0.16,'W1-W2(AGN OFF)='+'{:.2f}'.format(pdata['observables']['agn_off_mag'][ii]),transform=sedax.transAxes,color=noagn_color,ha='right') sedax.text(0.95,0.22,'W1-W2(OBS)='+'{:.2f}'.format(pdata['observables']['obs_mag'][ii]),transform=sedax.transAxes,color='black',ha='right') lsfr = pdata['lsfr'][idx] if lsfr > 0: sedax.text(1.15,0.5,r'L$_{\mathrm{X}}$(obs)/L$_{\mathrm{X}}$(SFR)='+'{:.2f}'.format(lsfr),transform=sedax.transAxes,color='black',fontsize=18,weight='bold') else: sedax.text(1.15,0.5,r'No X-ray information',transform=sedax.transAxes,color='black',fontsize=18,weight='bold') bpt = pdata['bpt'][idx] if bpt == 'None': sedax.text(1.15,0.42,'No BPT measurement',transform=sedax.transAxes,color='black',fontsize=18,weight='bold') else: sedax.text(1.15,0.42,'BPT: '+bpt,transform=sedax.transAxes,color='black',fontsize=18,weight='bold') ### scaling and labels sedax.set_yscale('log',nonposx='clip',subsx=(1,2,4)) sedax.set_xscale('log',nonposx='clip',subsx=(1,2,4)) sedax.xaxis.set_minor_formatter(minorFormatter) sedax.xaxis.set_major_formatter(majorFormatter) sedax.set_xlabel(r'wavelength $\mu$m') sedax.set_ylabel(r'f$_{\nu}$') sedax.axvline(3.4, linestyle='--', color='0.5',lw=1.5,alpha=0.8,zorder=-5) sedax.axvline(4.6, linestyle='--', color='0.5',lw=1.5,alpha=0.8,zorder=-5) sedax.set_xlim(wavlims) padding = '' if ii <= 9: padding='0' plt.savefig(outfolder+'/'+padding+str(ii)+'_'+objname+'.png',dpi=150) plt.close()
def IRIS_Query(name, ra, dec, width, band, bands_dict, temp_dir, montage_path=None): # If Montage commands directory provided, append it to path try: import montage_wrapper except: sys.path.append(montage_path) os.environ['PATH'] = os.environ['PATH'] + ':' + montage_path import montage_wrapper # Generate list of all IRIS plate fields in this band (which take form iYYYBXh0.fits, where YYY is a number between 001 and 430, and X is the field between 1 and 4) iris_url = 'https://irsa.ipac.caltech.edu/data/IRIS/images/' iris_fields = np.arange(1, 431).astype(str) iris_fields = [ ''.join(['I', field.zfill(3), 'BXH0']) for field in iris_fields ] # Check if a folder for the raw IRIS plates exists in the temporary directory; if not, create it print('Ensuring all raw ' + bands_dict[band]['wavelength'] + 'um IRAS-IRIS plates are available') band = bands_dict[band]['wavelength'] raw_dir = os.path.join(temp_dir, 'Raw', band) if not os.path.exists(raw_dir): os.makedirs(raw_dir) # Look to see if all IRIS fields for this band are already present in the temporary directory; if not, wget them wget_list = [] for iris_field in np.random.permutation(iris_fields): iris_ref_file = iris_field.replace( 'X', bands_dict[band]['band_num']) + '.fits' iris_ref_path = os.path.join(raw_dir, iris_ref_file) if not os.path.exists(iris_ref_path): wget_list.append([iris_url + iris_ref_file, iris_ref_path]) if len(wget_list) > 0: print( 'Downloading raw ' + bands_dict[band]['wavelength'] + 'um IRAS-IRIS plates (note that this will entail downloding up to ~4GB of data)' ) if mp.current_process().name == 'MainProcess': joblib.Parallel( n_jobs=mp.cpu_count()-2 )\ ( joblib.delayed( IRIS_wget )\ ( wget_list[w][0], wget_list[w][1] )\ for w in range(len(wget_list)) ) else: for w in range(len(wget_list)): os.system('curl ' + wget_list[w][0] + ' -o ' + '"' + wget_list[w][1] + '"') # If image metadata table doesn't yet exist for this band, run mImgtbl over raw data to generate it mImgtbl_tablepath = os.path.join(raw_dir, 'IRIS_' + band + '_Metadata_Table.tbl') if os.path.exists(mImgtbl_tablepath): os.remove(mImgtbl_tablepath) montage_wrapper.mImgtbl(raw_dir, mImgtbl_tablepath, corners=True) # Now that we know we have data, set up processing for this source in particular print('Computing overlap of ' + bands_dict[band]['wavelength'] + 'um IRAS-IRIS plates with ' + name) ra, dec, width = float(ra), float(dec), float(width) pix_size = bands_dict[band]['pix_size'] # Find which plates have coverage over our target region mCoverageCheck_tablepath = os.path.join( raw_dir, u'IRIS_' + band + '_Coverage_Table.tbl') if os.path.exists(mCoverageCheck_tablepath): os.remove(mCoverageCheck_tablepath) montage_wrapper.mCoverageCheck(mImgtbl_tablepath, mCoverageCheck_tablepath, ra=ra, dec=dec, mode='box', width=width) # Read in coveage tables; if no coverage, write null output file and stop here print('Reprojecting IRAS-IRIS ' + bands_dict[band]['wavelength'] + 'um plates that cover ' + name) mCoverageCheck_table = np.genfromtxt(mCoverageCheck_tablepath, skip_header=3, dtype=None, encoding=None) if mCoverageCheck_table.size == 0: os.system('touch ' + os.path.join(temp_dir, '.' + name + '_IRAS-IRIS_' + band + '.null')) print('No IRAS-IRIS ' + band + 'um data for ' + name) return reproj_dir = os.path.join(temp_dir, 'Reproject', band) if not os.path.exists(reproj_dir): os.makedirs(reproj_dir) # Extract paths from coverage table, with handling for weird astropy behavior when table has only one row if mCoverageCheck_table.size == 1: raw_paths = [mCoverageCheck_table['f36'].tolist()] else: raw_paths = [ str(mCoverageCheck_table['f36'][i]) for i in range(mCoverageCheck_table['f36'].size) ] reproj_paths = [ raw_paths[i].replace(raw_dir, reproj_dir) for i in range(len(raw_paths)) ] reproj_hdr = FitsHeader(ra, dec, width, pix_size) # Reproject identified plates in turn (dealing with possible corrupt downloads, and stupid unecessary third axis, grrr) for i in range(len(raw_paths)): raw_path, reproj_path = raw_paths[i], reproj_paths[i] try: raw_img, raw_hdr = astropy.io.fits.getdata(raw_path, header=True, memmap=False) except: raw_url = iris_url + raw_path.split('/')[-1] os.system('curl ' + raw_url + ' -o ' + '"' + raw_path + '"') raw_img, raw_hdr = astropy.io.fits.getdata(raw_path, header=True, memmap=False) raw_hdr.set('NAXIS', value=2) raw_hdr.remove('NAXIS3') raw_hdr.remove('CRVAL3') raw_hdr.remove('CRPIX3') raw_hdr.remove('CTYPE3') raw_hdr.remove('CDELT3') raw_hdu = astropy.io.fits.PrimaryHDU(data=raw_img, header=raw_hdr) reproj_img = reproject.reproject_exact(raw_hdu, reproj_hdr, parallel=False)[0] astropy.io.fits.writeto(reproj_path, data=reproj_img, header=reproj_hdr, overwrite=True) del (raw_hdu) del (raw_img) del (raw_hdr) gc.collect() # Now mosaic the reprojected images mosaic_list = [] [ mosaic_list.append(astropy.io.fits.getdata(reproj_path)) for reproj_path in reproj_paths ] mosaic_array = np.array(mosaic_list) mosaic_img = np.nanmean(mosaic_array, axis=0) mosaic_hdr = FitsHeader(ra, dec, width, pix_size) """# Write finished mosaic to file astropy.io.fits.writeto(os.path.join(temp_dir,name+'_IRAS-IRIS_'+band+'.fits'), data=mosaic_img, header=mosaic_hdr, overwrite=True)""" # Check that target coords have coverage in mosaic mosaic_wcs = astropy.wcs.WCS(mosaic_hdr) mosaic_centre = mosaic_wcs.all_world2pix([[ra]], [[dec]], 0, ra_dec_order=True) mosaic_i, mosaic_j = mosaic_centre[1][0], mosaic_centre[0][0] if np.isnan(mosaic_img[int(np.round(mosaic_i)), int(np.round(mosaic_j))]): os.system('touch ' + os.path.join(temp_dir, '.' + name + '_IRAS-IRIS_' + band + '.null')) print('No IRAS-IRIS ' + band + 'um data for ' + name) # If mosaic is good, write it to temporary directory else: astropy.io.fits.writeto(os.path.join( temp_dir, name + '_IRAS-IRIS_' + band + '.fits'), data=mosaic_img, header=mosaic_hdr, overwrite=True)
import sys from astropy.io import fits from astropy.utils.data import get_pkg_data_filename #hdu1 = fits.open('chan1_pixel6_convol18_han1_mask_imfit_13co_pix_2_Tmb.fits')[0] #hdu2 = fits.open('../../OrionAdust/herschelAmelia/OrionA_all_spire250_nh_mask_corr_apex.fits')[0] #from reproject import reproject_exact #array, footprint = reproject_exact(hdu2, hdu1.header) #fits.writeto('stutz_on_13co_header.fits', array, hdu1.header, clobber=True) #hdu1 = fits.open('chan1_pixel6_convol18_han1_mask_imfit_13co_pix_2_Tmb.fits')[0] #hdu2 = fits.open('../../OrionAdust/lombardi_planck_herschel_plane3_colorT.fits')[0] #from reproject import reproject_exact #array, footprint = reproject_exact(hdu2, hdu1.header) #fits.writeto('lombardi_colorT_on_13co_header.fits', array, hdu1.header, clobber=True) hdu1 = fits.open( 'chan1_pixel6_convol18_han1_mask_imfit_13co_pix_2_Tmb.fits')[0] hdu2 = fits.open( '../../OrionAdust/lombardi_planck_herschel_plane4_colorTerror.fits')[0] from reproject import reproject_exact array, footprint = reproject_exact(hdu2, hdu1.header) fits.writeto('lombardi_colorTerror_on_13co_header.fits', array, hdu1.header, clobber=True)
from matplotlib import ticker # activate latex text rendering rc('font', **{'family': 'serif', 'serif': ['Computer Modern Roman']}) rc('text', usetex=True) K_map = fits.getdata('K_map.fits') * u.cm**(-2) * u.s**(-1) wcs = WCS(fits.getheader('K_map.fits'), naxis=2) E_span = np.linspace(50, 1000, 10000) I_spectrum = spectrum(E_span) E_flux = trapz(I_spectrum * E_span, E_span) * u.MeV integral_map = np.array((K_map * E_flux) / (u.erg * u.cm**(-2) * u.s**(-1))) integral_map = reproject_exact((integral_map, wcs), wcs, shape_out=(1441, 2881), return_footprint=False) ustr = (u.erg * u.cm**(-2) * u.s**(-1)).to_string(format="latex_inline") plt.figure(figsize=(5, 3.5)) plt.axis('off') plt.title(r'Integrated flux threshold [{}]'.format(ustr) + '\n' + r'MAMA source spectrum') plt.imshow(integral_map[85:1441 - 85, :], origin='lower', cmap=cm.inferno, norm=LogNorm(vmin=1e-7, vmax=1e-5)) formatter = LogFormatter(10, labelOnlyBase=False) cb = plt.colorbar(orientation='horizontal') #cb.ax.minorticks_on()
def plot_composites(pdata,outfolder,contours,contour_colors=True, calibration_plot=True,brown_data=False,paperplot=False): ### image qualities fs = 10 # fontsize maxlim = 0.01 # limit of maximum ### contour color limits (customized for W1-W2) color_limits = [-1.0,2.6] kernel = None ### output blobs gradient, gradient_error, arcsec,objname_out, obj_size_kpc, background_out = [], [], [], [], [], [] ### begin loop fig = None for idx in xrange(len(pdata['objname'])): if paperplot: if ('NGC 4168' not in pdata['objname'][idx]) & ('NGC 1275' not in pdata['objname'][idx]): continue ### load object information objname = pdata['objname'][idx] fagn = pdata['pars']['fagn']['q50'][idx] ra, dec = load_coordinates(objname) phot_size = load_structure(objname,long_axis=True) # in arcseconds ### load image and WCS try: if brown_data: ### convert from DN to flux in Janskies, from this table: # http://wise2.ipac.caltech.edu/docs/release/allsky/expsup/sec2_3f.html img1, noise1 = load_image(objname,contours[0]), None img1, noise1 = img1*1.9350E-06, noise1*(1.9350e-06)**-2 img2, noise2 = load_image(objname,contours[1]), None img2, noise2 = img2*2.7048E-06, noise2*(2.7048E-06)**-2 ### translate object location into pixels using WCS coordinates wcs = WCS(img1.header) pix_center = wcs.all_world2pix([[ra[0],dec[0]]],1) else: img1, noise1 = load_wise_data(objname,contours[0].split(' ')[1]) img2, noise2 = load_wise_data(objname,contours[1].split(' ')[1]) ### translate object location into pixels using WCS coordinates wcs = WCS(img1.header) pix_center = wcs.all_world2pix([[ra[0],dec[0]]],1) if (pix_center.squeeze()[0]-4 > img1.shape[1]) or \ (pix_center.squeeze()[1]-4 > img1.shape[0]) or \ (np.any(pix_center < 4)): print 'object not in image, checking for additional image' print pix_center, img1.shape img1, noise1 = load_wise_data(objname,contours[0].split(' ')[1],load_other = True) img2, noise2 = load_wise_data(objname,contours[1].split(' ')[1],load_other = True) wcs = WCS(img1.header) pix_center = wcs.all_world2pix([[ra[0],dec[0]]],1) print pix_center, img1.shape except: gradient.append(None) gradient_error.append(None) arcsec.append(None) kpc.append(None) objname_out.append(None) continue size = calc_dist(wcs, pix_center, phot_size, img1.data.shape) ### convert inverse variance to noise noise1.data = (1./noise1.data)**0.5 noise2.data = (1./noise2.data)**0.5 ### build image extents extent = image_extent(size,pix_center,wcs) ### convolve W1 to W2 resolution w1_convolved, kernel = match_resolution(img1.data,contours[0],contours[1], kernel=kernel,data1_res=px_scale) w1_convolved_noise, kernel = match_resolution(noise1.data,contours[0],contours[1], kernel=kernel,data1_res=px_scale) #### put onto same scale, and grab slices data2, footprint = reproject_exact(img2, img1.header) noise2, footprint = reproject_exact(noise2, img1.header) img1_slice = w1_convolved[size[2]:size[3],size[0]:size[1]] img2_slice = data2[size[2]:size[3],size[0]:size[1]] noise1_slice = w1_convolved_noise[size[2]:size[3],size[0]:size[1]] noise2_slice = noise2[size[2]:size[3],size[0]:size[1]] ### subtract background from both images # identify background pixels. # background is any pixel consistent within X sigma of background! sigma = 3.0 if paperplot: sigma = 5.0 mean1, median1, std1 = sigma_clipped_stats(w1_convolved, sigma=sigma,iters=10) background1 = img1_slice < (median1+std1) img1_slice -= median1 mean2, median2, std2 = sigma_clipped_stats(data2, sigma=sigma, iters=10) background2 = img2_slice < (median2+std2) img2_slice -= median2 #### calculate the color flux_color = convert_to_color(img1_slice, img2_slice,None,None,contours[0],contours[1], minflux=-np.inf, vega_conversions=brown_data) ### don't show any "background" pixels! background = background1 | background2 flux_color[background] = np.nan ### plot colormap count = 0 if paperplot: if fig is None: fig, axall = plt.subplots(1,2, figsize=(12,6)) fig.subplots_adjust(right=0.8,wspace=0.4,hspace=0.3,left=0.12) cb_ax = fig.add_axes([0.83, 0.15, 0.05, 0.7]) ax = np.ravel(axall[0]) else: ax = np.ravel(axall[1]) count = 1 vmin, vmax = -0.4,1.05 else: fig, ax = plt.subplots(1,2, figsize=(12,6)) vmin, vmax = color_limits[0], color_limits[1] ax = np.ravel(ax) img = ax[0].imshow(flux_color, origin='lower',extent=extent,vmin=vmin,vmax=vmax,cmap='plasma') if not paperplot: cbar = fig.colorbar(img, ax=ax[0]) elif count == 0: cbar = fig.colorbar(img, cax=cb_ax) cbar.set_label('(W1-W2) [Vega]', fontdict={'fontsize':18}) ax[0].set_xlabel(r'$\Delta$(arcsec)') ax[0].set_ylabel(r'$\Delta$(arcsec)') ### plot W1 contours if not paperplot: plot_contour(ax[0],np.log10(img2_slice),ncontours=20) ### find image center in W2 image, and mark it # do this by finding the source closest to center tbl = [] nthresh, box_size = 20, 4 fake_noise2_error = copy.copy(noise2_slice) bad = np.logical_or(np.isinf(noise2_slice),np.isnan(noise2_slice)) fake_noise2_error[bad] = fake_noise2_error[~bad].max() while len(tbl) < 1: threshold = nthresh * std1 # peak threshold, @ 20 sigma tbl = find_peaks(img2_slice, threshold, box_size=box_size, subpixel=True, border_width=3, error = fake_noise2_error) nthresh -=2 if nthresh < 2: nthresh = 20 box_size += 1 ''' center = np.array(img2_slice.shape)/2. idxmax = ((center[0]-tbl['x_peak'])**2 + (center[1]-tbl['y_peak'])**2).argmin() fig, ax = plt.subplots(1,1, figsize=(6,6)) ax.plot(tbl['x_peak'][idxmax],tbl['y_peak'][idxmax],'x',color='red',ms=10) ax.imshow(img2_slice,origin='lower') plot_contour(ax, np.log10(img2_slice),ncontours=20) plt.show() ''' ### find size of biggest one imgcenter = np.array(img2_slice.shape)/2. idxmax = ((imgcenter[0]-tbl['x_centroid'])**2 + (imgcenter[1]-tbl['y_centroid'])**2).argmin() center = [tbl['x_centroid'][idxmax], tbl['y_centroid'][idxmax]] ### find center in arcseconds (NEW) center_coordinates = SkyCoord.from_pixel(imgcenter[0],imgcenter[1],wcs) x_pos_obj = SkyCoord.from_pixel(center[0],imgcenter[1],wcs) y_pos_obj = SkyCoord.from_pixel(imgcenter[0],center[1],wcs) xarcsec = x_pos_obj.separation(center_coordinates).arcsec if center[0] < imgcenter[0]: xarcsec = -xarcsec yarcsec = y_pos_obj.separation(center_coordinates).arcsec if center[1] < imgcenter[1]: yarcsec = -yarcsec #xarcsec = (extent[1]-extent[0])*center[0]/float(img2_slice.shape[0]) + extent[0] #yarcsec = (extent[3]-extent[2])*center[1]/float(img2_slice.shape[1]) + extent[2] ax[0].scatter(xarcsec,yarcsec,color='black',marker='x',s=50,linewidth=2) ### add in WISE PSF wise_psf = 6 # in arcseconds start = 0.85*extent[0] if not paperplot: ax[0].plot([start,start+wise_psf],[start,start],lw=2,color='k') ax[0].text(start+wise_psf/2.,start+1, '6"', ha='center') ax[0].set_xlim(extent[0],extent[1]) # reset plot limits b/c of text stuff ax[0].set_ylim(extent[2],extent[3]) else: ax[0].set_xlim(-65,65) ax[0].set_ylim(-65,65) ### gradient phys_scale = float(1./WMAP9.arcsec_per_kpc_proper(pdata['z'][idx]).value) if objname == 'CGCG 436-030': center[1] = center[1]+1.5 yarcsec += px_scale*1.5 grad, graderr, x_arcsec, back = measure_gradient(img1_slice,img2_slice, noise1_slice, noise2_slice, background, ax, center, tbl['peak_value'][idxmax], (xarcsec,yarcsec), phys_scale,paperplot=paperplot) obj_size_phys = phot_size*phys_scale if not paperplot: ax[1].text(0.05,0.06,r'f$_{\mathrm{AGN,MIR}}$='+"{:.2f}".format(pdata['pars']['fagn']['q50'][idx])+\ ' ('+"{:.2f}".format(pdata['pars']['fagn']['q84'][idx]) + ') ('+"{:.2f}".format(pdata['pars']['fagn']['q16'][idx])+')', transform=ax[1].transAxes,color='black',fontsize=9) ax[1].axvline(phot_size, linestyle='--', color='0.2',lw=2,zorder=-1) else: ax[0].text(0.98,0.94,objname,transform=ax[0].transAxes,fontsize=14,weight='bold',ha='right') ax[0].text(0.98,0.88,r'$\nabla$(2 kpc)='+"{:.2f}".format(grad[1]),fontsize=14,transform=ax[0].transAxes,ha='right') gradient.append(grad) gradient_error.append(graderr) arcsec.append(x_arcsec) obj_size_kpc.append(obj_size_phys) objname_out.append(objname) background_out.append(back) print objname, back # I/O outname = outfolder+'/'+objname+'.png' if paperplot: outname = outfolder+'/sample_wise_gradient.png' if (not paperplot) | (count == 1): if not paperplot: plt.tight_layout() plt.savefig(outname,dpi=150) plt.close() out = { 'gradient': np.array(gradient), 'gradient_error': np.array(gradient_error), 'arcsec': np.array(arcsec), 'obj_size_brown_kpc': np.array(obj_size_kpc), 'objname': objname_out, 'background_fraction': np.array(background_out) } if not paperplot: pickle.dump(out,open(outfile, "wb"))
def corr_sk(obs_folder, obs_filter): """ Correct counts images for LSS and mask them counts_new = counts_old / lss * mask Parameters ---------- obs_folder : string the 11-digit name of the folder downloaded from HEASARC obs_filter : string one of the UVOT filters ['w2','m2','w1','uu','bb','vv'] Returns ------- nothing """ print('') print(' ** correcting sk images') print('') # counts image (labeled as sk) sk_image = obs_folder + '/uvot/image/sw' + obs_folder + 'u' + obs_filter + '_sk.img' # LSS image lss_image = obs_folder + '/uvot/image/sw' + obs_folder + 'u' + obs_filter + '.lss' # mask image mask_image = obs_folder + '/uvot/image/sw' + obs_folder + 'u' + obs_filter + '_mask.img' with fits.open(sk_image) as hdu_sk, fits.open( lss_image) as hdu_lss, fits.open(mask_image) as hdu_mask: # create HDU for the new counts image hdu_sk_new = fits.HDUList() # copy over the primary header hdu_sk_new.append(fits.PrimaryHDU(header=hdu_sk[0].header)) # for each image extension, make the new image for i in range(1, len(hdu_sk)): #Test to make sure the LSS image is the same size and aligned with the sky image if len(hdu_lss[i].data) != len(hdu_sk[i].data) or len( hdu_lss[i].data[0]) != len(hdu_sk[i].data[0]): #If not, align images print('LSS Misaligned...') hd_test = fits.open((sk_image))[i] hdu1 = fits.open((lss_image))[i] print('Aligning LSS to Sky Image') lss_test, footprint = reproject_exact(hdu1, hd_test.header) print('Aligned Images') # divide by lss and multiply by mask new_sk_array = hdu_sk[i].data / lss_test * hdu_mask[i].data else: # divide by lss and multiply by mask new_sk_array = hdu_sk[i].data / hdu_lss[i].data * hdu_mask[ i].data # remove NaNs from dividing by 0 new_sk_array[np.isnan(new_sk_array)] = 0 # append to the big fits file hdu_sk_new.append( fits.ImageHDU(data=new_sk_array, header=hdu_sk[i].header)) # write out the new fits file sk_image_corr = obs_folder + '/uvot/image/sw' + obs_folder + 'u' + obs_filter + '_sk_corr.img' hdu_sk_new.writeto(sk_image_corr, overwrite=True)
def reproject(image_1: Union[fits.HDUList, str], image_2: Union[fits.HDUList, str], image_1_output: str = None, image_2_output: str = None, show: bool = False, force: int = None): """ Determines which image has the best pixel scale, and reprojects it onto the other; in the process, its spatial resolution will be downgraded to match the worse. :param image_1: :param image_2: :param image_1_output: :param image_2_output: :param show: :return: """ import reproject as rp image_1, path_1 = path_or_hdu(image_1) image_2, path_2 = path_or_hdu(image_2) pix_scale_1 = get_pixel_scale(image_1) pix_scale_2 = get_pixel_scale(image_2) # Take the image with the better spatial resolution and down-sample it to match the worse-resolution one # (unfortunately) # TODO: The header transfer is coarse and won't convey necessary information about the downgraded image. Take the # time to go through and select which header elements to keep and which to take from the other image. print('Reprojecting...') if force is None: if pix_scale_1 <= pix_scale_2: reprojected, footprint = rp.reproject_exact( image_1, image_2[0].header) image_1[0].data = reprojected image_1, image_2 = trim_nan(image_1, image_2) n_reprojected = 1 image_1[0].header = wcs_transfer(image_2[0].header, image_1[0].header) else: reprojected, footprint = rp.reproject_exact( image_2, image_1[0].header) n_reprojected = 2 image_2[0].data = reprojected image_2, image_1 = trim_nan(image_2, image_1) image_2[0].header = wcs_transfer(image_1[0].header, image_2[0].header) elif force == 1: reprojected, footprint = rp.reproject_exact(image_1, image_2[0].header) image_1[0].data = reprojected image_1, image_2 = trim_nan(image_1, image_2) n_reprojected = 1 image_1[0].header = wcs_transfer(image_2[0].header, image_1[0].header) elif force == 2: reprojected, footprint = rp.reproject_exact(image_2, image_1[0].header) n_reprojected = 2 image_2[0].data = reprojected image_2, image_1 = trim_nan(image_2, image_1) image_2[0].header = wcs_transfer(image_1[0].header, image_2[0].header) else: raise ValueError('force must be 1, 2 or None') print(image_1_output) print(image_2_output) if image_1_output is not None: image_1.writeto(image_1_output, overwrite=True) if image_2_output is not None: image_2.writeto(image_2_output, overwrite=True) if path_1: image_1.close() if path_2: image_2.close() return n_reprojected
ax2 = plt.subplot(122, projection=WCS(hdu2_hires[0].header).sub(2)) ax2.imshow(hdu2_hires[0].data[0, 0, :, :], origin='lower', cmap='Reds_r', norm=LogNorm(vmin=5e-5, vmax=5e-2)) ax2.coords.grid(color='white') ax2.coords['ra'].set_axislabel('Right Ascension') ax2.coords['dec'].set_axislabel('Declination') ax2.coords['dec'].set_axislabel_position('r') ax2.coords['dec'].set_ticklabel_position('r') ax2.set_title('Radio') plt.show() plt.close() array_hires, footprint_hires = reproject_exact( (hdu2_hires[0].data[0, 0, :, :], WCS(hdu2_hires[0].header).sub(2)), hdu1[0].header) xray_pixel_size_in_arcsec = hdu1[0].header['CDELT2'] * 3600 radio_pixel_size_in_arcsec = hdu2_hires[0].header['CDELT2'] * 3600 ratio = xray_pixel_size_in_arcsec**2 / radio_pixel_size_in_arcsec**2 # correcting for the pixel change print('Pixel size in arcsec', hdu1[0].header['CDELT2'] * 3600) array_hires *= ratio # FLUX CALIBRATION array_hires[array_hires <= 5e-3] = 0 # cutting out noise to 0 sum_image = 11895.7 # measured in ds9 # green_cat = (2720*4.72**(-0.77)) perly = 700 # result of 2_radio_calibration
ax2 = plt.subplot(122, projection=WCS(hdu2_ch1[0].header)) # ax1.axes(projection=WCS(hdu2_ch1.header)) im = ax2.imshow(hdu2_ch1[0].data, origin='lower', norm=LogNorm(vmin=5e-7, vmax=2e-5)) cbar = fig.colorbar(im) ax2.coords.grid(color='white') ax2.coords['ra'].set_axislabel('Right Ascension') ax2.coords['dec'].set_axislabel('Declination') ax2.coords['dec'].set_axislabel_position('r') ax2.coords['dec'].set_ticklabel_position('r') ax2.set_title('Spitzer ch1') plt.savefig(outpath + '3_Spitzer_original.pdf', bbox_inches='tight', dpi=100) plt.show() array_ch1, footprint_ch1 = reproject_exact(hdu2_ch1[0], hdu1[0].header) xray_pixel_size_in_arcsec = hdu1[0].header['CDELT2'] * 3600 spitzer_pixel_size_in_arcsec = hdu2_ch1[0].header['PXSCAL2'] ratio = xray_pixel_size_in_arcsec**2 / spitzer_pixel_size_in_arcsec**2 new_array_ch1 = array_ch1 * ratio plt.clf() wcs_hdr = WCS(hdu1[0].header) fig, ax = mp.plot_casa(figsize=[8, 6], coords=True, wcs=wcs_hdr) im = ax.imshow(new_array_ch1, origin='lower', norm=LogNorm(vmin=5e-6, vmax=2e-4)) cbar = mp.set_colorbar(fig, im, title=r'Flux [Jy]') ax.set_title('Reprojected Spitzer ch1') plt.savefig(outpath + '3_Spitzer_bin3_ch1_xheader_jy.pdf', bbox_inches='tight',
titles = ['A','C','D','E'] for file, title in zip(files,titles): make_polmap(file,title) # In[10]: # reproject from reproject import reproject_exact a_orig = fits.open(afile)['STOKES I'] hawc_a_header = a_orig.header new_c, footprint = reproject_exact(cfile,output_projection=hawc_a_header,hdu_in='STOKES I') c_repr = fits.PrimaryHDU(new_c,header=hawc_a_header) afig = FITSFigure(a_orig,subplot=(1,2,1)) afig.show_colorscale(cmap=cmap) cfig = FITSFigure(c_repr, subplot=(1,2,2), figure=plt.gcf()) cfig.show_colorscale(cmap=cmap) # FORMATTING afig.set_title('A') cfig.set_title('C') cfig.axis_labels.set_yposition('right') cfig.tick_labels.set_yposition('right') afig.set_tick_labels_font(size='small')
def reproj_califa(galname, get_map, do_balmer=False, is_uncertainty=False): """ Args: get_map : Function that will take an opened fits file and give the desired map array. do_balmer : Whether or not to apply the Balmer decrement to the map. Note that if do_balmer is True, get_map must also have a do_balmer argument (True/False). is_uncertainty : Whether this is a map of uncertainty or not (True/False) Returns: map_param_reproj : map of the desired parameter reprojected into the same pixel size and array size as the corresponding CO map. """ fname_niu_map = '/Users/ryan/venus/shared_data/califa/DR3-Niu/%s/califa-%s-ppxf-Maps-corr.fits.gz' % ( galname, galname) if do_balmer == True: fname_niu_map = '/Users/ryan/venus/shared_data/califa/DR3-Niu/%s/califa-%s-ppxf-Maps.fits' % ( galname, galname) if len(glob.glob(fname_niu_map)) == 0: fname_niu_map = '/Users/ryan/venus/shared_data/califa/DR3-V500-Niu/%s/califa-%s-ppxf-Maps-corr.fits.gz' % ( galname, galname) if do_balmer == True: fname_niu_map = '/Users/ryan/venus/shared_data/califa/DR3-V500-Niu/%s/califa-%s-ppxf-Maps.fits.gz' % ( galname, galname) if len(glob.glob(fname_niu_map)) == 0: print("This galaxy doesn't have a V500 data cube") return 0 maps_corr = fits.open(fname_niu_map) if do_balmer == True: map_param = get_map(maps_corr, do_balmer) else: map_param = get_map(maps_corr) if is_uncertainty == True: map_param = map_param**2 w = wcs.WCS(maps_corr[0].header) w = w.dropaxis(2) co_map, co_header = fits.getdata( '/Users/ryan/Dropbox/mac/wise_w3_vs_co/%s_co_smooth_wise_v2_rebin6_mom0.fits' % (galname, ), header=True) co_wcs = wcs.WCS(co_header) co_shape = co_map.shape map_param[map_param == 0] = np.nan mask = np.zeros(map_param.shape) mask[np.isnan(map_param)] = 1 mask[map_param == 0] = 1 mask = mask.astype(bool) map_param_reproj, footprint = reproject_exact((map_param, w), co_wcs, co_shape, parallel=False) if is_uncertainty == True: map_param_reproj = np.sqrt(map_param_reproj) area_fac = (co_wcs.wcs.cdelt[1] / w.wcs.cd[1, 1])**2 map_param_reproj *= area_fac # Check for not fully sampled pixels msk = np.ones(map_param.shape) msk[np.isnan(map_param)] = 0 msk[map_param == 0] = 0. msk_reproj, footprint = reproject_exact((msk, w), co_wcs, co_shape, parallel=False) # msk_reproj *= area_fac msk_reproj[msk_reproj != 1.] = 0. # Mask these pixels in the reprojected map map_param_reproj[msk_reproj == 0] = np.nan return map_param_reproj #, msk, msk_reproj
projhd['CTYPE2'] = 'GLAT-CAR' #Convert coordinates val = w.all_pix2world([[projhd['CRPIX1'], projhd['CRPIX2'], 0]], 0) Skyval = SkyCoord(ra=val[0, 0] * u.degree, dec=val[0, 1] * u.degree, frame='icrs') Gval = Skyval.galactic projhd['CRVAL1'] = Gval.l.degree projhd['CRVAL2'] = Gval.b.degree #Size of the projection projhd['NAXIS1'] = 1074 projhd['NAXIS2'] = 1074 #Projection #---------------------------------- Qproj = proj.reproject_exact(Qhdu, projhd) fits.writeto( '/Users/jfrob/postdoc/GALFACTS/GALFACTS_S2_average_image_Qgal.fits', Qproj, header=projhd) projhd['OBJECT'] = Uhdu[0].header['OBJECT'] Uproj = proj.reproject_exact(Uhdu, projhd) fits.writeto( '/Users/jfrob/postdoc/GALFACTS/GALFACTS_S2_average_image_Ugal.fits', Uproj, header=projhd)