X-shooter Pipeline Reference Manual 3.8.15
test-xsh_localize_obj.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.41 $
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_spectrum.h>
51#include <xsh_drl.h>
52#include <xsh_pfits.h>
53#include <xsh_parameters.h>
54#include <xsh_badpixelmap.h>
55
56#include <cpl.h>
57#include <math.h>
58
59#include <string.h>
60#include <getopt.h>
61
62/*--------------------------------------------------------------------------
63 Defines
64 --------------------------------------------------------------------------*/
65
66#define MODULE_ID "XSH_LOCALIZE_OBJ"
67
68enum {
71} ;
72
73static struct option long_options[] = {
74 {"nbchunk", required_argument, 0, NBCHUNK_OPT},
75 {"threshold", required_argument, 0, TRESHOLD_OPT},
76 {"deg_poly", required_argument, 0, DEG_POLY_OPT},
77 {"method", required_argument, 0, METHOD_OPT},
78 {"slit_position", required_argument, 0, SLIT_POS_OPT},
79 {"slit_hheight", required_argument, 0, SLIT_HHEIGHT_OPT},
80 {"kappa", required_argument, 0, KAPPA_OPT},
81 {"niter", required_argument, 0, NITER_OPT},
82 {"skymask", required_argument, 0, SKYMASK_OPT},
83 {"help", 0, 0, HELP_OPT},
84 {0, 0, 0, 0}
85};
86
87static void Help( void )
88{
89 puts( "Unitary test of Localization");
90 puts( "Usage: test_xsh_localize_obj [options] REC_FRAME [LOC_TABLE]");
91
92 puts( "Options" ) ;
93 puts( " --help : What you see" ) ;
94 puts( " --method= : MANUAL | AUTO | GAUSSIAN [GAUSSIAN]");
95 puts( " MANUAL options");
96 puts( " --slit_position=<n>: (MANUAL only) the reference slit position in arcsec [0]");
97 puts( " --slit_hheight=<n> : (MANUAL only) the half size of slit height in arcsec [0.5]");
98 puts( " AUTO or GAUSSIAN options");
99 puts( " --deg_poly=<n> : (AUTO, GAUSSIAN) polynomial degree of fit [0]");
100 puts( " --niter=<n> : (AUTO, GAUSSIAN) Number of iterations for sigma clipping[3]");
101 puts( " --kappa=<n> : (AUTO, GAUSSIAN) Kappa for sigma clipping [1.0]");
102 puts( " --nbchunk=<n> : (AUTO, GAUSSIAN) Number of chunk [10]");
103 puts( " --skymask=<file> : (AUTO, GAUSSIAN) Sky mask file");
104 puts( " AUTO options");
105 puts( " --threshold=<n> : (AUTO) Threshold to find edges [10]");
106 puts( "\nInput Files" ) ;
107 puts( "The input files argument MUST be in this order:" ) ;
108 puts( " 1. DRL rectified 2D frame tag DRL_ORDER2D" ) ;
109 puts( " 2. (optional] Localization table" );
110 TEST_END();
111}
112
113
114static void HandleOptions( int argc, char **argv,
115 xsh_localize_obj_param *loc_par, char **skymask_name)
116{
117 int opt ;
118 int option_index = 0;
119
120 while (( opt = getopt_long (argc, argv,
121 "nbchunk:threshold:deg_poly:method:slit_position:slit_hheight",
122 long_options, &option_index)) != EOF ){
123
124 switch ( opt ) {
125 case NBCHUNK_OPT:
126 loc_par->loc_chunk_nb = atoi(optarg);
127 break ;
128 case TRESHOLD_OPT:
129 loc_par->loc_thresh = atof(optarg);
130 break ;
131 case DEG_POLY_OPT:
132 loc_par->loc_deg_poly = atoi(optarg);
133 break;
134 case METHOD_OPT:
135 if ( strcmp(optarg, LOCALIZE_METHOD_PRINT(LOC_MANUAL_METHOD)) == 0){
136 loc_par->method = LOC_MANUAL_METHOD;
137 }
138 else if ( strcmp(optarg, LOCALIZE_METHOD_PRINT(LOC_MAXIMUM_METHOD)) == 0){
139 loc_par->method = LOC_MAXIMUM_METHOD;
140 }
141 else if ( strcmp(optarg, LOCALIZE_METHOD_PRINT(LOC_GAUSSIAN_METHOD)) == 0){
142 loc_par->method = LOC_GAUSSIAN_METHOD;
143 }
144 else{
145 xsh_msg("WRONG method %s", optarg);
146 exit(-1);
147 }
148 break;
149
150 case SLIT_POS_OPT:
151 loc_par->slit_position = atof( optarg);
152 break ;
153 case SLIT_HHEIGHT_OPT:
154 loc_par->slit_hheight = atof( optarg);
155 break;
156 case KAPPA_OPT:
157 loc_par->kappa = atof( optarg);
158 break;
159 case NITER_OPT:
160 loc_par->niter = atoi( optarg);
161 break;
162 case SKYMASK_OPT:
163 loc_par->use_skymask = TRUE;
164 *skymask_name = optarg;
165 break;
166
167 default:
168 Help(); exit(-1);
169 }
170 }
171 return;
172}
173
174
175
176
177static void analyse_localization( cpl_frame *merge_frame, cpl_frame* loc_frame,
178 xsh_instrument* instr);
179
180/*--------------------------------------------------------------------------
181 Implementation
182 --------------------------------------------------------------------------*/
183static void analyse_localization( cpl_frame* rec_frame, cpl_frame* loc_frame,
184 xsh_instrument* instr)
185{
186 const char* rec_name = NULL;
187 cpl_frame* merge_frame = NULL;
188 const char* loc_name = NULL;
189 xsh_localization *loc_list = NULL;
190 xsh_spectrum *spectrum = NULL;
191 int ilambda;
192 FILE *loc_regfile = NULL ;
193 FILE *loc_datfile = NULL;
194 int merge_par = 0;
195 //const int decode_bp=2147483647;
196 XSH_ASSURE_NOT_NULL( rec_frame);
197 XSH_ASSURE_NOT_NULL( loc_frame);
198
199 check( rec_name = cpl_frame_get_filename( rec_frame));
200 check( loc_name = cpl_frame_get_filename( loc_frame));
201
202 xsh_msg("---Dump the localization");
203 printf("DRL RECTIFY frame : %s\n", rec_name);
204 printf("LOCALIZE frame : %s\n", loc_name);
205
206 check( merge_frame = xsh_merge_ord( rec_frame, instr, merge_par,
207 "TEST_MERGE"));
208
209 check( spectrum = xsh_spectrum_load( merge_frame));
210 check( loc_list = xsh_localization_load( loc_frame));
211
212 loc_regfile = fopen( "LOCALIZATION.reg", "w");
213 fprintf(loc_regfile, "# Region file format: DS9 version 4.0\n"\
214 "#red center, blue low, green up\n"\
215 "global color=red font=\"helvetica 4 normal\""\
216 "select=1 highlite=1 edit=1 move=1 delete=1 include=1 fixed=0"\
217 " source=1\nwcs\n");
218
219 loc_datfile = fopen( "LOCALIZATION.dat", "w");
220 fprintf( loc_datfile, "#lambda slow scen sup\n");
221
222 for( ilambda=0; ilambda < spectrum->size_lambda; ilambda++){
223 double slit_cen = 0.0, slit_low= 0.0, slit_up = 0.0;
224 double lambda;
225
226 lambda = spectrum->lambda_min+ilambda*spectrum->lambda_step;
227 check( slit_cen = cpl_polynomial_eval_1d( loc_list->cenpoly,
228 lambda, NULL));
229 check( slit_low = cpl_polynomial_eval_1d( loc_list->edglopoly,
230 lambda, NULL));
231 check( slit_up = cpl_polynomial_eval_1d( loc_list->edguppoly,
232 lambda, NULL));
233 fprintf( loc_regfile, "point(%f,%f) "\
234 "#point=cross color=red font=\"helvetica 4 normal\"\n", lambda, slit_cen);
235 fprintf( loc_regfile, "point(%f,%f) "\
236 "#point=cross color=blue font=\"helvetica 4 normal\"\n", lambda, slit_low);
237 fprintf( loc_regfile, "point(%f,%f) "\
238 "#point=cross color=green font=\"helvetica 4 normal\"\n", lambda, slit_up);
239 fprintf( loc_datfile, "%f %f %f %f\n", lambda, slit_low, slit_cen, slit_up);
240 }
241
242 xsh_msg( "Produce ds9 region file : LOCALIZATION.reg");
243 xsh_msg( "Produce data file : LOCALIZATION.dat");
244 xsh_msg(" Produce MERGE frame : %s", cpl_frame_get_filename( merge_frame));
245
246 cleanup:
247 if(loc_regfile != NULL) {
248 fclose( loc_regfile);
249 }
250
251 if(loc_datfile != NULL) {
252 fclose( loc_datfile);
253 }
254 xsh_free_frame( &merge_frame);
255 xsh_spectrum_free( &spectrum);
256 xsh_localization_free( &loc_list);
257
258 return;
259}
260
268int main( int argc, char **argv)
269{
270 /* Declarations */
271 int ret = 0 ;
273 xsh_localize_obj_param loc_obj =
274 { 10, 0.1, 1, 0., LOC_MANUAL_METHOD, 0.0, 0.5, 3,3, FALSE};
275 char* rec_name = NULL;
276 cpl_frame *rec_frame = NULL;
277
278 const char *tag = NULL;
279 char* loc_name = NULL;
280 cpl_frame* loc_frame = NULL;
281 cpl_propertylist *plist = NULL;
283 char * skymask_name = NULL;
284 cpl_frame *skymask_frame = NULL;
285 const int decode_bp=2147483647;
286 /* Initialize libraries */
288
289 cpl_msg_set_level(CPL_MSG_DEBUG);
291
292 loc_obj.kappa = 1.0;
293 loc_obj.niter = 3;
295 loc_obj.loc_deg_poly = 0;
296 loc_obj.loc_chunk_nb = 10;
297
298 /* Analyse parameters */
299 HandleOptions( argc, argv, &loc_obj, &skymask_name);
300
301 if ( (argc - optind) > 0 ) {
302 rec_name = argv[optind];
303 if ((argc - optind) > 1){
304 loc_name = argv[optind+1];
305 }
306 }
307 else {
308 Help();
309 exit( 0);
310 }
311
312 check( plist = cpl_propertylist_load( rec_name, 0));
313 check( arm = xsh_pfits_get_arm( plist));
314
316 XSH_LAMP_UNDEFINED, "xsh_scired_slit_stare");
319
320 TESTS_XSH_FRAME_CREATE( rec_frame, tag, rec_name);
321
322 xsh_msg("---Input Frames");
323 xsh_msg("DRL RECTIFY frame : %s tag %s", rec_name, tag);
324
325 if (loc_name == NULL){
326 xsh_msg( "---Localize parameters");
327 xsh_msg( " --method %s ", LOCALIZE_METHOD_PRINT(loc_obj.method));
328 if ( loc_obj.method == LOC_MANUAL_METHOD){
329 xsh_msg(" slit_position : %f",loc_obj.slit_position);
330 xsh_msg(" slit_hheight : %f",loc_obj.slit_hheight);
331 }
332 else {
333 if (loc_obj.method == LOC_MAXIMUM_METHOD){
334 xsh_msg(" threshold : %f",loc_obj.loc_thresh);
335 }
336 xsh_msg(" nb_chunks : %d",loc_obj.loc_chunk_nb);
337 xsh_msg(" deg_poly : %d",loc_obj.loc_deg_poly);
338 xsh_msg(" kappa : %f", loc_obj.kappa);
339 xsh_msg(" niter : %d", loc_obj.niter);
340 if ( loc_obj.use_skymask == TRUE){
341 xsh_msg(" skymask : %s", skymask_name);
343 TESTS_XSH_FRAME_CREATE( skymask_frame, tag, skymask_name);
344 }
345 }
346 check( loc_frame = xsh_localize_obj( rec_frame, skymask_frame, instrument, &loc_obj,
347 NULL, "LOCALIZE_TABLE.fits"));
348 }
349 else{
350 XSH_ASSURE_NOT_NULL (loc_name);
352 TESTS_XSH_FRAME_CREATE( loc_frame, tag, loc_name);
353 }
354
355 /* Create REGION FILES for rectify */
356 check( analyse_localization( rec_frame, loc_frame, instrument));
357
358 cleanup:
359 xsh_free_frame( &loc_frame);
360 xsh_free_frame( & rec_frame);
361 xsh_free_frame( &skymask_frame);
363 xsh_free_propertylist( &plist);
364
365 if (cpl_error_get_code() != CPL_ERROR_NONE) {
366 xsh_error_dump(CPL_MSG_ERROR);
367 ret=1;
368 }
369 TEST_END();
370 return ret ;
371}
372
int main()
Unit test of xsh_bspline_interpol.
static void analyse_localization(cpl_frame *merge_frame, cpl_frame *loc_frame, xsh_instrument *instr)
static void HandleOptions(int argc, char **argv, xsh_localize_obj_param *loc_par, char **skymask_name)
#define MODULE_ID
static struct option long_options[]
@ METHOD_OPT
@ TRESHOLD_OPT
@ NBCHUNK_OPT
@ SLIT_POS_OPT
@ SKYMASK_OPT
@ SLIT_HHEIGHT_OPT
@ DEG_POLY_OPT
static xsh_instrument * instrument
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
xsh_spectrum * xsh_spectrum_load(cpl_frame *s1d_frame)
Load a 1D spectrum structure.
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
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
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_localize_obj(cpl_frame *sci_frame, cpl_frame *skymask_frame, xsh_instrument *instrument, xsh_localize_obj_param *loc_obj_par, xsh_slit_limit_param *slit_limit_param, const char *fname)
Build the localization table.
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
XSH_ARM xsh_pfits_get_arm(const cpl_propertylist *plist)
Definition: xsh_pfits.c:270
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_polynomial * cenpoly
cpl_polynomial * edguppoly
cpl_polynomial * edglopoly
enum localize_method method
static void Help(void)
#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_LOCALIZATION
Definition: xsh_dfs.h:600
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
Definition: xsh_dfs.h:1548
#define XSH_MERGE2D
Definition: xsh_dfs.h:594
#define XSH_SKY_LINE_LIST
Definition: xsh_dfs.h:944
#define LOCALIZE_METHOD_PRINT(method)
@ LOC_MAXIMUM_METHOD
@ LOC_GAUSSIAN_METHOD
@ LOC_MANUAL_METHOD
@ XSH_DEBUG_LEVEL_MEDIUM
Definition: xsh_utils.h:138