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
00026 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030
00035
00038
00039
00040
00041
00042 #include <tests.h>
00043 #include <xsh_data_pre.h>
00044 #include <xsh_error.h>
00045 #include <xsh_msg.h>
00046 #include <xsh_data_instrument.h>
00047 #include <xsh_data_rec.h>
00048 #include <xsh_data_localization.h>
00049 #include <xsh_drl.h>
00050 #include <xsh_pfits.h>
00051
00052 #include <xsh_badpixelmap.h>
00053
00054 #include <cpl.h>
00055 #include <math.h>
00056
00057 #include <getopt.h>
00058
00059
00060
00061
00062
00063 #define MODULE_ID "XSH_OPT_EXTRACT"
00064
00065 enum {
00066 OVERSAMPLE_OPT, BOX_HSIZE_OPT, CHUNK_SIZE_OPT, LAMBDA_STEP_OPT,
00067 CLIP_KAPPA_OPT, CLIP_FRAC_OPT, CLIP_NITER_OPT, NITER_OPT, METHOD_OPT,
00068 MIN_ORDER_OPT, MAX_ORDER_OPT, DEBUG_OPT, HELP_OPT
00069 };
00070
00071 static struct option long_options[] = {
00072 {"oversample", required_argument, 0, OVERSAMPLE_OPT},
00073 {"box-hsize", required_argument, 0, BOX_HSIZE_OPT},
00074 {"chunk-size", required_argument, 0, CHUNK_SIZE_OPT},
00075 {"lambda-step", required_argument, 0, LAMBDA_STEP_OPT},
00076 {"clip-kappa", required_argument, 0, CLIP_KAPPA_OPT},
00077 {"clip-frac", required_argument, 0, CLIP_FRAC_OPT},
00078 {"clip-niter", required_argument, 0, CLIP_NITER_OPT},
00079 {"niter", required_argument, 0, NITER_OPT},
00080 {"method", required_argument, 0, METHOD_OPT},
00081 {"order-min", required_argument, 0, MIN_ORDER_OPT},
00082 {"order-max", required_argument, 0, MAX_ORDER_OPT},
00083 {"debug", required_argument, 0, DEBUG_OPT},
00084 {"help", 0, 0, HELP_OPT},
00085 {0, 0, 0, 0}
00086 };
00087
00088 static void Help( void )
00089 {
00090 puts( "Unitary test of xsh_opt_extract");
00091 puts( "Usage: test_xsh_opt_extract [options] <input_files>");
00092
00093 puts( "Options" ) ;
00094 puts( " --oversample=<n> : Oversample factor [5]" ) ;
00095 puts( " --box-hsize=<n> : Extract box half size [pixels] [10]" ) ;
00096 puts( " --chunk-size=<n> : Chunk size [pixels] [50]" ) ;
00097 puts( " --lambda-step=<n> : Step in wavelength [0.02]" );
00098 puts( " --clip-kappa=<nn> : Kappa for cosmics Ray hits rejection [3]" ) ;
00099 puts( " --clip-frac=<nn> : Maxium bad pixels fraction for cosmics Ray hits rejection [0.4]" );
00100 puts( " --clip-niter=<n> : Number of iterations for cosmics Ray hits rejection [2]" ) ;
00101 puts( " --niter=<n> : Number of iterations [1]" ) ;
00102 puts( " --method=<string> : Extraction method GAUSSIAN | GENERAL [GAUSSIAN]" ) ;
00103 puts( " --order-min=<n> : Minimum abs order" );
00104 puts( " --order-max=<n> : Maximum abs order" );
00105 puts( " --debug=<n> : Level of debug NONE | LOW | MEDIUM | HIGH [MEDIUM]" );
00106 puts( " --help : What you see" ) ;
00107 puts( "\nInput Files" ) ;
00108 puts( "The input files argument MUST be in this order:" ) ;
00109 puts( " 1. Science frame in PRE format sky subtracted" ) ;
00110 puts( " 2. Localization table" );
00111 puts( " 3. SOF [SPECTRAL_FORMAT, ORDER_TAB_EDGES,WAVE_TAB_2D, WAVEMAP, SLITMAP, MASTER_FLAT]\n" ) ;
00112 TEST_END();
00113 }
00114
00115 static void HandleOptions( int argc, char **argv,
00116 xsh_opt_extract_param *opt_extract_par, int *order_min, int *order_max)
00117 {
00118 int opt ;
00119 int option_index = 0;
00120
00121 while (( opt = getopt_long (argc, argv, "oversample:box-hsize:chunk-size",
00122 long_options, &option_index)) != EOF ){
00123
00124 switch ( opt ) {
00125 case OVERSAMPLE_OPT:
00126 opt_extract_par->oversample = atoi( optarg);
00127 break ;
00128 case BOX_HSIZE_OPT:
00129 opt_extract_par->box_hsize = atoi( optarg);
00130 break ;
00131 case CHUNK_SIZE_OPT:
00132 opt_extract_par->chunk_size = atoi( optarg);
00133 break;
00134 case LAMBDA_STEP_OPT:
00135 opt_extract_par->lambda_step = atof( optarg);
00136 break;
00137 case CLIP_KAPPA_OPT:
00138 opt_extract_par->clip_kappa = atof( optarg);
00139 break;
00140 case CLIP_FRAC_OPT:
00141 opt_extract_par->clip_frac = atof( optarg);
00142 break;
00143 case CLIP_NITER_OPT:
00144 opt_extract_par->clip_niter = atoi( optarg);
00145 break;
00146 case NITER_OPT:
00147 opt_extract_par->niter = atoi( optarg);
00148 break;
00149 case METHOD_OPT:
00150 if ( strcmp( OPTEXTRACT_METHOD_PRINT(GAUSS_METHOD), optarg) == 0){
00151 opt_extract_par->method = GAUSS_METHOD;
00152 }
00153 else {
00154 opt_extract_par->method = GENERAL_METHOD;
00155 }
00156 break;
00157 case MIN_ORDER_OPT:
00158 sscanf( optarg, "%d", order_min);
00159 break;
00160 case MAX_ORDER_OPT:
00161 sscanf( optarg, "%d", order_max);
00162 break;
00163 case DEBUG_OPT:
00164 if ( strcmp( optarg, "LOW")==0){
00165 xsh_debug_level_set( XSH_DEBUG_LEVEL_LOW);
00166 }
00167 else if ( strcmp( optarg, "HIGH")==0){
00168 xsh_debug_level_set( XSH_DEBUG_LEVEL_HIGH);
00169 }
00170 else if ( strcmp( optarg, "NONE")==0){
00171 xsh_debug_level_set( XSH_DEBUG_LEVEL_NONE);
00172 }
00173 break;
00174 default:
00175 Help();
00176 exit(-1);
00177 }
00178 }
00179 return;
00180 }
00181
00182
00183
00184
00185
00193 int main( int argc, char **argv)
00194 {
00195 int ret;
00196
00197 xsh_instrument* instrument = NULL;
00198 const char *sof_name = NULL;
00199 cpl_frameset *set = NULL;
00200
00201 const char* sci_name = NULL;
00202 const char* loc_name = NULL;
00203 xsh_opt_extract_param opt_extract_par = { 5, 10, 10, 0.01, 10, GAUSS_METHOD};
00204 xsh_merge_param merge_par = { MEAN_MERGE_METHOD};
00205 int order_min = -1;
00206 int order_max = -1;
00207 int rec_min_index = -1;
00208 int rec_max_index = -1;
00209
00210 xsh_order_list* order_list = NULL;
00211
00212 cpl_frame *sci_frame = NULL;
00213 cpl_frame *loc_frame = NULL;
00214 cpl_frame *orderlist_frame = NULL;
00215 cpl_frame *wavesol_frame = NULL;
00216 cpl_frame *model_frame = NULL;
00217 cpl_frame *wavemap_frame = NULL;
00218 cpl_frame *slitmap_frame = NULL;
00219 cpl_frame *spectralformat_frame = NULL;
00220 cpl_frame *masterflat_frame = NULL;
00221 cpl_frame *orderext1d_frame = NULL;
00222 cpl_frame *orderoxt1d_frame = NULL;
00223 cpl_frame *spectrumext1d_frame = NULL;
00224 cpl_frame *spectrumoxt1d_frame = NULL;
00225
00226
00227
00228 TESTS_INIT( MODULE_ID);
00229 cpl_msg_set_level( CPL_MSG_DEBUG);
00230 xsh_debug_level_set( XSH_DEBUG_LEVEL_MEDIUM);
00231
00232 opt_extract_par.oversample = 5;
00233 opt_extract_par.box_hsize = 10;
00234 opt_extract_par.chunk_size = 50;
00235 opt_extract_par.lambda_step = 0.02;
00236 opt_extract_par.clip_kappa = 3;
00237 opt_extract_par.clip_frac = 0.4;
00238 opt_extract_par.clip_niter = 2;
00239 opt_extract_par.niter = 1;
00240 opt_extract_par.method = GAUSS_METHOD;
00241
00242
00243 HandleOptions( argc, argv, &opt_extract_par, &order_min, &order_max);
00244
00245 if ( (argc - optind) >= 3 ) {
00246 sci_name = argv[optind];
00247 loc_name = argv[optind+1];
00248 sof_name = argv[optind+2];
00249 }
00250 else{
00251 Help();
00252 exit(0);
00253 }
00254
00255
00256 check( set = sof_to_frameset( sof_name));
00257
00258
00259 check( instrument = xsh_dfs_set_groups( set));
00260
00261 check( spectralformat_frame = xsh_find_spectral_format( set, instrument));
00262 check( orderlist_frame = xsh_find_order_tab_edges( set, instrument));
00263 check( wavemap_frame = xsh_find_wavemap( set, instrument));
00264 check( slitmap_frame = xsh_find_slitmap( set, instrument));
00265 check( masterflat_frame = xsh_find_master_flat( set, instrument));
00266
00267 if(( model_frame = xsh_find_frame_with_tag( set, XSH_MOD_CFG_OPT_2D,
00268 instrument)) == NULL) {
00269 xsh_error_reset();
00270 if ((model_frame = xsh_find_frame_with_tag( set,XSH_MOD_CFG_TAB,
00271 instrument)) == NULL) {
00272 xsh_error_reset();
00273 }
00274 }
00275 if ( model_frame == NULL){
00276 check( wavesol_frame = xsh_find_wave_tab_2d( set, instrument));
00277 }
00278 TESTS_XSH_FRAME_CREATE( sci_frame, "OBJECT_SLIT_STARE_arm", sci_name);
00279 TESTS_XSH_FRAME_CREATE( loc_frame, "LOCALIZATION_arm", loc_name);
00280
00281 xsh_msg("SCI : %s",
00282 cpl_frame_get_filename( sci_frame));
00283 xsh_msg("LOCALIZATION : %s",
00284 cpl_frame_get_filename( loc_frame));
00285 xsh_msg("ORDERLIST : %s",
00286 cpl_frame_get_filename( orderlist_frame));
00287 if (model_frame != NULL){
00288 bool found_line=true;
00289 check(xsh_model_temperature_update_frame(&model_frame,sci_frame,
00290 instrument,&found_line));
00291 xsh_msg("MODEL : %s",
00292 cpl_frame_get_filename( model_frame));
00293 }
00294 else{
00295 xsh_msg("WAVESOL : %s",
00296 cpl_frame_get_filename( wavesol_frame));
00297 }
00298 xsh_msg("SPECTRALFORMAT : %s",
00299 cpl_frame_get_filename( spectralformat_frame));
00300 xsh_msg("WAVEMAP : %s",
00301 cpl_frame_get_filename( wavemap_frame));
00302 xsh_msg("SLITMAP : %s",
00303 cpl_frame_get_filename( slitmap_frame));
00304 xsh_msg("MASTERFLAT : %s",
00305 cpl_frame_get_filename( masterflat_frame));
00306
00307 xsh_msg(" Parameters ");
00308 xsh_msg(" oversample %d", opt_extract_par.oversample);
00309 xsh_msg(" box-hsize %d", opt_extract_par.box_hsize);
00310 xsh_msg(" chunk-size %d", opt_extract_par.chunk_size);
00311 xsh_msg(" lambda-step %f", opt_extract_par.lambda_step);
00312 xsh_msg(" clip-kappa %f", opt_extract_par.clip_kappa);
00313 xsh_msg(" clip-frac %f", opt_extract_par.clip_frac);
00314 xsh_msg(" clip-niter %d", opt_extract_par.clip_niter);
00315 xsh_msg(" niter %d", opt_extract_par.niter);
00316 xsh_msg(" method %s", OPTEXTRACT_METHOD_PRINT(opt_extract_par.method));
00317
00318 check( order_list = xsh_order_list_load ( orderlist_frame, instrument));
00319
00320 if ( order_min != -1) {
00321 check( rec_min_index = xsh_order_list_get_index_by_absorder( order_list,
00322 order_min));
00323 xsh_msg("Order min %d => index %d", order_min, rec_min_index);
00324 }
00325 else{
00326 rec_min_index = 0;
00327 }
00328
00329 if ( order_max != -1) {
00330 check( rec_max_index = xsh_order_list_get_index_by_absorder( order_list,
00331 order_max));
00332 xsh_msg("Order max %d => index %d", order_max, rec_max_index);
00333 }
00334 else{
00335 rec_max_index = order_list->size;
00336 }
00337
00338 check( xsh_opt_extract_orders( sci_frame, orderlist_frame, wavesol_frame, model_frame,
00339 wavemap_frame, slitmap_frame, loc_frame, spectralformat_frame, masterflat_frame,
00340 instrument, &opt_extract_par, rec_min_index, rec_max_index, "TEST",
00341 &orderext1d_frame, &orderoxt1d_frame));
00342
00343 check( spectrumext1d_frame = xsh_merge_ord( orderext1d_frame, instrument, &merge_par,"test"));
00344 check( spectrumoxt1d_frame = xsh_merge_ord( orderoxt1d_frame, instrument, &merge_par,"test"));
00345
00346 cleanup:
00347 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00348 xsh_error_dump(CPL_MSG_ERROR);
00349 ret = 1;
00350 }
00351 xsh_order_list_free( &order_list);
00352
00353 xsh_free_frame( &sci_frame);
00354 xsh_free_frame( &loc_frame);
00355 xsh_free_frameset( &set);
00356 xsh_instrument_free( &instrument);
00357 xsh_free_frame( &orderext1d_frame);
00358 xsh_free_frame( &orderoxt1d_frame);
00359
00360 xsh_free_frame( &spectrumext1d_frame);
00361 xsh_free_frame( &spectrumoxt1d_frame);
00362
00363 TEST_END();
00364 return ret;
00365 }
00366