Debian Codegraph @

tests/pgspheretest.py

raw
1 """
2 Unit tests for our pgsphere interface.
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 astropy import units as u
12
13 from gavo.helpers import testhelpers
14
15 from gavo import utils
16 from gavo.stc import bboxes
17 from gavo.utils import DEG
18 from gavo.utils import pgsphere
19
20 import tresc
21
22
23 # this is http://alasky.u-strasbg.fr/footprints/cats/vizier/I/206?product=MOC&nside=64
24 _SAMPLE_FITS_MOC = utils.getDirtyBytes("""
25 eJztl2tMW2Ucxh810WhijNH4QY05iRq2qEOI2eYFI20PUGxPO9rKZbqtQGFFKKNQKAiJTjeNt206
26 3eYE4m2aZYOp87YpxGxeolHHFplm4gSdGjM1Xpbsg7dfwUQyKQRcjInnSX4f+p7zPu//9ianAbfX
27 7zENI8eYQEEj0yiOR5uaIjGjvNXw+pxGTbg5bOT63cYs/5y8SDwWbUhEZo/b4nAH/e6SNH7zJ1qc
28 XFZuiTtgpPG7fPp+Bf6SVBoT+80l3wIz1+OPJg1vorYp6otXRuKGs745Eg9XR4y68DJjVl04adSn
29 1kfTNkuCpuVKW79pa9TMli1btmzZsmXL1n9Wqc+/gNtn5RgZDrcVzHV4zIxxj//6nqwY/xGZXv/q
30 93P2DP2y0vhdMUO/7An9sudlTd/P7/SFrODx+7+QP5nfDOIL5rlNjytwHP18Rd7RfmRkFY4tZUy+
31 Ywq/YKnfHPNjDk3PMX6ZBrMcqU1NdWqoQ5Z7gRFL1JXzL2li+RyBxRZOafqbaVhju+urjGUp58Yp
32 4vMVucwit5VPfNbo6cfG543Eq6OxaoNb6TKNcKzSsMxA0HSN3qq/y+nDMVAawM/559I/qp/9/82W
33 LVu2bNmaWlJuBwzAIPwmOR6B/TAE30rO+bAe+mEXsO76QDJdcAdsga+kvCfhRSm/EX6XCrzwPnwv
34 uQUnAD7ul6VC3ilcAc/ATnhNuv5CuAzmQjWsgSdgk+Q5BRZIXt717pGs86AQFkE3DEv+TGnBfVIR
35 eQROhbNhHXwIn0vB06UQMYTYFyLn0CvwnnTDlbAcboeH4Cep+CQgxuJ7pRLyKr0HOqUy8ijDu2wO
36 cF7ZVXBYWpgNxHzjbGnRPGnx+dKSr6Xy76QK8qngWcXbUuVZEIYH4CUgtsoRKXIBmFJVBuBdRR+q
37 24C6LvVDMfwgRa8B6h7leQ0x1bwj1Z4LO6RYr1S/AX6UGsivgXfi10mNUdgI9KbxVdgLv0hN1C5x
38 GjADCeJPXA0LgbonHgN6k3gW3gTmI0GszXfBC4BXC/taOK/lGylJLZKclaRASTySz0utF8GlwLNW
39 4m7NAfrQRg/azgBybcuDh+Fx6ZZrYbfUTq/byam9DlYDzzroXQe97eB3x7vws3RrjbT8JqAuK1qk
40 lazfH5NWOeCotPp1aQ3xPJgAZnftiXCOtH67tIFzNxLLoyGpk3p3srdrJTBvXcxPNz3rrgBmq5s+
41 dRPXJuJ7ulXazH3YTF+39Ehb6dHW26QeatBDTXsZ9F7mddsb0nPksJ0a7Vgi7WSe+si5j/vS96vU
42 T136iau/S9pFb3bfLb3FndpzRBpYCsztAHM3QP33OqV95L8vDvR2EN9Bct9fKX3E748vAeb0AHfr
43 AOd+Qq5D9HyImf70YlgL1PAg+R3kLn7GO8PlgMcwMz28TRo5GejTSBAiwF0YuROIa+Qp6YtVcEg6
44 dLN0+EzpyJfSUWK3ZcuWLVu2/p/6A9x9I/E=""")
45
46
47 class AsPolyTest(testhelpers.VerboseTest):
48 def _assertCircleBecomesPolygon(self, alpha, delta, radius):
49 alpha, delta, radius = alpha*DEG, delta*DEG, radius*DEG
50 c = pgsphere.SCircle(pgsphere.SPoint(alpha, delta), radius)
51 poly = c.asPoly()
52 for pt in poly.points:
53 self.assertAlmostEqual(radius,
54 bboxes.angular_separation(
55 pt.x*u.rad, pt.y*u.rad, alpha*u.rad, delta*u.rad).to(u.rad).value)
56
57 def testCircle1AsPoly(self):
58 self._assertCircleBecomesPolygon(0, 90, 3)
59
60 def testCircle2AsPoly(self):
61 self._assertCircleBecomesPolygon(0, 0, 8)
62
63 def testCircle3AsPoly(self):
64 self._assertCircleBecomesPolygon(80, -10, 20)
65
66 def testCircle4AsPoly(self):
67 self._assertCircleBecomesPolygon(120, -80, 1)
68
69 def testCircle5AsPoly(self):
70 self._assertCircleBecomesPolygon(220, -45, 1)
71
72 def testCircle6AsPoly(self):
73 self._assertCircleBecomesPolygon(320, 45, 90)
74
75 def testPolyAsPoly(self):
76 p = pgsphere.SPoly([pgsphere.SPoint(*p) for p in
77 ((0.5, 0.4), (1, -0.2), (1.5, 0))])
78 self.assertTrue(p.asPoly() is p)
79
80 def testNormSboxAsPoly(self):
81 b = pgsphere.SBox(pgsphere.SPoint(0.2, -0.5), pgsphere.SPoint(2, 0.1))
82 self.assertEqual(b.asPoly(),
83 pgsphere.SPoly([pgsphere.SPoint(*p) for p in
84 ((0.2, -0.5), (0.2, 0.1), (2, 0.1), (2, -0.5))]))
85
86 def testInvSboxAsPoly(self):
87 b = pgsphere.SBox(pgsphere.SPoint(2, 0.1), pgsphere.SPoint(-0.1, -0.5))
88 self.assertEqual(b.asPoly(),
89 pgsphere.SPoly([pgsphere.SPoint(*p) for p in
90 ((-0.1, -0.5), (-0.1, 0.1), (2, 0.1), (2, -0.5))]))
91
92
93 class FromDALITest(testhelpers.VerboseTest):
94 def testShortPolyDALIConstructor(self):
95 self.assertRaisesWithMsg(ValueError,
96 "Need an even-numbered number (>=6) of floats in a DALI polygon representation, got 4 floats.",
97 pgsphere.SPoly.fromDALI,
98 ([1, 1, 2, 2],))
99
100 def testOddPolyDALIConstructor(self):
101 self.assertRaisesWithMsg(ValueError,
102 "Need an even-numbered number (>=6) of floats in a DALI polygon representation, got 7 floats.",
103 pgsphere.SPoly.fromDALI,
104 ([-1.5372606267401516, 85.26415832196923, -1.5372606267401516, 85.25833260227361, -1.5401725504711685, 85.25541973647518, -1.5416285063290134],))
105
106 def testCoveringCircle(self):
107 points = [pgsphere.SPoint.fromDegrees(*v) for v in [
108 (2, 3), (355, -9), (0, 0)]]
109 c = pgsphere.SCircle.fromPointSet(points)
110 self.assertEqual(c.asSTCS("X"),
111 "Circle X 359.0157337607 -1.9990620034 6.2881158141")
112
113
114 class MOCDryTest(testhelpers.VerboseTest):
115 def testBadMOCRandom(self):
116 self.assertRaisesWithMsg(ValueError,
117 "No order separator visible in MOC literal 'Habe nun Philosophie"
118 "/Juristerei/Medic...'",
119 pgsphere.SMoc.fromASCII,
120 ("Habe nun Philosophie/Juristerei/Medicin und leider ach Theologie",))
121
122 def testBadMOCJunkAtStart(self):
123 self.assertRaisesWithMsg(ValueError,
124 "MOC literal 'Habe 1/3,4 3/45,9' does not start with order spec",
125 pgsphere.SMoc.fromASCII,
126 ("Habe 1/3,4 3/45,9",))
127
128 def testJunkInCenter(self):
129 self.assertRaisesWithMsg(ValueError,
130 "MOC literal syntax error at char 4",
131 pgsphere.SMoc.fromASCII,
132 ("1/3, Habe, 4 3/45,9",))
133
134 def testMOCConstruction(self):
135 m = pgsphere.SMoc.fromASCII("1/1\n3 4 2/4 25 12-14 21")
136 self.assertEqual(m.moc[2], frozenset([4, 12, 13, 14, 21, 25]))
137
138 def testFITSConstructor(self):
139 m = pgsphere.SMoc.fromFITS(_SAMPLE_FITS_MOC)
140 self.assertTrue(14400 in m.moc[6])
141
142 def testToASCII(self):
143 m = pgsphere.SMoc.fromASCII("1/1,3,4 2/4,25,12-14,21")
144 self.assertEqual(m.asASCII(),
145 "1/1 3-4 2/4 12-14 21 25")
146
147 def testMocFromPoly(self):
148 m = pgsphere.SPoly.fromDALI([0, 0, 45, 0, 0, 90]).asSMoc(order=4)
149 self.assertEqual(m.asASCII(),
150 '1/2 2/2 14 71 77 3/2 14 50 62 279 283 305 317'
151 ' 4/2 14 50 62 194 206 242 254 1111 1115 1127 1131 1217 1229 1265 1277'
152 ' 4/')
153
154 def testMocFromCircle(self):
155 m = pgsphere.SCircle.fromDALI([-50, 33, 2]).asPoly().asSMoc()
156 self.assertEqual(m.asASCII(),
157 '5/3309 6/13233-13235 13240-13241 13243-13246 13280 13282 6/')
158
159 def testMocFromCircleInclusive(self):
160 m = pgsphere.SCircle.fromDALI([-50, 33, 2]).asPoly().asSMoc(inclusive=True)
161 self.assertEqual(m.asASCII(),
162 '4/827 6/13211 13214-13215 13258 13280 13282 13288 13290 14609 14612 6/')
163
164 def testCircleSMoc(self):
165 c = pgsphere.SCircle.fromDALI([250, -79, 0.25])
166 self.assertEqual(
167 c.asSMoc(order=7).asSTCS("X"),
168 "MOC 7/164157 7/")
169
170 def testMaxOrderParsed(self):
171 m = pgsphere.SMoc.fromASCII("3/5,6 7/")
172 self.assertEqual(m.maxOrder, 7)
173
174 def testMaxOrderSerialialised(self):
175 m = pgsphere.SMoc.fromASCII("7/ 0/1-2")
176 self.assertEqual(m.asASCII(), "0/1-2 7/")
177
178 def testMaxOrderWithRegrid(self):
179 m = pgsphere.SMoc.fromASCII("4/55 9/40031-43210")
180 m2 = m.asSMoc(3)
181 self.assertEqual(m2.maxOrder, 3)
182
183 def testMaxOrderLower(self):
184 m = pgsphere.SMoc.fromASCII("0/2-3 4/")
185 m2 = m.asSMoc(7)
186 self.assertEqual(m2.maxOrder, 4)
187
188
189 class InDBTest(testhelpers.VerboseTest):
190 resources = [("conn", tresc.dbConnection)]
191
192 def makeTable(self, testedType, testValue):
193 self.conn.execute("CREATE TABLE pgstest (col %s)"%testedType)
194 self.conn.execute("INSERT INTO pgstest (col) VALUES (%(val)s)",
195 {"val": testValue})
196
197 def assertTripsRound(self, testedType, testValue):
198 try:
199 self.makeTable(testedType, testValue)
200 res = list(self.conn.query("SELECT * from pgstest"))
201 self.assertEqual(res[0][0], testValue)
202 finally:
203 self.conn.rollback()
204
205 def testSPoints(self):
206 self.assertTripsRound("spoint", pgsphere.SPoint(2,0.5))
207
208 def testSCircle(self):
209 self.assertTripsRound("scircle",
210 pgsphere.SCircle(pgsphere.SPoint(2,0.5), 0.25))
211
212 def testSPoly(self):
213 self.assertTripsRound("spoly",
214 pgsphere.SPoly([pgsphere.SPoint(2,0.5),
215 pgsphere.SPoint(2.5,-0.5),
216 pgsphere.SPoint(1.5,0),]))
217
218 def testSBox(self):
219 self.assertTripsRound("sbox",
220 pgsphere.SBox(pgsphere.SPoint(2.5,-0.5),
221 pgsphere.SPoint(2.0,0.5)))
222
223 def testSMocRoundTrip(self):
224 self.assertTripsRound("smoc",
225 pgsphere.SMoc.fromASCII('29/2-5,20-29,123,444,17-21,33-39,332-339,0-1'))
226
227
228 if __name__=="__main__":
229 testhelpers.main(InDBTest)