1
|
|
|
import numpy as np |
|
|
|
|
2
|
|
|
import astropy.units as u |
3
|
|
|
import healpy as hp |
4
|
|
|
|
5
|
|
|
from threeML.exceptions.custom_exceptions import custom_warnings |
6
|
|
|
from astromodels.core.sky_direction import SkyDirection |
7
|
|
|
|
8
|
|
|
from hawc_hal.region_of_interest.healpix_roi_base import HealpixROIBase |
9
|
|
|
from hawc_hal.region_of_interest.healpix_cone_roi import HealpixConeROI, _get_radians |
10
|
|
|
from ..flat_sky_projection import FlatSkyProjection |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class HealpixMapROI(HealpixROIBase): |
|
|
|
|
14
|
|
|
|
15
|
|
|
def __init__(self, model_radius, roimap=None, roifile=None, threshold=0.5, *args, **kwargs): |
|
|
|
|
16
|
|
|
""" |
17
|
|
|
A cone Region of Interest defined by a healpix map (can be read from a fits file). |
18
|
|
|
User needs to supply a cone region (center and radius) defining the plane projection for the model map. |
19
|
|
|
|
20
|
|
|
Examples: |
21
|
|
|
|
22
|
|
|
Model map centered on (R.A., Dec) = (1.23, 4.56) in J2000 ICRS coordinate system, |
23
|
|
|
with a radius of 5 degrees, ROI defined in healpix map in fitsfile: |
24
|
|
|
|
25
|
|
|
> roi = HealpixMapROI(5.0, ra=1.23, dec=4.56, file = "myROI.fits" ) |
26
|
|
|
|
27
|
|
|
Model map centered on (L, B) = (1.23, 4.56) (Galactic coordiantes) |
28
|
|
|
with a radius of 30 arcmin, ROI defined on-the-fly in healpix map: |
29
|
|
|
|
30
|
|
|
> roi = HealpixMapROI(30.0 * u.arcmin, l=1.23, dec=4.56, map = my_roi) |
31
|
|
|
|
32
|
|
|
:param model_radius: radius of the model cone. Either an astropy.Quantity instance, or a float, in which case it |
33
|
|
|
is assumed to be the radius in degrees |
34
|
|
|
:param map: healpix map containing the ROI. |
35
|
|
|
:param file: fits file containing a healpix map with the ROI. |
36
|
|
|
:param threshold: value below which pixels in the map will be set inactive (=not in ROI). |
37
|
|
|
:param args: arguments for the SkyDirection class of astromodels |
38
|
|
|
:param kwargs: keywords for the SkyDirection class of astromodels |
39
|
|
|
""" |
40
|
|
|
|
|
|
|
|
41
|
|
|
assert roifile is not None or roimap is not None, "Must supply either healpix map or fitsfile to create HealpixMapROI" |
|
|
|
|
42
|
|
|
|
43
|
|
|
self._center = SkyDirection(*args, **kwargs) |
44
|
|
|
|
45
|
|
|
self._model_radius_radians = _get_radians(model_radius) |
46
|
|
|
|
47
|
|
|
self._threshold = threshold |
48
|
|
|
|
49
|
|
|
self.read_map(roimap=roimap, roifile=roifile) |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def read_map(self, roimap=None, roifile=None): |
|
|
|
|
53
|
|
|
assert roifile is not None or roimap is not None, \ |
54
|
|
|
"Must supply either healpix map or fits file" |
55
|
|
|
|
|
|
|
|
56
|
|
|
if roimap is not None: |
57
|
|
|
roimap = roimap |
58
|
|
|
self._filename = None |
59
|
|
|
|
60
|
|
|
elif roifile is not None: |
61
|
|
|
self._filename = roifile |
62
|
|
|
roimap = hp.fitsfunc.read_map(self._filename) |
|
|
|
|
63
|
|
|
|
64
|
|
|
self._roimaps = {} |
65
|
|
|
|
66
|
|
|
self._original_nside = hp.npix2nside(roimap.shape[0]) |
67
|
|
|
self._roimaps[self._original_nside] = roimap |
68
|
|
|
|
|
|
|
|
69
|
|
|
self.check_roi_inside_model() |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def check_roi_inside_model(self): |
|
|
|
|
73
|
|
|
|
74
|
|
|
active_pixels = self.active_pixels(self._original_nside) |
75
|
|
|
|
76
|
|
|
radius = np.rad2deg(self._model_radius_radians) |
77
|
|
|
ra, dec = self.ra_dec_center |
|
|
|
|
78
|
|
|
temp_roi = HealpixConeROI(data_radius = radius , model_radius=radius, ra=ra, dec=dec) |
|
|
|
|
79
|
|
|
|
80
|
|
|
model_pixels = temp_roi.active_pixels( self._original_nside ) |
|
|
|
|
81
|
|
|
|
82
|
|
|
if not all(p in model_pixels for p in active_pixels): |
83
|
|
|
custom_warnings.warn("Some pixels inside your ROI are not contained in the model map.") |
84
|
|
|
|
85
|
|
|
def to_dict(self): |
86
|
|
|
|
87
|
|
|
ra, dec = self.ra_dec_center |
|
|
|
|
88
|
|
|
|
89
|
|
|
s = {'ROI type': type(self).__name__.split(".")[-1], |
|
|
|
|
90
|
|
|
'ra': ra, |
91
|
|
|
'dec': dec, |
92
|
|
|
'model_radius_deg': np.rad2deg(self._model_radius_radians), |
93
|
|
|
'roimap': self._roimaps[self._original_nside], |
94
|
|
|
'threshold': self._threshold, |
95
|
|
|
'roifile': self._filename } |
|
|
|
|
96
|
|
|
|
97
|
|
|
return s |
98
|
|
|
|
99
|
|
|
@classmethod |
100
|
|
|
def from_dict(cls, data): |
101
|
|
|
|
102
|
|
|
return cls(data['model_radius_deg'], threshold=data['threshold'], |
103
|
|
|
roimap=data['roimap'], ra=data['ra'], |
104
|
|
|
dec=data['dec'], roifile=data['roifile']) |
105
|
|
|
|
106
|
|
|
def __str__(self): |
107
|
|
|
|
108
|
|
|
s = ("%s: Center (R.A., Dec) = (%.3f, %.3f), model radius: %.3f deg, threshold = %.2f" % |
|
|
|
|
109
|
|
|
(type(self).__name__, self.ra_dec_center[0], self.ra_dec_center[1], |
|
|
|
|
110
|
|
|
self.model_radius.to(u.deg).value, self._threshold)) |
|
|
|
|
111
|
|
|
|
112
|
|
|
if self._filename is not None: |
|
|
|
|
113
|
|
|
s = "%s, data ROI from %s" % (s, self._filename) |
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
return s |
116
|
|
|
|
117
|
|
|
def display(self): |
118
|
|
|
|
119
|
|
|
print(self) |
|
|
|
|
120
|
|
|
|
121
|
|
|
@property |
122
|
|
|
def ra_dec_center(self): |
|
|
|
|
123
|
|
|
|
124
|
|
|
return self._get_ra_dec() |
125
|
|
|
|
126
|
|
|
@property |
127
|
|
|
def model_radius(self): |
|
|
|
|
128
|
|
|
return self._model_radius_radians * u.rad |
129
|
|
|
|
130
|
|
|
@property |
131
|
|
|
def threshold(self): |
|
|
|
|
132
|
|
|
return self._threshold |
133
|
|
|
|
134
|
|
|
def _get_ra_dec(self): |
135
|
|
|
|
136
|
|
|
lon, lat = self._center.get_ra(), self._center.get_dec() |
137
|
|
|
|
138
|
|
|
return lon, lat |
139
|
|
|
|
140
|
|
|
def _active_pixels(self, nside, ordering): |
141
|
|
|
|
142
|
|
|
if not nside in self._roimaps: |
143
|
|
|
self._roimaps[nside] = hp.ud_grade(self._roimaps[self._original_nside], nside_out=nside) |
|
|
|
|
144
|
|
|
|
145
|
|
|
pixels_inside_roi = np.where(self._roimaps[nside] >= self._threshold)[0] |
146
|
|
|
|
147
|
|
|
return pixels_inside_roi |
148
|
|
|
|
149
|
|
View Code Duplication |
def get_flat_sky_projection(self, pixel_size_deg): |
|
|
|
|
150
|
|
|
|
151
|
|
|
# Decide side for image |
152
|
|
|
|
153
|
|
|
# Compute number of pixels, making sure it is going to be even (by approximating up) |
154
|
|
|
npix_per_side = 2 * int(np.ceil(np.rad2deg(self._model_radius_radians) / pixel_size_deg)) |
155
|
|
|
|
156
|
|
|
# Get lon, lat of center |
157
|
|
|
ra, dec = self._get_ra_dec() |
|
|
|
|
158
|
|
|
|
159
|
|
|
# This gets a list of all RA, Decs for an AIT-projected image of npix_per_size x npix_per_side |
160
|
|
|
flat_sky_proj = FlatSkyProjection(ra, dec, pixel_size_deg, npix_per_side, npix_per_side) |
161
|
|
|
|
162
|
|
|
return flat_sky_proj |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.