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