def geton24imageflag(self): iraf.imgets(image=self.image24,param='CD1_1')#get x plate scale on rotated image #print iraf.imgets.value xplate=abs(float(iraf.imgets.value))#deg/pixel dpix=deltaCutout/3600./xplate/2. # requires a 50 arcsec buffer b/w center of galaxy and edge of image iraf.imgets(image=self.image24,param='naxis1')#get x value corresponding to RA xpixmax=(int(iraf.imgets.value))#deg/pixel iraf.imgets(image=self.image24,param='naxis2')#get x value corresponding to RA ypixmax=(int(iraf.imgets.value))#deg/pixel # write RA and Dec to ascii file t = atpy.Table() t.add_column('RA',self.ndat.RA) t.add_column('DEC',self.ndat.DEC) outfile=self.nsadir+self.prefix+'_RADEC.txt' out1=open(outfile,'w') for i in range(len(self.ndat.RA)): s='%12.8f %12.8f \n'%(self.ndat.RA[i],self.ndat.DEC[i]) out1.write(s) out1.close() # transform RA and Dec to pixels using wcsctran print 'transforming 24um coords' outcoords=self.nsadir+str(self.prefix)+'xy24.txt' if os.path.exists(outcoords): os.remove(outcoords) iraf.imcoords.wcsctran(image=self.image24,input=outfile,output=outcoords,inwcs='world',outwcs='logical',verbose='no') # read in pixel coordinates self.mipscoords=atpy.Table(outcoords,type='ascii') x=self.mipscoords['col1'] y=self.mipscoords['col2'] self.On24ImageFlag= (x > dpix) & (x < (xpixmax-dpix)) & (y > dpix) & (y < (ypixmax-dpix)) print self.prefix,': # on 24um image = ',sum(self.On24ImageFlag)
def process_brite_output(): """ Make xym files for the brite stars. """ stars1 = atpy.Table(work_dir + '12.KS2_2010/BRITE.XYMMMM', type='ascii') stars2 = atpy.Table(work_dir + '13.KS2_2005/BRITE.XYM', type='ascii') _out1 = open(phot_dir + 'brite_F125W.xym', 'w') _out2 = open(phot_dir + 'brite_F139M.xym', 'w') _out3 = open(phot_dir + 'brite_F160W.xym', 'w') _out4 = open(phot_dir + 'brite_F814W.xym', 'w') for ii in range(len(stars1)): _out1.write('%8.2f %8.2f %6.2f\n' % (stars1.col1[ii], stars1.col2[ii], stars1.col5[ii])) _out2.write('%8.2f %8.2f %6.2f\n' % (stars1.col1[ii], stars1.col2[ii], stars1.col4[ii])) _out3.write('%8.2f %8.2f %6.2f\n' % (stars1.col1[ii], stars1.col2[ii], stars1.col3[ii])) for ii in range(len(stars2)): _out4.write('%8.2f %8.2f %6.2f\n' % (stars2.col1[ii], stars2.col2[ii], stars2.col3[ii])) _out1.close() _out2.close() _out3.close() _out4.close()
def __init__(self): # infile=homedir+'research/NSA/nsa_v0_1_2.fits' infile = homedir + 'research/GalaxyZooTables/GalaxyZoo1_DR_table2.fits' self.zdat = atpy.Table(infile, type='fits') infile = homedir + 'research/GalaxyZooTables/GalaxyZoo1_DR_table3.fits' self.zphotdat = atpy.Table(infile, type='fits') self.zRA = zeros(len(self.zdat.RA), 'd') self.zDEC = zeros(len(self.zdat.RA), 'd') for i in range(len(self.zdat.RA)): r = self.zdat.RA[i].split(':') self.zRA[i] = (float(r[0]) + float(r[1]) / 60. + float(r[2]) / 3600.) * 15 d = self.zdat.DEC[i].split(':') self.zDEC[i] = (float(d[0]) + float(d[1]) / 60. + float(d[2]) / 3600.) self.zphotRA = zeros(len(self.zphotdat.RA), 'd') self.zphotDEC = zeros(len(self.zphotdat.RA), 'd') for i in range(len(self.zphotdat.RA)): r = self.zphotdat.RA[i].split(':') self.zphotRA[i] = (float(r[0]) + float(r[1]) / 60. + float(r[2]) / 3600.) * 15 d = self.zphotdat.DEC[i].split(':') self.zphotDEC[i] = (float(d[0]) + float(d[1]) / 60. + float(d[2]) / 3600.) self.zoodict = dict( (a, b) for a, b in zip(self.zdat.OBJID, arange(len(self.zdat.OBJID)))) self.zoophotdict = dict((a, b) for a, b in zip( self.zphotdat.OBJID, arange(len(self.zphotdat.OBJID))))
def __init__(self): # infile=homedir+'research/NSA/nsa_v0_1_2.fits' infile=homedir+'research/GalaxyZoo/GalaxyZoo1_DR_table2_LCSregion.fits' self.zdat=atpy.Table(infile,type='fits') infile=homedir+'research/GalaxyZoo/GalaxyZoo1_DR_table3_LCSregion.fits' self.zphotdat=atpy.Table(infile,type='fits') self.zoodict=dict((a,b) for a,b in zip(self.zdat.OBJID,arange(len(self.zdat.OBJID)))) self.zoophotdict=dict((a,b) for a,b in zip(self.zphotdat.OBJID,arange(len(self.zphotdat.OBJID)))) self.convert_angles()
def reformat_jlu(): """ Make sure to run this first on all the *_badcoo.lis files: python ~/code/python/jlu_python/jlu/photometry/calibrate.py -N ~/data/gsaoi/commission/reduce/ngc1851/ngc1851_photo_calib.dat -I psf032 -c 13 -M 4 -V S20121230S0060_3_jlu_badcoo.lis """ files = [58, 59, 60] year = 2012.95 for ff in files: file_root_1 = 'S20121230S00{0}_1_jlu'.format(ff) file_root_2 = 'S20121230S00{0}_2_jlu'.format(ff) file_root_3 = 'S20121230S00{0}_3_jlu'.format(ff) file_root_4 = 'S20121230S00{0}_4_jlu'.format(ff) tab_1 = atpy.Table(file_root_1 + '_badcoo_cal.lis', type='ascii') tab_2 = atpy.Table(file_root_2 + '_badcoo_cal.lis', type='ascii') tab_3 = atpy.Table(file_root_3 + '_badcoo_cal.lis', type='ascii') tab_4 = atpy.Table(file_root_4 + '_badcoo_cal.lis', type='ascii') # Fix X positions of G1 and G4 tab_1['col4'] += 2048 + 170.0 tab_4['col4'] += 2048 + 170.0 # Fix Y positions of G3 and G4 tab_3['col5'] += 2048 + 170.0 tab_4['col5'] += 2048 + 170.0 _f1 = open(file_root_1 + '.lis', 'w') _f2 = open(file_root_2 + '.lis', 'w') _f3 = open(file_root_3 + '.lis', 'w') _f4 = open(file_root_4 + '.lis', 'w') fmt = '{0:10s} {1:6.3f} {2:8.3f} {3:10.3f} {4:10.3f} 1 1 1 {5:12.1f}\n' for ii in range(len(tab_1)): _f1.write(fmt.format(tab_1['col1'][ii], tab_1['col2'][ii], tab_1['col3'][ii], tab_1['col4'][ii], tab_1['col5'][ii], tab_1['col9'][ii])) for ii in range(len(tab_2)): _f2.write(fmt.format(tab_2['col1'][ii], tab_2['col2'][ii], tab_2['col3'][ii], tab_2['col4'][ii], tab_2['col5'][ii], tab_2['col9'][ii])) for ii in range(len(tab_3)): _f3.write(fmt.format(tab_3['col1'][ii], tab_3['col2'][ii], tab_3['col3'][ii], tab_3['col4'][ii], tab_3['col5'][ii], tab_3['col9'][ii])) for ii in range(len(tab_4)): _f4.write(fmt.format(tab_4['col1'][ii], tab_4['col2'][ii], tab_4['col3'][ii], tab_4['col4'][ii], tab_4['col5'][ii], tab_4['col9'][ii])) _f1.close() _f2.close() _f3.close() _f4.close()
def loadModels(dir=basedir+'TMB03/', file='alpha-models', abundance='', suffix='.dat', \ Fe1='Fe5270', Fe2='Fe5335', Fe3='Fe5406', rawTable=False, verbose=True): """ RH 23/6/16 Load the TMB03 or TMJ10 basic Lick Indices for varying alpha/Fe and Fe/H with age. Abundance can be '', -calcium, -carbon, -nitrogen """ tab = ap.Table(dir+file+abundance+suffix, type='ascii') tab2 = ap.Table(dir+file+abundance+suffix, type='ascii') # 2nd table for removing columns tab2.remove_columns(['age', '[Z/H]','[alpha/Fe]']) # add [MgFe], [MgFe]' tab2.add_column('MgFeP', it.calcMgFePrime(tab2.Mgb, tab2[Fe1], tab2[Fe2])) tab2.add_column('MgFe', it.calcMgFe(tab2.Mgb, tab2[Fe1], tab2[Fe2])) if Fe3 is not None: tab2.add_column('MgFe3', it.calcMgFe(tab2.Mgb, tab2[Fe1], tab2[Fe2], tab2[Fe3])) tab2.add_column('meanFe', it.calcMeanFe(tab2[Fe1], tab2[Fe2])) if Fe3 is not None: tab2.add_column('mean3Fe', it.calcMeanFe(tab2[Fe1], tab2[Fe2], tab2[Fe3])) if verbose: print 'Available rows are: ', tab2.keys() if rawTable: newtab=tab else: Zs = list(set(tab['[Z/H]'])) As = list(set(tab['[alpha/Fe]'])) Zs.sort() As.sort() if verbose: print "Found [Z/H] values of ", Zs print 'Found [alpha/H] values of ', As newtab={} for zi in Zs: minitab={} for ai in As: loc = np.where((tab['[alpha/Fe]']==ai) & (tab['[Z/H]']==zi))[0] minitab['A='+str(ai)]=tab2.rows(loc) newtab['Z='+str(zi)]=minitab if verbose: print 'Length of Z='+str(zi)+', A='+str(ai)+' data is '+str(len(loc)) # add helper keys newtab['Zkeys']=np.array(['Z='+str(x) for x in Zs]) # don't just call tab.keys() here as order gets scrambled compared to Zs, As newtab['Akeys']=np.array(['A='+str(y) for y in As]) newtab['Zs']=np.array(Zs) newtab['As']=np.array(As) newtab['age']=tab.age[loc] return newtab
def readsexcat(self): self.sex = atpy.Table(self.imagepath24 + self.prefix + '-test.cat', type='ascii') print 'writing new table' newtab = atpy.Table() newtab.add_column('x', self.sex['col2']) newtab.add_column('y', self.sex['col3']) outfile = self.prefix + '_sex_xy.txt' if os.path.exists(outfile): os.remove(outfile) newtab.write(outfile, type='ascii')
def rv_sample(obs = None, tspan = 180, npernight = 3, drun = 10, \ nrun = 3, nrand = 10, dnight = 8./24.): if obs != None: # Read in RV data if obs == 'corot7': rv = atpy.Table(corotdefs.ROOTDIR + 'LRa01/cands/corot7_rv.ipac') time = rv.JDB if obs == 'hd189': rv = atpy.Table('/Users/suz/Data/HD189_rv.ipac') time = rv.hjd else: # One point per night days = scipy.arange(tspan) dt_night = dnight / float(npernight+1) # Multiple points per night, with small deviations from regularity obs = scipy.zeros((tspan, npernight)) for i in scipy.arange(npernight): obs[:,i] = days[:] + dt_night * float(i) + \ pylab.normal(0, dt_night/2., tspan) # Select points in "intensive" runs if drun == tspan: take = scipy.ones((tspan, npernight), 'int') else: take = scipy.zeros((tspan, npernight), 'int') for i in scipy.arange(nrun): ok = 0 while ok == 0: tstart = scipy.fix(scipy.rand(1) * float(tspan)) tstart = tstart[0] tend = tstart + drun if tend > tspan: continue if take[tstart:tend,:].any(): continue take[tstart:tend,:] = 1 ok = 1 # Select additional individual points ntot = tspan*npernight obs = scipy.reshape(obs, ntot) take = scipy.reshape(take, ntot) index = scipy.argsort(obs) obs = obs[index] take = take[index] for i in scipy.arange(nrand): ok = 0 while ok == 0: t = scipy.fix(scipy.rand(1) * float(ntot)) t = t[0] if take[t] == 1: continue take[t] = 1 ok = 1 time = obs[(take==1)] time -= time[0] return time
def combine_in_out_ks2(ks2_input_file, matchup_files, suffixes=None): """ Read in a LOGA.INPUT file and the corresponding MATCHUP.XYMEEE files and cross-match them. They should already be the same length and the in same order. Inputs ------ ks2_input_file -- the LOGA.INPUT file matchup_files -- a list of MATCHUP.XYMEEE files Optional Inputs --------------- suffixes -- a list (same length as matchup_files) """ final = atpy.Table() final.table_name = '' # Read in the input ks2 file inp = atpy.Table('LOGA.INPUT', type='ascii') final.add_column('name', inp.col17) final.add_column('x_in', inp.col1) final.add_column('y_in', inp.col2) for ff in range(len(matchup_files)): if suffixes == None: suf = '_{0}'.format(ff + 1) else: suf = '_' + suffixes[ff] f_in = 'col{0}'.format(18 + 4 * ff) m_in = -2.5 * np.log10(inp[f_in]) final.add_column('m_in' + suf, m_in) match = starlists.read_matchup(matchup_files[ff]) if len(match) != len(final): print 'Problem reading ' + matchup_files[ ff] + ': incorrect file length' final.add_column('x_out' + suf, match.x) final.add_column('y_out' + suf, match.y) final.add_column('m_out' + suf, match.m) final.add_column('xe_out' + suf, match.xe) final.add_column('ye_out' + suf, match.ye) final.add_column('me_out' + suf, match.me) final.write('ks2_in_out_catalog.fits', overwrite=True)
def make_field_population(): """ Generate a randomly distributed set of field stars with the specified field density. Also set their brightnesses based on a """ # Load up the field table and use to draw magnitude information. field_sdss = atpy.Table(workDir + 'sdss_field_near_segue1.csv', type='ascii') # Load up field velocity information from Simon+ 2011 study of Segue 1. tmp = atpy.Table(workDir + 'simon_2011_table_3.txt', type='ascii') field_spec = tmp.where((tmp.BProb > -10) & (tmp.BProb < -9)) # This will be our final field ATpy table field = atpy.Table() # Figure out the total number of stars in the FOV num_field_exp = fieldDensity * fov**2 num_field = scipy.stats.poisson.rvs(num_field_exp) # Generate some metallicities metal = scipy.stats.uniform.rvs(size=num_field, loc=-6, scale=7.0) metalErr = np.ones(num_field) * 0.1 # Figure out the brightness information for the stars. # Randomly select from the SDSS field sample. field_idx_sdss = np.random.randint(0, len(field_sdss), size=num_field) field_idx_spec = np.random.randint(0, len(field_spec), size=num_field) vel = scipy.stats.uniform.rvs(size=num_field, loc=-500, scale=1300) velErr = np.ones(num_field) * 10.0 #vel = field_spec.Vel[field_idx_spec] #velErr = field_spec.e_Vel[field_idx_spec] # Figure out the positions of the stars field.add_column('x', scipy.stats.uniform.rvs(size=num_field) * fov) field.add_column('y', scipy.stats.uniform.rvs(size=num_field) * fov) field.add_column('g', field_sdss.g[field_idx_sdss]) field.add_column('r', field_sdss.r[field_idx_sdss]) field.add_column('i', field_sdss.i[field_idx_sdss]) field.add_column('Err_g', field_sdss.Err_g[field_idx_sdss]) field.add_column('Err_r', field_sdss.Err_r[field_idx_sdss]) field.add_column('Err_i', field_sdss.Err_i[field_idx_sdss]) field.add_column('vel', vel) field.add_column('Err_vel', velErr) field.add_column('FeH', metal) field.add_column('Err_FeH', metalErr) field.add_column('member', np.zeros(num_field)) return field
def glue_stuff(splits=10, start=0): ''' Read in the tables from earlier and glue them together ''' if type(splits) is not int: raise TypeError spread = atpy.Table(path2 + 'sp%d.fits' % start) spread_list = [] for i in range(1 + start, splits): other_spread = atpy.Table(path2 + 'sp%d.fits' % i) spread.append(other_spread) return spread
def __init__(self,clustername): self.prefix=clustername self.image24=homedir+'research/LocalClusters/Images/'+self.prefix+'/24um/Full'+self.prefix+'ch1rf_mosaic_minus_median_extract.fits' self.noise24=homedir+'research/LocalClusters/Images/'+self.prefix+'/24um/Full'+self.prefix+'ch1rf_mosaic_unc.fits' if (clustername.find('A1367') > -1): self.image24='/home/rfinn/research/LocalClusters/Images/'+self.prefix+'/24um/'+self.prefix+'ch1r1_mosaic_minus_median_extract.fits' self.noise24='/home/rfinn/research/LocalClusters/Images/'+self.prefix+'/24um/'+self.prefix+'ch1r1_mosaic_unc.fits' elif (clustername.find('Herc')>-1):#for Abell 1367 and Hercules cluster self.image24='/home/rfinn/research/LocalClusters/Images/'+self.prefix+'/24um/'+self.prefix+'ch1r1_mosaic_minus_median_extract.fits' self.noise24='/home/rfinn/research/LocalClusters/Images/'+self.prefix+'/24um/'+self.prefix+'ch1r1_mosaic_unc.fits' # read NSA table for each cluster infile=homedir+'research/NSA/'+self.prefix+'_NSA.Fits' self.ndat=atpy.Table(infile,type='fits') self.nsadir=homedir+'research/NSA/' self.cra=clusterRA[self.prefix] self.cdec=clusterDec[self.prefix] self.cz=clusterz[self.prefix] self.biweightvel=clusterbiweightcenter[self.prefix] self.biweightscale=clusterbiweightscale[self.prefix] self.r200=2.02*(self.biweightscale)/1000./sqrt(OmegaL+OmegaM*(1.+self.cz)**3)*H0/70. # in Mpc self.r200deg=self.r200*1000./my.DA(self.cz,h)/3600. self.cdMpc=self.biweightvel/H0 self.cdcm=self.cdMpc*3.e24 self.csigma=self.biweightscale self.mcl=my.clusterMass(self.csigma,self.cz,h) self.AngDistance=my.DA(self.cz,h)
def make_wise_lookup_table(t): """ Old code to make a table for getting WISE data. Should not be needed. """ import atpy ras = [] decs = [] #bad_hand = [4926] bad_hand = bad_mips for i, source in enumerate(t['ag_id']): num = float(source[2:]) sourcename = t['malt90_map_filename'][i] a_lon = t['ag_long'][i] a_lat = t['ag_lat'][i] cat_row = t[i] #if (a_lat > 1.) or (a_lat < -1) or (num in bad_hand) : if (num in bad_hand): print(num) agal = AgalSource.AgalSource(source, cat_row, None, None, None) ras.append(agal.malt90source.apos_ra) decs.append(agal.malt90source.bpos_dec) tbl = atpy.Table() tbl.add_column('ra', np.array(ras), unit='degrees') tbl.add_column('dec', np.array(decs), unit='degrees') tbl.write("wise-table-for-lookup-more.tbl")
def read(self, path, length, offset): """ Reads the selected file. If the file is from the server it is downloaded and read without being saved locally. If it is a local file, it is read locally. self: object, object(file/directory) that the function was called on. path: string, current working directory path. length: interger?, amount of the file to read. offset: interger?, starting position in file (invalid for reading from server. """ #Create a list of available files T = atpy.Table(SERVER_LOCATION + 'QUERY?query=files_list&format=list',type='ascii') #Check is file is local if not (path[1:] in T['col3']): if not path in self.__openfiles: self.open(path, 0) return self.__openfiles[path].read(length, offset) #File is not local so open and read from server else: urlString = SERVER_LOCATION + 'RETRIEVE?file_id=' + path[1:] ht = None ht = urllib2.urlopen(urlString) return ht.read(length)
def plot_wd1_pm(): """ Plot the proper motion vectors for Wd 1. """ dataFile = '/u/jlu/data/Wd1/hst/from_jay/EXPORT_WEST1.2012.02.04/wd1_catalog.fits' d = atpy.Table(dataFile) d = d.where((d.x2005_e < 0.05) & (d.y2005_e < 0.05) & (d.x2010_e < 0.05) & (d.y2010_e < 0.05) & (d.mag125_e < 0.03)) f_pmX = 1.8 # mas/yr f_pmY = 1.3 # mas/yr # f_pmX = 0. # mas/yr # f_pmY = 0. # mas/yr pmX = (d.dx / 5.0) - f_pmX pmY = (d.dy / 5.0) - f_pmY py.clf() py.subplots_adjust(left=0.02, right=0.98, bottom=0.02, top=0.98) Q = py.quiver(d.x2005 * 0.06, d.y2005 * 0.06, pmX, pmY, scale=100) py.setp(py.gca().get_xticklabels(), visible=False) py.setp(py.gca().get_yticklabels(), visible=False) py.xlim(30, 200) py.ylim(30, 200) # py.quiverkey(Q, 0.5, 0.94, 10**5, '30 km/s', coordinates='figure', # color='red', labelpos='E') py.savefig('wd1_pm.png') return
def search_for_fits(rad, dec): """ Get Metadata for images at a specific coordinate, For more infomation on bulk data searches, see: Parameters ---------- rad : float Right ascension (0 - 360) dec : float Declination (0 - 360) Returns ------- image_metadata : table Dictionary containing the astropy fits files for every band. """ search = 'https://irsa.ipac.caltech.edu/ibe/search/wise/neowiser/p1bm_frm?POS=' \ + str(rad) + ',' + str(dec) html = requests.get(search) html.raise_for_status() with tempfile.NamedTemporaryFile(suffix='.tbl') as tbl: tbl.write(html.content) image_metadata = atpy.Table(tbl.name) return image_metadata
def fiteach(self, **kwargs): """ Fit each spectrum within the Spectra object """ for sp in self.speclist: sp.specfit(**kwargs) if atpyOK: self.fittable = atpy.Table() self.fittable.add_column('name', [sp.specname for sp in self.speclist]) self.fittable.add_column( 'amplitude', [sp.specfit.modelpars[0] for sp in self.speclist], unit=self.unit) self.fittable.add_column( 'center', [sp.specfit.modelpars[1] for sp in self.speclist], unit=self.xarr.unit) self.fittable.add_column( 'width', [sp.specfit.modelpars[2] for sp in self.speclist], unit=self.xarr.unit) self.fittable.add_column( 'amplitudeerr', [sp.specfit.modelerrs[0] for sp in self.speclist], unit=self.unit) self.fittable.add_column( 'centererr', [sp.specfit.modelerrs[1] for sp in self.speclist], unit=self.xarr.unit) self.fittable.add_column( 'widtherr', [sp.specfit.modelerrs[2] for sp in self.speclist], unit=self.xarr.unit)
def examine_sdss_field(): # This is a 10' radius circle from SDSS at coordinates of: # RA: 10:00:00 # DEC: 17:00:00 # This is near Segue 1; but we will use it for our entire field sample tab = atpy.Table(workDir + 'sdss_field_near_segue1.csv', type='ascii') # Examine spatial density py.clf() py.plot(tab.ra, tab.dec, 'k.') py.xlabel('RA') py.ylabel('Dec.') py.title('Field Stars at 10:00:00 +17:00:00') py.savefig(workDir + 'field_density.png') # Examine color-magnitude diagram py.clf() py.plot(tab.g - tab.i, tab.r, 'k.', ms=3) # filters used by Simon+ Segue 1 paper py.xlabel('SDSS g - i') py.ylabel('SDSS r') py.xlim(-2, 4) py.ylim(24, 12) py.title('Field Stars at 10:00:00 +17:00:00') py.savefig(workDir + 'field_cmd.png') idx = np.where(tab.r < 23)[0] # Mean spatial density volume = 4.0 * math.pi * 10.0**2 surfaceDensity = len(idx) / volume print 'Field surface density for r<23 is: %.2f stars / arcmin^2' % surfaceDensity
def __init__(self, filename): self.catalog = filename # self.table = atpy.Table(filename,type='fits') self.table = atpy.Table(filename) self.number = len(self.table) self.name = 'PS1 source list' # Don't compute ellipticity parameters until asked to do so: self.done_ellipticities = False # Always want bands, as filter strings are long and annoying... # Might as well assign plot colors as we go! self.table.add_empty_column('band', 'a1') self.table.add_empty_column('plotcolor', 'a25') # Strip filter name into band: self.table.band = [f[0] for f in self.table.filterid] # Loop over filterids - must be better way to do this... for i in range(len(bands)): index = numpy.where(self.table['band'] == bands[i]) if len(index) > 0: self.table.plotcolor[index] = plotcolors[i] # print i, plotcolors[i], index, self.table.plotcolor[index] return None
def gamaTest(): import atpy print('requires InputCatA.fits; download from GAMA DR2 webpages.') gama = atpy.Table('/Users/ent/data/gama/dr2/InputCatA.fits') ebv0 = gama.EXTINCTION_R / 2.751 plt.figure(5) plt.clf() plt.xlabel('EBV from GAMA catalogues') plt.ylabel('EBV from this code') plt.title('red = Schlegel+98 dust map; black = Planck dust map') for i, (ra, dec) in enumerate(zip(gama.RA, gama.DEC)): theta, phi = thetaPhiFromRaDec(ra, dec) dust = Planck_EBV(theta, phi) dust2 = Schlegel_EBV(theta, phi) plt.scatter(gama.EXTINCTION_R[i] / 3.1, dust, 1, 'k', edgecolors='none') plt.scatter(gama.EXTINCTION_R[i] / 3.1, dust2, 1, 'r', edgecolors='none') if i % 1000 == 999: plt.draw()
def ngc1851_image(): """ Plot an image of NGC 1851 for the paper. """ image_root = '/u/jlu/data/gsaoi/commission/reduce/ngc1851/combo/ngc1851' # Load up the PSF stars file to get the coordinates. stars = atpy.Table(image_root + '_psf_stars_pixel.txt', type='ascii') stars.Xarc = stars.X * scale stars.Yarc = stars.Y * scale scale = 0.00995 # gc = aplpy.FITSFigure(image_file) # gc.show_grayscale() img = pyfits.getdata(image_root + '.fits') img = img_scale.log(img, scale_min=0, scale_max=1e4) #img = img_scale.sqrt(img, scale_min=500, scale_max=5e4) # img = img_scale.linear(img, scale_min=500, scale_max=4e4) xmin = ((0 - cooPix[0]) * scale * -1.0) + cooAsec[0] xmax = ((img.shape[1] - cooPix[0]) * scale * -1.0) + cooAsec[0] ymin = ((0 - cooPix[1]) * scale) + cooAsec[1] ymax = ((img.shape[0] - cooPix[1]) * scale) + cooAsec[1] extent = [xmin, xmax, ymin, ymax] py.clf() py.imshow(img, extent=extent, cmap=py.cm.gray_r)
def calc_profiles(self): """Calculate latitude and longitude profiles""" logging.info('---> Calculating profiles') for axis in ['GLON', 'GLAT']: if axis == 'GLON': pos = self.glon axis_index = 0 else: pos = self.glat axis_index = 1 table = atpy.Table() table.add_column(axis, pos) for ii in range(len(components)): in_file = filename(self.tag, ii, 'image') sb = fits.getdata(in_file) # Compute average surface brightness flux = self.mask * self.area * sb area = self.mask * self.area flux_sum = flux.sum(axis_index) area_sum = area.sum(axis_index) mean_sb = flux_sum / area_sum table.add_column(components[ii], mean_sb) out_file = '{0}_profiles.fits'.format(axis) logging.info('Writing {0}'.format(out_file)) table.write(out_file, overwrite=self.clobber)
def examine_sdss_cluster(): # This is a 5' radius circle from SDSS at Segue 1 coordinates. tab = atpy.Table(workDir + 'sdss_segue1.csv', type='ascii') # Examine spatial density py.clf() py.plot(tab.ra, tab.dec, 'k.') py.xlabel('RA') py.ylabel('Dec.') py.title('Segue 1') py.savefig(workDir + 'segue1_density.png') # Examine color-magnitude diagram py.clf() py.plot(tab.g - tab.i, tab.r, 'k.', ms=3) # filters used by Simon+ Segue 1 paper py.xlabel('SDSS g - i') py.ylabel('SDSS r') py.xlim(-2, 4) py.ylim(24, 12) py.title('Segue 1') py.savefig(workDir + 'segue1_cmd.png') idx = np.where(tab.r < 23)[0] # Mean spatial density volume = 4.0 * math.pi * 10.0**2 surfaceDensity = len(idx) / volume print 'Segue 1 surface density for r<23 is: %.2f stars / arcmin^2' % surfaceDensity
def bgps_to_reg(filename, outfile): bolocat = atpy.Table(filename) outfile = open(outfile, 'w') print >> outfile, "galactic" for ii in xrange(len(bolocat)): flux = float(bolocat.flux_40[ii]) if flux < 0.2: color = 'darkblue' elif flux < 0.5: color = 'blue' elif flux < 1.0: color = 'cyan' elif flux < 2.0: color = 'turquoise' elif flux < 3.0: color = 'green' elif flux < 5.0: color = 'yellow' elif flux < 10.0: color = 'orange' elif flux < 20.0: color = 'red' elif flux < 50.0: color = 'maroon' else: color = 'purple' try: major = bolocat.maj[ii] minor = bolocat.min[ii] posang = bolocat.pos_ang[ii] + 90. except AttributeError: major = bolocat.mommaj_as[ii] minor = bolocat.mommin_as[ii] posang = bolocat.posang[ii] + 90. print >> outfile, "ellipse(%s,%s,%s\",%s\",%s) # text={%s} width=%i color=%s" % ( bolocat.glon[ii], bolocat.glat[ii], major, minor, posang, bolocat.name[ii], np.round(np.log(flux + 1) + 1), color) outfile.close()
def calc_spec_index(self): """Calculate spectral indices""" # Note that we can't compute a forward difference # of the rightmost flux value, so the table is one shorter. logging.info('---> Calculating spectral indices') in_table = atpy.Table('spec.fits') energy = in_table.data['energy'] out_table = atpy.Table(name='Spectral Index') out_table.add_column('energy', energy[:-1]) for ii in range(len(components)): flux = in_table.data[components[ii]] spec_index = g_from_points(energy[:-1], energy[1:], flux[:-1], flux[1:]) spec_index += 2 # Because we stored E**2 dN / dE out_table.add_column(components[ii], spec_index) out_table.write('spec_index.fits', overwrite=self.clobber)
def combine_matched_lists(filt, brite=False): finalTab = None for ii in range(len(images[filt])): inFile = images[filt][ii] + '_' + filt + '_a' if brite: inFile += '_brite' inFile += '.txt' starsTab = atpy.Table(inFile, type='ascii') img_id = np.ones(len(starsTab)) + ii + 1 starsTab.add_column('img_id', img_id) # concatenate all the results if finalTab == None: finalTab = starsTab else: finalTab.append(starsTab) outFile = 'matched_ks2_apphot_' + filt if brite: outFile += '_brite' outFile += '.fits' finalTab.rename_column('col1', 'x_ks2') finalTab.rename_column('col2', 'y_ks2') finalTab.rename_column('col3', 'x_ap') finalTab.rename_column('col4', 'y_ap') finalTab.rename_column('col5', 'm_ks2') finalTab.rename_column('col6', 'm_ap') finalTab.table_name = '' finalTab.write(outFile, type='fits', overwrite=True) return
def xym2bar_singles(): """ Loop through and run xym2bar on a single star list. This will transform the entire star list into the coordinate system of the average star list. Also run with the "I" flag so that only stars with matches in the average list and printed out... this allows us to cross-match. This needs to be run in the test_overlap directory. """ images = range(301, 328 + 1) # images = range(301, 302) master = '../MASTER.F160W.ref5' old_in = atpy.Table(mat_dir + '/IN.xym2mat', type='ascii') for ii in images: mat_file = '{0}/MAT.{1}'.format(mat_dir, ii + 1) xym_file = old_in.col2[old_in.col1 == ii][0] in_file = test_dir + '/IN.xym2bar' _in = open(in_file, 'w') _in.write('000 "{0}"\n'.format(master)) _in.write('{0} "../{1}" c9\n'.format(ii, xym_file)) _in.close() os.system('xym2bar 1 I') shutil.copyfile(in_file, '{0}.{1}'.format(in_file, ii)) shutil.copyfile('MATCHUP.XYMEEE', 'MATCHUP.XYMEEE.{0}'.format(ii))
def process_ks2_output(): """ Pre-process the ks2 output to make a *.XYM file for all three filters. """ stars = atpy.Table(work_dir + '20.KS2_PMA/wd1_catalog.fits') _out1 = open(phot_dir + 'ks2_F125W.xym', 'w') _out2 = open(phot_dir + 'ks2_F139M.xym', 'w') _out3 = open(phot_dir + 'ks2_F160W.xym', 'w') _out4 = open(phot_dir + 'ks2_F814W.xym', 'w') # KS2 for F814W output is in Electrons (not e-/sec) per exposure. # Lets convert this to e-/sec instrumental magnitudes to match drizzle. tint_814 = 2407.0 nexp_814 = 3.0 # stars.m_814 -= -2.5 * math.log10(tint_814 / nexp_814) for ii in range(len(stars)): if (stars.me_125[ii] > 0) and (stars.me_125[ii] < 8): _out1.write('%8.2f %8.2f %6.2f\n' % (stars.x_125[ii], stars.y_125[ii], stars.m_125[ii])) if (stars.me_139[ii] > 0) and (stars.me_139[ii] < 8): _out2.write('%8.2f %8.2f %6.2f\n' % (stars.x_139[ii], stars.y_139[ii], stars.m_139[ii])) if (stars.me_160[ii] > 0) and (stars.me_160[ii] < 8): _out3.write('%8.2f %8.2f %6.2f\n' % (stars.x_160[ii], stars.y_160[ii], stars.m_160[ii])) if (stars.me_814[ii] > 0) and (stars.me_814[ii] < 8): _out4.write('%8.2f %8.2f %6.2f\n' % (stars.x_814[ii], stars.y_814[ii], stars.m_814[ii])) _out1.close() _out2.close() _out3.close() _out4.close()
def check_vpd_ks2_astrometry(): """ Check the VPD and quiver plots for our KS2-extracted, re-transformed astrometry. """ catFile = workDir + '20.KS2_PMA/wd1_catalog.fits' tab = atpy.Table(catFile) good = (tab.xe_160 < 0.05) & (tab.ye_160 < 0.05) & \ (tab.xe_814 < 0.05) & (tab.ye_814 < 0.05) & \ (tab.me_814 < 0.05) & (tab.me_160 < 0.05) tab2 = tab.where(good) dx = (tab2.x_160 - tab2.x_814) * ast.scale['WFC'] * 1e3 dy = (tab2.y_160 - tab2.y_814) * ast.scale['WFC'] * 1e3 py.clf() q = py.quiver(tab2.x_814, tab2.y_814, dx, dy, scale=5e2) py.quiverkey(q, 0.95, 0.85, 5, '5 mas', color='red', labelcolor='red') py.savefig(workDir + '20.KS2_PMA/vec_diffs_ks2_all.png') py.clf() py.plot(dy, dx, 'k.', ms=2) lim = 30 py.axis([-lim, lim, -lim, lim]) py.xlabel('Y Proper Motion (mas)') py.ylabel('X Proper Motion (mas)') py.savefig(workDir + '20.KS2_PMA/vpd_ks2_all.png') idx = np.where((np.abs(dx) < 10) & (np.abs(dy) < 10))[0] print('Cluster Members (within dx < 10 mas and dy < 10 mas)') print((' dx = {dx:6.2f} +/- {dxe:6.2f} mas'.format(dx=dx[idx].mean(), dxe=dx[idx].std()))) print((' dy = {dy:6.2f} +/- {dye:6.2f} mas'.format(dy=dy[idx].mean(), dye=dy[idx].std())))
def plot_test_amplitude(out_file_root): outroot = '/u/jlu/work/stats/test_bernoulli/' + out_file_root #+ '_' tab = atpy.Table(outroot + '.txt', type='ascii') tab['col2'] /= -2.0 tab.rename_column('col1', 'weights') tab.rename_column('col2', 'logLike') tab.rename_column('col3', 'alpha') tab.rename_column('col4', 'N') tab.rename_column('col5', 'logL_d') py.figure(1) py.clf() bins_alpha = np.arange(1.0, 5.0, 0.01) (n, b, p) = py.hist(tab.alpha, weights=tab.weights, histtype='step', bins=bins_alpha) py.ylim(0, 1.1 * n.max()) idx = np.where(n > 0)[0] py.xlim(b[idx[0] - 1], b[idx[-1] + 1]) py.axvline(3.0) py.xlabel('alpha') py.savefig(outroot + 'posterior_alpha.png') py.figure(2) py.clf() bins_N = np.arange(1000, 50000, 100) (n, b, p) = py.hist(tab.N, weights=tab.weights, histtype='step', bins=bins_N) py.ylim(0, 1.1 * n.max()) idx = np.where(n > 0)[0] py.xlim(b[idx[0] - 1], b[idx[-1] + 1]) py.axvline(5000) py.ylabel('N') py.savefig(outroot + 'posterior_N.png') H, xedges, yedges = np.histogram2d(tab.alpha, tab.N, weights=tab.weights, bins=[bins_alpha, bins_N]) py.figure(3) py.clf() extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]] py.imshow(H, extent=extent, interpolation='nearest') py.colorbar() py.axis('tight') idx = np.where(H > 0) print bins_N[idx[1][0]], bins_N[idx[1][-1]] print bins_alpha[idx[0][0]], bins_alpha[idx[0][-1]] py.xlim(bins_N[idx[1][0]], bins_N[idx[1][-1]]) py.ylim(bins_alpha[idx[0][0]], bins_alpha[idx[0][-1]]) py.xlabel('N') py.ylabel('alpha') py.savefig(outroot + 'posterior_2d.png')