X-shooter Pipeline Reference Manual 3.8.15
test-xsh_subtract_sky_single.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-04-02 07:36:38 $
23 * $Revision: 1.28 $
24*/
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#endif
28
29/*--------------------------------------------------------------------------*/
35/*--------------------------------------------------------------------------*/
38/*---------------------------------------------------------------------------
39 Includes
40 ---------------------------------------------------------------------------*/
41
42#include <xsh_data_instrument.h>
43#include <xsh_pfits.h>
44#include <xsh_drl_check.h>
45#include <xsh_msg.h>
46#include <xsh_utils.h>
47#include <tests.h>
48#include <cpl.h>
49#include <math.h>
50#include <stdlib.h>
51#include <getopt.h>
52#include <xsh_cpl_size.h>
53
54/*---------------------------------------------------------------------------
55 Defines
56 ---------------------------------------------------------------------------*/
57#define MODULE_ID "XSH_SUBTRACT_SKY_SINGLE"
58
59#define SYNTAX "Test the xsh_subtract_sky_single function\n"\
60 "usage :\n test_xsh_subtract_sky_single [<opt>] SCI_FRAME ORDER_TABLE SLITMAP WAVEMAP [LOCALIZATION} \n" \
61 "SCI_FRAME => Science frame NOCOSMIC and flat field [DIV_FF]\n"\
62 "ORDER_TABLE => Order table frame\n"\
63 "SLITMAP => Slit Map Frame\n"\
64 "WAVEMAP => Wave Map frame\n"\
65 "LOCALIZATION => Localization frame (optional)\n\n"\
66 "Options:\n"\
67 " --nbkpts=<nn> : Number of break points (default 100)\n"\
68 " --method=<n> : 0 => BSPLINE 1 => MEDIAN [BSPLINE]\n"\
69 " --pos1=<n> : Sky position 1 [0]\n"\
70 " --hheight1=<n> : Sky half height 1 [0]\n"\
71 " --pos2=<n> : Sky position 2 [0]\n"\
72 " --hheight2=<n> : Sky half height 2 [0]\n"\
73 " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]\n"\
74 "The output file (after subtraction) is 'SUBTRACTED_FLUX.fits'\n"
75
76
77static const char * Options = "?" ;
78
79enum {
87} ;
88
89static struct option LongOptions[] = {
90 {"nbkpts", required_argument, 0, NBKPTS_OPT},
91 {"method", required_argument, 0, METHOD_OPT},
92 {"pos1", required_argument, 0, POS1_OPT},
93 {"hheight1", required_argument, 0, HHEIGHT1_OPT},
94 {"pos2", required_argument, 0, POS2_OPT},
95 {"hheight2", required_argument, 0, HHEIGHT2_OPT},
96 {"debug", required_argument, 0, DEBUG_OPT},
97 {NULL, 0, 0, 0}
98} ;
99
100static void HandleOptions( int argc, char ** argv, xsh_subtract_sky_single_param* sky_par)
101{
102 int opt ;
103 int option_index = 0;
104
105 while( (opt = getopt_long( argc, argv, Options,
106 LongOptions, &option_index )) != EOF )
107 switch( opt ) {
108 case NBKPTS_OPT:
109 sscanf( optarg, "%64d", &(sky_par->nbkpts1) ) ;
110 break ;
111 case METHOD_OPT:
112 sscanf( optarg, "%64d", &(sky_par->method));
113 break ;
114 case POS1_OPT:
115 sky_par->pos1 = atof( optarg);
116 break ;
117 case HHEIGHT1_OPT:
118 sky_par->hheight1 = atof(optarg);
119 break ;
120 case POS2_OPT:
121 sky_par->pos2 = atof( optarg);
122 break ;
123 case HHEIGHT2_OPT:
124 sky_par->hheight2 = atof(optarg);
125 break ;
126 case DEBUG_OPT:
127 if ( strcmp( optarg, "LOW")==0){
129 }
130 else if ( strcmp( optarg, "HIGH")==0){
132 }
133 break;
134 default:
135 printf( SYNTAX);
136 exit( 0 ) ;
137 }
138}
139
140static void get_max_pos( cpl_frame * frame, xsh_instrument * instrument )
141{
142 cpl_size x = 0, y = 0 ;
143 xsh_pre * pre = NULL ;
144 double max = 0 ;
145
146 /* Load pre frame */
147 pre = xsh_pre_load( frame, instrument ) ;
148 max = cpl_image_get_max( pre->data ) ;
149 cpl_image_get_maxpos( pre->data, &x, &y ) ;
150 xsh_msg( "Maximum value: %lf at %" CPL_SIZE_FORMAT ",%" CPL_SIZE_FORMAT "", max, x, y ) ;
151 xsh_pre_free( &pre ) ;
152}
153
154/*--------------------------------------------------------------------------*/
161/*--------------------------------------------------------------------------*/
162
163int main(int argc, char** argv)
164{
165 cpl_frame * sci_frame = NULL ;
166 cpl_frame * order_frame = NULL ;
167 cpl_frame * slitmap_frame = NULL ;
168 cpl_frame * wavemap_frame= NULL ;
169 cpl_frame * local_frame = NULL ;
170 cpl_frame * sky_line_list_frame = NULL ;
171
172 char * sci_name = NULL, * order_name = NULL, * slitmap_name = NULL,
173 * wavemap_name = NULL, * local_name = NULL , * sky_line_list_name = NULL;
174
175 xsh_instrument * instrument = NULL ;
176 cpl_frame * sky_spectrum = NULL ;
177 cpl_frame * sky_spectrum_eso = NULL ;
178 //cpl_frame *sky_frame_ima = NULL;
179
180 cpl_frame * result = NULL ;
181 xsh_subtract_sky_single_param sky_par = {1000, 1000,7,20, 5., -1, -1,
182 BSPLINE_METHOD,FINE, 7, 1.5,
183 0.,0.,0.,0.};
184 int nb_frames = 0 ;
185 char * tag ;
187 cpl_propertylist *plist = NULL;
188 int ret=0;
189 const int decode_bp=2140143615;
191 cpl_msg_set_level(CPL_MSG_DEBUG);
193
194 /* init parameters */
195 sky_par.slit_edges_mask = 0.5;
196 sky_par.median_hsize = 20;
197 sky_par.nbkpts1 = 3000;
198 sky_par.nbkpts2 = 3000;
199 sky_par.bezier_spline_order = 4;
200 sky_par.niter = 1;
201 sky_par.ron = -1;
202 sky_par.kappa = 5;
203 sky_par.gain = -1;
204 sky_par.bspline_sampling = FINE;
205 sky_par.pos1 = 0.0;
206 sky_par.hheight1 = 0.0;
207 sky_par.pos2 = 0.0;
208 sky_par.hheight2 = 0.0;
209
210 /* Analyse parameters */
211 HandleOptions( argc, argv, &sky_par );
212
213 xsh_msg( "Nb of breakpts = %d", sky_par.nbkpts1);
214
215 /* Retrieve arguments */
216 nb_frames = argc - optind;
217 xsh_msg( "Nb of inputs = %d", nb_frames);
218 if ( nb_frames >= 4 ) {
219 sci_name = argv[optind] ;
220 order_name = argv[optind+1] ;
221 slitmap_name = argv[optind+2] ;
222 wavemap_name = argv[optind+3] ;
223 if ( nb_frames >= 5 )
224 sky_line_list_name = argv[optind+4] ;
225 if ( nb_frames == 6 )
226 local_name = argv[optind+5] ;
227
228 }
229 else{
230 xsh_msg( "Not enough Input Frames\n" ) ;
231 printf(SYNTAX);
232 TEST_END();
233 exit(0);
234 }
235
236 xsh_msg("Sci frame : %s", sci_name);
237 xsh_msg("Order frame : %s", order_name);
238 xsh_msg("Slit map frame : %s",slitmap_name);
239 xsh_msg("Wave map frame : %s",wavemap_name);
240 if (local_name != NULL){
241 xsh_msg("Localization frame : %s", local_name);
242 }
243 if (sky_line_list_name != NULL){
244 xsh_msg("Ref sky line list frame : %s", sky_line_list_name);
245 }
246 xsh_msg("---Parameters");
247 xsh_msg("method : %s", SKY_METHOD_PRINT(sky_par.method));
248 xsh_msg("slit edges mask : %f", sky_par.slit_edges_mask);
249
250 xsh_msg("position1 : %f hheight1 : %f", sky_par.pos1, sky_par.hheight1);
251 xsh_msg("position2 : %f hheight2 : %f", sky_par.pos2, sky_par.hheight2);
252
253 if (sky_par.method == MEDIAN_METHOD){
254 xsh_msg("median half size : %d", sky_par.median_hsize);
255 }
256 check( plist = cpl_propertylist_load( sci_name, 0));
257 check( arm = xsh_pfits_get_arm( plist));
258
260 XSH_LAMP_UNDEFINED, "xsh_scired_slit_stare");
261
262
263 /* This generates SEG fault
264 if(instrument->arm == XSH_ARM_NIR) {
265 if(xsh_instrument_nir_is_JH(sci_frame,instrument)) {
266
267 instrument->config->order_min=13;
268 instrument->config->order_max=26;
269 instrument->config->orders=14;
270 }
271 }
272*/
273
274 /* load frames */
276 TESTS_XSH_FRAME_CREATE( sci_frame, tag, sci_name ) ;
277 /* Find maximum in frame */
278 get_max_pos( sci_frame, instrument ) ;
281 TESTS_XSH_FRAME_CREATE( order_frame, tag, order_name ) ;
282 cpl_frame_set_group( order_frame, CPL_FRAME_GROUP_CALIB ) ;
283
285 TESTS_XSH_FRAME_CREATE( slitmap_frame, tag, slitmap_name ) ;
286 cpl_frame_set_group( slitmap_frame, CPL_FRAME_GROUP_CALIB ) ;
287
289 TESTS_XSH_FRAME_CREATE( wavemap_frame, tag, wavemap_name ) ;
290 cpl_frame_set_group( wavemap_frame, CPL_FRAME_GROUP_CALIB ) ;
291
292 if ( local_name != NULL ) {
294 TESTS_XSH_FRAME_CREATE( local_frame, tag, local_name ) ;
295 cpl_frame_set_group( local_frame, CPL_FRAME_GROUP_CALIB ) ;
296 }
297
298 if ( sky_line_list_name != NULL ) {
300 TESTS_XSH_FRAME_CREATE( sky_line_list_frame, tag, sky_line_list_name ) ;
301 cpl_frame_set_group( sky_line_list_frame, CPL_FRAME_GROUP_CALIB ) ;
302 }
303
304 check(result = xsh_subtract_sky_single( sci_frame, order_frame,
305 slitmap_frame, wavemap_frame,
306 local_frame, sky_line_list_frame,
307 NULL,NULL,instrument,
308 sky_par.nbkpts1, &sky_par,
309 &sky_spectrum,&sky_spectrum_eso,
310 "test",1));
311
312
313/*
314 tag = "SKY_MODEL";
315 sky_frame_ima= xsh_save_sky_model( sci_frame, result, tag);
316*/
317
318 cleanup:
320 xsh_free_frame( &sci_frame);
321 xsh_free_frame( &order_frame);
322 xsh_free_frame( &slitmap_frame);
323 xsh_free_frame( &wavemap_frame);
324 xsh_free_frame( &local_frame);
325 xsh_free_frame( &sky_spectrum);
326 xsh_free_frame( &sky_spectrum_eso);
327 xsh_free_frame( &result);
328 xsh_free_propertylist( &plist);
329
330 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
331 xsh_msg( "ERROR in xsh_subtract_sky_single" ) ;
332 xsh_error_dump( CPL_MSG_ERROR);
333 ret=1;
334 }
335 TEST_END();
336 return ret;
337}
int main()
Unit test of xsh_bspline_interpol.
static const char * Options
static void HandleOptions(int argc, char **argv, xsh_subtract_sky_single_param *sky_par)
static void get_max_pos(cpl_frame *frame, xsh_instrument *instrument)
#define MODULE_ID
static struct option LongOptions[]
#define SYNTAX
static xsh_instrument * instrument
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
#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
void xsh_instrument_set_decode_bp(xsh_instrument *i, const int decode_bp)
Set bad pixel code.
int * y
int * x
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
XSH_ARM xsh_pfits_get_arm(const cpl_propertylist *plist)
Definition: xsh_pfits.c:270
cpl_frame * xsh_subtract_sky_single(cpl_frame *sci_frame, cpl_frame *order_table_frame, cpl_frame *slitmap_frame, cpl_frame *wavemap_frame, cpl_frame *locTable_frame, cpl_frame *ref_sky_list, cpl_frame *sky_orders_chunks, cpl_frame *usr_defined_break_points_frame, xsh_instrument *instrument, int nbkpts, xsh_subtract_sky_single_param *sky_par, cpl_frame **sky_spectrum, cpl_frame **sky_spectrum_eso, const char *rec_prefix, const int clean_tmp)
Subtract the sky background for single frame. If sky_spectrum is NOT NULL it is saved as a product,...
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
int xsh_debug_level_set(int level)
set debug level
Definition: xsh_utils.c:3125
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
cpl_image * data
Definition: xsh_data_pre.h:65
enum bspline_sampling bspline_sampling
#define TESTS_XSH_INSTRUMENT_CREATE(instr, mode, arm, lamp, recipe)
Definition: tests.h:115
#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_LAMP_UNDEFINED
@ XSH_ARM_UNDEFINED
@ XSH_MODE_SLIT
#define XSH_WAVE_MAP
Definition: xsh_dfs.h:928
#define XSH_ORDER_TAB_EDGES
Definition: xsh_dfs.h:554
#define XSH_LOCALIZATION
Definition: xsh_dfs.h:600
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
Definition: xsh_dfs.h:1548
#define XSH_SLIT_MAP
Definition: xsh_dfs.h:907
#define XSH_SKY_LINE_LIST
Definition: xsh_dfs.h:944
#define XSH_OBJECT_SLIT_STARE
Definition: xsh_dfs.h:226
#define XSH_GET_TAG_FROM_LAMP(TAG, instr)
Definition: xsh_dfs.h:1608
#define max(a, b)
#define SKY_METHOD_PRINT(method)
@ FINE
@ MEDIAN_METHOD
@ BSPLINE_METHOD
@ 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