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
00036
00039
00040
00041
00042
00043 #include <tests.h>
00044
00045 #include <xsh_data_pre.h>
00046 #include <xsh_error.h>
00047 #include <xsh_msg.h>
00048 #include <xsh_data_instrument.h>
00049 #include <xsh_data_rec.h>
00050 #include <xsh_data_localization.h>
00051 #include <xsh_drl.h>
00052 #include <xsh_pfits.h>
00053
00054 #include <xsh_badpixelmap.h>
00055 #include <xsh_parameters.h>
00056
00057 #include <cpl.h>
00058 #include <math.h>
00059
00060 #include <getopt.h>
00061
00062
00063
00064
00065
00066 #define MODULE_ID "XSH_EXTRACT_CLEAN"
00067
00068 enum {
00069 DECODEBP_OPT,METHOD_OPT,DEBUG_OPT,HELP_OPT
00070 } ;
00071
00072 static struct option long_options[] = {
00073 {"decode-bp", required_argument, 0, DECODEBP_OPT},
00074 {"method", required_argument, 0, METHOD_OPT},
00075 {"debug", required_argument, 0, DEBUG_OPT},
00076 {"help", 0, 0, HELP_OPT},
00077 {0, 0, 0, 0}
00078 };
00079
00080 static void Help( void )
00081 {
00082 puts( "Unitary test of xsh_extract_clean");
00083 puts( "Usage: test_xsh_extract_clean [options] <input_files>");
00084
00085 puts( "Options" ) ;
00086 puts( " --decode-bp=<n> : Integer representation of the bits to be considered bad when decoding the bad pixel mask pixel values.");
00087 puts( " --method=<n> : method for extraction CLEAN | LOCALIZATION | FULL | NOD");
00088 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
00089 puts( " --help : What you see" ) ;
00090 puts( "\nInput Files" ) ;
00091 puts( "The input files argument MUST be in this order:" ) ;
00092 puts( " 1. Rectified frame 2D" ) ;
00093 puts( " 2. Localization table");
00094 TEST_END();
00095 exit(0);
00096 }
00097
00098 static void HandleOptions( int argc, char **argv,
00099 xsh_extract_param *extract_par,int* decode_bp)
00100 {
00101 int opt ;
00102 int option_index = 0;
00103
00104 while (( opt = getopt_long (argc, argv, "slit_position:slit_height:method",
00105 long_options, &option_index)) != EOF ){
00106
00107 switch ( opt ) {
00108 case METHOD_OPT:
00109 if ( strcmp(optarg, EXTRACT_METHOD_PRINT( LOCALIZATION_METHOD)) == 0){
00110 extract_par->method = LOCALIZATION_METHOD;
00111 }
00112 else if ( strcmp(optarg, EXTRACT_METHOD_PRINT( FULL_METHOD)) == 0){
00113 extract_par->method = FULL_METHOD;
00114 }
00115 else if ( strcmp(optarg, EXTRACT_METHOD_PRINT( CLEAN_METHOD)) == 0){
00116 extract_par->method = CLEAN_METHOD;
00117 }
00118 else if ( strcmp(optarg, EXTRACT_METHOD_PRINT( NOD_METHOD)) == 0){
00119 extract_par->method = NOD_METHOD;
00120 }
00121 else{
00122 xsh_msg("WRONG method %s", optarg);
00123 exit(-1);
00124 }
00125 break ;
00126 case DEBUG_OPT:
00127 if ( strcmp( optarg, "LOW")==0){
00128 xsh_debug_level_set( XSH_DEBUG_LEVEL_LOW);
00129 }
00130 else if ( strcmp( optarg, "HIGH")==0){
00131 xsh_debug_level_set( XSH_DEBUG_LEVEL_HIGH);
00132 }
00133 break;
00134 case DECODEBP_OPT:
00135 *decode_bp=atoi(optarg);
00136 break;
00137
00138 case HELP_OPT:
00139 Help();
00140 break;
00141 default:
00142 Help();
00143 break;
00144 }
00145 }
00146 return;
00147 }
00148
00149
00150
00151 static void analyse_extraction( cpl_frame* rec_frame, xsh_instrument* instr);
00152
00153
00154
00155
00156 static void analyse_extraction( cpl_frame* rec_frame, xsh_instrument* instr)
00157 {
00158 const char* rec_name = NULL;
00159 xsh_rec_list* rec_list = NULL;
00160 int iorder;
00161
00162 XSH_ASSURE_NOT_NULL( rec_frame);
00163
00164 check( rec_name = cpl_frame_get_filename( rec_frame));
00165
00166 printf("RECTIFY frame : %s\n", rec_name);
00167 check( rec_list = xsh_rec_list_load( rec_frame, instr));
00168
00169 for(iorder=0; iorder< rec_list->size; iorder++){
00170 int order = 0, ilambda = 0;
00171 int nlambda = 0;
00172 float *flux = NULL;
00173 float *err = NULL;
00174 double *lambda = NULL;
00175 char name[256];
00176 FILE* datfile = NULL;
00177
00178 check( order = xsh_rec_list_get_order(rec_list, iorder));
00179 check( nlambda = xsh_rec_list_get_nlambda(rec_list, iorder));
00180 check( flux = xsh_rec_list_get_data1( rec_list, iorder));
00181 check( err = xsh_rec_list_get_errs1( rec_list, iorder));
00182 check( lambda = xsh_rec_list_get_lambda( rec_list, iorder));
00183
00184 sprintf( name, "extract_order%d.dat",order);
00185 xsh_msg("Save file %s",name);
00186 datfile = fopen( name, "w");
00187
00188 for(ilambda=0; ilambda < nlambda; ilambda++){
00189 fprintf( datfile,"%f %f\n",lambda[ilambda],flux[ilambda]);
00190 }
00191 fclose(datfile);
00192
00193 sprintf( name, "extract_err_order%d.dat",order);
00194 xsh_msg("Save file %s",name);
00195 datfile = fopen( name, "w");
00196
00197 for(ilambda=0; ilambda < nlambda; ilambda++){
00198 fprintf( datfile,"%f %f\n",lambda[ilambda],err[ilambda]);
00199 }
00200 fclose(datfile);
00201 }
00202 cleanup:
00203 xsh_rec_list_free( &rec_list);
00204 return;
00205 }
00206
00214 int main( int argc, char **argv)
00215 {
00216
00217 int ret = 0 ;
00218 xsh_instrument* instrument = NULL;
00219 xsh_extract_param extract_obj = { CLEAN_METHOD};
00220 cpl_propertylist *header= NULL;
00221 const char *tag = NULL;
00222 cpl_frame* result = NULL;
00223 cpl_frame* result_eso = NULL;
00224
00225 char* rec_name = NULL;
00226 cpl_frame* rec_frame = NULL;
00227 char* loc_name = NULL;
00228 cpl_frame* loc_frame = NULL;
00229 XSH_ARM arm;
00230 int decode_bp=DECODE_BP_FLAG_DEF;
00231 xsh_interpolate_bp_param * ipol_bp_par={FALSE, 7 };
00232
00233
00234 TESTS_INIT(MODULE_ID);
00235
00236 cpl_msg_set_level(CPL_MSG_DEBUG);
00237 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM) ;
00238
00239
00240
00241 HandleOptions( argc, argv, &extract_obj,&decode_bp);
00242 xsh_msg("argc=%d optind=%d",argc,optind);
00243 if ( (argc - optind) > 0 ) {
00244 rec_name = argv[optind];
00245 if ( (argc - optind) > 1){
00246 loc_name = argv[optind+1];
00247 }
00248 }
00249 else{
00250 Help();
00251 }
00252 rec_frame = cpl_frame_new();
00253 XSH_ASSURE_NOT_NULL (rec_frame);
00254 cpl_frame_set_filename( rec_frame, rec_name) ;
00255
00256 check( header = cpl_propertylist_load( rec_name, 0));
00257 check( tag = xsh_pfits_get_pcatg( header));
00258 check( arm = xsh_pfits_get_arm( header))
00259 ;
00260 cpl_frame_set_level( rec_frame, CPL_FRAME_LEVEL_TEMPORARY);
00261 cpl_frame_set_group( rec_frame, CPL_FRAME_GROUP_RAW ) ;
00262 cpl_frame_set_tag( rec_frame, tag);
00263
00264
00265 instrument = xsh_instrument_new();
00266 xsh_instrument_set_mode( instrument, XSH_MODE_IFU ) ;
00267 xsh_instrument_set_lamp( instrument, XSH_LAMP_QTH ) ;
00268 xsh_instrument_set_arm( instrument, arm);
00269 xsh_instrument_set_decode_bp(instrument,decode_bp);
00270
00271
00272
00273 if ( loc_name != NULL){
00274 loc_frame = cpl_frame_new();
00275 cpl_frame_set_filename( loc_frame, loc_name);
00276 cpl_frame_set_level( rec_frame, CPL_FRAME_LEVEL_TEMPORARY);
00277 cpl_frame_set_group( rec_frame, CPL_FRAME_GROUP_RAW );
00278 }
00279
00280 xsh_msg("Extract Parameters");
00281 xsh_msg("Rectified frame : %s", rec_name);
00282 if (loc_name != NULL){
00283 xsh_msg("Localization table : %s", loc_name);
00284 }
00285
00286 check(result = xsh_extract_clean(rec_frame, loc_frame, instrument,
00287 &extract_obj,&ipol_bp_par,&result_eso,
00288 "test"));
00289
00290 check( analyse_extraction( result, instrument));
00291
00292 cleanup:
00293
00294 xsh_instrument_free( &instrument);
00295 xsh_free_frame( &rec_frame);
00296 xsh_free_frame( &result);
00297 xsh_free_frame( &result_eso);
00298 xsh_free_frame( &loc_frame);
00299 xsh_free_frame( &rec_frame);
00300 xsh_free_propertylist( &header);
00301
00302 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00303 xsh_error_dump(CPL_MSG_ERROR);
00304 ret= 1;
00305 }
00306 TEST_END();
00307 return ret ;
00308 }
00309