Debian Codegraph @

tests/stctest.py

raw
1 """
2 Generic tests for the STC library.
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 re
13 import math
14
15 from gavo.helpers import testhelpers
16
17 from gavo import stc
18 from gavo import utils
19 from gavo.stc import cli
20 from gavo.stc import dm
21 from gavo.stc import geo
22 from gavo.stc import stcs
23 from gavo.stc import stcsast
24
25
26 class CoordSysTest(testhelpers.VerboseTest):
27 def testBasic(self):
28 cs = dm.CoordSys(name="testCase", ucd="test;useless")
29 self.assertEqual(cs.timeFrame, None)
30 self.assertEqual(cs.ucd, "test;useless")
31 self.assertEqual(cs.name, "testCase")
32
33 def testBasicRaises(self):
34 self.assertRaises(TypeError, dm.CoordSys, x=8)
35
36 def testFromSTCS(self):
37 cst = stcs.getCST("TimeInterval TT BARYCENTER"
38 " PositionInterval FK5 TOPOCENTER"
39 " SpectralInterval GEOCENTER"
40 " RedshiftInterval HELIOCENTER VELOCITY")
41 cs = stcsast.getCoordSys(cst)[1]
42 self.assertEqual(cs.redshiftFrame.dopplerDef, "OPTICAL")
43 self.assertEqual(cs.spectralFrame.refPos.standardOrigin, "GEOCENTER")
44 self.assertEqual(cs.spaceFrame.flavor, "SPHERICAL")
45 self.assertEqual(cs.spaceFrame.nDim, 2)
46 self.assertEqual(cs.spaceFrame.refFrame, "FK5")
47 self.assertEqual(cs.timeFrame.timeScale, "TT")
48
49 def testEquinoxes(self):
50 ast = stcsast.parseSTCS("Position FK4 B1975 30 30")
51 self.assertEqual(ast.astroSystem.spaceFrame.equinox, "B1975.0")
52 self.assertEqual(ast.astroSystem.spaceFrame.getEquinox(),
53 datetime.datetime(1974, 12, 31, 23, 28, 56, 228856))
54
55 def testPlanetaryEphemeris(self):
56 ast = stcsast.parseSTCS("Time TT TOPOCENTER JPL-DE200")
57 self.assertEqual(ast.astroSystem.timeFrame.refPos.planetaryEphemeris,
58 "JPL-DE200")
59
60
61 class OtherCoordTest(testhelpers.VerboseTest):
62 def testSimpleTime(self):
63 ast = stcsast.parseSTCS("Time TT 2000-12-20T23:02:12 unit yr Error 2")
64 self.assertEqual(ast.time.frame.timeScale, "TT")
65 self.assertEqual(ast.time.error.values, (2.0,))
66 self.assertEqual(ast.time.value,
67 datetime.datetime(2000, 12, 20, 23, 2, 12))
68
69 def testSimpleSpectral(self):
70 ast = stcsast.parseSTCS("Spectral BARYCENTER 23 Resolution 0.25 0.5")
71 self.assertEqual(ast.freq.frame.refPos.standardOrigin, "BARYCENTER")
72 self.assertEqual(ast.freq.value, 23.)
73 self.assertEqual(ast.freq.resolution.values, (0.25, 0.5))
74
75 def testSimpleRedshift(self):
76 ast = stcsast.parseSTCS("Redshift BARYCENTER 2 unit km/s")
77 self.assertEqual(ast.redshift.frame.refPos.standardOrigin,
78 "BARYCENTER")
79 self.assertEqual(ast.redshift.value, 2.)
80
81 def testComplexRedshift(self):
82 ast = stcsast.parseSTCS("Redshift BARYCENTER REDSHIFT"
83 " RADIO 2 Error 0 0.125")
84 self.assertEqual(ast.redshift.error.values, (0, 0.125))
85 self.assertEqual(ast.redshift.frame.type, "REDSHIFT")
86 self.assertEqual(ast.redshift.frame.dopplerDef, "RADIO")
87
88 def testRaising(self):
89 self.assertRaises(stc.STCValueError, stcsast.parseSTCS,
90 "Time TT Error 1 2 3")
91 self.assertRaises(stc.STCValueError, stcsast.parseSTCS,
92 "Spectral BARYCENTER 23 Resolution 0.25 0.5 2.5")
93
94
95 class SpaceCoordTest(testhelpers.VerboseTest):
96 def testSimple(self):
97 ast = stcsast.parseSTCS("Position FK5 TOPOCENTER 2 4.25 unit deg"
98 " PixSize 4.5 3.75")
99 self.assertEqual(ast.place.frame.flavor, "SPHERICAL")
100 self.assertEqual(ast.place.frame.nDim, 2)
101 self.assertEqual(ast.place.value, (2., 4.25))
102 self.assertEqual(ast.place.unit, ("deg", "deg"))
103 self.assertEqual(ast.place.pixSize.values, ((4.5, 3.75),))
104
105 def testPixSizeRange(self):
106 ast = stcsast.parseSTCS("Position FK5 TOPOCENTER 2 4.25 unit deg"
107 " PixSize 4.5 3.75 1 5")
108 self.assertEqual(ast.place.pixSize.values, ((4.5, 3.75), (1., 5.)))
109
110 def testSizeRange(self):
111 ast = stcsast.parseSTCS("Position FK5 TOPOCENTER 2 4.25 unit deg"
112 " Size 4.5 3.75 1 5")
113 self.assertEqual(ast.place.size.values, ((4.5, 3.75), (1., 5.)))
114
115 def testRaises(self):
116 self.assertRaises(stc.STCSParseError, stcsast.parseSTCS,
117 "Position FK5 TOPOCENTER 2 4.25 unit deg PixSize 4.5 3.75 2")
118
119
120 class OtherCoordIntervalTest(testhelpers.VerboseTest):
121 def testEmptyInterval(self):
122 ast = stcsast.parseSTCS("TimeInterval TOPOCENTER unit s")
123 self.assertEqual(ast.time.frame.refPos.standardOrigin,
124 "TOPOCENTER")
125
126 def testHalfOpenInterval(self):
127 ast = stcsast.parseSTCS("TimeInterval MJD 2000")
128 self.assertEqual(ast.timeAs[0].upperLimit, None)
129 self.assertEqual(ast.timeAs[0].lowerLimit,
130 datetime.datetime(1864, 5, 9, 0, 0, 0, 1))
131
132 def testOneInterval(self):
133 ast = stcsast.parseSTCS("TimeInterval 2000-02-02 2000-02-02T13:20:33")
134 self.assertEqual(ast.timeAs[0].upperLimit,
135 datetime.datetime(2000, 2, 2, 13, 20, 33))
136 self.assertEqual(ast.timeAs[0].lowerLimit,
137 datetime.datetime(2000, 2, 2, 0, 0))
138
139 def _testOneAndAHalfInterval(self):
140 ast = stcsast.parseSTCS("TimeInterval 2000-02-02 2000-02-02T13:20:33"
141 " MJD 80002")
142 self.assertEqual(ast.timeAs[1].upperLimit, None)
143 self.assertEqual(ast.timeAs[1].lowerLimit,
144 datetime.datetime(2077, 11, 30, 0, 0, 0, 4))
145
146 def testTimeWithPosition(self):
147 ast = stcsast.parseSTCS("TimeInterval 2000-02-02 2000-02-02T13:20:33"
148 " Time 2000-02-02T10:34:03.25")
149 self.assertEqual(len(ast.timeAs), 1)
150 self.assertEqual(ast.time.value,
151 datetime.datetime(2000, 2, 2, 10, 34, 3, 250000))
152
153 def testSpecInterval(self):
154 ast = stcsast.parseSTCS("SpectralInterval 23 45 unit Hz")
155 freqA = testhelpers.pickSingle(ast.freqAs)
156 self.assertEqual(freqA.frame.refPos.standardOrigin, None)
157 self.assertEqual(freqA.lowerLimit, 23.0)
158 self.assertEqual(freqA.upperLimit, 45.0)
159
160 def testRedshiftInterval(self):
161 ast = stcsast.parseSTCS("RedshiftInterval VELOCITY 2 4")
162 redshiftA = testhelpers.pickSingle(ast.redshiftAs)
163 self.assertEqual(redshiftA.frame.type,
164 "VELOCITY")
165 self.assertEqual(redshiftA.lowerLimit, 2.0)
166 self.assertEqual(redshiftA.upperLimit, 4.0)
167 self.assertEqual(ast.redshift.unit, "km")
168 self.assertEqual(ast.redshift.velTimeUnit, "s")
169
170 def testStartTime(self):
171 ast = stcsast.parseSTCS("StartTime TT MJD24000.5")
172 timeA = testhelpers.pickSingle(ast.timeAs)
173 self.assertEqual(timeA.upperLimit, None)
174 self.assertEqual(timeA.lowerLimit,
175 datetime.datetime(1924, 8, 3, 12, 0))
176
177 def testStopTime(self):
178 ast = stcsast.parseSTCS("StopTime TT MJD24000.5")
179 timeA = testhelpers.pickSingle(ast.timeAs)
180 self.assertEqual(timeA.lowerLimit, None)
181 self.assertEqual(timeA.upperLimit,
182 datetime.datetime(1924, 8, 3, 12, 0))
183
184
185 class SpaceCoordIntervalTest(testhelpers.VerboseTest):
186 def testSimple2D(self):
187 ast = stcsast.parseSTCS("PositionInterval ICRS 12.25 23.75 13.5 25.0")
188 area = testhelpers.pickSingle(ast.areas)
189 self.assertEqual(area.frame.refPos.standardOrigin, None)
190 self.assertEqual(area.lowerLimit, (12.25, 23.75))
191 self.assertEqual(area.upperLimit, (13.5, 25.0))
192 self.assertTrue(ast.place is not None)
193
194 def test2DWithError(self):
195 ast = stcsast.parseSTCS("PositionInterval ICRS 12.25 23.75 13.5 25.0 Error 1 2")
196 self.assertEqual(len(ast.areas), 1)
197 self.assertEqual(ast.place.error.values, ((1., 2.),))
198
199 def testSimple3D(self):
200 ast = stcsast.parseSTCS("PositionInterval ICRS CART3 1 2 3")
201 area = testhelpers.pickSingle(ast.areas)
202 self.assertEqual(area.lowerLimit, (1.0, 2.0, 3.0))
203 self.assertEqual(area.upperLimit, None)
204
205 def test3DWithError(self):
206 ast = stcsast.parseSTCS("PositionInterval ICRS CART3 1 2 3 4 5 6"
207 " Error 0.25 0.5 0.75")
208 area = testhelpers.pickSingle(ast.areas)
209 self.assertEqual(area.lowerLimit, (1.0, 2.0, 3.0))
210 self.assertEqual(area.upperLimit, (4.0, 5.0, 6.0))
211 self.assertEqual(ast.place.error.values, ((.25, .5, .75),))
212
213 def testWithPosition(self):
214 ast = stcsast.parseSTCS("PositionInterval ICRS 12.25 23.75 13.5 25.0"
215 " Position 12 24")
216 self.assertEqual(len(ast.areas), 1)
217 self.assertEqual(ast.place.value, (12., 24.))
218
219 def test1D(self):
220 ast = stcsast.parseSTCS("PositionInterval ICRS CART1 1"
221 " Error 0.25 0.5")
222 self.assertEqual(len(ast.areas), 1)
223 self.assertEqual(ast.areas[0].lowerLimit, (1.0,))
224 self.assertEqual(ast.place.error.values[0], (0.25,))
225 self.assertEqual(ast.place.error.values[1], (0.5,))
226
227 def testFunkyUnit(self):
228 ast = stcsast.parseSTCS("PositionInterval ICRS CART2 1 2 3 4"
229 " unit m kpc Error 0.25 0.5")
230 self.assertEqual(len(ast.areas), 1)
231 self.assertEqual(ast.place.unit, ("m", "kpc"))
232
233 def testBadPositionRaises(self):
234 self.assertRaises(stc.STCValueError, stcsast.parseSTCS,
235 "PositionInterval ICRS 12.25 23.75 13.5 25.0 Position 12 24 3 4")
236
237
238 class GeometryTest(testhelpers.VerboseTest):
239 def testAllSky(self):
240 ast = stcsast.parseSTCS("AllSky ICRS")
241 self.assertTrue(isinstance(ast.areas[0], dm.AllSky))
242 self.assertEqual(ast.areas[0].frame.refFrame, 'ICRS')
243
244 def testAllSkyRaises(self):
245 self.assertRaises(stc.STCSParseError, stcsast.parseSTCS,
246 "AllSky ICRS 12")
247
248 def testCircle2D(self):
249 ast = stcsast.parseSTCS("Circle ICRS CART2 12 13 1.5")
250 c = ast.areas[0]
251 self.assertTrue(isinstance(c, dm.Circle))
252 self.assertEqual(c.frame.refFrame, 'ICRS')
253 self.assertEqual(c.center, (12., 13.))
254 self.assertEqual(c.radius, 1.5)
255
256 def testCircle3D(self):
257 ast = stcsast.parseSTCS("Circle FK5 CART3 12 13 15 1.5")
258 c = ast.areas[0]
259 self.assertTrue(isinstance(c, dm.Circle))
260 self.assertEqual(c.frame.refFrame, 'FK5')
261 self.assertEqual(c.center, (12., 13., 15.))
262 self.assertEqual(c.radius, 1.5)
263
264 def testCircleRaises(self):
265 self.assertRaises(stc.STCSParseError, stcsast.parseSTCS,
266 "Circle ICRS 12")
267
268 def testEllipse(self):
269 ast = stcsast.parseSTCS("Ellipse ICRS 12 13 1.5 0.75 0")
270 c = ast.areas[0]
271 self.assertTrue(isinstance(c, dm.Ellipse))
272 self.assertEqual(c.center, (12., 13.))
273 self.assertEqual(c.smajAxis, 1.5)
274 self.assertEqual(c.sminAxis, 0.75)
275 self.assertEqual(c.posAngle, 0)
276
277 def testEllipseRaises(self):
278 self.assertRaises(stc.STCSParseError, stcsast.parseSTCS,
279 "Ellipse ICRS 12 13 14")
280
281 def testBox(self):
282 ast = stcsast.parseSTCS("Box ICRS 12 13 1.5 0.75")
283 c = ast.areas[0]
284 self.assertTrue(isinstance(c, dm.Box))
285 self.assertEqual(c.center, (12., 13.))
286 self.assertEqual(c.boxsize, (1.5, 0.75))
287
288 def testBoxRaises(self):
289 self.assertRaises(stc.STCSParseError, stcsast.parseSTCS,
290 "Box ICRS 12 13 1.5 0.75 0")
291
292 def testPolygon(self):
293 ast = stcsast.parseSTCS("Polygon ICRS 12 13 15 14 11 10")
294 c = ast.areas[0]
295 self.assertTrue(isinstance(c, dm.Polygon))
296 self.assertEqual(c.vertices[0], (12., 13.))
297 self.assertEqual(c.vertices[1], (15., 14.))
298 self.assertEqual(c.vertices[2], (11., 10.))
299 self.assertEqual(len(c.vertices), 3)
300
301 def testPolygonRaises(self):
302 self.assertRaises(stc.STCSParseError, stcsast.parseSTCS,
303 "Polygon ICRS 12 13 1.5 0.75 0")
304
305 def testConvex(self):
306 ast = stcsast.parseSTCS("Convex ICRS 12 13 15 0.25 14 11 10 -0.25")
307 c = ast.areas[0]
308 self.assertEqual(c.vectors[0], (12., 13., 15., 0.25))
309 self.assertEqual(c.vectors[1], (14., 11., 10., -0.25))
310 self.assertEqual(len(c.vectors), 2)
311
312
313 class UnitParseTest(testhelpers.VerboseTest):
314 """tests for parsing units.
315 """
316 def test2DSpatial(self):
317 ast = stcsast.parseSTCS("Position ICRS 1 200 unit deg")
318 self.assertEqual(ast.place.unit, ("deg", "deg"))
319
320 def test3DGeo(self):
321 ast = stcsast.parseSTCS("Position GEO_D SPHER3 49.39861 8.72083 560")
322 self.assertEqual(ast.place.unit, ("deg", "deg", "m"))
323
324 def test1DSpatialInterval(self):
325 ast = stcsast.parseSTCS("PositionInterval ICRS CART1 1 unit km")
326 self.assertEqual(ast.place.unit, ("km",))
327
328 def test2DSpatialInterval(self):
329 ast = stcsast.parseSTCS("PositionInterval ICRS 1 2 2 3 unit rad")
330 self.assertEqual(ast.place.unit, ("rad", "rad"))
331
332 def testSpectral(self):
333 ast = stcsast.parseSTCS("SpectralInterval 1 2 unit nm")
334 self.assertEqual(ast.freq.unit, "nm")
335
336 def testTime(self):
337 ast = stcsast.parseSTCS("TimeInterval MJD56 MJD57 unit a")
338 self.assertEqual(ast.time.unit, "a")
339
340 def testRedshift(self):
341 ast = stcsast.parseSTCS("RedshiftInterval 1000 2000 unit km/s")
342 self.assertEqual(ast.redshift.unit, "km")
343 self.assertEqual(ast.redshift.velTimeUnit, "s")
344
345
346 class VelocityTest(testhelpers.VerboseTest):
347 """tests for velocity handling.
348 """
349 def testTrivial(self):
350 ast = stcsast.parseSTCS("Position ICRS VelocityInterval 1 2")
351 self.assertTrue(ast.velocity.frame is ast.astroSystem.spaceFrame)
352 self.assertEqual(ast.velocity.unit, ("m", "m"))
353 self.assertEqual(ast.velocity.velTimeUnit, ("s", "s"))
354 self.assertEqual(ast.velocityAs[0].lowerLimit, (1., 2.0))
355 self.assertEqual(ast.velocityAs[0].upperLimit, None)
356
357 def testSimple(self):
358 ast = stcsast.parseSTCS("Position ICRS VelocityInterval Velocity 1 2"
359 " unit deg/cy")
360 self.assertEqual(ast.velocity.unit, ("deg", "deg"))
361 self.assertEqual(ast.velocity.velTimeUnit, ("cy", "cy"))
362 self.assertEqual(ast.velocity.value, (1., 2.))
363 self.assertEqual(len(ast.velocityAs), 0)
364
365 def testComplex(self):
366 ast = stcsast.parseSTCS("Position ICRS VelocityInterval -0.125 2.5"
367 " 0.125 3 unit deg/cy Error 0.125 0.25 Resolution 1.5 1"
368 " PixSize 0.5 0.5")
369 self.assertEqual(ast.velocityAs[0].lowerLimit, (-0.125, 2.5))
370 self.assertEqual(ast.velocityAs[0].upperLimit, (0.125, 3))
371 self.assertEqual(ast.velocity.error.values[0], (0.125, 0.25))
372 self.assertEqual(ast.velocity.resolution.values[0], (1.5, 1))
373 self.assertEqual(ast.velocity.pixSize.radii[0], 0.5)
374
375
376 class BinarizationTest(testhelpers.VerboseTest):
377 def testSimpleBinarization(self):
378 ast = stc.parseSTCS("Union ICRS (Circle 10 10 1"
379 " Circle 10 11 1 Circle 11 10 1)").binarize()
380 self.assertTrue(isinstance(ast.areas[0], dm.Union))
381 left, right = ast.areas[0].children
382 self.assertTrue(isinstance(right, dm.Union))
383 self.assertTrue(isinstance(left, dm.Circle))
384
385 def testComplexBinarization(self):
386 ast = stc.parseSTCS("Union ICRS (Difference (Box 1 2 3 4 Circle 10 10 1)"
387 " Circle 10 11 1"
388 " Intersection (Circle 11 10 1 Circle 12 10 1 Box 11 11 3 3 ))"
389 ).binarize()
390 self.assertTrue(isinstance(ast.areas[0].children[1].children[1].
391 children[1], dm.Intersection))
392
393 def testDebinarization(self):
394 ast0 = stc.parseSTCS("Union ICRS (Difference (Box 1 2 3 4 Circle 10 10 1)"
395 " Circle 10 11 1"
396 " Intersection (Circle 11 10 1 Circle 12 10 1 Box 11 11 3 3 ))")
397 ast1 = ast0.binarize().debinarize()
398 self.assertEqual(ast0, ast1)
399
400
401 class STCSRoundtripTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
402 """tests for STC-S strings going though parsing, STC-X generation, STC-X parsing, and STC-Sgeneration largely unscathed.
403 """
404
405 def _runTest(self, item):
406 stcsIn, stcsOut = item
407 ast = stc.parseSTCS(stcsIn)
408 stcx = stc.getSTCXProfile(ast)
409 ast = stc.parseSTCX(stcx)[0][1]
410 self.assertEqual(stc.getSTCS(ast), stcsOut)
411
412 samples = [
413 # 0
414 ("Position ICRS 12.75 14.25", "Position ICRS 12.75 14.25"),
415 ("Position ICRS 12000.3 14000 unit arcsec Error 0.1 0.14 Resolution 0.5 0.55"
416 " Size 1 1.1 4.8 2.3 PixSize 0.2 0.2",
417 "Position ICRS 12000.3 14000.0 unit arcsec Error 0.1 0.14 Resolution 0.5 0.55"
418 " Size 1.0 1.1 4.8 2.3 PixSize 0.2 0.2"),
419 ("PositionInterval UNKNOWNFrame CART1 1 2 unit mm",
420 "PositionInterval UNKNOWNFrame CART1 1.0 2.0 unit mm"),
421 ("PositionInterval ICRS Epoch J1900.3 12 13 14 15",
422 "PositionInterval ICRS Epoch J1900.3 12.0 13.0 14.0 15.0"),
423 ("PositionInterval ICRS 12 13 14 15 Size 1 1.5 1.75 2",
424 "PositionInterval ICRS 12.0 13.0 14.0 15.0 Size 1.0 1.5 1.75 2.0"),
425 # 5
426 ("PositionInterval ECLIPTIC CART3 12 13 10 14 15 9 PixSize 1 1 1",
427 "PositionInterval ECLIPTIC CART3 12.0 13.0 10.0 14.0 15.0 9.0 PixSize 1.0 1.0 1.0"),
428 ("Circle ICRS Epoch J1999.9 12 13 1 unit arcsec",
429 "Circle ICRS Epoch J1999.9 12.0 13.0 1.0 unit arcsec"),
430 ("Circle ICRS 12 13 1 unit arcsec Resolution 1 2 PixSize 2 1",
431 "Circle ICRS 12.0 13.0 1.0 unit arcsec Resolution 1.0 2.0 PixSize 2.0 1.0"),
432 ("Ellipse ICRS 12 13 1 0.75 0 Resolution 1 1",
433 "Ellipse ICRS 12.0 13.0 1.0 0.75 0.0 Resolution 1.0 1.0"),
434 ("Box fillfactor 0.125 ICRS 70 190 23 18",
435 "Box fillfactor 0.125 ICRS 70.0 190.0 23.0 18.0"),
436 # 10
437 ("Polygon ICRS 70 190 23 18 12 45 30 -10",
438 "Polygon ICRS 70.0 190.0 23.0 18.0 12.0 45.0 30.0 -10.0"),
439 ("Convex FK5 J1990 70 190 23 0.125 12 45 30 -0.25",
440 "Convex FK5 J1990.0 70.0 190.0 23.0 0.125 12.0 45.0 30.0 -0.25 unit deg"),
441 ("TimeInterval TT 2009-03-10T09:56:10.015625"
442 " SpectralInterval 1e10 1e11 unit Hz"
443 " RedshiftInterval VELOCITY 1000 7500 unit km/s",
444 "StartTime TT 2009-03-10T09:56:10.015625\nSpectralInterval"
445 " 10000000000.0 100000000000.0\nRedshiftInterval VELOCITY 1000.0 7500.0"),
446 ("Time TT 2009-03-10T09:56:10.015625",
447 "Time TT 2009-03-10T09:56:10.015625"),
448 ("Time TDT 2009-03-10T09:56:10.015625 unit s"
449 " Error 0.0001 0.0002 Resolution 0.0001 PixSize 2",
450 "Time TDT 2009-03-10T09:56:10.015625"
451 " Error 0.0001 0.0002 Resolution 0.0001 PixSize 2.0"),
452 # 15
453 ("TimeInterval TDT 2009-03-10T09:56:10.015625 unit s"
454 " Error 0.0001 0.0002 Resolution 0.0001 PixSize 2",
455 "StartTime TDT 2009-03-10T09:56:10.015625"
456 " Error 0.0001 0.0002 Resolution 0.0001 PixSize 2.0"),
457 ("Spectral NEPTUNE 12 unit Angstrom Error 4 3"
458 " Redshift TOPOCENTER REDSHIFT RELATIVISTIC 0.1",
459 "Spectral NEPTUNE 12.0 unit Angstrom Error 4.0 3.0\nRedshift"
460 " TOPOCENTER RELATIVISTIC 0.1"),
461 ('Position ICRS Epoch J1999.9 VelocityInterval Velocity 1 2'
462 ' unit pc/cy Error 0.25 0.5',
463 'Position ICRS Epoch J1999.9 Velocity 1.0 2.0'
464 ' unit pc/cy Error 0.25 0.5'),
465 ("Position ICRS VelocityInterval 0.25 0.5 2 3 Velocity 1 2",
466 "Position ICRS VelocityInterval 0.25 0.5 2.0 3.0 Velocity 1.0 2.0"),
467 ("Position ICRS CART1 1 unit Mpc VelocityInterval 0.25 0.5 Velocity 2"
468 " unit pc/cy",
469 "Position ICRS CART1 1.0 unit Mpc VelocityInterval 0.25 0.5"
470 " Velocity 2.0 unit pc/cy"),
471 # 20
472 ("PositionInterval ICRS CART3 1.0 2.0 3.0 4.0 5.0 6.0 VelocityInterval"
473 " 0 0 0 10 10 10 Velocity 2 3 4 unit pc/cy",
474 "PositionInterval ICRS CART3 1.0 2.0 3.0 4.0 5.0 6.0 VelocityInterval"
475 " 0.0 0.0 0.0 10.0 10.0 10.0 Velocity 2.0 3.0 4.0 unit pc/cy"),
476 ("Circle ICRS 1 2 3 unit arcsec VelocityInterval Velocity 1 2",
477 "Circle ICRS 1.0 2.0 3.0 unit arcsec Velocity 1.0 2.0"),
478 ("Redshift 2",
479 "Redshift 2.0"),
480 ("Time nil UNKNOWNRefPos MJD302",
481 "Time 1859-09-15T00:00:00"),
482 ("Difference ICRS Epoch J1999.9 (AllSky Union (Circle 10 10 2"
483 " Intersection (Polygon 10 2 2 10 10 10 Intersection( Ellipse 11 11 2 3 30"
484 " Not (Difference (Circle 12 12 3 Box 11 11 2 3))))))",
485 "Difference ICRS Epoch J1999.9 (AllSky Union (Circle 10.0 10.0 2.0 Intersection (Polygon 10.0 2.0 2.0 10.0 10.0 10.0 Intersection (Ellipse 11.0 11.0 2.0 3.0 30.0 Not (Difference (Circle 12.0 12.0 3.0 Box 11.0 11.0 2.0 3.0))))))"),
486 # 25
487 ("Union ICRS (PositionInterval 0.5 1 2 1.5 Box 0.5 0.5 0.25 0.125) unit rad"
488 " Error 0.0001 0.0001",
489 "Union ICRS (SpaceInterval 0.5 1.0 2.0 1.5 Box 0.5 0.5 0.25 0.125) unit rad Error 0.0001 0.0001"),
490 ("Union ICRS TOPOCENTER (Circle 180 10 20 Circle 190 20 20"
491 " Intersection (Circle 120 -10 20 Difference (Circle 130 -10 20"
492 " Circle 125 -10 2) Not (Circle 118 -8 3)))",
493 "Union ICRS TOPOCENTER (Circle 180.0 10.0 20.0 Circle 190.0 20.0 20.0 Intersection (Circle 120.0 -10.0 20.0 Difference (Circle 130.0 -10.0 20.0 Circle 125.0 -10.0 2.0) Not (Circle 118.0 -8.0 3.0)))"),
494 ("Redshift VELOCITY 2",
495 "Redshift VELOCITY 2.0"),
496 ("Redshift BARYCENTER JPL-DE405 3.5",
497 "Redshift BARYCENTER JPL-DE405 3.5"),
498 ]
499
500
501 class DMTest(testhelpers.VerboseTest):
502 """tests for direct manipulation of DM objects.
503 """
504 def testBadUnitsRaise(self):
505 self.assertRaises(stc.STCValueError, dm.RedshiftCoo, unit="m")
506 self.assertRaises(stc.STCValueError, dm.VelocityCoo, unit=("m", "m"))
507 self.assertRaises(stc.STCValueError, dm.VelocityCoo, unit=("m", "m"),
508 velTimeUnit=("s",))
509
510 def testEquality(self):
511 ast = stc.parseSTCS("Position ICRS 1 2 VelocityInterval 0.125 0.25")
512 ast2 = ast.change()
513 self.assertTrue(ast==ast2)
514 ast3 = ast.change(place=ast.place.change(value=(2,1)))
515 self.assertTrue(ast!=ast3)
516
517
518 class EclipticEquinoxTest(testhelpers.VerboseTest):
519 """tests for equinox defaulting in ecliptic coordinates.
520 """
521 def testDefaulting(self):
522 ast = stc.parseSTCS("Time TT 1980-12-13T08:22:01 Position ECLIPTIC")
523 self.assertEqual(ast.place.frame.equinox, "J1980.94961977")
524
525 def testNoDefaulting(self):
526 ast = stc.parseSTCS("Position ECLIPTIC")
527 self.assertEqual(ast.place.frame.equinox, None)
528
529 def testOverriding(self):
530 ast = stc.parseSTCS("Time TT 1980-12-13T08:22:01 Position ECLIPTIC J2000")
531 self.assertEqual(ast.place.frame.equinox, "J2000.0")
532
533
534 class NullTest(testhelpers.VerboseTest):
535 """tests for handling of UNKNOWNx.
536 """
537 def testSpaceFromSTCS(self):
538 ast = stc.parseSTCS("Position UNKNOWNFrame UNKNOWNRefPos")
539 self.assertEqual(ast.astroSystem.spaceFrame.refFrame, None)
540 self.assertEqual(ast.astroSystem.spaceFrame.refPos.standardOrigin, None)
541
542 def testSpaceFromUtypes(self):
543 ast = stc.parseFromUtypes([
544 ('AstroCoordSystem.SpaceFrame.CoordRefFrame', 'UNKNOWNFrame'),
545 ('AstroCoordSystem.SpaceFrame.ReferencePosition', 'UNKNOWNRefPos'),
546 ])
547 self.assertEqual(ast.astroSystem.spaceFrame.refFrame, None)
548 self.assertEqual(ast.astroSystem.spaceFrame.refPos.standardOrigin, None)
549
550 def testSpaceFromSTCX(self):
551 ast = stc.parseSTCX(
552 '<STCSpec xmlns="http://www.ivoa.net/xml/STC/stc-v1.30.xsd">'
553 "<AstroCoordSystem><SpaceFrame><UNKNOWNFrame/>"
554 "<UNKNOWNRefPos/></SpaceFrame></AstroCoordSystem></STCSpec>")[0][1]
555 self.assertEqual(ast.astroSystem.spaceFrame.refFrame, None)
556 self.assertEqual(ast.astroSystem.spaceFrame.refPos.standardOrigin, None)
557
558 def testTimeFromSTCS(self):
559 ast = stc.parseSTCS("Time nil UNKNOWNRefPos")
560 # XXX STC pain: only STC-S seems to allow entering None time scales.
561 self.assertEqual(ast.astroSystem.timeFrame.timeScale, None)
562 self.assertEqual(ast.astroSystem.timeFrame.refPos.standardOrigin, None)
563
564 def testTimeFromUtypes(self):
565 ast = stc.parseFromUtypes([
566 ('stc:AstroCoordSystem.TimeFrame.ReferencePosition', 'UNKNOWNRefPos')])
567 self.assertEqual(ast.astroSystem.timeFrame.timeScale, "TT")
568 self.assertEqual(ast.astroSystem.timeFrame.refPos.standardOrigin, None)
569
570 def testTimeFromSTCX(self):
571 ast = stc.parseSTCX(
572 '<STCSpec xmlns="http://www.ivoa.net/xml/STC/stc-v1.30.xsd">'
573 "<AstroCoordSystem><TimeFrame>"
574 "<UNKNOWNRefPos/></TimeFrame></AstroCoordSystem></STCSpec>")[0][1]
575 self.assertEqual(ast.astroSystem.timeFrame.timeScale, "TT")
576 self.assertEqual(ast.astroSystem.timeFrame.refPos.standardOrigin, None)
577
578 def _makeEmptySTC(self):
579 sys = dm.CoordSys(timeFrame=dm.TimeFrame(), spaceFrame=dm.SpaceFrame())
580 return dm.STCSpec(astroSystem=sys,
581 place=dm.SpaceCoo(frame=sys.spaceFrame),
582 time=dm.TimeCoo(frame=sys.timeFrame))
583
584 def testToSTCS(self):
585 self.assertEqual(stc.getSTCS(self._makeEmptySTC()),
586 'Time\nPosition UNKNOWNFrame')
587
588 def testToSTCX(self):
589 self.assertEqual(
590 testhelpers.cleanXML(
591 stc.getSTCX(self._makeEmptySTC(), stc.STC.STCSpec).render(
592 prefixForEmpty="stc")),
593 '<STCSpec><AstroCoordSystem><TimeFrame><TimeScale/><UNKNOWNRefPos'
594 '/></TimeFrame><SpaceFrame><UNKNOWNFrame/><UNKNOWNRefPos/><SPHERIC'
595 'AL coord_naxes="2"/></SpaceFrame></AstroCoordSystem></STCSpec>')
596
597 def testToUtypes(self):
598 self.assertEqual(dict(stc.getUtypes(self._makeEmptySTC())), {
599 'stc:AstroCoordSystem.SpaceFrame.CoordFlavor': 'SPHERICAL',
600 'stc:AstroCoordSystem.SpaceFrame.CoordRefFrame': 'UNKNOWNFrame'})
601
602
603 class GeoTest(testhelpers.VerboseTest):
604 """tests for raw geographic conversions.
605 """
606 def testForObs(self):
607 # Values from obscode list
608 long, cp, sp = 8.7216, 0.65211, 0.75570
609 phip = math.atan2(sp, cp)/utils.DEG
610 long, phi, h = geo.geocToGeod(long, phip, math.sqrt(cp**2+sp**2))
611 self.assertAlmostEqual(long, 8.7216, 4)
612 self.assertAlmostEqual(phi, 49.3986, 4)
613 self.assertAlmostEqual(h, 569, 0)
614
615 def testRoundtripFromGeod(self):
616 for sample in [
617 (245,80,300),
618 (145,60,300),
619 (45,30,-300),
620 (0,0,3000),
621 (0,0,0),
622 (90,0,0),
623 (180,0,0),
624 (270,0,0),
625 ]:
626 res = geo.geocToGeod(*geo.geodToGeoc(*sample))
627 for i in range(3):
628 self.assertAlmostEqual(sample[i], res[i], 8, "Bad: %s, %s"%(
629 sample, res))
630
631 def testRoundtripFromGeoc(self):
632 for sample in [
633 (245,80,1.001),
634 (145,60,0.999),
635 (45,30,1),
636 (0,0,1.001),
637 (0,0,1),
638 (90,0,1),
639 (180,0,1),
640 (270,0,1),
641 ]:
642 res = geo.geodToGeoc(*geo.geocToGeod(*sample))
643 for i in range(3):
644 self.assertAlmostEqual(sample[i], res[i], 8, "Bad: %s, %s"%(
645 sample, res))
646
647
648 class CLITest(testhelpers.VerboseTest):
649 """tests for working cli.
650
651 This assumes the package has been installed.
652 """
653 def testHelpOpt(self):
654 self.assertOutput(cli.main, [],
655 expectedStderr=lambda err: b"Use command 'help' to see" in err,
656 expectedRetcode=1)
657
658 def testHelpCmd(self):
659 self.assertOutput(cli.main, ["help"],
660 lambda out: b"Commands include" in out)
661
662 baseResprof = '<STCResourceProfile xmlns="http://www.ivoa.net/xml/STC/stc-v1.30.xsd" xmlns:stc="http://www.ivoa.net/xml/STC/stc-v1.30.xsd" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/1999/xlink http://vo.ari.uni-heidelberg.de/docs/schemata/xlink.xsd http://www.ivoa.net/xml/STC/stc-v1.30.xsd http://vo.ari.uni-heidelberg.de/docs/schemata/stc-v1.30.xsd"><AstroCoordSystem id="luoopnh"><SpaceFrame id="lggopnh"><ICRS /><UNKNOWNRefPos /><SPHERICAL coord_naxes="2" /></SpaceFrame></AstroCoordSystem><AstroCoordArea coord_system_id="luoopnh"><Circle frame_id="lggopnh" unit="deg"><Center><C1>10.0</C1><C2>10.0</C2></Center><Radius>4.0</Radius></Circle></AstroCoordArea></STCResourceProfile>'
663 def testResprofGood(self):
664 def test(res):
665 return testhelpers.cleanXML(self.baseResprof)==testhelpers.cleanXML(res)
666 self.assertOutput(cli.main, ["resprof", "Circle ICRS 10 10 4"],
667 test)
668
669 def testResprofError(self):
670 self.assertOutput(cli.main, ["resprof", "Circle ICRS 10 10 quatsch"],
671 expectedRetcode=1, expectedStderr="STCS expression 'Circle ICRS 10 10 quatsch' bad somewhere after 18\n (Invalid STCS expression (Expected end of text at 18))\n")
672
673 def testParseX(self):
674 self.assertOutput(cli.main, ["parseX", "-"], input=self.baseResprof,
675 expectedStdout="Circle ICRS 10.0 10.0 4.0\n")
676
677 def testConform(self):
678 self.assertOutput(cli.main, ["conform",
679 "Position GALACTIC 3 4 VelocityInterval Velocity 0.01 -0.002"
680 " unit deg/cy",
681 "Position FK5"],
682 expectedStdout=lambda s: re.match(
683 b"Position FK5 264.3719740.* -24.27950404.*"
684 b" Velocity 0.007689304978.* 0.007374596245.*"
685 b" unit deg/cy\n", s))
686
687 _utypeInput = 'Position GALACTIC "l" "b" Velocity "vl" "vb"'
688 _utypeOutput = utils.getDirtyBytes('eJwrLkm2ciwuKcp3zs8vSgmuLC5JzdULLkhMTnUrSsxN1QMLu+UkluUXKWAAW4XgAA/XIE9nRx+u\nYmIMCkpNA/MwDXJ39HF0DvF0jvf0RDOqWC8gvzizJDM/z8hFLywxpzTVSM/ZENMxUKBrp5BDjAlG\n+ExIwjAhLDUnPzmzpJJYN5RhOgKLEXgdUZbEBQBATnG5\n')
689
690 def testUtypeGen(self):
691 self.assertOutput(cli.main, ["utypes", self._utypeInput],
692 expectedStdout=self._utypeOutput)
693
694 def testUtypeParse(self):
695 self.assertOutput(cli.main, ["parseUtypes"], input=self._utypeOutput,
696 expectedStdout=self._utypeInput+"\n")
697
698
699 class SystemLibraryTest(testhelpers.VerboseTest):
700 """tests for retrieving library systems.
701 """
702 def testBasic(self):
703 res = stc.getLibrarySystem("TT-ICRS-TOPO")
704 self.assertEqual(res.timeFrame.timeScale, "TT")
705 self.assertTrue(stc.getLibrarySystem("TT-ICRS-TOPO") is res)
706
707 def testResolvesWithId(self):
708 self.assertRuns(stc.getLibrarySystem,
709 ("ivo://STClib/CoordSys#TT-FK5-TOPO",))
710
711 def testRaising(self):
712 self.assertRaisesWithMsg(utils.NotFoundError,
713 "STC library system 'JUNK-RANDOM-LOST' could not be located"
714 " in IVOA defined systems",
715 stc.getLibrarySystem, ("JUNK-RANDOM-LOST",))
716
717
718 if __name__=="__main__":
719 testhelpers.main(GeoTest)