-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquery_all_sdss.py
executable file
·201 lines (162 loc) · 7.57 KB
/
query_all_sdss.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
#
# Copyright 2012-2013 Ralf Kotulla
# kotulla@uwm.edu
#
# This file is part of the ODI QuickReduce pipeline package.
#
# If you find this program or parts thereof please make sure to
# cite it appropriately (please contact the author for the most
# up-to-date reference to use). Also if you find any problems
# or have suggestiosn on how to improve the code or its
# functionality please let me know. Comments and questions are
# always welcome.
#
# The code is made publicly available. Feel free to share the link
# with whoever might be interested. However, I do ask you to not
# publish additional copies on your own website or other sources.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
import sys
import numpy
import os
import math
from podi_definitions import *
import astropy.io.fits as pyfits
import podi_photcalib
def run_query(sql_query):
# Taken from Tomas Budavari's sqlcl script
# see http://skyserver.sdss3.org/dr8/en/help/download/sqlcl/default.asp
import urllib
# Filter out comments starting with "--"
fsql = ""
for line in sql_query.split('\n'):
fsql += line.split('--')[0] + ' ' + os.linesep
#print "_______",fsql,"_________"
params = urllib.urlencode({'cmd': fsql, 'format': 'csv'})
#print params
url = 'http://skyserver.sdss3.org/dr8/en/tools/search/x_sql.asp'
sdss = urllib.urlopen(url+'?%s' % params)
# Budavari end
answer = []
for line in sdss:
answer.append(line)
if (((len(answer)-1)%10) == 0):
stdout_write("\rFound %d stars so far ..." % (len(answer)-1))
#answer = sdss.readlines()
if (answer[0].strip() == "No objects have been found"):
return numpy.zeros(shape=(0,0))
else:
stdout_write("\rFound a total of %d stars in SDSS catalog\n" % (len(answer)-1))
return answer
if __name__ == "__main__":
basedir = sys.argv[1]
startat = int(cmdline_arg_set_or_default("-startat", 0))
# Load the SkyTable so we know in what files to look for the catalog"
skytable_filename = "%s/SkyTable.fits" % (basedir)
skytable_hdu = pyfits.open(skytable_filename)
#print skytable_hdu.info()
skytable = skytable_hdu['SKY_REGION'].data
#print skytable[:3]
r_min = skytable.field('R_MIN')
r_max = skytable.field('R_MAX')
d_min = skytable.field('D_MIN')
d_max = skytable.field('D_MAX')
outputfile = skytable.field("name")
print(r_min)
print(outputfile[0:5])
for i in range(startat, len(outputfile)):
directory, name = os.path.split(outputfile[i])
stdout_write("\nNext: %s/%s ---> RA=%.3f-%.3f, DEC=%.3f...%.3f\n" % (
directory, name, r_min[i], r_max[i], d_min[i], d_max[i], ))
if (not os.path.isdir(directory)):
os.mkdir(directory)
cat_fitsfile = outputfile[i]+".fits"
if (os.path.isfile(cat_fitsfile)):
hdu = pyfits.open(cat_fitsfile)
expected = hdu[0].header['EXPCOUNT']
read = hdu[0].header['CNTSRECV']
print("%s: exp. %d, completed %d" % (cat_fitsfile, expected, read))
hdu.close()
if (read >= expected):
print("This file already is complete, continuing with the next one")
continue
else:
print("#########################")
print("This file was started, but not complete, trying again:")
print("@@@", cat_fitsfile)
print("#########################")
count_entries = """\
SELECT COUNT(*) from Star
where
ra>%(ramin)f and ra<%(ramax)f and
dec between %(min_dec)f and %(max_dec)f
AND ((flags_r & 0x10000000) != 0)
-- detected in BINNED1
AND ((flags_r & 0x8100000c00a4) = 0)
-- not EDGE, NOPROFILE, PEAKCENTER, NOTCHECKED, PSF_FLUX_INTERP,
-- SATURATED, or BAD_COUNTS_ERROR
AND (((flags_r & 0x400000000000) = 0) or (psfmagerr_r <= 0.2))
-- not DEBLEND_NOPEAK or small PSF error
-- (substitute psfmagerr in other band as appropriate)
AND (((flags_r & 0x100000000000) = 0) or (flags_r & 0x1000) = 0)
-- not INTERP_CENTER or not COSMIC_RAY""" % \
{"ramin": r_min[i],
"ramax": r_max[i],
# "min_dec": -30,
# "max_dec": -32,}
"min_dec": d_min[i],
"max_dec": d_max[i],}
#print count_entries
answer = run_query(count_entries)
#print "___",answer,"___"
if (len(answer) > 2):
print(''.join(answer))
continue
count = int(answer[1].strip())
print("---> expecting %s stars as result" % (count))
#print ''.join(answer)
if (count > 0):
catalog, sqlquery = podi_photcalib.load_catalog_from_sdss([r_min[i], r_max[i]], [d_min[i], d_max[i]], "r", return_query=True)
#print catalog.shape
#print catalog[:10]
sdss_cat = catalog
else:
sqlquery = "No results expected, did not query"
sdss_cat = numpy.zeros(shape=(0,12))
print("nothing expected...")
# Create a catalog to hold the results:
#sys.exit(0)
columns = [\
pyfits.Column(name='RA', format='E', unit='degrees', array=sdss_cat[:, 0], disp='right ascension'),
pyfits.Column(name='DEC', format='E', unit='degrees', array=sdss_cat[:, 1], disp='declination'),
pyfits.Column(name='MAG_U', format='E', unit='magnitudes', array=sdss_cat[:, 2], disp='photometry in u'),
pyfits.Column(name='MAGERR_U', format='E', unit='magnitudes', array=sdss_cat[:, 3], disp='phot. error in u'),
pyfits.Column(name='MAG_G', format='E', unit='magnitudes', array=sdss_cat[:, 4], disp='photometry in g'),
pyfits.Column(name='MAGERR_G', format='E', unit='magnitudes', array=sdss_cat[:, 5], disp='phot. error in g'),
pyfits.Column(name='MAG_R', format='E', unit='magnitudes', array=sdss_cat[:, 6], disp='photometry in r'),
pyfits.Column(name='MAGERR_R', format='E', unit='magnitudes', array=sdss_cat[:, 7], disp='phot. error in r'),
pyfits.Column(name='MAG_I', format='E', unit='magnitudes', array=sdss_cat[:, 8], disp='photometry in i'),
pyfits.Column(name='MAGERR_I', format='E', unit='magnitudes', array=sdss_cat[:, 9], disp='phot. error in i'),
pyfits.Column(name='MAG_Z', format='E', unit='magnitudes', array=sdss_cat[:,10], disp='photometry in z'),
pyfits.Column(name='MAGERR_Z', format='E', unit='magnitudes', array=sdss_cat[:,11], disp='phot. error in z'),
]
stdout_write("writing catalog ...")
primhdu = pyfits.PrimaryHDU()
#print sqlquery.replace("\n", " ")
primhdu.header.add_history("SQL-Query sent to SDSS:")
primhdu.header.add_history(sqlquery.replace("\n", " "))
primhdu.header["EXPCOUNT"] = (count, "expected return count")
primhdu.header["CNTSRECV"] = (sdss_cat.shape[0], "returned count")
primhdu.header["R_MIN"] = (r_min[i], "min RA")
primhdu.header["R_MAX"] = (r_max[i], "max RA")
primhdu.header["D_MIN"] = (d_min[i], "min DEC")
primhdu.header["D_MAX"] = (d_max[i], "max DEC")
coldefs = pyfits.ColDefs(columns)
tbhdu = pyfits.new_table(coldefs, tbtype='BinTableHDU')
hdulist = pyfits.HDUList([primhdu, tbhdu])
hdulist.writeto(cat_fitsfile, overwrite=True)
stdout_write(" done!\n")