Debian Codegraph @

astropy_healpix/utils.py

raw
1 import numpy as np
2
3 from astropy.io import fits
4 from astropy.io.fits import TableHDU, BinTableHDU
5 from astropy.coordinates import BaseCoordinateFrame, frame_transform_graph, Galactic, ICRS
6
7 FRAMES = {
8 'g': Galactic(),
9 'c': ICRS()
10 }
11
12
13 def parse_coord_system(system):
14 if isinstance(system, BaseCoordinateFrame):
15 return system
16 elif isinstance(system, str):
17 system = system.lower()
18 if system == 'e':
19 raise ValueError("Ecliptic coordinate frame not yet supported")
20 elif system in FRAMES:
21 return FRAMES[system]
22 else:
23 system_new = frame_transform_graph.lookup_name(system)
24 if system_new is None:
25 raise ValueError(f"Could not determine frame for system={system}")
26 else:
27 return system_new()
28
29
30 def parse_input_healpix_data(input_data, field=0, hdu_in=None, nested=None):
31 """
32 Parse input HEALPIX data to return a Numpy array and coordinate frame object.
33 """
34
35 if isinstance(input_data, (TableHDU, BinTableHDU)):
36 data = input_data.data
37 header = input_data.header
38 coordinate_system_in = parse_coord_system(header['COORDSYS'])
39 array_in = data[data.columns[field].name].ravel()
40 if 'ORDERING' in header:
41 nested = header['ORDERING'].lower() == 'nested'
42 elif isinstance(input_data, str):
43 hdu = fits.open(input_data)[hdu_in or 1]
44 return parse_input_healpix_data(hdu, field=field)
45 elif isinstance(input_data, tuple) and isinstance(input_data[0], np.ndarray):
46 array_in = input_data[0]
47 coordinate_system_in = parse_coord_system(input_data[1])
48 else:
49 raise TypeError("input_data should either be an HDU object or a tuple of (array, frame)")
50
51 return array_in, coordinate_system_in, nested