X-shooter Pipeline Reference Manual 3.8.15
xsh_data_star_flux.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-03-15 11:50:29 $
23 * $Revision: 1.26 $
24 */
25
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*---------------------------------------------------------------------------*/
40/*---------------------------------------------------------------------------*/
41
42
43/*----------------------------------------------------------------------------
44 Includes
45 ----------------------------------------------------------------------------*/
46
47#include <xsh_dfs.h>
48#include <xsh_error.h>
49#include <xsh_msg.h>
50#include <xsh_pfits.h>
51#include <cpl.h>
52#include <string.h>
53#include <time.h>
54#include <math.h>
55#include <xsh_utils_table.h>
56#include <xsh_data_star_flux.h>
57#include <xsh_data_spectrum.h>
58#include <xsh_data_spectrum1D.h>
59
60/*----------------------------------------------------------------------------
61 Function prototypes
62 ----------------------------------------------------------------------------*/
63#define XSH_STAR_FLUX_UVB_WAV_MIN 308
64/*-----------------------------------------------------------------------------
65 Implementation
66 -----------------------------------------------------------------------------*/
67cpl_error_code
69 int hsize )
70{
71
72 cpl_vector* vflux=NULL;
73 cpl_vector* vmedian=NULL;
74 double* pvmedian=NULL;
75 int i=0;
76
77 XSH_ASSURE_NOT_NULL_MSG(result, "Null input flux list table frame");
78 XSH_ASSURE_NOT_ILLEGAL_MSG(result->size > 2*hsize,"size < 2*hsize. You set a too large half window size.");
79
80 vflux=cpl_vector_wrap(result->size,result->flux);
81 vmedian=cpl_vector_filter_median_create(vflux,hsize);
82 pvmedian=cpl_vector_get_data(vmedian);
83
84 for(i=0;i<result->size;i++) {
85 result->flux[i]=pvmedian[i];
86 }
87cleanup:
88
89 cpl_vector_unwrap(vflux);
90 xsh_free_vector(&vmedian);
91 return cpl_error_get_code();
92}
93
94cpl_error_code
96 cpl_lowpass filter_type,
97 int hsize )
98{
99
100 cpl_vector* vflux=NULL;
101 cpl_vector* vlowpass=NULL;
102 double* pvlowpass=NULL;
103 int i=0;
104
105 XSH_ASSURE_NOT_NULL_MSG(result, "Null input flux list table frame");
106 XSH_ASSURE_NOT_ILLEGAL_MSG(result->size > 2*hsize,"size < 2*hsize. You set a too large half window size.");
107
108 vflux=cpl_vector_wrap(result->size,result->flux);
109 vlowpass=cpl_vector_filter_lowpass_create(vflux,filter_type,hsize);
110 pvlowpass=cpl_vector_get_data(vlowpass);
111
112 for(i=0;i<result->size;i++) {
113 result->flux[i]=pvlowpass[i];
114 }
115cleanup:
116
117 cpl_vector_unwrap(vflux);
118 xsh_free_vector(&vlowpass);
119 return cpl_error_get_code();
120}
121
122
124{
125 xsh_star_flux_list * result = NULL ;
126
127 /* Create the list */
128 XSH_CALLOC( result, xsh_star_flux_list, 1 ) ;
129 result->header = NULL ;
130 result->size = size ;
131 XSH_CALLOC( result->lambda, double, size ) ;
132 XSH_CALLOC( result->flux, double, size ) ;
133
134 cleanup:
135 return result ;
136}
137
139{
140 cpl_table *table = NULL ;
141 const char * tablename = NULL ;
142 xsh_star_flux_list * result = NULL ;
143 int nentries, i ;
144 double * plambda, * pflux ;
145
146 /* verify input */
147 XSH_ASSURE_NOT_NULL( star_frame);
148
149 /* get table filename */
150 check(tablename = cpl_frame_get_filename( star_frame ));
151
152 /* Load the table */
153 XSH_TABLE_LOAD( table, tablename ) ;
154
155 /*
156 cpl_table_and_selected_double(table,XSH_STAR_FLUX_LIST_COLNAME_WAVELENGTH,CPL_LESS_THAN,XSH_STAR_FLUX_UVB_WAV_MIN);
157 cpl_table_erase_selected(table);
158 */
159
160 check( nentries = cpl_table_get_nrow( table ) ) ;
161
162 /* Create the list */
163 check( result = xsh_star_flux_list_create( nentries ) ) ;
164
165 plambda = result->lambda ;
166 pflux = result->flux ;
167
168 check(result->header = cpl_propertylist_load(tablename, 0));
169
170 check(cpl_table_cast_column(table,XSH_STAR_FLUX_LIST_COLNAME_WAVELENGTH,"F_WAVELENGTH",CPL_TYPE_FLOAT));
171 check(cpl_table_cast_column(table,XSH_STAR_FLUX_LIST_COLNAME_FLUX,"F_FLUX",CPL_TYPE_FLOAT))
172;
173
174 /* Fill the list */
175 for( i = 0 ; i< nentries ; i++, plambda++, pflux++ ) {
176 float value ;
177
178 check( xsh_get_table_value( table, "F_WAVELENGTH",
179 CPL_TYPE_FLOAT, i, &value ) ) ;
180 *plambda = value ;
181 check( xsh_get_table_value( table, "F_FLUX",
182 CPL_TYPE_FLOAT, i, &value ) ) ;
183 *pflux = value ;
184 }
185
186 cleanup:
187 if (cpl_error_get_code () != CPL_ERROR_NONE) {
188 xsh_error_msg("can't load frame %s",cpl_frame_get_filename(star_frame));
190 }
191 XSH_TABLE_FREE( table);
192 return result ;
193}
194
195cpl_error_code
197{
198 xsh_spectrum *s = NULL ;
199 const char * name = NULL ;
200 int size=0;
201 double* pflux=NULL;
202 int i=0;
203 const char* tag=NULL;
204 cpl_frame* frm=NULL;
205 /* verify input */
206 XSH_ASSURE_NOT_NULL( frame);
207 XSH_ASSURE_NOT_NULL( list);
208
209 s=xsh_spectrum_load(frame);
210
211 /* basic checks: verify list versus spectrum size and wave range */
212 size=s->size;
213 cpl_ensure_code(list->size == s->size, CPL_ERROR_ILLEGAL_INPUT);
214
215 cpl_ensure_code(list->lambda[0] == s->lambda_min, CPL_ERROR_ILLEGAL_INPUT);
216 cpl_ensure_code(list->lambda[size-1] == s->lambda_max, CPL_ERROR_ILLEGAL_INPUT);
217
218 /* copy flux from list to specrum */
220 for(i=0;i<size;i++) {
221 pflux[i]=list->flux[i];
222 }
223
224 /* save result */
225 name = cpl_frame_get_filename(frame);
226 tag = cpl_frame_get_tag(frame);
227 frm=xsh_spectrum_save(s,name,tag);
228
229 cleanup:
230 xsh_free_frame(&frm);
232 return cpl_error_get_code() ;
233}
234
235
236
238{
239
240 //const char * imagename = NULL ;
241 xsh_star_flux_list * result = NULL ;
242 int nentries, i ;
243 double * plambda, * pflux ;
244 xsh_spectrum* spectrum=NULL;
245
246 /* verify input */
247 XSH_ASSURE_NOT_NULL( star_frame);
248
249 /* get table filename */
250 //check(imagename = cpl_frame_get_filename( star_frame ));
251
252 /* Load the image */
253 check(spectrum=xsh_spectrum_load(star_frame)) ;
254 nentries = xsh_pfits_get_naxis1(spectrum->flux_header);
255
256 /* Create the list */
257 check( result = xsh_star_flux_list_create( nentries ) ) ;
258 result->header = cpl_propertylist_duplicate(spectrum->flux_header);
259 plambda = result->lambda ;
260 pflux = result->flux ;
261
262 double* pima=NULL;
263 pima=cpl_image_get_data_double(spectrum->flux);
264
265 double wmin=0;
266 wmin = xsh_pfits_get_crval1(result->header);
267 double wstep=0;
268 wstep = xsh_pfits_get_cdelt1(result->header);
269
270 /* Fill the list */
271 for( i = 0 ; i< nentries ; i++, plambda++, pflux++ ) {
272
273 *plambda = (float) (wmin+i*wstep) ;
274 *pflux = (float) pima[i];
275
276 }
277
278 cleanup:
279 if (cpl_error_get_code () != CPL_ERROR_NONE) {
280 xsh_error_msg("can't load frame %s",cpl_frame_get_filename(star_frame));
282 }
283
284 xsh_spectrum_free( &spectrum );
285 return result ;
286}
287
289{
290 if ( list != NULL && *list != NULL ) {
291 xsh_free_propertylist(&(*list)->header);
292 cpl_free( (*list)->lambda ) ;
293 cpl_free( (*list)->flux ) ;
294 //check( cpl_free( (*list)->header ) );
295 cpl_free( *list ) ;
296 *list = NULL ;
297 }
298
299
300 return ;
301}
302
303cpl_error_code
305{
306 FILE * fout ;
307 int size=0;
308 int i=0;
309 double * plambda=NULL;
310 double * pflux=NULL;
311
312 XSH_ASSURE_NOT_NULL_MSG(list,"Null input std star flux list!Exit");
313 size=list->size;
314
315 plambda = list->lambda ;
316 pflux = list->flux ;
317
318 if((fout=fopen(filename,"w"))==NULL) {
319
320 return CPL_ERROR_FILE_IO ;
321 } else {
322 for(i=0;i<size;i++) {
323 fprintf(fout, "%f %f \n", plambda[i], pflux[i]);
324 }
325 }
326 if ( fout ) fclose( fout ) ;
327
328 cleanup:
329 return cpl_error_get_code() ;
330}
331
332
333
335 const char * filename, const char * tag )
336{
337 cpl_table * table = NULL ;
338 cpl_frame * result = NULL ;
339 int size, i ;
340 double * plambda, * pflux ;
341
343 XSH_ASSURE_NOT_NULL(filename);
344
345 /* create a table */
346 check(table = cpl_table_new( 2 ));
347
348 /* create column names */
349 check(
350 cpl_table_new_column(table, XSH_STAR_FLUX_LIST_COLNAME_WAVELENGTH,
351 CPL_TYPE_FLOAT));
352 check(
353 cpl_table_new_column(table, XSH_STAR_FLUX_LIST_COLNAME_FLUX,
354 CPL_TYPE_FLOAT));
355
356 size = list->size ;
357 plambda = list->lambda ;
358 pflux = list->flux ;
359
360 check(cpl_table_set_size(table, size));
361
362 /* insert data */
363 for( i = 0 ; i<size ; i++, plambda++, pflux++ ) {
364 float value ;
365
366 value = *plambda ;
367 check(cpl_table_set_float(table, XSH_STAR_FLUX_LIST_COLNAME_WAVELENGTH,
368 i, value ));
369 value = *pflux ;
370 check(cpl_table_set_float(table, XSH_STAR_FLUX_LIST_COLNAME_FLUX,
371 i, value ));
372 }
373
374 /* create fits file */
375 check(cpl_table_save( table, list->header, NULL, filename, CPL_IO_DEFAULT));
376 //check( xsh_add_temporary_file( filename ));
377
378 /* Create the frame */
379 check(result=xsh_frame_product( filename, tag,
380 CPL_FRAME_TYPE_TABLE,
381 CPL_FRAME_GROUP_PRODUCT,
382 CPL_FRAME_LEVEL_TEMPORARY));
383
384 xsh_msg_dbg_low( "Star Flux Frame Saved" ) ;
385
386 cleanup:
387 XSH_TABLE_FREE( table);
388 return result ;
389}
390
391
392
393
394cpl_frame *
396 const char * filename,
397 const char * tag,
398 const int order )
399{
400 cpl_table * table = NULL ;
401 cpl_frame * result = NULL ;
402 int size, i ;
403 double * plambda, * pflux ;
404
406 XSH_ASSURE_NOT_NULL(filename);
407
408 /* create a table */
409 check(table = cpl_table_new( 2 ));
410
411 /* create column names */
412 check(
413 cpl_table_new_column(table, XSH_STAR_FLUX_LIST_COLNAME_WAVELENGTH,
414 CPL_TYPE_FLOAT));
415 check(
416 cpl_table_new_column(table, XSH_STAR_FLUX_LIST_COLNAME_FLUX,
417 CPL_TYPE_FLOAT));
418
419 size = list->size ;
420 plambda = list->lambda ;
421 pflux = list->flux ;
422
423 check(cpl_table_set_size(table, size));
424
425 /* insert data */
426 for( i = 0 ; i<size ; i++, plambda++, pflux++ ) {
427 float value ;
428
429 value = *plambda ;
430 check(cpl_table_set_float(table, XSH_STAR_FLUX_LIST_COLNAME_WAVELENGTH,
431 i, value ));
432 value = *pflux ;
433 check(cpl_table_set_float(table, XSH_STAR_FLUX_LIST_COLNAME_FLUX,
434 i, value ));
435 }
436
437 /* create fits file */
438 if(order==0) {
439 check(cpl_table_save( table,list->header,NULL,filename,CPL_IO_DEFAULT));
440 } else {
441 check(cpl_table_save( table,list->header,NULL,filename,CPL_IO_EXTEND));
442 }
443 //check( xsh_add_temporary_file( filename ));
444
445 /* Create the frame */
446 check(result=xsh_frame_product( filename, tag,
447 CPL_FRAME_TYPE_TABLE,
448 CPL_FRAME_GROUP_PRODUCT,
449 CPL_FRAME_LEVEL_TEMPORARY));
450
451 xsh_msg_dbg_low( "Star Flux Frame Saved" ) ;
452
453 cleanup:
454 XSH_TABLE_FREE( table);
455 return result ;
456}
457
458
459
460
462{
463 XSH_ASSURE_NOT_NULL( list ) ;
464
465 cleanup:
466 return list->lambda ;
467}
468
470{
471 XSH_ASSURE_NOT_NULL( list ) ;
472
473 cleanup:
474 return list->flux ;
475}
476
477
479{
480 xsh_star_flux_list* result=NULL;
481 XSH_ASSURE_NOT_NULL( list ) ;
482
483
484 int size=list->size;
485 /* Create the list */
487
488 //check(result->header = cpl_propertylist_duplicate(list->header));
489
490
491 memcpy(result->lambda,list->lambda,size*sizeof(double));
492 memcpy(result->flux,list->flux,size*sizeof(double));
493
494 cleanup:
495 return result;
496}
497
499{
500 XSH_ASSURE_NOT_NULL( list ) ;
501 int size=list->size;
502 int i=0;
503 int k=0;
504 int off=10;
505 double f1=0;
506 double f2=0;
507 double w1=0;
508 double w2=0;
509 double x1=0;
510 double x2=0;
511 double m=0;
512 double x=0;
513 double w=0;
514 int computed=0;
515
516
517 /* Rayleigh-Jeans extrapolation of null flux values:
518 * we can linearize the formula to have a quick fit
519 * flux=A/lambda^4+B. X=1/lambda^4, Y=f==>
520 * Y=A*X+B==> we can linearize */
521 for (i = 0; i < size; i++) {
522 if (list->lambda[i]<wmax) {
523 k++;
524 } else {
525
526 if (computed == 0) {
527 f2 = list->flux[k];
528 f1 = list->flux[k - off];
529 w2 = 1./list->lambda[k];
530 x2 = w2*w2*w2*w2;
531 w1 = 1./list->lambda[k - off];
532 x1 = w1*w1*w1*w1;
533 m = (f2 - f1) / (x2 - x1);
534 computed=1;
535
536 } else {
537 w=1./list->lambda[i];
538 x=w*w*w*w;
539
540 list->flux[i]=f1+m*(x-x1);
541
542 }
543 }
544 }
545
546 cleanup:
547 return;
548}
549
550cpl_error_code
552{
553
554 XSH_ASSURE_NOT_NULL( result ) ;
555 XSH_ASSURE_NOT_NULL( factor ) ;
556 /* Create the list */
557
558 XSH_ASSURE_NOT_ILLEGAL_MSG(result->size==factor->size,"List of different sizes");
559 int size=result->size;
560 //XSH_ASSURE_NOT_ILLEGAL_MSG(result->lambda[0]==factor->lambda[0],"List of different wave start");
561 //XSH_ASSURE_NOT_ILLEGAL_MSG(result->lambda[size-1]==factor->lambda[size-1],"List of different wave end");
562
563 int i=0;
564 for(i=0;i<size;i++) {
565 result->flux[i] /= factor->flux[i];
566 }
567
568 cleanup:
569 return cpl_error_get_code();
570}
571
xsh_spectrum * xsh_spectrum_load(cpl_frame *s1d_frame)
Load a 1D spectrum structure.
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_ASSURE_NOT_NULL_MSG(pointer, msg)
Definition: xsh_error.h:103
#define check(COMMAND)
Definition: xsh_error.h:71
#define XSH_ASSURE_NOT_ILLEGAL_MSG(cond, msg)
Definition: xsh_error.h:111
#define xsh_error_msg(...)
Definition: xsh_error.h:94
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
int size
int * x
static SimAnneal s
Definition: xsh_model_sa.c:99
#define xsh_msg_dbg_low(...)
Definition: xsh_msg.h:48
double xsh_pfits_get_cdelt1(const cpl_propertylist *plist)
find out the cdelt1
Definition: xsh_pfits.c:2196
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
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_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
cpl_error_code xsh_get_table_value(const cpl_table *table, const char *colname, cpl_type coltype, int i, void *result)
Read a table value from a fits table.
cpl_propertylist * flux_header
cpl_image * flux
cpl_propertylist * header
double * xsh_star_flux_list_get_lambda(xsh_star_flux_list *list)
cpl_error_code xsh_star_flux_list_filter_lowpass(xsh_star_flux_list *result, cpl_lowpass filter_type, int hsize)
cpl_error_code xsh_star_flux_list_divide(xsh_star_flux_list *result, xsh_star_flux_list *factor)
void xsh_star_flux_list_extrapolate_wave_end(xsh_star_flux_list *list, const double wmax)
cpl_frame * xsh_star_flux_list_save_order(xsh_star_flux_list *list, const char *filename, const char *tag, const int order)
cpl_error_code xsh_star_flux_list_to_frame(xsh_star_flux_list *list, cpl_frame *frame)
void xsh_star_flux_list_free(xsh_star_flux_list **list)
xsh_star_flux_list * xsh_star_flux_list_load_spectrum(cpl_frame *star_frame)
cpl_error_code xsh_star_flux_list_dump_ascii(xsh_star_flux_list *list, const char *filename)
xsh_star_flux_list * xsh_star_flux_list_load(cpl_frame *star_frame)
cpl_error_code xsh_star_flux_list_filter_median(xsh_star_flux_list *result, int hsize)
xsh_star_flux_list * xsh_star_flux_list_create(int size)
cpl_frame * xsh_star_flux_list_save(xsh_star_flux_list *list, const char *filename, const char *tag)
double * xsh_star_flux_list_get_flux(xsh_star_flux_list *list)
xsh_star_flux_list * xsh_star_flux_list_duplicate(xsh_star_flux_list *list)
#define XSH_STAR_FLUX_LIST_COLNAME_FLUX
#define XSH_STAR_FLUX_LIST_COLNAME_WAVELENGTH
int m
Definition: xsh_detmon_lg.c:91
int order
Definition: xsh_detmon_lg.c:80
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_CALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:56
#define XSH_TABLE_LOAD(TABLE, NAME)
#define XSH_TABLE_FREE(TABLE)