def Add_Srces(Input, fl): """ Take stock of confused sources, find their locations """ f = open('MODEL_all.xml','r') dat = f.readlines() f.close() nm = [] for line in dat: if ('source name' in line): nm.append( line.split('name="')[1].split('"')[0] ) check_cut = 18 check = [] for name in nm: if ( len(name) == check_cut ): check.append( name ) mdl = [] if ( len( check ) > 0 ): for nm in check: for i in np.arange( len(dat) ): if ( nm in dat[i] ): mdl = dat[i:i+11] GLON = [] GLAT = [] for j in np.arange( len(mdl) ): for line in mdl[j]: if ('RA' in line): RA = float(line.split('value="')[1].split('"')[0]) elif ('DEC' in line): DE = float(line.split('value="')[1].split('"')[0]) (lon,lat) = astCoords.convertCoords("J2000","GALACTIC",RA,DE,2000) GLON.append(lon) GLAT.append(lat) # Go through clouds. Check to see if CO clouds are close to any confused # sources. RAadd = [] DEadd = [] dirc = os.listdir('../Clouds_2sig') for nm in dirc: if (nm[:2] == 'CO'): close = 0 cld = pyfits.open('../Clouds_2sig/' + nm ) cldwcs = astWCS.WCS('../Clouds_2sig' + nm ) if ( cld[0].data.max() > 3 ): (xx,yy) = np.where( cld[0].data == cld[0].data.max() ) cld[0].data[ cld[0].data > 0 ] = 1 for j in np.arange( len(GLON) ): (gln,glt) = cldwcs.wcs2pix(GLON[j],GLAT[j]) if (np.sqrt( (xx-gln)**2 + (yy-glt)**2) < 15): close += 1 if ( (close == 0) and (cld[0].data.sum() > 50) ): (l,b) = cldwcs.pix2wcs(xx,yy) (RA1,DE1) = astCoords.convertCoords("GALACTIC","J2000",l,b,2000) RAadd.append(RA1) DEadd.append(DE1)
def get_MW_dens(raDec_Coord, Hthin=0.3): """ Calculate the Milky Way density at coordinates raDEC_Coord Parameters ---------- reDEC_Coord : list of 3 floats The coordinates of the cell for which to calculate the density. It is in the form of [RA, DEC, Distance] Hthin : float Scale height of the thin disk in kpc Returns ------- dens_tot : 1D-numpy array of 4 floats The Milky Way density as divided into its three components: [The thin disk contribution, thick disk, bulge, halo contribution] """ this_RA, this_DEC, this_Dist = raDec_Coord lval, bval = astCoords.convertCoords("J2000", "GALACTIC", this_RA, this_DEC, 2000) R, Z = galactic_to_cylindrical(bval, lval, this_Dist) rho_thin = rho_stellar_sun * MW_dens_thin(R, Z, Hthin) rho_thick = rho_stellar_sun * MW_fThick * MW_dens_thick(R, Z) rho_bulge = MW_dens_bulge(R, Z) rho_halo = rho_stellar_sun * MW_fHalo * MW_dens_halo(R, Z) dens_tot = [rho_thin, rho_thick, rho_bulge, rho_halo] return np.array(dens_tot)
def Sample_extinction(self, ra, dec, D): """ Sample the 3D dust grid to obtain the EJK at the location of the transient This is then converted to an extinction The result might be either '-100' or 'nan'. In that case we sample the Green extinction map if possible Parameters ---------- ra : float RA-coordinate in degrees dec : float DEC-coordinate in degrees D : float distance to transient in kpc Returns ------- A : dict The dust extinction in units of magnitude """ lon, lat = astCoords.convertCoords( "J2000", "GALACTIC", ra, dec, 2000 ) if lon > 180: lon -= 360 #Here lon runs from -180 to 180 A = {} EJK = self.f( [lon, lat, D] ) if EJK == emptyval or math.isnan(EJK): #Out of intpol range if self.useGreen: A = self.Green.Sample_extinction(ra, dec, D) else: A = self.Xtr_dust.Sample_extinction(ra, dec, D) else: for color in self.bands: A[color] = float(EJK) * self.RV_JK[color] return A
def InSchultheisBoundary(RA_lo, RA_hi, DEC_lo, DEC_hi): """ Test if any of the outer coordinates of the field of view are within the Schultheis+ dust map boundary Parameters ---------- RA_lo : float Lowest RA coordinate of the frame RA_hi : float Highest RA coordinate of the frame DEC_lo : float Lowest DEC coordinate of the frame DEC_hi : float Highest DEC coordinate of the frame Returns ------- A boolean. If any of the coordinates is inside the boundary, return True, else return False """ RAs = [RA_lo, RA_hi, RA_lo, RA_hi] DECs = [DEC_lo, DEC_lo, DEC_hi, DEC_hi] inSchultheis = False for i, RA in enumerate(RAs): lon, lat = astCoords.convertCoords("J2000", "GALACTIC", RA, DECs[i], 2000) lonbool = 0. < lon < 10. or 350. < lon < 360 latbool = -10. < lat < 5. if lonbool and latbool: inSchultheis = True break return inSchultheis
def Select(Inputs, fl): """ Select data centered around specified coordinates. Input: input data, ROI position, energy range, etc. fl : list of file nafl['srcmp_nocodg']= 'NoCODG/srcmap.fits' fl['outmdl1_nocodg']='NoCODG/mdl1_nocodg.xml' fl['outmdl2_nocodg']='NoCODG/mdl2_nocodg.xml' fl['outmap1_nocodg']='NoCODG/mdlmap1_nocodg.fits' fl['outmap2_nocodg']='NoCODG/mdlmap2_nocodg.fits'mes from SelectLoad.FileNames""" (ra, de) = astCoords.convertCoords("GALACTIC", "J2000", Inputs['glon'][1], Inputs['glat'][1], 2000) # gaps.filter['evclass']=2 # P7REP and earlier gaps.filter['evclass'] = 128 # source = 128, ultracleanveto = 1024 gaps.filter['evtype'] = 3 gaps.filter['ra'] = ra gaps.filter['dec'] = de gaps.filter['rad'] = Inputs['rad'][1] gaps.filter['zmax'] = 90 gaps.filter['tmin'] = Inputs['tmin'][1] gaps.filter['tmax'] = Inputs['tmax'][1] gaps.filter['infile'] = fl['files'] gaps.filter['outfile'] = fl['sel_fl'] gaps.filter['emin'] = Inputs['emin'][1] gaps.filter['emax'] = Inputs['emax'][1] if (fl['sel_fl'] in os.listdir('.')): return "Already ran gtselect, continue" else: gaps.filter.run() return "Done gtselect"
def pixtosystem(self, idxs, system=None, coords='data'): if self.coordsys == 'raw': raise WCSError("No usable WCS") if system == None: system = 'j2000' # Get a coordinates object based on ra/dec wcs transform ra_deg, dec_deg = self.pixtoradec(idxs, coords=coords) self.logger.debug("ra, dec = %f, %f" % (ra_deg, dec_deg)) # convert to alternate coord try: fromsys = self.coordsys.upper() tosys = system.upper() if fromsys == 'B1950': equinox = 1950.0 else: equinox = 2000.0 lon_deg, lat_deg = astCoords.convertCoords(fromsys, tosys, ra_deg, dec_deg, equinox) except Exception as e: raise WCSError("Error converting between coordinate systems '%s' and '%s': %s" % ( fromsys, tosys, str(e))) return (lon_deg, lat_deg)
def pixtosystem(self, idxs, system=None, coords='data'): if self.coordsys == 'raw': raise WCSError("No usable WCS") if system is None: system = 'j2000' # Get a coordinates object based on ra/dec wcs transform ra_deg, dec_deg = self.pixtoradec(idxs, coords=coords) self.logger.debug("ra, dec = %f, %f" % (ra_deg, dec_deg)) # convert to alternate coord try: fromsys = self.coordsys.upper() if fromsys == 'PIXEL': # these are really pixel values return (ra_deg, dec_deg) tosys = system.upper() if fromsys == 'B1950': equinox = 1950.0 else: equinox = 2000.0 lon_deg, lat_deg = astCoords.convertCoords(fromsys, tosys, ra_deg, dec_deg, equinox) except Exception as e: raise WCSError( "Error converting between coordinate systems '%s' and '%s': %s" % (fromsys, tosys, str(e))) return (lon_deg, lat_deg)
def do_reprojection(filename,input_sys,output_sys,size=None,center=None,outfile=None,list_o_files=False,hdu=0): system_lookup= {"GALACTIC":"galactic","J2000":"equatorial"} filename_lookup = {"GALACTIC":"GAL","J2000":"EQ"} if list_o_files: filename = filename[0] d,h = pyfits.getdata(filename,hdu,header=True) if not size: size = get_size(h) #Center coords always in J2000 if not center: x,y = get_center_position(h,input_sys,"J2000") else: x,y = ac.convertCoords(input_sys,"J2000",center[0],center[1],2000.) pix_size = get_pixel_size(h) #print(x,y) print(pix_size) coord_string = str(x)+" "+str(y) #print(coord_string) #Note that older versions on Montage do not work because of normal split #dividing the coordinates. Fix is to use shlex.split instead. montage.mHdr(coord_string,size,"Test.hdr",system=system_lookup[output_sys],pix_size=pix_size*3600) #print(yo) montage.mSubimage(filename,"Test.fits",x,y,size*2,hdu=hdu) if not outfile: #Try a reasonable guess for output file outfile = filename.replace(".fits","_"+filename_lookup[output_sys]+".fits") montage.mProject("Test.fits",outfile,"Test.hdr")
def loadDataFromHealpixMap(self, hpm, interpolate=False, hpCoords="J2000"): """ @brief copy data from a Healpix map (from healpy), return a lite map @param hpm healpy map @param interpolate use interpolation when copying @param hpCoords coordinates of hpm (e.g., "J2000"(RA, Dec) or "GALACTIC") Assumes that liteMap is in J2000 RA Dec. The Healpix map must contain the liteMap. """ inds = np.indices([self.Nx, self.Ny]) x = inds[0].ravel() y = inds[1].ravel() skyLinear = np.array(self.pixToSky(x, y)) ph = skyLinear[:, 0] th = skyLinear[:, 1] thOut = [] phOut = [] if hpCoords != "J2000": for i in range(len(th)): crd = astCoords.convertCoords("J2000", hpCoords, ph[i], th[i], 0.) phOut.append(crd[0]) thOut.append(crd[1]) thOut = np.array(thOut) phOut = np.array(phOut) else: thOut = th phOut = ph flTrace.issue("flipper.liteMap", 3, "theta (min, max): %f, %f" % (th.min(), th.max())) flTrace.issue("flipper.liteMap", 3, "phi (min, max): %f, %f" % (ph.min(), ph.max())) flTrace.issue( "flipper.liteMap", 3, "phiOut (min, max): (%f, %f) " % (phOut.min(), phOut.max())) flTrace.issue( "flipper.liteMap", 3, "thetaOut (min, max): (%f, %f) " % (thOut.min(), thOut.max())) phOut *= np.pi / 180 thOut = 90. - thOut #polar angle is 0 at north pole thOut *= np.pi / 180 flTrace.issue( "flipper.liteMap", 3, "phiOut rad (min, max): (%f, %f) " % (phOut.min(), phOut.max())) flTrace.issue( "flipper.liteMap", 3, "thetaOut rad (min, max): (%f, %f) " % (thOut.min(), thOut.max())) if interpolate: self.data[y, x] = healpy.get_interp_val(hpm, thOut, phOut) else: ind = healpy.ang2pix(healpy.get_nside(hpm), thOut, phOut) flTrace.issue( "flipper.liteMap", 3, "healpix indices (min,max): %d, %d" % (ind.min(), ind.max())) self.data[:] = 0. self.data[[y, x]] = hpm[ind]
def get_center_position(h,input_sys,output_sys): """Get position at center of map, convert to other system.""" WCS = aw.WCS(h,mode="pyfits") xcen = int(h["NAXIS1"]/2.) ycen = int(h["NAXIS2"]/2.) original_coords = WCS.pix2wcs(xcen,ycen) modified_coords = ac.convertCoords(input_sys,output_sys,original_coords[0],original_coords[1],2000.) modified_coords = original_coords return(modified_coords[0],modified_coords[1])
def get_galactic_height(alpha, delta, dist_kpc): """ Get the galactic height that corresponds to equatorial coordinates (RA,DEC) = (alpha, delta) at distance dist_kpc ------- returns: galactic height in kpc """ l, b = astCoords.convertCoords("J2000", "GALACTIC", alpha, delta, 2000) r, z = galactic_to_cylindrical(b, l, dist_kpc) return z
def get_center_position(h, input_sys, output_sys): """Get position at center of map, convert to other system.""" WCS = aw.WCS(h, mode="pyfits") xcen = int(h["NAXIS1"] / 2.) ycen = int(h["NAXIS2"] / 2.) original_coords = WCS.pix2wcs(xcen, ycen) modified_coords = ac.convertCoords(input_sys, output_sys, original_coords[0], original_coords[1], 2000.) modified_coords = original_coords return (modified_coords[0], modified_coords[1])
def zhelio(z, ra, dec): c = 299792.458 # from NED Vapex = 232.3 # km/s rad = numpy.pi / 180 lapex = 87.8 * rad bapex = 1.7 * rad gal = [astCoords.convertCoords('J2000', 'GALACTIC', x, y, 2000.0) for x, y in izip(ra, dec)] l, b = numpy.transpose(gal) l *= rad b *= rad Vh = numpy.sin(b) * numpy.sin(bapex) - \ numpy.cos(b) * numpy.cos(bapex)*numpy.cos(l-lapex) Vh *= Vapex return (c * z - Vh) / c
def loadDataFromHealpixMap(self, hpm, interpolate = False, hpCoords = "J2000"): """ @brief copy data from a Healpix map (from healpy), return a lite map @param hpm healpy map @param interpolate use interpolation when copying @param hpCoords coordinates of hpm (e.g., "J2000"(RA, Dec) or "GALACTIC") Assumes that liteMap is in J2000 RA Dec. The Healpix map must contain the liteMap. """ inds = np.indices([self.Nx, self.Ny]) x = inds[0].ravel() y = inds[1].ravel() skyLinear = np.array(self.pixToSky(x,y)) ph = skyLinear[:,0] th = skyLinear[:,1] thOut = [] phOut = [] if hpCoords != "J2000": for i in xrange(len(th)): crd = astCoords.convertCoords("J2000", hpCoords, ph[i], th[i], 0.) phOut.append(crd[0]) thOut.append(crd[1]) thOut = np.array(thOut) phOut = np.array(phOut) else: thOut = th phOut = ph flTrace.issue("flipper.liteMap", 3, "theta (min, max): %f, %f" % (th.min(), th.max())) flTrace.issue("flipper.liteMap", 3, "phi (min, max): %f, %f" % (ph.min(), ph.max())) flTrace.issue("flipper.liteMap", 3, "phiOut (min, max): (%f, %f) " % ( phOut.min(), phOut.max() )) flTrace.issue("flipper.liteMap", 3, "thetaOut (min, max): (%f, %f) " % ( thOut.min(), thOut.max() )) phOut *= np.pi/180 thOut = 90. - thOut #polar angle is 0 at north pole thOut *= np.pi/180 flTrace.issue("flipper.liteMap", 3, "phiOut rad (min, max): (%f, %f) " % ( phOut.min(), phOut.max() )) flTrace.issue("flipper.liteMap", 3, "thetaOut rad (min, max): (%f, %f) " % ( thOut.min(), thOut.max() )) if interpolate: self.data[y,x] = healpy.get_interp_val(hpm, thOut, phOut) else: ind = healpy.ang2pix( healpy.get_nside(hpm), thOut, phOut ) flTrace.issue("flipper.liteMap", 3, "healpix indices (min,max): %d, %d" % (ind.min(), ind.max())) self.data[:] = 0. self.data[[y,x]]=hpm[ind]
def Bexpmap_mk(Input,fl): """ Create the binned exposure map. Input: input data, ROI position, energy range, etc. fl : list of file names from SelectLoad.FileNames """ # need gaps.gtexpcube2 and it's parameters if (fl['bexpmp'] in os.listdir('.')): return "Binned exposure map made. Continue" (ra,de) = astCoords.convertCoords("GALACTIC","J2000", Input['glon'][1],Input['glat'][1],2000) if ( fl['time'] == False): gaps.gtexpcube2['infile'] = fl['ltcube'] elif (fl['time'] == True): gaps.gtexpcube2['infile'] = fl['ltcube_t'] else: print "Error in binned exposure map input." gaps.gtexpcube2['cmap'] = 'none' gaps.gtexpcube2['outfile'] = fl['bexpmp'] gaps.gtexpcube2['irfs'] = fl['IRFS'] gaps.gtexpcube2['nxpix'] = int(500) gaps.gtexpcube2['nypix'] = int(500) gaps.gtexpcube2['binsz'] = Input['binsz'][1] gaps.gtexpcube2['ebinalg'] = 'LOG' gaps.gtexpcube2['emin'] = Input['emin'][1] gaps.gtexpcube2['emax'] = Input['emax'][1] gaps.gtexpcube2['enumbins'] = int(Input['enumbins'][1]) if (Input['glat'][1] > 70): gaps.gtexpcube2['coordsys'] = 'CEL' gaps.gtexpcube2['xref'] = ra gaps.gtexpcube2['yref'] = de gaps.gtexpcube2['proj'] = 'AIT' gaps.gtexpcube2.run() return 0 else: gaps.gtexpcube2['coordsys'] = 'GAL' gaps.gtexpcube2['xref'] = Input['glon'][1] gaps.gtexpcube2['yref'] = Input['glat'][1] gaps.gtexpcube2['proj'] = 'CAR' gaps.gtexpcube2.run() return 0
def do_reprojection(filename, input_sys, output_sys, size=None, center=None, outfile=None, list_o_files=False, hdu=0): system_lookup = {"GALACTIC": "galactic", "J2000": "equatorial"} filename_lookup = {"GALACTIC": "GAL", "J2000": "EQ"} if list_o_files: filename = filename[0] d, h = pyfits.getdata(filename, hdu, header=True) if not size: size = get_size(h) #Center coords always in J2000 if not center: x, y = get_center_position(h, input_sys, "J2000") else: x, y = ac.convertCoords(input_sys, "J2000", center[0], center[1], 2000.) pix_size = get_pixel_size(h) #print(x,y) print(pix_size) coord_string = str(x) + " " + str(y) #print(coord_string) #Note that older versions on Montage do not work because of normal split #dividing the coordinates. Fix is to use shlex.split instead. montage.mHdr(coord_string, size, "Test.hdr", system=system_lookup[output_sys], pix_size=pix_size * 3600) #print(yo) montage.mSubimage(filename, "Test.fits", x, y, size * 2, hdu=hdu) if not outfile: #Try a reasonable guess for output file outfile = filename.replace(".fits", "_" + filename_lookup[output_sys] + ".fits") montage.mProject("Test.fits", outfile, "Test.hdr")
def Diffuse_ptsrc(Inputs,fl,dat,glon,glat,cnt): """ Modify model file to include additional point source Inputs: standard analysis inputs fl : file names for analysis dat : model xml string list glon : galactic longitude of source glat : galactic latitude of source""" (RA,dec) = astCoords.convertCoords("GALACTIC","J2000",glon,glat,2000) dat.append('<source name="Add_%s" type="PointSource">\n'%cnt) dat.append('\t<spectrum type="PowerLaw2">\n') dat.append('\t\t<parameter free="1" max="100000" min="1e-06" name="Integral" scale="1e-11" value="7" />\n') dat.append('\t\t<parameter free="1" max="1" min="-4" name="Index" scale="1" value="-2.2" />\n') dat.append('\t\t<parameter free="0" max="200000" min="20" name="LowerLimit" scale="1" value="%s" />\n'%Inputs['emin'][1]) dat.append('\t\t<parameter free="0" max="200000" min="20" name="UpperLimit" scale="1" value="%s" />\n'%Inputs['emax'][1]) dat.append('\t</spectrum>\n') dat.append('\t<spatialModel type="SkyDirFunction">\n') dat.append('\t\t<parameter free="0" max="360" min="-360" name="RA" scale="1" value="%s" />\n'%RA) dat.append('\t\t<parameter free="0" max="90" min="-90" name="DEC" scale="1" value="%s" />\n'%dec) dat.append('\t</spatialModel>\n') dat.append('</source>\n') return dat
def __init__(self, dimensions, fitsfile, location, name="undefined", real_units=True, epoch="J2000", maxnoise=float('inf'), fitswcs=None, savewcs=False, galcoords=False): self.header = Heady(name) #pull out the FITS header from the given file self.fitsheader = fitsfile[0].header self.header['units'] = self.fitsheader['BUNIT'] #The reason for the fitswcs argument is the astWCS.WCS method is #VERY SLOW. #generate a new WCS for the stamp if one isn't given if fitswcs == None: fitswcs = astWCS.WCS(self.fitsheader, mode="pyfits") self.header["radec"] = location if galcoords: self.header["galcoords"] = astCoords.convertCoords(epoch, "GALACTIC", location[0], location[1], fitswcs.getEpoch()) #This is the ndarray to pull the stamp from source = fitsfile[0].data #This check is needed for some odd fits files if len(fitsfile[0].data.shape) == 4: source = fitsfile[0].data[0][0] #pixcentre is the central pixel of the source pixcentre = fitswcs.wcs2pix(location[0], location[1]) self.header["offset"] = [pixcentre[0] - int(round(pixcentre[0])), pixcentre[1] - int(round(pixcentre[1]))] if pixcentre[0] < 0 or pixcentre[1] < 0: self.data = np.array([]) else: self.data = astImages.clipImageSectionPix(source, pixcentre[0], pixcentre[1], dimensions) #Once again, the astWCS methods are quite slow, disable them by default. if savewcs == True: #generate a new WCS wcsdimensions = dimensions * fitswcs.getPixelSizeDeg() clip = astImages.clipImageSectionWCS(source, fitswcs, location[0], location[1], wcsdimensions) self.fitswcs = clip['wcs'] else: self.fitswcs = None #compute_rms does not work on 0 sized images #Check the dimension error flag if self.data.shape != (dimensions, dimensions): self.header["flag"]["dimension_error"] = True self.header["flag"]["error_flag"] = 'd' else: self.header["bmean"] = self.compute_bmean() self.header["noise"] = self.compute_rms() self.header["bmedian"] = self.compute_bmedian() #We currently use bmedian rather than rms in order to #avoid discarding stamps with nearby sources #The factor of 0.674443 is used to convert a maximum value for the rms #into a maximum value for the median border. (50% of the gaussian falls #between -0.674443 and 0.674443 sigma.) if self.header["bmedian"] > 0.674443*maxnoise: self.header["flag"]["high_noise"] = True self.header["flag"]["error_flag"] = 'n' #This likely means that there is a nearby source, #as the source won't majorly affect the median if self.header["noise"] > 0.674443*maxnoise and not self.header["flag"]["high_noise"]: self.header["flag"]["near_source"] = True if np.isnan(self.data).any(): #Check for blank pixels self.header["flag"]["has_nans"] = True self.header["flag"]["error_flag"] = 'b'
def __init__(self, name, agal_id): self.name = name self.safe_name = name.replace('.', '_') self.id = agal_id try: self.glon = float(self.name[1:7]) self.glat = float(self.name[8:14]) except ValueError: #This is a not-observed source pass radec = ac.convertCoords("GALACTIC", "J2000", self.glon, self.glat, 2000.) self.ra = radec[0] self.dec = radec[1] self.comments = self.get_comments() ### Define the cutouts ### self.cutouts = [ "M24c", "IR4c", "IR3c", "IR2c", "IR1c", "TMkc", "TMhc", "TMjc", "H500c", "H350c", "H250c", "H170c", "H70c", "Agal", "RH_T", "RH_N", ] ### Define the standard cubes ### self.cubes = { "Mcube": ["M24c", "IR4c", "IR1c"], "Gcube": ["IR4c", "IR2c", "IR1c"], "Tcube": ["TMkc", "TMhc", "TMjc"], "Hcube": ["H500c", "H350c", "H250c"] } ### Define the standard images ### self.images = { "Mim": ["Mcube"], "Gim": ["Gcube"], "Tim": ["Tcube"], "Him": ["Hcube"], "Aim": ["Agal"], "RH_Tim": ["RH_T"], "RH_Nim": ["RH_N"], } self.storage_dir = malt.base + "/results/" self.auxiliary_log = self.storage_dir + "aux_data.log" ### Define the filename conventions ### self.set_filenames() #This seems possibly redundant self.apos = self.glon self.bpos = self.glat try: posradec = ac.convertCoords("GALACTIC", "J2000", self.apos, self.bpos, 2000.) self.apos_ra = posradec[0] self.bpos_dec = posradec[1] except IndexError: print("Failed to convert coordinates") self.apos = self.glon self.bpos = self.glat self.data_base = malt.base + "/sources/" self.line_name_trans = { "n2hp": r"$\mathrm{N_2H^+}$", "hcop": r"$\mathrm{HCO^+}$", "hcn": r"$\mathrm{HCN}$", "hnc": r"$\mathrm{HNC}$", "13c34s": r"$\mathrm{^{13}C^{34}S}$", "13cs": r"$\mathrm{^{13}CS}$", "c2h": r"$\mathrm{C_2H}$", "ch3cn": r"$\mathrm{CH_3CN}$", "h13cop": r"$\mathrm{H^{13}CO^{+}}$", "h41a": r"$\mathrm{H41\alpha}$", "hc3n": r"$\mathrm{HC_3N}$", "hc13ccn": r"$\mathrm{HC^{13}CCN}$", "hn13c": r"$\mathrm{HN^{13}C}$", "hnco404": r"$\mathrm{HNCO 4_{0,4}}$", "hnco413": r"$\mathrm{HNCO 4_{1,3}}$", "sio": r"$\mathrm{SiO}$" } self.line_spectra = { "n2hp": [], "hcop": [], "hcn": [], "hnc": [], "13c34s": [], "13cs": [], "c2h": [], "ch3cn": [], "h13cop": [], "h41a": [], "hc3n": [], "hc13ccn": [], "hn13c": [], "hnco404": [], "hnco413": [], "sio": [] } self.moment_name_trans = {"snr0": r"$\mathrm{SNR}$"}
def __init__(self,name,agal_id): self.name = name self.safe_name = name.replace('.','_') self.id = agal_id try: self.glon = float(self.name[1:7]) self.glat = float(self.name[8:14]) except ValueError: #This is a not-observed source pass radec = ac.convertCoords("GALACTIC","J2000",self.glon,self.glat,2000.) self.ra = radec[0] self.dec = radec[1] self.comments = self.get_comments() ### Define the cutouts ### self.cutouts = ["M24c","IR4c","IR3c","IR2c","IR1c", "TMkc","TMhc","TMjc", "H500c","H350c","H250c","H170c","H70c", "Agal","RH_T","RH_N", ] ### Define the standard cubes ### self.cubes = {"Mcube":["M24c","IR4c","IR1c"], "Gcube":["IR4c","IR2c","IR1c"], "Tcube":["TMkc","TMhc","TMjc"], "Hcube":["H500c","H350c","H250c"] } ### Define the standard images ### self.images = {"Mim":["Mcube"], "Gim":["Gcube"], "Tim":["Tcube"], "Him":["Hcube"], "Aim":["Agal"], "RH_Tim":["RH_T"], "RH_Nim":["RH_N"], } self.storage_dir = malt.base+"/results/" self.auxiliary_log = self.storage_dir+"aux_data.log" ### Define the filename conventions ### self.set_filenames() #This seems possibly redundant self.apos = self.glon self.bpos = self.glat try: posradec = ac.convertCoords("GALACTIC","J2000",self.apos,self.bpos,2000.) self.apos_ra = posradec[0] self.bpos_dec = posradec[1] except IndexError: print("Failed to convert coordinates") self.apos = self.glon self.bpos = self.glat self.data_base = malt.base+"/sources/" self.line_name_trans = {"n2hp":r"$\mathrm{N_2H^+}$","hcop":r"$\mathrm{HCO^+}$","hcn":r"$\mathrm{HCN}$","hnc":r"$\mathrm{HNC}$", "13c34s":r"$\mathrm{^{13}C^{34}S}$","13cs":r"$\mathrm{^{13}CS}$","c2h":r"$\mathrm{C_2H}$","ch3cn":r"$\mathrm{CH_3CN}$", "h13cop":r"$\mathrm{H^{13}CO^{+}}$","h41a":r"$\mathrm{H41\alpha}$","hc3n":r"$\mathrm{HC_3N}$", "hc13ccn":r"$\mathrm{HC^{13}CCN}$","hn13c":r"$\mathrm{HN^{13}C}$","hnco404":r"$\mathrm{HNCO 4_{0,4}}$", "hnco413":r"$\mathrm{HNCO 4_{1,3}}$","sio":r"$\mathrm{SiO}$"} self.line_spectra = {"n2hp":[],"hcop":[],"hcn":[],"hnc":[], "13c34s":[],"13cs":[],"c2h":[],"ch3cn":[], "h13cop":[],"h41a":[],"hc3n":[], "hc13ccn":[],"hn13c":[],"hnco404":[], "hnco413":[],"sio":[]} self.moment_name_trans = {"snr0":r"$\mathrm{SNR}$"}
def binning(Inputs, fl): """ Bin selected data in three ways. (1) Counts map: used primarily for visual inspection, not used in full likelihood analysis. (2) Light curve: to be used to remove data during a blazar flare for model robustness. (3) Counts cube: data binned in spatial position AND energy. """ gaps.evtbin['algorithm'] = fl['binalg'] gaps.evtbin['evfile'] = fl['mkt_fl'] gaps.evtbin['scfile'] = fl['SC_fl'] (ra, de) = astCoords.convertCoords("GALACTIC", "J2000", Inputs['glon'][1], Inputs['glat'][1], 2000) gaps.evtbin['axisrot'] = 0.0 # Change other things in case we choose lightcurve, counts map, # or counts cube if (fl['binalg'].lower() == 'cmap'): if (fl['CMAP'] in os.listdir('.')): return "CMAP made, continue" else: gaps.evtbin['nxpix'] = int(Inputs['nxpix'][1]) gaps.evtbin['nypix'] = int(Inputs['nypix'][1]) gaps.evtbin['binsz'] = Inputs['binsz'][1] gaps.evtbin['outfile'] = fl['CMAP'] if (Inputs['glat'][1] > 70): gaps.evtbin['coordsys'] = 'CEL' gaps.evtbin['xref'] = ra gaps.evtbin['yref'] = de gaps.evtbin['proj'] = 'AIT' gaps.evtbin.run() else: gaps.evtbin['coordsys'] = 'GAL' gaps.evtbin['xref'] = Inputs['glon'][1] gaps.evtbin['yref'] = Inputs['glat'][1] gaps.evtbin['proj'] = 'CAR' gaps.evtbin.run() gaps.evtbin['nxpix'] = int(Inputs['nxpix'][1] + (10. / Inputs['binsz'][1])) gaps.evtbin['nypix'] = int(Inputs['nypix'][1] + (10. / Inputs['binsz'][1])) gaps.evtbin['outfile'] = fl['CMAP_big'] if (Inputs['glat'][1] > 70): gaps.evtbin['coordsys'] = 'CEL' gaps.evtbin['xref'] = ra gaps.evtbin['yref'] = de gaps.evtbin['proj'] = 'AIT' gaps.evtbin.run() else: gaps.evtbin['coordsys'] = 'GAL' gaps.evtbin['xref'] = Inputs['glon'][1] gaps.evtbin['yref'] = Inputs['glat'][1] gaps.evtbin['proj'] = 'CAR' gaps.evtbin.run() return 0 elif (fl['binalg'].lower() == 'lc'): gaps.evtbin['outfile'] = fl['LC'] gaps.evtbin['tbinalg'] = 'LIN' gaps.evtbin['tstart'] = Inputs['tmin'][1] gaps.evtbin['tstop'] = Inputs['tmax'][1] gaps.evtbin['dtime'] = 3592000 # 30 day time bins if (fl['LC'] in os.listdir('.')): return "Light Curve made, continue" else: gaps.evtbin.run() return 0 elif (fl['binalg'].lower() == 'ccube'): if (fl['CCUBE'] in os.listdir('.')): return "CCUBE present, continue" else: gaps.evtbin['outfile'] = fl['CCUBE'] gaps.evtbin['nxpix'] = int(Inputs['nxpix'][1]) gaps.evtbin['nypix'] = int(Inputs['nypix'][1]) gaps.evtbin['binsz'] = Inputs['binsz'][1] gaps.evtbin['ebinalg'] = 'LOG' gaps.evtbin['emin'] = Inputs['emin'][1] gaps.evtbin['emax'] = Inputs['emax'][1] gaps.evtbin['enumbins'] = int(Inputs['enumbins'][1]) if (Inputs['glat'][1] > 70): gaps.evtbin['coordsys'] = 'CEL' gaps.evtbin['xref'] = ra gaps.evtbin['yref'] = de gaps.evtbin['proj'] = 'AIT' gaps.evtbin.run() return 0 else: gaps.evtbin['coordsys'] = 'GAL' gaps.evtbin['xref'] = Inputs['glon'][1] gaps.evtbin['yref'] = Inputs['glat'][1] gaps.evtbin['proj'] = 'CAR' gaps.evtbin.run() return 0
def testconvertCoords(self): for insystem, outsystem, xcoor, ycoor, epoch, result in self.convertCoords: answer = astCoords.convertCoords(insystem, outsystem, xcoor, ycoor, epoch) self.assertEqual(result, answer)
f = open(flname,'r') dat = f.readlines() f.close() #coim = pyfits.open('/home/abrahams/HICO_survey/SourceSearch/l%sb%s/CO_temp.fits'%(l,b)) coim = pyfits.open('/home/abrahams/HICO_survey/SourceSearch/RhoOph/l353b17/ro_pass8_files/Aug7_Ryan_files/CO_temp.fits') co_wcs = astWCS.WCS(coim[0].header,mode='pyfits') coim[0].data = np.transpose( coim[0].data ) d = np.array([]) co = np.array([]) for i in range( len(dat) ): glon = float( dat[i].split(',')[0][1:] ) glat = float( dat[i].split(',')[1][:-1] ) (glon,glat) = astCoords.convertCoords("GALACTIC","J2000",glon,glat,2000) try: converge = int( dat[i].split('converge = ')[1] ) d = np.append( d, 0 ) continue except: try: d = np.append(d, float(dat[i].split(',')[2].split('[ ')[1].split(']')[0])) except: d = np.append(d,0) (x,y) = co_wcs.wcs2pix(glon,glat) co = np.append( co, coim[0].data[x,y] ) if ( len(co) == len(d)-1 ):
def MaskSrc(Inputs,fl,like): """ We go through the likelihood model and mask the point sources in the CMAP, CCUBE, and bexpmap. Input = input parameters, from data selection stage fl = list of filenames used for analysis like = likelihood object, used to get point sources to mask ----------------------------------------------------- Outputs: Writes a masked binned exposure map and masked counts map and counts cube to use to later analysis. Also removes all point sources from the likelihood objects and returns the object 'like'. """ bx = pyfits.open(fl['bexpmp']) cm = pyfits.open(fl['CMAP']) ccb= pyfits.open(fl['CCUBE']) co = pyfits.open(fl['COmpnon']) ebv= pyfits.open(fl['Ebv_%snon'%fl['opacity']]) hi = pyfits.open(fl['HImp_%snon'%fl['opacity']]) hi_= pyfits.open(fl['HImp_%snon_gt'%fl['opacity']]) cat = fl['cat_var'] cmwcs = astWCS.WCS( cm[0].header, mode="pyfits" ) bxwcs = astWCS.WCS( bx[0].header, mode="pyfits" ) cowcs = astWCS.WCS( co[0].header, mode="pyfits" ) Eslp = (np.log10(Inputs['emax'][1])-np.log10(Inputs['emin'][1]))/ccb[0].data.shape[0] En = 10**(0.5*(np.log10(Inputs['emin'][1])+np.log10(Inputs['emax'][1]))) # Prep for gas templates co1 = np.zeros( (ccb[0].header['NAXIS3'],co[0].header['NAXIS2'], co[0].header['NAXIS1']) ) ebv1 = np.zeros( (ccb[0].header['NAXIS3'],co[0].header['NAXIS2'], co[0].header['NAXIS1']) ) hi1 = np.zeros( (ccb[0].header['NAXIS3'],co[0].header['NAXIS2'], co[0].header['NAXIS1']) ) hi2 = np.zeros( (ccb[0].header['NAXIS3'],co[0].header['NAXIS2'], co[0].header['NAXIS1']) ) for i in np.arange( ccb[0].header['NAXIS3'] ): co1[i,:,:] = co[0].data ebv1[i,:,:]= ebv[0].data hi1[i,:,:] = hi[0].data hi2[i,:,:] = hi_[0].data # Cycle through names and remove.... for name in like.sourceNames(): if ('Add' in name ): ra = like.model[name].funcs['Position'].params['RA'].getValue() de = like.model[name].funcs['Position'].params['DEC'].getValue() (glon,glat) = astCoords.convertCoords("J2000","GALACTIC",ra,de,2000) (cm,ccb,bx) = Mask_RM(fl,glon,glat,-1,cm,ccb, bx,cmwcs,bxwcs,Eslp,En) (co1,ebv1,hi1,hi2) = TempMask_RM(fl,glon,glat,cowcs,co1,ebv1,hi1,hi2,-1) like.deleteSource(name) elif ( '3FGL' in name ): nm = name[1:5] + ' ' + name[5:] ind = np.where( cat[1].data['Source_Name'] == nm )[0][0] (glon,glat) = ( cat[1].data['GLON'][ind], cat[1].data['GLAT'][ind] ) (cm,ccb,bx) = Mask_RM(fl,glon,glat,ind,cm,ccb, bx,cmwcs,bxwcs,Eslp,En) (co1,ebv1,hi1,hi2) = TempMask_RM(fl,glon,glat,cowcs,co1,ebv1,hi1,hi2,ind) like.deleteSource(name) os.system('mv %s bexpmap_nomsk.fits'%fl['bexpmp']) os.system('mv %s CMAP_nomsk.fits'%fl['CMAP']) os.system('mv %s CCUBE_nomsk.fits'%fl['CCUBE']) bx.writeto(fl['bexpmp']) ccb.writeto(fl['CCUBE']) cm.writeto(fl['CMAP']) os.system('mv %s ../CO_temp_nomsk.fits'%fl['COmpnon']) os.system('mv %s ../Ebv_%s_nomsk.fits'%(fl['Ebv_%snon'%fl['opacity']],fl['opacity'])) os.system('mv %s ../HI_%s_nomsk.fits'%(fl['HImp_%snon'%fl['opacity']],fl['opacity'])) os.system('mv %s ../HI_%s_gt_nomsk.fits'%(fl['HImp_%snon_gt'%fl['opacity']],fl['opacity'])) com= pyfits.PrimaryHDU( co1 ) him= pyfits.PrimaryHDU( hi1 ) hi_m=pyfits.PrimaryHDU( hi2 ) ebvm=pyfits.PrimaryHDU( ebv1) com.header = co[0].header com.header['CTYPE3'] = ('ENERGY') com.header['CRPIX3'] = ( 1, 'Reference Pixel') com.header['CRVAL3'] = (Inputs['emin'][1], 'Energy at the reference pixel') com.header['CDELT3'] = (ccb[0].header['CDELT3'],'Z-axis incr per pixel of physical coord at posi') com.header['CUNIT3'] = ('MeV','Physical Units for z-axis') him.header = com.header ebvm.header= com.header hi_m.header= com.header return "Finished Masking"