-
Notifications
You must be signed in to change notification settings - Fork 0
/
delve_select.py
154 lines (133 loc) · 5.19 KB
/
delve_select.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
"""
Select objects from DELVE catalogs at Fermilab.
"""
__author__ = "Alex Drlica-Wagner"
import os
import pandas as pd
import numpy as np
import pylab as plt
import healpy as hp
import fitsio
NSIDE = 32
BANDS = ['g','r','i','z']
DIRNAME = '/data/des91.b/data/kadrlica/projects/delve/cat'
BASENAME = 'cat_hpx_%05d.fits'
HPXNAME = '%s/hpx_%s_%05d.fits'
RELEASES = {
'dr1' :os.path.join(DIRNAME,'dr1'),
'dr2': os.path.join(DIRNAME,'dr2'),
}
COLUMNS = None
#COLUMNS = ['QUICK_OBJECT_ID','RA','DEC']
def ang2disc(nside, lon, lat, radius, inclusive=False, fact=4, nest=False):
"""
Wrap `query_disc` to use lon, lat, and radius in degrees.
Parameters
----------
nside : pixel resolution
lon : longitude (deg)
lat : latitude (deg)
radius: disc radius (deg)
"""
vec = hp.ang2vec(lon,lat,lonlat=True)
return hp.query_disc(nside,vec,np.radians(radius),inclusive,fact,nest)
def angsep(lon1,lat1,lon2,lat2):
"""
Angular separation (deg) between two sky coordinates.
Borrowed from astropy (www.astropy.org)
Notes
-----
The angular separation is calculated using the Vincenty formula [1],
which is slighly more complex and computationally expensive than
some alternatives, but is stable at at all distances, including the
poles and antipodes.
[1] http://en.wikipedia.org/wiki/Great-circle_distance
"""
lon1,lat1 = np.radians([lon1,lat1])
lon2,lat2 = np.radians([lon2,lat2])
sdlon = np.sin(lon2 - lon1)
cdlon = np.cos(lon2 - lon1)
slat1 = np.sin(lat1)
slat2 = np.sin(lat2)
clat1 = np.cos(lat1)
clat2 = np.cos(lat2)
num1 = clat2 * sdlon
num2 = clat1 * slat2 - slat1 * clat2 * cdlon
denominator = slat1 * slat2 + clat1 * clat2 * cdlon
return np.degrees(np.arctan2(np.hypot(num1,num2), denominator))
def find_filenames(ra,dec,radius=1.0,release='dr1',style='cat'):
""" Find catalog filenames. """
pixels = ang2disc(NSIDE,ra,dec,radius,inclusive=True,nest=False)
dirname = RELEASES[release]
if style == 'cat':
filenames = [os.path.join(dirname,style,BASENAME%p) for p in pixels]
elif style == 'hpx':
filenames = []
for band in BANDS:
for p in pixels:
filenames.append(os.path.join(dirname,style,HPXNAME%(band,band,p)))
filenames = [f for f in filenames if os.path.exists(f)]
return filenames
def load_catalogs(ra,dec,radius=1.0,release='dr1',style='cat',verbose=False):
""" Load catalogs."""
filenames = find_filenames(ra,dec,radius,release,style=style)
if args.verbose:
print("Catalog filenames:")
for f in filenames:
print(" "+f)
catalogs = []
for f in filenames:
if verbose: print("Loading %s..."%os.path.basename(f))
cat = fitsio.read(f,columns=COLUMNS)
sel = np.ones(len(cat),dtype=bool)
if radius is not None:
sep = angsep(ra,dec,cat['RA'],cat['DEC'])
sel = sep < radius
catalogs.append(cat[sel])
return np.concatenate(catalogs)
def write_catalog(outfile,catalog):
if args.verbose: print("Writing %s..."%outfile)
if outfile.endswith(('.fits','.fz')):
fitsio.write(outfile,catalog,clobber=True)
elif outfile.endswith(('.csv','.txt','.dat')):
df = pd.DataFrame(catalog.byteswap().newbyteorder())
sep = ',' if outfile.endswith('.csv') else ' '
df.to_csv(args.outfile,index=False,sep=sep)
else:
raise Exception("Unrecognized output file format: %s"%outfile)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('ra',type=float,
help = 'Right ascension (deg)')
parser.add_argument('dec',type=float,
help = 'Declination (deg)')
parser.add_argument('-r','--radius',default=180, type=float,
help = 'selection radius (arcsec)')
parser.add_argument('--release',choices=RELEASES.keys(),default='dr1',
help='data releases')
parser.add_argument('--style',choices=['cat','hpx'],default='cat',
help='catalog style')
parser.add_argument('-v','--verbose',action='store_true',
help='verbosity')
parser.add_argument('-o','--outfile',
help='output filename (.fits,.csv,...)')
args = parser.parse_args()
if args.verbose:
print("Selecting catalogs from release: %s"%args.release)
print("RA, DEC, RADIUS = (%.5f deg, %.5f deg, %.1f arcsec)"%(args.ra,args.dec,args.radius))
ra,dec = args.ra,args.dec # deg
radius = args.radius/3600. # arcsec to deg
catalog = load_catalogs(ra,dec,radius,release=args.release,
style=args.style,verbose=args.verbose)
print("Catalog with %s objects."%len(catalog))
if args.outfile:
write_catalog(args.outfile,catalog)
else:
df = pd.DataFrame(catalog.byteswap().newbyteorder())
if args.style == 'cat':
columns=['RA','DEC','MAG_AUTO_G','MAG_AUTO_R','MAG_AUTO_I','MAG_AUTO_Z']
elif args.style == 'hpx':
columns = ['RA','DEC','MAG_AUTO','MJD_OBS']
print(df[columns])