X-shooter Pipeline Reference Manual 3.8.15
xsh_util_efficiency.c
Go to the documentation of this file.
1/*
2 * This file is part of the XSHOOTER Pipeline
3 * Copyright (C) 2002,2003 European Southern Observatory
4 *
5 * This program 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2012-04-26 14:08:15 $
23 * $Revision: 1.11 $
24 * $Name: not supported by cvs2svn $
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*-----------------------------------------------------------------------------
32 Includes
33 ----------------------------------------------------------------------------*/
34#include <string.h>
35#include <math.h>
36
37/* cpl */
38#include <cpl.h>
39
40#include <xsh_dfs.h>
41#include <xsh_data_pre.h>
42#include <xsh_parameters.h>
43#include <xsh_drl.h>
44#include <xsh_msg.h>
45#include <xsh_pfits.h>
46#include <xsh_pfits_qc.h>
47#include <xsh_error.h>
48#include <xsh_utils_image.h>
49#include <xsh_data_atmos_ext.h>
50
51/*-----------------------------------------------------------------------------
52 Defines
53 ----------------------------------------------------------------------------*/
54
55#define RECIPE_ID "xsh_util_efficiency"
56#define RECIPE_AUTHOR "A.Modigliani"
57#define RECIPE_CONTACT "Andrea.Modigliani@eso.org"
58#define PRO_IMA "PRO_IMA_UVB"
59#define KEY_VALUE_HPRO_DID "PRO-1.15"
60
61#define SPEED_LIGHT 2.99792458*1.E8 /* m s-1 */
62#define TEL_AREA 51.2e4 /* collecting area in cm2 */
63/*-----------------------------------------------------------------------------
64 Functions prototypes
65 ----------------------------------------------------------------------------*/
66
67static int xsh_util_efficiency_create(cpl_plugin *) ;
68static int xsh_util_efficiency_exec(cpl_plugin *) ;
69static int xsh_util_efficiency_destroy(cpl_plugin *) ;
70static int xsh_util_efficiency(cpl_parameterlist *, cpl_frameset *) ;
71
72/*-----------------------------------------------------------------------------
73 Static variables
74 ----------------------------------------------------------------------------*/
75
76static char
79"This recipe compute efficiency.\n"
80"The input files should be tagged as: \n"
81"input observed spectrum: IMA_arm,\n"
82"input reference spectrum: FLUX_STD_TABLE,\n"
83"input atmospheric extintion table: EXTCOEFF_TABLE)\n"
84"The output is a table with efficiency as function of wavelength\n"
85"Information on relevant parameters can be found with\n"
86"esorex --params xsh_util_efficiency\n"
87"esorex --help xsh_util_efficiency\n"
88"\n";
89
90/*-----------------------------------------------------------------------------
91 Functions code
92 ----------------------------------------------------------------------------*/
93/*---------------------------------------------------------------------------*/
98/*---------------------------------------------------------------------------*/
99
101/*---------------------------------------------------------------------------*/
109/*---------------------------------------------------------------------------*/
110int cpl_plugin_get_info(cpl_pluginlist * list)
111{
112 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
113 cpl_plugin * plugin = &recipe->interface ;
114
115 cpl_plugin_init(plugin,
116 CPL_PLUGIN_API,
117 XSH_BINARY_VERSION,
118 CPL_PLUGIN_TYPE_RECIPE,
119 RECIPE_ID,
128
129 cpl_pluginlist_append(list, plugin) ;
130
131 return 0;
132}
133
134/*---------------------------------------------------------------------------*/
143/*---------------------------------------------------------------------------*/
144static int xsh_util_efficiency_create(cpl_plugin * plugin)
145{
146 cpl_recipe * recipe ;
147 cpl_parameter * p ;
148
149 /* Reset library state */
150 xsh_init();
151
152 /* Get the recipe out of the plugin */
153 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
154 recipe = (cpl_recipe *)plugin ;
155 else return -1 ;
156 cpl_error_reset();
157
158
159 /* Create the parameters list in the cpl_recipe object */
160 recipe->parameters = cpl_parameterlist_new() ;
161
162
163 /* Set generic parameters (common to all recipes) */
165 recipe->parameters ) ) ;
166 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,-1);
167 /* Fill the parameters list */
168 p = cpl_parameter_new_value("xsh.xsh_util_efficiency.airmass",
169 CPL_TYPE_DOUBLE,
170 "Airmass value",
171 "xsh.xsh_util_efficiency",1.2);
172 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "airmass") ;
173 cpl_parameterlist_append(recipe->parameters, p) ;
174
175 /* Fill the parameters list */
176 p = cpl_parameter_new_value("xsh.xsh_util_efficiency.gain",
177 CPL_TYPE_DOUBLE,
178 "Detector gain value",
179 "xsh.xsh_util_efficiency",1.5);
180 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "gain") ;
181 cpl_parameterlist_append(recipe->parameters, p) ;
182
183 cleanup:
184
185 /* Return */
186 return 0;
187}
188
189/*---------------------------------------------------------------------------*/
195/*---------------------------------------------------------------------------*/
196static int xsh_util_efficiency_exec(cpl_plugin * plugin)
197{
198 cpl_recipe * recipe ;
199 int code=0;
200 cpl_errorstate initial_errorstate = cpl_errorstate_get();
201
202 /* Get the recipe out of the plugin */
203 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
204 recipe = (cpl_recipe *)plugin ;
205 else return -1 ;
206 cpl_error_reset();
207
208 code = xsh_util_efficiency(recipe->parameters, recipe->frames) ;
209
210
211 if (!cpl_errorstate_is_equal(initial_errorstate)) {
212 /* Dump the error history since recipe execution start.
213 At this point the recipe cannot recover from the error */
214 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
215 }
216
217 return code ;
218}
219
220/*---------------------------------------------------------------------------*/
226/*---------------------------------------------------------------------------*/
227static int xsh_util_efficiency_destroy(cpl_plugin * plugin)
228{
229 cpl_recipe * recipe ;
230
231 /* Get the recipe out of the plugin */
232 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
233 recipe = (cpl_recipe *)plugin ;
234 else return -1 ;
235
236 cpl_parameterlist_delete(recipe->parameters) ;
237 return 0 ;
238}
239
240static double
242 double wav,
243 const char* colx,
244 const char* coly)
245{
246
247 double y=0;
248 double w1=0;
249 double w2=0;
250 double y1=0;
251 double y2=0;
252 float* pe=NULL;
253 float* pw=NULL;
254 int nrow=0;
255 int i=0;
256
257
258 w1=cpl_table_get_column_min(tbl,colx);
259 w2=cpl_table_get_column_max(tbl,colx);
260 y1=cpl_table_get_column_min(tbl,coly);
261 y2=cpl_table_get_column_max(tbl,coly);
262
263
264 pw=cpl_table_get_data_float(tbl,colx);
265 pe=cpl_table_get_data_float(tbl,coly);
266 nrow=cpl_table_get_nrow(tbl);
267
268
269 for(i=0;i<nrow-1;i++) {
270 if(pw[i]<wav) {
271 w1=pw[i];
272 w2=pw[i+1];
273 y1=pe[i];
274 y2=pe[i+1];
275 }
276 }
277
278 y=y1+(y2-y1)/(w2-w1)*(wav-w1);
279
280
281return y;
282
283}
284
285
286/*---------------------------------------------------------------------------*/
293/*---------------------------------------------------------------------------*/
294static int
295xsh_util_efficiency( cpl_parameterlist * parlist,
296 cpl_frameset * frames)
297{
298 const char* recipe_tags[1] = {XSH_EFFICIENCY};
299 int recipe_tags_size = 1;
300
302 cpl_parameter * param= NULL ;
303 cpl_frame* frm_sci=NULL;
304 cpl_frame* frm_ref=NULL;
305 cpl_frame* frm_atmext=NULL;
306
307 cpl_frameset* raws=NULL;
308 cpl_frameset* calib=NULL;
309
310 cpl_image* ima_sci=NULL;
311 cpl_table* tbl_ref=NULL;
312 cpl_table* tbl_ext=NULL;
313 cpl_table* tbl_atmext=NULL;
314
315 const char* name=NULL;
316 double aimprim=1.446;
317 //double gain=1.75; /* e-/adu 400k,low gain, UVB */
318 double gain=1.5; /* e-/adu 400k,low gain, VIS */
319 double ref_wmin=0;
320 double ref_wmax=0;
321 double src_wmin=0;
322 double src_wmax=0;
323 double ext_wmin=0;
324 double ext_wmax=0;
325 double ext_wdel=0;
326
327 double wmin=0;
328 double wmax=0;
329
330 cpl_propertylist* plist=NULL;
331 double crpix1=0;
332 double crval1=0;
333 double cdelt1=0;
334 int naxis1=0;
335 int nrow=0;
336 float* psrc=NULL;
337 float* pext=NULL;
338 float* pwav=NULL;
339 float* pcor=NULL;
340 float* pref=NULL;
341 float* peph=NULL;
342
343
344 double exptime=0;
345 double confidence=0;
346 cpl_vector* rec_profile=NULL;
347 double x=0;
348 int i=0;
349 double airmass=0;
350 double binsize=0.1; /* AA/pxl */
351
352 xsh_msg("Welcome to XSHOOTER Pipeline release %d.%d.%d",
353 XSH_MAJOR_VERSION,XSH_MINOR_VERSION,XSH_MICRO_VERSION);
354
355 /*
356 check( xsh_begin( frames, parlist, &instrument, &raws, &calib,
357 recipe_tags, recipe_tags_size,
358 RECIPE_ID, XSH_BINARY_VERSION,
359 xsh_util_efficiency_description_short ) ) ;
360 */
361
362 check(param=cpl_parameterlist_find(parlist,"xsh.xsh_util_efficiency.airmass"));
363 check(airmass=cpl_parameter_get_double(param));
364
365
366 check(param=cpl_parameterlist_find(parlist,"xsh.xsh_util_efficiency.gain"));
367 check(gain=cpl_parameter_get_double(param));
368
369 /* HOW TO RETRIEVE INPUT PARAMETERS */
370 /* --stropt */
371 check(frm_sci=cpl_frameset_get_frame(frames,0));
372 check(frm_ref=cpl_frameset_get_frame(frames,1));
373 check(frm_atmext=cpl_frameset_get_frame(frames,2));
374
375 check(name=cpl_frame_get_filename(frm_sci));
376 check(ima_sci=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
377 check(plist=cpl_propertylist_load(name,0));
378 check(crpix1=xsh_pfits_get_crpix1(plist));
379 check(crval1=xsh_pfits_get_crval1(plist));
380 check(cdelt1=xsh_pfits_get_cdelt1(plist));
381 check(naxis1=xsh_pfits_get_naxis1(plist));
383 xsh_free_propertylist(&plist);
384
385 src_wmin=crval1;
386 src_wmax=src_wmin+naxis1*cdelt1;
387
388 check(name=cpl_frame_get_filename(frm_ref));
389 check(tbl_ref=cpl_table_load(name,1,0));
390
391 check(name=cpl_frame_get_filename(frm_atmext));
392 check(tbl_atmext=cpl_table_load(name,1,0));
393
394 cpl_table_divide_scalar(tbl_ref,"WAVELENGTH",10.);
395 check(ref_wmin=cpl_table_get_column_min(tbl_ref,"WAVELENGTH"));
396 check(ref_wmax=cpl_table_get_column_max(tbl_ref,"WAVELENGTH"));
397
398
399 cpl_table_divide_scalar(tbl_atmext,"LAMBDA",10.);
400 check(ext_wmin=cpl_table_get_column_min(tbl_atmext,"LAMBDA"));
401 check(ext_wmax=cpl_table_get_column_max(tbl_atmext,"LAMBDA"));
402 nrow=cpl_table_get_nrow(tbl_atmext);
403 ext_wdel=(ext_wmax-ext_wmin)/(nrow-1);
404 xsh_msg("wdel=%g",ext_wdel);
405 wmin=(src_wmin>ref_wmin) ? src_wmin : ref_wmin;
406 wmax=(src_wmax<ref_wmax) ? src_wmax : ref_wmax;
407 xsh_msg("eff wave min: %g max: %g",wmin,wmax);
408
409 wmin=(ext_wmin>wmin) ? ext_wmin : wmin;
410 wmax=(ext_wmax<wmax) ? ext_wmax : wmax;
411
412 xsh_msg("ref wave min: %g max: %g",ref_wmin,ref_wmax);
413 xsh_msg("src wave min: %g max: %g",src_wmin,src_wmax);
414 xsh_msg("ext wave min: %g max: %g",ext_wmin,ext_wmax);
415 xsh_msg("eff wave min: %g max: %g",wmin,wmax);
416
417 check(cpl_table_and_selected_float(tbl_ref,"WAVELENGTH",CPL_NOT_LESS_THAN,wmin));
418 check(cpl_table_and_selected_float(tbl_ref,"WAVELENGTH",CPL_NOT_GREATER_THAN,wmax));
419 check(tbl_ext=cpl_table_extract_selected(tbl_ref));
420 check(ref_wmin=cpl_table_get_column_min(tbl_ext,"WAVELENGTH"));
421 check(ref_wmax=cpl_table_get_column_max(tbl_ext,"WAVELENGTH"));
422 xsh_msg("sel wave min: %g max: %g",ref_wmin,ref_wmax);
423
424 check(nrow=cpl_table_get_nrow(tbl_ext));
425
426 check(cpl_table_new_column(tbl_ext,"SRC",CPL_TYPE_FLOAT));
427 check(cpl_table_fill_column_window_float(tbl_ext,"SRC",0,nrow,-1));
428
429 check(cpl_table_new_column(tbl_ext,"REF",CPL_TYPE_FLOAT));
430 check(cpl_table_fill_column_window_float(tbl_ext,"REF",0,nrow,-1));
431
432 check(cpl_table_new_column(tbl_ext,"EXT",CPL_TYPE_FLOAT));
433 check(cpl_table_fill_column_window_float(tbl_ext,"EXT",0,nrow,-1));
434
435 check(cpl_table_new_column(tbl_ext,"COR",CPL_TYPE_FLOAT));
436 check(cpl_table_fill_column_window_float(tbl_ext,"COR",0,nrow,-1));
437
438 check(cpl_table_new_column(tbl_ext,"EPHOT",CPL_TYPE_FLOAT));
439 check(cpl_table_fill_column_window_float(tbl_ext,"EPHOT",0,nrow,-1));
440
441
442 check(psrc=cpl_table_get_data_float(tbl_ext,"SRC"));
443 check(pref=cpl_table_get_data_float(tbl_ext,"REF"));
444 check(pext=cpl_table_get_data_float(tbl_ext,"EXT"));
445 check(pcor=cpl_table_get_data_float(tbl_ext,"COR"));
446 check(peph=cpl_table_get_data_float(tbl_ext,"EPHOT"));
447 check(peph=cpl_table_get_data_float(tbl_ext,"EPHOT"));
448
449 check(pwav=cpl_table_get_data_float(tbl_ext,"WAVELENGTH"));
450 check( rec_profile = cpl_vector_new( CPL_KERNEL_DEF_SAMPLES ) ) ;
451 check( cpl_vector_fill_kernel_profile( rec_profile,
452 CPL_KERNEL_DEFAULT,
453 1 ) ) ;
454 for(i=0;i<nrow;i++) {
455 x=(pwav[i]-src_wmin)/cdelt1;
456 /* xsh_msg("x=%g",x); */
457 check(psrc[i]=cpl_image_get_interpolated( ima_sci,x, 1,
459 1,
461 1,
462 &confidence ) ) ;
463
464
465
466
467 check(pext[i]=xsh_table_get_interpolated(tbl_atmext,pwav[i],"LAMBDA",
469
470 check(pref[i]=xsh_table_get_interpolated(tbl_ref,pwav[i],
471 "WAVELENGTH","FLUX"));
472
473 pcor[i]=pow(10,(0.4*pext[i]*aimprim));
474 if(airmass != 0) pcor[i]/=pow(10,(0.4*pext[i]*airmass));
475 peph[i]=1.986e-16/(pwav[i]*1e-8);
476
477 }
478
479 check(cpl_table_duplicate_column(tbl_ext,"SRC_COR",tbl_ext,"SRC"));
480 check(cpl_table_multiply_columns(tbl_ext,"SRC_COR","COR"));
481 check(cpl_table_duplicate_column(tbl_ext,"EFF",tbl_ext,"SRC_COR"));
482 //check(cpl_table_divide_scalar(tbl_ext,"EFF",binsize));
483 check(cpl_table_multiply_scalar(tbl_ext,"EFF",gain));
484 check(cpl_table_multiply_columns(tbl_ext,"EFF","EPHOT"));
485 check(cpl_table_divide_scalar(tbl_ext,"EFF",exptime));
486 check(cpl_table_divide_scalar(tbl_ext,"EFF",TEL_AREA));
487 check(cpl_table_divide_columns(tbl_ext,"EFF","REF"));
488
489 check(cpl_table_save(tbl_ext,NULL,NULL,"tab.fits",CPL_IO_DEFAULT));
490 cleanup:
491 xsh_free_propertylist(&plist);
492 xsh_free_image(&ima_sci);
493 xsh_free_table(&tbl_ref);
494 xsh_free_table(&tbl_ext);
495 xsh_free_table(&tbl_atmext);
497 if (cpl_error_get_code()) {
498 return -1 ;
499 } else {
500 return 0 ;
501 }
502
503}
static double exptime
static xsh_instrument * instrument
#define check(COMMAND)
Definition: xsh_error.h:71
int * y
int * x
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
double xsh_pfits_get_crpix1(const cpl_propertylist *plist)
find out the CRPIX1 value
Definition: xsh_pfits.c:1965
double xsh_pfits_get_cdelt1(const cpl_propertylist *plist)
find out the cdelt1
Definition: xsh_pfits.c:2196
double xsh_pfits_get_exptime(const cpl_propertylist *plist)
find out the exposure time
Definition: xsh_pfits.c:2254
int xsh_pfits_get_naxis1(const cpl_propertylist *plist)
find out the NAXIS1 value
Definition: xsh_pfits.c:227
double xsh_pfits_get_crval1(const cpl_propertylist *plist)
find out the crval1
Definition: xsh_pfits.c:1907
static cpl_vector * rec_profile
Definition: xsh_rectify.c:80
static int xsh_util_efficiency_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
static int xsh_util_efficiency_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
static int xsh_util_efficiency(cpl_parameterlist *, cpl_frameset *)
Get the command line options and execute the data reduction.
static double xsh_table_get_interpolated(cpl_table *tbl, double wav, const char *colx, const char *coly)
static int xsh_util_efficiency_create(cpl_plugin *)
Setup the recipe options
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
Definition: xsh_utils.c:2284
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
Definition: xsh_utils.c:2116
const char * xsh_get_license(void)
Get the pipeline copyright and license.
Definition: xsh_utils.c:1193
void xsh_init(void)
Reset library state.
Definition: xsh_utils.c:1160
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
Definition: xsh_utils.c:2133
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
#define XSH_ATMOS_EXT_LIST_COLNAME_K
#define XSH_EFFICIENCY
Definition: xsh_dfs.h:217
cpl_error_code xsh_parameters_decode_bp(const char *recipe_id, cpl_parameterlist *plist, const int ival)
void xsh_parameters_generic(const char *recipe_id, cpl_parameterlist *plist)
#define RECIPE_CONTACT
static char xsh_util_efficiency_description[]
#define TEL_AREA
static char xsh_util_efficiency_description_short[]
#define RECIPE_ID
#define RECIPE_AUTHOR