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] = val;
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
00287 const char * raw_sof_name = NULL ;
00288 cpl_frameset *raw_set = NULL;
00289
00290 cpl_frame *sci_frame = NULL;
00291 cpl_frame *orderlist_frame = NULL;
00292 cpl_frame *wavesol_frame = NULL;
00293 cpl_frame *model_frame = NULL;
00294 cpl_frame *spectralformat_frame = NULL;
00295 cpl_frame *rec_ab_frame = NULL ;
00296 cpl_frame *rec_abeso_frame = NULL ;
00297 cpl_frame *rec_abtab_frame = NULL ;
00298 cpl_frame *rec_ba_frame = NULL ;
00299 cpl_frame *rec_bashift_frame = NULL ;
00300 cpl_frame *rec_bashifteso_frame = NULL ;
00301 cpl_frame *loc_ab_frame = NULL;
00302 cpl_frame *loc_ba_frame = NULL;
00303 cpl_frameset *nod_set = NULL;
00304 cpl_frameset *zero_nod_set = NULL;
00305 cpl_frameset *combine_set = NULL;
00306
00307 cpl_frame *combine_nod_frame = NULL;
00308 cpl_frame *combine_nodeso_frame = NULL;
00309
00310 int order_min = -1, order_max = -1;
00311 int inod = 0;
00312 int rec_min_index=-1, rec_max_index=-1;
00313 xsh_order_list* order_list = NULL;
00314 xsh_rectify_param rectify_par;
00315 xsh_localize_obj_param loc_par;
00316 xsh_combine_nod_param cnod_par;
00317 int nslit_c, nslit=-100;
00318 int use_loc_obj = 0;
00319 double slit_min_c, slit_min=-100;
00320 XSH_MODE mode;
00321 const char *tag = NULL;
00322 int i, nb_frames;
00323 double *ref_ra = NULL, *ref_dec = NULL;
00324 const int decode_bp=2147483647;
00325
00326 TESTS_INIT(MODULE_ID);
00327 cpl_msg_set_level(CPL_MSG_DEBUG);
00328 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM);
00329
00330
00331 strcpy( rectify_par.rectif_kernel, "default");
00332 rectify_par.kernel_type = 0 ;
00333 rectify_par.rectif_radius = 4 ;
00334 rectify_par.rectif_bin_lambda = 0.1 ;
00335 rectify_par.rectif_bin_space = 0.1 ;
00336 rectify_par.conserve_flux = FALSE;
00337
00338 loc_par.method = LOC_MAXIMUM_METHOD;
00339 loc_par.loc_chunk_nb = 10;
00340 loc_par.loc_deg_poly = 0;
00341 loc_par.loc_thresh = 0.1;
00342
00343 HandleOptions( argc, argv, &rectify_par, &order_min, &order_max,
00344 &slit_min, &nslit, &use_loc_obj, &sof_name);
00345
00346 if ( sof_name != NULL){
00347 xsh_msg("Using nod set : %s", sof_name);
00348 check( nod_set = sof_to_frameset( sof_name));
00349 check( instrument = xsh_dfs_set_groups( nod_set));
00350 xsh_instrument_set_decode_bp( instrument, decode_bp ) ;
00351 check( nb_frames = cpl_frameset_get_size( nod_set));
00352
00353 for ( i = 0 ; i < nb_frames ; i++ ) {
00354 check( sci_frame = cpl_frameset_get_frame( nod_set, i));
00355 xsh_msg("NOD : %s", cpl_frame_get_filename( sci_frame));
00356 check( analyse_rectify( sci_frame, inod, instrument));
00357 }
00358
00359 }
00360 else{
00361 xsh_msg("Building nod set");
00362 if ( (argc - optind) >=2 ) {
00363 raw_sof_name = argv[optind];
00364 sof_name = argv[optind+1];
00365 }
00366 else {
00367 Help();
00368 exit(0);
00369 }
00370
00371 check( set = sof_to_frameset( sof_name));
00372
00373 check( instrument = xsh_dfs_set_groups( set));
00374 xsh_instrument_set_decode_bp( instrument, decode_bp ) ;
00375 check( spectralformat_frame = xsh_find_spectral_format( set, instrument));
00376 check( orderlist_frame = xsh_find_order_tab_edges( set, instrument));
00377
00378 check( raw_set = sof_to_frameset( raw_sof_name));
00379 check( mode = xsh_instrument_get_mode( instrument));
00380
00381
00382 check( nb_frames = cpl_frameset_get_size( raw_set));
00383
00384 for ( i = 0 ; i < nb_frames ; i++ ) {
00385 check( sci_frame = cpl_frameset_get_frame( raw_set, i));
00386 xsh_msg("SCI : %s",
00387 cpl_frame_get_filename( sci_frame));
00388 }
00389 xsh_msg("ORDERLIST : %s",
00390 cpl_frame_get_filename( orderlist_frame));
00391 xsh_msg("SPECTRALFORMAT : %s",
00392 cpl_frame_get_filename( spectralformat_frame));
00393
00394 if ( mode == XSH_MODE_SLIT){
00395 wavesol_frame = xsh_find_wave_tab_2d( set, instrument);
00396
00397 if ( wavesol_frame == NULL){
00398 model_frame = xsh_find_model_config_tab( set, instrument);
00399 }
00400
00401 if ( wavesol_frame != NULL){
00402 xsh_msg("WAVESOL : %s",
00403 cpl_frame_get_filename( wavesol_frame));
00404 }
00405 else {
00406 xsh_msg("MODEL : %s",
00407 cpl_frame_get_filename( model_frame));
00408 }
00409 }
00410
00411 xsh_msg(" Parameters ");
00412 xsh_msg(" kernel %s", rectify_par.rectif_kernel);
00413 xsh_msg(" radius %f", rectify_par.rectif_radius);
00414 xsh_msg(" bin-space %f", rectify_par.rectif_bin_space);
00415 xsh_msg(" bin-lambda %f", rectify_par.rectif_bin_lambda);
00416
00417 check( order_list = xsh_order_list_load ( orderlist_frame, instrument));
00418
00419 if ( order_min != -1) {
00420 check( rec_min_index = xsh_order_list_get_index_by_absorder( order_list,
00421 order_min));
00422 xsh_msg("Order min %d => index %d", order_min, rec_min_index);
00423 }
00424 else{
00425 rec_min_index = 0;
00426 }
00427
00428 if ( order_max != -1) {
00429 check( rec_max_index = xsh_order_list_get_index_by_absorder( order_list,
00430 order_max));
00431 xsh_msg("Order max %d => index %d", order_max, rec_max_index);
00432 }
00433 else{
00434 rec_max_index = order_list->size;
00435 }
00436
00437
00438
00439 check( xsh_rec_slit_size( &rectify_par,
00440 &slit_min_c, &nslit_c, mode));
00441
00442 if (slit_min == -100){
00443 slit_min = slit_min_c;
00444 }
00445 if ( nslit == -100){
00446 nslit = nslit_c;
00447 }
00448
00449 xsh_msg("SLIT min = %f and has size %d", slit_min, nslit);
00450
00451 tag = XSH_GET_TAG_FROM_ARM( XSH_ORDER2D, instrument);
00452
00453 check( rec_ab_frame = xsh_rectify_orders( sci_frame, order_list,
00454 wavesol_frame, model_frame, instrument, &rectify_par,
00455 spectralformat_frame, NULL, "REC_A_B.fits", tag,
00456 &rec_abeso_frame,&rec_abtab_frame,
00457 rec_min_index, rec_max_index, slit_min, nslit, 0,NULL));
00458
00459 check( rec_ba_frame = xsh_rec_list_frame_invert( rec_ab_frame, tag,
00460 instrument));
00461 nod_set = cpl_frameset_new();
00462 cpl_frameset_insert( nod_set, rec_ab_frame);
00463
00464 if ( use_loc_obj == 1){
00465 check( loc_ab_frame = xsh_localize_obj( rec_ab_frame, NULL,instrument,
00466 &loc_par, NULL, "LOC_AB.fits"));
00467 check( loc_ba_frame = xsh_localize_obj( rec_ba_frame, NULL,instrument,
00468 &loc_par, NULL, "LOC_BA.fits"));
00469 }
00470 else{
00471 check( rec_bashift_frame = shift_with_kw( rec_ba_frame, instrument,
00472 &rectify_par, "REC_SHIFT_B_A.fits", &rec_bashifteso_frame, &ref_ra,
00473 &ref_dec, 1));
00474
00475 xsh_msg("TEST REF RA DEC %f %f", *ref_ra, *ref_dec);
00476
00477 cpl_frameset_insert( nod_set, rec_bashift_frame);
00478 }
00479 }
00480
00481 zero_nod_set = cpl_frameset_new();
00482
00483
00484 for ( i = 0 ; i < nb_frames ; i++ ) {
00485 cpl_frame *new_frame = NULL;
00486
00487 check( sci_frame = cpl_frameset_get_frame( nod_set, i));
00488 check( new_frame = create_zero_rectify( sci_frame, inod, instrument));
00489 check( cpl_frameset_insert( zero_nod_set, new_frame));
00490 }
00491
00492 cnod_par.nod_clip = FALSE;
00493
00494 for ( i = 0 ; i < nb_frames ; i++ ) {
00495 cpl_frame *analyse_frame = NULL;
00496 cpl_frame *orig_frame = NULL;
00497 const char * orig_name = NULL;
00498 char tag[256];
00499
00500 combine_set = cpl_frameset_duplicate( zero_nod_set);
00501 check( analyse_frame = cpl_frameset_get_frame( combine_set, i));
00502 check( orig_frame = cpl_frameset_get_frame( nod_set, i));
00503 check( orig_name = cpl_frame_get_filename( orig_frame));
00504 check( cpl_frame_set_filename( analyse_frame, orig_name));
00505
00506 sprintf( tag, "COMBINE_NOD%d", i);
00507 check( combine_nod_frame = xsh_combine_nod( combine_set, &cnod_par,
00508 tag, instrument, &combine_nodeso_frame));
00509 check( analyse_rectify( combine_nod_frame, inod, instrument));
00510
00511 xsh_free_frameset( &combine_set);
00512 xsh_free_frame( &combine_nod_frame);
00513 xsh_free_frame( &combine_nodeso_frame);
00514 }
00515
00516 check( combine_nod_frame = xsh_combine_nod( nod_set, &cnod_par,
00517 "FULL_COMBINE_NOD", instrument,&combine_nodeso_frame));
00518 check( analyse_rectify( combine_nod_frame, inod, instrument));
00519
00520 cleanup:
00521 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00522 xsh_error_dump(CPL_MSG_ERROR);
00523 ret = 1;
00524 }
00525 xsh_order_list_free( &order_list);
00526 xsh_free_frameset( &raw_set);
00527 xsh_free_frameset( &set);
00528 xsh_instrument_free( &instrument);
00529 xsh_free_frameset( &nod_set);
00530 xsh_free_frameset( &zero_nod_set);
00531 xsh_free_frameset( &combine_set);
00532 xsh_free_frame( &rec_abeso_frame);
00533 xsh_free_frame( &rec_abtab_frame);
00534 xsh_free_frame( &rec_ba_frame);
00535 xsh_free_frame( &rec_bashifteso_frame);
00536 xsh_free_frame( &loc_ab_frame);
00537 xsh_free_frame( &loc_ba_frame);
00538 xsh_free_frame( &combine_nod_frame);
00539 xsh_free_frame( &combine_nodeso_frame);
00540 XSH_FREE( ref_ra);
00541 XSH_FREE( ref_dec);
00542 TEST_END();
00543 return ret;
00544 }