UVES Pipeline Reference Manual  5.4.0
uves-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: 2009-06-05 05:49:02 $
23  * $Revision: 1.5 $
24  * $Name: not supported by cvs2svn $
25  * $Log: not supported by cvs2svn $
26  * Revision 1.4 2007/05/23 06:43:23 jmlarsen
27  * Removed unused variables
28  *
29  * Revision 1.3 2007/04/12 11:41:09 jmlarsen
30  * Check CPL+QFITS version numbers
31  *
32  * Revision 1.2 2007/03/30 07:07:56 jmlarsen
33  * Added commented out profiling test of xmemory
34  *
35  * Revision 1.1 2007/03/15 12:27:18 jmlarsen
36  * Moved unit tests to ./uves/tests and ./flames/tests
37  *
38  * Revision 1.2 2007/02/27 14:04:14 jmlarsen
39  * Move unit test infrastructure to IRPLIB
40  *
41  * Revision 1.1 2007/02/21 12:38:26 jmlarsen
42  * Renamed _test -> -test
43  *
44  * Revision 1.23 2007/01/29 12:17:54 jmlarsen
45  * Support setting verbosity from command line
46  *
47  * Revision 1.22 2006/11/24 09:39:35 jmlarsen
48  * Factored out termination code
49  *
50  * Revision 1.21 2006/11/22 08:04:59 jmlarsen
51  * Added uves_dfs unit test module
52  *
53  * Revision 1.20 2006/11/16 09:49:25 jmlarsen
54  * Fixed doxygen bug
55  *
56  * Revision 1.19 2006/11/08 14:04:03 jmlarsen
57  * Doxybugfix
58  *
59  * Revision 1.18 2006/11/06 15:30:54 jmlarsen
60  * Added missing includes
61  *
62  * Revision 1.17 2006/11/03 15:02:06 jmlarsen
63  * Added test of uves_align
64  *
65  * Revision 1.16 2006/09/11 13:59:01 jmlarsen
66  * Renamed identifier reserved by POSIX
67  *
68  * Revision 1.15 2006/08/24 11:46:18 jmlarsen
69  * Fixed typo
70  *
71  * Revision 1.14 2006/08/24 07:18:17 amodigli
72  * fixed doxygen warnings
73  *
74  * Revision 1.13 2006/08/24 06:39:57 jmlarsen
75  * Reduced maximum line length
76  *
77  * Revision 1.12 2006/08/17 14:11:25 jmlarsen
78  * Use assure_mem macro to check for memory allocation failure
79  *
80  * Revision 1.11 2006/08/14 12:13:27 jmlarsen
81  * Reset irplib error handler
82  *
83  * Revision 1.10 2006/08/14 07:45:41 amodigli
84  * doxigen doc
85  *
86  * Revision 1.9 2006/03/03 13:54:11 jmlarsen
87  * Changed syntax of check macro
88  *
89  * Revision 1.8 2006/02/03 07:47:53 jmlarsen
90  * Moved recipe implementations to ./uves directory
91  *
92  * Revision 1.7 2006/01/31 08:26:56 jmlarsen
93  * Disabled recipe run tests
94  *
95  * Revision 1.6 2006/01/25 16:14:14 jmlarsen
96  * Changed interface of gauss.fitting routine
97  *
98  * Revision 1.5 2005/12/16 14:22:22 jmlarsen
99  * Removed midas test data; Added sof files
100  *
101  * Revision 1.4 2005/11/18 10:54:43 jmlarsen
102  * Minor changes
103  *
104  * Revision 1.3 2005/11/14 13:18:44 jmlarsen
105  * Minor update
106  *
107  * Revision 1.2 2005/11/11 13:18:54 jmlarsen
108  * Reorganized code, renamed source files
109  *
110  * Revision 1.1 2005/11/10 16:33:41 jmlarsen
111  * Added weighted extraction, test of gauss. fit
112  *
113  */
114 
115 /*-----------------------------------------------------------------------------
116  Includes
117  -----------------------------------------------------------------------------*/
118 
119 #ifdef HAVE_CONFIG_H
120 # include <config.h>
121 #endif
122 
123 #include <uves_utils_cpl.h>
124 #include <uves_utils.h>
125 #include <uves_utils_wrappers.h>
126 #include <uves_error.h>
127 
128 #include <cpl_test.h>
129 
130 #include <cpl.h>
131 
132 #include <float.h>
133 /*-----------------------------------------------------------------------------
134  Defines
135  -----------------------------------------------------------------------------*/
136 
137 /*-----------------------------------------------------------------------------
138  Functions prototypes
139  -----------------------------------------------------------------------------*/
140 
141 
142 /*----------------------------------------------------------------------------*/
146 /*----------------------------------------------------------------------------*/
151 static cpl_error_code
153 {
154  cpl_image *image = NULL;
155  cpl_image *noise = NULL;
156 
157  int sizex = 200;
158  int sizey = 100;
159  int center_x = 85; /* Position should be well inside image */
160  int center_y = 55;
161  int norm[2] = {1, 1000};
162  int background[3] = {-3, 2, 900};
163  int sigma_x[2] = {2, 15};
164  int sigma_y[2] = {6, 10};
165 
166  int n_norm = sizeof(norm) / sizeof(int);
167  int n_back = sizeof(background) / sizeof(int);
168  int n_sx = sizeof(sigma_x) / sizeof(int);
169  int n_sy = sizeof(sigma_y) / sizeof(int);
170  int i_norm, i_back, i_sx, i_sy;
171 
172 
173  double tolerance_xy = 1; /* Test to this precision (pixels) */
174  double tolerance_z = 1; /* Test to this precision (height/flux) */
175 
176 
177  /* Loop over combinations of center/sigma/norm/background */
178  for (i_norm = 0; i_norm < n_norm; i_norm++)
179  for (i_back = 0; i_back < n_back; i_back++)
180  for (i_sx = 0; i_sx < n_sx ; i_sx++)
181  for (i_sy = 0; i_sy < n_sy ; i_sy++)
182  {
183  cpl_image *noisep[2] = {NULL, NULL};
184  int n_noise = sizeof(noisep) / sizeof(cpl_image *);
185  int i_noise;
186 
187  /* Create test image + poisson noise */
188  uves_free_image(&image);
189  uves_free_image(&noise);
190  image = cpl_image_new(sizex, sizey, CPL_TYPE_DOUBLE);
191  noise = cpl_image_new(sizex, sizey, CPL_TYPE_DOUBLE);
192  assure_mem( image );
193  assure_mem( noise );
194 
195  check(( cpl_image_fill_gaussian(image,
196  center_x, center_y,
197  norm[i_norm],
198  sigma_x[i_sx], sigma_y[i_sy]),
199  cpl_image_add_scalar(image, background[i_back])),
200  "Error creating test image");
201 
202  /* Set noise := sqrt(image - background)
203  * Add constant, so that noise
204  * is always positive (which is required
205  * by the fitting algorithm)
206  */
207  check(( cpl_image_fill_gaussian(noise,
208  center_x, center_y,
209  norm[i_norm],
210  sigma_x[i_sx], sigma_y[i_sy]),
211  cpl_image_power(noise, 0.5),
212  cpl_image_add_scalar(noise, .0001)),
213  "Error creating noise image");
214 
215 
216  noisep[0] = noise;
217  noisep[1] = NULL;
218  for (i_noise = 0; i_noise < n_noise; i_noise++)
219  {
220  double x0, y_0, sx, sy;
221  double height; /* Height minus background */
222  double norm_fit;
223 
224  uves_msg_debug(" In: Center = (%.2f, %.2f) "
225  "Sigma = (%.2f, %.2f) Norm = %.2f Bkg = %.2f",
226  (double) center_x, (double) center_y,
227  (double) sigma_x[i_sx], (double) sigma_y[i_sy],
228  (double) norm[i_norm], (double) background[i_back]);
229 
230  check( uves_fit_gaussian_2d_image(image, noisep[i_noise],
231  1, 1,
232  sizex, sizey,
233  &x0, &y_0, &sx, &sy,
234  &height,
235  NULL, NULL),
236  "2d fitting routine failed");
237 
238  /* Fitted height is norm / (2pi sx sy) */
239  norm_fit = height * 2 * M_PI * sx * sy;
240 
241  uves_msg_debug("Fit: Center = (%.2f, %.2f) "
242  "Sigma = (%.2f, %.2f) Norm = %.2f Height = %.2e",
243  x0, y_0,
244  sx, sy,
245  norm_fit, height);
246 
247  assure( fabs(center_x - x0) < tolerance_xy,
248  CPL_ERROR_ILLEGAL_OUTPUT,
249  "x-center deviates more than %f pixel(s)",
250  tolerance_xy);
251  assure( fabs(center_y - y_0) < tolerance_xy,
252  CPL_ERROR_ILLEGAL_OUTPUT,
253  "y-center deviates more than %f pixel(s)",
254  tolerance_xy);
255  assure( fabs(sigma_x[i_sx] - sx) < tolerance_xy,
256  CPL_ERROR_ILLEGAL_OUTPUT,
257  "sigma_x deviates more than %f pixel(s)",
258  tolerance_xy);
259  assure( fabs(sigma_y[i_sy] - sy) < tolerance_xy,
260  CPL_ERROR_ILLEGAL_OUTPUT,
261  "sigma_y deviates more than %f pixel(s)",
262  tolerance_xy);
263 
264  /* The function doesn't return the background level,
265  but this is implicitly checked when comparing the
266  inferred height */
267  assure( fabs(norm[i_norm] - norm_fit) < tolerance_z,
268  CPL_ERROR_ILLEGAL_OUTPUT,
269  "Norm deviates more than %f", tolerance_z);
270 
271  }
272  }
273  cleanup:
274  uves_free_image(&image);
275  uves_free_image(&noise);
276 
277  return cpl_error_get_code();
278 }
279 
280 
281 #if 0
282 
283 #define QFITS_MEMORY_MAXPTRS 200003
284 #define PTR_HASH(ptr) (((unsigned long int) ptr) % QFITS_MEMORY_MAXPTRS)
285 
286 //#define LOOP 100000
287 #define LOOP 1000
288 
289 static void
290 realloc_cpl(void *p)
291 {
292  int i;
293  for (i = LOOP; i >=0; i--) p = cpl_realloc(p, 16);
294  return;
295 }
296 
297 static void
298 realloc_system(void *p)
299 {
300  long i;
301  int j;
302  for (j = 0; j < 5000; j++)
303  for (i = LOOP; i >=0; i--) p = realloc(p, 16);
304  return;
305 }
306 
307 static void
308 test_xmemory(void)
309 {
310  int i;
311  int j;
312 /* int N[] = {
313  15, 15, 15, 15, 15,
314  15, 15, 15, 15, 15,
315  15, 15, 15, 15, 15,
316  15, 15, 15, 15, 15};*/
317  const int N = 15;
318  const int size[] = {
319  99440, 99820, 99820, 99820, 99820,
320  99820, 99820, 99820, 99820, 99800,
321  99820, 99820, 99820, 99820, 99820,
322  99820, 99820, 99820, 99820, 99800,
323  99820, 99820, 99820, 99820, 99820,
324  99820, 99820, 99820, 99820, 99800,
325  99820, 99820, 99820, 99820, 99800,
326  99820, 99820, 99820, 99820, 99800,
327  99820, 99820, 99820, 99820, 99800,
328  99820, 99820, 99820, 99820, 99800,
329  99820, 99820};
330 
331  for (j = 0; j < sizeof(size)/sizeof(int); j++)
332  {
333  for (i = 0; i < N; i++)
334  {
335  cpl_malloc(16);
336  }
337 
338  cpl_malloc(size[j]);
339  cpl_malloc(size[j]);
340  }
341 
342  void *p1 = cpl_malloc(16);
343  void *p2 = malloc(16);
344 
345  realloc_cpl (p1);
346  realloc_system(p2);
347 
348  const char *p = NULL;
349  printf("%c", *p);
350 
351  return;
352 
353  int M = sizeof(size)/sizeof(int);
354 
355 #if 0
356  for (j = 0; j < M; j++)
357  {
358  unsigned long alloc = 0;
359  void *p;
360  for (i = 0; i < N; i++)
361  {
362  p = cpl_malloc(16);
363  alloc += 16;
364 
365  fprintf(stderr, "%x, %d, %d alloc=%d\n", p, p, PTR_HASH(p), alloc);
366  }
367 
368  fprintf(stderr, "-----------------------%d\n", j);
369 
370  for (i = 0; i < 2; i++)
371  {
372  p = cpl_malloc(size[j]);
373  alloc += size[j];
374  fprintf(stderr, "%d %x, %d, %d alloc=%d\n", size, p, p, PTR_HASH(p), alloc);
375  }
376  fprintf(stderr, "-----------------------\n");
377  }
378 #endif
379 }
380 #endif
381 
382 /*----------------------------------------------------------------------------*/
391 /*----------------------------------------------------------------------------*/
392 
393 int main(void)
394 {
395  /* Initialize CPL + UVES messaging */
396  cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
397 
398  check( uves_check_version(),
399  "Dependency libraries version check failed");
400 
401  /* test_xmemory(); */
402 
404  "Test of gaussian fitting failed");
405 
406  cleanup:
407  return cpl_test_end(0);
408 }
409 
410