1
|
|
|
import numpy as np |
|
|
|
|
2
|
|
|
import astropy.units as u |
3
|
|
|
import healpy as hp |
4
|
|
|
from healpix_roi_base import HealpixROIBase, _RING, _NESTED |
|
|
|
|
5
|
|
|
|
6
|
|
|
from astromodels.core.sky_direction import SkyDirection |
|
|
|
|
7
|
|
|
|
8
|
|
|
from ..healpix_handling import radec_to_vec |
9
|
|
|
from ..flat_sky_projection import FlatSkyProjection |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def _get_radians(my_angle): |
13
|
|
|
|
14
|
|
|
if isinstance(my_angle, u.Quantity): |
15
|
|
|
|
16
|
|
|
my_angle_radians = my_angle.to(u.rad).value |
17
|
|
|
|
18
|
|
|
else: |
19
|
|
|
|
20
|
|
|
my_angle_radians = np.deg2rad(my_angle) |
21
|
|
|
|
22
|
|
|
return my_angle_radians |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class HealpixConeROI(HealpixROIBase): |
|
|
|
|
26
|
|
|
|
27
|
|
|
def __init__(self, data_radius, model_radius, *args, **kwargs): |
28
|
|
|
""" |
29
|
|
|
A cone Region of Interest defined by a center and a radius. |
30
|
|
|
|
31
|
|
|
Examples: |
32
|
|
|
|
33
|
|
|
ROI centered on (R.A., Dec) = (1.23, 4.56) in J2000 ICRS coordinate system, with a radius of 5 degrees: |
34
|
|
|
|
35
|
|
|
> roi = HealpixConeROI(5.0, ra=1.23, dec=4.56) |
36
|
|
|
|
37
|
|
|
ROI centered on (L, B) = (1.23, 4.56) (Galactic coordiantes) with a radius of 30 arcmin: |
38
|
|
|
|
39
|
|
|
> roi = HealpixConeROI(30.0 * u.arcmin, l=1.23, dec=4.56) |
40
|
|
|
|
41
|
|
|
:param data_radius: radius of the cone. Either an astropy.Quantity instance, or a float, in which case it is assumed |
|
|
|
|
42
|
|
|
to be the radius in degrees |
43
|
|
|
:param model_radius: radius of the model cone. Either an astropy.Quantity instance, or a float, in which case it |
44
|
|
|
is assumed to be the radius in degrees |
45
|
|
|
:param args: arguments for the SkyDirection class of astromodels |
46
|
|
|
:param kwargs: keywords for the SkyDirection class of astromodels |
47
|
|
|
""" |
48
|
|
|
|
49
|
|
|
self._center = SkyDirection(*args, **kwargs) |
50
|
|
|
|
51
|
|
|
self._data_radius_radians = _get_radians(data_radius) |
52
|
|
|
self._model_radius_radians = _get_radians(model_radius) |
53
|
|
|
|
54
|
|
|
def to_dict(self): |
55
|
|
|
|
56
|
|
|
ra, dec = self.ra_dec_center |
|
|
|
|
57
|
|
|
|
58
|
|
|
s = {'ROI type': type(self).__name__.split(".")[-1], |
|
|
|
|
59
|
|
|
'ra': ra, |
60
|
|
|
'dec': dec, |
61
|
|
|
'data_radius_deg': np.rad2deg(self._data_radius_radians), |
62
|
|
|
'model_radius_deg': np.rad2deg(self._model_radius_radians)} |
63
|
|
|
|
64
|
|
|
return s |
65
|
|
|
|
66
|
|
|
@classmethod |
67
|
|
|
def from_dict(cls, data): |
68
|
|
|
|
69
|
|
|
return cls(data['data_radius_deg'], data['model_radius_deg'], ra=data['ra'], dec=data['dec']) |
70
|
|
|
|
71
|
|
|
def __str__(self): |
72
|
|
|
|
73
|
|
|
s = ("%s: Center (R.A., Dec) = (%.3f, %.3f), data radius = %.3f deg, model radius: %.3f deg" % |
|
|
|
|
74
|
|
|
(type(self).__name__, self.ra_dec_center[0], self.ra_dec_center[1], |
|
|
|
|
75
|
|
|
self.data_radius.to(u.deg).value, self.model_radius.to(u.deg).value)) |
|
|
|
|
76
|
|
|
|
77
|
|
|
return s |
78
|
|
|
|
79
|
|
|
def display(self): |
80
|
|
|
|
81
|
|
|
print(self) |
|
|
|
|
82
|
|
|
|
83
|
|
|
@property |
84
|
|
|
def ra_dec_center(self): |
|
|
|
|
85
|
|
|
|
86
|
|
|
return self._get_ra_dec() |
87
|
|
|
|
88
|
|
|
@property |
89
|
|
|
def data_radius(self): |
|
|
|
|
90
|
|
|
|
91
|
|
|
return self._data_radius_radians * u.rad |
92
|
|
|
|
93
|
|
|
@property |
94
|
|
|
def model_radius(self): |
|
|
|
|
95
|
|
|
return self._model_radius_radians * u.rad |
96
|
|
|
|
97
|
|
|
def _get_ra_dec(self): |
98
|
|
|
|
99
|
|
|
lon, lat = self._center.get_ra(), self._center.get_dec() |
100
|
|
|
|
101
|
|
|
return lon, lat |
102
|
|
|
|
103
|
|
|
def _get_healpix_vec(self): |
104
|
|
|
|
105
|
|
|
lon, lat = self._get_ra_dec() |
106
|
|
|
|
107
|
|
|
vec = radec_to_vec(lon, lat) |
108
|
|
|
|
109
|
|
|
return vec |
110
|
|
|
|
111
|
|
|
def _active_pixels(self, nside, ordering): |
112
|
|
|
|
113
|
|
|
vec = self._get_healpix_vec() |
114
|
|
|
|
115
|
|
|
nest = ordering is _NESTED |
116
|
|
|
|
117
|
|
|
pixels_inside_cone = hp.query_disc(nside, vec, self._data_radius_radians, inclusive=False, nest=nest) |
118
|
|
|
|
119
|
|
|
return pixels_inside_cone |
120
|
|
|
|
121
|
|
View Code Duplication |
def get_flat_sky_projection(self, pixel_size_deg): |
|
|
|
|
122
|
|
|
|
123
|
|
|
# Decide side for image |
124
|
|
|
|
125
|
|
|
# Compute number of pixels, making sure it is going to be even (by approximating up) |
126
|
|
|
npix_per_side = 2 * int(np.ceil(np.rad2deg(self._model_radius_radians) / pixel_size_deg)) |
127
|
|
|
|
128
|
|
|
# Get lon, lat of center |
129
|
|
|
ra, dec = self._get_ra_dec() |
|
|
|
|
130
|
|
|
|
131
|
|
|
# This gets a list of all RA, Decs for an AIT-projected image of npix_per_size x npix_per_side |
132
|
|
|
flat_sky_proj = FlatSkyProjection(ra, dec, pixel_size_deg, npix_per_side, npix_per_side) |
133
|
|
|
|
134
|
|
|
return flat_sky_proj |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
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.