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_the_map.h>
00050 #include <xsh_data_localization.h>
00051 #include <xsh_data_spectralformat.h>
00052
00053 #include <xsh_drl.h>
00054 #include <xsh_pfits.h>
00055
00056 #include <xsh_badpixelmap.h>
00057
00058 #include <cpl.h>
00059 #include <math.h>
00060
00061 #include <getopt.h>
00062
00063
00064
00065
00066
00067 #define MODULE_ID "XSH_COMBINE_NOD"
00068
00069
00070
00071
00072 enum {
00073 KERNEL_OPT, RADIUS_OPT, BIN_LAMBDA_OPT, BIN_SPACE_OPT, HELP_OPT,
00074 MIN_ORDER_OPT, MAX_ORDER_OPT, SLIT_MIN_OPT, NSLIT_OPT, USELOC_OPT,
00075 NOD_SET_OPT, NOD_ORDER_OPT
00076 } ;
00077
00078 static const char * Options = "" ;
00079
00080 static struct option long_options[] = {
00081 {"kernel", required_argument, 0, KERNEL_OPT},
00082 {"radius", required_argument, 0, RADIUS_OPT},
00083 {"bin-lambda", required_argument, 0, BIN_LAMBDA_OPT},
00084 {"bin-space", required_argument, 0, BIN_SPACE_OPT},
00085 {"order-min", required_argument, 0, MIN_ORDER_OPT},
00086 {"order-max", required_argument, 0, MAX_ORDER_OPT},
00087 {"slit-min",required_argument, 0, SLIT_MIN_OPT},
00088 {"slit-n",required_argument, 0, NSLIT_OPT},
00089 {"use-localization",required_argument, 0, USELOC_OPT},
00090 {"nod-set",required_argument, 0, NOD_SET_OPT},
00091 {"nod-order",required_argument, 0, NOD_ORDER_OPT},
00092 {"help", 0, 0, HELP_OPT},
00093 {0, 0, 0, 0}
00094 };
00095
00096 static void Help( void )
00097 {
00098 puts( "Unitary test of xsh_combine_nod" ) ;
00099 puts( "Usage: test_rectify [options] <input_files>" ) ;
00100 puts( "Options" ) ;
00101 puts( " --kernel=<name> : Name of the rectify kernel" ) ;
00102 puts( " --radius=<nn> : Radius (default 4)" ) ;
00103 puts( " --bin-lambda=<n> : Bin in Lambda (default 0.1)" ) ;
00104 puts( " --bin-space=<n> : Bin in Slit (default 0.1)" ) ;
00105 puts( " --order-min=<n> : Minimum abs order" );
00106 puts( " --order-max=<n> : Maximum abs order" );
00107 puts( " --slit-min=<n> : Minimum slit to rectify" );
00108 puts( " --slit-n=<n> : Number of pixels in slit rectified frame" );
00109 puts( " --use-localization : 1 if tou want to use it");
00110 puts( " --nod-set=<file> : Set of rectified shifted frames with tag OBJECT_SLIT_NOD_arm");
00111 puts( " --nod-order=<n> : abs order where we analyse nod" );
00112 puts( " --help : What you see" ) ;
00113 puts( "\nInput Files" ) ;
00114 puts( "The input files argument MUST be in this order:" ) ;
00115 puts( " 1. Science frame in PRE format" ) ;
00116 puts( " 2. SOF\n" ) ;
00117 TEST_END();
00118 }
00119
00120 static void HandleOptions( int argc, char **argv,
00121 xsh_rectify_param *rectify_par, int *order_min, int *order_max,
00122 double *slit_min, int *nslit, int *use_loc, char** nod_set_name)
00123 {
00124 int opt ;
00125 int option_index = 0;
00126
00127 while (( opt = getopt_long (argc, argv, Options,
00128 long_options, &option_index)) != EOF )
00129 switch ( opt ) {
00130 case KERNEL_OPT:
00131 strcpy( rectify_par->rectif_kernel, optarg ) ;
00132 break ;
00133 case RADIUS_OPT:
00134 rectify_par->rectif_radius = atof( optarg);
00135 break ;
00136 case BIN_LAMBDA_OPT:
00137 sscanf( optarg, "%lf", &rectify_par->rectif_bin_lambda ) ;
00138 break ;
00139 case BIN_SPACE_OPT:
00140 sscanf( optarg, "%lf", &rectify_par->rectif_bin_space ) ;
00141 break ;
00142 case MIN_ORDER_OPT:
00143 sscanf( optarg, "%d", order_min);
00144 break;
00145 case MAX_ORDER_OPT:
00146 sscanf( optarg, "%d", order_max);
00147 break;
00148 case SLIT_MIN_OPT:
00149 sscanf( optarg, "%lf", slit_min) ;
00150 break ;
00151 case NSLIT_OPT:
00152 sscanf( optarg, "%d", nslit);
00153 break ;
00154 case USELOC_OPT:
00155 sscanf( optarg, "%d", use_loc);
00156 break;
00157 case NOD_SET_OPT:
00158 *nod_set_name = optarg;
00159 break;
00160 case NOD_ORDER_OPT:
00161 sscanf( optarg, "%d", order_min);
00162 break;
00163 default: Help() ; exit( 0 ) ;
00164 }
00165 }
00166
00167
00168 cpl_frame *create_zero_rectify( cpl_frame* rec_frame, int iorder, xsh_instrument* instr)
00169 {
00170 const char* rec_name = NULL;
00171 xsh_rec_list* rec_list = NULL;
00172 int ilambda=0, islit=0;
00173 int nlambda = 0, nslit=0;
00174 float *flux = NULL;
00175 char name[256];
00176 char *shortname = NULL;
00177 cpl_frame *res_frame = NULL;
00178
00179 XSH_ASSURE_NOT_NULL( rec_frame);
00180
00181 check( rec_name = cpl_frame_get_filename( rec_frame));
00182
00183 shortname = strrchr( rec_name, '/');
00184
00185 if (shortname != NULL){
00186 sprintf( name, "ZERO_%s",shortname+1);
00187 }
00188 else{
00189 sprintf( name, "ZERO_%s",rec_name);
00190 }
00191
00192 xsh_msg( "name %s", name);
00193
00194 check( rec_list = xsh_rec_list_load( rec_frame, instr));
00195
00196 check( nlambda = xsh_rec_list_get_nlambda( rec_list, iorder));
00197 check( nslit = xsh_rec_list_get_nslit( rec_list, iorder));
00198 check( flux = xsh_rec_list_get_data1( rec_list, iorder));
00199
00200 for( islit=0; islit < nslit; islit++){
00201 double val =0.0;
00202
00203 for(ilambda=0; ilambda < nlambda; ilambda++){
00204 flux[ilambda+islit*nlambda] = 0.0;
00205 }
00206 }
00207
00208 check( res_frame = xsh_rec_list_save( rec_list, name, "ZERO_REC", 1));
00209
00210 cleanup:
00211 if (cpl_error_get_code() != CPL_ERROR_NONE){
00212 xsh_free_frame( &res_frame);
00213 }
00214 xsh_rec_list_free( &rec_list);
00215 return res_frame;
00216 }
00217
00218
00219
00220
00221
00222 void analyse_rectify( cpl_frame* rec_frame, int iorder, xsh_instrument* instr)
00223 {
00224 const char* rec_name = NULL;
00225 xsh_rec_list* rec_list = NULL;
00226 int ilambda=0, islit=0;
00227 int nlambda = 0, nslit=0;
00228 float *flux = NULL;
00229 float *slit = NULL;
00230 char name[256];
00231 FILE* datfile = NULL;
00232 char *shortname = NULL;
00233
00234 XSH_ASSURE_NOT_NULL( rec_frame);
00235
00236 check( rec_name = cpl_frame_get_filename( rec_frame));
00237
00238 shortname = strrchr( rec_name, '/');
00239
00240 if (shortname != NULL){
00241 rec_name = shortname+1;
00242 }
00243
00244 check( rec_list = xsh_rec_list_load( rec_frame, instr));
00245
00246 check( nlambda = xsh_rec_list_get_nlambda( rec_list, iorder));
00247 check( nslit = xsh_rec_list_get_nslit( rec_list, iorder));
00248 check( flux = xsh_rec_list_get_data1( rec_list, iorder));
00249 check( slit = xsh_rec_list_get_slit( rec_list, iorder));
00250
00251 sprintf( name, "%s_profil.dat",rec_name);
00252 datfile = fopen( name, "w");
00253 fprintf( datfile, "#slit flux\n");
00254
00255 for( islit=0; islit < nslit; islit++){
00256 double val =0.0;
00257
00258 for(ilambda=0; ilambda < nlambda; ilambda++){
00259 val+= flux[ilambda+islit*nlambda];
00260 }
00261 fprintf( datfile,"%f %f\n", slit[islit], val);
00262 }
00263 fclose(datfile);
00264
00265 cleanup:
00266 xsh_rec_list_free( &rec_list);
00267 return;
00268 }
00277 int main( int argc, char **argv)
00278 {
00279
00280 int ret = 0 ;
00281
00282 xsh_instrument* instrument = NULL;
00283
00284 char *sof_name = NULL;
00285 cpl_frameset *set = NULL;
00286 cpl_frame *sliceoffset_frame = NULL;
00287
00288 const char * raw_sof_name = NULL ;
00289 cpl_frameset *raw_set = NULL;
00290
00291 cpl_frame *sci_frame = NULL;
00292 cpl_frame *orderlist_frame = NULL;
00293 cpl_frame *wavesol_frame = NULL;
00294 cpl_frame *model_frame = NULL;
00295 cpl_frame *spectralformat_frame = NULL;
00296 cpl_frame *rec_ab_frame = NULL ;
00297 cpl_frame *rec_abeso_frame = NULL ;
00298 cpl_frame *rec_abtab_frame = NULL ;
00299 cpl_frame *rec_ba_frame = NULL ;
00300 cpl_frame *rec_bashift_frame = NULL ;
00301 cpl_frame *rec_bashifteso_frame = NULL ;
00302 cpl_frame *loc_ab_frame = NULL;
00303 cpl_frame *loc_ba_frame = NULL;
00304 cpl_frameset *nod_set = NULL;
00305 cpl_frameset *zero_nod_set = NULL;
00306 cpl_frameset *combine_set = NULL;
00307
00308 cpl_frame *combine_nod_frame = NULL;
00309 cpl_frame *combine_nodeso_frame = NULL;
00310
00311 int order_min = -1, order_max = -1;
00312 int inod = 0;
00313 int rec_min_index=-1, rec_max_index=-1;
00314 int slitlet;
00315 xsh_order_list* order_list = NULL;
00316 xsh_rectify_param rectify_par;
00317 xsh_localize_obj_param loc_par;
00318 xsh_combine_nod_param cnod_par;
00319 int nslit_c, nslit=-100;
00320 int use_loc_obj = 0;
00321 double slit_min_c, slit_min=-100;
00322 XSH_MODE mode;
00323 const char *tag = NULL;
00324 int i, nb_frames;
00325 double *ref_ra = NULL, *ref_dec = NULL;
00326
00327
00328 TESTS_INIT(MODULE_ID);
00329 cpl_msg_set_level(CPL_MSG_DEBUG);
00330 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM);
00331
00332
00333 strcpy( rectify_par.rectif_kernel, "default");
00334 rectify_par.kernel_type = 0 ;
00335 rectify_par.rectif_radius = 4 ;
00336 rectify_par.rectif_bin_lambda = 0.1 ;
00337 rectify_par.rectif_bin_space = 0.1 ;
00338 rectify_par.conserve_flux = FALSE;
00339
00340 loc_par.method = LOC_MAXIMUM_METHOD;
00341 loc_par.loc_chunk_nb = 10;
00342 loc_par.loc_deg_poly = 0;
00343 loc_par.loc_thresh = 0.1;
00344
00345 HandleOptions( argc, argv, &rectify_par, &order_min, &order_max,
00346 &slit_min, &nslit, &use_loc_obj, &sof_name);
00347
00348 if ( sof_name != NULL){
00349 xsh_msg("Using nod set : %s", sof_name);
00350 check( nod_set = sof_to_frameset( sof_name));
00351 check( instrument = xsh_dfs_set_groups( nod_set));
00352
00353 check( nb_frames = cpl_frameset_get_size( nod_set));
00354
00355 for ( i = 0 ; i < nb_frames ; i++ ) {
00356 check( sci_frame = cpl_frameset_get_frame( nod_set, i));
00357 xsh_msg("NOD : %s", cpl_frame_get_filename( sci_frame));
00358 check( analyse_rectify( sci_frame, inod, instrument));
00359 }
00360
00361 }
00362 else{
00363 xsh_msg("Building nod set");
00364 if ( (argc - optind) >=2 ) {
00365 raw_sof_name = argv[optind];
00366 sof_name = argv[optind+1];
00367 }
00368 else {
00369 Help();
00370 exit(0);
00371 }
00372
00373 check( set = sof_to_frameset( sof_name));
00374
00375 check( instrument = xsh_dfs_set_groups( set));
00376
00377 check( spectralformat_frame = xsh_find_spectral_format( set, instrument));
00378 check( orderlist_frame = xsh_find_order_tab_edges( set, instrument));
00379
00380 check( raw_set = sof_to_frameset( raw_sof_name));
00381 check( mode = xsh_instrument_get_mode( instrument));
00382
00383
00384 check( nb_frames = cpl_frameset_get_size( raw_set));
00385
00386 for ( i = 0 ; i < nb_frames ; i++ ) {
00387 check( sci_frame = cpl_frameset_get_frame( raw_set, i));
00388 xsh_msg("SCI : %s",
00389 cpl_frame_get_filename( sci_frame));
00390 }
00391 xsh_msg("ORDERLIST : %s",
00392 cpl_frame_get_filename( orderlist_frame));
00393 xsh_msg("SPECTRALFORMAT : %s",
00394 cpl_frame_get_filename( spectralformat_frame));
00395
00396 if ( mode == XSH_MODE_SLIT){
00397 wavesol_frame = xsh_find_wave_tab_2d( set, instrument);
00398
00399 if ( wavesol_frame == NULL){
00400 model_frame = xsh_find_model_config_tab( set, instrument);
00401 }
00402
00403 if ( wavesol_frame != NULL){
00404 xsh_msg("WAVESOL : %s",
00405 cpl_frame_get_filename( wavesol_frame));
00406 }
00407 else {
00408 xsh_msg("MODEL : %s",
00409 cpl_frame_get_filename( model_frame));
00410 }
00411 }
00412
00413 xsh_msg(" Parameters ");
00414 xsh_msg(" kernel %s", rectify_par.rectif_kernel);
00415 xsh_msg(" radius %f", rectify_par.rectif_radius);
00416 xsh_msg(" bin-space %f", rectify_par.rectif_bin_space);
00417 xsh_msg(" bin-lambda %f", rectify_par.rectif_bin_lambda);
00418
00419 check( order_list = xsh_order_list_load ( orderlist_frame, instrument));
00420
00421 if ( order_min != -1) {
00422 check( rec_min_index = xsh_order_list_get_index_by_absorder( order_list,
00423 order_min));
00424 xsh_msg("Order min %d => index %d", order_min, rec_min_index);
00425 }
00426 else{
00427 rec_min_index = 0;
00428 }
00429
00430 if ( order_max != -1) {
00431 check( rec_max_index = xsh_order_list_get_index_by_absorder( order_list,
00432 order_max));
00433 xsh_msg("Order max %d => index %d", order_max, rec_max_index);
00434 }
00435 else{
00436 rec_max_index = order_list->size;
00437 }
00438
00439
00440
00441 check( xsh_rec_slit_size( &rectify_par,
00442 &slit_min_c, &nslit_c, mode));
00443
00444 if (slit_min == -100){
00445 slit_min = slit_min_c;
00446 }
00447 if ( nslit == -100){
00448 nslit = nslit_c;
00449 }
00450
00451 xsh_msg("SLIT min = %f and has size %d", slit_min, nslit);
00452
00453 tag = XSH_GET_TAG_FROM_ARM( XSH_ORDER2D, instrument);
00454
00455 check( rec_ab_frame = xsh_rectify_orders( sci_frame, order_list,
00456 wavesol_frame, model_frame, instrument, &rectify_par,
00457 spectralformat_frame, NULL, "REC_A_B.fits", tag,
00458 &rec_abeso_frame,&rec_abtab_frame,
00459 rec_min_index, rec_max_index, slit_min, nslit, 0));
00460
00461 check( rec_ba_frame = xsh_rec_list_frame_invert( rec_ab_frame, tag,
00462 instrument));
00463 nod_set = cpl_frameset_new();
00464 cpl_frameset_insert( nod_set, rec_ab_frame);
00465
00466 if ( use_loc_obj == 1){
00467 check( loc_ab_frame = xsh_localize_obj( rec_ab_frame, NULL,instrument,
00468 &loc_par, NULL, "LOC_AB.fits"));
00469 check( loc_ba_frame = xsh_localize_obj( rec_ba_frame, NULL,instrument,
00470 &loc_par, NULL, "LOC_BA.fits"));
00471 }
00472 else{
00473 check( rec_bashift_frame = shift_with_kw( rec_ba_frame, instrument,
00474 &rectify_par, "REC_SHIFT_B_A.fits", &rec_bashifteso_frame, &ref_ra,
00475 &ref_dec, 1));
00476
00477 xsh_msg("TEST REF RA DEC %f %f", *ref_ra, *ref_dec);
00478
00479 cpl_frameset_insert( nod_set, rec_bashift_frame);
00480 }
00481 }
00482
00483 zero_nod_set = cpl_frameset_new();
00484
00485
00486 for ( i = 0 ; i < nb_frames ; i++ ) {
00487 cpl_frame *new_frame = NULL;
00488
00489 check( sci_frame = cpl_frameset_get_frame( nod_set, i));
00490 check( new_frame = create_zero_rectify( sci_frame, inod, instrument));
00491 check( cpl_frameset_insert( zero_nod_set, new_frame));
00492 }
00493
00494 cnod_par.nod_clip = FALSE;
00495
00496 for ( i = 0 ; i < nb_frames ; i++ ) {
00497 cpl_frame *analyse_frame = NULL;
00498 cpl_frame *orig_frame = NULL;
00499 const char * orig_name = NULL;
00500 char tag[256];
00501
00502 combine_set = cpl_frameset_duplicate( zero_nod_set);
00503 check( analyse_frame = cpl_frameset_get_frame( combine_set, i));
00504 check( orig_frame = cpl_frameset_get_frame( nod_set, i));
00505 check( orig_name = cpl_frame_get_filename( orig_frame));
00506 check( cpl_frame_set_filename( analyse_frame, orig_name));
00507
00508 sprintf( tag, "COMBINE_NOD%d", i);
00509 check( combine_nod_frame = xsh_combine_nod( combine_set, &cnod_par,
00510 tag, instrument, &combine_nodeso_frame));
00511 check( analyse_rectify( combine_nod_frame, inod, instrument));
00512
00513 xsh_free_frameset( &combine_set);
00514 xsh_free_frame( &combine_nod_frame);
00515 xsh_free_frame( &combine_nodeso_frame);
00516 }
00517
00518 check( combine_nod_frame = xsh_combine_nod( nod_set, &cnod_par,
00519 "FULL_COMBINE_NOD", instrument, &combine_nodeso_frame));
00520 check( analyse_rectify( combine_nod_frame, inod, instrument));
00521
00522 cleanup:
00523 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00524 xsh_error_dump(CPL_MSG_ERROR);
00525 ret = 1;
00526 }
00527 xsh_order_list_free( &order_list);
00528 xsh_free_frameset( &raw_set);
00529 xsh_free_frameset( &set);
00530 xsh_instrument_free( &instrument);
00531 xsh_free_frameset( &nod_set);
00532 xsh_free_frameset( &zero_nod_set);
00533 xsh_free_frameset( &combine_set);
00534 xsh_free_frame( &rec_abeso_frame);
00535 xsh_free_frame( &rec_abtab_frame);
00536 xsh_free_frame( &rec_ba_frame);
00537 xsh_free_frame( &rec_bashifteso_frame);
00538 xsh_free_frame( &loc_ab_frame);
00539 xsh_free_frame( &loc_ba_frame);
00540 xsh_free_frame( &combine_nod_frame);
00541 xsh_free_frame( &combine_nodeso_frame);
00542 XSH_FREE( ref_ra);
00543 XSH_FREE( ref_dec);
00544 TEST_END();
00545 return ret;
00546 }