def reprojection(orig_file, im, old_hdr, new_hdr, data_dir, name=None): montage_dir = os.path.join(data_dir, '_montage') ## write im (in MJy/sr) to a file newfile = orig_file.split('/')[-1].replace('.fits', '_to_mjysr.fits') imfile = os.path.join(_HOME_DIR, newfile) if not os.path.exists(imfile): pyfits.writeto(imfile, im, old_hdr) #hdu = pyfits.PrimaryHDU(im, header=old_hdr) #hdu.writeto(imfile) # wrie new_hdr to a file hdr_file = os.path.join(_HOME_DIR, 'template.hdr') if name is not None: hdr_file = os.path.join(_HOME_DIR, name + '_template.hdr') write_headerfile(hdr_file, new_hdr) # reproject with new imfile and headerfile outfile = imfile.replace('.fits', '_reproj.fits') montage.mProject(imfile, outfile, hdr_file) #new_hdu = montage.reproject_hdu(hdu, header=hdr_file) #return outfile return new_hdu.data
def _montage_test(): # create density images input_dir = os.path.dirname(density_files[0]) # image metadata meta1_file = os.path.join(input_dir, 'meta1.tbl') montage.mImgtbl(input_dir, meta1_file, corners=True) # make header #lon, lat = [], [] #for density_file in density_files: # data, hdr = astropy.io.fits.getdata(density_file, header=True) # wcs = astropy.wcs.WCS(hdr) # x1, y1 = 0.5, 0.5 # y2, x2 = data.shape # x2, y2 = x2 + 0.5, y2 + 0.5 # x, y = [x1, x2, x2, x1], [y1, y1, y2, y2] # ln, lt = wcs.wcs_pix2world(x, y, 1) # lon += list(ln) # lat += list(lt) #lon1, lon2 = np.min(lon), np.max(lon) #lat1, lat2 = np.min(lat), np.max(lat) hdr_file = os.path.join(os.path.dirname(input_dir), 'test.hdr') montage.mMakeHdr(meta1_file, hdr_file) # reproject proj_dir = os.path.dirname(proj_files[0]) safe_mkdir(proj_dir) stats_file = os.path.join(proj_dir, 'stats.tbl') montage.mProjExec(meta1_file, hdr_file, proj_dir, stats_file, raw_dir=input_dir, exact=True) # image metadata meta2_file = os.path.join(proj_dir, 'meta2.tbl') montage.mImgtbl(proj_dir, meta2_file, corners=True) # Background modeling diff_dir = os.path.join(os.path.dirname(proj_dir), 'difference') safe_mkdir(diff_dir) diff_file = os.path.join(diff_dir, 'diffs.tbl') montage.mOverlaps(meta2_file, diff_file) montage.mDiffExec(diff_file, hdr_file, diff_dir, proj_dir) fits_file = os.path.join(diff_dir, 'fits.tbl') montage.mFitExec(diff_file, fits_file, diff_dir) # Background matching corr_dir = os.path.join(os.path.dirname(proj_dir), 'correct') safe_mkdir(corr_dir) corr_file = os.path.join(corr_dir, 'corrections.tbl') montage.mBgModel(meta2_file, fits_file, corr_file, level_only=False) montage.mBgExec(meta2_file, corr_file, corr_dir, proj_dir=proj_dir) # Native mosaic projadd_file = config.path('{:s}.reproject.add'.format(kind)) projadd_dir, filename = os.path.split(projadd_file) filename, ext = os.path.splitext(filename) filename = '{0:s}_native{1:s}'.format(filename, ext) projaddnative_file = os.path.join(projadd_dir, filename) safe_mkdir(projadd_dir) montage.mAdd(meta2_file, hdr_file, projaddnative_file, img_dir=corr_dir, exact=True) # Reproject to final header header_file = config.path('{:s}.hdr'.format(kind)) montage.mProject(projaddnative_file, projadd_file, header_file) # Postprocess data, hdr = astropy.io.fits.getdata(projaddnative_file, header=True) x1, x2 = 900, 1900 y1, y2 = 3000, 4500 val = np.mean(data[y1:y2,x1:x2]) data, hdr = astropy.io.fits.getdata(projadd_file, header=True) data = data - val areaadd_file = config.path('{:s}.area.add'.format(kind)) area = astropy.io.fits.getdata(areaadd_file) * (180/np.pi*3600)**2 # arcsec2 data = data * area add_file = config.path('{:s}.add'.format(kind)) dirname = os.path.dirname(add_file) safe_mkdir(dirname) if os.path.exists(add_file): os.remove(add_file) hdu = astropy.io.fits.PrimaryHDU(data, header=hdr) hdu.writeto(add_file)
def galex(band='fuv', ra_ctr=None, dec_ctr=None, size_deg=None, index=None, name=None): tel = 'galex' data_dir = _FILE_DIR problem_file = os.path.join(_HOME_DIR, 'problem_galaxies.txt') galaxy_mosaic_file = os.path.join(_HOME_DIR, '_'.join([name, band]).upper() + '.FITS') if not os.path.exists(galaxy_mosaic_file): # CALIBRATION FROM COUNTS TO ABMAG fuv_toab = 18.82 nuv_toab = 20.08 # PIXEL SCALE IN ARCSECONDS pix_as = 1.5 # galex pixel scale -- from galex docs # MAKE A HEADER pix_scale = 1.5 / 3600. # 1.5 arbitrary: how should I set it? pix_len = size_deg / pix_scale target_hdr = create_hdr(ra_ctr, dec_ctr, pix_len, pix_scale) # SET UP THE OUTPUT ri_targ, di_targ = make_axes(target_hdr) sz_out = ri_targ.shape outim = ri_targ * np.nan prihdu = pyfits.PrimaryHDU(data=outim, header=target_hdr) target_hdr = prihdu.header # CREATE NEW DIRECTORY TO STORE TEMPORARY FILES gal_dir = os.path.join(_HOME_DIR, name) os.makedirs(gal_dir) infiles = glob.glob(os.path.join(_INTSUBSKYFILE_DIR, '*.fits')) bgfiles = glob.glob(os.path.join(_BGFILE_DIR, '*.fits')) rrhrfiles = glob.glob(os.path.join(_RRHRFILE_DIR, '*.fits')) # CREATE SUBDIRECTORY INSIDE TEMP DIRECTORY FOR THE INPUT FILES input_dir = os.path.join(gal_dir, 'input') os.makedirs(input_dir) outfiles = [ os.path.join(input_dir, f.split('/')[-1].replace('.fits', '_mjysr.fits')) for f in infiles ] # CONVERT TO MJY/SR AND WRITE NEW FILES INTO TEMPORARY DIRECTORY w_sum = 0. for i in range(len(infiles)): im, hdr = pyfits.getdata(infiles[i], header=True) bg = pyfits.getdata(bgfiles[i]) rrhr, rrhdr = pyfits.getdata(rrhrfiles[i], header=True) mean_bg = np.nanmean(bg[bg > 0]) med_bg = np.nanmedian(bg[bg > 0]) w = 1. / rrhr w_sum += w if band.lower() == 'fuv': im = counts2jy_galex(im, fuv_toab, pix_as) if band.lower() == 'nuv': im = counts2jy_galex(im, nuv_toab, pix_as) im -= mean_bg im *= w if not os.path.exists(outfiles[i]): pyfits.writeto(outfiles[i], im, hdr) # APPEND UNIT INFORMATION TO THE NEW HEADER target_hdr['BUNIT'] = 'MJY/SR' # WRITE OUT A HEADER FILE hdr_file = os.path.join(gal_dir, name + '_template.hdr') write_headerfile(hdr_file, target_hdr) final_dir = os.path.join(gal_dir, 'mosaic') # WRITE OUT TOTAL WEIGHT FILE weightfile = os.path.join(gal_dir, 'weights.fits') pyfits.writeto(weightfile, w_sum, rrhdr) reprojweightfile = os.path.join(gal_dir, 'reproj_weights.fits') # MOSAIC THE TILES FOR THIS GALAXY try: # THE MEAN COADD WORKS THE BEST montage.mosaic(input_dir, final_dir, header=hdr_file, background_match=True, combine='mean') montage.mProject(weightfile, reprojweightfile, hdr_file) # COPY MOSAIC FILE TO CUTOUTS DIRECTORY mosaic_file = os.path.join(final_dir, 'mosaic.fits') newmosaic_file = os.path.join(final_dir, 'weighted_mosaic.fits') im, hdr = pyfits.getdata(mosaic_file, header=True) wt = pyfits.getdata(reprojweightfile) im /= wt #set_trace() pyfits.writeto(newmosaic_file, im, hdr) sys.exit() #set_trace() # REMOVE GALAXY DIRECTORY AND EXTRA FILES shutil.rmtree(gal_dir) except Exception as inst: me = sys.exc_info()[0] problem_file = os.path.join(_HOME_DIR, 'problem_galaxies.txt') with open(problem_file, 'a') as myfile: myfile.write(name + ': ' + str(me) + ': ' + str(inst) + '\n') shutil.rmtree(gal_dir) return