X-shooter Pipeline Reference Manual 3.8.15
test-xsh_rectify.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.37 $
24 */
25
26#ifdef HAVE_CONFIG_H
27# include <config.h>
28#endif
29
30/*-------------------------------------------------------------------------*/
36/*-------------------------------------------------------------------------*/
39/*--------------------------------------------------------------------------
40 Includes
41 --------------------------------------------------------------------------*/
42
43#include <tests.h>
44
45#include <xsh_data_pre.h>
46#include <xsh_error.h>
47#include <xsh_msg.h>
48#include <xsh_data_instrument.h>
49#include <xsh_data_the_map.h>
52
53#include <xsh_drl.h>
54#include <xsh_pfits.h>
55
56#include <xsh_badpixelmap.h>
57
58#include <cpl.h>
59#include <math.h>
60
61#include <getopt.h>
62
63/*--------------------------------------------------------------------------
64 Defines
65 --------------------------------------------------------------------------*/
66
67#define MODULE_ID "XSH_RECTIFY"
68
69/*--------------------------------------------------------------------------
70 Implementation
71 --------------------------------------------------------------------------*/
72enum {
76} ;
77
78static const char * Options = "" ;
79
80static struct option long_options[] = {
81 {"kernel", required_argument, 0, KERNEL_OPT},
82 {"radius", required_argument, 0, RADIUS_OPT},
83 {"bin-lambda", required_argument, 0, BIN_LAMBDA_OPT},
84 {"bin-space", required_argument, 0, BIN_SPACE_OPT},
85 {"order-min", required_argument, 0, MIN_ORDER_OPT},
86 {"order-max", required_argument, 0, MAX_ORDER_OPT},
87 {"slit-min",required_argument, 0, SLIT_MIN_OPT},
88 {"slit-n",required_argument, 0, NSLIT_OPT},
89 {"debug", required_argument, 0, DEBUG_OPT},
90 {"help", 0, 0, HELP_OPT},
91 {0, 0, 0, 0}
92};
93
94static void Help( void )
95{
96 puts( "Unitary test of xsh_rectify" ) ;
97 puts( "Usage: test_rectify [options] <input_files>" ) ;
98 puts( "Options" ) ;
99 puts( " --kernel=<name> : TANH, SINC, SINC2, LANCZOS, HAMMING, HANN [TANH]");
100 puts( " --radius=<nn> : Radius [4]" ) ;
101 puts( " --bin-lambda=<n> : Bin in Lambda [0.1]" ) ;
102 puts( " --bin-space=<n> : Bin in Slit [0.1]" ) ;
103 puts( " --order-min=<n> : Minimum abs order" );
104 puts( " --order-max=<n> : Maximum abs order" );
105 puts( " --slit-min=<n> : Minimum slit to rectify" );
106 puts( " --slit-n=<n> : Number of pixels in slit rectified frame" );
107 puts( " --debug=<n> : Level of debug NONE | LOW | MEDIUM | HIGH [MEDIUM]" );
108 puts( " --help : What you see" ) ;
109 puts( "\nInput Files" ) ;
110 puts( "The input files argument MUST be in this order:" ) ;
111 puts( " 1. Science frame in PRE format" ) ;
112 puts( " 2. SOF\n" ) ;
113 puts( " 3. [OPTIONAL] Shift slit tab\n" ) ;
114 TEST_END();
115}
116
117static void HandleOptions( int argc, char **argv,
118 xsh_rectify_param *rectify_par, int *order_min, int *order_max,
119 double *slit_min, int *nslit)
120{
121 int opt ;
122 int option_index = 0;
123
124 while (( opt = getopt_long (argc, argv, Options,
125 long_options, &option_index)) != EOF )
126 switch ( opt ) {
127 case KERNEL_OPT:
128 strcpy( rectify_par->rectif_kernel, optarg);
129 if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_TANH)) == 0){
130 rectify_par->kernel_type = CPL_KERNEL_TANH;
131 }
132 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_TANH)) == 0){
133 rectify_par->kernel_type = CPL_KERNEL_TANH;
134 }
135 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_SINC)) == 0){
136 rectify_par->kernel_type = CPL_KERNEL_SINC;
137 }
138 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_SINC2)) == 0){
139 rectify_par->kernel_type = CPL_KERNEL_SINC2;
140 }
141 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_LANCZOS)) == 0){
142 rectify_par->kernel_type = CPL_KERNEL_LANCZOS;
143 }
144 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_HAMMING)) == 0){
145 rectify_par->kernel_type = CPL_KERNEL_HAMMING;
146 }
147 else if ( strcmp( optarg, RECTIFY_KERNEL_PRINT( CPL_KERNEL_HANN)) == 0){
148 rectify_par->kernel_type = CPL_KERNEL_HANN;
149 }
150 else{
151 xsh_msg("Invalid kernel option %s", optarg);
152 Help();
153 exit(0);
154 }
155 break;
156 case RADIUS_OPT:
157 rectify_par->rectif_radius = atof( optarg);
158 break ;
159 case BIN_LAMBDA_OPT:
160 sscanf( optarg, "%64lf", &rectify_par->rectif_bin_lambda ) ;
161 break ;
162 case BIN_SPACE_OPT:
163 sscanf( optarg, "%64lf", &rectify_par->rectif_bin_space ) ;
164 break ;
165 case MIN_ORDER_OPT:
166 sscanf( optarg, "%64d", order_min);
167 break;
168 case MAX_ORDER_OPT:
169 sscanf( optarg, "%64d", order_max);
170 break;
171 case SLIT_MIN_OPT:
172 sscanf( optarg, "%64lf", slit_min) ;
173 break ;
174 case NSLIT_OPT:
175 sscanf( optarg, "%64d", nslit);
176 break ;
177 case DEBUG_OPT:
178 if ( strcmp( optarg, "LOW")==0){
180 }
181 else if ( strcmp( optarg, "HIGH")==0){
183 }
184 else if ( strcmp( optarg, "NONE")==0){
186 }
187 break;
188 default: Help() ; exit( 0 ) ;
189 }
190}
191
200int main( int argc, char **argv)
201{
202 /* Declarations */
203 int ret = 0 ;
204
206
207 const char *sof_name = NULL;
208 cpl_frameset *set = NULL;
209 const char * sci_name = NULL ;
210
211 cpl_frameset *wavesol_frameset = NULL;
212 cpl_frameset *result_frameset = NULL;
213 cpl_frameset *merge_frameset = NULL;
214 cpl_frame *sci_frame = NULL;
215 cpl_frame *orderlist_frame = NULL;
216 cpl_frame *slitmap_frame = NULL;
217 cpl_frame *wavesol_frame = NULL;
218 cpl_frame *model_frame = NULL;
219 cpl_frame *spectralformat_frame = NULL;
220 cpl_frame *recorder_frame = NULL ;
221 cpl_frame *recordereso_frame = NULL ;
222 cpl_frame *recordertab_frame = NULL ;
223 int order_min = -1, order_max = -1;
224 int rec_min_index=-1, rec_max_index=-1;
225 xsh_order_list* order_list = NULL;
226 xsh_rectify_param rectify_par;
227 int nslit_c=20, nslit=-100;
228 double slit_min_c=0, slit_min=-100;
230 int merge_par = 0;
231 cpl_frame *spectrum_frame = NULL;
232 cpl_frame *slit_shifttab_frame = NULL;
233 const char *slit_shifttab_name = NULL;
234 cpl_frameset *slit_shifttab_frameset = NULL;
235 const int decode_bp=2147483647;
236
237 /* Initialize libraries */
239 cpl_msg_set_level(CPL_MSG_DEBUG);
241
242 /* Set rectify default params */
243 strcpy( rectify_par.rectif_kernel, "default");
244 rectify_par.kernel_type = CPL_KERNEL_TANH;
245 rectify_par.rectif_radius = 4 ;
246 rectify_par.rectif_bin_lambda = 0.1 ;
247 rectify_par.rectif_bin_space = 0.1 ;
248 rectify_par.conserve_flux = FALSE;
249 rectify_par.rectify_full_slit=CPL_TRUE;
250
251 HandleOptions( argc, argv, &rectify_par, &order_min, &order_max,
252 &slit_min, &nslit);
253
254 if ( (argc - optind) >=2 ) {
255 sci_name = argv[optind];
256 sof_name = argv[optind+1];
257 if ( (argc - optind) >=3 ) {
258 slit_shifttab_name = argv[optind+2];
259 }
260 }
261 else {
262 Help();
263 exit(0);
264 }
265 /* Create frameset from sof */
266 check( set = sof_to_frameset( sof_name));
267 /* Validate frame set */
269
270 check( spectralformat_frame = xsh_find_spectral_format( set, instrument));
271 check( orderlist_frame = xsh_find_order_tab_edges( set, instrument));
272 check( slitmap_frame = xsh_find_slitmap( set, instrument));
273
274 TESTS_XSH_FRAME_CREATE( sci_frame, "SLIT_STARE_DIVIDED_arm", sci_name);
277 if ( slit_shifttab_name != NULL){
278 if ( mode == XSH_MODE_SLIT){
279 TESTS_XSH_FRAME_CREATE( slit_shifttab_frame, "SHIFTTAB_arm", slit_shifttab_name);
280 }
281 else{
282 int i;
283 slit_shifttab_frameset = cpl_frameset_new();
284 for( i=0; i< 3; i++){
285 TESTS_XSH_FRAME_CREATE( slit_shifttab_frame, "SHIFTTAB_arm", slit_shifttab_name);
286 cpl_frameset_insert( slit_shifttab_frameset, slit_shifttab_frame);
287 }
288 }
289 }
290 xsh_msg("SCI : %s",
291 cpl_frame_get_filename( sci_frame));
292 xsh_msg("ORDERLIST : %s",
293 cpl_frame_get_filename( orderlist_frame));
294 xsh_msg("SPECTRALFORMAT : %s",
295 cpl_frame_get_filename( spectralformat_frame));
296 if ( slitmap_frame != NULL){
297 xsh_msg("SLITMAP : %s",
298 cpl_frame_get_filename( slitmap_frame));
299 }
300 if ( slit_shifttab_frame != NULL){
301 xsh_msg("SHIFTTAB : %s",
302 cpl_frame_get_filename( slit_shifttab_frame));
303 }
304
305 if ( mode == XSH_MODE_SLIT){
306 wavesol_frame = xsh_find_wave_tab_2d( set, instrument);
307
308 if ( wavesol_frame == NULL){
309 model_frame = xsh_find_model_config_tab( set, instrument);
310 }
311
312 if ( wavesol_frame != NULL){
313 xsh_msg("WAVESOL : %s",
314 cpl_frame_get_filename( wavesol_frame));
315 }
316 else {
317 xsh_msg("MODEL : %s",
318 cpl_frame_get_filename( model_frame));
319 }
320 }
321 else{
322 wavesol_frameset = xsh_find_wave_tab_ifu( set, instrument);
323 cpl_error_reset();
324 if ( wavesol_frameset == NULL){
325 model_frame = xsh_find_model_config_tab( set, instrument);
326 xsh_msg("MODEL : %s",
327 cpl_frame_get_filename( model_frame));
328 }
329 else{
330 int iframe;
331 int size;
332
333 size = cpl_frameset_get_size( wavesol_frameset);
334 for(iframe=0; iframe < size; iframe++){
335 cpl_frame *frame = cpl_frameset_get_frame(wavesol_frameset, iframe);
336 xsh_msg("WAVESOL : %s",
337 cpl_frame_get_filename( frame));
338 }
339 }
340 }
341
342 xsh_msg(" Parameters ");
343 xsh_msg(" kernel %s", RECTIFY_KERNEL_PRINT( rectify_par.kernel_type));
344 xsh_msg(" radius %f", rectify_par.rectif_radius);
345 xsh_msg(" bin-space %f", rectify_par.rectif_bin_space);
346 xsh_msg(" bin-lambda %f", rectify_par.rectif_bin_lambda);
347
348
349 check( order_list = xsh_order_list_load ( orderlist_frame, instrument));
350
351 if ( order_min != -1) {
352 check( rec_min_index = xsh_order_list_get_index_by_absorder( order_list,
353 order_min));
354 xsh_msg("Order min %d => index %d", order_min, rec_min_index);
355 }
356 else{
357 rec_min_index = 0;
358 }
359
360 if ( order_max != -1) {
361 check( rec_max_index = xsh_order_list_get_index_by_absorder( order_list,
362 order_max));
363 xsh_msg("Order max %d => index %d", order_max, rec_max_index);
364 }
365 else{
366 rec_max_index = order_list->size;
367 }
368
369
370 /* Now rectify the frame */
371 check( xsh_rec_slit_size( &rectify_par,
372 &slit_min_c, &nslit_c, mode));
373
374 if (slit_min == -100){
375 slit_min = slit_min_c;
376 }
377 if ( nslit == -100){
378 nslit = nslit_c;
379 }
380
381 if ( mode == XSH_MODE_SLIT){
382 const char *tag = XSH_GET_TAG_FROM_ARM( XSH_ORDER2D, instrument);
383 check( recorder_frame = xsh_rectify_orders( sci_frame, order_list,
384 wavesol_frame, model_frame, instrument, &rectify_par,
385 spectralformat_frame, NULL, "RECTIFIED.fits", tag, &recordereso_frame,
386 &recordertab_frame,rec_min_index, rec_max_index, slit_min, nslit, 0,
387 slit_shifttab_frame));
388 check( spectrum_frame = xsh_merge_ord( recorder_frame, instrument,
389 merge_par,"test"));
390 }
391 else{
392 xsh_msg("Rectify in IFU");
393 check( result_frameset = xsh_rectify_orders_ifu( sci_frame, order_list,
394 wavesol_frameset, slit_shifttab_frameset, model_frame, instrument, &rectify_par,
395 spectralformat_frame, slitmap_frame,NULL,NULL,rec_min_index,
396 rec_max_index, "test"));
397 xsh_msg( "Merge in IFU");
398 check( merge_frameset = xsh_merge_ord_ifu( result_frameset, instrument, merge_par,"test"));
399 }
400
401 cleanup:
402 if (cpl_error_get_code() != CPL_ERROR_NONE) {
403 xsh_error_dump(CPL_MSG_ERROR);
404 ret = 1;
405 }
406 xsh_free_frame( &slit_shifttab_frame);
407 xsh_order_list_free( &order_list);
408 xsh_free_frame( &sci_frame);
409 xsh_free_frameset( &result_frameset);
410 xsh_free_frameset( &wavesol_frameset);
411 xsh_free_frameset( &set);
412 xsh_free_frameset(&merge_frameset);
414 xsh_free_frame( &recorder_frame);
415 xsh_free_frame( &recordereso_frame);
416 xsh_free_frame( &recordertab_frame);
417 xsh_free_frame( &spectrum_frame);
418 TEST_END();
419 return ret;
420}
int main()
Unit test of xsh_bspline_interpol.
static char mode[32]
static const char * Options
static void HandleOptions(int argc, char **argv, xsh_rectify_param *rectify_par, int *order_min, int *order_max, double *slit_min, int *nslit)
#define MODULE_ID
static struct option long_options[]
@ KERNEL_OPT
@ HELP_OPT
@ MAX_ORDER_OPT
@ RADIUS_OPT
@ BIN_SPACE_OPT
@ DEBUG_OPT
@ NSLIT_OPT
@ MIN_ORDER_OPT
@ SLIT_MIN_OPT
@ BIN_LAMBDA_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
XSH_MODE xsh_instrument_get_mode(xsh_instrument *i)
Get a mode on instrument structure.
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_frameset * xsh_merge_ord_ifu(cpl_frameset *rec_frameset, xsh_instrument *instrument, int merge_par, const char *rec_prefix)
Merge orders of the rectified frame using merge parameters.
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.
int size
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
cpl_frameset * xsh_rectify_orders_ifu(cpl_frame *sci_frame, xsh_order_list *orderlist, cpl_frameset *wavesol_frameset, cpl_frameset *shift_frameset, cpl_frame *model_frame, xsh_instrument *instrument, xsh_rectify_param *rectify_par, cpl_frame *spectralformat_frame, cpl_frame *slitmap_frame, cpl_frameset **rec_frameset_ext, cpl_frameset **rec_frameset_tab, int min_index, int max_index, const char *rec_prefix)
Definition: xsh_rectify.c:1514
cpl_frame * xsh_rectify_orders(cpl_frame *sci_frame, xsh_order_list *orderlist_frame, cpl_frame *wavesol_frame, cpl_frame *model_frame, xsh_instrument *instrument, xsh_rectify_param *rectify_par, cpl_frame *spectralformat_frame, cpl_frame *disp_tab_frame, const char *res_name, const char *tag, cpl_frame **res_frame_ext, cpl_frame **res_frame_tab, int min_index, int max_index, double slit_min, int nslit, double slit_shift, cpl_frame *slitshift_tab)
Create a grid in wavelength with the step in lambda and slit. Steps are defined in the parameters....
Definition: xsh_rectify.c:1079
void xsh_rec_slit_size(xsh_rectify_param *rectify_par, double *slit_min, int *nslit, XSH_MODE mode)
rectify frame
Definition: xsh_rectify.c:758
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
cpl_kernel kernel_type
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
@ XSH_MODE_SLIT
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_frameset * xsh_find_wave_tab_ifu(cpl_frameset *frames, xsh_instrument *instrument)
Find wave tab ARC (for IFU 3 frames)
Definition: xsh_dfs.c:3756
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_model_config_tab(cpl_frameset *frames, xsh_instrument *instr)
Find a model configuration table frame.
Definition: xsh_dfs.c:3957
#define XSH_ORDER2D
Definition: xsh_dfs.h:588
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
Definition: xsh_dfs.h:1548
#define RECTIFY_KERNEL_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