uves_response-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-3_3_1 $
00025  * $Log: uves_response-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/24 12:50:29  jmlarsen
00030  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00031  *
00032  * Revision 1.2  2007/03/20 07:28:02  jmlarsen
00033  * Test TYPE = 'NULL'
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.22  2007/01/29 12:17:54  jmlarsen
00045  * Support setting verbosity from command line
00046  *
00047  * Revision 1.21  2006/11/24 09:39:35  jmlarsen
00048  * Factored out termination code
00049  *
00050  * Revision 1.20  2006/11/16 09:49:25  jmlarsen
00051  * Fixed doxygen bug
00052  *
00053  * Revision 1.19  2006/11/08 14:03:59  jmlarsen
00054  * Doxybugfix
00055  *
00056  * Revision 1.18  2006/11/07 13:59:19  jmlarsen
00057  * Removed memory leaks
00058  *
00059  * Revision 1.17  2006/11/06 15:31:52  jmlarsen
00060  * Added check for memory leak
00061  *
00062  * Revision 1.16  2006/11/06 15:30:54  jmlarsen
00063  * Added missing includes
00064  *
00065  * Revision 1.15  2006/11/06 15:19:41  jmlarsen
00066  * Removed unused include directives
00067  *
00068  * Revision 1.14  2006/11/03 15:15:46  jmlarsen
00069  * Added test of uves_align
00070  *
00071  * Revision 1.16  2006/09/11 13:59:01  jmlarsen
00072  * Renamed identifier reserved by POSIX
00073  *
00074  */
00075 
00076 /*-----------------------------------------------------------------------------
00077                                 Includes
00078  -----------------------------------------------------------------------------*/
00079 
00080 #ifdef HAVE_CONFIG_H
00081 #  include <config.h>
00082 #endif
00083 
00084 #include <uves_response_utils.h>
00085 #include <uves_utils_wrappers.h>
00086 #include <uves_pfits.h>
00087 #include <uves_error.h>
00088 
00089 #include <irplib_test.h>
00090 #include <cpl.h>
00091 
00092 /*-----------------------------------------------------------------------------
00093                                 Defines
00094  -----------------------------------------------------------------------------*/
00095 
00096 /*-----------------------------------------------------------------------------
00097                             Functions prototypes
00098  -----------------------------------------------------------------------------*/
00099 
00100 /*----------------------------------------------------------------------------*/
00104 /*----------------------------------------------------------------------------*/
00107 /*----------------------------------------------------------------------------*/
00111 /*----------------------------------------------------------------------------*/
00112 static void
00113 test_3dtable(void)
00114 {
00115     /* Create flux table + header */
00116     cpl_table *flux_table = NULL;
00117     uves_propertylist *raw_header = NULL;
00118     cpl_array *values = NULL;
00119     int depth = 10;
00120     int nrow = 2;
00121     const char *object[] = {"first std", "another standard star"};
00122     const char *type  [] = {"NULL", "type 2"};
00123     const double lambda[] = {8000, 2000};
00124     const double fluxes[] = {1000000, 200};
00125     const double bin_width[] = {0.8, 0.0003};
00126     const double ra[] = {10, 80};
00127     const double dec[] = {-8, 0};
00128 
00129     cpl_table *cat_flux = NULL;
00130     char *ref_name = NULL;
00131 
00132     int i;
00133 
00134     /* Create flux table */
00135     flux_table = cpl_table_new(nrow);
00136 
00137     cpl_table_new_column(flux_table, "RA_DEG", CPL_TYPE_DOUBLE);
00138     cpl_table_new_column(flux_table, "DEC_DEG", CPL_TYPE_DOUBLE);
00139 
00140     cpl_table_new_column(flux_table, "OBJECT", CPL_TYPE_STRING);
00141     cpl_table_new_column(flux_table, "TYPE", CPL_TYPE_STRING);
00142 
00143     cpl_table_new_column(flux_table, "NDATA", CPL_TYPE_INT); /* depth of arrays */
00144 
00145     cpl_table_new_column_array(flux_table, "LAMBDA", CPL_TYPE_FLOAT, depth);
00146     cpl_table_new_column_array(flux_table, "BIN_WIDTH", CPL_TYPE_FLOAT, depth);
00147     cpl_table_new_column_array(flux_table, "F_LAMBDA", CPL_TYPE_FLOAT, depth);
00148 
00149     values = cpl_array_new(depth, CPL_TYPE_FLOAT);
00150 
00151     for (i = 0; i < nrow; i++)
00152     {
00153         cpl_table_set_double(flux_table, "RA_DEG", i, ra[i]); 
00154         cpl_table_set_double(flux_table, "DEC_DEG", i, dec[i]);
00155         cpl_table_set_string(flux_table, "OBJECT", i, object[i]);
00156         cpl_table_set_string(flux_table, "TYPE", i, type[i]);
00157         cpl_table_set_int   (flux_table, "NDATA", i, depth);
00158 
00159         cpl_array_fill_window_float(values, 0, depth, lambda[i]); 
00160         cpl_table_set_array(flux_table, "LAMBDA", i, values);
00161 
00162         cpl_array_fill_window_float(values, 0, depth, bin_width[i]); 
00163         cpl_table_set_array(flux_table, "BIN_WIDTH", i, values);
00164 
00165         cpl_array_fill_window_float(values, 0, depth, fluxes[i]); 
00166         cpl_table_set_array(flux_table, "F_LAMBDA", i, values);
00167     }
00168 
00169     /* Create header */
00170     raw_header = uves_propertylist_new();
00171 
00172     /* Set RA, DEC to match each row of flux table, and verify that
00173        we got the corresponding std star */
00174     for (i = 0; i < nrow; i++)
00175     {
00176         double paccuracy = 60; /* arcsecs */
00177         double residual = 30;
00178         
00179         uves_pfits_set_ra (raw_header, ra[i]+residual/3600);
00180         uves_pfits_set_dec(raw_header, dec[i]-residual/3600);
00181         
00182         uves_free_table(&cat_flux);
00183         cpl_free(ref_name); ref_name = NULL;
00184         check_nomsg( cat_flux = uves_align(raw_header,
00185                            flux_table,
00186                            paccuracy,
00187                            &ref_name));
00188 
00189         /* Saving table string values to FITS might add trailing blanks;
00190            replace with end-of-string */
00191         while(ref_name[strlen(ref_name)-1] == ' ')
00192         {
00193             /* O(n^2) but that is okay */
00194             ref_name[strlen(ref_name)-1] = '\0';
00195         }
00196 
00197         /* cpl_table_dump(cat_flux, 0, cpl_table_get_nrow(cat_flux), stdout); */
00198         
00199         assure( cpl_table_get_nrow(cat_flux) == depth, CPL_ERROR_ILLEGAL_OUTPUT,
00200             "Flux table has %d rows, %d expected",
00201             cpl_table_get_nrow(cat_flux), depth);
00202         
00203         assure( strcmp(ref_name, object[i]) == 0, CPL_ERROR_ILLEGAL_OUTPUT,
00204             "Found '%s'; Expected '%s'", ref_name, object[i]);
00205         
00206 #if 0
00207         assure( float_equal(cpl_table_get_column_mean(cat_flux, "LAMBDA"), 
00208                 lambda[i], 0.001),
00209             CPL_ERROR_ILLEGAL_OUTPUT,
00210             "LAMBDA = %f; Expected = %f", 
00211             cpl_table_get_column_mean(cat_flux, "LAMBDA"), lambda[i]);
00212         
00213         assure( float_equal(cpl_table_get_column_mean(cat_flux, "BIN_WIDTH"), 
00214                 bin_width[i], 0.001),
00215             CPL_ERROR_ILLEGAL_OUTPUT,
00216             "BIN_WIDTH = %f; Expected = %f", 
00217             cpl_table_get_column_mean(cat_flux, "BIN_WIDTH"), bin_width[i]);
00218         
00219         assure( float_equal(cpl_table_get_column_mean(cat_flux, "F_LAMBDA"), 
00220                 fluxes[i], 0.001),
00221             CPL_ERROR_ILLEGAL_OUTPUT,
00222             "F_LAMBDA = %f; Expected = %f", 
00223             cpl_table_get_column_mean(cat_flux, "F_LAMBDA"), fluxes[i]);
00224 #endif
00225         irplib_test_rel(cpl_table_get_column_mean(cat_flux, "LAMBDA"), 
00226                             lambda[i], 0.001);
00227         
00228         irplib_test_rel(cpl_table_get_column_mean(cat_flux, "BIN_WIDTH"), 
00229                             bin_width[i], 0.001);
00230         
00231         irplib_test_rel(cpl_table_get_column_mean(cat_flux, "F_LAMBDA"),
00232                             fluxes[i], 0.001);
00233     }
00234     
00235   cleanup:
00236     uves_free_table(&flux_table);
00237     uves_free_table(&cat_flux);
00238     uves_free_array(&values);
00239     uves_free_propertylist(&raw_header);
00240     cpl_free(ref_name);
00241 
00242     
00243     return;
00244 }
00245 
00246 /*----------------------------------------------------------------------------*/
00250 /*----------------------------------------------------------------------------*/
00251 
00252 int main(void)
00253 {
00254     IRPLIB_TEST_INIT;
00255 
00256     check_nomsg( test_3dtable() );
00257 
00258   cleanup:
00259     IRPLIB_TEST_END;
00260 }
00261 

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