-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzh_scraper.py
More file actions
130 lines (105 loc) · 4.7 KB
/
zh_scraper.py
File metadata and controls
130 lines (105 loc) · 4.7 KB
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
# Requirements
import requests
from bs4 import BeautifulSoup
__all__ = ['get_extinction']
def get_extinction(galaxy, ra, dec, radius, teff='all'):
"""
Query the Zaritsky & Harris dust extinction map for LMC or SMC at https://www.as.arizona.edu/~dennis/mcsurvey/Data_Products.html.
Please cite Zaritsky, Harris, Thompson, Grebel, and Massey 2002, AJ, 123, 855
and/or Zaritsky, Harris, Thompson, and Grebel, 2004, AJ, 128, 1606 if used.
Parameters:
galaxy : string
'LMC' or 'SMC'
ra : float
Right Ascension in units of hours
dec : float
Declination in units of degrees
radius : float
Search radius (max 12 arcminutes)
teff : string, optional
'all', 'cool', or 'hot', default is all
Returns:
mean_av, stdev_av : tuple of floats or None
Mean extinction Av and standard deviation within the radius if found, else None
Raises:
ValueError
If inputs are invalid or no stars found within the radius.
"""
galaxy = galaxy.upper()
# Validate galaxy and coordinate ranges
if not (type(ra) in {float,int}):
raise ValueError("RA must be in units of hours, and a float or integer (e.g. 4.50, not 04:30:00)")
if not (type(dec) in {float,int}):
raise ValueError("Dec must be in units of degrees, and a float or integer (e.g. -69.50, not -69:30:00)")
if galaxy == 'LMC':
base_url = 'https://www.as.arizona.edu/~dennis/cgi-bin/lmcext.cgi'
if (ra > 7.0):
raise ValueError("RA must be in units of hours, not degrees (e.g. 5.20 (hours), not 78.0 (degrees))")
if not (4.48 <= ra <= 6.27):
raise ValueError("For LMC, RA must be between 4.48 and 6.27 hours.")
if not (-72.5 <= dec <= -65.2):
raise ValueError("For LMC, Dec must be between -72.5 and -65.2 degrees.")
elif galaxy == 'SMC':
base_url = 'https://www.as.arizona.edu/~dennis/cgi-bin/smcext.cgi'
if (ra > 6.0):
raise ValueError("RA must be in units of hours, not degrees (e.g. 0.75 (hours), not 11.25 (degrees))")
if not (0.375 <= ra <= 1.375):
raise ValueError("For SMC, RA must be between 0.375 and 1.375 hours.")
if not (-74.9 <= dec <= -70.4):
raise ValueError("For SMC, Dec must be between -74.9 and -70.4 degrees.")
else:
raise ValueError("Galaxy must be 'LMC' or 'SMC'.")
if not (0 < radius <= 12):
raise ValueError("Radius must be greater than 0 and no more than 12 arcminutes.")
if teff not in {'all', 'cool', 'hot'}:
raise ValueError("teff must be 'all', 'cool', or 'hot'.")
params = {
'ra': ra,
'dec': dec,
'searchrad': radius,
'teff': teff
}
response = requests.get(base_url, params=params)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
mean_av = None
stdev_av = None
for line in soup.get_text().splitlines():
line = line.strip()
if "<Av> =" in line:
try:
mean_av = float(line.split('=')[1].split()[0])
except (ValueError, IndexError):
continue
elif "Standard deviation of extinction values =" in line:
try:
stdev_av = float(line.split('=')[1].split()[0])
except (ValueError, IndexError):
continue
if mean_av is not None and stdev_av is not None:
return mean_av, stdev_av
else:
raise ValueError("No stars found within the given radius. Try increasing the radius.")
# Script usage:
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Query Harris & Zaritsky dust extinction maps for LMC/SMC. Returns Av and standard deviation.")
parser.add_argument("--galaxy", required=True, choices=["LMC", "SMC"], help="Galaxy name: LMC or SMC")
parser.add_argument("--ra", required=True, type=float, help="Right Ascension in hours")
parser.add_argument("--dec", required=True, type=float, help="Declination in degrees")
parser.add_argument("--radius", required=True, type=float, help="Search radius (max 12 arcminutes)")
parser.add_argument("--teff", choices=["all", "cool", "hot"], default="all", help="Temperature selection, default is all")
args = parser.parse_args()
try:
mean_av, stdev_av = get_extinction(
galaxy=args.galaxy,
ra=args.ra,
dec=args.dec,
radius=args.radius,
teff=args.teff
)
print(f"Mean Av: {mean_av} mag, Standard deviation: {stdev_av} mag")
except ValueError as e:
print(f"Input error: {e}")
except requests.RequestException as e:
print(f"Request error: {e}")