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

Generated on 8 Mar 2011 for UVES Pipeline Reference Manual by  doxygen 1.6.1