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_DATA_CUBE"
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_data_cube" ) ;
00099 puts( "Usage: test_xsh_data_cube [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( " --merge-method : WEIGHT or MEAN [MEAN]");
00110 puts( " --lambda-ref=<n> : Lambda reference for SLICE OFFSET");
00111 puts( " --help : What you see" ) ;
00112 puts( "\nInput Files" ) ;
00113 puts( "The input files argument MUST be in this order:" ) ;
00114 puts( " 1. Science frame in PRE format" ) ;
00115 puts( " 2. SOF\n" ) ;
00116 TEST_END();
00117 }
00118
00119 static void HandleOptions( int argc, char **argv,
00120 xsh_rectify_param *rectify_par, xsh_merge_param *merge_par,
00121 int *order_min, int *order_max,
00122 double *slit_min, int *nslit, double *lambda_ref)
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 if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_TANH)) == 0){
00133 rectify_par->kernel_type = CPL_KERNEL_TANH;
00134 }
00135 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_TANH)) == 0){
00136 rectify_par->kernel_type = CPL_KERNEL_TANH;
00137 }
00138 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_SINC)) == 0){
00139 rectify_par->kernel_type = CPL_KERNEL_SINC;
00140 }
00141 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_SINC2)) == 0){
00142 rectify_par->kernel_type = CPL_KERNEL_SINC2;
00143 }
00144 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_LANCZOS)) == 0){
00145 rectify_par->kernel_type = CPL_KERNEL_LANCZOS;
00146 }
00147 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_HAMMING)) == 0){
00148 rectify_par->kernel_type = CPL_KERNEL_HAMMING;
00149 }
00150 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_HANN)) == 0){
00151 rectify_par->kernel_type = CPL_KERNEL_HANN;
00152 }
00153 else{
00154 xsh_msg("Invalid kernel option %s", optarg);
00155 Help();
00156 exit(0);
00157 }
00158 break;
00159 case RADIUS_OPT:
00160 rectify_par->rectif_radius = atof( optarg);
00161 break ;
00162 case BIN_LAMBDA_OPT:
00163 sscanf( optarg, "%lf", &rectify_par->rectif_bin_lambda ) ;
00164 break ;
00165 case BIN_SPACE_OPT:
00166 sscanf( optarg, "%lf", &rectify_par->rectif_bin_space ) ;
00167 break ;
00168 case MIN_ORDER_OPT:
00169 sscanf( optarg, "%d", order_min);
00170 break;
00171 case MAX_ORDER_OPT:
00172 sscanf( optarg, "%d", order_max);
00173 break;
00174 case SLIT_MIN_OPT:
00175 sscanf( optarg, "%lf", slit_min) ;
00176 break ;
00177 case LAMBDA_REF_OPT:
00178 sscanf( optarg, "%lf", lambda_ref);
00179 break ;
00180 case NSLIT_OPT:
00181 sscanf( optarg, "%d", nslit);
00182 break ;
00183 case MERGE_METHOD_OPT:
00184 if ( strcmp(optarg, MERGE_METHOD_PRINT( MEAN_MERGE_METHOD)) == 0){
00185 merge_par->method = MEAN_MERGE_METHOD;
00186 }
00187 else if ( strcmp(optarg, MERGE_METHOD_PRINT( WEIGHTED_MERGE_METHOD)) == 0){
00188 merge_par->method = WEIGHTED_MERGE_METHOD;
00189 }
00190 else{
00191 xsh_msg("WRONG method %s", optarg);
00192 exit(-1);
00193 }
00194 break;
00195 default: Help() ; exit( 0 ) ;
00196 }
00197 }
00198
00207 int main( int argc, char **argv)
00208 {
00209
00210 int ret = 0 ;
00211
00212 xsh_instrument* instrument = NULL;
00213
00214 const char *sof_name = NULL;
00215 cpl_frameset *set = NULL;
00216
00217 const char * sci_name = NULL ;
00218 xsh_merge_param merge_par = { MEAN_MERGE_METHOD} ;
00219
00220 cpl_frameset *wavesol_frameset = NULL;
00221 cpl_frameset *rect_frameset = NULL;
00222 cpl_frameset *res_2D_frameset = NULL;
00223 cpl_frame *sci_frame = NULL;
00224 cpl_frame *orderlist_frame = NULL;
00225 cpl_frame *slitmap_frame = NULL;
00226 cpl_frame *model_frame = NULL;
00227 cpl_frame *spectralformat_frame = NULL;
00228 cpl_frame *recorder_frame = NULL ;
00229 cpl_frame *recordereso_frame = NULL ;
00230 cpl_frame *recordertab_frame = NULL ;
00231 cpl_frame *cube = NULL;
00232 int order_min = -1, order_max = -1;
00233 int rec_min_index=-1, rec_max_index=-1;
00234 double lambda_ref = -1;
00235 xsh_order_list* order_list = NULL;
00236 xsh_rectify_param rectify_par;
00237 int nslit_c, nslit=-100;
00238 double slit_min_c, slit_min=-100;
00239 XSH_MODE mode;
00240 const int decode_bp=2147483647;
00241
00242 TESTS_INIT(MODULE_ID);
00243 cpl_msg_set_level(CPL_MSG_DEBUG);
00244 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM);
00245
00246
00247 strcpy( rectify_par.rectif_kernel, "default");
00248 rectify_par.kernel_type = CPL_KERNEL_TANH;
00249 rectify_par.rectif_radius = 4 ;
00250 rectify_par.rectif_bin_lambda = 0.1 ;
00251 rectify_par.rectif_bin_space = 0.1 ;
00252 rectify_par.conserve_flux = FALSE;
00253
00254 HandleOptions( argc, argv, &rectify_par, &merge_par, &order_min, &order_max,
00255 &slit_min, &nslit, &lambda_ref);
00256
00257 if ( (argc - optind) >=2 ) {
00258 sci_name = argv[optind];
00259 sof_name = argv[optind+1];
00260 }
00261 else {
00262 Help();
00263 exit(0);
00264 }
00265
00266 check( set = sof_to_frameset( sof_name));
00267
00268 check( instrument = xsh_dfs_set_groups( set));
00269
00270 check( spectralformat_frame = xsh_find_spectral_format( set, instrument));
00271 check( orderlist_frame = xsh_find_order_tab_edges( set, instrument));
00272 check( slitmap_frame = xsh_find_slitmap( set, instrument));
00273 xsh_instrument_set_decode_bp( instrument, decode_bp ) ;
00274 TESTS_XSH_FRAME_CREATE( sci_frame, "SLIT_STARE_DIVIDED_arm", sci_name);
00275 check( mode = xsh_instrument_get_mode( instrument));
00276
00277 xsh_msg("SCI : %s",
00278 cpl_frame_get_filename( sci_frame));
00279 xsh_msg("ORDERLIST : %s",
00280 cpl_frame_get_filename( orderlist_frame));
00281 xsh_msg("SPECTRALFORMAT : %s",
00282 cpl_frame_get_filename( spectralformat_frame));
00283 if ( slitmap_frame != NULL){
00284 xsh_msg("SLITMAP : %s",
00285 cpl_frame_get_filename( slitmap_frame));
00286 }
00287
00288 wavesol_frameset = xsh_find_wave_tab_ifu( set, instrument);
00289 cpl_error_reset();
00290 if ( wavesol_frameset == NULL){
00291 model_frame = xsh_find_model_config_tab( set, instrument);
00292 xsh_msg("MODEL : %s",
00293 cpl_frame_get_filename( model_frame));
00294 }
00295 else{
00296 int iframe;
00297 int size;
00298
00299 size = cpl_frameset_get_size( wavesol_frameset);
00300 for(iframe=0; iframe < size; iframe++){
00301 cpl_frame *frame = cpl_frameset_get_frame(wavesol_frameset, iframe);
00302 xsh_msg("WAVESOL : %s",
00303 cpl_frame_get_filename( frame));
00304 }
00305 }
00306
00307 xsh_msg(" Parameters ");
00308 xsh_msg(" kernel %s", RECTIFY_KERNEL_PRINT( rectify_par.kernel_type));
00309 xsh_msg(" radius %f", rectify_par.rectif_radius);
00310 xsh_msg(" bin-space %f", rectify_par.rectif_bin_space);
00311 xsh_msg(" bin-lambda %f", rectify_par.rectif_bin_lambda);
00312
00313
00314 check( order_list = xsh_order_list_load ( orderlist_frame, instrument));
00315
00316 if ( order_min != -1) {
00317 check( rec_min_index = xsh_order_list_get_index_by_absorder( order_list,
00318 order_min));
00319 xsh_msg("Order min %d => index %d", order_min, rec_min_index);
00320 }
00321 else{
00322 rec_min_index = 0;
00323 }
00324
00325 if ( order_max != -1) {
00326 check( rec_max_index = xsh_order_list_get_index_by_absorder( order_list,
00327 order_max));
00328 xsh_msg("Order max %d => index %d", order_max, rec_max_index);
00329 }
00330 else{
00331 rec_max_index = order_list->size;
00332 }
00333
00334
00335
00336
00337 check( xsh_rec_slit_size( &rectify_par,
00338 &slit_min_c, &nslit_c, mode));
00339
00340 if (slit_min == -100){
00341 slit_min = slit_min_c;
00342 }
00343 if ( nslit == -100){
00344 nslit = nslit_c;
00345 }
00346
00347 xsh_msg("SLIT min = %f and has size %d", slit_min, nslit);
00348 xsh_msg("Lambda ref %f", lambda_ref);
00349 if (lambda_ref < 0.0){
00350 check( rect_frameset = xsh_rectify_orders_ifu( sci_frame, order_list,
00351 wavesol_frameset, NULL, model_frame, instrument, &rectify_par,
00352 spectralformat_frame, slitmap_frame,NULL,NULL,rec_min_index,
00353 rec_max_index, "test"));
00354 }
00355 else{
00356 double lambda_ref_min;
00357 double lambda_ref_max;
00358 const char * tablename = NULL;
00359 cpl_table *table = NULL;
00360 cpl_frame *spectralformat2_frame = NULL;
00361 xsh_spectralformat_list* list = NULL;
00362 cpl_vector* orders = NULL;
00363 int size, absorder, iorder;
00364
00365 lambda_ref_min = lambda_ref-rectify_par.rectif_bin_lambda*10;
00366 lambda_ref_max = lambda_ref+rectify_par.rectif_bin_lambda*10;
00367
00368 xsh_msg( "Do cube for reference %f ==> %f", lambda_ref_min, lambda_ref_max);
00369 check( list = xsh_spectralformat_list_load( spectralformat_frame,
00370 instrument));
00371 check( orders = xsh_spectralformat_list_get_orders( list, lambda_ref));
00372 size = cpl_vector_get_size( orders);
00373
00374 tablename = cpl_frame_get_filename( spectralformat_frame);
00375 XSH_TABLE_LOAD( table, tablename);
00376
00377 for( iorder=0; iorder < size; iorder++){
00378 absorder = cpl_vector_get( orders, iorder);
00379 xsh_msg("In Order %d", absorder);
00380 cpl_table_set_int(table, XSH_SPECTRALFORMAT_TABLE_COLNAME_ORDER, iorder, absorder);
00381 cpl_table_set_float(table, XSH_SPECTRALFORMAT_TABLE_COLNAME_WLMIN, iorder, lambda_ref_min);
00382 cpl_table_set_float(table, XSH_SPECTRALFORMAT_TABLE_COLNAME_WLMAX, iorder, lambda_ref_max);
00383 }
00384 cpl_table_save( table, NULL,NULL,"LAMBDA_REF_FORMAT.fits",CPL_IO_DEFAULT);
00385 TESTS_XSH_FRAME_CREATE( spectralformat2_frame, "SPEC_arm", "LAMBDA_REF_FORMAT.fits");
00386
00387 check( rect_frameset = xsh_rectify_orders_ifu( sci_frame, order_list,
00388 wavesol_frameset, NULL, model_frame, instrument, &rectify_par,
00389 spectralformat2_frame, slitmap_frame,NULL,NULL, 0,
00390 size-1, "test"));
00391 }
00392 check( res_2D_frameset = xsh_merge_ord_ifu( rect_frameset,instrument,
00393 &merge_par, "test"));
00394
00395
00396 check( cube = xsh_format( res_2D_frameset, "CUBE.fits",instrument,
00397 "test") ) ;
00398 cleanup:
00399 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00400 xsh_error_dump(CPL_MSG_ERROR);
00401 ret = 1;
00402 }
00403 xsh_order_list_free( &order_list);
00404 xsh_free_frame( &sci_frame);
00405 xsh_free_frameset( &rect_frameset);
00406 xsh_free_frameset( &res_2D_frameset);
00407 xsh_free_frameset( &wavesol_frameset);
00408 xsh_free_frameset( &set);
00409 xsh_instrument_free( &instrument);
00410 xsh_free_frame( &recorder_frame);
00411 xsh_free_frame( &recordereso_frame);
00412 xsh_free_frame( &recordertab_frame);
00413 xsh_free_frame( &cube);
00414 TEST_END();
00415 return ret;
00416 }