def reproject_to(self, reference_cube, projection_type='bicubic'): """ Spatially reprojects a `SpectralCube` onto a reference cube. Parameters ---------- reference_cube : `SpectralCube` Reference cube with the desired spatial projection. projection_type : {'nearest-neighbor', 'bilinear', 'biquadratic', 'bicubic', 'flux-conserving'} Specify method of reprojection. Default: 'bilinear'. Returns ------- reprojected_cube : `SpectralCube` Cube spatially reprojected to the reference cube. """ from reproject import reproject reference = reference_cube.data shape_out = reference[0].shape try: wcs_in = self.wcs.dropaxis(2) except: wcs_in = self.wcs try: wcs_out = reference_cube.wcs.dropaxis(2) except: wcs_out = reference_cube.wcs energy = self.energy cube = self.data new_cube = np.zeros( (cube.shape[0], reference.shape[1], reference.shape[2])) energy_slices = np.arange(cube.shape[0]) # TODO: Re-implement to reproject cubes directly without needing # to loop over energies here. Errors with reproject when doing this # first need to be understood and fixed. for i in energy_slices: array = cube[i] data_in = (array.value, wcs_in) new_cube[i] = reproject(data_in, wcs_out, shape_out, projection_type)[0] new_cube = Quantity(new_cube, array.unit) # Create new wcs header_in = self.wcs.to_header() header_out = reference_cube.wcs.to_header() # Keep output energy info the same as input, but changes spatial information # So need to restore energy parameters to input values here try: header_out['CRPIX3'] = header_in['CRPIX3'] header_out['CDELT3'] = header_in['CDELT3'] header_out['CTYPE3'] = header_in['CTYPE3'] header_out['CRVAL3'] = header_in['CRVAL3'] except: pass wcs_out = WCS(header_out) return SpectralCube(new_cube, wcs_out, energy)
def reproject_to(self, reference_cube, projection_type='bicubic'): """ Spatially reprojects a `SpectralCube` onto a reference cube. Parameters ---------- reference_cube : `SpectralCube` Reference cube with the desired spatial projection. projection_type : {'nearest-neighbor', 'bilinear', 'biquadratic', 'bicubic', 'flux-conserving'} Specify method of reprojection. Default: 'bilinear'. Returns ------- reprojected_cube : `SpectralCube` Cube spatially reprojected to the reference cube. """ from reproject import reproject reference = reference_cube.data shape_out = reference[0].shape try: wcs_in = self.wcs.dropaxis(2) except: wcs_in = self.wcs try: wcs_out = reference_cube.wcs.dropaxis(2) except: wcs_out = reference_cube.wcs energy = self.energy cube = self.data new_cube = np.zeros((cube.shape[0], reference.shape[1], reference.shape[2])) energy_slices = np.arange(cube.shape[0]) # TODO: Re-implement to reproject cubes directly without needing # to loop over energies here. Errors with reproject when doing this # first need to be understood and fixed. for i in energy_slices: array = cube[i] data_in = (array.value, wcs_in) new_cube[i] = reproject(data_in, wcs_out, shape_out, projection_type)[0] new_cube = Quantity(new_cube, array.unit) # Create new wcs header_in = self.wcs.to_header() header_out = reference_cube.wcs.to_header() # Keep output energy info the same as input, but changes spatial information # So need to restore energy parameters to input values here try: header_out['CRPIX3'] = header_in['CRPIX3'] header_out['CDELT3'] = header_in['CDELT3'] header_out['CTYPE3'] = header_in['CTYPE3'] header_out['CRVAL3'] = header_in['CRVAL3'] except: pass wcs_out = WCS(header_out) return SpectralCube(new_cube, wcs_out, energy)
# os3hw.py # Solution to Open Source week 3 homework -- working with filters & geoprocessing # cgarrard 2-12-08 # import the modules import ogr, sys, os, reproject # set the working directory os.chdir('d:/data/classes/python/os4') # set the filenames inFn = 'ut_counties.shp' outFn = 'ut_counties_utm.shp' # set the epsg codes inEPSG = 4269 outEPSG = 26912 # outEPSG = 9999999 # reproject using the reproject method in the reproject module reproject.reproject(inFn, outFn, inEPSG, outEPSG)
from montage_wrapper import reproject as reproject_montage hdu = fits.open('MSX_E.fits')[0] wcs_in = WCS(hdu.header) print('{0:10s} {1:9s} {2:9s}'.format(' header', 'reproject', ' montage ')) print('---------- --------- ---------') for header in glob.glob('*.hdr'): name = header.split('.')[0] # With 'reproject' package header_out = fits.Header.fromtextfile(header) wcs_out = WCS(header_out) time1 = time.time() array_out = reproject(hdu, wcs_out, shape_out=hdu.data.shape, projection_type='flux-conserving') time2 = time.time() fits.writeto(name + '_reproject.fits', array_out, header_out, clobber=True) # With montage time3 = time.time() reproject_montage('MSX_E.fits', name + '_montage.fits', header=header) time4 = time.time() print("{0:10s} {1:9.3f} {2:9.3f}".format(name, time2-time1, time4-time3))
from montage_wrapper import reproject as reproject_montage hdu = fits.open('data_small.fits')[0] wcs_in = WCS(hdu.header) print('{0:10s} {1:9s} {2:9s}'.format(' header', 'reproject', ' montage ')) print('---------- --------- ---------') header = 'small_eq.hdr' name = 'small' # With 'reproject' package header_out = fits.Header.fromtextfile(header) wcs_out = WCS(header_out) time1 = time.time() array_out = reproject(hdu, wcs_out, shape_out=(header_out['NAXIS2'], header_out['NAXIS1']), projection_type='flux-conserving') time2 = time.time() fits.writeto(name + '_reproject.fits', array_out, header_out, clobber=True) # With montage time3 = time.time() reproject_montage('data_small.fits', name + '_montage.fits', header=header, exact_size=True) time4 = time.time() print("{0:10s} {1:9.3f} {2:9.3f}".format(name, time2-time1, time4-time3))
def time_flux_conserving(self): reproject(self.hdu_in, self.header_out_size, projection_type='flux-conserving')
def time_bilinear(self): reproject(self.hdu_in, self.header_out_size, projection_type='bilinear')
beam1 = Beam.from_fits_header(epoch1[0].header) epoch1[0].header = epoch1header = wcs.WCS(epoch1[0].header).sub([wcs.WCSSUB_CELESTIAL]).to_header() epoch1header['NAXIS1'] = epoch1[0].data.shape[1] epoch1header['NAXIS2'] = epoch1[0].data.shape[0] wcs1 = wcs.WCS(epoch1[0].header).sub([wcs.WCSSUB_CELESTIAL]) epoch3 = fits.open(paths.dpath("W51C_ACarray_continuum_4096_both_uniform_contsplit.clean.image.fits")) beam3 = Beam.from_fits_header(epoch3[0].header) epoch3[0].data = epoch3[0].data.squeeze() wcs3 = wcs.WCS(epoch3[0].header).sub([wcs.WCSSUB_CELESTIAL]) epoch3header = wcs3.to_header() epoch3header['NAXIS'] = 2 epoch3header['NAXIS1'] = epoch3[0].data.shape[1] epoch3header['NAXIS2'] = epoch3[0].data.shape[0] #epoch1reproj = FITS_tools.hcongrid.hcongrid(epoch1[0].data, epoch1header, epoch3header) epoch1reproj, footprint = reproject.reproject(epoch1[0], epoch3header) scalefactor = (beam3.sr/beam1.sr).value xshift,yshift, ex, ey = image_registration.chi2_shift(epoch3[0].data, epoch1reproj*scalefactor, err=0.001) center = coordinates.SkyCoord(290.92443*u.deg, 14.515755*u.deg, frame='fk5') xx,yy = wcs1.wcs_world2pix([[center.fk4.ra.deg, center.fk4.dec.deg]], 0)[0] subim1 = epoch1[0].data[yy-10:yy+10, xx-10:xx+10] wcs1sub = wcs1[yy-10:yy+10, xx-10:xx+10] xx,yy = wcs3.wcs_world2pix([[center.fk5.ra.deg, center.fk5.dec.deg]], 0)[0] wcs3sub = wcs3[yy-10:yy+10, xx-10:xx+10] subim3 = epoch3[0].data[yy-10:yy+10, xx-10:xx+10] gf1 = gaussfitter.gaussfit(subim1) gf3 = gaussfitter.gaussfit(subim3)