50#include <gsl/gsl_bspline.h>
51#include <gsl/gsl_linalg.h>
52#include <gsl/gsl_rng.h>
53#include <gsl/gsl_statistics_double.h>
54#include <gsl/gsl_multifit.h>
76#define REGDEBUG_MEDIAN_SPLINE 0
77#define REGDEBUG_BSPLINE 0
84 cpl_frame *order_table_frame,
xsh_pre *pre_sci,
int nbkpts,
85 cpl_frame* usr_def_sampl_points,cpl_frame* ref_sky_list,
86 cpl_frame* sky_orders_chunks,
97 const char* rec_prefix,
105 cpl_table* xtab=NULL;
106 xtab=cpl_table_duplicate(tab);
107 int norg=cpl_table_get_nrow(xtab);
109 double* pwave=cpl_table_get_data_double(xtab,wname);
110 double* pflux=cpl_table_get_data_double(xtab,fname);
116 for(j=0;ninv>0 && j<3;j++) {
118 nrow=cpl_table_get_nrow(xtab);
125 pwave=cpl_table_get_data_double(xtab,wname);
126 pflux=cpl_table_get_data_double(xtab,fname);
127 for(
int i=1;i < nrow; i++) {
129 if ((pwave[i-1] == pwave[i])) {
135 if( ( pflux[i-1] <= pflux[i] ) && i >1 ) {
137 cpl_table_set_invalid(xtab,fname,i-1);
138 cpl_table_set_invalid(xtab,wname,i-1);
140 cpl_table_set_invalid(xtab,fname,i);
141 cpl_table_set_invalid(xtab,wname,i);
153 ninv=cpl_table_count_invalid(xtab,fname);
154 xsh_msg(
"iter=%d nrow=%d nbad=%d ninv=%d",j,nrow,nbad,ninv);
156 cpl_table_erase_invalid(xtab);
163 int nnew=cpl_table_get_nrow(xtab);
164 xsh_msg(
"niter %d N orig %d flagged %d expected %d new %d",
165 j,norg,nbad,norg-nbad,nnew);
174 double* swave,
const int sno,
184 gsl_bspline_workspace *bw;
192 gsl_multifit_linear_workspace *mw;
193 double chisq=0, Rsq=0, dof=0;
196 cpl_table* tab_resp_fit = NULL;
198 double dump_factor=1.e10;
209 xsh_msg(
"instrument arm not set");
212 ncoeffs=(wave[0]-wave[
n-1])/0.5;
214 nbreak = ncoeffs + 2 -
order;
221 bw = gsl_bspline_alloc(
order, nbreak);
223 B = gsl_vector_alloc(ncoeffs);
225 Bkpts = gsl_vector_alloc(nbreak);
227 x = gsl_vector_alloc(
n);
228 y = gsl_vector_alloc(
n);
229 X = gsl_matrix_alloc(
n, ncoeffs);
230 c = gsl_vector_alloc(ncoeffs);
231 w = gsl_vector_alloc(
n);
233 cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
234 mw = gsl_multifit_linear_alloc(
n, ncoeffs);
237 for (i = 0; i <
n; ++i) {
242 variance = ron2 * fabs(yi)/gain ;
244 gsl_vector_set(
x, i, xi);
245 if(isnan(yi) || isinf(yi)) {
246 gsl_vector_set(
y, i, 0);
247 gsl_vector_set(w, i, 1.0 / dump_factor);
249 gsl_vector_set(
y, i, yi);
250 gsl_vector_set(w, i, 1.0 / variance );
255 Bkpts_ptr=gsl_vector_ptr(Bkpts,0);
256 int nfit_div_nbreak=
n/nbreak;
258 for(i=0;i<nbreak;i++) {
259 Bkpts_ptr[i]=wave[i*nfit_div_nbreak];
261 Bkpts_ptr[0]=wave[0];
262 Bkpts_ptr[nbreak-1]=wave[
n-1];
264 gsl_bspline_knots(Bkpts, bw);
276 for (i = 0; i <
n; ++i) {
277 double xi = gsl_vector_get(
x, i);
281 gsl_bspline_eval(xi, B, bw);
284 for (j = 0; j < ncoeffs; ++j) {
285 double Bj = gsl_vector_get(B, j);
287 gsl_matrix_set(X, i, j, Bj);
295 gsl_multifit_wlinear(X, w,
y, c, cov, &chisq, mw);
302 printf(
"chisq/dof = %e, Rsq = %f\n", chisq / dof, Rsq);
304 tab_resp_fit = cpl_table_new(sno);
305 cpl_table_new_column(tab_resp_fit,
"wave", CPL_TYPE_DOUBLE);
306 cpl_table_new_column(tab_resp_fit,
"fit", CPL_TYPE_DOUBLE);
307 cpl_table_fill_column_window_double(tab_resp_fit,
"wave", 0, sno, 0);
308 cpl_table_fill_column_window_double(tab_resp_fit,
"fit", 0, sno, 0);
309 pwav = cpl_table_get_data_double(tab_resp_fit,
"wave");
310 pfit = cpl_table_get_data_double(tab_resp_fit,
"fit");
311 sflux=cpl_calloc(sno,
sizeof(
double));
316 for (i = 0; i < sno; i++) {
317 xi = (double) swave[i];
318 gsl_bspline_eval(xi, B, bw);
319 gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
327 cpl_table_save(tab_resp_fit, NULL, NULL,
"resp_fit.fits", CPL_IO_DEFAULT);
332 gsl_bspline_free(bw);
334 gsl_vector_free(Bkpts);
340 gsl_matrix_free(cov);
341 gsl_multifit_linear_free(mw);
348xsh_detect_outliers_tab(
const int order,
const int idx,
const int nitem,
366 cpl_table* otab=cpl_table_new(nitem);
367 cpl_table_new_column(otab,
"WAVE",CPL_TYPE_DOUBLE);
368 cpl_table_new_column(otab,
"SLIT",CPL_TYPE_DOUBLE);
369 cpl_table_new_column(otab,
"FLUX",CPL_TYPE_DOUBLE);
370 cpl_table_new_column(otab,
"SIGMA",CPL_TYPE_DOUBLE);
371 cpl_table_new_column(otab,
"FIT",CPL_TYPE_DOUBLE);
373 cpl_table_new_column(otab,
"ERR",CPL_TYPE_DOUBLE);
374 cpl_table_new_column(otab,
"RAT",CPL_TYPE_DOUBLE);
375 cpl_table_new_column(otab,
"X",CPL_TYPE_DOUBLE);
376 cpl_table_new_column(otab,
"Y",CPL_TYPE_DOUBLE);
377 cpl_table_new_column(otab,
"FLAG",CPL_TYPE_INT);
379 cpl_table_fill_column_window_double(otab,
"WAVE",0,nitem,0.);
380 cpl_table_fill_column_window_double(otab,
"SLIT",0,nitem,0.);
381 cpl_table_fill_column_window_double(otab,
"FLUX",0,nitem,0.);
382 cpl_table_fill_column_window_double(otab,
"SIGMA",0,nitem,0.);
383 cpl_table_fill_column_window_double(otab,
"FIT",0,nitem,0.);
384 cpl_table_fill_column_window_double(otab,
"ERR",0,nitem,0.);
385 cpl_table_fill_column_window_double(otab,
"RAT",0,nitem,0.);
386 cpl_table_fill_column_window_int(otab,
"FLAG",0,nitem,0.);
387 cpl_table_fill_column_window_double(otab,
"X",0,nitem,0.);
388 cpl_table_fill_column_window_double(otab,
"Y",0,nitem,0.);
390 pw=cpl_table_get_data_double(otab,
"WAVE");
391 pf=cpl_table_get_data_double(otab,
"FLUX");
392 pslit=cpl_table_get_data_double(otab,
"SLIT");
393 ps=cpl_table_get_data_double(otab,
"SIGMA");
394 pfit=cpl_table_get_data_double(otab,
"FIT");
395 prat=cpl_table_get_data_double(otab,
"RAT");
396 perr=cpl_table_get_data_double(otab,
"ERR");
397 px=cpl_table_get_data_double(otab,
"X");
398 py=cpl_table_get_data_double(otab,
"Y");
405 for (i = 0; i < nitem; i++, pentry++) {
412 pslit[i] = pentry->
slit;
413 pf[i] = pentry->
flux;
414 ps[i] = pentry->
sigma;
417 prat[i] = pf[i] / pfit[i];
422 cpl_table_duplicate_column(otab,
"FLUX_SMOOTH",otab,
"FLUX");
423 cpl_vector* vec_flux=NULL;
424 cpl_vector* vec_smooth=NULL;
425 vec_flux = cpl_vector_wrap(nitem,cpl_table_get_data_double(otab,
"FLUX"));
426 vec_smooth=cpl_vector_filter_lowpass_create(vec_flux,CPL_LOWPASS_LINEAR,1);
427 cpl_vector_unwrap(vec_flux);
428 double* pts=cpl_table_get_data_double(otab,
"FLUX_SMOOTH");
429 double* pvs=cpl_vector_get_data(vec_smooth);
431 for (i = 0; i < nitem; i++, pentry++) {
434 cpl_vector_delete(vec_smooth);
435 cpl_table_duplicate_column(otab,
"FLUX_SMO",otab,
"FLUX");
436 cpl_table_divide_columns(otab,
"FLUX_SMO",
"FLUX_SMOOTH");
449 prat=cpl_table_get_data_double(otab,
"RAT");
450 pflag=cpl_table_get_data_int(otab,
"FLAG");
453 prat=cpl_table_get_data_double(otab,
"FLUX_SMO");
454 pflag=cpl_table_get_data_int(otab,
"FLAG");
456 for(i=0 ;i<
niter;i++) {
457 mean=cpl_table_get_column_mean(otab,
"FLUX_SMO");
458 median=cpl_table_get_column_median(otab,
"FLUX_SMO");
459 rms=cpl_table_get_column_stdev(otab,
"FLUX_SMO");
460 min=cpl_table_get_column_min(otab,
"FLUX_SMO");
461 max=cpl_table_get_column_max(otab,
"FLUX_SMO");
462 for(j=0;j<nitem;j++) {
463 if( prat[j] < mean-
kappa*rms ) {
464 cpl_table_set_invalid(otab,
"FLUX_SMO",j);
466 }
else if( prat[j] > mean+
kappa*rms ) {
467 cpl_table_set_invalid(otab,
"FLUX_SMO",j);
472 n=cpl_table_count_invalid(otab,
"FLUX_SMO");
473 xsh_msg(
"mean=%g median=%g rms=%g min=%g max=%g invalid=%d",
474 mean,median,rms,min,
max,
n);
479 prat=cpl_table_get_data_double(otab,
"RAT");
481 for(i=0 ;i<
niter;i++) {
482 mean=cpl_table_get_column_mean(otab,
"RAT");
483 median=cpl_table_get_column_median(otab,
"RAT");
484 rms=cpl_table_get_column_stdev(otab,
"RAT");
485 min=cpl_table_get_column_min(otab,
"RAT");
486 max=cpl_table_get_column_max(otab,
"RAT");
487 for(j=0;j<nitem;j++) {
488 if( prat[j] < mean-
kappa*rms ) {
489 cpl_table_set_invalid(otab,
"RAT",j);
491 }
else if( prat[j] > mean+
kappa*rms ) {
492 cpl_table_set_invalid(otab,
"RAT",j);
497 n=cpl_table_count_invalid(otab,
"RAT");
498 xsh_msg(
"mean=%g median=%g rms=%g min=%g max=%g invalid=%d",
499 mean,median,rms,min,
max,
n);
507 sprintf(fname,
"outlier1d_ord_%02d_iter_%02d.reg",
order,iter);
508 fout = fopen(fname,
"w");
509 fprintf(fout,
"# x y\n");
512 for (i = 0; i < nitem; i++, pentry++) {
514 if(pflag[i] == -1 || pflag[i] == 1 )
517 fprintf(fout,
"x point (%f %f) #color=red \n", px[i]+1,py[i]+1);
521 if(pflag[i] == -2 || pflag[i] == 2 ) {
523 fprintf(fout,
"x point (%f %f) #color=blue \n",px[i]+1,py[i]+1);
531 sprintf(fname,
"outlier1d_order_%02d_iter_%02d.fits",
order,iter);
533 cpl_table_and_selected_int(otab,
"FLAG", CPL_EQUAL_TO, 2);
534 cpl_table_or_selected_int(otab,
"FLAG", CPL_EQUAL_TO, -2);
535 cpl_table* xtab=cpl_table_extract_selected(otab);
539 cpl_table_erase_column(xtab,
"RAT");
540 cpl_table_erase_column(xtab,
"FLUX_SMOOTH");
541 cpl_table_erase_column(xtab,
"FLUX_SMO");
542 cpl_table_erase_column(xtab,
"SIGMA");
543 cpl_table_erase_column(xtab,
"FIT");
544 cpl_table_erase_column(xtab,
"FLAG");
545 cpl_table_erase_column(xtab,
"SLIT");
546 cpl_table_erase_column(xtab,
"FLUX");
547 cpl_table_erase_column(xtab,
"ERR");
550 cpl_table_name_column(otab,
"WAVE",
"WAVELENGTH");
552 cpl_table_and_selected_double(otab,
"WAVELENGTH",CPL_EQUAL_TO,0);
553 cpl_table_erase_selected(otab);
560 if ( fout ) fclose( fout ) ;
586 cpl_table* otab=cpl_table_new(sky_ndata);
587 cpl_table_new_column(otab,
"WAVE",CPL_TYPE_DOUBLE);
588 cpl_table_new_column(otab,
"FLUX",CPL_TYPE_DOUBLE);
589 cpl_table_new_column(otab,
"ERR",CPL_TYPE_DOUBLE);
590 cpl_table_new_column(otab,
"QUAL",CPL_TYPE_INT);
591 cpl_table_new_column(otab,
"SLIT",CPL_TYPE_DOUBLE);
592 cpl_table_new_column(otab,
"X",CPL_TYPE_DOUBLE);
593 cpl_table_new_column(otab,
"Y",CPL_TYPE_DOUBLE);
595 cpl_table_fill_column_window_double(otab,
"WAVE",0,sky_ndata,0.);
596 cpl_table_fill_column_window_double(otab,
"FLUX",0,sky_ndata,0.);
597 cpl_table_fill_column_window_double(otab,
"ERR",0,sky_ndata,0.);
598 cpl_table_fill_column_window_int(otab,
"QUAL",0,sky_ndata,0);
599 cpl_table_fill_column_window_double(otab,
"SLIT",0,sky_ndata,0.);
600 cpl_table_fill_column_window_double(otab,
"X",0,sky_ndata,0.);
601 cpl_table_fill_column_window_double(otab,
"Y",0,sky_ndata,0.);
603 pwave=cpl_table_get_data_double(otab,
"WAVE");
604 pflux=cpl_table_get_data_double(otab,
"FLUX");
605 perr=cpl_table_get_data_double(otab,
"ERR");
606 pqual=cpl_table_get_data_int(otab,
"QUAL");
607 pslit=cpl_table_get_data_double(otab,
"SLIT");
608 px=cpl_table_get_data_double(otab,
"X");
609 py=cpl_table_get_data_double(otab,
"Y");
615 for (
int i = 0; i < sky_ndata; i++, psky++) {
617 pflux[i] = psky->
flux;
618 perr[i] = psky->
sigma;
619 pqual[i] = psky->
qual;
620 pslit[i] = psky->
slit;
639 const char* name_i,
const char* name_o,
const int hsize)
642 cpl_table_duplicate_column(*tab,name_o,*tab,name_i);
644 cpl_vector* v_flux=NULL;
645 cpl_vector* v_smooth=NULL;
646 int nrow=cpl_table_get_nrow(*tab);
647 v_flux = cpl_vector_wrap(nrow,cpl_table_get_data_double(*tab,name_i));
648 v_smooth=cpl_vector_filter_median_create(v_flux,hsize);
649 cpl_vector_unwrap(v_flux);
650 double* pts=cpl_table_get_data_double(*tab,
"FLUX_SMOOTH");
652 double* pvs=cpl_vector_get_data(v_smooth);
654 for (
int i = 0; i < nrow; i++) {
657 cpl_vector_delete(v_smooth);
659 return cpl_error_get_code();
666 const double gain,
const double ron2,
const int decode_bp)
671 int nrow=cpl_table_get_nrow(*tab);
672 cpl_table_new_column(*tab,
"FLAG",CPL_TYPE_INT);
673 cpl_table_fill_column_window_int(*tab,
"FLAG",0,nrow,0);
674 int* pflag=cpl_table_get_data_int(*tab,
"FLAG");
677 cpl_table_duplicate_column(*tab,
"DIF",*tab,name_d);
678 cpl_table_subtract_columns(*tab,
"DIF",name_s);
680 cpl_table_duplicate_column(*tab,
"SIG",*tab,name_s);
683 cpl_table_abs_column(*tab,
"SIG");
684 cpl_table_divide_scalar(*tab,
"SIG",gain);
685 cpl_table_add_scalar(*tab,
"SIG",ron2);
686 cpl_table_power_column(*tab,
"SIG",0.5);
689 double* pdif=cpl_table_get_data_double(*tab,
"DIF");
690 double* perr=cpl_table_get_data_double(*tab,
"SIG");
691 int* pqual=cpl_table_get_data_int(*tab,
"QUAL");
697 for(
int i=0 ;i<
niter;i++) {
698 median=cpl_table_get_column_median(*tab,
"DIF");
704 for (k = 1; k < nrow-1; k++) {
705 int check_data_quality = (( ( pqual[k] & decode_bp ) > 0 ) &&
707 check_data_quality=0;
709 if( (pdif[k] < median-
kappa*perr[k] ||
710 pdif[k] > median+
kappa*perr[k] ) ||
711 (check_data_quality == 1) ){
717 cpl_table_set_invalid(*tab,
"DIF",k);
722 n=cpl_table_count_invalid(*tab,
"DIF");
730 xsh_msg(
"Points flagged as invalid %d vs n_tot %d", ninv,n_tot);
733 return cpl_error_get_code();
752 const double ron2,
const double gain,
753 const int order,
const int iter,
757 cpl_table* otab=NULL;
774 sprintf(fname,
"outlier1d_ord_%02d_iter_%02d.reg",
order,iter);
775 fout = fopen(fname,
"w");
776 fprintf(fout,
"# x y\n");
788 px=cpl_table_get_data_double(otab,
"X");
789 py=cpl_table_get_data_double(otab,
"Y");
791 pflag=cpl_table_get_data_int(otab,
"FLAG");
792 int* pqual=cpl_table_get_data_int(otab,
"QUAL");
795 for (
int i = 1; i < sky_ndata-1; i++,psky++) {
796 if( pflag[i] == 1 ) {
798 fprintf(fout,
"x point (%f %f) #color=blue \n",px[i]+1,py[i]+1);
806 sprintf(fname,
"outlier1d_order_%02d_iter_%02d.fits",
order,iter);
807 cpl_table_save(otab,NULL,NULL,fname,CPL_IO_DEFAULT);
811 cpl_table_and_selected_int(otab,
"FLAG", CPL_EQUAL_TO, 0);
812 cpl_table* xtab=cpl_table_extract_selected(otab);
813 int n=cpl_table_get_nrow(xtab);
814 xsh_msg(
"Points remained after flagging outliers %d",
n);
816 cpl_table_and_selected_double(xtab,
"WAVE",CPL_EQUAL_TO,0);
817 cpl_table_erase_selected(xtab);
818 n=cpl_table_get_nrow(xtab);
820 sprintf(fname,
"tab_clean_order_%02d_iter_%02d.fits",
order,iter);
821 cpl_table_save(xtab,NULL,NULL,fname,CPL_IO_DEFAULT);
823 xsh_msg(
"Points remained after removal 0 waves %d",
n);
825 cpl_table_erase_column(xtab,
"SIG");
827 if ( fout ) fclose( fout ) ;
848 const double ron2,
const double gain,
849 const int order,
const int iter,
852 const double s2,
const int slice_id)
855 cpl_table* otab=NULL;
859 cpl_table_and_selected_double(otab,
"SLIT",CPL_NOT_LESS_THAN,s1);
860 cpl_table_and_selected_double(otab,
"SLIT",CPL_LESS_THAN,s2);
862 cpl_table* stab=NULL;
863 stab=cpl_table_extract_selected(otab);
879 sprintf(fname,
"outlier1d_ord_%02d_slice_%02d_order_%02d.reg",
880 order,slice_id,iter);
881 fout = fopen(fname,
"w");
882 fprintf(fout,
"# x y\n");
895 nrow=cpl_table_get_nrow(stab);
896 px=cpl_table_get_data_double(stab,
"X");
897 py=cpl_table_get_data_double(stab,
"Y");
898 pw=cpl_table_get_data_double(stab,
"WAVE");
899 pflag=cpl_table_get_data_int(stab,
"FLAG");
900 int* pqual=cpl_table_get_data_int(stab,
"QUAL");
903 for(
int k=0; k<nrow; k++) {
904 if( pflag[k] == 1 ) {
906 for (
int i = 1; i < sky_ndata-1; i++,psky++) {
907 if( psky->
slit >= s1 && psky->
slit <= s2 ) {
908 if( pw[k] == psky->
lambda &&
909 px[k] == psky->
ix && py[k] == psky->
iy ) {
911 fprintf(fout,
"x point (%f %f) #color=blue \n",px[k]+1,py[k]+1);
922 sprintf(fname,
"outlier1d_order_%02d_slice_%02d_iter_%02d.fits",
923 order,slice_id,iter);
924 cpl_table_save(stab,NULL,NULL,fname,CPL_IO_DEFAULT);
928 check(cpl_table_and_selected_int(stab,
"FLAG", CPL_EQUAL_TO, 0));
929 cpl_table* xtab=cpl_table_extract_selected(stab);
932 int n=cpl_table_get_nrow(xtab);
933 xsh_msg(
"Points remained after flagging outliers %d",
n);
935 cpl_table_and_selected_double(xtab,
"WAVE",CPL_EQUAL_TO,0);
936 cpl_table_erase_selected(xtab);
937 n=cpl_table_get_nrow(xtab);
939 sprintf(fname,
"tab_clean_order_%02d_slice_%02d_iter_%02d.fits",
940 order,slice_id,iter);
941 cpl_table_save(xtab,NULL,NULL,fname,CPL_IO_DEFAULT);
943 xsh_msg(
"Points remained after removal 0 waves %d",
n);
944 cpl_table_erase_column(xtab,
"SIG");
946 if ( fout ) fclose( fout ) ;
954xsh_dump_sky_fit(
const int order,
const int idx,
const int nitem,
973 cpl_table* debug_fit=cpl_table_new(nitem);
974 cpl_table_new_column(debug_fit,
"WAVE",CPL_TYPE_DOUBLE);
975 cpl_table_new_column(debug_fit,
"SLIT",CPL_TYPE_DOUBLE);
976 cpl_table_new_column(debug_fit,
"FLUX",CPL_TYPE_DOUBLE);
977 cpl_table_new_column(debug_fit,
"SIGMA",CPL_TYPE_DOUBLE);
978 cpl_table_new_column(debug_fit,
"DIF",CPL_TYPE_DOUBLE);
979 cpl_table_new_column(debug_fit,
"FIT",CPL_TYPE_DOUBLE);
980 cpl_table_new_column(debug_fit,
"ERR",CPL_TYPE_DOUBLE);
981 cpl_table_new_column(debug_fit,
"RAT",CPL_TYPE_DOUBLE);
982 cpl_table_new_column(debug_fit,
"X",CPL_TYPE_DOUBLE);
983 cpl_table_new_column(debug_fit,
"Y",CPL_TYPE_DOUBLE);
985 cpl_table_fill_column_window_double(debug_fit,
"WAVE",0,nitem,0.);
986 cpl_table_fill_column_window_double(debug_fit,
"SLIT",0,nitem,0.);
987 cpl_table_fill_column_window_double(debug_fit,
"FLUX",0,nitem,0.);
988 cpl_table_fill_column_window_double(debug_fit,
"SIGMA",0,nitem,0.);
989 cpl_table_fill_column_window_double(debug_fit,
"DIF",0,nitem,0.);
990 cpl_table_fill_column_window_double(debug_fit,
"FIT",0,nitem,0.);
991 cpl_table_fill_column_window_double(debug_fit,
"ERR",0,nitem,0.);
992 cpl_table_fill_column_window_double(debug_fit,
"RAT",0,nitem,0.);
993 cpl_table_fill_column_window_double(debug_fit,
"X",0,nitem,0.);
994 cpl_table_fill_column_window_double(debug_fit,
"Y",0,nitem,0.);
996 pw=cpl_table_get_data_double(debug_fit,
"WAVE");
997 pf=cpl_table_get_data_double(debug_fit,
"FLUX");
998 pslit=cpl_table_get_data_double(debug_fit,
"SLIT");
999 ps=cpl_table_get_data_double(debug_fit,
"SIGMA");
1000 pfit=cpl_table_get_data_double(debug_fit,
"FIT");
1001 pdif=cpl_table_get_data_double(debug_fit,
"DIF");
1002 prat=cpl_table_get_data_double(debug_fit,
"RAT");
1003 perr=cpl_table_get_data_double(debug_fit,
"ERR");
1004 px=cpl_table_get_data_double(debug_fit,
"X");
1005 py=cpl_table_get_data_double(debug_fit,
"Y");
1010 sprintf(fname,
"sky_fit_%02d_%02d.dat",
order,iter);
1011 fout = fopen(fname,
"w");
1013 pentry = wlist->
list[idx].
sky;
1015 for (i = 0; i < nitem; i++, pentry++) {
1021 fprintf(fout,
"%f %f %f %d %d ", pentry->
lambda, pentry->
flux,
1023 fprintf(fout,
"%lf %lf\n", yi, yerr);
1027 pslit[i] = pentry->
slit;
1028 pf[i] = pentry->
flux;
1029 ps[i] = pentry->
sigma;
1030 pfit[i] = pentry->
fitted;
1032 prat[i] = pf[i] / pfit[i];
1033 pdif[i] = pf[i] - pfit[i];
1040 sprintf(fname,
"sky_fit_%02d_%02d.fits",
order,iter);
1041 cpl_table_save(debug_fit,NULL,NULL,fname,CPL_IO_DEFAULT);
1043 double rms =cpl_table_get_column_stdev(debug_fit,
"DIF");
1044 xsh_msg(
"iter=%d idx=%d rms=%g",iter,idx,rms);
1047 if ( fout ) fclose( fout ) ;
1048 return cpl_error_get_code();
1055 int x_min_offset = +1;
1066 int x_max_offset = -1;
1150 fout = fopen(fname,
"w");
1151 int nrow = cpl_table_get_nrow(tab);
1154 x=cpl_table_get_data_double(tab,
"X");
1155 y=cpl_table_get_data_double(tab,
"Y");
1156 fprintf(fout,
"# x y\n");
1159 for(
int i=0;i<nrow;i++) {
1160 fprintf(fout,
"x point (%g %g) #color=green \n",
x[i]+1,
y[i]+1);
1170 for(
int i=0;i<nrow;i++) {
1171 fprintf(fout,
"circle point (%g %g) #color=red \n",
x[i]+1,
y[i]+1);
1180 for(
int i=0;i<nrow;i++) {
1181 fprintf(fout,
"diamond point (%g %g) #color=cyan \n",
x[i]+1,
y[i]+1);
1190 for(
int i=0;i<nrow;i++) {
1191 fprintf(fout,
"cross point (%g %g) #color=black \n",
x[i]+1,
y[i]+1);
1200 for(
int i=0;i<nrow;i++) {
1201 fprintf(fout,
"point (%g %g) #color=red \n",
x[i]+1,
y[i]+1);
1210 for(
int i=0;i<nrow;i++) {
1211 fprintf(fout,
"box point (%g %g) #color=blue \n",
x[i]+1,
y[i]+1);
1220 for(
int i=0;i<nrow;i++) {
1221 fprintf(fout,
"point (%g %g) #color=yellow \n",
x[i]+1,
y[i]+1);
1222 fprintf( fout,
"point(%g,%g) #point=cross color=yellow "\
1223 "font=\"helvetica 10 normal\" text={%5.1f %5.1f}\n",
1224 x[i]+1,
y[i]+1,
x[i]+1,
y[i]+1);
1229 if ( fout ) fclose( fout ) ;
1239 double * w_mod = NULL;
1240 double * x_mod = NULL;
1241 double * y_mod = NULL;
1246 pentry_start = wlist->
list[iorder].
sky;
1247 int nsampl_line=2*nsampl_2+1;
1249 double wtol = 10.e-3;
1250 int nsampl=nsampl_line*nref;
1253 cpl_table* sampl_tab=cpl_table_new(nsampl);
1254 cpl_table_new_column(sampl_tab,
"WAVELENGTH",CPL_TYPE_DOUBLE);
1255 cpl_table_new_column(sampl_tab,
"X",CPL_TYPE_DOUBLE);
1256 cpl_table_new_column(sampl_tab,
"Y",CPL_TYPE_DOUBLE);
1257 cpl_table_new_column(sampl_tab,
"ORDER",CPL_TYPE_INT);
1258 cpl_table_fill_column_window_double(sampl_tab,
"WAVELENGTH",0,nsampl,0.);
1259 cpl_table_fill_column_window_double(sampl_tab,
"X",0,nsampl,0.);
1260 cpl_table_fill_column_window_double(sampl_tab,
"Y",0,nsampl,0.);
1261 cpl_table_fill_column_window_int(sampl_tab,
"ORDER",0,nsampl,0.);
1262 w_mod=cpl_table_get_data_double(sampl_tab,
"WAVELENGTH");
1263 x_mod=cpl_table_get_data_double(sampl_tab,
"X");
1264 y_mod=cpl_table_get_data_double(sampl_tab,
"Y");
1265 o_mod=cpl_table_get_data_int(sampl_tab,
"ORDER");
1271 double wdif_min_start=9999;
1272 double wdif_min=wdif_min_start;
1273 double wdif_max=-wdif_min_start;
1283 double lambda_margin=(pentry_start+k_step)->lambda-lambda_min;
1284 xsh_msg(
"Ref sky tab sampl points=%d sky data sampl points=%d",nref,nitem);
1285 for(
int i = 0; i < nref ; i++ ) {
1287 wdif_min=wdif_min_start;
1289 for(j = 0, pentry = pentry_start ; j < nitem ; j++, pentry++) {
1294 if( fabs( wave_ref[i] - pentry->
lambda ) <= wtol ) {
1299 wdif_tmp=fabs(wave_ref[i] - pentry->
lambda);
1300 if(wdif_tmp<wdif_min) {
1303 pentry_ref = pentry;
1318 for(k = -nsampl_2; k <= nsampl_2 &&
1319 (pentry_ref-k)->lambda > lambda_min+lambda_margin ; k+=k_step) {
1320 m=i*nsampl_line+k+nsampl_2;
1327 w_mod[
m]=(pentry_ref-k)->lambda;
1328 x_mod[
m]=(pentry_ref-k)->ix;
1329 y_mod[
m]=(pentry_ref-k)->iy;
1337 xsh_msg(
"wdif_min=%18.15g wdif_max=%18.15g",wdif_min,wdif_max);
1344 cpl_table_and_selected_double(sampl_tab,
"WAVELENGTH",CPL_LESS_THAN,200.);
1345 cpl_table_erase_selected(sampl_tab);
1346 cpl_table_and_selected_double(sampl_tab,
"WAVELENGTH",CPL_GREATER_THAN,3000.);
1347 cpl_table_erase_selected(sampl_tab);
1349 sprintf(rname,
"%s.reg",prefix);
1350 sprintf(tname,
"%s.fits",prefix);
1352 int nrow=cpl_table_get_nrow(sampl_tab);
1353 xsh_msg(
">>>>>order %d line sampling points %d",iorder,nrow);
1357 cpl_table_save(sampl_tab,NULL,NULL,tname,CPL_IO_DEFAULT);
1363 if(cpl_error_get_code() != CPL_ERROR_NONE) {
1472 double * w_mod = NULL;
1473 double * x_mod = NULL;
1474 double * y_mod = NULL;
1479 pentry_start = wlist->
list[iorder].
sky;
1485 cpl_table* sampl_tab=cpl_table_new(nrow);
1486 cpl_table_new_column(sampl_tab,
"WAVELENGTH",CPL_TYPE_DOUBLE);
1487 cpl_table_new_column(sampl_tab,
"X",CPL_TYPE_DOUBLE);
1488 cpl_table_new_column(sampl_tab,
"Y",CPL_TYPE_DOUBLE);
1489 cpl_table_new_column(sampl_tab,
"ORDER",CPL_TYPE_INT);
1490 cpl_table_fill_column_window_double(sampl_tab,
"WAVELENGTH",0,nrow,0.);
1491 cpl_table_fill_column_window_double(sampl_tab,
"X",0,nrow,0.);
1492 cpl_table_fill_column_window_double(sampl_tab,
"Y",0,nrow,0.);
1493 cpl_table_fill_column_window_int(sampl_tab,
"ORDER",0,nrow,0.);
1494 w_mod=cpl_table_get_data_double(sampl_tab,
"WAVELENGTH");
1495 x_mod=cpl_table_get_data_double(sampl_tab,
"X");
1496 y_mod=cpl_table_get_data_double(sampl_tab,
"Y");
1497 o_mod=cpl_table_get_data_int(sampl_tab,
"ORDER");
1502 sprintf(rname,
"%s.reg",prefix);
1503 sprintf(tname,
"%s.fits",prefix);
1511 pentry=pentry_start;
1514 x_mod[0]=pentry->
ix;
1515 y_mod[0]=pentry->
iy;
1518 w_mod[nrow-1]=(pentry+nitem-1)->lambda;
1519 x_mod[nrow-1]=(pentry+nitem-1)->ix;
1520 y_mod[nrow-1]=(pentry+nitem-1)->iy;
1521 o_mod[nrow-1]=iorder;
1522 xsh_msg(
"edges=%g %g",pentry->
lambda,(pentry+nitem-1)->lambda);
1523 for(
int i = 1; i < nrow-1 ; i++ ) {
1525 for(j = 0, pentry = pentry_start ; j < nitem ; j++, pentry++) {
1526 if( fabs( wave - pentry->
lambda ) <= wtol ) {
1527 w_mod[i]=(pentry-k)->lambda;
1528 x_mod[i]=(pentry-k)->ix;
1529 y_mod[i]=(pentry-k)->iy;
1539 if(cpl_error_get_code() != CPL_ERROR_NONE) {
1635static cpl_error_code
1637 const double ron2,
const double gain,
1642 cpl_image* sky_rms = NULL;
1643 cpl_image* sky_dif = NULL;
1644 cpl_image* sky_rat = NULL;
1645 float* psky_rms =NULL;
1646 float* psky_dif =NULL;
1647 float* psky_rat =NULL;
1656 xsh_msg(
"Image size:%d,%d", sx, sy ) ;
1660 psky_rms = cpl_image_get_data_float(sky_rms);
1663 psky_dif = cpl_image_get_data_float(sky_dif);
1666 psky_rat = cpl_image_get_data_float(sky_rat);
1681 for (
int k = 0; k < sky_size; k++) {
1687 sigma2 =ron2+(fabs(fit)/gain);
1694 psky_rat[pix]=dif/flux;
1699 sprintf(fname,
"sky_model_rms_%2.2d.fits",iter);
1700 cpl_image_save(sky_rms, fname, CPL_TYPE_FLOAT, NULL, CPL_IO_DEFAULT);
1701 sprintf(fname,
"sky_model_dif_%2.2d.fits",iter);
1702 cpl_image_save(sky_dif, fname, CPL_TYPE_FLOAT, NULL, CPL_IO_DEFAULT);
1703 sprintf(fname,
"sky_model_rat_%2.2d.fits",iter);
1704 cpl_image_save(sky_rat, fname, CPL_TYPE_FLOAT, NULL, CPL_IO_DEFAULT);
1711 return cpl_error_get_code() ;
1716 const double ron2,
const double gain)
1730 double line_ratio=0;
1732 psky_start = wlist->
list[iorder].
sky;
1733 for(i = 0, psky = psky_start ; i < nitem ; i++, psky++) {
1734 if( ( ( psky->
qual & decode_bp ) == 0) ) {
1738 sigma2 =ron2+(fabs(fit)/gain);
1740 line_ratio += fabs(arg/flux);
1749 xsh_msg(
"Average spectrum ratio: %g",line_ratio);
1769 for(i = 0, psky = psky_start ; i < nitem ; i++, psky++) {
1770 if( ( ( psky->
qual & decode_bp ) == 0) ) {
1775 arg = (flux-fit)/
sigma;
1790 cpl_table* tab_out=NULL;
1794 tab_out=cpl_table_duplicate(tab_inp);
1795 int nrow=cpl_table_get_nrow(tab_out);
1796 pcol_w=cpl_table_get_data_double(tab_inp,col_wav);
1798 nrow=cpl_table_get_nrow(tab_out);
1800 for(
int i=1;i<nrow;i++) {
1801 if(pcol_w[i]==pcol_w[i-1]) {
1803 cpl_table_set_column_invalid(tab_out,col_wav,i,1);
1807 cpl_table_erase_invalid(tab_out);
1808 nrow=cpl_table_get_nrow(tab_out);
1816 const int ord,
const int n){
1821 double* wave=cpl_calloc(
n,
sizeof(
double));
1822 double wstep=(wmax-wmin)/
n;
1823 xsh_msg(
"order=%d wmin=%g wmax=%g",ord,wmin,wmax);
1824 for(
int i = 0; i <
n ; i+=i_step ) {
1825 wave[i]=wmin+i*wstep;
1828 sprintf(tname,
"points_sampl_cont_%02d", ord);
1832 int nrow=cpl_table_get_nrow(ctab);
1833 xsh_msg(
"Total sampling points sky continuum on order %d(%d) is =%d",
1843 const cpl_table* tab_ord,
const int ord)
1845 int hsampl_line=169;
1846 cpl_table* xtab=cpl_table_duplicate(tab_ord);
1847 int nrow = cpl_table_get_nrow(tab_ord);
1849 xsh_msg(
"Total number of sky lines in reference table on order %d is =%d",ord,nrow);
1850 cpl_table_cast_column(xtab,
"FLUX",
"DFLUX",CPL_TYPE_DOUBLE);
1851 cpl_table_cast_column(xtab,
"WAVELENGTH",
"DWAVE",CPL_TYPE_DOUBLE);
1852 double* wref = cpl_table_get_data_double(xtab,
"DWAVE");
1854 sprintf(tname,
"points_sampl_lines_%02d", ord);
1856 nrow=cpl_table_get_nrow(ltab);
1857 xsh_msg(
"Total sampling points ref sky line on order %d is =%d",ord,nrow);
1870res=cpl_table_new(sky_ndata);
1871cpl_table_new_column(res,
"WAVE",CPL_TYPE_DOUBLE);
1872cpl_table_new_column(res,
"FLUX",CPL_TYPE_DOUBLE);
1873cpl_table_fill_column_window_double(res,
"WAVE",0,sky_ndata,0.);
1874cpl_table_fill_column_window_double(res,
"FLUX",0,sky_ndata,0.);
1875double* pwave=cpl_table_get_data_double(res,
"WAVE");
1876double* pflux=cpl_table_get_data_double(res,
"FLUX");
1879int check_data_quality=0;
1882for(
int i=0;i<sky_ndata;i+=i_step,psky+=i_step) {
1887 check_data_quality = (( ( psky->
qual & decode_bp ) == 0 ) ||
1892 if( check_data_quality == 0) {
1895 pflux[i]=psky->
flux;
1897 cpl_table_set_invalid(res,
"WAVE",i);
1902xsh_msg(
"elements before check %" CPL_SIZE_FORMAT ,cpl_table_get_nrow(res));
1903xsh_msg(
"invalid elements %" CPL_SIZE_FORMAT ,cpl_table_count_invalid(res,
"WAVE"));
1904cpl_table_erase_invalid(res);
1905cpl_table_and_selected_double(res,
"WAVE",CPL_EQUAL_TO,0);
1907cpl_table_erase_selected(res);
1908xsh_msg(
"elements after check %" CPL_SIZE_FORMAT ,cpl_table_get_nrow(res));
1917 cpl_table* ltab=NULL;
1941 cpl_table* xtab=NULL;
1943 int nrow=cpl_table_get_nrow(dtab);
1950 cpl_table_duplicate_column(dtab,
"FLUX_SMOOTH",dtab,
"FLUX");
1951 cpl_vector* v_flux=NULL;
1952 cpl_vector* v_smooth=NULL;
1953 v_flux = cpl_vector_wrap(nrow,cpl_table_get_data_double(dtab,
"FLUX"));
1954 v_smooth=cpl_vector_filter_lowpass_create(v_flux,CPL_LOWPASS_LINEAR,hsize);
1955 cpl_vector_unwrap(v_flux);
1956 double* pts=cpl_table_get_data_double(dtab,
"FLUX_SMOOTH");
1957 double* pvs=cpl_vector_get_data(v_smooth);
1958 for (
int i = 0; i < nrow; i++) {
1961 cpl_vector_delete(v_smooth);
1963 cpl_table_save(dtab,NULL,NULL,
"tab_sel.fits",CPL_IO_DEFAULT);
1973 double med=cpl_table_get_column_median(dtab,
"FLUX");
1974 double rms=cpl_table_get_column_stdev(dtab,
"FLUX");
1975 cpl_table_and_selected_double(dtab,
"FLUX",CPL_LESS_THAN,med+
kappa*rms);
1976 cpl_table_and_selected_double(dtab,
"FLUX",CPL_GREATER_THAN,med-
kappa*rms);
1977 xtab=cpl_table_extract_selected(dtab);
1978 med=cpl_table_get_column_median(xtab,
"FLUX");
1979 rms=cpl_table_get_column_stdev(xtab,
"FLUX");
1981 for(
int i=0;i<
niter;i++){
1982 cpl_table_select_all(dtab);
1983 cpl_table_and_selected_double(dtab,
"FLUX",CPL_LESS_THAN,med+
kappa*rms);
1984 cpl_table_and_selected_double(dtab,
"FLUX",CPL_GREATER_THAN,med-
kappa*rms);
1985 cpl_table* xtab=cpl_table_extract_selected(dtab);
1986 med=cpl_table_get_column_median(xtab,
"FLUX");
1987 rms=cpl_table_get_column_stdev(xtab,
"FLUX");
1988 xsh_msg(
"med=%g rms=%g",med,rms);
1991 nrow=cpl_table_get_nrow(dtab);
1992 cpl_table_new_column(dtab,
"FLAG",CPL_TYPE_INT);
1993 cpl_table_fill_column_window_int(dtab,
"FLAG",1,nrow,0);
1994 int* pflag=cpl_table_get_data_int(dtab,
"FALG");
1995 double* pflux=cpl_table_get_data_double(dtab,
"FLUX");
1996 for(
int i=0;i<nrow;i++){
1997 if( pflux[i] < med-
kappa*rms &&
1998 pflux[i] > med+
kappa*rms ) {
2002 cpl_table_save(dtab,NULL,NULL,
"tab_sel.fits",CPL_IO_DEFAULT);
2008 const int nbkpts_ord,
2009 const cpl_table* bkpts_tab)
2011 cpl_table* rtab=NULL;
2012 cpl_table* ltab=NULL;
2013 cpl_table* ctab=NULL;
2022 xsh_msg(
"sky_slit_size=%g",sky_slit_size);
2031 nrow=cpl_table_get_nrow(ltab);
2032 cpl_table_insert(ltab,ctab,nrow);
2035 nrow=cpl_table_get_nrow(ltab);
2037 cpl_table_and_selected_double(ltab,
"WAVELENGTH",CPL_EQUAL_TO,0);
2038 cpl_table_erase_selected(ltab);
2044 sprintf(rname,
"points_sampl_order_%02d.reg", ord);
2048 cpl_table_erase_column(rtab,
"ORDER");
2049 nrow=cpl_table_get_nrow(rtab);
2051 xsh_msg(
"Total sampling points for sky spectrum on order %d is =%d",ord,nrow);
2056 if(cpl_error_get_code() != CPL_ERROR_NONE) {
2438 const double ron2,
const double gain,
2439 const int order,
const int iter,
2440 cpl_table** tab_line_res)
2446 cpl_table* tab_bad_fit=cpl_table_new(sky_ndata);
2448 cpl_table_new_column(tab_bad_fit,
"WAVE",CPL_TYPE_DOUBLE);
2449 cpl_table_new_column(tab_bad_fit,
"FLUX",CPL_TYPE_FLOAT);
2450 cpl_table_new_column(tab_bad_fit,
"FIT",CPL_TYPE_DOUBLE);
2451 cpl_table_new_column(tab_bad_fit,
"SIGMA",CPL_TYPE_DOUBLE);
2452 cpl_table_new_column(tab_bad_fit,
"X",CPL_TYPE_DOUBLE);
2453 cpl_table_new_column(tab_bad_fit,
"Y",CPL_TYPE_DOUBLE);
2454 cpl_table_new_column(tab_bad_fit,
"FLAG",CPL_TYPE_INT);
2456 cpl_table_fill_column_window_double(tab_bad_fit,
"WAVE", 0, sky_ndata, 0);
2457 cpl_table_fill_column_window_float(tab_bad_fit,
"FLUX", 0, sky_ndata, 0);
2458 cpl_table_fill_column_window_double(tab_bad_fit,
"FIT", 0, sky_ndata, 0);
2459 cpl_table_fill_column_window_double(tab_bad_fit,
"SIGMA", 0, sky_ndata, 0);
2460 cpl_table_fill_column_window_double(tab_bad_fit,
"X", 0, sky_ndata, 0);
2461 cpl_table_fill_column_window_double(tab_bad_fit,
"Y", 0, sky_ndata, 0);
2462 cpl_table_fill_column_window_int(tab_bad_fit,
"FLAG", 0, sky_ndata, 0);
2464 double* pbwav=cpl_table_get_data_double(tab_bad_fit,
"WAVE");
2465 float* pbflux=cpl_table_get_data_float(tab_bad_fit,
"FLUX");
2466 double* pbfit=cpl_table_get_data_double(tab_bad_fit,
"FIT");
2467 double* pbsigma=cpl_table_get_data_double(tab_bad_fit,
"SIGMA");
2468 double* pbx=cpl_table_get_data_double(tab_bad_fit,
"X");
2469 double* pby=cpl_table_get_data_double(tab_bad_fit,
"Y");
2470 int* pflag=cpl_table_get_data_int(tab_bad_fit,
"FLAG");
2474 xsh_msg(
"kappa2=%g ron2=%g gain=%g",kappa2,ron2,gain);
2476 for (
int i = 0 ; i<sky_ndata ; i++, psky++ ) {
2478 double tmp_err = psky->
sigma;
2480 double tmp_var = tmp_err * tmp_err;
2481 int check_data_value = (( tmp_val * tmp_val ) < kappa2 * tmp_var) ? 0:1;
2490 if( check_data_value == 1){
2495 pbwav[nbad]=psky->
lambda;
2496 pbflux[nbad]=psky->
flux;
2497 pbfit[nbad]=psky->
fitted;
2499 pbsigma[nbad]=tmp_err;
2509 cpl_table_set_size(tab_bad_fit,nbad);
2510 xsh_msg(
"Flagging sky line residuals order %d iter %d ndata %d nbad_thresh %d",
2511 order,iter,sky_ndata,nbad);
2520 sprintf(bname,
"bad_fit_thres_ord_%2.2d_%2.2d.fits",
order,iter);
2522 sprintf(bname,
"bad_fit_thres_ord_%2.2d_%2.2d.reg",
order,iter);
2525 int nsel=cpl_table_and_selected_int(tab_bad_fit,
"FLAG",CPL_EQUAL_TO,1);
2526 cpl_table_name_column(tab_bad_fit,
"WAVE",
"WAVELENGTH");
2531 double hmin=cpl_table_get_column_min(tab_bad_fit,
"WAVELENGTH");
2532 double hmax=cpl_table_get_column_max(tab_bad_fit,
"WAVELENGTH");
2534 int nbins=(int)(hmax-hmin);
2538 double hstp=(hmax-hmin)/(nbins-1);
2540 cpl_table* h =
xsh_histogram(tab_bad_fit,
"WAVELENGTH",nbins,hmin,hmax);
2544 double histo_mean=0;
2545 double histo_median=0;
2546 double histo_stdev=0;
2548 histo_min=cpl_table_get_column_min(h,
"HY");
2549 histo_max=cpl_table_get_column_max(h,
"HY");
2551 histo_mean=cpl_table_get_column_mean(h,
"HY");
2552 histo_median=cpl_table_get_column_median(h,
"HY");
2553 histo_stdev=cpl_table_get_column_stdev(h,
"HY");
2554 xsh_msg(
"histo info: min=%g max=%g mean=%g median=%g,stdev=%g",
2555 histo_min,histo_max,histo_mean,histo_median,histo_stdev);
2558 int* phy=cpl_table_get_data_int(h,
"HY");
2559 double* phw=cpl_table_get_data_double(h,
"HL");
2560 double* pbw=cpl_table_get_data_double(tab_bad_fit,
"WAVELENGTH");
2561 pflag=cpl_table_get_data_int(tab_bad_fit,
"FLAG");
2562 pbx=cpl_table_get_data_double(tab_bad_fit,
"X");
2563 pby=cpl_table_get_data_double(tab_bad_fit,
"Y");
2564 *tab_line_res = cpl_table_new(nbins);
2565 cpl_table_new_column(*tab_line_res,
"WAVELENGTH",CPL_TYPE_FLOAT);
2566 cpl_table_new_column(*tab_line_res,
"FLUX",CPL_TYPE_FLOAT);
2568 cpl_table_fill_column_window_float(*tab_line_res,
"WAVELENGTH", 0, nbad, 0);
2569 cpl_table_fill_column_window_float(*tab_line_res,
"FLUX", 0, nbad, 999);
2570 float* plwav=cpl_table_get_data_float(*tab_line_res,
"WAVELENGTH");
2573 sprintf(bname,
"residual_lines_thres_ord_%2.2d_%2.2d.reg",
order,iter);
2575 fout = fopen(bname,
"w");
2578 for(
int i=0;i<nbins;i++) {
2579 if(phy[i]>histo_median+
kappa*histo_stdev) {
2581 for(
int j=0;j<nbad;j++) {
2582 if( pbw[j] > phw[i] - hstp &&
2583 pbw[j] < phw[i] + hstp ) {
2586 fprintf(fout,
"x point (%g %g) #color=green \n",
2598 cpl_table_set_size(*tab_line_res,k);
2599 cpl_table_set_column_unit(*tab_line_res,
"WAVELENGTH",
"nm");
2600 cpl_table_set_column_unit(*tab_line_res,
"FLUX",
"rel-flux");
2601 xsh_msg(
"Flagging outliers found %d line outliers",k);
2605 sprintf(hname,
"histo_ord_%2.2d_%2.2d.fits",
order,iter);
2609 cpl_table_erase_column(tab_bad_fit,
"FLUX");
2610 cpl_table_erase_column(tab_bad_fit,
"FIT");
2611 cpl_table_erase_column(tab_bad_fit,
"SIGMA");
2613 cpl_table_and_selected_double(tab_bad_fit,
"WAVELENGTH",CPL_EQUAL_TO,0);
2614 cpl_table_erase_selected(tab_bad_fit);
2802static cpl_error_code
2804 const float ron2,
const float gain){
2810 double* pwave=cpl_table_get_data_double(stab,
"WAVE");
2811 double* pfit=cpl_table_get_data_double(stab,
"FIT");
2815 int nrow=cpl_table_get_nrow(stab);
2818 cpl_table_dump_structure(stab,stdout);
2819 for(
int i=0;i<sky_ndata;i++,psky++) {
2820 for(
int j=j_min;j<nrow;j++) {
2821 if(pwave[j]==psky->
lambda) {
2833 for(
int i=0;i<all_ndata;i++,pall++) {
2834 for(
int j=j_min;j<nrow;j++) {
2835 if(pwave[j]==pall->
lambda) {
2846 return cpl_error_get_code();
2850static cpl_error_code
2852 const double s2,cpl_table* stab,
2853 const float ron2,
const float gain){
2860 double* pwave=cpl_table_get_data_double(stab,
"WAVE");
2861 double* pfit=cpl_table_get_data_double(stab,
"FIT");
2866 int nrow=cpl_table_get_nrow(stab);
2869 cpl_table_dump_structure(stab,stdout);
2871 for(
int i=0;i<all_ndata;i++,pall++) {
2873 for(
int j=0;j<sky_ndata;j++,psky++) {
2883 for(
int i=0;i<all_ndata;i++,pall++) {
2884 for(
int j=j_min;j<nrow;j++) {
2885 if(pwave[j]==pall->
lambda && pall->
slit >= s1 && pall->
slit<=s2) {
2895 return cpl_error_get_code();
2898static cpl_error_code
2900 const float ron2,
const float gain){
2904 double* pwave=cpl_table_get_data_double(stab,
"WAVE");
2905 double* pfit=cpl_table_get_data_double(stab,
"BFIT");
2908 int nrow=cpl_table_get_nrow(stab);
2911 for(
int i=0;i<sky_ndata;i++,psky++) {
2912 for(
int j=j_min;j<nrow;j++) {
2913 if(pwave[j]==psky->
lambda) {
2924 for(
int i=0;i<all_ndata;i++,pall++) {
2925 for(
int j=j_min;j<nrow;j++) {
2926 if(pwave[j]==pall->
lambda) {
2936 return cpl_error_get_code();
2940static cpl_error_code
2942 const double s1,
const double s2, cpl_table* stab,
2943 const float ron2,
const float gain){
2947 double* pwave=cpl_table_get_data_double(stab,
"WAVE");
2948 double* pfit=cpl_table_get_data_double(stab,
"FIT");
2951 int nrow=cpl_table_get_nrow(stab);
2954 for(
int i=0;i<sky_ndata;i++,psky++) {
2955 for(
int j=j_min;j<nrow;j++) {
2956 if(pwave[j]==psky->
lambda &&
2969 for(
int i=0;i<all_ndata;i++,pall++) {
2970 for(
int j=j_min;j<nrow;j++) {
2971 if(pwave[j]==pall->
lambda) {
2980 return cpl_error_get_code();
2984static cpl_error_code
2986 const double s1,
const double s2, cpl_table* stab,
2987 const float ron2,
const float gain){
2991 double* pwave=cpl_table_get_data_double(stab,
"WAVE");
2992 double* pfit=cpl_table_get_data_double(stab,
"BFIT");
2995 int nrow=cpl_table_get_nrow(stab);
2998 for(
int i=0;i<sky_ndata;i++,psky++) {
2999 for(
int j=j_min;j<nrow;j++) {
3000 if(pwave[j]==psky->
lambda &&
3013 for(
int i=0;i<all_ndata;i++,pall++) {
3014 for(
int j=j_min;j<nrow;j++) {
3015 if(pwave[j]==pall->
lambda &&
3026 return cpl_error_get_code();
3032 const int i_start,
const size_t n,
const size_t ncoeffs) {
3034 const size_t nbreak = ncoeffs-2;
3036 gsl_bspline_workspace *bw;
3042 gsl_matrix *X, *cov;
3043 gsl_multifit_linear_workspace *mw;
3050 gsl_rng_env_setup();
3051 r = gsl_rng_alloc(gsl_rng_default);
3054 bw = gsl_bspline_alloc(4, nbreak);
3055 B = gsl_vector_alloc(ncoeffs);
3057 x = gsl_vector_alloc(
n);
3058 y = gsl_vector_alloc(
n);
3059 X = gsl_matrix_alloc(
n, ncoeffs);
3060 c = gsl_vector_alloc(ncoeffs);
3061 w = gsl_vector_alloc(
n);
3062 cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
3063 mw = gsl_multifit_linear_alloc(
n, ncoeffs);
3068 for (i = 0; i <
n; ++i)
3073 gsl_vector_set(
x, i, data_x[i]);
3074 gsl_vector_set(
y, i, data_y[i]);
3081 gsl_bspline_knots_uniform(data_x[0], data_x[
n-1], bw);
3084 for (i = 0; i <
n; ++i)
3086 double xi = gsl_vector_get(
x, i);
3089 gsl_bspline_eval(xi, B, bw);
3092 for (j = 0; j < ncoeffs; ++j)
3094 double Bj = gsl_vector_get(B, j);
3095 gsl_matrix_set(X, i, j, Bj);
3100 gsl_multifit_wlinear(X, w,
y, c, cov, &chisq, mw);
3117 for (i = 0; i <
n; i++)
3119 gsl_bspline_eval(data_x[i], B, bw);
3120 gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
3128 gsl_bspline_free(bw);
3135 gsl_matrix_free(cov);
3136 gsl_multifit_linear_free(mw);
3144 double* bkpts,
double* yfit,
3145 const int i_start,
const size_t n,
const size_t ncoeffs) {
3147 const size_t nbreak = ncoeffs-2;
3149 gsl_bspline_workspace *bw;
3155 gsl_matrix *X, *cov;
3156 gsl_multifit_linear_workspace *mw;
3162 gsl_rng_env_setup();
3163 r = gsl_rng_alloc(gsl_rng_default);
3166 bw = gsl_bspline_alloc(4, nbreak);
3168 B = gsl_vector_alloc(ncoeffs);
3169 gsl_vector* bkpts_v = gsl_vector_alloc(nbreak);
3170 x = gsl_vector_alloc(
n);
3171 y = gsl_vector_alloc(
n);
3172 X = gsl_matrix_alloc(
n, ncoeffs);
3173 c = gsl_vector_alloc(ncoeffs);
3174 w = gsl_vector_alloc(
n);
3175 cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
3176 mw = gsl_multifit_linear_alloc(
n, ncoeffs);
3185 bkpts[nbreak-1]=data_x[
n-1];
3186 for (i = 0; i < nbreak; ++i){
3187 gsl_vector_set(bkpts_v, i, bkpts[i]);
3192 for (i = 0; i <
n; ++i)
3197 gsl_vector_set(
x, i, data_x[i]);
3198 gsl_vector_set(
y, i, data_y[i]);
3212 gsl_bspline_knots(bkpts_v, bw);
3217 for (i = 0; i <
n; ++i)
3220 double xi = gsl_vector_get(
x, i);
3228 gsl_bspline_eval(xi, B, bw);
3231 for (j = 0; j < ncoeffs; ++j)
3233 double Bj = gsl_vector_get(B, j);
3234 gsl_matrix_set(X, i, j, Bj);
3240 gsl_multifit_wlinear(X, w,
y, c, cov, &chisq, mw);
3255 for (i = 0; i <
n; i++)
3257 gsl_bspline_eval(data_x[i], B, bw);
3258 gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
3265 gsl_bspline_free(bw);
3272 gsl_matrix_free(cov);
3275 gsl_multifit_linear_free(mw);
3281 const double s1,
const double s2){
3284 int nrow=cpl_table_get_nrow(*tab);
3287 int npart=(int)(nrow/nchunks+0.5);
3288 double* pwave=cpl_table_get_data_double(*tab,
"WAVE");
3290 double* perr=cpl_table_get_data_double(*tab,
"ERR");
3291 double* pbfit=cpl_table_get_data_double(*tab,
"BFIT");
3292 cpl_table* xtab=NULL;
3297 for(
int i=0;i<=nchunks-1;i++) {
3299 i_max=((i+1)*npart<nrow) ? (i+1)*npart: nrow-1;
3300 cpl_table_and_selected_double(*tab,
"SLIT",CPL_NOT_LESS_THAN,s1);
3301 cpl_table_and_selected_double(*tab,
"SLIT",CPL_LESS_THAN,s2);
3302 cpl_table_and_selected_double(*tab,
"WAVE",CPL_NOT_LESS_THAN,pwave[i_min]);
3303 cpl_table_and_selected_double(*tab,
"WAVE",CPL_LESS_THAN,pwave[i_max]);
3304 xtab=cpl_table_extract_selected(*tab);
3306 npoints=cpl_table_get_nrow(xtab);
3308 double* pw=cpl_table_get_data_double(xtab,
"WAVE");
3309 double* pf=cpl_table_get_data_double(xtab,
"FLUX_SMOOTH");
3313 xsh_msg(
"nscan=%d",i_max-i_min+1);
3315 cpl_table_save(*tab,NULL,NULL,
"pippo.fits",CPL_IO_DEFAULT);
3317 cpl_table_select_all(*tab);
3320 cpl_table_save(*tab,NULL,NULL,
"pippo.fits",CPL_IO_DEFAULT);
3322 return cpl_error_get_code();
3329 int nrow=cpl_table_get_nrow(*tab);
3331 cpl_table_new_column(*tab,
"BFIT",CPL_TYPE_DOUBLE);
3332 cpl_table_fill_column_window_double(*tab,
"BFIT",0,nrow,0);
3333 int npart=(int)(nrow/nchunks+0.5);
3334 double* pwave=cpl_table_get_data_double(*tab,
"WAVE");
3336 double* perr=cpl_table_get_data_double(*tab,
"ERR");
3337 double* pbfit=cpl_table_get_data_double(*tab,
"BFIT");
3338 cpl_table* xtab=NULL;
3344 for(
int i=0;i<=nchunks-1;i++) {
3346 i_max=((i+1)*npart<nrow) ? (i+1)*npart: nrow-1;
3347 cpl_table_and_selected_double(*tab,
"WAVE",CPL_NOT_LESS_THAN,pwave[i_min]);
3348 cpl_table_and_selected_double(*tab,
"WAVE",CPL_LESS_THAN,pwave[i_max]);
3349 xtab=cpl_table_extract_selected(*tab);
3350 npoints=cpl_table_get_nrow(xtab);
3352 double* pw=cpl_table_get_data_double(xtab,
"WAVE");
3353 double* pf=cpl_table_get_data_double(xtab,
"FLUX_SMOOTH");
3364 if( i_max-i_min+1 <= nbkpts) {
3365 nbkpts=i_max-i_min-1;
3373 cpl_table_select_all(*tab);
3378 return cpl_error_get_code();
3382static cpl_error_code
3383xsh_bspline_smooth_non_uniform(cpl_table* bkpts, cpl_table** tab,
3387 int nrow=cpl_table_get_nrow(*tab);
3389 cpl_table_new_column(*tab,
"BFIT",CPL_TYPE_DOUBLE);
3390 cpl_table_fill_column_window_double(*tab,
"BFIT",0,nrow,0);
3391 int npart=(int)(nrow/nchunks+0.5);
3392 double* pwave=cpl_table_get_data_double(*tab,
"WAVE");
3393 double* pflux=cpl_table_get_data_double(*tab,
"FLUX_SMOOTH");
3394 double* perr=cpl_table_get_data_double(*tab,
"ERR");
3395 double* pbfit=cpl_table_get_data_double(*tab,
"BFIT");
3396 cpl_table* xtab=NULL;
3397 cpl_table* btab=NULL;
3403 for(
int i=0;i<=nchunks-1;i++) {
3405 i_max=((i+1)*npart<nrow) ? (i+1)*npart: nrow-1;
3407 cpl_table_and_selected_double(bkpts,
"WAVELENGTH",CPL_NOT_LESS_THAN,pwave[i_min]);
3408 cpl_table_and_selected_double(bkpts,
"WAVELENGTH",CPL_LESS_THAN,pwave[i_max]);
3409 btab=cpl_table_extract_selected(bkpts);
3410 int nbkpts=cpl_table_get_nrow(btab);
3411 double* pbkpts=cpl_table_get_data_double(btab,
"WAVELENGTH");
3415 cpl_table_and_selected_double(*tab,
"WAVE",CPL_NOT_LESS_THAN,pwave[i_min]);
3416 cpl_table_and_selected_double(*tab,
"WAVE",CPL_LESS_THAN,pwave[i_max]);
3417 xtab=cpl_table_extract_selected(*tab);
3418 npoints=cpl_table_get_nrow(xtab);
3421 double* pw=cpl_table_get_data_double(xtab,
"WAVE");
3422 double* pf=cpl_table_get_data_double(xtab,
"FLUX_SMOOTH");
3423 double* pbf=cpl_table_get_data_double(xtab,
"BFIT");
3425 cpl_table_save(xtab,NULL,NULL,
"pippo_xtab.fits",CPL_IO_DEFAULT);
3426 cpl_table_save(btab,NULL,NULL,
"pippo_btab.fits",CPL_IO_DEFAULT);
3436 cpl_table_save(*tab,NULL,NULL,
"pippo.fits",CPL_IO_DEFAULT);
3437 cpl_table_select_all(*tab);
3438 cpl_table_select_all(bkpts);
3444 return cpl_error_get_code();
3448static cpl_error_code
3454 xsh_msg(
"Spline smooth of non uniformly sampled & chopped profile");
3455 const char* name=cpl_frame_get_filename(sky_orders_chunks);
3465 xsh_msg(
"name=%s iorder=%d order_nb=%d",name,iorder,order_nb);
3466 cpl_table* tab_chunks=cpl_table_load(name,order_nb-iorder,1);
3468 int nchunks=cpl_table_get_nrow(tab_chunks);
3470 int nrow=cpl_table_get_nrow(*tab);
3471 double* pchunk=cpl_table_get_data_double(tab_chunks,
"col1");
3474 cpl_table_new_column(*tab,
"BFIT",CPL_TYPE_DOUBLE);
3475 cpl_table_fill_column_window_double(*tab,
"BFIT",0,nrow,0);
3476 int npart=(int)(nrow/nchunks+0.5);
3477 double* pwave=cpl_table_get_data_double(*tab,
"WAVE");
3479 double* perr=cpl_table_get_data_double(*tab,
"ERR");
3480 double* pbfit=cpl_table_get_data_double(*tab,
"BFIT");
3481 cpl_table* xtab=NULL;
3482 cpl_table* btab=NULL;
3485 double chunk_wmin=0;
3486 double chunk_wmax=0;
3492 for(
int i=0;i<nchunks-1;i++) {
3494 i_max=((i+1)*npart<nrow) ? (i+1)*npart: nrow-1;
3496 chunk_wmin=pchunk[i];
3497 chunk_wmax=pchunk[i+1];
3499 wmin = pwave[i_min];
3500 xsh_msg(
"order splitting wmin=%13.8g wmax=%13.8g",chunk_wmin,chunk_wmax);
3502 cpl_table_and_selected_double(bkpts,
"WAVELENGTH",CPL_NOT_LESS_THAN,wmin);
3503 cpl_table_and_selected_double(bkpts,
"WAVELENGTH",CPL_LESS_THAN,chunk_wmax);
3504 btab=cpl_table_extract_selected(bkpts);
3505 int nbkpts=cpl_table_get_nrow(btab);
3506 double* pbkpts=cpl_table_get_data_double(btab,
"WAVELENGTH");
3511 xsh_msg(
"wmin=%g chunk_wmin=%g pwave[i_min]=%g chunk_wmax=%g",
3512 wmin,chunk_wmin,pwave[i_min],chunk_wmax);
3514 cpl_table_and_selected_double(*tab,
"WAVE",CPL_NOT_LESS_THAN,wmin);
3515 cpl_table_and_selected_double(*tab,
"WAVE",CPL_LESS_THAN,chunk_wmax);
3516 xtab=cpl_table_extract_selected(*tab);
3517 npoints=cpl_table_get_nrow(xtab);
3520 double* pw=cpl_table_get_data_double(xtab,
"WAVE");
3521 double* pf=cpl_table_get_data_double(xtab,
"FLUX_SMOOTH");
3524 cpl_table_save(xtab,NULL,NULL,
"pippo_xtab.fits",CPL_IO_DEFAULT);
3525 cpl_table_save(btab,NULL,NULL,
"pippo_btab.fits",CPL_IO_DEFAULT);
3533 if( i_max-i_min+1 <= nbkpts) {
3534 nbkpts=i_max-i_min-1;
3544 cpl_table_save(*tab,NULL,NULL,
"pippo.fits",CPL_IO_DEFAULT);
3548 cpl_table_select_all(*tab);
3549 cpl_table_select_all(bkpts);
3555 return cpl_error_get_code();
3577 cpl_frame* sky_orders_chunks,
int sampl_pts_no,
int iter_no,
3579 float ron2,
float gain, cpl_table** tab_line_res)
3583 cpl_table* tab_bad_fit=NULL;
3593 cpl_table* tab_mod=NULL;
3598 cpl_table* tab_clean=NULL;
3608 ron2,gain,
order, iter_no, decode_bp);
3610 n=cpl_table_get_nrow(tab_clean);
3632 sprintf(sname,
"tab_bspline_smooth_ord_%2.2d_%2.2d.fits",abs_order,iter_no);
3634 sprintf(sname,
"tab_bspline_bkpts_ord_%2.2d_%2.2d.fits",abs_order,iter_no);
3636 n=cpl_table_get_nrow(xtab);
3637 xsh_msg(
"After removal wave duplicates %d",
n);
3642 sprintf(sname,
"tab_data_ord_%2.2d_%2.2d.fits",abs_order,iter_no);
3646 double* wave=cpl_table_get_data_double(xtab,
"WAVE");
3653 flux=cpl_table_get_data_double(xtab,
"BFIT");
3656 sprintf(sname,
"xtab_ord_%2.2d_%2.2d.fits",abs_order,iter_no);
3657 cpl_table_save(xtab,NULL,NULL,sname,CPL_IO_DEFAULT);
3661 cpl_table* mtab=NULL;
3666 cpl_table* stab=NULL;
3686 cpl_table_wrap_double(atab,swave,
"WAVE");
3695 cpl_table_wrap_double(atab,sflux,
"FIT");
3698 sprintf(sname,
"tab_sampl_ord_%2.2d_%2.2d.fits",abs_order,iter_no);
3699 cpl_table_save(atab,NULL,NULL,sname,CPL_IO_DEFAULT);
3711 gain,
order,iter_no,tab_line_res));
4083 cpl_frame* sky_orders_chunks,
int sampl_pts_no,
int iter_no,
4085 float ron2,
float gain, cpl_table** tab_line_res)
4089 cpl_table* tab_bad_fit=NULL;
4109 double s_step=(smax-smin)/ns;
4113 cpl_table* xtab=NULL;
4114 cpl_table* tab_clean=NULL;
4124 cpl_table* ttab=cpl_table_new(0);
4126 double s_margin=0.001;
4129 for(
s=smin, sid=0;
s<smax-0.0001;
s+=s_step,sid++) {
4130 xsh_msg(
"sid=%d start s=%10.8g smax=%10.8g splus=%10.8g",sid,
s,smax,
s+s_step);
4132 xsh_msg(
"sid=%d sky slit small",sid);
4135 s2=(
s+s_step < omin-s_margin ) ?
s+s_step: omin;
4142 }
else if (
s > omin &&
s < omax) {
4144 if(
s+s_step < omax+s_margin) {
4145 xsh_msg(
"sid=%d object slit continue",sid);
4148 xsh_msg(
"sid=%d obj slit special",sid);
4153 xsh_msg(
"sid=%d sky slit large",sid);
4155 s2 = (
s+s_step < smax) ?
s+s_step: smax;
4158 xsh_msg(
"sid=%d s1=%g s2=%g",sid,s1,s2);
4160 ron2, gain,
order, iter_no, decode_bp,s1,s2,sid);
4162 n=cpl_table_get_nrow(tab_clean);
4164 cpl_table_copy_structure(ttab,tab_clean);
4165 cpl_table_new_column(ttab,
"BFIT",CPL_TYPE_DOUBLE);
4187 cpl_table_insert(ttab,xtab,ntot);
4188 ntot += cpl_table_get_nrow(xtab);
4189 sprintf(sname,
"tab_bspline_smooth_ord_%2.2d_slice_%2.2d_iter_%2.2d.fits",
4190 abs_order,sid,iter_no);
4193 sprintf(sname,
"tab_bspline_bkpts_ord_%2.2d_slice_%2.2d_iter_%2.2d.fits",
4194 abs_order,sid,iter_no);
4197 n=cpl_table_get_nrow(xtab);
4198 xsh_msg(
"After removal wave duplicates %d",
n);
4201 sprintf(sname,
"tab_data_ord_%2.2d_slice_%2.2d_iter_%2.2d.fits",
4202 abs_order,sid,iter_no);
4207 sprintf(sname,
"model_ord_%2.2d_slice_%2.2d_iter_%2.2d.fits",
4208 abs_order,sid,iter_no);
4209 xsh_msg(
"order=%d sid=%d s1=%g s2=%g omin=%g omax=%g",
4210 order,sid,s1,s2,omin,omax);
4223 s1=(omin-s_step<smin)? smin: omin-s_step;
4224 s2=(omax+s_step>smax)? smax: omax+s_step;
4228 cpl_table_and_selected_double(ttab,
"SLIT",CPL_GREATER_THAN,s1);
4229 cpl_table_and_selected_double(ttab,
"SLIT",CPL_LESS_THAN,s2);
4230 xtab=cpl_table_extract_selected(ttab);
4235 cpl_table_save(utab,NULL,NULL,
"utab.fits",CPL_IO_DEFAULT);
4240 n=cpl_table_get_nrow(utab);
4241 double* flux=cpl_table_get_data_double(utab,
"BFIT");
4242 double* wave=cpl_table_get_data_double(utab,
"WAVE");
4243 wmin=cpl_table_get_column_min(utab,
"WAVE");
4244 wmax=cpl_table_get_column_max(utab,
"WAVE");
4256 cpl_table_wrap_double(otab,swave,
"WAVE");
4257 cpl_table_and_selected_double(otab,
"WAVE",CPL_NOT_LESS_THAN,wmin);
4258 cpl_table_and_selected_double(otab,
"WAVE",CPL_NOT_GREATER_THAN,wmax);
4259 cpl_table* stab=cpl_table_extract_selected(otab);
4260 cpl_table_save(stab,NULL,NULL,
"stab.fits",CPL_IO_DEFAULT);
4261 sno=cpl_table_get_nrow(stab);
4262 swave=cpl_table_get_data_double(stab,
"WAVE");
4265 check(cpl_table_wrap_double(stab,sflux,
"FIT"));
4268 sprintf(sname,
"tab_sampl_ord_%2.2d_%2.2d.fits",abs_order,iter_no);
4269 cpl_table_save(stab,NULL,NULL,sname,CPL_IO_DEFAULT);
4271 sprintf(sname,
"tab_sampl_ord_%2.2d_%2.2d.fits",abs_order,iter_no);
4282 sprintf(sname,
"tab_bspline_smooth_ord_%2.2d_iter_%2.2d.fits",
4285 sprintf(sname,
"tab_bspline_bkpts_ord_%2.2d_iter_%2.2d.fits",
4296 gain,
order,iter_no,tab_line_res));
4360static cpl_error_code
4363 const double smap_min,
4364 const double smap_max,
4365 double* obj_slit_min,
4366 double* obj_slit_max,
4367 double* sky_slit_min,
4368 double* sky_slit_max)
4371 double pos1 = sky_par->
pos1;
4372 double hheight1 = sky_par->
hheight1;
4373 double pos2 = sky_par->
pos2;
4374 double hheight2 = sky_par->
hheight2;
4375 double s2_min, s2_max, s1_min, s1_max;
4377 xsh_msg_warning(
"Start obj_slit_min=%g obj_slit_max=%g",*obj_slit_min,*obj_slit_max);
4378 xsh_msg_warning(
"pos1=%g hheight1=%g pos2=%g hheight2=%g",pos1,hheight1,pos2,hheight2);
4380 if ((hheight1 == 0) && (hheight2 == 0) && (list != NULL)) {
4381 *obj_slit_min = cpl_polynomial_eval_1d(list->
edglopoly, 600, NULL);
4382 *obj_slit_max = cpl_polynomial_eval_1d(list->
edguppoly, 600, NULL);
4384 *obj_slit_min, *obj_slit_max);
4387 *obj_slit_min, *obj_slit_max);
4389 s1_max=*obj_slit_min;
4390 s2_min=*obj_slit_max;
4396 *sky_slit_min = pos1 - hheight1;
4397 *sky_slit_max = pos1 + hheight1;
4398 s1_min=*sky_slit_min;
4399 s1_max=*sky_slit_max;
4405 s1_min = pos1 - hheight1;
4406 s1_max = pos1 + hheight1;
4408 s2_min = pos2 - hheight2;
4409 s2_max = pos2 + hheight2;
4411 if (s2_min < s1_min) {
4412 *sky_slit_min = s2_min;
4415 *sky_slit_min = s1_min;
4418 if (s2_max > s1_max) {
4419 *sky_slit_max = s2_max;
4422 *sky_slit_max = s1_max;
4425 if (s2_min >= s1_max) {
4426 *obj_slit_min = s1_max;
4427 *obj_slit_max = s2_min;
4430 if (s1_min >= s2_max) {
4431 *obj_slit_min = s2_max;
4432 *obj_slit_max = s1_min;
4435 xsh_msg(
"User Set sky window1: [%g,%g] window2=[%g,%g] arcsec",s1_min,s1_max,s2_min,s2_max);
4437 xsh_msg(
"User Set sky window: [%g,%g] arcsec",s1_min,s1_max);
4442 *sky_slit_min = pos2 - hheight2;
4443 *sky_slit_max = pos2 + hheight2;
4444 s2_min=*sky_slit_min;
4445 s2_max=*sky_slit_max;
4446 xsh_msg(
"User Set sky window: [%g,%g]",s2_min,s2_max);
4450 xsh_msg(
"Object window: [%g,%g]",*obj_slit_min,*obj_slit_max);
4452 return cpl_error_get_code();
4455static cpl_error_code
4457 const int miny,
const int maxy,
4458 const int iorder,
const int order,
4459 const int nx,
const int wmap_xsize_diff,
4462 double* sky_slit_min,
4463 double* sky_slit_max,
4464 int *max_size,
int*max_size_x)
4474 double* pslit = cpl_image_get_data_double( slitmap);
4475 double sky_slit_min_edge=0;
4476 double sky_slit_max_edge=0;
4477 for (
int iy = miny; iy < maxy; iy++) {
4481 int offset = iy * (
nx - wmap_xsize_diff);
4482 float slit_val = pslit[minx + offset];
4484 if (slit_val > sky_slit_max_edge) {
4485 sky_slit_max_edge = slit_val;
4487 if (slit_val < sky_slit_min_edge) {
4488 sky_slit_min_edge = slit_val;
4491 slit_val = pslit[maxx + offset];
4493 if (slit_val > sky_slit_max_edge) {
4494 sky_slit_max_edge = slit_val;
4496 if (slit_val < sky_slit_min_edge) {
4497 sky_slit_min_edge = slit_val;
4500 *max_size += (maxx - minx + 1);
4501 if(maxx - minx + 1 > *max_size_x) {
4502 *max_size_x = (maxx - minx + 1);
4508 XSH_CMP_INT( *max_size, >, 0,
"Not enough points for the sky order %d",
4513 sky_slit_min_edge = sky_slit_min_edge+slit_edges_mask;
4514 sky_slit_max_edge = sky_slit_max_edge-slit_edges_mask;
4517 double hheight1 = sky_par->
hheight1;
4518 double hheight2 = sky_par->
hheight2;
4520 if ( (hheight1 == 0) && (hheight2 == 0)){
4521 *sky_slit_min = sky_slit_min_edge;
4522 *sky_slit_max = sky_slit_max_edge;
4525 if (sky_slit_min_edge > *sky_slit_min){
4526 *sky_slit_min = sky_slit_min_edge;
4528 if (sky_slit_max_edge < *sky_slit_max){
4529 *sky_slit_max = sky_slit_max_edge;
4536 return cpl_error_get_code();
4539static cpl_error_code
4541 const int maxy,
const int wmap_xsize_diff,
4542 const int decode_bp,
const double obj_slit_min,
4543 const double obj_slit_max,
const double sky_slit_min,
4544 const double sky_slit_max,
const int order,
4553 double* plambda = cpl_image_get_data_double( wavemap);
4554 double* pslit = cpl_image_get_data_double( slitmap);
4561 int object_size = 0;
4566 double lambda_min = 10000., lambda_max = 0. ;
4580 for (
int iy = miny; iy < maxy; iy++) {
4585 int offset_idx = iy *
nx;
4586 int offset_sdx = iy * (
nx - wmap_xsize_diff);
4590 for (
int ix = minx; ix <= maxx; ix++) {
4592 idx = ix + offset_idx;
4594 if ((pqual[idx] & decode_bp) == 0) {
4597 sdx = ix + offset_sdx;
4599 double lambda = plambda[sdx];
4603 if (lambda < lambda_min) {
4604 lambda_min = lambda;
4606 if (lambda > lambda_max) {
4607 lambda_max = lambda;
4609 slit_val = pslit[sdx];
4613 pall->
slit = slit_val;
4614 pall->
flux = *(pflux + idx);
4615 derrs = *(perrs + idx);
4619 pall->
sigma = (double) derrs;
4620 pall->
qual = *(pqual + idx);
4643 if ( ( (slit_val >= obj_slit_min) && (slit_val <= obj_slit_max) ) ) {
4646 pobject->
lambda = lambda;
4647 pobject->
slit = slit_val;
4648 pobject->
flux = *(pflux + idx);
4649 derrs = *(perrs + idx);
4652 pobject->
sigma = (double) derrs;
4653 pobject->
qual = *(pqual + idx);
4662 psky->
slit = slit_val;
4663 psky->
flux = *(pflux + idx);
4664 derrs = *(perrs + idx);
4666 psky->
sigma = (double) derrs;
4667 psky->
qual = *(pqual + idx);
4684 assure(*sky_size > 0, CPL_ERROR_ILLEGAL_INPUT,
4685 "On order %d sky_size 0. Order edge tab may "
4686 "over-estimate corresponding order size or "
4687 "localize-slit-hheight is too large or "
4688 "sky-slit-edges-mask too large or "
4689 "sky-hheight1 too small or too large ",
4711 return cpl_error_get_code();
4714static cpl_error_code
4717 int bspline_sampling_method,
int* nbkpts_ord)
4720 if (break_pts_frame != NULL) {
4722 const char * name = cpl_frame_get_filename(break_pts_frame);
4723 cpl_table* break_pts_tab = cpl_table_load(name, 1, 0);
4724 int nrow = cpl_table_get_nrow(break_pts_tab);
4725 const double* pfactor = cpl_table_get_data_double_const(break_pts_tab,
4731 "Wrong number of factors for single frame sky subtraction table");
4736 "Wrong number of factors for single frame sky subtraction table");
4737 xsh_msg(
"iorder=%d factor=%f", iorder, pfactor[iorder]);
4742 "Wrong number of factors for single frame sky subtraction table");
4751 double nbkpts_arm=0;
4761 xsh_msg(
"Arm not supported");
break;
4763 if (bspline_sampling_method ==
FINE) {
4764 *nbkpts_ord = (int) (nbkpts * nbkpts_arm + 0.5);
4767 *nbkpts_ord = nbkpts;
4771 return cpl_error_get_code();
4779 cpl_frame* break_points_frame,
4780 cpl_frame* ref_sky_list, cpl_frame* sky_orders_chunks,
4785 cpl_image *wavemap=NULL;
4786 cpl_image *slitmap=NULL;
4787 const char* wavemap_name=NULL;
4788 const char* slitmap_name=NULL;
4790 int wmap_xsize_diff=0;
4791 double obj_slit_min=0, obj_slit_max=0;
4792 double sky_slit_min=0, sky_slit_max=0;
4794 double gain=sky_par->
gain;
4795 double ron2=sky_par->
ron*sky_par->
ron;
4797 int nbkpts_ord_max=50000;
4808 check( wavemap_name = cpl_frame_get_filename( wavemap_frame));
4809 check( wavemap = cpl_image_load( wavemap_name, CPL_TYPE_DOUBLE, 0, 0));
4810 check( slitmap_name = cpl_frame_get_filename( slitmap_frame));
4811 check( slitmap = cpl_image_load( slitmap_name, CPL_TYPE_DOUBLE,0, 0));
4813 wmap_xsize_diff=pre_sci->
nx-cpl_image_get_size_x(slitmap);
4816 nbkpts_ord_max=90000;
4819 nbkpts_ord_max=90000;
4822 double smap_min = cpl_image_get_min(slitmap);
4823 double smap_max = cpl_image_get_max(slitmap);
4825 &obj_slit_min, &obj_slit_max, &sky_slit_min, &sky_slit_max);
4829 cpl_table* bkpts_tab=NULL;
4833 fname = cpl_frame_get_filename(ref_sky_list);
4844 bkpts_tab = cpl_table_load(fname, 1, 0);
4845 if (cpl_table_get_column_type(bkpts_tab,
"WAVELENGTH") == CPL_TYPE_DOUBLE) {
4846 cpl_table_duplicate_column(bkpts_tab,
"TWAVELENGTH",bkpts_tab,
"WAVELENGTH");
4847 cpl_table_erase_column(bkpts_tab,
"WAVELENGTH");
4848 cpl_table_cast_column(bkpts_tab,
"TWAVELENGTH",
"WAVELENGTH",CPL_TYPE_FLOAT);
4850 if (cpl_table_get_column_type(bkpts_tab,
"FLUX") == CPL_TYPE_DOUBLE) {
4851 cpl_table_duplicate_column(bkpts_tab,
"TFLUX",bkpts_tab,
"FLUX");
4852 cpl_table_erase_column(bkpts_tab,
"FLUX");
4853 cpl_table_cast_column(bkpts_tab,
"TFLUX",
"FLUX",CPL_TYPE_FLOAT);
4855 cpl_table_erase_column(bkpts_tab,
"NAME");
4856 cpl_table_erase_column(bkpts_tab,
"COMMENT");
4861 for (
int iorder = 0; iorder < order_table->
size; iorder++){
4872 maxy = ( maxy <
endy ) ? maxy :
endy;
4880 nx, wmap_xsize_diff, order_table,
4881 slitmap, &sky_slit_min,
4882 &sky_slit_max, &max_size, &max_size_x);
4884 xsh_msg(
"SKY data slit range : [%f,%f],[%f,%f]",
4885 sky_slit_min, obj_slit_min, obj_slit_max, sky_slit_max);
4891 decode_bp, obj_slit_min, obj_slit_max,
4892 sky_slit_min, sky_slit_max,
order, wave_list,
4893 &sky_size, order_table, wavemap, slitmap);
4899 double* wsampl=NULL;
4900 cpl_table* wsampl_tab=NULL;
4908 cpl_table* tab_ord=NULL;
4917 cpl_table_and_selected_float(bkpts_tab,
"WAVELENGTH",CPL_NOT_GREATER_THAN,wmax);
4918 cpl_table_and_selected_float(bkpts_tab,
"WAVELENGTH",CPL_GREATER_THAN,wmin);
4919 tab_ord=cpl_table_extract_selected(bkpts_tab);
4920 cpl_table_select_all(bkpts_tab);
4929 name=
"xsh_kelson_becker_bkpts_uvb.fits";
4932 name=
"xsh_kelson_becker_bkpts_vis.fits";
4935 name=
"xsh_kelson_becker_bkpts_nir.fits";
4937 xsh_msg(
"iorder=%d wmin=%g wmax=%g orders=%d",
4938 iorder,wmin,wmax,orders_nb);
4940 wsampl_tab = cpl_table_load(name, orders_nb-iorder, 0);
4941 cpl_table_name_column(wsampl_tab,
"col1",
"WAVELENGTH");
4942 cpl_table_divide_scalar(wsampl_tab,
"WAVELENGTH",10.);
4946 cpl_table* tab_line_res=NULL;
4950 for(
int i=0;i<sky_par->
niter;i++) {
4958 int nbkpts=cpl_table_get_nrow(tab_ord);
4960 if(tab_line_res != NULL) {
4961 int nres=cpl_table_get_nrow(tab_line_res);
4963 if (cpl_table_get_column_type(tab_ord,
"FLUX") == CPL_TYPE_INT) {
4964 cpl_table_duplicate_column(tab_ord,
"FFLUX",tab_ord,
"FLUX");
4965 cpl_table_erase_column(tab_ord,
"FLUX");
4966 cpl_table_cast_column(tab_ord,
"FFLUX",
"FLUX",CPL_TYPE_FLOAT);
4967 cpl_table_erase_column(tab_ord,
"FFLUX");
4968 cpl_table_set_column_unit(tab_ord,
"FLUX",
"rel-flux");
4969 cpl_table_set_column_unit(tab_ord,
"WAVELENGTH",
"nm");
4971 check(cpl_table_insert(tab_ord,tab_line_res,nbkpts));
4979 iorder,nbkpts_ord,tab_ord));
4983 check(wsampl=cpl_table_get_data_double(wsampl_tab,
"WAVELENGTH"));
4986 cpl_table* tab_bad_fit=NULL;
4987 nbkpts_ord=cpl_table_get_nrow(wsampl_tab);
4988 xsh_msg(
"Sampling iter=%d nbkpts_ord=%d",i,nbkpts_ord);
4989 if(nbkpts_ord > nbkpts_ord_max) {
4998 psky = wave_list->
list[iorder].
sky;
4999 psky_start = wave_list->
list[iorder].
sky;
5000 for(j=0, psky=psky_start;j< sky_ndata;j++,psky++) {
5012 wsampl_tab,sky_orders_chunks,nbkpts_ord,i,
5013 sky_par, ron2, gain, &tab_line_res));
5017 wsampl_tab,sky_orders_chunks,nbkpts_ord,i,
5018 sky_par, ron2, gain, &tab_line_res));
5023 if(tab_bad_fit!=NULL) {
5025 cpl_table_and_selected_int(tab_bad_fit,
"FLAG",CPL_EQUAL_TO,1);
5026 cpl_table* tab_bp=cpl_table_extract_selected(tab_bad_fit);
5027 cpl_table_select_all(tab_bad_fit);
5028 cpl_table_and_selected_int(tab_bad_fit,
"FLAG",CPL_EQUAL_TO,2);
5029 cpl_table* tab_lr=cpl_table_extract_selected(tab_bad_fit);
5031 cpl_table_save(tab_lr,NULL,NULL,
"tab_lr.fits",CPL_IO_DEFAULT);
5032 cpl_table_save(tab_bp,NULL,NULL,
"tab_bp.fits",CPL_IO_DEFAULT);
5035 cpl_table_erase_column(tab_lr,
"FLAG");
5036 cpl_table_erase_column(tab_bp,
"FLAG");
5038 sprintf(sname,
"tab_lr_ord_%2.2d_%2.2d.reg",
order,i);
5040 sprintf(sname,
"tab_lr_ord_%2.2d_%2.2d.fits",
order,i);
5042 sprintf(sname,
"tab_bp_ord_%2.2d_%2.2d.reg",
order,i);
5044 sprintf(sname,
"tab_bp_ord_%2.2d_%2.2d.fits",
order,i);
5059 int nbp_row=cpl_table_get_nrow(tab_bp);
5060 int nlr_row=cpl_table_get_nrow(tab_lr);
5061 int nsrow=cpl_table_get_nrow(wsampl_tab);
5062 xsh_msg(
"Sampling: adding %d removing %d points",nlr_row,nbp_row);
5063 xsh_msg(
"Sampling: original %d points",nbkpts_ord);
5064 double* pbw = cpl_table_get_data_double(tab_bp,
"WAVELENGTH");
5065 double* psw = cpl_table_get_data_double(wsampl_tab,
"WAVELENGTH");
5067 for(
int k=0;k<nbp_row;k++) {
5072 for(
int j=1;j<nsrow-1;j++) {
5073 if(psw[j] == pbw[k]) {
5074 check(cpl_table_set_invalid(wsampl_tab,
"WAVELENGTH",j));
5079 cpl_table_erase_invalid(wsampl_tab);
5080 nbkpts_ord=cpl_table_get_nrow(wsampl_tab);
5081 xsh_msg(
"Sampling: removed %d bad pixels",nbad);
5082 xsh_msg(
"Sampling: After update %d points",nbkpts_ord);
5085 sprintf(sname,
"samplin_ord_%2.2d_%2.2d.reg",
order,i);
5087 sprintf(sname,
"samplin_ord_%2.2d_%2.2d.fits",
order,i);
5117 xsh_msg(
"Sampling points nbkpts_ord=%d",nbkpts_ord);
5122 int nb_points = 2*median_hsize+1;
5124 cpl_vector* median_vector = cpl_vector_new( nb_points);
5127 for(
int i=median_hsize; i< (sky_size-median_hsize); i++){
5129 int pixel = i-median_hsize;
5131 for(
int mediani=0; mediani< nb_points; mediani++){
5132 cpl_vector_set( median_vector, mediani, psky[pixel+mediani].flux);
5142 double median = cpl_vector_get_median(median_vector);
5194 for (i = 0; i < wave_list->
size; i++) {
5199 double *lambdas = NULL;
5200 float *sky_flux = NULL;
5201 float *sky_err = NULL;
5202 int *sky_qua = NULL;
5206 psky = wave_list->
list[i].
sky;
5216 for (j = 0; j < sky_size; j++) {
5217 lambdas[j] = psky->
lambda;
5218 sky_flux[j] = psky->
fitted;
5220 sky_qua[j] = psky->
qual;
5226 char debug_name[256];
5228 sprintf(debug_name,
"fitted_data_sky_%d.log",
order);
5229 debug = fopen(debug_name,
"w");
5230 fprintf(debug,
"# lambda flux err x y slit\n");
5231 psky = wave_list->
list[i].
sky;
5233 for (j = 0; j < sky_size; j++) {
5234 fprintf(debug,
"%f %f %f %d %d %f\n", psky->
lambda, psky->
fitted,
5244 if (cpl_error_get_code() != CPL_ERROR_NONE) {
5267 cpl_image* sky_ima=cpl_image_new(
nx,
ny,CPL_TYPE_FLOAT);
5268 cpl_image* qua_ima=cpl_image_new(
nx,
ny,CPL_TYPE_INT);
5269 float* psky_ima=cpl_image_get_data_float(sky_ima);
5270 int* pqua_ima=cpl_image_get_data_int(qua_ima);
5272 for (i = 0; i < wave_list->
size; i++) {
5280 for (j = 0; j < sky_size; j++,psky++) {
5285 for (j = 0; j < obj_size; j++,pobj++) {
5301 if (cpl_error_get_code() != CPL_ERROR_NONE) {
5305 return cpl_error_get_code();
5312 for (iorder = 0; iorder < wave_list->
size; iorder++) {
5314 int j,
order, object_size;
5316 char debug_name[256];
5322 sprintf(debug_name,
"fitted_data_obj_%d.log",
order);
5323 debug = fopen(debug_name,
"w");
5324 fprintf(debug,
"# lambda flux err x y slit\n");
5326 for (j = 0; j < object_size; j++) {
5327 fprintf(debug,
"%f %f %f %d %d %f\n", pobject->
lambda,
5329 pobject->
iy, pobject->
slit);
5338static cpl_error_code
5341 float* pflux,
float* pflat,
float* perrs,
int* pqual)
5343 for (
int i = 0; i < all_size; i++) {
5345 float flux, fitted, err, fitted_err;
5349 int pixel =
x +
y *
nx;
5350 flux = pflux[pixel];
5353 pflux[pixel] = flux - fitted;
5355 perrs[pixel] = sqrt(fitted_err * fitted_err + err * err);
5356 pqual[pixel] = pall->
qual;
5360 for (
int i = 0; i < sky_size; i++) {
5365 int pixel =
x +
y *
nx;
5372 pqual[pixel] = psky->
qual;
5375 return cpl_error_get_code();
5378static cpl_error_code
5380 float* pflux,
float* pflat,
float* perrs,
int* pqual)
5382 for (
int i = 0; i < sky_size; i++) {
5384 float flux, fitted, err, fitted_err;
5388 int pixel =
x +
y *
nx;
5389 flux = pflux[pixel];
5392 pflux[pixel] = flux - fitted;
5394 perrs[pixel] = sqrt(fitted_err * fitted_err + err * err);
5395 pqual[pixel] = psky->
qual;
5398 return cpl_error_get_code();
5403static cpl_error_code
5405 const int object_size,
const int nx,
5407 float* perrs,
int* pqual)
5414 int sky_lambdas_idx=0;
5415 for (
int i = 0; i < object_size; i++) {
5417 float flux, err, fitted, fitted_err;
5418 float lambda, lambda_min, lambda_max;
5419 float flux_min, flux_max, err_min, err_max;
5420 float cte_min, cte_max;
5424 lambda = (*pobject)->lambda;
5426 int pixel =
x +
y *
nx;
5427 flux = pflux[pixel];
5428 while ((sky_lambdas_idx < sky_lambdas_size)
5429 && (sky_lambdas[sky_lambdas_idx] <= lambda)) {
5430 (sky_lambdas_idx)++;
5432 if (sky_lambdas_idx >= (sky_lambdas_size - 1)) {
5435 lambda_max = sky_lambdas[sky_lambdas_idx];
5436 if (sky_lambdas_idx == 0) {
5440 lambda_min = sky_lambdas[sky_lambdas_idx - 1];
5441 flux_min = sky_flux[sky_lambdas_idx - 1];
5442 flux_max = sky_flux[sky_lambdas_idx];
5443 err_min = sky_err[sky_lambdas_idx - 1];
5444 err_max = sky_err[sky_lambdas_idx];
5445 cte_max = (lambda - lambda_min) / (lambda_max - lambda_min);
5446 cte_min = 1 - cte_max;
5449 fitted = flux_min * cte_min + flux_max * cte_max;
5454 err_min * err_min * cte_min
5455 + err_max * err_max * cte_max);
5456 (*pobject)->fitted = fitted;
5457 (*pobject)->fit_err = fitted_err;
5458 pflux[pixel] = flux - fitted;
5460 perrs[pixel] = sqrt(err * err + fitted_err * fitted_err);
5465 return cpl_error_get_code();
5473xsh_image_clean(
xsh_pre* pre,
const int decode_bp){
5474 double *spectrum_flux = NULL;
5475 double *spectrum_errs = NULL;
5476 int *spectrum_qual = NULL;
5481 cpl_mask * filtered =
xsh_bpm_filter(mask,5,5,CPL_FILTER_CLOSING);
5486 cpl_image_reject_from_mask(pre->
data,filtered);
5488 cpl_image_reject_from_mask(pre->
errs,filtered);
5510 float* perrs = NULL;
5511 float* pflux = NULL;
5512 float* pflat = NULL;
5516 cpl_image* res_image = NULL;
5517 cpl_frame* res_frame = NULL;
5529 check( pflux = cpl_image_get_data_float( res_image));
5536 for (iorder = 0; iorder < wave_list->
size; iorder++) {
5540 int order, sky_size, object_size,all_size;
5543 psky = wave_list->
list[iorder].
sky;
5544 pall = wave_list->
list[iorder].
all;
5562 "Subtract Sky on object - Order %d size %d",
order, object_size);
5572 pflux, perrs, pqual);
5583 sprintf(tag,
"%s_SUB_SKY_%s", rec_prefix,
5585 sprintf(fname,
"%s.fits", tag);
5590 if (strstr(fname,
"TMPSKY") != NULL) {
5597 xsh_frame_config(fname,tag,CPL_FRAME_TYPE_IMAGE,CPL_FRAME_GROUP_CALIB, CPL_FRAME_LEVEL_FINAL,&res_frame));
5675 cpl_frame *order_table_frame,
5676 cpl_frame *slitmap_frame,
5677 cpl_frame *wavemap_frame,
5678 cpl_frame *loc_table_frame,
5679 cpl_frame *ref_sky_list,
5680 cpl_frame *ref_sky_orders_chunks,
5681 cpl_frame *usr_def_sampl_points,
5685 cpl_frame **sky_spectrum,
5686 cpl_frame** sky_spectrum_eso,
5687 const char* rec_prefix,
const int clean_tmp)
5689 cpl_frame *res_frame = NULL ;
5694 cpl_frame *sky_frame = NULL;
5695 cpl_frame *sky_frame2 = NULL;
5702 XSH_ASSURE_NOT_NULL_MSG( slitmap_frame,
"Required slitmap frame is missing, provide it or set compute-map to TRUE");
5714 if(sky_par->
niter>3) {
5716 xsh_msg_warning(
"With sky-method=BSPLINE, sky-bspline-niter must be < 4");
5723 if ( loc_table_frame == NULL ) {
5724 xsh_msg(
"Subtract sky single no localization");
5727 xsh_msg(
"Subtract sky single using localization");
5734 sky_par->
gain = 2.12;
5745 local_list, order_table_frame, pre_sci, nbkpts,
5746 usr_def_sampl_points,ref_sky_list,ref_sky_orders_chunks,
5755 sprintf(tag,
"%s_DRL_SKY_ORD1D_%s", rec_prefix,
5757 sprintf(fname,
"%s.fits",tag);
5762 if ( sky_spectrum != NULL){
5763 *sky_spectrum = cpl_frame_duplicate( sky_frame);
5766 sprintf( tag,
"%s_SKY_ORD1D_%s", rec_prefix,
5768 sprintf(fname,
"%s.fits",tag);
5772 if( (clean_tmp==1) || (strstr(rec_prefix,
"TMPSKY")!=NULL) ) {
5776 if ( sky_spectrum != NULL){
5777 *sky_spectrum_eso = cpl_frame_duplicate( sky_frame2);
5783 rec_prefix,sky_par));
5804 cpl_frame* result=NULL;
5806 sprintf(sky_name,
"%s.fits",sky_tag);
5809 cpl_frame_set_filename(result,sky_name);
5810 cpl_frame_set_tag(result,sky_tag);
5821 char result_tag[256];
5822 char result_name[256];
5823 cpl_frame *result = NULL;
5825 const char *sky_name = NULL;
5826 cpl_image *sky_img = NULL;
5828 sprintf( result_tag,
"%s_OBJ_AND_SKY_NOCRH_%s", prefix,
5830 sprintf( result_name,
"%s.fits", result_tag);
5835 check( sky_name = cpl_frame_get_filename(sky_frame));
5838 check( sky_img = cpl_image_load( sky_name, CPL_TYPE_FLOAT, 0, 0));
5839 check( cpl_image_add( pre_sci->
data, sky_img));
static xsh_instrument * instrument
cpl_mask * xsh_qual_to_cpl_mask(cpl_image *qual, const int decode_bp)
cpl_mask * xsh_bpm_filter(const cpl_mask *input_mask, cpl_size kernel_nx, cpl_size kernel_ny, cpl_filter_mode filter)
Allows the growing and shrinking of bad pixel masks. It can be used to e.g. set pixels to bad if the ...
void xsh_bpmap_mask_bad_pixel(cpl_image *bpmap, cpl_mask *mask, int flag)
xsh_localization * xsh_localization_load(cpl_frame *frame)
Load a localization list from a frame.
void xsh_localization_free(xsh_localization **list)
free memory associated to a localization_list
int xsh_order_list_eval_int(xsh_order_list *list, cpl_polynomial *poly, double y)
Evaluate an order list poly but return the central pixel position rounding the polynomial.
xsh_order_list * xsh_order_list_load(cpl_frame *frame, xsh_instrument *instr)
load an order list from a frame
int xsh_order_list_get_starty(xsh_order_list *list, int i)
get position on Y axis of first pixel detected on order
int xsh_order_list_get_endy(xsh_order_list *list, int i)
get position on Y axis of last pixel detected on order
void xsh_order_list_free(xsh_order_list **list)
free memory associated to an order_list
cpl_image * xsh_pre_get_data(xsh_pre *pre)
Get data.
xsh_pre * xsh_pre_duplicate(const xsh_pre *pre)
Copy a PRE structure.
xsh_pre * xsh_pre_load(cpl_frame *frame, xsh_instrument *instr)
Load a xsh_pre structure from a frame.
cpl_image * xsh_pre_get_qual(xsh_pre *pre)
Get qual.
int xsh_pre_get_ny(const xsh_pre *pre)
Get ny of pre structure.
cpl_frame * xsh_pre_frame_subtract(cpl_frame *one, cpl_frame *two, const char *filename, xsh_instrument *instr, const int clean_tmp)
Subtract 2 frames (in XSH_PRE format) Just loads the 2 frames, subtract (xsh_pre_subtract) and save r...
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
int xsh_pre_get_nx(const xsh_pre *pre)
Get nx of pre structure.
double xsh_compute_ron_nir(const double dit)
cpl_image * xsh_pre_get_errs(xsh_pre *pre)
Get errs.
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
double * xsh_rec_list_get_lambda(xsh_rec_list *list, int idx)
float * xsh_rec_list_get_data1(xsh_rec_list *list, int idx)
cpl_frame * xsh_rec_list_save(xsh_rec_list *list, const char *filename, const char *tag, int is_temp)
Save a rec list in a frame.
xsh_rec_list * xsh_rec_list_create_with_size(int size, xsh_instrument *instr)
Create an empty order list.
float * xsh_rec_list_get_errs1(xsh_rec_list *list, int idx)
void xsh_rec_list_set_data_size(xsh_rec_list *list, int idx, int absorder, int nlambda, int ns)
Allocate memory for the order idx of the rectify list.
cpl_frame * xsh_rec_list1D_save_as_tab(xsh_rec_list *list, const char *filename, const char *tag)
save an rec list to a frame
int * xsh_rec_list_get_qual1(xsh_rec_list *list, int idx)
int xsh_rec_list_get_nlambda(xsh_rec_list *list, int idx)
void xsh_rec_list_free(xsh_rec_list **list)
free memory associated to a rec_list
void xsh_wavemap_list_free(xsh_wavemap_list **list)
free memory associated to a wavemap_list
cpl_error_code xsh_wavemap_list_sky_image_save(xsh_wavemap_list *smap, xsh_instrument *instr, const int abs_ord, const int sid, const int iter)
cpl_error_code xsh_wavemap_list_object_image_save(xsh_wavemap_list *omap, xsh_instrument *instr, const int iter)
void xsh_wavemap_list_set_max_size(xsh_wavemap_list *list, int idx, int absorder, int max_size)
set max size of wavemap
xsh_wavemap_list * xsh_wavemap_list_create(xsh_instrument *instr)
create an empty order list
#define XSH_ASSURE_NOT_NULL_MSG(pointer, msg)
#define XSH_ASSURE_NOT_ILLEGAL(cond)
#define assure(CONDITION, ERROR_CODE,...)
#define XSH_CMP_INT(A, OPERATOR, B, SUFFIX,...)
#define XSH_ASSURE_NOT_ILLEGAL_MSG(cond, msg)
#define XSH_ASSURE_NOT_NULL(pointer)
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_error(...)
Print an error message.
#define xsh_msg(...)
Print a message on info level.
#define xsh_msg_dbg_low(...)
double xsh_pfits_get_dit(const cpl_propertylist *plist)
find out the DIT value
double xsh_pfits_get_gain(const cpl_propertylist *plist)
find out the GAIN value
double xsh_pfits_get_ron(const cpl_propertylist *plist)
find out the RON value
static cpl_error_code xsh_model_fill_obj(xsh_wavemap_list *wlist, const int order, const double s1, const double s2, cpl_table *stab, const float ron2, const float gain)
static cpl_table * xsh_fit_spline1(xsh_wavemap_list *wlist, int order, cpl_table *sampl_tab, cpl_frame *sky_orders_chunks, int sampl_pts_no, int iter_no, xsh_subtract_sky_single_param *sky_par, float ron2, float gain, cpl_table **tab_line_res)
This routine performs the B-SPLINE fit of a set of sky data non-uniformly sampled.
static void xsh_gsl_bspline_non_uniform(double *data_x, double *data_y, double *data_e, double *bkpts, double *yfit, const int i_start, const size_t n, const size_t ncoeffs)
static cpl_error_code xsh_subtract_sky_on_obj_at_pix(xsh_rec_list *sky_list, const int iorder, const int object_size, const int nx, wavemap_item **pobject, float *pflux, float *perrs, int *pqual)
cpl_frame * xsh_save_sky_model(cpl_frame *obj_frame, cpl_frame *sub_sky_frame, const char *sky_tag, xsh_instrument *instrument)
static xsh_rec_list * xsh_wavemap_list_build_sky(xsh_wavemap_list *list, xsh_instrument *instrument)
Build the sky spectrum list.
static cpl_error_code xsh_model_fill_fit_bfit(xsh_wavemap_list *wlist, const int order, cpl_table *stab, const float ron2, const float gain)
static cpl_error_code xsh_subtract_sky_at_pix2(const int all_size, const int sky_size, const int nx, wavemap_item *pall, wavemap_item *psky, float *pflux, float *pflat, float *perrs, int *pqual)
static int xsh_get_edge_x_min(xsh_order_list *otab, const int iorder, const int iy)
static cpl_table * xsh_table_unique_wave(cpl_table *tab, const char *wname, const char *fname)
static cpl_table * xsh_detect_raw_data_outliers_1d_slice(const xsh_wavemap_list *wlist, const int hsize, const double ron2, const double gain, const int order, const int iter, const int decode_bp, const double s1, const double s2, const int slice_id)
determines sky outliers on a 1D spectrum via kappa-sigma clipping
static void xsh_dump_table_on_region_file(cpl_table *tab, const char *fname, const int symbol)
static int xsh_wave_compare(const void *un, const void *deux)
static cpl_table * xsh_table_unique_column(cpl_table *tab_inp, const char *col_wav)
double xsh_nbkpts_vis[XSH_ORDERS_VIS]
static cpl_table * xsh_skycorr_sampl_lines_from_static_tab(xsh_wavemap_list *wlist, const cpl_table *tab_ord, const int ord)
cpl_frame * xsh_add_sky_model(cpl_frame *subsky_frame, cpl_frame *sky_frame, xsh_instrument *instrument, const char *prefix)
cpl_frame * xsh_subtract_sky_single(cpl_frame *sci_frame, cpl_frame *order_table_frame, cpl_frame *slitmap_frame, cpl_frame *wavemap_frame, cpl_frame *loc_table_frame, cpl_frame *ref_sky_list, cpl_frame *ref_sky_orders_chunks, cpl_frame *usr_def_sampl_points, xsh_instrument *instrument, int nbkpts, xsh_subtract_sky_single_param *sky_par, cpl_frame **sky_spectrum, cpl_frame **sky_spectrum_eso, const char *rec_prefix, const int clean_tmp)
Subtract the sky background for single frame. If sky_spectrum is NOT NULL it is saved as a product,...
static cpl_error_code xsh_skycor_def_nbkpts_ord(const int iorder, const int nbkpts, cpl_frame *break_pts_frame, xsh_instrument *inst, int bspline_sampling_method, int *nbkpts_ord)
static double xsh_skycorr_rms(xsh_wavemap_list *wlist, const int iorder, const double ron2, const double gain)
static cpl_error_code xsh_wavecal_list_pupulate(xsh_pre *pre_sci, const int iorder, const int miny, const int maxy, const int wmap_xsize_diff, const int decode_bp, const double obj_slit_min, const double obj_slit_max, const double sky_slit_min, const double sky_slit_max, const int order, xsh_wavemap_list *wave_list, int *sky_size, xsh_order_list *ord_tab, cpl_image *wavemap, cpl_image *slitmap)
static cpl_error_code xsh_get_sky_edges_min_max_and_size(xsh_subtract_sky_single_param *sky_par, const int miny, const int maxy, const int iorder, const int order, const int nx, const int wmap_xsize_diff, xsh_order_list *ord_tab, cpl_image *slitmap, double *sky_slit_min, double *sky_slit_max, int *max_size, int *max_size_x)
static xsh_wavemap_list * xsh_wavemap_list_new(cpl_frame *wavemap_frame, cpl_frame *slitmap_frame, xsh_localization *list, cpl_frame *order_table_frame, xsh_pre *pre_sci, int nbkpts, cpl_frame *usr_def_sampl_points, cpl_frame *ref_sky_list, cpl_frame *sky_orders_chunks, xsh_subtract_sky_single_param *sky_par, xsh_instrument *instrument)
static cpl_frame * xsh_wavelist_subtract_sky(xsh_pre *pre_sci, xsh_rec_list *sky_list, xsh_wavemap_list *wave_list, xsh_instrument *instrument, const char *rec_prefix, xsh_subtract_sky_single_param *sky_par)
subtract sky
static cpl_table * xsh_model_to_table(const xsh_wavemap_list *wlist, const int order)
determines sky outliers on a 1D spectrum via kappa-sigma clipping
static cpl_error_code xsh_wavemap_list_rms_sky_image_save(xsh_wavemap_list *smap, const double ron2, const double gain, xsh_instrument *instr, const int iter)
static cpl_error_code xsh_model_fill_fit_slice_bfit(xsh_wavemap_list *wlist, const int order, const double s1, const double s2, cpl_table *stab, const float ron2, const float gain)
static cpl_table * xsh_fit_spline2(xsh_wavemap_list *wlist, int order, cpl_table *sampl_tab, cpl_frame *sky_orders_chunks, int sampl_pts_no, int iter_no, xsh_subtract_sky_single_param *sky_par, float ron2, float gain, cpl_table **tab_line_res)
This routine performs the B-SPLINE fit of a set of sky data non-uniformly sampled.
static cpl_table * xsh_skycorr_sample_continuum(xsh_wavemap_list *wlist, const int ord, const int n)
static void xsh_obj_fit_dump(int iorder, xsh_wavemap_list *wave_list)
static cpl_error_code xsh_model_fill_fit(xsh_wavemap_list *wlist, const int order, cpl_table *stab, const float ron2, const float gain)
static cpl_error_code xsh_bspline_smooth_non_uniform2(cpl_table *bkpts, cpl_frame *sky_orders_chunks, cpl_table **tab, const int iorder, xsh_instrument *instrument)
static cpl_error_code xsh_subtract_sky_at_pix(const int sky_size, const int nx, wavemap_item *psky, float *pflux, float *pflat, float *perrs, int *pqual)
static cpl_error_code xsh_table_column_smooth(cpl_table **tab, const char *name_i, const char *name_o, const int hsize)
determines sky outliers on a 1D spectrum via kappa-sigma clipping
static int xsh_get_edge_x_max(xsh_order_list *otab, const int iorder, const int iy)
static cpl_table * xsh_model2tab(xsh_wavemap_list *wlist, int order)
static cpl_table * xsh_detect_raw_data_outliers_1d(const xsh_wavemap_list *wlist, const int hsize, const double ron2, const double gain, const int order, const int iter, const int decode_bp)
determines sky outliers on a 1D spectrum via kappa-sigma clipping
static cpl_error_code xsh_table_column_clip(cpl_table **tab, const char *name_d, const char *name_s, const double kappa, const int niter, const double gain, const double ron2, const int decode_bp)
double xsh_nbkpts_uvb[XSH_ORDERS_UVB]
Create the wavemap list.
static cpl_table * xsh_create_sampl_uniform_continuum(const int iorder, xsh_wavemap_list *wlist, const int nsampl, const char *prefix)
static cpl_error_code xsh_get_obj_and_sky_extraction_slits(xsh_subtract_sky_single_param *sky_par, xsh_localization *list, const double smap_min, const double smap_max, double *obj_slit_min, double *obj_slit_max, double *sky_slit_min, double *sky_slit_max)
cpl_error_code xsh_bspline_smooth_uniform_sl(cpl_table **tab, xsh_instrument *instrument, const double s1, const double s2)
cpl_error_code xsh_bspline_smooth_uniform(cpl_table **tab, xsh_instrument *instrument)
cpl_error_code xsh_wavemap_list_full_sky_save(xsh_wavemap_list *wave_list, xsh_instrument *instr)
static cpl_error_code xsh_model_fill_fit_slice(xsh_wavemap_list *wlist, const int order, const double s1, const double s2, cpl_table *stab, const float ron2, const float gain)
static cpl_table * xsh_skycorr_wave_sampling_create(xsh_wavemap_list *wlist, const int ord, const int nbkpts_ord, const cpl_table *bkpts_tab)
static cpl_table * xsh_detect_outliers_thres_new(xsh_wavemap_list *wlist, const double kappa2, const double ron2, const double gain, const int order, const int iter, cpl_table **tab_line_res)
static cpl_table * xsh_create_sampl_table(const int iorder, double *wave_ref, const int nref, xsh_wavemap_list *wlist, const int nsampl_2, const char *prefix)
double xsh_nbkpts_nir[XSH_ORDERS_NIR]
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
int xsh_debug_level_get(void)
get debug level
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
cpl_error_code xsh_sort_table_1(cpl_table *t, const char *column, cpl_boolean reverse)
Sort a table by one column.
void xsh_free_mask(cpl_mask **m)
Deallocate an image mask and set the pointer to NULL.
cpl_table * xsh_histogram(const cpl_table *data, const char *cname, const int nbins, const double min, const double max)
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
cpl_error_code xsh_sort_table_3(cpl_table *t, const char *column1, const char *column2, const char *column3, cpl_boolean reverse1, cpl_boolean reverse2, cpl_boolean reverse3)
Sort a table by two columns.
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
cpl_polynomial * edguppoly
cpl_polynomial * edglopoly
cpl_polynomial * edguppoly
cpl_polynomial * edglopoly
cpl_propertylist * data_header
enum bspline_sampling bspline_sampling
xsh_instrument * instrument
#define QFLAG_QUESTIONABLE_PIXEL
#define QFLAG_SKY_MODEL_BAD_FIT
#define QFLAG_SKY_MODEL_BAD_PIX
#define QFLAG_SATURATED_DATA
static xsh_star_flux_list * xsh_bspline_fit_smooth(xsh_star_flux_list *ref_std_star_list, HIGH_ABS_REGION *phigh, xsh_instrument *inst)
double * xsh_bspline_interpolate_data_at_pos(double *w_data, double *f_data, const int n_data, double *w_pos, const int n_pos)
#define XSH_PRE_DATA_TYPE
int xsh_print_rec_status(const int val)
Check if an error has happened and returns error kind and location.
void xsh_frame_config(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level, cpl_frame **frame)
Define a frame characteristics.
#define SKY_METHOD_PRINT(method)