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

Generated on 9 Mar 2012 for UVES Pipeline Reference Manual by  doxygen 1.6.1