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_spectrum.h>
00050 #include <xsh_data_localization.h>
00051 #include <xsh_drl.h>
00052 #include <xsh_pfits.h>
00053 #include <xsh_parameters.h>
00054 #include <xsh_badpixelmap.h>
00055
00056 #include <cpl.h>
00057 #include <math.h>
00058
00059 #include <string.h>
00060 #include <getopt.h>
00061
00062
00063
00064
00065
00066 #define MODULE_ID "XSH_LOCALIZE_OBJ"
00067
00068 enum {
00069 NBCHUNK_OPT, TRESHOLD_OPT, DEG_POLY_OPT, METHOD_OPT, SLIT_POS_OPT,
00070 SLIT_HHEIGHT_OPT, KAPPA_OPT, NITER_OPT, SKYMASK_OPT, HELP_OPT
00071 } ;
00072
00073 static struct option long_options[] = {
00074 {"nbchunk", required_argument, 0, NBCHUNK_OPT},
00075 {"threshold", required_argument, 0, TRESHOLD_OPT},
00076 {"deg_poly", required_argument, 0, DEG_POLY_OPT},
00077 {"method", required_argument, 0, METHOD_OPT},
00078 {"slit_position", required_argument, 0, SLIT_POS_OPT},
00079 {"slit_hheight", required_argument, 0, SLIT_HHEIGHT_OPT},
00080 {"kappa", required_argument, 0, KAPPA_OPT},
00081 {"niter", required_argument, 0, NITER_OPT},
00082 {"skymask", required_argument, 0, SKYMASK_OPT},
00083 {"help", 0, 0, HELP_OPT},
00084 {0, 0, 0, 0}
00085 };
00086
00087 static void Help( void )
00088 {
00089 puts( "Unitary test of Localization");
00090 puts( "Usage: test_xsh_localize_obj [options] REC_FRAME [LOC_TABLE]");
00091
00092 puts( "Options" ) ;
00093 puts( " --help : What you see" ) ;
00094 puts( " --method= : MANUAL | AUTO | GAUSSIAN [GAUSSIAN]");
00095 puts( " MANUAL options");
00096 puts( " --slit_position=<n>: (MANUAL only) the reference slit position in arcsec [0]");
00097 puts( " --slit_hheight=<n> : (MANUAL only) the half size of slit height in arcsec [0.5]");
00098 puts( " AUTO or GAUSSIAN options");
00099 puts( " --deg_poly=<n> : (AUTO, GAUSSIAN) polynomial degree of fit [0]");
00100 puts( " --niter=<n> : (AUTO, GAUSSIAN) Number of iterations for sigma clipping[3]");
00101 puts( " --kappa=<n> : (AUTO, GAUSSIAN) Kappa for sigma clipping [1.0]");
00102 puts( " --nbchunk=<n> : (AUTO, GAUSSIAN) Number of chunk [10]");
00103 puts( " --skymask=<file> : (AUTO, GAUSSIAN) Sky mask file");
00104 puts( " AUTO options");
00105 puts( " --threshold=<n> : (AUTO) Threshold to find edges [10]");
00106 puts( "\nInput Files" ) ;
00107 puts( "The input files argument MUST be in this order:" ) ;
00108 puts( " 1. DRL rectified 2D frame tag DRL_ORDER2D" ) ;
00109 puts( " 2. (optional] Localization table" );
00110 TEST_END();
00111 }
00112
00113
00114 static void HandleOptions( int argc, char **argv,
00115 xsh_localize_obj_param *loc_par, char **skymask_name)
00116 {
00117 int opt ;
00118 int option_index = 0;
00119
00120 while (( opt = getopt_long (argc, argv,
00121 "nbchunk:threshold:deg_poly:method:slit_position:slit_hheight",
00122 long_options, &option_index)) != EOF ){
00123
00124 switch ( opt ) {
00125 case NBCHUNK_OPT:
00126 loc_par->loc_chunk_nb = atoi(optarg);
00127 break ;
00128 case TRESHOLD_OPT:
00129 loc_par->loc_thresh = atof(optarg);
00130 break ;
00131 case DEG_POLY_OPT:
00132 loc_par->loc_deg_poly = atoi(optarg);
00133 break;
00134 case METHOD_OPT:
00135 if ( strcmp(optarg, LOCALIZE_METHOD_PRINT(LOC_MANUAL_METHOD)) == 0){
00136 loc_par->method = LOC_MANUAL_METHOD;
00137 }
00138 else if ( strcmp(optarg, LOCALIZE_METHOD_PRINT(LOC_MAXIMUM_METHOD)) == 0){
00139 loc_par->method = LOC_MAXIMUM_METHOD;
00140 }
00141 else if ( strcmp(optarg, LOCALIZE_METHOD_PRINT(LOC_GAUSSIAN_METHOD)) == 0){
00142 loc_par->method = LOC_GAUSSIAN_METHOD;
00143 }
00144 else{
00145 xsh_msg("WRONG method %s", optarg);
00146 exit(-1);
00147 }
00148 break;
00149
00150 case SLIT_POS_OPT:
00151 loc_par->slit_position = atof( optarg);
00152 break ;
00153 case SLIT_HHEIGHT_OPT:
00154 loc_par->slit_hheight = atof( optarg);
00155 break;
00156 case KAPPA_OPT:
00157 loc_par->kappa = atof( optarg);
00158 break;
00159 case NITER_OPT:
00160 loc_par->niter = atoi( optarg);
00161 break;
00162 case SKYMASK_OPT:
00163 loc_par->use_skymask = TRUE;
00164 *skymask_name = optarg;
00165 break;
00166
00167 default:
00168 Help(); exit(-1);
00169 }
00170 }
00171 return;
00172 }
00173
00174
00175
00176
00177 static void analyse_localization( cpl_frame *merge_frame, cpl_frame* loc_frame,
00178 xsh_instrument* instr);
00179
00180
00181
00182
00183 static void analyse_localization( cpl_frame* rec_frame, cpl_frame* loc_frame,
00184 xsh_instrument* instr)
00185 {
00186 const char* rec_name = NULL;
00187 cpl_frame* merge_frame = NULL;
00188 const char* loc_name = NULL;
00189 xsh_localization *loc_list = NULL;
00190 xsh_spectrum *spectrum = NULL;
00191 int ilambda;
00192 FILE *loc_regfile = NULL ;
00193 FILE *loc_datfile = NULL;
00194 xsh_merge_param merge_par = {MEAN_MERGE_METHOD};
00195
00196 XSH_ASSURE_NOT_NULL( rec_frame);
00197 XSH_ASSURE_NOT_NULL( loc_frame);
00198
00199 check( rec_name = cpl_frame_get_filename( rec_frame));
00200 check( loc_name = cpl_frame_get_filename( loc_frame));
00201
00202 xsh_msg("---Dump the localization");
00203 printf("DRL RECTIFY frame : %s\n", rec_name);
00204 printf("LOCALIZE frame : %s\n", loc_name);
00205
00206 check( merge_frame = xsh_merge_ord( rec_frame, instr, &merge_par,
00207 "TEST_MERGE"));
00208
00209 check( spectrum = xsh_spectrum_load( merge_frame));
00210 check( loc_list = xsh_localization_load( loc_frame));
00211
00212 loc_regfile = fopen( "LOCALIZATION.reg", "w");
00213 fprintf(loc_regfile, "# Region file format: DS9 version 4.0\n"\
00214 "#red center, blue low, green up\n"\
00215 "global color=red font=\"helvetica 4 normal\""\
00216 "select=1 highlite=1 edit=1 move=1 delete=1 include=1 fixed=0"\
00217 " source=1\nwcs\n");
00218
00219 loc_datfile = fopen( "LOCALIZATION.dat", "w");
00220 fprintf( loc_datfile, "#lambda slow scen sup\n");
00221
00222 for( ilambda=0; ilambda < spectrum->size_lambda; ilambda++){
00223 double slit_cen = 0.0, slit_low= 0.0, slit_up = 0.0;
00224 double lambda;
00225
00226 lambda = spectrum->lambda_min+ilambda*spectrum->lambda_step;
00227 check( slit_cen = cpl_polynomial_eval_1d( loc_list->cenpoly,
00228 lambda, NULL));
00229 check( slit_low = cpl_polynomial_eval_1d( loc_list->edglopoly,
00230 lambda, NULL));
00231 check( slit_up = cpl_polynomial_eval_1d( loc_list->edguppoly,
00232 lambda, NULL));
00233 fprintf( loc_regfile, "point(%f,%f) "\
00234 "#point=cross color=red font=\"helvetica 4 normal\"\n", lambda, slit_cen);
00235 fprintf( loc_regfile, "point(%f,%f) "\
00236 "#point=cross color=blue font=\"helvetica 4 normal\"\n", lambda, slit_low);
00237 fprintf( loc_regfile, "point(%f,%f) "\
00238 "#point=cross color=green font=\"helvetica 4 normal\"\n", lambda, slit_up);
00239 fprintf( loc_datfile, "%f %f %f %f\n", lambda, slit_low, slit_cen, slit_up);
00240 }
00241
00242 xsh_msg( "Produce ds9 region file : LOCALIZATION.reg");
00243 xsh_msg( "Produce data file : LOCALIZATION.dat");
00244 xsh_msg(" Produce MERGE frame : %s", cpl_frame_get_filename( merge_frame));
00245
00246 cleanup:
00247 if(loc_regfile != NULL) {
00248 fclose( loc_regfile);
00249 }
00250
00251 if(loc_datfile != NULL) {
00252 fclose( loc_datfile);
00253 }
00254 xsh_free_frame( &merge_frame);
00255 xsh_spectrum_free( &spectrum);
00256 xsh_localization_free( &loc_list);
00257
00258 return;
00259 }
00260
00268 int main( int argc, char **argv)
00269 {
00270
00271 int ret = 0 ;
00272 xsh_instrument* instrument = NULL;
00273 xsh_localize_obj_param loc_obj =
00274 { 10, 0.1, 1, 0., LOC_MANUAL_METHOD, 0.0, 0.5, 3,3, FALSE};
00275 char* rec_name = NULL;
00276 cpl_frame *rec_frame = NULL;
00277
00278 const char *tag = NULL;
00279 char* loc_name = NULL;
00280 cpl_frame* loc_frame = NULL;
00281 cpl_propertylist *plist = NULL;
00282 XSH_ARM arm = XSH_ARM_UNDEFINED;
00283 char * skymask_name = NULL;
00284 cpl_frame *skymask_frame = NULL;
00285 const int decode_bp=2147483647;
00286
00287 TESTS_INIT(MODULE_ID);
00288
00289 cpl_msg_set_level(CPL_MSG_DEBUG);
00290 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM);
00291
00292 loc_obj.kappa = 1.0;
00293 loc_obj.niter = 3;
00294 loc_obj.method=LOC_GAUSSIAN_METHOD;
00295 loc_obj.loc_deg_poly = 0;
00296 loc_obj.loc_chunk_nb = 10;
00297
00298
00299 HandleOptions( argc, argv, &loc_obj, &skymask_name);
00300
00301 if ( (argc - optind) > 0 ) {
00302 rec_name = argv[optind];
00303 if ((argc - optind) > 1){
00304 loc_name = argv[optind+1];
00305 }
00306 }
00307 else {
00308 Help();
00309 exit( 0);
00310 }
00311
00312 check( plist = cpl_propertylist_load( rec_name, 0));
00313 check( arm = xsh_pfits_get_arm( plist));
00314
00315 TESTS_XSH_INSTRUMENT_CREATE( instrument, XSH_MODE_SLIT, arm,
00316 XSH_LAMP_UNDEFINED, "xsh_scired_slit_stare");
00317 xsh_instrument_set_decode_bp( instrument, decode_bp ) ;
00318 tag = XSH_GET_TAG_FROM_ARM( XSH_MERGE2D, instrument);
00319
00320 TESTS_XSH_FRAME_CREATE( rec_frame, tag, rec_name);
00321
00322 xsh_msg("---Input Frames");
00323 xsh_msg("DRL RECTIFY frame : %s tag %s", rec_name, tag);
00324
00325 if (loc_name == NULL){
00326 xsh_msg( "---Localize parameters");
00327 xsh_msg( " --method %s ", LOCALIZE_METHOD_PRINT(loc_obj.method));
00328 if ( loc_obj.method == LOC_MANUAL_METHOD){
00329 xsh_msg(" slit_position : %f",loc_obj.slit_position);
00330 xsh_msg(" slit_hheight : %f",loc_obj.slit_hheight);
00331 }
00332 else {
00333 if (loc_obj.method == LOC_MAXIMUM_METHOD){
00334 xsh_msg(" threshold : %f",loc_obj.loc_thresh);
00335 }
00336 xsh_msg(" nb_chunks : %d",loc_obj.loc_chunk_nb);
00337 xsh_msg(" deg_poly : %d",loc_obj.loc_deg_poly);
00338 xsh_msg(" kappa : %f", loc_obj.kappa);
00339 xsh_msg(" niter : %d", loc_obj.niter);
00340 if ( loc_obj.use_skymask == TRUE){
00341 xsh_msg(" skymask : %s", skymask_name);
00342 tag = XSH_GET_TAG_FROM_ARM( XSH_SKY_LINE_LIST, instrument);
00343 TESTS_XSH_FRAME_CREATE( skymask_frame, tag, skymask_name);
00344 }
00345 }
00346 check( loc_frame = xsh_localize_obj( rec_frame, skymask_frame, instrument, &loc_obj,
00347 NULL, "LOCALIZE_TABLE.fits"));
00348 }
00349 else{
00350 XSH_ASSURE_NOT_NULL (loc_name);
00351 tag = XSH_GET_TAG_FROM_ARM( XSH_LOCALIZATION, instrument);
00352 TESTS_XSH_FRAME_CREATE( loc_frame, tag, loc_name);
00353 }
00354
00355
00356 check( analyse_localization( rec_frame, loc_frame, instrument));
00357
00358 cleanup:
00359 xsh_free_frame( &loc_frame);
00360 xsh_free_frame( & rec_frame);
00361 xsh_free_frame( &skymask_frame);
00362 xsh_instrument_free( &instrument);
00363 xsh_free_propertylist( &plist);
00364
00365 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00366 xsh_error_dump(CPL_MSG_ERROR);
00367 ret=1;
00368 }
00369 TEST_END();
00370 return ret ;
00371 }
00372