Debian Codegraph @

tests/stcconvtest.py

raw
1 """
2 Tests for STC conversions and conforming.
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 re
12
13 from gavo.helpers import testhelpers
14
15 from gavo import stc
16 from gavo import utils
17 from gavo.stc import bboxes
18 from gavo.stc import units
19 from gavo.utils import DEG
20
21
22
23 class UnitTest(testhelpers.VerboseTest):
24 """tests for simple unit conversion.
25 """
26 def testSpatial(self):
27 self.assertAlmostEqual(units.getDistConv("mm", "km")(1), 1e-6)
28 self.assertAlmostEqual(units.getDistConv("lyr", "m")(1),
29 9.4607304725808e15, 13)
30 self.assertAlmostEqual(units.getDistConv("pc", "AU")(0.00000484813681113),
31 1., 10)
32
33 def testAngular(self):
34 self.assertAlmostEqual(units.getAngleConv("rad", "deg")(1),
35 57.2957795130823, 13)
36 self.assertAlmostEqual(units.getAngleConv("h", "deg")(1), 15.)
37 self.assertAlmostEqual(units.getAngleConv("arcsec", "h")(1),
38 1.8518518518518518e-05)
39
40 def testTime(self):
41 self.assertAlmostEqual(units.getTimeConv("cy", "s")(1),
42 3.15576e9)
43
44 def testFrequency(self):
45 self.assertAlmostEqual(units.getFreqConv("eV", "GHz")(1),
46 241798.94566132812)
47 self.assertAlmostEqual(units.getFreqConv("MeV", "Hz")(1),
48 2.4179894566132813e+20)
49
50 def testSpectral(self):
51 self.assertAlmostEqual(units.getSpectralConv("eV", "Angstrom")(4),
52 3099.6046858274931)
53 self.assertAlmostEqual(units.getSpectralConv("mm", "MHz")(50000),
54 5.9958491599999997)
55
56 def testFromParallax(self):
57 conv = units.getVectorConverter(
58 ("deg", "deg", "arcsec"), ("deg", "deg", "kpc"))
59 self.assertAlmostEqual(conv((2,2,2))[2], 0.0005)
60
61 def testToParallax(self):
62 conv = units.getVectorConverter(
63 ("deg", "deg", "m"), ("deg", "deg", "arcmin"))
64 self.assertAlmostEqual(conv((0,0,3.0856775813e+16/60.))[2], 1.)
65
66 def testRaises(self):
67 self.assertRaisesWithMsg(stc.STCUnitError, "One of 'deg' or 'm'"
68 " is no valid distance unit", units.getDistConv, ('deg', 'm'))
69 self.assertRaisesWithMsg(stc.STCUnitError, "One of 'cy' or 'pc'"
70 " is no valid time unit", units.getTimeConv, ('cy', 'pc'))
71
72
73 class GenericConverterTest(testhelpers.VerboseTest):
74 def testScalars(self):
75 self.assertAlmostEqual(units.getBasicConverter("AU", "lyr")(500),
76 0.0079062537044346792)
77 self.assertAlmostEqual(units.getBasicConverter("arcmin", "rad")(
78 60*57), 0.99483767363676778)
79 self.assertAlmostEqual(units.getBasicConverter("yr", "s")(0.1),
80 3.155760e6)
81 self.assertAlmostEqual(units.getBasicConverter("mm", "GHz")(210),
82 1.4275831333333335)
83
84 def testRedshifts(self):
85 self.assertAlmostEqual(units.getRedshiftConverter("km", "h",
86 "m", "s")(3.6), 1)
87
88 def testSpatial(self):
89 self.assertAlmostEqualVector(
90 units.getVectorConverter(("deg", "arcmin"), ("rad", "rad"))((2, 300)),
91 (0.034906585039886591, 0.087266462599716474))
92 self.assertAlmostEqualVector(
93 units.getVectorConverter(("m", "lyr", "deg"), ("km", "pc", "arcsec"))(
94 (2100, 6.6, 0.001)),
95 (2.1000000000000001, 2.0235691991103377, 3.6000000000000001))
96
97 def testVelocity(self):
98 self.assertAlmostEqualVector(units.getVelocityConverter(("m",),
99 ("s",), "km", "h")((1,)), (3.6,))
100 self.assertAlmostEqualVector(units.getVelocityConverter(("deg", "deg"),
101 ("cy", "cy"), "arcsec", "a")((1,2)),
102 (36.000000000000007, 72.000000000000014))
103 self.assertAlmostEqualVector(units.getVelocityConverter(("rad",),
104 ("cy",), "arcsec", "yr")((1,)), (2062.6480624709639,))
105
106 def testRaising(self):
107 self.assertRaises(stc.STCUnitError, units.getBasicConverter,
108 "Mhz", "lyr")
109 self.assertRaises(stc.STCUnitError, units.getVectorConverter,
110 ("m", "m"), ("km", "pc", "Mpc"))
111 self.assertRaises(stc.STCUnitError, units.getVectorConverter,
112 ("m", "m", "deg"), ("km", "pc", "Hz"))
113
114
115 class WiggleCoercionTest(testhelpers.VerboseTest):
116 """tests for unit coercion of wiggles parsed from STX-C.
117 """
118 def _getAST(self, coo):
119 return stc.parseSTCX(('<ObservationLocation xmlns="%s">'%stc.STCNamespace)+
120 '<AstroCoordSystem id="x"><SpaceFrame><ICRS/></SpaceFrame>'
121 '</AstroCoordSystem>'
122 '<AstroCoords coord_system_id="x">'+
123 coo+'</AstroCoords></ObservationLocation>')[0][1]
124
125 def testBasic(self):
126 ast = self._getAST('<Position2D unit="deg"><C1>1</C1><C2>2</C2>'
127 '<Error2 unit="arcsec"><C1>0.1</C1><C2>0.15</C2></Error2>'
128 '<Size2><C1 unit="arcsec">60</C1><C2 unit="arcmin">1</C2></Size2>'
129 '<Resolution2Radius unit="rad">0.00001</Resolution2Radius>'
130 '</Position2D>')
131 pos = ast.place
132 self.assertAlmostEqual(pos.value[0], 1)
133 self.assertAlmostEqual(pos.error.values[0][0], 2.7777777777777779e-05)
134 self.assertAlmostEqual(pos.resolution.radii[0], 0.00057295779513082329)
135 self.assertAlmostEqual(pos.size.values[0][1], 0.016666666666666666)
136
137 def testWeirdBase(self):
138 ast = self._getAST('<Position2D><C1 unit="arcsec">1</C1>'
139 '<C2 unit="rad">2</C2>'
140 '<Error2 unit="arcsec"><C1>0.1</C1><C2>0.15</C2></Error2>'
141 '<Size2><C1 unit="arcmin">0.1</C1><C2 unit="arcsec">1</C2></Size2>'
142 '<Resolution2Radius unit="rad">0.00001</Resolution2Radius>'
143 '</Position2D>')
144 pos = ast.place
145 self.assertAlmostEqual(pos.error.values[0][0], 0.1)
146 self.assertAlmostEqual(pos.error.values[0][1], 7.2722052166430391e-07)
147 self.assertAlmostEqual(pos.resolution.radii[0], 2.0626480624709638)
148 self.assertAlmostEqual(pos.size.values[0][0], 6.0)
149 self.assertAlmostEqual(pos.size.values[0][1], 4.8481368110953598e-06)
150
151 def testWithTime(self):
152 ast = self._getAST('<Velocity2D><C1 unit="arcsec" vel_time_unit="yr">1'
153 '</C1><C2 unit="rad" vel_time_unit="cy">2</C2>'
154 '<Error2 unit="arcmin" vel_time_unit="cy"><C1>0.01</C1><C2>0.015</C2>'
155 '</Error2>'
156 '<Size2><C1 unit="rad" vel_time_unit="s">1</C1>'
157 '<C2 unit="arcsec" vel_time_unit="yr">1</C2></Size2>'
158 '<Resolution2Radius unit="deg" vel_time_unit="cy">0.00001'
159 '</Resolution2Radius>'
160 '</Velocity2D>')
161 pos = ast.velocity
162 self.assertEqual(pos.unit, ('arcsec', 'rad'))
163 self.assertEqual(pos.velTimeUnit, ('yr', 'cy'))
164 self.assertAlmostEqual(pos.error.values[0][0], 0.006)
165 self.assertAlmostEqual(pos.error.values[0][1], 4.3633231299858233e-06)
166 self.assertAlmostEqual(pos.size.values[0][0], 6509222249623.3682)
167
168 def testOthersDefaultUnits(self):
169 ast = self._getAST('<Time unit="s"><TimeInstant>'
170 '<ISOTime>2009-03-10T09:56:10'
171 '</ISOTime></TimeInstant><Error>2</Error><Resolution unit="h">'
172 '0.001</Resolution><Size unit="yr">0.1</Size></Time>'
173 '<Spectral unit="Angstrom"><Value>12.0</Value><Error unit="nm">'
174 '0.01</Error><Error>0.02</Error></Spectral>'
175 '<Redshift><Value>0.1</Value><Error>0.01</Error></Redshift>')
176 t = ast.time
177 self.assertEqual(t.unit, "s")
178 self.assertEqual(t.error.values[0], 2)
179 self.assertAlmostEqual(t.resolution.values[0], 3.6)
180 self.assertAlmostEqual(t.size.values[0], 3155760.0)
181 s = ast.freq
182 self.assertEqual(s.unit, "Angstrom")
183 self.assertAlmostEqual(s.error.values[0], 0.1)
184 self.assertAlmostEqual(s.error.values[1], 0.2)
185 r = ast.redshift
186 self.assertEqual(r.unit, None)
187 self.assertAlmostEqual(r.error.values[0], 0.01)
188
189 def testOthersFunnyUnits(self):
190 ast = self._getAST('<Time unit="yr"><TimeInstant>'
191 '<ISOTime>2009-03-10T09:56:10'
192 '</ISOTime></TimeInstant><Error>0.0001</Error><Resolution unit="d">'
193 '1</Resolution><Size unit="cy">0.1</Size></Time>'
194 '<Spectral unit="keV"><Value>12.0</Value><Error unit="eV">'
195 '0.01</Error><Resolution unit="Hz">200</Resolution></Spectral>'
196 '<Redshift unit="pc" vel_time_unit="cy"><Value>0.1</Value>'
197 '<Error unit="km" vel_time_unit="s">10</Error></Redshift>')
198 t = ast.time
199 self.assertEqual(t.unit, "yr")
200 self.assertEqual(t.error.values[0], 0.0001)
201 self.assertAlmostEqual(t.resolution.values[0], 0.0027378507871321013)
202 self.assertAlmostEqual(t.size.values[0], 10.0)
203 s = ast.freq
204 self.assertEqual(s.unit, "keV")
205 self.assertAlmostEqual(s.error.values[0], 1e-5)
206 self.assertAlmostEqual(s.resolution.values[0], 8.2713346600000008e-16)
207 r = ast.redshift
208 self.assertEqual(r.unit, 'pc')
209 self.assertAlmostEqual(r.error.values[0], 0.0010227121651092258)
210
211
212 class GeoCoercionTest(testhelpers.VerboseTest):
213 """tests for various forms of unit coercion with geometries.
214 """
215 def _getAST(self, coo, pos=""):
216 if pos:
217 pos = '<AstroCoords coord_system_id="x">%s</AstroCoords>'%pos
218 return stc.parseSTCX(('<ObservationLocation xmlns="%s">'%stc.STCNamespace)+
219 '<AstroCoordSystem id="x"><SpaceFrame><ICRS/></SpaceFrame>'
220 '</AstroCoordSystem>'+pos+
221 '<AstroCoordArea coord_system_id="x">'+
222 coo+'</AstroCoordArea></ObservationLocation>')[0][1]
223
224 def testCircleDefault(self):
225 ast = self._getAST("<Circle><Center><C1>1.5</C1><C2>1.5</C2></Center>"
226 '<Radius pos_unit="arcsec">1</Radius></Circle>')
227 a = ast.areas[0]
228 self.assertAlmostEqual(a.radius, 1/3600.)
229
230 def testCircleCenterUnit(self):
231 ast = self._getAST('<Circle><Center unit="km">'
232 '<C1>1.5</C1><C2>1.5</C2></Center>'
233 '<Radius pos_unit="m">1</Radius></Circle>')
234 a = ast.areas[0]
235 self.assertAlmostEqual(a.radius, 1/1000.)
236
237 def testCircleCompUnit(self):
238 ast = self._getAST('<Circle><Center>'
239 '<C1 unit="kpc">1.5</C1><C2 unit="kpc">1.5</C2></Center>'
240 '<Radius pos_unit="pc">2</Radius></Circle>')
241 a = ast.areas[0]
242 self.assertAlmostEqual(a.radius, 2/1000.)
243
244 def testCircleGlobalUnit(self):
245 ast = self._getAST('<Circle unit="kpc"><Center>'
246 '<C1>1.5</C1><C2>1.5</C2></Center>'
247 '<Radius pos_unit="pc">2</Radius></Circle>')
248 a = ast.areas[0]
249 self.assertAlmostEqual(a.radius, 2/1000.)
250
251 def testEllipse(self):
252 ast = self._getAST('<Ellipse unit="kpc"><Center>'
253 '<C1>1.5</C1><C2>1.5</C2></Center>'
254 '<SemiMajorAxis pos_unit="pc">2</SemiMajorAxis>'
255 '<SemiMinorAxis pos_unit="lyr">1</SemiMinorAxis>'
256 '<PosAngle unit="rad">1</PosAngle>'
257 '</Ellipse>')
258 a = ast.areas[0]
259 self.assertAlmostEqual(a.smajAxis, 2/1000.)
260 self.assertAlmostEqual(a.sminAxis, 0.00030660139380459661)
261 self.assertAlmostEqual(a.posAngle, 57.295779513082323)
262
263 def testBoxGlobalCoo(self):
264 ast = self._getAST('<Box unit="deg"><Center>'
265 '<C1>1.5</C1><C2>1.5</C2></Center>'
266 '<Size unit="arcsec"><C1>1</C1><C2>2</C2></Size>'
267 '</Box>')
268 a = ast.areas[0]
269 self.assertAlmostEqual(a.boxsize[0], 1/3600.)
270 self.assertAlmostEqual(a.boxsize[1], 2/3600.)
271
272 def testBoxCenterCoo(self):
273 ast = self._getAST('<Box><Center unit="deg">'
274 '<C1>1.5</C1><C2>1.5</C2></Center>'
275 '<Size unit="arcsec"><C1>1</C1><C2>2</C2></Size>'
276 '</Box>')
277 a = ast.areas[0]
278 self.assertAlmostEqual(a.boxsize[0], 1/3600.)
279 self.assertAlmostEqual(a.boxsize[1], 2/3600.)
280
281 def testBoxCompCoo(self):
282 ast = self._getAST('<Box><Center>'
283 '<C1>1.5</C1><C2>1.5</C2></Center>'
284 '<Size><C1 unit="arcmin">1</C1><C2 unit="arcsec">2</C2></Size>'
285 '</Box>')
286 a = ast.areas[0]
287 self.assertAlmostEqual(a.boxsize[0], 1/60.)
288 self.assertAlmostEqual(a.boxsize[1], 2/3600.)
289
290 def testBoxPositionCoerc(self):
291 ast = self._getAST('<Box><Center>'
292 '<C1>1.5</C1><C2>1.5</C2></Center>'
293 '<Size><C1 unit="arcmin">1</C1><C2 unit="arcsec">2</C2></Size>'
294 '</Box>',
295 '<Position2D><Value2><C1 unit="rad"/><C2 unit="deg"/>'
296 '</Value2></Position2D>')
297 a = ast.areas[0]
298 self.assertAlmostEqual(a.boxsize[0], 1/60.*utils.DEG)
299 self.assertAlmostEqual(a.boxsize[1], 2/3600.)
300 self.assertAlmostEqual(a.center[0], 1.5*utils.DEG)
301 self.assertAlmostEqual(a.center[1], 1.5)
302
303 def testCirclePositionCoerc(self):
304 ast = self._getAST('<Circle unit="kpc"><Center>'
305 '<C1>1.5</C1><C2>1.5</C2></Center>'
306 '<Radius pos_unit="pc">2</Radius></Circle>',
307 '<Position2D><Value2><C1 unit="m"/><C2 unit="km"/>'
308 '</Value2></Position2D>')
309 a = ast.areas[0]
310 self.assertAlmostEqual(a.center[0]*1e-13, 1.5*units.onePc*1000*1e-13)
311 self.assertAlmostEqual(a.center[1]*1e-13, 1.5*units.onePc*1e-13)
312 self.assertAlmostEqual(a.radius*1e-13, 2*units.onePc*1e-13)
313
314
315 class UnitConformTest(testhelpers.VerboseTest):
316 """tests for simple unit conforming.
317 """
318 def testSimplePos(self):
319 ast0 = stc.parseSTCS("Position ICRS 10 12 unit deg Error 0.01 0.01"
320 " Spectral 1250 unit MHz")
321 ast1 = stc.parseSTCS("Position ICRS unit arcmin Spectral unit GHz")
322 res = stc.conformTo(ast0, ast1)
323 self.assertEqual(res.place.unit, ("arcmin", "arcmin"))
324 self.assertEqual(res.place.value[0], 10*60)
325 self.assertEqual(res.place.value[1], 12*60)
326 self.assertAlmostEqual(res.place.error.radii[0], 0.6)
327 self.assertEqual(res.freq.unit, "GHz")
328 self.assertEqual(res.freq.value, 1.25)
329
330 def testAreaConform(self):
331 ast0 = stc.parseSTCS("PositionInterval ICRS 9 11 11 13 Position 10 12"
332 " RedshiftInterval 1000 2000 Redshift 1500 unit m/s")
333 ast1 = stc.parseSTCS("Position ICRS unit arcmin Redshift unit km/s")
334 res = stc.conformTo(ast0, ast1)
335 self.assertEqual(res.place.value[0], 10*60)
336 self.assertEqual(res.areas[0].lowerLimit[0], 9*60)
337 self.assertEqual(res.areas[0].upperLimit[1], 13*60)
338 self.assertEqual(res.redshiftAs[0].upperLimit, 2)
339 self.assertEqual(res.redshift.unit, "km")
340 self.assertEqual(res.redshift.velTimeUnit, "s")
341
342
343 def _cutExcessiveDigits(s):
344 return re.sub(r"(\.[0-9]{8})[0-9]+", r"\1", s)
345
346
347 class _STCSMatchTestBase(testhelpers.VerboseTest):
348 def assertMatchingSTCS(self, srcSTCS, sysSTCS, expected):
349 srcAst, sysAst = stc.parseSTCS(srcSTCS), stc.parseSTCS(sysSTCS)
350 found = stc.getSTCS(stc.conformTo(srcAst, sysAst))
351 self.assertEqual(
352 _cutExcessiveDigits(found),
353 _cutExcessiveDigits(expected))
354
355
356 class GeometryConformTest(_STCSMatchTestBase):
357 """tests for conforming of Boxes, Circles and friends.
358 """
359 def testCircleToGal(self):
360 self.assertMatchingSTCS("Circle ICRS 45 -60 1",
361 "Position GALACTIC unit rad",
362 "Circle GALACTIC 4.85565465308 -0.881494801129 0.0174532925199 unit rad")
363
364 def testCircleFromGal(self):
365 self.assertMatchingSTCS(
366 "Circle GALACTIC 4.85565465308 -0.881494801129 0.0174532925199 unit rad",
367 "Position ICRS",
368 "Circle ICRS 44.9999999998 -60.0000000001 0.999999999998")
369
370 def testBox(self):
371 self.assertMatchingSTCS(
372 "Box ICRS 360000 36000 20 30 unit arcsec",
373 "Position ECLIPTIC J1950",
374 "Polygon ECLIPTIC J1950.0 99.4089149593 -13.1039695064 99.4077016165 -13.0873447912 99.4189071078 -13.0865685226 99.420121778 -13.1031931456")
375
376 def testVelocityNoSystem(self):
377 self.assertMatchingSTCS(
378 "Position ICRS VelocityInterval 0.1 0.2 unit arcsec/yr",
379 "Position ICRS VelocityInterval unit deg/cy",
380 "Position ICRS VelocityInterval 0.00277777777778 0.00555555555556 unit"
381 " deg/cy")
382
383
384 class TimeConformTest(_STCSMatchTestBase):
385 def testNoDestSystem(self):
386 self.assertMatchingSTCS("Time TT 2005-03-07T16:33:20",
387 "Position GALACTIC unit rad",
388 "Time TT 2005-03-07T16:33:20")
389
390 def testToUTC(self):
391 self.assertMatchingSTCS(
392 "Time TT 2005-03-07T16:33:20",
393 "Time UTC",
394 "Time UTC 2005-03-07T16:33:20.184000")
395
396 def testFromUTC(self):
397 self.assertMatchingSTCS(
398 "Time UTC 2005-03-07T16:33:20.184000",
399 "Time TT",
400 "Time TT 2005-03-07T16:33:20")
401
402 def testTwostep(self):
403 self.assertMatchingSTCS(
404 "Time TCB 2005-03-07T16:33:20",
405 "Time TAI",
406 "Time TAI 2005-03-07T16:31:57.946772")
407
408
409 class HeadingTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
410 def _runTest(self, sample):
411 args, expected = sample
412 res = bboxes.getHeading(*(a*DEG for a in args))
413 self.assertAlmostEqual(res, expected*DEG, 7,
414 "%s, %s!=%s"%(args, expected, res/DEG))
415
416 samples = [
417 ((0, 0, 1, 0), 90.),
418 ((0, 0, -1, 0), 270.),
419 ((0, 0, 0, 1), 0.),
420 ((0, 20, 0, 1), 180.),
421 ((50, 20, 70, 40), 36.2122360568),
422 #5
423 ((50, -20, 70, -40), 180-36.2122360568),
424 ((100-50, -20, 100-70, -40), 180+36.2122360568),
425 ((100-50, 20, 100-70, 40), 360-36.2122360568),
426 ((50, 20, 230, 40), 0),
427 ((230, 40, 50, 20), 0),
428 #10
429 ((230, 90, 50, 20), 180),
430 ((120, 40, 110, 40), 273.218731205),
431 ((110, -40, 120, -10), 19.2250728671),
432 ((110, -40, 150, -30), 85.5408032012),
433 ((110, -40, 210, -30), 119.355123886),
434 ]
435
436
437 class GreatCircleSpecialTest(testhelpers.VerboseTest):
438
439 def testLatForLongLat(self):
440 gc = bboxes.GCSegment.fromDegrees(20, 60, 130, 60)
441 for int, expected in [
442 (20, 60),
443 (75, 71.677478167328047),
444 (130, 60)]:
445 self.assertAlmostEqual(gc.latForLong(int*DEG), expected*DEG)
446
447 def testLatForLongMerid(self):
448 gc = bboxes.GCSegment.fromDegrees(20, 60, 20, -60)
449 self.assertAlmostEqual(gc.latForLong(23*DEG), 20*DEG)
450
451 def testLatForLongOblique(self):
452 gc = bboxes.GCSegment.fromDegrees(20, -60, 180, 45)
453 for int, expected in [
454 (20, -60),
455 (75, -68.171528804315329),
456 (156.65, -0.0066479410359413316),
457 (180, 45)]:
458 self.assertAlmostEqual(gc.latForLong(int*DEG), expected*DEG, 7,
459 "Bad sample: %f %f"%(int, expected))
460
461 def testNoNull(self):
462 self.assertRaisesWithMsg(ValueError,
463 "Null segment: start and end are identical",
464 bboxes.GCSegment,
465 (0.1, 0.3, 0.1, 0.3))
466
467 class GreatCircleBboxTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
468 def _assertBBox(self, found, expected):
469 # lazy: found is in rad, expected in deg
470 terminal = [(0,0,0,0)]
471 for fb, eb in zip(found+terminal, expected+terminal):
472 fb = tuple(f/utils.DEG for f in fb)
473 for f, e in zip(fb, eb):
474 self.assertAlmostEqual(f, e, 7, "%s!=%s"%(fb, eb))
475
476 def _runTest(self, sample):
477 args, expected = sample
478 gc = bboxes.GCSegment.fromDegrees(*args)
479 self._assertBBox(gc.getBBs(), expected)
480
481 samples = [
482 ((20, 60, 120, 60), [(20, 60, 120, 69.63942512)]),
483 ((20, -60, 120, -60), [(20, -69.63942512, 120, -60)]),
484 ((0, 0, 10, 1), [(0, 0, 10, 1)]),
485 ((0, -10, 20, 1), [(0, -10, 20, 1)]),
486 ((20, 30, 100, -1), [(20, -1, 100, 30)]),
487 # 5
488 ((110, 40, 160, 20), [(110, 20, 160, 40)]),
489 ((-10, -40, 40, 20), [
490 (0.0, -31.864374833103472, 40.0, 20.0),
491 (350.0, -40.0, 360.0, -31.864374833103472)]),
492 ]
493
494
495 class BboxTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
496 def _runTest(self, sample):
497 stcsString, expected = sample
498 ast = stc.parseSTCS(stcsString)
499 found = list(bboxes.getBboxes(ast))
500 self.assertEqual(len(found), len(expected), "Wrong # of bboxes: %s %s"%(
501 expected, found))
502 for fbb, ebb in zip(found, expected):
503 for fc, ec in zip (fbb, ebb):
504 self.assertAlmostEqual(fc, ec, 7, "Bad coo:%s %s"%(
505 expected, found))
506
507 samples = [
508 ("Circle ICRS 1.1 -0.2 0.1 unit rad",
509 [(57.295779513082323, -17.188733853924699,
510 68.754935415698796, -5.729577951308233)]),
511 ("Circle GALACTIC 1.1 -0.2 0.1 unit rad",
512 [(302.43329340145374, 14.559998345530186,
513 313.89244930407017, 26.019154248146652)]),
514 ("Circle ICRS 40 70 30", [(0, 40.0, 360, 90)]),
515 ("Circle ICRS 40 70 60", [(0, 10.0, 360, 90)]),
516 ("Circle ICRS 40 10 60",
517 [(0, -50.0, 100, 70), (340, -50, 360, 70)]),
518 # 5
519 ("Circle ICRS 40 90 10", [(0, 80, 360, 90)]),
520 ("Ellipse ICRS 40 10 60 20 280",
521 [(0, -50.0, 100, 70), (340, -50, 360, 70)]),
522 ("Box ICRS 90 0 30 30",
523 [(60, -33.690067525979771, 120, 33.690067525979771)]),
524 ("Box ICRS 0 0 30 30", [
525 (0, -33.690067525979771, 30, 33.690067525979771),
526 (330, -33.690067525979771, 360, 33.690067525979771)]),
527 ("Box ICRS 0 -90 20 10", [(0, -90, 360, -80)]),
528 # 10
529 ("Box ICRS 0 -89 20 1", [(0, -90, 360, -88)]),
530 ("Box ICRS 0 89 20 1", [(0, 88, 360, 90)]),
531 ("AllSky ICRS", [(0, -90, 360, 90)]),
532 ("Polygon ICRS 0 0 10 3 12 -3", [(0, -3, 12, 3)]),
533 ("Polygon ICRS 100 30 120 30 122 9 110 -2",
534 [(100, -2, 122, 30.381255142470486)]),
535 # 15
536 ("Polygon ICRS -10 30 120 30 122 9 110 -2", [
537 (0.0, -2.0, 122, 53.796010254893815),
538 (350.0, 30, 360.0, 38.081479977806467)]),
539 ("PositionInterval ICRS 90 -90 180 0", [
540 (90.0, -90.0, 180.0, 0.0)]),
541 ("PositionInterval ICRS 180 0", [
542 (180, 0, 360, 90)]),
543 ("PositionInterval ICRS", []),
544 ("PositionInterval ICRS -10 -10 10 10", [
545 (0, -10, 10, 10),
546 (350., -10, 360, 10),]),
547 # 20
548 ("Union ICRS (Circle 1.1 -0.2 0.1"
549 " Box 0 0 0.5235987755982989 0.5235987755982989) unit rad", [
550 (57.295779513082323, -17.188733853924699,
551 68.754935415698796, -5.729577951308233),
552 (0, -33.690067525979771, 30, 33.690067525979771),
553 (330, -33.690067525979771, 360, 33.690067525979771)]),
554 ("Intersection ICRS (AllSky Circle 50 0 30)", [
555 (20, -30, 80, 30)]),
556 ("Intersection ICRS (Box 0 0 30 30 AllSky)", [
557 (0, -33.690067525979771, 30, 33.690067525979771),
558 (330, -33.690067525979771, 360, 33.690067525979771)]),
559 ("Intersection ICRS (Circle 20 0 30 Union ("
560 " Circle 20 10 5 Circle 355 0 10)"
561 " AllSky)", [
562 (15.0, 5.0, 25.0, 15.0),
563 (0, -10.0, 5.0, 10.0),
564 (350.0, -10.0, 360, 10.0)]),
565 ("Difference ICRS (Circle 100 30 50 Circle 50 50 70)", [
566 (50., -20, 150, 80)]),
567 # 25
568 ("Not ICRS (Circle 180 0 180)", [
569 (0, -90, 360, 90)]),
570 ]
571
572 if __name__=="__main__":
573 testhelpers.main(BboxTest)