42 #include <uves_utils.h>
43 #include <uves_utils_cpl.h>
44 #include <irplib_ksigma_clip.h>
50 #include <uves_extract_profile.h>
51 #include <uves_plot.h>
53 #include <uves_pfits.h>
54 #include <uves_utils_wrappers.h>
55 #include <uves_wavecal_utils.h>
57 #include <uves_dump.h>
58 #include <uves_error.h>
60 #include <irplib_utils.h>
63 #include <uves_time.h>
74 #define C_TO_FORTRAN_INDEXING(a) &a[-1]
75 #define FORTRAN_TO_C_INDEXING(a) &a[1]
98 const double sigma_y);
100 static cpl_error_code
101 uves_find_next(cpl_image** msk,
106 static cpl_error_code
107 uves_sort(
const int kmax,
float* inp,
int* ord);
258 return CPL_ERROR_NONE;
279 cpl_vector *accepted;
282 double *data = cpl_vector_get_data(values);
283 int n = cpl_vector_get_size(values);
295 for (i = 0; i < n; i++) {
296 sigma += (mean - data[i]) * (mean - data[i]);
298 sigma = sqrt(sigma / (n - 1));
302 for (i = 0; i < ngood; i++) {
303 if (data[i]-mean < khigh*sigma && mean-data[i] < klow*sigma) {
304 data[count] = data[i];
318 check_nomsg(accepted = cpl_vector_wrap(count, data));
321 check_nomsg(sigma = cpl_vector_get_stdev(accepted));
325 if (count == ngood) {
358 int ni, nx, ny, npix;
359 cpl_image *out_ima=NULL;
360 cpl_imagelist *loc_iml=NULL;
361 double *pout_ima=NULL;
362 cpl_image *image=NULL;
363 const double **data=NULL;
365 cpl_vector *time_line=NULL;
367 double *ptime_line=NULL;
369 double mean_of_medians=0;
371 passure(imlist != NULL,
"Null input imagelist!");
373 ni = cpl_imagelist_get_size(imlist);
374 loc_iml = cpl_imagelist_duplicate(imlist);
375 image = cpl_imagelist_get(loc_iml, 0);
376 nx = cpl_image_get_size_x(image);
377 ny = cpl_image_get_size_y(image);
380 out_ima = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
381 pout_ima = cpl_image_get_data_double(out_ima);
383 time_line = cpl_vector_new(ni);
385 ptime_line = cpl_vector_get_data(time_line);
387 data = cpl_calloc(
sizeof(
double *), ni);
388 med = cpl_calloc(
sizeof(
double), ni);
390 for (i = 0; i < ni; i++) {
391 image = cpl_imagelist_get(loc_iml, i);
392 med[i]=cpl_image_get_median(image);
393 cpl_image_subtract_scalar(image,med[i]);
394 data[i] = cpl_image_get_data_double(image);
395 mean_of_medians+=med[i];
399 for (i = 0; i < npix; i++) {
400 for (j = 0; j < ni; j++) {
401 ptime_line[j] = data[j][i];
406 cpl_image_add_scalar(out_ima,mean_of_medians);
411 cpl_vector_delete(time_line);
412 uves_free_imagelist(&loc_iml);
455 const cpl_parameterlist *parameters,
456 const cpl_table *ordertable,
457 const cpl_table *linetable,
460 const int first_abs_order,
461 const int last_abs_order,
465 cpl_image* wave_map=NULL;
488 nx = cpl_image_get_size_x(ima_sci);
489 ny = cpl_image_get_size_y(ima_sci);
492 ord_min=cpl_table_get_column_min(ordertable,
"Order");
493 ord_max=cpl_table_get_column_max(ordertable,
"Order");
494 norders=ord_max-ord_min+1;
496 check_nomsg(wave_map=cpl_image_new(nx,ny,CPL_TYPE_DOUBLE));
497 pwmap=cpl_image_get_data_double(wave_map);
499 for (order = 1; order <= norders; order++){
506 for (jj=-hs;jj<hs;jj++) {
507 j=(int)(ypos+jj+0.5);
509 if( (j>0) && ( (j*nx+i)<nx*ny) ) {
552 const cpl_table *ordertable,
554 const cpl_image* mflat)
557 cpl_imagelist* flats_norm=NULL;
559 cpl_image* master_flat=NULL;
561 cpl_image* flat=NULL;
562 cpl_image* flat_mflat=NULL;
564 cpl_vector* vec_flux=NULL;
565 double* pvec_flux=NULL;
588 double flux_median=0;
589 double mean_explevel=0;
594 ni=cpl_imagelist_get_size(flats);
597 sx = cpl_image_get_size_x(mflat);
598 sy = cpl_image_get_size_y(mflat);
601 ord_min=cpl_table_get_column_min(ordertable,
"Order");
602 ord_max=cpl_table_get_column_max(ordertable,
"Order");
603 nord=ord_max-ord_min+1;
605 hbox_sx=(int)((sx-2*x_space)/(2*nsam)+0.5);
606 flats_norm=cpl_imagelist_new();
608 uves_free_vector(&vec_flux);
609 vec_flux=cpl_vector_new(nord*nsam);
610 pvec_flux=cpl_vector_get_data(vec_flux);
611 uves_free_image(&flat_mflat);
612 uves_free_image(&flat);
613 check_nomsg(flat = cpl_image_duplicate(cpl_imagelist_get(flats, i)));
615 flat_mflat=cpl_image_duplicate(flat);
616 cpl_image_divide(flat_mflat,mflat);
619 for(ord=0;ord<nord;ord++) {
622 for(is=0;is<nsam;is++) {
623 pos_x+=(2*hbox_sx+x_space);
645 check_nomsg(pvec_flux[k]=cpl_image_get_median_window(flat_mflat,llx,lly,urx,ury));
652 flux_median=cpl_vector_get_median(vec_flux);
653 uves_msg(
"Flat %d normalize factor iter2: %g",i,flux_median);
654 cpl_image_divide_scalar(flat,flux_median);
655 cpl_imagelist_set(flats_norm,cpl_image_duplicate(flat),i);
656 mean_explevel+=flux_median;
660 check_nomsg(cpl_imagelist_multiply_scalar(flats_norm,mean_explevel));
662 check( master_flat = cpl_imagelist_collapse_median_create(flats_norm),
663 "Error computing median");
670 uves_free_imagelist(&flats_norm);
671 uves_free_vector(&vec_flux);
672 uves_free_image(&flat_mflat);
673 uves_free_image(&flat);
674 uves_check_rec_status(0);
703 const cpl_table *ordertable,
705 const cpl_vector* gain_vals ,
709 cpl_image *image=NULL;
710 cpl_image* master_flat=NULL;
711 cpl_imagelist* flats_norm=NULL;
733 cpl_vector* vec_flux_ord=NULL;
734 cpl_vector* vec_flux_sam=NULL;
735 double* pvec_flux_ord=NULL;
736 double* pvec_flux_sam=NULL;
739 const double* pgain_vals=NULL;
740 double fnoise_local=0;
742 passure(flats != NULL,
"Null input flats imagelist!");
743 passure(order_locations != NULL,
"Null input order locations polinomial!");
745 ni = cpl_imagelist_get_size(flats);
747 image = cpl_image_duplicate(cpl_imagelist_get(flats, 0));
748 sx = cpl_image_get_size_x(image);
749 sy = cpl_image_get_size_y(image);
751 uves_free_image(&image);
752 ord_min=cpl_table_get_column_min(ordertable,
"Order");
753 ord_max=cpl_table_get_column_max(ordertable,
"Order");
754 nord=ord_max-ord_min+1;
755 vec_flux_ord=cpl_vector_new(nord);
756 vec_flux_sam=cpl_vector_new(nsam);
757 pvec_flux_ord=cpl_vector_get_data(vec_flux_ord);
758 pvec_flux_sam=cpl_vector_get_data(vec_flux_sam);
759 hbox_sx=(int)((sx-2*x_space)/(2*nsam)+0.5);
760 flats_norm=cpl_imagelist_new();
761 pgain_vals=cpl_vector_get_data_const(gain_vals);
764 uves_free_image(&image);
765 image = cpl_image_duplicate(cpl_imagelist_get(flats, k));
766 for(ord=0;ord<nord;ord++) {
769 for(is=0;is<nsam;is++) {
770 pos_x+=(2*hbox_sx+x_space);
777 llx=uves_max_int(pos_x-hbox_sx,1);
778 lly=uves_max_int(pos_y-hbox_sy,1);
779 llx=uves_min_int(llx,sx);
780 lly=uves_min_int(lly,sy);
782 urx=uves_min_int(pos_x+hbox_sx,sx);
783 ury=uves_min_int(pos_y+hbox_sy,sy);
784 urx=uves_max_int(urx,1);
785 ury=uves_max_int(ury,1);
787 llx=uves_min_int(llx,urx);
788 lly=uves_min_int(lly,ury);
790 check_nomsg(pvec_flux_sam[is]=cpl_image_get_median_window(image,llx,lly,urx,ury));
793 pvec_flux_ord[ord]=cpl_vector_get_mean(vec_flux_sam);
796 flux_mean=cpl_vector_get_mean(vec_flux_ord);
797 uves_msg(
"Flat %d normalize factor inter1: %g",k,flux_mean);
798 fnoise_local+=pgain_vals[k]*flux_mean;
799 cpl_image_divide_scalar(image,flux_mean);
800 cpl_imagelist_set(flats_norm,cpl_image_duplicate(image),k);
802 *fnoise=1./sqrt(fnoise_local);
803 check( master_flat = cpl_imagelist_collapse_median_create(flats_norm),
804 "Error computing median");
809 uves_free_vector(&vec_flux_ord);
810 uves_free_vector(&vec_flux_sam);
811 uves_free_image(&image);
812 uves_free_imagelist(&flats_norm);
845 static cpl_error_code
922 *msk=cpl_image_new(sx,sy,CPL_TYPE_INT);
969 for(j=1;j<sy-1;j++) {
970 for(i=1;i<sx-1;i++) {
972 sigma=sqrt(ron*ron+pf[pix]/gain);
973 if ( (pi[pix]-pf[pix]) >= (ns*sigma) ) {
1009 check_nomsg(uves_find_next(msk,first[1],&next_x, &next_y));
1011 if(next_x==-1)
return CPL_ERROR_NONE;
1062 i_min=(i_min<ii) ? i_min: ii;
1063 i_max=(i_max>ii) ? i_max: ii;
1064 j_min=(j_min<jj) ? j_min: jj;
1065 j_max=(j_max>jj) ? j_max: jj;
1073 }
else if(pm[pix]==0) {
1110 uves_msg_debug(
"p[%d,%d]= 3 -> member of a group which has been examined",i,j);
1127 for(l=j_min;l<=j_max;l++){
1128 for(k=i_min;k<=i_max;k++){
1137 first[0] = next_x+1;
1162 s1=pi[(jdu_max-1)*sx+idu_max-1]+
1163 pi[(jdu_max-1)*sx+idu_max+1]+
1164 pi[(jdu_max-1)*sx+idu_max]+
1165 pi[(jdu_max+1)*sx+idu_max];
1167 s2=pi[(jdu_max+1)*sy+idu_max-1]+
1168 pi[(jdu_max+1)*sy+idu_max+1]+
1169 pi[(jdu_max)*sy+idu_max-1]+
1170 pi[(jdu_max)*sy+idu_max+1];
1171 asum=(s1+s2)/8.-sky;
1193 if((f_max-sky) > rc*asum) {
1195 for( l = j_min-1; l <= j_max+1; l++) {
1196 for( k = i_min-1; k<= i_max+1;k++) {
1198 vec[num]=pi[l*sx+k];
1220 uves_sort(num-1,vec,ord);
1221 a_median=vec[ord[(num-1)/2]];
1222 for(l = j_min-1; l <= j_max+1 ; l++){
1223 for(k = i_min-1 ; k <= i_max+1 ; k++){
1224 if(pm[l*sx+k] == 3) {
1228 po[l*sx+k]=a_median;
1229 }
else if (pm[l*sx+k] == 4) {
1231 po[l*sx+k]=a_median;
1253 for( l = j_min-1 ; l <= j_max+1 ; l++) {
1254 for( k = i_min-1 ; k <= i_max+1 ; k++) {
1255 if(pm[l*sx+k] != -1) {
1264 if (next_x >0)
goto lab100;
1291 return CPL_ERROR_NONE;
1299 static cpl_error_code
1300 uves_find_next(cpl_image** msk,
1305 int sx=cpl_image_get_size_x(*msk);
1306 int sy=cpl_image_get_size_y(*msk);
1315 for(j=first_y;j<sy;j++) {
1321 return CPL_ERROR_NONE;
1329 return CPL_ERROR_NONE;
1357 static cpl_error_code
1358 uves_sort(
const int kmax,
float* inp,
int* ord)
1368 for(k=0;k<kmax;k++) {
1377 for(j=2;j<kmax;j++) {
1398 if(inp[l]<=f)
goto lab4400;
1401 if(f<=inp[l])
goto lab4250;
1422 if(i_max>(i_min+1))
goto lab4200;
1425 for(k=j-2;k>=i_min;k--) {
1430 return CPL_ERROR_NONE;
1432 return CPL_ERROR_NONE;
1463 cpl_parameter* p=NULL;
1464 cpl_parameterlist* pout=NULL;
1466 pout=cpl_parameterlist_new();
1467 p=cpl_parameterlist_get_first((cpl_parameterlist*)pin);
1470 cpl_parameterlist_append(pout,p);
1471 p=cpl_parameterlist_get_next((cpl_parameterlist*)pin);
1499 cpl_error_set(cpl_func,CPL_ERROR_NULL_INPUT);
1533 cpl_error_set(cpl_func,CPL_ERROR_NULL_INPUT);
1560 cpl_frameset *subset = NULL;
1565 assure( frames != NULL, CPL_ERROR_ILLEGAL_INPUT,
"Null frameset" );
1566 assure( tag != NULL, CPL_ERROR_ILLEGAL_INPUT,
"Null tag" );
1568 subset = cpl_frameset_new();
1570 for (f = cpl_frameset_find_const(frames, tag);
1572 f = cpl_frameset_find_const(frames, NULL)) {
1574 cpl_frameset_insert(subset, cpl_frame_duplicate(f));
1595 double result = 1.0;
1644 if (major != NULL) *major = UVES_MAJOR_VERSION;
1645 if (minor != NULL) *minor = UVES_MINOR_VERSION;
1646 if (micro != NULL) *micro = UVES_MICRO_VERSION;
1648 return cpl_error_get_code();
1662 return UVES_BINARY_VERSION;
1679 "This file is part of the ESO UVES Instrument Pipeline\n"
1680 "Copyright (C) 2004,2005,2006 European Southern Observatory\n"
1682 "This program is free software; you can redistribute it and/or modify\n"
1683 "it under the terms of the GNU General Public License as published by\n"
1684 "the Free Software Foundation; either version 2 of the License, or\n"
1685 "(at your option) any later version.\n"
1687 "This program is distributed in the hope that it will be useful,\n"
1688 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1689 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1690 "GNU General Public License for more details.\n"
1692 "You should have received a copy of the GNU General Public License\n"
1693 "along with this program; if not, write to the Free Software\n"
1694 "Foundation, 51 Franklin St, Fifth Floor, Boston, \n"
1695 "MA 02111-1307 USA" ;
1712 #define REQ_CPL_MAJOR 3
1713 #define REQ_CPL_MINOR 1
1714 #define REQ_CPL_MICRO 0
1716 #define REQ_QF_MAJOR 6
1717 #define REQ_QF_MINOR 2
1718 #define REQ_QF_MICRO 0
1721 uves_check_version(
void)
1723 #ifdef CPL_VERSION_CODE
1724 #if CPL_VERSION_CODE >= CPL_VERSION(REQ_CPL_MAJOR, REQ_CPL_MINOR, REQ_CPL_MICRO)
1726 "(version %d-%d-%d, code %d required)",
1727 CPL_VERSION_CODE,
REQ_CPL_MAJOR, REQ_CPL_MINOR, REQ_CPL_MICRO,
1730 #error CPL version too old
1733 #error CPL_VERSION_CODE not defined. CPL version too old
1738 (
int) cpl_version_get_minor() < REQ_CPL_MINOR) ||
1741 cpl_version_get_minor() == REQ_CPL_MINOR &&
1742 (
int) cpl_version_get_micro() < REQ_CPL_MICRO)
1746 "Please update to CPL version %d.%d.%d or later",
1747 cpl_version_get_version(),
1748 cpl_version_get_major(),
1749 cpl_version_get_minor(),
1750 cpl_version_get_micro(),
1757 uves_msg_debug(
"Runtime CPL version %s (%d.%d.%d) detected (%d.%d.%d or later required)",
1758 cpl_version_get_version(),
1759 cpl_version_get_major(),
1760 cpl_version_get_minor(),
1761 cpl_version_get_micro(),
1768 const char *qfts_v =
" ";
1775 qfts_v = qfits_version();
1777 assure( qfts_v != NULL, CPL_ERROR_ILLEGAL_INPUT,
1778 "Error reading qfits version");
1781 qfts_major = strtol(qfts_v, &suffix, 10);
1782 assure( suffix != NULL && suffix[0] ==
'.' && suffix[1] !=
'\0',
1783 CPL_ERROR_ILLEGAL_INPUT,
1784 "Error parsing version string '%s'. "
1785 "Format 'X.Y.Z' expected", qfts_v);
1788 qfts_minor = strtol(suffix+1, &suffix, 10);
1789 assure( suffix != NULL && suffix[0] ==
'.' && suffix[1] !=
'\0',
1790 CPL_ERROR_ILLEGAL_INPUT,
1791 "Error parsing version string '%s'. "
1792 "Format 'X.Y.Z' expected", qfts_v);
1795 qfts_micro = strtol(suffix+1, &suffix, 10);
1798 if (qfts_major < REQ_QF_MAJOR ||
1799 (qfts_major == REQ_QF_MAJOR && qfts_minor < REQ_QF_MINOR) ||
1800 (qfts_major == REQ_QF_MAJOR && qfts_minor == REQ_QF_MINOR &&
1801 qfts_micro < REQ_QF_MICRO)
1805 "Please update to qfits version %d.%d.%d or later",
1814 "(%d.%d.%d or later required)",
1815 qfts_major, qfts_minor, qfts_micro,
1840 uves_end(
const char *recipe_id,
const cpl_frameset *frames)
1842 cpl_frameset *products = NULL;
1846 recipe_id = recipe_id;
1853 products = cpl_frameset_new();
1857 nfrm=cpl_frameset_get_size(frames);
1858 for (i=0;i<nfrm;i++)
1860 f=cpl_frameset_get_frame_const(frames,i);
1861 if (cpl_frame_get_group(f) == CPL_FRAME_GROUP_PRODUCT)
1864 cpl_frameset_insert(products, cpl_frame_duplicate(f)));
1879 (warnings > 1) ?
"s" :
"");
1883 uves_free_frameset(&products);
1884 return cpl_error_get_code();
1911 const char *recipe_id,
const char *short_descr)
1913 char *recipe_string = NULL;
1915 char *spaces1 = NULL;
1916 char *spaces2 = NULL;
1917 char *spaces3 = NULL;
1918 char *spaces4 = NULL;
1919 char *start_time = NULL;
1923 check( uves_check_version(),
"Library validation failed");
1927 const char *plotter_command;
1933 check( uves_get_parameter(parlist, NULL,
"uves",
"msginfolevel",
1934 CPL_TYPE_INT, &msglevel),
1935 "Could not read parameter");
1941 check( uves_get_parameter(parlist, NULL,
"uves",
"plotter",
1942 CPL_TYPE_STRING, &plotter_command),
"Could not read parameter");
1945 check( uves_plot_initialize(plotter_command),
1946 "Could not initialize plotting");
1955 recipe_string = uves_sprintf(
"Recipe: %s", recipe_id);
1957 int field = uves_max_int(strlen(PACKAGE_STRING), strlen(recipe_string));
1958 int nstars = 3+1 + field + 1+3;
1959 int nspaces1, nspaces2, nspaces3, nspaces4;
1963 nspaces1 = (field - strlen(PACKAGE_STRING)) / 2;
1964 nspaces2 = field - strlen(PACKAGE_STRING) - nspaces1;
1966 nspaces3 = (field - strlen(recipe_string)) / 2;
1967 nspaces4 = field - strlen(recipe_string) - nspaces3;
1969 spaces1 = cpl_calloc(nspaces1 + 1,
sizeof(
char));
1970 spaces2 = cpl_calloc(nspaces2 + 1,
sizeof(
char));
1971 spaces3 = cpl_calloc(nspaces3 + 1,
sizeof(
char));
1972 spaces4 = cpl_calloc(nspaces4 + 1,
sizeof(
char));
1973 for (i = 0; i < nspaces1; i++) spaces1[i] =
' ';
1974 for (i = 0; i < nspaces2; i++) spaces2[i] =
' ';
1975 for (i = 0; i < nspaces3; i++) spaces3[i] =
' ';
1976 for (i = 0; i < nspaces4; i++) spaces4[i] =
' ';
1978 stars = cpl_calloc(nstars + 1,
sizeof(
char));
1979 for (i = 0; i < nstars; i++) stars[i] =
'*';
1982 uves_msg(
"*** %s%s%s ***", spaces1, PACKAGE_STRING, spaces2);
1983 uves_msg(
"*** %s%s%s ***", spaces3, recipe_string, spaces4);
1987 uves_msg(
"This recipe %c%s", tolower(short_descr[0]), short_descr+1);
1989 if (cpl_frameset_is_empty(frames)) {
1990 uves_msg_debug(
"Guvf cvcryvar unf ernpurq vgf uvtu dhnyvgl qhr na npgvir "
1991 "hfre pbzzhavgl naq gur erfcbafvoyr naq vqrnyvfgvp jbex bs "
1992 "vaqvivqhny cvcryvar qrirybcref, naq qrfcvgr orvat 'onfrq ba' "
1993 "PCY juvpu vf n cvrpr bs cbyvgvpny penc");
1999 check( uves_dfs_set_groups(frames),
"Could not classify input frames");
2006 cpl_free(recipe_string);
2047 const cpl_image *image2,
const cpl_image *noise2,
2050 cpl_image *result = NULL;
2055 assure( image1 != NULL, CPL_ERROR_NULL_INPUT,
"Null image");
2056 assure( image2 != NULL, CPL_ERROR_NULL_INPUT,
"Null image");
2057 assure( noise1 != NULL, CPL_ERROR_NULL_INPUT,
"Null image");
2058 assure( noise2 != NULL, CPL_ERROR_NULL_INPUT,
"Null image");
2059 assure( noise != NULL, CPL_ERROR_NULL_INPUT,
"Null image");
2061 assure( cpl_image_get_min(noise1) > 0, CPL_ERROR_ILLEGAL_INPUT,
2062 "Noise must be everywhere positive, minimum = %e", cpl_image_get_min(noise1));
2063 assure( cpl_image_get_min(noise2) > 0, CPL_ERROR_ILLEGAL_INPUT,
2064 "Noise must be everywhere positive, minimum = %e", cpl_image_get_min(noise2));
2066 nx = cpl_image_get_size_x(image1);
2067 ny = cpl_image_get_size_y(image1);
2069 assure( nx == cpl_image_get_size_x(image2), CPL_ERROR_INCOMPATIBLE_INPUT,
2070 "Size mismatch %" CPL_SIZE_FORMAT
" != %" CPL_SIZE_FORMAT
"",
2071 nx, cpl_image_get_size_x(image2));
2072 assure( nx == cpl_image_get_size_x(noise1), CPL_ERROR_INCOMPATIBLE_INPUT,
2073 "Size mismatch %" CPL_SIZE_FORMAT
" != %" CPL_SIZE_FORMAT
"",
2074 nx, cpl_image_get_size_x(noise1));
2075 assure( nx == cpl_image_get_size_x(noise2), CPL_ERROR_INCOMPATIBLE_INPUT,
2076 "Size mismatch %" CPL_SIZE_FORMAT
" != %" CPL_SIZE_FORMAT
"",
2077 nx, cpl_image_get_size_x(noise2));
2078 assure( ny == cpl_image_get_size_y(image2), CPL_ERROR_INCOMPATIBLE_INPUT,
2079 "Size mismatch %" CPL_SIZE_FORMAT
" != %" CPL_SIZE_FORMAT
"",
2080 ny, cpl_image_get_size_y(image2));
2081 assure( ny == cpl_image_get_size_y(noise1), CPL_ERROR_INCOMPATIBLE_INPUT,
2082 "Size mismatch %" CPL_SIZE_FORMAT
" != %" CPL_SIZE_FORMAT
"",
2083 ny, cpl_image_get_size_y(noise1));
2084 assure( ny == cpl_image_get_size_y(noise2), CPL_ERROR_INCOMPATIBLE_INPUT,
2085 "Size mismatch %" CPL_SIZE_FORMAT
" != %" CPL_SIZE_FORMAT
"",
2086 ny, cpl_image_get_size_y(noise2));
2088 result = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
2089 *noise = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
2092 for (y = 1; y <= ny; y++)
2094 for (x = 1; x <= nx; x++)
2096 double flux1, flux2;
2097 double sigma1, sigma2;
2098 int pis_rejected1, noise_rejected1;
2099 int pis_rejected2, noise_rejected2;
2101 flux1 = cpl_image_get(image1, x, y, &pis_rejected1);
2102 flux2 = cpl_image_get(image2, x, y, &pis_rejected2);
2103 sigma1 = cpl_image_get(noise1, x, y, &noise_rejected1);
2104 sigma2 = cpl_image_get(noise2, x, y, &noise_rejected2);
2106 pis_rejected1 = pis_rejected1 || noise_rejected1;
2107 pis_rejected2 = pis_rejected2 || noise_rejected2;
2109 if (pis_rejected1 && pis_rejected2)
2111 cpl_image_reject(result, x, y);
2112 cpl_image_reject(*noise, x, y);
2120 if (pis_rejected1 && !pis_rejected2)
2125 else if (!pis_rejected1 && pis_rejected2)
2134 1 / (sigma1*sigma1) +
2135 1 / (sigma2*sigma2);
2137 flux = flux1/(sigma1*sigma1) + flux2/(sigma2*sigma2);
2140 sigma = sqrt(sigma);
2143 cpl_image_set(result, x, y, flux);
2144 cpl_image_set(*noise, x, y, sigma);
2150 if (cpl_error_get_code() != CPL_ERROR_NONE)
2152 uves_free_image(&result);
2175 const char *cunit1,
const char *cunit2,
2176 const char *bunit,
const double bscale,
2177 double crval1,
double crval2,
2178 double crpix1,
double crpix2,
2179 double cdelt1,
double cdelt2)
2188 if(cunit2 != NULL) {
2228 int ncom,
enum uves_chip chip)
2235 cpl_image *noise = NULL;
2243 const double *image_data;
2244 bool has_bnoise=
false;
2245 bool has_dnoise=
false;
2253 double tot_noise2=0;
2254 double var_bias_dark=0;
2257 assure( ncom >= 1, CPL_ERROR_ILLEGAL_INPUT,
"Number of combined frames = %d", ncom);
2260 "Could not read read-out noise");
2263 "Could not read gain factor");
2264 assure( gain > 0, CPL_ERROR_ILLEGAL_INPUT,
"Non-positive gain: %e", gain);
2266 nx = cpl_image_get_size_x(image);
2267 ny = cpl_image_get_size_y(image);
2274 assure(cpl_image_get_type(image) == CPL_TYPE_DOUBLE,
2275 CPL_ERROR_UNSUPPORTED_MODE,
2276 "Input image is of type %s. double expected",
2279 noise = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
2282 noise_data = cpl_image_get_data_double(noise);
2284 image_data = cpl_image_get_data_double_const(image);
2287 if(image_header != NULL) {
2294 bnoise2=bnoise*bnoise;
2299 dnoise2=dnoise*dnoise;
2302 exptime2=exptime*exptime/dtime/dtime;
2304 var_bias_dark=bnoise2+dnoise2*exptime2;
2305 uves_msg_debug(
"bnoise=%g dnoise=%g sci exptime=%g dark exptime=%g",
2306 bnoise,dnoise,exptime,dtime);
2326 double median_factor = (ncom > 1) ? 2.0/M_PI : 1.0;
2327 double gain2=gain*gain;
2329 double quant_var = uves_max_double(0, (1 - gain2)/12.0);
2333 double flux_var_adu=0;
2334 double ron2=ron*ron;
2335 double inv_ncom_median_factor=1./(ncom * median_factor);
2336 for (i = 0; i < nx*ny; i++)
2341 flux_var_adu = uves_max_double(image_data[i],0)*gain;
2362 tot_noise2=(( ron2 + quant_var + flux_var_adu )*inv_ncom_median_factor)+
2368 noise_data[i] = sqrt(tot_noise2);
2373 if (cpl_error_get_code() != CPL_ERROR_NONE)
2375 uves_free_image(&noise);
2396 passure ( image != NULL,
" ");
2397 passure ( master_bias != NULL,
" ");
2399 check( cpl_image_subtract(image, master_bias),
2400 "Error subtracting bias");
2414 check( cpl_image_threshold(image,
2417 "Error thresholding image");
2421 return cpl_error_get_code();
2439 const cpl_image *master_dark,
2442 cpl_image *normalized_mdark = NULL;
2443 double image_exptime = 0.0;
2444 double mdark_exptime = 0.0;
2446 passure ( image != NULL,
" ");
2447 passure ( image_header != NULL,
" ");
2448 passure ( master_dark != NULL,
" ");
2449 passure ( mdark_header != NULL,
" ");
2453 "Error reading input image exposure time");
2455 "Error reading master dark exposure time");
2457 uves_msg(
"Rescaling master dark from %f s to %f s exposure time",
2458 mdark_exptime, image_exptime);
2460 check( normalized_mdark =
2461 cpl_image_multiply_scalar_create(master_dark,
2462 image_exptime / mdark_exptime),
2463 "Error normalizing master dark");
2465 check( cpl_image_subtract(image, normalized_mdark),
2466 "Error subtracting master dark");
2468 uves_msg_warning(
"noise rescaled master dark %g",cpl_image_get_stdev(normalized_mdark));
2472 uves_free_image(&normalized_mdark);
2473 return cpl_error_get_code();
2493 return (first_abs_order +
2494 (relative_order-1)*((last_abs_order > first_abs_order) ? 1 : -1));
2515 const char *residual2,
2518 double mean = 0, median, sigma2;
2523 check_nomsg(median = cpl_table_get_column_median(t, column));
2527 check_nomsg(cpl_table_duplicate_column(t, residual2, t, column));
2528 check_nomsg(cpl_table_subtract_scalar(t, residual2, median));
2529 check_nomsg(cpl_table_multiply_columns(t, residual2, residual2));
2536 check_nomsg(sigma2 = cpl_table_get_column_median(t, residual2) / (0.6744 * 0.6744));
2541 check_nomsg( rejected = uves_erase_table_rows(t, residual2,
2543 kappa*kappa*sigma2));
2545 check_nomsg(cpl_table_erase_column(t, residual2));
2547 }
while (rejected > 0);
2549 check_nomsg(mean = cpl_table_get_column_mean(t, column));
2591 const char *X,
const char *Y,
const char *sigmaY,
2593 const char *polynomial_fit,
const char *residual_square,
2594 double *mean_squared_error,
double kappa)
2597 int total_rejected = 0;
2604 cpl_vector *vx = NULL;
2605 cpl_vector *vy = NULL;
2606 cpl_vector *vsy = NULL;
2610 assure( t != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
2611 assure( X != NULL, CPL_ERROR_NULL_INPUT,
"Null column name");
2612 assure( Y != NULL, CPL_ERROR_NULL_INPUT,
"Null column name");
2613 assure( cpl_table_has_column(t, X), CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", X);
2614 assure( cpl_table_has_column(t, Y), CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", Y);
2615 assure( sigmaY == NULL || cpl_table_has_column(t, sigmaY) , CPL_ERROR_ILLEGAL_INPUT,
2616 "No such column: %s", sigmaY);
2618 assure( polynomial_fit == NULL || !cpl_table_has_column(t, polynomial_fit),
2619 CPL_ERROR_ILLEGAL_INPUT,
"Column '%s' already present", polynomial_fit);
2621 assure( residual_square == NULL || !cpl_table_has_column(t, residual_square),
2622 CPL_ERROR_ILLEGAL_INPUT,
"Column '%s' already present", residual_square);
2625 type = cpl_table_get_column_type(t, Y);
2626 assure( type == CPL_TYPE_DOUBLE || type == CPL_TYPE_INT, CPL_ERROR_INVALID_TYPE,
2628 type = cpl_table_get_column_type(t, X);
2629 assure( type == CPL_TYPE_DOUBLE || type == CPL_TYPE_INT, CPL_ERROR_INVALID_TYPE,
2633 type = cpl_table_get_column_type(t, sigmaY);
2634 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE,
2635 CPL_ERROR_INVALID_TYPE,
2636 "Input column '%s' has wrong type (%s)",
2640 check( cpl_table_cast_column(t, X,
"_X_double", CPL_TYPE_DOUBLE),
2641 "Could not cast table column '%s' to double", X);
2642 check( cpl_table_cast_column(t, Y,
"_Y_double", CPL_TYPE_DOUBLE),
2643 "Could not cast table column '%s' to double", Y);
2646 check( cpl_table_cast_column(t, sigmaY,
"_sY_double", CPL_TYPE_DOUBLE),
2647 "Could not cast table column '%s' to double", sigmaY);
2653 check( cpl_table_new_column(t,
"_residual_square", CPL_TYPE_DOUBLE),
2654 "Could not create column");
2656 check( (N = cpl_table_get_nrow(t),
2657 x = cpl_table_get_data_double(t,
"_X_double"),
2658 y = cpl_table_get_data_double(t,
"_Y_double")),
2659 "Could not read table data");
2663 check( sy = cpl_table_get_data_double(t,
"_sY_double"),
2664 "Could not read table data");
2671 assure( N > 0, CPL_ERROR_ILLEGAL_INPUT,
"Empty table. "
2672 "No points to fit in poly 1d regression. At least 2 needed");
2674 assure( N > degree, CPL_ERROR_ILLEGAL_INPUT,
"%d points to fit in poly 1d "
2675 "regression of degree %d. At least %d needed.",
2679 uves_unwrap_vector(&vx);
2680 uves_unwrap_vector(&vy);
2682 vx = cpl_vector_wrap(N, x);
2683 vy = cpl_vector_wrap(N, y);
2687 uves_unwrap_vector(&vsy);
2688 vsy = cpl_vector_wrap(N, sy);
2698 "Could not fit polynomial");
2706 for (i = 0; i < N; i++)
2708 double xval, yval, yfit;
2710 check(( xval = cpl_table_get_double(t,
"_X_double", i, NULL),
2711 yval = cpl_table_get_double(t,
"_Y_double" ,i, NULL),
2714 cpl_table_set_double(t,
"_residual_square", i,
2715 (yfit-yval)*(yfit-yval))),
2716 "Could not evaluate polynomial");
2725 sigma2 = cpl_table_get_column_median(t,
"_residual_square") / (0.6744 * 0.6744);
2728 check( rejected = uves_erase_table_rows(t,
"_residual_square",
2729 CPL_GREATER_THAN, kappa*kappa*sigma2),
2730 "Could not remove outlier points");
2732 uves_msg_debug(
"%d of %d points rejected in kappa-sigma clipping. rms=%f",
2733 rejected, N, sqrt(mse));
2736 total_rejected += rejected;
2737 N = cpl_table_get_nrow(t);
2740 }
while (rejected > 0);
2742 cpl_table_erase_column(t,
"_residual_square");
2746 uves_msg_debug(
"%d of %d points (%f %%) rejected in kappa-sigma clipping",
2749 (100.0*total_rejected)/(N + total_rejected)
2753 if (mean_squared_error != NULL) *mean_squared_error = mse;
2756 if (polynomial_fit != NULL || residual_square != NULL)
2760 check( cpl_table_new_column(t,
"_polynomial_fit", CPL_TYPE_DOUBLE),
2761 "Could not create column");
2762 for (i = 0; i < N; i++){
2767 xval = cpl_table_get_double(t,
"_X_double", i, NULL),
2769 cpl_table_set_double(t,
"_polynomial_fit", i, yfit)),
2770 "Could not evaluate polynomial");
2774 if (residual_square != NULL)
2776 check(( cpl_table_duplicate_column(t, residual_square,
2777 t,
"_polynomial_fit"),
2778 cpl_table_subtract_columns(t, residual_square, Y),
2779 cpl_table_multiply_columns(t, residual_square, residual_square)),
2781 "Could not calculate Residual of fit");
2785 if (polynomial_fit != NULL)
2787 cpl_table_name_column(t,
"_polynomial_fit", polynomial_fit);
2791 cpl_table_erase_column(t,
"_polynomial_fit");
2795 check(( cpl_table_erase_column(t,
"_X_double"),
2796 cpl_table_erase_column(t,
"_Y_double")),
2797 "Could not delete temporary columns");
2801 check( cpl_table_erase_column(t,
"_sY_double"),
2802 "Could not delete temporary column");
2806 uves_unwrap_vector(&vx);
2807 uves_unwrap_vector(&vy);
2808 uves_unwrap_vector(&vsy);
2809 if (cpl_error_get_code() != CPL_ERROR_NONE)
2870 const char *X1,
const char *X2,
const char *Y,
2872 int degree1,
int degree2,
2873 const char *polynomial_fit,
const char *residual_square,
2874 const char *variance_fit,
2875 double *mse,
double *red_chisq,
2889 cpl_vector *vx1 = NULL;
2890 cpl_vector *vx2 = NULL;
2891 cpl_bivector *vx = NULL;
2892 cpl_vector *vy = NULL;
2893 cpl_vector *vsy= NULL;
2897 assure( t != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
2898 N = cpl_table_get_nrow(t);
2899 assure( N > 0, CPL_ERROR_ILLEGAL_INPUT,
"The table with column to compute regression has 0 rows!");
2900 assure( N > 8, CPL_ERROR_ILLEGAL_INPUT,
"For poly regression you need at least 9 points. The table with column to compute regression has %d rows!",N);
2902 assure( cpl_table_has_column(t, X1), CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", X1);
2903 assure( cpl_table_has_column(t, X2), CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", X2);
2904 assure( cpl_table_has_column(t, Y) , CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", Y);
2905 assure( (variance == NULL && variance_fit == NULL) || sigmaY != NULL,
2906 CPL_ERROR_INCOMPATIBLE_INPUT,
"Cannot calculate variances without sigmaY");
2909 assure( cpl_table_has_column(t, sigmaY) , CPL_ERROR_ILLEGAL_INPUT,
2910 "No such column: %s", sigmaY);
2912 if (polynomial_fit != NULL)
2914 assure( !cpl_table_has_column(t, polynomial_fit) , CPL_ERROR_ILLEGAL_INPUT,
2915 "Table already has '%s' column", polynomial_fit);
2917 if (residual_square != NULL)
2919 assure( !cpl_table_has_column(t, residual_square), CPL_ERROR_ILLEGAL_INPUT,
2920 "Table already has '%s' column", residual_square);
2922 if (variance_fit != NULL)
2924 assure( !cpl_table_has_column(t, variance_fit) , CPL_ERROR_ILLEGAL_INPUT,
2925 "Table already has '%s' column", variance_fit);
2929 type = cpl_table_get_column_type(t, X1);
2930 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
2932 type = cpl_table_get_column_type(t, X2);
2933 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
2935 type = cpl_table_get_column_type(t, Y);
2936 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
2940 type = cpl_table_get_column_type(t, sigmaY);
2941 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
2942 "Input column '%s' has wrong type (%s)",
2947 check( cpl_table_cast_column(t, X1 ,
"_X1_double", CPL_TYPE_DOUBLE),
2948 "Could not cast table column to double");
2949 check( cpl_table_cast_column(t, X2 ,
"_X2_double", CPL_TYPE_DOUBLE),
2950 "Could not cast table column to double");
2951 check( cpl_table_cast_column(t, Y ,
"_Y_double", CPL_TYPE_DOUBLE),
2952 "Could not cast table column to double");
2955 check( cpl_table_cast_column(t, sigmaY,
"_sY_double", CPL_TYPE_DOUBLE),
2956 "Could not cast table column to double");
2961 check( cpl_table_new_column(t,
"_residual_square", CPL_TYPE_DOUBLE),
2962 "Could not create column");
2967 check(( N = cpl_table_get_nrow(t),
2968 x1 = cpl_table_get_data_double(t,
"_X1_double"),
2969 x2 = cpl_table_get_data_double(t,
"_X2_double"),
2970 y = cpl_table_get_data_double(t,
"_Y_double"),
2971 res= cpl_table_get_data_double(t,
"_residual_square")),
2972 "Could not read table data");
2976 check (sy = cpl_table_get_data_double(t,
"_sY_double"),
2977 "Could not read table data");
2984 assure( N > 0, CPL_ERROR_ILLEGAL_INPUT,
"Empty table");
2987 uves_unwrap_vector(&vx1);
2988 uves_unwrap_vector(&vx2);
2989 uves_unwrap_vector(&vy);
2991 vx1 = cpl_vector_wrap(N, x1);
2992 vx2 = cpl_vector_wrap(N, x2);
2993 vy = cpl_vector_wrap(N, y);
2996 uves_unwrap_vector(&vsy);
2997 vsy = cpl_vector_wrap(N, sy);
3005 uves_unwrap_bivector_vectors(&vx);
3006 vx = cpl_bivector_wrap_vectors(vx1, vx2);
3012 "Could not fit polynomial");
3020 cpl_table_fill_column_window_double(t,
"_residual_square", 0,
3021 cpl_table_get_nrow(t), 0.0);
3023 for (i = 0; i < N; i++)
3029 res[i] = (yfit-y[i])*(yfit-y[i]);
3038 sigma2 = cpl_table_get_column_median(t,
"_residual_square") / (0.6744 * 0.6744);
3042 check( rejected = uves_erase_table_rows(t,
"_residual_square",
3043 CPL_GREATER_THAN, kappa*kappa*sigma2),
3044 "Could not remove outlier points");
3048 uves_msg_debug(
"%d of %d points rejected in kappa-sigma clipping. rms=%f",
3049 rejected, N, sqrt(sigma2));
3052 total_rejected += rejected;
3053 N = cpl_table_get_nrow(t);
3059 }
while (rejected > 0 && rejected > min_reject*(N+rejected) &&
3060 N >= (degree1 + 1)*(degree2 + 1) + 1);
3064 uves_msg_debug(
"%d of %d points (%f %%) rejected in kappa-sigma clipping",
3067 (100.0*total_rejected)/(N + total_rejected)
3077 check(( N = cpl_table_get_nrow(t),
3078 x1 = cpl_table_get_data_double(t,
"_X1_double"),
3079 x2 = cpl_table_get_data_double(t,
"_X2_double"),
3080 y = cpl_table_get_data_double(t,
"_Y_double"),
3081 res= cpl_table_get_data_double(t,
"_residual_square")),
3082 "Could not read table data");
3086 check (sy = cpl_table_get_data_double(t,
"_sY_double"),
3087 "Could not read table data");
3094 assure( N > 0, CPL_ERROR_ILLEGAL_INPUT,
"Empty table");
3097 uves_unwrap_vector(&vx1);
3098 uves_unwrap_vector(&vx2);
3099 uves_unwrap_vector(&vy);
3101 vx1 = cpl_vector_wrap(N, x1);
3102 vx2 = cpl_vector_wrap(N, x2);
3103 vy = cpl_vector_wrap(N, y);
3106 uves_unwrap_vector(&vsy);
3107 vsy = cpl_vector_wrap(N, sy);
3115 uves_unwrap_bivector_vectors(&vx);
3116 vx = cpl_bivector_wrap_vectors(vx1, vx2);
3120 if (variance_fit != NULL || variance != NULL)
3124 mse, red_chisq, &variance_local),
3125 "Could not fit polynomial");
3130 mse, red_chisq, NULL),
3131 "Could not fit polynomial");
3134 cpl_table_erase_column(t,
"_residual_square");
3137 if (polynomial_fit != NULL || residual_square != NULL)
3142 check( cpl_table_new_column(t,
"_polynomial_fit", CPL_TYPE_DOUBLE),
3143 "Could not create column");
3145 cpl_table_fill_column_window_double(t,
"_polynomial_fit", 0,
3146 cpl_table_get_nrow(t), 0.0);
3148 x1 = cpl_table_get_data_double(t,
"_X1_double");
3149 x2 = cpl_table_get_data_double(t,
"_X2_double");
3150 pf = cpl_table_get_data_double(t,
"_polynomial_fit");
3152 for (i = 0; i < N; i++){
3154 double x1val, x2val, yfit;
3156 check(( x1val = cpl_table_get_double(t,
"_X1_double", i, NULL),
3157 x2val = cpl_table_get_double(t,
"_X2_double", i, NULL),
3160 cpl_table_set_double(t,
"_polynomial_fit", i, yfit)),
3161 "Could not evaluate polynomial");
3169 if (residual_square != NULL)
3171 check(( cpl_table_duplicate_column(t, residual_square,
3172 t,
"_polynomial_fit"),
3173 cpl_table_subtract_columns(t, residual_square, Y),
3174 cpl_table_multiply_columns(t, residual_square, residual_square)),
3176 "Could not calculate Residual of fit");
3180 if (polynomial_fit != NULL)
3182 cpl_table_name_column(t,
"_polynomial_fit", polynomial_fit);
3186 cpl_table_erase_column(t,
"_polynomial_fit");
3191 if (variance_fit != NULL)
3196 check( cpl_table_new_column(t, variance_fit, CPL_TYPE_DOUBLE),
3197 "Could not create column");
3199 cpl_table_fill_column_window_double(t, variance_fit, 0,
3200 cpl_table_get_nrow(t), 0.0);
3202 x1 = cpl_table_get_data_double(t,
"_X1_double");
3203 x2 = cpl_table_get_data_double(t,
"_X2_double");
3204 vf = cpl_table_get_data_double(t, variance_fit);
3206 for (i = 0; i < N; i++)
3209 double x1val, x2val, yfit_variance;
3210 check(( x1val = cpl_table_get_double(t,
"_X1_double", i, NULL),
3211 x2val = cpl_table_get_double(t,
"_X2_double", i, NULL),
3215 cpl_table_set_double(t, variance_fit, i, yfit_variance)),
3216 "Could not evaluate polynomial");
3225 check(( cpl_table_erase_column(t,
"_X1_double"),
3226 cpl_table_erase_column(t,
"_X2_double"),
3227 cpl_table_erase_column(t,
"_Y_double")),
3228 "Could not delete temporary columns");
3232 check( cpl_table_erase_column(t,
"_sY_double"),
3233 "Could not delete temporary column");
3237 uves_unwrap_bivector_vectors(&vx);
3238 uves_unwrap_vector(&vx1);
3239 uves_unwrap_vector(&vx2);
3240 uves_unwrap_vector(&vy);
3241 uves_unwrap_vector(&vsy);
3243 if (variance != NULL)
3245 *variance = variance_local;
3251 if (cpl_error_get_code() != CPL_ERROR_NONE)
3306 const char *X1,
const char *X2,
const char *Y,
3308 const char *polynomial_fit,
3309 const char *residual_square,
3310 const char *variance_fit,
3311 double *mean_squared_error,
double *red_chisq,
3313 int maxdeg1,
int maxdeg2,
double min_rms,
3316 const double *min_val,
3317 const double *max_val,
3318 int npos,
double positions[][2])
3324 double **mse = NULL;
3325 bool adjust1 =
true;
3326 bool adjust2 =
true;
3327 bool finished =
false;
3330 cpl_table *temp = NULL;
3333 assure( (min_val == NULL && max_val == NULL) || positions != NULL,
3334 CPL_ERROR_NULL_INPUT,
3335 "Missing positions array");
3337 check_nomsg( y_unit = cpl_table_get_column_unit(t, Y));
3343 assure(maxdeg1 >= 1 && maxdeg2 >= 1, CPL_ERROR_ILLEGAL_INPUT,
3344 "Illegal max. degrees: (%d, %d)",
3347 mse = cpl_calloc(maxdeg1+1,
sizeof(
double *));
3349 for (i = 0; i < maxdeg1+1; i++)
3352 mse[i] = cpl_calloc(maxdeg2+1,
sizeof(
double));
3355 for (j = 0; j < maxdeg2+1; j++)
3361 temp = cpl_table_duplicate(t);
3370 &mse[deg1][deg2], NULL,
3373 "Error fitting polynomial");
3375 uves_msg_low(
"(%d, %d)-degree: RMS = %.3g %s (%" CPL_SIZE_FORMAT
"/%" CPL_SIZE_FORMAT
" outliers)",
3376 deg1, deg2, sqrt(mse[deg1][deg2]), y_unit,
3377 cpl_table_get_nrow(t) - cpl_table_get_nrow(temp),
3378 cpl_table_get_nrow(t));
3380 uves_msg_debug(
"(%d, %d)-degree: RMS = %.3g %s (%" CPL_SIZE_FORMAT
"/%" CPL_SIZE_FORMAT
" outliers)",
3381 deg1, deg2, sqrt(mse[deg1][deg2]), y_unit,
3382 cpl_table_get_nrow(t) - cpl_table_get_nrow(temp),
3383 cpl_table_get_nrow(t));
3388 int new_deg1, new_deg2;
3393 adjust1 = adjust1 && (deg1 + 2 <= maxdeg1);
3394 adjust2 = adjust2 && (deg2 + 2 <= maxdeg2);
3410 for (new_deg1 = deg1; new_deg1 <= deg1+2; new_deg1++)
3411 for (new_deg2 = deg2; new_deg2 <= deg2+2; new_deg2++)
3413 (new_deg1 == deg1+1 && new_deg2 == deg2 && adjust1) ||
3414 (new_deg1 == deg1+2 && new_deg2 == deg2 && adjust1) ||
3415 (new_deg1 == deg1 && new_deg2 == deg2+1 && adjust2) ||
3416 (new_deg1 == deg1 && new_deg2 == deg2+2 && adjust2) ||
3417 (new_deg1 == deg1+1 && new_deg2 == deg2+1 && adjust1 && adjust2)
3419 && mse[new_deg1][new_deg2] < 0)
3423 uves_free_table(&temp);
3424 temp = cpl_table_duplicate(t);
3439 if (cpl_error_get_code() == CPL_ERROR_SINGULAR_MATRIX)
3445 new_deg1, new_deg2);
3448 new_deg1, new_deg2);
3450 mse[new_deg1][new_deg2] = DBL_MAX/2;
3454 assure( cpl_error_get_code() == CPL_ERROR_NONE,
3455 cpl_error_get_code(),
3456 "Error fitting (%d, %d)-degree polynomial",
3457 new_deg1, new_deg2 );
3459 rejected = cpl_table_get_nrow(t) - cpl_table_get_nrow(temp);
3462 uves_msg_low(
"(%d,%d)-degree: RMS = %.3g %s (%d/%" CPL_SIZE_FORMAT
" outliers)",
3463 new_deg1, new_deg2, sqrt(mse[new_deg1][new_deg2]), y_unit,
3464 rejected, cpl_table_get_nrow(t));
3466 uves_msg_debug(
"(%d,%d)-degree: RMS = %.3g %s (%d/%" CPL_SIZE_FORMAT
" outliers)",
3467 new_deg1, new_deg2, sqrt(mse[new_deg1][new_deg2]), y_unit,
3468 rejected, cpl_table_get_nrow(t));
3471 if (min_val != NULL || max_val != NULL)
3473 for (i = 0; i < npos; i++)
3477 positions[i][0], positions[i][1]);
3478 if (min_val != NULL && val < *min_val)
3483 mse[new_deg1][new_deg2] = DBL_MAX/2;
3486 if (max_val != NULL && val > *max_val)
3491 mse[new_deg1][new_deg2] = DBL_MAX/2;
3499 if (rejected >= (4*cpl_table_get_nrow(t))/5)
3501 mse[new_deg1][new_deg2] = DBL_MAX/2;
3515 m = mse[deg1][deg2];
3518 && (m - mse[deg1+1][deg2])/m > 0.1
3519 && (!adjust2 || mse[deg1+1][deg2] <= mse[deg1][deg2+1])
3531 (m - mse[deg1][deg2+1])/m > 0.1
3532 && (!adjust1 || mse[deg1+1][deg2] > mse[deg1][deg2+1])
3538 else if (adjust1 && adjust2 && (m - mse[deg1+1][deg2+1])/m > 0.1)
3545 && (m - mse[deg1+2][deg2])/m > 0.1
3546 && (!adjust2 || mse[deg1+2][deg2] <= mse[deg1][deg2+2])
3553 && (m - mse[deg1][deg2+2])/m > 0.1
3554 && (!adjust1 || mse[deg1+2][deg2] < mse[deg1][deg2+2]))
3561 finished = finished || (sqrt(mse[deg1][deg2]) < min_rms);
3563 }
while (!finished);
3570 polynomial_fit, residual_square,
3572 mean_squared_error, red_chisq,
3573 variance, kappa, min_reject),
3574 "Error fitting (%d, %d)-degree polynomial", deg1, deg2);
3577 uves_msg_low(
"Using degree (%d, %d), RMS = %.3g %s", deg1, deg2,
3578 sqrt(mse[deg1][deg2]), y_unit);
3580 uves_msg_debug(
"Using degree (%d, %d), RMS = %.3g %s", deg1, deg2,
3581 sqrt(mse[deg1][deg2]), y_unit);
3586 for (i = 0; i < maxdeg1+1; i++)
3595 uves_free_table(&temp);
3597 return bivariate_fit;
3614 const char *result = NULL;
3615 unsigned int prefix_length;
3617 assure( s != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
3618 assure( prefix != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
3620 prefix_length = strlen(prefix);
3622 assure( strlen(s) >= prefix_length &&
3623 strncmp(s, prefix, prefix_length) == 0,
3624 CPL_ERROR_INCOMPATIBLE_INPUT,
"'%s' is not a prefix of '%s'",
3627 result = s + prefix_length;
3647 static double V1, V2, S;
3648 static int phase = 0;
3653 double U1 = (double)rand() / RAND_MAX;
3654 double U2 = (double)rand() / RAND_MAX;
3658 S = V1 * V1 + V2 * V2;
3659 }
while(S >= 1 || S == 0);
3661 X = V1 * sqrt(-2 * log(S) / S);
3663 X = V2 * sqrt(-2 * log(S) / S);
3684 const char *column_y,
int *istart )
3689 const double *x, *y;
3691 check( x = cpl_table_get_data_double_const(t, column_x),
3692 "Error reading column '%s'", column_x);
3693 check( y = cpl_table_get_data_double_const(t, column_y),
3694 "Error reading column '%s'", column_y);
3696 n = cpl_table_get_nrow(t);
3723 double yp1, yp2, yp = 0;
3724 double xpi, xpi1, l1, l2, lp1, lp2;
3727 if ( x[0] <= x[n-1] && (xp < x[0] || xp > x[n-1]) )
return 0.0;
3728 if ( x[0] > x[n-1] && (xp > x[0] || xp < x[n-1]) )
return 0.0;
3730 if ( x[0] <= x[n-1] )
3732 for ( i = (*istart)+1; i <= n && xp >= x[i-1]; i++ )
3737 for ( i = (*istart)+1; i <= n && xp <= x[i-1]; i++ )
3744 lp1 = 1.0 / (x[i-1] - x[i]);
3749 yp1 = (y[1] - y[0]) / (x[1] - x[0]);
3753 yp1 = (y[i] - y[i-2]) / (x[i] - x[i-2]);
3758 yp2 = (y[n-1] - y[n-2]) / (x[n-1] - x[n-2]);
3762 yp2 = (y[i+1] - y[i-1]) / (x[i+1] - x[i-1]);
3770 yp = y[i-1]*(1 - 2.0*lp1*xpi)*l1*l1 +
3771 y[i]*(1 - 2.0*lp2*xpi1)*l2*l2 +
3772 yp1*xpi*l1*l1 + yp2*xpi1*l2*l2;
3796 double a, b, h, yp = 0;
3806 if ( xp < x[1] || xp > x[n] )
3810 else if ( xp == x[1] )
3815 for ( k = klo; k < n && xp > x[k]; k++ )
3818 klo = *kstart = k-1;
3821 h = x[khi] - x[klo];
3822 assure( h != 0.0, CPL_ERROR_DIVISION_BY_ZERO,
3823 "Empty x-value range: xlo = %e ; xhi = %e", x[khi], x[klo]);
3825 a = (x[khi] - xp) / h;
3826 b = (xp - x[klo]) / h;
3828 yp = a*y[klo] + b*y[khi] + ((a*a*a - a)*y2[klo] + (b*b*b - b)*y2[khi])*
3849 bool is_sorted =
true;
3852 double previous, current;
3855 passure(cpl_table_has_column(t, column),
"No column '%s'", column);
3856 passure(cpl_table_get_column_type(t, column) == CPL_TYPE_DOUBLE,
" ");
3858 N = cpl_table_get_nrow(t);
3862 previous = cpl_table_get_double(t, column, 0, NULL);
3864 for(i = 1; i < N && is_sorted; i++)
3866 current = cpl_table_get_double(t, column, i, NULL);
3870 is_sorted = is_sorted && ( current >= previous );
3875 is_sorted = is_sorted && ( current <= previous );
3900 cpl_table *result = NULL;
3903 result = cpl_table_new(0),
3904 cpl_table_new_column(result,
"TraceID" , CPL_TYPE_INT),
3905 cpl_table_new_column(result,
"Offset" , CPL_TYPE_DOUBLE),
3906 cpl_table_new_column(result,
"Tracemask", CPL_TYPE_INT)),
3907 "Error creating table");
3926 int fibre_ID,
double fibre_offset,
int fibre_mask)
3930 assure( traces != NULL, CPL_ERROR_NULL_INPUT,
"Null table!");
3934 size = cpl_table_get_nrow(traces),
3935 cpl_table_set_size (traces, size+1),
3936 cpl_table_set_int (traces,
"TraceID" , size, fibre_ID),
3937 cpl_table_set_double(traces,
"Offset" , size, fibre_offset),
3938 cpl_table_set_int (traces,
"Tracemask", size, fibre_mask)),
3939 "Error updating table");
3942 return cpl_error_get_code();
3956 cpl_table* tab=NULL;
3958 tab=cpl_table_load(tname,1,0);
3961 check_nomsg(uves_table_save(tab,head,NULL,tname,CPL_IO_DEFAULT));
3964 uves_free_table(&tab);
3965 uves_free_propertylist(&head);
3966 return cpl_error_get_code();
3982 cpl_table* tab1=NULL;
3983 cpl_table* tab2=NULL;
3986 tab1=cpl_table_load(tname1,1,0);
3988 tab2=cpl_table_load(tname2,1,0);
3992 check_nomsg(uves_table_save(tab2,head2,NULL,tname2,CPL_IO_DEFAULT));
3995 uves_free_table(&tab1);
3996 uves_free_table(&tab2);
3997 uves_free_propertylist(&head2);
3998 return cpl_error_get_code();
4015 const char* colname=NULL;
4017 cpl_array *names=NULL;
4019 assure( *table != NULL, CPL_ERROR_NULL_INPUT,
"Null input table!");
4020 ncols = cpl_table_get_ncol(*table);
4021 names = cpl_table_get_column_names(*table);
4022 for(i=0;i<ncols;i++) {
4023 colname=cpl_array_get_string(names, i);
4024 cpl_table_set_column_unit(*table,colname,NULL);
4028 uves_free_array(&names);
4030 return cpl_error_get_code();
4048 const char* colname=NULL;
4049 const char* unit1=NULL;
4052 cpl_array *names=NULL;
4054 assure( table1 != NULL, CPL_ERROR_NULL_INPUT,
"Null input table!");
4055 assure( *table2 != NULL, CPL_ERROR_NULL_INPUT,
"Null input table!");
4056 ncols1 = cpl_table_get_ncol(*table1);
4057 ncols2 = cpl_table_get_ncol(*table2);
4058 assure( ncols1 == ncols2, CPL_ERROR_NULL_INPUT,
4059 "n columns (tab1) != n columns (tab2)");
4061 names = cpl_table_get_column_names(*table1);
4062 for(i=0;i<ncols1;i++) {
4063 colname=cpl_array_get_string(names, i);
4064 unit1=cpl_table_get_column_unit(*table1,colname);
4065 cpl_table_set_column_unit(*table2,colname,unit1);
4069 uves_free_array(&names);
4071 return cpl_error_get_code();
4105 static void fmoffa_i(
float x,
const double a[],
double *y,
double dyda[])
4110 double fac=0, fac2=0, fac4= 0, fac4i=0, arg=0, arg2=0;
4111 double a2i=0, m = 0, p = 0, dif =0;
4112 double sqrt5 = 2.23606797749979;
4116 a2i = 1.0/(a[2]*sqrt5);
4128 m = a[3]*fac4i * a2i*16/(5.0*M_PI);
4129 *y = m + a[4]*(1.0+dif*a[5]);
4130 p = 8.0*m/fac*arg*a2i;
4133 dyda[2] = p*dif/a[2] - m/a[2];
4136 dyda[1]=p-a[4]*a[5];
4138 dyda[4]=1.0+dif*a[5];
4145 printf(
"fmoffat_i \n");
4146 for (i = 1;i<=npar;i++) printf(
"a[%1i] %f :\n",i,a[i]);
4148 printf(
"fmoffat_i ");
4149 for (i = 1;i<=npar;i++) printf(
"%i %f :",i,dyda[i]);
4174 static void fmoffa_c(
float x,
const double a[],
double *y,
double dyda[])
4182 double const xgl[3] = {-0.387298334621,0.,0.387298334621};
4183 double const wgl[3] = {.2777777777778,0.444444444444,0.2777777777778};
4193 a = C_TO_FORTRAN_INDEXING(a);
4194 dyda = C_TO_FORTRAN_INDEXING(dyda);
4197 for (i = 1;i<=npar;i++) dyda[i] = 0.;
4202 for (j=0; j < npoint; j++)
4206 fmoffa_i(xmod,a,&ypar,&dydapar[-1]);
4208 *y = *y + ypar*wgl[j];
4210 for (i = 1; i <= npar; i++)
4212 dyda[i] = dyda[i] + dydapar[i-1]*wgl[j] ;
4224 printf(
"fmoffat_c ");
4225 for (i = 1;i<=npar;i++) printf(
"%i %f %f: \n",i,a[i],dyda[i]);
4294 double sigma = a[1];
4315 A/(sqrt(2*M_PI*sigma*sigma)) *
4316 exp(- (x[0] - my)*(x[0] - my)
4349 double sigma = a[1];
4376 factor = exp( -(x[0] - my)*(x[0] - my)/(2*sigma*sigma) )
4377 / (sqrt(2*M_PI*sigma*sigma));
4379 result[0] = A * factor * (x[0]-my) / (sigma*sigma);
4380 result[1] = A * factor * ((x[0]-my)*(x[0]-my) / (sigma*sigma) - 1) / sigma;
4414 double sigma = a[1];
4435 *result = B + C*(x[0] - my) +
4436 A/(sqrt(2*M_PI*sigma*sigma)) *
4437 exp(- (x[0] - my)*(x[0] - my)
4473 double sigma = a[1];
4504 factor = exp( -(x[0] - my)*(x[0] - my)/(2*sigma*sigma) )
4505 / (sqrt(2*M_PI*sigma*sigma));
4507 result[0] = A * factor * (x[0]-my) / (sigma*sigma);
4508 result[1] = A * factor * ((x[0]-my)*(x[0]-my) / (sigma*sigma) - 1) / sigma;
4511 result[4] = x[0] - my;
4535 const cpl_image *spectrum,
const cpl_image *sky,
4536 const cpl_image *cosmic_image,
4537 const uves_extract_profile *profile,
4540 cpl_image *image = NULL;
4542 cpl_binary *bpm = NULL;
4543 bool loop_y =
false;
4547 bool new_format =
true;
4549 image = cpl_image_new(pos->nx, pos->ny, CPL_TYPE_DOUBLE);
4551 if (image_noise != NULL) {
4552 *image_noise = cpl_image_new(pos->nx, pos->ny, CPL_TYPE_DOUBLE);
4554 cpl_image_add_scalar(*image_noise, 0.01);
4557 if (image_header != NULL) {
4560 uves_propertylist_append_double(*image_header, UVES_MJDOBS, 60000);
4561 uves_propertylist_append_double(*image_header, UVES_RON(new_format, chip), ron);
4562 uves_propertylist_append_double(*image_header, UVES_GAIN(new_format, chip), gain);
4567 pos->minorder, pos->maxorder,
4574 uves_extract_profile_set(profile, pos, NULL);
4575 for (pos->y = pos->ylow; pos->y <= pos->yhigh; pos->y++) {
4578 double flux, sky_flux;
4580 int spectrum_row = pos->order - pos->minorder + 1;
4582 double prof = uves_extract_profile_evaluate(profile, pos);
4586 sky_flux = cpl_image_get(sky, pos->x, spectrum_row, &bad)/pos->sg.length;
4593 flux = cpl_image_get(spectrum, pos->x, spectrum_row, &bad) * prof + sky_flux;
4596 noise = sqrt(gain)*sqrt(ron*ron/(gain*gain) + sky_flux/gain + flux/gain);
4598 cpl_image_set(image, pos->x, pos->y,
4600 if (image_noise != NULL) cpl_image_set(*image_noise, pos->x, pos->y, noise);
4605 if (cosmic_image != NULL) {
4606 double cr_val = 2*cpl_image_get_max(image);
4613 pos->minorder, pos->maxorder,
4620 if (cpl_image_get(cosmic_image, pos->x, pos->y, &is_rejected) > 0) {
4621 cpl_image_set(image, pos->x, pos->y, cr_val);
4631 uves_frameset_dump(cpl_frameset* set)
4634 cpl_frame* frm=NULL;
4638 cknull(set,
"Null input frameset");
4641 frm=cpl_frameset_get_frame(set,i);
4642 uves_msg(
"frame %d tag %s filename %s group %d",
4644 cpl_frame_get_tag(frm),
4645 cpl_frame_get_filename(frm),
4646 cpl_frame_get_group(frm));
4675 uves_image_smooth_x(cpl_image * inp,
const int r)
4694 cpl_image* out=NULL;
4696 cknull(inp,
"Null in put image, exit");
4703 for(i=r;i<sx-r;i++) {
4705 pout[j*sx+i]+=pinp[j*sx+i+k];
4713 if(cpl_error_get_code() != CPL_ERROR_NONE) {
4743 uves_image_smooth_y(cpl_image * inp,
const int r)
4762 cpl_image* out=NULL;
4764 cknull(inp,
"Null in put image, exit");
4770 for(j=r;j<sy-r;j++) {
4773 pout[j*sx+i]+=pinp[(j+k)*sx+i];
4781 if(cpl_error_get_code() != CPL_ERROR_NONE) {
4808 uves_image_smooth_mean_x(cpl_image * inp,
const int r)
4827 cpl_image* out=NULL;
4829 cknull(inp,
"Null in put image, exit");
4836 for(i=r;i<sx-r;i++) {
4838 pout[j*sx+i]+=pinp[j*sx+i+k];
4846 if(cpl_error_get_code() != CPL_ERROR_NONE) {
4873 uves_image_smooth_median_x(cpl_image * inp,
const int r)
4890 cpl_image* out=NULL;
4893 cknull(inp,
"Null in put image, exit");
4900 for(i=1+r;i<sx-r;i++) {
4901 pout[j*sx+i]=(float)cpl_image_get_median_window(inp,i,j,i+r,j);
4907 if(cpl_error_get_code() != CPL_ERROR_NONE) {
4932 uves_image_smooth_fft(cpl_image * inp,
const int fx)
4938 cpl_image* out=NULL;
4939 cpl_image* im_re=NULL;
4940 cpl_image* im_im=NULL;
4941 cpl_image* ifft_re=NULL;
4942 cpl_image* ifft_im=NULL;
4943 cpl_image* filter=NULL;
4948 cknull(inp,
"Null in put image, exit");
4949 check_nomsg(im_re = cpl_image_cast(inp, CPL_TYPE_DOUBLE));
4950 check_nomsg(im_im = cpl_image_cast(inp, CPL_TYPE_DOUBLE));
4953 check_nomsg(cpl_image_fft(im_re,im_im,CPL_FFT_DEFAULT));
4963 cpl_image_multiply(im_re,filter);
4964 cpl_image_multiply(im_im,filter);
4966 uves_free_image(&filter);
4968 check_nomsg(ifft_re = cpl_image_duplicate(im_re));
4969 check_nomsg(ifft_im = cpl_image_duplicate(im_im));
4971 uves_free_image(&im_re);
4972 uves_free_image(&im_im);
4975 check_nomsg(cpl_image_fft(ifft_re,ifft_im,CPL_FFT_INVERSE));
4976 check_nomsg(out = cpl_image_cast(ifft_re, CPL_TYPE_FLOAT));
4980 uves_free_image(&ifft_re);
4981 uves_free_image(&ifft_im);
4982 uves_free_image(&filter);
4983 uves_free_image(&im_re);
4984 uves_free_image(&im_im);
4986 if(cpl_error_get_code() != CPL_ERROR_NONE) {
5008 cpl_image* img=NULL;
5011 cpl_vector* values=NULL;
5018 pval=cpl_vector_get_data(values);
5019 for(i=0;i<size;i++) {
5020 img=cpl_imagelist_get(iml,i);
5021 irplib_ksigma_clip(img,1,1,
5022 cpl_image_get_size_x(img),
5023 cpl_image_get_size_y(img),
5024 5,kappa,1.e-5,&mean,&stdev);
5025 uves_msg(
"Ima %d mean level: %g",i+1,mean);
5049 cpl_image* img=NULL;
5055 pval=cpl_vector_get_data(values);
5056 for(i=0;i<size;i++) {
5057 img=cpl_imagelist_get(*iml,i);
5058 cpl_image_subtract_scalar(img,pval[i]);
5059 cpl_imagelist_set(*iml,img,i);
5064 return cpl_error_get_code();
5088 const double sigma_x,
5089 const double sigma_y)
5098 double gaussval= 0.0;
5099 double inv_sigma_x=1./sigma_x;
5100 double inv_sigma_y=1./sigma_y;
5104 cpl_image *lowpass_image=NULL;
5107 lowpass_image = cpl_image_new (xs, ys, CPL_TYPE_FLOAT);
5108 if (lowpass_image == NULL) {
5110 cpl_error_get_message());
5117 data = cpl_image_get_data_float(lowpass_image);
5132 for (i=1 ; i<=hlx ; i++) {
5133 x = i * inv_sigma_x;
5134 gaussval = exp(-0.5*x*x);
5136 data[xs-i] = gaussval;
5139 for (j=1; j<=hly ; j++) {
5140 y = j * inv_sigma_y;
5142 data[j*xs] = exp(-0.5*y*y);
5143 data[(ys-j)*xs] = exp(-0.5*y*y);
5145 for (i=1 ; i<=hlx ; i++) {
5147 x = i * inv_sigma_x;
5148 gaussval = exp (-0.5*(x*x+y*y));
5149 data[j*xs+i] = gaussval;
5150 data[(j+1)*xs-i] = gaussval;
5151 data[(ys-j)*xs+i] = gaussval;
5152 data[(ys+1-j)*xs-i] = gaussval;
5164 return lowpass_image;
5180 cpl_image* result=NULL;
5181 cpl_image* diff=NULL;
5182 cpl_image* flat_smooth=NULL;
5183 cpl_array* val=NULL;
5184 cpl_matrix* mx=NULL;
5195 int filter_width_x=7;
5196 int filter_width_y=7;
5205 const double* pima=NULL;
5211 passure( flat !=NULL ,
"NULL input flat ");
5212 passure( head !=NULL ,
"NULL input head ");
5215 sx=cpl_image_get_size_x(flat);
5216 sy=cpl_image_get_size_y(flat);
5223 if (binx>1) filter_width_x=5;
5224 if (biny>1) filter_width_y=5;
5228 check_nomsg(mx=cpl_matrix_new(filter_width_x,filter_width_y));
5230 for(j=0; j< filter_width_y; j++){
5231 for(i=0; i< filter_width_x; i++){
5232 cpl_matrix_set( mx, i,j,1.0);
5238 check_nomsg(flat_smooth=uves_image_filter_median(flat,mx));
5243 check_nomsg(cpl_image_subtract(diff,flat_smooth));
5252 val=cpl_array_new(npixs,CPL_TYPE_DOUBLE);
5253 check_nomsg(cpl_array_fill_window_double(val,0,npixs,0));
5255 check_nomsg(pima=cpl_image_get_data_double_const(flat));
5256 check_nomsg(pdif=cpl_image_get_data_double(diff));
5258 for(i=0;i<npixs;i++) {
5259 if(pima[i]>med_flat) {
5273 for(i=0;i<niter;i++) {
5274 for(k=0;k<size;k++) {
5275 if(fabs(pval[k]-mean)>stdev_x_4) {
5276 cpl_array_set_invalid(val,k);
5279 mean=cpl_array_get_mean(val);
5280 stdev=cpl_array_get_stdev(val);
5285 result=cpl_image_new(sx,sy,CPL_TYPE_DOUBLE);
5286 pres=cpl_image_get_data_double(result);
5287 for(i=0;i<npixs;i++) {
5288 if(fabs(pdif[i])<stdev_x_4) {
5300 uves_free_array(&val);
5301 uves_free_image(&diff);
5302 uves_free_image(&flat_smooth);
5303 uves_free_matrix(&mx);
const char * uves_string_tolower(char *s)
Convert all uppercase characters in a string into lowercase characters.
polynomial * uves_polynomial_fit_1d(const cpl_vector *x_pos, const cpl_vector *values, const cpl_vector *sigmas, int poly_deg, double *mse)
Fit a 1d function with a polynomial.
#define uves_msg_error(...)
Print an error message.
cpl_error_code uves_pfits_set_crpix2(uves_propertylist *plist, double crpix2)
Write the crpix2 keyword.
cpl_error_code uves_filter_image_median(cpl_image **image, int xwindow, int ywindow, bool extrapolate_border)
Median filter.
void uves_polynomial_delete(polynomial **p)
Delete a polynomial.
char * uves_get_datetime_iso8601(void)
Returns the current date and time as a static string.
#define uves_msg_warning(...)
Print an warning message.
bool uves_table_is_sorted_double(const cpl_table *t, const char *column, const bool reverse)
Determine if a table is sorted.
cpl_image * uves_get_wave_map(cpl_image *ima_sci, const char *context, const cpl_parameterlist *parameters, const cpl_table *ordertable, const cpl_table *linetable, const polynomial *order_locations, const polynomial *dispersion_relation, const int first_abs_order, const int last_abs_order, const int slit_size)
Generates wave map.
void uves_msg_set_level(int olevel)
Set output level.
cpl_error_code uves_pfits_set_ctype1(uves_propertylist *plist, const char *ctype1)
Write the ctype1 keyword.
cpl_error_code uves_rcosmic(cpl_image *ima, cpl_image **flt, cpl_image **out, cpl_image **msk, const double sky, const double ron, const double gain, const int ns, const double rc)
Remove cosmic ray events on single ccd exposure and replace them by interpolation on neighbourhood pi...
int uves_msg_get_warnings(void)
Get number of warnings printed so far.
cpl_error_code uves_pfits_set_cdelt1(uves_propertylist *plist, double cdelt1)
Write the cdelt1 keyword.
double uves_propertylist_get_double(const uves_propertylist *self, const char *name)
Get the double value of the given property list entry.
static double uves_ksigma_vector(cpl_vector *values, double klow, double khigh, int kiter)
Perform kappa-sigma clip.
cpl_image * uves_define_noise(const cpl_image *image, const uves_propertylist *image_header, int ncom, enum uves_chip chip)
Create noise image.
cpl_image * uves_flat_create_normalized_master(cpl_imagelist *flats, const cpl_table *ordertable, const polynomial *order_locations, const cpl_vector *gain_vals, double *fnoise)
Stack images using k-sigma clipping.
double uves_pfits_get_gain(const uves_propertylist *plist, enum uves_chip chip)
Find out the gain.
int uves_absolute_order(int first_abs_order, int last_abs_order, int relative_order)
Get the absolute order number.
const char * uves_get_license(void)
Get the pipeline copyright and license.
static void fmoffa_i(float x, const double a[], double *y, double dyda[])
This subroutine gives the value of the Moffat (beta=4)+ linear functions at pixel of coordinates x es...
int uves_gauss_linear(const double x[], const double a[], double *result)
Evaluate a gaussian with linear background.
double uves_pow_int(double x, int y)
Calculate x to the y'th.
cpl_table * uves_ordertable_traces_new(void)
Create the table that describes fibre traces.
cpl_error_code uves_pfits_set_crval1(uves_propertylist *plist, double crval1)
Write the crval1 keyword.
cpl_image * uves_flat_create_normalized_master2(cpl_imagelist *flats, const cpl_table *ordertable, const polynomial *order_locations, const cpl_image *mflat)
Stack images using k-sigma clipping.
cpl_error_code uves_tablename_remove_units(const char *tname)
Remove column units from a table.
cpl_error_code uves_subtract_bias(cpl_image *image, const cpl_image *master_bias)
Subtract bias.
cpl_error_code uves_pfits_set_cunit2(uves_propertylist *plist, const char *cunit2)
Write the cunit2 keyword.
#define passure(BOOL,...)
int uves_gauss_derivative(const double x[], const double a[], double result[])
Evaluate the derivatives of a gaussian.
double uves_average_reject(cpl_table *t, const char *column, const char *residual2, double kappa)
Get average with iterative rejection.
cpl_image * uves_image_mflat_detect_blemishes(const cpl_image *flat, const uves_propertylist *head)
Flag blemishes in a flat image.
cpl_error_code uves_table_unify_units(cpl_table **table2, cpl_table **table1)
Unify column units of table2 to table1.
cpl_error_code uves_tablenames_unify_units(const char *tname2, const char *tname1)
Unify column units in tables.
cpl_error_code uves_subtract_dark(cpl_image *image, const uves_propertylist *image_header, const cpl_image *master_dark, const uves_propertylist *mdark_header)
Subtract dark.
uves_propertylist * uves_initialize_image_header(const char *ctype1, const char *ctype2, const char *cunit1, const char *cunit2, const char *bunit, const double bscale, double crval1, double crval2, double crpix1, double crpix2, double cdelt1, double cdelt2)
Initialize image header.
static cpl_image * uves_gen_lowpass(const int xs, const int ys, const double sigma_x, const double sigma_y)
Generate a low pass filter for FFT convolution .
cpl_error_code uves_pfits_set_bscale(uves_propertylist *plist, const double bscale)
Write the bscale keyword.
uves_propertylist * uves_propertylist_new(void)
Create an empty property list.
double uves_gaussrand(void)
Pseudo-random gaussian distributed number.
int uves_pfits_get_binx(const uves_propertylist *plist)
Find out the x binning factor.
double uves_spline_hermite_table(double xp, const cpl_table *t, const char *column_x, const char *column_y, int *istart)
Spline interpolation based on Hermite polynomials.
#define uves_msg(...)
Print a message on 'info' or 'debug' level.
int uves_gauss(const double x[], const double a[], double *result)
Evaluate a gaussian.
int uves_moffat(const double x[], const double a[], double *result)
Evaluate a Moffat.
uves_propertylist * uves_propertylist_load(const char *name, int position)
Create a property list from a file.
cpl_error_code uves_pfits_set_ctype2(uves_propertylist *plist, const char *ctype2)
Write the ctype2 keyword.
static cpl_error_code uves_cosrout(cpl_image *ima, cpl_image **msk, const double ron, const double gain, const int ns, const double sky, const double rc, cpl_image **flt, cpl_image **out)
Remove cosmic ray events on single ccd exposure and replace them by interpolation on neighbourhood pi...
polynomial * uves_polynomial_regression_2d(cpl_table *t, const char *X1, const char *X2, const char *Y, const char *sigmaY, int degree1, int degree2, const char *polynomial_fit, const char *residual_square, const char *variance_fit, double *mse, double *red_chisq, polynomial **variance, double kappa, double min_reject)
Fit a 2d polynomial to three table columns.
cpl_error_code uves_pfits_set_crpix1(uves_propertylist *plist, double crpix1)
Write the crpix1 keyword.
int uves_pfits_get_biny(const uves_propertylist *plist)
Find out the y binning factor.
int uves_gauss_linear_derivative(const double x[], const double a[], double result[])
Evaluate the derivatives of a gaussian with linear background.
polynomial * uves_polynomial_regression_2d_autodegree(cpl_table *t, const char *X1, const char *X2, const char *Y, const char *sigmaY, const char *polynomial_fit, const char *residual_square, const char *variance_fit, double *mean_squared_error, double *red_chisq, polynomial **variance, double kappa, int maxdeg1, int maxdeg2, double min_rms, double min_reject, bool verbose, const double *min_val, const double *max_val, int npos, double positions[][2])
Fit a 2d polynomial to three table columns.
cpl_vector * uves_imagelist_get_clean_mean_levels(cpl_imagelist *iml, double kappa)
Computes kappa-sigma clean mean (free bad pixels) for each input image of the input imagelist...
const char * uves_remove_string_prefix(const char *s, const char *prefix)
Remove named prefix from string.
double uves_pfits_get_exptime(const uves_propertylist *plist)
Find out the exposure time in seconds.
cpl_image * uves_create_image(uves_iterate_position *pos, enum uves_chip chip, const cpl_image *spectrum, const cpl_image *sky, const cpl_image *cosmic_image, const uves_extract_profile *profile, cpl_image **image_noise, uves_propertylist **image_header)
Reconstruct echelle image from spectrum.
cpl_error_code uves_table_remove_units(cpl_table **table)
Remove column units from a table.
double uves_polynomial_evaluate_2d(const polynomial *p, double x1, double x2)
Evaluate a 2d polynomial.
double uves_polynomial_evaluate_1d(const polynomial *p, double x)
Evaluate a 1d polynomial.
cpl_image * uves_average_images(const cpl_image *image1, const cpl_image *noise1, const cpl_image *image2, const cpl_image *noise2, cpl_image **noise)
Optimally average images.
cpl_error_code uves_pfits_set_cdelt2(uves_propertylist *plist, double cdelt2)
Write the cdelt2 keyword.
char * uves_initialize(cpl_frameset *frames, const cpl_parameterlist *parlist, const char *recipe_id, const char *short_descr)
Recipe initialization.
int uves_moffat_derivative(const double x[], const double a[], double result[])
Evaluate Moffat derivative.
cpl_error_code uves_ordertable_traces_add(cpl_table *traces, int fibre_ID, double fibre_offset, int fibre_mask)
Add a trace.
#define REQ_CPL_MAJOR
Check compile time and runtime library versions.
polynomial * uves_polynomial_regression_1d(cpl_table *t, const char *X, const char *Y, const char *sigmaY, int degree, const char *polynomial_fit, const char *residual_square, double *mean_squared_error, double kappa)
Fit a 1d polynomial to two table columns.
const char * uves_tostring_cpl_type(cpl_type t)
Convert a CPL type to a string.
cpl_image * uves_ksigma_stack(const cpl_imagelist *imlist, double klow, double khigh, int kiter)
Stack images using k-sigma clipping.
#define uves_error_reset()
#define uves_msg_low(...)
Print a message on a lower message level.
cpl_error_code uves_end(const char *recipe_id, const cpl_frameset *frames)
Recipe termination.
cpl_error_code uves_pfits_set_cunit1(uves_propertylist *plist, const char *cunit1)
Write the cunit1 keyword.
static void fmoffa_c(float x, const double a[], double *y, double dyda[])
Moffat profile.
#define uves_msg_debug(...)
Print a debug message.
#define assure_nomsg(BOOL, CODE)
cpl_error_code uves_get_version(int *major, int *minor, int *micro)
Get UVES library version number.
cpl_error_code uves_pfits_set_bunit(uves_propertylist *plist, const char *bunit)
Write the bunit keyword.
const char * uves_string_toupper(char *s)
Convert all lowercase characters in a string into uppercase characters.
int uves_propertylist_contains(const uves_propertylist *self, const char *name)
Check whether a property is present in a property list.
double uves_spline_hermite(double xp, const double *x, const double *y, int n, int *istart)
Spline interpolation based on Hermite polynomials.
cpl_parameterlist * uves_parameterlist_duplicate(const cpl_parameterlist *pin)
Extract frames with given tag from frameset.
int uves_get_version_binary(void)
Get UVES library binary version number.
double uves_pfits_get_ron_adu(const uves_propertylist *plist, enum uves_chip chip)
Find out the readout noise in ADU.
cpl_error_code uves_print_cpl_frameset(const cpl_frameset *frames)
Print a frame set.
cpl_error_code uves_imagelist_subtract_values(cpl_imagelist **iml, cpl_vector *values)
Subtract from input imagelist values specified in input vector.
cpl_error_code uves_pfits_set_crval2(uves_propertylist *plist, double crval2)
Write the crval2 keyword.
double uves_spline_cubic(double xp, double *x, float *y, float *y2, int n, int *kstart)
Natural cubic-spline interpolation.
cpl_frameset * uves_frameset_extract(const cpl_frameset *frames, const char *tag)
Extract frames with given tag from frameset.
polynomial * uves_polynomial_fit_2d(const cpl_bivector *xy_pos, const cpl_vector *values, const cpl_vector *sigmas, int poly_deg1, int poly_deg2, double *mse, double *red_chisq, polynomial **variance)
Fit a 2d surface with a polynomial in x and y.