X-shooter Pipeline Reference Manual 3.8.15
test-xsh_lambda_err.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2012-12-07 16:51:34 $
23 * $Revision: 1.13 $
24 */
25
26#ifdef HAVE_CONFIG_H
27# include <config.h>
28#endif
29
30/*-------------------------------------------------------------------------*/
35/*-------------------------------------------------------------------------*/
38/*--------------------------------------------------------------------------
39 Includes
40 --------------------------------------------------------------------------*/
41
42#include <tests.h>
43#include <xsh_data_pre.h>
44#include <xsh_error.h>
45#include <xsh_msg.h>
46#include <xsh_data_instrument.h>
47#include <xsh_data_rec.h>
49#include <xsh_data_spectrum.h>
50#include <xsh_drl.h>
51#include <xsh_pfits.h>
52
53#include <xsh_badpixelmap.h>
54
55#include <cpl.h>
56#include <math.h>
57
58#include <getopt.h>
59
60/*--------------------------------------------------------------------------
61 Defines
62 --------------------------------------------------------------------------*/
63
64#define MODULE_ID "XSH_LAMBDA_ERR"
65
66enum {
68};
69
70static struct option long_options[] = {
71 {"order-min", required_argument, 0, MIN_ORDER_OPT},
72 {"order-max", required_argument, 0, MAX_ORDER_OPT},
73 {"debug", required_argument, 0, DEBUG_OPT},
74 {"help", 0, 0, HELP_OPT},
75 {0, 0, 0, 0}
76};
77
78static void Help( void )
79{
80 puts( "Unitary test of xsh_lambda_err");
81 puts( "Usage: test_xsh_lambda_err [options] <input_files>");
82
83 puts( "Options" ) ;
84 puts( " --order-min=<n> : Minimum abs order" );
85 puts( " --order-max=<n> : Maximum abs order" );
86 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
87 puts( " --help : What you see" ) ;
88 puts( "\nInput Files" ) ;
89 puts( "The input files argument MUST be in this order:" ) ;
90 puts( " 1. Science frame in PRE format" ) ;
91 puts( " 2. Localization table" );
92 puts( " 3. SOF [SPECTRAL_FORMAT, WAVEMAP, SLITMAP]\n" ) ;
93 TEST_END();
94}
95
96static void HandleOptions( int argc, char **argv,
97 int *order_min, int *order_max)
98{
99 int opt ;
100 int option_index = 0;
101
102 while (( opt = getopt_long (argc, argv, "oversample:box-hsize:chunk-size",
103 long_options, &option_index)) != EOF ){
104
105 switch ( opt ) {
106 case MIN_ORDER_OPT:
107 sscanf( optarg, "%64d", order_min);
108 break;
109 case MAX_ORDER_OPT:
110 sscanf( optarg, "%64d", order_max);
111 break;
112 case DEBUG_OPT:
113 if ( strcmp( optarg, "LOW")==0){
115 }
116 else if ( strcmp( optarg, "HIGH")==0){
118 }
119 break;
120 default:
121 Help();
122 exit(-1);
123 }
124 }
125 return;
126}
127
128/*--------------------------------------------------------------------------
129 Implementation
130 --------------------------------------------------------------------------*/
131
139int main( int argc, char **argv)
140{
141 int ret;
142 /* Declarations */
144 const char *sof_name = NULL;
145 cpl_frameset *set = NULL;
146
147 const char* sci_name = NULL;
148 const char* loc_name = NULL;
149 int order_min = -1;
150 int order_max = -1;
151 int rec_min_index = -1;
152 int rec_max_index = -1;
153 int iorder = 0;
154
155 cpl_propertylist *header = NULL;
156 const char* pcatg = NULL;
157 xsh_order_list* order_list = NULL;
158 xsh_pre *pre = NULL;
159 float *pre_flux = NULL;
160 float *pre_errs = NULL;
161
162 cpl_image *wavemap = NULL;
163 cpl_image *slitmap = NULL;
164 float* wavemap_data = NULL;
165 float* slitmap_data = NULL;
166 int nx;
167 xsh_rec_list *rec_list = NULL;
168 xsh_localization *loc_list = NULL;
169 double obj_slit;
170 xsh_spectrum *spectrum = NULL;
171
172 char name[256];
173 FILE *file = NULL;
174 char *pch = NULL;
175 const char *prefix = NULL;
176
177 cpl_frame *sci_frame = NULL;
178 cpl_frame *loc_frame = NULL;
179 cpl_frame *orderlist_frame = NULL;
180 cpl_frame *wavemap_frame = NULL;
181 cpl_frame *slitmap_frame = NULL;
182 cpl_frame *spectralformat_frame = NULL;
183
184 /* Initialize libraries */
186 cpl_msg_set_level( CPL_MSG_DEBUG);
188
189 /* Analyse parameters */
190 HandleOptions( argc, argv, &order_min, &order_max);
191
192 if ( (argc - optind) >= 3 ) {
193 sci_name = argv[optind];
194 loc_name = argv[optind+1];
195 sof_name = argv[optind+2];
196 }
197 else{
198 Help();
199 exit(0);
200 }
201
202 /* Create frameset from sof */
203 check( set = sof_to_frameset( sof_name));
204
205 /* Validate frame set */
207
208 check( spectralformat_frame = xsh_find_spectral_format( set, instrument));
209 check( orderlist_frame = xsh_find_order_tab_edges( set, instrument));
210 check( wavemap_frame = xsh_find_wavemap( set, instrument));
211 check( slitmap_frame = xsh_find_slitmap( set, instrument));
212
213 TESTS_XSH_FRAME_CREATE( sci_frame, "OBJECT_SLIT_STARE_arm", sci_name);
214 TESTS_XSH_FRAME_CREATE( loc_frame, "LOCALIZATION_arm", loc_name);
215 /* USE load function */
216 xsh_msg("SCI : %s",
217 cpl_frame_get_filename( sci_frame));
218 xsh_msg("LOCALIZATION : %s",
219 cpl_frame_get_filename( loc_frame));
220 xsh_msg("ORDERLIST : %s",
221 cpl_frame_get_filename( orderlist_frame));
222 xsh_msg("SPECTRALFORMAT : %s",
223 cpl_frame_get_filename( spectralformat_frame));
224 xsh_msg("WAVEMAP : %s",
225 cpl_frame_get_filename( wavemap_frame));
226 xsh_msg("SLITMAP : %s",
227 cpl_frame_get_filename( slitmap_frame));
228
229 pch=strrchr( sci_name,'/');
230 if ( pch == NULL){
231 prefix = sci_name;
232 }
233 else{
234 prefix = pch+1;
235 }
236
237 check( order_list = xsh_order_list_load ( orderlist_frame, instrument));
238
239 if ( order_min != -1) {
240 check( rec_min_index = xsh_order_list_get_index_by_absorder( order_list,
241 order_min));
242 xsh_msg("Order min %d => index %d", order_min, rec_min_index);
243 }
244 else{
245 rec_min_index = 0;
246 }
247
248 if ( order_max != -1) {
249 check( rec_max_index = xsh_order_list_get_index_by_absorder( order_list,
250 order_max));
251 xsh_msg("Order max %d => index %d", order_max, rec_max_index);
252 }
253 else{
254 rec_max_index = order_list->size-1;
255 xsh_msg("Doing all %d orders", order_list->size);
256 }
257 /* load wavemap */
258 check( wavemap = cpl_image_load( cpl_frame_get_filename( wavemap_frame),
259 CPL_TYPE_FLOAT, 0, 0));
260 check( slitmap = cpl_image_load( cpl_frame_get_filename( slitmap_frame),
261 CPL_TYPE_FLOAT, 0, 0));
262
263 check( nx = cpl_image_get_size_x( wavemap));
264 check( wavemap_data = cpl_image_get_data_float( wavemap));
265 check( slitmap_data = cpl_image_get_data_float( slitmap));
266
267 check( loc_list = xsh_localization_load( loc_frame));
268 obj_slit = cpl_polynomial_eval_1d( loc_list->cenpoly,
269 600, NULL);
270
271 xsh_msg("Localization center is around %f arcsec", obj_slit);
272
273 check( header = cpl_propertylist_load( sci_name, 0));
274 pcatg = xsh_pfits_get_pcatg( header);
275
276 if (strstr( pcatg, "ORDER2D") != NULL){
277 double slit_step=0;
278 int nslit=0, s=0;
279 float *slitdata=NULL;
280
281 xsh_msg("Rectified frame");
282 check( rec_list = xsh_rec_list_load( sci_frame, instrument));
283 check( nslit = xsh_rec_list_get_nslit( rec_list, 0));
284 check( slitdata = xsh_rec_list_get_slit( rec_list, 0));
286 s = (int) xsh_round_double( (obj_slit-slitdata[0]) / slit_step);
287 xsh_msg("s index %d/%d", s, nslit);
288
289 for( iorder=rec_min_index; iorder<= rec_max_index; iorder++){
290 int nlambda=0;
291 double *lambdadata = NULL;
292 float *flux = NULL, *errs = NULL;
293 int abs_order=0;
294 int ilambda;
295
296 check( nlambda = xsh_rec_list_get_nlambda( rec_list, iorder));
297 check( abs_order = xsh_rec_list_get_order( rec_list, iorder));
298 check( lambdadata = xsh_rec_list_get_lambda( rec_list, iorder));
299 check( flux = xsh_rec_list_get_data1( rec_list, iorder));
300 check( errs = xsh_rec_list_get_errs1( rec_list, iorder));
301
302 sprintf( name, "%s_lambda_err_%d.dat", prefix, abs_order);
303
304 xsh_msg("Name %s", name);
305
306 file = fopen( name, "w");
307
308 fprintf( file, "#lambda flux err sn\n");
309
310 for( ilambda =0; ilambda < nlambda; ilambda++){
311 double lambda_v;
312 double flux_v, err_v;
313 double sn;
314
315 lambda_v = lambdadata[ilambda];
316 flux_v = flux[ilambda+nlambda*s];
317 err_v = errs[ilambda+nlambda*s];
318 sn = flux_v/err_v;
319 fprintf( file, "%f %f %f %f\n", lambda_v, flux_v, err_v,
320 sn);
321 }
322 fclose( file);
323 }
324
325 }
326 else if( strstr( pcatg, "MERGE2D") != NULL){
327 xsh_msg("Merge frame");
328 double *flux = NULL;
329 double *errs = NULL;
330 int s, ilambda;
331
332 check( spectrum = xsh_spectrum_load( sci_frame));
333 s = (int) round( (obj_slit-spectrum->slit_min) / spectrum->slit_step);
334 xsh_msg("s index %d/%d", s, spectrum->size_slit);
335 check( flux = xsh_spectrum_get_flux( spectrum));
336 check( errs = xsh_spectrum_get_errs(spectrum));
337 sprintf( name, "%s_lambda_err.dat", prefix);
338 file = fopen( name,"w");
339 fprintf( file, "#lambda flux err sn\n");
340
341 for(ilambda=0; ilambda< spectrum->size_lambda; ilambda++){
342 double lambda_v;
343 double flux_v, err_v;
344 double sn;
345
346 lambda_v = spectrum->lambda_min+ilambda*spectrum->lambda_step;
347 flux_v = flux[ilambda+s*spectrum->size_lambda];
348 err_v = errs[ilambda+s*spectrum->size_lambda];
349 sn = flux_v/err_v;
350 fprintf( file, "%f %f %f %f\n", lambda_v, flux_v, err_v, sn);
351 }
352 fclose( file);
353 }
354 else{
355 /* load image */
356 check( pre = xsh_pre_load( sci_frame, instrument));
357 check( pre_errs = cpl_image_get_data_float( pre->errs));
358 check( pre_flux = cpl_image_get_data_float( pre->data));
359
360 xsh_msg("Work with frame in PRE format binning: %dx%d", pre->binx, pre->biny);
361
362 check( xsh_order_list_set_bin_x( order_list, pre->binx));
363 check( xsh_order_list_set_bin_y( order_list, pre->biny));
364
365 xsh_msg("Generate files");
366 for( iorder=rec_min_index; iorder<= rec_max_index; iorder++){
367 int abs_order, start_y, end_y;
368 int y, x;
369
370 check( start_y = xsh_order_list_get_starty( order_list, iorder));
371 check( end_y = xsh_order_list_get_endy( order_list, iorder));
372 abs_order = order_list->list[iorder].absorder;
373
374 sprintf( name, "%s_lambda_err_%d.dat", prefix, abs_order);
375
376 file = fopen( name, "w");
377 fprintf( file, "#lambda flux err sn\n");
378
379 for(y=start_y; y <= end_y; y++){
380 double lambda;
381 double slit;
382 double err, flux;
383 double sn;
384 double diff=12;
385 int xlow, xup, xnear=0;
386
387 check( xlow = xsh_order_list_eval_int( order_list,
388 order_list->list[iorder].edglopoly, y));
389 check( xup = xsh_order_list_eval_int( order_list,
390 order_list->list[iorder].edguppoly, y));
391
392 for( x=xlow-1; x < xup; x++){
393 slit = slitmap_data[x+(y-1)*nx];
394 if ( fabs( slit-obj_slit) < diff){
395 if ( wavemap_data[x+(y-1)*nx] > 0){
396 diff = fabs( slit-obj_slit);
397 xnear = x;
398 }
399 }
400 }
401 lambda = wavemap_data[xnear+(y-1)*nx];
402 if (lambda > 0){
403 err = pre_errs[xnear+(y-1)*nx];
404 flux = pre_flux[xnear+(y-1)*nx];
405 sn = flux/err;
406 fprintf( file, "%f %f %f %f\n", lambda, flux, err, sn);
407 }
408 else {
409 xsh_msg("Skipping Lambda %f for x %d y %d", lambda, xnear, y);
410 }
411 }
412 fclose( file);
413 }
414 }
415
416 cleanup:
417 if (cpl_error_get_code() != CPL_ERROR_NONE) {
418 xsh_error_dump(CPL_MSG_ERROR);
419 ret = 1;
420 }
421 if(file != NULL) {
422 fclose( file);
423 }
424 xsh_order_list_free( &order_list);
425 xsh_pre_free( &pre);
426 xsh_free_image( &wavemap);
427 xsh_free_image( &slitmap);
428 xsh_free_propertylist( &header);
429 xsh_rec_list_free( &rec_list);
430 xsh_localization_free( &loc_list);
431 xsh_free_frame( &sci_frame);
432 xsh_free_frame( &loc_frame);
433 xsh_spectrum_free( &spectrum);
434 xsh_free_frameset( &set);
436
437 TEST_END();
438 return ret;
439}
440
int main()
Unit test of xsh_bspline_interpol.
static void HandleOptions(int argc, char **argv, int *order_min, int *order_max)
static void Help(void)
#define MODULE_ID
static struct option long_options[]
@ HELP_OPT
@ MAX_ORDER_OPT
@ DEBUG_OPT
@ MIN_ORDER_OPT
static xsh_instrument * instrument
static float slit_step
cpl_frameset * sof_to_frameset(const char *sof_name)
Definition: tests.c:576
xsh_localization * xsh_localization_load(cpl_frame *frame)
Load a localization list from a frame.
void xsh_localization_free(xsh_localization **list)
free memory associated to a localization_list
void xsh_order_list_set_bin_y(xsh_order_list *list, int bin)
Set the bin of image in y.
int xsh_order_list_get_index_by_absorder(xsh_order_list *list, double absorder)
void xsh_order_list_set_bin_x(xsh_order_list *list, int bin)
Set the bin of image in x.
int xsh_order_list_eval_int(xsh_order_list *list, cpl_polynomial *poly, double y)
Evaluate an order list poly but return the central pixel position rounding the polynomial.
xsh_order_list * xsh_order_list_load(cpl_frame *frame, xsh_instrument *instr)
load an order list from a frame
int xsh_order_list_get_starty(xsh_order_list *list, int i)
get position on Y axis of first pixel detected on order
int xsh_order_list_get_endy(xsh_order_list *list, int i)
get position on Y axis of last pixel detected on order
void xsh_order_list_free(xsh_order_list **list)
free memory associated to an order_list
xsh_pre * xsh_pre_load(cpl_frame *frame, xsh_instrument *instr)
Load a xsh_pre structure from a frame.
Definition: xsh_data_pre.c:849
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
Definition: xsh_data_pre.c:823
int xsh_rec_list_get_nslit(xsh_rec_list *list, int idx)
Definition: xsh_data_rec.c:827
double * xsh_rec_list_get_lambda(xsh_rec_list *list, int idx)
xsh_rec_list * xsh_rec_list_load(cpl_frame *frame, xsh_instrument *instrument)
load an rec list from a frame
Definition: xsh_data_rec.c:289
float * xsh_rec_list_get_data1(xsh_rec_list *list, int idx)
float * xsh_rec_list_get_errs1(xsh_rec_list *list, int idx)
int xsh_rec_list_get_order(xsh_rec_list *list, int idx)
Definition: xsh_data_rec.c:846
float * xsh_rec_list_get_slit(xsh_rec_list *list, int idx)
Definition: xsh_data_rec.c:884
int xsh_rec_list_get_nlambda(xsh_rec_list *list, int idx)
Definition: xsh_data_rec.c:865
void xsh_rec_list_free(xsh_rec_list **list)
free memory associated to a rec_list
Definition: xsh_data_rec.c:760
xsh_spectrum * xsh_spectrum_load(cpl_frame *s1d_frame)
Load a 1D spectrum structure.
double * xsh_spectrum_get_errs(xsh_spectrum *s)
Get errs of spectrum.
double * xsh_spectrum_get_flux(xsh_spectrum *s)
Get flux of spectrum.
void xsh_spectrum_free(xsh_spectrum **s)
free memory associated to an 1D spectrum
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
int * y
int * x
static SimAnneal s
Definition: xsh_model_sa.c:99
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
double xsh_pfits_get_rectify_bin_space(cpl_propertylist *plist)
find out the rectify space (slit) binning
Definition: xsh_pfits.c:3302
const char * xsh_pfits_get_pcatg(const cpl_propertylist *plist)
find out the pcatg
Definition: xsh_pfits.c:1627
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
Definition: xsh_utils.c:2116
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
void xsh_free_frameset(cpl_frameset **f)
Deallocate a frame set and set the pointer to NULL.
Definition: xsh_utils.c:2254
int xsh_debug_level_set(int level)
set debug level
Definition: xsh_utils.c:3125
long xsh_round_double(double x)
Computes round(x)
Definition: xsh_utils.c:4383
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
cpl_polynomial * cenpoly
xsh_order * list
cpl_polynomial * edguppoly
cpl_polynomial * edglopoly
cpl_image * data
Definition: xsh_data_pre.h:65
int biny
Definition: xsh_data_pre.h:80
int binx
Definition: xsh_data_pre.h:80
cpl_image * errs
Definition: xsh_data_pre.h:68
#define TESTS_XSH_FRAME_CREATE(frame, tag, name)
Definition: tests.h:123
#define TEST_END()
Definition: tests.h:111
#define TESTS_INIT(DRL_ID)
Definition: tests.h:105
int nx
cpl_frame * xsh_find_wavemap(cpl_frameset *frames, xsh_instrument *instr)
Find Wave Map frame. The frame returned should not be free by the caller.
Definition: xsh_dfs.c:3983
cpl_frame * xsh_find_spectral_format(cpl_frameset *frames, xsh_instrument *instr)
Find spectral format frame.
Definition: xsh_dfs.c:4318
cpl_frame * xsh_find_order_tab_edges(cpl_frameset *frames, xsh_instrument *instr)
Find an order tab EDGES.
Definition: xsh_dfs.c:3595
cpl_frame * xsh_find_slitmap(cpl_frameset *frames, xsh_instrument *instr)
Find a slit map.
Definition: xsh_dfs.c:3673
xsh_instrument * xsh_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset and return the instrument detected.
Definition: xsh_dfs.c:1046
@ XSH_DEBUG_LEVEL_HIGH
Definition: xsh_utils.h:138
@ XSH_DEBUG_LEVEL_LOW
Definition: xsh_utils.h:137
@ XSH_DEBUG_LEVEL_MEDIUM
Definition: xsh_utils.h:138