|
23 | 23 | from functools import partial |
24 | 24 |
|
25 | 25 | import coord as _coord |
| 26 | +import equinox |
26 | 27 | import galsim as _galsim |
27 | 28 | import jax |
28 | 29 | import jax.numpy as jnp |
| 30 | +import numpy as np |
29 | 31 | from jax.tree_util import register_pytree_node_class |
30 | 32 |
|
31 | 33 | from jax_galsim.angle import Angle, _Angle, arcsec, degrees, radians |
@@ -74,6 +76,16 @@ def __init__(self, ra, dec=None): |
74 | 76 | elif not isinstance(dec, Angle): |
75 | 77 | raise TypeError("dec must be a galsim.Angle") |
76 | 78 | else: |
| 79 | + if isinstance(dec._rad, (float, int)): |
| 80 | + if dec._rad < -np.pi / 2 or dec._rad > np.pi / 2: |
| 81 | + raise ValueError("dec must be between -90 deg and +90 deg.") |
| 82 | + else: |
| 83 | + dec._rad = equinox.error_if( |
| 84 | + jnp.array(dec._rad), |
| 85 | + jnp.any((dec._rad < -jnp.pi / 2) | (dec._rad > jnp.pi / 2)), |
| 86 | + "dec must be between -90 deg and +90 deg.", |
| 87 | + ) |
| 88 | + |
77 | 89 | # Normal case |
78 | 90 | self._ra = ra |
79 | 91 | self._dec = dec |
@@ -121,15 +133,14 @@ def get_xyz(self): |
121 | 133 |
|
122 | 134 | @staticmethod |
123 | 135 | @jax.jit |
124 | | - @implements( |
125 | | - _galsim.celestial.CelestialCoord.from_xyz, |
126 | | - lax_description=( |
127 | | - "The JAX version of this static method does not check that the norm of the input " |
128 | | - "vector is non-zero." |
129 | | - ), |
130 | | - ) |
| 136 | + @implements(_galsim.celestial.CelestialCoord.from_xyz) |
131 | 137 | def from_xyz(x, y, z): |
132 | 138 | norm = jnp.sqrt(x * x + y * y + z * z) |
| 139 | + norm = equinox.error_if( |
| 140 | + norm, |
| 141 | + jnp.any(norm == 0), |
| 142 | + "CelestialCoord for position (0,0,0) is undefined.", |
| 143 | + ) |
133 | 144 | ret = CelestialCoord.__new__(CelestialCoord) |
134 | 145 | ret._x = x / norm |
135 | 146 | ret._y = y / norm |
@@ -236,13 +247,7 @@ def distanceTo(self, coord2): |
236 | 247 |
|
237 | 248 | return _Angle(theta) |
238 | 249 |
|
239 | | - @implements( |
240 | | - _galsim.celestial.CelestialCoord.greatCirclePoint, |
241 | | - lax_description=( |
242 | | - "The JAX version of this method does not check that coord2 defines a unique great " |
243 | | - "circle with the current coord at angle theta." |
244 | | - ), |
245 | | - ) |
| 250 | + @implements(_galsim.celestial.CelestialCoord.greatCirclePoint) |
246 | 251 | @jax.jit |
247 | 252 | def greatCirclePoint(self, coord2, theta): |
248 | 253 | aux = self._get_aux() |
@@ -280,8 +285,11 @@ def greatCirclePoint(self, coord2, theta): |
280 | 285 |
|
281 | 286 | # Normalize |
282 | 287 | wr = (wx**2 + wy**2 + wz**2) ** 0.5 |
283 | | - # if wr == 0.: |
284 | | - # raise ValueError("coord2 does not define a unique great circle with self.") |
| 288 | + wr = equinox.error_if( |
| 289 | + wr, |
| 290 | + jnp.any(wr == 0), |
| 291 | + "coord2 does not define a unique great circle with self.", |
| 292 | + ) |
285 | 293 | wx /= wr |
286 | 294 | wy /= wr |
287 | 295 | wz /= wr |
|
0 commit comments