X-shooter Pipeline Reference Manual 3.8.15
test-xsh_build_cube.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: 2011-09-14 11:37:12 $
23 * $Revision: 1.3 $
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#include <xsh_data_spectrum.h>
53#include <xsh_utils_table.h>
54
55#include <xsh_drl.h>
56#include <xsh_pfits.h>
57
58#include <xsh_badpixelmap.h>
59
60#include <cpl.h>
61#include <math.h>
62
63#include <getopt.h>
64
65/*--------------------------------------------------------------------------
66 Defines
67 --------------------------------------------------------------------------*/
68
69#define MODULE_ID "XSH_BUILD_CUBE"
70
71/*--------------------------------------------------------------------------
72 Implementation
73 --------------------------------------------------------------------------*/
74enum {
76};
77
78static struct option long_options[] = {
79 {"slit-bin", required_argument, 0, SLITBIN_OPT},
80 {"center-ifu", required_argument, 0, CENTER_IFU_OPT},
81 {"debug", required_argument, 0, DEBUG_OPT},
82 {"help", 0, 0, HELP_OPT},
83 {0, 0, 0, 0}
84};
85
86static void Help( void )
87{
88 puts( "Unitary test of xsh_build_cube");
89 puts( "Usage: test_xsh_build_cube [options] <sof>");
90
91 puts( "Options" ) ;
92 puts( " --slit-bin=<n> : Binning on slit");
93 puts( " --center-ifu=<n> : If n=1 center cube at 0 arcsec");
94 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
95 puts( " --help : What you see" ) ;
96 puts( "\nInput Files" ) ;
97 puts( " 1. SOF [ORDER_TAB_EDGES_IFU_arm, OFFSET_TAB_DOWN_IFU_arm, OFFSET_TAB_CEN_IFU_arm, OFFSET_TAB_UP_IFU_arm]");
98 TEST_END();
99 exit(0);
100}
101
102static void HandleOptions( int argc, char **argv, double *slit_bin, int *ifu_center)
103{
104 int opt ;
105 int option_index = 0;
106 while (( opt = getopt_long (argc, argv, "debug:help",
107 long_options, &option_index)) != EOF ){
108
109 switch ( opt ) {
110 case SLITBIN_OPT:
111 *slit_bin = atof(optarg);
112 break;
113 case CENTER_IFU_OPT:
114 *ifu_center = atoi(optarg);
115 break;
116 case DEBUG_OPT:
117 if ( strcmp( optarg, "LOW")==0){
119 }
120 else if ( strcmp( optarg, "HIGH")==0){
122 }
123 break;
124 case HELP_OPT:
125 Help();
126 break;
127 default:
128 break;
129 }
130 }
131 return;
132}
133
134static void create_gaussian_profile( int nslit, double slitmin, double slitcen,
135 double slit_bin, double height, double sigma, double offset, const char* filename,
136 cpl_vector **x_vect, cpl_vector **y_vect)
137{
138 int i;
139 FILE *file = NULL;
140
141 XSH_ASSURE_NOT_NULL( x_vect);
142 XSH_ASSURE_NOT_NULL( y_vect);
143
144 *x_vect = cpl_vector_new( nslit);
145 *y_vect = cpl_vector_new( nslit);
146
147 /* create gaussian profile */
148 for(i=0; i<nslit; i++){
149 double s,z,val;
150 s = slitmin+i*slit_bin;
151 z = (s-slitcen)/(sigma*XSH_MATH_SQRT_2);
152 val = height*exp(-(z*z))+offset;
153 cpl_vector_set( *x_vect, i, s);
154 cpl_vector_set( *y_vect, i, val);
155 }
156
157 file = fopen( filename, "w+");
158 fprintf( file, "#i j x y\n");
159
160 for( i=0; i<nslit; i++){
161 double x, y;
162
163 x = cpl_vector_get( *x_vect, i);
164 y = cpl_vector_get( *y_vect, i);
165 fprintf( file, "%d %d %f %f\n", i,nslit-1-i, x, y);
166 }
167 fclose( file);
168
169 cleanup:
170 if (cpl_error_get_code() != CPL_ERROR_NONE){
171 xsh_free_vector( x_vect);
172 xsh_free_vector( y_vect);
173 }
174 return;
175}
183int main( int argc, char **argv)
184{
185 /* Declarations */
186 int ret = 0 ;
187
189
190 const char *sof_name = NULL;
191 cpl_frameset *set = NULL;
192 cpl_frame *slitmap_frame = NULL;
193 cpl_frameset *offsettab_frameset = NULL;
194 int i, size;
195 double sdown=0, sup=0, sldown=0, slup=0;
196 double slit_bin = 0.15;
197 double slitmin_tab[3];
198 int nslit_tab[3];
199 double slitcen_tab[3];
200 double sigma = 1, height = 1, offset=0;
201 cpl_vector *downx_vect = NULL;
202 cpl_vector *downy_vect = NULL;
203 cpl_vector *cenx_vect = NULL;
204 cpl_vector *ceny_vect = NULL;
205 cpl_vector *upx_vect = NULL;
206 cpl_vector *upy_vect = NULL;
207 cpl_vector *yvect_tab[3];
208 double lmin=100, lmax=105, lstep=1;
209 xsh_spectrum *spectrum = NULL;
210 double smin, smax, sstep;
211 double *flux = NULL;
212 int s,l, k, sizes, sizel;
213 cpl_frameset *merge2d_frameset = NULL;
214 cpl_frame *cube_frame = NULL;
215 int ifu_center = 0;
216
217 /* Initialize libraries */
219 cpl_msg_set_level(CPL_MSG_DEBUG);
221
222 HandleOptions( argc, argv, &slit_bin, &ifu_center);
223
224 if ( (argc - optind) >=1 ) {
225 sof_name = argv[optind];
226 }
227 else {
228 Help();
229 exit(0);
230 }
231
232 /* Create frameset from sof */
233 check( set = sof_to_frameset( sof_name));
234 /* Validate frame set */
236 check( slitmap_frame = xsh_find_slitmap( set, instrument));
237 check( offsettab_frameset = xsh_find_offset_tab_ifu( set, instrument));
238
239 size = cpl_frameset_get_size( offsettab_frameset);
240
241 xsh_msg( "Slit binning : %f", slit_bin);
242 xsh_msg( "Ifu center : %d", ifu_center);
243 xsh_msg( "Slit map : %s" , cpl_frame_get_filename( slitmap_frame));
244 for( i=0; i< size; i++){
245 cpl_frame *offsettab_frame = NULL;
246
247 offsettab_frame = cpl_frameset_get_frame( offsettab_frameset, i);
248 xsh_msg( "Offset tab %s %s", cpl_frame_get_filename( offsettab_frame),
249 cpl_frame_get_tag( offsettab_frame));
250 }
251 /* get slit edges */
252 xsh_get_slit_edges( slitmap_frame, &sdown, &sup, &sldown, &slup, instrument);
253
254 xsh_msg("Estimate by FLAT slitlets");
255 xsh_msg("DOWN %f --> %f", sdown, sldown);
256 xsh_msg("CEN %f --> %f", sldown, slup);
257 xsh_msg("UP %f --> %f", slup, sup);
258
259 xsh_compute_slitlet_limits( offsettab_frameset, sdown,
260 sldown, slup, sup, slit_bin, slitmin_tab, nslit_tab, slitcen_tab);
261
262 sigma = (nslit_tab[0]/10)*slit_bin;
263 height = 10*sigma;
264 offset = 0;
265
266 create_gaussian_profile( nslit_tab[0], slitmin_tab[0], slitcen_tab[0],
267 slit_bin, height, sigma, offset, "gauss_down.dat", &downx_vect,
268 &downy_vect);
269 yvect_tab[0] = downy_vect;
270 create_gaussian_profile( nslit_tab[1], slitmin_tab[1], slitcen_tab[1],
271 slit_bin, height, sigma, offset, "gauss_cen.dat", &cenx_vect,
272 &ceny_vect);
273 yvect_tab[1] = ceny_vect;
274 create_gaussian_profile( nslit_tab[2], slitmin_tab[2], slitcen_tab[2],
275 slit_bin, height, sigma, offset, "gauss_up.dat", &upx_vect,
276 &upy_vect);
277 yvect_tab[2] = upy_vect;
278
279 /* create merge2d from profile */
280 merge2d_frameset = cpl_frameset_new();
281
282 for( k=0; k<3; k++){
283 char mergename[256];
284 cpl_frame *frame = NULL;
285
286 smin = slitmin_tab[k];
287 sstep = slit_bin;
288 smax = slitmin_tab[k]+(nslit_tab[k]-1)*slit_bin;
289
290 check( spectrum = xsh_spectrum_2D_create( lmin, lmax, lstep,
291 smin, smax, sstep));
292 check( flux = xsh_spectrum_get_flux( spectrum));
293 check( sizel = xsh_spectrum_get_size_lambda( spectrum));
294 check( sizes = xsh_spectrum_get_size_slit( spectrum));
295 for( s=0; s< sizes; s++){
296 for( l=0; l<sizel; l++){
297 flux[l+s*sizel] = cpl_vector_get( yvect_tab[k], s);
298 }
299 }
300 sprintf( mergename, "merge2d_%d.fits", k);
301
302 check( cpl_propertylist_update_double( spectrum->flux_header, "ESO DET OUT1 RON", 10));
303 check( cpl_propertylist_update_double( spectrum->flux_header, "ESO DET OUT1 CONAD", 10));
304 check( cpl_propertylist_update_double( spectrum->flux_header, "ESO DET OUT1 GAIN", 10));
305 check( cpl_propertylist_update_double( spectrum->flux_header, "ESO DET CHIP1 PSZX", 10));
306 check( cpl_propertylist_update_double( spectrum->flux_header, "ESO DET CHIP1 PSZY", 10));
307
308 check( cpl_propertylist_update_double( spectrum->flux_header, "ESO PRO RECT LAMBDA MIN", lmin));
309 check( cpl_propertylist_update_double( spectrum->flux_header, "ESO PRO RECT LAMBDA MAX", lmax));
310 check( cpl_propertylist_update_double( spectrum->flux_header,"ESO PRO RECT BIN LAMBDA", lstep));
311
312
313 frame = xsh_spectrum_save( spectrum, mergename,
314 "MERGE_2D");
315 cpl_frame_set_tag( frame, "MERGE_2D");
316 xsh_spectrum_free( &spectrum);
317 cpl_frameset_insert( merge2d_frameset, frame);
318 }
319
320 check( cube_frame = xsh_cube( merge2d_frameset, instrument,
321 "TEST"));
322
323 cleanup:
324 if (cpl_error_get_code() != CPL_ERROR_NONE) {
325 xsh_error_dump(CPL_MSG_ERROR);
326 ret = 1;
327 }
328 xsh_free_frame( &cube_frame);
329 xsh_free_frameset( &merge2d_frameset);
330 xsh_free_frameset( &set);
332 TEST_END();
333 return ret;
334}
int main()
Unit test of xsh_bspline_interpol.
static void HandleOptions(int argc, char **argv, double *slit_bin, int *ifu_center)
static void Help(void)
#define MODULE_ID
static struct option long_options[]
static void create_gaussian_profile(int nslit, double slitmin, double slitcen, double slit_bin, double height, double sigma, double offset, const char *filename, cpl_vector **x_vect, cpl_vector **y_vect)
@ HELP_OPT
@ CENTER_IFU_OPT
@ DEBUG_OPT
@ SLITBIN_OPT
static double sigma
static xsh_instrument * instrument
cpl_frameset * sof_to_frameset(const char *sof_name)
Definition: tests.c:576
int xsh_spectrum_get_size_lambda(xsh_spectrum *s)
Get lambda axis size of spectrum.
cpl_frame * xsh_spectrum_save(xsh_spectrum *s, const char *filename, const char *tag)
save a spectrum
double * xsh_spectrum_get_flux(xsh_spectrum *s)
Get flux of spectrum.
xsh_spectrum * xsh_spectrum_2D_create(double lambda_min, double lambda_max, double lambda_step, double slit_min, double slit_max, double slit_step)
Create a 2D spectrum structure.
void xsh_spectrum_free(xsh_spectrum **s)
free memory associated to an 1D spectrum
int xsh_spectrum_get_size_slit(xsh_spectrum *s)
Get slit axis ize of 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
cpl_frame * xsh_cube(cpl_frameset *merge2d_frameset, xsh_instrument *instrument, const char *rec_prefix)
Create a cube.
Definition: xsh_format.c:472
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
int size
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
void xsh_compute_slitlet_limits(cpl_frameset *shift_set, double sdown, double sldown, double slup, double sup, double slit_bin, double *slitmin_tab, int *nslit_tab, double *slitcen_tab)
Definition: xsh_rectify.c:1372
void xsh_get_slit_edges(cpl_frame *slitmap_frame, double *sdown, double *sup, double *sldown, double *slup, xsh_instrument *instrument)
Trace slit edges in a master flat.
Definition: xsh_rectify.c:671
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
Definition: xsh_utils.c:2284
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_propertylist * flux_header
#define TEST_END()
Definition: tests.h:111
#define TESTS_INIT(DRL_ID)
Definition: tests.h:105
cpl_frameset * xsh_find_offset_tab_ifu(cpl_frameset *frames, xsh_instrument *instr)
Find offset tab (One for each slitlet)
Definition: xsh_dfs.c:3832
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
#define XSH_MATH_SQRT_2
Definition: xsh_drl.h:567
@ 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