UVES Pipeline Reference Manual  5.4.0
uves_cd_align-test.c
1 /* *
2  * This file is part of the ESO UVES Pipeline *
3  * Copyright (C) 2004,2005 European Southern Observatory *
4  * *
5  * This library is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18  * */
19 
20 /*
21  * $Author: amodigli $
22  * $Date: 2013-08-08 13:36:14 $
23  * $Revision: 1.6 $
24  * $Name: not supported by cvs2svn $
25  * $Log: not supported by cvs2svn $
26  * Revision 1.5 2013/07/02 12:42:30 amodigli
27  * Rename DEBUG to debug_mode to remove compiler error on some platforms (that name is reserved to special compiler options)
28  *
29  * Revision 1.4 2009/06/05 05:49:02 amodigli
30  * updated init/end to cpl5
31  *
32  * Revision 1.3 2007/08/30 07:56:05 amodigli
33  * fixed some doxygen warnings
34  *
35  * Revision 1.2 2007/05/23 06:43:23 jmlarsen
36  * Removed unused variables
37  *
38  * Revision 1.1 2007/03/15 12:27:18 jmlarsen
39  * Moved unit tests to ./uves/tests and ./flames/tests
40  *
41  * Revision 1.2 2007/02/27 14:04:14 jmlarsen
42  * Move unit test infrastructure to IRPLIB
43  *
44  * Revision 1.1 2007/02/08 11:38:56 jmlarsen
45  * Added cd_align recipe
46  *
47  * Revision 1.5 2007/01/31 15:11:09 jmlarsen
48  * Test of inf+nan when saving FITS files
49  */
50 
51 /*-----------------------------------------------------------------------------
52  Includes
53  -----------------------------------------------------------------------------*/
54 
55 #ifdef HAVE_CONFIG_H
56 # include <config.h>
57 #endif
58 
59 #include <uves_cd_align_impl.h>
60 #include <uves_utils_wrappers.h>
61 #include <uves_error.h>
62 #include <cpl_test.h>
63 
64 #include <cpl.h>
65 
66 #include <float.h>
67 /*-----------------------------------------------------------------------------
68  Defines
69  -----------------------------------------------------------------------------*/
70 
71 /*-----------------------------------------------------------------------------
72  Functions prototypes
73  -----------------------------------------------------------------------------*/
74 
75 
76 /*----------------------------------------------------------------------------*/
80 /*----------------------------------------------------------------------------*/
83 /*----------------------------------------------------------------------------*/
92 /*----------------------------------------------------------------------------*/
93 static double eval_gauss(double x, double my, double sigma, double norm, double back)
94 {
95  double result;
96  double a[5];
97  double xa[1];
98 
99  xa[0] = x;
100 
101  a[0] = my;
102  a[1] = sigma;
103  a[2] = norm;
104  a[3] = back;
105  a[4] = 0.01; /* linear background term */
106 
107  /* Use moffat, to have something that is not perfectly Gaussian */
108  assure( uves_moffat(xa, a, &result) == 0,
109  CPL_ERROR_ILLEGAL_OUTPUT,
110  "Moffat evalutation failed");
111 
112  cleanup:
113  return result;
114 }
115 
116 
117 /*----------------------------------------------------------------------------*/
121 /*----------------------------------------------------------------------------*/
122 static void
124 {
125  const int nx = 100;
126  const int ny = 100;
127  const double maxrow = 61.1;
128  const double sigma = 2;
129  const double norm = 6000;
130  const double background = 200;
131  cpl_image *im[2] = {NULL, NULL};
132  cpl_table *cd_align = NULL;
133  double shift;
134 
135  /* Create data */
136  im[0] = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
137  im[1] = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
138 
139  assure_mem( im[0] );
140  assure_mem( im[1] );
141 
142 
143 
144  for (shift = -20; shift < 5; shift = (shift < -5) ? shift/1.5 : shift + 0.7)
145  {
146  int x, y;
147  for (y = 1; y <= ny; y++)
148  {
149  for (x = 1; x <= nx; x++)
150  {
151  cpl_image_set(im[0], x, y,
152  eval_gauss(y, maxrow, sigma, norm, background));
153  cpl_image_set(im[1], x, y,
154  eval_gauss(y, maxrow+shift, sigma, norm, background));
155  }
156  }
157 
158  /* Call function */
159  {
160  int steps = 10;
161  int xborder = 0;
162  int window = 20;
163 
164  bool debug_mode = false;
165  enum uves_chip chip = UVES_CHIP_BLUE; /* not used */
166 
167  uves_free_table(&cd_align);
168  check( cd_align = uves_cd_align_process(
169  im[0],
170  im[1],
171  NULL, NULL,
172  steps, xborder, window, debug_mode, chip),
173  "Processing failed");
174  }
175 
176  /* Check results */
177  assure_nomsg( cpl_table_has_column(cd_align, "X" ), CPL_ERROR_ILLEGAL_OUTPUT);
178  assure_nomsg( cpl_table_has_column(cd_align, "YCEN1"), CPL_ERROR_ILLEGAL_OUTPUT);
179  assure_nomsg( cpl_table_has_column(cd_align, "YCEN2"), CPL_ERROR_ILLEGAL_OUTPUT);
180  assure_nomsg( cpl_table_has_column(cd_align, "SIGMA1"), CPL_ERROR_ILLEGAL_OUTPUT);
181  assure_nomsg( cpl_table_has_column(cd_align, "SIGMA2"), CPL_ERROR_ILLEGAL_OUTPUT);
182  assure_nomsg( cpl_table_has_column(cd_align, "BACK1"), CPL_ERROR_ILLEGAL_OUTPUT);
183  assure_nomsg( cpl_table_has_column(cd_align, "BACK2"), CPL_ERROR_ILLEGAL_OUTPUT);
184  assure_nomsg( cpl_table_has_column(cd_align, "NORM1"), CPL_ERROR_ILLEGAL_OUTPUT);
185  assure_nomsg( cpl_table_has_column(cd_align, "NORM2"), CPL_ERROR_ILLEGAL_OUTPUT);
186  assure_nomsg( cpl_table_has_column(cd_align, "YDIFF"), CPL_ERROR_ILLEGAL_OUTPUT);
187 
188  uves_msg("Shift: %f pixels. Measured shift: %f pixels",
189  shift, cpl_table_get_column_mean(cd_align, "YDIFF"));
190 
191  {
192  double abs_tolerance = 0.1; /* pixels */
193 
194  cpl_test_rel(cpl_table_get_column_mean(cd_align, "YDIFF"),
195  shift, abs_tolerance);
196  }
197 
198  } /* for shift */
199 
200  cleanup:
201  uves_free_image(&im[0]);
202  uves_free_image(&im[1]);
203  uves_free_table(&cd_align);
204 
205  return;
206 }
207 
208 /*----------------------------------------------------------------------------*/
212 /*----------------------------------------------------------------------------*/
213 
214 int main(void)
215 {
216  /* Initialize CPL + UVES messaging */
217  cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
218 
219  check( test_process(),
220  "Test of CD align failed");
221 
222  cleanup:
223  return cpl_test_end(0);
224 }
225 
226