uves_dfs-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.11 $
00024  * $Name: uves-3_3_1 $
00025  * $Log: uves_dfs-test.c,v $
00026  * Revision 1.11  2007/05/23 06:43:23  jmlarsen
00027  * Removed unused variables
00028  *
00029  * Revision 1.10  2007/05/22 14:51:02  jmlarsen
00030  * Removed unused variables
00031  *
00032  * Revision 1.9  2007/04/24 12:50:29  jmlarsen
00033  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00034  *
00035  * Revision 1.8  2007/04/24 08:07:11  jmlarsen
00036  * Shortened line
00037  *
00038  * Revision 1.7  2007/04/24 08:04:29  jmlarsen
00039  * Added performance test
00040  *
00041  * Revision 1.6  2007/04/10 12:51:43  jmlarsen
00042  * Fixed wrong reference value
00043  *
00044  * Revision 1.5  2007/04/10 12:50:56  jmlarsen
00045  * Added check of float values
00046  *
00047  * Revision 1.4  2007/04/10 11:35:06  jmlarsen
00048  * Added error checking
00049  *
00050  * Revision 1.3  2007/04/03 11:02:30  jmlarsen
00051  * Support reading float MIDAS arrays
00052  *
00053  * Revision 1.2  2007/03/15 15:04:14  jmlarsen
00054  * Allow spaces in HISTORY keyword string values
00055  *
00056  * Revision 1.1  2007/03/15 12:27:18  jmlarsen
00057  * Moved unit tests to ./uves/tests and ./flames/tests
00058  *
00059  * Revision 1.6  2007/02/27 14:04:14  jmlarsen
00060  * Move unit test infrastructure to IRPLIB
00061  *
00062  * Revision 1.5  2007/01/31 15:11:09  jmlarsen
00063  * Test of inf+nan when saving FITS files
00064  *
00065  * Revision 1.4  2007/01/29 12:17:54  jmlarsen
00066  * Support setting verbosity from command line
00067  *
00068  * Revision 1.3  2006/11/28 08:26:23  jmlarsen
00069  * Changed message
00070  *
00071  * Revision 1.2  2006/11/24 09:39:35  jmlarsen
00072  * Factored out termination code
00073  *
00074  * Revision 1.1  2006/11/22 08:04:59  jmlarsen
00075  * Added uves_dfs unit test module
00076  *
00077  * Revision 1.20  2006/11/16 09:49:25  jmlarsen
00078  * Fixed doxygen bug
00079  *
00080  * Revision 1.19  2006/11/08 14:04:03  jmlarsen
00081  * Doxybugfix
00082  *
00083  * Revision 1.18  2006/11/06 15:30:54  jmlarsen
00084  * Added missing includes
00085  *
00086  * Revision 1.17  2006/11/03 15:02:06  jmlarsen
00087  * Added test of uves_align
00088  *
00089  * Revision 1.16  2006/09/11 13:59:01  jmlarsen
00090  * Renamed identifier reserved by POSIX
00091  *
00092  * Revision 1.15  2006/08/24 11:46:18  jmlarsen
00093  * Fixed typo
00094  *
00095  * Revision 1.14  2006/08/24 07:18:17  amodigli
00096  * fixed doxygen warnings
00097  *
00098  * Revision 1.13  2006/08/24 06:39:57  jmlarsen
00099  * Reduced maximum line length
00100  *
00101  * Revision 1.12  2006/08/17 14:11:25  jmlarsen
00102  * Use assure_mem macro to check for memory allocation failure
00103  *
00104  * Revision 1.11  2006/08/14 12:13:27  jmlarsen
00105  * Reset irplib error handler
00106  *
00107  * Revision 1.10  2006/08/14 07:45:41  amodigli
00108  * doxigen doc
00109  *
00110  * Revision 1.9  2006/03/03 13:54:11  jmlarsen
00111  * Changed syntax of check macro
00112  *
00113  * Revision 1.8  2006/02/03 07:47:53  jmlarsen
00114  * Moved recipe implementations to ./uves directory
00115  *
00116  * Revision 1.7  2006/01/31 08:26:56  jmlarsen
00117  * Disabled recipe run tests
00118  *
00119  * Revision 1.6  2006/01/25 16:14:14  jmlarsen
00120  * Changed interface of gauss.fitting routine
00121  *
00122  * Revision 1.5  2005/12/16 14:22:22  jmlarsen
00123  * Removed midas test data; Added sof files
00124  *
00125  * Revision 1.4  2005/11/18 10:54:43  jmlarsen
00126  * Minor changes
00127  *
00128  * Revision 1.3  2005/11/14 13:18:44  jmlarsen
00129  * Minor update
00130  *
00131  * Revision 1.2  2005/11/11 13:18:54  jmlarsen
00132  * Reorganized code, renamed source files
00133  *
00134  * Revision 1.1  2005/11/10 16:33:41  jmlarsen
00135  * Added weighted extraction, test of gauss. fit
00136  *
00137  */
00138 
00139 /*-----------------------------------------------------------------------------
00140                                 Includes
00141  -----------------------------------------------------------------------------*/
00142 
00143 #ifdef HAVE_CONFIG_H
00144 #  include <config.h>
00145 #endif
00146 
00147 #include <uves_dfs.h>
00148 #include <uves_parameters.h>
00149 #include <uves_utils_wrappers.h>
00150 #include <uves_test_simulate.h>
00151 #include <uves_pfits.h>
00152 #include <uves_error.h>
00153 
00154 #include <irplib_test.h>
00155 #include <cpl.h>
00156 
00157 #include <float.h>
00158 /*-----------------------------------------------------------------------------
00159                                 Defines
00160  -----------------------------------------------------------------------------*/
00161 
00162 /*-----------------------------------------------------------------------------
00163                             Functions prototypes
00164  -----------------------------------------------------------------------------*/
00165 
00166 
00167 /*----------------------------------------------------------------------------*/
00171 /*----------------------------------------------------------------------------*/
00174 /*----------------------------------------------------------------------------*/
00178 /*----------------------------------------------------------------------------*/
00179 static void
00180 test_load_linetable(void)
00181 {
00182     const char * const filename = "linetable.fits";
00183     cpl_table *linetable_in = NULL;
00184     polynomial *dispersion = NULL;
00185     polynomial *absorder = NULL;
00186     uves_propertylist *header = uves_propertylist_new();
00187     uves_propertylist *eheader = uves_propertylist_new();
00188     cpl_frame *f = cpl_frame_new();
00189     cpl_frameset *frames = cpl_frameset_new();
00190     bool flames = false;
00191     const char *const chip_id = "CCD42";
00192     polynomial *order_locations = NULL;
00193     enum uves_chip chip = UVES_CHIP_BLUE;
00194     int minorder = 1;
00195     int maxorder = 5;
00196     int nx = 150;
00197     /*int ny = 100;*/
00198     int trace_id = 1;
00199     int window = 1;
00200 
00201     /* output */
00202     const char *linetable_filename;
00203     cpl_table *linetable_out = NULL;
00204     //uves_propertylist *header_out = NULL;
00205 
00206     /* build data */
00207     check_nomsg( create_line_table(&linetable_in, &dispersion, &absorder,
00208                                    minorder, maxorder, nx));
00209     check_nomsg( uves_propertylist_append_string(header, UVES_CHIP_ID(chip), chip_id));
00210     check_nomsg( uves_propertylist_append_string(header, UVES_DRS_ID, "CPL"));
00211     check_nomsg( uves_pfits_set_traceid(eheader, trace_id) );
00212     check_nomsg( uves_pfits_set_windownumber(eheader, window) );
00213 
00214     check_nomsg( uves_table_save(linetable_in, header, eheader, filename, CPL_IO_DEFAULT) );
00215     check_nomsg( uves_save_polynomial(dispersion, filename, eheader) );
00216     check_nomsg( uves_save_polynomial(absorder, filename, eheader) );
00217 
00218     irplib_test_eq(cpl_error_get_code(), CPL_ERROR_NONE);
00219 
00220     cpl_frame_set_filename(f, filename);
00221     cpl_frame_set_tag(f, "LINE_TABLE_BLUE");
00222     cpl_frameset_insert(frames, f);
00223 
00224     check_nomsg( uves_load_linetable(frames,
00225                                      flames,
00226                                      chip_id,
00227                                      order_locations, minorder, maxorder,
00228                                      &linetable_filename,
00229                                      &linetable_out,
00230                                      NULL,
00231                                      NULL,
00232                                      NULL,
00233                                      chip, trace_id, window));
00234 
00235     irplib_test_eq(cpl_error_get_code(), CPL_ERROR_NONE);
00236 
00237   cleanup:
00238     uves_free_frameset(&frames);
00239     uves_free_table(&linetable_in);
00240     uves_free_table(&linetable_out);
00241     uves_polynomial_delete(&order_locations);
00242     uves_polynomial_delete(&dispersion);
00243     uves_polynomial_delete(&absorder);
00244     uves_free_propertylist(&header);
00245     uves_free_propertylist(&eheader);
00246     return;
00247 }
00248 
00249 
00250 /*----------------------------------------------------------------------------*/
00254 /*----------------------------------------------------------------------------*/
00255 static void
00256 convert_midas_array(void)
00257 {
00258     const char *values[] = {"HISTORY", "", 
00259                 "HISTORY", "'FIBREPOS','R*8',1,9,'3E23.15'",
00260                 "HISTORY", " -3.243571124678650E+01 -2.309646501161805E+01 -1.402902770375962E+01",
00261                 "HISTORY", " -4.772375924542811E+00  4.827040349175236E+00  1.378761244187003E+01",
00262                 "HISTORY", "  2.321337764943556E+01  3.243571124678650E+01 -3.552713678800501E-15",
00263                 "HISTORY", "",
00264                             "HISTORY", "'COEFFR','R*4',1,20,'5E14.7'",
00265                             "HISTORY", "9.4893160E+00 4.0716226E+03 0.0000000E+00 2.3000000E+01 1.8538159E-04",
00266                             "HISTORY", "0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00",
00267                 "HISTORY", "",
00268                 "HISTORY", "'INTVAL','I",
00269                 "HISTORY", "1 2 3 4 5 6",
00270                 "HISTORY", "7",
00271                 "HISTORY", "",
00272                 "HISTORY", "'LEGAL','C",
00273                 "HISTORY", " a sdfasdf",
00274                 "HISTORY", "",
00275                 "HISTORY", "'ILLEGAL','C",
00276                 "HISTORY", "1bsdf",
00277                 "HISTORY", "bsdf",
00278                 "HISTORY", "",
00279                 "HISTORY", "'CHIPCHOICE','C",
00280                 "HISTORY", "abcd",
00281                 "HISTORY", ""};
00282 
00283     int N = sizeof(values) / sizeof(const char *) / 2;
00284 
00285     uves_propertylist *header = NULL;
00286     double *resultd = NULL;
00287     int *resulti = NULL;
00288     float *resultf = NULL;
00289     const char *results = NULL;
00290     int result_length, i;
00291     int nkeys;
00292     cpl_type result_type;
00293 
00294     header = uves_propertylist_new();
00295     for (i = 0; i < N; i++)
00296     {
00297         uves_propertylist_append_string(header, values[i*2], values[i*2+1]);
00298     }
00299 
00300     check_nomsg(resultd = uves_read_midas_array(header, "FIBREPOS", &result_length,
00301                                                &result_type, &nkeys));
00302 
00303     irplib_test_eq(result_type, CPL_TYPE_DOUBLE);
00304     irplib_test_eq(result_length, 9);
00305     irplib_test_eq(nkeys, 5);
00306 
00307     /* Check numbers, see above */
00308     irplib_test_rel(resultd[0], -32, 0.10);
00309     irplib_test_rel(resultd[3], -4.7, 0.10);
00310     irplib_test_rel(resultd[6], 23, 0.10);
00311     irplib_test( fabs(resultd[8]) < 0.001);
00312 
00313     /* float */
00314     check_nomsg(resultf = uves_read_midas_array(header, "COEFFR", &result_length,
00315                                                 &result_type, &nkeys));
00316     irplib_test_eq(result_type, CPL_TYPE_FLOAT);
00317     irplib_test_eq(result_length, 10);
00318     irplib_test_eq(nkeys, 4);
00319 
00320     irplib_test_rel(resultf[0], 9.489, 0.01);
00321     irplib_test_rel(resultf[1], 4071,  0.01);
00322     irplib_test_abs(resultf[2], 0.000, 0.01);
00323     irplib_test_rel(resultf[3], 23.00, 0.01);
00324 
00325     /* integer */
00326     check_nomsg(resulti = uves_read_midas_array(header, "INTVAL", &result_length,
00327                                                &result_type, &nkeys));
00328 
00329     irplib_test_eq(result_type, CPL_TYPE_INT);
00330     irplib_test_eq(result_length, 7);
00331     irplib_test_eq(nkeys, 4);
00332     for (i = 1; i <= 7; i++)
00333         {
00334             irplib_test_eq(resulti[i-1], i);
00335         }
00336 
00337 
00338     /* string */
00339     check_nomsg( results = uves_read_midas_array(header, "LEGAL", &result_length,
00340                                        &result_type, &nkeys) );
00341 
00342     irplib_test_eq(result_type, CPL_TYPE_STRING);
00343     irplib_test_eq(result_length, 8);
00344     irplib_test_eq(nkeys, 3);
00345     irplib_test_eq_string(results, "asdfasdf");
00346 
00347     irplib_test(uves_read_midas_array(header, "ILLEGAL2", &result_length,
00348                                       &result_type, &nkeys) == NULL);
00349     uves_error_reset();
00350 
00351     uves_free_string_const(&results);
00352     check_nomsg(results = uves_read_midas_array(header, "CHIPCHOICE", &result_length,
00353                                                &result_type, &nkeys));
00354 
00355     irplib_test_eq(result_type, CPL_TYPE_STRING);
00356     irplib_test_eq(result_length, 4);
00357     irplib_test_eq(nkeys, 3);
00358     irplib_test_eq_string(results, "abcd");
00359 
00360     /* Performance test (relevant for long FLAMES FITS headers) */
00361     N = 9000;
00362     uves_free_propertylist(&header);
00363     header = uves_propertylist_new();
00364     uves_propertylist_append_string(header, "HISTORY", "'SELIDX','I*4',1,48389,'7I10'");
00365     for (i = 0; i < N; i++)
00366     {
00367         uves_propertylist_append_string(
00368                 header, "HISTORY", 
00369                 "  64605     64606     64607     64608     64609     64610     64611");
00370     }
00371     uves_propertylist_append_string(header, "HISTORY", "");
00372     
00373     uves_free_int(&resulti);
00374     check_nomsg( resulti = uves_read_midas_array(header, "SELIDX", &result_length,
00375                                                  &result_type, &nkeys));
00376 
00377     irplib_test_eq(result_type, CPL_TYPE_INT);
00378     irplib_test_eq(result_length, N*7);
00379     irplib_test_eq(nkeys, 1+N+1);
00380 
00381   cleanup:
00382     uves_free_propertylist(&header);
00383     uves_free_double(&resultd);
00384     uves_free_int(&resulti);
00385     uves_free_float(&resultf);
00386     uves_free_string_const(&results);
00387     return;
00388 }
00389    
00390 /*----------------------------------------------------------------------------*/
00394 /*----------------------------------------------------------------------------*/
00395 static void
00396 test_save_image(void)
00397 {
00398     const int N = 100;
00399     int i;
00400 
00401     cpl_image *image = cpl_image_new(N, 1, CPL_TYPE_DOUBLE);
00402 
00403     double inf = DBL_MAX;
00404     for (i = 1; i <= N; i++)
00405     {
00406         cpl_image_set(image, i, 1, -FLT_MAX*200);
00407         inf *= 10;
00408     }
00409 
00410 
00411     cpl_image_set(image, 1, 1, inf);
00412     cpl_image_set(image, 2, 1, inf/inf);
00413 
00414     uves_save_image(image, "dfs.fits", NULL);
00415     uves_free_image(&image);
00416 
00417     return;
00418 }
00419 
00420 /*----------------------------------------------------------------------------*/
00424 /*----------------------------------------------------------------------------*/
00425 static void
00426 test_save_frame(void)
00427 {
00428     cpl_frameset *frames = cpl_frameset_new();
00429     cpl_parameterlist *parameters = cpl_parameterlist_new();
00430     int nx = 1500;
00431     int ny = 1024;
00432     int nkey = 360;
00433     cpl_image *image = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
00434     uves_propertylist *raw_header = uves_propertylist_new();
00435     uves_propertylist *product_header = uves_propertylist_new();
00436     const char *starttime;
00437     const char *recipe_id = "uves_cal_phony";
00438     const char *tag = "PHONY_TAG";
00439     const char *raw_filename = "raw_file.fits";
00440 
00441     uves_define_global_parameters(parameters);
00442     irplib_test_eq( cpl_error_get_code(), CPL_ERROR_NONE );
00443 
00444     /* Create raw image */
00445     {
00446         cpl_image *raw_image = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
00447         cpl_frame *raw_frame = cpl_frame_new();
00448 
00449         {
00450             int i;
00451             for (i = 0; i < nkey; i++)
00452                 {
00453                     const char *key_name = uves_sprintf("KEY%d", i);
00454                     uves_propertylist_append_int(raw_header, key_name, i);
00455                     uves_free_string_const(&key_name);
00456                 }
00457             uves_propertylist_append_string(raw_header, "ORIGIN", "unknown...");
00458         }
00459 
00460         uves_image_save(raw_image, 
00461                        raw_filename,
00462                        CPL_BPP_IEEE_FLOAT,
00463                        raw_header,
00464                        CPL_IO_DEFAULT);
00465         irplib_test_eq( cpl_error_get_code(), CPL_ERROR_NONE );
00466         
00467         uves_free_image(&raw_image);
00468 
00469         /* Wrap frame */
00470         cpl_frame_set_tag(raw_frame, "BIAS_BLUE"); /* Use recognized tag,
00471                                                       so that FRAME_TYPE
00472                                                       is set to RAW */
00473         cpl_frame_set_filename(raw_frame, raw_filename);
00474         cpl_frameset_insert(frames, raw_frame);
00475     }
00476 
00477     starttime = uves_initialize(frames, parameters, recipe_id,
00478                                 "This recipe does not do anything");
00479     irplib_test_eq( cpl_error_get_code(), CPL_ERROR_NONE );
00480 
00481     uves_frameset_insert(frames, 
00482                          image, 
00483                          CPL_FRAME_GROUP_PRODUCT,
00484                          CPL_FRAME_TYPE_IMAGE,
00485                          CPL_FRAME_LEVEL_INTERMEDIATE,
00486                          "dfs_product.fits",
00487                          tag,
00488                          raw_header,
00489                          product_header,
00490                          NULL, /* table header */
00491                          parameters, 
00492                          recipe_id,
00493                          PACKAGE "/" PACKAGE_VERSION,
00494                          NULL, /* qc table */
00495                          starttime,
00496                          false,   /* dump PAF */
00497                          0   /* stats_mask */);
00498 
00499     irplib_test_eq( cpl_error_get_code(), CPL_ERROR_NONE );
00500     irplib_test( cpl_frameset_find(frames, tag) != NULL);
00501 
00502     uves_free_frameset(&frames);
00503     uves_free_parameterlist(&parameters);
00504     uves_free_image(&image);
00505     uves_free_propertylist(&raw_header);
00506     uves_free_propertylist(&product_header);
00507     uves_free_string_const(&starttime);
00508     return;
00509 }
00510 
00511 
00512 /*----------------------------------------------------------------------------*/
00516 /*----------------------------------------------------------------------------*/
00517 
00518 int main(void)
00519 {
00520     /* Initialize CPL + UVES messaging */
00521     IRPLIB_TEST_INIT;
00522 //    cpl_msg_set_level(CPL_MSG_DEBUG);
00523 
00524     check_nomsg( test_load_linetable() );
00525 
00526     check( convert_midas_array(),
00527        "Test of MIDAS array conversion failed");
00528 
00529     check_nomsg( test_save_image() );
00530 
00531     check_nomsg( test_save_frame() );
00532 
00533   cleanup:
00534     if (cpl_error_get_code() != CPL_ERROR_NONE)
00535         {
00536             uves_error_dump();
00537         }
00538     IRPLIB_TEST_END;
00539 }
00540 
00541 

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