uves-test.c

00001 /*                                                                              *
00002  *   This file is part of the ESO UVES Pipeline                                 *
00003  *   Copyright (C) 2004,2005 European Southern Observatory                      *
00004  *                                                                              *
00005  *   This library is free software; you can redistribute it and/or modify       *
00006  *   it under the terms of the GNU General Public License as published by       *
00007  *   the Free Software Foundation; either version 2 of the License, or          *
00008  *   (at your option) any later version.                                        *
00009  *                                                                              *
00010  *   This program is distributed in the hope that it will be useful,            *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
00013  *   GNU General Public License for more details.                               *
00014  *                                                                              *
00015  *   You should have received a copy of the GNU General Public License          *
00016  *   along with this program; if not, write to the Free Software                *
00017  *   Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA       *
00018  *                                                                              */
00019  
00020 /*
00021  * $Author: jmlarsen $
00022  * $Date: 2007/05/23 06:43:23 $
00023  * $Revision: 1.4 $
00024  * $Name: uves-4_2_2 $
00025  * $Log: uves-test.c,v $
00026  * Revision 1.4  2007/05/23 06:43:23  jmlarsen
00027  * Removed unused variables
00028  *
00029  * Revision 1.3  2007/04/12 11:41:09  jmlarsen
00030  * Check CPL+QFITS version numbers
00031  *
00032  * Revision 1.2  2007/03/30 07:07:56  jmlarsen
00033  * Added commented out profiling test of xmemory
00034  *
00035  * Revision 1.1  2007/03/15 12:27:18  jmlarsen
00036  * Moved unit tests to ./uves/tests and ./flames/tests
00037  *
00038  * Revision 1.2  2007/02/27 14:04:14  jmlarsen
00039  * Move unit test infrastructure to IRPLIB
00040  *
00041  * Revision 1.1  2007/02/21 12:38:26  jmlarsen
00042  * Renamed _test -> -test
00043  *
00044  * Revision 1.23  2007/01/29 12:17:54  jmlarsen
00045  * Support setting verbosity from command line
00046  *
00047  * Revision 1.22  2006/11/24 09:39:35  jmlarsen
00048  * Factored out termination code
00049  *
00050  * Revision 1.21  2006/11/22 08:04:59  jmlarsen
00051  * Added uves_dfs unit test module
00052  *
00053  * Revision 1.20  2006/11/16 09:49:25  jmlarsen
00054  * Fixed doxygen bug
00055  *
00056  * Revision 1.19  2006/11/08 14:04:03  jmlarsen
00057  * Doxybugfix
00058  *
00059  * Revision 1.18  2006/11/06 15:30:54  jmlarsen
00060  * Added missing includes
00061  *
00062  * Revision 1.17  2006/11/03 15:02:06  jmlarsen
00063  * Added test of uves_align
00064  *
00065  * Revision 1.16  2006/09/11 13:59:01  jmlarsen
00066  * Renamed identifier reserved by POSIX
00067  *
00068  * Revision 1.15  2006/08/24 11:46:18  jmlarsen
00069  * Fixed typo
00070  *
00071  * Revision 1.14  2006/08/24 07:18:17  amodigli
00072  * fixed doxygen warnings
00073  *
00074  * Revision 1.13  2006/08/24 06:39:57  jmlarsen
00075  * Reduced maximum line length
00076  *
00077  * Revision 1.12  2006/08/17 14:11:25  jmlarsen
00078  * Use assure_mem macro to check for memory allocation failure
00079  *
00080  * Revision 1.11  2006/08/14 12:13:27  jmlarsen
00081  * Reset irplib error handler
00082  *
00083  * Revision 1.10  2006/08/14 07:45:41  amodigli
00084  * doxigen doc
00085  *
00086  * Revision 1.9  2006/03/03 13:54:11  jmlarsen
00087  * Changed syntax of check macro
00088  *
00089  * Revision 1.8  2006/02/03 07:47:53  jmlarsen
00090  * Moved recipe implementations to ./uves directory
00091  *
00092  * Revision 1.7  2006/01/31 08:26:56  jmlarsen
00093  * Disabled recipe run tests
00094  *
00095  * Revision 1.6  2006/01/25 16:14:14  jmlarsen
00096  * Changed interface of gauss.fitting routine
00097  *
00098  * Revision 1.5  2005/12/16 14:22:22  jmlarsen
00099  * Removed midas test data; Added sof files
00100  *
00101  * Revision 1.4  2005/11/18 10:54:43  jmlarsen
00102  * Minor changes
00103  *
00104  * Revision 1.3  2005/11/14 13:18:44  jmlarsen
00105  * Minor update
00106  *
00107  * Revision 1.2  2005/11/11 13:18:54  jmlarsen
00108  * Reorganized code, renamed source files
00109  *
00110  * Revision 1.1  2005/11/10 16:33:41  jmlarsen
00111  * Added weighted extraction, test of gauss. fit
00112  *
00113  */
00114 
00115 /*-----------------------------------------------------------------------------
00116                                 Includes
00117  -----------------------------------------------------------------------------*/
00118 
00119 #ifdef HAVE_CONFIG_H
00120 #  include <config.h>
00121 #endif
00122 
00123 #include <uves_utils_cpl.h>
00124 #include <uves_utils.h>
00125 #include <uves_utils_wrappers.h>
00126 #include <uves_error.h>
00127 
00128 #include <irplib_test.h>
00129 
00130 #include <cpl.h>
00131 
00132 #include <float.h>
00133 /*-----------------------------------------------------------------------------
00134                                 Defines
00135  -----------------------------------------------------------------------------*/
00136 
00137 /*-----------------------------------------------------------------------------
00138                             Functions prototypes
00139  -----------------------------------------------------------------------------*/
00140 
00141 
00142 /*----------------------------------------------------------------------------*/
00146 /*----------------------------------------------------------------------------*/
00151 static cpl_error_code
00152 test_gaussian_fitting(void)
00153 {
00154     cpl_image *image = NULL;
00155     cpl_image *noise = NULL;
00156 
00157     int sizex = 200;
00158     int sizey = 100;
00159     int center_x = 85;   /* Position should be well inside image */
00160     int center_y = 55;
00161     int norm[2] = {1, 1000};
00162     int background[3] = {-3, 2, 900};
00163     int sigma_x[2] = {2, 15};
00164     int sigma_y[2] = {6, 10};
00165 
00166     int n_norm = sizeof(norm) / sizeof(int);
00167     int n_back = sizeof(background) / sizeof(int);
00168     int n_sx   = sizeof(sigma_x) / sizeof(int);
00169     int n_sy   = sizeof(sigma_y) / sizeof(int);
00170     int i_norm, i_back, i_sx, i_sy;
00171 
00172 
00173     double tolerance_xy = 1;  /* Test to this precision (pixels) */
00174     double tolerance_z  = 1;  /* Test to this precision (height/flux) */
00175 
00176 
00177     /* Loop over combinations of center/sigma/norm/background */
00178     for (i_norm = 0; i_norm < n_norm; i_norm++)
00179     for (i_back = 0; i_back < n_back; i_back++)
00180     for (i_sx   = 0; i_sx   < n_sx  ; i_sx++)
00181     for (i_sy   = 0; i_sy   < n_sy  ; i_sy++)
00182     {
00183         cpl_image *noisep[2] = {NULL, NULL};
00184         int n_noise   = sizeof(noisep) / sizeof(cpl_image *);
00185         int i_noise;
00186 
00187         /* Create test image + poisson noise */
00188         uves_free_image(&image);
00189         uves_free_image(&noise);
00190         image = cpl_image_new(sizex, sizey, CPL_TYPE_DOUBLE);
00191         noise = cpl_image_new(sizex, sizey, CPL_TYPE_DOUBLE);
00192         assure_mem( image );
00193         assure_mem( noise );
00194         
00195         check(( cpl_image_fill_gaussian(image,
00196                         center_x, center_y,
00197                         norm[i_norm],
00198                         sigma_x[i_sx], sigma_y[i_sy]),
00199             cpl_image_add_scalar(image, background[i_back])),
00200            "Error creating test image");
00201 
00202         /* Set noise := sqrt(image - background) 
00203          * Add constant, so that noise
00204          * is always positive (which is required
00205          * by the fitting algorithm)
00206          */
00207         check(( cpl_image_fill_gaussian(noise,
00208                         center_x, center_y,
00209                         norm[i_norm],
00210                         sigma_x[i_sx], sigma_y[i_sy]),
00211             cpl_image_power(noise, 0.5),
00212             cpl_image_add_scalar(noise, .0001)),
00213            "Error creating noise image");
00214      
00215 
00216         noisep[0] = noise;
00217         noisep[1] = NULL;
00218         for (i_noise = 0; i_noise < n_noise; i_noise++)
00219         {
00220             double x0, y_0, sx, sy;
00221             double height;       /* Height minus background */
00222             double norm_fit;
00223             
00224             uves_msg_debug(" In: Center = (%.2f, %.2f) "
00225                  "Sigma = (%.2f, %.2f) Norm = %.2f Bkg = %.2f",
00226                  (double) center_x, (double) center_y,
00227                  (double) sigma_x[i_sx], (double) sigma_y[i_sy],
00228                  (double) norm[i_norm], (double) background[i_back]);
00229             
00230             check( uves_fit_gaussian_2d_image(image, noisep[i_noise],
00231                         1, 1,
00232                         sizex, sizey,
00233                         &x0, &y_0, &sx, &sy,
00234                         &height,
00235                         NULL, NULL),
00236                "2d fitting routine failed");
00237             
00238             /* Fitted height is norm / (2pi sx sy) */
00239             norm_fit = height * 2 * M_PI * sx * sy;
00240             
00241             uves_msg_debug("Fit: Center = (%.2f, %.2f) "
00242                  "Sigma = (%.2f, %.2f) Norm = %.2f Height = %.2e",
00243                  x0, y_0,
00244                  sx, sy,
00245                  norm_fit, height);
00246             
00247             assure( fabs(center_x - x0) < tolerance_xy, 
00248                             CPL_ERROR_ILLEGAL_OUTPUT, 
00249                 "x-center deviates more than %f pixel(s)", 
00250                             tolerance_xy);
00251             assure( fabs(center_y - y_0) < tolerance_xy, 
00252                             CPL_ERROR_ILLEGAL_OUTPUT, 
00253                 "y-center deviates more than %f pixel(s)", 
00254                             tolerance_xy);
00255             assure( fabs(sigma_x[i_sx] - sx) < tolerance_xy, 
00256                             CPL_ERROR_ILLEGAL_OUTPUT,
00257                 "sigma_x deviates more than %f pixel(s)", 
00258                             tolerance_xy);
00259             assure( fabs(sigma_y[i_sy] - sy) < tolerance_xy, 
00260                             CPL_ERROR_ILLEGAL_OUTPUT,
00261                 "sigma_y deviates more than %f pixel(s)", 
00262                              tolerance_xy);
00263             
00264             /* The function doesn't return the background level,
00265                but this is implicitly checked when comparing the 
00266                inferred height */
00267             assure( fabs(norm[i_norm] - norm_fit) < tolerance_z, 
00268                 CPL_ERROR_ILLEGAL_OUTPUT,
00269                 "Norm deviates more than %f", tolerance_z);
00270 
00271         }
00272     }
00273   cleanup:
00274     uves_free_image(&image);
00275     uves_free_image(&noise);
00276     
00277     return cpl_error_get_code();
00278 }
00279 
00280    
00281 #if 0
00282 
00283 #define QFITS_MEMORY_MAXPTRS     200003
00284 #define PTR_HASH(ptr) (((unsigned long int) ptr) % QFITS_MEMORY_MAXPTRS)
00285 
00286 //#define LOOP 100000
00287 #define LOOP 1000
00288 
00289 static void
00290 realloc_cpl(void *p)
00291 {
00292     int i;
00293     for (i = LOOP; i >=0; i--) p = cpl_realloc(p, 16);
00294     return;
00295 }
00296 
00297 static void
00298 realloc_system(void *p)
00299 {
00300     long i;
00301     int j;
00302     for (j = 0; j < 5000; j++)
00303     for (i = LOOP; i >=0; i--) p = realloc(p, 16);
00304     return;
00305 }
00306 
00307 static void
00308 test_xmemory(void)
00309 {
00310     int i;
00311     int j;
00312 /*    int N[] = {
00313         15, 15, 15, 15, 15, 
00314         15, 15, 15, 15, 15,
00315         15, 15, 15, 15, 15, 
00316         15, 15, 15, 15, 15};*/
00317     const int N = 15;
00318     const int size[] = {
00319         99440, 99820, 99820, 99820, 99820,
00320         99820, 99820, 99820, 99820, 99800,
00321         99820, 99820, 99820, 99820, 99820,
00322         99820, 99820, 99820, 99820, 99800,
00323         99820, 99820, 99820, 99820, 99820,
00324         99820, 99820, 99820, 99820, 99800,
00325         99820, 99820, 99820, 99820, 99800,
00326         99820, 99820, 99820, 99820, 99800,
00327         99820, 99820, 99820, 99820, 99800,
00328         99820, 99820, 99820, 99820, 99800,
00329         99820, 99820};
00330 
00331     for (j = 0; j < sizeof(size)/sizeof(int); j++)
00332         {
00333             for (i = 0; i < N; i++)
00334                 {
00335                     cpl_malloc(16);
00336                 }
00337 
00338             cpl_malloc(size[j]);
00339             cpl_malloc(size[j]);
00340         }
00341 
00342     void *p1 = cpl_malloc(16);
00343     void *p2 = malloc(16);
00344     
00345     realloc_cpl   (p1);
00346     realloc_system(p2);
00347 
00348     const char *p = NULL;
00349     printf("%c", *p);
00350 
00351     return;
00352 
00353     int M = sizeof(size)/sizeof(int);
00354 
00355 #if 0
00356     for (j = 0; j < M; j++)
00357         {
00358             unsigned long alloc = 0;
00359             void *p;
00360             for (i = 0; i < N; i++)
00361                 {
00362                     p = cpl_malloc(16);
00363                     alloc += 16;                    
00364                     
00365                     fprintf(stderr, "%x, %d, %d  alloc=%d\n", p, p, PTR_HASH(p), alloc);
00366                 }
00367 
00368             fprintf(stderr, "-----------------------%d\n", j);
00369 
00370             for (i = 0; i < 2; i++)
00371                 {
00372                     p = cpl_malloc(size[j]);
00373                     alloc += size[j];
00374                     fprintf(stderr, "%d %x, %d, %d  alloc=%d\n", size, p, p, PTR_HASH(p), alloc);
00375                 }
00376             fprintf(stderr, "-----------------------\n");
00377         }
00378 #endif
00379 }
00380 #endif
00381 
00382 /*----------------------------------------------------------------------------*/
00391 /*----------------------------------------------------------------------------*/
00392 
00393 int main(void)
00394 {
00395     /* Initialize CPL + UVES messaging */
00396     IRPLIB_TEST_INIT;
00397 
00398     check( uves_check_version(),
00399            "Dependency libraries version check failed");
00400 
00401     /* test_xmemory(); */
00402 
00403     check( test_gaussian_fitting(),
00404        "Test of gaussian fitting failed");
00405 
00406   cleanup:
00407     IRPLIB_TEST_END;
00408 }
00409 
00410 

Generated on Mon Apr 21 10:56:54 2008 for UVES Pipeline Reference Manual by  doxygen 1.5.1