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

Generated on Thu Nov 15 14:32:28 2007 for UVES Pipeline Reference Manual by  doxygen 1.5.1