test_GSky.py
10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# ==========================================================================
# This module performs unit tests for the GammaLib sky module.
#
# Copyright (C) 2012-2015 Juergen Knoedlseder
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ==========================================================================
from gammalib import *
from math import *
import os
# =========== #
# Text parser #
# =========== #
def loadtxt(filename):
"""
Parse text file with columns of floats.
We have files with multiple spaces as delimiter,
which I think csv.reader can't handle.
"""
# Initialise result array
data = []
# Loop over all rows in file
for row in [_.split() for _ in open(filename)]:
row_data = []
for item in row:
row_data.append(float(item))
data.append(row_data)
# Return result
return data
# ================================== #
# Test class for GammaLib sky module #
# ================================== #
class Test(GPythonTestSuite):
"""
Test class for GammaLib sky module.
"""
def __init__(self):
"""
Constructor.
"""
# Call base class constructor
GPythonTestSuite.__init__(self)
# Return
return
def set(self):
"""
Set all test functions.
"""
# Set test name
self.name("sky")
# Append tests
self.append(self.test_skymap_healpix, "Test HEALPix map")
self.append(self.test_skymap_ait, "Test AIT projection map")
self.append(self.test_skymap_azp, "Test AZP projection map")
self.append(self.test_skymap_car, "Test CAR projection map")
self.append(self.test_skymap_mer, "Test MER projection map")
self.append(self.test_skymap_mol, "Test MOL projection map")
self.append(self.test_skymap_stg, "Test STG projection map")
self.append(self.test_skymap_tan, "Test TAN projection map")
self.append(self.test_fk5_to_galactic, "Test FK5 to Galactic coordinate conversion")
self.append(self.test_operators, "Test skymap operators")
# Return
return
# Generic skymap pixel transformation test
def test_skymap_pixels(self, pixels, map):
"""
Control coordinate and pixel transformation methods.
"""
# Control inx2dir/dir2inx methods for all pixels
for i in range(pixels.npix()):
dir = pixels.inx2dir(i)
inx = pixels.dir2inx(dir)
msg = map + " inx2dir/dir2inx check for pixel " + str(i)
err = map + " GSkyMap trouble with pixel " + str(i) + " (" + str(inx) + \
"), RA=" + str(dir.ra() * 180 / pi) + ", Dec=" + str(dir.dec() * 180 / pi)
self.test_assert(i == inx, msg, err)
# Control SkyDir coordinate transformation for all pixels
for i in range(pixels.npix()):
dir = pixels.inx2dir(i)
dir_new = GSkyDir()
dir_new.lb(dir.l(), dir.b())
dra = abs(dir.ra() - dir_new.ra())
if (dra >= 5.0):
dra -= 2.0 * pi
ddec = abs(dir.dec() - dir_new.dec())
msg = map + " dir check for pixel " + str(i)
err = map + " GSkyMap trouble with pixel " + str(i) + " (" + str(dra) + \
"," + str(ddec) + ")"
self.test_assert(not (dra > 1.0e-9 or ddec > 1.0e-9), msg, err)
# Return
return
# Generic skymap projection test
def test_skymap_proj(self, proj):
"""
Test skymap projection.
"""
# Set filename
file = "test_python_skymap_" + proj + ".fits"
# Remove test files
try:
os.remove(file)
except:
pass
# Create skymap
pixels = GSkyMap(proj, "CEL", 83.6331, 22.0145, -3.7, 2.6, 5, 5, 20)
for map in range(pixels.nmaps()):
for i in range(pixels.npix()):
pixels[i, map] = i + map * pixels.npix()
pixels.save(file)
# Test coordinate and pixel transformations
self.test_skymap_pixels(pixels, proj)
# Return
return
# Test HEALPix projection
def test_skymap_healpix(self):
"""
Test HEALPix interface.
"""
# Set filenames
file1 = "test_python_skymap_hpx_v1.fits"
file2 = "test_python_skymap_hpx_v2.fits"
# Remove test files
try:
os.remove(file1)
os.remove(file2)
except:
pass
# Create HEALPix skymap
pixels = GSkyMap("GAL", 2, "RING", 2)
for i in range(pixels.npix()):
pixels[i] = i + 1.0
pixels[i, 1] = i + 1.0 + 1000.0
pixels.save(file1)
# Load HEALPix skymap
pixels = GSkyMap(file1)
# Control coordinate and pixel transformations
self.test_skymap_pixels(pixels, "HEALPix")
# Save HEALPix skymap twice. The second saving should fail.
try:
pixels.save(file2, True)
pixels.save(file2)
except RuntimeError:
pass
else:
raise RuntimeError("*** TEST ERROR: FITS file overwritten!")
pixels.save(file2, True)
# Load again HEALPix skymap
pixels = GSkyMap(file1)
# Return
return
# Test AIT projection
def test_skymap_ait(self):
"""
Test AIT projection.
"""
# Execute generic test
self.test_skymap_proj("AIT")
# Return
return
# Test AZP projection
def test_skymap_azp(self):
"""
Test AZP projection.
"""
# Execute generic test
self.test_skymap_proj("AZP")
# Return
return
# Test CAR projection
def test_skymap_car(self):
"""
Test CAR projection.
"""
# Execute generic test
self.test_skymap_proj("CAR")
# Return
return
# Test MER projection
def test_skymap_mer(self):
"""
Test MER projection.
"""
# Execute generic test
self.test_skymap_proj("MER")
# Return
return
# Test MOL projection
def test_skymap_mol(self):
"""
Test MOL projection.
"""
# Execute generic test
self.test_skymap_proj("MOL")
# Return
return
# Test STG projection
def test_skymap_stg(self):
"""
Test STG projection.
"""
# Execute generic test
self.test_skymap_proj("STG")
# Return
return
# Test TAN projection
def test_skymap_tan(self):
"""
Test TAN projection.
"""
# Execute generic test
self.test_skymap_proj("TAN")
# Return
return
# FK5 J2000 to Galactic coordinate tranformations
def test_fk5_to_galactic(self):
"""
Test precision of FK5 J2000 to Galactic coordinate tranformations
against pyast (http://dsberry.github.com/starlink/pyast.html)
for 1000 random positions on the sky.
Test txt files taken from:
See https://github.com/astropy/coordinates-benchmark
"""
# Set parameters
fk5_filename = "data/initial_coords.txt"
galactic_filename = "data/fk5_to_galactic.txt"
fk5_coordinates = loadtxt(fk5_filename)
galactic_coordinates = loadtxt(galactic_filename)
coordinates = zip(fk5_coordinates, galactic_coordinates)
max_dist = 0
# Loop over coordinates
for (ra, dec), (glon, glat) in coordinates:
# Create point A in FK5 J2000 coordinates
input_point = GSkyDir()
input_point.radec_deg(ra, dec)
# Convert point A to Galactic coordinates
ll, bb = input_point.l_deg(), input_point.b_deg()
actual_point = GSkyDir()
actual_point.lb_deg(ll, bb)
# Compute offset to reference result
reference_point = GSkyDir()
reference_point.lb_deg(glon, glat)
# Compute distance and remember maximum
this_dist = actual_point.dist_deg(reference_point)
max_dist = max(max_dist, this_dist)
# Convert max_dist from deg to milli-arcsec
max_dist *= 1e3 * 3600
# Results match within 10 milli-arcsec on my machine,
# I put 20 arcsec here to avoid test failures on machines with
# slightly different numerical results
msg = ''
err = ''
self.test_assert(max_dist < 20, msg, err)
# Return
return
# Test skymap operators
def test_operators(self):
"""
Test the skymap operators.
"""
# Setup skymaps
map = GSkyMap("CAR", "CEL", 83.6331, 22.0145, -3.7, 2.6, 2, 2)
map[0] = 1.0
map[1] = 2.0
map[2] = 3.0
map[3] = 4.0
map_b = map.copy()
# Addition operator
map += map_b
self.test_value(map[0], 2.0)
self.test_value(map[1], 4.0)
self.test_value(map[2], 6.0)
self.test_value(map[3], 8.0)
# Multiplication operator
map *= map_b
self.test_value(map[0], 2.0)
self.test_value(map[1], 8.0)
self.test_value(map[2], 18.0)
self.test_value(map[3], 32.0)
# Subtraction operator
map -= map_b
self.test_value(map[0], 1.0)
self.test_value(map[1], 6.0)
self.test_value(map[2], 15.0)
self.test_value(map[3], 28.0)
# Division operator
map /= map_b
self.test_value(map[0], 1.0)
self.test_value(map[1], 3.0)
self.test_value(map[2], 5.0)
self.test_value(map[3], 7.0)
# Scaling operator
map *= 2.0
self.test_value(map[0], 2.0)
self.test_value(map[1], 6.0)
self.test_value(map[2], 10.0)
self.test_value(map[3], 14.0)
# Division operator
map /= 2.0
self.test_value(map[0], 1.0)
self.test_value(map[1], 3.0)
self.test_value(map[2], 5.0)
self.test_value(map[3], 7.0)
# Return
return