Debian Codegraph @

tests/stcutypetest.py

raw
1 """
2 Tests for conversion between utype sequences and STC ASTs.
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 from gavo.helpers import testhelpers
12
13 from gavo import stc
14 from gavo.stc import dm
15
16
17 class CoosysGenerTest(testhelpers.VerboseTest):
18 """tests for generation of coosys utype dicts from STC ASTs.
19 """
20 def _assertSetmatch(self, literal, expected):
21 ast = stc.parseSTCS(literal)
22 self.assertEqual(dict(stc.getUtypes(ast)),
23 expected)
24
25 def testRef(self):
26 tree = dm.STCSpec(astroSystem=stc.getLibrarySystem("TT-ICRS-OPT-BARY-TOPO"))
27 self.assertEqual(stc.getUtypes(tree),
28 [('stc:AstroCoordSystem.href',
29 'ivo://STClib/CoordSys#TT-ICRS-OPT-BARY-TOPO')])
30
31 def testSimpleSystems(self):
32 self._assertSetmatch("Time TT Position ICRS GEOCENTER Spectral BARYCENTER"
33 " Redshift BARYCENTER VELOCITY OPTICAL", {
34 'stc:AstroCoordSystem.SpaceFrame.CoordRefFrame': 'ICRS',
35 'stc:AstroCoordSystem.TimeFrame.TimeScale': 'TT',
36 'stc:AstroCoordSystem.RedshiftFrame.DopplerDefinition': 'OPTICAL',
37 'stc:AstroCoordSystem.RedshiftFrame.value_type': 'VELOCITY',
38 'stc:AstroCoordSystem.RedshiftFrame.ReferencePosition': 'BARYCENTER',
39 'stc:AstroCoordSystem.SpaceFrame.CoordFlavor': 'SPHERICAL',
40 'stc:AstroCoordSystem.SpectralFrame.ReferencePosition': 'BARYCENTER',
41 'stc:AstroCoordSystem.SpaceFrame.ReferencePosition': 'GEOCENTER'})
42
43 def testWithEquinox(self):
44 self._assertSetmatch("Position FK4 J1975.0", {
45 'stc:AstroCoordSystem.SpaceFrame.CoordRefFrame': 'FK4',
46 'stc:AstroCoordSystem.SpaceFrame.CoordRefFrame.Equinox': 'J1975.0',
47 'stc:AstroCoordSystem.SpaceFrame.CoordFlavor': 'SPHERICAL',})
48
49 def testWithDistance(self):
50 self._assertSetmatch("Position ICRS BARYCENTER SPHER3 unit deg deg pc",{
51 'stc:AstroCoordSystem.SpaceFrame.CoordFlavor.coord_naxes': '3',
52 'stc:AstroCoordSystem.SpaceFrame.CoordRefFrame': 'ICRS',
53 'stc:AstroCoordSystem.SpaceFrame.CoordFlavor': 'SPHERICAL',
54 'stc:AstroCoordSystem.SpaceFrame.ReferencePosition': 'BARYCENTER'})
55
56 def testWithPleph(self):
57 self._assertSetmatch("Position ICRS BARYCENTER JPL-DE405", {
58 'stc:AstroCoordSystem.SpaceFrame.CoordFlavor': 'SPHERICAL',
59 'stc:AstroCoordSystem.SpaceFrame.CoordRefFrame': 'ICRS',
60 'stc:AstroCoordSystem.SpaceFrame.ReferencePosition.PlanetaryEphem':
61 'JPL-DE405',
62 'stc:AstroCoordSystem.SpaceFrame.ReferencePosition': 'BARYCENTER'})
63
64
65 CR = stc.ColRef
66
67
68 class CooGenerTest(testhelpers.VerboseTest):
69 """tests for generation of column utype dicts from STC ASTs.
70 """
71 def _assertAssmatch(self, literal, truth):
72 ast = stc.parseQSTCS(literal)
73 gr = [p for p in stc.getUtypes(ast)
74 if not p[0].startswith("stc:AstroCoordSystem")]
75 self.assertEqual(dict(gr), truth)
76
77 def testTrivialPos(self):
78 self._assertAssmatch('Position ICRS "ra" "dec"', {
79 'stc:AstroCoords.Position2D.Value2.C1': CR("ra"),
80 'stc:AstroCoords.Position2D.Value2.C2': CR("dec"),
81 })
82
83 def testMixedPos(self):
84 self._assertAssmatch('Position ICRS "ra" 20.0', {
85 'stc:AstroCoords.Position2D.Value2.C1': CR("ra"),
86 'stc:AstroCoords.Position2D.Value2.C2': '20.0',
87 })
88
89 def testVecPos(self):
90 self._assertAssmatch('Position ICRS [point]',
91 {'stc:AstroCoords.Position2D.Value2': CR("point")})
92
93 def testVecPos3(self):
94 self._assertAssmatch('Position ICRS SPHER3 [point]',
95 {'stc:AstroCoords.Position3D.Value3': CR('point')})
96
97 def testVecEpoch(self):
98 self._assertAssmatch('Position ICRS Epoch J2010.5', {
99 'stc:AstroCoords.Position2D.Epoch': '2010.5',
100 'stc:AstroCoords.Position2D.Epoch.yearDef': 'J'})
101
102 def testBesselEpoch(self):
103 self._assertAssmatch('Position ICRS Epoch B2010.5', {
104 'stc:AstroCoords.Position2D.Epoch': '2010.5',
105 'stc:AstroCoords.Position2D.Epoch.yearDef': 'B'})
106
107 def testVeloc(self):
108 self._assertAssmatch(
109 'Position ICRS VelocityInterval Velocity "pmra" "pmde"', {
110 'stc:AstroCoords.Velocity2D.Value2.C2': CR('pmde'),
111 'stc:AstroCoords.Velocity2D.Value2.C1': CR('pmra')})
112
113 def testErrorRadius(self):
114 self._assertAssmatch(
115 'Position ICRS Error "ep" "ep"', {
116 'stc:AstroCoords.Position2D.Error2Radius': CR('ep')})
117
118 def testTime(self):
119 self._assertAssmatch('Time TT "obsDate"', {
120 'stc:AstroCoords.Time.TimeInstant': CR("obsDate"),})
121
122 def testTimeA(self):
123 self._assertAssmatch(
124 'TimeInterval TT "start" "end"', {
125 'stc:AstroCoordArea.TimeInterval.StartTime': CR('start'),
126 'stc:AstroCoordArea.TimeInterval.StopTime': CR('end')})
127
128 def testGeoRef(self):
129 self._assertAssmatch('Circle FK5 J1000.0 [circle]', {
130 'stc:AstroCoordArea.Circle': CR('circle')})
131 self._assertAssmatch('Box FK5 J1000.0 [bbox]', {
132 'stc:AstroCoordArea.Box': CR('bbox')})
133 self._assertAssmatch('Polygon FK5 J1000.0 [poly]', {
134 'stc:AstroCoordArea.Polygon': CR('poly')})
135
136 def testGeoSplit(self):
137 self._assertAssmatch('Circle FK5 J1000.0 "cx" "cy" "radius"', {
138 'stc:AstroCoordArea.Circle.Center.C2': CR('cy'),
139 'stc:AstroCoordArea.Circle.Center.C1': CR('cx'),
140 'stc:AstroCoordArea.Circle.Radius': CR('radius')})
141
142 def testError(self):
143 self._assertAssmatch('Position ICRS Error "e_ra" "e_dec"', {
144 'stc:AstroCoords.Position2D.Error2.C1': CR('e_ra'),
145 'stc:AstroCoords.Position2D.Error2.C2': CR('e_dec')})
146
147 def testEpoch(self):
148 self._assertAssmatch('Position ICRS Epoch "epum" 10 20', {
149 'stc:AstroCoords.Position2D.Epoch': CR('epum'),
150 'stc:AstroCoords.Position2D.Epoch.yearDef': 'J',
151 'stc:AstroCoords.Position2D.Value2.C1': '10.0',
152 'stc:AstroCoords.Position2D.Value2.C2': '20.0'})
153
154 def testRedshift(self):
155 self._assertAssmatch('Redshift "z" Error "zErr"', {
156 'stc:AstroCoords.Redshift.Value': CR('z'),
157 'stc:AstroCoords.Redshift.Error': CR('zErr')
158 })
159
160 def testCombined(self):
161 self._assertAssmatch('Time TT "dateObs" Error "e_date"'
162 ' Circle ICRS "cra" "cdec" "crad" Position "ra" "dec"'
163 ' Error "e_ra" "e_dec" Size "s_ra" "s_dec"'
164 ' SpectralInterval "bandLow" "bandHigh"'
165 ' Redshift "z" Error "zErr"', {
166 'stc:AstroCoordArea.SpectralInterval.HiLimit': CR('bandHigh'),
167 'stc:AstroCoordArea.SpectralInterval.LoLimit': CR('bandLow'),
168 'stc:AstroCoordArea.Circle.Center.C2': CR('cdec'),
169 'stc:AstroCoordArea.Circle.Center.C1': CR('cra'),
170 'stc:AstroCoordArea.Circle.Radius': CR('crad'),
171 'stc:AstroCoords.Time.TimeInstant': CR('dateObs'),
172 'stc:AstroCoords.Position2D.Value2.C2': CR('dec'),
173 'stc:AstroCoords.Position2D.Error2.C2': CR('e_dec'),
174 'stc:AstroCoords.Position2D.Error2.C1': CR('e_ra'),
175 'stc:AstroCoords.Time.Error': CR('e_date'),
176 'stc:AstroCoords.Position2D.Value2.C1': CR('ra'),
177 'stc:AstroCoords.Position2D.Size2.C2': CR('s_dec'),
178 'stc:AstroCoords.Position2D.Size2.C1': CR('s_ra'),
179 'stc:AstroCoords.Redshift.Value': CR('z'),
180 'stc:AstroCoords.Redshift.Error': CR('zErr'),
181 })
182
183
184 class UtypeASTTest(testhelpers.VerboseTest):
185 """tests for building STC ASTs out of utype sequences.
186 """
187 def _getASTFromSTCS(self, stcs):
188 ast = stc.parseQSTCS(stcs)
189 utypes = stc.getUtypes(ast)
190 return stc.parseFromUtypes(utypes)
191
192 def testSimplePos(self):
193 ast = self._getASTFromSTCS('Position GALACTIC "long" "lat"')
194 self.assertEqual(ast.astroSystem.spaceFrame.refFrame, "GALACTIC_II")
195 self.assertEqual(ast.place.value[0].dest, "long")
196 self.assertEqual(ast.place.frame.refFrame, "GALACTIC_II")
197
198 def testWithError(self):
199 ast = self._getASTFromSTCS('Position GALACTIC Error "e1" "e1"')
200 self.assertEqual(ast.place.error.radii[0].dest, "e1")
201
202 def testWithEquinox(self):
203 ast = self._getASTFromSTCS("Position FK4 J1975.0 Epoch B2000.0")
204 self.assertEqual(ast.astroSystem.spaceFrame.equinox, "J1975.0")
205 self.assertEqual(ast.place.yearDef, "B")
206 self.assertEqual(ast.place.epoch, 2000.)
207
208 def testTime(self):
209 ast = self._getASTFromSTCS(
210 'Time TT TOPOCENTER "dateObs" Error "clockdamage"')
211 self.assertEqual(ast.time.value.dest, "dateObs")
212 self.assertEqual(ast.time.error.values[0].dest, "clockdamage")
213 self.assertEqual(ast.time.frame.refPos.standardOrigin, "TOPOCENTER")
214 self.assertEqual(ast.time.frame.timeScale, "TT")
215
216 def testGeoComp(self):
217 ast = self._getASTFromSTCS('Circle ICRS [errc]')
218 self.assertEqual(ast.areas[0].geoColRef.dest, "errc")
219
220 def testTimeTDBScale(self):
221 ast = self._getASTFromSTCS('Time TDB "time"')
222 self.assertEqual(ast.time.frame.timeScale, 'TDB')
223
224 def testPleph(self):
225 ast = self._getASTFromSTCS('Time TDB TOPOCENTER JPL-DE405')
226 self.assertEqual(ast.astroSystem.timeFrame.refPos.planetaryEphemeris,
227 "JPL-DE405")
228
229
230 class UtypeRoundtripTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
231 """tests for working roundtrip of utype de-/serialization.
232 """
233
234 def _assertSublist(self, sub, full):
235 sub, full = dict(sub), dict(full)
236 for k, v in sub.items():
237 if k not in full:
238 raise AssertionError("%s not in full"%k)
239 if sub[k]!=full[k]:
240 raise AssertionError("%s has wrong value, %s instead of %s"%(
241 k, full[k], sub[k]))
242
243 def _runTest(self, args):
244 inTypes = args
245 ast = stc.parseFromUtypes(inTypes)
246 outTypes = stc.getUtypes(ast, includeDMURI=True)
247 # allow additional keys in output (non-removed defaults)
248 self._assertSublist(inTypes, outTypes)
249
250 samples = [
251 [],
252 [('stc:AstroCoordSystem.SpaceFrame.CoordRefFrame', 'ICRS')],
253 [('stc:AstroCoordSystem.SpaceFrame.CoordRefFrame', 'ICRS'),
254 ('stc:AstroCoords.Position2D.Epoch', '2002.0'),
255 ('stc:DataModel.URI', 'http://www.ivoa.net/xml/STC/stc-v1.30.xsd'),
256 ],
257 [('stc:AstroCoords.Position2D.Value2.C1', stc.ColRef('ra')),
258 ('stc:AstroCoords.Position2D.Value2.C2', stc.ColRef('dec'))],
259 [('stc:AstroCoordSystem.SpaceFrame.CoordRefFrame', 'ICRS'),
260 ('stc:AstroCoordSystem.TimeFrame.TimeScale', 'TT'),
261 ('stc:AstroCoordSystem.RedshiftFrame.DopplerDefinition', 'OPTICAL'),
262 ('stc:AstroCoordSystem.RedshiftFrame.value_type', 'VELOCITY'),
263 ('stc:AstroCoordSystem.RedshiftFrame.ReferencePosition', 'BARYCENTER'),
264 ('stc:AstroCoordSystem.SpaceFrame.CoordFlavor', 'SPHERICAL'),
265 ('stc:AstroCoordSystem.SpectralFrame.ReferencePosition', 'BARYCENTER'),
266 ('stc:AstroCoordSystem.SpaceFrame.ReferencePosition', 'GEOCENTER'),
267 ('stc:AstroCoords.Position2D.Value2.C1', stc.ColRef('ra')),
268 ('stc:AstroCoords.Position2D.Value2.C2', stc.ColRef('dec'))],
269 [('stc:AstroCoordSystem.SpaceFrame.CoordRefFrame', 'ICRS'),
270 ('stc:AstroCoordSystem.SpaceFrame.CoordFlavor', 'CARTESIAN'),
271 ('stc:AstroCoordSystem.SpaceFrame.CoordFlavor.coord_naxes', '3')],
272 [
273 ('stc:AstroCoords.Time.TimeInstant', '2000-01-01T00:00:00'),
274 ('stc:AstroCoordArea.Circle', stc.ColRef('errc'))],
275 [
276 ('stc:AstroCoordSystem.href',
277 'ivo://STClib/CoordSys#TT-ICRS-OPT-BARY-TOPO')],
278 [
279 ('stc:AstroCoordSystem.SpaceFrame.CoordFlavor', 'SPHERICAL'),
280 ('stc:AstroCoordSystem.SpaceFrame.CoordFlavor.coord_naxes', '3'),
281 ('stc:AstroCoordSystem.SpaceFrame.CoordRefFrame', 'ICRS'),
282 ('stc:AstroCoords.Position3D.Value3.C1', stc.ColRef('raj2000')),
283 ('stc:AstroCoords.Position3D.Value3.C2', stc.ColRef('dej2000')),
284 ('stc:AstroCoords.Position3D.Value3.C3', stc.ColRef('dist_binney'))
285 ],
286 [
287 ('stc:AstroCoordSystem.SpaceFrame.CoordFlavor', 'SPHERICAL'),
288 ('stc:AstroCoordSystem.SpaceFrame.CoordFlavor.coord_naxes', '3'),
289 ('stc:AstroCoordSystem.SpaceFrame.CoordRefFrame', 'ICRS'),
290 ('stc:AstroCoords.Position3D.Value3.C2', stc.ColRef('dej2000')),
291 ],
292
293 ]
294
295
296 if __name__=="__main__":
297 testhelpers.main(CooGenerTest)