Debian Codegraph @

tests/coordtest.py

raw
1 """
2 Tests for gavo.coords.
3
4 Helpful when dealing with polygons:
5 http://archive.eso.org/programmatic/vo_polygons/vo_polygons.html
6
7 (note that both TOPCAT and pgsphere 1 ignore the winding direction and
8 thus aren't ideal for visualising these guys).
9 """
10
11 #c Copyright 2008-2025, the GAVO project <[email protected]>
12 #c
13 #c This program is free software, covered by the GNU GPL. See the
14 #c COPYING file in the source distribution.
15
16
17 import unittest
18
19 import numpy
20
21 from gavo.helpers import testhelpers
22
23 from gavo import utils
24 from gavo.base import coords
25
26 import tresc
27
28
29 interestingPlaces = [ (250, 89), (0,0), (23.0, 42.0), (23.0, -42.0),
30 (-23.0, 42.0), (-23.0, -42.0), (359, 0), (314.,-42.0), (0., 89.), (0, 90),
31 (90, -90), (90, 90)]
32
33
34 def _getWCSExample(**kwargs):
35 res = {
36 "CUNIT1": "deg", "CUNIT2": "deg",
37 "CTYPE1": 'RA---TAN-SIP', "CTYPE2": 'DEC--TAN-SIP',
38 "CRVAL1": 0., "CRVAL2": 0.,
39 "CRPIX1": 0., "CRPIX2": 0.,
40 "CD1_1": 0.001, "CD1_2": 0.,
41 "CD2_1": 0., "CD2_2": 0.001,
42 "LONPOLE": 180.,
43 "NAXIS1": 100, "NAXIS2": 100, "NAXIS": 2,
44 }
45 res.update(**kwargs)
46 return res
47
48
49 def writeAsVOT(poly):
50 """write a VOTable test.vot containing just poly.
51
52 This is for lazy visualisation.
53 """
54 from gavo import api
55 td = api.parseFromString(api.TableDef,
56 """<table id="poly"><column name="p" type="spoly"/></table>""")
57 t = api.TableForDef(td, rows=[{"p": poly}])
58 with open("test.vot", "wb") as f:
59 api.writeAsVOTable(t, f)
60
61
62 class TestCartesian(unittest.TestCase):
63 """Tests for cartesian coordinates on the unit sphere.
64 """
65 def testTangential(self):
66 """tests for correct tangential unit vectors all over the sky.
67 """
68 for alpha in range(0, 360, 10):
69 for delta in range(-90, 90, 10):
70 cPos = utils.spherToCart(alpha*utils.DEG, delta*utils.DEG)
71 ua, ud = coords.getTangentialUnits(cPos)
72 self.assertAlmostEqual(
73 numpy.dot(cPos, ua), 0, msg="unit alpha not perp cPos")
74 self.assertAlmostEqual(
75 numpy.dot(cPos, ud), 0, msg="unit delta not perp cPos")
76 self.assertAlmostEqual(
77 numpy.dot(ua, ud), 0, msg="unit vectors not perp")
78 self.assertTrue(ud[2]*delta>=0,
79 "unit delta doesn't point to pole.")
80 if delta<0:
81 self.assertTrue(numpy.dot(numpy.cross(ua, ud), cPos)<0,
82 "unit alpha points backwards")
83 else:
84 self.assertTrue(numpy.dot(numpy.cross(ua, ud), cPos)>0,
85 "unit alpha points backwards")
86
87
88 class TestWCS(unittest.TestCase):
89 """Tests for WCS-handling routines in coords
90 """
91 def _doIdempotencyTest(self, wcs):
92 for ra, dec in interestingPlaces:
93 wcs["CRVAL1"], wcs["CRVAL2"] = ra, dec
94 proj = coords.getWCSTrafo(wcs)
95 inv = coords.getInvWCSTrafo(wcs)
96 for pxCoo in [(0, 0), (200, 200), (-200, 200), (200, -200),
97 (-200, -200)]:
98 wCoo = proj(*pxCoo)
99 res = inv(*wCoo)
100 self.assertAlmostEqual(pxCoo[0], res[0], 8, "Failure in x for %f, %f "
101 "(%d,%d): %f vs. %f"%(ra, dec, pxCoo[0], pxCoo[1], pxCoo[0], res[0]))
102 self.assertAlmostEqual(pxCoo[1], res[1], 8, "Failure in y for %f, %f "
103 "(%d,%d): %f vs. %f"%(ra, dec, pxCoo[0], pxCoo[1], pxCoo[1], res[1]))
104
105 def testIdempotencyPos(self):
106 """tests for idempotency of WCSTrafo o InvWCSTrafo over the sky.
107 """
108 wcs = _getWCSExample()
109 self._doIdempotencyTest(wcs)
110
111 def testIdempotencySkew(self):
112 """tests for idempotency of WCSTrafo o InvWCSTrafo for skewed coordinates.
113 """
114 wcs = _getWCSExample()
115 wcs.update({"CD1_1": -0.002, "CD1_2": 0.008, "CD2_1": 0.0021,
116 "CD2_2":-0.0012})
117 self._doIdempotencyTest(wcs)
118
119 def testCalabretta(self):
120 """tests for reprodution of published example.
121
122 The example is from Calabretta and Greisen, A&A 395 (2002), 1077,
123 adapted for what we're doing.
124 """
125 wcs = _getWCSExample()
126 wcs.update({
127 "CRPIX1": 256, "CRPIX2": 257,
128 "CRVAL1": 45.83, "CRVAL2": 63.57,
129 "CD1_1": -0.003, "CD1_2": 0,
130 "CD2_1": 0, "CD2_2": 0.003})
131 proj = coords.getWCSTrafo(wcs)
132 for coos, expected in [
133 ((1,2), (47.503264, 62.795111)),
134 ((1,512), (47.595581, 64.324332)),
135 ((511,512), (44.064419, 64.324332)),
136 ]:
137 res = proj(*coos)
138 self.assertAlmostEqual(expected[0], res[0], 5)
139 self.assertAlmostEqual(expected[1], res[1], 5)
140
141 def testAtPole(self):
142 wcs = _getWCSExample(
143 CRVAL1=0, CRVAL2=90,
144 CD1_1=0.1, CD2_2=0.1,
145 CRPIX1=50.5, CRPIX2=50.5)
146 dp = coords.getSpolyFromWCSFields(wcs).asDALI()
147 self.assertEqual(
148 [dp[i] for i in range(0, 8, 2)],
149 [315.0, 225.0, 135.0, 45.0])
150 for i in range(1, 8, 2):
151 self.assertAlmostEqual(dp[i], 83.0341674208458)
152
153 def testSouthern(self):
154 wcs = _getWCSExample(
155 CRVAL1=256, CRVAL2=-80,
156 CD1_1=0.01, CD2_2=0.01,
157 CRPIX1=256.5, CRPIX2=-80.5)
158 dp = coords.getSpolyFromWCSFields(wcs).asDALI()
159 self.assertAlmostEqual(dp[0], 242.63263104258056)
160 self.assertAlmostEqual(dp[-1], -79.07381994704433)
161
162 def testMirrored(self):
163 wcs = _getWCSExample(
164 CD1_1=-0.001)
165 dp = coords.getSpolyFromWCSFields(wcs).asDALI()
166 self.assertAlmostEqual(dp[0], 359.9000001015389)
167 self.assertAlmostEqual(dp[-1], 0.000999999999751763)
168
169 def testMirroredUpended(self):
170 wcs = _getWCSExample(
171 CD1_1=-0.001, CD2_2=-0.001)
172 dp = coords.getSpolyFromWCSFields(wcs).asDALI()
173 self.assertAlmostEqual(dp[0], 359.9990000000001)
174 self.assertAlmostEqual(dp[-1], -0.0009999984768148558)
175
176 def testInvalidWCSRejection(self):
177 """tests for correct rejection of unknown WCS specs.
178 """
179 wcs = _getWCSExample()
180 wcs["CTYPE1"] = "Weird Stuff"
181 self.assertRaises(Exception, coords.getWCSTrafo, wcs)
182 wcs = _getWCSExample()
183 wcs["CTYPE2"] = "Weird Stuff"
184 self.assertRaises(Exception, coords.getWCSTrafo, wcs)
185 wcs = _getWCSExample()
186 wcs["CUNIT1"] = "arcsel"
187 self.assertRaises(Exception, coords.getWCSTrafo, wcs)
188
189
190 class TestMovePm(unittest.TestCase):
191 """Tests for working proper motion application.
192 """
193 def testMovePm(self):
194 self.assertEqual("%.8f %.8f"%coords.movePm(23, 50, 0., 0., 2),
195 "23.00000000 50.00000000")
196 self.assertEqual("%.8f %.8f"%coords.movePm(23, 50, 0.001, 0.001, 20),
197 "23.03112743 50.01999584")
198 self.assertEqual("%.8f %.8f"%coords.movePm(23, -50, -0.001, 0.004, -20),
199 "23.03116636 -50.07999583")
200 self.assertEqual("%.8f %.8f"%coords.movePm(232, -80, -0.001, 0.004, -20),
201 "232.11609464 -80.07998004")
202
203
204 class TestGetGCDist(unittest.TestCase):
205 def testGcDist(self):
206 self.assertAlmostEqual(coords.getGCDist((0, 0), (0, 0)), 0)
207 for dec in range(-90, 91, 30):
208 self.assertAlmostEqual(coords.getGCDist((0, 0), (0, dec)), abs(dec))
209 for ra in range(0, 181, 30):
210 self.assertAlmostEqual(coords.getGCDist((0, 0), (ra, 0)), ra)
211 for ra in range(180, 361, 30):
212 self.assertAlmostEqual(coords.getGCDist((0, 0), (ra, 0)), 360-ra)
213
214 def _d(**kwargs):
215 return kwargs
216
217
218 class SpolyTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
219 resources = [('conn', tresc.DBConnection())]
220
221 @staticmethod
222 def _getHeader(overrides):
223 hdr = {
224 "CUNIT1": "deg", "CUNIT2": "deg",
225 "CTYPE1": 'RA---TAN-SIP', "CTYPE2": 'DEC--TAN-SIP',
226 "CRVAL1": 0., "CRVAL2": 0.,
227 "CRPIX1": 0., "CRPIX2": 0.,
228 "CD1_1": 0.01, "CD1_2": 0.,
229 "CD2_1": 0., "CD2_2": 0.01,
230 "LONPOLE": 180.,
231 "NAXIS1": 100, "NAXIS2": 100, "NAXIS": 2,
232 }
233 hdr.update(overrides)
234 return hdr
235
236 def assertAreaAlmostEqual(self, wcs, area):
237 cursor = self.conn.cursor()
238 cursor.execute("SELECT area(%(poly)s)",
239 {'poly': coords.getSpolyFromWCSFields(wcs)})
240 foundArea = list(cursor)[0][0]
241 cursor.close()
242 self.assertAlmostEqual(foundArea/utils.DEG/utils.DEG, area)
243
244 def _runTest(self, sample):
245 overrides, area = sample
246 self.assertAreaAlmostEqual(self._getHeader(overrides), area)
247
248 SQ2 = 0.70710678118654746/100
249 SQ3_2 = 0.8660254037844386/100
250
251 samples = [
252 (_d(), 0.9797985366),
253 (_d(CD1_1=-0.01),0.9797985366),
254 (_d(CD2_2=-0.01),0.9797985366),
255 (_d(CD1_1=-0.01, CD2_2=-0.01),0.9797985366),
256 (_d(CD1_1=SQ2, CD1_2=-SQ2, CD2_1=SQ2, CD2_2=SQ2),0.9797985366),
257 #5
258 (_d(CRVAL1=45),0.9797985366),
259 (_d(CRVAL2=45),0.9797985366),
260 (_d(CRVAL2=89),0.9797985366),
261 (_d(CRVAL2=90),0.9797985366),
262 (_d(CRVAL1=180),0.9797985366),
263 #10
264 (_d(CRVAL1=359.5),0.9797985366),
265 (_d(CD1_1=0.005, CD1_2=SQ3_2, CD2_1=SQ3_2, CD2_2=0.005),
266 0.4898004649),
267 (_d(CD1_1=-0.0028, CD1_2=-8.57e-5, CD2_1=-3.198e-4, CD2_2=-0.0028),
268 0.076569163),
269 ]
270
271
272 class PixelLimitsTest(testhelpers.VerboseTest):
273 def testNoCutout(self):
274 wcs, _ = coords.getSkyWCS(_getWCSExample(CRVAL1=90, CRPIX2=50))
275 self.assertEqual(
276 coords.getPixelLimits([(80, -10), (100, 10)], wcs),
277 [])
278
279 def testRAOnly(self):
280 wcs, _ = coords.getSkyWCS(_getWCSExample(CRVAL1=90, CRPIX1=50,
281 CD1_1=0.1, CD2_2=0.1))
282 self.assertEqual(
283 coords.getPixelLimits([(89., -15), (91, 15)], wcs),
284 [(1, 40, 60)])
285
286 def testRAFlipped(self):
287 wcs, _ = coords.getSkyWCS(_getWCSExample(CRVAL1=90, CRPIX1=50,
288 CD1_1=0, CD2_2=0, CD1_2=0.1, CD2_1=0.1))
289 self.assertEqual(
290 coords.getPixelLimits([(89., -15), (91, 15)], wcs),
291 [(2, 1, 10)])
292
293 def testStitching(self):
294 wcs, _ = coords.getSkyWCS(_getWCSExample(CRPIX1=50, CRPIX2=50,
295 CD1_1=0.1, CD2_2=0.1))
296 self.assertEqual(
297 coords.getPixelLimits([(358, 3), (3, 2)], wcs),
298 [(1, 30, 80), (2, 70, 80)])
299
300 def testNearPole(self):
301 # # I'd have to read the WCS paper to figure out what's even expected
302 # here, I guess.
303 wcs, _ = coords.getSkyWCS(_getWCSExample(CRPIX1=50, CRPIX2=50,
304 CRVAL1=100, CRVAL2=89, CD1_1=0.1, CD2_2=0.1))
305 self.assertEqual(
306 coords.getPixelLimits([(25, 90), (300, 87)], wcs),
307 [(1, 40, 50), (2, 60, 88)])
308
309 def testMultipleSwallowed(self):
310 wcs, _ = coords.getSkyWCS(_getWCSExample(CRVAL1=90, CRPIX2=50,
311 CD1_1=0.1, CD2_2=0.1))
312 self.assertEqual(
313 coords.getPixelLimits([(80, -10), (100, 10),
314 (87, -2), (93, 2)], wcs),
315 [])
316
317 def testMultipleJoined(self):
318 wcs, _ = coords.getSkyWCS(_getWCSExample(CRVAL1=90,
319 CRPIX1=50, CRPIX2=50, CD1_1=0.1, CD2_2=0.1))
320 self.assertEqual(
321 coords.getPixelLimits([(87, -4), (88, 1.5), (92, 1), (93, -3)], wcs),
322 [(1, 20, 80), (2, 10, 65)])
323
324 def testNegative(self):
325 wcs, _ = coords.getSkyWCS(_getWCSExample(CRPIX1=50, CD1_1=0.1, CD2_2=0.1))
326 self.assertEqual(
327 coords.getPixelLimits([(-2, 0), (1, 1)], wcs),
328 [(1, 30, 60), (2, 1, 10)])
329
330
331 if __name__=="__main__":
332 testhelpers.main(SpolyTest)