test_GSky.cpp
22.3 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
/***************************************************************************
* test_GSky.cpp - test GSky classes *
* ----------------------------------------------------------------------- *
* copyright (C) 2010-2012 by Jurgen Knodlseder *
* ----------------------------------------------------------------------- *
* *
* 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/>. *
* *
***************************************************************************/
/**
* @file test_GSky.cpp
* @brief Testing of sky module.
* @author J. Knodlseder
*/
/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream> // cout, cerr
#include <stdexcept> // std::exception
#include <stdlib.h>
#include "test_GSky.hpp"
#include "GTools.hpp"
/* __ Namespaces _________________________________________________________ */
/* __ Globals ____________________________________________________________ */
/* __ Debug definitions __________________________________________________ */
#define G_WCS_FORTH_BACK_PIXEL_DEBUG
#define G_WCS_COPY_DEBUG
/***********************************************************************//**
* @brief Set parameters and tests
***************************************************************************/
void TestGSky::set(void){
// Test name
name("GSky");
//add tests
add_test(static_cast<pfunction>(&TestGSky::test_GWcslib),"Test GWcslib");
add_test(static_cast<pfunction>(&TestGSky::test_GSkymap_healpix_construct),"Test Healpix GSkymap constructors");
add_test(static_cast<pfunction>(&TestGSky::test_GSkymap_healpix_io),"Test Healpix GSkymap I/O");
add_test(static_cast<pfunction>(&TestGSky::test_GSkymap_wcs_construct),"Test WCS GSkymap constructors");
add_test(static_cast<pfunction>(&TestGSky::test_GSkymap_wcs_io),"Test WCS GSkymap I/O");
return;
}
/***********************************************************************//**
* @brief Test consistency of forward and background transformations
*
* @param[in] nx Number of points in X
* @param[in] ny Number of points in Y
* @param[in] crpix1 Reference pixel in X
* @param[in] crpix2 Reference pixel in Y
***************************************************************************/
double TestGSky::wcs_forth_back_pixel(GWcslib* wcs, int nx, int ny, double& crpix1, double& crpix2)
{
// Initialise maximal distance
double dist_max = 0.0;
// Loop over x pixels
for (int ix = -nx; ix <= nx; ++ix) {
// Set x value
double x = double(ix) + crpix1;
// Skip pixels outside valid range (they are not expected to
// transform bijectively)
if (x < 0 || x >= nx)
continue;
// Loop over y pixels
for (int iy = -ny; iy <= ny; ++iy) {
// Set y value
double y = double(iy) + crpix2;
// Skip pixels outside valid range (they are not expected to
// transform bijectively)
if (y < 0 || y >= ny)
continue;
// Set sky pixel
GSkyPixel pix_in(x,y);
// Forth: Transform to world
GSkyDir dir = wcs->xy2dir(pix_in);
// Back: Transform to pixel
GSkyPixel pix_out = wcs->dir2xy(dir);
// Compute distance
double dx = pix_in.x()-pix_out.x();
double dy = pix_in.y()-pix_out.y();
double dist = sqrt(dx*dx+dy*dy);
// Debug option: Dump discrepancies
#if defined(G_WCS_FORTH_BACK_PIXEL_DEBUG)
if (dist > 0.001) {
std::cout << std::endl;
std::cout << "dist=" << dist;
std::cout << " dx=" << dx;
std::cout << " dy=" << dy;
std::cout << " pix_in=" << pix_in;
std::cout << " pix_out=" << pix_out;
std::cout << " (x,y)=(" << x << "," << y << ")";
std::cout << " dir=" << dir;
}
#endif
// Store maximum distance
if (dist > dist_max)
dist_max = dist;
} // endfor: y pixels
} // endfor: x pixels
// Return
return dist_max;
}
/***********************************************************************//**
* @brief Test consistency of copy
*
* @param[in] nx Number of points in X
* @param[in] ny Number of points in Y
* @param[in] crpix1 Reference pixel in X
* @param[in] crpix2 Reference pixel in Y
***************************************************************************/
double TestGSky::wcs_copy(GWcslib* wcs, int nx, int ny, double& crpix1, double& crpix2)
{
// Make a copy using clone
GWcslib* cpy = wcs->clone();
// Initialise maximal angle and distance
double angle_max = 0.0;
double dist_max = 0.0;
// Loop over x pixels
for (int ix = -nx; ix <= nx; ++ix) {
// Set x value
double x = double(ix) + crpix1;
// Skip pixels outside valid range (they are not expected to
// transform bijectively)
if (x < 0 || x >= nx)
continue;
// Loop over y pixels
for (int iy = -ny; iy <= ny; ++iy) {
// Set y value
double y = double(iy) + crpix2;
// Skip pixels outside valid range (they are not expected to
// transform bijectively)
if (y < 0 || y >= ny)
continue;
// Set sky pixel
GSkyPixel pix_in(x,y);
// Transform to world
GSkyDir dir1 = wcs->xy2dir(pix_in);
GSkyDir dir2 = cpy->xy2dir(pix_in);
double angle = dir1.dist_deg(dir2);
if (angle > angle_max)
angle_max = angle;
// Debug option: Dump discrepancies
#if defined(G_WCS_COPY_DEBUG)
if (angle > 0.001) {
std::cout << std::endl;
std::cout << "angle=" << angle;
std::cout << " dir1=" << dir1;
std::cout << " dir2=" << dir2;
}
#endif
// Transform to pixel
GSkyPixel pix_out1 = wcs->dir2xy(dir1);
GSkyPixel pix_out2 = cpy->dir2xy(dir2);
// Compute distance
double dx = pix_out1.x()-pix_out2.x();
double dy = pix_out1.y()-pix_out2.y();
double dist = sqrt(dx*dx+dy*dy);
if (dist > dist_max)
dist_max = dist;
// Debug option: Dump discrepancies
#if defined(G_WCS_COPY_DEBUG)
if (dist > 0.001) {
std::cout << std::endl;
std::cout << "dist=" << dist;
std::cout << " dx=" << dx;
std::cout << " dy=" << dy;
std::cout << " pix_out1=" << pix_out1;
std::cout << " pix_out2=" << pix_out2;
std::cout << " dir1=" << dir1;
std::cout << " dir2=" << dir2;
}
#endif
} // endfor: y pixels
} // endfor: x pixels
// Return
return ((dist_max > angle_max) ? dist_max : angle_max);
}
/***********************************************************************//**
* @brief Test GWcslib projections
*
* This function test all non-HealPix projections that have been registered.
***************************************************************************/
void TestGSky::test_GWcslib(void)
{
// Allocate parameters for testing
double crval1 = 83.63;
double crval2 = 22.01;
double crpix1 = 100.5;
double crpix2 = 100.5;
double cdelt1 = 0.5;
double cdelt2 = 0.5;
int nx = 200;
int ny = 200;
// Loop over all non-HealPix projections
GWcsRegistry registry;
for (int i = 0; i < registry.size(); ++i) {
// Skip HealPix
if (registry.code(i) == "HPX")
continue;
std::string test_class = "GWcs" + registry.code(i);
// Perform tests
test_try("Test "+test_class);
try {
// Allocate projection CEL and GAL
GWcslib *cel = dynamic_cast<GWcslib*>(registry.alloc(registry.code(i)));
GWcslib *gal = dynamic_cast<GWcslib*>(registry.alloc(registry.code(i)));
// Throw an error if allocation failed
if (cel == NULL || gal == NULL) {
std::cout << std::endl
<< "TEST ERROR: Could not allocate "+test_class+"."
<< std::endl;
throw exception_error("Could not allocate"+test_class);
}
// Set projections
test_try("Set projections");
try {
cel->set("CEL", crval1, crval2, crpix1, crpix2, cdelt1, cdelt2);
gal->set("GAL", crval1, crval2, crpix1, crpix2, cdelt1, cdelt2);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test forth-back CEL conversion
test_try("Test forth-back CEL conversion");
try {
double tol = 0.0;
if ((tol = wcs_forth_back_pixel(cel, nx, ny, crpix1, crpix2)) > 1.0e-10) {
throw exception_failure("CEL forth-back transformation tolerance 1.0e-10 exceeded: "+str(tol));
}
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test forth-back GAL conversion
test_try("Test forth-back GAL conversion");
try {
double tol = 0.0;
if ((tol = wcs_forth_back_pixel(gal, nx, ny, crpix1, crpix2)) > 1.0e-10) {
throw exception_failure("GAL forth-back transformation tolerance 1.0e-10 exceeded: "+str(tol));
}
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test CEL copy
test_try("Test CEL copy");
try {
double tol = 0.0;
if ((tol = wcs_copy(cel, nx, ny, crpix1, crpix2)) > 1.0e-4) {
throw exception_failure("CEL copy tolerance 1.0e-4 exceeded: "+str(tol));
}
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test GAL copy
test_try("Test GAL copy");
try {
double tol = 0.0;
if ((tol = wcs_copy(gal, nx, ny, crpix1, crpix2)) > 1.0e-4) {
throw exception_failure("GAL copy tolerance 1.0e-4 exceeded: "+str(tol));
}
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
} // endfor: looped over all non-HealPix projections
// Exit test
return;
}
/***************************************************************************
* Test: GSkymap_healpix_construct *
***************************************************************************/
void TestGSky::test_GSkymap_healpix_construct(void)
{
// Test void constructor
test_try("Test void constructor");
try {
GSkymap map;
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test correct Healpix constructors
test_try("Test correct Healpix constructors");
try {
GSkymap ring1("HPX", "GAL", 1, "RING", 1);
GSkymap ring2("HPX", "GAL", 2, "RING", 1);
GSkymap ring4("HPX", "GAL", 4, "RING", 1);
GSkymap ring8("HPX", "GAL", 8, "RING", 1);
GSkymap ring16("HPX", "GAL", 16, "RING", 1);
GSkymap ring32("HPX", "GAL", 32, "RING", 1);
GSkymap ring64("HPX", "GAL", 64, "RING", 1);
GSkymap ring128("HPX", "GAL", 128, "RING", 1);
GSkymap ring256("HPX", "GAL", 256, "RING", 1);
GSkymap ring512("HPX", "GAL", 512, "RING", 1);
/*
GSkymap ring1024("HPX", "GAL", 1024, "RING", 1);
GSkymap ring2048("HPX", "GAL", 2048, "RING", 1);
GSkymap ring4096("HPX", "GAL", 4096, "RING", 1);
GSkymap ring8192("HPX", "GAL", 8192, "RING", 1);
*/
GSkymap nest1("HPX", "GAL", 1, "NEST", 1);
GSkymap nest2("HPX", "GAL", 2, "NEST", 1);
GSkymap nest4("HPX", "GAL", 4, "NEST", 1);
GSkymap nest8("HPX", "GAL", 8, "NEST", 1);
GSkymap nest16("HPX", "GAL", 16, "NEST", 1);
GSkymap nest32("HPX", "GAL", 32, "NEST", 1);
GSkymap nest64("HPX", "GAL", 64, "NEST", 1);
GSkymap nest128("HPX", "GAL", 128, "NEST", 1);
GSkymap nest256("HPX", "GAL", 256, "NEST", 1);
GSkymap nest512("HPX", "GAL", 512, "NEST", 1);
/*
GSkymap nest1024("HPX", "GAL", 1024, "NEST", 1);
GSkymap nest2048("HPX", "GAL", 2048, "NEST", 1);
GSkymap nest4096("HPX", "GAL", 4096, "NEST", 1);
GSkymap nest8192("HPX", "GAL", 8192, "NEST", 1);
*/
GSkymap map1("HPX", "CEL", 1, "RING", 1);
GSkymap map2("HPX", "CEL", 1, "RING", 2);
GSkymap map3("HPX", "EQU", 1, "RING", 1);
GSkymap map4("HPX", "EQU", 1, "NESTED", 1);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test Healpix copy constructor
test_try("Test Healpix copy constructor");
try {
GSkymap ring1("HPX", "GAL", 1, "RING", 1);
GSkymap ring2 = ring1;
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test invalid wcs in constructor
test_try("Test invalid wcs in constructor");
try {
GSkymap map("CAR", "GAL", 1, "RING", 1);
test_try_failure();
}
catch (GException::wcs_invalid &e) {
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test invalid coordsys in constructor
test_try("Test invalid coordsys in constructor");
try {
GSkymap map("HPX", "HOR", 1, "RING", 1);
test_try_failure();
}
catch (GException::wcs_bad_coords &e) {
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test invalid nside in constructor
test_try("Test invalid nside in constructor");
try {
GSkymap map("HPX", "GAL", 3, "RING", 1);
test_try_failure();
}
catch (GException::wcs_hpx_bad_nside &e) {
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test invalid ordering in constructor
test_try("Test invalid ordering in constructor");
try {
GSkymap map("HPX", "GAL", 2, "SPHERICAL", 1);
test_try_failure();
}
catch (GException::wcs_hpx_bad_ordering &e) {
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test invalid nmaps in constructor
test_try("Test invalid nmaps in constructor");
try {
GSkymap map("HPX", "GAL", 2, "NEST", 0);
test_try_failure();
}
catch (GException::skymap_bad_par &e) {
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Exit test
return;
}
/***************************************************************************
* Test: GSkymap_healpix_io *
***************************************************************************/
void TestGSky::test_GSkymap_healpix_io(void)
{
// Set filenames
const std::string file1 = "test_skymap_hpx_1.fits";
const std::string file2 = "test_skymap_hpx_2.fits";
// Define Healpix map for comparison
GSkymap refmap("HPX", "GAL", 4, "RING", 1);
// Test Healpix map saving
test_try("Test Healpix map saving");
try {
for (int pix = 0; pix < refmap.npix(); ++pix)
refmap(pix) = pix+1;
refmap.save(file1, true);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test Healpix map loading
test_try("Test Healpix map loading");
try {
GSkymap map;
map.load(file1);
int diff = 0;
for (int pix = 0; pix < refmap.npix(); ++pix) {
if (map(pix) != refmap(pix))
diff++;
}
if (diff > 0) {
throw exception_error("Loaded file differs from saved file.");
}
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test Healpix map instantiation
test_try("Test Healpix map instantiation");
try {
GSkymap map(file1);
int diff = 0;
for (int pix = 0; pix < refmap.npix(); ++pix) {
if (map(pix) != refmap(pix))
diff++;
}
if (diff > 0) {
throw exception_error("Loaded file differs from saved file.");
}
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Exit test
return;
}
/***************************************************************************
* Test: GSkymap_wcs_construct *
***************************************************************************/
void TestGSky::test_GSkymap_wcs_construct(void)
{
// Set precision
double eps = 1.0e-5;
// Test void constructor
test_try("Test void constructor");
try {
GSkymap map;
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test non-Healpix constructors
test_try("Test non-Healpix constructors");
try {
GSkymap map1("CAR", "GAL", 0.0, 0.0, 1.0, 1.0, 100, 100);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test CAR projection
test_try("Test CAR projection");
try {
GSkymap map1("CAR", "GAL", 138.817, 37.293, 0.521, 0.931, 100, 100);
GSkyDir dir;
for (int l = -180; l < 180; ++l) {
for (int b = -90; b < 90; ++b) {
dir.lb_deg(double(l),double(b));
GSkyPixel pixel = map1.dir2xy(dir);
GSkyDir dir_back = map1.xy2dir(pixel);
double dist = dir.dist_deg(dir_back);
if (dist > eps) {
throw exception_failure("Sky direction differs: dir="+dir.print()+" pixel="+pixel.print()+" dir_back"+ dir_back.print()+" dist="+str(dist)+" deg");
}
}
}
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Exit test
return;
}
/***************************************************************************
* Test: GSkymap_wcs_io *
***************************************************************************/
void TestGSky::test_GSkymap_wcs_io(void)
{
// Set filenames
const std::string file1 = "test_skymap_wcs_1.fits";
const std::string file2 = "test_skymap_wcs_2.fits";
// Define WCS map for comparison
GSkymap refmap1("CAR", "GAL", 0.0, 0.0, -1.0, 1.0, 100, 100);
GSkymap refmap2("CAR", "CEL", 0.0, 0.0, -0.3, 0.3, 200, 200);
// Test WCS map saving
test_try("Test WCS map saving");
try {
for (int pix = 0; pix < refmap1.npix(); ++pix)
refmap1(pix) = pix+1;
refmap1.save(file1, 1);
for (int pix = 0; pix < refmap2.npix(); ++pix)
refmap2(pix) = pix+1;
refmap2.save(file2, 1);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test WCS map loading (1)
test_try("Test WCS map loading (1)");
try {
GSkymap map;
map.load(file1);
int diff = 0;
for (int pix = 0; pix < refmap1.npix(); ++pix) {
if (map(pix) != refmap1(pix))
diff++;
}
if (diff > 0) {
throw exception_error("Loaded file "+file1+" differs from saved file .");
}
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test WCS map loading (2)
test_try("Test WCS map loading (2)");
try {
GSkymap map;
map.load(file2);
int diff = 0;
for (int pix = 0; pix < refmap2.npix(); ++pix) {
if (map(pix) != refmap2(pix))
diff++;
}
if (diff > 0) {
throw exception_error("Loaded file "+file2+" differs from saved file .");
}
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Exit test
return;
}
/***************************************************************************
* Main test function *
***************************************************************************/
int main(void)
{
GTestSuites testsuites("GSky");
bool was_successful=true;
//Create a test suite
TestGSky test;
//Append to the container
testsuites.append(test);
//Run
was_successful=testsuites.run();
//save xml report
testsuites.save("reports/GSky.xml");
// Return
return was_successful ? 0:1;
}