irplib_detmon-test.c

00001 /*                                                                              *
00002  *   This file is part of the ESO IRPLIB package                                *
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 #ifdef HAVE_CONFIG_H
00021 #include <config.h>
00022 #endif
00023 
00024 /*----------------------------------------------------------------------------
00025                                 Includes
00026  ----------------------------------------------------------------------------*/
00027 
00028 #include <irplib_test.h>
00029 
00030 #include <irplib_detmon.h>
00031 
00032 #include <irplib_math_const.h>
00033 
00034 #ifdef HAVE_FFTW
00035 #include <complex.h>
00036 #include <fftw3.h>
00037 #endif
00038 
00039 #include <math.h>
00040 /*---------------------------------------------------------------------------*/
00041 /*
00042  * @defgroup irplib_detmon_test   Testing of the IRPLIB utilities
00043  */
00044 /*---------------------------------------------------------------------------*/
00045 
00046 #define PI 3.141592653
00047 /*----------------------------------------------------------------------------
00048                                 Defines
00049  ----------------------------------------------------------------------------*/
00050 
00051 #ifndef IMAGESIZE
00052 #define IMAGESIZE       64
00053 #endif
00054 
00055 #define IMAGELISTSIZE   10
00056 
00057 #define NBADPIXS        10
00058 
00059 #define REGEXP                            "^(ARCFILE|MJD-OBS|ESO TPL ID|" \
00060                                           "DATE-OBS|ESO DET DIT|ESO DET NDIT|"\
00061                                           "ESO DET NCORRS|"\
00062                                           "ESO DET MODE NAME)$"
00063 
00064 /*----------------------------------------------------------------------------
00065                             Private Function prototypes
00066  ----------------------------------------------------------------------------*/
00067 
00068 static void irplib_detmon_tests(void);
00069 
00070 
00071 cpl_frameset * irplib_detmon_pernoise_tests_fill_frameset(cpl_boolean,
00072                                                           int);
00073 
00074 cpl_frameset * irplib_detmon_tests_fill_frameset(const char     * tag_on,
00075                                                  const char     * tag_off,
00076                                                  int              non,
00077                                                  cpl_boolean      opt_nir,
00078                                                  cpl_boolean      with_exts,
00079                                                  int              nexts);
00080 
00081 static int
00082 irplib_detmon_clean_fits(void);
00083 
00084 
00085 /*---------------------------------------------------------------------------*/
00086 /*
00087  * @brief   Unit tests of fit module
00088  */
00089 /*---------------------------------------------------------------------------*/
00090 
00091 int main(void)
00092 {
00093     /* Initialize CPL + IRPLIB */
00094     IRPLIB_TEST_INIT;
00095 
00096     irplib_detmon_tests();
00097 
00098     IRPLIB_TEST_END;
00099 }
00100 
00101 static void irplib_detmon_tests(void)
00102 {
00103 
00104     cpl_parameterlist   * parlist;
00105     cpl_frameset        * frameset;
00106     const char * tag = "RAW";
00107     cpl_error_code error;
00108 
00109     /* HIGH LEVEL FUNCTION FOR RON/BIAS RECIPE */
00110 
00111     frameset = irplib_detmon_tests_fill_frameset(tag, tag, 20, TRUE, FALSE, 0);
00112 
00113     parlist = cpl_parameterlist_new();
00114     irplib_detmon_fill_ronbias_params(parlist, "detmon_ronbias", "detmon");
00115 
00116     /*
00117     irplib_detmon_ronbias(frameset, parlist, tag, "detmon_ronbias", "detmon",
00118                         "PRO-1.15", NULL, NULL, "PRO-1.15", "DETMON / 1.0", NULL);
00119 //Move the NULLs away when X and Y structure built
00120 
00121     irplib_test(cpl_error_get_code() == CPL_ERROR_NONE);
00122     cpl_error_reset();
00123     */
00124     cpl_parameterlist_delete(parlist);
00125     cpl_frameset_delete(frameset);
00126 
00127 #ifdef HAVE_FFTW
00128     /* Tests for Periodic Noise Characterisation */
00129 
00130 //    cpl_msg_error(cpl_func, "Starting PNC test");
00131 
00132     frameset = irplib_detmon_pernoise_tests_fill_frameset(0,0);
00133 
00134     parlist = cpl_parameterlist_new();
00135     irplib_detmon_fill_pernoise_params_default(parlist,
00136                                                "detmon_pernoise", "detmon");
00137 
00138     irplib_detmon_pernoise(frameset, parlist, "RAW", "detmon_pernoise", "detmon", "PRO-1.15", "DETMON / 1.0", NULL);
00139 
00140     cpl_parameterlist_delete(parlist);
00141     cpl_frameset_delete(frameset);
00142 
00143 #endif
00144 
00145     /* Test for DARK recipe */
00146 
00147     frameset = irplib_detmon_tests_fill_frameset("DARK", "DARK", 20,FALSE, FALSE, 0);
00148     parlist = cpl_parameterlist_new();
00149     error = irplib_detmon_fill_dark_params_default(parlist,
00150                                                "detmon_dark", "detmon");
00151     irplib_test(!error);
00152 
00153     irplib_detmon_dark(frameset, parlist, "DARK", "detmon_dark", "detmon",
00154                        "MASTER", "DSNU", "DSNU_TABLE", "DETMON / 1.0", NULL);
00155 
00156     cpl_parameterlist_delete(parlist);
00157     cpl_frameset_delete(frameset);
00158 
00159     irplib_detmon_clean_fits();
00160 
00161 }
00162 
00163 
00164 cpl_frameset * irplib_detmon_pernoise_tests_fill_frameset(cpl_boolean with_exts,
00165                                                           int nexts)
00166 {
00167 
00168     cpl_frameset                * frameset = cpl_frameset_new();
00169     cpl_frame                   * frame;
00170     cpl_propertylist            * plist;
00171 
00172     /* Create dummy image(s) */
00173 
00174     cpl_image * raw1 = cpl_image_new(512, 512, CPL_TYPE_FLOAT);
00175     cpl_image * raw2 = cpl_image_new(512, 512, CPL_TYPE_FLOAT);
00176     int i, j, k = 0;
00177 
00178     cpl_image_fill_noise_uniform(raw1, -2, 2);
00179 
00180     cpl_image_add_scalar(raw1, 100);
00181 
00182 
00183     for(i = 1; i <= 512; i++){
00184         for(j = 1; j <= 512; j++){
00185             int rejected;
00186             double value = cpl_image_get(raw1, j, i, &rejected);
00187             cpl_image_set(raw1, j, i, 4*sin((double)i/16*IRPLIB_MATH_2PI) + value); 
00188             if(rejected < 0) {
00189                cpl_msg_error(cpl_func, "Error adding test noise");
00190                k++;
00191             } 
00192         }
00193     }
00194 
00195     cpl_image_fill_noise_uniform(raw2, -3, 3);
00196 
00197 //    cpl_image_add(raw2, raw1);
00198 
00199     frame = cpl_frame_new();
00200     cpl_frame_set_filename(frame, "pertest1.fits");
00201     cpl_frame_set_tag(frame, "RAW");
00202     cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE);
00203     cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW);
00204 
00205     cpl_frameset_insert(frameset, frame);
00206 
00207     plist = cpl_propertylist_new();
00208 
00209     cpl_propertylist_append_double(plist, "ESO DET DIT", (double)(3));
00210     cpl_propertylist_append_double(plist, "EXPTIME", (double)(3));
00211     cpl_propertylist_append_double(plist, "MJD-OBS", 0);
00212 
00213 
00214     if(with_exts == TRUE) {
00215         cpl_image_save(NULL, "pertest1.fits", CPL_BPP_IEEE_FLOAT, plist, CPL_IO_DEFAULT);
00216         for(j = 0; j < nexts; j++) {
00217             cpl_image_save(raw1, "pertest1.fits", CPL_BPP_IEEE_FLOAT, plist, CPL_IO_EXTEND);
00218         }
00219     } else {
00220         cpl_image_save(raw1, "pertest1.fits", CPL_BPP_IEEE_FLOAT, plist, CPL_IO_DEFAULT);
00221     }
00222 
00223 
00224     frame = cpl_frame_new();
00225     cpl_frame_set_filename(frame, "pertest2.fits");
00226     cpl_frame_set_tag(frame, "RAW");
00227     cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE);
00228     cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW);
00229 
00230     cpl_frameset_insert(frameset, frame);
00231 
00232     if(with_exts == TRUE) {
00233         cpl_image_save(NULL, "pertest2.fits", CPL_BPP_IEEE_FLOAT, plist, CPL_IO_DEFAULT);
00234         for(j = 0; j < nexts; j++) {
00235             cpl_image_save(raw2, "pertest2.fits", CPL_BPP_IEEE_FLOAT, plist, CPL_IO_EXTEND);
00236         }
00237     } else {
00238         cpl_image_save(raw2, "pertest2.fits", CPL_BPP_IEEE_FLOAT, plist, CPL_IO_DEFAULT);
00239     }
00240     cpl_propertylist_delete(plist);
00241     cpl_image_delete(raw2);
00242     cpl_image_delete(raw1);
00243             
00244     return frameset;
00245 }
00246 
00247 static int
00248 irplib_detmon_clean_fits(void)
00249 {
00250     int i;
00251     for (i=0; i<=20; i++) {
00252         char * frame_name = cpl_sprintf("dummyon%d.fits",i);
00253         remove(frame_name);
00254         frame_name = cpl_sprintf("dummyoff%d.fits",i);
00255         remove(frame_name);
00256     }
00257     for (i=0; i<12; i++) {
00258         char * frame_name = cpl_sprintf("detmon_dark_dsnu_map_dit_%d.fits",i);
00259         remove(frame_name);
00260         frame_name = cpl_sprintf("detmon_dark_master_dit_%d.fits",i);
00261         remove(frame_name);
00262     }
00263     remove("detmon_dark_dsnu_table.fits");
00264     remove("detmon_dark.paf");
00265     for (i=1; i<=8; i++) {
00266         char * frame_name = cpl_sprintf("detmon_ir_lg_ext%02d.paf",i);
00267         remove(frame_name);
00268     }
00269     remove("detmon_ir_lg_bpm.fits");
00270     remove("detmon_ir_lg_gain_table.fits");
00271     remove("detmon_ir_lg_lin_coeff_0.fits");
00272     remove("detmon_ir_lg_lin_coeff_1.fits");
00273     remove("detmon_ir_lg_lin_coeff_2.fits");
00274     remove("detmon_ir_lg_lin_coeff_3.fits");
00275     remove("detmon_ir_lg_linearity_table.fits");
00276     remove("detmon_ir_lg.paf");
00277     remove("detmon_opt_lg_gain_table.fits");
00278     remove("detmon_opt_lg_linearity_table.fits");
00279     remove("detmon_opt_lg.paf");
00280 
00281     return 0;
00282 }
00283 /*---------------------------------------------------------------------------*/
00284 /*
00285  * @brief  Detect bad pixels of a selected type
00286  * @param  in           Input image
00287  * @param  mean         Mean level of the image - used to compute threshold
00288  * @param  sigma        Stdev level of the image - used to compute threshold
00289  * @param  flag         Flag - represents selected type of bad pixels (see enum)
00290  * @return The detected bad pixels in a cpl_mask.
00291  */
00292 /*---------------------------------------------------------------------------*/
00293 
00294 cpl_frameset * irplib_detmon_tests_fill_frameset(const char     * tag_on,
00295                                                  const char     * tag_off,
00296                                                  int              non,
00297                                                  cpl_boolean      opt_nir,
00298                                                  cpl_boolean      with_exts,
00299                                                  int              nexts)
00300 {
00301     cpl_frameset                * frameset;
00302     cpl_frame                   * frame;
00303     int i;
00304     char                        frame_name[20];
00305     cpl_propertylist            * plist;
00306     cpl_image                   * image;
00307     frameset = cpl_frameset_new();
00308 
00309     for (i=0; i<non; i++) {
00310         frame = cpl_frame_new();
00311         sprintf(frame_name, "dummyon%d.fits",i);
00312         cpl_frame_set_filename(frame, frame_name);
00313         cpl_frame_set_tag(frame, tag_on);
00314         cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE);
00315         cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW);
00316 
00317         cpl_frameset_insert(frameset, frame);
00318 
00319         plist = cpl_propertylist_new();
00320         if(opt_nir == TRUE) {
00321             cpl_propertylist_append_double(plist, "ESO DET DIT", (double)(i/2+1));
00322             cpl_propertylist_append_double(plist, "EXPTIME", (double)(i/2+1));
00323             cpl_propertylist_append_double(plist, "MJD-OBS", 0);
00324         } else {
00325             cpl_propertylist_append_double(plist, "ESO DET DIT", (double)((i+1)/2));
00326             cpl_propertylist_append_double(plist, "EXPTIME", (double)((i+1)/2));
00327             cpl_propertylist_append_double(plist, "MJD-OBS", 0);
00328         }
00329 
00330         if(with_exts == TRUE) {
00331           int j;
00332           cpl_image_save(NULL, frame_name, CPL_BPP_IEEE_FLOAT, plist, CPL_IO_DEFAULT);
00333           for(j = 0; j < nexts; j++) {
00334             image = cpl_image_new(IMAGESIZE, IMAGESIZE, CPL_TYPE_FLOAT);
00335             cpl_image_fill_noise_uniform(image, 25, 50);
00336             cpl_ensure(cpl_error_get_code()== CPL_ERROR_NONE, CPL_ERROR_NULL_INPUT, NULL);
00337 
00338             cpl_image_save(image, frame_name, CPL_BPP_IEEE_FLOAT, plist, CPL_IO_EXTEND);
00339             cpl_image_delete(image);
00340           }
00341         } else {
00342             image = cpl_image_new(IMAGESIZE, IMAGESIZE, CPL_TYPE_FLOAT);
00343             cpl_image_fill_noise_uniform(image, 25, 50);
00344             cpl_ensure(cpl_error_get_code()== CPL_ERROR_NONE, CPL_ERROR_NULL_INPUT, NULL);
00345 
00346             cpl_image_save(image, frame_name, CPL_BPP_IEEE_FLOAT, plist, CPL_IO_DEFAULT);
00347 
00348 
00349             cpl_image_delete(image);
00350         }
00351         cpl_propertylist_delete(plist);
00352 
00353 
00354         frame = cpl_frame_new();
00355         sprintf(frame_name, "dummyoff%d.fits",i);
00356         cpl_frame_set_filename(frame, frame_name);
00357         cpl_frame_set_tag(frame, tag_off);
00358         cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE);
00359         cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW);
00360 
00361         cpl_frameset_insert(frameset, frame);
00362 
00363         plist = cpl_propertylist_new();
00364         if(opt_nir == TRUE) {
00365             cpl_propertylist_append_double(plist, "ESO DET DIT", (double)(i/2+1));
00366             cpl_propertylist_append_double(plist, "EXPTIME", (double)(i/2+1));
00367             cpl_propertylist_append_double(plist, "MJD-OBS", 0);
00368         } else {
00369             cpl_propertylist_append_double(plist, "ESO DET DIT", (double)((i+1)/2));
00370             cpl_propertylist_append_double(plist, "EXPTIME", (double)((i+1)/2));
00371             cpl_propertylist_append_double(plist, "MJD-OBS", 0);
00372         }
00373 
00374         if(with_exts == TRUE) {
00375           int j;
00376           cpl_image_save(NULL, frame_name, CPL_BPP_IEEE_FLOAT, plist, CPL_IO_DEFAULT);
00377           for(j = 0; j < nexts; j++) {
00378             image = cpl_image_new(IMAGESIZE, IMAGESIZE, CPL_TYPE_FLOAT);
00379             cpl_image_fill_noise_uniform(image, 25, 50);
00380             cpl_ensure(cpl_error_get_code()== CPL_ERROR_NONE, CPL_ERROR_NULL_INPUT, NULL);
00381 
00382             cpl_image_save(image, frame_name, CPL_BPP_IEEE_FLOAT, plist, CPL_IO_EXTEND);
00383             cpl_image_delete(image);
00384           }
00385         } else {
00386             image = cpl_image_new(IMAGESIZE, IMAGESIZE, CPL_TYPE_FLOAT);
00387             cpl_image_fill_noise_uniform(image, 25, 50);
00388             cpl_ensure(cpl_error_get_code()== CPL_ERROR_NONE, CPL_ERROR_NULL_INPUT, NULL);
00389 
00390             cpl_image_save(image, frame_name, CPL_BPP_IEEE_FLOAT, plist, CPL_IO_DEFAULT);
00391 
00392 
00393             cpl_image_delete(image);
00394         }
00395         cpl_propertylist_delete(plist);
00396     }
00397 
00398     return frameset;
00399 }

Generated on Thu Nov 15 14:32:25 2007 for UVES Pipeline Reference Manual by  doxygen 1.5.1