Debian Codegraph @

tests/stcstest.py

raw
1 """
2 Tests for STC-S handling.
3
4 STC-S to AST tests are in stctest.py.
5 """
6
7 #c Copyright 2008-2025, the GAVO project <[email protected]>
8 #c
9 #c This program is free software, covered by the GNU GPL. See the
10 #c COPYING file in the source distribution.
11
12
13 import datetime
14 import os
15
16 from gavo.helpers import testhelpers
17
18 from gavo import stc
19 from gavo.stc import stcs
20 from gavo.stc import stcsgen
21 from gavo.stc import syslib
22
23
24
25 class STCSParsesTestBase(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
26 """an abstract base for STCS parse tests asserting valid expressions.
27 """
28
29 syms = stcs.getSymbols()
30
31 def _runTest(self, args):
32 sym, literal = args
33 try:
34 self.syms[sym].parseString(literal, parseAll=True)
35 except stcs.ParseException:
36 raise AssertionError("'%s' didn't parse but should have"%literal)
37
38
39 class STCSFailsTestBase(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
40 """an abstract base for STCS parse tests asserting invalid expressions.
41 """
42
43 syms = stcs.getSymbols()
44
45 def _runTest(self, args):
46 sym, literal = args
47 self.assertRaisesVerbose((stcs.ParseException, stcs.ParseSyntaxException),
48 self.syms[sym].parseString,
49 (literal, True), "No exception when parsing '%s' with %s"%
50 (literal, sym))
51
52
53 class STCSTimeParsesTest(STCSParsesTestBase):
54 """Tests for parsing of time sub-phrases.
55 """
56 samples = [
57 ("jdLiteral", "JD2569903.78"),
58 ("jdLiteral", "JD 2569403.78"),
59 ("mjdLiteral", "MJD2503.78"),
60 ("isoTimeLiteral", "1980-10-10"),
61 ("isoTimeLiteral", "1980-10-10T12:12:15"),
62 #5
63 ("isoTimeLiteral", "1980-10-10T12:12:15Z"),
64 ("timeInterval", "TimeInterval"),
65 ("timeInterval", "TimeInterval 1900-01-01"),
66 ("timeInterval", "TimeInterval 1900-01-01 2000-01-01"),
67 ("timeInterval", "TimeInterval 1900-01-01 2000-01-01"),
68 #10
69 ("timeInterval", "TimeInterval 1900-01-01 2000-01-01"
70 " 2001-01-10 2002-03-12"),
71 ("timeInterval", "TimeInterval 1900-01-01T12:30:14Z 2000-01-01T14:30:21"),
72 ("timeInterval", "TimeInterval TT 1900-01-01 2000-01-01"),
73 ("timeInterval", "TimeInterval GEOCENTER 1900-01-01 2000-01-01"),
74 ("timeInterval", "TimeInterval TT GEOCENTER 1900-01-01 2000-01-01"),
75 #15
76 ("timeInterval", "TimeInterval fillfactor 0.1 1900-01-01 2000-01-01"),
77 ("timeInterval", "TimeInterval fillfactor 1e-9 1900-01-01 2000-01-01"),
78 ("timeInterval", "TimeInterval 1900-01-01 2000-01-01"
79 " Time 1920-01-20T05:03:20Z"),
80 ("timeInterval", "TimeInterval 1900-01-01 2000-01-01 unit s"),
81 ("startTime", "StartTime 1900-01-01 unit s"),
82 #20
83 ("startTime", "StartTime fillfactor 0.1 1900-01-01 unit yr"),
84 ("stopTime", "StopTime 1900-01-01 unit s"),
85 ("stopTime", "StopTime fillfactor 0.1 1900-01-01 unit yr"),
86 ("stopTime", "StopTime 1900-01-01 unit yr Error 19"),
87 ("stopTime", "StopTime 1900-01-01 unit yr Error 19 20"),
88 #25
89 ("stopTime", "StopTime 1900-01-01 unit yr Resolution 19"),
90 ("stopTime", "StopTime 1900-01-01 unit yr Resolution 19 20"),
91 ("stopTime", "StopTime 1900-01-01 unit yr PixSize 19"),
92 ("stopTime", "StopTime 1900-01-01 unit yr Size 19"),
93 ("stopTime", "StopTime 1900-01-01 unit yr PixSize 19 20"),
94 #30
95 ("stopTime", "StopTime fillfactor 0.1 TT GEOCENTER 1900-01-01"
96 " Time 2000-12-31 unit yr Error 1 2 Resolution 0.1 0.1 PixSize 19 20"),
97 ]
98
99
100 class STCSTimeFails(STCSFailsTestBase):
101 """Tests for failing on invalid time sub-phrases.
102 """
103 samples = [
104 ("timeInterval", "TimeInterval unit s fillfactor 0.1"),
105 ("timeInterval", "TimeInterval fillfactor 0.1 foobar"),
106 ("timeInterval", "fillfactor 0.1 foobar"),
107 ("timeInterval", "TimeInterval 0.2 0.2"),
108 ("jdLiteral", "0.2"),
109 ("jdLiteral", "JD x20"),
110 ("isoTimeLiteral", "JD"),
111 ("isoTimeLiteral", "30x50"),
112 ("startTime", "startTime 1900-01-01 2000-01-01 unit s")]
113
114
115 class STCSSpaceParsesTest(STCSParsesTestBase):
116 samples = [
117 ("velocityUnit", "unit kpc/a"),
118 ("velocityUnit", "unit kpc/a m/s"),
119 ("positionInterval", "PositionInterval ICRS"),
120 ("positionInterval", "PositionInterval ICRS 12 12"),
121 ("positionInterval", "PositionInterval ICRS 12 11 10 9 8 7 6 5 4 3 2 1"),
122 ("positionInterval", "PositionInterval ICRS 12 11 unit m Error 10 10"
123 " Resolution 12 Size 2 PixSize 14 14"),
124 ("atomicGeometry", "AllSky ICRS"),
125 ("atomicGeometry", "Circle FK4 B1975.0 1 2 3"),
126 ("atomicGeometry", "Circle FK4 J1975.0 1 2 3"),
127 ("atomicGeometry", "Circle fillfactor 0.1 FK4 TOPOCENTER SPHER2 1 2 3"
128 " unit deg Error 3 3 Size 23"),
129 ("atomicGeometry", "Ellipse J2000 unit deg"),
130 ("atomicGeometry", "Box GALACTIC 12 12 10 20 unit deg"),
131 ("atomicGeometry", "Polygon GALACTIC_II 12 12 10 20 21 21 20 19"),
132 ("atomicGeometry", "Convex GEO_C 12 12 10 20 21 21 20 19"),
133 ("position", "Position UNKNOWNFrame 12 13 Error 0.1 0.1"),
134 ("position", "Position UNKNOWNFrame Epoch J1992.5 12 13 Error 0.1 0.1"
135 " VelocityInterval fillfactor 0.125 1 1.5 2 3 Error 0.25 0.5"
136 " Resolution 0.25 0.25 PixSize 0.5 0.75"),
137 ("position", "Position ICRS TOPOCENTER JPL-DE200"),
138 ]
139
140
141 class STCSSpaceFailsTest(STCSFailsTestBase):
142 samples = [
143 ("positionInterval", "PositionInterval"),
144 ("positionInterval", "PositionInterval 12 12"),
145 ("positionInterval", "PositionInterval 12 12 Error x"),
146 ("positionInterval", "PositionInterval 12 12 Error 5 unit m"),
147 ("positionInterval", "PositionInterval 12 12 unit s"),
148 ("positionInterval", "PositionInterval 12 12 unit 14"),
149 ("atomicGeometry", "Circle FK4 fillfactor 0.1"),
150 ("atomicGeometry", "Elipse J2000 unit deg Error 3 3 Size 23"),
151 ("atomicGeometry", "Ellipse J200 unit deg Error 3 3 Size 23"),
152 ("spaceSubPhrase", "Ellipse Box J2000"),
153 ]
154
155
156 class STCSRedshiftParsesTest(STCSParsesTestBase):
157 samples = [
158 ("redshiftSubPhrase", "Redshift TOPOCENTER VELOCITY RELATIVISTIC 0.1"
159 " unit km/s Error 10 12 Resolution 1 2 Size 3 PixSize 4 5"),
160 ("redshiftSubPhrase", "RedshiftInterval fillfactor 0.4"
161 " BARYCENTER REDSHIFT"),
162 ("redshiftSubPhrase", "RedshiftInterval fillfactor 0.4"
163 " BARYCENTER REDSHIFT 12 13 Redshift 11.3"),
164 ]
165
166
167 class STCSRedshiftFailsTest(STCSFailsTestBase):
168 samples = [
169 ("redshiftSubPhrase", "Redshift GEOCENTER 0.1 unit mm"),
170 ("redshiftSubPhrase", "Redshift TOPOCENTER 0.1 RELATIVISTIC VELOCITY"),
171 ]
172
173
174 class STCSSpectralParsesTest(STCSParsesTestBase):
175 samples = [
176 ("spectralSubPhrase", "Spectral 12 unit mm"),
177 ("spectralSubPhrase", "Spectral 12 unit mm Size 3"),
178 ("spectralSubPhrase", "Spectral NEPTUNE 12 unit mm"),
179 ("spectralSubPhrase", "Spectral UNKNOWNRefPos 12 unit Angstrom Error 4 3"
180 " Resolution 0.2 PixSize 12"),
181 ("spectralSubPhrase", "SpectralInterval HELIOCENTER 12 13 Spectral 12.2"
182 " unit nm Error 4 Resolution 0.2 PixSize 12"),
183 ]
184
185
186 class STCSSpectralFailsTest(STCSFailsTestBase):
187 samples = [
188 ("spectralSubPhrase", "Spectral ab"),
189 ("spectralSubPhrase", "Spectral 1e10 unit pc"),
190 ("spectralSubPhrase", "SpectralInterval ICRS 1e10 unit Angstrom"),
191 ]
192
193
194 class STCSCompoundParsesTest(STCSParsesTestBase):
195 samples = [
196 ("compoundGeoPhrase",
197 "Union ICRS (Circle 10 12 1 Circle 11 11 1)"),
198 ("compoundGeoPhrase",
199 "Union ICRS (Circle 10 12 1 Not (Circle 11 11 1))"),
200 ("compoundGeoPhrase",
201 "Union ICRS (Circle 10 12 1 Union (Not (Circle 11 11 1) Box 12 22 1 2))"),
202 ("compoundGeoPhrase",
203 "Intersection ICRS (Circle 10 12 1 Union (Not (Circle 11 11 1)"
204 " Box 12 22 1 2))"),
205 ("compoundGeoPhrase",
206 "Difference ICRS (Circle 10 12 1 Intersection (Not (Circle 11 11 1)"
207 " Box 12 22 1 2))"),
208 # 5
209 ("compoundGeoPhrase",
210 "Union ICRS (Circle 10 12 1"
211 " Not (Intersection( Circle 11 11 1 Box 12 22 1 2)))"),
212 ("compoundGeoPhrase",
213 "Union ICRS (Circle 10 12 1"
214 " Not (Intersection (Circle 11 11 1 Not (Union (Box 12 22 1 2"
215 " Ellipse 1 2 3 4 5)))))"),
216 ("compoundGeoPhrase",
217 "Union Galactic (PositionInterval 10 12 20 14"
218 " PositionInterval 20 12 20 24)"),
219 ]
220
221
222 class STCSCompoundFailsTest(STCSFailsTestBase):
223 samples = [
224 ("compoundGeoPhrase",
225 "Not (Union ICRS (Circle 10 12 1 Circle 11 11 1))"),
226 ("compoundGeoPhrase",
227 "Union ICRS (Position 12 13 Circle 10 12 1 Circle 11 11 1)"),
228 ("compoundGeoPhrase",
229 "Difference ICRS (Circle 10 12 1 Not Circle 11 11 1 Box 12 22 1 2)"),
230 ("compoundGeoPhrase",
231 "Union ICRS"),
232 ]
233
234
235 class STCSTreeParseTestBase(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
236 """A base for parse tests checking the concrete syntax trees.
237
238 Fill out the samples class variable with tuples of
239 (symbolName, inputString, resultTree)
240 """
241
242 syms = stcs.getSymbols()
243 samples = []
244
245 def _runTest(self, sample):
246 symbolName, inputString, resultTree = sample
247 try:
248 parseResult = self.syms[symbolName].parseString(
249 inputString, parseAll=True)
250 tree = stcs.makeTree(parseResult)
251 self.assertEqual(tree, resultTree)
252 except stcs.ParseException:
253 raise AssertionError(
254 "Sample '%s' with expected result %s didn't parse"%(
255 inputString, resultTree))
256
257
258 class SimpleSTCSTreesTest(STCSTreeParseTestBase):
259 samples = [
260 ("timeUnit", "unit s", {'unit': 's'}),
261 ("spaceUnit", "unit pc", {"unit": 'pc'}),
262 ("spaceUnit", "unit pc kpc m", {"unit": 'pc kpc m'}),
263 ("velocityUnit", "unit pc/cy km/s", {"unit": 'pc/cy km/s'}),
264 ("positionSpec", "Position 2 1 3.5 7e9", {"pos": [2., 1., 3.5, 7e9]}),
265 ("timescale", "TT", {'timescale': 'TT'}),
266 ("jdLiteral", "JD 2450000.5", [datetime.datetime(
267 1995, 10, 9, 23, 59, 59, 999997)]),
268 ("redshiftType", "VELOCITY", {'redshiftType': 'VELOCITY'}),
269 ("frame", "FK4 B1940.5", {'frame': 'FK4', 'equinox': 'B1940.5'}),
270 ("systemDefinition", "System TT-ICRS-TOPO",
271 {'libSystem': 'TT-ICRS-TOPO'}),
272 ]
273
274
275 class ComplexSTCSTreesTest(STCSTreeParseTestBase):
276 samples = [
277 ("positionInterval", "PositionInterval ICRS Epoch J2003.4 2 3",
278 {'coos': [2., 3.], 'frame': 'ICRS', 'type': 'PositionInterval',
279 'epoch': 'J2003.4'}),
280 ("positionInterval", "PositionInterval ICRS 2 3 Error 5 7",
281 {'coos': [2., 3.], 'frame': 'ICRS', 'type': 'PositionInterval',
282 'error': [5., 7.]}),
283 ("timeInterval", "TimeInterval 1980-10-15 JD 2454930.7",
284 {'coos': [datetime.datetime(1980, 10, 15, 0, 0),
285 datetime.datetime(2009, 4, 9, 4, 48, 0, 18)], 'type': 'TimeInterval'}),
286 ("stopTime", "StopTime fillfactor 0.1 TT GEOCENTER 1900-01-01"
287 " Time 2000-12-31 unit yr Error 1 2 Resolution 0.5 0.5 PixSize 19 20",
288 {'fillfactor': 0.1, 'timescale': 'TT', 'type': 'StopTime',
289 'refpos': 'GEOCENTER', 'coos': [datetime.datetime(1900, 1, 1, 0, 0)],
290 'pos': [datetime.datetime(2000, 12, 31, 0, 0)],
291 'error': [1., 2.], 'resolution': [0.5, 0.5], 'unit': 'yr',
292 'pixSize': [19., 20.]}),
293 ("spaceSubPhrase", "Circle fillfactor 0.1 FK4 TOPOCENTER SPHER2 1 2 3"
294 " unit deg Error 3 3 Size 23", [{'coos': [1., 2., 3.],
295 'frame': 'FK4', 'refpos': 'TOPOCENTER', 'fillfactor': 0.1,
296 'error': [3., 3.], 'flavor': 'SPHER2', 'type': 'Circle',
297 'unit': 'deg', 'size': [23.]}]),
298 ("spaceSubPhrase", "PositionInterval FK4 VelocityInterval fillfactor 0.25"
299 " 12 13"
300 " Velocity 12.5 unit km/s Error 4 5 Resolution 1.25 PixSize 1.5",
301 [{'frame': 'FK4', 'type': 'PositionInterval', 'velocity': [
302 {'coos': [12., 13.], 'fillfactor': 0.25,
303 'error': [4., 5.], 'pos': [12.5], 'resolution': [1.25],
304 'unit': 'km/s', 'pixSize': [1.5], 'type': "Velocity"}]}]),
305 ("stcsPhrase", "Circle ICRS 2 23 12 RedshiftInterval RADIO 0.125 0.25", {
306 'space': {
307 'coos': [2., 23., 12.], 'frame': 'ICRS', 'type': 'Circle'},
308 'redshift': {
309 'coos': [0.125, 0.25], 'dopplerdef': 'RADIO',
310 'type': 'RedshiftInterval'}}),
311 ]
312
313
314 class STCSPhraseTest(STCSTreeParseTestBase):
315 samples = [
316 ("stcsPhrase", "StopTime TT 2009-03-10T09:56:10.015625",
317 {'time': {'coos': [datetime.datetime(2009, 3, 10, 9, 56, 10, 15625)],
318 'type': 'StopTime', 'timescale': 'TT'}}),
319 ("stcsPhrase", "AllSky FK4 B1975.0 Epoch J1970.0 Position 12 13",
320 {'space': {'type': 'AllSky', 'frame': 'FK4', 'equinox': 'B1975.0',
321 'pos': [12., 13.], 'epoch': 'J1970.0'}}),
322 ("stcsPhrase", "Spectral BARYCENTER 200000 unit Hz PixSize 1",
323 {'spectral': {'type': 'Spectral', 'pos': [200000.],
324 "refpos": "BARYCENTER", "unit": "Hz", "pixSize": [1.0]}}),
325 ("stcsPhrase", "Time TT 2008-05-05T12:33:45",
326 {'time': {'type': 'Time', 'pos':
327 [datetime.datetime(2008, 5, 5, 12, 33, 45)], 'timescale': 'TT'}}),
328 ("stcsPhrase", "AllSky ICRS",
329 {'space': {'type': 'AllSky', 'frame': 'ICRS'}}),
330 ("stcsPhrase", "Position ECLIPTIC System TT-ICRS-TOPO",
331 {'libSystem': 'TT-ICRS-TOPO', 'space':
332 {'frame': 'ECLIPTIC', 'type': 'Position'}}),
333 ("stcsPhrase", 'Position ICRS Epoch J2000.0 \n 20 21',
334 {'space': {
335 'frame': 'ICRS',
336 'type': 'Position',
337 'epoch': 'J2000.0',
338 'pos': [20.0, 21.0]}}),
339 ]
340
341
342 class TreeIterTest(testhelpers.VerboseTest):
343 """tests for sensible traversal of STCS CSTs.
344 """
345 syms = stcs.getSymbols()
346
347 def _getTree(self, inputString):
348 return stcs.makeTree(self.syms["stcsPhrase"].parseString(
349 inputString, parseAll=True))
350
351 def testSimple(self):
352 self.assertEqual(list(stcs.iterNodes(self._getTree("Position ICRS 2 3"))),
353 [(('space',), {'pos': [2.0, 3.0],
354 'frame': 'ICRS', 'type': 'Position'}),
355 ((), {'space': {'pos': [2.0, 3.0],
356 'frame': 'ICRS', 'type': 'Position'}})])
357
358 def testComplex(self):
359 self.assertEqual(list(stcs.iterNodes(self._getTree(
360 "Circle ICRS 2 23 12 VelocityInterval fillfactor 0.5 12 13"
361 " RedshiftInterval RADIO 0.125 0.25"))), [
362 (('space', 'velocity'),
363 {'coos': [12.0, 13.0], 'fillfactor': 0.5,
364 'type': 'VelocityInterval'}),
365 (('space',), {
366 'coos': [2.0, 23.0, 12.0], 'frame': 'ICRS', 'type': 'Circle',
367 'velocity': [
368 {'coos': [12.0, 13.0], 'fillfactor': 0.5,
369 'type': 'VelocityInterval'}]}),
370 (('redshift',), {
371 'coos': [0.125, 0.25], 'dopplerdef': 'RADIO',
372 'type': 'RedshiftInterval'}),
373 ((), {'space': {'coos': [2.0, 23.0, 12.0], 'frame': 'ICRS',
374 'type': 'Circle', 'velocity': [{'coos': [12., 13.],
375 'fillfactor': 0.5, 'type': 'VelocityInterval'}]},
376 'redshift': {'coos': [0.125, 0.25],
377 'dopplerdef': 'RADIO', 'type': 'RedshiftInterval'}})])
378
379
380 class DefaultingTest(testhelpers.VerboseTest):
381 def testPositionInterval(self):
382 self.assertEqual(stcs.getCST("PositionInterval ICRS"),
383 {'space': {'frame': 'ICRS', 'unit': 'deg', 'type': 'PositionInterval',
384 'flavor': 'SPHER2'}})
385
386 def testSpatialWithVelInterval(self):
387 self.assertEqual(stcs.getCST("PositionInterval ICRS VelocityInterval"
388 " Velocity 1 2"),
389 {'space': {'frame': 'ICRS', 'unit': 'deg', 'type': 'PositionInterval',
390 'flavor': 'SPHER2',
391 'velocity': [{'pos': [1.0, 2.0], 'unit': 'm/s',
392 'type': 'Velocity'}]}})
393
394 def testWithUnit(self):
395 self.assertEqual(stcs.getCST("PositionInterval ICRS unit arcsec"),
396 {'space': {'frame': 'ICRS', 'unit': 'arcsec',
397 'type': 'PositionInterval',
398 'flavor': 'SPHER2'}})
399
400 def testConvex(self):
401 self.assertEqual(stcs.getCST("Convex ICRS"),
402 {'space': {'frame': 'ICRS', 'unit': '', 'type': 'Convex',
403 'flavor': 'UNITSPHER'}})
404
405 def testCart2(self):
406 self.assertEqual(stcs.getCST("PositionInterval FK4 TOPOCENTER CART2"),
407 {'space': {'frame': 'FK4', 'unit': 'm', 'type': 'PositionInterval',
408 'refpos': 'TOPOCENTER', 'flavor': 'CART2', "equinox": 'B1950.0'}})
409
410 def testGeoC(self):
411 self.assertEqual(stcs.getCST("PositionInterval GEO_C"),
412 {'space': {'frame': 'GEO_C', 'unit': 'deg deg m',
413 'type': 'PositionInterval', 'flavor': 'SPHER2'}})
414
415 def testTemporal(self):
416 self.assertEqual(stcs.getCST("TimeInterval"), {'time': {'unit': 's',
417 'type': 'TimeInterval'}})
418
419 def testSpectral(self):
420 self.assertEqual(stcs.getCST("SpectralInterval"), {'spectral':
421 {'type': 'SpectralInterval', 'unit': 'Hz'}})
422
423 def testRedshift(self):
424 self.assertEqual(stcs.getCST("RedshiftInterval"),
425 {'redshift': {'redshiftType': 'REDSHIFT', 'type': 'RedshiftInterval',
426 'unit': ['nil'], 'dopplerdef': 'OPTICAL'}})
427
428
429 def assertMapsto(stcsInput, expectedOutput):
430 ast = stc.parseSTCS(stcsInput)
431 foundOutput = stcsgen.getSTCS(ast)
432 if foundOutput!=expectedOutput:
433 matchLen = len(os.path.commonprefix([expectedOutput, foundOutput]))
434 raise AssertionError("Didn't get expected STC-S for example '%s';"
435 " non-matching part: '%s'"%(stcsInput, foundOutput[matchLen:]))
436
437
438 class GeneralGenerationTest(testhelpers.VerboseTest):
439 """tests for STCS-STCS-round trips.
440 """
441 def testEmpty(self):
442 assertMapsto('', '')
443
444 def testTimeCoo(self):
445 assertMapsto("Time TT 2009-03-10T09:56:10.015625 unit s"
446 " Error 0.0001 0.0002 Resolution 0.0001 PixSize 2",
447 'Time TT 2009-03-10T09:56:10.015625 Error 0.0001 0.0002 Resolution 0.0001 PixSize 2.0')
448
449 def testTimeInterval(self):
450 assertMapsto("TimeInterval TAI MJD52000.5 MJD52011.5",
451 'TimeInterval TAI 2001-04-01T12:00:00 2001-04-12T12:00:00')
452
453 def testTimeHalfInterval(self):
454 assertMapsto("StopTime TT 2009-03-10T09:56:10.015625",
455 'StopTime TT 2009-03-10T09:56:10.015625')
456
457 def testTimeIntervalWithValue(self):
458 assertMapsto("TimeInterval TAI MJD52000.5 MJD52011.5"
459 " Time 2001-04-01T18:23:50",
460 'TimeInterval TAI 2001-04-01T12:00:00 2001-04-12T12:00:00 Time 2001-04-01T18:23:50'),
461
462 def testOtherIntervals(self):
463 assertMapsto("SpectralInterval 20 30 unit m"
464 " RedshiftInterval VELOCITY 1 2 Error 0 0.125",
465 'SpectralInterval 20.0 30.0 unit m\nRedshiftInterval VELOCITY 1.0 2.0 Error 0.0 0.125')
466
467 def testOtherCoodinates(self):
468 assertMapsto("Spectral BARYCENTER 200000 unit Hz PixSize 1"
469 " Redshift TOPOCENTER REDSHIFT RELATIVISTIC 2",
470 'Spectral BARYCENTER 200000.0 PixSize 1.0\n'
471 'Redshift TOPOCENTER RELATIVISTIC 2.0')
472
473 def testSpatialCoo(self):
474 assertMapsto("Position ICRS -50 320",
475 'Position ICRS -50.0 320.0')
476
477 def testComplexSpatialCoo(self):
478 assertMapsto("Position ICRS UNKNOWNRefPos CART3 -50 320 20 unit pc"
479 " Error 1 2 3 1.25 2.25 3.25 Resolution 0.125 0.125 0.125"
480 " Size 4 3 2",
481 'Position ICRS CART3 -50.0 320.0 20.0 unit pc Error 1.0 2.0 3.0 1.25 2.25 3.25 Resolution 0.125 0.125 0.125 Size 4.0 3.0 2.0')
482
483 def testSpatialInterval(self):
484 assertMapsto('PositionInterval J2000 Epoch J2010.2 12 13 19 29'
485 ' Position 15 16',
486 'PositionInterval FK5 Epoch J2010.2 12.0 13.0 19.0 29.0'
487 ' Position 15.0 16.0')
488
489 def testSystem(self):
490 assertMapsto('Position FK5 J2010 15 16 System TT-ICRS-TOPO',
491 "Position ICRS TOPOCENTER 15.0 16.0\nSystem TT-ICRS-TOPO")
492
493
494 class SampleGenerationTestBase(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
495 samples = []
496 def _runTest(self, sample):
497 assertMapsto(*sample)
498
499
500 class BaseGenerationTest(SampleGenerationTestBase):
501 samples = [
502 ("Redshift BARYCENTER JPL-DE405 3.5",
503 "Redshift BARYCENTER JPL-DE405 3.5"),]
504
505
506 class GeometriesGenerationTest(SampleGenerationTestBase):
507 samples = [
508 ("AllSky ICRS", "AllSky ICRS"),
509 ("AllSky FK5 J2010 Position 12 13",
510 'AllSky FK5 J2010.0 Position 12.0 13.0'),
511 ("Circle ICRS 12 13 0.25 Position 12.5 13.5",
512 'Circle ICRS 12.0 13.0 0.25 Position 12.5 13.5'),
513 ("Ellipse ECLIPTIC TOPOCENTER -40 38 0.75 0.5 45"
514 " PixSize 0.25 0.25 0.5 0.5",
515 'Ellipse ECLIPTIC TOPOCENTER -40.0 38.0 0.75 0.5 45.0'
516 ' PixSize 0.25 0.25 0.5 0.5'),
517 ("Box ICRS 32 24 1 2",
518 "Box ICRS 32.0 24.0 1.0 2.0"),
519 ("Polygon UNKNOWNFrame CART2 1 2 3 4 5 6 Size 3 4",
520 'Polygon UNKNOWNFrame CART2 1.0 2.0 3.0 4.0 5.0 6.0 Size 3.0 4.0'),
521 ("Convex GEO_C 2 3 4 0.5 5 6 7 0.25",
522 'Convex GEO_C 2.0 3.0 4.0 0.5 5.0 6.0 7.0 0.25'),
523 ]
524
525
526 class CompoundGenerationTest(SampleGenerationTestBase):
527 samples = [
528 ("Union FK5 (Box 12 -13 2 2 Circle 14 -13.5 3)",
529 'Union FK5 (Box 12.0 -13.0 2.0 2.0 Circle 14.0 -13.5 3.0)'),
530 ("Not FK5 (Circle 14 14 3)",
531 'Not FK5 (Circle 14.0 14.0 3.0)'),
532 ("Union FK5 (Box 12 -13 2 2 Not (Circle 14 -13.5 3))",
533 'Union FK5 (Box 12.0 -13.0 2.0 2.0 Not (Circle 14.0 -13.5 3.0))'),
534 ("Intersection GALACTIC (Polygon 12 13 14 15 16 17 Union (Circle 340 -30 3"
535 " Not (Circle 341 -32 2)))",
536 'Intersection GALACTIC (Polygon 12.0 13.0 14.0 15.0 16.0 17.0 Union'
537 ' (Circle 340.0 -30.0 3.0 Not (Circle 341.0 -32.0 2.0)))'),
538 ("Difference GALACTIC (Polygon 12 13 14 15 16 17"
539 " Not (Intersection (Circle 340 -30 3 Circle 341 -32 2)))",
540 'Difference GALACTIC (Polygon 12.0 13.0 14.0 15.0 16.0 17.0 Not'
541 ' (Intersection (Circle 340.0 -30.0 3.0 Circle 341.0 -32.0 2.0)))'),
542 ]
543
544
545 class VelocitiesGenerationTest(SampleGenerationTestBase):
546 samples = [
547 ("Position ICRS VelocityInterval 1 2",
548 "Position ICRS VelocityInterval 1.0 2.0"),
549 ("Position ICRS VelocityInterval 1 2 Velocity 1.5 2.5",
550 "Position ICRS VelocityInterval 1.0 2.0 Velocity 1.5 2.5"),
551 ("Position ICRS 12 13 VelocityInterval Velocity 1.5 2.5",
552 "Position ICRS 12.0 13.0 Velocity 1.5 2.5"),
553 ("Position ICRS 12 13 Velocity 1.5 2.5",
554 "Position ICRS 12.0 13.0 Velocity 1.5 2.5"),
555 ("Position ICRS VelocityInterval unit deg/s Error 0.125 0.125",
556 "Position ICRS Velocity unit deg/s Error 0.125 0.125"),
557 ("Position ICRS Velocity 1 2 unit deg/s Error 0.125 0.125",
558 "Position ICRS Velocity 1.0 2.0 unit deg/s Error 0.125 0.125"),
559 ]
560
561
562 class ColrefTest(testhelpers.VerboseTest):
563 def testPosCol(self):
564 tree = stc.parseQSTCS('Position ICRS "foo" "bar" Error "e_foo" "e_bar"')
565 self.assertEqual(tree.place.value[0].dest, "foo")
566 self.assertEqual(tree.place.error.values[0][0].dest, "e_foo")
567
568 def testPosGeo(self):
569 tree = stc.parseQSTCS('Position ICRS [pos]')
570 self.assertEqual(tree.place.value.dest, "pos")
571
572 def testErrorRadius(self):
573 tree = stc.parseQSTCS('Position ICRS Error "p1" "p1"')
574 self.assertEqual(tree.place.error.radii[0].dest, "p1")
575
576 def testEpochReference(self):
577 tree = stc.parseQSTCS('Position ICRS Epoch "ep" [pos]')
578 self.assertEqual(tree.place.value.dest, "pos")
579 self.assertEqual(tree.place.yearDef, "J")
580 self.assertEqual(tree.place.epoch.dest, "ep")
581
582
583 class SyslibTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
584 samples = syslib.SYSDEFS
585
586 def _runTest(self, sysId):
587 self.assertRuns(syslib.getLibrarySystem, (sysId,),
588 "Cannot build system %s"%sysId)
589
590
591 class SpatialSystemTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
592 def _runTest(self, sample):
593 stcsInput, expected = sample
594 ast = stc.parseSTCS(stcsInput)
595 self.assertEqual(stc.getSpatialSystem(ast), expected)
596
597 samples = [
598 ("Position ICRS", "ICRS"),
599 ("Position FK4 B1875.0", "FK4 B1875.0"),
600 ("Time TT 2000-01-01 Circle GALACTIC GEOCENTER 10 10 2",
601 "GALACTIC GEOCENTER"),
602 ]
603
604
605 if __name__=="__main__":
606 testhelpers.main(VelocitiesGenerationTest)