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

Generated on 9 Mar 2012 for UVES Pipeline Reference Manual by  doxygen 1.6.1