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");
2456 "Error reading master dark exposure time");
2458 uves_msg(
"Rescaling master dark from %f s to %f s exposure time",
2459 mdark_exptime, image_exptime);
2461 check( normalized_mdark =
2462 cpl_image_multiply_scalar_create(master_dark,
2463 image_exptime / mdark_exptime),
2464 "Error normalizing master dark");
2466 check( cpl_image_subtract(image, normalized_mdark),
2467 "Error subtracting master dark");
2469 uves_msg_warning(
"noise rescaled master dark %g",cpl_image_get_stdev(normalized_mdark));
2473 uves_free_image(&normalized_mdark);
2474 return cpl_error_get_code();
2494 return (first_abs_order +
2495 (relative_order-1)*((last_abs_order > first_abs_order) ? 1 : -1));
2516 const char *residual2,
2519 double mean = 0, median, sigma2;
2524 check_nomsg(median = cpl_table_get_column_median(t, column));
2528 check_nomsg(cpl_table_duplicate_column(t, residual2, t, column));
2529 check_nomsg(cpl_table_subtract_scalar(t, residual2, median));
2530 check_nomsg(cpl_table_multiply_columns(t, residual2, residual2));
2537 check_nomsg(sigma2 = cpl_table_get_column_median(t, residual2) / (0.6744 * 0.6744));
2542 check_nomsg( rejected = uves_erase_table_rows(t, residual2,
2544 kappa*kappa*sigma2));
2546 check_nomsg(cpl_table_erase_column(t, residual2));
2548 }
while (rejected > 0);
2550 check_nomsg(mean = cpl_table_get_column_mean(t, column));
2592 const char *X,
const char *Y,
const char *sigmaY,
2594 const char *polynomial_fit,
const char *residual_square,
2595 double *mean_squared_error,
double kappa)
2598 int total_rejected = 0;
2605 cpl_vector *vx = NULL;
2606 cpl_vector *vy = NULL;
2607 cpl_vector *vsy = NULL;
2611 assure( t != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
2612 assure( X != NULL, CPL_ERROR_NULL_INPUT,
"Null column name");
2613 assure( Y != NULL, CPL_ERROR_NULL_INPUT,
"Null column name");
2614 assure( cpl_table_has_column(t, X), CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", X);
2615 assure( cpl_table_has_column(t, Y), CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", Y);
2616 assure( sigmaY == NULL || cpl_table_has_column(t, sigmaY) , CPL_ERROR_ILLEGAL_INPUT,
2617 "No such column: %s", sigmaY);
2619 assure( polynomial_fit == NULL || !cpl_table_has_column(t, polynomial_fit),
2620 CPL_ERROR_ILLEGAL_INPUT,
"Column '%s' already present", polynomial_fit);
2622 assure( residual_square == NULL || !cpl_table_has_column(t, residual_square),
2623 CPL_ERROR_ILLEGAL_INPUT,
"Column '%s' already present", residual_square);
2626 type = cpl_table_get_column_type(t, Y);
2627 assure( type == CPL_TYPE_DOUBLE || type == CPL_TYPE_INT, CPL_ERROR_INVALID_TYPE,
2629 type = cpl_table_get_column_type(t, X);
2630 assure( type == CPL_TYPE_DOUBLE || type == CPL_TYPE_INT, CPL_ERROR_INVALID_TYPE,
2634 type = cpl_table_get_column_type(t, sigmaY);
2635 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE,
2636 CPL_ERROR_INVALID_TYPE,
2637 "Input column '%s' has wrong type (%s)",
2641 check( cpl_table_cast_column(t, X,
"_X_double", CPL_TYPE_DOUBLE),
2642 "Could not cast table column '%s' to double", X);
2643 check( cpl_table_cast_column(t, Y,
"_Y_double", CPL_TYPE_DOUBLE),
2644 "Could not cast table column '%s' to double", Y);
2647 check( cpl_table_cast_column(t, sigmaY,
"_sY_double", CPL_TYPE_DOUBLE),
2648 "Could not cast table column '%s' to double", sigmaY);
2654 check( cpl_table_new_column(t,
"_residual_square", CPL_TYPE_DOUBLE),
2655 "Could not create column");
2657 check( (N = cpl_table_get_nrow(t),
2658 x = cpl_table_get_data_double(t,
"_X_double"),
2659 y = cpl_table_get_data_double(t,
"_Y_double")),
2660 "Could not read table data");
2664 check( sy = cpl_table_get_data_double(t,
"_sY_double"),
2665 "Could not read table data");
2672 assure( N > 0, CPL_ERROR_ILLEGAL_INPUT,
"Empty table. "
2673 "No points to fit in poly 1d regression. At least 2 needed");
2675 assure( N > degree, CPL_ERROR_ILLEGAL_INPUT,
"%d points to fit in poly 1d "
2676 "regression of degree %d. At least %d needed.",
2680 uves_unwrap_vector(&vx);
2681 uves_unwrap_vector(&vy);
2683 vx = cpl_vector_wrap(N, x);
2684 vy = cpl_vector_wrap(N, y);
2688 uves_unwrap_vector(&vsy);
2689 vsy = cpl_vector_wrap(N, sy);
2699 "Could not fit polynomial");
2707 for (i = 0; i < N; i++)
2709 double xval, yval, yfit;
2711 check(( xval = cpl_table_get_double(t,
"_X_double", i, NULL),
2712 yval = cpl_table_get_double(t,
"_Y_double" ,i, NULL),
2715 cpl_table_set_double(t,
"_residual_square", i,
2716 (yfit-yval)*(yfit-yval))),
2717 "Could not evaluate polynomial");
2726 sigma2 = cpl_table_get_column_median(t,
"_residual_square") / (0.6744 * 0.6744);
2729 check( rejected = uves_erase_table_rows(t,
"_residual_square",
2730 CPL_GREATER_THAN, kappa*kappa*sigma2),
2731 "Could not remove outlier points");
2733 uves_msg_debug(
"%d of %d points rejected in kappa-sigma clipping. rms=%f",
2734 rejected, N, sqrt(mse));
2737 total_rejected += rejected;
2738 N = cpl_table_get_nrow(t);
2741 }
while (rejected > 0);
2743 cpl_table_erase_column(t,
"_residual_square");
2747 uves_msg_debug(
"%d of %d points (%f %%) rejected in kappa-sigma clipping",
2750 (100.0*total_rejected)/(N + total_rejected)
2754 if (mean_squared_error != NULL) *mean_squared_error = mse;
2757 if (polynomial_fit != NULL || residual_square != NULL)
2761 check( cpl_table_new_column(t,
"_polynomial_fit", CPL_TYPE_DOUBLE),
2762 "Could not create column");
2763 for (i = 0; i < N; i++){
2768 xval = cpl_table_get_double(t,
"_X_double", i, NULL),
2770 cpl_table_set_double(t,
"_polynomial_fit", i, yfit)),
2771 "Could not evaluate polynomial");
2775 if (residual_square != NULL)
2777 check(( cpl_table_duplicate_column(t, residual_square,
2778 t,
"_polynomial_fit"),
2779 cpl_table_subtract_columns(t, residual_square, Y),
2780 cpl_table_multiply_columns(t, residual_square, residual_square)),
2782 "Could not calculate Residual of fit");
2786 if (polynomial_fit != NULL)
2788 cpl_table_name_column(t,
"_polynomial_fit", polynomial_fit);
2792 cpl_table_erase_column(t,
"_polynomial_fit");
2796 check(( cpl_table_erase_column(t,
"_X_double"),
2797 cpl_table_erase_column(t,
"_Y_double")),
2798 "Could not delete temporary columns");
2802 check( cpl_table_erase_column(t,
"_sY_double"),
2803 "Could not delete temporary column");
2807 uves_unwrap_vector(&vx);
2808 uves_unwrap_vector(&vy);
2809 uves_unwrap_vector(&vsy);
2810 if (cpl_error_get_code() != CPL_ERROR_NONE)
2871 const char *X1,
const char *X2,
const char *Y,
2873 int degree1,
int degree2,
2874 const char *polynomial_fit,
const char *residual_square,
2875 const char *variance_fit,
2876 double *mse,
double *red_chisq,
2890 cpl_vector *vx1 = NULL;
2891 cpl_vector *vx2 = NULL;
2892 cpl_bivector *vx = NULL;
2893 cpl_vector *vy = NULL;
2894 cpl_vector *vsy= NULL;
2898 assure( t != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
2899 N = cpl_table_get_nrow(t);
2900 assure( N > 0, CPL_ERROR_ILLEGAL_INPUT,
"The table with column to compute regression has 0 rows!");
2901 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);
2903 assure( cpl_table_has_column(t, X1), CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", X1);
2904 assure( cpl_table_has_column(t, X2), CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", X2);
2905 assure( cpl_table_has_column(t, Y) , CPL_ERROR_ILLEGAL_INPUT,
"No such column: %s", Y);
2906 assure( (variance == NULL && variance_fit == NULL) || sigmaY != NULL,
2907 CPL_ERROR_INCOMPATIBLE_INPUT,
"Cannot calculate variances without sigmaY");
2910 assure( cpl_table_has_column(t, sigmaY) , CPL_ERROR_ILLEGAL_INPUT,
2911 "No such column: %s", sigmaY);
2913 if (polynomial_fit != NULL)
2915 assure( !cpl_table_has_column(t, polynomial_fit) , CPL_ERROR_ILLEGAL_INPUT,
2916 "Table already has '%s' column", polynomial_fit);
2918 if (residual_square != NULL)
2920 assure( !cpl_table_has_column(t, residual_square), CPL_ERROR_ILLEGAL_INPUT,
2921 "Table already has '%s' column", residual_square);
2923 if (variance_fit != NULL)
2925 assure( !cpl_table_has_column(t, variance_fit) , CPL_ERROR_ILLEGAL_INPUT,
2926 "Table already has '%s' column", variance_fit);
2930 type = cpl_table_get_column_type(t, X1);
2931 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
2933 type = cpl_table_get_column_type(t, X2);
2934 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
2936 type = cpl_table_get_column_type(t, Y);
2937 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
2941 type = cpl_table_get_column_type(t, sigmaY);
2942 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
2943 "Input column '%s' has wrong type (%s)",
2948 check( cpl_table_cast_column(t, X1 ,
"_X1_double", CPL_TYPE_DOUBLE),
2949 "Could not cast table column to double");
2950 check( cpl_table_cast_column(t, X2 ,
"_X2_double", CPL_TYPE_DOUBLE),
2951 "Could not cast table column to double");
2952 check( cpl_table_cast_column(t, Y ,
"_Y_double", CPL_TYPE_DOUBLE),
2953 "Could not cast table column to double");
2956 check( cpl_table_cast_column(t, sigmaY,
"_sY_double", CPL_TYPE_DOUBLE),
2957 "Could not cast table column to double");
2962 check( cpl_table_new_column(t,
"_residual_square", CPL_TYPE_DOUBLE),
2963 "Could not create column");
2968 check(( N = cpl_table_get_nrow(t),
2969 x1 = cpl_table_get_data_double(t,
"_X1_double"),
2970 x2 = cpl_table_get_data_double(t,
"_X2_double"),
2971 y = cpl_table_get_data_double(t,
"_Y_double"),
2972 res= cpl_table_get_data_double(t,
"_residual_square")),
2973 "Could not read table data");
2977 check (sy = cpl_table_get_data_double(t,
"_sY_double"),
2978 "Could not read table data");
2985 assure( N > 0, CPL_ERROR_ILLEGAL_INPUT,
"Empty table");
2988 uves_unwrap_vector(&vx1);
2989 uves_unwrap_vector(&vx2);
2990 uves_unwrap_vector(&vy);
2992 vx1 = cpl_vector_wrap(N, x1);
2993 vx2 = cpl_vector_wrap(N, x2);
2994 vy = cpl_vector_wrap(N, y);
2997 uves_unwrap_vector(&vsy);
2998 vsy = cpl_vector_wrap(N, sy);
3006 uves_unwrap_bivector_vectors(&vx);
3007 vx = cpl_bivector_wrap_vectors(vx1, vx2);
3013 "Could not fit polynomial");
3021 cpl_table_fill_column_window_double(t,
"_residual_square", 0,
3022 cpl_table_get_nrow(t), 0.0);
3024 for (i = 0; i < N; i++)
3030 res[i] = (yfit-y[i])*(yfit-y[i]);
3039 sigma2 = cpl_table_get_column_median(t,
"_residual_square") / (0.6744 * 0.6744);
3043 check( rejected = uves_erase_table_rows(t,
"_residual_square",
3044 CPL_GREATER_THAN, kappa*kappa*sigma2),
3045 "Could not remove outlier points");
3049 uves_msg_debug(
"%d of %d points rejected in kappa-sigma clipping. rms=%f",
3050 rejected, N, sqrt(sigma2));
3053 total_rejected += rejected;
3054 N = cpl_table_get_nrow(t);
3060 }
while (rejected > 0 && rejected > min_reject*(N+rejected) &&
3061 N >= (degree1 + 1)*(degree2 + 1) + 1);
3065 uves_msg_debug(
"%d of %d points (%f %%) rejected in kappa-sigma clipping",
3068 (100.0*total_rejected)/(N + total_rejected)
3078 check(( N = cpl_table_get_nrow(t),
3079 x1 = cpl_table_get_data_double(t,
"_X1_double"),
3080 x2 = cpl_table_get_data_double(t,
"_X2_double"),
3081 y = cpl_table_get_data_double(t,
"_Y_double"),
3082 res= cpl_table_get_data_double(t,
"_residual_square")),
3083 "Could not read table data");
3087 check (sy = cpl_table_get_data_double(t,
"_sY_double"),
3088 "Could not read table data");
3095 assure( N > 0, CPL_ERROR_ILLEGAL_INPUT,
"Empty table");
3098 uves_unwrap_vector(&vx1);
3099 uves_unwrap_vector(&vx2);
3100 uves_unwrap_vector(&vy);
3102 vx1 = cpl_vector_wrap(N, x1);
3103 vx2 = cpl_vector_wrap(N, x2);
3104 vy = cpl_vector_wrap(N, y);
3107 uves_unwrap_vector(&vsy);
3108 vsy = cpl_vector_wrap(N, sy);
3116 uves_unwrap_bivector_vectors(&vx);
3117 vx = cpl_bivector_wrap_vectors(vx1, vx2);
3121 if (variance_fit != NULL || variance != NULL)
3125 mse, red_chisq, &variance_local),
3126 "Could not fit polynomial");
3131 mse, red_chisq, NULL),
3132 "Could not fit polynomial");
3135 cpl_table_erase_column(t,
"_residual_square");
3138 if (polynomial_fit != NULL || residual_square != NULL)
3143 check( cpl_table_new_column(t,
"_polynomial_fit", CPL_TYPE_DOUBLE),
3144 "Could not create column");
3146 cpl_table_fill_column_window_double(t,
"_polynomial_fit", 0,
3147 cpl_table_get_nrow(t), 0.0);
3149 x1 = cpl_table_get_data_double(t,
"_X1_double");
3150 x2 = cpl_table_get_data_double(t,
"_X2_double");
3151 pf = cpl_table_get_data_double(t,
"_polynomial_fit");
3153 for (i = 0; i < N; i++){
3155 double x1val, x2val, yfit;
3157 check(( x1val = cpl_table_get_double(t,
"_X1_double", i, NULL),
3158 x2val = cpl_table_get_double(t,
"_X2_double", i, NULL),
3161 cpl_table_set_double(t,
"_polynomial_fit", i, yfit)),
3162 "Could not evaluate polynomial");
3170 if (residual_square != NULL)
3172 check(( cpl_table_duplicate_column(t, residual_square,
3173 t,
"_polynomial_fit"),
3174 cpl_table_subtract_columns(t, residual_square, Y),
3175 cpl_table_multiply_columns(t, residual_square, residual_square)),
3177 "Could not calculate Residual of fit");
3181 if (polynomial_fit != NULL)
3183 cpl_table_name_column(t,
"_polynomial_fit", polynomial_fit);
3187 cpl_table_erase_column(t,
"_polynomial_fit");
3192 if (variance_fit != NULL)
3197 check( cpl_table_new_column(t, variance_fit, CPL_TYPE_DOUBLE),
3198 "Could not create column");
3200 cpl_table_fill_column_window_double(t, variance_fit, 0,
3201 cpl_table_get_nrow(t), 0.0);
3203 x1 = cpl_table_get_data_double(t,
"_X1_double");
3204 x2 = cpl_table_get_data_double(t,
"_X2_double");
3205 vf = cpl_table_get_data_double(t, variance_fit);
3207 for (i = 0; i < N; i++)
3210 double x1val, x2val, yfit_variance;
3211 check(( x1val = cpl_table_get_double(t,
"_X1_double", i, NULL),
3212 x2val = cpl_table_get_double(t,
"_X2_double", i, NULL),
3216 cpl_table_set_double(t, variance_fit, i, yfit_variance)),
3217 "Could not evaluate polynomial");
3226 check(( cpl_table_erase_column(t,
"_X1_double"),
3227 cpl_table_erase_column(t,
"_X2_double"),
3228 cpl_table_erase_column(t,
"_Y_double")),
3229 "Could not delete temporary columns");
3233 check( cpl_table_erase_column(t,
"_sY_double"),
3234 "Could not delete temporary column");
3238 uves_unwrap_bivector_vectors(&vx);
3239 uves_unwrap_vector(&vx1);
3240 uves_unwrap_vector(&vx2);
3241 uves_unwrap_vector(&vy);
3242 uves_unwrap_vector(&vsy);
3244 if (variance != NULL)
3246 *variance = variance_local;
3252 if (cpl_error_get_code() != CPL_ERROR_NONE)
3307 const char *X1,
const char *X2,
const char *Y,
3309 const char *polynomial_fit,
3310 const char *residual_square,
3311 const char *variance_fit,
3312 double *mean_squared_error,
double *red_chisq,
3314 int maxdeg1,
int maxdeg2,
double min_rms,
3317 const double *min_val,
3318 const double *max_val,
3319 int npos,
double positions[][2])
3325 double **mse = NULL;
3326 bool adjust1 =
true;
3327 bool adjust2 =
true;
3328 bool finished =
false;
3331 cpl_table *temp = NULL;
3334 assure( (min_val == NULL && max_val == NULL) || positions != NULL,
3335 CPL_ERROR_NULL_INPUT,
3336 "Missing positions array");
3338 check_nomsg( y_unit = cpl_table_get_column_unit(t, Y));
3344 assure(maxdeg1 >= 1 && maxdeg2 >= 1, CPL_ERROR_ILLEGAL_INPUT,
3345 "Illegal max. degrees: (%d, %d)",
3348 mse = cpl_calloc(maxdeg1+1,
sizeof(
double *));
3350 for (i = 0; i < maxdeg1+1; i++)
3353 mse[i] = cpl_calloc(maxdeg2+1,
sizeof(
double));
3356 for (j = 0; j < maxdeg2+1; j++)
3362 temp = cpl_table_duplicate(t);
3371 &mse[deg1][deg2], NULL,
3374 "Error fitting polynomial");
3376 uves_msg_low(
"(%d, %d)-degree: RMS = %.3g %s (%" CPL_SIZE_FORMAT
"/%" CPL_SIZE_FORMAT
" outliers)",
3377 deg1, deg2, sqrt(mse[deg1][deg2]), y_unit,
3378 cpl_table_get_nrow(t) - cpl_table_get_nrow(temp),
3379 cpl_table_get_nrow(t));
3381 uves_msg_debug(
"(%d, %d)-degree: RMS = %.3g %s (%" CPL_SIZE_FORMAT
"/%" CPL_SIZE_FORMAT
" outliers)",
3382 deg1, deg2, sqrt(mse[deg1][deg2]), y_unit,
3383 cpl_table_get_nrow(t) - cpl_table_get_nrow(temp),
3384 cpl_table_get_nrow(t));
3389 int new_deg1, new_deg2;
3394 adjust1 = adjust1 && (deg1 + 2 <= maxdeg1);
3395 adjust2 = adjust2 && (deg2 + 2 <= maxdeg2);
3411 for (new_deg1 = deg1; new_deg1 <= deg1+2; new_deg1++)
3412 for (new_deg2 = deg2; new_deg2 <= deg2+2; new_deg2++)
3414 (new_deg1 == deg1+1 && new_deg2 == deg2 && adjust1) ||
3415 (new_deg1 == deg1+2 && new_deg2 == deg2 && adjust1) ||
3416 (new_deg1 == deg1 && new_deg2 == deg2+1 && adjust2) ||
3417 (new_deg1 == deg1 && new_deg2 == deg2+2 && adjust2) ||
3418 (new_deg1 == deg1+1 && new_deg2 == deg2+1 && adjust1 && adjust2)
3420 && mse[new_deg1][new_deg2] < 0)
3424 uves_free_table(&temp);
3425 temp = cpl_table_duplicate(t);
3440 if (cpl_error_get_code() == CPL_ERROR_SINGULAR_MATRIX)
3446 new_deg1, new_deg2);
3449 new_deg1, new_deg2);
3451 mse[new_deg1][new_deg2] = DBL_MAX/2;
3455 assure( cpl_error_get_code() == CPL_ERROR_NONE,
3456 cpl_error_get_code(),
3457 "Error fitting (%d, %d)-degree polynomial",
3458 new_deg1, new_deg2 );
3460 rejected = cpl_table_get_nrow(t) - cpl_table_get_nrow(temp);
3463 uves_msg_low(
"(%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));
3467 uves_msg_debug(
"(%d,%d)-degree: RMS = %.3g %s (%d/%" CPL_SIZE_FORMAT
" outliers)",
3468 new_deg1, new_deg2, sqrt(mse[new_deg1][new_deg2]), y_unit,
3469 rejected, cpl_table_get_nrow(t));
3472 if (min_val != NULL || max_val != NULL)
3474 for (i = 0; i < npos; i++)
3478 positions[i][0], positions[i][1]);
3479 if (min_val != NULL && val < *min_val)
3484 mse[new_deg1][new_deg2] = DBL_MAX/2;
3487 if (max_val != NULL && val > *max_val)
3492 mse[new_deg1][new_deg2] = DBL_MAX/2;
3500 if (rejected >= (4*cpl_table_get_nrow(t))/5)
3502 mse[new_deg1][new_deg2] = DBL_MAX/2;
3516 m = mse[deg1][deg2];
3519 && (m - mse[deg1+1][deg2])/m > 0.1
3520 && (!adjust2 || mse[deg1+1][deg2] <= mse[deg1][deg2+1])
3532 (m - mse[deg1][deg2+1])/m > 0.1
3533 && (!adjust1 || mse[deg1+1][deg2] > mse[deg1][deg2+1])
3539 else if (adjust1 && adjust2 && (m - mse[deg1+1][deg2+1])/m > 0.1)
3546 && (m - mse[deg1+2][deg2])/m > 0.1
3547 && (!adjust2 || mse[deg1+2][deg2] <= mse[deg1][deg2+2])
3554 && (m - mse[deg1][deg2+2])/m > 0.1
3555 && (!adjust1 || mse[deg1+2][deg2] < mse[deg1][deg2+2]))
3562 finished = finished || (sqrt(mse[deg1][deg2]) < min_rms);
3564 }
while (!finished);
3571 polynomial_fit, residual_square,
3573 mean_squared_error, red_chisq,
3574 variance, kappa, min_reject),
3575 "Error fitting (%d, %d)-degree polynomial", deg1, deg2);
3578 uves_msg_low(
"Using degree (%d, %d), RMS = %.3g %s", deg1, deg2,
3579 sqrt(mse[deg1][deg2]), y_unit);
3581 uves_msg_debug(
"Using degree (%d, %d), RMS = %.3g %s", deg1, deg2,
3582 sqrt(mse[deg1][deg2]), y_unit);
3587 for (i = 0; i < maxdeg1+1; i++)
3596 uves_free_table(&temp);
3598 return bivariate_fit;
3615 const char *result = NULL;
3616 unsigned int prefix_length;
3618 assure( s != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
3619 assure( prefix != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
3621 prefix_length = strlen(prefix);
3623 assure( strlen(s) >= prefix_length &&
3624 strncmp(s, prefix, prefix_length) == 0,
3625 CPL_ERROR_INCOMPATIBLE_INPUT,
"'%s' is not a prefix of '%s'",
3628 result = s + prefix_length;
3648 static double V1, V2, S;
3649 static int phase = 0;
3654 double U1 = (double)rand() / RAND_MAX;
3655 double U2 = (double)rand() / RAND_MAX;
3659 S = V1 * V1 + V2 * V2;
3660 }
while(S >= 1 || S == 0);
3662 X = V1 * sqrt(-2 * log(S) / S);
3664 X = V2 * sqrt(-2 * log(S) / S);
3685 const char *column_y,
int *istart )
3690 const double *x, *y;
3692 check( x = cpl_table_get_data_double_const(t, column_x),
3693 "Error reading column '%s'", column_x);
3694 check( y = cpl_table_get_data_double_const(t, column_y),
3695 "Error reading column '%s'", column_y);
3697 n = cpl_table_get_nrow(t);
3724 double yp1, yp2, yp = 0;
3725 double xpi, xpi1, l1, l2, lp1, lp2;
3728 if ( x[0] <= x[n-1] && (xp < x[0] || xp > x[n-1]) )
return 0.0;
3729 if ( x[0] > x[n-1] && (xp > x[0] || xp < x[n-1]) )
return 0.0;
3731 if ( x[0] <= x[n-1] )
3733 for ( i = (*istart)+1; i <= n && xp >= x[i-1]; i++ )
3738 for ( i = (*istart)+1; i <= n && xp <= x[i-1]; i++ )
3745 lp1 = 1.0 / (x[i-1] - x[i]);
3750 yp1 = (y[1] - y[0]) / (x[1] - x[0]);
3754 yp1 = (y[i] - y[i-2]) / (x[i] - x[i-2]);
3759 yp2 = (y[n-1] - y[n-2]) / (x[n-1] - x[n-2]);
3763 yp2 = (y[i+1] - y[i-1]) / (x[i+1] - x[i-1]);
3771 yp = y[i-1]*(1 - 2.0*lp1*xpi)*l1*l1 +
3772 y[i]*(1 - 2.0*lp2*xpi1)*l2*l2 +
3773 yp1*xpi*l1*l1 + yp2*xpi1*l2*l2;
3797 double a, b, h, yp = 0;
3807 if ( xp < x[1] || xp > x[n] )
3811 else if ( xp == x[1] )
3816 for ( k = klo; k < n && xp > x[k]; k++ )
3819 klo = *kstart = k-1;
3822 h = x[khi] - x[klo];
3823 assure( h != 0.0, CPL_ERROR_DIVISION_BY_ZERO,
3824 "Empty x-value range: xlo = %e ; xhi = %e", x[khi], x[klo]);
3826 a = (x[khi] - xp) / h;
3827 b = (xp - x[klo]) / h;
3829 yp = a*y[klo] + b*y[khi] + ((a*a*a - a)*y2[klo] + (b*b*b - b)*y2[khi])*
3850 bool is_sorted =
true;
3853 double previous, current;
3856 passure(cpl_table_has_column(t, column),
"No column '%s'", column);
3857 passure(cpl_table_get_column_type(t, column) == CPL_TYPE_DOUBLE,
" ");
3859 N = cpl_table_get_nrow(t);
3863 previous = cpl_table_get_double(t, column, 0, NULL);
3865 for(i = 1; i < N && is_sorted; i++)
3867 current = cpl_table_get_double(t, column, i, NULL);
3871 is_sorted = is_sorted && ( current >= previous );
3876 is_sorted = is_sorted && ( current <= previous );
3901 cpl_table *result = NULL;
3904 result = cpl_table_new(0),
3905 cpl_table_new_column(result,
"TraceID" , CPL_TYPE_INT),
3906 cpl_table_new_column(result,
"Offset" , CPL_TYPE_DOUBLE),
3907 cpl_table_new_column(result,
"Tracemask", CPL_TYPE_INT)),
3908 "Error creating table");
3927 int fibre_ID,
double fibre_offset,
int fibre_mask)
3931 assure( traces != NULL, CPL_ERROR_NULL_INPUT,
"Null table!");
3935 size = cpl_table_get_nrow(traces),
3936 cpl_table_set_size (traces, size+1),
3937 cpl_table_set_int (traces,
"TraceID" , size, fibre_ID),
3938 cpl_table_set_double(traces,
"Offset" , size, fibre_offset),
3939 cpl_table_set_int (traces,
"Tracemask", size, fibre_mask)),
3940 "Error updating table");
3943 return cpl_error_get_code();
3957 cpl_table* tab=NULL;
3959 tab=cpl_table_load(tname,1,0);
3962 check_nomsg(uves_table_save(tab,head,NULL,tname,CPL_IO_DEFAULT));
3965 uves_free_table(&tab);
3966 uves_free_propertylist(&head);
3967 return cpl_error_get_code();
3983 cpl_table* tab1=NULL;
3984 cpl_table* tab2=NULL;
3987 tab1=cpl_table_load(tname1,1,0);
3989 tab2=cpl_table_load(tname2,1,0);
3993 check_nomsg(uves_table_save(tab2,head2,NULL,tname2,CPL_IO_DEFAULT));
3996 uves_free_table(&tab1);
3997 uves_free_table(&tab2);
3998 uves_free_propertylist(&head2);
3999 return cpl_error_get_code();
4016 const char* colname=NULL;
4018 cpl_array *names=NULL;
4020 assure( *table != NULL, CPL_ERROR_NULL_INPUT,
"Null input table!");
4021 ncols = cpl_table_get_ncol(*table);
4022 names = cpl_table_get_column_names(*table);
4023 for(i=0;i<ncols;i++) {
4024 colname=cpl_array_get_string(names, i);
4025 cpl_table_set_column_unit(*table,colname,NULL);
4029 uves_free_array(&names);
4031 return cpl_error_get_code();
4049 const char* colname=NULL;
4050 const char* unit1=NULL;
4053 cpl_array *names=NULL;
4055 assure( table1 != NULL, CPL_ERROR_NULL_INPUT,
"Null input table!");
4056 assure( *table2 != NULL, CPL_ERROR_NULL_INPUT,
"Null input table!");
4057 ncols1 = cpl_table_get_ncol(*table1);
4058 ncols2 = cpl_table_get_ncol(*table2);
4059 assure( ncols1 == ncols2, CPL_ERROR_NULL_INPUT,
4060 "n columns (tab1) != n columns (tab2)");
4062 names = cpl_table_get_column_names(*table1);
4063 for(i=0;i<ncols1;i++) {
4064 colname=cpl_array_get_string(names, i);
4065 unit1=cpl_table_get_column_unit(*table1,colname);
4066 cpl_table_set_column_unit(*table2,colname,unit1);
4070 uves_free_array(&names);
4072 return cpl_error_get_code();
4106 static void fmoffa_i(
float x,
const double a[],
double *y,
double dyda[])
4111 double fac=0, fac2=0, fac4= 0, fac4i=0, arg=0, arg2=0;
4112 double a2i=0, m = 0, p = 0, dif =0;
4113 double sqrt5 = 2.23606797749979;
4117 a2i = 1.0/(a[2]*sqrt5);
4129 m = a[3]*fac4i * a2i*16/(5.0*M_PI);
4130 *y = m + a[4]*(1.0+dif*a[5]);
4131 p = 8.0*m/fac*arg*a2i;
4134 dyda[2] = p*dif/a[2] - m/a[2];
4137 dyda[1]=p-a[4]*a[5];
4139 dyda[4]=1.0+dif*a[5];
4146 printf(
"fmoffat_i \n");
4147 for (i = 1;i<=npar;i++) printf(
"a[%1i] %f :\n",i,a[i]);
4149 printf(
"fmoffat_i ");
4150 for (i = 1;i<=npar;i++) printf(
"%i %f :",i,dyda[i]);
4175 static void fmoffa_c(
float x,
const double a[],
double *y,
double dyda[])
4183 double const xgl[3] = {-0.387298334621,0.,0.387298334621};
4184 double const wgl[3] = {.2777777777778,0.444444444444,0.2777777777778};
4194 a = C_TO_FORTRAN_INDEXING(a);
4195 dyda = C_TO_FORTRAN_INDEXING(dyda);
4198 for (i = 1;i<=npar;i++) dyda[i] = 0.;
4203 for (j=0; j < npoint; j++)
4207 fmoffa_i(xmod,a,&ypar,&dydapar[-1]);
4209 *y = *y + ypar*wgl[j];
4211 for (i = 1; i <= npar; i++)
4213 dyda[i] = dyda[i] + dydapar[i-1]*wgl[j] ;
4225 printf(
"fmoffat_c ");
4226 for (i = 1;i<=npar;i++) printf(
"%i %f %f: \n",i,a[i],dyda[i]);
4295 double sigma = a[1];
4316 A/(sqrt(2*M_PI*sigma*sigma)) *
4317 exp(- (x[0] - my)*(x[0] - my)
4350 double sigma = a[1];
4377 factor = exp( -(x[0] - my)*(x[0] - my)/(2*sigma*sigma) )
4378 / (sqrt(2*M_PI*sigma*sigma));
4380 result[0] = A * factor * (x[0]-my) / (sigma*sigma);
4381 result[1] = A * factor * ((x[0]-my)*(x[0]-my) / (sigma*sigma) - 1) / sigma;
4415 double sigma = a[1];
4436 *result = B + C*(x[0] - my) +
4437 A/(sqrt(2*M_PI*sigma*sigma)) *
4438 exp(- (x[0] - my)*(x[0] - my)
4474 double sigma = a[1];
4505 factor = exp( -(x[0] - my)*(x[0] - my)/(2*sigma*sigma) )
4506 / (sqrt(2*M_PI*sigma*sigma));
4508 result[0] = A * factor * (x[0]-my) / (sigma*sigma);
4509 result[1] = A * factor * ((x[0]-my)*(x[0]-my) / (sigma*sigma) - 1) / sigma;
4512 result[4] = x[0] - my;
4536 const cpl_image *spectrum,
const cpl_image *sky,
4537 const cpl_image *cosmic_image,
4538 const uves_extract_profile *profile,
4541 cpl_image *image = NULL;
4543 cpl_binary *bpm = NULL;
4544 bool loop_y =
false;
4548 bool new_format =
true;
4550 image = cpl_image_new(pos->nx, pos->ny, CPL_TYPE_DOUBLE);
4552 if (image_noise != NULL) {
4553 *image_noise = cpl_image_new(pos->nx, pos->ny, CPL_TYPE_DOUBLE);
4555 cpl_image_add_scalar(*image_noise, 0.01);
4558 if (image_header != NULL) {
4561 uves_propertylist_append_double(*image_header, UVES_MJDOBS, 60000);
4562 uves_propertylist_append_double(*image_header, UVES_RON(new_format, chip), ron);
4563 uves_propertylist_append_double(*image_header, UVES_GAIN(new_format, chip), gain);
4568 pos->minorder, pos->maxorder,
4575 uves_extract_profile_set(profile, pos, NULL);
4576 for (pos->y = pos->ylow; pos->y <= pos->yhigh; pos->y++) {
4579 double flux, sky_flux;
4581 int spectrum_row = pos->order - pos->minorder + 1;
4583 double prof = uves_extract_profile_evaluate(profile, pos);
4587 sky_flux = cpl_image_get(sky, pos->x, spectrum_row, &bad)/pos->sg.length;
4594 flux = cpl_image_get(spectrum, pos->x, spectrum_row, &bad) * prof + sky_flux;
4597 noise = sqrt(gain)*sqrt(ron*ron/(gain*gain) + sky_flux/gain + flux/gain);
4599 cpl_image_set(image, pos->x, pos->y,
4601 if (image_noise != NULL) cpl_image_set(*image_noise, pos->x, pos->y, noise);
4606 if (cosmic_image != NULL) {
4607 double cr_val = 2*cpl_image_get_max(image);
4614 pos->minorder, pos->maxorder,
4621 if (cpl_image_get(cosmic_image, pos->x, pos->y, &is_rejected) > 0) {
4622 cpl_image_set(image, pos->x, pos->y, cr_val);
4632 uves_frameset_dump(cpl_frameset* set)
4635 cpl_frame* frm=NULL;
4639 cknull(set,
"Null input frameset");
4642 frm=cpl_frameset_get_frame(set,i);
4643 uves_msg(
"frame %d tag %s filename %s group %d",
4645 cpl_frame_get_tag(frm),
4646 cpl_frame_get_filename(frm),
4647 cpl_frame_get_group(frm));
4676 uves_image_smooth_x(cpl_image * inp,
const int r)
4695 cpl_image* out=NULL;
4697 cknull(inp,
"Null in put image, exit");
4704 for(i=r;i<sx-r;i++) {
4706 pout[j*sx+i]+=pinp[j*sx+i+k];
4714 if(cpl_error_get_code() != CPL_ERROR_NONE) {
4744 uves_image_smooth_y(cpl_image * inp,
const int r)
4763 cpl_image* out=NULL;
4765 cknull(inp,
"Null in put image, exit");
4771 for(j=r;j<sy-r;j++) {
4774 pout[j*sx+i]+=pinp[(j+k)*sx+i];
4782 if(cpl_error_get_code() != CPL_ERROR_NONE) {
4809 uves_image_smooth_mean_x(cpl_image * inp,
const int r)
4828 cpl_image* out=NULL;
4830 cknull(inp,
"Null in put image, exit");
4837 for(i=r;i<sx-r;i++) {
4839 pout[j*sx+i]+=pinp[j*sx+i+k];
4847 if(cpl_error_get_code() != CPL_ERROR_NONE) {
4874 uves_image_smooth_median_x(cpl_image * inp,
const int r)
4891 cpl_image* out=NULL;
4894 cknull(inp,
"Null in put image, exit");
4901 for(i=1+r;i<sx-r;i++) {
4902 pout[j*sx+i]=(float)cpl_image_get_median_window(inp,i,j,i+r,j);
4908 if(cpl_error_get_code() != CPL_ERROR_NONE) {
4933 uves_image_smooth_fft(cpl_image * inp,
const int fx)
4939 cpl_image* out=NULL;
4940 cpl_image* im_re=NULL;
4941 cpl_image* im_im=NULL;
4942 cpl_image* ifft_re=NULL;
4943 cpl_image* ifft_im=NULL;
4944 cpl_image* filter=NULL;
4949 cknull(inp,
"Null in put image, exit");
4950 check_nomsg(im_re = cpl_image_cast(inp, CPL_TYPE_DOUBLE));
4951 check_nomsg(im_im = cpl_image_cast(inp, CPL_TYPE_DOUBLE));
4954 check_nomsg(cpl_image_fft(im_re,im_im,CPL_FFT_DEFAULT));
4964 cpl_image_multiply(im_re,filter);
4965 cpl_image_multiply(im_im,filter);
4967 uves_free_image(&filter);
4969 check_nomsg(ifft_re = cpl_image_duplicate(im_re));
4970 check_nomsg(ifft_im = cpl_image_duplicate(im_im));
4972 uves_free_image(&im_re);
4973 uves_free_image(&im_im);
4976 check_nomsg(cpl_image_fft(ifft_re,ifft_im,CPL_FFT_INVERSE));
4977 check_nomsg(out = cpl_image_cast(ifft_re, CPL_TYPE_FLOAT));
4981 uves_free_image(&ifft_re);
4982 uves_free_image(&ifft_im);
4983 uves_free_image(&filter);
4984 uves_free_image(&im_re);
4985 uves_free_image(&im_im);
4987 if(cpl_error_get_code() != CPL_ERROR_NONE) {
5009 cpl_image* img=NULL;
5012 cpl_vector* values=NULL;
5019 pval=cpl_vector_get_data(values);
5020 for(i=0;i<size;i++) {
5021 img=cpl_imagelist_get(iml,i);
5022 irplib_ksigma_clip(img,1,1,
5023 cpl_image_get_size_x(img),
5024 cpl_image_get_size_y(img),
5025 5,kappa,1.e-5,&mean,&stdev);
5026 uves_msg(
"Ima %d mean level: %g",i+1,mean);
5050 cpl_image* img=NULL;
5056 pval=cpl_vector_get_data(values);
5057 for(i=0;i<size;i++) {
5058 img=cpl_imagelist_get(*iml,i);
5059 cpl_image_subtract_scalar(img,pval[i]);
5060 cpl_imagelist_set(*iml,img,i);
5065 return cpl_error_get_code();
5089 const double sigma_x,
5090 const double sigma_y)
5099 double gaussval= 0.0;
5100 double inv_sigma_x=1./sigma_x;
5101 double inv_sigma_y=1./sigma_y;
5105 cpl_image *lowpass_image=NULL;
5108 lowpass_image = cpl_image_new (xs, ys, CPL_TYPE_FLOAT);
5109 if (lowpass_image == NULL) {
5111 cpl_error_get_message());
5118 data = cpl_image_get_data_float(lowpass_image);
5133 for (i=1 ; i<=hlx ; i++) {
5134 x = i * inv_sigma_x;
5135 gaussval = exp(-0.5*x*x);
5137 data[xs-i] = gaussval;
5140 for (j=1; j<=hly ; j++) {
5141 y = j * inv_sigma_y;
5143 data[j*xs] = exp(-0.5*y*y);
5144 data[(ys-j)*xs] = exp(-0.5*y*y);
5146 for (i=1 ; i<=hlx ; i++) {
5148 x = i * inv_sigma_x;
5149 gaussval = exp (-0.5*(x*x+y*y));
5150 data[j*xs+i] = gaussval;
5151 data[(j+1)*xs-i] = gaussval;
5152 data[(ys-j)*xs+i] = gaussval;
5153 data[(ys+1-j)*xs-i] = gaussval;
5165 return lowpass_image;
5181 cpl_image* result=NULL;
5182 cpl_image* diff=NULL;
5183 cpl_image* flat_smooth=NULL;
5184 cpl_array* val=NULL;
5185 cpl_matrix* mx=NULL;
5196 int filter_width_x=7;
5197 int filter_width_y=7;
5206 const double* pima=NULL;
5212 passure( flat !=NULL ,
"NULL input flat ");
5213 passure( head !=NULL ,
"NULL input head ");
5216 sx=cpl_image_get_size_x(flat);
5217 sy=cpl_image_get_size_y(flat);
5224 if (binx>1) filter_width_x=5;
5225 if (biny>1) filter_width_y=5;
5229 check_nomsg(mx=cpl_matrix_new(filter_width_x,filter_width_y));
5231 for(j=0; j< filter_width_y; j++){
5232 for(i=0; i< filter_width_x; i++){
5233 cpl_matrix_set( mx, i,j,1.0);
5239 check_nomsg(flat_smooth=uves_image_filter_median(flat,mx));
5244 check_nomsg(cpl_image_subtract(diff,flat_smooth));
5253 val=cpl_array_new(npixs,CPL_TYPE_DOUBLE);
5254 check_nomsg(cpl_array_fill_window_double(val,0,npixs,0));
5256 check_nomsg(pima=cpl_image_get_data_double_const(flat));
5257 check_nomsg(pdif=cpl_image_get_data_double(diff));
5259 for(i=0;i<npixs;i++) {
5260 if(pima[i]>med_flat) {
5274 for(i=0;i<niter;i++) {
5275 for(k=0;k<size;k++) {
5276 if(fabs(pval[k]-mean)>stdev_x_4) {
5277 cpl_array_set_invalid(val,k);
5280 mean=cpl_array_get_mean(val);
5281 stdev=cpl_array_get_stdev(val);
5286 result=cpl_image_new(sx,sy,CPL_TYPE_DOUBLE);
5287 pres=cpl_image_get_data_double(result);
5288 for(i=0;i<npixs;i++) {
5289 if(fabs(pdif[i])<stdev_x_4) {
5301 uves_free_array(&val);
5302 uves_free_image(&diff);
5303 uves_free_image(&flat_smooth);
5304 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.
double uves_pfits_get_uit(const uves_propertylist *plist)
Find out the user integration time.
#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.