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();
1856 for (f = cpl_frameset_get_first_const(frames);
1858 f = cpl_frameset_get_next_const(frames))
1860 if (cpl_frame_get_group(f) == CPL_FRAME_GROUP_PRODUCT)
1863 cpl_frameset_insert(products, cpl_frame_duplicate(f)));
1878 (warnings > 1) ?
"s" :
"");
1882 uves_free_frameset(&products);
1883 return cpl_error_get_code();
1910 const char *recipe_id,
const char *short_descr)
1912 char *recipe_string = NULL;
1914 char *spaces1 = NULL;
1915 char *spaces2 = NULL;
1916 char *spaces3 = NULL;
1917 char *spaces4 = NULL;
1918 char *start_time = NULL;
1922 check( uves_check_version(),
"Library validation failed");
1926 const char *plotter_command;
1932 check( uves_get_parameter(parlist, NULL,
"uves",
"msginfolevel",
1933 CPL_TYPE_INT, &msglevel),
1934 "Could not read parameter");
1940 check( uves_get_parameter(parlist, NULL,
"uves",
"plotter",
1941 CPL_TYPE_STRING, &plotter_command),
"Could not read parameter");
1944 check( uves_plot_initialize(plotter_command),
1945 "Could not initialize plotting");
1954 recipe_string = uves_sprintf(
"Recipe: %s", recipe_id);
1956 int field = uves_max_int(strlen(PACKAGE_STRING), strlen(recipe_string));
1957 int nstars = 3+1 + field + 1+3;
1958 int nspaces1, nspaces2, nspaces3, nspaces4;
1962 nspaces1 = (field - strlen(PACKAGE_STRING)) / 2;
1963 nspaces2 = field - strlen(PACKAGE_STRING) - nspaces1;
1965 nspaces3 = (field - strlen(recipe_string)) / 2;
1966 nspaces4 = field - strlen(recipe_string) - nspaces3;
1968 spaces1 = cpl_calloc(nspaces1 + 1,
sizeof(
char));
1969 spaces2 = cpl_calloc(nspaces2 + 1,
sizeof(
char));
1970 spaces3 = cpl_calloc(nspaces3 + 1,
sizeof(
char));
1971 spaces4 = cpl_calloc(nspaces4 + 1,
sizeof(
char));
1972 for (i = 0; i < nspaces1; i++) spaces1[i] =
' ';
1973 for (i = 0; i < nspaces2; i++) spaces2[i] =
' ';
1974 for (i = 0; i < nspaces3; i++) spaces3[i] =
' ';
1975 for (i = 0; i < nspaces4; i++) spaces4[i] =
' ';
1977 stars = cpl_calloc(nstars + 1,
sizeof(
char));
1978 for (i = 0; i < nstars; i++) stars[i] =
'*';
1981 uves_msg(
"*** %s%s%s ***", spaces1, PACKAGE_STRING, spaces2);
1982 uves_msg(
"*** %s%s%s ***", spaces3, recipe_string, spaces4);
1986 uves_msg(
"This recipe %c%s", tolower(short_descr[0]), short_descr+1);
1988 if (cpl_frameset_is_empty(frames)) {
1989 uves_msg_debug(
"Guvf cvcryvar unf ernpurq vgf uvtu dhnyvgl qhr na npgvir "
1990 "hfre pbzzhavgl naq gur erfcbafvoyr naq vqrnyvfgvp jbex bs "
1991 "vaqvivqhny cvcryvar qrirybcref, naq qrfcvgr orvat 'onfrq ba' "
1992 "PCY juvpu vf n cvrpr bs cbyvgvpny penc");
1998 check( uves_dfs_set_groups(frames),
"Could not classify input frames");
2005 cpl_free(recipe_string);
2046 const cpl_image *image2,
const cpl_image *noise2,
2049 cpl_image *result = NULL;
2054 assure( image1 != NULL, CPL_ERROR_NULL_INPUT,
"Null image");
2055 assure( image2 != NULL, CPL_ERROR_NULL_INPUT,
"Null image");
2056 assure( noise1 != NULL, CPL_ERROR_NULL_INPUT,
"Null image");
2057 assure( noise2 != NULL, CPL_ERROR_NULL_INPUT,
"Null image");
2058 assure( noise != NULL, CPL_ERROR_NULL_INPUT,
"Null image");
2060 assure( cpl_image_get_min(noise1) > 0, CPL_ERROR_ILLEGAL_INPUT,
2061 "Noise must be everywhere positive, minimum = %e", cpl_image_get_min(noise1));
2062 assure( cpl_image_get_min(noise2) > 0, CPL_ERROR_ILLEGAL_INPUT,
2063 "Noise must be everywhere positive, minimum = %e", cpl_image_get_min(noise2));
2065 nx = cpl_image_get_size_x(image1);
2066 ny = cpl_image_get_size_y(image1);
2068 assure( nx == cpl_image_get_size_x(image2), CPL_ERROR_INCOMPATIBLE_INPUT,
2069 "Size mismatch %" CPL_SIZE_FORMAT
" != %" CPL_SIZE_FORMAT
"",
2070 nx, cpl_image_get_size_x(image2));
2071 assure( nx == cpl_image_get_size_x(noise1), CPL_ERROR_INCOMPATIBLE_INPUT,
2072 "Size mismatch %" CPL_SIZE_FORMAT
" != %" CPL_SIZE_FORMAT
"",
2073 nx, cpl_image_get_size_x(noise1));
2074 assure( nx == cpl_image_get_size_x(noise2), CPL_ERROR_INCOMPATIBLE_INPUT,
2075 "Size mismatch %" CPL_SIZE_FORMAT
" != %" CPL_SIZE_FORMAT
"",
2076 nx, cpl_image_get_size_x(noise2));
2077 assure( ny == cpl_image_get_size_y(image2), CPL_ERROR_INCOMPATIBLE_INPUT,
2078 "Size mismatch %" CPL_SIZE_FORMAT
" != %" CPL_SIZE_FORMAT
"",
2079 ny, cpl_image_get_size_y(image2));
2080 assure( ny == cpl_image_get_size_y(noise1), CPL_ERROR_INCOMPATIBLE_INPUT,
2081 "Size mismatch %" CPL_SIZE_FORMAT
" != %" CPL_SIZE_FORMAT
"",
2082 ny, cpl_image_get_size_y(noise1));
2083 assure( ny == cpl_image_get_size_y(noise2), CPL_ERROR_INCOMPATIBLE_INPUT,
2084 "Size mismatch %" CPL_SIZE_FORMAT
" != %" CPL_SIZE_FORMAT
"",
2085 ny, cpl_image_get_size_y(noise2));
2087 result = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
2088 *noise = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
2091 for (y = 1; y <= ny; y++)
2093 for (x = 1; x <= nx; x++)
2095 double flux1, flux2;
2096 double sigma1, sigma2;
2097 int pis_rejected1, noise_rejected1;
2098 int pis_rejected2, noise_rejected2;
2100 flux1 = cpl_image_get(image1, x, y, &pis_rejected1);
2101 flux2 = cpl_image_get(image2, x, y, &pis_rejected2);
2102 sigma1 = cpl_image_get(noise1, x, y, &noise_rejected1);
2103 sigma2 = cpl_image_get(noise2, x, y, &noise_rejected2);
2105 pis_rejected1 = pis_rejected1 || noise_rejected1;
2106 pis_rejected2 = pis_rejected2 || noise_rejected2;
2108 if (pis_rejected1 && pis_rejected2)
2110 cpl_image_reject(result, x, y);
2111 cpl_image_reject(*noise, x, y);
2119 if (pis_rejected1 && !pis_rejected2)
2124 else if (!pis_rejected1 && pis_rejected2)
2133 1 / (sigma1*sigma1) +
2134 1 / (sigma2*sigma2);
2136 flux = flux1/(sigma1*sigma1) + flux2/(sigma2*sigma2);
2139 sigma = sqrt(sigma);
2142 cpl_image_set(result, x, y, flux);
2143 cpl_image_set(*noise, x, y, sigma);
2149 if (cpl_error_get_code() != CPL_ERROR_NONE)
2151 uves_free_image(&result);
2174 const char *cunit1,
const char *cunit2,
2175 const char *bunit,
const double bscale,
2176 double crval1,
double crval2,
2177 double crpix1,
double crpix2,
2178 double cdelt1,
double cdelt2)
2187 if(cunit2 != NULL) {
2227 int ncom,
enum uves_chip chip)
2234 cpl_image *noise = NULL;
2242 const double *image_data;
2243 bool has_bnoise=
false;
2244 bool has_dnoise=
false;
2252 double tot_noise2=0;
2253 double var_bias_dark=0;
2256 assure( ncom >= 1, CPL_ERROR_ILLEGAL_INPUT,
"Number of combined frames = %d", ncom);
2259 "Could not read read-out noise");
2262 "Could not read gain factor");
2263 assure( gain > 0, CPL_ERROR_ILLEGAL_INPUT,
"Non-positive gain: %e", gain);
2265 nx = cpl_image_get_size_x(image);
2266 ny = cpl_image_get_size_y(image);
2269 assure(cpl_image_count_rejected(image) == 0,
2270 CPL_ERROR_UNSUPPORTED_MODE,
"Input image contains bad pixels");
2271 assure(cpl_image_get_type(image) == CPL_TYPE_DOUBLE,
2272 CPL_ERROR_UNSUPPORTED_MODE,
2273 "Input image is of type %s. double expected",
2276 noise = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
2279 noise_data = cpl_image_get_data_double(noise);
2281 image_data = cpl_image_get_data_double_const(image);
2284 if(image_header != NULL) {
2291 bnoise2=bnoise*bnoise;
2296 dnoise2=dnoise*dnoise;
2299 exptime2=exptime*exptime/dtime/dtime;
2301 var_bias_dark=bnoise2+dnoise2*exptime2;
2302 uves_msg_debug(
"bnoise=%g dnoise=%g sci exptime=%g dark exptime=%g",
2303 bnoise,dnoise,exptime,dtime);
2323 double median_factor = (ncom > 1) ? 2.0/M_PI : 1.0;
2324 double gain2=gain*gain;
2326 double quant_var = uves_max_double(0, (1 - gain2)/12.0);
2330 double flux_var_adu=0;
2331 double ron2=ron*ron;
2332 double inv_ncom_median_factor=1./(ncom * median_factor);
2333 for (i = 0; i < nx*ny; i++)
2338 flux_var_adu = uves_max_double(image_data[i],0)*gain;
2359 tot_noise2=(( ron2 + quant_var + flux_var_adu )*inv_ncom_median_factor)+
2365 noise_data[i] = sqrt(tot_noise2);
2370 if (cpl_error_get_code() != CPL_ERROR_NONE)
2372 uves_free_image(&noise);
2393 passure ( image != NULL,
" ");
2394 passure ( master_bias != NULL,
" ");
2396 check( cpl_image_subtract(image, master_bias),
2397 "Error subtracting bias");
2411 check( cpl_image_threshold(image,
2414 "Error thresholding image");
2418 return cpl_error_get_code();
2436 const cpl_image *master_dark,
2439 cpl_image *normalized_mdark = NULL;
2440 double image_exptime = 0.0;
2441 double mdark_exptime = 0.0;
2443 passure ( image != NULL,
" ");
2444 passure ( image_header != NULL,
" ");
2445 passure ( master_dark != NULL,
" ");
2446 passure ( mdark_header != NULL,
" ");
2450 "Error reading input image exposure time");
2452 "Error reading master dark exposure time");
2454 uves_msg(
"Rescaling master dark from %f s to %f s exposure time",
2455 mdark_exptime, image_exptime);
2457 check( normalized_mdark =
2458 cpl_image_multiply_scalar_create(master_dark,
2459 image_exptime / mdark_exptime),
2460 "Error normalizing master dark");
2462 check( cpl_image_subtract(image, normalized_mdark),
2463 "Error subtracting master dark");
2465 uves_msg_warning(
"noise rescaled master dark %g",cpl_image_get_stdev(normalized_mdark));
2469 uves_free_image(&normalized_mdark);
2470 return cpl_error_get_code();
2490 return (first_abs_order +
2491 (relative_order-1)*((last_abs_order > first_abs_order) ? 1 : -1));
2512 const char *residual2,
2515 double mean = 0, median, sigma2;
2520 check_nomsg(median = cpl_table_get_column_median(t, column));
2524 check_nomsg(cpl_table_duplicate_column(t, residual2, t, column));
2525 check_nomsg(cpl_table_subtract_scalar(t, residual2, median));
2526 check_nomsg(cpl_table_multiply_columns(t, residual2, residual2));
2533 check_nomsg(sigma2 = cpl_table_get_column_median(t, residual2) / (0.6744 * 0.6744));
2538 check_nomsg( rejected = uves_erase_table_rows(t, residual2,
2540 kappa*kappa*sigma2));
2542 check_nomsg(cpl_table_erase_column(t, residual2));
2544 }
while (rejected > 0);
2546 check_nomsg(mean = cpl_table_get_column_mean(t, column));
2588 const char *X,
const char *Y,
const char *sigmaY,
2590 const char *polynomial_fit,
const char *residual_square,
2591 double *mean_squared_error,
double kappa)
2594 int total_rejected = 0;
2601 cpl_vector *vx = NULL;
2602 cpl_vector *vy = NULL;
2603 cpl_vector *vsy = NULL;
2607 assure( t != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
2608 assure( X != NULL, CPL_ERROR_NULL_INPUT,
"Null column name");
2609 assure( Y != NULL, CPL_ERROR_NULL_INPUT,
"Null column name");
2610 assure( cpl_table_has_column(t, X), CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", X);
2611 assure( cpl_table_has_column(t, Y), CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", Y);
2612 assure( sigmaY == NULL || cpl_table_has_column(t, sigmaY) , CPL_ERROR_ILLEGAL_INPUT,
2613 "No such column: %s", sigmaY);
2615 assure( polynomial_fit == NULL || !cpl_table_has_column(t, polynomial_fit),
2616 CPL_ERROR_ILLEGAL_INPUT,
"Column '%s' already present", polynomial_fit);
2618 assure( residual_square == NULL || !cpl_table_has_column(t, residual_square),
2619 CPL_ERROR_ILLEGAL_INPUT,
"Column '%s' already present", residual_square);
2622 type = cpl_table_get_column_type(t, Y);
2623 assure( type == CPL_TYPE_DOUBLE || type == CPL_TYPE_INT, CPL_ERROR_INVALID_TYPE,
2625 type = cpl_table_get_column_type(t, X);
2626 assure( type == CPL_TYPE_DOUBLE || type == CPL_TYPE_INT, CPL_ERROR_INVALID_TYPE,
2630 type = cpl_table_get_column_type(t, sigmaY);
2631 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE,
2632 CPL_ERROR_INVALID_TYPE,
2633 "Input column '%s' has wrong type (%s)",
2637 check( cpl_table_cast_column(t, X,
"_X_double", CPL_TYPE_DOUBLE),
2638 "Could not cast table column '%s' to double", X);
2639 check( cpl_table_cast_column(t, Y,
"_Y_double", CPL_TYPE_DOUBLE),
2640 "Could not cast table column '%s' to double", Y);
2643 check( cpl_table_cast_column(t, sigmaY,
"_sY_double", CPL_TYPE_DOUBLE),
2644 "Could not cast table column '%s' to double", sigmaY);
2650 check( cpl_table_new_column(t,
"_residual_square", CPL_TYPE_DOUBLE),
2651 "Could not create column");
2653 check( (N = cpl_table_get_nrow(t),
2654 x = cpl_table_get_data_double(t,
"_X_double"),
2655 y = cpl_table_get_data_double(t,
"_Y_double")),
2656 "Could not read table data");
2660 check( sy = cpl_table_get_data_double(t,
"_sY_double"),
2661 "Could not read table data");
2668 assure( N > 0, CPL_ERROR_ILLEGAL_INPUT,
"Empty table. "
2669 "No points to fit in poly 1d regression. At least 2 needed");
2671 assure( N > degree, CPL_ERROR_ILLEGAL_INPUT,
"%d points to fit in poly 1d "
2672 "regression of degree %d. At least %d needed.",
2676 uves_unwrap_vector(&vx);
2677 uves_unwrap_vector(&vy);
2679 vx = cpl_vector_wrap(N, x);
2680 vy = cpl_vector_wrap(N, y);
2684 uves_unwrap_vector(&vsy);
2685 vsy = cpl_vector_wrap(N, sy);
2695 "Could not fit polynomial");
2703 for (i = 0; i < N; i++)
2705 double xval, yval, yfit;
2707 check(( xval = cpl_table_get_double(t,
"_X_double", i, NULL),
2708 yval = cpl_table_get_double(t,
"_Y_double" ,i, NULL),
2711 cpl_table_set_double(t,
"_residual_square", i,
2712 (yfit-yval)*(yfit-yval))),
2713 "Could not evaluate polynomial");
2722 sigma2 = cpl_table_get_column_median(t,
"_residual_square") / (0.6744 * 0.6744);
2725 check( rejected = uves_erase_table_rows(t,
"_residual_square",
2726 CPL_GREATER_THAN, kappa*kappa*sigma2),
2727 "Could not remove outlier points");
2729 uves_msg_debug(
"%d of %d points rejected in kappa-sigma clipping. rms=%f",
2730 rejected, N, sqrt(mse));
2733 total_rejected += rejected;
2734 N = cpl_table_get_nrow(t);
2737 }
while (rejected > 0);
2739 cpl_table_erase_column(t,
"_residual_square");
2743 uves_msg_debug(
"%d of %d points (%f %%) rejected in kappa-sigma clipping",
2746 (100.0*total_rejected)/(N + total_rejected)
2750 if (mean_squared_error != NULL) *mean_squared_error = mse;
2753 if (polynomial_fit != NULL || residual_square != NULL)
2757 check( cpl_table_new_column(t,
"_polynomial_fit", CPL_TYPE_DOUBLE),
2758 "Could not create column");
2759 for (i = 0; i < N; i++){
2764 xval = cpl_table_get_double(t,
"_X_double", i, NULL),
2766 cpl_table_set_double(t,
"_polynomial_fit", i, yfit)),
2767 "Could not evaluate polynomial");
2771 if (residual_square != NULL)
2773 check(( cpl_table_duplicate_column(t, residual_square,
2774 t,
"_polynomial_fit"),
2775 cpl_table_subtract_columns(t, residual_square, Y),
2776 cpl_table_multiply_columns(t, residual_square, residual_square)),
2778 "Could not calculate Residual of fit");
2782 if (polynomial_fit != NULL)
2784 cpl_table_name_column(t,
"_polynomial_fit", polynomial_fit);
2788 cpl_table_erase_column(t,
"_polynomial_fit");
2792 check(( cpl_table_erase_column(t,
"_X_double"),
2793 cpl_table_erase_column(t,
"_Y_double")),
2794 "Could not delete temporary columns");
2798 check( cpl_table_erase_column(t,
"_sY_double"),
2799 "Could not delete temporary column");
2803 uves_unwrap_vector(&vx);
2804 uves_unwrap_vector(&vy);
2805 uves_unwrap_vector(&vsy);
2806 if (cpl_error_get_code() != CPL_ERROR_NONE)
2867 const char *X1,
const char *X2,
const char *Y,
2869 int degree1,
int degree2,
2870 const char *polynomial_fit,
const char *residual_square,
2871 const char *variance_fit,
2872 double *mse,
double *red_chisq,
2886 cpl_vector *vx1 = NULL;
2887 cpl_vector *vx2 = NULL;
2888 cpl_bivector *vx = NULL;
2889 cpl_vector *vy = NULL;
2890 cpl_vector *vsy= NULL;
2894 assure( t != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
2895 N = cpl_table_get_nrow(t);
2896 assure( N > 0, CPL_ERROR_ILLEGAL_INPUT,
"The table with column to compute regression has 0 rows!");
2897 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);
2899 assure( cpl_table_has_column(t, X1), CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", X1);
2900 assure( cpl_table_has_column(t, X2), CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", X2);
2901 assure( cpl_table_has_column(t, Y) , CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", Y);
2902 assure( (variance == NULL && variance_fit == NULL) || sigmaY != NULL,
2903 CPL_ERROR_INCOMPATIBLE_INPUT,
"Cannot calculate variances without sigmaY");
2906 assure( cpl_table_has_column(t, sigmaY) , CPL_ERROR_ILLEGAL_INPUT,
2907 "No such column: %s", sigmaY);
2909 if (polynomial_fit != NULL)
2911 assure( !cpl_table_has_column(t, polynomial_fit) , CPL_ERROR_ILLEGAL_INPUT,
2912 "Table already has '%s' column", polynomial_fit);
2914 if (residual_square != NULL)
2916 assure( !cpl_table_has_column(t, residual_square), CPL_ERROR_ILLEGAL_INPUT,
2917 "Table already has '%s' column", residual_square);
2919 if (variance_fit != NULL)
2921 assure( !cpl_table_has_column(t, variance_fit) , CPL_ERROR_ILLEGAL_INPUT,
2922 "Table already has '%s' column", variance_fit);
2926 type = cpl_table_get_column_type(t, X1);
2927 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
2929 type = cpl_table_get_column_type(t, X2);
2930 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
2932 type = cpl_table_get_column_type(t, Y);
2933 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
2937 type = cpl_table_get_column_type(t, sigmaY);
2938 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
2939 "Input column '%s' has wrong type (%s)",
2944 check( cpl_table_cast_column(t, X1 ,
"_X1_double", CPL_TYPE_DOUBLE),
2945 "Could not cast table column to double");
2946 check( cpl_table_cast_column(t, X2 ,
"_X2_double", CPL_TYPE_DOUBLE),
2947 "Could not cast table column to double");
2948 check( cpl_table_cast_column(t, Y ,
"_Y_double", CPL_TYPE_DOUBLE),
2949 "Could not cast table column to double");
2952 check( cpl_table_cast_column(t, sigmaY,
"_sY_double", CPL_TYPE_DOUBLE),
2953 "Could not cast table column to double");
2958 check( cpl_table_new_column(t,
"_residual_square", CPL_TYPE_DOUBLE),
2959 "Could not create column");
2964 check(( N = cpl_table_get_nrow(t),
2965 x1 = cpl_table_get_data_double(t,
"_X1_double"),
2966 x2 = cpl_table_get_data_double(t,
"_X2_double"),
2967 y = cpl_table_get_data_double(t,
"_Y_double"),
2968 res= cpl_table_get_data_double(t,
"_residual_square")),
2969 "Could not read table data");
2973 check (sy = cpl_table_get_data_double(t,
"_sY_double"),
2974 "Could not read table data");
2981 assure( N > 0, CPL_ERROR_ILLEGAL_INPUT,
"Empty table");
2984 uves_unwrap_vector(&vx1);
2985 uves_unwrap_vector(&vx2);
2986 uves_unwrap_vector(&vy);
2988 vx1 = cpl_vector_wrap(N, x1);
2989 vx2 = cpl_vector_wrap(N, x2);
2990 vy = cpl_vector_wrap(N, y);
2993 uves_unwrap_vector(&vsy);
2994 vsy = cpl_vector_wrap(N, sy);
3002 uves_unwrap_bivector_vectors(&vx);
3003 vx = cpl_bivector_wrap_vectors(vx1, vx2);
3009 "Could not fit polynomial");
3017 cpl_table_fill_column_window_double(t,
"_residual_square", 0,
3018 cpl_table_get_nrow(t), 0.0);
3020 for (i = 0; i < N; i++)
3026 res[i] = (yfit-y[i])*(yfit-y[i]);
3035 sigma2 = cpl_table_get_column_median(t,
"_residual_square") / (0.6744 * 0.6744);
3039 check( rejected = uves_erase_table_rows(t,
"_residual_square",
3040 CPL_GREATER_THAN, kappa*kappa*sigma2),
3041 "Could not remove outlier points");
3045 uves_msg_debug(
"%d of %d points rejected in kappa-sigma clipping. rms=%f",
3046 rejected, N, sqrt(sigma2));
3049 total_rejected += rejected;
3050 N = cpl_table_get_nrow(t);
3056 }
while (rejected > 0 && rejected > min_reject*(N+rejected) &&
3057 N >= (degree1 + 1)*(degree2 + 1) + 1);
3061 uves_msg_debug(
"%d of %d points (%f %%) rejected in kappa-sigma clipping",
3064 (100.0*total_rejected)/(N + total_rejected)
3074 check(( N = cpl_table_get_nrow(t),
3075 x1 = cpl_table_get_data_double(t,
"_X1_double"),
3076 x2 = cpl_table_get_data_double(t,
"_X2_double"),
3077 y = cpl_table_get_data_double(t,
"_Y_double"),
3078 res= cpl_table_get_data_double(t,
"_residual_square")),
3079 "Could not read table data");
3083 check (sy = cpl_table_get_data_double(t,
"_sY_double"),
3084 "Could not read table data");
3091 assure( N > 0, CPL_ERROR_ILLEGAL_INPUT,
"Empty table");
3094 uves_unwrap_vector(&vx1);
3095 uves_unwrap_vector(&vx2);
3096 uves_unwrap_vector(&vy);
3098 vx1 = cpl_vector_wrap(N, x1);
3099 vx2 = cpl_vector_wrap(N, x2);
3100 vy = cpl_vector_wrap(N, y);
3103 uves_unwrap_vector(&vsy);
3104 vsy = cpl_vector_wrap(N, sy);
3112 uves_unwrap_bivector_vectors(&vx);
3113 vx = cpl_bivector_wrap_vectors(vx1, vx2);
3117 if (variance_fit != NULL || variance != NULL)
3121 mse, red_chisq, &variance_local),
3122 "Could not fit polynomial");
3127 mse, red_chisq, NULL),
3128 "Could not fit polynomial");
3131 cpl_table_erase_column(t,
"_residual_square");
3134 if (polynomial_fit != NULL || residual_square != NULL)
3139 check( cpl_table_new_column(t,
"_polynomial_fit", CPL_TYPE_DOUBLE),
3140 "Could not create column");
3142 cpl_table_fill_column_window_double(t,
"_polynomial_fit", 0,
3143 cpl_table_get_nrow(t), 0.0);
3145 x1 = cpl_table_get_data_double(t,
"_X1_double");
3146 x2 = cpl_table_get_data_double(t,
"_X2_double");
3147 pf = cpl_table_get_data_double(t,
"_polynomial_fit");
3149 for (i = 0; i < N; i++){
3151 double x1val, x2val, yfit;
3153 check(( x1val = cpl_table_get_double(t,
"_X1_double", i, NULL),
3154 x2val = cpl_table_get_double(t,
"_X2_double", i, NULL),
3157 cpl_table_set_double(t,
"_polynomial_fit", i, yfit)),
3158 "Could not evaluate polynomial");
3166 if (residual_square != NULL)
3168 check(( cpl_table_duplicate_column(t, residual_square,
3169 t,
"_polynomial_fit"),
3170 cpl_table_subtract_columns(t, residual_square, Y),
3171 cpl_table_multiply_columns(t, residual_square, residual_square)),
3173 "Could not calculate Residual of fit");
3177 if (polynomial_fit != NULL)
3179 cpl_table_name_column(t,
"_polynomial_fit", polynomial_fit);
3183 cpl_table_erase_column(t,
"_polynomial_fit");
3188 if (variance_fit != NULL)
3193 check( cpl_table_new_column(t, variance_fit, CPL_TYPE_DOUBLE),
3194 "Could not create column");
3196 cpl_table_fill_column_window_double(t, variance_fit, 0,
3197 cpl_table_get_nrow(t), 0.0);
3199 x1 = cpl_table_get_data_double(t,
"_X1_double");
3200 x2 = cpl_table_get_data_double(t,
"_X2_double");
3201 vf = cpl_table_get_data_double(t, variance_fit);
3203 for (i = 0; i < N; i++)
3206 double x1val, x2val, yfit_variance;
3207 check(( x1val = cpl_table_get_double(t,
"_X1_double", i, NULL),
3208 x2val = cpl_table_get_double(t,
"_X2_double", i, NULL),
3212 cpl_table_set_double(t, variance_fit, i, yfit_variance)),
3213 "Could not evaluate polynomial");
3222 check(( cpl_table_erase_column(t,
"_X1_double"),
3223 cpl_table_erase_column(t,
"_X2_double"),
3224 cpl_table_erase_column(t,
"_Y_double")),
3225 "Could not delete temporary columns");
3229 check( cpl_table_erase_column(t,
"_sY_double"),
3230 "Could not delete temporary column");
3234 uves_unwrap_bivector_vectors(&vx);
3235 uves_unwrap_vector(&vx1);
3236 uves_unwrap_vector(&vx2);
3237 uves_unwrap_vector(&vy);
3238 uves_unwrap_vector(&vsy);
3240 if (variance != NULL)
3242 *variance = variance_local;
3248 if (cpl_error_get_code() != CPL_ERROR_NONE)
3303 const char *X1,
const char *X2,
const char *Y,
3305 const char *polynomial_fit,
3306 const char *residual_square,
3307 const char *variance_fit,
3308 double *mean_squared_error,
double *red_chisq,
3310 int maxdeg1,
int maxdeg2,
double min_rms,
3313 const double *min_val,
3314 const double *max_val,
3315 int npos,
double positions[][2])
3321 double **mse = NULL;
3322 bool adjust1 =
true;
3323 bool adjust2 =
true;
3324 bool finished =
false;
3327 cpl_table *temp = NULL;
3330 assure( (min_val == NULL && max_val == NULL) || positions != NULL,
3331 CPL_ERROR_NULL_INPUT,
3332 "Missing positions array");
3334 check_nomsg( y_unit = cpl_table_get_column_unit(t, Y));
3340 assure(maxdeg1 >= 1 && maxdeg2 >= 1, CPL_ERROR_ILLEGAL_INPUT,
3341 "Illegal max. degrees: (%d, %d)",
3344 mse = cpl_calloc(maxdeg1+1,
sizeof(
double *));
3346 for (i = 0; i < maxdeg1+1; i++)
3349 mse[i] = cpl_calloc(maxdeg2+1,
sizeof(
double));
3352 for (j = 0; j < maxdeg2+1; j++)
3358 temp = cpl_table_duplicate(t);
3367 &mse[deg1][deg2], NULL,
3370 "Error fitting polynomial");
3372 uves_msg_low(
"(%d, %d)-degree: RMS = %.3g %s (%" CPL_SIZE_FORMAT
"/%" CPL_SIZE_FORMAT
" outliers)",
3373 deg1, deg2, sqrt(mse[deg1][deg2]), y_unit,
3374 cpl_table_get_nrow(t) - cpl_table_get_nrow(temp),
3375 cpl_table_get_nrow(t));
3377 uves_msg_debug(
"(%d, %d)-degree: RMS = %.3g %s (%" CPL_SIZE_FORMAT
"/%" CPL_SIZE_FORMAT
" outliers)",
3378 deg1, deg2, sqrt(mse[deg1][deg2]), y_unit,
3379 cpl_table_get_nrow(t) - cpl_table_get_nrow(temp),
3380 cpl_table_get_nrow(t));
3385 int new_deg1, new_deg2;
3390 adjust1 = adjust1 && (deg1 + 2 <= maxdeg1);
3391 adjust2 = adjust2 && (deg2 + 2 <= maxdeg2);
3407 for (new_deg1 = deg1; new_deg1 <= deg1+2; new_deg1++)
3408 for (new_deg2 = deg2; new_deg2 <= deg2+2; new_deg2++)
3410 (new_deg1 == deg1+1 && new_deg2 == deg2 && adjust1) ||
3411 (new_deg1 == deg1+2 && new_deg2 == deg2 && adjust1) ||
3412 (new_deg1 == deg1 && new_deg2 == deg2+1 && adjust2) ||
3413 (new_deg1 == deg1 && new_deg2 == deg2+2 && adjust2) ||
3414 (new_deg1 == deg1+1 && new_deg2 == deg2+1 && adjust1 && adjust2)
3416 && mse[new_deg1][new_deg2] < 0)
3420 uves_free_table(&temp);
3421 temp = cpl_table_duplicate(t);
3436 if (cpl_error_get_code() == CPL_ERROR_SINGULAR_MATRIX)
3442 new_deg1, new_deg2);
3445 new_deg1, new_deg2);
3447 mse[new_deg1][new_deg2] = DBL_MAX/2;
3451 assure( cpl_error_get_code() == CPL_ERROR_NONE,
3452 cpl_error_get_code(),
3453 "Error fitting (%d, %d)-degree polynomial",
3454 new_deg1, new_deg2 );
3456 rejected = cpl_table_get_nrow(t) - cpl_table_get_nrow(temp);
3459 uves_msg_low(
"(%d,%d)-degree: RMS = %.3g %s (%d/%" CPL_SIZE_FORMAT
" outliers)",
3460 new_deg1, new_deg2, sqrt(mse[new_deg1][new_deg2]), y_unit,
3461 rejected, cpl_table_get_nrow(t));
3463 uves_msg_debug(
"(%d,%d)-degree: RMS = %.3g %s (%d/%" CPL_SIZE_FORMAT
" outliers)",
3464 new_deg1, new_deg2, sqrt(mse[new_deg1][new_deg2]), y_unit,
3465 rejected, cpl_table_get_nrow(t));
3468 if (min_val != NULL || max_val != NULL)
3470 for (i = 0; i < npos; i++)
3474 positions[i][0], positions[i][1]);
3475 if (min_val != NULL && val < *min_val)
3480 mse[new_deg1][new_deg2] = DBL_MAX/2;
3483 if (max_val != NULL && val > *max_val)
3488 mse[new_deg1][new_deg2] = DBL_MAX/2;
3496 if (rejected >= (4*cpl_table_get_nrow(t))/5)
3498 mse[new_deg1][new_deg2] = DBL_MAX/2;
3512 m = mse[deg1][deg2];
3515 && (m - mse[deg1+1][deg2])/m > 0.1
3516 && (!adjust2 || mse[deg1+1][deg2] <= mse[deg1][deg2+1])
3528 (m - mse[deg1][deg2+1])/m > 0.1
3529 && (!adjust1 || mse[deg1+1][deg2] > mse[deg1][deg2+1])
3535 else if (adjust1 && adjust2 && (m - mse[deg1+1][deg2+1])/m > 0.1)
3542 && (m - mse[deg1+2][deg2])/m > 0.1
3543 && (!adjust2 || mse[deg1+2][deg2] <= mse[deg1][deg2+2])
3550 && (m - mse[deg1][deg2+2])/m > 0.1
3551 && (!adjust1 || mse[deg1+2][deg2] < mse[deg1][deg2+2]))
3558 finished = finished || (sqrt(mse[deg1][deg2]) < min_rms);
3560 }
while (!finished);
3567 polynomial_fit, residual_square,
3569 mean_squared_error, red_chisq,
3570 variance, kappa, min_reject),
3571 "Error fitting (%d, %d)-degree polynomial", deg1, deg2);
3574 uves_msg_low(
"Using degree (%d, %d), RMS = %.3g %s", deg1, deg2,
3575 sqrt(mse[deg1][deg2]), y_unit);
3577 uves_msg_debug(
"Using degree (%d, %d), RMS = %.3g %s", deg1, deg2,
3578 sqrt(mse[deg1][deg2]), y_unit);
3583 for (i = 0; i < maxdeg1+1; i++)
3592 uves_free_table(&temp);
3594 return bivariate_fit;
3611 const char *result = NULL;
3612 unsigned int prefix_length;
3614 assure( s != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
3615 assure( prefix != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
3617 prefix_length = strlen(prefix);
3619 assure( strlen(s) >= prefix_length &&
3620 strncmp(s, prefix, prefix_length) == 0,
3621 CPL_ERROR_INCOMPATIBLE_INPUT,
"'%s' is not a prefix of '%s'",
3624 result = s + prefix_length;
3644 static double V1, V2, S;
3645 static int phase = 0;
3650 double U1 = (double)rand() / RAND_MAX;
3651 double U2 = (double)rand() / RAND_MAX;
3655 S = V1 * V1 + V2 * V2;
3656 }
while(S >= 1 || S == 0);
3658 X = V1 * sqrt(-2 * log(S) / S);
3660 X = V2 * sqrt(-2 * log(S) / S);
3681 const char *column_y,
int *istart )
3686 const double *x, *y;
3688 check( x = cpl_table_get_data_double_const(t, column_x),
3689 "Error reading column '%s'", column_x);
3690 check( y = cpl_table_get_data_double_const(t, column_y),
3691 "Error reading column '%s'", column_y);
3693 n = cpl_table_get_nrow(t);
3720 double yp1, yp2, yp = 0;
3721 double xpi, xpi1, l1, l2, lp1, lp2;
3724 if ( x[0] <= x[n-1] && (xp < x[0] || xp > x[n-1]) )
return 0.0;
3725 if ( x[0] > x[n-1] && (xp > x[0] || xp < x[n-1]) )
return 0.0;
3727 if ( x[0] <= x[n-1] )
3729 for ( i = (*istart)+1; i <= n && xp >= x[i-1]; i++ )
3734 for ( i = (*istart)+1; i <= n && xp <= x[i-1]; i++ )
3741 lp1 = 1.0 / (x[i-1] - x[i]);
3746 yp1 = (y[1] - y[0]) / (x[1] - x[0]);
3750 yp1 = (y[i] - y[i-2]) / (x[i] - x[i-2]);
3755 yp2 = (y[n-1] - y[n-2]) / (x[n-1] - x[n-2]);
3759 yp2 = (y[i+1] - y[i-1]) / (x[i+1] - x[i-1]);
3767 yp = y[i-1]*(1 - 2.0*lp1*xpi)*l1*l1 +
3768 y[i]*(1 - 2.0*lp2*xpi1)*l2*l2 +
3769 yp1*xpi*l1*l1 + yp2*xpi1*l2*l2;
3793 double a, b, h, yp = 0;
3803 if ( xp < x[1] || xp > x[n] )
3807 else if ( xp == x[1] )
3812 for ( k = klo; k < n && xp > x[k]; k++ )
3815 klo = *kstart = k-1;
3818 h = x[khi] - x[klo];
3819 assure( h != 0.0, CPL_ERROR_DIVISION_BY_ZERO,
3820 "Empty x-value range: xlo = %e ; xhi = %e", x[khi], x[klo]);
3822 a = (x[khi] - xp) / h;
3823 b = (xp - x[klo]) / h;
3825 yp = a*y[klo] + b*y[khi] + ((a*a*a - a)*y2[klo] + (b*b*b - b)*y2[khi])*
3846 bool is_sorted =
true;
3849 double previous, current;
3852 passure(cpl_table_has_column(t, column),
"No column '%s'", column);
3853 passure(cpl_table_get_column_type(t, column) == CPL_TYPE_DOUBLE,
" ");
3855 N = cpl_table_get_nrow(t);
3859 previous = cpl_table_get_double(t, column, 0, NULL);
3861 for(i = 1; i < N && is_sorted; i++)
3863 current = cpl_table_get_double(t, column, i, NULL);
3867 is_sorted = is_sorted && ( current >= previous );
3872 is_sorted = is_sorted && ( current <= previous );
3897 cpl_table *result = NULL;
3900 result = cpl_table_new(0),
3901 cpl_table_new_column(result,
"TraceID" , CPL_TYPE_INT),
3902 cpl_table_new_column(result,
"Offset" , CPL_TYPE_DOUBLE),
3903 cpl_table_new_column(result,
"Tracemask", CPL_TYPE_INT)),
3904 "Error creating table");
3923 int fibre_ID,
double fibre_offset,
int fibre_mask)
3927 assure( traces != NULL, CPL_ERROR_NULL_INPUT,
"Null table!");
3931 size = cpl_table_get_nrow(traces),
3932 cpl_table_set_size (traces, size+1),
3933 cpl_table_set_int (traces,
"TraceID" , size, fibre_ID),
3934 cpl_table_set_double(traces,
"Offset" , size, fibre_offset),
3935 cpl_table_set_int (traces,
"Tracemask", size, fibre_mask)),
3936 "Error updating table");
3939 return cpl_error_get_code();
3953 cpl_table* tab=NULL;
3955 tab=cpl_table_load(tname,1,0);
3958 check_nomsg(uves_table_save(tab,head,NULL,tname,CPL_IO_DEFAULT));
3961 uves_free_table(&tab);
3962 uves_free_propertylist(&head);
3963 return cpl_error_get_code();
3979 cpl_table* tab1=NULL;
3980 cpl_table* tab2=NULL;
3983 tab1=cpl_table_load(tname1,1,0);
3985 tab2=cpl_table_load(tname2,1,0);
3989 check_nomsg(uves_table_save(tab2,head2,NULL,tname2,CPL_IO_DEFAULT));
3992 uves_free_table(&tab1);
3993 uves_free_table(&tab2);
3994 uves_free_propertylist(&head2);
3995 return cpl_error_get_code();
4012 const char* colname=NULL;
4014 cpl_array *names=NULL;
4016 assure( *table != NULL, CPL_ERROR_NULL_INPUT,
"Null input table!");
4017 ncols = cpl_table_get_ncol(*table);
4018 names = cpl_table_get_column_names(*table);
4019 for(i=0;i<ncols;i++) {
4020 colname=cpl_array_get_string(names, i);
4021 cpl_table_set_column_unit(*table,colname,NULL);
4025 uves_free_array(&names);
4027 return cpl_error_get_code();
4045 const char* colname=NULL;
4046 const char* unit1=NULL;
4049 cpl_array *names=NULL;
4051 assure( table1 != NULL, CPL_ERROR_NULL_INPUT,
"Null input table!");
4052 assure( *table2 != NULL, CPL_ERROR_NULL_INPUT,
"Null input table!");
4053 ncols1 = cpl_table_get_ncol(*table1);
4054 ncols2 = cpl_table_get_ncol(*table2);
4055 assure( ncols1 == ncols2, CPL_ERROR_NULL_INPUT,
4056 "n columns (tab1) != n columns (tab2)");
4058 names = cpl_table_get_column_names(*table1);
4059 for(i=0;i<ncols1;i++) {
4060 colname=cpl_array_get_string(names, i);
4061 unit1=cpl_table_get_column_unit(*table1,colname);
4062 cpl_table_set_column_unit(*table2,colname,unit1);
4066 uves_free_array(&names);
4068 return cpl_error_get_code();
4102 static void fmoffa_i(
float x,
const double a[],
double *y,
double dyda[])
4107 double fac=0, fac2=0, fac4= 0, fac4i=0, arg=0, arg2=0;
4108 double a2i=0, m = 0, p = 0, dif =0;
4109 double sqrt5 = 2.23606797749979;
4113 a2i = 1.0/(a[2]*sqrt5);
4125 m = a[3]*fac4i * a2i*16/(5.0*M_PI);
4126 *y = m + a[4]*(1.0+dif*a[5]);
4127 p = 8.0*m/fac*arg*a2i;
4130 dyda[2] = p*dif/a[2] - m/a[2];
4133 dyda[1]=p-a[4]*a[5];
4135 dyda[4]=1.0+dif*a[5];
4142 printf(
"fmoffat_i \n");
4143 for (i = 1;i<=npar;i++) printf(
"a[%1i] %f :\n",i,a[i]);
4145 printf(
"fmoffat_i ");
4146 for (i = 1;i<=npar;i++) printf(
"%i %f :",i,dyda[i]);
4171 static void fmoffa_c(
float x,
const double a[],
double *y,
double dyda[])
4179 double const xgl[3] = {-0.387298334621,0.,0.387298334621};
4180 double const wgl[3] = {.2777777777778,0.444444444444,0.2777777777778};
4190 a = C_TO_FORTRAN_INDEXING(a);
4191 dyda = C_TO_FORTRAN_INDEXING(dyda);
4194 for (i = 1;i<=npar;i++) dyda[i] = 0.;
4199 for (j=0; j < npoint; j++)
4203 fmoffa_i(xmod,a,&ypar,&dydapar[-1]);
4205 *y = *y + ypar*wgl[j];
4207 for (i = 1; i <= npar; i++)
4209 dyda[i] = dyda[i] + dydapar[i-1]*wgl[j] ;
4221 printf(
"fmoffat_c ");
4222 for (i = 1;i<=npar;i++) printf(
"%i %f %f: \n",i,a[i],dyda[i]);
4291 double sigma = a[1];
4312 A/(sqrt(2*M_PI*sigma*sigma)) *
4313 exp(- (x[0] - my)*(x[0] - my)
4346 double sigma = a[1];
4373 factor = exp( -(x[0] - my)*(x[0] - my)/(2*sigma*sigma) )
4374 / (sqrt(2*M_PI*sigma*sigma));
4376 result[0] = A * factor * (x[0]-my) / (sigma*sigma);
4377 result[1] = A * factor * ((x[0]-my)*(x[0]-my) / (sigma*sigma) - 1) / sigma;
4411 double sigma = a[1];
4432 *result = B + C*(x[0] - my) +
4433 A/(sqrt(2*M_PI*sigma*sigma)) *
4434 exp(- (x[0] - my)*(x[0] - my)
4470 double sigma = a[1];
4501 factor = exp( -(x[0] - my)*(x[0] - my)/(2*sigma*sigma) )
4502 / (sqrt(2*M_PI*sigma*sigma));
4504 result[0] = A * factor * (x[0]-my) / (sigma*sigma);
4505 result[1] = A * factor * ((x[0]-my)*(x[0]-my) / (sigma*sigma) - 1) / sigma;
4508 result[4] = x[0] - my;
4532 const cpl_image *spectrum,
const cpl_image *sky,
4533 const cpl_image *cosmic_image,
4534 const uves_extract_profile *profile,
4537 cpl_image *image = NULL;
4539 cpl_binary *bpm = NULL;
4540 bool loop_y =
false;
4544 bool new_format =
true;
4546 image = cpl_image_new(pos->nx, pos->ny, CPL_TYPE_DOUBLE);
4548 if (image_noise != NULL) {
4549 *image_noise = cpl_image_new(pos->nx, pos->ny, CPL_TYPE_DOUBLE);
4551 cpl_image_add_scalar(*image_noise, 0.01);
4554 if (image_header != NULL) {
4557 uves_propertylist_append_double(*image_header, UVES_MJDOBS, 60000);
4558 uves_propertylist_append_double(*image_header, UVES_RON(new_format, chip), ron);
4559 uves_propertylist_append_double(*image_header, UVES_GAIN(new_format, chip), gain);
4564 pos->minorder, pos->maxorder,
4571 uves_extract_profile_set(profile, pos, NULL);
4572 for (pos->y = pos->ylow; pos->y <= pos->yhigh; pos->y++) {
4575 double flux, sky_flux;
4577 int spectrum_row = pos->order - pos->minorder + 1;
4579 double prof = uves_extract_profile_evaluate(profile, pos);
4583 sky_flux = cpl_image_get(sky, pos->x, spectrum_row, &bad)/pos->sg.length;
4590 flux = cpl_image_get(spectrum, pos->x, spectrum_row, &bad) * prof + sky_flux;
4593 noise = sqrt(gain)*sqrt(ron*ron/(gain*gain) + sky_flux/gain + flux/gain);
4595 cpl_image_set(image, pos->x, pos->y,
4597 if (image_noise != NULL) cpl_image_set(*image_noise, pos->x, pos->y, noise);
4602 if (cosmic_image != NULL) {
4603 double cr_val = 2*cpl_image_get_max(image);
4610 pos->minorder, pos->maxorder,
4617 if (cpl_image_get(cosmic_image, pos->x, pos->y, &is_rejected) > 0) {
4618 cpl_image_set(image, pos->x, pos->y, cr_val);
4628 uves_frameset_dump(cpl_frameset*
set)
4631 cpl_frame* frm=NULL;
4635 cknull(
set,
"Null input frameset");
4639 uves_msg(
"frame %d tag %s filename %s group %d",
4641 cpl_frame_get_tag(frm),
4642 cpl_frame_get_filename(frm),
4643 cpl_frame_get_group(frm));
4645 }
while ((frm=cpl_frameset_get_next(
set)) != NULL);
4672 uves_image_smooth_x(cpl_image * inp,
const int r)
4691 cpl_image* out=NULL;
4693 cknull(inp,
"Null in put image, exit");
4700 for(i=r;i<sx-r;i++) {
4702 pout[j*sx+i]+=pinp[j*sx+i+k];
4710 if(cpl_error_get_code() != CPL_ERROR_NONE) {
4740 uves_image_smooth_y(cpl_image * inp,
const int r)
4759 cpl_image* out=NULL;
4761 cknull(inp,
"Null in put image, exit");
4767 for(j=r;j<sy-r;j++) {
4770 pout[j*sx+i]+=pinp[(j+k)*sx+i];
4778 if(cpl_error_get_code() != CPL_ERROR_NONE) {
4805 uves_image_smooth_mean_x(cpl_image * inp,
const int r)
4824 cpl_image* out=NULL;
4826 cknull(inp,
"Null in put image, exit");
4833 for(i=r;i<sx-r;i++) {
4835 pout[j*sx+i]+=pinp[j*sx+i+k];
4843 if(cpl_error_get_code() != CPL_ERROR_NONE) {
4870 uves_image_smooth_median_x(cpl_image * inp,
const int r)
4887 cpl_image* out=NULL;
4890 cknull(inp,
"Null in put image, exit");
4897 for(i=1+r;i<sx-r;i++) {
4898 pout[j*sx+i]=(float)cpl_image_get_median_window(inp,i,j,i+r,j);
4904 if(cpl_error_get_code() != CPL_ERROR_NONE) {
4929 uves_image_smooth_fft(cpl_image * inp,
const int fx)
4935 cpl_image* out=NULL;
4936 cpl_image* im_re=NULL;
4937 cpl_image* im_im=NULL;
4938 cpl_image* ifft_re=NULL;
4939 cpl_image* ifft_im=NULL;
4940 cpl_image* filter=NULL;
4945 cknull(inp,
"Null in put image, exit");
4946 check_nomsg(im_re = cpl_image_cast(inp, CPL_TYPE_DOUBLE));
4947 check_nomsg(im_im = cpl_image_cast(inp, CPL_TYPE_DOUBLE));
4950 check_nomsg(cpl_image_fft(im_re,im_im,CPL_FFT_DEFAULT));
4960 cpl_image_multiply(im_re,filter);
4961 cpl_image_multiply(im_im,filter);
4963 uves_free_image(&filter);
4965 check_nomsg(ifft_re = cpl_image_duplicate(im_re));
4966 check_nomsg(ifft_im = cpl_image_duplicate(im_im));
4968 uves_free_image(&im_re);
4969 uves_free_image(&im_im);
4972 check_nomsg(cpl_image_fft(ifft_re,ifft_im,CPL_FFT_INVERSE));
4973 check_nomsg(out = cpl_image_cast(ifft_re, CPL_TYPE_FLOAT));
4977 uves_free_image(&ifft_re);
4978 uves_free_image(&ifft_im);
4979 uves_free_image(&filter);
4980 uves_free_image(&im_re);
4981 uves_free_image(&im_im);
4983 if(cpl_error_get_code() != CPL_ERROR_NONE) {
5005 cpl_image* img=NULL;
5008 cpl_vector* values=NULL;
5015 pval=cpl_vector_get_data(values);
5016 for(i=0;i<size;i++) {
5017 img=cpl_imagelist_get(iml,i);
5018 irplib_ksigma_clip(img,1,1,
5019 cpl_image_get_size_x(img),
5020 cpl_image_get_size_y(img),
5021 5,kappa,1.e-5,&mean,&stdev);
5022 uves_msg(
"Ima %d mean level: %g",i+1,mean);
5046 cpl_image* img=NULL;
5052 pval=cpl_vector_get_data(values);
5053 for(i=0;i<size;i++) {
5054 img=cpl_imagelist_get(*iml,i);
5055 cpl_image_subtract_scalar(img,pval[i]);
5056 cpl_imagelist_set(*iml,img,i);
5061 return cpl_error_get_code();
5085 const double sigma_x,
5086 const double sigma_y)
5095 double gaussval= 0.0;
5096 double inv_sigma_x=1./sigma_x;
5097 double inv_sigma_y=1./sigma_y;
5101 cpl_image *lowpass_image=NULL;
5104 lowpass_image = cpl_image_new (xs, ys, CPL_TYPE_FLOAT);
5105 if (lowpass_image == NULL) {
5107 cpl_error_get_message());
5114 data = cpl_image_get_data_float(lowpass_image);
5129 for (i=1 ; i<=hlx ; i++) {
5130 x = i * inv_sigma_x;
5131 gaussval = exp(-0.5*x*x);
5133 data[xs-i] = gaussval;
5136 for (j=1; j<=hly ; j++) {
5137 y = j * inv_sigma_y;
5139 data[j*xs] = exp(-0.5*y*y);
5140 data[(ys-j)*xs] = exp(-0.5*y*y);
5142 for (i=1 ; i<=hlx ; i++) {
5144 x = i * inv_sigma_x;
5145 gaussval = exp (-0.5*(x*x+y*y));
5146 data[j*xs+i] = gaussval;
5147 data[(j+1)*xs-i] = gaussval;
5148 data[(ys-j)*xs+i] = gaussval;
5149 data[(ys+1-j)*xs-i] = gaussval;
5161 return lowpass_image;
5177 cpl_image* result=NULL;
5178 cpl_image* diff=NULL;
5179 cpl_image* flat_smooth=NULL;
5180 cpl_array* val=NULL;
5181 cpl_matrix* mx=NULL;
5192 int filter_width_x=7;
5193 int filter_width_y=7;
5202 const double* pima=NULL;
5208 passure( flat !=NULL ,
"NULL input flat ");
5209 passure( head !=NULL ,
"NULL input head ");
5212 sx=cpl_image_get_size_x(flat);
5213 sy=cpl_image_get_size_y(flat);
5220 if (binx>1) filter_width_x=5;
5221 if (biny>1) filter_width_y=5;
5225 check_nomsg(mx=cpl_matrix_new(filter_width_x,filter_width_y));
5227 for(j=0; j< filter_width_y; j++){
5228 for(i=0; i< filter_width_x; i++){
5229 cpl_matrix_set( mx, i,j,1.0);
5235 check_nomsg(flat_smooth=uves_image_filter_median(flat,mx));
5240 check_nomsg(cpl_image_subtract(diff,flat_smooth));
5249 val=cpl_array_new(npixs,CPL_TYPE_DOUBLE);
5250 check_nomsg(cpl_array_fill_window_double(val,0,npixs,0));
5252 check_nomsg(pima=cpl_image_get_data_double_const(flat));
5253 check_nomsg(pdif=cpl_image_get_data_double(diff));
5255 for(i=0;i<npixs;i++) {
5256 if(pima[i]>med_flat) {
5270 for(i=0;i<niter;i++) {
5271 for(k=0;k<size;k++) {
5272 if(fabs(pval[k]-mean)>stdev_x_4) {
5273 cpl_array_set_invalid(val,k);
5276 mean=cpl_array_get_mean(val);
5277 stdev=cpl_array_get_stdev(val);
5282 result=cpl_image_new(sx,sy,CPL_TYPE_DOUBLE);
5283 pres=cpl_image_get_data_double(result);
5284 for(i=0;i<npixs;i++) {
5285 if(fabs(pdif[i])<stdev_x_4) {
5297 uves_free_array(&val);
5298 uves_free_image(&diff);
5299 uves_free_image(&flat_smooth);
5300 uves_free_matrix(&mx);