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_RECTIFY"
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
00075 } ;
00076
00077 static const char * Options = "" ;
00078
00079 static struct option long_options[] = {
00080 {"kernel", required_argument, 0, KERNEL_OPT},
00081 {"radius", required_argument, 0, RADIUS_OPT},
00082 {"bin-lambda", required_argument, 0, BIN_LAMBDA_OPT},
00083 {"bin-space", required_argument, 0, BIN_SPACE_OPT},
00084 {"order-min", required_argument, 0, MIN_ORDER_OPT},
00085 {"order-max", required_argument, 0, MAX_ORDER_OPT},
00086 {"slit-min",required_argument, 0, SLIT_MIN_OPT},
00087 {"slit-n",required_argument, 0, NSLIT_OPT},
00088 {"help", 0, 0, HELP_OPT},
00089 {0, 0, 0, 0}
00090 };
00091
00092 static void Help( void )
00093 {
00094 puts( "Unitary test of xsh_rectify" ) ;
00095 puts( "Usage: test_rectify [options] <input_files>" ) ;
00096 puts( "Options" ) ;
00097 puts( " --kernel=<name> : TANH, SINC, SINC2, LANCZOS, HAMMING, HANN [TANH]");
00098 puts( " --radius=<nn> : Radius [4]" ) ;
00099 puts( " --bin-lambda=<n> : Bin in Lambda [0.1]" ) ;
00100 puts( " --bin-space=<n> : Bin in Slit [0.1]" ) ;
00101 puts( " --order-min=<n> : Minimum abs order" );
00102 puts( " --order-max=<n> : Maximum abs order" );
00103 puts( " --slit-min=<n> : Minimum slit to rectify" );
00104 puts( " --slit-n=<n> : Number of pixels in slit rectified frame" );
00105 puts( " --help : What you see" ) ;
00106 puts( "\nInput Files" ) ;
00107 puts( "The input files argument MUST be in this order:" ) ;
00108 puts( " 1. Science frame in PRE format" ) ;
00109 puts( " 2. SOF\n" ) ;
00110 TEST_END();
00111 }
00112
00113 static void HandleOptions( int argc, char **argv,
00114 xsh_rectify_param *rectify_par, int *order_min, int *order_max,
00115 double *slit_min, int *nslit)
00116 {
00117 int opt ;
00118 int option_index = 0;
00119
00120 while (( opt = getopt_long (argc, argv, Options,
00121 long_options, &option_index)) != EOF )
00122 switch ( opt ) {
00123 case KERNEL_OPT:
00124 strcpy( rectify_par->rectif_kernel, optarg);
00125 if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_TANH)) == 0){
00126 rectify_par->kernel_type = CPL_KERNEL_TANH;
00127 }
00128 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_TANH)) == 0){
00129 rectify_par->kernel_type = CPL_KERNEL_TANH;
00130 }
00131 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_SINC)) == 0){
00132 rectify_par->kernel_type = CPL_KERNEL_SINC;
00133 }
00134 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_SINC2)) == 0){
00135 rectify_par->kernel_type = CPL_KERNEL_SINC2;
00136 }
00137 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_LANCZOS)) == 0){
00138 rectify_par->kernel_type = CPL_KERNEL_LANCZOS;
00139 }
00140 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_HAMMING)) == 0){
00141 rectify_par->kernel_type = CPL_KERNEL_HAMMING;
00142 }
00143 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_HANN)) == 0){
00144 rectify_par->kernel_type = CPL_KERNEL_HANN;
00145 }
00146 else{
00147 xsh_msg("Invalid kernel option %s", optarg);
00148 Help();
00149 exit(0);
00150 }
00151 break;
00152 case RADIUS_OPT:
00153 rectify_par->rectif_radius = atof( optarg);
00154 break ;
00155 case BIN_LAMBDA_OPT:
00156 sscanf( optarg, "%lf", &rectify_par->rectif_bin_lambda ) ;
00157 break ;
00158 case BIN_SPACE_OPT:
00159 sscanf( optarg, "%lf", &rectify_par->rectif_bin_space ) ;
00160 break ;
00161 case MIN_ORDER_OPT:
00162 sscanf( optarg, "%d", order_min);
00163 break;
00164 case MAX_ORDER_OPT:
00165 sscanf( optarg, "%d", order_max);
00166 break;
00167 case SLIT_MIN_OPT:
00168 sscanf( optarg, "%lf", slit_min) ;
00169 break ;
00170 case NSLIT_OPT:
00171 sscanf( optarg, "%d", nslit);
00172 break ;
00173 default: Help() ; exit( 0 ) ;
00174 }
00175 }
00176
00185 int main( int argc, char **argv)
00186 {
00187
00188 int ret = 0 ;
00189
00190 xsh_instrument* instrument = NULL;
00191
00192 const char *sof_name = NULL;
00193 cpl_frameset *set = NULL;
00194 cpl_frame *sliceoffset_frame = NULL;
00195
00196 const char * sci_name = NULL ;
00197
00198 cpl_frameset *wavesol_frameset = NULL;
00199 cpl_frameset *result_frameset = NULL;
00200 cpl_frame *sci_frame = NULL;
00201 cpl_frame *orderlist_frame = NULL;
00202 cpl_frame *slitmap_frame = NULL;
00203 cpl_frame *wavesol_frame = NULL;
00204 cpl_frame *model_frame = NULL;
00205 cpl_frame *spectralformat_frame = NULL;
00206 cpl_frame *recorder_frame = NULL ;
00207 cpl_frame *recordereso_frame = NULL ;
00208 cpl_frame *recordertab_frame = NULL ;
00209 int order_min = -1, order_max = -1;
00210 int rec_min_index=-1, rec_max_index=-1;
00211 int slitlet;
00212 xsh_order_list* order_list = NULL;
00213 xsh_slice_offset *sliceoffset = NULL;
00214 xsh_rectify_param rectify_par;
00215 int nslit_c=20, nslit=-100;
00216 double slit_min_c=0, slit_min=-100;
00217 XSH_MODE mode = XSH_MODE_SLIT;
00218 xsh_merge_param merge_par = { MEAN_MERGE_METHOD};
00219 cpl_frame *spectrum_frame = NULL;
00220
00221
00222 TESTS_INIT(MODULE_ID);
00223 cpl_msg_set_level(CPL_MSG_DEBUG);
00224 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM);
00225
00226
00227 strcpy( rectify_par.rectif_kernel, "default");
00228 rectify_par.kernel_type = CPL_KERNEL_TANH;
00229 rectify_par.rectif_radius = 4 ;
00230 rectify_par.rectif_bin_lambda = 0.1 ;
00231 rectify_par.rectif_bin_space = 0.1 ;
00232 rectify_par.conserve_flux = FALSE;
00233 rectify_par.rectify_full_slit=CPL_TRUE;
00234
00235 HandleOptions( argc, argv, &rectify_par, &order_min, &order_max,
00236 &slit_min, &nslit);
00237
00238 if ( (argc - optind) >=2 ) {
00239 sci_name = argv[optind];
00240 sof_name = argv[optind+1];
00241 }
00242 else {
00243 Help();
00244 exit(0);
00245 }
00246
00247 check( set = sof_to_frameset( sof_name));
00248
00249 check( instrument = xsh_dfs_set_groups( set));
00250
00251 check( spectralformat_frame = xsh_find_spectral_format( set, instrument));
00252 check( orderlist_frame = xsh_find_order_tab_edges( set, instrument));
00253 check( slitmap_frame = xsh_find_slitmap( set, instrument));
00254
00255 TESTS_XSH_FRAME_CREATE( sci_frame, "SLIT_STARE_DIVIDED_arm", sci_name);
00256 check( mode = xsh_instrument_get_mode( instrument));
00257
00258 xsh_msg("SCI : %s",
00259 cpl_frame_get_filename( sci_frame));
00260 xsh_msg("ORDERLIST : %s",
00261 cpl_frame_get_filename( orderlist_frame));
00262 xsh_msg("SPECTRALFORMAT : %s",
00263 cpl_frame_get_filename( spectralformat_frame));
00264 if ( slitmap_frame != NULL){
00265 xsh_msg("SLITMAP : %s",
00266 cpl_frame_get_filename( slitmap_frame));
00267 }
00268
00269 if ( mode == XSH_MODE_SLIT){
00270 wavesol_frame = xsh_find_wave_tab_2d( set, instrument);
00271
00272 if ( wavesol_frame == NULL){
00273 model_frame = xsh_find_model_config_tab( set, instrument);
00274 }
00275
00276 if ( wavesol_frame != NULL){
00277 xsh_msg("WAVESOL : %s",
00278 cpl_frame_get_filename( wavesol_frame));
00279 }
00280 else {
00281 xsh_msg("MODEL : %s",
00282 cpl_frame_get_filename( model_frame));
00283 }
00284 }
00285 else{
00286 wavesol_frameset = xsh_find_wave_tab_ifu( set, instrument);
00287 cpl_error_reset();
00288 if ( wavesol_frameset == NULL){
00289 model_frame = xsh_find_model_config_tab( set, instrument);
00290 xsh_msg("MODEL : %s",
00291 cpl_frame_get_filename( model_frame));
00292 }
00293 else{
00294 int iframe;
00295 int size;
00296
00297 size = cpl_frameset_get_size( wavesol_frameset);
00298 for(iframe=0; iframe < size; iframe++){
00299 cpl_frame *frame = cpl_frameset_get_frame(wavesol_frameset, iframe);
00300 xsh_msg("WAVESOL : %s",
00301 cpl_frame_get_filename( frame));
00302 }
00303 }
00304 sliceoffset_frame = xsh_find_frame_with_tag( set, XSH_SLICE_OFFSET,
00305 instrument);
00306 if ( sliceoffset_frame !=NULL){
00307 xsh_msg("SLICEOFFSET : %s",
00308 cpl_frame_get_filename( sliceoffset_frame));
00309 }
00310 }
00311
00312 xsh_msg(" Parameters ");
00313 xsh_msg(" kernel %s", RECTIFY_KERNEL_PRINT( rectify_par.kernel_type));
00314 xsh_msg(" radius %f", rectify_par.rectif_radius);
00315 xsh_msg(" bin-space %f", rectify_par.rectif_bin_space);
00316 xsh_msg(" bin-lambda %f", rectify_par.rectif_bin_lambda);
00317
00318
00319 check( order_list = xsh_order_list_load ( orderlist_frame, instrument));
00320
00321 if ( order_min != -1) {
00322 check( rec_min_index = xsh_order_list_get_index_by_absorder( order_list,
00323 order_min));
00324 xsh_msg("Order min %d => index %d", order_min, rec_min_index);
00325 }
00326 else{
00327 rec_min_index = 0;
00328 }
00329
00330 if ( order_max != -1) {
00331 check( rec_max_index = xsh_order_list_get_index_by_absorder( order_list,
00332 order_max));
00333 xsh_msg("Order max %d => index %d", order_max, rec_max_index);
00334 }
00335 else{
00336 rec_max_index = order_list->size;
00337 }
00338
00339
00340
00341 check( xsh_rec_slit_size( &rectify_par,
00342 &slit_min_c, &nslit_c, mode));
00343
00344 if (slit_min == -100){
00345 slit_min = slit_min_c;
00346 }
00347 if ( nslit == -100){
00348 nslit = nslit_c;
00349 }
00350
00351 if ( mode == XSH_MODE_SLIT){
00352 const char *tag = XSH_GET_TAG_FROM_ARM( XSH_ORDER2D, instrument);
00353 check( recorder_frame = xsh_rectify_orders( sci_frame, order_list,
00354 wavesol_frame, model_frame, instrument, &rectify_par,
00355 spectralformat_frame, NULL, "RECTIFIED.fits", tag, &recordereso_frame,
00356 &recordertab_frame,rec_min_index, rec_max_index, slit_min, nslit, 0));
00357 check( spectrum_frame = xsh_merge_ord( recorder_frame, instrument,
00358 &merge_par,"test"));
00359 }
00360 else{
00361 check( result_frameset = xsh_rectify_orders_ifu( sci_frame, order_list,
00362 wavesol_frameset, model_frame, instrument, &rectify_par,
00363 spectralformat_frame, sliceoffset_frame, slitmap_frame,NULL,NULL,rec_min_index,
00364 rec_max_index, "test"));
00365 }
00366
00367 cleanup:
00368 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00369 xsh_error_dump(CPL_MSG_ERROR);
00370 ret = 1;
00371 }
00372 xsh_order_list_free( &order_list);
00373 xsh_free_frame( &sci_frame);
00374 xsh_free_frameset( &result_frameset);
00375 xsh_free_frameset( &wavesol_frameset);
00376 xsh_free_frameset( &set);
00377 xsh_instrument_free( &instrument);
00378 xsh_free_frame( &recorder_frame);
00379 xsh_free_frame( &recordereso_frame);
00380 xsh_free_frame( &recordertab_frame);
00381 xsh_free_frame( &spectrum_frame);
00382 TEST_END();
00383 return ret;
00384 }