UVES Pipeline Reference Manual  5.4.0
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 
39 /*-----------------------------------------------------------------------------
40  Static functions
41  -----------------------------------------------------------------------------*/
42 static void irplib_wcs_all_test(void);
43 
44 static void irplib_wcs_mjd_test(void);
45 
46 
47 /*-----------------------------------------------------------------------------
48  Main
49  -----------------------------------------------------------------------------*/
50 int main (void)
51 {
52 
53  cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
54 
55  irplib_wcs_all_test();
56 
57  irplib_wcs_mjd_test();
58 
59  return cpl_test_end(0);
60 }
61 
62 /*----------------------------------------------------------------------------*/
68 /*----------------------------------------------------------------------------*/
69 static void irplib_wcs_all_test(void)
70 {
71  const double xorig = 1.0;
72  const double yorig = 2.0;
73  double xnew,ynew;
74  double ra, dec;
75  cpl_propertylist * prop_wcs;
76  cpl_wcs * wcs = NULL;
77  cpl_error_code error;
78 
79 
80  /* Create WCS object */
81  prop_wcs = cpl_propertylist_new();
82  cpl_test_nonnull(prop_wcs);
83 
84  cpl_propertylist_append_double(prop_wcs, "CRVAL1", 10.);
85  cpl_propertylist_append_double(prop_wcs, "CRVAL2", 20.);
86  cpl_propertylist_append_int(prop_wcs, "CRPIX1", 1);
87  cpl_propertylist_append_int(prop_wcs, "CRPIX2", 2);
88  cpl_propertylist_append_double(prop_wcs, "CD1_1", 10.);
89  cpl_propertylist_append_double(prop_wcs, "CD1_2", 11.);
90  cpl_propertylist_append_double(prop_wcs, "CD2_1", 13.);
91  cpl_propertylist_append_double(prop_wcs, "CD2_2", 14.);
92 
93  cpl_test_error(CPL_ERROR_NONE);
94 
95  wcs = cpl_wcs_new_from_propertylist(prop_wcs);
96  cpl_propertylist_delete(prop_wcs);
97 
98  if (cpl_error_get_code() == CPL_ERROR_NO_WCS) {
99 
100  cpl_msg_warning(cpl_func, "No WCS present. Tests disabled");
101  cpl_test_error(CPL_ERROR_NO_WCS);
102  cpl_test_null(wcs);
103 
104  } else {
105 
106  cpl_test_nonnull(wcs);
107 
108  /* Test that a simple call to xytoradec does not fail*/
109  error = irplib_wcs_xytoradec(wcs, xorig, yorig, &ra, &dec);
110  cpl_test_eq_error(error, CPL_ERROR_NONE);
111 
112  /* Get the transformation back and compare */
113  error = irplib_wcs_radectoxy(wcs, ra, dec, &xnew, &ynew);
114  cpl_test_eq_error(error, CPL_ERROR_NONE);
115 
116  cpl_test_abs(xnew, xorig, 2.0 * DBL_EPSILON);
117  cpl_test_abs(ynew, yorig, 2.0 * DBL_EPSILON);
118 
119  /* Error testing */
120 
121  error = irplib_wcs_xytoradec(wcs, xorig, yorig, NULL, &dec);
122  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
123 
124  error = irplib_wcs_radectoxy(wcs, ra, dec, NULL, &ynew);
125  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
126 
127  error = irplib_wcs_xytoradec(wcs, xorig, yorig, &ra, NULL);
128  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
129 
130  error = irplib_wcs_radectoxy(wcs, ra, dec, &xnew, NULL);
131  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
132 
133  cpl_wcs_delete(wcs);
134 
135  }
136 
137  /* Error testing */
138 
139  error = irplib_wcs_xytoradec(NULL, xorig, yorig, &ra, &dec);
140  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
141 
142  error = irplib_wcs_radectoxy(NULL, ra, dec, &xnew, &ynew);
143  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
144 
145 
146 }
147 
148 
149 /*----------------------------------------------------------------------------*/
155 /*----------------------------------------------------------------------------*/
156 static void irplib_wcs_mjd_test(void)
157 {
158 
159  /* Matching example from some VLT header */
160  const char * iso8601 = "2010-07-13T23:24:39.284";
161  const double mjd = 55390.97545467;
162 
163  /* Two equal dates */
164  const char * iso8601a = "2010-07-13T24:00:00";
165  const char * iso8601b = "2010-07-14T00:00:00.000";
166 
167  const double mstol = 1e-3/86400.0; /* 1ms tolerance in MJD */
168  int year, day, month, hour, minute;
169  double second;
170  double tmjd, tmjd2;
171  cpl_error_code error;
172 
173  /* The MJD counts the number of days since November 17, 1858 */
174  /* Test 1a: Conversion of MJD == 0 */
175  error = irplib_wcs_iso8601_from_mjd(&year, &month, &day, &hour,
176  &minute, &second, 0.0);
177  cpl_test_eq_error(error, CPL_ERROR_NONE);
178 
179  cpl_test_eq(year, 1858);
180  cpl_test_eq(month, 11);
181  cpl_test_eq(day, 17);
182  cpl_test_eq(hour, 0);
183  cpl_test_eq(minute, 0);
184  cpl_test_abs(second, 0.0, 2.0 * DBL_EPSILON);
185 
186  /* Test 1b: - and convert back */
187  error = irplib_wcs_mjd_from_iso8601(&tmjd, year, month, day, hour, minute,
188  second);
189  cpl_test_eq_error(error, CPL_ERROR_NONE);
190 
191  cpl_test_abs(tmjd, 0.0, 2.0 * DBL_EPSILON);
192 
193  /* Test 2: Conversion back and forth of some recent date */
194  error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
195  &minute, &second, iso8601);
196  cpl_test_eq_error(error, CPL_ERROR_NONE);
197 
198  error = irplib_wcs_mjd_from_iso8601(&tmjd, year, month, day, hour, minute,
199  second);
200  cpl_test_eq_error(error, CPL_ERROR_NONE);
201 
202  cpl_test_abs(mjd, tmjd, mstol);
203 
204  error = irplib_wcs_mjd_from_string(&tmjd, iso8601);
205  cpl_test_eq_error(error, CPL_ERROR_NONE);
206 
207  cpl_test_abs(mjd, tmjd, mstol);
208 
209  error = irplib_wcs_iso8601_from_mjd(&year, &month, &day, &hour,
210  &minute, &second, mjd);
211  cpl_test_eq_error(error, CPL_ERROR_NONE);
212 
213  error = irplib_wcs_mjd_from_iso8601(&tmjd, year, month, day, hour, minute,
214  second);
215  cpl_test_eq_error(error, CPL_ERROR_NONE);
216 
217  cpl_test_abs(mjd, tmjd, 2.0 * DBL_EPSILON);
218 
219  /* Test 3: 24:00:00 == 00.00.00 + 1 day */
220  error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
221  &minute, &second, iso8601a);
222  cpl_test_eq_error(error, CPL_ERROR_NONE);
223 
224  error = irplib_wcs_mjd_from_iso8601(&tmjd, year, month, day, hour, minute,
225  second);
226  cpl_test_eq_error(error, CPL_ERROR_NONE);
227 
228  error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
229  &minute, &second, iso8601b);
230  cpl_test_eq_error(error, CPL_ERROR_NONE);
231 
232  error = irplib_wcs_mjd_from_iso8601(&tmjd2, year, month, day, hour, minute,
233  second);
234  cpl_test_eq_error(error, CPL_ERROR_NONE);
235 
236  cpl_test_abs(tmjd, tmjd2, 2.0 * DBL_EPSILON);
237 
238  /* Test 4: Do not allow days from y10k */
239  error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
240  &minute, &second,
241  "10000-07-13T23:24:39.284");
242  cpl_test_eq_error(error, CPL_ERROR_ILLEGAL_INPUT);
243 
244  /* Test 5: Verify validation of length of a non-leap year month */
245  error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
246  &minute, &second,
247  "2010-02-29T23:24:39.284");
248  cpl_test_eq_error(error, CPL_ERROR_ILLEGAL_INPUT);
249 
250  /* Test 6: NULL pointer checking */
251  error = irplib_wcs_mjd_from_iso8601(NULL, year, month, day, hour, minute,
252  second);
253  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
254 
255  error = irplib_wcs_iso8601_from_string(NULL, &month, &day, &hour,
256  &minute, &second, iso8601);
257  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
258 
259  error = irplib_wcs_iso8601_from_string(&year, NULL, &day, &hour,
260  &minute, &second, iso8601);
261  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
262 
263  error = irplib_wcs_iso8601_from_string(&year, &month, NULL, &hour,
264  &minute, &second, iso8601);
265  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
266 
267  error = irplib_wcs_iso8601_from_string(&year, &month, &day, NULL,
268  &minute, &second, iso8601);
269  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
270 
271  error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
272  NULL, &second, iso8601);
273  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
274 
275  error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
276  &minute, NULL, iso8601);
277  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
278 
279  error = irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
280  &minute, &second, NULL);
281  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
282 
283  error = irplib_wcs_iso8601_from_mjd(NULL, &month, &day, &hour,
284  &minute, &second, mjd);
285  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
286 
287  error = irplib_wcs_iso8601_from_mjd(&year, NULL, &day, &hour,
288  &minute, &second, mjd);
289  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
290 
291  error = irplib_wcs_iso8601_from_mjd(&year, &month, NULL, &hour,
292  &minute, &second, mjd);
293  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
294 
295  error = irplib_wcs_iso8601_from_mjd(&year, &month, &day, NULL,
296  &minute, &second, mjd);
297  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
298 
299  error = irplib_wcs_iso8601_from_mjd(&year, &month, &day, &hour,
300  NULL, &second, mjd);
301  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
302 
303  error = irplib_wcs_iso8601_from_mjd(&year, &month, &day, &hour,
304  &minute, NULL, mjd);
305  cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
306 
307 }