FORS Pipeline Reference Manual  4.12.5
fors_cpl_wcs-test.c
1 /* $Id: fors_cpl_wcs-test.c,v 1.2 2008-02-07 14:41:21 cizzo Exp $
2  *
3  * This file is part of the ESO Common Pipeline Library
4  * Copyright (C) 2001-2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: cizzo $
23  * $Date: 2008-02-07 14:41:21 $
24  * $Revision: 1.2 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_WCS
29 
30 /*----------------------------------------------------------------------------
31  Includes
32  ----------------------------------------------------------------------------*/
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <math.h>
38 #include <float.h>
39 
40 #include <cpl_wcs.h>
41 #include <cpl.h>
42 //#include "cpl_tools.h"
43 
44 #define NSK 2
45 #define NDK 13
46 #define NIK 3
47 
48 
49 
50 #define fors_cpl_test(bool) \
51  ((bool) ? (cpl_msg_debug(__FILE__, \
52  "OK in line %d (CPL-error state: '%s' in %s): %s",__LINE__, \
53  cpl_error_get_message(), cpl_error_get_where(), #bool), 0) \
54  : (cpl_msg_error(__FILE__, \
55  "Failure in line %d (CPL-error state: '%s' in %s): %s",__LINE__, \
56  cpl_error_get_message(), cpl_error_get_where(), #bool), 1))
57 
58 
59 
60 
61 
62 static const char *skeys[NSK] = {"CTYPE1", "CTYPE2"};
63 
64 static const char *dkeys[NDK] = {"CRVAL1", "CRVAL2", "CRPIX1", "CRPIX2",
65  "CD1_1", "CD1_2", "CD2_1", "CD2_2", "PV2_1",
66  "PV2_2", "PV2_3", "PV2_4", "PV2_5"};
67 static const char *ikeys[NIK] = {"NAXIS","NAXIS1","NAXIS2"};
68 
69 
70 static const char *svals[NSK] = {"RA---ZPN", "DEC--ZPN"};
71 static const double dvals[NDK] = {5.57368333333, -72.0576388889, 5401.6, 6860.8,
72  5.81347849634012E-21, 9.49444444444444E-05,
73  -9.49444444444444E-05, -5.81347849634012E-21,
74  1.0, 0.0, 42.0, 0.0, 0.0};
75 static const int ivals[NIK] = {2, 2048, 2048};
76 
77 #define NP 2
78 static double physin[2*NP] = {1024.0, 1024.0, 1025.0, 1023.0};
79 
80 static double worldout[2*NP] = {3.825029720, -71.636524754,
81  3.824722171, -71.636616487};
82 static double stdcout[2] = {-0.554171733, 0.415628800};
83 
84 
85 static double worldin[2] = {3.824875946, -71.636570620};
86 static double physout[2] = {1024.5, 1023.5};
87 
88 
89 int main (void) {
90  cpl_boolean is_debug;
91  int i,nfail;
92  cpl_propertylist *pl;
93  cpl_wcs *wcs;
94  cpl_matrix *from,*to;
95  cpl_array *status;
96  double d1,d2;
97 
98  /* Initialise */
99 
100  cpl_init(CPL_INIT_DEFAULT);
101  cpl_msg_set_level(CPL_MSG_WARNING);
102  cpl_msg_set_level_from_env();
103  cpl_msg_set_domain_off();
104  is_debug = cpl_msg_get_level() <= CPL_MSG_DEBUG ? TRUE : FALSE;
105 
106  /* Read in all the WCS properties, except for NAXIS? entries*/
107 
108  pl = cpl_propertylist_new();
109  for (i = 0; i < NSK; i++)
110  cpl_propertylist_append_string(pl,skeys[i],svals[i]);
111  for (i = 0; i < NDK; i++)
112  cpl_propertylist_append_double(pl,dkeys[i],dvals[i]);
113 
114  /* Now test cpl_wcs_new_from_propertylist to make sure we get the
115  correct errors */
116 
117  nfail = 0;
118  wcs = cpl_wcs_new_from_propertylist(NULL);
119  nfail += fors_cpl_test(cpl_error_get_code() == CPL_ERROR_NULL_INPUT);
120  nfail += fors_cpl_test(wcs == NULL);
121  cpl_error_reset();
122  if (wcs != NULL)
123  cpl_wcs_delete(wcs);
124  wcs = cpl_wcs_new_from_propertylist(pl);
125  nfail += fors_cpl_test(cpl_error_get_code() == CPL_ERROR_NONE);
126  nfail += fors_cpl_test(wcs != NULL);
127  cpl_error_reset();
128  if (wcs != NULL)
129  cpl_wcs_delete(wcs);
130 
131  /* OK, now insert the rest of the propertylist */
132 
133  for (i = 0; i < NIK; i++)
134  cpl_propertylist_append_int(pl,ikeys[i],ivals[i]);
135 
136  /* Get a wcs structure */
137 
138  wcs = cpl_wcs_new_from_propertylist(pl);
139  nfail += fors_cpl_test(wcs != NULL);
140  cpl_propertylist_delete(pl);
141 
142  /* Test cpl_wcs_convert to see if we get the correct error messages */
143 
144  nfail += fors_cpl_test(cpl_wcs_convert(NULL,NULL,&to,&status,
145  CPL_WCS_PHYS2WORLD) == CPL_ERROR_NULL_INPUT);
146  cpl_error_reset();
147  nfail += fors_cpl_test(cpl_wcs_convert(wcs,NULL,&to,&status,
148  CPL_WCS_PHYS2WORLD) == CPL_ERROR_NULL_INPUT);
149  cpl_error_reset();
150 
151  /* Ok, do a conversion of physical to world coordinates */
152 
153  cpl_msg_info("","Transform physical -> world (2 points)");
154  from = cpl_matrix_wrap(NP,2,physin);
155  cpl_wcs_convert(wcs,from,&to,&status,CPL_WCS_PHYS2WORLD);
156  nfail += (cpl_error_get_code() != CPL_ERROR_NONE);
157  cpl_error_reset();
158  cpl_matrix_unwrap(from);
159 
160  /* Test the output values. The status should all be 0. The output matrix
161  is compared to predifined world coordinate values */
162 
163  for (i = 0; i < NP; i++)
164  nfail += fors_cpl_test(cpl_array_get_data_int(status)[i] == 0);
165  d1 = fabs(worldout[0] - cpl_matrix_get(to,0,0));
166  d2 = fabs(worldout[1] - cpl_matrix_get(to,0,1));
167  cpl_msg_info("","phys1,phys2: %15.9f %15.9f",physin[0],physin[1]);
168  cpl_msg_info("","world1,world2: %15.9f %15.9f",worldout[0],worldout[1]);
169  cpl_msg_info("","calc1,calc2: %15.9f %15.9f",cpl_matrix_get(to,0,0),
170  cpl_matrix_get(to,0,1));
171  cpl_msg_info("","diff1,diff2: %15.9f %15.9f",d1,d2);
172  cpl_msg_info("","status: %d",(cpl_array_get_data_int(status)[0]));
173  nfail += fors_cpl_test(d1 < 1.0e-6);
174  nfail += fors_cpl_test(d2 < 1.0e-6);
175  d1 = fabs(worldout[2] - cpl_matrix_get(to,1,0));
176  d2 = fabs(worldout[3] - cpl_matrix_get(to,1,1));
177  cpl_msg_info("","phys1,phys2: %15.9f %15.9f",physin[2],physin[3]);
178  cpl_msg_info("","world1,world2: %15.9f %15.9f",worldout[2],worldout[3]);
179  cpl_msg_info("","calc1,calc2: %15.9f %15.9f",cpl_matrix_get(to,1,0),
180  cpl_matrix_get(to,1,1));
181  cpl_msg_info("","diff1,diff2: %15.9f %15.9f",d1,d2);
182  cpl_msg_info("","status: %d",
183  (cpl_array_get_data_int(status)[1]));
184  nfail += fors_cpl_test(d1 < 1.0e-6);
185  nfail += fors_cpl_test(d2 < 1.0e-6);
186  cpl_matrix_delete(to);
187  cpl_array_delete(status);
188 
189  /* Do world to physical conversion */
190 
191  cpl_msg_info("","Transform world -> physical");
192  from = cpl_matrix_wrap(1,2,worldin);
193 // cpl_matrix_dump(from, stdout);
194  cpl_wcs_convert(wcs,from,&to,&status,CPL_WCS_WORLD2PHYS);
195  nfail += (cpl_error_get_code() != CPL_ERROR_NONE);
196  cpl_error_reset();
197  cpl_matrix_unwrap(from);
198 
199  /* Test the output values again */
200 
201  nfail += fors_cpl_test(cpl_array_get_data_int(status)[0] == 0);
202  d1 = fabs(physout[0] - cpl_matrix_get(to,0,0));
203  d2 = fabs(physout[1] - cpl_matrix_get(to,0,1));
204  cpl_msg_info("","world1,world2: %15.9f %15.9f",worldin[0],worldin[1]);
205  cpl_msg_info("","phys1,phys2: %15.9f %15.9f",physout[0],physout[1]);
206  cpl_msg_info("","calc1,calc2: %15.9f %15.9f",cpl_matrix_get(to,0,0),
207  cpl_matrix_get(to,0,1));
208  cpl_msg_info("","diff1,diff2: %15.9f %15.9f",d1,d2);
209  cpl_msg_info("","status: %d",(cpl_array_get_data_int(status)[0]));
210  nfail += fors_cpl_test(d1 < 2.5e-4);
211  nfail += fors_cpl_test(d2 < 2.5e-4);
212  cpl_matrix_delete(to);
213  cpl_array_delete(status);
214 
215  /* Do physical to standard */
216 
217  cpl_msg_info("","Transform physical -> standard");
218  from = cpl_matrix_wrap(1,2,physin);
219 // cpl_matrix_dump(from, stdout);
220 
221  cpl_wcs_convert(wcs,from,&to,&status,CPL_WCS_PHYS2STD);
222  nfail += (cpl_error_get_code() != CPL_ERROR_NONE);
223  cpl_error_reset();
224  cpl_matrix_unwrap(from);
225 
226  /* Test the output values again */
227 
228  nfail += fors_cpl_test(cpl_array_get_data_int(status)[0] == 0);
229  d1 = fabs(stdcout[0] - cpl_matrix_get(to,0,0));
230  d2 = fabs(stdcout[1] - cpl_matrix_get(to,0,1));
231  cpl_msg_info("","phys1,phys2: %15.9f %15.9f",physin[0],physin[1]);
232  cpl_msg_info("","std1,std2: %15.9f %15.9f",stdcout[0],stdcout[1]);
233  cpl_msg_info("","calc1,calc2: %15.9f %15.9f",cpl_matrix_get(to,0,0),
234  cpl_matrix_get(to,0,1));
235  cpl_msg_info("","diff1,diff2: %15.9f %15.9f",d1,d2);
236  cpl_msg_info("","status: %d",(cpl_array_get_data_int(status)[0]));
237  nfail += fors_cpl_test(d1 < 1.7e-9);
238  nfail += fors_cpl_test(d2 < 1.7e-9);
239  cpl_matrix_delete(to);
240  cpl_array_delete(status);
241 
242  /* Do world to standard */
243 
244  cpl_msg_info("","Transform world -> standard");
245  from = cpl_matrix_wrap(1,2,worldout);
246 // cpl_matrix_dump(from, stdout);
247 
248  cpl_wcs_convert(wcs,from,&to,&status,CPL_WCS_WORLD2STD);
249  nfail += (cpl_error_get_code() != CPL_ERROR_NONE);
250  cpl_error_reset();
251  cpl_matrix_unwrap(from);
252 
253  /* Test the output values again */
254 
255  nfail += fors_cpl_test(cpl_array_get_data_int(status)[0] == 0);
256  d1 = fabs(stdcout[0] - cpl_matrix_get(to,0,0));
257  d2 = fabs(stdcout[1] - cpl_matrix_get(to,0,1));
258  cpl_msg_info("","world1,world2: %15.9f %15.9f",worldout[0],worldout[1]);
259  cpl_msg_info("","std1,std2: %15.9f %15.9f",stdcout[0],stdcout[1]);
260  cpl_msg_info("","calc1,calc2: %15.9f %15.9f",cpl_matrix_get(to,0,0),
261  cpl_matrix_get(to,0,1));
262  cpl_msg_info("","diff1,diff2: %15.9f %15.9f",d1,d2);
263  cpl_msg_info("","status: %d",(cpl_array_get_data_int(status)[0]));
264  nfail += fors_cpl_test(d1 < 1.7e-9);
265  nfail += fors_cpl_test(d2 < 1.7e-9);
266  cpl_matrix_delete(to);
267  cpl_array_delete(status);
268 
269  /* Tidy */
270 
271  cpl_wcs_delete(wcs);
272 
273  /* Are there any memory leaks (NB: this only covers CPL. Memory is
274  allocated separately by WCSLIB */
275 
276  nfail += fors_cpl_test(cpl_memory_is_empty());
277 
278  /* Did any tests fail? */
279 
280  cpl_msg_info("","%d test(s) failed",nfail);
281 
282  /* Tidy and exit */
283 
284  if (is_debug || !cpl_memory_is_empty())
285  cpl_memory_dump();
286  cpl_end();
287  return(nfail);
288 }
289 
290 
291 #else
292 
293 #include <test.h>
294 #include <cpl.h>
295 #include <stdio.h>
296 #include <stdlib.h>
297 int main(void)
298 {
299  TEST_INIT;
300 
301  cpl_msg_info(cpl_func, "WCS module not available");
302 
303  TEST_END;
304 }
305 
306 #endif
307 
308 
309 
310 
311 
312 
int main(void)
Generic library tests.
Definition: fors-test.c:57