/
wcsmap.py
286 lines (246 loc) · 9.38 KB
/
wcsmap.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import json
import numpy as np
from astropy.io import fits
from .base import Map
from .wcs import WcsGeom
from .utils import find_hdu, find_bands_hdu
__all__ = ["WcsMap"]
class WcsMap(Map):
"""Base class for WCS map classes.
Parameters
----------
geom : `~gammapy.maps.WcsGeom`
A WCS geometry object.
data : `~numpy.ndarray`
Data array.
"""
@classmethod
def create(
cls,
map_type="wcs",
npix=None,
binsz=0.1,
width=None,
proj="CAR",
coordsys="CEL",
refpix=None,
axes=None,
skydir=None,
dtype="float32",
conv="gadf",
meta=None,
unit="",
):
"""Factory method to create an empty WCS map.
Parameters
----------
map_type : {'wcs', 'wcs-sparse'}
Map type. Selects the class that will be used to
instantiate the map.
npix : int or tuple or list
Width of the map in pixels. A tuple will be interpreted as
parameters for longitude and latitude axes. For maps with
non-spatial dimensions, list input can be used to define a
different map width in each image plane. This option
supersedes width.
width : float or tuple or list
Width of the map in degrees. A tuple will be interpreted
as parameters for longitude and latitude axes. For maps
with non-spatial dimensions, list input can be used to
define a different map width in each image plane.
binsz : float or tuple or list
Map pixel size in degrees. A tuple will be interpreted
as parameters for longitude and latitude axes. For maps
with non-spatial dimensions, list input can be used to
define a different bin size in each image plane.
skydir : tuple or `~astropy.coordinates.SkyCoord`
Sky position of map center. Can be either a SkyCoord
object or a tuple of longitude and latitude in deg in the
coordinate system of the map.
coordsys : {'CEL', 'GAL'}, optional
Coordinate system, either Galactic ('GAL') or Equatorial ('CEL').
axes : list
List of non-spatial axes.
proj : string, optional
Any valid WCS projection type. Default is 'CAR' (cartesian).
refpix : tuple
Reference pixel of the projection. If None then this will
be chosen to be center of the map.
dtype : str, optional
Data type, default is float32
conv : {'fgst-ccube','fgst-template','gadf'}, optional
FITS format convention. Default is 'gadf'.
meta : `~collections.OrderedDict`
Dictionary to store meta data.
unit : str or `~astropy.units.Unit`
The unit of the map
Returns
-------
map : `~WcsMap`
A WCS map object.
"""
from .wcsnd import WcsNDMap
# from .wcssparse import WcsMapSparse
geom = WcsGeom.create(
npix=npix,
binsz=binsz,
width=width,
proj=proj,
skydir=skydir,
coordsys=coordsys,
refpix=refpix,
axes=axes,
conv=conv,
)
if map_type == "wcs":
return WcsNDMap(geom, dtype=dtype, meta=meta, unit=unit)
elif map_type == "wcs-sparse":
raise NotImplementedError
else:
raise ValueError("Invalid map type: {!r}".format(map_type))
@classmethod
def from_hdulist(cls, hdu_list, hdu=None, hdu_bands=None):
"""Make a WcsMap object from a FITS HDUList.
Parameters
----------
hdu_list : `~astropy.io.fits.HDUList`
HDU list containing HDUs for map data and bands.
hdu : str
Name or index of the HDU with the map data.
hdu_bands : str
Name or index of the HDU with the BANDS table.
Returns
-------
wcs_map : `WcsMap`
Map object
"""
if hdu is None:
hdu = find_hdu(hdu_list)
else:
hdu = hdu_list[hdu]
if hdu_bands is None:
hdu_bands = find_bands_hdu(hdu_list, hdu)
if hdu_bands is not None:
hdu_bands = hdu_list[hdu_bands]
return cls.from_hdu(hdu, hdu_bands)
def to_hdulist(self, hdu=None, hdu_bands=None, sparse=False, conv=None):
"""Convert to `~astropy.io.fits.HDUList`.
Parameters
----------
hdu : str
Name or index of the HDU with the map data.
hdu_bands : str
Name or index of the HDU with the BANDS table.
sparse : bool
Sparsify the map by only writing pixels with non-zero
amplitude.
conv : {'fgst-ccube','fgst-template','gadf',None}, optional
FITS format convention. If None this will be set to the
default convention of the map.
Returns
-------
hdu_list : `~astropy.io.fits.HDUList`
"""
if sparse:
hdu = "SKYMAP" if hdu is None else hdu.upper()
else:
hdu = "PRIMARY" if hdu is None else hdu.upper()
if sparse and hdu == "PRIMARY":
raise ValueError("Sparse maps cannot be written to the PRIMARY HDU.")
if self.geom.axes:
hdu_bands_out = self.geom.make_bands_hdu(
hdu=hdu_bands, hdu_skymap=hdu, conv=conv
)
hdu_bands = hdu_bands_out.name
else:
hdu_bands = None
hdu_out = self.make_hdu(hdu=hdu, hdu_bands=hdu_bands, sparse=sparse, conv=conv)
hdu_out.header["META"] = json.dumps(self.meta)
hdu_out.header["BUNIT"] = self.unit.to_string("fits")
if hdu == "PRIMARY":
hdulist = [hdu_out]
else:
hdulist = [fits.PrimaryHDU(), hdu_out]
if self.geom.axes:
hdulist += [hdu_bands_out]
return fits.HDUList(hdulist)
def make_hdu(self, hdu="SKYMAP", hdu_bands=None, sparse=False, conv=None):
"""Make a FITS HDU from this map.
Parameters
----------
hdu : str
The HDU extension name.
hdu_bands : str
The HDU extension name for BANDS table.
sparse : bool
Set INDXSCHM to SPARSE and sparsify the map by only
writing pixels with non-zero amplitude.
Returns
-------
hdu : `~astropy.io.fits.BinTableHDU` or `~astropy.io.fits.ImageHDU`
HDU containing the map data.
"""
header = self.geom.make_header()
if hdu_bands is not None:
header["BANDSHDU"] = hdu_bands
if sparse:
hdu_out = self._make_hdu_sparse(self.data, self.geom.npix, hdu, header)
elif hdu == "PRIMARY":
hdu_out = fits.PrimaryHDU(self.data, header=header)
else:
hdu_out = fits.ImageHDU(self.data, header=header, name=hdu)
return hdu_out
@staticmethod
def _make_hdu_sparse(data, npix, hdu, header):
shape = data.shape
# We make a copy, because below we modify `data` to handle non-finite entries
# TODO: The code below could probably be simplified to use expressions
# that create new arrays instead of in-place modifications
# But first: do we want / need the non-finite entry handling at all and always cast to 64-bit float?
data = data.copy()
if len(shape) == 2:
data_flat = np.ravel(data)
data_flat[~np.isfinite(data_flat)] = 0
nonzero = np.where(data_flat > 0)
value = data_flat[nonzero].astype(float)
cols = [
fits.Column("PIX", "J", array=nonzero[0]),
fits.Column("VALUE", "E", array=value),
]
elif npix[0].size == 1:
shape_flat = shape[:-2] + (shape[-1] * shape[-2],)
data_flat = np.ravel(data).reshape(shape_flat)
data_flat[~np.isfinite(data_flat)] = 0
nonzero = np.where(data_flat > 0)
channel = np.ravel_multi_index(nonzero[:-1], shape[:-2])
value = data_flat[nonzero].astype(float)
cols = [
fits.Column("PIX", "J", array=nonzero[-1]),
fits.Column("CHANNEL", "I", array=channel),
fits.Column("VALUE", "E", array=value),
]
else:
data_flat = []
channel = []
pix = []
for i, _ in np.ndenumerate(npix[0]):
data_i = np.ravel(data[i[::-1]])
data_i[~np.isfinite(data_i)] = 0
pix_i = np.where(data_i > 0)
data_i = data_i[pix_i]
data_flat += [data_i]
pix += pix_i
channel += [
np.ones(data_i.size, dtype=int)
* np.ravel_multi_index(i[::-1], shape[:-2])
]
pix = np.concatenate(pix)
channel = np.concatenate(channel)
value = np.concatenate(data_flat).astype(float)
cols = [
fits.Column("PIX", "J", array=pix),
fits.Column("CHANNEL", "I", array=channel),
fits.Column("VALUE", "E", array=value),
]
return fits.BinTableHDU.from_columns(cols, header=header, name=hdu)