X-shooter Pipeline Reference Manual 3.8.15
test-xsh_opt_extract.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: 2013-05-14 07:02:49 $
23 * $Revision: 1.30 $
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_drl.h>
50#include <xsh_pfits.h>
51#include <xsh_model_utils.h>
52#include <xsh_badpixelmap.h>
53
54#include <cpl.h>
55#include <math.h>
56
57#include <getopt.h>
58
59/*--------------------------------------------------------------------------
60 Defines
61 --------------------------------------------------------------------------*/
62
63#define MODULE_ID "XSH_OPT_EXTRACT"
64
65enum {
69};
70
71static struct option long_options[] = {
72 {"oversample", required_argument, 0, OVERSAMPLE_OPT},
73 {"box-hsize", required_argument, 0, BOX_HSIZE_OPT},
74 {"chunk-size", required_argument, 0, CHUNK_SIZE_OPT},
75 {"lambda-step", required_argument, 0, LAMBDA_STEP_OPT},
76 {"clip-kappa", required_argument, 0, CLIP_KAPPA_OPT},
77 {"clip-frac", required_argument, 0, CLIP_FRAC_OPT},
78 {"clip-niter", required_argument, 0, CLIP_NITER_OPT},
79 {"niter", required_argument, 0, NITER_OPT},
80 {"method", required_argument, 0, METHOD_OPT},
81 {"order-min", required_argument, 0, MIN_ORDER_OPT},
82 {"order-max", required_argument, 0, MAX_ORDER_OPT},
83 {"debug", required_argument, 0, DEBUG_OPT},
84 {"help", 0, 0, HELP_OPT},
85 {0, 0, 0, 0}
86};
87
88static void Help( void )
89{
90 puts( "Unitary test of xsh_opt_extract");
91 puts( "Usage: test_xsh_opt_extract [options] <input_files>");
92
93 puts( "Options" ) ;
94 puts( " --oversample=<n> : Oversample factor [5]" ) ;
95 puts( " --box-hsize=<n> : Extract box half size [pixels] [10]" ) ;
96 puts( " --chunk-size=<n> : Chunk size [pixels] [50]" ) ;
97 puts( " --lambda-step=<n> : Step in wavelength [0.02]" );
98 puts( " --clip-kappa=<nn> : Kappa for cosmics Ray hits rejection [3]" ) ;
99 puts( " --clip-frac=<nn> : Maxium bad pixels fraction for cosmics Ray hits rejection [0.4]" );
100 puts( " --clip-niter=<n> : Number of iterations for cosmics Ray hits rejection [2]" ) ;
101 puts( " --niter=<n> : Number of iterations [1]" ) ;
102 puts( " --method=<string> : Extraction method GAUSSIAN | GENERAL [GAUSSIAN]" ) ;
103 puts( " --order-min=<n> : Minimum abs order" );
104 puts( " --order-max=<n> : Maximum abs order" );
105 puts( " --debug=<n> : Level of debug NONE | LOW | MEDIUM | HIGH [MEDIUM]" );
106 puts( " --help : What you see" ) ;
107 puts( "\nInput Files" ) ;
108 puts( "The input files argument MUST be in this order:" ) ;
109 puts( " 1. Science frame in PRE format sky subtracted" ) ;
110 puts( " 2. Localization table" );
111 puts( " 3. SOF [SPECTRAL_FORMAT, ORDER_TAB_EDGES,WAVE_TAB_2D, WAVEMAP, SLITMAP, MASTER_FLAT]\n" ) ;
112 TEST_END();
113}
114
115static void HandleOptions( int argc, char **argv,
116 xsh_opt_extract_param *opt_extract_par, int *order_min, int *order_max)
117{
118 int opt ;
119 int option_index = 0;
120
121 while (( opt = getopt_long (argc, argv, "oversample:box-hsize:chunk-size",
122 long_options, &option_index)) != EOF ){
123
124 switch ( opt ) {
125 case OVERSAMPLE_OPT:
126 opt_extract_par->oversample = atoi( optarg);
127 break ;
128 case BOX_HSIZE_OPT:
129 opt_extract_par->box_hsize = atoi( optarg);
130 break ;
131 case CHUNK_SIZE_OPT:
132 opt_extract_par->chunk_size = atoi( optarg);
133 break;
134 case LAMBDA_STEP_OPT:
135 opt_extract_par->lambda_step = atof( optarg);
136 break;
137 case CLIP_KAPPA_OPT:
138 opt_extract_par->clip_kappa = atof( optarg);
139 break;
140 case CLIP_FRAC_OPT:
141 opt_extract_par->clip_frac = atof( optarg);
142 break;
143 case CLIP_NITER_OPT:
144 opt_extract_par->clip_niter = atoi( optarg);
145 break;
146 case NITER_OPT:
147 opt_extract_par->niter = atoi( optarg);
148 break;
149 case METHOD_OPT:
150 if ( strcmp( OPTEXTRACT_METHOD_PRINT(GAUSS_METHOD), optarg) == 0){
151 opt_extract_par->method = GAUSS_METHOD;
152 }
153 else {
154 opt_extract_par->method = GENERAL_METHOD;
155 }
156 break;
157 case MIN_ORDER_OPT:
158 sscanf( optarg, "%64d", order_min);
159 break;
160 case MAX_ORDER_OPT:
161 sscanf( optarg, "%64d", order_max);
162 break;
163 case DEBUG_OPT:
164 if ( strcmp( optarg, "LOW")==0){
166 }
167 else if ( strcmp( optarg, "HIGH")==0){
169 }
170 else if ( strcmp( optarg, "NONE")==0){
172 }
173 break;
174 default:
175 Help();
176 exit(-1);
177 }
178 }
179 return;
180}
181
182/*--------------------------------------------------------------------------
183 Implementation
184 --------------------------------------------------------------------------*/
185
193int main( int argc, char **argv)
194{
195 int ret;
196 /* Declarations */
198 const char *sof_name = NULL;
199 cpl_frameset *set = NULL;
200
201 const char* sci_name = NULL;
202 const char* loc_name = NULL;
203 xsh_opt_extract_param opt_extract_par = { 5, 10, 10, 0.01, 10, 1., 2, 2, GAUSS_METHOD};
204 int merge_par = 0; /* MEAN_MERGE_METHOD */
205 int order_min = -1;
206 int order_max = -1;
207 int rec_min_index = -1;
208 int rec_max_index = -1;
209
210 xsh_order_list* order_list = NULL;
211
212 cpl_frame *sci_frame = NULL;
213 cpl_frame *loc_frame = NULL;
214 cpl_frame *orderlist_frame = NULL;
215 cpl_frame *wavesol_frame = NULL;
216 cpl_frame *model_frame = NULL;
217 cpl_frame *wavemap_frame = NULL;
218 cpl_frame *slitmap_frame = NULL;
219 cpl_frame *spectralformat_frame = NULL;
220 cpl_frame *masterflat_frame = NULL;
221 cpl_frame *orderext1d_frame = NULL;
222 cpl_frame *orderoxt1d_frame = NULL;
223 cpl_frame *orderoxt1d_eso_frame = NULL;
224 cpl_frame *qc_subex_frame = NULL;
225 cpl_frame *qc_s2ddiv1d_frame = NULL;
226 cpl_frame *qc_model_frame = NULL;
227 cpl_frame *qc_weight_frame = NULL;
228
229 cpl_frame *spectrumext1d_frame = NULL;
230 cpl_frame *spectrumoxt1d_frame = NULL;
231 const int decode_bp=2147483647;
232
233 /* Initialize libraries */
235 cpl_msg_set_level( CPL_MSG_DEBUG);
237
238 opt_extract_par.oversample = 5;
239 opt_extract_par.box_hsize = 10;
240 opt_extract_par.chunk_size = 50;
241 opt_extract_par.lambda_step = 0.02;
242 opt_extract_par.clip_kappa = 3;
243 opt_extract_par.clip_frac = 0.4;
244 opt_extract_par.clip_niter = 2;
245 opt_extract_par.niter = 1;
246 opt_extract_par.method = GAUSS_METHOD;
247
248 /* Analyse parameters */
249 HandleOptions( argc, argv, &opt_extract_par, &order_min, &order_max);
250
251 if ( (argc - optind) >= 3 ) {
252 sci_name = argv[optind];
253 loc_name = argv[optind+1];
254 sof_name = argv[optind+2];
255 }
256 else{
257 Help();
258 exit(0);
259 }
260
261 /* Create frameset from sof */
262 check( set = sof_to_frameset( sof_name));
263
264 /* Validate frame set */
266
267 check( spectralformat_frame = xsh_find_spectral_format( set, instrument));
268 check( orderlist_frame = xsh_find_order_tab_edges( set, instrument));
269 check( wavemap_frame = xsh_find_wavemap( set, instrument));
270 check( slitmap_frame = xsh_find_slitmap( set, instrument));
271 check( masterflat_frame = xsh_find_master_flat( set, instrument));
273
274 if(( model_frame = xsh_find_frame_with_tag( set, XSH_MOD_CFG_OPT_2D,
275 instrument)) == NULL) {
277 if ((model_frame = xsh_find_frame_with_tag( set,XSH_MOD_CFG_TAB,
278 instrument)) == NULL) {
280 }
281 }
282 if ( model_frame == NULL){
283 check( wavesol_frame = xsh_find_wave_tab_2d( set, instrument));
284 }
285 TESTS_XSH_FRAME_CREATE( sci_frame, "OBJECT_SLIT_STARE_arm", sci_name);
286 TESTS_XSH_FRAME_CREATE( loc_frame, "LOCALIZATION_arm", loc_name);
287 /* USE load function */
288 xsh_msg("SCI : %s",
289 cpl_frame_get_filename( sci_frame));
290 xsh_msg("LOCALIZATION : %s",
291 cpl_frame_get_filename( loc_frame));
292 xsh_msg("ORDERLIST : %s",
293 cpl_frame_get_filename( orderlist_frame));
294 if (model_frame != NULL){
295 int found_line=0;
296 check(xsh_model_temperature_update_frame(&model_frame,sci_frame,
297 instrument,&found_line));
298 xsh_msg("MODEL : %s",
299 cpl_frame_get_filename( model_frame));
300 }
301 else{
302 xsh_msg("WAVESOL : %s",
303 cpl_frame_get_filename( wavesol_frame));
304 }
305 xsh_msg("SPECTRALFORMAT : %s",
306 cpl_frame_get_filename( spectralformat_frame));
307 xsh_msg("WAVEMAP : %s",
308 cpl_frame_get_filename( wavemap_frame));
309 xsh_msg("SLITMAP : %s",
310 cpl_frame_get_filename( slitmap_frame));
311 xsh_msg("MASTERFLAT : %s",
312 cpl_frame_get_filename( masterflat_frame));
313
314 xsh_msg(" Parameters ");
315 xsh_msg(" oversample %d", opt_extract_par.oversample);
316 xsh_msg(" box-hsize %d", opt_extract_par.box_hsize);
317 xsh_msg(" chunk-size %d", opt_extract_par.chunk_size);
318 xsh_msg(" lambda-step %f", opt_extract_par.lambda_step);
319 xsh_msg(" clip-kappa %f", opt_extract_par.clip_kappa);
320 xsh_msg(" clip-frac %f", opt_extract_par.clip_frac);
321 xsh_msg(" clip-niter %d", opt_extract_par.clip_niter);
322 xsh_msg(" niter %d", opt_extract_par.niter);
323 xsh_msg(" method %s", OPTEXTRACT_METHOD_PRINT(opt_extract_par.method));
324
325 check( order_list = xsh_order_list_load ( orderlist_frame, instrument));
326
327 if ( order_min != -1) {
328 check( rec_min_index = xsh_order_list_get_index_by_absorder( order_list,
329 order_min));
330 xsh_msg("Order min %d => index %d", order_min, rec_min_index);
331 }
332 else{
333 rec_min_index = 0;
334 }
335
336 if ( order_max != -1) {
337 check( rec_max_index = xsh_order_list_get_index_by_absorder( order_list,
338 order_max));
339 xsh_msg("Order max %d => index %d", order_max, rec_max_index);
340 }
341 else{
342 rec_max_index = order_list->size;
343 }
344
345 check( xsh_opt_extract_orders( sci_frame, orderlist_frame, wavesol_frame, model_frame,
346 wavemap_frame, slitmap_frame, loc_frame, spectralformat_frame, masterflat_frame,
347 instrument, &opt_extract_par, rec_min_index, rec_max_index, "TEST",
348 &orderext1d_frame, &orderoxt1d_frame,
349 &orderoxt1d_eso_frame, &qc_subex_frame, &qc_s2ddiv1d_frame,
350 &qc_model_frame, &qc_weight_frame));
351
352 check( spectrumext1d_frame = xsh_merge_ord( orderext1d_frame, instrument, merge_par,"test"));
353 check( spectrumoxt1d_frame = xsh_merge_ord( orderoxt1d_frame, instrument, merge_par,"test"));
354
355 cleanup:
356 if (cpl_error_get_code() != CPL_ERROR_NONE) {
357 xsh_error_dump(CPL_MSG_ERROR);
358 ret = 1;
359 }
360 xsh_order_list_free( &order_list);
361
362 xsh_free_frame( &sci_frame);
363 xsh_free_frame( &loc_frame);
364 xsh_free_frameset( &set);
366 xsh_free_frame( &orderext1d_frame);
367 xsh_free_frame( &orderoxt1d_frame);
368 xsh_free_frame( &orderoxt1d_eso_frame);
369 xsh_free_frame( &qc_subex_frame);
370 xsh_free_frame(&qc_s2ddiv1d_frame);
371 xsh_free_frame(&qc_model_frame);
372 xsh_free_frame(&qc_weight_frame);
373
374 xsh_free_frame( &spectrumext1d_frame);
375 xsh_free_frame( &spectrumoxt1d_frame);
376
377 TEST_END();
378 return ret;
379}
380
int main()
Unit test of xsh_bspline_interpol.
static void HandleOptions(int argc, char **argv, xsh_opt_extract_param *opt_extract_par, int *order_min, int *order_max)
#define MODULE_ID
static struct option long_options[]
@ METHOD_OPT
@ HELP_OPT
@ BOX_HSIZE_OPT
@ OVERSAMPLE_OPT
@ CLIP_NITER_OPT
@ MAX_ORDER_OPT
@ LAMBDA_STEP_OPT
@ NITER_OPT
@ CLIP_FRAC_OPT
@ DEBUG_OPT
@ CHUNK_SIZE_OPT
@ MIN_ORDER_OPT
@ CLIP_KAPPA_OPT
static xsh_instrument * instrument
cpl_frameset * sof_to_frameset(const char *sof_name)
Definition: tests.c:576
int xsh_order_list_get_index_by_absorder(xsh_order_list *list, double absorder)
xsh_order_list * xsh_order_list_load(cpl_frame *frame, xsh_instrument *instr)
load an order list from a frame
void xsh_order_list_free(xsh_order_list **list)
free memory associated to an order_list
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
#define xsh_error_reset()
Definition: xsh_error.h:87
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
void xsh_instrument_set_decode_bp(xsh_instrument *i, const int decode_bp)
Set bad pixel code.
cpl_frame * xsh_merge_ord(cpl_frame *sci_frame, xsh_instrument *instrument, int merge, const char *rec_prefix)
Merge orders of the rectified frame using merge parameters.
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
void xsh_opt_extract_orders(cpl_frame *sci_frame, cpl_frame *orderlist_frame, cpl_frame *wavesol_frame, cpl_frame *model_frame, cpl_frame *wavemap_frame, cpl_frame *slitmap_frame, cpl_frame *loc_frame, cpl_frame *spectralformat_frame, cpl_frame *masterflat_frame, xsh_instrument *instrument, xsh_opt_extract_param *opt_extract_par, int min_index, int max_index, const char *rec_refix, cpl_frame **orderext1d_frame, cpl_frame **orderoxt1d_frame, cpl_frame **orderoxt1d_eso_frame, cpl_frame **qc_subextract_frame, cpl_frame **qc_s2ddiv1d_frame, cpl_frame **qc_model_frame, cpl_frame **qc_weight_frame)
Create a SPECTRUM 1D from a Science frame.
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
enum optextract_method method
static void Help(void)
#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
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_wave_tab_2d(cpl_frameset *frames, xsh_instrument *instr)
Find a wave tab 2D.
Definition: xsh_dfs.c:3650
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
cpl_frame * xsh_find_frame_with_tag(cpl_frameset *frames, const char *tag, xsh_instrument *instr)
Find frame with a given tag.
Definition: xsh_dfs.c:3347
cpl_frame * xsh_find_master_flat(cpl_frameset *frames, xsh_instrument *instr)
Find master flat frame.
Definition: xsh_dfs.c:3426
#define XSH_MOD_CFG_OPT_2D
Definition: xsh_dfs.h:1233
#define XSH_MOD_CFG_TAB
Definition: xsh_dfs.h:1251
cpl_error_code xsh_model_temperature_update_frame(cpl_frame **model_config_frame, cpl_frame *ref_frame, xsh_instrument *instrument, int *found_temp)
@ GENERAL_METHOD
@ GAUSS_METHOD
#define OPTEXTRACT_METHOD_PRINT(method)
@ XSH_DEBUG_LEVEL_HIGH
Definition: xsh_utils.h:138
@ XSH_DEBUG_LEVEL_NONE
Definition: xsh_utils.h:137
@ XSH_DEBUG_LEVEL_LOW
Definition: xsh_utils.h:137
@ XSH_DEBUG_LEVEL_MEDIUM
Definition: xsh_utils.h:138