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
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
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
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
00200
00201 crcount = 4;
00202
00203 xsh_msg( "Nb of Cosmics found: %d - Should be %d", crcount, totCr ) ;
00204
00205
00206 if ( crcount == totCr ) ret = 1 ;
00207 #else
00208 fname = cpl_frame_get_filename( medframe ) ;
00209 xsh_msg( " File name: %s", fname ) ;
00210 {
00211 int nbextensions = 0 ;
00212
00213 nbextensions = cpl_frame_get_nextensions(medframe );
00214 assure(nbextensions == 2, CPL_ERROR_ILLEGAL_INPUT,
00215 "Unrecognized format of file '%s'. No enough extensions. %d found.",
00216 fname, nbextensions);
00217 }
00218
00219
00220
00221 median = cpl_image_load( fname, CPL_TYPE_DOUBLE, 0, 0 ) ;
00222 assure( median != NULL, cpl_error_get_code(),
00223 "Cant load Median" ) ;
00224
00225
00226
00227
00228 bpmap = cpl_image_load( fname, CPL_TYPE_INT, 0, 2 );
00229 assure( bpmap != NULL, cpl_error_get_code(),
00230 "Cant load Bpmap" ) ;
00231 crcount = xsh_bpmap_count ( bpmap, imgSize, imgSize ) ;
00232 if ( Debug ) {
00233
00234 int ix , iy ;
00235 int rej ;
00236 for( ix = 1 ; ix<=imgSize ; ix++ )
00237 for ( iy = 1 ; iy <= imgSize ; iy++ ) {
00238 double value = cpl_image_get( median, ix, iy, &rej ) ;
00239 double bpval = cpl_image_get( bpmap, ix, iy, &rej ) ;
00240 if ( bpval != 0 )
00241 xsh_msg( " Median[%d,%d] = %lf [REJECTED]", ix, iy, value ) ;
00242 else
00243 xsh_msg( " Median[%d,%d] = %lf", ix, iy, value ) ;
00244 }
00245 }
00246
00247 xsh_msg( "Nb of Bad pixels in BpMap: %d", crcount ) ;
00248
00249 ret = 1 ;
00250 #endif
00251
00252 cleanup:
00253 xsh_pre_free( &pre) ;
00254
00255 return ret ;
00256 }
00257
00258 static void Help( void )
00259 {
00260 puts( "Unitary test of xsh_remove_crh_multiple" ) ;
00261 puts( "Usage: test_remove_crh [options]" ) ;
00262 puts( "Options" ) ;
00263 puts( " --nbcr=<nn> : Number of Cosmic Rays (default 1)" ) ;
00264 puts( " --nimg=<nn> : Number of dark images (default 4)" ) ;
00265 puts( " --size=<n> test-xsh_remove_crh_multiple.c : Nb of pixels along the 2 axes (default 10)" ) ;
00266 puts( " --bgmin=<f> : Minimum value for background (default 80)" ) ;
00267 puts( " --bgmax=<f> : Maximum value for background (default 90)" ) ;
00268 puts( " --sigma=<f> : Sigma value for clipping (default 2.5)" ) ;
00269 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
00270 puts( " --help : What you see" ) ;
00271 TEST_END();
00272 exit( 1 ) ;
00273 }
00274
00275 static void HandleOptions( int argc, char **argv )
00276 {
00277 int opt ;
00278 int option_index = 0;
00279
00280 while (( opt = getopt_long (argc, argv, "debug:help",
00281 long_options, &option_index)) != EOF )
00282 switch ( opt ) {
00283 case NBCR_OPT:
00284 sscanf( optarg, "%d", &nbCr ) ;
00285 break ;
00286 case NIMG_OPT:
00287 sscanf( optarg, "%d", &nbImages ) ;
00288 break ;
00289 case SIZE_OPT:
00290 sscanf( optarg, "%d", &imgSize ) ;
00291 break ;
00292 case BGMIN_OPT:
00293 sscanf( optarg, "%lf", &bgMin ) ;
00294 break ;
00295 case BGMAX_OPT:
00296 sscanf( optarg, "%lf", &bgMax ) ;
00297 break ;
00298 case SIGMA_OPT:
00299 sscanf( optarg, "%lf", &sigma ) ;
00300 break ;
00301 case DEBUG_OPT:
00302 if ( strcmp( optarg, "LOW")==0){
00303 xsh_debug_level_set( XSH_DEBUG_LEVEL_LOW);
00304 }
00305 else if ( strcmp( optarg, "HIGH")==0){
00306 xsh_debug_level_set( XSH_DEBUG_LEVEL_HIGH);
00307 }
00308 break;
00309 default:
00310 Help();
00311 }
00312
00313 }
00314
00325 int main( int argc, char **argv )
00326 {
00327
00328 int ret = 0 ;
00329 char sof_name[256];
00330 char crh_tag[64] ;
00331 cpl_frame * medFrame = NULL ;
00332 cpl_frameset *set = NULL;
00333 cpl_frameset *raws = NULL;
00334 cpl_frameset *calib = NULL;
00335 XSH_INSTRCONFIG * iconfig ;
00336 xsh_stack_param stack_param = {"median",5.,5.};
00337 const int decode_bp=2144337919;
00338
00339 TESTS_INIT(MODULE_ID);
00340 cpl_msg_set_level(CPL_MSG_DEBUG);
00341 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM) ;
00342
00343
00344 crh_clipping.sigma = sigma ;
00345 crh_clipping.niter = 2 ;
00346 crh_clipping.frac = 0.7 ;
00347 crh_clipping.res_max = 0.3 ;
00348
00349 HandleOptions( argc, argv);
00350
00351 if ( (argc-optind) >= 1 ) {
00352 sprintf(sof_name, "%s", argv[optind]);
00353
00354 xsh_msg("SOF name %s", sof_name);
00355
00356
00357 check( set = sof_to_frameset( sof_name));
00358
00359
00360 check( instrument = xsh_dfs_set_groups( set));
00361
00362 XSH_NEW_FRAMESET( raws);
00363 XSH_NEW_FRAMESET( calib);
00364
00365 check( xsh_dfs_split_in_group( set, raws, calib));
00366 }
00367 else{
00368
00369 instrument = xsh_instrument_new() ;
00370 xsh_instrument_set_mode( instrument, XSH_MODE_IFU ) ;
00371 xsh_instrument_set_arm( instrument, XSH_ARM_UVB ) ;
00372 xsh_instrument_set_lamp( instrument, XSH_LAMP_QTH ) ;
00373 iconfig = xsh_instrument_get_config( instrument ) ;
00374
00375
00376 check_msg( raws = createFakeFrames( iconfig ),
00377 "Error in createFakeFrames" ) ;
00378 }
00379 xsh_instrument_set_decode_bp( instrument, decode_bp ) ;
00380
00381 check_msg( xsh_prepare( raws, NULL, NULL,"CRH", instrument,0,CPL_FALSE),
00382 "Error in xsh_prepare" ) ;
00383
00384 strcpy( crh_tag, "test_remove_crh" ) ;
00385
00386 xsh_instrument_set_recipe_id( instrument, "xsh_mdark" ) ;
00387
00388 check(medFrame = xsh_remove_crh_multiple( raws,
00389 crh_tag,&stack_param,NULL,
00390 instrument,
00391 NULL,NULL,0 )) ;
00392 if ( (argc-optind) < 1 ) {
00393
00394 assure ( verifCr( medFrame ) == 1, CPL_ERROR_ILLEGAL_INPUT,
00395 "Verification failed" ) ;
00396 xsh_msg( "Remove CRH Multiple OK" ) ;
00397 }
00398
00399 cleanup:
00400
00401 xsh_instrument_free( &instrument);
00402 xsh_free_frame( &medFrame);
00403 xsh_free_frameset( &raws);
00404 xsh_free_frameset( &calib);
00405 xsh_free_frameset( &set);
00406
00407 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00408 xsh_error_dump(CPL_MSG_ERROR);
00409 ret = 1;
00410 }
00411 TEST_END();
00412 return ret ;
00413 }
00414