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
00044 #include <xsh_data_order.h>
00045 #include <xsh_error.h>
00046 #include <xsh_msg.h>
00047 #include <xsh_data_instrument.h>
00048 #include <xsh_dfs.h>
00049 #include <xsh_pfits.h>
00050 #include <tests.h>
00051 #include <xsh_data_spectralformat.h>
00052 #include <xsh_data_wavesol.h>
00053 #include <xsh_data_arclist.h>
00054 #include <xsh_utils_table.h>
00055 #include <cpl.h>
00056 #include <math.h>
00057 #include <getopt.h>
00058
00059
00060
00061
00062 #define XSH_FLOAT_PRECISION 0.000001
00063 #define MODULE_ID "XSH_DATA_WAVE_TAB_2D"
00064
00065 enum {
00066 LAMBDA_STEP_OPT, SLIT_STEP_OPT, BINX_OPT, BINY_OPT,DEBUG_OPT, HELP_OPT
00067 } ;
00068
00069 static float lambda_step = 0.5;
00070 static float slit_step = 1.458;
00071 int binx =1;
00072 int biny =1;
00073
00074 static struct option LongOptions[] = {
00075 {"lambda-step", required_argument, 0, LAMBDA_STEP_OPT},
00076 {"slit-step", required_argument, 0, SLIT_STEP_OPT},
00077 {"binx", required_argument, 0, BINX_OPT},
00078 {"biny", required_argument, 0, BINY_OPT},
00079 {"debug", required_argument, 0, DEBUG_OPT},
00080 {"help", 0, 0, HELP_OPT},
00081 {NULL, 0, 0, 0}
00082 } ;
00083
00084
00085 static void Help( void )
00086 {
00087 puts ("Unitary test of wave tab 2d");
00088 puts( "Usage : ./the_xsh_data_wave_tab_2d [options] <SOF>");
00089
00090 puts( "Options" ) ;
00091 puts( " --binx=<n> : Binning in X (default 1)" );
00092 puts( " --biny=<n> : Binning in Y (default 1)" );
00093 puts( " --lambda-step=<n> : Step in lambda. Default 0.2" );
00094 puts( " --slit-step=<n> : Step in slit. Default 0.2" );
00095 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
00096 puts( " --help : What you see" ) ;
00097
00098 puts( "The input files argument MUST be in this order:" ) ;
00099 puts( " 1. SOF [WAVE_TAB_2D, SPECTRAL_FORMAT, ARC_LIST]");
00100 TEST_END();
00101 exit(0);
00102 }
00103
00104
00105
00106
00107
00108 static void HandleOptions( int argc, char ** argv)
00109 {
00110 int opt ;
00111 int option_index = 0;
00112
00113 while( (opt = getopt_long( argc, argv, "debug:help",
00114 LongOptions, &option_index )) != EOF )
00115 switch( opt ) {
00116 case LAMBDA_STEP_OPT:
00117 lambda_step = atof(optarg);
00118 break ;
00119 case SLIT_STEP_OPT:
00120 slit_step = atof(optarg);
00121 break ;
00122 case BINX_OPT:
00123 binx = atoi(optarg);
00124 case BINY_OPT:
00125 biny = atoi(optarg);
00126 break ;
00127 case DEBUG_OPT:
00128 if ( strcmp( optarg, "LOW")==0){
00129 xsh_debug_level_set( XSH_DEBUG_LEVEL_LOW);
00130 }
00131 else if ( strcmp( optarg, "HIGH")==0){
00132 xsh_debug_level_set( XSH_DEBUG_LEVEL_HIGH);
00133 }
00134 break;
00135 case HELP_OPT:
00136 Help();
00137 break;
00138 default:
00139 break;
00140 }
00141 }
00142
00143
00151
00152 int main(int argc, char** argv)
00153 {
00154 int ret = 0;
00155 int i;
00156 float j,k;
00157 xsh_instrument * instrument = NULL ;
00158 cpl_frameset* set = NULL;
00159 char *sof_name = NULL;
00160 cpl_frame *wave_tab_2d_frame = NULL;
00161 cpl_frame *spectralformat_frame = NULL;
00162 cpl_frame *arclist_frame = NULL;
00163 xsh_spectralformat_list *spec_list = NULL;
00164 xsh_wavesol *wave_tab_2d = NULL;
00165 xsh_arclist* arclist = NULL;
00166 FILE* wave_tab_2d_file = NULL;
00167 FILE* wave_tab_2d_dat_file = NULL;
00168
00169
00170 TESTS_INIT(MODULE_ID);
00171 cpl_msg_set_level(CPL_MSG_DEBUG);
00172 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM) ;
00173
00174 HandleOptions( argc, argv);
00175
00176
00177 if ( (argc-optind) >= 1) {
00178 sof_name = argv[optind];
00179
00180 check( set = sof_to_frameset( sof_name));
00181
00182
00183 check( instrument = xsh_dfs_set_groups( set));
00184
00185 check( wave_tab_2d_frame = xsh_find_wave_tab_2d( set, instrument));
00186 check( spectralformat_frame = xsh_find_spectral_format( set, instrument));
00187 arclist_frame = xsh_find_arc_line_list( set, instrument);
00188 }
00189 else{
00190 Help();
00191 }
00192 xsh_msg("Wave tab 2d : %s", cpl_frame_get_filename(wave_tab_2d_frame));
00193 xsh_msg("Spectral format : %s", cpl_frame_get_filename(spectralformat_frame));
00194 if ( arclist_frame != NULL){
00195 xsh_msg("Arclines : %s", cpl_frame_get_filename( arclist_frame));
00196 }
00197 xsh_msg(" Options : lambda-step %f slit-step %f", lambda_step, slit_step);
00198
00199 check( spec_list = xsh_spectralformat_list_load( spectralformat_frame,
00200 instrument));
00201 check( wave_tab_2d = xsh_wavesol_load( wave_tab_2d_frame, instrument));
00202 xsh_wavesol_set_bin_x( wave_tab_2d, binx);
00203 xsh_wavesol_set_bin_y( wave_tab_2d, biny);
00204 xsh_msg("bin %dx%d",binx, biny);
00205 xsh_msg("Wavesol lambda (%f,%f)", wave_tab_2d->min_lambda,
00206 wave_tab_2d->max_lambda);
00207 xsh_msg("Wavesol order (%f,%f)", wave_tab_2d->min_order,
00208 wave_tab_2d->max_order);
00209 xsh_msg("Wavesol slit (%f,%f)", wave_tab_2d->min_slit,
00210 wave_tab_2d->max_slit);
00211
00212 wave_tab_2d_file = fopen( "WAVE_TAB_2D.reg", "w");
00213 wave_tab_2d_dat_file = fopen( "WAVE_TAB_2D.dat", "w");
00214 fprintf( wave_tab_2d_file, "# Region file format: DS9 version 4.0\n"\
00215 "global color=red font=\"helvetica 10 normal\""\
00216 "select=1 highlite=1 edit=1 move=1 delete=1 include=1 fixed=0 "\
00217 "source\nimage\n");
00218 fprintf( wave_tab_2d_dat_file, "# x y lambda slit\n");
00219
00220 if ( arclist_frame == NULL){
00221 for( i=0; i< spec_list->size; i++){
00222 double absorder;
00223 float lambda_min, lambda_max;
00224 float lambda_min_full, lambda_max_full;
00225
00226 absorder= (double)spec_list->list[i].absorder;
00227 lambda_min = spec_list->list[i].lambda_min;
00228 lambda_max = spec_list->list[i].lambda_max;
00229 lambda_min_full = spec_list->list[i].lambda_min_full;
00230 lambda_max_full = spec_list->list[i].lambda_max_full;
00231 xsh_msg("order %f lambda %f->%f-%f<-%f",absorder, lambda_min_full,
00232 lambda_min, lambda_max, lambda_max_full);
00233
00234 for(j=lambda_min_full; j <= lambda_max_full; j+=lambda_step){
00235 double x, y;
00236 const char* full_lambda_color = "yellow";
00237 const char* lambda_color = "red";
00238 const char* color = NULL;
00239
00240 if ( (j < lambda_min)|| (j > lambda_max) ){
00241 color = full_lambda_color;
00242 }
00243 else{
00244 color = lambda_color;
00245 }
00246 check( x = xsh_wavesol_eval_polx( wave_tab_2d, j, absorder, 0.0));
00247 check( y = xsh_wavesol_eval_poly( wave_tab_2d, j, absorder, 0.0));
00248
00249
00250 fprintf( wave_tab_2d_file, "point(%f,%f) #point=cross color=%s font="\
00251 "\"helvetica 10 normal\" text={ %.3f}\n", x, y, color, j);
00252 fprintf( wave_tab_2d_dat_file, "%f %f %f %f\n", x, y, j, 0.0);
00253
00254 for( k=wave_tab_2d->min_slit; k <= wave_tab_2d->max_slit; k+=slit_step){
00255 check( x = xsh_wavesol_eval_polx( wave_tab_2d, j, absorder, k));
00256 check( y = xsh_wavesol_eval_poly( wave_tab_2d, j, absorder, k));
00257 fprintf( wave_tab_2d_file, "point(%f,%f) #point=cross color=green font="\
00258 "\"helvetica 10 normal\" text={ %.3f}\n", x, y, k);
00259 fprintf( wave_tab_2d_dat_file, "%f %f %f %f\n", x, y, j, k);
00260 }
00261 }
00262 }
00263 }
00264 else{
00265 int size;
00266
00267 arclist = xsh_arclist_load( arclist_frame);
00268 size = arclist->size;
00269
00270 for( i=0; i< size; i++){
00271 float lambda = 0.0;
00272 cpl_vector *orders = NULL;
00273 int order_size;
00274
00275 lambda = xsh_arclist_get_wavelength( arclist, i);
00276 orders = xsh_spectralformat_list_get_orders( spec_list, lambda);
00277 order_size = cpl_vector_get_size( orders);
00278
00279 for( j=0; j < order_size; j++){
00280 double absorder;
00281 double x, y;
00282
00283 absorder = cpl_vector_get( orders, j);
00284
00285 check( x = xsh_wavesol_eval_polx( wave_tab_2d, lambda, absorder, 0.0));
00286 check( y = xsh_wavesol_eval_poly( wave_tab_2d, lambda, absorder, 0.0));
00287
00288 fprintf( wave_tab_2d_file, "point(%f,%f) #point=cross color=red font="\
00289 "\"helvetica 10 normal\" text={ %.3f}\n", x, y, lambda);
00290 fprintf( wave_tab_2d_dat_file, "%f %f %f %f\n", x, y, lambda, 0.0);
00291
00292 for( k=slit_step; k < wave_tab_2d->max_slit; k+=slit_step){
00293 check( x = xsh_wavesol_eval_polx( wave_tab_2d, lambda, absorder, k));
00294 check( y = xsh_wavesol_eval_poly( wave_tab_2d, lambda, absorder, k));
00295 fprintf( wave_tab_2d_file, "point(%f,%f) #point=cross color=green font="\
00296 "\"helvetica 10 normal\" text={ %.3f}\n", x, y, k);
00297 fprintf( wave_tab_2d_dat_file, "%f %f %f %f\n", x, y, lambda, k);
00298 check( x = xsh_wavesol_eval_polx( wave_tab_2d, lambda, absorder, -k));
00299 check( y = xsh_wavesol_eval_poly( wave_tab_2d, lambda, absorder, -k));
00300 fprintf( wave_tab_2d_file, "point(%f,%f) #point=cross color=green font="\
00301 "\"helvetica 10 normal\" text={ %.3f}\n", x, y, -k);
00302 fprintf( wave_tab_2d_dat_file, "%f %f %f %f\n", x, y, lambda, -k);
00303 }
00304 }
00305 xsh_free_vector( &orders);
00306 }
00307 }
00308
00309
00310 cleanup:
00311 xsh_instrument_free( &instrument);
00312 xsh_free_frameset( &set);
00313
00314 xsh_arclist_free( &arclist);
00315 xsh_spectralformat_list_free( &spec_list);
00316 xsh_wavesol_free( &wave_tab_2d);
00317
00318 TEST_END();
00319 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00320 xsh_error_dump(CPL_MSG_ERROR);
00321 ret = 1;
00322 }
00323 if ( NULL != wave_tab_2d_dat_file) {
00324 fclose( wave_tab_2d_dat_file);
00325 }
00326 if ( NULL != wave_tab_2d_file) {
00327 fclose( wave_tab_2d_file);
00328 }
00329
00330 return ret ;
00331 }
00332