def test_blot_file(tmpdir): """ Blot an image read from a file """ output_file = str(tmpdir.join('output_blot_file.fits')) test_file = str(tmpdir.join('output_blot_image.fits')) input_file = os.path.join(DATA_DIR, 'j8bt06nyq_flt.fits[1]') output_template = os.path.join(DATA_DIR, 'reference_blot_image.fits') blotwcs = read_wcs(input_file) driz = drizzle.Drizzle(infile=output_template) driz.add_fits_file(input_file) driz.blot_image(blotwcs) driz.write(test_file) driz = drizzle.Drizzle(infile=output_template) driz.add_fits_file(input_file) driz.blot_fits_file(input_file) driz.write(output_file) output_image = read_image(output_file) test_image = read_image(test_file) diff_image = np.absolute(output_image - test_image) assert np.amax(diff_image) == 0.0
def test_point_with_grid(): """ Test do_driz point kernel with grid """ input_file = os.path.join(DATA_DIR, 'j8bt06nyq_flt.fits') output = os.path.join(OUTPUT_DIR, 'output_point_grid.fits') output_difference = os.path.join(OUTPUT_DIR, 'difference_point_grid.txt') output_template = os.path.join(DATA_DIR, 'reference_point_grid.fits') insci = read_image(input_file) inwcs = read_wcs(input_file) insci = make_grid_image(insci, 64, 100.0) inwht = np.ones(insci.shape, dtype=insci.dtype) output_wcs = read_wcs(output_template) driz = drizzle.Drizzle(outwcs=output_wcs, wt_scl="", kernel='point') driz.add_image(insci, inwcs, inwht=inwht) if ok: driz.write(output_template) else: driz.write(output) template_data = read_image(output_template) (min_diff, med_diff, max_diff) = centroid_statistics("point with grid", output_difference, driz.outsci, template_data, 20.0, 8) assert (med_diff < 1.0e-6) assert (max_diff < 1.0e-5)
def test_blot_with_lan5(): """ Test do_blot with lan5 grid image """ input_file = os.path.join(DATA_DIR, 'j8bt06nyq_flt.fits') output = os.path.join(OUTPUT_DIR, 'output_blot_lan5.fits') output_difference = os.path.join(OUTPUT_DIR, 'difference_blot_lan5.txt') output_template = os.path.join(DATA_DIR, 'reference_blot_lan5.fits') outsci = read_image(input_file) outsci = make_grid_image(outsci, 64, 100.0) outwcs = read_wcs(input_file) inwcs = read_wcs(output_template) driz = drizzle.Drizzle(outwcs=outwcs) driz.outsci = outsci driz.blot_image(inwcs, interp="lan5") if ok: driz.write(output_template) else: driz.write(output) template_data = read_image(output_template) (min_diff, med_diff, max_diff) = centroid_statistics("blot with lan5", output_difference, driz.outsci, template_data, 20.0, 16) assert (med_diff < 1.0e-6) assert (max_diff < 1.0e-5)
def test_blot_with_lan3(tmpdir): """ Test do_blot with lan3 grid image """ output = str(tmpdir.join('output_blot_lan3.fits')) output_difference = str(tmpdir.join('difference_blot_lan3.txt')) input_file = os.path.join(DATA_DIR, 'j8bt06nyq_flt.fits') output_template = os.path.join(DATA_DIR, 'reference_blot_lan3.fits') outsci = read_image(input_file) outsci = make_grid_image(outsci, 64, 100.0) outwcs = read_wcs(input_file) inwcs = read_wcs(output_template) driz = drizzle.Drizzle(outwcs=outwcs) driz.outsci = outsci driz.blot_image(inwcs, interp="lan3") if ok: driz.write(output_template) else: driz.write(output) template_data = read_image(output_template) min_diff, med_diff, max_diff = centroid_statistics( "blot with lan3", output_difference, driz.outsci, template_data, 20.0, 16) assert med_diff < 1.0e-6 assert max_diff < 1.0e-5
def test_null_run(): """ Create an empty drizzle image """ output_file = os.path.join(OUTPUT_DIR, 'output_null_run.fits') output_template = os.path.join(DATA_DIR, 'reference_square_point.fits') output_wcs = read_wcs(output_template) driz = drizzle.Drizzle(outwcs=output_wcs, wt_scl="expsq", pixfrac=0.5, kernel="turbo", fillval="NaN") driz.write(output_file) assert (os.path.exists(output_file)) handle = fits.open(output_file) assert (handle.index_of("SCI") == 1) assert (handle.index_of("WHT") == 2) assert (handle.index_of("CTX") == 3) pheader = handle[0].header assert (pheader['DRIZOUDA'] == 'SCI') assert (pheader['DRIZOUWE'] == 'WHT') assert (pheader['DRIZOUCO'] == 'CTX') assert (pheader['DRIZWTSC'] == 'expsq') assert (pheader['DRIZKERN'] == 'turbo') assert (pheader['DRIZPIXF'] == 0.5) assert (pheader['DRIZFVAL'] == 'NaN') assert (pheader['DRIZOUUN'] == 'cps') assert (pheader['EXPTIME'] == 0.0) assert (pheader['DRIZEXPT'] == 1.0)
def test_tophat_with_grid(tmpdir): """ Test do_driz tophat kernel with grid """ output = str(tmpdir.join('output_tophat_grid.fits')) output_difference = str(tmpdir.join('difference_tophat_grid.txt')) input_file = os.path.join(DATA_DIR, 'j8bt06nyq_flt.fits') output_template = os.path.join(DATA_DIR, 'reference_tophat_grid.fits') insci = read_image(input_file) inwcs = read_wcs(input_file) insci = make_grid_image(insci, 64, 100.0) inwht = np.ones(insci.shape, dtype=insci.dtype) output_wcs = read_wcs(output_template) driz = drizzle.Drizzle(outwcs=output_wcs, wt_scl="", kernel='tophat') driz.add_image(insci, inwcs, inwht=inwht) if ok: driz.write(output_template) else: driz.write(output) template_data = read_image(output_template) min_diff, med_diff, max_diff = centroid_statistics( "tophat with grid", output_difference, driz.outsci, template_data, 20.0, 8) assert med_diff < 1.0e-6 assert max_diff < 1.0e-5
def test_file_init(run_drizzle_reference_square_points): """ Initialize drizzle object from a file """ input_file = run_drizzle_reference_square_points driz = drizzle.Drizzle(infile=input_file) assert driz.outexptime == 1.0 assert driz.wt_scl == 'expsq' assert driz.kernel == 'turbo' assert driz.pixfrac == 0.5 assert driz.fillval == 'NaN'
def run_drizzle_reference_square_points(tmpdir): """Create an empty drizzle image""" output_file = str(tmpdir.join('output_null_run.fits')) output_template = os.path.join(DATA_DIR, 'reference_square_point.fits') output_wcs = read_wcs(output_template) driz = drizzle.Drizzle(outwcs=output_wcs, wt_scl="expsq", pixfrac=0.5, kernel="turbo", fillval="NaN") driz.write(output_file) return output_file
def test_file_init(): """ Initialize drizzle object from a file """ input_file = os.path.join(OUTPUT_DIR, 'output_null_run.fits') output_file = os.path.join(OUTPUT_DIR, 'output_null_run.fits') driz = drizzle.Drizzle(infile=input_file) driz.write(output_file) assert (driz.outexptime == 1.0) assert (driz.wt_scl == 'expsq') assert (driz.kernel == 'turbo') assert (driz.pixfrac == 0.5) assert (driz.fillval == 'NaN')
def test_context_planes(): """Reproduce error seen in issue #50""" shape = [10, 10] outwcs = wcs.WCS() outwcs.pixel_shape = shape driz = drizzle.Drizzle(outwcs=outwcs) image = np.ones(shape) inwcs = wcs.WCS() inwcs.pixel_shape = shape for i in range(32): driz.add_image(image, inwcs) assert driz.outcon.shape == (1, 10, 10) driz.add_image(image, inwcs) assert driz.outcon.shape == (2, 10, 10)
def test_add_file(): """ Add an image read from a file """ input_file = os.path.join(DATA_DIR, 'j8bt06nyq_flt.fits[1]') output_file = os.path.join(OUTPUT_DIR, 'output_add_file.fits') test_file = os.path.join(OUTPUT_DIR, 'output_add_header.fits') output_template = os.path.join(DATA_DIR, 'reference_square_point.fits') driz = drizzle.Drizzle(infile=output_template) driz.add_fits_file(input_file) driz.write(output_file) output_image = read_image(output_file) test_image = read_image(test_file) diff_image = np.absolute(output_image - test_image) assert (np.amax(diff_image) == 0.0)
def add_header(tmpdir): """Add extra keywords read from the header""" output_file = str(tmpdir.join('output_add_header.fits')) input_file = os.path.join(DATA_DIR, 'j8bt06nyq_flt.fits') output_template = os.path.join(DATA_DIR, 'reference_square_point.fits') driz = drizzle.Drizzle(infile=output_template) image = read_image(input_file) inwcs = read_wcs(input_file) driz.add_image(image, inwcs) header = fits.header.Header() header['ONEVAL'] = (1.0, 'test value') header['TWOVAL'] = (2.0, 'test value') driz.write(output_file, outheader=header) return output_file
def test_add_header(): """ Add extra keywords read from the header """ input_file = os.path.join(DATA_DIR, 'j8bt06nyq_flt.fits') output_file = os.path.join(OUTPUT_DIR, 'output_add_header.fits') output_template = os.path.join(DATA_DIR, 'reference_square_point.fits') driz = drizzle.Drizzle(infile=output_template) image = read_image(input_file) inwcs = read_wcs(input_file) driz.add_image(image, inwcs) header = fits.header.Header() header['ONEVAL'] = (1.0, 'test value') header['TWOVAL'] = (2.0, 'test value') driz.write(output_file, outheader=header) header = read_header(output_file) assert (header['ONEVAL'] == 1.0) assert (header['TWOVAL'] == 2.0) assert (header['DRIZKERN'] == 'square')
w = wcs.WCS(naxis=2) w.wcs.crpix = [nVPix / 2., nVPix / 2.] w.wcs.cdelt = np.array([vPlateScale, vPlateScale]) w.wcs.crval = [coords.ra.deg, coords.dec.deg] w.wcs.ctype = ["RA-----", "DEC----"] w._naxis1 = nVPix w._naxis2 = nVPix return w w = get_header() print(w) from drizzle import drizzle as stdrizzle driz = stdrizzle.Drizzle(outwcs=w, pixfrac=1) def makeImage(angle, xy, plot=False): x, y = xy # rot_sky, cent = rot(image, np.array([sky_span//2,sky_span//2]), np.rad2deg(angle)) rot_sky = rot(image, np.array([700, 400]), np.rad2deg(angle)) # rot_sky = image if plot: plt.imshow(rot_sky, origin='lower') plt.show() left = rot_sky.shape[0] // 2 + x - width // 2 right = left + width bottom = rot_sky.shape[1] // 2 + y - width // 2 top = bottom + width print(rot_sky.shape, x, y, left, right, bottom, top)
def run(self): """ Runs the mosaicing algorithm. The self.datain is run through the code, the result is in self.dataout. """ #calculate platescale of first input image try: det = np.linalg.det(wcs.WCS(self.datain[0].header).wcs.cd) pscale = np.sqrt(np.abs(det))*3600. except: try: det = np.linalg.det(wcs.WCS(self.datain[0].header).wcs.pc) pscale = np.sqrt(np.abs(det))*3600. except: pscale = self.datain[0].header['PIXSCAL'] #filtering out images which are too far away from the others #passing images added to a list of (image, WCS) tuples ''' image_centers = [] for f in self.datain: image_centers.append((f.header['CRVAL1'], f.header['CRVAL2'])) filtered_datain = [] dist_list = [[[0]*(len(image_centers)-1)]*len(image_centers)] for i in range(len(image_centers)): for j in range(len(image_centers)-1): dist_list[i][j+1] = np.sqrt((image_)**2+()**2) ''' #calculations necessary for updating wcs information px = [] py = [] #in order to avoid NaN interactions, creating weight map weights=[] for f in self.datain: weights.append((np.where(np.isnan(f.image) == True, 0, 1))) for f in self.datain: px.extend(wcs.WCS(f.header).calc_footprint()[:,0]) py.extend(wcs.WCS(f.header).calc_footprint()[:,1]) x0 = (max(px)+min(px))/2. y0 = (max(py)+min(py))/2. sx = (max(px)-min(px))*np.cos(y0/180*np.pi) # arcsec sy = (max(py)-min(py)) # arcsec size = (sx*3600+self.getarg('pad')*2, sy*3600+self.getarg('pad')*2) xpix = size[0]//pscale ypix = size[1]//pscale cdelt = [pscale/3600.]*2 #create self.dataout and give it a copy of an input's header self.dataout = DataFits(config = self.config) self.dataout.header = self.datain[0].header.copy() #update header wcs information self.log.info('Creating new WCS header') self.dataout.header['CRPIX1'] = xpix/2 self.dataout.header['CRPIX2'] = ypix/2 self.dataout.header['CRVAL1'] = x0 self.dataout.header['CRVAL2'] = y0 self.dataout.header['CD1_1'] = -cdelt[0] self.dataout.header['CD1_2'] = self.dataout.header['CD2_1'] = 0. self.dataout.header['CD2_2'] = cdelt[1] self.dataout.header['NAXIS1'] = int(xpix) self.dataout.header['NAXIS2'] = int(ypix) self.dataout.header['CTYPE1'] = 'RA---TAN-SIP' self.dataout.header['CTYPE2'] = 'DEC--TAN-SIP' self.dataout.header['RADESYS'] = 'ICRS' self.dataout.header['EQUINOX'] = 2000 self.dataout.header['LATPOLE'] = self.datain[0].header['CRVAL2'] self.dataout.header['LONPOLE'] = 180 self.dataout.header['PIXASEC'] = pscale theta_rad = np.deg2rad(self.getarg('outangle')) rot_matrix = np.array([[np.cos(theta_rad), -np.sin(theta_rad)], [np.sin(theta_rad), np.cos(theta_rad)]]) rot_cd = np.dot(rot_matrix, np.array([[self.dataout.header['CD1_1'], 0.],[0., self.dataout.header['CD2_2']]])) for i in [0,1]: for j in [0,1]: self.dataout.header['CD{0:d}_{1:d}'.format(i+1, j+1)] = rot_cd[i,j] #check drizzle arguments if self.getarg('kernel') == 'smoothing': kernel = 'lanczos3' elif self.getarg('kernel') in ['square', 'point', 'gaussian', 'tophat']: kernel = self.getarg('kernel') else: self.log.error('Kernel name not recognized, using default') kernel = 'square' if self.getarg('drizzleweights') == 'uniform': driz_wt = '' elif self.getarg('drizzleweights') in ['exptime', 'expsq']: driz_wt = self.getarg('drizzleweights') else: self.log.error('Drizzle weighting not recognized, using default') driz_wt = '' #create drizzle object and add input images fullwcs = wcs.WCS(self.dataout.header) self.log.info('Starting drizzle') driz = drz.Drizzle(outwcs = fullwcs, pixfrac=self.getarg('pixfrac'), \ kernel=kernel, fillval='10000', wt_scl=driz_wt) for i,f in enumerate(self.datain): self.log.info('Adding %s to drizzle stack' % f.filename) driz.add_image(f.imgdata[0], wcs.WCS(f.header), inwht=weights[i]) try: fillval=float(self.getarg('fillval')) except: fillval=np.nan self.log.error('Fillvalue not recognized or missing, using default') #creates output fits file from drizzle output self.dataout.imageset(np.where(driz.outsci == 10000, fillval, driz.outsci)) self.dataout.imageset(driz.outwht,'OutWeight', self.dataout.header) self.dataout.filename = self.datain[0].filename #add history self.dataout.setheadval('HISTORY','Coadd: %d files combined with %s kernel, pixfrac %f at %f times resolution' \ % (len(self.datain), kernel, self.getarg('pixfrac'), self.getarg('resolution')))