-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolve.py
181 lines (149 loc) · 7.47 KB
/
solve.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
import os
import subprocess
import shlex
import argparse
import logging
from astropy.io import fits, ascii
from astropy.wcs import WCS
from astropy.coordinates import SkyCoord
from astropy import units
import numpy as np
from gaia_astrometry_index_files.catalog import make_catalog
from gaia_astrometry_index_files.utils import great_circle_distance
logger = logging.getLogger(__name__)
ASTROMETRY_NET_COMMAND = 'solve-field --crpix-center --no-verify --no-tweak ' \
' --radius {radius} --ra {ra} --dec {dec} --guess-scale ' \
'--scale-units arcsecperpix --scale-low {scale_low} --scale-high {scale_high} ' \
'--no-plots -N none --no-remove-lines ' \
'--code-tolerance 0.003 --pixel-error 1 -d 1-200 ' \
'--solved none --match none --rdls {catalog_source_file} --wcs {wcs_name} --corr none --overwrite ' \
'-X X -Y Y -s FLUX --width {nx} --height {ny} -b {config_name} {catalog_name}'
def get_relevant_index_files(ra, dec, radius, index_file_path):
index_file_meta_data = ascii.read(os.path.join(index_file_path, 'gaia-dr2-index-files.dat'))
offsets = great_circle_distance(index_file_meta_data['ra'], index_file_meta_data['dec'], ra, dec)
relevant_healpixels = offsets <= (index_file_meta_data['radius'] + radius)
return list(index_file_meta_data['filename'][relevant_healpixels])
def save_config(filename, index_files, index_file_path):
lines= ['inparallel\n', 'cpulimit 300\n', 'add_path {path}\n'.format(path=index_file_path)]
for index_file in index_files:
lines.append('index {index}\n'.format(index=index_file))
with open(filename, 'w') as f:
f.writelines(lines)
def solve_frame():
parser = argparse.ArgumentParser(description='Run astrometry.net on an LCO fits file using the GAIA DR2 Index files')
parser.add_argument('--ra', default=None,
help='RA of the center to search. Default is None which will then get a first guess from the header')
parser.add_argument('--dec', default=None,
help='Dec of the center to search. Default is None which will then get a first guess from the header')
parser.add_argument('--radius', default=2.0, help='radius of the search cone in degrees')
parser.add_argument('--index-file-path', dest='index_file_path', help='path to GAIA DR2 index files')
parser.add_argument('--filename', help='filename of the image to solve')
args = parser.parse_args()
data, header = fits.getdata(args.filename, header=True)
if args.ra is None or args.dec is None:
ra, dec = parse_ra_dec(header)
if args.ra is not None:
ra = args.ra
if args.dec is not None:
dec = args.dec
pixel_scale = header['PIXSCALE']
# Skip the image if we don't have some kind of initial RA and Dec guess
if np.isnan(ra) or np.isnan(dec):
logger.error('Skipping WCS solution. No initial pointing guess from header.')
return
basename = os.path.basename(args.filename)
catalog_name = os.path.join(os.getcwd(), basename.replace('.fits', '.cat.fits'))
try:
catalog = make_catalog(data, header)
catalog[:40].write(catalog_name, overwrite=True)
except:
logger.error('Could not produce source catalog')
return
index_files = get_relevant_index_files(ra, dec, args.radius, args.index_file_path)
config_file_name = 'astrometry.cfg'
save_config(config_file_name, index_files, args.index_file_path)
# Run astrometry.net
wcs_name = os.path.join(os.getcwd(), basename.replace('.fits', '.wcs.fits'))
catalog_source_name = basename.replace('.fits', '.rdls.fits')
command = ASTROMETRY_NET_COMMAND.format(ra=ra, dec=dec, scale_low=0.9 * pixel_scale, radius=args.radius,
scale_high=1.1 * pixel_scale, wcs_name=wcs_name,
catalog_name=catalog_name, nx=data.shape[1], ny=data.shape[0],
config_name=config_file_name, catalog_source_file=catalog_source_name)
try:
console_output = subprocess.check_output(shlex.split(command))
except subprocess.CalledProcessError:
logger.error('Astrometry.net threw an error.')
return
# Copy the WCS keywords into original image
new_header = fits.getheader(wcs_name)
header_keywords_to_update = ['CTYPE1', 'CTYPE2', 'CRPIX1', 'CRPIX2', 'CRVAL1',
'CRVAL2', 'CD1_1', 'CD1_2', 'CD2_1', 'CD2_2']
for keyword in header_keywords_to_update:
header[keyword] = new_header[keyword]
# Update the RA and Dec header keywords
header['RA'], header['DEC'] = get_ra_dec_in_sexagesimal(header['CRVAL1'], header['CRVAL2'])
# Add the RA and Dec values to the catalog
add_ra_dec_to_catalog(header, catalog)
save_solved_image(basename.replace('.fits', '.solved.fits'), header, data, catalog)
save_astrometry_catalog_regions(basename.replace('.fits', '.gaia.reg'), catalog_source_name)
def save_astrometry_catalog_regions(filename, catalog_fits_file):
hdu = fits.open(catalog_fits_file)
lines = ['{ra} {dec}\n'.format(ra=row['RA'], dec=row['DEC']) for row in hdu[1].data]
with open(filename, 'w') as f:
f.writelines(lines)
def save_solved_image(filename, header, data, catalog):
primary_hdu = fits.PrimaryHDU(data=data, header=header)
catalog_hdu = fits.BinTableHDU(catalog, name='CAT')
fits.HDUList([primary_hdu, catalog_hdu]).writeto(filename, overwrite=True)
def add_ra_dec_to_catalog(header, catalog):
image_wcs = WCS(header)
ras, decs = image_wcs.all_pix2world(catalog['x'], catalog['y'], 1)
catalog['ra'] = ras
catalog['dec'] = decs
catalog['ra'].unit = 'degree'
catalog['dec'].unit = 'degree'
catalog['ra'].description = 'Right Ascension'
catalog['dec'].description = 'Declination'
def get_ra_dec_in_sexagesimal(ra, dec):
"""
Convert a decimal RA and Dec to sexagesimal
Parameters
----------
ra : float
Right Ascension in decimal form
dec : float
Declination in decimal form
Returns
-------
tuple of str : RA, Dec converted to a string
"""
coord = SkyCoord(ra, dec, unit=(units.deg, units.deg))
coord_str = coord.to_string('hmsdms', precision=4, pad=True)
ra_str, dec_str = coord_str.split()
ra_str = ra_str.replace('h', ':').replace('m', ':').replace('s', '')
dec_str = dec_str.replace('d', ':').replace('m', ':').replace('s', '')
# Return one less digit of precision for the dec
dec_str = dec_str[:-1]
return ra_str, dec_str
def parse_ra_dec(header):
try:
coord = SkyCoord(header.get('RA'), header.get('DEC'), unit=(units.hourangle, units.degree))
ra = coord.ra.deg
dec = coord.dec.deg
except (ValueError, TypeError):
# Fallback to CRVAL1 and CRVAL2
try:
coord = SkyCoord(header.get('CRVAl1'), header.get('CRVAL2'), unit=(units.degree, units.degree))
ra = coord.ra.deg
dec = coord.dec.deg
except (ValueError, TypeError):
# Fallback to Cat-RA and CAT-DEC
try:
coord = SkyCoord(header.get('CAT-RA'), header.get('CAT-DEC'), unit=(units.hourangle, units.degree))
ra = coord.ra.deg
dec = coord.dec.deg
except (ValueError, TypeError) as e:
logger.error('Could not get initial pointing guess. {0}'.format(e),
extra_tags={'filename': header.get('ORIGNAME')})
ra, dec = np.nan, np.nan
return ra, dec