-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-data.py
50 lines (34 loc) · 1.27 KB
/
fetch-data.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import requests
import json
import pandas as pd
# In[2]:
def sample(r1,r2,d1,d2,n,clas):
url = "http://skyserver.sdss.org/dr16/en/tools/search/x_sql.aspx"
payload = {
"format": "json",
"cmd": """
SELECT top {0} s.class, s.z, s.ra, s.dec,
p.deVRad_u, p.deVRad_g, p.deVRad_r, p.deVRad_i, p.deVRad_z,
p.dered_u, p.dered_g, p.dered_r, p.dered_i, p.dered_z,
p.extinction_u, p.extinction_g, p.extinction_r, p.extinction_i, p.extinction_z
FROM SpecObjAll s JOIN PhotoObjAll p ON s.specObjID = p.specObjID
WHERE s.class='{5}' and p.clean = 1 and s.zWarning = 0
AND s.ra between {1} and {2}
AND p.dec between {3} and {4}
""".format(n,r1,r2,d1,d2,clas).strip()
}
try:
resp = requests.post(url, params=payload, timeout=600)
except requests.exceptions.RequestException as e:
print(e)
return None
data = resp.json()[0]['Rows']
df = pd.DataFrame(data)
return df
# In[12]:
r1, r2, d1, d2, n, clas = input('enter coordinates, number and class: ').split()
df = sample(r1,r2,d1,d2,n,clas)
df.to_csv("{0}[{1}-{2}].csv".format(clas,r1,r2), index=False)