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
00035
00038
00039
00040
00041
00042 #include <tests.h>
00043 #include <xsh_data_pre.h>
00044 #include <xsh_error.h>
00045 #include <xsh_msg.h>
00046 #include <xsh_data_instrument.h>
00047 #include <xsh_data_rec.h>
00048 #include <xsh_data_localization.h>
00049 #include <xsh_data_spectrum.h>
00050 #include <xsh_drl.h>
00051 #include <xsh_pfits.h>
00052
00053 #include <xsh_badpixelmap.h>
00054
00055 #include <cpl.h>
00056 #include <math.h>
00057
00058 #include <getopt.h>
00059
00060
00061
00062
00063
00064 #define MODULE_ID "XSH_LAMBDA_ERR"
00065
00066 enum {
00067 MIN_ORDER_OPT, MAX_ORDER_OPT, DEBUG_OPT, HELP_OPT
00068 };
00069
00070 static struct option long_options[] = {
00071 {"order-min", required_argument, 0, MIN_ORDER_OPT},
00072 {"order-max", required_argument, 0, MAX_ORDER_OPT},
00073 {"debug", required_argument, 0, DEBUG_OPT},
00074 {"help", 0, 0, HELP_OPT},
00075 {0, 0, 0, 0}
00076 };
00077
00078 static void Help( void )
00079 {
00080 puts( "Unitary test of xsh_lambda_err");
00081 puts( "Usage: test_xsh_lambda_err [options] <input_files>");
00082
00083 puts( "Options" ) ;
00084 puts( " --order-min=<n> : Minimum abs order" );
00085 puts( " --order-max=<n> : Maximum abs order" );
00086 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
00087 puts( " --help : What you see" ) ;
00088 puts( "\nInput Files" ) ;
00089 puts( "The input files argument MUST be in this order:" ) ;
00090 puts( " 1. Science frame in PRE format" ) ;
00091 puts( " 2. Localization table" );
00092 puts( " 3. SOF [SPECTRAL_FORMAT, WAVEMAP, SLITMAP]\n" ) ;
00093 TEST_END();
00094 }
00095
00096 static void HandleOptions( int argc, char **argv,
00097 int *order_min, int *order_max)
00098 {
00099 int opt ;
00100 int option_index = 0;
00101
00102 while (( opt = getopt_long (argc, argv, "oversample:box-hsize:chunk-size",
00103 long_options, &option_index)) != EOF ){
00104
00105 switch ( opt ) {
00106 case MIN_ORDER_OPT:
00107 sscanf( optarg, "%d", order_min);
00108 break;
00109 case MAX_ORDER_OPT:
00110 sscanf( optarg, "%d", order_max);
00111 break;
00112 case DEBUG_OPT:
00113 if ( strcmp( optarg, "LOW")==0){
00114 xsh_debug_level_set( XSH_DEBUG_LEVEL_LOW);
00115 }
00116 else if ( strcmp( optarg, "HIGH")==0){
00117 xsh_debug_level_set( XSH_DEBUG_LEVEL_HIGH);
00118 }
00119 break;
00120 default:
00121 Help();
00122 exit(-1);
00123 }
00124 }
00125 return;
00126 }
00127
00128
00129
00130
00131
00139 int main( int argc, char **argv)
00140 {
00141 int ret;
00142
00143 xsh_instrument* instrument = NULL;
00144 const char *sof_name = NULL;
00145 cpl_frameset *set = NULL;
00146
00147 const char* sci_name = NULL;
00148 const char* loc_name = NULL;
00149 int order_min = -1;
00150 int order_max = -1;
00151 int rec_min_index = -1;
00152 int rec_max_index = -1;
00153 int iorder = 0;
00154
00155 cpl_propertylist *header = NULL;
00156 const char* pcatg = NULL;
00157 xsh_order_list* order_list = NULL;
00158 xsh_pre *pre = NULL;
00159 float *pre_flux = NULL;
00160 float *pre_errs = NULL;
00161
00162 cpl_image *wavemap = NULL;
00163 cpl_image *slitmap = NULL;
00164 float* wavemap_data = NULL;
00165 float* slitmap_data = NULL;
00166 int nx, ny;
00167 xsh_rec_list *rec_list = NULL;
00168 xsh_localization *loc_list = NULL;
00169 double obj_slit;
00170 xsh_spectrum *spectrum = NULL;
00171
00172 char name[256];
00173 FILE *file = NULL;
00174 char *pch = NULL;
00175 const char *prefix = NULL;
00176
00177 cpl_frame *sci_frame = NULL;
00178 cpl_frame *loc_frame = NULL;
00179 cpl_frame *orderlist_frame = NULL;
00180 cpl_frame *wavemap_frame = NULL;
00181 cpl_frame *slitmap_frame = NULL;
00182 cpl_frame *spectralformat_frame = NULL;
00183
00184
00185 TESTS_INIT( MODULE_ID);
00186 cpl_msg_set_level( CPL_MSG_DEBUG);
00187 xsh_debug_level_set( XSH_DEBUG_LEVEL_MEDIUM);
00188
00189
00190 HandleOptions( argc, argv, &order_min, &order_max);
00191
00192 if ( (argc - optind) >= 3 ) {
00193 sci_name = argv[optind];
00194 loc_name = argv[optind+1];
00195 sof_name = argv[optind+2];
00196 }
00197 else{
00198 Help();
00199 exit(0);
00200 }
00201
00202
00203 check( set = sof_to_frameset( sof_name));
00204
00205
00206 check( instrument = xsh_dfs_set_groups( set));
00207
00208 check( spectralformat_frame = xsh_find_spectral_format( set, instrument));
00209 check( orderlist_frame = xsh_find_order_tab_edges( set, instrument));
00210 check( wavemap_frame = xsh_find_wavemap( set, instrument));
00211 check( slitmap_frame = xsh_find_slitmap( set, instrument));
00212
00213 TESTS_XSH_FRAME_CREATE( sci_frame, "OBJECT_SLIT_STARE_arm", sci_name);
00214 TESTS_XSH_FRAME_CREATE( loc_frame, "LOCALIZATION_arm", loc_name);
00215
00216 xsh_msg("SCI : %s",
00217 cpl_frame_get_filename( sci_frame));
00218 xsh_msg("LOCALIZATION : %s",
00219 cpl_frame_get_filename( loc_frame));
00220 xsh_msg("ORDERLIST : %s",
00221 cpl_frame_get_filename( orderlist_frame));
00222 xsh_msg("SPECTRALFORMAT : %s",
00223 cpl_frame_get_filename( spectralformat_frame));
00224 xsh_msg("WAVEMAP : %s",
00225 cpl_frame_get_filename( wavemap_frame));
00226 xsh_msg("SLITMAP : %s",
00227 cpl_frame_get_filename( slitmap_frame));
00228
00229 pch=strrchr( sci_name,'/');
00230 if ( pch == NULL){
00231 prefix = sci_name;
00232 }
00233 else{
00234 prefix = pch+1;
00235 }
00236
00237 check( order_list = xsh_order_list_load ( orderlist_frame, instrument));
00238
00239 if ( order_min != -1) {
00240 check( rec_min_index = xsh_order_list_get_index_by_absorder( order_list,
00241 order_min));
00242 xsh_msg("Order min %d => index %d", order_min, rec_min_index);
00243 }
00244 else{
00245 rec_min_index = 0;
00246 }
00247
00248 if ( order_max != -1) {
00249 check( rec_max_index = xsh_order_list_get_index_by_absorder( order_list,
00250 order_max));
00251 xsh_msg("Order max %d => index %d", order_max, rec_max_index);
00252 }
00253 else{
00254 rec_max_index = order_list->size-1;
00255 xsh_msg("Doing all %d orders", order_list->size);
00256 }
00257
00258 check( wavemap = cpl_image_load( cpl_frame_get_filename( wavemap_frame),
00259 CPL_TYPE_FLOAT, 0, 0));
00260 check( slitmap = cpl_image_load( cpl_frame_get_filename( slitmap_frame),
00261 CPL_TYPE_FLOAT, 0, 0));
00262
00263 check( nx = cpl_image_get_size_x( wavemap));
00264 check( ny = cpl_image_get_size_y( wavemap));
00265 check( wavemap_data = cpl_image_get_data_float( wavemap));
00266 check( slitmap_data = cpl_image_get_data_float( slitmap));
00267
00268 check( loc_list = xsh_localization_load( loc_frame));
00269 obj_slit = cpl_polynomial_eval_1d( loc_list->cenpoly,
00270 600, NULL);
00271
00272 xsh_msg("Localization center is around %f arcsec", obj_slit);
00273
00274 check( header = cpl_propertylist_load( sci_name, 0));
00275 pcatg = xsh_pfits_get_pcatg( header);
00276
00277 if (strstr( pcatg, "ORDER2D") != NULL){
00278 double slit_step=0;
00279 int nslit=0, s=0;
00280 float *slitdata=NULL;
00281
00282 xsh_msg("Rectified frame");
00283 check( rec_list = xsh_rec_list_load( sci_frame, instrument));
00284 check( nslit = xsh_rec_list_get_nslit( rec_list, 0));
00285 check( slitdata = xsh_rec_list_get_slit( rec_list, 0));
00286 check( slit_step = xsh_pfits_get_rectify_bin_space( header));
00287 s = (int) xsh_round_double( (obj_slit-slitdata[0]) / slit_step);
00288 xsh_msg("s index %d/%d", s, nslit);
00289
00290 for( iorder=rec_min_index; iorder<= rec_max_index; iorder++){
00291 int nlambda=0;
00292 double *lambdadata = NULL;
00293 float *flux = NULL, *errs = NULL;
00294 int abs_order=0;
00295 int ilambda;
00296
00297 check( nlambda = xsh_rec_list_get_nlambda( rec_list, iorder));
00298 check( abs_order = xsh_rec_list_get_order( rec_list, iorder));
00299 check( lambdadata = xsh_rec_list_get_lambda( rec_list, iorder));
00300 check( flux = xsh_rec_list_get_data1( rec_list, iorder));
00301 check( errs = xsh_rec_list_get_errs1( rec_list, iorder));
00302
00303 sprintf( name, "%s_lambda_err_%d.dat", prefix, abs_order);
00304
00305 xsh_msg("Name %s", name);
00306
00307 file = fopen( name, "w");
00308
00309 fprintf( file, "#lambda flux err sn\n");
00310
00311 for( ilambda =0; ilambda < nlambda; ilambda++){
00312 double lambda_v;
00313 double flux_v, err_v;
00314 double sn;
00315
00316 lambda_v = lambdadata[ilambda];
00317 flux_v = flux[ilambda+nlambda*s];
00318 err_v = errs[ilambda+nlambda*s];
00319 sn = flux_v/err_v;
00320 fprintf( file, "%f %f %f %f\n", lambda_v, flux_v, err_v,
00321 sn);
00322 }
00323 fclose( file);
00324 }
00325
00326 }
00327 else if( strstr( pcatg, "MERGE2D") != NULL){
00328 xsh_msg("Merge frame");
00329 double *flux = NULL;
00330 double *errs = NULL;
00331 int s, ilambda;
00332
00333 check( spectrum = xsh_spectrum_load( sci_frame));
00334 s = (int) round( (obj_slit-spectrum->slit_min) / spectrum->slit_step);
00335 xsh_msg("s index %d/%d", s, spectrum->size_slit);
00336 check( flux = xsh_spectrum_get_flux( spectrum));
00337 check( errs = xsh_spectrum_get_errs(spectrum));
00338 sprintf( name, "%s_lambda_err.dat", prefix);
00339 file = fopen( name,"w");
00340 fprintf( file, "#lambda flux err sn\n");
00341
00342 for(ilambda=0; ilambda< spectrum->size_lambda; ilambda++){
00343 double lambda_v;
00344 double flux_v, err_v;
00345 double sn;
00346
00347 lambda_v = spectrum->lambda_min+ilambda*spectrum->lambda_step;
00348 flux_v = flux[ilambda+s*spectrum->size_lambda];
00349 err_v = errs[ilambda+s*spectrum->size_lambda];
00350 sn = flux_v/err_v;
00351 fprintf( file, "%f %f %f %f\n", lambda_v, flux_v, err_v, sn);
00352 }
00353 fclose( file);
00354 }
00355 else{
00356
00357 check( pre = xsh_pre_load( sci_frame, instrument));
00358 check( pre_errs = cpl_image_get_data_float( pre->errs));
00359 check( pre_flux = cpl_image_get_data_float( pre->data));
00360
00361 xsh_msg("Work with frame in PRE format binning: %dx%d", pre->binx, pre->biny);
00362
00363 check( xsh_order_list_set_bin_x( order_list, pre->binx));
00364 check( xsh_order_list_set_bin_y( order_list, pre->biny));
00365
00366 xsh_msg("Generate files");
00367 for( iorder=rec_min_index; iorder<= rec_max_index; iorder++){
00368 int abs_order, start_y, end_y;
00369 int y, x;
00370
00371 check( start_y = xsh_order_list_get_starty( order_list, iorder));
00372 check( end_y = xsh_order_list_get_endy( order_list, iorder));
00373 abs_order = order_list->list[iorder].absorder;
00374
00375 sprintf( name, "%s_lambda_err_%d.dat", prefix, abs_order);
00376
00377 file = fopen( name, "w");
00378 fprintf( file, "#lambda flux err sn\n");
00379
00380 for(y=start_y; y <= end_y; y++){
00381 double lambda;
00382 double slit;
00383 double err, flux;
00384 double sn;
00385 double diff=12;
00386 int xlow, xup, xnear=0;
00387
00388 check( xlow = xsh_order_list_eval_int( order_list,
00389 order_list->list[iorder].edglopoly, y));
00390 check( xup = xsh_order_list_eval_int( order_list,
00391 order_list->list[iorder].edguppoly, y));
00392
00393 for( x=xlow-1; x < xup; x++){
00394 slit = slitmap_data[x+(y-1)*nx];
00395 if ( fabs( slit-obj_slit) < diff){
00396 if ( wavemap_data[x+(y-1)*nx] > 0){
00397 diff = fabs( slit-obj_slit);
00398 xnear = x;
00399 }
00400 }
00401 }
00402 lambda = wavemap_data[xnear+(y-1)*nx];
00403 if (lambda > 0){
00404 err = pre_errs[xnear+(y-1)*nx];
00405 flux = pre_flux[xnear+(y-1)*nx];
00406 sn = flux/err;
00407 fprintf( file, "%f %f %f %f\n", lambda, flux, err, sn);
00408 }
00409 else {
00410 xsh_msg("Skipping Lambda %f for x %d y %d", lambda, xnear, y);
00411 }
00412 }
00413 fclose( file);
00414 }
00415 }
00416
00417 cleanup:
00418 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00419 xsh_error_dump(CPL_MSG_ERROR);
00420 ret = 1;
00421 }
00422 if(file != NULL) {
00423 fclose( file);
00424 }
00425 xsh_order_list_free( &order_list);
00426 xsh_pre_free( &pre);
00427 xsh_free_image( &wavemap);
00428 xsh_free_image( &slitmap);
00429 xsh_free_propertylist( &header);
00430 xsh_rec_list_free( &rec_list);
00431 xsh_localization_free( &loc_list);
00432 xsh_free_frame( &sci_frame);
00433 xsh_free_frame( &loc_frame);
00434 xsh_spectrum_free( &spectrum);
00435 xsh_free_frameset( &set);
00436 xsh_instrument_free( &instrument);
00437
00438 TEST_END();
00439 return ret;
00440 }
00441