43#include <gsl/gsl_spline.h>
44#include <gsl/gsl_errno.h>
46#include <gsl/gsl_bspline.h>
47#include <gsl/gsl_multifit.h>
48#include <gsl/gsl_rng.h>
49#include <gsl/gsl_randist.h>
50#include <gsl/gsl_statistics.h>
80#define INTERPOL_WSTEP_NM 2
81#define FILTER_MEDIAN_HSIZE 3
102 for( idx = from, pwave = wave+from ; idx < to ; pwave++, idx++ ) {
103 if ( *pwave >= (lambda-
step/2) && *pwave < (lambda+
step/2) ) {
198 int from,
int to,
double step,
199 int * if0,
int * if1 )
207 for( idx = from, pspec = spectrum+from ; idx < to ; pspec++, idx++ ) {
210 if ( *pspec >= (min-(
step/2.)) ) break ;
213 for( ; idx < to ; pspec++, idx++ ) {
215 if ( *pspec > (
max+(
step/2.)) ) break ;
220 spectrum[*if0], spectrum[*if1], min,
max ) ;
221 if ( *if1 > to ) *if1 = to ;
238 cpl_table*atmos_ext_tab,
240 double** atmos_lambda,
249 double * lambda_star = NULL;
254 nlambda_star = star_list->
size ;
255 lambda_star = star_list->
lambda ;
259 XSH_CALLOC( *atmos_lambda,
double, nlambda_star ) ;
260 XSH_CALLOC( *atmos_K,
double, nlambda_star ) ;
262 xsh_msg_warning(
"You are using an obsolete atm extinction line table");
267 cpl_table_cast_column(atmos_ext_tab,
"LAMBDA",
"WAVE",CPL_TYPE_DOUBLE);
269 nrow=cpl_table_get_nrow(atmos_ext_tab);
270 pw=cpl_table_get_data_double(atmos_ext_tab,
"WAVE");
271 pk=cpl_table_get_data_double(atmos_ext_tab,
"K");
274 for( j = 0 ; j<nlambda_star ; j++) {
276 (*atmos_lambda)[j] = lambda_star[j] ;
282 for( j = 0 ; j<nlambda_star ; j++) {
284 (*atmos_lambda)[j] = lambda_star[j] ;
291 cpl_table_erase_column(atmos_ext_tab,
"WAVE");
292 cpl_table_erase_column(atmos_ext_tab,
"K");
296 return cpl_error_get_code();
325 double * lambda_star = NULL;
326 double lambda_star_min=0;
327 double lambda_star_max=0;
332 nlambda_star = star_list->
size ;
333 lambda_star = star_list->
lambda ;
334 lambda_star_min = *lambda_star ;
335 lambda_star_max = *(lambda_star+nlambda_star-1) ;
336 step_star = (lambda_star_max-lambda_star_min)/(nlambda_star-1) ;
338 if ( phigh != NULL ) {
341 for( kh = 0 ; phigh->
lambda_min != 0. ; kh++, phigh++ ) {
344 start_idx, nlambda_star, step_star ) ;
349 start_idx, nlambda_star, step_star ) ;
363 if(min_idx>=resp_list->
size || max_idx>=resp_list->
size) {
364 xsh_msg(
"min_idx=%d max_idx=%d size=%d",
365 min_idx,max_idx,resp_list->
size);
374 if( (min_idx==-1) && (max_idx == -1) ) {
375 xsh_msg_debug(
"The high abs region is longer than the order length");
376 return cpl_error_get_code();
377 }
else if(min_idx==-1) {
379 min_flux = resp_list->
flux[max_idx] ;
380 max_flux = resp_list->
flux[max_idx+min_step] ;
381 }
else if (max_idx == -1) {
382 max_idx=nlambda_star;
383 min_flux = resp_list->
flux[min_idx-min_step] ;
384 max_flux = resp_list->
flux[min_idx] ;
387 min_flux = resp_list->
flux[min_idx] ;
388 max_flux = resp_list->
flux[max_idx] ;
392 min_flux, max_flux ) ;
393 delta_flux = (max_flux-min_flux)/(min_step) ;
395 for ( ll = min_idx+1 ; ll < max_idx ; ll++ ) {
396 cur_flux=min_flux+delta_flux*(ll-min_idx);
399 resp_list->
flux[ll], cur_flux ) ;
400 resp_list->
flux[ll] = cur_flux ;
408 return cpl_error_get_code();
545 double * lambda_spectrum,
546 double * flux_spectrum,
553 int nlambda_spectrum=0;
554 double * lambda_star = NULL;
555 double * flux_star = NULL ;
560 nlambda_star = star_list->
size ;
561 lambda_star = star_list->
lambda ;
562 flux_star = star_list->
flux ;
564 fout = fopen(
"summed.dat",
"w" ) ;
565 for( i = 0 ; i < nlambda_star ; i++ ) {
566 fprintf( fout,
"%lf %le\n", resp_list->
lambda[i],
571 fout = fopen(
"response.dat",
"w" ) ;
572 for( i = 0 ; i < nlambda_star ; i++ ) {
573 fprintf( fout,
"%lf %le\n", resp_list->
lambda[i],
574 resp_list->
flux[i] ) ;
578 fout = fopen(
"spectrum.dat",
"w" ) ;
579 for ( i = 0 ; i < nlambda_spectrum ; i++ )
580 fprintf( fout,
"%lf %lf\n", lambda_spectrum[i], flux_spectrum[i] ) ;
583 fout = fopen(
"star.dat",
"w" ) ;
584 for ( i = 0 ; i < nlambda_star ; i++ )
585 fprintf( fout,
"%lf %le\n", lambda_star[i], flux_star[i] ) ;
589 return cpl_error_get_code();
596 double * flux_spectrum,
599 int i,
double** flux_added,
int* npixels,
int* nbad)
613 if ( (npix_in_interval - i_dif)>0.9 ) {
614 *nbad = npix_in_interval -i_dif ;
616 pf0 = flux_spectrum + if0 ;
617 pf1 = flux_spectrum + if1 ;
618 qf0 = qual_spectrum + if0 ;
619 for( pf = pf0, qf = qf0 ; pf < pf1 ; pf++, qf++ ) {
628 else (*flux_added)[i] += *pf;
631 if ( nbad != 0 && (npix_in_interval-*nbad) > 0 ) {
632 (*flux_added)[i] /= (double)(npix_in_interval-*nbad)/
633 (double)npix_in_interval ;
641 return cpl_error_get_code();
674 double * flux_added = NULL ;
675 double * lambda_star = NULL;
676 double * flux_star = NULL ;
678 double * lambda_spectrum = NULL;
679 double * plambda=NULL ;
680 double * flux_spectrum = NULL ;
685 int nlambda_spectrum=0;
686 double lambda_spectrum_min=0;
687 double lambda_spectrum_max=0;
688 double lambda_star_min=0;
689 double lambda_star_max=0;
690 double step_spectrum=0 ;
692 int npix_in_interval=0 ;
696 cpl_frame* obj_integrated=NULL;
716 flux_star = star_list->
flux ;
718 lambda_star = star_list->
lambda ;
721 lambda_star_min = *lambda_star ;
722 lambda_star_max = *(lambda_star+
size-1) ;
723 step_star = (lambda_star_max-lambda_star_min)/(
size-1) ;
725 xsh_msg(
"Spectrum - size %d Nlambda: %d, Min: %lf, Max: %lf, Step: %lf",
726 size,nlambda_spectrum, lambda_spectrum_min, lambda_spectrum_max,
740 XSH_CALLOC( lambda_spectrum,
double, nlambda_spectrum ) ;
742 for( lambda = lambda_spectrum_min, plambda = lambda_spectrum, i = 0 ;
744 i++, plambda++, lambda += step_spectrum )
752 npix_in_interval = step_star/step_spectrum ;
753 xsh_msg_dbg_low(
"Nb of pixels max in interval: %d", npix_in_interval ) ;
756 cpl_table* debug = NULL;
758 double* pflux_obs = NULL;
759 double* pflux_ref = NULL;
760 double* pflux_corr=NULL;
761 double* pkavalue = NULL;
762 double* pobj_atm_corr = NULL;
763 double* pobj_bin_cor = NULL;
765 debug = cpl_table_new(
size);
767 cpl_table_new_column(debug,
"wavelength", CPL_TYPE_DOUBLE);
768 cpl_table_new_column(debug,
"flux_obs", CPL_TYPE_DOUBLE);
769 cpl_table_new_column(debug,
"flux_ref", CPL_TYPE_DOUBLE);
770 cpl_table_new_column(debug,
"flux_obs_cor_gain_bin_exptime", CPL_TYPE_DOUBLE);
772 cpl_table_new_column(debug,
"kvalue", CPL_TYPE_DOUBLE);
773 cpl_table_new_column(debug,
"flux_obs_cor_atm", CPL_TYPE_DOUBLE);
774 cpl_table_new_column(debug,
"flux_obs_cor_obs2ref_bin_size", CPL_TYPE_DOUBLE);
776 cpl_table_fill_column_window_double(debug,
"wavelength", 0,
size, 0.);
777 cpl_table_fill_column_window_double(debug,
"flux_obs", 0,
size,0.);
778 cpl_table_fill_column_window_double(debug,
"flux_obs_cor_gain_bin_exptime", 0,
size, 0.);
779 cpl_table_fill_column_window_double(debug,
"flux_ref", 0,
size, 0.);
780 cpl_table_fill_column_window_double(debug,
"kvalue", 0,
size, 0.);
781 cpl_table_fill_column_window_double(debug,
"flux_obs_cor_atm", 0,
size,0.);
782 cpl_table_fill_column_window_double(debug,
"flux_obs_cor_obs2ref_bin_size", 0,
size,0.);
784 pwav = cpl_table_get_data_double(debug,
"wavelength");
785 pflux_obs = cpl_table_get_data_double(debug,
"flux_obs");
786 pflux_corr = cpl_table_get_data_double(debug,
"flux_obs_cor_gain_bin_exptime");
787 pflux_ref = cpl_table_get_data_double(debug,
"flux_ref");
788 pkavalue = cpl_table_get_data_double(debug,
"kvalue");
789 pobj_atm_corr = cpl_table_get_data_double(debug,
"flux_obs_cor_atm");
790 pobj_bin_cor = cpl_table_get_data_double(debug,
"flux_obs_cor_obs2ref_bin_size");
793 cor_fct = gain *
exptime * bin_size;
795 xsh_msg(
"cor lambda sampling: %g %g %g",step_star/step_spectrum,step_star,step_spectrum );
797 for ( i = 0 ; i <
size ; i++ ) {
800 (*obj_list)->flux[i] = flux_spectrum[i]/cor_fct ;
801 (*obj_list)->lambda[i] = lambda_star[i] ;
803 pwav[i]=lambda_star[i];
804 pflux_corr[i]=(*obj_list)->flux[i];
805 pflux_obs[i]=flux_spectrum[i];
806 pflux_ref[i]=flux_star[i];
809 if ( atmos_K != NULL ) {
810 kvalue = pow(10., -airmass*atmos_K[i]*0.4 ) ;
811 (*obj_list)->flux[i] /= kvalue ;
812 pobj_atm_corr[i]=(*obj_list)->flux[i];
818 (*obj_list)->flux[i] *= step_star/step_spectrum ;
819 pobj_bin_cor[i]=(*obj_list)->flux[i];
823 check(cpl_table_duplicate_column(debug,
"response",debug,
"flux_ref"));
825 check(cpl_table_divide_columns(debug,
"response",
"flux_obs_cor_obs2ref_bin_size"));
838 return cpl_error_get_code();
847 cpl_table * atmos_ext_tab,
864 double * lambda_resp = NULL ;
865 double * flux_resp = NULL ;
866 double * flux_added = NULL ;
867 double * lambda_star = NULL;
868 double * flux_star = NULL ;
870 double * lambda_spectrum = NULL;
871 double * plambda=NULL ;
872 double * flux_spectrum = NULL ;
873 int * qual_spectrum = NULL ;
879 int nlambda_spectrum=0;
880 double lambda_spectrum_min=0;
881 double lambda_spectrum_max=0;
882 double lambda_star_min=0;
883 double lambda_star_max=0;
884 double step_spectrum=0 ;
886 int npix_in_interval=0 ;
890 cpl_frame* resp_integrated=NULL;
891 cpl_frame* obj_integrated=NULL;
915 flux_star = star_list->
flux ;
916 nlambda_star = star_list->
size ;
917 lambda_star = star_list->
lambda ;
918 lambda_star_min = *lambda_star ;
919 lambda_star_max = *(lambda_star+nlambda_star-1) ;
920 step_star = (lambda_star_max-lambda_star_min)/(nlambda_star-1) ;
922 xsh_msg_dbg_low(
"Spectrum - Nlambda: %d, Min: %lf, Max: %lf, Step: %lf",
923 nlambda_spectrum, lambda_spectrum_min, lambda_spectrum_max,
927 XSH_CALLOC( lambda_resp,
double, nlambda_star ) ;
928 XSH_CALLOC( flux_resp,
double, nlambda_star ) ;
929 XSH_CALLOC( flux_added,
double, nlambda_star ) ;
938 XSH_CALLOC( lambda_spectrum,
double, nlambda_spectrum ) ;
942 for( lambda = lambda_spectrum_min, plambda = lambda_spectrum, i = 0 ;
944 i++, plambda++, lambda += step_spectrum )
954 npix_in_interval = step_star/step_spectrum ;
955 xsh_msg_dbg_low(
"Nb of pixels max in interval: %d", npix_in_interval ) ;
959 cpl_table* debug = NULL;
961 double* pflux_spectrum = NULL;
962 double* pflux_added = NULL;
963 double* pflux_star = NULL;
964 double* presp = NULL;
965 double* pkavalue = NULL;
966 double* presp_atm_corr = NULL;
967 double* presp_bin_cor = NULL;
969 debug=cpl_table_new(nlambda_star);
971 cpl_table_new_column(debug,
"wavelength", CPL_TYPE_DOUBLE);
972 cpl_table_new_column(debug,
"flux_spectrum", CPL_TYPE_DOUBLE);
973 cpl_table_new_column(debug,
"flux_added", CPL_TYPE_DOUBLE);
974 cpl_table_new_column(debug,
"flux_star", CPL_TYPE_DOUBLE);
975 cpl_table_new_column(debug,
"resp", CPL_TYPE_DOUBLE);
976 cpl_table_new_column(debug,
"kvalue", CPL_TYPE_DOUBLE);
977 cpl_table_new_column(debug,
"resp_atm_corr", CPL_TYPE_DOUBLE);
978 cpl_table_new_column(debug,
"resp_bin_corr", CPL_TYPE_DOUBLE);
980 cpl_table_fill_column_window_double(debug,
"wavelength", 0, nlambda_star, 0.);
981 cpl_table_fill_column_window_double(debug,
"flux_spectrum", 0, nlambda_star,0.);
982 cpl_table_fill_column_window_double(debug,
"flux_added", 0, nlambda_star, 0.);
983 cpl_table_fill_column_window_double(debug,
"flux_star", 0, nlambda_star, 0.);
984 cpl_table_fill_column_window_double(debug,
"resp", 0, nlambda_star, 0.);
985 cpl_table_fill_column_window_double(debug,
"kvalue", 0, nlambda_star, 0.);
986 cpl_table_fill_column_window_double(debug,
"resp_atm_corr", 0, nlambda_star,0.);
987 cpl_table_fill_column_window_double(debug,
"resp_bin_corr", 0, nlambda_star,0.);
989 pwav = cpl_table_get_data_double(debug,
"wavelength");
990 pflux_spectrum = cpl_table_get_data_double(debug,
"flux_spectrum");
991 pflux_added = cpl_table_get_data_double(debug,
"flux_added");
992 pflux_star = cpl_table_get_data_double(debug,
"flux_star");
993 presp = cpl_table_get_data_double(debug,
"resp");
994 pkavalue = cpl_table_get_data_double(debug,
"kvalue");
995 presp_atm_corr = cpl_table_get_data_double(debug,
"resp_atm_corr");
996 presp_bin_cor = cpl_table_get_data_double(debug,
"resp_bin_corr");
1001 for ( i = 0 ; i < nlambda_star ; i++ ) {
1004 flux_added[i] = 0. ;
1005 min = lambda_star[i] - step_star/2 ;
1006 max = lambda_star[i] + step_star/2 ;
1009 lambda_star[i], min,
max ) ;
1012 step_spectrum, &if0, &if1 ) ;
1015 npix_in_interval ) ;
1021 qual_spectrum,if0,if1,i,
1022 &flux_added,&npixels,&nbad);
1025 resp_list->
flux[i] = flux_star[i]/(flux_added[i]/cor_fct) ;
1026 resp_list->
lambda[i] = lambda_star[i] ;
1029 (*obj_list)->flux[i] = flux_added[i] ;
1030 (*obj_list)->lambda[i] = lambda_star[i] ;
1032 pwav[i]=resp_list->
lambda[i];
1033 pflux_spectrum[i]=flux_spectrum[i];
1034 pflux_added[i]=flux_added[i];
1035 pflux_star[i]=flux_star[i];
1036 presp[i]=resp_list->
flux[i];
1038 xsh_msg_dbg_low(
" Flux Star = %le, Integrated = %lf (%d pixels, %d bad)",
1039 flux_star[i], flux_added[i], npixels, nbad ) ;
1041 lambda_star[i], resp_list->
flux[i] ) ;
1048 if ( atmos_ext_tab != NULL ) {
1049 kvalue = pow(10., -airmass*atmos_K[i]*0.4 ) ;
1050 resp_list->
flux[i] *= kvalue ;
1051 presp_atm_corr[i]=resp_list->
flux[i];
1057 resp_list->
flux[i] *= step_star/step_spectrum ;
1058 presp_bin_cor[i]=resp_list->
flux[i];
1076 flux_spectrum,flux_added));
1090 if(cpl_error_get_code() == CPL_ERROR_NONE) {
1284 cpl_table * atmos_ext_tab,
1296 double * lambda_star = NULL;
1298 double lambda_star_min, lambda_star_max ;
1302 double * atmos_lambda = NULL ;
1303 double * atmos_K = NULL ;
1320 nlambda_star = star_list->
size ;
1321 lambda_star = star_list->
lambda ;
1323 lambda_star_min = *lambda_star ;
1324 lambda_star_max = *(lambda_star+nlambda_star-1) ;
1325 step_star = (lambda_star_max-lambda_star_min)/(nlambda_star-1) ;
1328 lambda_star_min, lambda_star_max, step_star ) ;
1331 &atmos_lambda,&atmos_K));
1334 atmos_ext_tab,the_arm,atmos_K,
1367 cpl_table* table=NULL;
1368 const char* name=NULL;
1369 cpl_propertylist* plist=NULL;
1377 name=cpl_frame_get_filename(result);
1378 plist=cpl_propertylist_load(name,0);
1379 table=cpl_table_load(name,1,0);
1380 nrow=cpl_table_get_nrow(table);
1382 check(cpl_table_name_column(table,
"FLUX",
"RESPONSE"));
1383 cpl_table_new_column(table,
"OBS",CPL_TYPE_DOUBLE);
1384 cpl_table_new_column(table,
"REF",CPL_TYPE_DOUBLE);
1385 cpl_table_new_column(table,
"REF_DIV_OBS",CPL_TYPE_DOUBLE);
1386 cpl_table_fill_column_window_double(table,
"OBS",0,nrow,0);
1387 cpl_table_fill_column_window_double(table,
"REF",0,nrow,0);
1388 cpl_table_fill_column_window_double(table,
"REF_DIV_OBS",0,nrow,0);
1390 pobj=cpl_table_get_data_double(table,
"OBS");
1391 pref=cpl_table_get_data_double(table,
"REF");
1392 pdiv=cpl_table_get_data_double(table,
"REF_DIV_OBS");
1394 for(i=0;i<nrow;i++) {
1395 pobj[i]=obj_list->
flux[i];
1396 pref[i]=star_list->
flux[i];
1397 pdiv[i]=pref[i]/pobj[i];
1400 cpl_table_save(table,plist,NULL,name,CPL_IO_DEFAULT);
1406 return cpl_error_get_code();
1436 const char* filename = NULL;
1437 cpl_propertylist* plist = NULL;
1442 check(filename = cpl_frame_get_filename(obs_std_star));
1443 check(plist = cpl_propertylist_load(filename, 0));
1459 cpl_table* atmos_ext_tab = NULL;
1461 if (atmos_ext != NULL) {
1463 filename=cpl_frame_get_filename(atmos_ext);
1464 atmos_ext_tab=cpl_table_load(filename,1,0);
1471 double * atmos_lambda = NULL ;
1472 double * atmos_K = NULL ;
1481 obs_std_spectrum,atmos_K,the_arm,
1482 airmass,
exptime,gain,&corr_obs_std_star_list));
1493 return corr_obs_std_star_list;
1593static cpl_error_code
1597 cpl_vector** vec_wave,
1598 cpl_vector** vec_flux)
1600 double* pwave_in=NULL;
1601 double* pflux_in=NULL;
1603 double* pwave_ou=NULL;
1604 double* pflux_ou=NULL;
1610 pwave_in=obs_std_star_list->
lambda;
1611 pflux_in=obs_std_star_list->
flux;
1614 *vec_wave=cpl_vector_new(
size);
1615 *vec_flux=cpl_vector_new(
size);
1616 pwave_ou=cpl_vector_get_data(*vec_wave);
1617 pflux_ou=cpl_vector_get_data(*vec_flux);
1619 for(i=0;i<
size;i++) {
1620 if(pwave_in[i]>=wmin && pwave_in[i]<= wmax) {
1621 pwave_ou[k]=pwave_in[i];
1622 pflux_ou[k]=pflux_in[i];
1626 cpl_vector_set_size(*vec_wave,k);
1627 cpl_vector_set_size(*vec_flux,k);
1629 return cpl_error_get_code();
1646 cpl_vector * biu1=NULL;
1647 cpl_vector * biu2=NULL;
1648 cpl_bivector * bi_all=NULL;
1651 return cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
1652 cpl_ensure_code(u1 != NULL, CPL_ERROR_NULL_INPUT);
1653 cpl_ensure_code(u2 != NULL, CPL_ERROR_NULL_INPUT);
1656 biu1=cpl_vector_wrap(
n,u1);
1657 biu2=cpl_vector_wrap(
n,u2);
1659 bi_all=cpl_bivector_wrap_vectors(biu1, biu2);
1660 cpl_bivector_sort(bi_all,bi_all, CPL_SORT_ASCENDING,CPL_SORT_BY_X);
1665 cpl_bivector_unwrap_vectors(bi_all);
1666 cpl_vector_unwrap(biu1);
1667 cpl_vector_unwrap(biu2);
1669 return CPL_ERROR_NONE;
1673static cpl_error_code
1674xsh_select_line_core(cpl_vector* wave,cpl_vector* flux,
const double wguess,
const double wrange,cpl_vector** xfit,cpl_vector** yfit)
1685 size=cpl_vector_get_size(wave);
1686 *xfit=cpl_vector_new(
size);
1687 *yfit=cpl_vector_new(
size);
1688 pxfit=cpl_vector_get_data(*xfit);
1689 pyfit=cpl_vector_get_data(*yfit);
1691 pwave=cpl_vector_get_data(wave);
1692 pflux=cpl_vector_get_data(flux);
1695 for(i=0;i<
size;i++) {
1696 if(pwave[i]>=(wguess-wrange) &&
1697 pwave[i]<=(wguess+wrange) ) {
1704 check(cpl_vector_set_size(*xfit,k));
1705 check(cpl_vector_set_size(*yfit,k));
1708 return cpl_error_get_code();
1711static cpl_error_code
1712xsh_line_balance(cpl_vector**
x,cpl_vector**
y,
const double ymax)
1721 px=cpl_vector_get_data(*
x);
1722 py=cpl_vector_get_data(*
y);
1724 size=cpl_vector_get_size(*
x);
1726 for(i=0;i<
size;i++) {
1734 cpl_vector_set_size(*
x,k);
1735 cpl_vector_set_size(*
y,k);
1738 return cpl_error_get_code();
1748 double wave_shift = 0;
1766 double hbox_wave=0.5;
1767 int hbox = (int)(hbox_wave/wstep+0.5);
1780 wmax = wmin + naxis1 * wstep;
1784 wmin=w->
wguess-hbox*wstep;
1785 wmax=w->
wguess+(hbox+1)*wstep;
1789 cpl_vector* vec_wave_obs = NULL;
1790 cpl_vector* vec_flux_obs = NULL;
1791 cpl_vector* vec_wave_ref=NULL;
1792 cpl_vector* vec_flux_ref=NULL;
1793 cpl_vector* vec_slope_obs=NULL;
1794 cpl_vector* vec_slope_ref=NULL;
1795 cpl_vector* correl=NULL;
1822 cpl_vector_divide(vec_flux_obs,vec_slope_obs);
1823 cpl_vector_divide(vec_flux_ref,vec_slope_ref);
1827 cpl_vector_add_scalar(vec_flux_obs,2);
1828 cpl_vector_add_scalar(vec_flux_ref,2);
1842 cpl_vector* xfit_obs=NULL;
1843 cpl_vector* yfit_obs=NULL;
1844 cpl_vector* xfit_ref=NULL;
1845 cpl_vector* yfit_ref=NULL;
1927 hbox=cpl_vector_get_size(vec_flux_ref);
1931 cpl_polynomial* poly_fit_obs=NULL;
1934 double* pindex=NULL;
1937 cpl_vector* yfit_pol_obs=NULL;
1938 cpl_vector* yfit_index=NULL;
1939 size_x_obs=cpl_vector_get_size(xfit_obs);
1940 yfit_pol_obs=cpl_vector_new(size_x_obs);
1941 yfit_index=cpl_vector_new(size_x_obs);
1942 check(pyfit=cpl_vector_get_data(yfit_pol_obs));
1943 check(pxfit=cpl_vector_get_data(xfit_obs));
1944 check(pindex=cpl_vector_get_data(yfit_index));
1946 for(i=0;i<size_x_obs;i++) {
1947 pyfit[i]=cpl_polynomial_eval_1d(poly_fit_obs,pxfit[i],NULL);
1959 xmin=pxfit[(int)pindex[0]];
1985 double peakpos_obs=xmin;
1986 double peakpos_ref=w->
wguess;
1987 xsh_msg(
"Pos guess: %g obs: %10.8g Pos ref: %10.8g diff=%10.8g",
1988 w->
wguess,peakpos_obs,peakpos_ref,peakpos_obs-peakpos_ref);
1991 xsh_msg(
"Now compute shift by spectra correlation");
2057 wave_shift=peakpos_obs-peakpos_ref;
2248 cpl_table* tab=NULL;
2249 cpl_table* ext_data=NULL;
2250 cpl_table* ext_fit=NULL;
2257 double* plambda=NULL;
2275 double * pflux=NULL;
2279 cpl_error_ensure(ref_std_star_list != NULL, CPL_ERROR_NULL_INPUT,
return res,
"NULL input reference std star list");
2280 cpl_error_ensure(resp_fit_points != NULL, CPL_ERROR_NULL_INPUT,
return res,
"NULL input table with response fit points");
2281 cpl_error_ensure(inst != NULL, CPL_ERROR_NULL_INPUT,
return res,
"NULL input instrument");
2285 plambda=cpl_table_get_data_double(resp_fit_points,
"LAMBDA");
2286 int resp_fit_points_n=cpl_table_get_nrow(resp_fit_points);
2295 size_data = ref_std_star_list->
size ;
2300 tab=cpl_table_new(size_data);
2301 cpl_table_wrap_double(tab,wave,
"wave");
2302 cpl_table_wrap_double(tab,flux,
"response");
2303 check(wmin_data=cpl_table_get_column_min(tab,
"wave"));
2304 check(wmax_data=cpl_table_get_column_max(tab,
"wave"));
2305 xsh_msg(
"wmin_data=%g wmax_data=%g",wmin_data,wmax_data);
2306 size_fit=cpl_table_get_nrow(resp_fit_points);
2309 pflux=cpl_table_get_data_double(tab,
"response");
2311 for(i=0;i<size_data;i++) {
2312 if(isnan(pflux[i])) {
2313 cpl_table_set_invalid(tab,
"response",i);
2315 if(isinf(pflux[i])) {
2317 cpl_table_set_invalid(tab,
"response",i);
2322 if (phigh != NULL) {
2323 xsh_msg(
"Flag High Absorption Regions" );
2326 for (i = 0; i < size_data; i++) {
2328 (wave[i] >= wmin_data && wave[i] <= wmax_data) ){
2329 cpl_table_set_invalid(tab,
"wave", i);
2336 check(wmin_fit=cpl_table_get_column_min(resp_fit_points,
"LAMBDA"));
2337 check(wmax_fit=cpl_table_get_column_max(resp_fit_points,
"LAMBDA"));
2339 cpl_table_and_selected_double(resp_fit_points,
"LAMBDA",CPL_NOT_LESS_THAN,wmin_data);
2340 cpl_table_and_selected_double(resp_fit_points,
"LAMBDA",CPL_NOT_GREATER_THAN,wmax_data);
2341 ext_fit=cpl_table_extract_selected(resp_fit_points);
2343 check(wmin_fit=cpl_table_get_column_min(ext_fit,
"LAMBDA"));
2344 check(wmax_fit=cpl_table_get_column_max(ext_fit,
"LAMBDA"));
2345 xsh_msg(
"wmin_fit=%g wmax_fit=%g",wmin_fit,wmax_fit);
2347 size_fit=cpl_table_get_nrow(ext_fit);
2350 if(wmin_fit>wmin_data) {
2356 if(wmax_fit<wmax_data) {
2361 xsh_msg(
"size_fit=%d",size_fit);
2363 xsh_msg(
"imin=%d imax=%d",imin,imax);
2367 if(wmin_fit>wmin_data) {
2368 xsh_msg(
"wmin_fit>wmin_data");
2370 nsel=cpl_table_and_selected_double(tab,
"wave",CPL_NOT_GREATER_THAN,wmin_data+wrange);
2372 xsh_msg(
"Problem to get spline fit sampling points");
2373 xsh_msg(
"Possibly XSH_HIGH_ABS_WIN table wave ranges incompatible with RESP_FIT_POINTS_CAT table");
2375 ext_data=cpl_table_extract_selected(tab);
2376 cpl_table_select_all(tab);
2377 if( cpl_table_has_valid(ext_data,
"response") > 0 ) {
2378 check(med=cpl_table_get_column_median(ext_data,
"response"));
2398 imax = (imax <= resp_fit_points_n-1) ? imax : resp_fit_points_n-1;
2399 for(i=imin;i<=imax;i++) {
2400 check(cpl_table_and_selected_double(tab,
"wave",CPL_NOT_LESS_THAN,plambda[i]-wrange));
2401 check(nsel=cpl_table_and_selected_double(tab,
"wave",CPL_NOT_GREATER_THAN,plambda[i]+wrange));
2402 check(n_invalid=cpl_table_count_invalid(tab,
"wave"));
2404 n_invalid=cpl_table_count_invalid(tab,
"response");
2409 check(ext_data=cpl_table_extract_selected(tab));
2410 nsel=cpl_table_get_nrow(ext_data);
2411 n_invalid=cpl_table_count_invalid(ext_data,
"response");
2413 if(nsel>0 && n_invalid < nsel) {
2425 check(med=cpl_table_get_column_median(ext_data,
"response"));
2435 check(cpl_table_select_all(tab));
2441 cpl_table_select_all(tab);
2443 if(wmax_fit<wmax_data) {
2446 cpl_table_and_selected_double(tab,
"wave",CPL_NOT_LESS_THAN,wmax_data-wrange);
2447 ext_data=cpl_table_extract_selected(tab);
2448 cpl_table_select_all(tab);
2449 if( cpl_table_has_valid(ext_data,
"response") > 0 ) {
2450 check(med=cpl_table_get_column_median(ext_data,
"response"));
2454 xfit[size_fit-1]=wmax_data;
2456 yfit[size_fit-1]=med;
2473 cpl_free(res->
flux);
2482 cpl_table_unwrap(tab,
"wave");
2483 cpl_table_unwrap(tab,
"response");
2497 gsl_bspline_workspace *bw;
2503 gsl_matrix *X, *cov;
2504 gsl_multifit_linear_workspace *mw;
2505 double chisq=0, Rsq=0, dof=0;
2510 double* wave = NULL;
2511 double* response = NULL;
2512 double* pwav = NULL;
2513 double* pfit = NULL;
2515 double dump_factor = 1.e10;
2517 cpl_table* tab_resp_fit = NULL;
2528 nbreak = ncoeffs + 2 -
order;
2530 n = ref_std_star_list->
size ;
2531 wave = ref_std_star_list->
lambda ;
2532 response = ref_std_star_list->
flux ;
2537 gsl_rng_env_setup();
2538 r = gsl_rng_alloc(gsl_rng_default);
2541 bw = gsl_bspline_alloc(
order, nbreak);
2542 B = gsl_vector_alloc(ncoeffs);
2544 x = gsl_vector_alloc(
n);
2545 y = gsl_vector_alloc(
n);
2546 X = gsl_matrix_alloc(
n, ncoeffs);
2547 c = gsl_vector_alloc(ncoeffs);
2548 w = gsl_vector_alloc(
n);
2549 cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
2550 mw = gsl_multifit_linear_alloc(
n, ncoeffs);
2554 for (i = 0; i <
n; ++i) {
2556 double xi = (double) wave[i];
2557 double yi = (double) response[i];
2560 dy = gsl_ran_gaussian(r,
sigma);
2563 gsl_vector_set(
x, i, xi);
2565 if(isnan(yi) || isinf(yi)) {
2566 gsl_vector_set(
y, i, 0);
2567 gsl_vector_set(w, i, 1.0 / dump_factor);
2570 gsl_vector_set(
y, i, yi);
2577 if (phigh != NULL) {
2579 xsh_msg(
"Flag High Absorption Regions" );
2582 for (i = 0; i <
n; i++) {
2585 gsl_vector_set(w, i, 1.0 / dump_factor);
2593 gsl_bspline_knots_uniform(wave[0], wave[
n - 1], bw);
2596 for (i = 0; i <
n; ++i) {
2597 double xi = gsl_vector_get(
x, i);
2600 gsl_bspline_eval(xi, B, bw);
2603 for (j = 0; j < ncoeffs; ++j) {
2604 double Bj = gsl_vector_get(B, j);
2605 gsl_matrix_set(X, i, j, Bj);
2610 gsl_multifit_wlinear(X, w,
y, c, cov, &chisq, mw);
2617 printf(
"chisq/dof = %e, Rsq = %f\n", chisq / dof, Rsq);
2619 tab_resp_fit = cpl_table_new(
n);
2620 cpl_table_new_column(tab_resp_fit,
"wave", CPL_TYPE_DOUBLE);
2621 cpl_table_new_column(tab_resp_fit,
"fit", CPL_TYPE_DOUBLE);
2622 cpl_table_fill_column_window_double(tab_resp_fit,
"wave", 0,
n, 0);
2623 cpl_table_fill_column_window_double(tab_resp_fit,
"fit", 0,
n, 0);
2624 pwav = cpl_table_get_data_double(tab_resp_fit,
"wave");
2625 pfit = cpl_table_get_data_double(tab_resp_fit,
"fit");
2629 double xi, yi, yerr;
2631 for (i = 0; i <
n; i++) {
2632 xi = (double) wave[i];
2633 gsl_bspline_eval(xi, B, bw);
2634 gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
2647 gsl_bspline_free(bw);
2654 gsl_matrix_free(cov);
2655 gsl_multifit_linear_free(mw);
2666 double* w_pos,
const int n_pos)
2668 double * result=NULL;
2672 gsl_interp_accel *acc = gsl_interp_accel_alloc ();
2674 xsh_msg(
"w_pos[0]=%g w_data[0]=%g",w_pos[0],w_data[0]);
2675 xsh_msg(
"w_pos[n_pos-1]=%g w_data[n_data-1]=%g",w_pos[n_pos-1],w_data[n_data-1]);
2676 cpl_ensure(w_pos[0] >= w_data[0], CPL_ERROR_ILLEGAL_INPUT,NULL);
2677 cpl_ensure(w_pos[n_pos-1] <= w_data[n_data-1], CPL_ERROR_ILLEGAL_INPUT,NULL);
2679 gsl_spline *spline = gsl_spline_alloc (gsl_interp_cspline, n_data);
2681 gsl_spline_init (spline, w_data, f_data, n_data);
2684 result=cpl_calloc(n_pos,
sizeof(
double));
2686 if(w_pos[0] == w_data[0]) {
2687 result[0] = f_data[0];
2691 if(w_pos[n_pos-1] == w_data[n_data-1]) {
2692 result[n_pos-1] = f_data[n_data-1];
2696 for (i = i_min; i < i_max; i ++) {
2698 yi = gsl_spline_eval (spline, xi, acc);
2703 gsl_spline_free (spline);
2704 gsl_interp_accel_free (acc);
2718 double * result=NULL;
2725 gsl_bspline_workspace *bw;
2731 gsl_matrix *X, *cov;
2732 gsl_multifit_linear_workspace *mw;
2733 double chisq=0, Rsq=0, dof=0;
2740 double* pwav = NULL;
2741 double* pfit = NULL;
2743 double dump_factor_in = 1.e10;
2744 double dump_factor_ou = 1.e10;
2746 cpl_table* tab_resp_fit = NULL;
2748 double dump_factor=1.e10;
2751 dump_factor_in = 1.;
2752 dump_factor_ou = 1.e10;
2754 dump_factor_in = 1.e10;
2765 nbreak = ncoeffs + 2 -
order;
2774 gsl_rng_env_setup();
2775 r = gsl_rng_alloc(gsl_rng_default);
2778 bw = gsl_bspline_alloc(
order, nbreak);
2779 B = gsl_vector_alloc(ncoeffs);
2781 x = gsl_vector_alloc(
n);
2782 y = gsl_vector_alloc(
n);
2783 X = gsl_matrix_alloc(
n, ncoeffs);
2784 c = gsl_vector_alloc(ncoeffs);
2785 w = gsl_vector_alloc(
n);
2786 cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
2787 mw = gsl_multifit_linear_alloc(
n, ncoeffs);
2791 for (i = 0; i <
n; ++i) {
2793 double xi = wave[i];
2794 double yi = flux[i];
2797 dy = gsl_ran_gaussian(r,
sigma);
2800 gsl_vector_set(
x, i, xi);
2802 if(isnan(yi) || isinf(yi)) {
2803 gsl_vector_set(
y, i, 0);
2804 gsl_vector_set(w, i, 1.0 / dump_factor);
2807 gsl_vector_set(
y, i, yi);
2814 printf(
"Dump factor in %g out %g\n", dump_factor_in, dump_factor_ou);
2815 if (phigh != NULL) {
2817 xsh_msg(
"Flag High Absorption Regions" );
2820 for (i = 0; i <
n; i++) {
2824 gsl_vector_set(w, i, gsl_vector_get(w, i) / dump_factor_in);
2828 gsl_vector_set(w, i, gsl_vector_get(w, i) / dump_factor_ou);
2836 gsl_bspline_knots_uniform(wave[0], wave[
n - 1], bw);
2839 for (i = 0; i <
n; ++i) {
2840 double xi = gsl_vector_get(
x, i);
2843 gsl_bspline_eval(xi, B, bw);
2846 for (j = 0; j < ncoeffs; ++j) {
2847 double Bj = gsl_vector_get(B, j);
2848 gsl_matrix_set(X, i, j, Bj);
2853 gsl_multifit_wlinear(X, w,
y, c, cov, &chisq, mw);
2860 printf(
"chisq/dof = %e, Rsq = %f\n", chisq / dof, Rsq);
2862 tab_resp_fit = cpl_table_new(
n);
2863 cpl_table_new_column(tab_resp_fit,
"wave", CPL_TYPE_DOUBLE);
2864 cpl_table_new_column(tab_resp_fit,
"fit", CPL_TYPE_DOUBLE);
2865 cpl_table_fill_column_window_double(tab_resp_fit,
"wave", 0,
n, 0);
2866 cpl_table_fill_column_window_double(tab_resp_fit,
"fit", 0,
n, 0);
2867 pwav = cpl_table_get_data_double(tab_resp_fit,
"wave");
2868 pfit = cpl_table_get_data_double(tab_resp_fit,
"fit");
2869 result=cpl_calloc(
n,
sizeof(
double));
2872 double xi, yi, yerr;
2874 for (i = 0; i <
n; i++) {
2875 xi = (double) wave[i];
2876 gsl_bspline_eval(xi, B, bw);
2877 gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
2891 gsl_bspline_free(bw);
2898 gsl_matrix_free(cov);
2899 gsl_multifit_linear_free(mw);
2914 double * result=NULL;
2921 gsl_bspline_workspace *bw;
2928 gsl_matrix *X, *cov;
2929 gsl_multifit_linear_workspace *mw;
2930 double chisq=0, Rsq=0, dof=0;
2937 double* pwav = NULL;
2938 double* pfit = NULL;
2943 cpl_table* tab_resp_fit = NULL;
2945 double dump_factor=1.e10;
2956 xsh_msg(
"instrument arm not set");
2959 nbreak = ncoeffs + 2 -
order;
2967 gsl_rng_env_setup();
2968 r = gsl_rng_alloc(gsl_rng_default);
2971 bw = gsl_bspline_alloc(
order, nbreak);
2973 B = gsl_vector_alloc(ncoeffs);
2975 Bkpts = gsl_vector_alloc(nbreak);
2978 x = gsl_vector_alloc(
n);
2979 y = gsl_vector_alloc(
n);
2980 X = gsl_matrix_alloc(
n, ncoeffs);
2981 c = gsl_vector_alloc(ncoeffs);
2982 w = gsl_vector_alloc(
n);
2984 cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
2986 mw = gsl_multifit_linear_alloc(
n, ncoeffs);
2991 for (i = 0; i <
n; ++i) {
2993 double xi = wave[i];
2994 double yi = flux[i];
2998 dy = gsl_ran_gaussian(r,
sigma);
3001 gsl_vector_set(
x, i, xi);
3002 if(isnan(yi) || isinf(yi)) {
3003 gsl_vector_set(
y, i, 0);
3004 gsl_vector_set(w, i, 1.0 / dump_factor);
3007 gsl_vector_set(
y, i, yi);
3042 Bkpts_ptr=gsl_vector_ptr(Bkpts,0);
3043 int nfit_div_nbreak=
n/nbreak;
3046 for(i=0;i<nbreak;i++) {
3047 Bkpts_ptr[i]=wave[i*nfit_div_nbreak];
3053 Bkpts_ptr[0]=wave[0];
3054 Bkpts_ptr[nbreak-1]=wave[
n-1];
3055 gsl_bspline_knots(Bkpts, bw);
3059 for (i = 0; i <
n; ++i) {
3060 double xi = gsl_vector_get(
x, i);
3063 gsl_bspline_eval(xi, B, bw);
3066 for (j = 0; j < ncoeffs; ++j) {
3067 double Bj = gsl_vector_get(B, j);
3068 gsl_matrix_set(X, i, j, Bj);
3074 gsl_multifit_wlinear(X, w,
y, c, cov, &chisq, mw);
3081 printf(
"chisq/dof = %e, Rsq = %f\n", chisq / dof, Rsq);
3083 tab_resp_fit = cpl_table_new(
n);
3084 cpl_table_new_column(tab_resp_fit,
"wave", CPL_TYPE_DOUBLE);
3085 cpl_table_new_column(tab_resp_fit,
"fit", CPL_TYPE_DOUBLE);
3086 cpl_table_fill_column_window_double(tab_resp_fit,
"wave", 0,
n, 0);
3087 cpl_table_fill_column_window_double(tab_resp_fit,
"fit", 0,
n, 0);
3088 pwav = cpl_table_get_data_double(tab_resp_fit,
"wave");
3089 pfit = cpl_table_get_data_double(tab_resp_fit,
"fit");
3090 result=cpl_calloc(
n,
sizeof(
double));
3093 double xi, yi, yerr;
3095 for (i = 0; i <
n; i++) {
3096 xi = (double) wave[i];
3097 gsl_bspline_eval(xi, B, bw);
3098 gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
3113 gsl_bspline_free(bw);
3115 gsl_vector_free(Bkpts);
3121 gsl_matrix_free(cov);
3122 gsl_multifit_linear_free(mw);
3146 cpl_frame * flux_std_star_cat,
3147 cpl_frame * atmos_ext,
3148 cpl_frame* high_abs,
3149 cpl_frame* resp_fit_points,
3150 cpl_frame* tell_mod_cat,
3153 const int tell_corr )
3156 cpl_frame* result = NULL;
3171 const char* filename = NULL;
3172 cpl_propertylist* plist = NULL;
3174 cpl_frame* ipol_ref_std_star_frame=NULL;
3176 cpl_frame* frm_tmp=NULL;
3179 cpl_table* tbl_shift_std_spectrum=NULL;
3180 cpl_frame* shift_std_star_frame=NULL;
3181 const char* tbl_shift_std_fname=
"shift_flux_std_star.fits";
3183 cpl_frame* ipol_shift_std_star_frame=NULL;
3189 char resp_obs_std_star_fname[256];
3190 cpl_frame* obs_std_star_resp=NULL;
3191 cpl_table* resp_fit_points_tbl=NULL;
3192 cpl_table* tbl_ref_std_spectrum = NULL;
3196 filename = cpl_frame_get_filename(obs_std_star);
3197 plist = cpl_propertylist_load(filename, 0);
3206 filename=cpl_frame_get_filename(obs_std_star);
3208 obs_std_star_resp=cpl_frame_duplicate(obs_std_star);
3212 check(filename=cpl_frame_get_filename(flux_std_star_cat));
3213 plist=cpl_propertylist_load(filename,0);
3214 is_complete=cpl_propertylist_has(plist,
"WRANGE");
3218 wmax = wmin + cdelt1 * naxis1;
3231 "Problem parsing input STD catalog. For robustness recipe goes on.");
3239 cpl_frame* ref_std_star_frame=NULL;
3240 const char* tbl_ref_std_fname=
"ref_flux_std_star.fits";
3241 check(cpl_table_save(tbl_ref_std_spectrum,NULL,NULL,tbl_ref_std_fname,
3246 CPL_FRAME_TYPE_TABLE,
3247 CPL_FRAME_GROUP_PRODUCT,
3248 CPL_FRAME_LEVEL_INTERMEDIATE));
3272 xsh_msg(
"tell_corr=%d tell_mod_cal=%p",tell_corr,tell_mod_cat);
3274 xsh_msg(
"Telluric correction");
3275 cpl_table* tab_res=NULL;
3277 cpl_size model_idx=0;
3299 check(pwav = cpl_table_get_data_double(tab_res,
"wavelength"));
3301 check(pflx = cpl_table_get_data_double(tab_res,
"ratio"));
3306 for(i=0;i<obs_std_star_list->
size;i++) {
3307 obs_std_star_list->
lambda[i]=pwav[i];
3308 obs_std_star_list->
flux[i]=pflx[i];
3353 double wave_shift=0;
3358 double offset=(wave_shift)/rv_ref_wave->
wguess;
3359 xsh_msg(
"wave: guess[nm] %g RV shift[nm] %g offset[unitless]=%g",rv_ref_wave->
wguess,wave_shift,offset);
3365 check(cpl_table_save(tbl_shift_std_spectrum,NULL,NULL,tbl_shift_std_fname,
3372 CPL_FRAME_TYPE_TABLE,
3373 CPL_FRAME_GROUP_PRODUCT,
3374 CPL_FRAME_LEVEL_INTERMEDIATE));
3445 if(resp_fit_points!=NULL) {
3449 &resp_fit_points_tbl, &std_star_id)) {
3451 "Problem parsing input STD catalog. For robustness recipe goes on.");
3457 xsh_msg(
"Determine response by cubic spline fit interpolation of points defined in input RESP_FIT_POINTS_CAT_%s",
3462 xsh_msg(
"Determine response by cubic spline smooth interpolation of pipeline defined regions");
3484 sprintf(fname,
"%s.fits",tag);
3524 cpl_frame * flux_std_star_cat_frame,
3525 cpl_frame * atmos_ext_frame,
3526 cpl_frame* high_abs_frame,
3530 cpl_frame * result = NULL ;
3534 cpl_frame * tmp_spectrum_frame = NULL ;
3540 cpl_frame* flux_std_star_frame=NULL;
3543 cpl_table* tbl_ref_std_spectrum=NULL;
3544 const char* tbl_ref_std_fname=
"ref_flux_std_star.fits";
3545 cpl_frame* ipol_flux_std_star_frame=NULL;
3546 cpl_frame* resampl_flux_std_star_frame=NULL;
3553 const char* filename=NULL;
3554 cpl_propertylist* plist=NULL;
3557 cpl_table* atmos_ext_tab=NULL;
3568 xsh_msg(
"Compute instrument response");
3578 &tbl_ref_std_spectrum,&std_star_id)){
3579 xsh_msg_warning(
"Problem parsing input STD catalog. For robustness recipe goes on.");
3587 check(cpl_table_save(tbl_ref_std_spectrum,NULL,NULL,tbl_ref_std_fname,
3591 CPL_FRAME_TYPE_TABLE,
3592 CPL_FRAME_GROUP_PRODUCT,
3593 CPL_FRAME_LEVEL_INTERMEDIATE));
3595 filename=cpl_frame_get_filename(spectrum_frame);
3596 plist=cpl_propertylist_load(filename,0);
3608 "Could not read gain factor");
3613 wmax=wmin+cdelt1*naxis1;
3637 name=cpl_sprintf(
"%s.fits",pcatg);
3644 if ( atmos_ext_frame != NULL ) {
3646 xsh_msg(
"ATMOS EXT Loaded" ) ;
3648 check(filename=cpl_frame_get_filename(atmos_ext_frame));
3649 check(atmos_ext_tab=cpl_table_load(filename,1,0));
3651 xsh_msg_warning(
"Missing input %s_%s frame. Skip response and efficiency computation",
3658 check( resp_list =
do_compute( star_list, &obj_list, spectrum, atmos_ext_tab,
3666 sprintf(fname,
"%s.fits",tag);
3668 xsh_msg(
"***********************************");
3672 if(high_abs_frame!=NULL) {
3710 cpl_frame * flux_std_star_cat_frame,
3711 cpl_frame * atmos_ext_frame,
3712 cpl_frame * high_abs_win_frame,
3716 cpl_frame * result = NULL ;
3720 cpl_frame * tmp_spectrum_frame = NULL ;
3726 cpl_frame* flux_std_star_frame=NULL;
3729 cpl_table* tbl_ref_std_spectrum=NULL;
3730 const char* tbl_ref_std_fname=
"ref_flux_std_star.fits";
3731 cpl_frame* ipol_flux_std_star_frame=NULL;
3736 const char* filename=NULL;
3737 cpl_propertylist* plist=NULL;
3740 cpl_table* atmos_ext_tab=NULL;
3757 xsh_msg(
"Compute instrument response");
3765 &tbl_ref_std_spectrum,&std_star_id)){
3766 xsh_msg_warning(
"Problem parsing input STD catalog. For robustness recipe goes on.");
3774 check(cpl_table_save(tbl_ref_std_spectrum,NULL,NULL,tbl_ref_std_fname,
3779 CPL_FRAME_TYPE_TABLE,
3780 CPL_FRAME_GROUP_PRODUCT,
3781 CPL_FRAME_LEVEL_INTERMEDIATE));
3784 if ( atmos_ext_frame != NULL ) {
3786 xsh_msg(
"ATMOS EXT Loaded" ) ;
3788 check(filename=cpl_frame_get_filename(atmos_ext_frame));
3789 check(atmos_ext_tab=cpl_table_load(filename,1,0));
3794 filename=cpl_frame_get_filename(spectrum_frame);
3795 next=cpl_frame_get_nextensions(spectrum_frame);
3798 plist=cpl_propertylist_load(filename,0);
3805 "Could not read gain factor");
3812 for(ext=0;ext<next;ext+=3) {
3815 plist=cpl_propertylist_load(filename,ext);
3819 wmax=wmin+cdelt1*naxis1;
3832 name=cpl_sprintf(
"%s.fits",pcatg);
3844 check(resp_list=
do_compute( star_list, &obj_list, spectrum, atmos_ext_tab,
3853 sprintf(fname,
"%s.fits",tag);
3862 if(high_abs_win_frame!=NULL) {
static xsh_instrument * instrument
double xsh_spectrum_get_lambda_min(xsh_spectrum *s)
Get minimum lambda of spectrum.
xsh_spectrum * xsh_spectrum_load(cpl_frame *s1d_frame)
Load a 1D spectrum structure.
double xsh_spectrum_get_lambda_step(xsh_spectrum *s)
Get bin in lambda of spectrum.
xsh_spectrum * xsh_spectrum_load_order(cpl_frame *s1d_frame, xsh_instrument *instr, const int order)
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
cpl_frame * xsh_spectrum_save_order(xsh_spectrum *s, const char *filename, const char *tag, const int order)
save a spectrum
double * xsh_spectrum_get_flux(xsh_spectrum *s)
Get flux of spectrum.
double xsh_spectrum_get_lambda_max(xsh_spectrum *s)
Get maximum lambda of spectrum.
void xsh_spectrum_free(xsh_spectrum **s)
free memory associated to an 1D spectrum
#define XSH_ASSURE_NOT_NULL_MSG(pointer, msg)
#define check_msg(COMMAND,...)
#define XSH_ASSURE_NOT_NULL(pointer)
const char * xsh_instrument_mode_tostring(xsh_instrument *i)
Get the string associated with a mode.
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
#define xsh_msg_warning(...)
Print an warning message.
#define xsh_msg_dbg_medium(...)
#define xsh_msg_debug(...)
Print a debug message.
#define xsh_msg(...)
Print a message on info level.
#define xsh_msg_dbg_low(...)
#define xsh_msg_dbg_high(...)
int xsh_pfits_get_binx(const cpl_propertylist *plist)
find out the BINX value
double xsh_pfits_get_gain(const cpl_propertylist *plist)
find out the GAIN value
double xsh_pfits_get_cdelt1(const cpl_propertylist *plist)
find out the cdelt1
int xsh_pfits_get_biny(const cpl_propertylist *plist)
find out the BINY value
double xsh_pfits_get_exptime(const cpl_propertylist *plist)
find out the exposure time
double xsh_pfits_get_airm_mean(const cpl_propertylist *plist)
find out the mean airmass value
int xsh_pfits_get_naxis1(const cpl_propertylist *plist)
find out the NAXIS1 value
double xsh_pfits_get_crval1(const cpl_propertylist *plist)
find out the crval1
void xsh_free_polynomial(cpl_polynomial **p)
Deallocate a polynomial and set the pointer to NULL.
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
int xsh_debug_level_get(void)
get debug level
cpl_frame * xsh_spectrum_interpolate_linear(cpl_frame *table_frame, const double wstep, const double wmin, const double wmax)
spectra interpolation
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
double xsh_data_interpolate(double wav, int nrow, double *pw, double *pe)
Interpolate data points.
char * xsh_get_basename(const char *filename)
Return base filename.
cpl_table * xsh_table_shift_rv(cpl_table *orig, const char *col_wave, const double offset)
cpl_frame * xsh_spectrum_resample(cpl_frame *frame_inp, const double wstep, const double wmin, const double wmax, xsh_instrument *instr)
resample a spectrum
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
cpl_frame * xsh_spectrum_interpolate(cpl_frame *table_frame, const double wstep, const double wmin, const double wmax)
spectra interpolation
void xsh_frame_spectrum_save(cpl_frame *frm, const char *name_o)
save an spectrum frame
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
cpl_propertylist * flux_header
cpl_propertylist * header
double * xsh_bspline_fit_smooth_data(double *wave, double *flux, const int size, HIGH_ABS_REGION *phigh, xsh_instrument *inst, const int fit_region)
static cpl_error_code xsh_response_crea_ascii(xsh_star_flux_list *resp_list, xsh_star_flux_list *star_list, double *lambda_spectrum, double *flux_spectrum, double *flux_added)
static xsh_star_flux_list * xsh_response_calculate(xsh_star_flux_list *star_list, xsh_star_flux_list **obj_list, xsh_spectrum *spectrum, cpl_table *atmos_ext_tab, XSH_ARM the_arm, double *atmos_K, double airmass, double exptime, double gain)
static cpl_error_code xsh_std_star_spectra_to_vector_range(xsh_star_flux_list *obs_std_star_list, const double wmin, const double wmax, cpl_vector **vec_wave, cpl_vector **vec_flux)
#define FILTER_MEDIAN_HSIZE
static void find_lambda_idx_limit(double min, double max, double *spectrum, int from, int to, double step, int *if0, int *if1)
static xsh_star_flux_list * xsh_bspline_fit_smooth(xsh_star_flux_list *ref_std_star_list, HIGH_ABS_REGION *phigh, xsh_instrument *inst)
cpl_frame * xsh_compute_response(cpl_frame *spectrum_frame, cpl_frame *flux_std_star_cat_frame, cpl_frame *atmos_ext_frame, cpl_frame *high_abs_frame, xsh_instrument *instrument, double exptime)
cpl_error_code xsh_response_merge_obj_std_info(cpl_frame *result, xsh_star_flux_list *star_list, xsh_star_flux_list *obj_list)
static cpl_error_code xsh_select_line_core(cpl_vector *wave, cpl_vector *flux, const double wguess, const double wrange, cpl_vector **xfit, cpl_vector **yfit)
double * xsh_bspline_fit_smooth_data2(double *wave, double *flux, const int size, HIGH_ABS_REGION *phigh, xsh_instrument *inst, const int fit_region)
cpl_frame * xsh_compute_response_ord(cpl_frame *spectrum_frame, cpl_frame *flux_std_star_cat_frame, cpl_frame *atmos_ext_frame, cpl_frame *high_abs_win_frame, xsh_instrument *instrument, double exptime)
static xsh_star_flux_list * do_compute(xsh_star_flux_list *star_list, xsh_star_flux_list **obj_list, xsh_spectrum *spectrum, cpl_table *atmos_ext_tab, HIGH_ABS_REGION *phigh, double airmass, double exptime, double gain, xsh_instrument *instrument)
static cpl_error_code xsh_spectrum_correct(xsh_star_flux_list *star_list, xsh_spectrum *spectrum, double *atmos_K, XSH_ARM the_arm, double airmass, double exptime, double gain, xsh_star_flux_list **obj_list)
static cpl_error_code xsh_flux_integrate_and_corr_for_badpix(int npix_in_interval, double *flux_spectrum, int *qual_spectrum, int if0, int if1, int i, double **flux_added, int *npixels, int *nbad)
static cpl_error_code xsh_interpolate_atm_ext(xsh_star_flux_list *star_list, cpl_table *atmos_ext_tab, xsh_instrument *instrument, double **atmos_lambda, double **atmos_K)
static xsh_star_flux_list * xsh_obs_std_correct(cpl_frame *obs_std_star, xsh_star_flux_list *ref_std_star_list, cpl_frame *atmos_ext, HIGH_ABS_REGION *phigh, xsh_instrument *instrument)
static xsh_star_flux_list * xsh_bspline_fit_interpol(xsh_star_flux_list *ref_std_star_list, cpl_table *resp_fit_points, HIGH_ABS_REGION *phigh, xsh_instrument *inst)
static cpl_error_code xsh_interpolate_high_abs_regions(xsh_star_flux_list *star_list, xsh_star_flux_list *resp_list, HIGH_ABS_REGION *phigh)
cpl_error_code xsh_sort_double_pairs(double *u1, double *u2, cpl_size n)
Sort an array u1 of doubles, and permute an array u2 in the same way as u1 is permuted.
cpl_frame * xsh_compute_response2(cpl_frame *obs_std_star, cpl_frame *flux_std_star_cat, cpl_frame *atmos_ext, cpl_frame *high_abs, cpl_frame *resp_fit_points, cpl_frame *tell_mod_cat, xsh_instrument *instrument, double exptime, const int tell_corr)
double * xsh_bspline_interpolate_data_at_pos(double *w_data, double *f_data, const int n_data, double *w_pos, const int n_pos)
static int find_lambda_idx(double lambda, double *wave, int from, int to, double step)
#define INTERPOL_WSTEP_NM
static double xsh_std_star_spectra_correlate(xsh_star_flux_list *obs_std_star_list, xsh_star_flux_list *ref_std_star_list, xsh_rv_ref_wave_param *w)
xsh_atmos_ext_list * xsh_atmos_ext_list_load(cpl_frame *ext_frame)
void xsh_atmos_ext_list_free(xsh_atmos_ext_list **list)
#define XSH_ATMOS_EXT_LIST_COLNAME_OLD
#define XSH_ATMOS_EXT_LIST_COLNAME_K
double * xsh_star_flux_list_get_lambda(xsh_star_flux_list *list)
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)
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)
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.
HIGH_ABS_REGION * xsh_fill_high_abs_regions(xsh_instrument *instrument, cpl_frame *high_abs_frame)
static const double STAR_MATCH_DEPSILON
#define XSH_FREE(POINTER)
#define XSH_CALLOC(POINTER, TYPE, SIZE)
cpl_error_code xsh_rv_ref_wave_init(xsh_std_star_id std_star_id, XSH_ARM arm, xsh_rv_ref_wave_param *w)
void xsh_rv_ref_wave_param_destroy(xsh_rv_ref_wave_param *p)
void xsh_frame_sci_get_ra_dec_airmass(cpl_frame *frm_sci, double *ra, double *dec, double *airmass)
get RA, DEC, airmass (mean) of a frame
xsh_rv_ref_wave_param * xsh_rv_ref_wave_param_create(void)
cpl_error_code xsh_parse_catalog_std_stars(cpl_frame *cat, double dRA, double dDEC, double EPSILON, cpl_table **pptable, xsh_std_star_id *std_star_id)
parse referece std stars catalog
cpl_vector * xsh_vector_fit_slope(cpl_vector *vec_wave, cpl_vector *vec_flux, const double wmin_max, const double wmax_min, const int degree)
cpl_polynomial * xsh_polynomial_fit_1d_create(const cpl_vector *x_pos, const cpl_vector *values, int degree, double *mse)