uves_extract-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/25 11:50:32 $
00023  * $Revision: 1.6 $
00024  * $Name: uves-3_3_1 $
00025  * $Log: uves_extract-test.c,v $
00026  * Revision 1.6  2007/05/25 11:50:32  jmlarsen
00027  * Re-added ORDER_TRACE_TABLE
00028  *
00029  * Revision 1.5  2007/05/23 06:43:23  jmlarsen
00030  * Removed unused variables
00031  *
00032  * Revision 1.4  2007/05/22 14:51:02  jmlarsen
00033  * Removed unused variables
00034  *
00035  * Revision 1.3  2007/05/02 13:20:28  jmlarsen
00036  * Changed interface of uves_extract_iterate
00037  *
00038  * Revision 1.2  2007/04/24 12:50:29  jmlarsen
00039  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00040  *
00041  * Revision 1.1  2007/03/15 12:27:18  jmlarsen
00042  * Moved unit tests to ./uves/tests and ./flames/tests
00043  *
00044  * Revision 1.3  2007/02/27 14:04:14  jmlarsen
00045  * Move unit test infrastructure to IRPLIB
00046  *
00047  * Revision 1.2  2007/02/22 15:38:53  jmlarsen
00048  * Changed tolerance
00049  *
00050  * Revision 1.1  2007/02/21 12:37:41  jmlarsen
00051  * Added uves_extract test
00052  *
00053  * Revision 1.2  2007/01/29 12:17:54  jmlarsen
00054  * Support setting verbosity from command line
00055  *
00056  * Revision 1.1  2006/11/28 08:26:35  jmlarsen
00057  * Added QC log unit test
00058  *
00059  */
00060 
00061 /*-----------------------------------------------------------------------------
00062                                 Includes
00063  -----------------------------------------------------------------------------*/
00064 
00065 #ifdef HAVE_CONFIG_H
00066 #  include <config.h>
00067 #endif
00068 
00069 #include <uves_extract.h>
00070 #include <uves_extract_iterate.h>
00071 #include <uves_extract_profile.h>
00072 #include <uves_test_simulate.h>
00073 #include <uves.h>
00074 #include <uves_parameters.h>
00075 #include <uves_pfits.h>
00076 #include <uves_utils_wrappers.h>
00077 #include <uves_utils_polynomial.h>
00078 #include <uves_dfs.h>
00079 #include <uves_chip.h>
00080 #include <uves_error.h>
00081 #include <irplib_test.h>
00082 
00083 #include <cpl.h>
00084 /*-----------------------------------------------------------------------------
00085                                 Defines
00086  -----------------------------------------------------------------------------*/
00087 
00088 /*-----------------------------------------------------------------------------
00089                             Functions prototypes
00090  -----------------------------------------------------------------------------*/
00091 
00092 
00093 /*----------------------------------------------------------------------------*/
00097 /*----------------------------------------------------------------------------*/
00100 /*----------------------------------------------------------------------------*/
00104 /*----------------------------------------------------------------------------*/
00105 
00106 static cpl_image *
00107 create_spectrum(int nx, int ny, int minorder, int maxorder, const polynomial *order_locations,
00108         slit_geometry sg, cpl_image **sky_spectrum)
00109 {
00110   int norders = maxorder - minorder + 1;
00111   cpl_image *spectrum = cpl_image_new(nx, norders, CPL_TYPE_DOUBLE);
00112   cpl_image *dummy = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
00113   uves_iterate_position *pos = NULL;
00114 
00115   cpl_binary *bpm = NULL;
00116   bool loop_y = false;
00117 
00118   double tot_flux = 3000;
00119 
00120   pos = uves_iterate_new(nx, ny, order_locations, minorder, maxorder, sg);
00121   *sky_spectrum = cpl_image_new(nx, norders, CPL_TYPE_DOUBLE);
00122 
00123   {
00124     int x, order;
00125     for (x = 1; x <= nx; x++)
00126       for (order = 1; order <= norders; order++)
00127     {
00128       cpl_image_reject(spectrum, x, order);
00129       cpl_image_reject(*sky_spectrum, x, order);
00130     }
00131   }
00132 
00133   for (uves_iterate_set_first(pos,
00134                   1, nx,
00135                   minorder, maxorder,
00136                   bpm,
00137                   loop_y);
00138        !uves_iterate_finished(pos); 
00139        uves_iterate_increment(pos))
00140     {
00141       int spectrum_row = pos->order - minorder + 1;
00142   
00143       check_nomsg( cpl_image_set(spectrum     , pos->x, spectrum_row, tot_flux) );
00144       check_nomsg( cpl_image_set(*sky_spectrum, pos->x, spectrum_row, 1000*pos->order) );
00145 
00146     }
00147   
00148  cleanup:
00149   uves_iterate_delete(&pos);
00150   uves_free_image(&dummy);
00151   return spectrum;
00152 }
00153 
00154 static void
00155 test_extract(void)
00156 {
00157   polynomial *order_locations = NULL;
00158   int minorder = 1;
00159   int maxorder = 30;
00160   int nx = 3000;
00161   int ny = 2000;
00162   slit_geometry sg = {30.0, 0.0};
00163   cpl_image *in_spectrum = NULL;
00164   cpl_image *in_sky = NULL;
00165   cpl_image *image = NULL;
00166   cpl_image *image_noise = NULL;
00167   uves_propertylist *image_header = NULL;
00168   cpl_table *ordertable = NULL;
00169   cpl_parameterlist *   parameters = NULL;
00170   const char *test_id = "uves_extract-test";
00171   bool extract_partial = false;
00172   bool DEBUG = true;
00173   bool blue = true;
00174   enum uves_chip chip = uves_chip_get_first(blue);
00175   cpl_image *cosmics = NULL;
00176   uves_iterate_position *pos = NULL;
00177   uves_extract_profile *profile = NULL;
00178 
00179   /* Output */
00180   cpl_image *out_spectrum = NULL;
00181   cpl_image *out_sky = NULL;
00182   cpl_image *out_sky_noise = NULL;
00183 //  uves_propertylist *spectrum_header = NULL;
00184 //  cpl_image *spectrum_noise = NULL;
00185   cpl_table *cosmic_mask = NULL;
00186   cpl_image *cosmic_image = NULL;
00187 //  cpl_table *profile_table = NULL;
00188   cpl_image *weights = NULL;
00189   cpl_table *info_tbl = NULL; 
00190   cpl_table *order_trace = NULL;
00191 
00192   /* Build data */
00193   check_nomsg( create_order_table(NULL, &order_locations, NULL,
00194                                   minorder, maxorder, nx) );
00195 
00196   check_nomsg( in_spectrum = create_spectrum(nx, ny,
00197                          minorder, maxorder,
00198                          order_locations,
00199                          sg,
00200                          &in_sky) );
00201 
00202   pos = uves_iterate_new(nx, ny, order_locations, minorder, maxorder, sg);
00203 
00204   profile = uves_extract_profile_new(uves_gauss, 
00205                                      uves_gauss_derivative, 
00206                                      4, 0, 0);
00207 
00208   profile->y0 = uves_polynomial_new_zero(2);
00209   profile->sigma = uves_polynomial_new_zero(2);
00210   uves_polynomial_shift(profile->sigma, 0, 2.5);
00211 
00212   check_nomsg( image = uves_create_image(pos,
00213                                          chip,
00214                                          in_spectrum, in_sky,
00215                                          cosmics,
00216                                          profile,
00217                                          &image_noise,
00218                                          &image_header) );
00219   
00220   uves_save_image(image, "image.fits", NULL);
00221   uves_save_image(image_noise, "noise.fits", NULL);
00222   
00223   ordertable = cpl_table_new(2);
00224   cpl_table_new_column(ordertable, "Order", CPL_TYPE_INT);
00225   cpl_table_set_int(ordertable, "Order", 0, minorder);
00226   cpl_table_set_int(ordertable, "Order", 1, maxorder);
00227 
00228   /* Extract */
00229   parameters = cpl_parameterlist_new();
00230   check_nomsg( uves_propagate_parameters_step(UVES_EXTRACT_ID, 
00231                           parameters,
00232                           test_id,
00233                           NULL));
00234 
00235   {
00236     const char *value = "optimal";  //fixme should test also linear
00237     uves_set_parameter(parameters, test_id, UVES_EXTRACT_ID ".method", CPL_TYPE_STRING, &value);
00238   }
00239 
00240   check( out_spectrum = 
00241      uves_extract(image,
00242               image_noise,
00243               image_header,
00244               ordertable,
00245               order_locations,
00246               sg.length,
00247               sg.offset,
00248               parameters,
00249               test_id,
00250               extract_partial,
00251                       DEBUG,
00252               chip,
00253               NULL,/* spectrum_header */
00254               NULL, /* spectrum_noise */
00255               &out_sky,
00256               &out_sky_noise,
00257               &cosmic_mask,
00258               &cosmic_image,
00259               NULL, /* profile_table */
00260               &weights,
00261               &info_tbl,
00262                       &order_trace),
00263      "Error during extraction");
00264 
00265   uves_save_image(out_spectrum, "spectrum.fits", NULL);
00266   
00267   /* Check results */
00268   {
00269     int x, order;
00270     
00271     for (order = minorder; order <= maxorder; order++)
00272       {
00273     int spectrum_row = order - minorder + 1;
00274     
00275     for (x = 1; x <= nx; x++)
00276       {
00277         int in_bad, out_bad;
00278         double in     = cpl_image_get( in_spectrum, x, spectrum_row, &in_bad);
00279         double sky    = cpl_image_get( in_sky     , x, spectrum_row, &in_bad);
00280         double out    = cpl_image_get(out_spectrum, x, spectrum_row, &out_bad); 
00281         double osky   = cpl_image_get(out_sky     , x, spectrum_row, &out_bad); 
00282         
00283 #if 0  /* linear */
00284         assure( out_bad || in_bad ||
00285             float_equal(out, in + sky, 0.001),
00286             CPL_ERROR_ILLEGAL_OUTPUT,
00287             "At (x, order) = (%d, %d): In = %f  +  %f (%d); Out = %f (%d)",
00288             x, order, sky, in, in_bad, out, out_bad);
00289 
00290 #else /* optimal */
00291 
00292 #if 0
00293         assure( out_bad || in_bad ||
00294             float_equal(out, in, 0.02),
00295             CPL_ERROR_ILLEGAL_OUTPUT,
00296             "Object spectrum differs at (x, order) = (%d, %d): In = %f (%d); Out = %f (%d)",
00297             x, order, in, in_bad, out, out_bad);
00298 
00299         assure( out_bad || in_bad ||
00300             float_equal(osky, sky, 0.01),
00301             CPL_ERROR_ILLEGAL_OUTPUT,
00302             "Sky spectrum differs at (x, order) = (%d, %d), sky: In = %f (%d); Out = %f (%d)",
00303             x, order, sky, in_bad, osky, out_bad);
00304 #endif
00305             if (!out_bad && !in_bad)
00306                 {
00307                     irplib_test_rel(out, in, 0.02);
00308                     irplib_test_rel(osky, sky, 0.01);
00309                 }
00310 #endif
00311         
00312       }
00313       }
00314   }
00315   
00316  cleanup:
00317   uves_free_image(&in_spectrum);
00318   uves_free_image(&in_sky);
00319   uves_free_image(&image);
00320   uves_free_image(&image_noise);
00321   uves_free_propertylist(&image_header);
00322   uves_polynomial_delete(&order_locations);
00323   uves_free_parameterlist(&parameters);
00324   uves_free_table(&ordertable);
00325   uves_iterate_delete(&pos);
00326   uves_extract_profile_delete(&profile);
00327   uves_free_image(&cosmics);
00328 
00329   uves_free_image(&out_spectrum);
00330   uves_free_image(&out_sky);
00331   uves_free_image(&out_sky_noise);
00332   uves_free_image(&weights);
00333   uves_free_table(&cosmic_mask);
00334   uves_free_image(&cosmic_image);
00335   uves_free_table(&info_tbl);
00336   uves_free_table(&order_trace);
00337   return;
00338 }
00339 
00340 
00341 static void
00342 test_iterate(void)
00343 {
00344   polynomial *order_locations;
00345   uves_iterate_position *pos = NULL;
00346   cpl_binary *bpm = NULL;
00347   bool loop_y = true;
00348   int nx = 2000;
00349   int ny = 1000;
00350   cpl_image *image = cpl_image_new(2000, 1000, CPL_TYPE_DOUBLE);
00351   int minorder = 3;
00352   int maxorder = 15;
00353   slit_geometry sg = {30.0, 0.0};
00354   
00355   check_nomsg( create_order_table(NULL, &order_locations, NULL,
00356                                   minorder, maxorder, nx) );
00357 
00358 
00359   pos = uves_iterate_new(nx, ny,
00360              order_locations,
00361              minorder, 
00362              maxorder, 
00363              sg);
00364 
00365 
00366   check( uves_iterate_set_first(pos,
00367                 1, nx,
00368                 minorder, maxorder,
00369                 bpm,
00370                 loop_y),
00371      "Set first position failed");
00372 
00373   assure( pos->x == 1 && pos->order == minorder,
00374       CPL_ERROR_ILLEGAL_OUTPUT,
00375       "Set first position failed: x, order, minorder = %d %d %d",
00376       pos->x, pos->order, minorder);
00377 
00378   {
00379     int y = pos->y;
00380 
00381     assure_nomsg( !uves_iterate_finished(pos), CPL_ERROR_ILLEGAL_OUTPUT );
00382 
00383     check( uves_iterate_increment(pos), "Increment failed");
00384     check( uves_iterate_increment(pos), "Increment failed");
00385     check( uves_iterate_increment(pos), "Increment failed");
00386     check( uves_iterate_increment(pos), "Increment failed");
00387 
00388     /* Must hold for the polynomial used */
00389     assure( pos->x == 1 && pos->y == y+4 &&
00390         pos->order == minorder, CPL_ERROR_ILLEGAL_OUTPUT,
00391         "Increment failed: x, y, order = %d, %d (%d), %d",
00392         pos->x, pos->y, y+1, pos->order);
00393   }
00394 
00395   /* Go to next order */
00396   while(pos->x < nx)
00397     {
00398       uves_iterate_increment(pos);
00399     }
00400   while(pos->x != 1)
00401     {
00402       uves_iterate_increment(pos);
00403     }
00404 
00405   {
00406     int y = pos->y;
00407     uves_iterate_increment(pos);
00408 
00409     irplib_test_eq( pos->x, 1 );
00410     irplib_test_eq( pos->y, y+1 );
00411     irplib_test_eq( pos->order, minorder+1 );
00412     /* Here was a bug... */
00413     
00414   }
00415 
00416   
00417 
00418  cleanup:
00419   uves_free_image(&image);
00420   uves_polynomial_delete(&order_locations);
00421   uves_iterate_delete(&pos);
00422   return;
00423 }
00424   
00425 /*----------------------------------------------------------------------------*/
00429 /*----------------------------------------------------------------------------*/
00430 
00431 int main(void)
00432 {
00433   IRPLIB_TEST_INIT;
00434 
00435 //  cpl_msg_set_level(CPL_MSG_DEBUG);
00436 
00437   test_iterate();
00438 
00439   test_extract();
00440 
00441   IRPLIB_TEST_END;
00442 }
00443 
00444 

Generated on Tue Jun 19 14:39:15 2007 for UVES Pipeline Reference Manual by  doxygen 1.4.6