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 #include <xsh_model_utils.h>
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, 1., 2, 2, GAUSS_METHOD};
00204 xsh_merge_param merge_par = { WEIGHTED_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 *orderoxt1d_eso_frame = NULL;
00224 cpl_frame *qc_subex_frame = NULL;
00225 cpl_frame *qc_s2ddiv1d_frame = NULL;
00226 cpl_frame *qc_model_frame = NULL;
00227 cpl_frame *qc_weight_frame = NULL;
00228
00229 cpl_frame *spectrumext1d_frame = NULL;
00230 cpl_frame *spectrumoxt1d_frame = NULL;
00231 const int decode_bp=2147483647;
00232
00233
00234 TESTS_INIT( MODULE_ID);
00235 cpl_msg_set_level( CPL_MSG_DEBUG);
00236 xsh_debug_level_set( XSH_DEBUG_LEVEL_MEDIUM);
00237
00238 opt_extract_par.oversample = 5;
00239 opt_extract_par.box_hsize = 10;
00240 opt_extract_par.chunk_size = 50;
00241 opt_extract_par.lambda_step = 0.02;
00242 opt_extract_par.clip_kappa = 3;
00243 opt_extract_par.clip_frac = 0.4;
00244 opt_extract_par.clip_niter = 2;
00245 opt_extract_par.niter = 1;
00246 opt_extract_par.method = GAUSS_METHOD;
00247
00248
00249 HandleOptions( argc, argv, &opt_extract_par, &order_min, &order_max);
00250
00251 if ( (argc - optind) >= 3 ) {
00252 sci_name = argv[optind];
00253 loc_name = argv[optind+1];
00254 sof_name = argv[optind+2];
00255 }
00256 else{
00257 Help();
00258 exit(0);
00259 }
00260
00261
00262 check( set = sof_to_frameset( sof_name));
00263
00264
00265 check( instrument = xsh_dfs_set_groups( set));
00266
00267 check( spectralformat_frame = xsh_find_spectral_format( set, instrument));
00268 check( orderlist_frame = xsh_find_order_tab_edges( set, instrument));
00269 check( wavemap_frame = xsh_find_wavemap( set, instrument));
00270 check( slitmap_frame = xsh_find_slitmap( set, instrument));
00271 check( masterflat_frame = xsh_find_master_flat( set, instrument));
00272 xsh_instrument_set_decode_bp( instrument, decode_bp ) ;
00273
00274 if(( model_frame = xsh_find_frame_with_tag( set, XSH_MOD_CFG_OPT_2D,
00275 instrument)) == NULL) {
00276 xsh_error_reset();
00277 if ((model_frame = xsh_find_frame_with_tag( set,XSH_MOD_CFG_TAB,
00278 instrument)) == NULL) {
00279 xsh_error_reset();
00280 }
00281 }
00282 if ( model_frame == NULL){
00283 check( wavesol_frame = xsh_find_wave_tab_2d( set, instrument));
00284 }
00285 TESTS_XSH_FRAME_CREATE( sci_frame, "OBJECT_SLIT_STARE_arm", sci_name);
00286 TESTS_XSH_FRAME_CREATE( loc_frame, "LOCALIZATION_arm", loc_name);
00287
00288 xsh_msg("SCI : %s",
00289 cpl_frame_get_filename( sci_frame));
00290 xsh_msg("LOCALIZATION : %s",
00291 cpl_frame_get_filename( loc_frame));
00292 xsh_msg("ORDERLIST : %s",
00293 cpl_frame_get_filename( orderlist_frame));
00294 if (model_frame != NULL){
00295 int found_line=0;
00296 check(xsh_model_temperature_update_frame(&model_frame,sci_frame,
00297 instrument,&found_line));
00298 xsh_msg("MODEL : %s",
00299 cpl_frame_get_filename( model_frame));
00300 }
00301 else{
00302 xsh_msg("WAVESOL : %s",
00303 cpl_frame_get_filename( wavesol_frame));
00304 }
00305 xsh_msg("SPECTRALFORMAT : %s",
00306 cpl_frame_get_filename( spectralformat_frame));
00307 xsh_msg("WAVEMAP : %s",
00308 cpl_frame_get_filename( wavemap_frame));
00309 xsh_msg("SLITMAP : %s",
00310 cpl_frame_get_filename( slitmap_frame));
00311 xsh_msg("MASTERFLAT : %s",
00312 cpl_frame_get_filename( masterflat_frame));
00313
00314 xsh_msg(" Parameters ");
00315 xsh_msg(" oversample %d", opt_extract_par.oversample);
00316 xsh_msg(" box-hsize %d", opt_extract_par.box_hsize);
00317 xsh_msg(" chunk-size %d", opt_extract_par.chunk_size);
00318 xsh_msg(" lambda-step %f", opt_extract_par.lambda_step);
00319 xsh_msg(" clip-kappa %f", opt_extract_par.clip_kappa);
00320 xsh_msg(" clip-frac %f", opt_extract_par.clip_frac);
00321 xsh_msg(" clip-niter %d", opt_extract_par.clip_niter);
00322 xsh_msg(" niter %d", opt_extract_par.niter);
00323 xsh_msg(" method %s", OPTEXTRACT_METHOD_PRINT(opt_extract_par.method));
00324
00325 check( order_list = xsh_order_list_load ( orderlist_frame, instrument));
00326
00327 if ( order_min != -1) {
00328 check( rec_min_index = xsh_order_list_get_index_by_absorder( order_list,
00329 order_min));
00330 xsh_msg("Order min %d => index %d", order_min, rec_min_index);
00331 }
00332 else{
00333 rec_min_index = 0;
00334 }
00335
00336 if ( order_max != -1) {
00337 check( rec_max_index = xsh_order_list_get_index_by_absorder( order_list,
00338 order_max));
00339 xsh_msg("Order max %d => index %d", order_max, rec_max_index);
00340 }
00341 else{
00342 rec_max_index = order_list->size;
00343 }
00344
00345 check( xsh_opt_extract_orders( sci_frame, orderlist_frame, wavesol_frame, model_frame,
00346 wavemap_frame, slitmap_frame, loc_frame, spectralformat_frame, masterflat_frame,
00347 instrument, &opt_extract_par, rec_min_index, rec_max_index, "TEST",
00348 &orderext1d_frame, &orderoxt1d_frame,
00349 &orderoxt1d_eso_frame, &qc_subex_frame, &qc_s2ddiv1d_frame,
00350 &qc_model_frame, &qc_weight_frame));
00351
00352 check( spectrumext1d_frame = xsh_merge_ord( orderext1d_frame, instrument, &merge_par,"test"));
00353 check( spectrumoxt1d_frame = xsh_merge_ord( orderoxt1d_frame, instrument, &merge_par,"test"));
00354
00355 cleanup:
00356 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00357 xsh_error_dump(CPL_MSG_ERROR);
00358 ret = 1;
00359 }
00360 xsh_order_list_free( &order_list);
00361
00362 xsh_free_frame( &sci_frame);
00363 xsh_free_frame( &loc_frame);
00364 xsh_free_frameset( &set);
00365 xsh_instrument_free( &instrument);
00366 xsh_free_frame( &orderext1d_frame);
00367 xsh_free_frame( &orderoxt1d_frame);
00368 xsh_free_frame( &orderoxt1d_eso_frame);
00369 xsh_free_frame( &qc_subex_frame);
00370 xsh_free_frame(&qc_s2ddiv1d_frame);
00371 xsh_free_frame(&qc_model_frame);
00372 xsh_free_frame(&qc_weight_frame);
00373
00374 xsh_free_frame( &spectrumext1d_frame);
00375 xsh_free_frame( &spectrumoxt1d_frame);
00376
00377 TEST_END();
00378 return ret;
00379 }
00380