| 1 |
""" |
| 2 |
Tests for the calculations with various spherical coordinate systems. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
import datetime |
| 12 |
import math |
| 13 |
import re |
| 14 |
|
| 15 |
import numpy |
| 16 |
|
| 17 |
from gavo.helpers import testhelpers |
| 18 |
|
| 19 |
from gavo import stc |
| 20 |
from gavo import utils |
| 21 |
from gavo.stc import spherc |
| 22 |
from gavo.stc import sphermath |
| 23 |
from gavo.stc import times |
| 24 |
from gavo.utils import DEG |
| 25 |
from gavo.utils import mathtricks |
| 26 |
|
| 27 |
import stcgroundtruth |
| 28 |
|
| 29 |
|
| 30 |
class PrecAnglesTest(testhelpers.VerboseTest): |
| 31 |
"""tests for various precessions. |
| 32 |
""" |
| 33 |
def testLieskeConstantsFromJ2000(self): |
| 34 |
for year, zetaL, zL, thetaL in [ |
| 35 |
(1950, -1153.036, -1152.838, -1002.257), |
| 36 |
(1980, -461.232, -461.200, -400.879), |
| 37 |
(2050, 1153.187, 1153.385, 1002.044)]: |
| 38 |
destEp = stc.jYearToDateTime(year) |
| 39 |
zeta, z, theta = spherc.prec_IAU1976(times.dtJ2000, destEp) |
| 40 |
self.assertAlmostEqual(zeta/utils.ARCSEC, zetaL, places=3) |
| 41 |
self.assertAlmostEqual(z/utils.ARCSEC, zL, places=3) |
| 42 |
self.assertAlmostEqual(theta/utils.ARCSEC, thetaL, places=3) |
| 43 |
|
| 44 |
def testLieskeConstantsToJ2000(self): |
| 45 |
for year, zetaL, zL, thetaL in [ |
| 46 |
(1850, 3456.881, 3458.664, 3007.246), |
| 47 |
(1920, 1844.273, 1844.781, 1603.692), |
| 48 |
(1965, 807.055, 807.152, 701.570),]: |
| 49 |
srcEp = stc.bYearToDateTime(year) |
| 50 |
zeta, z, theta = spherc.prec_IAU1976(srcEp, times.dtJ2000) |
| 51 |
self.assertAlmostEqual(zeta/utils.ARCSEC, zetaL, places=3) |
| 52 |
self.assertAlmostEqual(z/utils.ARCSEC, zL, places=3) |
| 53 |
self.assertAlmostEqual(theta/utils.ARCSEC, thetaL, places=3) |
| 54 |
|
| 55 |
|
| 56 |
_b = "BARYCENTER" |
| 57 |
_t = "TOPOCENTER" |
| 58 |
J2000 = times.dtJ2000 |
| 59 |
B1950 = times.dtB1950 |
| 60 |
B1885 = times.bYearToDateTime(1885) |
| 61 |
J1992 = times.bYearToDateTime(1992.25) |
| 62 |
|
| 63 |
class PathTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest): |
| 64 |
"""tests for finding paths in the "virtual graph" of transforms. |
| 65 |
|
| 66 |
This is a rather fragile test due to the whacky heuristics inherent in |
| 67 |
path finding by spherc. |
| 68 |
""" |
| 69 |
|
| 70 |
def _runTest(self, sample): |
| 71 |
fromTriple, toTriple, path0 = sample |
| 72 |
path = tuple(t[1] for t in |
| 73 |
spherc._findTransformsPath(fromTriple, toTriple)) |
| 74 |
self.assertEqual(path0, path) |
| 75 |
|
| 76 |
samples = [ |
| 77 |
(("FK4", B1950, _b), ("FK5", J2000, _b), ( |
| 78 |
('FK5', J2000, 'BARYCENTER'),)), |
| 79 |
(("FK4", B1950, _b), ("FK5", datetime.datetime(2009, 10, 2), _b), ( |
| 80 |
('FK5', J2000, _b), ('FK5', datetime.datetime(2009, 10, 2, 0, 0), _b))), |
| 81 |
(("FK5", B1950, _b), ("FK4", J2000, _b), ( |
| 82 |
('FK5', J2000, _b), ('FK4', B1950, _b), ('FK4', J2000, _b))), |
| 83 |
(("FK4", B1885, _b), ("FK5", J1992, _b), ( |
| 84 |
('FK4', B1950, _b), ('FK5', J2000, _b), |
| 85 |
('FK5', datetime.datetime(1992, 4, 1, 9, 45, 9, 292082), _b))), |
| 86 |
(("FK5", B1885, _b), ("FK5", J1992, _b), ( |
| 87 |
('FK5', J2000, _b), |
| 88 |
('FK5', datetime.datetime(1992, 4, 1, 9, 45, 9, 292082), _b))), |
| 89 |
(("FK5", B1885, _b), ("GALACTIC_II", None, _b), ( |
| 90 |
('FK5', J2000, _b), ('GALACTIC_II', None, _b))), |
| 91 |
(("FK4", B1885, _b), ("GALACTIC_II", None, _b), ( |
| 92 |
('FK4', B1950, _b), ('GALACTIC_II', None, _b))), |
| 93 |
(("FK4", B1885, _t), ("GALACTIC_II", None, _b), ( |
| 94 |
('FK4', B1950, _t), ('GALACTIC_II', None, _t), |
| 95 |
('GALACTIC_II', None, _b))), |
| 96 |
] |
| 97 |
|
| 98 |
|
| 99 |
class SixVectorTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest): |
| 100 |
"""tests for working spherical-to-six-vector full transforms. |
| 101 |
""" |
| 102 |
|
| 103 |
posSamples = [ |
| 104 |
((0,0), ('deg', 'deg')), |
| 105 |
((180,0), ('deg', 'deg')), |
| 106 |
((359.99,0), ('deg', 'deg')), |
| 107 |
((40,22), ('deg', 'deg')), |
| 108 |
((163,-52), ('deg', 'deg')), |
| 109 |
((253,-82), ('deg', 'deg')), |
| 110 |
((0,90), ('deg', 'deg')), |
| 111 |
((0,-90), ('deg', 'deg')), |
| 112 |
((2,10), ('rad', 'deg')), |
| 113 |
((2,10,5), ('rad', 'deg', 'arcsec')), |
| 114 |
((4,-10,5), ('rad', 'deg', 'pc')), |
| 115 |
] |
| 116 |
|
| 117 |
samples = [posSamp+velSamp |
| 118 |
for posSamp in posSamples |
| 119 |
for velSamp in [ |
| 120 |
(None, None, None), |
| 121 |
((0, 0), ("arcsec", "arcsec"), ("yr", "yr")), |
| 122 |
((0.1, 0.1), ("arcsec", "arcsec"), ("yr", "yr")), |
| 123 |
((0.1, -0.1), ("rad", "arcsec"), ("cy", "yr")), |
| 124 |
((-0.1, 0.1), ("rad", "rad"), ("cy", "cy")), |
| 125 |
((0.1, -0.1), ("rad", "rad"), ("cy", "cy")), |
| 126 |
((1, 1, 9), ("rad", "rad", "km"), ("cy", "cy", "s")), |
| 127 |
((1, 1, 0.01), ("rad", "rad", "pc"), ("cy", "cy", "cy"))]] |
| 128 |
|
| 129 |
def assertAlmostEqualST(self, pos1, pos2, vel1, vel2): |
| 130 |
try: |
| 131 |
self.assertEqual(len(pos1), len(pos2)) |
| 132 |
for v1, v2 in zip(pos1, pos2): |
| 133 |
self.assertAlmostEqual(v1, v2) |
| 134 |
if vel1 is None: |
| 135 |
self.assertEqual(vel2, None) |
| 136 |
else: |
| 137 |
self.assertEqual(len(vel1), len(vel2)) |
| 138 |
for v1, v2 in zip(vel1, vel2): # Numerical issues in RV -- fix? |
| 139 |
self.assertAlmostEqual(v1, v2, places=4) |
| 140 |
except AssertionError: |
| 141 |
raise AssertionError("%s, %s != %s, %s"%(pos1, vel1, pos2, vel2)) |
| 142 |
|
| 143 |
def _runTest(self, sample): |
| 144 |
pos, posUnit, vel, velUnitS, velUnitT = sample |
| 145 |
trans = sphermath.SVConverter(pos, posUnit, vel, velUnitS, velUnitT) |
| 146 |
newPos, newVel = trans.from6(trans.to6(pos, vel)) |
| 147 |
self.assertAlmostEqualST(pos, newPos, vel, newVel) |
| 148 |
|
| 149 |
|
| 150 |
class SpherMathTest(testhelpers.VerboseTest): |
| 151 |
"""tests for some basic functionality of sphermath. |
| 152 |
""" |
| 153 |
trans = sphermath.SVConverter((0,0), ('deg', 'deg')) |
| 154 |
|
| 155 |
def testRotateY(self): |
| 156 |
sv = self.trans.to6((0,10)) |
| 157 |
for angle in range(10): |
| 158 |
matrix = spherc.threeToSix(mathtricks.getRotY(angle*utils.DEG)) |
| 159 |
pos, _ = self.trans.from6(numpy.dot(matrix, sv)) |
| 160 |
self.assertAlmostEqual(pos[1], (10+angle)) |
| 161 |
|
| 162 |
def testRotateX(self): |
| 163 |
sv = self.trans.to6((270,10)) |
| 164 |
for angle in range(10): |
| 165 |
matrix = spherc.threeToSix(mathtricks.getRotX(angle*utils.DEG)) |
| 166 |
pos, _ = self.trans.from6(numpy.dot(matrix, sv)) |
| 167 |
self.assertAlmostEqual(pos[1], (10+angle)) |
| 168 |
|
| 169 |
def testRotateZ(self): |
| 170 |
sv = self.trans.to6((10,0)) |
| 171 |
for angle in range(10): |
| 172 |
matrix = spherc.threeToSix(mathtricks.getRotZ(angle*utils.DEG)) |
| 173 |
pos, _ = self.trans.from6(numpy.dot(matrix, sv)) |
| 174 |
self.assertAlmostEqual(pos[0], (10-angle)) |
| 175 |
|
| 176 |
def testSimpleSpher(self): |
| 177 |
for theta, phi in [(0, -90), (20, -89), (180, -45), (270, 0), |
| 178 |
(358, 45), (0, 90)]: |
| 179 |
thetaObs, phiObs = sphermath.cartToSpher( |
| 180 |
sphermath.spherToCart(theta*DEG, phi*DEG)) |
| 181 |
self.assertAlmostEqual(theta, thetaObs/DEG) |
| 182 |
self.assertAlmostEqual(phi, phiObs/DEG) |
| 183 |
|
| 184 |
def testArtificialRotation(self): |
| 185 |
transMat = sphermath.computeTransMatrixFromPole((0, math.pi/4), |
| 186 |
(0, -math.pi/4)) |
| 187 |
def trans(t, p): |
| 188 |
a, b = sphermath.cartToSpher(numpy.dot(transMat, |
| 189 |
sphermath.spherToCart(t*DEG, p*DEG))) |
| 190 |
return a/DEG, b/DEG |
| 191 |
for t, p, a0, b0 in [ |
| 192 |
(0, 90, 180, 45), |
| 193 |
(90, 0, 90, 0), |
| 194 |
(270, 0, 270, 0), |
| 195 |
(45, 45, 106.32494993689, 58.60028519008), # XXX think about this |
| 196 |
]: |
| 197 |
a, b = trans(t, p) |
| 198 |
self.assertAlmostEqual(a0, a) |
| 199 |
self.assertAlmostEqual(b0, b) |
| 200 |
|
| 201 |
def _assertEulerMatch(self, eulers): |
| 202 |
afterRoundtrip = sphermath.getEulerAnglesFromMatrix( |
| 203 |
sphermath.getMatrixFromEulerAngles(*[e*DEG for e in eulers])) |
| 204 |
afterRoundtrip = tuple([v/DEG for v in afterRoundtrip]) |
| 205 |
for i in range(3): |
| 206 |
self.assertAlmostEqual(eulers[i], afterRoundtrip[i], 8, "%s!=%s"%( |
| 207 |
eulers, afterRoundtrip)) |
| 208 |
|
| 209 |
def testEulerFromMatrix(self): |
| 210 |
# Cave degeneration of euler angles; when in doubt, check the matrices. |
| 211 |
self._assertEulerMatch((-88, 8, 3)) |
| 212 |
self._assertEulerMatch((92, 94, 133)) |
| 213 |
self._assertEulerMatch((92, 94, 33)) |
| 214 |
self._assertEulerMatch((92, 4, 33)) |
| 215 |
self._assertEulerMatch((27, 4, 33)) |
| 216 |
self._assertEulerMatch((27, 94, 33)) |
| 217 |
self._assertEulerMatch((-27, 94, 33)) |
| 218 |
self._assertEulerMatch((-27, 94, -33)) |
| 219 |
|
| 220 |
def _assertThreeVecRoundtrip(self, long, lat): |
| 221 |
afterRoundtrip = sphermath.toSpherical( |
| 222 |
sphermath.toThreeVec(long*DEG, lat*DEG)) |
| 223 |
self.assertAlmostEqual(long, afterRoundtrip[0]/DEG) |
| 224 |
self.assertAlmostEqual(lat, afterRoundtrip[1]/DEG) |
| 225 |
|
| 226 |
def testThreeVec(self): |
| 227 |
for lat in [0, -60, 40]: |
| 228 |
self._assertThreeVecRoundtrip(0, lat) |
| 229 |
self._assertThreeVecRoundtrip(90, lat) |
| 230 |
self._assertThreeVecRoundtrip(180, lat) |
| 231 |
self._assertThreeVecRoundtrip(-90, lat) |
| 232 |
self._assertThreeVecRoundtrip(0, 90) |
| 233 |
self._assertThreeVecRoundtrip(0, -90) |
| 234 |
|
| 235 |
|
| 236 |
class MiscTest(testhelpers.VerboseTest): |
| 237 |
def testAllFour(self): |
| 238 |
sysAST = stc.parseSTCS("Time TT unit d\n" |
| 239 |
"Position ICRS BARYCENTER 0 0 unit deg\n" |
| 240 |
"Spectral BARYCENTER unit m\n" |
| 241 |
"Redshift OPTICAL") |
| 242 |
baseAST = stc.parseSTCS("TimeInterval UTC 2000-01-01T00:00:00" |
| 243 |
" 2010-01-01T00:00:00\n" |
| 244 |
"Position GALACTIC 1.2 0.4 unit rad\n" |
| 245 |
"Spectral GEOCENTER 4000 unit Angstrom\n" |
| 246 |
"Redshift RADIO 4") |
| 247 |
newAST = stc.conformTo(baseAST, sysAST) |
| 248 |
self.assertEqual(newAST.timeAs[0].upperLimit.second, 1) |
| 249 |
self.assertEqual(newAST.timeAs[0].frame.timeScale, "TT") |
| 250 |
self.assertEqual(int(newAST.place.getValues()[0]), 275) |
| 251 |
self.assertEqual(newAST.place.unit, ("deg", "deg")) |
| 252 |
self.assertEqual(int(newAST.freq.getValues()[0]*1e7), 4) |
| 253 |
|
| 254 |
|
| 255 |
class ToGalacticTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest): |
| 256 |
_toSystem = stc.parseSTCS("Position J2000") |
| 257 |
|
| 258 |
def _runTest(self, sample): |
| 259 |
fromCoo, (ares, dres) = sample |
| 260 |
ast = stc.parseSTCS("Position GALACTIC %.11f %.11f"%fromCoo) |
| 261 |
st = sphermath.SVConverter.fromSTC(ast) |
| 262 |
sv = st.to6(ast.place.value) |
| 263 |
sv = numpy.dot(spherc._b1950ToGalMatrix, sv) |
| 264 |
pos, vel = st.from6(sv) |
| 265 |
a, d = pos |
| 266 |
self.assertAlmostEqual(ares, a, places=6) |
| 267 |
self.assertAlmostEqual(dres, d, places=6) |
| 268 |
|
| 269 |
samples = [ |
| 270 |
((265.6108440311, -28.9167903484), (0,0)), |
| 271 |
((129.5660460691, -19.7089490512), (243.78, 13.2)), |
| 272 |
] |
| 273 |
|
| 274 |
|
| 275 |
class SuperGalacticTest(testhelpers.VerboseTest): |
| 276 |
def testGalToSG(self): |
| 277 |
baseAST = stc.parseSTCS("Position GALACTIC 23.54 -45.32") |
| 278 |
sysAST = stc.parseSTCS("Position SUPER_GALACTIC") |
| 279 |
res = stc.conformTo(baseAST, sysAST) |
| 280 |
p = res.place.value |
| 281 |
self.assertAlmostEqual(p[0], 249.92902823) |
| 282 |
self.assertAlmostEqual(p[1], 34.12639039) |
| 283 |
|
| 284 |
def testSGToB1950(self): |
| 285 |
baseAST = stc.parseSTCS("Position SUPER_GALACTIC 23.54 -45.32") |
| 286 |
sysAST = stc.parseSTCS("Position B1950") |
| 287 |
res = stc.conformTo(baseAST, sysAST) |
| 288 |
p = res.place.value |
| 289 |
self.assertAlmostEqual(p[0], 100.62487077) |
| 290 |
self.assertAlmostEqual(p[1], 28.96712491) |
| 291 |
|
| 292 |
|
| 293 |
class PositionOnlyTestBase(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest): |
| 294 |
"""A base class for makestctruth-based tests involving positions only. |
| 295 |
""" |
| 296 |
|
| 297 |
def _runTest(self, sample): |
| 298 |
(ra, dec), (ra1, dec1) = sample |
| 299 |
ast = self.srcSystem.change( |
| 300 |
place=self.srcSystem.place.change(value=(ra,dec))) |
| 301 |
res = stc.conformTo(ast, self.destSystem) |
| 302 |
self.assertAlmostEqual(res.place.value[0], ra1) |
| 303 |
self.assertAlmostEqual(res.place.value[1], dec1) |
| 304 |
|
| 305 |
|
| 306 |
class SixVectorTestBase(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest): |
| 307 |
"""A base class for makestctruth-based tests involving full 6-vectors. |
| 308 |
""" |
| 309 |
|
| 310 |
def _runTest(self, sample): |
| 311 |
(ra, dec, prl, pma, pmd, rv |
| 312 |
), (ra1, dec1, prl1, pma1, pmd1, rv1) = sample |
| 313 |
ast = self.srcSystem.change( |
| 314 |
place=self.srcSystem.place.change(value=(ra,dec,prl)), |
| 315 |
velocity=self.srcSystem.velocity.change(value=(pma, pmd, rv))) |
| 316 |
res = stc.conformTo(ast, self.destSystem, slaComp=True) |
| 317 |
places = 6 |
| 318 |
self.assertAlmostEqual(res.place.value[0], ra1, places=places) |
| 319 |
self.assertAlmostEqual(res.place.value[1], dec1, places=places) |
| 320 |
self.assertAlmostEqual(res.place.value[2], prl1, places=places) |
| 321 |
self.assertAlmostEqual(res.velocity.value[0], pma1, places=places-3) |
| 322 |
self.assertAlmostEqual(res.velocity.value[1], pmd1, places=places-3) |
| 323 |
self.assertAlmostEqual(res.velocity.value[2], rv1, places=places-3) |
| 324 |
|
| 325 |
|
| 326 |
class SimpleTrafoTest(testhelpers.VerboseTest): |
| 327 |
def testBasic(self): |
| 328 |
conv = stc.getSimple2Converter( |
| 329 |
stc.parseSTCS("Position GALACTIC"), |
| 330 |
stc.parseSTCS("Position ICRS")) |
| 331 |
ra, dec = conv(4, 40) |
| 332 |
self.assertAlmostEqual(ra, 234.99533124940731) |
| 333 |
self.assertAlmostEqual(dec, -2.1043398500407746) |
| 334 |
|
| 335 |
|
| 336 |
# This mess creates tests from the samples in stcgroundtruth; |
| 337 |
# see the globals in there; the tests are called Test<varname> |
| 338 |
for sampleName in dir(stcgroundtruth): |
| 339 |
if not re.match("[a-zA-Z]", sampleName): |
| 340 |
continue |
| 341 |
_samples, _srcSystemSTC, _destSystemSTC = getattr(stcgroundtruth, sampleName) |
| 342 |
if sampleName.startswith("Six"): |
| 343 |
base = SixVectorTestBase |
| 344 |
else: |
| 345 |
base = PositionOnlyTestBase |
| 346 |
class GroundTruthTest(base): |
| 347 |
samples = _samples |
| 348 |
destSystem = stc.parseSTCS(_destSystemSTC) |
| 349 |
srcSystem = stc.parseSTCS(_srcSystemSTC) |
| 350 |
globals()["Test"+sampleName] = GroundTruthTest |
| 351 |
GroundTruthTest.__name__= "Test"+sampleName |
| 352 |
del GroundTruthTest |
| 353 |
del base |
| 354 |
|
| 355 |
|
| 356 |
if __name__=="__main__": |
| 357 |
testhelpers.main(PositionOnlyTestBase) |