X-shooter Pipeline Reference Manual 3.8.15
xsh_compute_absorp.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-02 09:08:49 $
23 * $Revision: 1.4 $
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>
58#include <xsh_dfs.h>
59#include <cpl.h>
60
61/*-----------------------------------------------------------------------------
62 Functions prototypes
63 ----------------------------------------------------------------------------*/
64
65/*-----------------------------------------------------------------------------
66 Implementation
67 ----------------------------------------------------------------------------*/
68
69
70
71
72/*---------------------------------------------------------------------------*/
82/*---------------------------------------------------------------------------*/
83void xsh_mark_tell( cpl_frame *s1d_frame, cpl_frame *tellmask_frame)
84{
85 xsh_spectrum *s1d = NULL;
86 int *qual = NULL;
87 int size;
88 cpl_vector *tellmask_vect = NULL;
89 int tellmask_size, i;
90 const char *s1d_name = NULL;
91 const char *tag = NULL;
92 cpl_frame *result = NULL;
93
94 XSH_ASSURE_NOT_NULL( s1d_frame);
95
96 check( tag = cpl_frame_get_tag( s1d_frame));
97 check( s1d_name = cpl_frame_get_filename( s1d_frame));
98
99 check( s1d = xsh_spectrum_load( s1d_frame));
100 check( qual = xsh_spectrum_get_qual( s1d));
102
103 if ( tellmask_frame != NULL){
104 const char* tellmask_name = NULL;
105
106 check( tellmask_name = cpl_frame_get_filename( tellmask_frame));
107 xsh_msg("Use telluric mask %s", tellmask_name);
108
109 check( tellmask_vect = cpl_vector_load( tellmask_name, 0));
110 check( tellmask_size = cpl_vector_get_size( tellmask_vect));
111
112 XSH_ASSURE_NOT_ILLEGAL( tellmask_size == size);
113
114 for( i=0; i< size; i++){
115 double mask_val;
116
117 mask_val = cpl_vector_get( tellmask_vect, i);
118
119 if ( mask_val > 0){
121 }
122 }
123 check( result = xsh_spectrum_save( s1d, s1d_name, tag));
124 }
125 else{
126 xsh_msg("No telluric mask");
127 }
128
129 cleanup:
130 xsh_free_frame( &result);
131 xsh_spectrum_free( &s1d);
132 xsh_free_vector( &tellmask_vect);
133}
134/*---------------------------------------------------------------------------*/
135
136/*---------------------------------------------------------------------------*/
152/*---------------------------------------------------------------------------*/
153cpl_frame* xsh_compute_absorp( cpl_frame *s1d_frame, cpl_frame *telllist_frame,
154 int filter_hsize, double threshold, xsh_instrument* instr)
155{
156 cpl_frame *result = NULL;
157 xsh_spectrum *s1d = NULL;
158 double *flux_data = NULL;
159 cpl_vector *flux_vect = NULL;
160 cpl_vector *smooth_vect = NULL;
161 double *diff_data = NULL;
162 cpl_vector *diff_vect = NULL;
163 cpl_vector *std_vect = NULL;
164 cpl_vector *std_box_vect = NULL;
165 cpl_vector *tellmask = NULL;
166 int i, size = 0;
167 double wmin=0, wstep=0;
168
169 char mask_name[256];
170 char tag[16];
171
172 int *tellflag = NULL;
173 const char* tellist_name = NULL;
174 cpl_table *tellist_table = NULL;
175
176 XSH_ASSURE_NOT_NULL( s1d_frame);
177 XSH_ASSURE_NOT_NULL( instr);
178
179 xsh_msg( "Compute absorp");
180
181 XSH_REGDEBUG("File %s list %s filter %d threshold %f", cpl_frame_get_filename( s1d_frame),
182 cpl_frame_get_filename( telllist_frame), filter_hsize, threshold);
183
184 check( s1d = xsh_spectrum_load( s1d_frame));
185
186 wmin = s1d->lambda_min;
187 wstep = s1d->lambda_step;
188
190 check( flux_data = xsh_spectrum_get_flux( s1d));
191 check( flux_vect = cpl_vector_wrap( size, flux_data));
192
193 check( smooth_vect = cpl_vector_filter_median_create( flux_vect,
194 filter_hsize));
195 check( diff_vect = cpl_vector_new( size));
196
197 for( i=0; i< size; i++){
198 double smooth_val, dval;
199
200 smooth_val = cpl_vector_get( smooth_vect, i);
201 dval = (flux_data[i]-smooth_val)/smooth_val;
202 cpl_vector_set( diff_vect, i, dval);
203 }
204
205
206 check( std_vect = cpl_vector_new( size));
207 check( diff_data = cpl_vector_get_data( diff_vect));
208
209 for(i=0; i< (size-2*filter_hsize-1); i++){
210 double dval;
211
212 std_box_vect = cpl_vector_wrap( filter_hsize*2+1, diff_data+i-filter_hsize);
213 dval = cpl_vector_get_stdev( std_box_vect);
214 cpl_vector_set( std_vect, i, dval);
215 xsh_unwrap_vector( &std_box_vect);
216 }
217
218 tellmask = cpl_vector_new( size);
219
220 XSH_CALLOC( tellflag, int , size);
221
222 /* load telluric list table */
223 if ( telllist_frame != NULL){
224 int it=0, table_size;
225 double slit=0.0;
226 double r1, r2;
227 double factor_min, factor_max;
228
229
230 check( tellist_name = cpl_frame_get_filename( telllist_frame));
231 XSH_TABLE_LOAD( tellist_table, tellist_name);
232
233 table_size = cpl_table_get_nrow( tellist_table);
234
235 check( xsh_sort_table_1( tellist_table, LSTART_COLUMN_NAME, CPL_FALSE));
236
237 if ( xsh_instrument_get_arm( instr) == XSH_ARM_UVB){
239 }
240 else if ( xsh_instrument_get_arm( instr) == XSH_ARM_VIS){
242 }
243 else{
245 }
246 check( slit = xsh_pfits_get_slit_width( s1d->flux_header, instr));
247 check ( r2 = xsh_resolution_get( instr, slit));
248 factor_min = (r2-1)/(r1-1)*r1/r2;
249 factor_max = (r2+1)/(r1+1)*r1/r2;
250 i=0;
251 while( i< size){
252 double wave, wtellmin, wtellmax;
253 int good;
254
255 tellflag[i] = 1;
256 wave = wmin+i*wstep;
257 check( wtellmin = cpl_table_get_float( tellist_table, LSTART_COLUMN_NAME,
258 it, &good));
259 check( wtellmax = cpl_table_get_float( tellist_table, LEND_COLUMN_NAME,
260 it, &good));
261 //xsh_msg("wave %f IT %d wtell [%f,%f]", wave, it, wtellmin, wtellmax);
262 wtellmin *= factor_min;
263 wtellmax *= factor_max;
264 //xsh_msg("With resolution correction: wave %f IT %d wtell [%f,%f]", wave, it, wtellmin, wtellmax);
265
266 if ( wave >= wtellmin){
267 if ( wave <= wtellmax){
268 tellflag[i] = 0;
269 i++;
270 }
271 else{
272 if ( it < table_size-1){
273 it++;
274 }
275 else{
276 i++;
277 }
278 }
279 }
280 else{
281 i++;
282 }
283 }
284 }
285
286
287 /* flag telluric */
288 for(i=0; i< size; i++){
289 //double wave;
290 double noise;
291 int flag;
292
293 //wave = wmin+i*wstep;
294 noise = cpl_vector_get( std_vect, i);
295 flag = tellflag[i];
296
297 /* detect telluric */
298 if ( flag == 0 && noise > threshold){
299 cpl_vector_set( tellmask, i,1.0);
300 }
301 else{
302 cpl_vector_set( tellmask, i,0.0);
303 }
304 }
305
306
307 {
308 FILE* debug_file = NULL;
309
310 debug_file = fopen( "out.dat", "w+");
311
312 fprintf( debug_file, "#lambda flux smooth diff flag is_tell\n");
313
314 for(i=0; i< size; i++){
315 double mask_val;
316
317 mask_val = cpl_vector_get( tellmask, i);
318 fprintf( debug_file, "%f %f %f %f %f %d\n", wmin+wstep*i, flux_data[i],
319 cpl_vector_get( smooth_vect, i), cpl_vector_get( std_vect, i),
320 mask_val*100, tellflag[i]*100);
321 }
322 fclose(debug_file);
323 }
324
325
326 /* save mask */
327
328 sprintf( mask_name, "TELL_MASK.fits");
329 sprintf(tag, "%s_%s", XSH_TELL_MASK, xsh_instrument_arm_tostring( instr));
330
331 check( cpl_vector_save( tellmask, mask_name, XSH_PRE_QUAL_BPP,
332 s1d->flux_header, CPL_IO_CREATE));
333 result = cpl_frame_new ();
334
335 check( cpl_frame_set_filename ( result, mask_name));
336 check( cpl_frame_set_tag( result,tag));
337 check( cpl_frame_set_type( result, CPL_FRAME_TYPE_IMAGE));
338 check( cpl_frame_set_group ( result, CPL_FRAME_GROUP_PRODUCT)) ;
339
340 check( cpl_frame_set_level( result, CPL_FRAME_LEVEL_TEMPORARY));
341 xsh_add_temporary_file( mask_name);
342
343
344 cleanup:
345 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
346 xsh_free_frame( &result);
347 }
348 XSH_FREE( tellflag);
349 xsh_unwrap_vector( &flux_vect);
350 xsh_spectrum_free( &s1d);
351 xsh_free_vector( &smooth_vect);
352 xsh_free_vector( &diff_vect);
353 xsh_free_vector( &std_vect);
354 xsh_free_vector( &tellmask);
355 XSH_TABLE_FREE( tellist_table);
356 return result;
357}
358/*---------------------------------------------------------------------------*/
cpl_frame * xsh_compute_absorp(cpl_frame *s1d_frame, cpl_frame *telllist_frame, int filter_hsize, double threshold, xsh_instrument *instr)
Compute the shift in slit between reference wavelength and others for all the slitlets.
void xsh_mark_tell(cpl_frame *s1d_frame, cpl_frame *tellmask_frame)
Mark telluric in spectrum.
int xsh_spectrum_get_size_lambda(xsh_spectrum *s)
Get lambda axis size of spectrum.
xsh_spectrum * xsh_spectrum_load(cpl_frame *s1d_frame)
Load a 1D spectrum structure.
int * xsh_spectrum_get_qual(xsh_spectrum *s)
Get qual of spectrum.
int xsh_spectrum_get_size(xsh_spectrum *s)
Get 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.
void xsh_spectrum_free(xsh_spectrum **s)
free memory associated to an 1D spectrum
#define XSH_REGDEBUG(...)
Definition: xsh_error.h:132
#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.
double xsh_resolution_get(xsh_instrument *instrument, double slit)
Get the resoltion.
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
int size
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
double xsh_pfits_get_slit_width(const cpl_propertylist *plist, xsh_instrument *instrument)
find out the INS OPTIx NAME value (the width of the slit)
Definition: xsh_pfits.c:580
void xsh_unwrap_vector(cpl_vector **v)
Unwrap a vector and set the pointer to NULL.
Definition: xsh_utils.c:2345
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
cpl_error_code xsh_sort_table_1(cpl_table *t, const char *column, cpl_boolean reverse)
Sort a table by one column.
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
Definition: xsh_utils.c:1432
cpl_propertylist * flux_header
#define QFLAG_TELLURIC_UNCORRECTED
@ XSH_ARM_UVB
@ XSH_ARM_VIS
#define XSH_PRE_QUAL_BPP
Definition: xsh_data_pre.h:47
int threshold
Definition: xsh_detmon_lg.c:90
#define XSH_TELL_MASK
Definition: xsh_dfs.h:1183
#define GUESS_TELL_MASK_RESOLUTION_NIR
Definition: xsh_drl.h:732
#define GUESS_TELL_MASK_RESOLUTION_UVB
Definition: xsh_drl.h:730
#define LSTART_COLUMN_NAME
Definition: xsh_drl.h:728
#define LEND_COLUMN_NAME
Definition: xsh_drl.h:729
#define GUESS_TELL_MASK_RESOLUTION_VIS
Definition: xsh_drl.h:731
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92
#define XSH_CALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:56
#define XSH_TABLE_LOAD(TABLE, NAME)
#define XSH_TABLE_FREE(TABLE)