-
Notifications
You must be signed in to change notification settings - Fork 19
/
gal.py
63 lines (58 loc) · 1.77 KB
/
gal.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
#!/usr/bin/env python
from __future__ import print_function
import astropy.io.fits as pyfits
import requests
url = "https://www.swift.ac.uk/analysis/nhtot/donhtot.php"
def parse_response(html_response):
""" parses the html response and extracts the weighted total NH value """
akey = "headers='htotw'>"
a = html_response.index(akey) + len(akey)
l = html_response[a:].index("</td>")
part = html_response[a:a+l]
if part.endswith('</sup>'):
part = part[:-len('</sup>')]
base, expo = part.split(' ×10<sup>')
return float(base) * 10**float(expo)
else:
return float(part)
def get_gal_nh(ra, dec):
""" asks swift.ac.uk for the total NH value at (ra,dec) """
r = requests.post(url, data=dict(
equinox=2000, Coords="%s %s" % (ra, dec),
submit='Calculate NH')
)
nh = parse_response(r.text)
return nh
if __name__ == '__main__':
import sys, os
cache = []
for infile in sys.argv[1:]:
outfile = infile + '.nh'
if not os.path.exists(outfile):
f = pyfits.open(infile)
for i in 0, 1:
header = f[i].header
print('looking for RA, DEC headers ...')
if 'RA_OBJ' in header:
print('using RA_OBJ, DEC_OBJ headers ...')
ra = header['RA_OBJ']
dec = header['DEC_OBJ']
break
elif 'RA_TARG' in header:
print('using RA_TARG, DEC_TARG headers ...')
ra = header['RA_TARG']
dec = header['DEC_TARG']
break
nhs = [nhc for rac, decc, nhc in cache if rac == ra and decc == decc]
if len(nhs) > 0:
print('same as a previous one')
nh = nhs[0]
else:
print('requesting galactic NH from swift.ac.uk...')
nh = get_gal_nh(ra, dec)
cache.append((ra, dec, nh))
print(('writing to %s ...' % outfile))
open(outfile, 'w').write("%e\n" % nh)
del ra, dec
else:
print(('File %s already exists.' % outfile))