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 cpl_frame *sliceoffset_frame = NULL;
00204
00205 const char * sci_name = NULL ;
00206 xsh_localize_obj_param loc_obj_par = {10, 0.1, 1, 0, LOC_GAUSSIAN_METHOD, 0, 0.5, 2.0, 3};;
00207
00208 cpl_frameset *wavesol_frameset = NULL;
00209 cpl_frameset *rect_frameset = NULL;
00210 cpl_frameset *loc_frameset = NULL;
00211 cpl_frame *sci_frame = NULL;
00212 cpl_frame *orderlist_frame = NULL;
00213 cpl_frame *slitmap_frame = NULL;
00214 cpl_frame *wavesol_frame = NULL;
00215 cpl_frame *model_frame = NULL;
00216 cpl_frame *spectralformat_frame = NULL;
00217 cpl_frame *recorder_frame = NULL ;
00218 cpl_frame *recordereso_frame = NULL ;
00219 cpl_frame *recordertab_frame = NULL ;
00220 cpl_frame *cube = NULL;
00221 int order_min = -1, order_max = -1;
00222 int rec_min_index=-1, rec_max_index=-1;
00223 int slitlet;
00224 double lambda_ref = -1;
00225 xsh_order_list* order_list = NULL;
00226 xsh_slice_offset *sliceoffset = NULL;
00227 xsh_rectify_param rectify_par;
00228 int nslit_c, nslit=-100;
00229 double slit_min_c, slit_min=-100;
00230 XSH_MODE mode;
00231
00232
00233 TESTS_INIT(MODULE_ID);
00234 cpl_msg_set_level(CPL_MSG_DEBUG);
00235 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM);
00236
00237
00238 strcpy( rectify_par.rectif_kernel, "default");
00239 rectify_par.kernel_type = CPL_KERNEL_TANH;
00240 rectify_par.rectif_radius = 4 ;
00241 rectify_par.rectif_bin_lambda = 0.1 ;
00242 rectify_par.rectif_bin_space = 0.1 ;
00243 rectify_par.conserve_flux = FALSE;
00244
00245 loc_obj_par.loc_deg_poly = 0;
00246
00247 HandleOptions( argc, argv, &rectify_par, &loc_obj_par, &order_min, &order_max,
00248 &slit_min, &nslit, &lambda_ref);
00249
00250 if ( (argc - optind) >=2 ) {
00251 sci_name = argv[optind];
00252 sof_name = argv[optind+1];
00253 }
00254 else {
00255 Help();
00256 exit(0);
00257 }
00258
00259 check( set = sof_to_frameset( sof_name));
00260
00261 check( instrument = xsh_dfs_set_groups( set));
00262
00263 check( spectralformat_frame = xsh_find_spectral_format( set, instrument));
00264 check( orderlist_frame = xsh_find_order_tab_edges( set, instrument));
00265 check( slitmap_frame = xsh_find_slitmap( set, instrument));
00266
00267 TESTS_XSH_FRAME_CREATE( sci_frame, "SLIT_STARE_DIVIDED_arm", sci_name);
00268 check( mode = xsh_instrument_get_mode( instrument));
00269
00270 xsh_msg("SCI : %s",
00271 cpl_frame_get_filename( sci_frame));
00272 xsh_msg("ORDERLIST : %s",
00273 cpl_frame_get_filename( orderlist_frame));
00274 xsh_msg("SPECTRALFORMAT : %s",
00275 cpl_frame_get_filename( spectralformat_frame));
00276 if ( slitmap_frame != NULL){
00277 xsh_msg("SLITMAP : %s",
00278 cpl_frame_get_filename( slitmap_frame));
00279 }
00280
00281 wavesol_frameset = xsh_find_wave_tab_ifu( set, instrument);
00282 cpl_error_reset();
00283 if ( wavesol_frameset == NULL){
00284 model_frame = xsh_find_model_config_tab( set, instrument);
00285 xsh_msg("MODEL : %s",
00286 cpl_frame_get_filename( model_frame));
00287 }
00288 else{
00289 int iframe;
00290 int size;
00291
00292 size = cpl_frameset_get_size( wavesol_frameset);
00293 for(iframe=0; iframe < size; iframe++){
00294 cpl_frame *frame = cpl_frameset_get_frame(wavesol_frameset, iframe);
00295 xsh_msg("WAVESOL : %s",
00296 cpl_frame_get_filename( frame));
00297 }
00298 }
00299
00300 xsh_msg(" Parameters ");
00301 xsh_msg(" kernel %s", RECTIFY_KERNEL_PRINT( rectify_par.kernel_type));
00302 xsh_msg(" radius %f", rectify_par.rectif_radius);
00303 xsh_msg(" bin-space %f", rectify_par.rectif_bin_space);
00304 xsh_msg(" bin-lambda %f", rectify_par.rectif_bin_lambda);
00305
00306
00307 check( order_list = xsh_order_list_load ( orderlist_frame, instrument));
00308
00309 if ( order_min != -1) {
00310 check( rec_min_index = xsh_order_list_get_index_by_absorder( order_list,
00311 order_min));
00312 xsh_msg("Order min %d => index %d", order_min, rec_min_index);
00313 }
00314 else{
00315 rec_min_index = 0;
00316 }
00317
00318 if ( order_max != -1) {
00319 check( rec_max_index = xsh_order_list_get_index_by_absorder( order_list,
00320 order_max));
00321 xsh_msg("Order max %d => index %d", order_max, rec_max_index);
00322 }
00323 else{
00324 rec_max_index = order_list->size;
00325 }
00326
00327
00328
00329
00330 check( xsh_rec_slit_size( &rectify_par,
00331 &slit_min_c, &nslit_c, mode));
00332
00333 if (slit_min == -100){
00334 slit_min = slit_min_c;
00335 }
00336 if ( nslit == -100){
00337 nslit = nslit_c;
00338 }
00339
00340 xsh_msg("SLIT min = %f and has size %d", slit_min, nslit);
00341 xsh_msg("Lambda ref %f", lambda_ref);
00342 if (lambda_ref < 0.0){
00343 check( rect_frameset = xsh_rectify_orders_ifu( sci_frame, order_list,
00344 wavesol_frameset, model_frame, instrument, &rectify_par,
00345 spectralformat_frame, NULL, slitmap_frame,NULL,NULL,rec_min_index,
00346 rec_max_index, "test"));
00347 }
00348 else{
00349 double lambda_ref_min;
00350 double lambda_ref_max;
00351 const char * tablename = NULL;
00352 cpl_table *table = NULL;
00353 cpl_frame *spectralformat2_frame = NULL;
00354 xsh_spectralformat_list* list = NULL;
00355 cpl_vector* orders = NULL;
00356 int size, absorder, iorder;
00357
00358 lambda_ref_min = lambda_ref-rectify_par.rectif_bin_lambda*10;
00359 lambda_ref_max = lambda_ref+rectify_par.rectif_bin_lambda*10;
00360
00361 xsh_msg( "Do cube for reference %f ==> %f", lambda_ref_min, lambda_ref_max);
00362 check( list = xsh_spectralformat_list_load( spectralformat_frame,
00363 instrument));
00364 check( orders = xsh_spectralformat_list_get_orders( list, lambda_ref));
00365 size = cpl_vector_get_size( orders);
00366
00367 tablename = cpl_frame_get_filename( spectralformat_frame);
00368 XSH_TABLE_LOAD( table, tablename);
00369
00370 for( iorder=0; iorder < size; iorder++){
00371 absorder = cpl_vector_get( orders, iorder);
00372 xsh_msg("In Order %d", absorder);
00373 cpl_table_set_int(table, XSH_SPECTRALFORMAT_TABLE_COLNAME_ORDER, iorder, absorder);
00374 cpl_table_set_float(table, XSH_SPECTRALFORMAT_TABLE_COLNAME_WLMIN, iorder, lambda_ref_min);
00375 cpl_table_set_float(table, XSH_SPECTRALFORMAT_TABLE_COLNAME_WLMAX, iorder, lambda_ref_max);
00376 }
00377 cpl_table_save( table, NULL,NULL,"LAMBDA_REF_FORMAT.fits",CPL_IO_DEFAULT);
00378 TESTS_XSH_FRAME_CREATE( spectralformat2_frame, "SPEC_arm", "LAMBDA_REF_FORMAT.fits");
00379
00380 check( rect_frameset = xsh_rectify_orders_ifu( sci_frame, order_list,
00381 wavesol_frameset, model_frame, instrument, &rectify_par,
00382 spectralformat2_frame, NULL, slitmap_frame,NULL,NULL, 0,
00383 size-1, "test"));
00384 }
00385
00386 xsh_msg( "---Localize the objects for IFU");
00387 check( loc_frameset = xsh_localize_obj_ifu( rect_frameset,
00388 NULL, instrument, &loc_obj_par, NULL));
00389
00390
00391 xsh_msg( "---Compute slice offset");
00392 check( sliceoffset_frame = xsh_compute_slice_dist( loc_frameset,
00393 orderlist_frame, slitmap_frame, rect_frameset, lambda_ref,
00394 instrument));
00395
00396 cleanup:
00397 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00398 xsh_error_dump(CPL_MSG_ERROR);
00399 ret = 1;
00400 }
00401 xsh_order_list_free( &order_list);
00402 xsh_free_frame( &sci_frame);
00403 xsh_free_frameset( &rect_frameset);
00404 xsh_free_frameset( &loc_frameset);
00405 xsh_free_frameset( &wavesol_frameset);
00406 xsh_free_frameset( &set);
00407 xsh_instrument_free( &instrument);
00408 xsh_free_frame( &recorder_frame);
00409 xsh_free_frame( &recordereso_frame);
00410 xsh_free_frame( &recordertab_frame);
00411 xsh_free_frame( &cube);
00412 TEST_END();
00413 return ret;
00414 }