64 m=(pf1[1]-pf1[0])/(pw1[1]-pw1[0]);
65 fout=
m*(wout-wmin)+pf1[0];
66 }
else if ( wout > wmax ) {
68 m=(pf1[size_obs-1]-pf1[size_obs-2])/(pw1[size_obs-1]-pw1[size_obs-2]);
69 fout=
m*(wout-wmax)+pf1[size_obs-1];
73 for(i=0;i<size_obs;i++) {
91 cpl_table* tref,
const char* cwref,
const char* cfref)
105 check(size_inp=cpl_table_get_nrow(tinp));
106 check(size_ref=cpl_table_get_nrow(tref));
108 check(wmin=cpl_table_get_column_min(tinp,cwinp));
109 check(wmax=cpl_table_get_column_max(tinp,cwinp));
114 tres=cpl_table_duplicate(tref);
115 check(pwinp=cpl_table_get_data_double(tinp,cwinp));
116 check(pwref=cpl_table_get_data_double(tref,cwref));
118 check(pfinp=cpl_table_get_data_double(tinp,cfinp));
119 check(pfres=cpl_table_get_data_double(tres,cfref));
121 for(i=0;i<size_ref;i++) {
135 cpl_table* tref,
const char* cwref,
const char* cfref)
138 cpl_table* tres=NULL;
153 check(size_inp=cpl_table_get_nrow(tinp));
154 check(size_ref=cpl_table_get_nrow(tref));
156 check(wmin=cpl_table_get_column_min(tinp,cwinp));
157 check(wmax=cpl_table_get_column_max(tinp,cwinp));
161 tres=cpl_table_new(size_ref);
163 cpl_table_new_column(tres,cwref,CPL_TYPE_DOUBLE);
164 cpl_table_new_column(tres,cfref,CPL_TYPE_DOUBLE);
165 cpl_table_fill_column_window_double(tres,cwref,0,size_ref,0.);
166 cpl_table_fill_column_window_double(tres,cfref,0,size_ref,0.);
169 check(pwinp=cpl_table_get_data_double(tinp,cwinp));
170 check(pwref=cpl_table_get_data_double(tref,cwref));
172 check(pfinp=cpl_table_get_data_double(tinp,cfinp));
173 check(pfres=cpl_table_get_data_double(tres,cfref));
175 check(pwres=cpl_table_get_data_double(tres,cwref));
176 check(pfres=cpl_table_get_data_double(tres,cfref));
178 for(i=0;i<size_ref;i++) {
193 cpl_table* tres=NULL;
203 check(size_inp=cpl_table_get_nrow(tinp));
205 check(wmin=cpl_table_get_column_min(tinp,cwinp));
206 check(wmax=cpl_table_get_column_max(tinp,cwinp));
208 size=(int)((wmax-wmin)/wstp+0.5);
212 tres=cpl_table_new(
size);
213 cpl_table_new_column(tres,cwinp,CPL_TYPE_DOUBLE);
214 cpl_table_new_column(tres,cfinp,CPL_TYPE_DOUBLE);
216 cpl_table_fill_column_window_double(tres,cwinp,0,
size,0.);
217 cpl_table_fill_column_window_double(tres,cfinp,0,
size,0.);
218 check(pwref=cpl_table_get_data_double(tres,cwinp));
219 for(i=0;i<
size;i++) {
220 pwref[i]=wmin+i*wstp;
222 check(pwinp=cpl_table_get_data_double(tinp,cwinp));
223 check(pfinp=cpl_table_get_data_double(tinp,cfinp));
224 check(pfres=cpl_table_get_data_double(tres,cfinp));
226 for(i=0;i<
size;i++) {
252 cpl_table* table_s=NULL;
275 table_s=cpl_table_new(
size);
276 cpl_table_new_column(table_s,
"wavelength",CPL_TYPE_DOUBLE);
277 cpl_table_fill_column_window_double(table_s,
"wavelength",0,
size,0);
278 pwave=cpl_table_get_data_double(table_s,
"wavelength");
280 cpl_table_new_column(table_s,
"flux",CPL_TYPE_DOUBLE);
281 cpl_table_fill_column_window_double(table_s,
"flux",0,
size,0);
282 pflux=cpl_table_get_data_double(table_s,
"flux");
284 cpl_table_new_column(table_s,
"errs",CPL_TYPE_DOUBLE);
285 cpl_table_fill_column_window_double(table_s,
"errs",0,
size,0);
286 perrs=cpl_table_get_data_double(table_s,
"errs");
288 cpl_table_new_column(table_s,
"qual",CPL_TYPE_INT);
289 cpl_table_fill_column_window_int(table_s,
"qual",0,
size,0);
290 pqual=cpl_table_get_data_int(table_s,
"qual");
297 for(i=0;i<
size;i++) {
298 pwave[i]=wmin+i*wstp;
304 cpl_table_duplicate_column(table_s,
"logwave",table_s,
"wavelength");
305 cpl_table_logarithm_column(table_s,
"logwave",CPL_MATH_E);
316 cpl_table* tab_tmp = NULL;
317 cpl_table* tab_stack = NULL;
328 nrow = cpl_table_and_selected_double(corr,
"wave", CPL_NOT_LESS_THAN,
331 nrow = cpl_table_and_selected_double(corr,
"wave", CPL_LESS_THAN,
334 tab_tmp = cpl_table_extract_selected(corr);
335 cpl_table_select_all(corr);
338 tab_stack = cpl_table_duplicate(tab_tmp);
341 nrow = cpl_table_get_nrow(tab_stack);
343 cpl_table_insert(tab_stack, tab_tmp, nrow);
350 sprintf(fname,
"tab_corr_sampl.fits");
353 *mean = fabs( ( cpl_table_get_column_mean(tab_stack,
"correction") - 1. ) );
354 *rms = cpl_table_get_column_stdev(tab_stack,
"correction");
359 return cpl_error_get_code();
366 cpl_table* stack = NULL;
371 cpl_table* tab_tmp = NULL;
387 check(nrow = cpl_table_and_selected_double(table_in, cwav, CPL_NOT_LESS_THAN,
390 check(nrow = cpl_table_and_selected_double(table_in,cwav, CPL_LESS_THAN,
393 check(tab_tmp = cpl_table_extract_selected(table_in));
394 cpl_table_select_all(table_in);
397 stack = cpl_table_duplicate(tab_tmp);
400 nrow=cpl_table_get_nrow(stack);
402 cpl_table_insert(stack, tab_tmp, nrow);
526 double* pvec_wave = NULL;
527 double* pvec_flux = NULL;
530 cpl_table* tab_tmp=NULL;
539 wmin=cpl_table_get_column_min(table,cwav);
540 wmax=cpl_table_get_column_max(table,cwav);
542 *vec_wave = cpl_vector_new(nsamples+2);
543 *vec_flux = cpl_vector_new(nsamples+2);
560 pvec_wave = cpl_vector_get_data(*vec_wave);
561 pvec_flux = cpl_vector_get_data(*vec_flux);
565 cpl_table_and_selected_double(table, cwav, CPL_LESS_THAN,wmin+wstp);
566 tab_tmp = cpl_table_extract_selected(table);
568 pvec_flux[0] = cpl_table_get_column_median(tab_tmp, cratio);
569 cpl_table_select_all(table);
576 for (kh = 0; phigh->
lambda_min != 0.; phigh++) {
578 cpl_table_and_selected_double(table, cwav,
581 cpl_table_and_selected_double(table, cwav, CPL_LESS_THAN,
584 tab_tmp = cpl_table_extract_selected(table);
585 cpl_table_select_all(table);
588 pvec_flux[kh+1] = cpl_table_get_column_median(tab_tmp, cratio);
609 cpl_table_select_all(table);
611 cpl_table_and_selected_double(table, cwav, CPL_NOT_LESS_THAN,wmax-wstp);
612 tab_tmp = cpl_table_extract_selected(table);
614 pvec_wave[kh+1] = wmax;
615 pvec_flux[kh+1] = cpl_table_get_column_median(tab_tmp, cratio);
621 return cpl_error_get_code();
628 cpl_table* tab = NULL;
631 cpl_vector* wave=NULL;
632 cpl_vector* flux=NULL;
634 size_x=cpl_vector_get_size(wcorr);
635 tab = cpl_table_new(size_x);
636 cpl_table_wrap_double(tab, cpl_vector_get_data(wcorr),
"logwave");
637 cpl_table_wrap_double(tab, cpl_vector_get_data(fcorr),
"flux");
639 sprintf(fname,
"fcorr_org.fits");
641 cpl_table_and_selected_double(tab,
"logwave", CPL_GREATER_THAN,
642 (*gfit).peakpos - range);
643 cpl_table_and_selected_double(tab,
"logwave", CPL_LESS_THAN,
644 (*gfit).peakpos + range);
646 ext_tab = cpl_table_extract_selected(tab);
647 cpl_table_unwrap(tab,
"logwave");
648 cpl_table_unwrap(tab,
"flux");
650 sprintf(fname,
"fcorr_ext.fits");
653 next = cpl_table_get_nrow(ext_tab);
654 sprintf(fname,
"fcorr_tab.fits");
662 wave = cpl_vector_wrap(next, cpl_table_get_data_double(ext_tab,
"logwave"));
663 flux = cpl_vector_wrap(next, cpl_table_get_data_double(ext_tab,
"flux"));
666 cpl_vector_fit_gaussian( wave, NULL, flux,NULL, CPL_FIT_ALL,
667 &peakpos, &
sigma,&area, &offset, &mse,NULL,NULL);
669 cpl_vector_fit_gaussian( wave, NULL, flux,NULL, CPL_FIT_ALL,
675 cpl_vector_unwrap(wave);
676 cpl_vector_unwrap(flux);
678 return cpl_error_get_code();
684 cpl_table* table_ext=NULL;
686 cpl_table_and_selected_double(table_in, col, CPL_NOT_LESS_THAN, wmin);
687 cpl_table_and_selected_double(table_in, col, CPL_NOT_GREATER_THAN, wmax);
688 table_ext=cpl_table_extract_selected(table_in);
699 const int hsearch,
const double wlogstp,
const int norm_xcorr,
702 double* xcorr = NULL;
703 double* pwcorr = NULL;
708 double size_corr = 2 * hsearch + 1;
711 cpl_vector* correl=NULL;
713 cpl_vector* fcorr = NULL;
714 cpl_vector* wcorr = NULL;
717 norm_xcorr,&corr, &delta));
721 f2=cpl_vector_wrap(
size, flux_m);
725 correl=cpl_vector_new(size_corr);
726 check(shift=cpl_vector_correlate(correl,f1,f2));
727 cpl_vector_unwrap(f1);
728 cpl_vector_unwrap(f2);
734 gfit->
peakpos = (hsearch + delta)*wlogstp;
735 gfit->
sigma = wlogstp*10;
740 check(fcorr = cpl_vector_wrap(size_corr, xcorr));
744 pwcorr= (
double*) cpl_calloc(size_corr,
sizeof(
double));
745 for (i = 0; i < size_corr; i++) {
746 pwcorr[i] = i*wlogstp;
748 check(wcorr = cpl_vector_wrap(size_corr, pwcorr));
749 sprintf(fname,
"fcorr.fits");
751 sprintf(fname,
"wcorr.fits");
757 xsh_msg(
"gauss fit: peak[lognm]: %12.8g sigma[lognm] %g peak[sampl_units]: %12.8g sigma[sampl_units] %g",
762 cpl_vector_unwrap(fcorr);
763 cpl_vector_unwrap(wcorr);
767 return cpl_error_get_code();
773 cpl_table* table_rm = NULL;
774 cpl_table* table_rs = NULL;
777 int size_mm=cpl_table_get_nrow(*table_mm);
779 cpl_vector* vfluxm = NULL;
780 cpl_vector* vlowpass_fluxm = NULL;
787 sprintf(fname,
"model_selected_wrange_log_resampled_uniform.fits");
794 sprintf(fname,
"spectrum_resampled_used_in_correlation.fits");
803 double* flux_s = NULL;
804 double* flux_m = NULL;
818 double wrange=0.0005;
821 flux_s = cpl_table_get_data_double(table_rs,
"flux");
822 flux_m = cpl_table_get_data_double(table_rm,
"flux");
823 size_s = cpl_table_get_nrow(table_rs);
829 xsh_msg(
"computed shift[lognm]=%g",(gfit.
peakpos-hsearch*wlogstp));
830 hsearch=(int)3.*CPL_MATH_FWHM_SIG * gfit.
sigma/wlogstp;
837 cpl_table_add_scalar(*table_mm,
"logwave", (gfit.
peakpos-hsearch*wlogstp));
838 xsh_msg(
"computed shift[lognm]=%g",(gfit.
peakpos-hsearch*wlogstp));
859 double* pvlowpass_fluxm = NULL;
860 double* pflux_c = NULL;
861 double fwhm = CPL_MATH_FWHM_SIG * gfit.
sigma;
862 double fwhm_nm = pow(CPL_MATH_E, fwhm);
863 cpl_vector * conv_kernel =NULL;
866 int fwhm_pix = (int) (fwhm / wlogstp + 0.5);
868 cpl_table_new_column(*table_mm,
"flux_c", CPL_TYPE_DOUBLE);
869 cpl_table_fill_column_window_double(*table_mm,
"flux_c", 0, size_mm, 0);
870 xsh_msg(
"fwhm_nm=%12.10g fwhm_pix=%d",fwhm_nm,fwhm_pix);
872 vfluxm = cpl_vector_wrap(size_mm,cpl_table_get_data_double(*table_mm,
"flux"));
875 conv_kernel = cpl_wlcalib_xc_convolve_create_kernel(fwhm_pix/CPL_MATH_FWHM_SIG,fwhm_pix/CPL_MATH_FWHM_SIG);
876 vlowpass_fluxm=cpl_vector_duplicate(vfluxm);
877 cpl_wlcalib_xc_convolve(vlowpass_fluxm, conv_kernel);
881 pvlowpass_fluxm = cpl_vector_get_data(vlowpass_fluxm);
882 cpl_vector_unwrap(vfluxm);
884 pflux_c = cpl_table_get_data_double(*table_mm,
"flux_c");
885 for (i = 0; i < size_mm; i++) {
886 pflux_c[i] = pvlowpass_fluxm[i];
888 sprintf(fname,
"model_convolved.fits");
898 return cpl_error_get_code();
906 cpl_table* tab_res=NULL;
907 cpl_table* table_s=NULL;
908 cpl_table* table_m=NULL;
909 cpl_table* table_se=NULL;
910 cpl_table* table_mm=NULL;
911 cpl_table* table_rm = NULL;
912 cpl_table* table_rs_div_mc=NULL;
913 cpl_table* table_me=NULL;
914 cpl_table* table_rs=NULL;
915 const char* name_model=NULL;
922 double wlogstp = 10.e-6;
933 cpl_table* tab_corr = NULL;
934 cpl_table* tab_stack = NULL;
936 cpl_table* ext_tab = NULL;
942 cpl_propertylist* qc_head=NULL;
957 sprintf(fname,
"spectrum_obs.fits");
965 check(name_model=cpl_frame_get_filename(frame_m));
967 check(next=cpl_frame_get_nextensions(frame_m));
968 check(tab_res=cpl_table_new(next));
969 cpl_table_new_column(tab_res,
"id",CPL_TYPE_INT);
970 cpl_table_new_column(tab_res,
"mean",CPL_TYPE_DOUBLE);
971 cpl_table_new_column(tab_res,
"rms",CPL_TYPE_DOUBLE);
973 cpl_table_fill_column_window_int(tab_res,
"id", 0, next, 0);
974 cpl_table_fill_column_window_double(tab_res,
"mean", 0, next, 0);
975 cpl_table_fill_column_window_double(tab_res,
"rms", 0, next, 0);
976 pext=cpl_table_get_data_int(tab_res,
"id");
977 pmean=cpl_table_get_data_double(tab_res,
"mean");
978 prms=cpl_table_get_data_double(tab_res,
"rms");
979 check(next=cpl_table_get_nrow(tab_res));
982 for (ext = 0; ext < next; ext++) {
985 check(table_m = cpl_table_load(name_model, ext+1, 1));
986 sprintf(fname,
"model_arm.fits");
989 cpl_table_multiply_scalar(table_m,
"lam", um2nm);
990 cpl_table_duplicate_column(table_m,
"logwave", table_m,
"lam");
991 cpl_table_logarithm_column(table_m,
"logwave", CPL_MATH_E);
997 sprintf(fname,
"model_range_arm.fits");
1010 sprintf(fname,
"model_selected_wrange_log.fits");
1015 cpl_table_duplicate_column(table_se,
"wav", table_se,
"logwave");
1016 cpl_table_exponential_column(table_se,
"wav", CPL_MATH_E);
1017 sprintf(fname,
"spectrum_selected_wrange_log.fits");
1052 check(cpl_table_duplicate_column(table_mm,
"wave",table_mm,
"logwave"));
1053 check(cpl_table_exponential_column(table_mm,
"wave",CPL_MATH_E));
1054 sprintf(fname,
"model_aligned_to_obs.fits");
1072 sprintf(fname,
"table_rm.fits");
1080 sprintf(fname,
"spectrum_resampled_to_model.fits");
1083 cpl_table_duplicate_column(table_rs,
"flux_model_convolved", table_mm,
"flux_c");
1084 cpl_table_duplicate_column(table_rs,
"ratio", table_rs,
"flux");
1086 check(cpl_table_divide_columns(table_rs,
"ratio",
"flux_model_convolved"));
1091 sprintf(fname,
"spectrum_observed_divided_by_model_convolved.fits");
1093 table_rs_div_mc=cpl_table_duplicate(table_rs);
1108 sprintf(fname,
"tab_stack.fits");
1116 cpl_vector* vec_wave = NULL;
1117 cpl_vector* vec_flux = NULL;
1139 check(nsel=cpl_table_get_nrow(tab_stack));
1140 cpl_table_new_column(tab_stack,
"fit", CPL_TYPE_DOUBLE);
1141 cpl_table_fill_column_window_double(tab_stack,
"fit", 0, nsel, 0);
1142 pfit = cpl_table_get_data_double(tab_stack,
"fit");
1167 double* pvec_wave=NULL;
1168 double* pvec_flux=NULL;
1169 pvec_wave=cpl_vector_get_data(vec_wave);
1170 pvec_flux=cpl_vector_get_data(vec_flux);
1178 pvec_wave,nsamples+2));
1198 cpl_table* tab_med = cpl_table_new(nsamples+2);
1200 check(cpl_table_wrap_double(tab_med,pvec_wave,
"wave"));
1201 check(cpl_table_wrap_double(tab_med,pvec_flux,
"ratio"));
1202 check(cpl_table_wrap_double(tab_med,sfit,
"fit"));
1203 sprintf(fname,
"ratio_med.fits");
1215 tab_corr=cpl_table_duplicate(table_rs);
1216 int nrow=cpl_table_get_nrow(tab_corr);
1217 cpl_table_new_column(tab_corr,
"fit",CPL_TYPE_DOUBLE);
1218 cpl_table_fill_column_window_double(tab_corr,
"fit", 0, nrow, 0);
1220 check(pfit=cpl_table_get_data_double(tab_corr,
"fit"));
1221 check(wave=cpl_table_get_data_double(tab_corr,
"wave"));
1225 nsamples+2,wave,nrow));
1226 for (i = 0; i < nrow; i ++) {
1238 check(cpl_table_duplicate_column(tab_corr,
"correction", table_rs,
"ratio"));
1239 check(cpl_table_divide_columns(tab_corr,
"correction",
"fit"));
1243 cpl_table_unwrap(tab_med,
"wave");
1244 cpl_table_unwrap(tab_med,
"ratio");
1245 cpl_table_unwrap(tab_med,
"fit");
1259 qc_head=cpl_propertylist_new();
1264 xsh_msg(
"ext=%d mean=%g rms=%g", ext,mean, rms);
1273 double model_mean=0;
1276 check(model_mean=cpl_table_get_column_min(tab_res,
"mean"));
1279 cpl_table_get_column_minpos(tab_res,
"mean",model_idx);
1280 check(model_rms=cpl_table_get_double(tab_res,
"rms",*model_idx,&status));
1281 xsh_msg(
"optimal model: mean=%g rms=%g",model_mean,model_rms);
1282 xsh_msg(
"optimal model id %d mean %g rms %g",
1283 (
int)(*model_idx),model_mean,model_rms);
1287 sprintf(fname,
"spectrum_observed_divided_by_model_convolved.fits");
1289 ext=(int)(*model_idx);
1290 xsh_msg(
"fname=%s ext=%d",fname,ext);
1291 check(table_rs=cpl_table_load(fname,ext+1,0));
1293 qc_head=cpl_propertylist_new();
static xsh_instrument * instrument
int xsh_spectrum_get_size_lambda(xsh_spectrum *s)
Get lambda axis size of spectrum.
double xsh_spectrum_get_lambda_min(xsh_spectrum *s)
Get minimum lambda of spectrum.
double * xsh_spectrum_get_errs(xsh_spectrum *s)
Get errs of spectrum.
double xsh_spectrum_get_lambda_step(xsh_spectrum *s)
Get bin in lambda of spectrum.
int * xsh_spectrum_get_qual(xsh_spectrum *s)
Get qual of 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.
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
#define xsh_msg(...)
Print a message on info level.
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
double * xsh_function1d_xcorrelate(double *line_i, int width_i, double *line_t, int width_t, int half_search, int normalise, double *xcorr_max, double *delta)
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.
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
double * xsh_bspline_interpolate_data_at_pos(double *w_data, double *f_data, const int n_data, double *w_pos, const int n_pos)
cpl_error_code xsh_table_save(cpl_table *t, cpl_propertylist *ph, cpl_propertylist *xh, const char *fname, const int ext)
HIGH_ABS_REGION * xsh_fill_tell_compute_resid_regions(xsh_instrument *instrument, cpl_frame *high_abs_frame)
HIGH_ABS_REGION * xsh_fill_tell_fit_regions(xsh_instrument *instrument, cpl_frame *high_abs_frame)
#define XSH_QC_TELLCORR_RATRMS
#define XSH_QC_TELLCORR_OPTEXTID
#define XSH_QC_TELLCORR_RATAVG