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 #include <xsh_utils_table.h>
00053
00054 #include <xsh_drl.h>
00055 #include <xsh_pfits.h>
00056
00057 #include <xsh_badpixelmap.h>
00058
00059 #include <cpl.h>
00060 #include <math.h>
00061
00062 #include <getopt.h>
00063
00064
00065
00066
00067
00068 #define MODULE_ID "XSH_GEOM_IFU"
00069
00070
00071
00072
00073 enum {
00074 KERNEL_OPT, RADIUS_OPT, BIN_LAMBDA_OPT, BIN_SPACE_OPT, HELP_OPT,
00075 MIN_ORDER_OPT, MAX_ORDER_OPT, SLIT_MIN_OPT, NSLIT_OPT, MERGE_METHOD_OPT,
00076 LAMBDA_REF_OPT
00077 } ;
00078
00079 static const char * Options = "" ;
00080
00081 static struct option long_options[] = {
00082 {"kernel", required_argument, 0, KERNEL_OPT},
00083 {"radius", required_argument, 0, RADIUS_OPT},
00084 {"bin-lambda", required_argument, 0, BIN_LAMBDA_OPT},
00085 {"bin-space", required_argument, 0, BIN_SPACE_OPT},
00086 {"order-min", required_argument, 0, MIN_ORDER_OPT},
00087 {"order-max", required_argument, 0, MAX_ORDER_OPT},
00088 {"slit-min",required_argument, 0, SLIT_MIN_OPT},
00089 {"slit-n",required_argument, 0, NSLIT_OPT},
00090 {"merge-method", required_argument, 0, MERGE_METHOD_OPT},
00091 {"lambda-ref", required_argument, 0, LAMBDA_REF_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_geom_ifu" ) ;
00099 puts( "Usage: test_xsh_geom_ifu [options] <input_files>" ) ;
00100 puts( "Options" ) ;
00101 puts( " --kernel=<name> : TANH, SINC, SINC2, LANCZOS, HAMMING, HANN [TANH]");
00102 puts( " --radius=<nn> : Radius [4]" ) ;
00103 puts( " --bin-lambda=<n> : Bin in Lambda [0.1]" ) ;
00104 puts( " --bin-space=<n> : Bin in Slit [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( " --lambda-ref=<n> : Lambda reference for creating SLICE OFFSET");
00110 puts( " --help : What you see" ) ;
00111 puts( "\nInput Files" ) ;
00112 puts( "The input files argument MUST be in this order:" ) ;
00113 puts( " 1. Science frame in PRE format" ) ;
00114 puts( " 2. SOF\n" ) ;
00115 TEST_END();
00116 }
00117
00118 static void HandleOptions( int argc, char **argv,
00119 xsh_rectify_param *rectify_par, xsh_localize_obj_param *locobj_par,
00120 int *order_min, int *order_max,
00121 double *slit_min, int *nslit, double *lambda_ref)
00122 {
00123 int opt ;
00124 int option_index = 0;
00125
00126 while (( opt = getopt_long (argc, argv, Options,
00127 long_options, &option_index)) != EOF )
00128 switch ( opt ) {
00129 case KERNEL_OPT:
00130 strcpy( rectify_par->rectif_kernel, optarg);
00131 if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_TANH)) == 0){
00132 rectify_par->kernel_type = CPL_KERNEL_TANH;
00133 }
00134 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_TANH)) == 0){
00135 rectify_par->kernel_type = CPL_KERNEL_TANH;
00136 }
00137 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_SINC)) == 0){
00138 rectify_par->kernel_type = CPL_KERNEL_SINC;
00139 }
00140 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_SINC2)) == 0){
00141 rectify_par->kernel_type = CPL_KERNEL_SINC2;
00142 }
00143 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_LANCZOS)) == 0){
00144 rectify_par->kernel_type = CPL_KERNEL_LANCZOS;
00145 }
00146 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_HAMMING)) == 0){
00147 rectify_par->kernel_type = CPL_KERNEL_HAMMING;
00148 }
00149 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_HANN)) == 0){
00150 rectify_par->kernel_type = CPL_KERNEL_HANN;
00151 }
00152 else{
00153 xsh_msg("Invalid kernel option %s", optarg);
00154 Help();
00155 exit(0);
00156 }
00157 break;
00158 case RADIUS_OPT:
00159 rectify_par->rectif_radius = atof( optarg);
00160 break ;
00161 case BIN_LAMBDA_OPT:
00162 sscanf( optarg, "%lf", &rectify_par->rectif_bin_lambda ) ;
00163 break ;
00164 case BIN_SPACE_OPT:
00165 sscanf( optarg, "%lf", &rectify_par->rectif_bin_space ) ;
00166 break ;
00167 case MIN_ORDER_OPT:
00168 sscanf( optarg, "%d", order_min);
00169 break;
00170 case MAX_ORDER_OPT:
00171 sscanf( optarg, "%d", order_max);
00172 break;
00173 case SLIT_MIN_OPT:
00174 sscanf( optarg, "%lf", slit_min) ;
00175 break ;
00176 case LAMBDA_REF_OPT:
00177 sscanf( optarg, "%lf", lambda_ref);
00178 break ;
00179 case NSLIT_OPT:
00180 sscanf( optarg, "%d", nslit);
00181 break ;
00182 default: Help() ; exit( 0 ) ;
00183 }
00184 }
00185
00194 int main( int argc, char **argv)
00195 {
00196
00197 int ret = 0 ;
00198
00199 xsh_instrument* instrument = NULL;
00200
00201 const char *sof_name = NULL;
00202 cpl_frameset *set = NULL;
00203 const char * sci_name = NULL ;
00204 xsh_localize_obj_param loc_obj_par = {10, 0.1, 1, 0, LOC_GAUSSIAN_METHOD, 0, 0.5, 2.0, 3, FALSE};;
00205
00206 cpl_frameset *wavesol_frameset = NULL;
00207 cpl_frameset *rect_frameset = NULL;
00208 cpl_frameset *loc_frameset = NULL;
00209 cpl_frame *sci_frame = NULL;
00210 cpl_frame *orderlist_frame = NULL;
00211 cpl_frame *slitmap_frame = NULL;
00212 cpl_frame *model_frame = NULL;
00213 cpl_frame *spectralformat_frame = NULL;
00214 cpl_frame *recorder_frame = NULL ;
00215 cpl_frame *recordereso_frame = NULL ;
00216 cpl_frame *recordertab_frame = NULL ;
00217 cpl_frame *cube = NULL;
00218 int order_min = -1, order_max = -1;
00219 int rec_min_index=-1, rec_max_index=-1;
00220 double lambda_ref = -1;
00221 xsh_order_list* order_list = NULL;
00222 xsh_rectify_param rectify_par;
00223 int nslit_c, nslit=-100;
00224 double slit_min_c, slit_min=-100;
00225 XSH_MODE mode;
00226 const int decode_bp=2147483647;
00227
00228 TESTS_INIT(MODULE_ID);
00229 cpl_msg_set_level(CPL_MSG_DEBUG);
00230 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM);
00231
00232
00233 strcpy( rectify_par.rectif_kernel, "default");
00234 rectify_par.kernel_type = CPL_KERNEL_TANH;
00235 rectify_par.rectif_radius = 4 ;
00236 rectify_par.rectif_bin_lambda = 0.1 ;
00237 rectify_par.rectif_bin_space = 0.1 ;
00238 rectify_par.conserve_flux = FALSE;
00239
00240 loc_obj_par.loc_deg_poly = 0;
00241
00242 HandleOptions( argc, argv, &rectify_par, &loc_obj_par, &order_min, &order_max,
00243 &slit_min, &nslit, &lambda_ref);
00244
00245 if ( (argc - optind) >=2 ) {
00246 sci_name = argv[optind];
00247 sof_name = argv[optind+1];
00248 }
00249 else {
00250 Help();
00251 exit(0);
00252 }
00253
00254 check( set = sof_to_frameset( sof_name));
00255
00256 check( instrument = xsh_dfs_set_groups( set));
00257 xsh_instrument_set_decode_bp( instrument, decode_bp ) ;
00258
00259 check( spectralformat_frame = xsh_find_spectral_format( set, instrument));
00260 check( orderlist_frame = xsh_find_order_tab_edges( set, instrument));
00261 check( slitmap_frame = xsh_find_slitmap( set, instrument));
00262
00263 TESTS_XSH_FRAME_CREATE( sci_frame, "SLIT_STARE_DIVIDED_arm", sci_name);
00264 check( mode = xsh_instrument_get_mode( instrument));
00265
00266 xsh_msg("SCI : %s",
00267 cpl_frame_get_filename( sci_frame));
00268 xsh_msg("ORDERLIST : %s",
00269 cpl_frame_get_filename( orderlist_frame));
00270 xsh_msg("SPECTRALFORMAT : %s",
00271 cpl_frame_get_filename( spectralformat_frame));
00272 if ( slitmap_frame != NULL){
00273 xsh_msg("SLITMAP : %s",
00274 cpl_frame_get_filename( slitmap_frame));
00275 }
00276
00277 wavesol_frameset = xsh_find_wave_tab_ifu( set, instrument);
00278 cpl_error_reset();
00279 if ( wavesol_frameset == NULL){
00280 model_frame = xsh_find_model_config_tab( set, instrument);
00281 xsh_msg("MODEL : %s",
00282 cpl_frame_get_filename( model_frame));
00283 }
00284 else{
00285 int iframe;
00286 int size;
00287
00288 size = cpl_frameset_get_size( wavesol_frameset);
00289 for(iframe=0; iframe < size; iframe++){
00290 cpl_frame *frame = cpl_frameset_get_frame(wavesol_frameset, iframe);
00291 xsh_msg("WAVESOL : %s",
00292 cpl_frame_get_filename( frame));
00293 }
00294 }
00295
00296 xsh_msg(" Parameters ");
00297 xsh_msg(" kernel %s", RECTIFY_KERNEL_PRINT( rectify_par.kernel_type));
00298 xsh_msg(" radius %f", rectify_par.rectif_radius);
00299 xsh_msg(" bin-space %f", rectify_par.rectif_bin_space);
00300 xsh_msg(" bin-lambda %f", rectify_par.rectif_bin_lambda);
00301
00302
00303 check( order_list = xsh_order_list_load ( orderlist_frame, instrument));
00304
00305 if ( order_min != -1) {
00306 check( rec_min_index = xsh_order_list_get_index_by_absorder( order_list,
00307 order_min));
00308 xsh_msg("Order min %d => index %d", order_min, rec_min_index);
00309 }
00310 else{
00311 rec_min_index = 0;
00312 }
00313
00314 if ( order_max != -1) {
00315 check( rec_max_index = xsh_order_list_get_index_by_absorder( order_list,
00316 order_max));
00317 xsh_msg("Order max %d => index %d", order_max, rec_max_index);
00318 }
00319 else{
00320 rec_max_index = order_list->size;
00321 }
00322
00323
00324
00325
00326 check( xsh_rec_slit_size( &rectify_par,
00327 &slit_min_c, &nslit_c, mode));
00328
00329 if (slit_min == -100){
00330 slit_min = slit_min_c;
00331 }
00332 if ( nslit == -100){
00333 nslit = nslit_c;
00334 }
00335
00336 xsh_msg("SLIT min = %f and has size %d", slit_min, nslit);
00337 xsh_msg("Lambda ref %f", lambda_ref);
00338 if (lambda_ref < 0.0){
00339 check( rect_frameset = xsh_rectify_orders_ifu( sci_frame, order_list,
00340 wavesol_frameset, NULL, model_frame, instrument, &rectify_par,
00341 spectralformat_frame, slitmap_frame,NULL,NULL,rec_min_index,
00342 rec_max_index, "test"));
00343 }
00344 else{
00345 double lambda_ref_min;
00346 double lambda_ref_max;
00347 const char * tablename = NULL;
00348 cpl_table *table = NULL;
00349 cpl_frame *spectralformat2_frame = NULL;
00350 xsh_spectralformat_list* list = NULL;
00351 cpl_vector* orders = NULL;
00352 int size, absorder, iorder;
00353
00354 lambda_ref_min = lambda_ref-rectify_par.rectif_bin_lambda*10;
00355 lambda_ref_max = lambda_ref+rectify_par.rectif_bin_lambda*10;
00356
00357 xsh_msg( "Do cube for reference %f ==> %f", lambda_ref_min, lambda_ref_max);
00358 check( list = xsh_spectralformat_list_load( spectralformat_frame,
00359 instrument));
00360 check( orders = xsh_spectralformat_list_get_orders( list, lambda_ref));
00361 size = cpl_vector_get_size( orders);
00362
00363 tablename = cpl_frame_get_filename( spectralformat_frame);
00364 XSH_TABLE_LOAD( table, tablename);
00365
00366 for( iorder=0; iorder < size; iorder++){
00367 absorder = cpl_vector_get( orders, iorder);
00368 xsh_msg("In Order %d", absorder);
00369 cpl_table_set_int(table, XSH_SPECTRALFORMAT_TABLE_COLNAME_ORDER, iorder, absorder);
00370 cpl_table_set_float(table, XSH_SPECTRALFORMAT_TABLE_COLNAME_WLMIN, iorder, lambda_ref_min);
00371 cpl_table_set_float(table, XSH_SPECTRALFORMAT_TABLE_COLNAME_WLMAX, iorder, lambda_ref_max);
00372 }
00373 cpl_table_save( table, NULL,NULL,"LAMBDA_REF_FORMAT.fits",CPL_IO_DEFAULT);
00374 TESTS_XSH_FRAME_CREATE( spectralformat2_frame, "SPEC_arm", "LAMBDA_REF_FORMAT.fits");
00375
00376 check( rect_frameset = xsh_rectify_orders_ifu( sci_frame, order_list,
00377 wavesol_frameset, NULL, model_frame, instrument, &rectify_par,
00378 spectralformat2_frame, slitmap_frame,NULL,NULL, 0,
00379 size-1, "test"));
00380 }
00381
00382 xsh_msg( "---Localize the objects for IFU");
00383 check( loc_frameset = xsh_localize_obj_ifu( rect_frameset,
00384 NULL, instrument, &loc_obj_par, NULL));
00385
00386 cleanup:
00387 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00388 xsh_error_dump(CPL_MSG_ERROR);
00389 ret = 1;
00390 }
00391 xsh_order_list_free( &order_list);
00392 xsh_free_frame( &sci_frame);
00393 xsh_free_frameset( &rect_frameset);
00394 xsh_free_frameset( &loc_frameset);
00395 xsh_free_frameset( &wavesol_frameset);
00396 xsh_free_frameset( &set);
00397 xsh_instrument_free( &instrument);
00398 xsh_free_frame( &recorder_frame);
00399 xsh_free_frame( &recordereso_frame);
00400 xsh_free_frame( &recordertab_frame);
00401 xsh_free_frame( &cube);
00402 TEST_END();
00403 return ret;
00404 }