Debian Codegraph @

tests/stcxtest.py

raw
1 """
2 Tests for STC-X parsing and generation.
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 os
13
14 from gavo.helpers import testhelpers
15
16 from gavo import stc
17 from gavo.stc import stcxgen
18 from gavo.stc import dm
19
20
21
22
23 def assertEqualWithoutIds(withId, template, desc="<Undescribed>"):
24 """raises an AssertionError if withId and template are different after
25 all ids (and similar) in withId are blanked.
26
27 We want this since the ids given to our elements are essentially random.
28 """
29 # etree-based serialization has blanks in empty elements; we don't
30 # want to fail on templates for that
31 withoutId = testhelpers.cleanXML(withId)
32 template = testhelpers.cleanXML(template)
33 if withoutId!=template:
34 matchLen = len(os.path.commonprefix([template, withoutId]))
35 raise AssertionError("Didn't get expected STC XML for example '%s';"
36 " non-matching part: %s"%(desc, withoutId[matchLen:]))
37
38
39 class SpaceFrameTest(testhelpers.VerboseTest):
40 def testSimple(self):
41 assertEqualWithoutIds(stcxgen._nodeToStan(dm.SpaceFrame(
42 flavor="CARTESIAN", nDim=2, refFrame="ICRS", name="rotten",
43 refPos=dm.RefPos(standardOrigin="GEOCENTER")), None).render(
44 prefixForEmpty="stc"),
45 '<SpaceFrame ><Name>rotten</Name><ICRS /><GEOCENTER />'
46 '<CARTESIAN coord_naxes="2" /></SpaceFrame>')
47
48 def testParsing(self):
49 ast = stc.parseSTCX(
50 '<STCSpec xmlns="http://www.ivoa.net/xml/STC/stc-v1.30.xsd"><Astro'
51 'CoordSystem><SpaceFrame><GALACTIC_II/><BARYCENTER/></SpaceFrame><'
52 '/AstroCoordSystem><AstroCoords><Position2D><Value2><C1>12.2</C1><'
53 'C2>0.3</C2></Value2></Position2D></AstroCoords></STCSpec>')[0][1]
54 self.assertEqual(ast.astroSystem.spaceFrame.refFrame, "GALACTIC_II")
55
56
57 class TimeFrameTest(testhelpers.VerboseTest):
58 def testSimple(self):
59 ast = stc.parseSTCX('<STCSpec xmlns="http://www.ivoa.net/xml/STC/stc-'
60 'v1.30.xsd"><AstroCoordSystem><TimeFrame><TOPOCENTER/><TimeScale>TT'
61 '</TimeScale></TimeFrame></AstroCoordSystem></STCSpec>')[0][1]
62 self.assertEqual(ast.astroSystem.timeFrame.refPos.standardOrigin,
63 "TOPOCENTER")
64 self.assertEqual(ast.astroSystem.timeFrame.timeScale, "TT")
65
66
67 class V(stc.STC.STCElement):
68 """A spartan STC-X root for test purposes.
69 """
70
71
72 class STCMappingTest(testhelpers.VerboseTest):
73 def assertMapsto(self, stcsLiteral, stcxExpected):
74 ast = stc.parseSTCS(stcsLiteral)
75 stcxResult = stcxgen.astToStan(ast, V).render(prefixForEmpty="stc")
76 assertEqualWithoutIds(stcxResult, stcxExpected,
77 stcsLiteral)
78
79
80 class OtherCoordTest(STCMappingTest):
81 def testSimple(self):
82 self.assertMapsto("Time TT 2009-03-10T09:56:10.015625",
83 '<V ><AstroCoordSystem ><TimeFrame ><TimeScale>TT</TimeScale><UNKNOWNRefPos /></TimeFrame></AstroCoordSystem><AstroCoords ><Time unit="s"><TimeInstant><ISOTime>2009-03-10T09:56:10.015625</ISOTime></TimeInstant></Time></AstroCoords></V>')
84
85 def testComplex(self):
86 self.assertMapsto("Time TT 2009-03-10T09:56:10.015625 unit s"
87 " Error 0.0001 0.0002 Resolution 0.0001 PixSize 2",
88 '<V ><AstroCoordSystem ><TimeFrame ><TimeScale>TT</TimeScale><UNKNOWNRefPos /></TimeFrame></AstroCoordSystem><AstroCoords ><Time unit="s"><TimeInstant><ISOTime>2009-03-10T09:56:10.015625</ISOTime></TimeInstant><Error>0.0001</Error><Error>0.0002</Error><Resolution>0.0001</Resolution><PixSize>2.0</PixSize></Time></AstroCoords></V>')
89
90 def testRedSpect(self):
91 self.assertMapsto("Spectral NEPTUNE 12 unit Angstrom Error 4 3"
92 " Redshift TOPOCENTER VELOCITY RELATIVISTIC 0.1",
93 '<V ><AstroCoordSystem ><SpectralFrame ><NEPTUNE /></SpectralFrame><RedshiftFrame value_type="VELOCITY"><DopplerDefinition>RELATIVISTIC</DopplerDefinition><TOPOCENTER /></RedshiftFrame></AstroCoordSystem><AstroCoords ><Spectral unit="Angstrom"><Value>12.0</Value><Error>4.0</Error><Error>3.0</Error></Spectral><Redshift unit="km" vel_time_unit="s"><Value>0.1</Value></Redshift></AstroCoords></V>')
94
95 def testTimeParse(self):
96 ast = stc.parseSTCX("""
97 <STCSpec xmlns="http://www.ivoa.net/xml/STC/stc-v1.30.xsd">
98 <AstroCoordSystem id="x">
99 <TimeFrame id="xt"><TimeScale>TT</TimeScale></TimeFrame>
100 </AstroCoordSystem>
101 <AstroCoords coord_system_id="x">
102 <Time><TimeInstant>
103 <ISOTime>2000-01-01T00:00:00</ISOTime>
104 </TimeInstant></Time>
105 </AstroCoords>
106 </STCSpec>""")[0][1]
107 self.assertEqual(ast.time.frame.timeScale, "TT")
108 self.assertEqual(ast.time.value, datetime.datetime(2000, 1, 1, 0, 0))
109 self.assertEqual(ast.time.unit, None)
110
111
112 class SpaceCoordTest(STCMappingTest):
113 def testSimple(self):
114 self.assertMapsto("Position ICRS 12.3 14.2",
115 '<V ><AstroCoordSystem ><SpaceFrame ><ICRS /><UNKNOWNRefPos /><SPHERICAL coord_naxes="2" /></SpaceFrame></AstroCoordSystem><AstroCoords ><Position2D unit="deg"><Value2><C1>12.3</C1><C2>14.2</C2></Value2></Position2D></AstroCoords></V>')
116
117 def testComplex(self):
118 self.assertMapsto("Position ICRS Epoch J200.3 12000.3 14000 unit arcsec"
119 " Error 0.1 0.14 Resolution 0.5 0.55 Size 1 1.1 4.8 2.3 PixSize 0.2 0.2",
120 '<V ><AstroCoordSystem ><SpaceFrame ><ICRS /><UNKNOWNRefPos /><SPHERICAL coord_naxes="2" /></SpaceFrame></AstroCoordSystem><AstroCoords ><Position2D unit="arcsec"><Value2><C1>12000.3</C1><C2>14000.0</C2></Value2><Error2><C1>0.1</C1><C2>0.14</C2></Error2><Resolution2><C1>0.5</C1><C2>0.55</C2></Resolution2><Size2><C1>1.0</C1><C2>1.1</C2></Size2><Size2><C1>4.8</C1><C2>2.3</C2></Size2><PixSize2Radius>0.2</PixSize2Radius><Epoch yearDef="J">200.3</Epoch></Position2D></AstroCoords></V>')
121
122
123 class OtherCoordIntervalTest(STCMappingTest):
124 def testSome(self):
125 self.assertMapsto("TimeInterval TT 2009-03-10T09:56:10.015625"
126 " SpectralInterval 1e10 1e11 unit Hz"
127 " RedshiftInterval VELOCITY 1000 7500 unit km/s",
128 '<V ><AstroCoordSystem ><TimeFrame ><TimeScale>TT</TimeScale><UNKNOWNRefPos /></TimeFrame><SpectralFrame ><UNKNOWNRefPos /></SpectralFrame><RedshiftFrame value_type="VELOCITY"><DopplerDefinition>OPTICAL</DopplerDefinition><UNKNOWNRefPos /></RedshiftFrame></AstroCoordSystem><AstroCoordArea ><TimeInterval ><StartTime><ISOTime>2009-03-10T09:56:10.015625</ISOTime></StartTime></TimeInterval><SpectralInterval unit="Hz"><LoLimit>10000000000.0</LoLimit><HiLimit>100000000000.0</HiLimit></SpectralInterval><RedshiftInterval unit="km" vel_time_unit="s"><LoLimit>1000.0</LoLimit><HiLimit>7500.0</HiLimit></RedshiftInterval></AstroCoordArea></V>')
129
130 def testTimeWithError(self):
131 self.assertMapsto("TimeInterval TT 2009-03-10T09:56:10 2009-03-10T09:57:10 Error"
132 " 4 Resolution 1 2 PixSize 1",
133 '<V ><AstroCoordSystem ><TimeFrame ><TimeScale>TT</TimeScale><UNKNOWNRefPos /></TimeFrame></AstroCoordSystem><AstroCoords ><Time unit="s"><Error>4.0</Error><Resolution>1.0</Resolution><Resolution>2.0</Resolution><PixSize>1.0</PixSize></Time></AstroCoords><AstroCoordArea ><TimeInterval ><StartTime><ISOTime>2009-03-10T09:56:10</ISOTime></StartTime><StopTime><ISOTime>2009-03-10T09:57:10</ISOTime></StopTime></TimeInterval></AstroCoordArea></V>')
134
135 def testSpectralWithError(self):
136 self.assertMapsto("SpectralInterval 1e10 1e11 unit Hz Error 1 2",
137 '<V ><AstroCoordSystem ><SpectralFrame ><UNKNOWNRefPos /></SpectralFrame></AstroCoordSystem><AstroCoords ><Spectral unit="Hz"><Error>1.0</Error><Error>2.0</Error></Spectral></AstroCoords><AstroCoordArea ><SpectralInterval unit="Hz"><LoLimit>10000000000.0</LoLimit><HiLimit>100000000000.0</HiLimit></SpectralInterval></AstroCoordArea></V>')
138
139
140 class SpaceCoordIntervalTest(STCMappingTest):
141 def test1D(self):
142 self.assertMapsto("PositionInterval UNKNOWNFrame CART1 1 2 unit mm",
143 '<V ><AstroCoordSystem ><SpaceFrame ><UNKNOWNFrame /><UNKNOWNRefPos /><CARTESIAN coord_naxes="1" /></SpaceFrame></AstroCoordSystem><AstroCoordArea ><PositionScalarInterval unit="mm"><LoLimit>1.0</LoLimit><HiLimit>2.0</HiLimit></PositionScalarInterval></AstroCoordArea></V>')
144
145 def test2D(self):
146 self.assertMapsto("PositionInterval ICRS 12 13 14 15",
147 '<V ><AstroCoordSystem ><SpaceFrame ><ICRS /><UNKNOWNRefPos /><SPHERICAL coord_naxes="2" /></SpaceFrame></AstroCoordSystem><AstroCoordArea ><Position2VecInterval unit="deg"><LoLimit2Vec><C1>12.0</C1><C2>13.0</C2></LoLimit2Vec><HiLimit2Vec><C1>14.0</C1><C2>15.0</C2></HiLimit2Vec></Position2VecInterval></AstroCoordArea></V>')
148
149 def test2DWithSize(self):
150 self.assertMapsto("PositionInterval ICRS 12 13 14 15 Size 1 1.5 1.75 2",
151 '<V ><AstroCoordSystem ><SpaceFrame ><ICRS /><UNKNOWNRefPos /><SPHERICAL coord_naxes="2" /></SpaceFrame></AstroCoordSystem><AstroCoords ><Position2D unit="deg"><Size2><C1>1.0</C1><C2>1.5</C2></Size2><Size2><C1>1.75</C1><C2>2.0</C2></Size2></Position2D></AstroCoords><AstroCoordArea ><Position2VecInterval unit="deg"><LoLimit2Vec><C1>12.0</C1><C2>13.0</C2></LoLimit2Vec><HiLimit2Vec><C1>14.0</C1><C2>15.0</C2></HiLimit2Vec></Position2VecInterval></AstroCoordArea></V>')
152
153 def test3D(self):
154 self.assertMapsto("Time TT 2000-01-01T12:00:00"
155 " PositionInterval ECLIPTIC CART3 12 13 10 14 15 9",
156 '<V ><AstroCoordSystem ><TimeFrame ><TimeScale>TT</TimeScale><UNKNOWNRefPos /></TimeFrame><SpaceFrame ><ECLIPTIC><Equinox>J2000.0</Equinox></ECLIPTIC><UNKNOWNRefPos /><CARTESIAN coord_naxes="3" /></SpaceFrame></AstroCoordSystem><AstroCoords ><Time unit="s"><TimeInstant><ISOTime>2000-01-01T12:00:00</ISOTime></TimeInstant></Time></AstroCoords><AstroCoordArea ><Position3VecInterval unit="m"><LoLimit3Vec><C1>12.0</C1><C2>13.0</C2><C3>10.0</C3></LoLimit3Vec><HiLimit3Vec><C1>14.0</C1><C2>15.0</C2><C3>9.0</C3></HiLimit3Vec></Position3VecInterval></AstroCoordArea></V>')
157
158
159 class RegionTest(STCMappingTest):
160 def assertRaisesValueError(self, stcsExpr):
161 self.assertRaises(stc.STCValueError,
162 lambda s:stcxgen.astToStan(stc.parseSTCS(s), V),
163 stcsExpr)
164
165 def testCircles(self):
166 self.assertRaisesValueError("Circle UNKNOWNFrame CART1 12 4")
167 self.assertMapsto("Circle ICRS 12 13 1",
168 '<V ><AstroCoordSystem ><SpaceFrame ><ICRS /><UNKNOWNRefPos /><SPHERICAL coord_naxes="2" /></SpaceFrame></AstroCoordSystem><AstroCoordArea ><Circle unit="deg"><Center><C1>12.0</C1><C2>13.0</C2></Center><Radius>1.0</Radius></Circle></AstroCoordArea></V>')
169 self.assertMapsto("Circle UNKNOWNFrame CART3 12 13 1 4",
170 '<V ><AstroCoordSystem ><SpaceFrame ><UNKNOWNFrame /><UNKNOWNRefPos /><CARTESIAN coord_naxes="3" /></SpaceFrame></AstroCoordSystem><AstroCoordArea ><Sphere unit="m"><Radius>4.0</Radius><Center><C1>12.0</C1><C2>13.0</C2><C3>1.0</C3></Center></Sphere></AstroCoordArea></V>')
171
172 def testEllipse(self):
173 self.assertMapsto("Ellipse ICRS 12 13 1 0.75 0",
174 '<V ><AstroCoordSystem ><SpaceFrame ><ICRS /><UNKNOWNRefPos /><SPHERICAL coord_naxes="2" /></SpaceFrame></AstroCoordSystem><AstroCoordArea ><Ellipse unit="deg"><Center><C1>12.0</C1><C2>13.0</C2></Center><SemiMajorAxis>1.0</SemiMajorAxis><SemiMinorAxis>0.75</SemiMinorAxis><PosAngle>0.0</PosAngle></Ellipse></AstroCoordArea></V>')
175
176 def testBox(self):
177 self.assertMapsto("Box fillfactor 0.1 ICRS 70 190 23 18",
178 '<V ><AstroCoordSystem ><SpaceFrame ><ICRS /><UNKNOWNRefPos /><SPHERICAL coord_naxes="2" /></SpaceFrame></AstroCoordSystem><AstroCoordArea ><Box fill_factor="0.1" unit="deg"><Center><C1>70.0</C1><C2>190.0</C2></Center><Size><C1>23.0</C1><C2>18.0</C2></Size></Box></AstroCoordArea></V>')
179 self.assertRaisesValueError("Box UNKNOWNFrame CART1 1 1")
180
181 def testPolygon(self):
182 self.assertMapsto("Polygon ICRS 70 190 23 18 12 45 30 -10",
183 '<V ><AstroCoordSystem ><SpaceFrame ><ICRS /><UNKNOWNRefPos /><SPHERICAL coord_naxes="2" /></SpaceFrame></AstroCoordSystem><AstroCoordArea ><Polygon unit="deg"><Vertex><Position><C1>70.0</C1><C2>190.0</C2></Position></Vertex><Vertex><Position><C1>23.0</C1><C2>18.0</C2></Position></Vertex><Vertex><Position><C1>12.0</C1><C2>45.0</C2></Position></Vertex><Vertex><Position><C1>30.0</C1><C2>-10.0</C2></Position></Vertex></Polygon></AstroCoordArea></V>')
184 self.assertRaisesValueError("Polygon UNKNOWNFrame CART1 1 2 3 4")
185
186 def testConvex(self):
187 self.assertMapsto("Convex ICRS 70 190 23 0.125 12 45 30 -0.25",
188 '<V ><AstroCoordSystem ><SpaceFrame ><ICRS /><UNKNOWNRefPos /><UNITSPHERE coord_naxes="3" /></SpaceFrame></AstroCoordSystem><AstroCoordArea ><Convex ><Halfspace><Vector><C1>70.0</C1><C2>190.0</C2><C3>23.0</C3></Vector><Offset>0.125</Offset></Halfspace><Halfspace><Vector><C1>12.0</C1><C2>45.0</C2><C3>30.0</C3></Vector><Offset>-0.25</Offset></Halfspace></Convex></AstroCoordArea></V>')
189
190
191 class CompoundTest(STCMappingTest):
192 def testSimpleUnion(self):
193 self.assertMapsto("Union ICRS (Circle 10 10 2 Box 11 11 2 3)",
194 '<V ><AstroCoordSystem ><SpaceFrame ><ICRS /><UNKNOWNRefPos /><SPHERICAL coord_naxes="2" /></SpaceFrame></AstroCoordSystem><AstroCoordArea ><Union><Circle unit="deg"><Center><C1>10.0</C1><C2>10.0</C2></Center><Radius>2.0</Radius></Circle><Box unit="deg"><Center><C1>11.0</C1><C2>11.0</C2></Center><Size><C1>2.0</C1><C2>3.0</C2></Size></Box></Union></AstroCoordArea></V>')
195
196 def testSimpleIntersection(self):
197 self.assertMapsto("Intersection ICRS (Polygon 10 2 2 10 10 10"
198 " Ellipse 11 11 2 3 30)",
199 '<V ><AstroCoordSystem ><SpaceFrame ><ICRS /><UNKNOWNRefPos /><SPHERICAL coord_naxes="2" /></SpaceFrame></AstroCoordSystem><AstroCoordArea ><Intersection><Polygon unit="deg"><Vertex><Position><C1>10.0</C1><C2>2.0</C2></Position></Vertex><Vertex><Position><C1>2.0</C1><C2>10.0</C2></Position></Vertex><Vertex><Position><C1>10.0</C1><C2>10.0</C2></Position></Vertex></Polygon><Ellipse unit="deg"><Center><C1>11.0</C1><C2>11.0</C2></Center><SemiMajorAxis>2.0</SemiMajorAxis><SemiMinorAxis>3.0</SemiMinorAxis><PosAngle>30.0</PosAngle></Ellipse></Intersection></AstroCoordArea></V>')
200
201 def testUnionWithNot(self):
202 self.assertMapsto("Union ICRS (Circle 10 10 2 Not (Box 11 11 2 3))",
203 '<V ><AstroCoordSystem ><SpaceFrame ><ICRS /><UNKNOWNRefPos /><SPHERICAL coord_naxes="2" /></SpaceFrame></AstroCoordSystem><AstroCoordArea ><Union><Circle unit="deg"><Center><C1>10.0</C1><C2>10.0</C2></Center><Radius>2.0</Radius></Circle><Negation><Box unit="deg"><Center><C1>11.0</C1><C2>11.0</C2></Center><Size><C1>2.0</C1><C2>3.0</C2></Size></Box></Negation></Union></AstroCoordArea></V>')
204
205 def testSimpleDifference(self):
206 self.assertMapsto("Difference ICRS (Circle 10 10 2 Not (Box 11 11 2 3))",
207 '<V ><AstroCoordSystem ><SpaceFrame ><ICRS /><UNKNOWNRefPos /><SPHERICAL coord_naxes="2" /></SpaceFrame></AstroCoordSystem><AstroCoordArea ><Difference><Circle unit="deg"><Center><C1>10.0</C1><C2>10.0</C2></Center><Radius>2.0</Radius></Circle><Negation2><Box unit="deg"><Center><C1>11.0</C1><C2>11.0</C2></Center><Size><C1>2.0</C1><C2>3.0</C2></Size></Box></Negation2></Difference></AstroCoordArea></V>')
208
209 def testInsaneRegion(self):
210 self.assertMapsto("Difference ICRS (AllSky Union (Circle 10 10 2"
211 " Intersection (Polygon 10 2 2 10 10 10 Ellipse 11 11 2 3 30"
212 " Not (Difference (Circle 12 12 3 Box 11 11 2 3)))))",
213 '<V ><AstroCoordSystem ><SpaceFrame ><ICRS /><UNKNOWNRefPos /><SPHERICAL coord_naxes="2" /></SpaceFrame></AstroCoordSystem><AstroCoordArea ><Difference><AllSky unit="deg" /><Union2><Circle unit="deg"><Center><C1>10.0</C1><C2>10.0</C2></Center><Radius>2.0</Radius></Circle><Intersection><Polygon unit="deg"><Vertex><Position><C1>10.0</C1><C2>2.0</C2></Position></Vertex><Vertex><Position><C1>2.0</C1><C2>10.0</C2></Position></Vertex><Vertex><Position><C1>10.0</C1><C2>10.0</C2></Position></Vertex></Polygon><Ellipse unit="deg"><Center><C1>11.0</C1><C2>11.0</C2></Center><SemiMajorAxis>2.0</SemiMajorAxis><SemiMinorAxis>3.0</SemiMinorAxis><PosAngle>30.0</PosAngle></Ellipse><Negation><Difference><Circle unit="deg"><Center><C1>12.0</C1><C2>12.0</C2></Center><Radius>3.0</Radius></Circle><Box2 unit="deg"><Center><C1>11.0</C1><C2>11.0</C2></Center><Size><C1>2.0</C1><C2>3.0</C2></Size></Box2></Difference></Negation></Intersection></Union2></Difference></AstroCoordArea></V>')
214
215
216 class STCSRoundtripTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
217 def _runTest(self, sample):
218 src, expected = sample
219 if expected is None:
220 expected = src
221 ast = stc.parseSTCS(src)
222 xml = stc.getSTCXProfile(ast)
223 ast2 = stc.parseSTCX(xml)[0][1]
224 stcs = stc.getSTCS(ast2)
225 self.assertEqual(stcs, expected)
226
227 samples = [
228 ("AllSky ICRS", None),
229 ("Box ICRS 12 1 15 3", 'Box ICRS 12.0 1.0 15.0 3.0'),
230 ]
231
232
233 if __name__=="__main__":
234 testhelpers.main(STCSRoundtripTest)