X-shooter Pipeline Reference Manual 3.8.15
xsh_compute_slice_dist.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-12-18 14:15:44 $
23 * $Revision: 1.17 $
24 */
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29
30/*----------------------------------------------------------------------------*/
37/*----------------------------------------------------------------------------*/
40/*-----------------------------------------------------------------------------
41 Includes
42 -----------------------------------------------------------------------------*/
43
44#include <math.h>
45#include <xsh_drl.h>
46
47#include <xsh_utils_table.h>
48#include <xsh_badpixelmap.h>
49#include <xsh_data_pre.h>
50#include <xsh_dfs.h>
51#include <xsh_pfits.h>
52#include <xsh_error.h>
53#include <xsh_msg.h>
54#include <xsh_fit.h>
55#include <xsh_data_instrument.h>
57#include <xsh_data_spectrum.h>
59#include <xsh_data_rec.h>
60#include <xsh_ifu_defs.h>
61
62#include <cpl.h>
63
64/*-----------------------------------------------------------------------------
65 Functions prototypes
66 -----------------------------------------------------------------------------*/
67
68/*-----------------------------------------------------------------------------
69 Implementation
70 -----------------------------------------------------------------------------*/
71
72cpl_frame* xsh_compute_slice_dist( cpl_frameset *loc_frame_set,
73 cpl_frame *order_tab_frame, cpl_frame *slitmap_frame,
74 cpl_frameset *rec_frameset,
75 double slicedist_lambda,
77{
78 cpl_frame * result = NULL ;
79 cpl_frame *loc_frame = NULL;
80 cpl_frame *rec_frame = NULL;
81 xsh_slice_offset *slice = NULL ;
82 xsh_localization *loc_down = NULL, *loc_cen = NULL, *loc_up = NULL;
83 char fname[256];
84 double slit_down, slit_cen, slit_up;
85 double sdown, sldown, slup, sup;
86 double dist_up, dist_down;
87 double lambda;
88 //xsh_order_list *orderlist = NULL ;
89 xsh_rec_list *reclist = NULL;
90
91 XSH_ASSURE_NOT_NULL( loc_frame_set);
92 XSH_ASSURE_NOT_NULL( rec_frameset);
94
95 xsh_msg( "Get Localization table for slitlet %s",
97 check( loc_frame = cpl_frameset_get_frame( loc_frame_set, 0));
98 xsh_msg( " '%s'", cpl_frame_get_filename( loc_frame ) ) ;
99 check( loc_down = xsh_localization_load( loc_frame));
100
101 xsh_msg( "Get Localization table for slitlet %s",
103 check( loc_frame = cpl_frameset_get_frame( loc_frame_set, 1));
104 xsh_msg( " '%s'", cpl_frame_get_filename( loc_frame ) ) ;
105 check( loc_cen = xsh_localization_load( loc_frame));
106
107 xsh_msg( "Get Localization table for slitlet %s",
109 check( loc_frame = cpl_frameset_get_frame( loc_frame_set, 2));
110 xsh_msg( " '%s'", cpl_frame_get_filename( loc_frame ));
111 check( loc_up = xsh_localization_load( loc_frame));
112
113 check( rec_frame = cpl_frameset_get_frame( rec_frameset, 0));
114 check( reclist = xsh_rec_list_load( rec_frame, instrument));
115
116 check( slice = xsh_slice_offset_create());
117
118 if ( slicedist_lambda < 0){
119 check( lambda = xsh_rec_list_get_lambda_max( reclist));
120 }
121 else{
122 lambda = slicedist_lambda;
123 }
124 xsh_msg(" Evaluate at lambda %f", lambda);
125 check( slit_down = cpl_polynomial_eval_1d(loc_down->cenpoly,
126 lambda, NULL));
127 check( slit_cen = cpl_polynomial_eval_1d(loc_cen->cenpoly,
128 lambda, NULL));
129 check( slit_up = cpl_polynomial_eval_1d(loc_up->cenpoly,
130 lambda, NULL));
131
132 xsh_msg("SLIT center from localization [%f,%f,%f]", slit_down, slit_cen,
133 slit_up);
134
136 slitmap_frame, &sdown, &sldown, &slup, &sup, instrument));
137
138 xsh_msg("EDGES form order tab [%f,[%f %f],%f]",sdown, sldown,
139 slup, sup);
140
141 dist_up = 2*slup-slit_cen-slit_up;
142 dist_down = 2*sldown-slit_cen-slit_down;
143 xsh_msg("DISTANCES edgU-->Up = %f edgU-->cen %f ==> %f", slup-slit_up,
144 slup-slit_cen, dist_up);
145 xsh_msg("DISTANCES edgD-->Down = %f edgD-->cen %f ==> %f", sldown-slit_down,
146 sldown-slit_cen, dist_down);
147
148 slice->cen_up = dist_up;
149 slice->cen_down = dist_down;
150
151 sprintf( fname, "SLICE_OFFSET_%s.fits",
153 check( result = xsh_slice_offset_save( slice, fname, instrument));
154
155 cleanup:
156 xsh_localization_free( &loc_down);
157 xsh_localization_free( &loc_cen);
158 xsh_localization_free( &loc_up);
159 xsh_slice_offset_free( &slice);
160 xsh_rec_list_free( &reclist);
161
162 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
163 xsh_free_frame( &result);
164 }
165 return result;
166}
167/*--------------------------------------------------------------------------*/
168
169/*---------------------------------------------------------------------------*/
182cpl_frame* xsh_compute_shift_ifu_slitlet( double lambda_ref, cpl_frame *objpos_frame,
183 cpl_frame *shiftifu_frame, double lambdaref_hsize, const char* resname)
184{
185 cpl_frame *result = NULL;
186 const char *objpos_name = NULL;
187 cpl_table *objpos_tab = NULL;
188 double *wave_pos_data = NULL;
189 double *slit_pos_data = NULL;
190 int pos_size;
191 double slitref=0;
192 cpl_table *res_tab = NULL;
193 int i;
194 char tablename[256];
195 cpl_propertylist *header = NULL;
196
197 const char *shiftifu_name = NULL;
198 cpl_table *shiftifu_tab = NULL;
199 //double *wave_shiftifu_data = NULL;
200 double *slit_shiftifu_data = NULL;
201 int shiftifu_size;
202 cpl_vector *median_slitref_vect = NULL;
203 double lambda=0;
204 int istart, iend;
205 int median_slitref_size = 0;
206
207 XSH_ASSURE_NOT_NULL( objpos_frame);
208 XSH_ASSURE_NOT_ILLEGAL( lambdaref_hsize >= 0);
209
210 /* Load objpos table */
211 check( objpos_name = cpl_frame_get_filename( objpos_frame));
212 XSH_TABLE_LOAD( objpos_tab, objpos_name);
213 /* Search slit position of reference wavelength */
214 check( wave_pos_data = cpl_table_get_data_double( objpos_tab,
216 check( slit_pos_data = cpl_table_get_data_double( objpos_tab,
218
219 pos_size = cpl_table_get_nrow( objpos_tab);
220
221 istart=0;
222
223 while( lambda <= (lambda_ref-lambdaref_hsize) && istart< pos_size){
224 lambda = wave_pos_data[istart];
225 istart++;
226 }
227 iend = istart;
228
229 while( lambda <= (lambda_ref+lambdaref_hsize) && iend< pos_size){
230 lambda = wave_pos_data[iend];
231 iend++;
232 }
233 median_slitref_size = iend-istart+1;
234
235 median_slitref_vect = cpl_vector_new( median_slitref_size);
236
237 for(i=istart; i<=iend; i++){
238 double resslit = slit_pos_data[i];
239
240 cpl_vector_set( median_slitref_vect, i-istart, resslit);
241 }
242
243
244
245 slitref = xsh_data_interpolate( lambda_ref, pos_size,
246 wave_pos_data, slit_pos_data);
247
248 xsh_msg_dbg_low("Old reference wavelength %f slit %f",
249 lambda_ref, slitref);
250
251 slitref = cpl_vector_get_median( median_slitref_vect);
252
253 xsh_msg_dbg_low("New reference wavelength %f slit %f",
254 lambda_ref, slitref);
255
256 check( res_tab = cpl_table_new( pos_size));
258 XSH_SHIFTIFU_UNIT_WAVELENGTH, CPL_TYPE_DOUBLE);
260 XSH_SHIFTIFU_UNIT_SHIFTSLIT, CPL_TYPE_DOUBLE);
261
262 /* case of summing old result */
263 if ( shiftifu_frame != NULL){
264 check( shiftifu_name = cpl_frame_get_filename( shiftifu_frame));
265 XSH_TABLE_LOAD( shiftifu_tab, shiftifu_name);
266 /*
267 check( wave_shiftifu_data = cpl_table_get_data_double( shiftifu_tab,
268 XSH_SHIFTIFU_COLNAME_WAVELENGTH));
269 */
270 check( slit_shiftifu_data = cpl_table_get_data_double( shiftifu_tab,
272 shiftifu_size = cpl_table_get_nrow( shiftifu_tab);
273 XSH_ASSURE_NOT_ILLEGAL( pos_size == shiftifu_size);
274 for( i=0; i< pos_size; i++){
275 double reswave, resslit;
276
277 reswave = wave_pos_data[i];
278 resslit = slit_pos_data[i]-slitref+slit_shiftifu_data[i];
279
280 check( cpl_table_set_double( res_tab, XSH_SHIFTIFU_COLNAME_WAVELENGTH,
281 i, reswave));
282 check( cpl_table_set_double( res_tab, XSH_SHIFTIFU_COLNAME_SHIFTSLIT,
283 i, resslit));
284 }
285 }
286 else{
287 for( i=0; i< pos_size; i++){
288 double reswave, resslit;
289
290 reswave = wave_pos_data[i];
291 resslit = slit_pos_data[i]-slitref;
292 check( cpl_table_set_double( res_tab, XSH_SHIFTIFU_COLNAME_WAVELENGTH,
293 i, reswave));
294 check( cpl_table_set_double( res_tab, XSH_SHIFTIFU_COLNAME_SHIFTSLIT,
295 i, resslit));
296 }
297 }
298 /* save result */
299 sprintf( tablename, resname);
300 header = cpl_propertylist_new();
301 /* write reference in header */
302 check( xsh_pfits_set_shiftifu_lambdaref( header, lambda_ref));
303 check( xsh_pfits_set_shiftifu_slitref( header, slitref));
304
305
306 check( cpl_table_save( res_tab, header, NULL, tablename, CPL_IO_DEFAULT));
307
308 /* Create the frame */
309 check(result=xsh_frame_product( tablename,
310 "OFFSET_TAB",
311 CPL_FRAME_TYPE_TABLE,
312 CPL_FRAME_GROUP_PRODUCT,
313 CPL_FRAME_LEVEL_TEMPORARY));
314 check (xsh_add_temporary_file( tablename));
315
316 cleanup:
317 xsh_free_propertylist( &header);
318 XSH_TABLE_FREE( shiftifu_tab);
319 XSH_TABLE_FREE( res_tab);
320 XSH_TABLE_FREE( objpos_tab);
321 xsh_free_vector( &median_slitref_vect);
322 return result;
323}
324/*---------------------------------------------------------------------------*/
325
326/*---------------------------------------------------------------------------*/
345cpl_frameset* xsh_compute_shift_ifu( double lambda_ref, double lambdaref_hsize,
346 cpl_frameset *objpos_frameset,
347 cpl_frameset *shiftifu_frameset, xsh_instrument* instrument, const char* prefix)
348{
349 int i, slitlet;
350 cpl_frameset *result_frameset = NULL;
351 char fname[256];
352
353 XSH_ASSURE_NOT_NULL( objpos_frameset);
355
356 check( result_frameset = cpl_frameset_new());
357
358 for( i = 0, slitlet = LOWER_IFU_SLITLET ; i < 3 ; i++, slitlet++ ) {
359 cpl_frame *comp_shift_frame = NULL;
360 cpl_frame *objpos_frame = NULL;
361 cpl_frame *shift_frame = NULL;
362 char tag[256];
363
364 sprintf( fname ,"%s_SHIFTIFU_%s_%s.fits", prefix, SlitletName[slitlet],
366
367 xsh_msg( "Compute IFU shift for slitlet %s, frame '%s'",
368 SlitletName[slitlet], fname);
369
370 check( objpos_frame = cpl_frameset_get_frame( objpos_frameset, i));
371
372 if ( shiftifu_frameset != NULL){
373 check( shift_frame = cpl_frameset_get_frame( shiftifu_frameset, i));
374 }
375 check( comp_shift_frame = xsh_compute_shift_ifu_slitlet( lambda_ref,
376 objpos_frame, shift_frame, lambdaref_hsize, fname));
377
378 sprintf( tag, "OFFSET_TAB_%s_IFU_%s", SlitletName[slitlet],
380 check( cpl_frame_set_tag( comp_shift_frame, tag));
381 check( cpl_frameset_insert( result_frameset, comp_shift_frame));
382 }
383
384 cleanup:
385 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
386 xsh_free_frameset( &result_frameset);
387 }
388 return result_frameset;
389}
static xsh_instrument * instrument
cpl_frameset * xsh_compute_shift_ifu(double lambda_ref, double lambdaref_hsize, cpl_frameset *objpos_frameset, cpl_frameset *shiftifu_frameset, xsh_instrument *instrument, const char *prefix)
Compute the shift in slit between reference wavelength and others for all the slitlets.
cpl_frame * xsh_compute_shift_ifu_slitlet(double lambda_ref, cpl_frame *objpos_frame, cpl_frame *shiftifu_frame, double lambdaref_hsize, const char *resname)
Compute the shift in slit between reference wavelength and others.
cpl_frame * xsh_compute_slice_dist(cpl_frameset *loc_frame_set, cpl_frame *order_tab_frame, cpl_frame *slitmap_frame, cpl_frameset *rec_frameset, double slicedist_lambda, 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_rec_list * xsh_rec_list_load(cpl_frame *frame, xsh_instrument *instrument)
load an rec list from a frame
Definition: xsh_data_rec.c:289
double xsh_rec_list_get_lambda_max(xsh_rec_list *list)
Definition: xsh_data_rec.c:957
void xsh_rec_list_free(xsh_rec_list **list)
free memory associated to a rec_list
Definition: xsh_data_rec.c:760
xsh_slice_offset * xsh_slice_offset_create(void)
Create an empty slice_offset.
cpl_frame * xsh_slice_offset_save(xsh_slice_offset *list, const char *filename, xsh_instrument *instrument)
Save an slice_offset list to a frame.
void xsh_slice_offset_free(xsh_slice_offset **list)
Free memory associated to a slice_offset.
#define XSH_ASSURE_NOT_ILLEGAL(cond)
Definition: xsh_error.h:107
#define check(COMMAND)
Definition: xsh_error.h:71
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
#define xsh_msg_dbg_low(...)
Definition: xsh_msg.h:48
void xsh_pfits_set_shiftifu_lambdaref(cpl_propertylist *plist, double value)
Definition: xsh_pfits.c:4015
void xsh_pfits_set_shiftifu_slitref(cpl_propertylist *plist, double value)
Definition: xsh_pfits.c:4044
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
double xsh_data_interpolate(double wav, int nrow, double *pw, double *pe)
Interpolate data points.
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
Definition: xsh_utils.c:1432
cpl_polynomial * cenpoly
cpl_frame * xsh_frame_product(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level)
Creates a frame with given characteristics.
Definition: xsh_dfs.c:930
#define XSH_SHIFTIFU_UNIT_SHIFTSLIT
Definition: xsh_drl.h:519
#define XSH_SHIFTIFU_COLNAME_WAVELENGTH
Definition: xsh_drl.h:516
#define XSH_OBJPOS_COLNAME_SLIT
Definition: xsh_drl.h:498
#define XSH_SHIFTIFU_COLNAME_SHIFTSLIT
Definition: xsh_drl.h:518
#define XSH_SHIFTIFU_UNIT_WAVELENGTH
Definition: xsh_drl.h:517
#define XSH_OBJPOS_COLNAME_WAVELENGTH
Definition: xsh_drl.h:496
@ CENTER_IFU_SLITLET
Definition: xsh_ifu_defs.h:42
@ LOWER_IFU_SLITLET
Definition: xsh_ifu_defs.h:42
@ UPPER_IFU_SLITLET
Definition: xsh_ifu_defs.h:42
static const char * SlitletName[]
Definition: xsh_ifu_defs.h:48
#define XSH_TABLE_NEW_COL(TABLE, NAME, UNIT, TYPE)
Definition: xsh_utils.h:113
#define XSH_TABLE_LOAD(TABLE, NAME)
#define XSH_TABLE_FREE(TABLE)