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_spectrum.h>
00050 #include <xsh_data_localization.h>
00051 #include <xsh_drl.h>
00052 #include <xsh_pfits.h>
00053 #include <xsh_parameters.h>
00054 #include <xsh_badpixelmap.h>
00055 #include <xsh_utils_ifu.h>
00056 #include <xsh_utils_table.h>
00057 #include <cpl.h>
00058 #include <math.h>
00059
00060 #include <string.h>
00061 #include <getopt.h>
00062
00063
00064
00065
00066
00067 #define MODULE_ID "XSH_LOCALIZE_IFU"
00068
00069 enum {
00070 SMOOTH_HSIZE_OPT, NSCALES_OPT, HF_SKIP_OPT, SIGMA_LOW_OPT, SIGMA_UP_OPT,
00071 SNR_LOW_OPT,SNR_UP_OPT,
00072 BOX_HSIZE_OPT,
00073 SLIT_MIN_OPT, SLIT_MAX_OPT, DEG_OPT, SKYMASK_OPT, HELP_OPT
00074 } ;
00075
00076 static struct option long_options[] = {
00077 {"smooth-hsize", required_argument, 0, SMOOTH_HSIZE_OPT},
00078 {"nscales", required_argument, 0, NSCALES_OPT},
00079 {"HF-skip", required_argument, 0, HF_SKIP_OPT},
00080 {"cut-sigma-low", required_argument, 0, SIGMA_LOW_OPT},
00081 {"cut-sigma-up", required_argument, 0, SIGMA_UP_OPT},
00082 {"cut-snr-low", required_argument, 0, SNR_LOW_OPT},
00083 {"cut-snr-up", required_argument, 0, SNR_UP_OPT},
00084 {"box-hsize", required_argument, 0, BOX_HSIZE_OPT},
00085 {"slit-min", required_argument, 0, SLIT_MIN_OPT},
00086 {"slit-max", required_argument, 0, SLIT_MAX_OPT},
00087 {"deg", required_argument, 0, DEG_OPT},
00088 {"skymask", required_argument, 0, SKYMASK_OPT},
00089 {"help", 0, 0, HELP_OPT},
00090 {0, 0, 0, 0}
00091 };
00092
00093 static void Help( void )
00094 {
00095 puts( "Unitary test of xsh_localize_ifu");
00096 puts( "Usage: test_xsh_localize_ifu [options] DATA_FILE");
00097
00098 puts( "Options" ) ;
00099 puts( " --help : What you see" ) ;
00100 puts( " --smooth-hsize= : Half size of median smooth filter");
00101 puts( " --nscales= : Number of scales");
00102 puts( " --HF-skip= : Number of skipping High Frequency");
00103 puts( " --cut-sigma-low= : Gaussian fits of the cross-dispersion profile whose FWHM is lower than this value are rejected.[0.05]");
00104 puts( " --cut-sigma-up = : Gaussian fits of the cross-dispersion profile whose FWHM is upper than this value are rejected.[0.95]");
00105 puts( " --cut-snr-low= : Gaussian fits of the cross-dispersion profile whose SNR is lower than this value are rejected.[0.05]");
00106 puts( " --cut-snr-up= : Gaussian fits of the cross-dispersion profile whose SNR is upper than this value are rejected.0.95]");
00107 puts( " --box-hsize= : Half size of running chunk box");
00108 puts( " --slit-min= : Maximum slit in arcsec [-6.0]");
00109 puts( " --slit-max= : Minimum slit in arcsec [6.0]");
00110 puts( " --deg = : Minimum slit in arcsec [2]");
00111 puts( " --skymask=<file> : Sky mask file");
00112 puts( "\nInput Files" ) ;
00113 puts( "DATA_FILE : Merge 2D frame");
00114 TEST_END();
00115 }
00116
00117
00118 static void HandleOptions( int argc, char **argv,
00119 int *smooth_hsize, int *nscales, int *HF_skip,
00120 double *sigma_low, double *sigma_up, double *snr_low, double *snr_up,
00121 int *box_hsize, double *slitmin, double *slitmax, int *deg, char **skymask_name)
00122 {
00123 int opt ;
00124 int option_index = 0;
00125
00126 while (( opt = getopt_long (argc, argv,
00127 "cut-sigma:smooth-hsize:nscales:HF-skip:deg",
00128 long_options, &option_index)) != EOF ){
00129
00130 switch ( opt ) {
00131 case SMOOTH_HSIZE_OPT:
00132 *smooth_hsize = atoi(optarg);
00133 break;
00134 case NSCALES_OPT:
00135 *nscales = atoi(optarg);
00136 break ;
00137 case HF_SKIP_OPT:
00138 *HF_skip = atoi(optarg);
00139 break ;
00140 case SIGMA_LOW_OPT:
00141 *sigma_low = atof( optarg);
00142 break;
00143 case SIGMA_UP_OPT:
00144 *sigma_up = atof( optarg);
00145 break;
00146 case SNR_LOW_OPT:
00147 *snr_low = atof( optarg);
00148 break;
00149 case SNR_UP_OPT:
00150 *snr_up = atof( optarg);
00151 break;
00152 case BOX_HSIZE_OPT:
00153 *box_hsize = atoi( optarg);
00154 break;
00155 case SLIT_MIN_OPT:
00156 *slitmin = atof( optarg);
00157 break;
00158 case SLIT_MAX_OPT:
00159 *slitmax = atof( optarg);
00160 break;
00161 case DEG_OPT:
00162 *deg = atoi( optarg);
00163 break;
00164 case SKYMASK_OPT:
00165 *skymask_name = optarg;
00166 break;
00167 default:
00168 Help(); exit(-1);
00169 }
00170 }
00171 return;
00172 }
00173
00174
00175 int main( int argc, char **argv)
00176 {
00177
00178 int ret = 0 ;
00179 int nscales = 5;
00180 int HF_skip = 2;
00181 int smooth_hsize = 2;
00182
00183 const char *file_name = NULL;
00184 cpl_frame *merge2d_frame = NULL;
00185 cpl_frame *result = NULL;
00186 char result_name[256];
00187 int i;
00188 cpl_table *result_tab = NULL;
00189 int result_size;
00190 double *wave_result_data = NULL;
00191 double *slit_result_data = NULL;
00192 FILE *result_file = NULL;
00193
00194 double sigma_low = 0.05;
00195 double sigma_up = 0.95;
00196 double snr_low = 0.05;
00197 double snr_up = 0.95;
00198
00199 int box_hsize = 0;
00200 double slitmin =-6;
00201 double slitmax = 6;
00202
00203 int deg =2;
00204
00205 char * skymask_name = NULL;
00206 cpl_frame *skymask_frame = NULL;
00207
00208 cpl_propertylist *plist = NULL;
00209 XSH_ARM arm = XSH_ARM_UNDEFINED;
00210 xsh_instrument* instrument = NULL;
00211 const char* tag = NULL;
00212
00213
00214
00215
00216
00217 TESTS_INIT(MODULE_ID);
00218
00219 cpl_msg_set_level(CPL_MSG_DEBUG);
00220 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM);
00221
00222
00223 HandleOptions( argc, argv, &smooth_hsize, &nscales, &HF_skip,
00224 &sigma_low, &sigma_up, &snr_low, &snr_up,
00225 &box_hsize, &slitmin, &slitmax, °, &skymask_name);
00226
00227 if ( (argc - optind) > 0 ) {
00228 file_name = argv[optind];
00229 }
00230 else {
00231 Help();
00232 exit( 0);
00233 }
00234
00235 check( plist = cpl_propertylist_load( file_name, 0));
00236 check( arm = xsh_pfits_get_arm( plist));
00237 TESTS_XSH_INSTRUMENT_CREATE( instrument, XSH_MODE_IFU, arm,
00238 XSH_LAMP_UNDEFINED, "xsh_geom_ifu");
00239
00240 xsh_msg("---Input Files");
00241 xsh_msg("File : %s ", file_name);
00242 xsh_msg("---Options");
00243 xsh_msg("SMOOTH_HSIZE : %d ", smooth_hsize);
00244 xsh_msg("NSCALES : %d ", nscales);
00245 xsh_msg("HF_SKIP : %d ", HF_skip);
00246 xsh_msg("SIGMA : [%f %f]", sigma_low, sigma_up);
00247 xsh_msg("SNR : [%f %f]", snr_low, snr_up);
00248 xsh_msg("BOX HSIZE: %d", box_hsize);
00249 xsh_msg("SLIT MIN: %f", slitmin);
00250 xsh_msg("SLIT MAX: %f", slitmax);
00251 xsh_msg("DEG : %d", deg);
00252 if ( skymask_name){
00253 xsh_msg("SKYMASK : %s", skymask_name);
00254 tag = XSH_GET_TAG_FROM_ARM( XSH_SKY_LINE_LIST, instrument);
00255 TESTS_XSH_FRAME_CREATE( skymask_frame, tag, skymask_name);
00256 }
00257
00258 tag = XSH_GET_TAG_FROM_ARM( XSH_MERGE2D, instrument);
00259 TESTS_XSH_FRAME_CREATE( merge2d_frame, tag, file_name);
00260
00261 if ( strstr( file_name, "CEN") != NULL){
00262 sprintf( result_name, "decomp_CEN.fits");
00263 }
00264 else if ( strstr( file_name, "UP") != NULL){
00265 sprintf( result_name, "decomp_UP.fits");
00266 }
00267 else if ( strstr( file_name, "DOWN") != NULL){
00268 sprintf( result_name, "decomp_DOWN.fits");
00269 }
00270 else{
00271 sprintf( result_name, "decomp.fits");
00272 }
00273
00274 check( result = xsh_localize_ifu_slitlet( merge2d_frame,
00275 skymask_frame, smooth_hsize, nscales,
00276 HF_skip, result_name, sigma_low, sigma_up,
00277 snr_low, snr_up, slitmin, slitmax, deg, box_hsize, instrument));
00278
00279 xsh_msg( "Produce file %s", result_name);
00280
00281 XSH_TABLE_LOAD( result_tab, result_name);
00282 check( wave_result_data = cpl_table_get_data_double( result_tab,
00283 XSH_OBJPOS_COLNAME_WAVELENGTH));
00284 check( slit_result_data = cpl_table_get_data_double( result_tab,
00285 XSH_OBJPOS_COLNAME_SLIT));
00286 result_size = cpl_table_get_nrow( result_tab);
00287
00288 result_file = fopen("LOCALIZE_IFU.reg", "w+");
00289
00290 fprintf( result_file, "# Region file format: DS9 version 4.0\n");
00291
00292 fprintf( result_file, "#yellow center\n");
00293 fprintf( result_file, "global color=red font=\"helvetica 4 normal\"select=1 highlite=1 edit=1 move=1 delete=1 include=1 fixed=0 source=1\n");
00294 fprintf( result_file, "wcs\n");
00295
00296 for( i=0; i< result_size; i++){
00297 double wave, slit;
00298
00299 wave = wave_result_data[i];
00300 slit = slit_result_data[i];
00301
00302 fprintf( result_file, "point(%f,%f)\n #point=cross color=yellow font=\"helvetica 4 normal\"\n", wave, slit);
00303 }
00304
00305 fclose( result_file);
00306
00307 xsh_msg( "Produce ds9 region file LOCALIZE_IFU.reg");
00308 cleanup:
00309 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00310 xsh_error_dump(CPL_MSG_ERROR);
00311 ret=1;
00312 }
00313 xsh_free_frame( &skymask_frame);
00314 xsh_instrument_free( &instrument);
00315 xsh_free_propertylist( &plist);
00316 xsh_free_frame( &result);
00317 xsh_free_frame( &merge2d_frame);
00318 XSH_TABLE_FREE( result_tab);
00319 TEST_END();
00320 return ret ;
00321 }
00322