def v_hist(): """""" t = Table.read("../data/deimos.fits") print(t.keys()) N = len(t) ra = np.zeros(N)*u.deg dec = np.zeros(N)*u.deg for j in range(N): ra[j] = Angle(t['RA'][j] + ' hours') dec[j] = Angle(t['DEC'][j] + 'degrees') # fname = '../data/TriS-1.out' ra_s, dec_s, i, priority, inmask = np.loadtxt(fname, usecols=(1,2,4,6,8), unpack=True, dtype='|S11, |S11, <f4, <f4, <i4', skiprows=4) Nm = np.size(ra_s) ra_m = np.zeros(Nm)*u.deg dec_m = np.zeros(Nm)*u.deg for j in range(Nm): ra_m[j] = Angle(ra_s[j] + ' hours') dec_m[j] = Angle(dec_s[j] + 'degrees') # match catalogs ref = ICRS(ra_m, dec_m, unit=(u.degree, u.degree)) cat = ICRS(ra, dec, unit=(u.degree, u.degree)) id1, d21, d31 = cat.match_to_catalog_sky(ref) matches = d21<0.5*u.arcsec t_priority = priority[id1] i_m = i[id1] indices = (t['ZQUALITY']>-1) t['Z'] *= 300000 add_npcolumn(t, name="priority", vec=t_priority) #print(t_priority, np.size(t_priority)) plt.close() plt.figure() plt.subplot(221) plt.hist(t['Z'], bins=np.arange(-400,200,10), histtype='step', color='k') plt.hist(t['Z'][indices], bins=np.arange(-400,200,10), histtype='step', color='b') plt.hist(t['Z'][t_priority>=500], bins=np.arange(-400,200,10), histtype='step', color='r') plt.minorticks_on() plt.subplot(222) plt.plot(i_m[indices], t['Z'][indices], 'ko') print(t['Z'][t_priority>=500], i_m[t_priority>=500]) print(np.max(i_m[indices]))
def sdss_IAU_id_to_ra_dec(sdssids, matchtocatalog=None): """ Converts IAU SDSS identifiers (e.g.,"J151503.37+421253.9") to their RA/Decs Returns an ICRS object if `matchtocatalog` is None, otherwise `catalogidx, skysep` """ import re from astropy.coordinates import ICRS, Latitude, Longitude rex = re.compile(r'J(\d{2})(\d{2})(\d{2}(?:\.\d{1,2})?)' r'([+-])(\d{2})(\d{2})(\d{2}(?:\.\d)?)') if isinstance(sdssids, six.string_types): sdssids = [sdssids] ras = [] decs = [] for idi in sdssids: res = rex.match(idi) if res is None: raise ValueError('Could not match ' + idi) res = res.groups() rah, ram, rasc = res[:3] desgn, ded, dem, des = res[-4:] ras.append(':'.join((rah, ram, rasc))) decs.append(desgn + ':'.join((ded, dem, des))) coords = ICRS(ra=Longitude(ras, u.hourangle), dec=Latitude(decs, u.degree)) if matchtocatalog: if not hasattr(matchtocatalog, 'match_to_catalog_sky'): matchtocatalog = ICRS(ra=np.array(matchtocatalog['ra'])*u.deg, dec=np.array(matchtocatalog['dec'])*u.deg) idx, sep, sep3d = coords.match_to_catalog_sky(matchtocatalog) return idx, sep.to(u.arcsec) else: return coords
def sdss_IAU_id_to_ra_dec(sdssids, matchtocatalog=None): """ Converts IAU SDSS identifiers (e.g.,"J151503.37+421253.9") to their RA/Decs Returns an ICRS object if `matchtocatalog` is None, otherwise `catalogidx, skysep` """ import re from astropy.coordinates import ICRS, Latitude, Longitude rex = re.compile(r'J(\d{2})(\d{2})(\d{2}(?:\.\d{1,2})?)' r'([+-])(\d{2})(\d{2})(\d{2}(?:\.\d)?)') if isinstance(sdssids, six.string_types): sdssids = [sdssids] ras = [] decs = [] for idi in sdssids: res = rex.match(idi) if res is None: raise ValueError('Could not match ' + idi) res = res.groups() rah, ram, rasc = res[:3] desgn, ded, dem, des = res[-4:] ras.append(':'.join((rah, ram, rasc))) decs.append(desgn + ':'.join((ded, dem, des))) coords = ICRS(ra=Longitude(ras, u.hourangle), dec=Latitude(decs, u.degree)) if matchtocatalog: if not hasattr(matchtocatalog, 'match_to_catalog_sky'): matchtocatalog = ICRS(ra=np.array(matchtocatalog['ra']) * u.deg, dec=np.array(matchtocatalog['dec']) * u.deg) idx, sep, sep3d = coords.match_to_catalog_sky(matchtocatalog) return idx, sep.to(u.arcsec) else: return coords
def match_objects(folder_labels): print("----------------------------------------") print("Running Module 2: Match Objects") # stores all "_" filter txt files (eventually will group by similar exposure times in event there are more than one J,Ks files j_files = np.hstack(np.array(glob.glob('Data/{}/*-J.txt'.format(folder))) for folder in folder_labels) k_files = np.hstack(np.array(glob.glob('Data/{}/*-Ks.txt'.format(folder))) for folder in folder_labels) print j_files print k_files j_filter_data = np.empty([0,5]) ra1 = np.empty([0,1]) ra2 = np.empty([0,1]) k_filter_data = np.empty([0,5]) dec1 = np.empty([0,1]) dec2 = np.empty([0,1]) print '\nPlease wait while all data is loaded from files...' # appends data from all J filter files in folder if multiple for j in j_files: # generate numpy array from txt files new_data1 = np.genfromtxt(j, unpack = False) j_filter_data = np.vstack([j_filter_data, new_data1]) print 'Data from J filter file', j, 'loaded...' print j_filter_data.shape ra1 = j_filter_data[:,3] dec1 = j_filter_data[:,4] print 'J Filter shape:', ra1.shape, dec1.shape # appends data from all Ks filter files in folder if multiple for k in k_files: new_data2 = np.genfromtxt(k, unpack = False) k_filter_data = np.vstack([k_filter_data, new_data2]) print 'Data from Ks filter file', k, 'loaded...' print k_filter_data.shape ra2 = k_filter_data[:,3] dec2 = k_filter_data[:,4] print 'Ks Filter shape:', ra2.shape, dec2.shape print 'Data from all files loaded.' print '\nPlease wait while objects are matched across the J and Ks Filter...' # j filter ra/dec info to be compared # in units of degrees obj_1 = ICRS(ra1, dec1, unit=(u.degree, u.degree)) # k filter ra/dec info to be compared # in units of degrees obj_2 = ICRS(ra2, dec2, unit=(u.degree, u.degree)) # This function will match all objects from obj_1 (j-filter) with an object in obj_2 (ks-filter) # By its nature, match_to_catalog_sky compares object data against catalog of sky, which would yield a closest match for every object # In our application, we compare two different sets of object data to each other, which should not guarantee a match # This means that we will get as many matches as there are objects in the smaller object data set (in this case, the j-filter will always have less objects) # This could be problematic since a "closest" match will be made relative to the available objects in obj_2 (ks-filter) # So a match does not necessarily constitute a match for us # To remedy this, I have run the matched objects through another test to compare the sum of differences against a set tolerance # This will ensure that all matches are within a controlled and tolerable closeness idx, d2d, d3d = obj_1.match_to_catalog_sky(obj_2) # match j band to k band since there are more objects in k # gets data matches from k-filter data based on matched indexes returned by match_to_catalog_sky matches = k_filter_data[idx,:] # appends all data columns for matched objects in a [:,10] array # j-filter # 0 -> Classification # 1 -> Aper_flux_3 # 2 -> Aper_flux_3_err # 3 -> RA # 4 -> DEC # k-filter # 5 -> Classification # 6 -> Aper_flux_3 # 7 -> Aper_flux_3_err # 8 -> RA # 9 -> DEC filter_matches = np.column_stack([j_filter_data, matches]) # CHANGE THE TOLERANCE TO SEE WHAT WORKS BEST tolerance = 0.0001 # compares sum of differences between RA/DEC values to set tolerance to ensure quality matches # see long comment above to see reasoning within_tol_index = ((abs(filter_matches[:,3]-filter_matches[:,8]) < tolerance) & (abs(filter_matches[:,4]-filter_matches[:,9]) < tolerance)).nonzero() stellar_obj_matches = (np.squeeze((filter_matches[within_tol_index,:]), axis=0)) print '\nNumber of matches found', stellar_obj_matches.shape np.savetxt('Resources/J_Ks-matches.txt', stellar_obj_matches, fmt=['%.1f', '%.10f', '%.10f', '%.10f', '%.10f','%.1f', '%.10f', '%.10f', '%.10f', '%.10f'], delimiter=' ')
def match_Stars(fits_file, save_location='./', nobsfile='obsout', standard_stars_file='standard_stars.dat', out_nobsfile='final_obsout', header_line=5, SN_coord=None, dist_treshold = 0.0004, sel_3D=True ): '''- fits_file is the full path of the reference fits file. - save_location is the directory in which the output will be saved. - nobsfile is the full path of the nobsfile. - standard_stars_file is the full path of the standard stars file. - phot_band is the photometric reference band. - index_band is the indec of the band in the standard_stars_file. - offset is an integer which changes the nobsfile line that is used to name the star. e.g, if g is the reference but the band order in nobsfile is u then g the offset should be'1' - header_line is the line number of the header of the standard_stars_file - dist_treshold the match is discarded if the stars are farther apart than this value (degrees) The output is four images showing the matched stars plus 'final_obsout', which is the nobsfile with the star names in place.''' import pyfits hdulist = pyfits.open(fits_file) prihdr = hdulist[0].header phot_band = prihdr['filter1'] index_band = phot_band.lower() print phot_band if SN_coord==None: SN_c = ICRS(prihdr['RA']+prihdr['DEC'], unit = (u.hourangle, u.degree)) SN_coord = [SN_c.ra.degree, SN_c.dec.degree] obsout_file = nobsfile gri_file = standard_stars_file if os.path.isfile(fits_file) == True: w = WCS(fits_file) ## obsout = genfromtxt(obsout_file,dtype=None, missing_values='INDEF') gri = genfromtxt(gri_file,dtype=None, missing_values='INDEF', skip_header=header_line-1,names=True) ## def marker_size_(in_mag): return 20*(nanmax(in_mag)-in_mag)/(nanmax(in_mag)-nanmin(in_mag)) def select_phot_(in_v,phot_out,phot_list): out_v = copy(in_v) for i in range(size(out_v)): if phot_list[i]!=phot_out: out_v[i]=nan return out_v ## figure(figsize=(8,8)) # all_pix2world is from http://docs.astropy.org/en/stable/wcs/ lon, lat =w.all_pix2world(obsout['f4'], obsout['f5'], 0) lon = select_phot_( lon , phot_out=phot_band, phot_list=obsout['f1']) lat = select_phot_( lat , phot_out=phot_band, phot_list=obsout['f1']) scatter(lon,lat,marker='o',s=marker_size_(obsout['f6']) ) scatter(gri['RA'],gri['dec'],color='r',marker='o',s=marker_size_(gri[index_band]) ) plot(SN_coord[0],SN_coord[1],'+g') gca().invert_yaxis() savefig(save_location+'out1.pdf') # # match_to_catalog_sky is from https://www.sites.google.com/site/mrpaulhancock/blog/theage-oldproblemofcross-matchingastronomicalsources indx1 = [i for i in range(size(lon)) if not isnan(lon*lat)[i]] cat1_ra = lon[indx1] cat1_dec = lat[indx1] cat2_ra = gri['RA'] cat2_dec = gri['dec'] cat3_ra = [SN_coord[0]] cat3_dec = [SN_coord[1]] cat1 = ICRS(cat1_ra,cat1_dec,unit = (u.degree,u.degree)) cat2 = ICRS(cat2_ra,cat2_dec,unit = (u.degree,u.degree)) cat3 = ICRS(cat3_ra,cat3_dec,unit = (u.degree,u.degree)) index_list,dist2d,dist3d = cat2.match_to_catalog_sky(cat1) index_list_SN,dist2d_3,dist3d_3 = cat3.match_to_catalog_sky(cat1) indexes_gri = [] indexes_obsout = [] distances=[] for i in range(size(gri)): j = indx1[index_list[i]] cos_dec = cos(lat[j]*pi/(180.)) dist_ = ( sqrt(sum(array([(lon[j] - gri['RA'][i])*cos_dec , lat[j] - gri['dec'][i]])**2)) ) if dist_ < dist_treshold: indexes_gri.append(i) indexes_obsout.append(j) distances.append(dist_) if sel_3D: valuesA_=[] valuesB_=[] for i in indexes_gri: j = indx1[index_list[i]] valuesA_.append(obsout['f6'][j]) valuesB_.append(gri[index_band][i]) valuesA_=array(valuesA_);valuesB_=array(valuesB_) sigma_res = median(abs(valuesA_- valuesB_ - median(valuesA_ - valuesB_))) res_ = abs(valuesA_- valuesB_ - median(valuesA_ - valuesB_)) indexes_gri_new = [] indexes_obsout = [] distances=[] ii=0 for i in indexes_gri: j = indx1[index_list[i]] cos_dec = cos(lat[j]*pi/(180.)) dist_ = ( sqrt(sum(array([(lon[j] - gri['RA'][i])*cos_dec , lat[j] - gri['dec'][i]])**2)) ) if dist_ < dist_treshold: if not sel_3D or res_[ii] < 4.*sigma_res: indexes_gri_new.append(i) indexes_obsout.append(j) distances.append(dist_) ii+=1 indexes_gri = indexes_gri_new indexes_gri_new = [] indexes_obsout = [] distances=[] ii=0 for i in indexes_gri: j = indx1[index_list[i]] cos_dec = cos(lat[j]*pi/(180.)) dist_ = ( sqrt(sum(array([(lon[j] - gri['RA'][i])*cos_dec , lat[j] - gri['dec'][i]])**2)) ) if dist_ < dist_treshold: if not sel_3D or res_[ii] < 3.*sigma_res: indexes_gri_new.append(i) indexes_obsout.append(j) distances.append(dist_) ii+=1 indexes_gri = indexes_gri_new indexes_obsout_SN = nan j = indx1[index_list_SN[0]] cos_dec = cos(lat[j]*pi/(180.)) dist_ = ( sqrt(sum(array([(lon[j] - SN_coord[0])*cos_dec , lat[j] - SN_coord[1]])**2)) ) if dist_ < dist_treshold: #indexes_gri.append(i) indexes_obsout_SN = j figure() hist(distances,1000,label='catalog stars') plot([dist_ ,dist_],[0,1],'g', linewidth=10, label='SN') plot([dist_treshold,dist_treshold],[0,3],':k', label='dist_treshold') xlim((0,0.002)) xlabel('distance (degree)') ylabel('N stars') legend() savefig(save_location+'out2.pdf') # figure(figsize=(8,8)) offset = list(obsout['f1']).index(phot_band) for i in indexes_gri: j = indx1[index_list[i]] obsout[j-offset][0]=gri[i][0] scatter(lon[j],lat[j],marker='o',s=marker_size_(obsout['f6'])[j] ) scatter(gri['RA'][i],gri['dec'][i],color='r',marker='o',s=marker_size_(gri[index_band])[i] ) plot(SN_coord[0],SN_coord[1],'+g') plot([lon[j],gri['RA'][i]],[lat[j],gri['dec'][i]]) if not isnan(indexes_obsout_SN): j=indexes_obsout_SN obsout[j-offset][0] = 'SN' scatter(lon[j],lat[j],marker='o',color='g',s=marker_size_(obsout['f6'])[j] ) gca().invert_yaxis() savefig(save_location+'out3.pdf') # figure() for i in indexes_gri: j = indx1[index_list[i]] plot( obsout['f6'][j],gri[index_band][i],'bo') savefig(save_location+'out4.pdf') ## Save final_obsout f=open(save_location+out_nobsfile,'w+') for j in range(len(obsout)): for k in range(len(obsout[j])): if k <len(obsout[j])-1: f.write(str(obsout[j][k]).replace('nan','INDEF').replace('False','INDEF')) for h in range(13-len(str(obsout[j][k]).replace('nan','INDEF').replace('False','INDEF'))): f.write(' ') else: f.write(str(obsout[j][k]).replace('nan','INDEF').replace('False','INDEF')+'\n') f.close() else: print fits_file + 'not found' return
def v_hist(): """""" t = Table.read("../data/deimos.fits") print(t.keys()) N = len(t) ra = np.zeros(N) * u.deg dec = np.zeros(N) * u.deg for j in range(N): ra[j] = Angle(t['RA'][j] + ' hours') dec[j] = Angle(t['DEC'][j] + 'degrees') # fname = '../data/TriS-1.out' ra_s, dec_s, i, priority, inmask = np.loadtxt( fname, usecols=(1, 2, 4, 6, 8), unpack=True, dtype='|S11, |S11, <f4, <f4, <i4', skiprows=4) Nm = np.size(ra_s) ra_m = np.zeros(Nm) * u.deg dec_m = np.zeros(Nm) * u.deg for j in range(Nm): ra_m[j] = Angle(ra_s[j] + ' hours') dec_m[j] = Angle(dec_s[j] + 'degrees') # match catalogs ref = ICRS(ra_m, dec_m, unit=(u.degree, u.degree)) cat = ICRS(ra, dec, unit=(u.degree, u.degree)) id1, d21, d31 = cat.match_to_catalog_sky(ref) matches = d21 < 0.5 * u.arcsec t_priority = priority[id1] i_m = i[id1] indices = (t['ZQUALITY'] > -1) t['Z'] *= 300000 add_npcolumn(t, name="priority", vec=t_priority) #print(t_priority, np.size(t_priority)) plt.close() plt.figure() plt.subplot(221) plt.hist(t['Z'], bins=np.arange(-400, 200, 10), histtype='step', color='k') plt.hist(t['Z'][indices], bins=np.arange(-400, 200, 10), histtype='step', color='b') plt.hist(t['Z'][t_priority >= 500], bins=np.arange(-400, 200, 10), histtype='step', color='r') plt.minorticks_on() plt.subplot(222) plt.plot(i_m[indices], t['Z'][indices], 'ko') print(t['Z'][t_priority >= 500], i_m[t_priority >= 500]) print(np.max(i_m[indices]))