42#define LINETAB_PIXELSIZE "Pixel"
43#define TWOSQRT2LN2 2.35482004503095
94 cpl_table* histo=NULL;
95 cpl_table* tmp_tbl1=NULL;
96 cpl_table* tmp_tbl2=NULL;
100 const double* pdt=NULL;
111 cpl_table_and_selected_double(
data,cname,CPL_NOT_GREATER_THAN,
max);
113 tmp_tbl1=cpl_table_extract_selected(
data);
114 cpl_table_and_selected_double(tmp_tbl1,cname,CPL_GREATER_THAN,min);
115 tmp_tbl2=cpl_table_extract_selected(tmp_tbl1);
118 ntot=cpl_table_get_nrow(tmp_tbl2);
121 vmin=cpl_table_get_column_min(tmp_tbl2,cname);
122 vmax=cpl_table_get_column_max(tmp_tbl2,cname);
123 vstp=(vmax-vmin)/(nbins-1);
125 histo=cpl_table_new(nbins);
126 cpl_table_new_column(histo,
"HL",CPL_TYPE_DOUBLE);
127 cpl_table_new_column(histo,
"HY",CPL_TYPE_INT);
128 cpl_table_new_column(histo,
"HX",CPL_TYPE_DOUBLE);
130 cpl_table_fill_column_window(histo,
"HL",0,nbins,0);
131 cpl_table_fill_column_window(histo,
"HY",0,nbins,0);
133 phy=cpl_table_get_data_int(histo,
"HY");
135 pdt=cpl_table_get_data_double_const(
data,cname);
137 for(i=0;i<nbins;i++) {
138 cpl_table_set_double(histo,
"HX",i,(
double)i);
140 cpl_table_set_double(histo,
"HL",i,vtmp);
143 for(i=0;i<ntot;i++) {
144 h=floor((pdt[i]-vmin)/vstp);
145 if((h<nbins) && (h>-1)) {
185 else if (lo + 1 == hi)
188 check(lhi = cpl_table_get_double(line_refer, col_ref, hi, NULL));
189 check(llo = cpl_table_get_double(line_refer, col_ref, lo, NULL));
192 return ((llo-lambda)*(llo-lambda) < (lhi-lambda)*(lhi-lambda)) ? lo : hi;
197 int mid = (lo + hi)/2;
200 check(lmid = cpl_table_get_double(line_refer, col_ref, mid, NULL));
226 cpl_propertylist** header_check,
231 char qc_intavg_name[40];
232 char qc_nlinint_name[40];
236 int nrows=cpl_table_get_nrow(temp);
244 mean = cpl_table_get_column_mean(temp,
"Intensity");
249 check(cpl_propertylist_append_double(*header_check,qc_intavg_name,mean));
250 check(cpl_propertylist_set_comment(*header_check,qc_intavg_name,comment));
254 sprintf(comment,
"exp norm avg int of line list");
255 check(cpl_propertylist_append_double(*header_check,qc_intavg_name,mean/
exptime));
256 check(cpl_propertylist_set_comment(*header_check,qc_intavg_name,comment));
261 check(cpl_propertylist_append_int(*header_check,qc_nlinint_name,nrows));
262 check(cpl_propertylist_set_comment(*header_check,qc_nlinint_name,comment));
265 return cpl_error_get_code();
278 const char* col_wave_intmon,
279 cpl_table* table_check,
280 const char* col_wave_intcheck,
293 int N_bright = cpl_table_get_nrow(table_intmon);
296 double wmin=cpl_table_get_column_min(table_intmon,
"WAVELENGTH");
297 double wmax=cpl_table_get_column_max(table_intmon,
"WAVELENGTH");
298 check(cpl_table_and_selected_double(table_check,
"Wavelength",CPL_LESS_THAN,wmax+
tolerance));
299 check(cpl_table_and_selected_double(table_check,
"Wavelength",CPL_GREATER_THAN,wmin-
tolerance));
301 cpl_table* table_ext=cpl_table_extract_selected(table_check);
303 check(cpl_table_new_column(table_ext,
"Intensity", CPL_TYPE_DOUBLE));
304 for (i = 0; i < cpl_table_get_nrow(table_ext); i++)
308 check(ident=cpl_table_get_double(table_ext,
"Wavelength", i, &is_null));
312 int bright_index = 0;
314 ident, 0, N_bright-1));
318 check(bright=cpl_table_get_double(table_intmon,
"WAVELENGTH", bright_index, NULL));
334 check(norm=cpl_table_get_double(table_ext,
"NormGauss", i, NULL));
341 check(cpl_table_set_double(table_ext,
"Intensity", i, intensity));
346 check(cpl_table_set_invalid(table_ext,
"Intensity", i));
351 check(cpl_table_set_invalid(table_ext,
"Intensity", i));
371 cpl_table *table_check,
373 const char* atom_name,
374 cpl_propertylist** header_check)
376 cpl_table *ext_intmon = NULL;
377 cpl_table* table_ext=NULL;
378 cpl_table *temp = NULL;
380 cpl_table_and_selected_string(table_intmon,
"element",CPL_EQUAL_TO,atom_name);
381 ext_intmon=cpl_table_extract_selected(table_intmon);
388 temp = cpl_table_duplicate(table_ext);
389 cpl_table_erase_invalid(temp);
391 cpl_table_select_all(table_intmon);
397 return cpl_error_get_code();
410 const cpl_frame *line_intmon,
414 cpl_table *temp = NULL;
415 cpl_table *table_check = NULL;
416 cpl_table *table_intmon = NULL;
417 const char* name_check=NULL;
418 const char* name_intmon=NULL;
419 cpl_propertylist* header_check=NULL;
420 cpl_table* table_ext=NULL;
422 check(name_check=cpl_frame_get_filename(line_check));
423 check(table_check=cpl_table_load(name_check,1,0));
424 check(header_check=cpl_propertylist_load(name_check,0));
426 check(name_intmon=cpl_frame_get_filename(line_intmon));
427 check(table_intmon=cpl_table_load(name_intmon,1,0));
432 "AR",&header_check));
435 "NE",&header_check));
438 "XE",&header_check));
443 "THAR",&header_check));
446 check(cpl_table_save(table_check,header_check,NULL,name_check,CPL_IO_DEFAULT));
455 return cpl_error_get_code();
473 const char *colname, cpl_type coltype,
int i,
void* result)
485 check_msg (*((
int *) result) = cpl_table_get_int(table,colname,i,&flag),
486 "Could not get (integer) value of %s at row %d", colname,i);
489 check_msg (*((
float *) result) = cpl_table_get_float( table,
491 "Could not get (float) value of %s at row %d",colname,i);
493 case CPL_TYPE_DOUBLE:
494 check_msg (*((
double *) result) = cpl_table_get_double (table,
496 "Could not get (double) value of %s at row %d",colname,i);
498 case CPL_TYPE_STRING:
500 cpl_table_get_string (table, colname,i),
501 "Could not get (string) value of %s at row %d",colname,i);
504 assure (
false, CPL_ERROR_INVALID_TYPE,
"Unknown type");
508 return cpl_error_get_code ();
532 cpl_propertylist *plist = NULL;
534 assure(t != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
535 assure(cpl_table_has_column(t, column), CPL_ERROR_ILLEGAL_INPUT,
536 "No column '%s'", column);
538 check_msg(( plist = cpl_propertylist_new(),
539 cpl_propertylist_append_bool(plist, column, reverse)),
540 "Could not create property list for sorting");
542 check_msg( cpl_table_sort(t, plist),
"Could not sort table");
546 return cpl_error_get_code();
569 cpl_boolean reverse1, cpl_boolean reverse2)
571 cpl_propertylist *plist = NULL;
573 assure(t != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
574 assure(cpl_table_has_column(t, column1), CPL_ERROR_ILLEGAL_INPUT,
575 "No column '%s'", column1);
576 assure(cpl_table_has_column(t, column2), CPL_ERROR_ILLEGAL_INPUT,
577 "No column '%s'", column2);
579 check_msg(( plist = cpl_propertylist_new(),
580 cpl_propertylist_append_bool(plist, column1, reverse1),
581 cpl_propertylist_append_bool(plist, column2, reverse2)),
582 "Could not create property list for sorting");
583 check_msg( cpl_table_sort(t, plist),
"Could not sort table");
587 return cpl_error_get_code();
614 cpl_boolean reverse1, cpl_boolean reverse2,cpl_boolean reverse3)
616 cpl_propertylist *plist = NULL;
618 assure(t != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
619 assure(cpl_table_has_column(t, column1), CPL_ERROR_ILLEGAL_INPUT,
620 "No column '%s'", column1);
621 assure(cpl_table_has_column(t, column2), CPL_ERROR_ILLEGAL_INPUT,
622 "No column '%s'", column2);
623 assure(cpl_table_has_column(t, column3), CPL_ERROR_ILLEGAL_INPUT,
624 "No column '%s'", column3);
626 check_msg(( plist = cpl_propertylist_new(),
627 cpl_propertylist_append_bool(plist, column1, reverse1),
628 cpl_propertylist_append_bool(plist, column2, reverse2),
629 cpl_propertylist_append_bool(plist, column3, reverse3)),
630 "Could not create property list for sorting");
631 check_msg( cpl_table_sort(t, plist),
"Could not sort table");
635 return cpl_error_get_code();
658 double w2=pw[nrow-1];
660 double y2=pe[nrow-1];
668 else if (wav > pw[nrow - 1])
680 curr_row = (h-l) / 2;
683 if(wav < pw[curr_row])
691 curr_row = (h-l) / 2 + l;
698 y=y1_+(y2-y1_)/(w2-w1)*(wav-w1);
726 check(pw=cpl_table_get_data_double(tbl,colx));
727 check(pe=cpl_table_get_data_double(tbl,coly));
728 check(nrow=cpl_table_get_nrow(tbl));
752 cpl_propertylist** header)
761 check(cpl_table_and_selected_double(table,
"W",CPL_GREATER_THAN,ws));
762 check(next=cpl_table_and_selected_double(table,
"W",CPL_LESS_THAN,we));
764 check(ext=cpl_table_extract_selected(table));
765 check(cpl_table_select_all(table));
766 if(cpl_table_has_valid(ext,
"F")) {
767 check(flux=cpl_table_get_column_mean(ext,
"F"));
771 sprintf(qc_key,
"ESO QC %s%d",prefix,index);
772 sprintf(comment,
"Mean value of %s in %4.0f-%4.0f nm",prefix,ws,we);
773 check(cpl_propertylist_append_double(*header,qc_key,flux));
774 check(cpl_propertylist_set_comment(*header,qc_key,comment));
781 return cpl_error_get_code();
798static cpl_propertylist*
805 cpl_propertylist* header=NULL;
808 header=cpl_propertylist_new();
809 check(cpl_table_cast_column(table,colw,
"W",CPL_TYPE_DOUBLE));
810 check(cpl_table_cast_column(table,colf,
"F",CPL_TYPE_DOUBLE));
832 cpl_table_erase_column(table,
"W");
833 cpl_table_erase_column(table,
"F");
836 if(cpl_error_get_code() != CPL_ERROR_NONE) {
863 const char* name=NULL;
864 cpl_table* table=NULL;
865 cpl_propertylist* header=NULL;
866 cpl_propertylist* qc_header=NULL;
872 next=cpl_frame_get_nextensions(frm);
873 name=cpl_frame_get_filename(frm);
878 for(ext=1;ext<=next;ext++) {
880 table=cpl_table_load(name,1,ext);
881 check(header=cpl_propertylist_load(name,0));
884 cpl_propertylist_append(header,qc_header);
886 check(cpl_table_save(table,header,NULL,name,CPL_IO_DEFAULT));
888 check(cpl_table_save(table,header,NULL,name,CPL_IO_EXTEND));
895 table=cpl_table_load(name,1,0);
896 check(header=cpl_propertylist_load(name,0));
899 cpl_propertylist_append(header,qc_header);
900 check(cpl_table_save(table,header,NULL,name,CPL_IO_DEFAULT));
910 return cpl_error_get_code();
918 double* pwav_resid=NULL;
919 float* pwav_clean=NULL;
920 double* pwav_extra=NULL;
921 cpl_table* resid=NULL;
922 cpl_table* clean=NULL;
928 double wave_acc=0.001;
929 const char* name=NULL;
930 cpl_propertylist* rhead=NULL;
931 cpl_propertylist* qc_head=NULL;
937 check(name = cpl_frame_get_filename(frm_clean));
938 check(clean = cpl_table_load(name, 1, 0));
939 check(nrows_clean = cpl_table_get_nrow(clean));
940 check(name = cpl_frame_get_filename(frm_resid));
941 resid = cpl_table_load(name, 1, 0);
943 rhead = cpl_propertylist_load(name, 0);
945 qc_head=cpl_propertylist_load_regexp(cpl_frame_get_filename(frm_clean),0,
"^ESO QC",0);
946 cpl_propertylist_append(rhead,qc_head);
948 cpl_frame_get_nextensions(frm_resid);
949 check(nrows_resid=cpl_table_get_nrow(resid));
950 cpl_table_new_column(resid,
"WavelengthClean",CPL_TYPE_DOUBLE);
951 cpl_table_fill_column_window_double(resid,
"WavelengthClean",0,nrows_resid,0);
954 check(pwav_extra=cpl_table_get_data_double(resid,
"WavelengthClean"));
956 for (i = 0; i < nrows_clean; i++) {
957 for (k = 0; k < nrows_resid; k++) {
958 if (fabs(pwav_resid[k] - pwav_clean[i]) < wave_acc) {
959 pwav_extra[i] = pwav_clean[i];
964 check(cpl_table_save(resid,rhead,NULL,name,CPL_IO_DEFAULT));
972 return cpl_error_get_code();
978 cpl_table* shifted = NULL;
984 shifted=cpl_table_duplicate(orig);
985 pwav = cpl_table_get_data_double(shifted, col_wave);
986 nrows=cpl_table_get_nrow(shifted);
987 for(i=0;i<nrows;i++){
988 pwav[i] *= (1.+offset);
static xsh_instrument * instrument
#define XSH_ASSURE_NOT_NULL_MSG(pointer, msg)
#define assure(CONDITION, ERROR_CODE,...)
#define check_msg(COMMAND,...)
#define XSH_ASSURE_NOT_NULL(pointer)
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
#define xsh_msg_warning(...)
Print an warning message.
cpl_error_code xsh_wavecal_qclog_intmon(cpl_frame *line_check, const cpl_frame *line_intmon, const double exptime, xsh_instrument *inst)
computes intmon QC log
static cpl_error_code xsh_wavecal_qclog_element(cpl_table *table_intmon, cpl_table *table_check, const double exptime, const char *atom_name, cpl_propertylist **header_check)
computes intmon QC log
static cpl_table * xsh_table_select_matching_waves(cpl_table *table_intmon, const char *col_wave_intmon, cpl_table *table_check, const char *col_wave_intcheck, const double exptime)
Match reference and actual table via wavelength column.
cpl_error_code xsh_table_merge_clean_and_resid_tabs(cpl_frame *frm_resid, cpl_frame *frm_clean)
cpl_error_code xsh_sort_table_2(cpl_table *t, const char *column1, const char *column2, cpl_boolean reverse1, cpl_boolean reverse2)
Sort a table by two columns.
static cpl_error_code xsh_table_monitor_flux_qc(cpl_table *table, const double ws, const double we, const char *prefix, const int index, cpl_propertylist **header)
Computes statistics on spectrum for QC.
cpl_error_code xsh_sort_table_1(cpl_table *t, const char *column, cpl_boolean reverse)
Sort a table by one column.
double xsh_data_interpolate(double wav, int nrow, double *pw, double *pe)
Interpolate data points.
cpl_table * xsh_table_shift_rv(cpl_table *orig, const char *col_wave, const double offset)
static cpl_propertylist * xsh_frame_table_monitor_flux_qc_ext(cpl_table *table, const char *colw, const char *colf, const char *prefix, xsh_instrument *instrument)
Computes statistics on spectrum for QC.
cpl_table * xsh_histogram(const cpl_table *data, const char *cname, const int nbins, const double min, const double max)
cpl_error_code xsh_frame_table_monitor_flux_qc(cpl_frame *frm, const char *colw, const char *colf, const char *prefix, xsh_instrument *instrument)
Computes statistics on spectrum for QC.
double xsh_table_interpolate(cpl_table *tbl, double wav, const char *colx, const char *coly)
Interpolate table columns.
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
cpl_error_code xsh_get_table_value(const cpl_table *table, const char *colname, cpl_type coltype, int i, void *result)
Read a table value from a fits table.
cpl_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.
static cpl_error_code xsh_wavecal_qclog_compute(cpl_table *temp, cpl_propertylist **header_check, const char *element, const double exptime)
computes intmon QC log
static int xsh_wavecal_find_nearest(const cpl_table *line_refer, const char *col_ref, double lambda, int lo, int hi)
Find best matching catalogue wavelength.
#define XSH_ARCLIST_TABLE_COLNAME_WAVELENGTH
#define XSH_RESID_TAB_TABLE_COLNAME_WAVELENGTH
#define QC_WAVECAL_NLININT
#define QC_WAVECAL_NLININT_C
#define QC_WAVECAL_INTAVG
#define QC_WAVECAL_INTAVG_C
#define XSH_TABLE_GET_ARRAY(TYPE)