def __init__(self, coordinate_or_galaxy): """ This function ... :param coordinate_or_galaxy: :return: """ # Check whether the user/extinction directory exists if not fs.is_directory(extinction_path): fs.create_directory(extinction_path) # Get query if isinstance(coordinate_or_galaxy, basestring): query = coordinate_or_galaxy else: query = coordinate_or_galaxy.to_astropy() # Determine the path to the local extinction table path = fs.join(extinction_path, str(query)) # Check if the local file exists if not fs.is_file(path): # Get the extinction table from IRSA self.table = IrsaDust.get_extinction_table(query) # Save the table tables.write(self.table, path) # Load the table else: self.table = tables.from_file(path)
def bsnip_edits(): bsnip = glob.glob ('../../data/bsnip/*.flm') bsnip_sn = [] b = [] v = [] for i in range (len(bsnip)): bsnip[i] = bsnip[i].split('-')[0] bsnip[i] = bsnip[i].split('/') bsnip[i] = bsnip[i][len(bsnip[i])-1] if i > 0:#removes redundant sn names if bsnip[i-1] != bsnip[i]: bsnip_sn.append(bsnip[i]) cut_index = 0 flag = False for i in range(len(bsnip_sn)): if (not flag): print "passing",bsnip_sn[i] #certain cases for specfic sn names if bsnip_sn[i] == 'sn2007s1':#want to start at first changed sn print "start with",len(bsnip_sn),"sn" print "START at index:",i cut_index = i flag = True #start here if(flag): print "looking at",bsnip_sn[i] if bsnip_sn[i] == "sn2007s1": bsnip_sn[i] = "SNF20071021-000" print "changed to",bsnip_sn[i] if bsnip_sn[i] == "sn2008s1": bsnip_sn[i] = "SNF20080514-002" print "changed to",bsnip_sn[i] if bsnip_sn[i] == "sn2008r3": bsnip_sn[i] = "sn1989a"###doesn't work print "changed to",bsnip_sn[i] if bsnip_sn[i] == "sn2008s3": bsnip_sn[i] = "SNF20080825-006" print "changed to",bsnip_sn[i] if bsnip_sn[i] == "sn2008s4": bsnip_sn[i] = "sn1989a"###can't find correct name print "changed to",bsnip_sn[i] if bsnip_sn[i] == "sn2008s5": bsnip_sn[i] = "SNF20080909-030" print "changed to",bsnip_sn[i] if bsnip_sn[i] == "sn2008s8": bsnip_sn[i] = "sn1989a"###look for correct names print "changed to",bsnip_sn[i] ext = IrsaDust.get_extinction_table(bsnip_sn[i]) print ext print ext[1][0],ext[1][3] print ext[2][0],ext[2][3] b.append(ext[1][3]) v.append(ext[2][3]) print "stopped successfullly?...cutting table" del bsnip_sn[:cut_index] param = Table([bsnip_sn,b,v]) print param ascii.write(param,'bsnip_extinc_added.dat')
def query_bsnip(): #Creates extinction.dat from bsnip data #list of files bsnip = glob.glob ('../../data/bsnip/*.flm') bsnip_sn = [] for i in range (len(bsnip)): bsnip[i] = bsnip[i].split('-')[0] bsnip[i] = bsnip[i].split('/') bsnip[i] = bsnip[i][len(bsnip[i])-1] if i > 0:#removes redundant sn names if bsnip[i-1] != bsnip[i]: bsnip_sn.append(bsnip[i]) b = [] v = [] for i in range(len(bsnip_sn)): print "looking at",bsnip_sn[i] #certain cases for specfic sn names if bsnip_sn[i] == 'sn2007s1': print "WARNING" del bsnip_sn[i:] break ext = IrsaDust.get_extinction_table(bsnip_sn[i]) print ext print ext[1][0],ext[1][3] print ext[2][0],ext[2][3] b.append(ext[1][3]) v.append(ext[2][3]) #makes table in format 'sn','B','V' param = Table([bsnip_sn,b,v]) ascii.write(param,'bsnip_extinc.dat')
def atable(self): """ Return the extinction in mag in starndard filters """ #query IRAS service #http://irsa.ipac.caltech.edu/applications/DUST/ if (self.coord == -1): print "Error: coordinates must be set" exit() from astroquery.irsa_dust import IrsaDust tab = IrsaDust.get_extinction_table(self.coord) return tab
def __init__(self, coordinate_or_galaxy): """ This function ... :param coordinate_or_galaxy: :return: """ # Check whether the user/attenuation directory exists if not fs.is_directory(attenuation_path): fs.create_directory(attenuation_path) # Get query if types.is_string_type(coordinate_or_galaxy): query = coordinate_or_galaxy else: query = coordinate_or_galaxy.to_astropy() # Determine the path to the local extinction table path = fs.join(attenuation_path, str(query)) # Check if the local file exists if not fs.is_file(path): # Get the extinction table from IRSA self.table = IrsaDust.get_extinction_table(query) # Save the table tables.write(self.table, path) # Load the table else: self.table = tables.from_file(path) # Check whether the RFILTER table is present, load if fs.is_file(rfilter_table_path): self.rfilter = RFilterTable.from_file(rfilter_table_path) else: # or create new self.rfilter = RFilterTable() self.rfilter.path = rfilter_table_path self.rfilter.save()
def query_cfa(): #gets cfa folder pathnames cfa = glob.glob ('../../data/cfa/*') #Truncates cfa pathname into needed format and removes cfa.dat files del cfa[0] del cfa[0] for i in range(len(cfa)): cfa[i]= cfa[i][15:] print cfa[i] b = [] v = [] for i in range(len(cfa)): print "looking at",cfa[i] ext = IrsaDust.get_extinction_table(cfa[i]) print ext print ext[1][0],ext[1][3] print ext[2][0],ext[2][3] b.append(ext[1][3]) v.append(ext[2][3]) #makes table in format 'sn','B','V' param = Table([cfa,b,v]) ascii.write(param,'extinctioncfa.dat')
def tableFill(dam, ra, dec, appender, nme): t = Table(None) Am = Column(name='Arcminute') North = Column(name='North') East = Column(name='East') South = Column(name='South') West = Column(name='West') t.add_columns([Am, North, East, South, West]) tA_v = [] curVal = [None] * 4 #n = 0, e = 1, s = 2, w = 3 coord = [None] * 4 #n = 0, e = 1, s = 2, w = 3 #get values for each arcminute for j in range(0, dam + 1): fourCoord(j, ra, dec, coord) t.add_row() t[j][0] = j for i in range(0, 4): C = coordinates.SkyCoord(coord[i]) table = IrsaDust.get_extinction_table(C.fk5, show_progress=False) curVal[i] = (table['A_SandF'][2]) t[j][i + 1] = curVal[i] curVal = curVal[:] tA_v.append(curVal) t.add_row() for i in range( 0, 5): #this adds a blank line to the table to separate queries t[j + 1][i] = None n = [nme] namesTable = Table([n], names=('n')) final_name = namesTable.to_pandas() final_vals = t.to_pandas() from pandas import ExcelWriter with open('A_v Values.csv', appender) as f: final_name.to_csv(f, header=False, index=False) appender = 'a' with open('A_v Values.csv', appender) as f: final_vals.to_csv(f, header=True, index=False, sep=',') return (tA_v) #gets the data from IRSA database and stores A_v in array
def sed(epic='211800191',ra='132.884782',dec='17.319834'): os.chdir('outputs/') urllib.urlretrieve('http://vizier.u-strasbg.fr/viz-bin/sed?-c='+ra+"%2C"+dec+'&-c.rs=1', epic+'_sed.vot') print 'http://vizier.u-strasbg.fr/viz-bin/sed?-c='+ra+"%2C"+dec+'&-c.rs=0.005' tb = votable.parse_single_table(epic+'_sed.vot') data = tb.array wav_all = 3e5 * 1e4 / data['_sed_freq'].data #angstrom f_all = data['_sed_flux'].data unc_all = data['_sed_eflux'].data filters = data['_sed_filter'].data filter_dict = {'2MASS:Ks':'2MASS Ks','2MASS:J':'2MASS J','2MASS:H':'2MASS H','WISE:W1':'WISE-1','WISE:W2':'WISE-2','SDSS:u':'SDSS u','SDSS:g':'SDSS g',\ 'SDSS:r':'SDSS r','SDSS:i':'SDSS i','SDSS:z':'SDSS z'} c = coord.SkyCoord(float(ra)*u.deg,float(dec)*u.deg,frame='icrs') tb = IrsaDust.get_extinction_table(c) filters2 = tb['Filter_name'] allA = tb['A_SandF'] A = [] f_ob_orig = [] wav_ob = [] unc = [] for f in filters: if f in filter_dict.keys(): filtmatch = filter_dict[f] ind = where(filters2==filtmatch)[0] A.append(mean(allA[ind])) ind = where(filters==f)[0] f_ob_orig.append(mean(f_all[ind])) wav_ob.append(mean(wav_all[ind])) unc.append(mean(unc_all[ind])) f_ob_orig = array(f_ob_orig) A = array(A) wav_ob = array(wav_ob) unc = array(unc) f_ob = (f_ob_orig*10**(A/2.5)) metallicity = ['ckp00'] m = [0.0] t = arange(3500,13000,250) t2 = arange(14000,50000,1000) t = concatenate((t,t2),axis=1) log_g = ['g20','g25','g30','g35','g40','g45','g50'] g = arange(2.,5.,0.5) best_m =0 best_t =0 best_g =0 best_off =0.0 chi2_rec = 1e6 os.chdir('..') for im, mval in enumerate(m): for it, tval in enumerate(t): for ig, gval in enumerate(g): #load model hdulist = pyfits.open('fits/'+metallicity[im]+'/'+metallicity[im]+'_'+str(tval)+'.fits') data = hdulist[1].data wmod = data['WAVELENGTH'] fmod = data[log_g[ig]]*3.34e4*wmod**2 #fit observations f_int = exp( interp(log(wav_ob), log(wmod), log(fmod)) ) offsets = linspace(log(min(f_ob/f_int)), log(max(f_ob/f_int)), 51) for i_off, offset in enumerate(offsets): chi2 = sum((f_int*exp(offset)-f_ob)**2) print 'chi2=', chi2, mval, tval, gval if chi2 < chi2_rec: chi2_rec = chi2 best_m = im best_g = ig best_t = it best_off = offset print 'best fit: m=', m[best_m], 'T=', t[best_t], 'log g=', g[best_g] hdulist = pyfits.open('fits/'+metallicity[best_m]+'/'+metallicity[best_m]+'_'+str(t[best_t])+'.fits') data = hdulist[1].data wmod = data['WAVELENGTH'] fmod = data[log_g[best_g]]*3.34e4*wmod**2 fmod *= exp(best_off) plt.close('all') fig = plt.figure(figsize=(8,5)) plt.plot(wmod/1e4, fmod,label='Castelli & Kurucz model') plt.xscale('log') plt.plot(wav_ob/1e4, f_ob_orig, lw=0, marker='s', label='Uncorrected',ms=10) plt.plot(wav_ob/1e4, f_ob, lw=0, marker='o', label='Corrected for extinction',ms=10) plt.xlabel(r'${\rm Wavelength} \ (\mu m)}$',fontsize=18) plt.xlim(0.1,max(wmod)/1e4) plt.ylabel(r'$F_{\nu} \ {\rm (Jy)}$',fontsize=18) plt.legend() plt.savefig('outputs/'+epic+'_sed.pdf',dpi=150) return t[best_t]
def sed(kic, ra, dec, ax): """ Retrieves star's magnitudes in different bands from ExoFOP and fits SED from Castelli & Kurucz library. Note: you need an account to access Kepler ExoFOP data. I do not have one, so I'm using the format for K2 targets instead. :param kic: K2 EPIC ID of target. :param ra: RA of target. :param dec: Dec of target. :param ax: plot handle of SED plot. :return: ax: SED plot. """ urllib.urlretrieve( 'http://vizier.u-strasbg.fr/viz-bin/sed?-c=' + ra + "%2C" + dec + '&-c.rs=1', kic + '_sed.vot') print 'http://vizier.u-strasbg.fr/viz-bin/sed?-c=' + ra + "%2C" + dec + '&-c.rs=0.005' tb = votable.parse_single_table(kic + '_sed.vot') data = tb.array wav_all = 3e5 * 1e4 / data['_sed_freq'].data # angstrom f_all = data['_sed_flux'].data unc_all = data['_sed_eflux'].data filters = data['_sed_filter'].data filter_dict = {'2MASS:Ks': '2MASS Ks', '2MASS:J': '2MASS J', '2MASS:H': '2MASS H', 'WISE:W1': 'WISE-1', 'WISE:W2': 'WISE-2', 'SDSS:u': 'SDSS u', 'SDSS:g': 'SDSS g', \ 'SDSS:r': 'SDSS r', 'SDSS:i': 'SDSS i', 'SDSS:z': 'SDSS z'} c = coord.SkyCoord(float(ra) * u.deg, float(dec) * u.deg, frame='icrs') tb = IrsaDust.get_extinction_table(c) filters2 = tb['Filter_name'] allA = tb['A_SandF'] A = [] f_ob_orig = [] wav_ob = [] unc = [] for f in filters: if f in filter_dict.keys(): filtmatch = filter_dict[f] ind = np.where(filters2 == filtmatch)[0] A.append(np.mean(allA[ind])) ind = np.where(filters == f)[0] f_ob_orig.append(np.mean(f_all[ind])) wav_ob.append(np.mean(wav_all[ind])) unc.append(np.mean(unc_all[ind])) f_ob_orig = np.array(f_ob_orig) A = np.array(A) wav_ob = np.array(wav_ob) f_ob = (f_ob_orig * 10**(A / 2.5)) metallicity = ['ckp00'] m = [0.0] t = np.arange(3500, 13000, 250) t2 = np.arange(14000, 50000, 1000) t = np.concatenate((t, t2)) log_g = ['g20', 'g25', 'g30', 'g35', 'g40', 'g45', 'g50'] g = np.arange(2., 5., 0.5) best_m = 0 best_t = 0 best_g = 0 best_off = 0.0 chi2_rec = 1e6 # Do grid search to find best-fit SED # This loop can probably be parallelized to save time for im, mval in enumerate(m): for it, tval in enumerate(t): for ig, gval in enumerate(g): # load model hdulist = pyfits.open('fits/' + metallicity[im] + '/' + metallicity[im] + '_' + str(tval) + '.fits') data = hdulist[1].data wmod = data['WAVELENGTH'] fmod = data[log_g[ig]] * 3.34e4 * wmod**2 # fit observations f_int = np.exp( np.interp(np.log(wav_ob), np.log(wmod), np.log(fmod))) offsets = np.linspace(np.log(min(f_ob / f_int)), np.log(max(f_ob / f_int)), 51) for i_off, offset in enumerate(offsets): chi2 = sum((f_int * np.exp(offset) - f_ob)**2) print 'chi2=', chi2, mval, tval, gval if chi2 < chi2_rec: chi2_rec = chi2 best_m = im best_g = ig best_t = it best_off = offset print 'best fit: m=', m[best_m], 'T=', t[best_t], 'log g=', g[best_g] hdulist = pyfits.open('fits/' + metallicity[best_m] + '/' + metallicity[best_m] + '_' + str(t[best_t]) + '.fits') data = hdulist[1].data wmod = data['WAVELENGTH'] fmod = data[log_g[best_g]] * 3.34e4 * wmod**2 fmod *= np.exp(best_off) ax.plot(wmod / 1e4, fmod, label='Castelli & Kurucz model') ax.set_xscale('log') ax.plot(wav_ob / 1e4, f_ob_orig, lw=0, marker='s', label='Uncorrected', ms=10) ax.plot(wav_ob / 1e4, f_ob, lw=0, marker='o', label='Corrected for extinction', ms=10) ax.set_xlabel(r'${\rm Wavelength} \ (\mu m)}$', fontsize=18) ax.set_xlim(0.1, max(wmod) / 1e4) ax.set_ylabel(r'$F_{\nu} \ {\rm (Jy)}$', fontsize=18) ax.legend() return ax
cspmini= [] # a shorter list for i in range(len(csp)): csp[i] = 'sn20'+csp[i][20:24] # print csp[i] for sn in csp: # delete duplicate items if sn not in cspmini: cspmini.append(sn) for i in range(len(cspmini)): # For single-letter SNs if cspmini[i].endswith('_') : cspmini[i] = cspmini[i][:-1] print cspmini b = [] v = [] for i in range(len(cspmini)): print "looking at SN",cspmini[i] ext = IrsaDust.get_extinction_table(cspmini[i]) print ext print ext[1][0],ext[1][3] print ext[2][0],ext[2][3] b.append(ext[1][3]) v.append(ext[2][3]) param = Table([cspmini,b,v],names=('sn','B','V')) ascii.write(param,'extinctioncsp.dat')
def add_extinction_old(catalogue, getOptical=True, getIR=True, url_ned='http://ned.ipac.caltech.edu/cgi-bin/calc'): nsrcs = len(catalogue) A = np.full((nsrcs, 10), np.nan) coords = SkyCoord(ra=catalogue['posRA'], dec=catalogue['posDec']) start = 0 for i in trange(nsrcs): # Get optical extinction from NED if getOptical: payload = { 'in_csys': 'Equatorial', 'in_equinox': 'J2000.0', 'obs_epoch': '2000.0', 'out_csys': 'Equatorial', 'out_equinox': 'J2000.0', 'lon': '{:.6f}d'.format(coords[i].ra.value), 'lat': '{:.6f}d'.format(coords[i].dec.value) } end = time.time() if end - start < 1.0: time.sleep(1.0) else: r = requests.get(url_ned, params=payload) start = time.time() soup = BeautifulSoup(r.text, 'html5lib') ned_table = soup.find('div', id='moreBANDS') ned_table = ned_table.find('table') j = 0 for row in ned_table.findAll("tr"): cells = row.findAll("td") if len(cells): if cells[0].contents[0] == 'PS1' and j < 5: A[i, j] = cells[3].contents[0] j += 1 if getIR: t = IrsaDust.get_extinction_table(coords[i]) # NIR extinction if catalogue['NIRobjID'][i] > 0: if catalogue['NIR_SURVEY'][i] == '2MASS': A[i, 5:8] = t['A_SandF'][16:19] elif (catalogue['NIR_SURVEY'][i] == 'UKIDSS' or catalogue['NIR_SURVEY'][i] == 'VISTA'): A[i, 5:8] = t['A_SandF'][13:16] # WISE extinction if catalogue['WSID'][i] > 0: A[i, 8:] = t['A_SandF'][23:25] A = Table( A, names=['Ag', 'Ar', 'Ai', 'Az', 'Ay', 'AJ', 'AH', 'AK', 'AW1', 'AW2']) catalogue = hstack([catalogue, A], join_type='exact') return catalogue