00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifdef HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028
00029
00035
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include <tests.h>
00049
00050 #include <xsh_data_pre.h>
00051 #include <xsh_error.h>
00052 #include <xsh_msg.h>
00053 #include <xsh_data_instrument.h>
00054 #include <xsh_drl.h>
00055 #include <xsh_pfits.h>
00056
00057 #include <xsh_badpixelmap.h>
00058
00059 #include <cpl.h>
00060 #include <math.h>
00061
00062 #include <getopt.h>
00063
00064
00065
00066
00067
00068 #define MODULE_ID "XSH_REMOVE_CRH_MULTIPLE"
00069
00070
00071 #define CR_PIX_VALUE 32000.
00072
00073
00074
00075 enum {
00076 NIMG_OPT, NBCR_OPT, DEBUG_OPT, SIZE_OPT, BGMIN_OPT, BGMAX_OPT,
00077 SIGMA_OPT, HELP_OPT
00078 } ;
00079
00080
00081
00082
00083
00084 static const char *Options = "?" ;
00085
00086 static struct option long_options[] = {
00087 {"nimg", required_argument, 0, NIMG_OPT},
00088 {"nbcr", required_argument, 0, NBCR_OPT},
00089 {"size", required_argument, 0, SIZE_OPT},
00090 {"bgmin", required_argument, 0, BGMIN_OPT},
00091 {"bgmax", required_argument, 0, BGMAX_OPT},
00092 {"sigma", required_argument, 0, SIGMA_OPT},
00093 {"debug", required_argument, 0, DEBUG_OPT},
00094 {"help", 0, 0, HELP_OPT},
00095 {0, 0, 0, 0}
00096 };
00097
00098 static xsh_instrument * instrument = NULL ;
00099 static xsh_clipping_param crh_clipping ;
00100 static int nbImages = 4 ;
00101 static int nbCr = 1 ;
00102 static int totCr = 0 ;
00103 static int Debug = 0 ;
00104 static int imgSize = 10 ;
00105 static double bgMin = 80. ;
00106 static double bgMax = 90. ;
00107 static double sigma = 4.0 ;
00108
00109 #define CR_PLACE_X 5
00110 #define CR_PLACE_Y 5
00111
00112
00113
00114
00115 static void Help( void ) ;
00116 static void HandleOptions( int argc, char **argv ) ;
00117 static cpl_frameset* createFakeFrames( XSH_INSTRCONFIG *iconfig ) ;
00118
00119
00120
00121
00122
00123 static cpl_frameset* createFakeFrames( XSH_INSTRCONFIG *iconfig )
00124 {
00125 int i ;
00126 cpl_propertylist * header = NULL ;
00127 cpl_frameset* result = NULL;
00128
00129
00130 result = cpl_frameset_new() ;
00131
00132
00133 header = mkHeader( iconfig, imgSize, imgSize, 1. ) ;
00134
00135
00136 for( i = 0 ; i<nbImages ; i++ ) {
00137 cpl_image * image = NULL;
00138 char iname[256];
00139
00140 int j ;
00141 int ix = CR_PLACE_X+i ;
00142 int iy = CR_PLACE_Y+i ;
00143
00144 cpl_frame * frame = cpl_frame_new() ;
00145
00146 image = cpl_image_new( imgSize, imgSize, CPL_TYPE_DOUBLE ) ;
00147
00148
00149 cpl_image_fill_noise_uniform ( image, bgMin, bgMax ) ;
00150
00151
00152 if ( ix >= imgSize ) ix = i ;
00153 if ( iy >= imgSize ) iy = i ;
00154 for ( j=0 ; j<nbCr ; j++ ) {
00155 cpl_image_set (image, ix, iy, CR_PIX_VALUE );
00156 xsh_msg( "CR at %d,%d [%d]", ix, iy, i ) ;
00157 totCr++ ;
00158 }
00159
00160 sprintf( iname, "test_crh_%02d.fits", i ) ;
00161 cpl_image_save(image, iname, CPL_BPP_IEEE_DOUBLE, header,
00162 CPL_IO_DEFAULT);
00163
00164
00165 cpl_frame_set_filename( frame, iname ) ;
00166 cpl_frame_set_tag( frame, "BIAS_UVB" ) ;
00167 cpl_frame_set_level( frame, CPL_FRAME_LEVEL_TEMPORARY);
00168 cpl_frame_set_group( frame, CPL_FRAME_GROUP_RAW ) ;
00169
00170 cpl_frameset_insert( result, frame ) ;
00171 xsh_free_image( &image);
00172 }
00173
00174 cleanup:
00175 xsh_free_propertylist( &header);
00176 return result;
00177 }
00178
00179 static int verifCr( cpl_frame *medframe )
00180 {
00181
00182 xsh_pre * pre ;
00183 int crcount = 0 ;
00184 int ret = 0 ;
00185
00186 xsh_msg( "Entering VerifCr function" ) ;
00187
00188
00189
00190
00191 assure( medframe != NULL, CPL_ERROR_ILLEGAL_INPUT,
00192 "medFrame is NULL in call to verifCr" ) ;
00193 #if 1
00194
00195 check_msg( pre = xsh_pre_load( medframe, instrument ),
00196 "Cant load medframe" ) ;
00197
00198
00199 crcount = xsh_pfits_get_qc_ncrh( pre->data_header ) ;
00200
00201 xsh_msg( "Nb of Cosmics found: %d - Should be %d", crcount, totCr ) ;
00202
00203
00204 if ( crcount == totCr ) ret = 1 ;
00205 #else
00206 fname = cpl_frame_get_filename( medframe ) ;
00207 xsh_msg( " File name: %s", fname ) ;
00208 {
00209 int nbextensions = 0 ;
00210
00211 nbextensions = cpl_frame_get_nextensions(medframe );
00212 assure(nbextensions == 2, CPL_ERROR_ILLEGAL_INPUT,
00213 "Unrecognized format of file '%s'. No enough extensions. %d found.",
00214 fname, nbextensions);
00215 }
00216
00217
00218
00219 median = cpl_image_load( fname, CPL_TYPE_DOUBLE, 0, 0 ) ;
00220 assure( median != NULL, cpl_error_get_code(),
00221 "Cant load Median" ) ;
00222
00223
00224
00225
00226 bpmap = cpl_image_load( fname, CPL_TYPE_INT, 0, 2 );
00227 assure( bpmap != NULL, cpl_error_get_code(),
00228 "Cant load Bpmap" ) ;
00229 crcount = xsh_bpmap_count ( bpmap, imgSize, imgSize ) ;
00230 if ( Debug ) {
00231
00232 int ix , iy ;
00233 int rej ;
00234 for( ix = 1 ; ix<=imgSize ; ix++ )
00235 for ( iy = 1 ; iy <= imgSize ; iy++ ) {
00236 double value = cpl_image_get( median, ix, iy, &rej ) ;
00237 double bpval = cpl_image_get( bpmap, ix, iy, &rej ) ;
00238 if ( bpval != 0 )
00239 xsh_msg( " Median[%d,%d] = %lf [REJECTED]", ix, iy, value ) ;
00240 else
00241 xsh_msg( " Median[%d,%d] = %lf", ix, iy, value ) ;
00242 }
00243 }
00244
00245 xsh_msg( "Nb of Bad pixels in BpMap: %d", crcount ) ;
00246
00247 ret = 1 ;
00248 #endif
00249
00250 cleanup:
00251 xsh_pre_free( &pre) ;
00252
00253 return ret ;
00254 }
00255
00256 static void Help( void )
00257 {
00258 puts( "Unitary test of xsh_remove_crh_multiple" ) ;
00259 puts( "Usage: test_remove_crh [options]" ) ;
00260 puts( "Options" ) ;
00261 puts( " --nbcr=<nn> : Number of Cosmic Rays (default 1)" ) ;
00262 puts( " --nimg=<nn> : Number of dark images (default 4)" ) ;
00263 puts( " --size=<n> : Nb of pixels along the 2 axes (default 10)" ) ;
00264 puts( " --bgmin=<f> : Minimum value for background (default 80)" ) ;
00265 puts( " --bgmax=<f> : Maximum value for background (default 90)" ) ;
00266 puts( " --sigma=<f> : Sigma value for clipping (default 2.5)" ) ;
00267 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
00268 puts( " --help : What you see" ) ;
00269 TEST_END();
00270 exit( 1 ) ;
00271 }
00272
00273 static void HandleOptions( int argc, char **argv )
00274 {
00275 int opt ;
00276 int option_index = 0;
00277
00278 while (( opt = getopt_long (argc, argv, "debug:help",
00279 long_options, &option_index)) != EOF )
00280 switch ( opt ) {
00281 case NBCR_OPT:
00282 sscanf( optarg, "%d", &nbCr ) ;
00283 break ;
00284 case NIMG_OPT:
00285 sscanf( optarg, "%d", &nbImages ) ;
00286 break ;
00287 case SIZE_OPT:
00288 sscanf( optarg, "%d", &imgSize ) ;
00289 break ;
00290 case BGMIN_OPT:
00291 sscanf( optarg, "%lf", &bgMin ) ;
00292 break ;
00293 case BGMAX_OPT:
00294 sscanf( optarg, "%lf", &bgMax ) ;
00295 break ;
00296 case SIGMA_OPT:
00297 sscanf( optarg, "%lf", &sigma ) ;
00298 break ;
00299 case DEBUG_OPT:
00300 if ( strcmp( optarg, "LOW")==0){
00301 xsh_debug_level_set( XSH_DEBUG_LEVEL_LOW);
00302 }
00303 else if ( strcmp( optarg, "HIGH")==0){
00304 xsh_debug_level_set( XSH_DEBUG_LEVEL_HIGH);
00305 }
00306 break;
00307 default:
00308 Help();
00309 }
00310
00311 }
00312
00323 int main( int argc, char **argv )
00324 {
00325
00326 int ret = 0 ;
00327 char sof_name[256];
00328 char crh_name[64] ;
00329 cpl_frame * medFrame = NULL ;
00330 cpl_frameset *set = NULL;
00331 cpl_frameset *raws = NULL;
00332 cpl_frameset *calib = NULL;
00333 XSH_INSTRCONFIG * iconfig ;
00334
00335
00336 TESTS_INIT(MODULE_ID);
00337 cpl_msg_set_level(CPL_MSG_DEBUG);
00338 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM) ;
00339
00340
00341 crh_clipping.sigma = sigma ;
00342 crh_clipping.niter = 2 ;
00343 crh_clipping.frac = 0.7 ;
00344 crh_clipping.res_max = 0.3 ;
00345
00346 HandleOptions( argc, argv);
00347
00348 if ( (argc-optind) >= 1 ) {
00349 sprintf(sof_name, "%s", argv[optind]);
00350
00351 xsh_msg("SOF name %s", sof_name);
00352
00353
00354 check( set = sof_to_frameset( sof_name));
00355
00356
00357 check( instrument = xsh_dfs_set_groups( set));
00358
00359 XSH_NEW_FRAMESET( raws);
00360 XSH_NEW_FRAMESET( calib);
00361
00362 check( xsh_dfs_split_in_group( set, raws, calib));
00363 }
00364 else{
00365
00366 instrument = xsh_instrument_new() ;
00367 xsh_instrument_set_mode( instrument, XSH_MODE_IFU ) ;
00368 xsh_instrument_set_arm( instrument, XSH_ARM_UVB ) ;
00369 xsh_instrument_set_lamp( instrument, XSH_LAMP_QTH ) ;
00370 iconfig = xsh_instrument_get_config( instrument ) ;
00371
00372
00373 check_msg( raws = createFakeFrames( iconfig ),
00374 "Error in createFakeFrames" ) ;
00375 }
00376
00377 check_msg( xsh_prepare( raws, NULL, NULL,"CRH", instrument,0),
00378 "Error in xsh_prepare" ) ;
00379
00380 strcpy( crh_name, "test_remove_crh.fits" ) ;
00381
00382 xsh_instrument_set_recipe_id( instrument, "xsh_mdark" ) ;
00383
00384 medFrame = xsh_remove_crh_multiple( raws,
00385 crh_name,
00386 &crh_clipping, instrument,
00387 NULL,NULL ) ;
00388 if ( (argc-optind) < 1 ) {
00389
00390 assure ( verifCr( medFrame ) == 1, CPL_ERROR_ILLEGAL_INPUT,
00391 "Verification failed" ) ;
00392 xsh_msg( "Remove CRH Multiple OK" ) ;
00393 }
00394
00395 cleanup:
00396
00397 xsh_instrument_free( &instrument);
00398 xsh_free_frame( &medFrame);
00399 xsh_free_frameset( &raws);
00400 xsh_free_frameset( &calib);
00401 xsh_free_frameset( &set);
00402
00403 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00404 xsh_error_dump(CPL_MSG_ERROR);
00405 ret = 1;
00406 }
00407 TEST_END();
00408 return ret ;
00409 }
00410