def retrieve_ugr(ra, dec, plate_num, width=1., scale=1.): ''' use montage to get a fits header, and then 3-band fits images ''' ra, dec = str(ra), str(dec) plate_num = str(plate_num) loc_sky = '{0} {1}'.format(ra, dec) m.mHdr(object_or_location=loc_sky, system='equatorial', width=width, pix_size=scale, out_file='temp.hdr') m.mExec(survey='SDSS', band='u', keep=False, corners=True, output_image=plate_num + '_u.fits', region_header='temp.hdr') m.mExec(survey='SDSS', band='g', keep=False, corners=True, output_image=plate_num + '_g.fits', region_header='temp.hdr') m.mExec(survey='SDSS', band='r', keep=False, corners=True, output_image=plate_num + '_r.fits', region_header='temp.hdr')
def rotcrop_cube(x1, y1, x2, y2, cubename, outname, xwidth=25, ywidth=25, in_system='galactic', out_system='equatorial', clobber=True, newheader=None, xcen=None, ycen=None): """ Crop a data cube and then rotate it with montage """ cubefile = fits.open(cubename) if xcen is None and ycen is None: pos1 = coords.Position([x1,y1],system=in_system) pos2 = coords.Position([x2,y2],system=in_system) if cubefile[0].header.get('CTYPE1')[:2] == 'RA': x1,y1 = pos1.j2000() x2,y2 = pos2.j2000() coord_system = 'celestial' elif cubefile[0].header.get('CTYPE1')[:4] == 'GLON': x1,y1 = pos1.galactic() x2,y2 = pos2.galactic() coord_system = 'galactic' xcen = (x1+x2)/2.0 ycen = (y1+y2)/2.0 print xcen,ycen,xwidth,ywidth,coord_system else: coord_system = in_system sc = subcube(cubefile[0].data, xcen, xwidth, ycen, ywidth, widthunits='pixels', units="wcs", header=cubefile[0].header, return_HDU=True) # note: there should be no security risk here because fits' writeto # will not overwrite by default tempcube = tempfile.mktemp(suffix='.fits') sc.writeto(tempcube) pa = posang.posang(x1,y1,x2,y2,system=coord_system) - 90 if newheader is None: newheader = sc.header.copy() cd11 = newheader.get('CDELT1') if newheader.get('CDELT1') else newheader.get('CD1_1') cd22 = newheader.get('CDELT2') if newheader.get('CDELT2') else newheader.get('CD2_2') cd12 = newheader.get('CD1_2') if newheader.get('CD1_2') else 0.0 cd21 = newheader.get('CD2_1') if newheader.get('CD2_1') else 0.0 cdelt = numpy.sqrt(cd11**2+cd12**2) tempheader = tempfile.mktemp(suffix='.hdr') ycensign = "+" if numpy.sign(ycen) >= 0 else "-" montage.mHdr("%s %1s%s" % (xcen, ycensign, numpy.abs(ycen)), xwidth*cdelt, tempheader, system=out_system, height=ywidth*cdelt, pix_size=cdelt*3600.0, rotation=pa) os.system("sed -i bck '/END/d' %s" % (tempheader)) newheader2 = fits.Header() newheader2.fromTxtFile(tempheader) #newheader2.fromtextfile(tempheader) for key in ('CRPIX3','CRVAL3','CDELT3','CD3_3','CUNIT3','WCSTYPE3','CTYPE3'): if newheader.get(key): newheader2.update(key,newheader.get(key)) if newheader.get('CD3_3') and newheader2.get('CDELT3') is None: newheader2.update('CDELT3',newheader.get('CD3_3')) newheader2.toTxtFile(tempheader,clobber=True) #if newheader2.get('CDELT3') is None: # raise Exception("No CD3_3 or CDELT3 in header.") else: if isinstance(newheader,str): newheader2 = fits.Header() newheader2.fromTxtFile(newheader) tempheader = tempfile.mktemp(suffix='.hdr') newheader2.toTxtFile(tempheader,clobber=True) montage.wrappers.reproject_cube(tempcube,outname,header=tempheader,clobber=clobber) #print "\n",outname #os.system('imhead %s | grep CDELT' % outname) # AWFUL hack because montage removes CDELT3 tempcube = fits.open(outname) tempcube.header = newheader2 #if tempcube.header.get('CDELT3') is None: # raise Exception("No CD3_3 or CDELT3 in header.") #print tempcube.header.get('CDELT3') tempcube.writeto(outname,clobber=True) #print tempcube.get('CDELT3') #print "\n",outname #os.system('imhead %s | grep CDELT' % outname) #print "\nnewheader2" #print newheader2.ascard #print return
def mosaic_band(self,band,ra,dec,margin,radius,pgc):#,clean=True): ''' Input: source info param Create a mosaic fit file for the specified band. Return: String filename of resulting mosaic ''' print ("------------------mosaic_band----------------------") DEBUG = True # output = open("rc3_galaxies_outside_SDSS_footprint.txt",'a') # 'a' for append #'w') # unclean = open("rc3_galaxies_unclean","a") # filename = "{},{}".format(str(ra),str(dec)) filename = str(ra)+str(dec) #print (margin/radius) if (DEBUG) : print ("Querying data that lies inside margin") #result = sqlcl.query( "SELECT distinct run,camcol,field FROM PhotoObj WHERE ra between {0}-{1} and {0}+{1}and dec between {2}-{3} and {2}+{3}".format(str(ra),str(margin),str(dec),str(margin))).readlines() result = sqlcl.query( "SELECT distinct run,camcol,field FROM PhotoObj WHERE ra between "+str(ra)+"-"+str(margin)+" and " +str(ra)+"+"+str(margin)+"and dec between "+str(dec)+"-"+str(margin)+" and "+ str(dec)+"+"+str(margin)).readlines() clean_result = sqlcl.query( "SELECT distinct run,camcol,field FROM PhotoObj WHERE CLEAN =1 and ra between "+str(ra)+"-"+str(margin)+" and " +str(ra)+"+"+str(margin)+"and dec between "+str(dec)+"-"+str(margin)+" and "+ str(dec)+"+"+str(margin)).readlines() # clean_result = sqlcl.query( "SELECT distinct run,camcol,field FROM PhotoObj WHERE CLEAN =1 and ra between {0}-{1} and {0}+{1}and dec between {2}-{3} and {2}+{3}".format(str(ra),str(margin),str(dec),str(margin))) .readlines() clean = True print (result) print (clean_result) if (result[0][5:]=="<html>"): print("strange error from SQL server") return -1 if (result[1]=='error_message\n' or clean_result[1]=='error_message\n'): #Case where doing more than 60 queries in 1 minute time.sleep(60) #results are messed up, need to re-query result = sqlcl.query( "SELECT distinct run,camcol,field FROM PhotoObj WHERE ra between "+str(ra)+"-"+str(margin)+" and " +str(ra)+"+"+str(margin)+"and dec between "+str(dec)+"-"+str(margin)+" and "+ str(dec)+"+"+str(margin)).readlines() clean_result = sqlcl.query( "SELECT distinct run,camcol,field FROM PhotoObj WHERE CLEAN =1 and ra between "+str(ra)+"-"+str(margin)+" and " +str(ra)+"+"+str(margin)+"and dec between "+str(dec)+"-"+str(margin)+" and "+ str(dec)+"+"+str(margin)).readlines() if (len(result)!=len(clean_result) and band=='u'): #only print this once in the u band. If it is unclean in u band (ex. cosmic ray, bright star..etc) then it must be unclean in the other bands too. print ("Data contain unclean images") clean=False unclean.write(str(ra)+" "+str(dec)+" "+str(radius)+" "+pgc) # unclean.write("{} {} {} {} \n".format(str(ra),str(dec),str(radius),pgc)) data =[] count =0 for i in result: if count>1: list =i.split(',') list[2]= list[2][:-1] data.append(list) count += 1 print (data) if (len(data)==0 and band=='r'): #you will only evounter non-footprint galaxy inint run , because after that we just take the footprint gaalxy already mosaiced (init) from rfits if (DEBUG): print ('The given ra, dec of this galaxy does not lie in the SDSS footprint. Onto the next galaxy!')#Exit Program.' output.write(str(ra)+ " "+ str(dec)+" "+str(radius)+"\n") # output.write("{} {} {} {} \n".format(str(ra),str(dec),str(radius),pgc)) output.write(str(ra)+" "+str(dec)+" "+str(radius)+" "+pgc) #sys.exit() return -1 #special value reserved for not in SDSS footprint galaxies else : if (DEBUG): print ( "Complete Query. These data lies within margin: ") print (data) # os.mkdir(filename) # os.chdir(filename) #if (os.path.exists(band)): #os.system("rm -r "+band) os.mkdir(band) os.chdir(band) os.mkdir ("raw") os.mkdir ("projected") os.chdir("raw") if (DEBUG): print ("Retrieving data from SDSS SAS server for "+ band +"band") for i in data : out = "frame-"+str(band)+"-"+str(i[0]).zfill(6)+"-"+str(i[1])+"-"+str(i[2]).zfill(4) os.system("wget http://mirror.sdss3.org/sas/dr10/boss/photoObj/frames/301/"+str(i[0])+"/"+str(i[1])+"/"+out+".fits.bz2") os.system("bunzip2 "+out+".fits.bz2") os.chdir("../") if (DEBUG) : print("Creating mosaic for "+band+" band.") outfile_r="SDSS_"+band+"_"+str(ra)+"_"+str(dec)+"r.fits" outfile="SDSS_"+band+"_"+str(ra)+"_"+str(dec)+".fits" if (len(data)==1): #With header info, len of processed result list is 1 if there is only 1 field lying in the margin, simply do mSubImage without mosaicing #This patch should not be necessary but the program is aparently not mosaicing for the case where there is only one field. print ("Only one field in region of interest") os.chdir("raw") montage.mSubimage(out+".fits",outfile,ra,dec,2*margin) # mSubImage takes xsize which should be twice the margin (margin measures center to edge of image) #os.chdir("../..") hdulist = pyfits.open(outfile) shutil.move(outfile,"../..") os.chdir("../..") else: montage.mImgtbl("raw","images.tbl") montage.mHdr(str(ra)+" "+str(dec),margin,out+".hdr") if (DEBUG): print ("Reprojecting images") os.chdir("raw") montage.mProjExec("../images.tbl","../"+out+".hdr","../projected", "../stats.tbl") os.chdir("..") montage.mImgtbl("projected","pimages.tbl") os.chdir("projected") montage.mAdd("../pimages.tbl","../"+out+".hdr","SDSS_"+out+".fits") # outfile_r="SDSS_{}_{}_{}r.fits".format(band,str(ra),str(dec)) #outfile_r="SDSS_"+band+"_"+str(ra)+"_"+str(dec)+"r.fits" montage.mSubimage("SDSS_"+out+".fits",outfile_r,ra,dec,2*margin) # mSubImage takes xsize which should be twice the margin (margin measures center to edge of image) shutil.move(outfile_r,os.getcwd()[:-11] )#if change to :-11 then move out of u,g,r,i,z directory, may be more convenient for mJPEG if (DEBUG) : print ("Completed Mosaic for " + band) os.chdir("../..") hdulist = pyfits.open(outfile_r) hdulist[0].header['RA']=ra hdulist[0].header['DEC']=dec hdulist[0].header['RADIUS']=radius hdulist[0].header['PGC']=pgc hdulist[0].header['NED']=("http://ned.ipac.caltech.edu/cgi-bin/objsearch?objname="+ str(hdulist[0].header['PGC'])+"&extend=no&hconst=73&omegam=0.27&omegav=0.73&corr_z=1&out_csys=Equatorial&out_equinox=J2000.0&obj_sort=RA+or+Longitude&of=pre_text&zv_breaker=30000.0&list_limit=5&img_stamp=YES") hdulist[0].header['CLEAN']=clean hdulist[0].header['MARGIN']=margin #if (os.path.exists(outfile)): #os.system("rm "+ outfile) hdulist.writeto(outfile) if (os.path.exists(outfile_r)): os.system("rm "+outfile_r) #print("Deleting") os.system("rm -r "+band+"/") print ("Completed Mosaic") return outfile
band =ele os.mkdir(band) os.chdir(band) os.mkdir ("raw") os.mkdir ("projected") os.chdir("raw") if (DEBUG): print ("Retrieving data from SDSS SAS server for "+ band +"band") for i in data : out = "frame-"+str(band)+"-"+str(i[0]).zfill(6)+"-"+str(i[1])+"-"+str(i[2]).zfill(4) os.system("wget http://data.sdss3.org/sas/dr10/boss/photoObj/frames/301/"+str(i[0])+"/"+ str(i[1]) +"/"+out+".fits.bz2") os.system("bunzip2 "+out+".fits.bz2") # print (os.getcwd()) os.chdir("../") if (DEBUG) : print("Creating mosaic for " +" "+ band + " band.") montage.mImgtbl("raw","images.tbl") montage.mHdr(str(ra)+" "+str(dec),radius,out+".hdr") if (DEBUG): print ("Reprojecting images") #Sometimes you can't find the files and result in images.tbl => empty doc #need to put data file inside raw AND unzip it so that Montage detect that it is a fit file os.chdir("raw") montage.mProjExec("../images.tbl","../"+out+".hdr","../projected", "../stats.tbl") os.chdir("..") montage.mImgtbl("projected","pimages.tbl") #mAdd coadds the reprojected images using the FITS header template and mImgtbl list. os.chdir("projected") montage.mAdd("../pimages.tbl","../"+out+".hdr","SDSS_"+out+".fits") montage.mSubimage("SDSS_"+out+".fits","SDSS_"+ele+"_"+str(trunc(ra))+"_"+str(trunc(dec))+".fits",ra,dec,2*margin) # mSubImage takes xsize which should be twice the margin (margin measures center to edge of image) shutil.move("SDSS_"+ele+"_"+str(trunc(ra))+"_"+str(trunc(dec))+".fits",os.getcwd()[:-11] )#if change to :-11 then move out of u,g,r,i,z directory, may be more convenient for mJPEG if (DEBUG) : print ("Completed Mosaic for " + band) os.chdir("../..") # Superimposing R,G,B image mosaics into TIFF using STIFF
def montage_cutout(ra=172.1305000, dec=+58.5615833, target=None, pix_size=None, width=5, survey='2MASS', band='j', output='montage.fits', verbose=True, clean=True, rotation=None): ''' Use Montage to automatically generate cutouts. https://github.com/Caltech-IPAC/Montage Propagate MAGZP keyword from the raw image header. ''' if verbose: print('Make header') if target is None: if isinstance(ra, str): target = '%s %s' % (ra, dec) else: target = '%.6f %.6f' % (ra, dec) montage.mHdr(target, width / 60., 'montage.hdr', system=None, equinox=None, height=None, pix_size=pix_size, rotation=rotation, band=band) if os.path.exists('montage-tmp'): os.system('rm -rf montage-tmp') if verbose: print('Generate %s/%s cutout:\n %s, width=%f arcmin' % (survey, band, target, width)) status = montage.mExec(survey, band, raw_dir=None, n_tile_x=None, n_tile_y=None, level_only=False, keep=True, remove=False, output_image=output, debug_level=1, region_header='montage.hdr', header=None, workspace_dir='montage-tmp') ### Get ZP file_ix = -1 scale_ix = -11 if survey == 'SDSS': file_ix = -3 scale_ix = -2 line = open('montage-tmp/remote.tbl').readlines()[3].split() print(line) scale = float(line[scale_ix]) print("scale: ", scale) raw_img = pyfits.open('montage-tmp/raw/%s' % (line[file_ix])) im = pyfits.open(output, mode='update') if survey == '2MASS': try: MAGZP = raw_img[0].header['MAGZP'] ZP = MAGZP - 2.5 * np.log10(scale) except: print( 'Error: There is no MAGZP value in the header. Using hard-coded value.' ) h = im[0].header c = h.cards for i in range(len(c)): if 'MAGZP' in ' '.join(np.cast[str](c[i])): break if band == 'j': MAGZP = 20.905 #average of the jband read_2 from 2MASS elif band == 'h': MAGZP = 20.3243041848514 #average of the 2 H band images from this region https://irsa.ipac.caltech.edu/workspace/TMP_M7TTAa_20769/IM/inv_qxqRD8/found.html ZP = MAGZP - 2.5 * np.log10(scale) if survey == 'SDSS': if 'FLUX20' not in list(raw_img[0].header.keys()): ZP = 27 else: ZP = 20 + 2.5 * np.log10(raw_img[0].header['FLUX20']) print(ZP) im[0].header['MAGZP'] = ZP im.flush()
band =ele os.mkdir(band) os.chdir(band) os.mkdir ("raw") os.mkdir ("projected") os.chdir("raw") if (DEBUG): print ("Retrieving data from SDSS SAS server for "+ band +"band") for i in data : out = "frame-"+str(band)+"-"+str(i[0]).zfill(6)+"-"+str(i[1])+"-"+str(i[2]).zfill(4) os.system("wget http://data.sdss3.org/sas/dr10/boss/photoObj/frames/301/"+str(i[0])+"/"+ str(i[1]) +"/"+out+".fits.bz2") os.system("bunzip2 "+out+".fits.bz2") # print (os.getcwd()) os.chdir("../") if (DEBUG) : print("Creating mosaic for " +" "+ band + " band.") montage.mImgtbl("raw","images.tbl") montage.mHdr(str(ra)+" "+str(dec),margin,out+".hdr") if (DEBUG): print ("Reprojecting images") #Sometimes you can't find the files and result in images.tbl => empty doc #need to put data file inside raw AND unzip it so that Montage detect that it is a fit file os.chdir("raw") montage.mProjExec("../images.tbl","../"+out+".hdr","../projected", "../stats.tbl") os.chdir("..") montage.mImgtbl("projected","pimages.tbl") #mAdd coadds the reprojected images using the FITS header template and mImgtbl list. os.chdir("projected") montage.mAdd("../pimages.tbl","../"+out+".hdr","SDSS_"+out+".fits") montage.mSubimage("SDSS_"+out+".fits","SDSS_"+ele+"_"+str(trunc(ra))+"_"+str(trunc(dec))+".fits",ra,dec,2*margin) # mSubImage takes xsize which should be twice the margin (margin measures center to edge of image) shutil.move("SDSS_"+ele+"_"+str(trunc(ra))+"_"+str(trunc(dec))+".fits",os.getcwd()[:-11] )#if change to :-11 then move out of u,g,r,i,z directory, may be more convenient for mJPEG if (DEBUG) : print ("Completed Mosaic for " + band) os.chdir("../..") # Superimposing R,G,B image mosaics into TIFF using STIFF