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: amodigli $
00022  * $Date: 2010/02/08 07:11:39 $
00023  * $Revision: 1.7 $
00024  * $Name: uves-4_9_1 $
00025  * $Log: uves_response-test.c,v $
00026  * Revision 1.7  2010/02/08 07:11:39  amodigli
00027  * added test_load_3dtable
00028  *
00029  * Revision 1.6  2009/06/05 05:49:02  amodigli
00030  * updated init/end to cpl5
00031  *
00032  * Revision 1.5  2008/09/29 07:02:05  amodigli
00033  * add #include <string.h>
00034  *
00035  * Revision 1.4  2007/05/23 06:43:23  jmlarsen
00036  * Removed unused variables
00037  *
00038  * Revision 1.3  2007/04/24 12:50:29  jmlarsen
00039  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00040  *
00041  * Revision 1.2  2007/03/20 07:28:02  jmlarsen
00042  * Test TYPE = 'NULL'
00043  *
00044  * Revision 1.1  2007/03/15 12:27:18  jmlarsen
00045  * Moved unit tests to ./uves/tests and ./flames/tests
00046  *
00047  * Revision 1.2  2007/02/27 14:04:14  jmlarsen
00048  * Move unit test infrastructure to IRPLIB
00049  *
00050  * Revision 1.1  2007/02/21 12:38:26  jmlarsen
00051  * Renamed _test -> -test
00052  *
00053  * Revision 1.22  2007/01/29 12:17:54  jmlarsen
00054  * Support setting verbosity from command line
00055  *
00056  * Revision 1.21  2006/11/24 09:39:35  jmlarsen
00057  * Factored out termination code
00058  *
00059  * Revision 1.20  2006/11/16 09:49:25  jmlarsen
00060  * Fixed doxygen bug
00061  *
00062  * Revision 1.19  2006/11/08 14:03:59  jmlarsen
00063  * Doxybugfix
00064  *
00065  * Revision 1.18  2006/11/07 13:59:19  jmlarsen
00066  * Removed memory leaks
00067  *
00068  * Revision 1.17  2006/11/06 15:31:52  jmlarsen
00069  * Added check for memory leak
00070  *
00071  * Revision 1.16  2006/11/06 15:30:54  jmlarsen
00072  * Added missing includes
00073  *
00074  * Revision 1.15  2006/11/06 15:19:41  jmlarsen
00075  * Removed unused include directives
00076  *
00077  * Revision 1.14  2006/11/03 15:15:46  jmlarsen
00078  * Added test of uves_align
00079  *
00080  * Revision 1.16  2006/09/11 13:59:01  jmlarsen
00081  * Renamed identifier reserved by POSIX
00082  *
00083  */
00084 
00085 /*-----------------------------------------------------------------------------
00086                                 Includes
00087  -----------------------------------------------------------------------------*/
00088 
00089 #ifdef HAVE_CONFIG_H
00090 #  include <config.h>
00091 #endif
00092 
00093 #include <uves_response_utils.h>
00094 #include <uves_utils_wrappers.h>
00095 #include <uves_pfits.h>
00096 #include <uves_error.h>
00097 #include <uves_msg.h>
00098 
00099 #include <cpl_test.h>
00100 #include <cpl.h>
00101 #include <string.h>
00102 /*-----------------------------------------------------------------------------
00103                                 Defines
00104  -----------------------------------------------------------------------------*/
00105 
00106 /*-----------------------------------------------------------------------------
00107                             Functions prototypes
00108  -----------------------------------------------------------------------------*/
00109 
00110 /*----------------------------------------------------------------------------*/
00114 /*----------------------------------------------------------------------------*/
00116 /*----------------------------------------------------------------------------*/
00120 /*----------------------------------------------------------------------------*/
00121 static void
00122 test_load_3dtable(void)
00123 {
00124    cpl_table *flux_table = NULL;
00125    const char* dir_name="/media/VERBATIM/data7/flames/flames_uves_demo/ref/";
00126    const char* tab_name="flxstd.fits";
00127    char res_name[80];
00128    char ref_name[25];
00129    char ref_type[25];
00130 
00131    char full_name[256];
00132    int nraws=0;
00133    int i=0;
00134    double ref_ra=0;
00135    double ref_dec=0;
00136    double dist=0;
00137    uves_propertylist* header=NULL;
00138    const char *columns[3] = {"LAMBDA", "BIN_WIDTH", "F_LAMBDA"};
00139    int ndata;                      /* Number of elements in column */
00140    int j=0;
00141    cpl_table* result=NULL;
00142    sprintf(full_name,"%s%s",dir_name,tab_name);
00143    uves_msg("full name: %s",full_name);
00144    flux_table=cpl_table_load(full_name,1,0);
00145    nraws=cpl_table_get_nrow(flux_table);
00146    header=uves_propertylist_load(full_name,0);
00147 
00148    uves_msg("std: name \t type \t ra \t dec");
00149    for(i=0;i<nraws; i++) {
00150 
00151      check( ref_ra  = cpl_table_get_double(flux_table, "RA_DEG", i, NULL),
00152         "Could not read catalogue star right ascension");
00153 
00154      check( ref_dec = cpl_table_get_double(flux_table, "DEC_DEG", i, NULL),
00155         "Could not read catalogue star declination");
00156 
00157      check( sprintf(ref_name, cpl_table_get_string(flux_table, "OBJECT", i)),
00158             "Could not read reference object name");
00159     
00160      check( sprintf(ref_type,cpl_table_get_string(flux_table, "TYPE", i)),
00161         "Could not read reference object type");
00162 
00163      uves_msg("std: %s \t %s \t %f \t %f",ref_name,ref_type,ref_ra,ref_dec);
00164 
00165      sprintf(res_name,"%s.fits",ref_name);
00166      check( ndata = cpl_table_get_int(flux_table, "NDATA", i, NULL),
00167         "Error reading length of flux array");
00168 
00169      result=cpl_table_new(ndata);
00170      for(j = 0; j < 3; j++)
00171        {
00172      const cpl_array *data;
00173      int indx;
00174         
00175      cpl_table_new_column(result, columns[j], CPL_TYPE_DOUBLE);
00176         
00177      data = cpl_table_get_array(flux_table, columns[j], i);
00178 
00179      /* Only the 'ndata' first elements of the array are used,
00180         and the array may be longer than this */
00181      uves_msg_debug("3d table array size = %d, ndata = %d",
00182             cpl_array_get_size(data), ndata);
00183 
00184      assure( cpl_array_get_size(data) >= ndata,
00185          CPL_ERROR_ILLEGAL_INPUT,
00186          "Flux table row %d: column '%s' depth (%d) "
00187          "is less than NDATA (%d)",
00188          i, columns[j], cpl_array_get_size(data), ndata);
00189 
00190      for (indx = 0; indx < ndata; indx++)
00191        {
00192          /* 3d columns are float */
00193          cpl_table_set_double(result, columns[j], indx, 
00194                   cpl_array_get_float(data, indx, NULL));
00195        }
00196        }
00197      
00198      cpl_table_save(result, header, NULL,res_name, CPL_IO_DEFAULT);
00199      uves_free_table(&result);
00200    }
00201   
00202 
00203   cleanup:
00204    uves_free_table(&flux_table);
00205    uves_free_propertylist(&header);
00206 
00207   return;
00208 }
00209 
00210 /*----------------------------------------------------------------------------*/
00214 /*----------------------------------------------------------------------------*/
00215 static void
00216 test_3dtable(void)
00217 {
00218     /* Create flux table + header */
00219     cpl_table *flux_table = NULL;
00220     uves_propertylist *raw_header = NULL;
00221     cpl_array *values = NULL;
00222     int depth = 10;
00223     int nrow = 2;
00224     const char *object[] = {"first std", "another standard star"};
00225     const char *type  [] = {"NULL", "type 2"};
00226     const double lambda[] = {8000, 2000};
00227     const double fluxes[] = {1000000, 200};
00228     const double bin_width[] = {0.8, 0.0003};
00229     const double ra[] = {10, 80};
00230     const double dec[] = {-8, 0};
00231 
00232     cpl_table *cat_flux = NULL;
00233     char *ref_name = NULL;
00234 
00235     int i;
00236 
00237     /* Create flux table */
00238     flux_table = cpl_table_new(nrow);
00239 
00240     cpl_table_new_column(flux_table, "RA_DEG", CPL_TYPE_DOUBLE);
00241     cpl_table_new_column(flux_table, "DEC_DEG", CPL_TYPE_DOUBLE);
00242 
00243     cpl_table_new_column(flux_table, "OBJECT", CPL_TYPE_STRING);
00244     cpl_table_new_column(flux_table, "TYPE", CPL_TYPE_STRING);
00245 
00246     cpl_table_new_column(flux_table, "NDATA", CPL_TYPE_INT); /* depth of arrays */
00247 
00248     cpl_table_new_column_array(flux_table, "LAMBDA", CPL_TYPE_FLOAT, depth);
00249     cpl_table_new_column_array(flux_table, "BIN_WIDTH", CPL_TYPE_FLOAT, depth);
00250     cpl_table_new_column_array(flux_table, "F_LAMBDA", CPL_TYPE_FLOAT, depth);
00251 
00252     values = cpl_array_new(depth, CPL_TYPE_FLOAT);
00253 
00254     for (i = 0; i < nrow; i++)
00255     {
00256         cpl_table_set_double(flux_table, "RA_DEG", i, ra[i]); 
00257         cpl_table_set_double(flux_table, "DEC_DEG", i, dec[i]);
00258         cpl_table_set_string(flux_table, "OBJECT", i, object[i]);
00259         cpl_table_set_string(flux_table, "TYPE", i, type[i]);
00260         cpl_table_set_int   (flux_table, "NDATA", i, depth);
00261 
00262         cpl_array_fill_window_float(values, 0, depth, lambda[i]); 
00263         cpl_table_set_array(flux_table, "LAMBDA", i, values);
00264 
00265         cpl_array_fill_window_float(values, 0, depth, bin_width[i]); 
00266         cpl_table_set_array(flux_table, "BIN_WIDTH", i, values);
00267 
00268         cpl_array_fill_window_float(values, 0, depth, fluxes[i]); 
00269         cpl_table_set_array(flux_table, "F_LAMBDA", i, values);
00270     }
00271 
00272     /* Create header */
00273     raw_header = uves_propertylist_new();
00274 
00275     /* Set RA, DEC to match each row of flux table, and verify that
00276        we got the corresponding std star */
00277     for (i = 0; i < nrow; i++)
00278     {
00279         double paccuracy = 60; /* arcsecs */
00280         double residual = 30;
00281         
00282         uves_pfits_set_ra (raw_header, ra[i]+residual/3600);
00283         uves_pfits_set_dec(raw_header, dec[i]-residual/3600);
00284         
00285         uves_free_table(&cat_flux);
00286         cpl_free(ref_name); ref_name = NULL;
00287         check_nomsg( cat_flux = uves_align(raw_header,
00288                            flux_table,
00289                            paccuracy,
00290                            &ref_name));
00291 
00292         /* Saving table string values to FITS might add trailing blanks;
00293            replace with end-of-string */
00294         while(ref_name[strlen(ref_name)-1] == ' ')
00295         {
00296             /* O(n^2) but that is okay */
00297             ref_name[strlen(ref_name)-1] = '\0';
00298         }
00299 
00300         /* cpl_table_dump(cat_flux, 0, cpl_table_get_nrow(cat_flux), stdout); */
00301         
00302         assure( cpl_table_get_nrow(cat_flux) == depth, CPL_ERROR_ILLEGAL_OUTPUT,
00303             "Flux table has %d rows, %d expected",
00304             cpl_table_get_nrow(cat_flux), depth);
00305         
00306         assure( strcmp(ref_name, object[i]) == 0, CPL_ERROR_ILLEGAL_OUTPUT,
00307             "Found '%s'; Expected '%s'", ref_name, object[i]);
00308         
00309 #if 0
00310         assure( float_equal(cpl_table_get_column_mean(cat_flux, "LAMBDA"), 
00311                 lambda[i], 0.001),
00312             CPL_ERROR_ILLEGAL_OUTPUT,
00313             "LAMBDA = %f; Expected = %f", 
00314             cpl_table_get_column_mean(cat_flux, "LAMBDA"), lambda[i]);
00315         
00316         assure( float_equal(cpl_table_get_column_mean(cat_flux, "BIN_WIDTH"), 
00317                 bin_width[i], 0.001),
00318             CPL_ERROR_ILLEGAL_OUTPUT,
00319             "BIN_WIDTH = %f; Expected = %f", 
00320             cpl_table_get_column_mean(cat_flux, "BIN_WIDTH"), bin_width[i]);
00321         
00322         assure( float_equal(cpl_table_get_column_mean(cat_flux, "F_LAMBDA"), 
00323                 fluxes[i], 0.001),
00324             CPL_ERROR_ILLEGAL_OUTPUT,
00325             "F_LAMBDA = %f; Expected = %f", 
00326             cpl_table_get_column_mean(cat_flux, "F_LAMBDA"), fluxes[i]);
00327 #endif
00328         cpl_test_rel(cpl_table_get_column_mean(cat_flux, "LAMBDA"), 
00329                             lambda[i], 0.001);
00330         
00331         cpl_test_rel(cpl_table_get_column_mean(cat_flux, "BIN_WIDTH"), 
00332                             bin_width[i], 0.001);
00333         
00334         cpl_test_rel(cpl_table_get_column_mean(cat_flux, "F_LAMBDA"),
00335                             fluxes[i], 0.001);
00336     }
00337     
00338   cleanup:
00339     uves_free_table(&flux_table);
00340     uves_free_table(&cat_flux);
00341     uves_free_array(&values);
00342     uves_free_propertylist(&raw_header);
00343     cpl_free(ref_name);
00344 
00345     
00346     return;
00347 }
00348 
00349 /*----------------------------------------------------------------------------*/
00353 /*----------------------------------------------------------------------------*/
00354 
00355 int main(void)
00356 {
00357     cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
00358 
00359     check_nomsg( test_3dtable() );
00360     //check_nomsg( test_load_3dtable() );
00361 
00362   cleanup:
00363     return cpl_test_end(0);
00364 }
00365 

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