CR2RE Pipeline Reference Manual 1.6.10
irplib_wcs-test.c
1/* $Id: irplib_wcs-test.c,v 1.9 2013-01-29 08:43:33 jtaylor Exp $
2 *
3 * This file is part of the ESO Common Pipeline Library
4 * Copyright (C) 2001-2008 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * $Author: jtaylor $
23 * $Date: 2013-01-29 08:43:33 $
24 * $Revision: 1.9 $
25 * $Name: not supported by cvs2svn $
26 */
27
28/*-----------------------------------------------------------------------------
29 Includes
30 -----------------------------------------------------------------------------*/
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include <cpl_test.h>
36
37#include "irplib_wcs.h"
38#include "math.h"
39
40/*-----------------------------------------------------------------------------
41 Static functions
42 -----------------------------------------------------------------------------*/
43static void irplib_wcs_all_test(int);
44
45static void irplib_wcs_mjd_test(void);
46
47static void irplib_wcs_great_circle_dist_test(void);
48
49/*-----------------------------------------------------------------------------
50 Main
51 -----------------------------------------------------------------------------*/
52int main (void)
53{
54
55 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
56
57 irplib_wcs_all_test(-1);
58 irplib_wcs_all_test(0);
59 irplib_wcs_all_test(1);
60
61 irplib_wcs_mjd_test();
62
63 irplib_wcs_great_circle_dist_test();
64
65 return cpl_test_end(0);
66}
67
68/*----------------------------------------------------------------------------*/
75/*----------------------------------------------------------------------------*/
76static void irplib_wcs_all_test(int naxismode)
77{
78 const double xorig = 1.0;
79 const double yorig = 2.0;
80 double xnew,ynew;
81 double ra, dec;
82 cpl_propertylist * prop_wcs;
83 cpl_wcs * wcs = NULL;
84 cpl_error_code error;
85
86
87 /* Create WCS object */
88 prop_wcs = cpl_propertylist_new();
89 cpl_test_nonnull(prop_wcs);
90
91 if (naxismode < 0) { /* NAXIS inconsistent with WCS keys */
92 cpl_propertylist_append_int(prop_wcs, "NAXIS", 1);
93 cpl_propertylist_append_int(prop_wcs, "NAXIS1", 42);
94 }
95 else if (naxismode > 0) {
96 cpl_propertylist_append_int(prop_wcs, "NAXIS", 2);
97 cpl_propertylist_append_int(prop_wcs, "NAXIS1", 42);
98 cpl_propertylist_append_int(prop_wcs, "NAXIS2", 42);
99 }
100 cpl_propertylist_append_double(prop_wcs, "CRVAL1", 10.);
101 cpl_propertylist_append_double(prop_wcs, "CRVAL2", 20.);
102 cpl_propertylist_append_int(prop_wcs, "CRPIX1", 1);
103 cpl_propertylist_append_int(prop_wcs, "CRPIX2", 2);
104 cpl_propertylist_append_double(prop_wcs, "CD1_1", 10.);
105 cpl_propertylist_append_double(prop_wcs, "CD1_2", 11.);
106 cpl_propertylist_append_double(prop_wcs, "CD2_1", 13.);
107 cpl_propertylist_append_double(prop_wcs, "CD2_2", 14.);
108
109 cpl_test_error(CPL_ERROR_NONE);
110
111 wcs = cpl_wcs_new_from_propertylist(prop_wcs);
112 cpl_propertylist_delete(prop_wcs);
113
114 if (cpl_error_get_code() == CPL_ERROR_NO_WCS) {
115
116 cpl_msg_warning(cpl_func, "No WCS present. Tests disabled");
117 cpl_test_error(CPL_ERROR_NO_WCS);
118 cpl_test_null(wcs);
119
120 } else {
121
122 cpl_test_nonnull(wcs);
123
124 /* Test that a simple call to xytoradec does not fail*/
125 error = irplib_wcs_xytoradec(wcs, xorig, yorig, &ra, &dec);
126 cpl_test_eq_error(error, CPL_ERROR_NONE);
127
128 /* Get the transformation back and compare */
129 error = irplib_wcs_radectoxy(wcs, ra, dec, &xnew, &ynew);
130 cpl_test_eq_error(error, CPL_ERROR_NONE);
131
132 cpl_test_abs(xnew, xorig, 2.0 * DBL_EPSILON);
133 cpl_test_abs(ynew, yorig, 2.0 * DBL_EPSILON);
134
135 /* Error testing */
136
137 error = irplib_wcs_xytoradec(wcs, xorig, yorig, NULL, &dec);
138 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
139
140 error = irplib_wcs_radectoxy(wcs, ra, dec, NULL, &ynew);
141 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
142
143 error = irplib_wcs_xytoradec(wcs, xorig, yorig, &ra, NULL);
144 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
145
146 error = irplib_wcs_radectoxy(wcs, ra, dec, &xnew, NULL);
147 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
148
149 cpl_wcs_delete(wcs);
150
151 }
152
153 /* Error testing */
154
155 error = irplib_wcs_xytoradec(NULL, xorig, yorig, &ra, &dec);
156 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
157
158 error = irplib_wcs_radectoxy(NULL, ra, dec, &xnew, &ynew);
159 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
160
161
162}
163
164
165/*----------------------------------------------------------------------------*/
171/*----------------------------------------------------------------------------*/
172static void irplib_wcs_mjd_test(void)
173{
174
175 /* Matching example from some VLT header */
176 const char * iso8601 = "2010-07-13T23:24:39.284";
177 const double mjd = 55390.97545467;
178
179 /* Two equal dates */
180 const char * iso8601a = "2010-07-13T24:00:00";
181 const char * iso8601b = "2010-07-14T00:00:00.000";
182
183 const double mstol = 1e-3/86400.0; /* 1ms tolerance in MJD */
184 int year, day, month, hour, minute;
185 double second;
186 double tmjd, tmjd2;
187 cpl_error_code error;
188
189 /* The MJD counts the number of days since November 17, 1858 */
190 /* Test 1a: Conversion of MJD == 0 */
191 error = irplib_wcs_iso8601_from_mjd(&year, &month, &day, &hour,
192 &minute, &second, 0.0);
193 cpl_test_eq_error(error, CPL_ERROR_NONE);
194
195 cpl_test_eq(year, 1858);
196 cpl_test_eq(month, 11);
197 cpl_test_eq(day, 17);
198 cpl_test_eq(hour, 0);
199 cpl_test_eq(minute, 0);
200 cpl_test_abs(second, 0.0, 2.0 * DBL_EPSILON);
201
202 /* Test 1b: - and convert back */
203 error = irplib_wcs_mjd_from_iso8601(&tmjd, year, month, day, hour, minute,
204 second);
205 cpl_test_eq_error(error, CPL_ERROR_NONE);
206
207 cpl_test_abs(tmjd, 0.0, 2.0 * DBL_EPSILON);
208
209 /* Test 2: Conversion back and forth of some recent date */
210 error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
211 &minute, &second, iso8601);
212 cpl_test_eq_error(error, CPL_ERROR_NONE);
213
214 error = irplib_wcs_mjd_from_iso8601(&tmjd, year, month, day, hour, minute,
215 second);
216 cpl_test_eq_error(error, CPL_ERROR_NONE);
217
218 cpl_test_abs(mjd, tmjd, mstol);
219
220 error = irplib_wcs_mjd_from_string(&tmjd, iso8601);
221 cpl_test_eq_error(error, CPL_ERROR_NONE);
222
223 cpl_test_abs(mjd, tmjd, mstol);
224
225 error = irplib_wcs_iso8601_from_mjd(&year, &month, &day, &hour,
226 &minute, &second, mjd);
227 cpl_test_eq_error(error, CPL_ERROR_NONE);
228
229 error = irplib_wcs_mjd_from_iso8601(&tmjd, year, month, day, hour, minute,
230 second);
231 cpl_test_eq_error(error, CPL_ERROR_NONE);
232
233 cpl_test_abs(mjd, tmjd, 2.0 * DBL_EPSILON);
234
235 /* Test 3: 24:00:00 == 00.00.00 + 1 day */
236 error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
237 &minute, &second, iso8601a);
238 cpl_test_eq_error(error, CPL_ERROR_NONE);
239
240 error = irplib_wcs_mjd_from_iso8601(&tmjd, year, month, day, hour, minute,
241 second);
242 cpl_test_eq_error(error, CPL_ERROR_NONE);
243
244 error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
245 &minute, &second, iso8601b);
246 cpl_test_eq_error(error, CPL_ERROR_NONE);
247
248 error = irplib_wcs_mjd_from_iso8601(&tmjd2, year, month, day, hour, minute,
249 second);
250 cpl_test_eq_error(error, CPL_ERROR_NONE);
251
252 cpl_test_abs(tmjd, tmjd2, 2.0 * DBL_EPSILON);
253
254 /* Test 4: Do not allow days from y10k */
255 error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
256 &minute, &second,
257 "10000-07-13T23:24:39.284");
258 cpl_test_eq_error(error, CPL_ERROR_ILLEGAL_INPUT);
259
260 /* Test 5: Verify validation of length of a non-leap year month */
261 error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
262 &minute, &second,
263 "2010-02-29T23:24:39.284");
264 cpl_test_eq_error(error, CPL_ERROR_ILLEGAL_INPUT);
265
266 /* Test 6: NULL pointer checking */
267 error = irplib_wcs_mjd_from_iso8601(NULL, year, month, day, hour, minute,
268 second);
269 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
270
271 error = irplib_wcs_iso8601_from_string(NULL, &month, &day, &hour,
272 &minute, &second, iso8601);
273 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
274
275 error = irplib_wcs_iso8601_from_string(&year, NULL, &day, &hour,
276 &minute, &second, iso8601);
277 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
278
279 error = irplib_wcs_iso8601_from_string(&year, &month, NULL, &hour,
280 &minute, &second, iso8601);
281 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
282
283 error = irplib_wcs_iso8601_from_string(&year, &month, &day, NULL,
284 &minute, &second, iso8601);
285 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
286
287 error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
288 NULL, &second, iso8601);
289 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
290
291 error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
292 &minute, NULL, iso8601);
293 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
294
295 error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
296 &minute, &second, NULL);
297 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
298
299 error = irplib_wcs_iso8601_from_mjd(NULL, &month, &day, &hour,
300 &minute, &second, mjd);
301 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
302
303 error = irplib_wcs_iso8601_from_mjd(&year, NULL, &day, &hour,
304 &minute, &second, mjd);
305 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
306
307 error = irplib_wcs_iso8601_from_mjd(&year, &month, NULL, &hour,
308 &minute, &second, mjd);
309 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
310
311 error = irplib_wcs_iso8601_from_mjd(&year, &month, &day, NULL,
312 &minute, &second, mjd);
313 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
314
315 error = irplib_wcs_iso8601_from_mjd(&year, &month, &day, &hour,
316 NULL, &second, mjd);
317 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
318
319 error = irplib_wcs_iso8601_from_mjd(&year, &month, &day, &hour,
320 &minute, NULL, mjd);
321 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
322
323}
324
325/*----------------------------------------------------------------------------*/
331/*----------------------------------------------------------------------------*/
332static void irplib_wcs_great_circle_dist_test(void)
333{
334 double dist1, dist2;
335 int i,j,k;
336
337 /* Commutative */
338 dist1 = irplib_wcs_great_circle_dist(12.0, 34.0, 56.0, 78.0);
339 dist2 = irplib_wcs_great_circle_dist(56.0, 78.0, 12.0, 34.0);
340 cpl_test_abs(dist1, dist2, 0.0);
341
342 for (j = 0; j <= 360; j += 4) {
343 const double ra2 = CPL_MATH_E + (double)j;
344
345 for (i = 0; i < 180; i++) {
346 const double ra1 = (double)i;
347 const double dec1 = (double)i;
348
349 /* Poles Apart */
350 dist1 = irplib_wcs_great_circle_dist(ra2, 90.0, ra2 + ra1, -90.0);
351 cpl_test_abs(dist1, 180.0, 0.0);
352
353 /* Equatorial */
354 dist1 = irplib_wcs_great_circle_dist(ra2, 0.0, ra2 + ra1, 0.0);
355 cpl_test_abs(dist1, ra1, 2560.0 * DBL_EPSILON);
356
357 /* "I will go on the slightest errand now to the Antipodes..." */
358 dist1 = irplib_wcs_great_circle_dist(ra2, dec1, ra2 + 180.0, -dec1);
359 cpl_test_abs(dist1, 180.0, 30.0 * FLT_EPSILON);
360
361 }
362 /* Meridional */
363 for (i = -90; i <= 90; i += 3) {
364 const double dec1 = (double)i;
365 for (k = 0; k <= 90; k += 3) {
366 const double dec2 = (double)k;
367
368 dist1 = irplib_wcs_great_circle_dist(ra2, dec1, ra2, dec2);
369 cpl_test_abs(dist1, fabs(dec1 - dec2), 1024.0 * DBL_EPSILON);
370 }
371
372 dist1 = irplib_wcs_great_circle_dist(ra2, dec1, ra2 + 180.0,
373 90.0 - dec1);
374 cpl_test_abs(dist1, 90.0, 512.0 * DBL_EPSILON);
375 }
376 }
377}