59#include <gsl/gsl_rng.h>
60#include <gsl/gsl_randist.h>
61#include <gsl/gsl_vector.h>
62#include <gsl/gsl_blas.h>
63#include <gsl/gsl_multifit_nlin.h>
64#include <gsl/gsl_version.h>
75#define MAXIMUM(x, y) ((x) > (y)) ? (x) : (y)
78#define DEV_BLOCKSIZE 4096
79#define XSH_ATM_EXT_UVB_WAV_MIN 310.
100#define CPL_TYPE_T CPL_TYPE_FLOAT
101#define CPL_IMAGE_GET_DATA cpl_image_get_data_float
102#define CPL_IMAGE_GET_DATA_CONST cpl_image_get_data_float_const
103#define CPL_IMAGE_GET_MEDIAN cpl_tools_get_median_float
114 srand((
unsigned) time(&t));
130 long double a=(double)v1,b=(
double)v2;
135 x1=( (
long double) rand() / RAND_MAX ) * (b-a) + a;
152 long double a=v1,b=v2;
155 srand((
unsigned)time(NULL));
158 x1=((
long double)rand()/RAND_MAX)*(b-a) + a;
194 const cpl_imagelist * imlist,
200 const cpl_image * cur_ima=NULL ;
202 cpl_table * time_line=NULL ;
203 cpl_image * out_ima=NULL ;
207 double low_thresh, high_thresh ;
211 cpl_ensure(imlist != NULL, CPL_ERROR_NULL_INPUT, NULL);
212 cpl_ensure(cpl_imagelist_is_uniform(imlist)==0, CPL_ERROR_ILLEGAL_INPUT,
214 cpl_ensure(sigma_low>1., CPL_ERROR_ILLEGAL_INPUT, NULL);
215 cpl_ensure(sigma_upp>1., CPL_ERROR_ILLEGAL_INPUT, NULL);
216 cpl_ensure(
niter > 0, CPL_ERROR_NULL_INPUT, NULL);
218 ni = cpl_imagelist_get_size(imlist) ;
220 cur_ima = cpl_imagelist_get_const(imlist, 0) ;
221 nx = cpl_image_get_size_x(cur_ima) ;
222 ny = cpl_image_get_size_y(cur_ima) ;
226 time_line = cpl_table_new(ni) ;
241 cpl_table_new_column(time_line,
"VAL",
CPL_TYPE_T);
242 cpl_table_fill_column_window(time_line,
"VAL",0,ni,0);
244 ptime_line = (
CPL_TYPE*) cpl_table_get_data_double(time_line,
"VAL") ;
246 ptime_line = (
CPL_TYPE*) cpl_table_get_data_float(time_line,
"VAL") ;
248 ptime_line = (
CPL_TYPE*) cpl_table_get_data_int(time_line,
"VAL") ;
253 for (j=0 ; j<
ny ; j++) {
254 for (i=0 ; i<
nx ; i++) {
257 for (k=0 ; k<ni ; k++) {
258 cur_ima = cpl_imagelist_get_const(imlist, k) ;
265 for(
n=0, nbad=0;(
n<
niter) && (nbad<ni-1);
n++) {
267 check(mean = cpl_table_get_column_mean(time_line,
"VAL")) ;
268 check(stdev = cpl_table_get_column_stdev(time_line,
"VAL")) ;
269 low_thresh = mean - sigma_low * stdev ;
270 high_thresh = mean + sigma_upp * stdev ;
275 for (k=0 ; k<ni ; k++) {
276 if (ptime_line[k]<=high_thresh &&
277 ptime_line[k]>=low_thresh) {
280 cpl_table_set_invalid(time_line,
"VAL",k);
286 out_val = cpl_table_get_column_mean(time_line,
"VAL") ;
295 cpl_table_delete(time_line) ;
326 hrs=(int)(rest/10000.);
327 rest=rest-(double)(hrs*10000.);
328 min=(int)(rest/100.);
329 sec=rest-(double)(min*100.);
330 deg=hrs*15+(double)(min/4.)+(double)(sec/240.);
358 grad=(int)(rest/10000.);
359 rest=rest-(double)(grad*10000.);
360 min=(int)(rest/100.);
361 sec=rest-(double)(min*100.);
362 deg=grad+(double)(min/60.)+(double)(sec/3600.);
378 cpl_propertylist* plist=NULL;
379 const char* name=NULL;
384 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
385 return cpl_error_get_code();
387 name=cpl_frame_get_filename(in);
388 plist=cpl_propertylist_load(name,0);
395 xsh_msg_error(
"This recipe expects unbinned input raw frames. Exit");
396 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
398 return cpl_error_get_code();
444if ((stat(srcpath,&sb) == 0) && (stat(dstpath,&db) == 0))
446 if (sb.st_ino == db.st_ino)
450if ((src = open (srcpath, O_RDONLY)) == -1)
return (-1);
452if ((fstat (src, &sb) == -1) || (!S_ISREG (sb.st_mode)))
458if ((dst = open (dstpath, O_CREAT | O_WRONLY | O_TRUNC, sb.st_mode)) == -1)
464if ((fstat (dst, &db) == -1) || (!S_ISREG (db.st_mode)))
468 (void) unlink (dstpath);
472#ifdef HAVE_ST_BLKSIZE
473blksize = db.st_blksize;
480if ((buf = (
char *) cpl_malloc ((
size_t)blksize)) == NULL)
484 (void) unlink (dstpath);
488while ((rbytes = (
int) read (src, buf, (
size_t)blksize)) > 0)
490 if ((wbytes = (
int) write (dst, buf, (
size_t)rbytes)) != rbytes)
502if ((rbytes == -1) || (wbytes == -1))
504 (void) unlink (dstpath);
557if (stat (srcpath, &sb) == -1 || !(sb.st_mode & S_IWUSR))
559 (void) unlink (dstpath);
563 (void) unlink (srcpath);
578 const char* sky_prefix=NULL;
579 if(strstr(rec_prefix,
"SCI") != NULL) {
580 sky_prefix=
"SKY_SLIT";
581 }
else if(strstr(rec_prefix,
"TELL") != NULL) {
582 sky_prefix=
"SKY_SLIT";
583 }
else if(strstr(rec_prefix,
"FLUX") != NULL) {
584 sky_prefix=
"SKY_SLIT";
586 sky_prefix=
"CAL_SLIT_SKY";
605 cpl_propertylist* plist=NULL;
606 const char* obj=NULL;
609 const char* dpr_catg=NULL;
611 const char* dpr_type=NULL;
612 const char* filename=NULL;
614 check(frm=cpl_frameset_get_frame(raw,0));
615 filename=cpl_frame_get_filename(frm);
616 plist=cpl_propertylist_load(filename,0);
621 if(strstr(dpr_catg,
"SCIENCE")!=NULL) {
622 if(strstr(dpr_type,
"SKY")!=NULL) {
627 }
else if(strstr(dpr_catg,
"CALIB")!=NULL) {
628 if(strstr(dpr_type,
"FLUX")!=NULL) {
630 }
else if(strstr(dpr_type,
"TELLURIC")!=NULL) {
639 if(strstr(recipe,
"respon_slit_stare")!=NULL) {
641 }
else if(strstr(recipe,
"respon_slit_offset")!=NULL) {
643 }
else if(strstr(recipe,
"respon_slit_nod")!=NULL) {
645 }
else if(strstr(recipe,
"scired_slit_stare")!=NULL) {
647 }
else if(strstr(recipe,
"scired_slit_offset")!=NULL) {
649 }
else if(strstr(recipe,
"scired_slit_nod")!=NULL) {
651 }
else if(strstr(recipe,
"scired_ifu_stare")!=NULL) {
653 }
else if(strstr(recipe,
"scired_ifu_offset")!=NULL) {
655 }
else if(strstr(recipe,
"geom_ifu")!=NULL) {
692 return cpl_error_get_code();
706 return cpl_error_get_code();
733 return cpl_error_get_code();
758 return cpl_error_get_code();
773 cpl_parameter* p=NULL;
774 cpl_parameterlist* pout=NULL;
776 pout=cpl_parameterlist_new();
777 p=cpl_parameterlist_get_first((cpl_parameterlist*)pin);
780 cpl_parameterlist_append(pout,p);
781 p=cpl_parameterlist_get_next((cpl_parameterlist*)pin);
800 const char *name = cpl_property_get_name(property);
801 const char *comment = cpl_property_get_comment(property);
805 long size = cpl_property_get_size(property);
807 cpl_type type = cpl_property_get_type(property);
810 fprintf(stderr,
"Property at address %p\n", property);
811 fprintf(stderr,
"\tname : %p '%s'\n", name, name);
812 fprintf(stderr,
"\tcomment: %p '%s'\n", comment, comment);
813 fprintf(stderr,
"\ttype : %#09x\n", type);
814 fprintf(stderr,
"\tsize : %ld\n",
size);
815 fprintf(stderr,
"\tvalue : ");
820 c = cpl_property_get_char(property);
822 fprintf(stderr,
"''");
824 fprintf(stderr,
"'%c'", c);
828 fprintf(stderr,
"%d", cpl_property_get_bool(property));
832 fprintf(stderr,
"%d", cpl_property_get_int(property));
836 fprintf(stderr,
"%ld", cpl_property_get_long(property));
840 fprintf(stderr,
"%.7g", cpl_property_get_float(property));
843 case CPL_TYPE_DOUBLE:
844 fprintf(stderr,
"%.15g", cpl_property_get_double(property));
847 case CPL_TYPE_STRING:
848 fprintf(stderr,
"'%s'", cpl_property_get_string(property));
852 fprintf(stderr,
"unknown.");
857 fprintf(stderr,
"\n");
877 cpl_frame* result=NULL;
878 cpl_frame* frame=NULL;
879 cpl_image* image=NULL;
880 cpl_imagelist* iml=NULL;
881 cpl_propertylist* plist=NULL;
883 const char* name=NULL;
888 iml=cpl_imagelist_new();
889 for(i=0;i<
size;i++) {
890 frame=cpl_frameset_get_frame(set,i);
891 name=cpl_frame_get_filename(frame);
892 image=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
893 cpl_imagelist_set(iml,cpl_image_duplicate(image),i);
896 image=cpl_imagelist_collapse_create(iml);
897 frame=cpl_frameset_get_frame(set,0);
898 name=cpl_frame_get_filename(frame);
899 plist=cpl_propertylist_load(name,0);
901 sprintf(name_o,
"%s.fits",tag);
902 cpl_image_save(image,name_o,CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT);
904 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
923 int iset, i, j, set_size = 0;
924 cpl_frame *result = NULL;
927 float *res_flux = NULL;
928 float *res_errs = NULL;
929 int *res_qual = NULL;
934 check( set_size = cpl_frameset_get_size( set));
939 for( iset=0; iset< set_size; iset++){
941 cpl_frame *frame = NULL;
943 check( frame = cpl_frameset_get_frame( set, iset));
946 pre_list[iset] = pre;
956 for( j=0; j<
ny; j++){
957 for( i=0; i<
nx; i++){
959 float good_flux=0, bad_flux=0;
960 float good_errs=0, bad_errs=0;
966 for(iset = 0; iset < set_size; iset++){
967 float *img_flux = NULL;
968 float *img_errs = NULL;
969 int *img_qual = NULL;
975 if ( (img_qual[idx] & decode_bp) == 0 ){
978 good_qual|=img_qual[idx];
979 good_flux += img_flux[idx];
980 good_errs += img_errs[idx]*img_errs[idx];
983 bad_qual |= img_qual[idx];
984 bad_flux += img_flux[idx];
985 bad_errs += img_errs[idx]*img_errs[idx];
990 res_flux[idx] = bad_flux;
991 res_errs[idx] = sqrt( bad_errs);
992 res_qual[idx] |= bad_qual;
995 res_flux[idx] = good_flux*set_size/good;
996 res_errs[idx] = sqrt( good_errs)*set_size/good;
997 res_qual[idx] |= good_qual;
1005 if (cpl_error_get_code() != CPL_ERROR_NONE){
1009 for( i=0; i< set_size; i++){
1030 long sz = cpl_propertylist_get_size(plist);
1033 fprintf(stderr,
"Property list at address %p:\n", plist);
1035 for (i = 0; i < sz; i++) {
1036 cpl_property *p = cpl_propertylist_get(plist, i);
1057 const cpl_frame* f=NULL;
1060 const char* name=NULL;
1061 const char* tag=NULL;
1063 n=cpl_frameset_get_size(set);
1064 xsh_msg(
"files present in set");
1067 f=cpl_frameset_get_frame(set,i);
1068 name=cpl_frame_get_filename(f);
1069 tag=cpl_frame_get_tag(f);
1070 group=cpl_frame_get_group(f);
1071 xsh_msg(
"filename=%s tag=%s group=%d",name,tag,group);
1075 return cpl_error_get_code();
1091 const cpl_frame* f=NULL;
1094 const char* name=NULL;
1095 const char* tag=NULL;
1096 cpl_propertylist* plist=NULL;
1100 double jitter_width=0;
1103 n=cpl_frameset_get_size(set);
1104 xsh_msg(
"files present in set");
1107 f=cpl_frameset_get_frame(set,i);
1108 name=cpl_frame_get_filename(f);
1109 tag=cpl_frame_get_tag(f);
1112 plist=cpl_propertylist_load(name,0);
1136 xsh_msg(
"filename=%s tag=%s cum_off_y=%f nod_throw=%f jitter_width=%f",
1137 name,tag,cum_off_y,nod_throw,jitter_width);
1141 return cpl_error_get_code();
1178 p = strrchr (filename,
'/');
1179 return p ? p + 1 : (
char *) filename;
1195 const char *xsh_license =
1196 "This file is part of the X-shooter Instrument Pipeline\n"
1197 "Copyright (C) 2006 European Southern Observatory\n"
1199 "This program is free software; you can redistribute it and/or modify\n"
1200 "it under the terms of the GNU General Public License as published by\n"
1201 "the Free Software Foundation; either version 2 of the License, or\n"
1202 "(at your option) any later version.\n"
1204 "This program is distributed in the hope that it will be useful,\n"
1205 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1206 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1207 "GNU General Public License for more details.\n"
1209 "You should have received a copy of the GNU General Public License\n"
1210 "along with this program; if not, write to the Free Software\n"
1211 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, \n"
1212 "MA 02111-1307 USA";
1245 const cpl_parameterlist * parameters,
1247 cpl_frameset ** raws,
1248 cpl_frameset ** calib,
1249 const char * tag_list[],
1251 const char *recipe_id,
1252 unsigned int binary_version,
1253 const char *short_descr)
1255 char *recipe_string = NULL;
1256 char *recipe_version = NULL;
1258 char *spaces1 = NULL;
1259 char *spaces2 = NULL;
1260 char *spaces3 = NULL;
1261 char *spaces4 = NULL;
1263 cpl_parameter* p=NULL;
1286 xsh_msg(
"Keep Temporary File = no" ) ;
1288 xsh_msg(
"Keep Temporary File = yes" ) ;
1308 int version_string_length;
1309 int major_version, minor_version, micro_version;
1311 major_version = binary_version / 10000;
1312 minor_version = (binary_version % 10000) / 100;
1313 micro_version = binary_version % 100;
1316 assure (major_version < 100, CPL_ERROR_UNSUPPORTED_MODE,
1317 "Major version: %d", major_version);
1319 version_string_length = strlen (
"XX.YY.ZZ");
1320 recipe_version = cpl_calloc (
sizeof (
char), version_string_length + 1);
1322 snprintf (recipe_version, version_string_length + 1,
1323 "%d.%d.%d", major_version, minor_version, micro_version);
1330 int field =
MAXIMUM (strlen (PACKAGE_STRING), strlen (recipe_string));
1331 int nstars = 3 + 1 + field + 1 + 3;
1332 int nspaces1, nspaces2, nspaces3, nspaces4;
1336 nspaces1 = (field - strlen (PACKAGE_STRING)) / 2;
1337 nspaces2 = field - strlen (PACKAGE_STRING) - nspaces1;
1339 nspaces3 = (field - strlen (recipe_string)) / 2;
1340 nspaces4 = field - strlen (recipe_string) - nspaces3;
1342 spaces1 = cpl_calloc (
sizeof (
char), nspaces1 + 1);
1343 for (i = 0; i < nspaces1; i++)
1345 spaces2 = cpl_calloc (
sizeof (
char), nspaces2 + 1);
1346 for (i = 0; i < nspaces2; i++)
1348 spaces3 = cpl_calloc (
sizeof (
char), nspaces3 + 1);
1349 for (i = 0; i < nspaces3; i++)
1351 spaces4 = cpl_calloc (
sizeof (
char), nspaces4 + 1);
1352 for (i = 0; i < nspaces4; i++)
1355 stars = cpl_calloc (
sizeof (
char), nstars + 1);
1356 for (i = 0; i < nstars; i++)
1360 xsh_msg (
"*** %s%s%s ***", spaces1, PACKAGE_STRING, spaces2);
1361 xsh_msg (
"*** %s%s%s ***", spaces3, recipe_string, spaces4);
1370 decode_bp=cpl_parameter_get_int(p);
1379 nraws=cpl_frameset_get_size(*raws);
1388 if ((strcmp(recipe_id,
"xsh_util_physmod") != 0)
1389 && (strcmp(recipe_id,
"xsh_util_compute_response") != 0)) {
1392 "Provide at least a valid input raw frame");
1397 if ((strcmp(recipe_id,
"xsh_lingain") != 0)
1398 && (strcmp(recipe_id,
"xsh_mdark") != 0)) {
1407 cpl_free (recipe_string);
1408 cpl_free (recipe_version);
1414 return cpl_error_get_code ();
1435 TempFiles = cpl_malloc(
sizeof(
char*));
1455 for( i = 0 ; i<
NbTemp ; i++){
1475 ProdFiles = cpl_malloc(
sizeof(
char*));
1520 cpl_frameset * frames,
1521 cpl_parameterlist * parameters )
1528 cpl_frame * current = NULL;
1531 nframes = cpl_frameset_get_size( frames ) ;
1532 for( j = 0 ; j<nframes ; j++ ) {
1533 current = cpl_frameset_get_frame( frames, j);
1534 if ( cpl_frame_get_group( current ) == CPL_FRAME_GROUP_PRODUCT ) {
1544 xsh_msg(
"---- Deleting Temporary Files" ) ;
1545 for( i = 0 ; i<
NbTemp ; i++ ) {
1562 xsh_msg_warning (
"Recipe '%s' produced %d warning %s (excluding this one)",
1564 (warnings > 1) ?
"s" :
"");
1570 return cpl_error_get_code ();
1601 const char *keyword,
1602 cpl_type keywordtype,
1608 assure (plist != NULL, CPL_ERROR_NULL_INPUT,
"Null property list");
1609 assure (keyword != NULL, CPL_ERROR_NULL_INPUT,
"Null keyword");
1611 assure (cpl_propertylist_has (plist, keyword), CPL_ERROR_DATA_NOT_FOUND,
1612 "Keyword %s does not exist", keyword);
1614 check_msg (t = cpl_propertylist_get_type (plist, keyword),
1615 "Could not read type of keyword '%s'", keyword);
1616 assure (t == keywordtype, CPL_ERROR_TYPE_MISMATCH,
1617 "Keyword '%s' has wrong type (%s). %s expected",
1621 switch (keywordtype) {
1623 check_msg (*((
int *) result) = cpl_propertylist_get_int (plist, keyword),
1624 "Could not get (integer) value of %s", keyword);
1627 check_msg (*((
bool *) result) = cpl_propertylist_get_bool (plist, keyword),
1628 "Could not get (boolean) value of %s", keyword);
1630 case CPL_TYPE_DOUBLE:
1632 cpl_propertylist_get_double (plist, keyword),
1633 "Could not get (double) value of %s", keyword);
1635 case CPL_TYPE_STRING:
1637 cpl_propertylist_get_string (plist, keyword),
1638 "Could not get (string) value of %s", keyword);
1641 assure (
false, CPL_ERROR_INVALID_TYPE,
"Unknown type");
1645 return cpl_error_get_code ();
1660 char *result = NULL;
1662 assure (
s != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1664 result = cpl_calloc (
sizeof (
char), strlen (
s) + 1);
1665 assure (result != NULL, CPL_ERROR_ILLEGAL_OUTPUT,
1666 "Memory allocation failed");
1668 sprintf (result,
"%s",
s);
1671 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1696 ttm = *gmtime( now ) ;
1699 sprintf( date,
"%04d%02d%02d-%02d%02d%02d",
1700 ttm.tm_year+1900, ttm.tm_mon+1, ttm.tm_mday,
1701 ttm.tm_hour, ttm.tm_min, ttm.tm_sec ) ;
1721 char *result = NULL;
1723 assure (s1 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1724 assure (s2 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1726 result = cpl_calloc (
sizeof (
char), strlen (s1) + strlen (s2) + 1);
1727 assure (result != NULL, CPL_ERROR_ILLEGAL_OUTPUT,
1728 "Memory allocation failed");
1730 sprintf (result,
"%s%s", s1, s2);
1733 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1755 char *result = NULL;
1757 assure (s1 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1758 assure (s2 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1759 assure (s3 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1762 cpl_calloc (
sizeof (
char), strlen (s1) + strlen (s2) + strlen (s3) + 1);
1763 assure (result != NULL, CPL_ERROR_ILLEGAL_OUTPUT,
1764 "Memory allocation failed");
1766 sprintf (result,
"%s%s%s", s1, s2, s3);
1769 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1793 char *result = NULL;
1795 assure (s1 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1796 assure (s2 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1797 assure (s3 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1798 assure (s4 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1800 result = cpl_calloc (
sizeof (
char), strlen (s1) + strlen (s2) +
1801 strlen (s3) + strlen (s4) + 1);
1802 assure (result != NULL, CPL_ERROR_ILLEGAL_OUTPUT,
1803 "Memory allocation failed");
1805 sprintf (result,
"%s%s%s%s", s1, s2, s3, s4);
1808 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1832 const char *s4,
const char *s5)
1834 char *result = NULL;
1836 assure (s1 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1837 assure (s2 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1838 assure (s3 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1839 assure (s4 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1840 assure (s5 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1842 result = cpl_calloc (
sizeof (
char), strlen (s1) + strlen (s2) +
1843 strlen (s3) +strlen (s4) + strlen (s5) + 1);
1844 assure (result != NULL, CPL_ERROR_ILLEGAL_OUTPUT,
1845 "Memory allocation failed");
1847 sprintf (result,
"%s%s%s%s%s", s1, s2, s3, s4, s5);
1850 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1876 const char *s4,
const char *s5,
const char *s6)
1878 char *result = NULL;
1880 assure (s1 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1881 assure (s2 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1882 assure (s3 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1883 assure (s4 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1884 assure (s5 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1885 assure (s6 != NULL, CPL_ERROR_NULL_INPUT,
"Null string");
1887 result = cpl_calloc (
sizeof (
char),
1888 strlen (s1) + strlen (s2) + strlen (s3) +
1889 strlen (s4) + strlen (s5) + strlen (s6) + 1);
1890 assure (result != NULL, CPL_ERROR_ILLEGAL_OUTPUT,
1891 "Memory allocation failed");
1893 sprintf (result,
"%s%s%s%s%s%s", s1, s2, s3, s4, s5, s6);
1896 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1927 char *result = NULL;
1932 result = cpl_malloc (2);
1933 assure (result != NULL, CPL_ERROR_ILLEGAL_OUTPUT,
1934 "Memory allocation failed");
1937 size += strlen (
s) + 2;
1938 result = cpl_realloc (result,
size);
1939 assure (result != NULL, CPL_ERROR_ILLEGAL_OUTPUT,
1940 "Memory allocation failed");
1942 s = va_arg (av,
char *);
1943 if (
s == NULL || *
s ==
'\0')
1948 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1967 int (*compar)(
const void *,
const void *)) {
1984 for( i=0; i< (int)nmemb; i++) {
1985 sort[i].
data = (
void*)((
size_t)base+i*
size);
1993 for( i=0; i< (int)nmemb; i++) {
1994 idx[i] = sort[i].
idx;
2016 for(i=0; i<
size; i++){
2047 for(i=0; i<
size; i++){
2079 for(i=0; i<
size; i++){
2104 cpl_free((
void *)mem);
2119 cpl_image_delete (*i);
2136 cpl_table_delete (*t);
2152 cpl_mask_delete (*
m);
2167 cpl_imagelist_delete (*i);
2182 cpl_propertylist_delete (*p);
2197 cpl_polynomial_delete (*p);
2212 cpl_matrix_delete (*
m);
2227 cpl_parameterlist_delete (*p);
2242 cpl_parameter_delete (*p);
2257 cpl_frameset_delete (*f);
2272 cpl_frame_delete (*f);
2287 cpl_vector_delete (*v);
2302 cpl_array_delete (*
m);
2317 cpl_stats_delete (*
s);
2333 cpl_image_unwrap( *i);
2348 cpl_vector_unwrap (*v);
2364 cpl_array_unwrap (*a);
2379 cpl_bivector_unwrap_vectors (*b);
2393 getrusage( 0, &tm0 ) ;
2394 xsh_msg(
"%s - User: %u.%06u - Syst: %u.%06u",
2395 comment, (
unsigned)tm0.ru_utime.tv_sec,
2396 (
unsigned)tm0.ru_utime.tv_usec,
2397 (
unsigned)tm0.ru_stime.tv_sec,
2398 (
unsigned)tm0.ru_stime.tv_usec ) ;
2403#define XSH_DOUBLE_SWAP(a,b) { register double t=(a);(a)=(b);(b)=t; }
2404#define XSH_FLOAT_SWAP(a,b) { register float t=(a);(a)=(b);(b)=t; }
2405#define XSH_INT_SWAP(a,b) { register int t=(a);(a)=(b);(b)=t; }
2407#define XSH_PIX_STACK_SIZE 50
2422 double* mean,
double* stdev)
2424 cpl_vector* tab_vec = NULL;
2435 check (tab_vec = cpl_vector_new(
size));
2436 for( i=0; i<
size; i++){
2437 check( cpl_vector_set( tab_vec, i, tab[i]));
2440 check( *median = cpl_vector_get_median( tab_vec));
2441 check( *stdev = cpl_vector_get_stdev( tab_vec));
2442 check( *mean = cpl_vector_get_mean( tab_vec));
2469 if ( pix_arr == NULL ) {
2470 return CPL_ERROR_NULL_INPUT ;
2479 for (j=l+1 ; j<=ir ; j++) {
2481 for (i=j-1 ; i>=1 ; i--) {
2482 if (pix_arr[i-1] <= a)
break;
2483 pix_arr[i] = pix_arr[i-1];
2487 if (j_stack == 0)
break;
2488 ir = i_stack[j_stack-- -1];
2489 l = i_stack[j_stack-- -1];
2493 if (pix_arr[l] > pix_arr[ir-1]) {
2496 if (pix_arr[l-1] > pix_arr[ir-1]) {
2499 if (pix_arr[l] > pix_arr[l-1]) {
2506 do i++;
while (pix_arr[i-1] < a);
2507 do j--;
while (pix_arr[j-1] > a);
2511 pix_arr[l-1] = pix_arr[j-1];
2517 return CPL_ERROR_ILLEGAL_INPUT ;
2519 if (ir-i+1 >= j-l) {
2520 i_stack[j_stack-1] = ir;
2521 i_stack[j_stack-2] = i;
2524 i_stack[j_stack-1] = j-1;
2525 i_stack[j_stack-2] = l;
2531 return CPL_ERROR_NONE ;
2556 if ( pix_arr == NULL ) {
2557 return CPL_ERROR_NULL_INPUT ;
2566 for (j=l+1 ; j<=ir ; j++) {
2568 for (i=j-1 ; i>=1 ; i--) {
2569 if (pix_arr[i-1] <= a)
break;
2570 pix_arr[i] = pix_arr[i-1];
2574 if (j_stack == 0)
break;
2575 ir = i_stack[j_stack-- -1];
2576 l = i_stack[j_stack-- -1];
2580 if (pix_arr[l] > pix_arr[ir-1]) {
2583 if (pix_arr[l-1] > pix_arr[ir-1]) {
2586 if (pix_arr[l] > pix_arr[l-1]) {
2593 do i++;
while (pix_arr[i-1] < a);
2594 do j--;
while (pix_arr[j-1] > a);
2598 pix_arr[l-1] = pix_arr[j-1];
2604 return CPL_ERROR_ILLEGAL_INPUT ;
2606 if (ir-i+1 >= j-l) {
2607 i_stack[j_stack-1] = ir;
2608 i_stack[j_stack-2] = i;
2611 i_stack[j_stack-1] = j-1;
2612 i_stack[j_stack-2] = l;
2618 return CPL_ERROR_NONE ;
2641 if ( pix_arr == NULL ) {
2642 return CPL_ERROR_NULL_INPUT ;
2651 for (j=l+1 ; j<=ir ; j++) {
2653 for (i=j-1 ; i>=1 ; i--) {
2654 if (pix_arr[i-1] <= a)
break;
2655 pix_arr[i] = pix_arr[i-1];
2659 if (j_stack == 0)
break;
2660 ir = i_stack[j_stack-- -1];
2661 l = i_stack[j_stack-- -1];
2665 if (pix_arr[l] > pix_arr[ir-1]) {
2668 if (pix_arr[l-1] > pix_arr[ir-1]) {
2671 if (pix_arr[l] > pix_arr[l-1]) {
2678 do i++;
while (pix_arr[i-1] < a);
2679 do j--;
while (pix_arr[j-1] > a);
2683 pix_arr[l-1] = pix_arr[j-1];
2689 return CPL_ERROR_ILLEGAL_INPUT ;
2691 if (ir-i+1 >= j-l) {
2692 i_stack[j_stack-1] = ir;
2693 i_stack[j_stack-2] = i;
2696 i_stack[j_stack-1] = j-1;
2697 i_stack[j_stack-2] = l;
2703 return CPL_ERROR_NONE ;
2723 double result = 0. ;
2729 if ( (
size % 2) == 1 ) result = *(array+(
size/2)) ;
2733 min = *(array+(
size/2)-1) ;
2735 result = (min+
max)/2. ;
2752 double * wtab = NULL ;
2753 double max = -1000000. ;
2765 for( i = wsize ; i<(
size-wsize) ; i++ ) {
2770 for( k=0, j = i-wsize ; j<=(i+wsize) ; j++, k++ ) {
2812 for(i=1; i <
size; i++) {
2816 else if ( tab[i] > *
max) {
2838 cpl_vector *result = NULL;
2841 check( result = cpl_vector_new(
n+1));
2842 check( cpl_vector_set(result, 0, 1.0));
2846 check( cpl_vector_set(result, 1, X));
2847 while( indice <=
n){
2848 double T_indice = 0.0;
2849 double T_indice_1 = 0.0;
2850 double T_indice_2 = 0.0;
2852 check( T_indice_1 = cpl_vector_get( result, indice-1));
2853 check( T_indice_2 = cpl_vector_get( result, indice-2));
2854 T_indice = 2*X*T_indice_1-T_indice_2;
2855 check( cpl_vector_set( result, indice, T_indice));
2860 if ( cpl_error_get_code() != CPL_ERROR_NONE){
2882 double max,
double* tcheb_pos)
2895 for(i=0; i <
size; i++) {
2896 tcheb_pos[i] = pos[i]*a+b;
2897 if ( tcheb_pos[i] < -1. ) tcheb_pos[i] = -1. ;
2898 if ( tcheb_pos[i] > 1. ) tcheb_pos[i] = 1. ;
2928 if(res <= -1.000001) {
2932 if(res >= +1.000001) {
2983 int i = 0, ja, jb, idx = 0, vxsize,
size;
2984 double *
data = NULL;
2987 double *vline = NULL;
2997 check(
data = cpl_image_get_data_double(img));
2998 check(
nx = cpl_image_get_size_x(img));
2999 check(
ny = cpl_image_get_size_y(img));
3013 double a = 0.0, b = 0.0;
3022 cur_y = pointA->
y-1;
3023 ylines[nbylines-1]= cur_y;
3024 xline[vxsize-1] = pointA->
x-1;
3025 vline[vxsize-1] = pointA->
v;
3027 while( ( jb <
size) && ( pointA->
y == pointB->
y) ){
3030 if ( (pointB->
x-1) != xline[vxsize-1]){
3031 xline[vxsize] = pointB->
x-1;
3032 vline[vxsize] = pointB->
v;
3039 for(idx=0; idx<vxsize-1; idx++){
3050 a = ( vb - va) / ( xb - xa);
3055 for(i=0; i<= nbpix; i++){
3056 data[xa+i+cur_y*
nx] = a*(xa+i)+b;
3066 for( ja=0; ja< nbylines-1; ja++) {
3067 int line1 = ylines[ja];
3068 int line2 = ylines[ja+1];
3070 for(i=0; i<
nx; i++){
3071 double val1 =
data[i+line1*
nx];
3072 double val2 =
data[i+line2*
nx];
3073 double a = (val2-val1)/(line2-line1);
3077 for(k=line1+1; k < line2; k++){
3078 data[i+k*
nx] = a*(k-line1)+b;
3109 cpl_vector_fit_gaussian(
x, NULL,
y, NULL, CPL_FIT_ALL, &result->
peakpos,
3210 fprintf( stderr,
"\n******** %s *******\n", prompt ) ;
3230 return (bin_data*binning-0.5*(binning-1));
3248 return (
data+0.5*(binning-1))/binning;
3257 int yy,
mm, dd, hh, min, sec, millis ;
3264 sscanf( the_date,
"%64d%*c%64d%*c%64d%*c%64d%*c%64d%*c%64d%*c%64d",
3265 &yy, &
mm, &dd, &hh, &min, &sec, &millis ) ;
3267 date = yy*10000 +
mm*100 + dd ;
3268 temps = hh*10000000 + min*100000 + sec*1000 + millis ;
3269 fdate = (double)date + (
double)temps/1000000000. ;
3289 if (
first->frame_date > scnd->frame_date )
return 1 ;
3290 else if (
first->frame_date == scnd->frame_date )
return 0 ;
3310 cpl_frameset *result = NULL ;
3311 cpl_propertylist *header = NULL;
3315 check( nb = cpl_frameset_get_size( frameset));
3317 check( result = cpl_frameset_duplicate( frameset));
3323 for( i = 0 ; i < nb ; i++){
3324 const char *fname = NULL ;
3325 cpl_frame *frame = NULL ;
3326 const char *the_date = NULL;
3329 check( frame = cpl_frameset_get_frame( frameset, i));
3330 check( fname = cpl_frame_get_filename( frame));
3333 check( header = cpl_propertylist_load( fname, 0));
3339 frame_date[i].
frame = frame;
3348 for( i = 0 ; i<nb ; i++ ) {
3349 xsh_msg(
"%d: %.9lf '%s'", frame_date[i].frame_idx,
3350 frame_date[i].frame_date, frame_date[i].frame_name);
3354 check( result = cpl_frameset_new() ) ;
3357 for( i = 0 ; i <nb ; i++ ){
3358 check( cpl_frameset_insert( result,
3359 cpl_frame_duplicate(frame_date[i].frame)));
3363 if ( cpl_error_get_code() != CPL_ERROR_NONE){
3424 const char *Y,
const char *sigmaY,
int degree1,
int degree2,
3425 const char *polynomial_fit,
const char *residual_square,
3426 const char *variance_fit,
double *mse,
double *red_chisq,
3438 cpl_vector *vx1 = NULL;
3439 cpl_vector *vx2 = NULL;
3440 cpl_bivector *vx = NULL;
3441 cpl_vector *vy = NULL;
3442 cpl_vector *vsy = NULL;
3446 assure( t != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
3447 assure( cpl_table_has_column(t, X1), CPL_ERROR_ILLEGAL_INPUT,
3448 "No such column: %s", X1);
3449 assure( cpl_table_has_column(t, X2), CPL_ERROR_ILLEGAL_INPUT,
3450 "No such column: %s", X2);
3451 assure( cpl_table_has_column(t, Y), CPL_ERROR_ILLEGAL_INPUT,
3452 "No such column: %s", Y);
3453 assure( (variance == NULL && variance_fit == NULL) || sigmaY != NULL,
3454 CPL_ERROR_INCOMPATIBLE_INPUT,
3455 "Cannot calculate variances without sigmaY");
3456 if (sigmaY != NULL) {
3457 assure( cpl_table_has_column(t, sigmaY), CPL_ERROR_ILLEGAL_INPUT,
3458 "No such column: %s", sigmaY);
3460 if (polynomial_fit != NULL) {
3461 assure( !cpl_table_has_column(t, polynomial_fit), CPL_ERROR_ILLEGAL_INPUT,
3462 "Table already has '%s' column", polynomial_fit);
3464 if (residual_square != NULL) {
3465 assure( !cpl_table_has_column(t, residual_square), CPL_ERROR_ILLEGAL_INPUT,
3466 "Table already has '%s' column", residual_square);
3468 if (variance_fit != NULL) {
3469 assure( !cpl_table_has_column(t, variance_fit), CPL_ERROR_ILLEGAL_INPUT,
3470 "Table already has '%s' column", variance_fit);
3474 type = cpl_table_get_column_type(t, X1);
3475 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE,
3476 CPL_ERROR_INVALID_TYPE,
3478 type = cpl_table_get_column_type(t, X2);
3479 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE,
3480 CPL_ERROR_INVALID_TYPE,
3482 type = cpl_table_get_column_type(t, Y);
3483 assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE,
3484 CPL_ERROR_INVALID_TYPE,
3486 if (sigmaY != NULL) {
3487 type = cpl_table_get_column_type(t, sigmaY);
3489 type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE,
3490 CPL_ERROR_INVALID_TYPE,
3495 check_msg( cpl_table_cast_column(t, X1,
"_X1_double", CPL_TYPE_DOUBLE),
3496 "Could not cast table column to double");
3497 check_msg( cpl_table_cast_column(t, X2,
"_X2_double", CPL_TYPE_DOUBLE),
3498 "Could not cast table column to double");
3499 check_msg( cpl_table_cast_column(t, Y,
"_Y_double", CPL_TYPE_DOUBLE),
3500 "Could not cast table column to double");
3501 if (sigmaY != NULL) {
3502 check_msg( cpl_table_cast_column(t, sigmaY,
"_sY_double", CPL_TYPE_DOUBLE),
3503 "Could not cast table column to double");
3508 check_msg( cpl_table_new_column(t,
"_residual_square", CPL_TYPE_DOUBLE),
3509 "Could not create column");
3515 (
N = cpl_table_get_nrow(t), x1 = cpl_table_get_data_double(t,
"_X1_double"), x2 = cpl_table_get_data_double(t,
"_X2_double"),
y = cpl_table_get_data_double(t,
"_Y_double"), res= cpl_table_get_data_double(t,
"_residual_square")),
3516 "Could not read table data");
3518 if (sigmaY != NULL) {
3519 check_msg(sy = cpl_table_get_data_double(t,
"_sY_double"),
3520 "Could not read table data");
3525 assure(
N > 0, CPL_ERROR_ILLEGAL_INPUT,
"Empty table");
3532 vx1 = cpl_vector_wrap(
N, x1);
3533 vx2 = cpl_vector_wrap(
N, x2);
3534 vy = cpl_vector_wrap(
N,
y);
3537 vsy = cpl_vector_wrap(
N, sy);
3544 vx = cpl_bivector_wrap_vectors(vx1, vx2);
3550 "Could not fit polynomial");
3557 cpl_table_fill_column_window_double(t,
"_residual_square", 0,
3558 cpl_table_get_nrow(t), 0.0);
3560 for (i = 0; i <
N; i++) {
3565 res[i] = (yfit - yval) * (yfit - yval);
3574 sigma2 = cpl_table_get_column_median(t,
"_residual_square")
3575 / (0.6744 * 0.6744);
3580 "Could not remove outlier points");
3584 "%d of %d points rejected in kappa-sigma clipping. rms=%f", rejected,
N, sqrt(sigma2));
3587 total_rejected += rejected;
3588 N = cpl_table_get_nrow(t);
3594 }
while (rejected > 0 && rejected > min_reject * (
N + rejected)
3595 &&
N >= (degree1 + 1) * (degree2 + 1) + 1);
3599 "%d of %d points (%f %%) rejected in kappa-sigma clipping", total_rejected,
N + total_rejected, (100.0*total_rejected)/(
N + total_rejected));
3609 (
N = cpl_table_get_nrow(t), x1 = cpl_table_get_data_double(t,
"_X1_double"), x2 = cpl_table_get_data_double(t,
"_X2_double"),
y = cpl_table_get_data_double(t,
"_Y_double"), res= cpl_table_get_data_double(t,
"_residual_square")),
3610 "Could not read table data");
3612 if (sigmaY != NULL) {
3613 check_msg(sy = cpl_table_get_data_double(t,
"_sY_double"),
3614 "Could not read table data");
3619 assure(
N > 0, CPL_ERROR_ILLEGAL_INPUT,
"Empty table");
3626 vx1 = cpl_vector_wrap(
N, x1);
3627 vx2 = cpl_vector_wrap(
N, x2);
3628 vy = cpl_vector_wrap(
N,
y);
3631 vsy = cpl_vector_wrap(
N, sy);
3638 vx = cpl_bivector_wrap_vectors(vx1, vx2);
3642 if (variance_fit != NULL || variance != NULL) {
3646 "Could not fit polynomial");
3650 "Could not fit polynomial");
3653 cpl_table_erase_column(t,
"_residual_square");
3656 if (polynomial_fit != NULL || residual_square != NULL) {
3660 check_msg( cpl_table_new_column(t,
"_polynomial_fit", CPL_TYPE_DOUBLE),
3661 "Could not create column");
3663 cpl_table_fill_column_window_double(t,
"_polynomial_fit", 0,
3664 cpl_table_get_nrow(t), 0.0);
3666 x1 = cpl_table_get_data_double(t,
"_X1_double");
3667 x2 = cpl_table_get_data_double(t,
"_X2_double");
3668 pf = cpl_table_get_data_double(t,
"_polynomial_fit");
3670 for (i = 0; i <
N; i++) {
3672 double x1val, x2val, yfit;
3674 check_msg(( x1val = cpl_table_get_double(t,
"_X1_double", i, NULL),
3675 x2val = cpl_table_get_double(t,
"_X2_double", i, NULL),
3678 cpl_table_set_double(t,
"_polynomial_fit", i, yfit)),
3679 "Could not evaluate polynomial");
3687 if (residual_square != NULL) {
3688 check_msg(( cpl_table_duplicate_column(t, residual_square,
3689 t,
"_polynomial_fit"), cpl_table_subtract_columns(t, residual_square, Y),
3690 cpl_table_multiply_columns(t, residual_square, residual_square)),
3692 "Could not calculate Residual of fit");
3696 if (polynomial_fit != NULL) {
3697 cpl_table_name_column(t,
"_polynomial_fit", polynomial_fit);
3699 cpl_table_erase_column(t,
"_polynomial_fit");
3704 if (variance_fit != NULL) {
3708 check_msg( cpl_table_new_column(t, variance_fit, CPL_TYPE_DOUBLE),
3709 "Could not create column");
3711 cpl_table_fill_column_window_double(t, variance_fit, 0,
3712 cpl_table_get_nrow(t), 0.0);
3714 x1 = cpl_table_get_data_double(t,
"_X1_double");
3715 x2 = cpl_table_get_data_double(t,
"_X2_double");
3716 vf = cpl_table_get_data_double(t, variance_fit);
3718 for (i = 0; i <
N; i++) {
3720 double x1val, x2val, yfit_variance;
3721 check_msg(( x1val = cpl_table_get_double(t,
"_X1_double", i, NULL),
3722 x2val = cpl_table_get_double(t,
"_X2_double", i, NULL),
3726 cpl_table_set_double(t, variance_fit, i, yfit_variance)),
3727 "Could not evaluate polynomial");
3736 ( cpl_table_erase_column(t,
"_X1_double"), cpl_table_erase_column(t,
"_X2_double"), cpl_table_erase_column(t,
"_Y_double")),
3737 "Could not delete temporary columns");
3739 if (sigmaY != NULL) {
3740 check_msg( cpl_table_erase_column(t,
"_sY_double"),
3741 "Could not delete temporary column");
3750 if (variance != NULL) {
3751 *variance = variance_local;
3755 if (cpl_error_get_code() != CPL_ERROR_NONE) {
3786 cpl_table_select_operator
operator,
3792 assure( t != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
3793 assure( cpl_table_has_column(t, column), CPL_ERROR_INCOMPATIBLE_INPUT,
3794 "No such column: %s", column);
3796 type = cpl_table_get_column_type(t, column);
3798 assure( type == CPL_TYPE_DOUBLE || type == CPL_TYPE_FLOAT ||
3799 type == CPL_TYPE_INT, CPL_ERROR_INVALID_TYPE,
3800 "Column '%s' must be double or int. %s found", column,
3803 check_msg( cpl_table_select_all(t),
"Error selecting rows");
3805 if (type == CPL_TYPE_DOUBLE)
3807 result = cpl_table_and_selected_double(t, column,
operator, value);
3809 else if (type == CPL_TYPE_FLOAT)
3811 result = cpl_table_and_selected_float(t, column,
operator, value);
3813 else if (type == CPL_TYPE_INT)
3815 result = cpl_table_and_selected_int(t, column,
operator,
3851 cpl_table_select_operator
operator,
3856 assure( t != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
3857 assure( cpl_table_has_column(t, column), CPL_ERROR_INCOMPATIBLE_INPUT,
3858 "No such column: %s", column);
3861 "Error selecting rows");
3863 check_msg( cpl_table_erase_selected(t),
"Error deleting rows");
3882 cpl_frame *result = NULL;
3883 const char* tag =
"INV_PRE";
3891 check( cpl_frame_set_tag( result, tag));
3894 if (cpl_error_get_code() != CPL_ERROR_NONE){
3915 cpl_frame *result = NULL;
3916 cpl_frame *sign_frame = NULL;
3918 const char* tag =
"ABS_PRE";
3919 const char* sign_tag =
"SIGN_PRE";
3920 const char *filename = NULL;
3921 cpl_image *sign_img = NULL;
3926 check( filename = cpl_frame_get_filename( in));
3929 sprintf( name ,
"ABS_%s", filename);
3931 check( cpl_frame_set_tag( result, tag));
3932 sprintf( name ,
"SIGN_%s", filename);
3933 check( cpl_image_save( sign_img, name, CPL_BPP_32_SIGNED,
3934 NULL,CPL_IO_DEFAULT));
3936 check( sign_frame = cpl_frame_new());
3937 check( cpl_frame_set_filename( sign_frame, name));
3938 check( cpl_frame_set_tag( sign_frame, sign_tag));
3942 if (cpl_error_get_code() != CPL_ERROR_NONE){
3963 const char* tag=NULL;
3964 cpl_frame* loc_frm=NULL;
3968 cpl_frame_set_filename(frm,name_o);
3985 cpl_image* ima=NULL;
3986 const char* name=NULL;
3988 int next=cpl_frame_get_nextensions(frm);
3989 int type=CPL_TYPE_FLOAT;
3990 cpl_propertylist* plist=NULL;
3991 name=cpl_frame_get_filename(frm);
3993 for(i=0;i<=next;i++) {
3994 ima=cpl_image_load(name,CPL_TYPE_UNSPECIFIED,0,i);
3995 type = cpl_image_get_type(ima);
3996 plist=cpl_propertylist_load(name,i);
3998 cpl_image_save(ima,name_o,type,plist,CPL_IO_DEFAULT);
4000 cpl_image_save(ima,name_o,type,plist,CPL_IO_EXTEND);
4019 const char* fname=NULL;
4023 cpl_table *tbl_ext = NULL;
4024 cpl_propertylist *tbl_ext_header = NULL;
4025 cpl_propertylist *primary_header = NULL;
4028 fname=cpl_frame_get_filename(frame);
4029 nbext = cpl_frame_get_nextensions( frame);
4031 for( i = 0 ; i<nbext ; i++ ) {
4033 check( tbl_ext = cpl_table_load( fname, i+1, 0));
4034 check( tbl_ext_header = cpl_propertylist_load( fname, i+1));
4036 if ( i == 0 ) extension = CPL_IO_DEFAULT ;
4037 else extension = CPL_IO_EXTEND ;
4038 check(cpl_table_save( tbl_ext, primary_header, tbl_ext_header,
4039 name_o, extension));
4065 cpl_frame *result = NULL;
4066 cpl_image *sign_img = NULL;
4067 const char* name = NULL;
4068 const char* tag =
"MULT_IMG_PRE";
4073 check( name = cpl_frame_get_filename( sign));
4075 check( sign_img = cpl_image_load( name, CPL_TYPE_INT, 0, 0));
4078 check( cpl_frame_set_tag( result, tag));
4081 if (cpl_error_get_code() != CPL_ERROR_NONE){
4114 const char* name_ima=NULL;
4115 const char* name_tab=NULL;
4116 cpl_table* tab=NULL;
4117 cpl_image* ima=NULL;
4118 cpl_image* qua=NULL;
4119 cpl_table* ext=NULL;
4125 double flux_min_init=FLT_MAX;
4126 double flux_max_init=-FLT_MAX;
4128 double flux_min_ord=flux_min_init;
4129 double flux_max_ord=flux_max_init;
4131 double flux_min=flux_min_init;
4132 double flux_max=flux_max_init;
4137 cpl_propertylist* plist=NULL;
4138 char qc_flux_min[40];
4139 char qc_flux_max[40];
4146 cpl_ensure_code(frm_ima != NULL, CPL_ERROR_NULL_INPUT);
4147 cpl_ensure_code(frm_tab != NULL, CPL_ERROR_NULL_INPUT);
4149 check(name_ima=cpl_frame_get_filename(frm_ima));
4150 check(name_tab=cpl_frame_get_filename(frm_tab));
4152 check(plist=cpl_propertylist_load(name_ima,0));
4158 check(tab=cpl_table_load(name_tab,2,0));
4159 check(ima=cpl_image_load(name_ima,CPL_TYPE_DOUBLE,0,0));
4160 check(qua=cpl_image_load(name_ima,CPL_TYPE_INT,0,2));
4161 check(sx=cpl_image_get_size_x(ima));
4162 check(sy=cpl_image_get_size_y(ima));
4167 check(pima=cpl_image_get_data_double(ima));
4168 check(pqua=cpl_image_get_data_int(qua));
4169 check(ord_min=cpl_table_get_column_min(tab,
"ABSORDER"));
4170 check(ord_max=cpl_table_get_column_max(tab,
"ABSORDER"));
4173 for(i=ord_min;i<=ord_max;i++) {
4174 flux_min_ord=flux_min_init;
4175 flux_max_ord=flux_max_init;
4177 check(next=cpl_table_and_selected_int(tab,
"ABSORDER",CPL_EQUAL_TO,i));
4178 ext=cpl_table_extract_selected(tab);
4179 cx=cpl_table_get_data_double(ext,
"CENTER_X");
4180 cy=cpl_table_get_data_double(ext,
"CENTER_Y");
4182 for(j=0;j<next;j++) {
4184 ix=(int)(cx[j]/
binx+0.5);
4185 iy=(int)(cy[j]/
biny+0.5);
4187 if( ( (ix>=0) && (ix<sx) ) &&
4188 ( (iy>=0) && (iy<sy) ) ) {
4189 check(flux=pima[ix+sx*iy]);
4190 check(qual=pqua[ix+sx*iy]);
4191 if(!isnan(flux) && ((qual & decode_bp)==0)) {
4192 if(flux<flux_min_ord) {
4196 if(flux>flux_max_ord) {
4206 cpl_table_select_all(tab);
4209 flux_min=(flux_min_ord<flux_min)? flux_min_ord:flux_min;
4210 flux_max=(flux_max_ord>flux_max)? flux_max_ord:flux_max;
4213 sprintf(qc_flux_min,
"%s%d%s",qc_key_prefix,i,
" MIN");
4214 sprintf(qc_flux_max,
"%s%d%s",qc_key_prefix,i,
" MAX");
4217 cpl_propertylist_append_double(plist,qc_flux_min,flux_min_ord);
4219 cpl_propertylist_append_double(plist,qc_flux_max,flux_max_ord);
4223 sprintf(qc_flux_min,
"%s%s",qc_key_prefix,
" MIN");
4224 sprintf(qc_flux_max,
"%s%s",qc_key_prefix,
" MAX");
4225 cpl_propertylist_append_double(plist,qc_flux_min,flux_min);
4227 cpl_propertylist_append_double(plist,qc_flux_max,flux_max);
4239 return cpl_error_get_code();
4259 const cpl_propertylist* pheader)
4261 const char *fname = NULL;
4264 cpl_image *image = NULL ;
4267 cpl_image* ext_img=NULL;
4268 cpl_propertylist* ext_header=NULL;
4271 nbext = cpl_frame_get_nextensions( frame);
4273 check( fname = cpl_frame_get_filename( frame));
4274 check( image = cpl_image_load( fname, CPL_TYPE_FLOAT, 0, 0));
4279 cpl_image_save(image,
"tmp.fits", CPL_BPP_IEEE_FLOAT, pheader,
4283 for( i = 1 ; i<=nbext ; i++ ) {
4285 check( ext_img = cpl_image_load( fname, CPL_TYPE_FLOAT,0, i));
4286 check( ext_header = cpl_propertylist_load( fname ,i));
4289 extension = CPL_IO_DEFAULT ;
4292 extension = CPL_IO_EXTEND ;
4293 check(cpl_image_save( ext_img,
"tmp.fits", CPL_BPP_IEEE_FLOAT,ext_header,
4300 sprintf(cmd,
"mv tmp.fits %s",fname);
4302 system(
"rm -f tmp.fits");
4310 return cpl_error_get_code();
4321 double err_median =0.0;
4322 int i, vect_size =0;
4323 double *
data = NULL;
4327 check( vect_size = cpl_vector_get_size( vect));
4328 check(
data = cpl_vector_get_data( vect));
4331 for( i=0; i< vect_size; i++){
4335 err_median = sqrt(
M_PI / 2.0 *
4336 ((
double) vect_size / ((
double) vect_size- 1.))) *
4337 (1./(double)vect_size) * sqrt (err_median);
4340 err_median =
data[0];
4356 double err_mean =0.0;
4357 int i, vect_size =0;
4358 double *
data = NULL;
4362 check( vect_size = cpl_vector_get_size( vect));
4363 check(
data = cpl_vector_get_data( vect));
4365 for( i=0; i< vect_size; i++){
4368 err_mean = sqrt( err_mean)/(double)vect_size;
4385 return (
x >=0) ? (long)(
x+0.5) : (long)(
x-0.5);
4401 return (
x <=
y) ?
x :
y;
4417 return (
x >=
y) ?
x :
y;
4434 return (
x <=
y) ?
x :
y;
4451 return (
x >=
y) ?
x :
y;
4465 double result = 1.0;
4579 double yp1, yp2, yp = 0;
4580 double xpi, xpi1, l1, l2, lp1, lp2;
4583 if (
x[0] <=
x[
n-1] && (xp <
x[0] || xp >
x[
n-1]) )
return 0.0;
4584 if (
x[0] >
x[
n-1] && (xp >
x[0] || xp <
x[
n-1]) )
return 0.0;
4586 if (
x[0] <=
x[
n-1] )
4588 for ( i = (*istart)+1; i <= n && xp >=
x[i-1]; i++ )
4593 for ( i = (*istart)+1; i <=
n && xp <=
x[i-1]; i++ )
4600 lp1 = 1.0 / (
x[i-1] -
x[i]);
4605 yp1 = (
y[1] -
y[0]) / (
x[1] -
x[0]);
4609 yp1 = (
y[i] -
y[i-2]) / (
x[i] -
x[i-2]);
4614 yp2 = (
y[
n-1] -
y[
n-2]) / (
x[
n-1] -
x[
n-2]);
4618 yp2 = (
y[i+1] -
y[i-1]) / (
x[i+1] -
x[i-1]);
4626 yp =
y[i-1]*(1 - 2.0*lp1*xpi)*l1*l1 +
4627 y[i]*(1 - 2.0*lp2*xpi1)*l2*l2 +
4628 yp1*xpi*l1*l1 + yp2*xpi1*l2*l2;
4649 const char *column_y,
int *istart )
4654 const double *
x, *
y;
4656 check_msg(
x = cpl_table_get_data_double_const(t, column_x),
4657 "Error reading column '%s'", column_x);
4658 check_msg(
y = cpl_table_get_data_double_const(t, column_y),
4659 "Error reading column '%s'", column_y);
4661 n = cpl_table_get_nrow(t);
4682 cpl_vector * result =NULL;
4683 cpl_type type=CPL_TYPE_FLOAT;
4698 ilx=cpl_image_get_size_x(spectrum);
4699 ily=cpl_image_get_size_y(spectrum);
4702 result=cpl_vector_new(
size);
4703 pv=cpl_vector_get_data(result);
4708 pi=cpl_image_get_data_int(spectrum);
4709 for ( i = 0 ; i <
size ; i++ ) pv[i] = (
double) pi[i] ;
4712 case CPL_TYPE_FLOAT:
4713 pf=cpl_image_get_data_float(spectrum);
4714 for ( i = 0 ; i <
size ; i++ ) pv[i] = (
double) pf[i] ;
4717 case CPL_TYPE_DOUBLE:
4718 pd=cpl_image_get_data_double(spectrum);
4719 for ( i = 0 ; i <
size ; i++ ) pv[i] = (
double) pd[i] ;
4748 cpl_image* image=NULL;
4750 const double* pv=NULL;
4756 size=cpl_vector_get_size(vector);
4757 image=cpl_image_new(
size,1,type);
4758 pv=cpl_vector_get_data_const(vector);
4759 if(type == CPL_TYPE_INT) {
4760 pi=cpl_image_get_data_int(image);
4761 for(i=0;i<
size;i++) {
4764 }
else if (type == CPL_TYPE_FLOAT) {
4765 pf=cpl_image_get_data_float(image);
4766 for(i=0;i<
size;i++) {
4769 }
else if (type == CPL_TYPE_DOUBLE) {
4770 pd=cpl_image_get_data_double(image);
4771 for(i=0;i<
size;i++) {
4775 assure(
false, CPL_ERROR_INVALID_TYPE,
4776 "No CPL type to represent BITPIX = %d", type);
4780 if (cpl_error_get_code() != CPL_ERROR_NONE){
4801 cpl_frame* result=NULL;
4802 cpl_image* data_sci=NULL;
4803 cpl_image* errs_sci=NULL;
4804 cpl_image* qual_sci=NULL;
4806 cpl_image* data_tmp=NULL;
4807 cpl_image* errs_tmp=NULL;
4809 cpl_vector* data_vec=NULL;
4810 cpl_vector* errs_vec=NULL;
4811 cpl_vector* qual_vec=NULL;
4813 cpl_propertylist* hdat=NULL;
4814 cpl_propertylist* herr=NULL;
4815 cpl_propertylist* hqua=NULL;
4816 cpl_propertylist* plist=NULL;
4818 cpl_table* response_table=NULL;
4820 const char* name_s=NULL;
4822 const char* name_r=NULL;
4825 double lambda_start=0;
4832 double *fluxcal_science_data = NULL;
4833 double *fluxcal_science_noise = NULL;
4838 check(name_s=cpl_frame_get_filename(merged_sci));
4840 name_o=cpl_sprintf(
"%s.fits",tag_o);
4842 check(hdat=cpl_propertylist_load(name_s,0));
4843 check(herr=cpl_propertylist_load(name_s,1));
4844 check(hqua=cpl_propertylist_load(name_s,2));
4850 check(data_vec=cpl_vector_load(name_s,0));
4851 check(errs_vec=cpl_vector_load(name_s,1));
4867 data_tmp=cpl_image_cast(data_sci,CPL_TYPE_DOUBLE);
4868 errs_tmp=cpl_image_cast(errs_sci,CPL_TYPE_DOUBLE);
4872 check(name_r=cpl_frame_get_filename(response));
4873 check(response_table=cpl_table_load(name_r,1,0));
4888 xsh_msg(
"Multiply by response function");
4890 check(nbins = cpl_image_get_size_x(data_tmp));
4891 check(ntraces = cpl_image_get_size_y(data_tmp));
4893 check(fluxcal_science_data = cpl_image_get_data_double(data_tmp));
4894 check(fluxcal_science_noise = cpl_image_get_data_double(errs_tmp));
4897 "Error reading start wavelength from reduced science header");
4899 "Error reading bin width from header");
4901 check(cpl_table_cast_column(response_table,
"LAMBDA",
"DLAMBDA",CPL_TYPE_DOUBLE));
4902 check(cpl_table_cast_column(response_table,
"FLUX",
"DFLUX",CPL_TYPE_DOUBLE));
4904 for (bin = 1; bin <= nbins; bin++)
4914 "DLAMBDA",
"DFLUX", &istart),
4915 "Error interpolating response curve at lambda = %f wlu", lambda);
4919 for (trace = 1; trace <= ntraces; trace++)
4925 fluxcal_science_data [(bin-1) + (trace-1)*nbins] *= response;
4927 fluxcal_science_noise[(bin-1) + (trace-1)*nbins] *= fabs(response);
4939 cpl_table_erase_column(response_table,
"DLAMBDA");
4940 cpl_table_erase_column(response_table,
"DFLUX");
4946 "FLUX",
"ERRS",
"QUAL",0));
4951 "FLUX",
"ERRS",
"QUAL",1));
4956 "FLUX",
"ERRS",
"QUAL",2));
4968 check(qual_vec=cpl_vector_load(name_s,2));
4981 result=
xsh_frame_product(name_o,tag_o,CPL_FRAME_TYPE_IMAGE,CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
5019 cpl_frame* result=NULL;
5020 cpl_image* data_sci=NULL;
5021 cpl_image* errs_sci=NULL;
5022 cpl_image* qual_sci=NULL;
5024 cpl_image* data_tmp=NULL;
5025 cpl_image* errs_tmp=NULL;
5027 cpl_vector* data_vec=NULL;
5028 cpl_vector* errs_vec=NULL;
5029 cpl_vector* qual_vec=NULL;
5031 cpl_propertylist* hdat=NULL;
5032 cpl_propertylist* herr=NULL;
5033 cpl_propertylist* hqua=NULL;
5034 cpl_propertylist* plist=NULL;
5036 cpl_table* response_table=NULL;
5038 const char* name_s=NULL;
5040 const char* name_r=NULL;
5042 double lambda_start=0;
5055 double *fluxcal_science_data = NULL;
5056 double *fluxcal_science_noise = NULL;
5061 check(name_s=cpl_frame_get_filename(extracted_sci));
5064 name_o=cpl_sprintf(
"%s.fits",tag_o);
5065 next_sci=cpl_frame_get_nextensions(extracted_sci);
5066 next_res=cpl_frame_get_nextensions(response);
5067 xsh_msg(
"Multiply by response function");
5068 check(name_r=cpl_frame_get_filename(response));
5078 for(ext=0,
order=0;ext<next_sci;ext+=3,
order++) {
5083 check(hdat=cpl_propertylist_load(name_s,ext+0));
5084 check(herr=cpl_propertylist_load(name_s,ext+1));
5085 check(hqua=cpl_propertylist_load(name_s,ext+2));
5091 check(data_vec=cpl_vector_load(name_s,ext+0));
5092 check(errs_vec=cpl_vector_load(name_s,ext+1));
5115 data_tmp=cpl_image_cast(data_sci,CPL_TYPE_DOUBLE);
5116 errs_tmp=cpl_image_cast(errs_sci,CPL_TYPE_DOUBLE);
5121 check(response_table=cpl_table_load(name_r,
order+1,0));
5124 check(response_table=cpl_table_load(name_r,next_res,0));
5131 check(nbins = cpl_image_get_size_x(data_tmp));
5132 check(ntraces = cpl_image_get_size_y(data_tmp));
5134 check(fluxcal_science_data = cpl_image_get_data_double(data_tmp));
5135 check(fluxcal_science_noise = cpl_image_get_data_double(errs_tmp));
5138 "Error reading start wavelength from reduced science header");
5140 "Error reading bin width from header");
5142 check(cpl_table_cast_column(response_table,
"LAMBDA",
"DLAMBDA",CPL_TYPE_DOUBLE));
5143 check(cpl_table_cast_column(response_table,
"RESPONSE",
"DFLUX",CPL_TYPE_DOUBLE));
5145 for (bin = 1; bin <= nbins; bin++)
5155 "DLAMBDA",
"DFLUX", &istart),
5156 "Error interpolating response curve at lambda = %f wlu", lambda);
5160 for (trace = 1; trace <= ntraces; trace++)
5166 fluxcal_science_data [(bin-1) + (trace-1)*nbins] *= response;
5168 fluxcal_science_noise[(bin-1) + (trace-1)*nbins] *= fabs(response);
5180 cpl_table_erase_column(response_table,
"DLAMBDA");
5181 cpl_table_erase_column(response_table,
"DFLUX");
5187 "FLUX",
"ERRS",
"QUAL",0));
5192 "FLUX",
"ERRS",
"QUAL",1));
5197 "FLUX",
"ERRS",
"QUAL",2));
5211 check(qual_vec=cpl_vector_load(name_s,ext+2));
5229 result=
xsh_frame_product(name_o,tag_o,CPL_FRAME_TYPE_IMAGE,CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
5254static cpl_error_code
5264 for(i = i_start;i < i_end;i++) {
5271 return cpl_error_get_code();
5298 for(i=i1_sup;i<i2_inf;i++) {
5302 sum2+=pif[i]*(piw[i+1]-piw[i]);
5319 return (sum1+sum2+sum3);
5343 cpl_frame* result=NULL;
5344 cpl_table* tab_inp=NULL;
5345 cpl_table* tab_out=NULL;
5352 double wave_start=0;
5354 const char* fname=NULL;
5373 const char* tag=NULL;
5375 cpl_propertylist* plist=NULL;
5378 check(fname=cpl_frame_get_filename(frame_inp));
5379 tag=cpl_frame_get_tag(frame_inp);
5380 plist=cpl_propertylist_load(fname,0);
5384 tab_inp=cpl_table_load(fname,1,0);
5385 nrow_inp=cpl_table_get_nrow(tab_inp);
5386 wave_min=cpl_table_get_column_min(tab_inp,
"LAMBDA");
5387 wave_max=cpl_table_get_column_max(tab_inp,
"LAMBDA");
5389 wave_min=(wave_min>wmin) ? wave_min : wmin;
5390 wave_max=(wave_max<wmax) ? wave_max : wmax;
5393 wave_start=floor(wave_min);
5399 xsh_msg(
"Resample ref flux std spectrum to %g [nm] step",wstep);
5405 nrow_out=ceil((wave_max-wave_start)/wstep);
5406 tab_out=cpl_table_new(nrow_out);
5408 cpl_table_new_column(tab_out,
"LAMBDA",CPL_TYPE_DOUBLE);
5409 cpl_table_new_column(tab_out,
"FLUX",CPL_TYPE_DOUBLE);
5410 cpl_table_new_column(tab_out,
"BIN_WIDTH",CPL_TYPE_DOUBLE);
5412 cpl_table_fill_column_window_double(tab_out,
"LAMBDA",0,nrow_out,0.);
5413 cpl_table_fill_column_window_double(tab_out,
"FLUX",0,nrow_out,0.);
5414 cpl_table_fill_column_window_double(tab_out,
"BIN_WIDTH",0,nrow_out,wstep);
5417 pow=cpl_table_get_data_double(tab_out,
"LAMBDA");
5418 pof=cpl_table_get_data_double(tab_out,
"FLUX");
5420 piw=cpl_table_get_data_double(tab_inp,
"LAMBDA");
5421 pif=cpl_table_get_data_double(tab_inp,
"FLUX");
5429 istep=(int) (wstep/(wave_max-wave_min)*nrow_inp+0.5);
5435 for(i=0;i<nrow_out;i++) {
5436 pow[i]=wave_start+i*wstep;
5497 i2_inf,i2_sup,pow[i],wstep);
5506 cpl_table_and_selected_double(tab_out,
"LAMBDA",CPL_LESS_THAN,wmin);
5507 cpl_table_erase_selected(tab_out);
5508 cpl_table_and_selected_double(tab_out,
"LAMBDA",CPL_GREATER_THAN,wmax);
5509 cpl_table_erase_selected(tab_out);
5511 name=cpl_sprintf(
"RESAMPLED_%s_%s.fits",tag,
5514 check(cpl_table_save(tab_out,plist,NULL,name,CPL_IO_DEFAULT));
5517 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
5541 cpl_frame* result=NULL;
5542 cpl_frame* frm=NULL;
5544 cpl_image* ima=NULL;
5545 cpl_image* err=NULL;
5547 cpl_image* ima_sum=NULL;
5548 cpl_image* err_sum=NULL;
5549 cpl_image* qua_sum=NULL;
5550 cpl_propertylist* hdat=NULL;
5551 cpl_propertylist* herr=NULL;
5552 cpl_propertylist* hqua=NULL;
5555 const char* fname=NULL;
5560 size=cpl_frameset_get_size(set);
5561 for(i=0;i<
size;i++) {
5563 frm=cpl_frameset_get_frame(set,i);
5564 fname=cpl_frame_get_filename(frm);
5569 cpl_image_multiply(err,err);
5571 ima_sum=cpl_image_duplicate(ima);
5572 err_sum=cpl_image_duplicate(err);
5574 cpl_image_add(ima_sum,ima);
5575 cpl_image_add(err_sum,err);
5584 cpl_image_divide_scalar(ima_sum,
size);
5585 cpl_image_divide_scalar(err_sum,
size);
5588 check(cpl_image_power(err_sum,0.5));
5593 frm=cpl_frameset_get_frame(set,0);
5594 fname=cpl_frame_get_filename(frm);
5595 hdat=cpl_propertylist_load(fname,0);
5596 herr=cpl_propertylist_load(fname,1);
5597 hqua=cpl_propertylist_load(fname,2);
5606 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
5656 const double airmass,
5657 const cpl_table* tbl_atm_ext)
5661 cpl_image* data_ima=NULL;
5662 cpl_image* errs_ima=NULL;
5663 cpl_image* qual_ima=NULL;
5664 cpl_image* data_nrm=NULL;
5665 cpl_image* errs_nrm=NULL;
5667 cpl_image* data_tmp=NULL;
5668 cpl_image* errs_tmp=NULL;
5670 cpl_image* data_nrm_tmp=NULL;
5671 cpl_image* errs_nrm_tmp=NULL;
5673 cpl_vector* data_vec=NULL;
5674 cpl_vector* errs_vec=NULL;
5675 cpl_vector* qual_vec=NULL;
5677 cpl_propertylist* hdat=NULL;
5678 cpl_propertylist* herr=NULL;
5679 cpl_propertylist* hqua=NULL;
5683 sprintf(name_o,
"%s.fits",tag_o);
5689 hdat=cpl_propertylist_load(name_s,ext+0);
5690 herr=cpl_propertylist_load(name_s,ext+1);
5691 hqua=cpl_propertylist_load(name_s,ext+2);
5698 check(data_vec=cpl_vector_load(name_s,ext+0));
5699 check(errs_vec=cpl_vector_load(name_s,ext+1));
5719 data_tmp=cpl_image_cast(data_ima,CPL_TYPE_DOUBLE);
5720 errs_tmp=cpl_image_cast(errs_ima,CPL_TYPE_DOUBLE);
5726 bin_size,gain,
exptime,airmass,1,
5727 tbl_atm_ext,&errs_nrm_tmp));
5731 check(data_nrm=cpl_image_cast(data_nrm_tmp,CPL_TYPE_FLOAT));
5732 check(errs_nrm=cpl_image_cast(errs_nrm_tmp,CPL_TYPE_FLOAT));
5748 check(qual_vec=cpl_vector_load(name_s,ext+2));
5784 return cpl_error_get_code();
5808 const cpl_frame *atm_ext_frame,
5809 cpl_boolean correct_binning,
5814 cpl_frame* result=NULL;
5815 const char* name_s=NULL;
5816 const char* aname=NULL;
5818 cpl_table* tbl_atm_ext=NULL;
5819 cpl_propertylist* hdat=NULL;
5830 name_s=cpl_frame_get_filename(obj_frame);
5831 aname=cpl_frame_get_filename(atm_ext_frame);
5832 tbl_atm_ext=cpl_table_load(aname,1,0);
5834 cpl_table_cast_column( tbl_atm_ext,
"LAMBDA",
"D_LAMBDA",CPL_TYPE_DOUBLE);
5838 hdat=cpl_propertylist_load(name_s,0);
5847 if (correct_binning) {
5855 name_o=cpl_sprintf(
"%s.fits",tag_o);
5858 exptime,airmass,tbl_atm_ext));
5860 result=
xsh_frame_product(name_o,tag_o,CPL_FRAME_TYPE_IMAGE,CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
5894 const cpl_frame *atm_ext_frame,
5895 cpl_boolean correct_binning,
5900 cpl_frame* result=NULL;
5901 const char* name_s=NULL;
5902 const char* aname=NULL;
5904 cpl_table* tbl_atm_ext=NULL;
5905 cpl_propertylist* hdat=NULL;
5920 next=cpl_frame_get_nextensions( obj_frame);
5921 name_s=cpl_frame_get_filename(obj_frame);
5923 aname=cpl_frame_get_filename(atm_ext_frame);
5924 tbl_atm_ext=cpl_table_load(aname,1,0);
5925 check(cpl_table_cast_column( tbl_atm_ext,
"LAMBDA",
"D_LAMBDA",CPL_TYPE_DOUBLE));
5927 xsh_msg_warning(
"You are using an obsolete atm extinction line table");
5932 name_o=cpl_sprintf(
"%s.fits",tag_o);
5934 hdat=cpl_propertylist_load(name_s,0);
5954 for(ext=0;ext<next;ext+=3) {
5956 exptime,airmass,tbl_atm_ext));
5959 result=
xsh_frame_product(name_o,tag_o,CPL_FRAME_TYPE_IMAGE,CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
5997 const cpl_image *spectrum_error,
5998 const cpl_propertylist *spectrum_header,
6002 const double airmass,
6004 const cpl_table *atm_extinction,
6005 cpl_image **scaled_error)
6007 cpl_image *scaled = NULL;
6008 int norders,
ny,
nx;
6009 double cor_fct=gain*
exptime*bin_size;
6016 nx = cpl_image_get_size_x(spectrum);
6017 ny = cpl_image_get_size_y(spectrum);
6020 if (spectrum_error != NULL)
6022 assure(
nx == cpl_image_get_size_x(spectrum_error) &&
6023 ny == cpl_image_get_size_y(spectrum_error), CPL_ERROR_INCOMPATIBLE_INPUT,
6024 "Error spectrum geometry differs from spectrum: %" CPL_SIZE_FORMAT
"x%" CPL_SIZE_FORMAT
" vs. %dx%d",
6025 cpl_image_get_size_x(spectrum_error),
6026 cpl_image_get_size_y(spectrum_error),
6030 assure(
ny % n_traces == 0, CPL_ERROR_INCOMPATIBLE_INPUT,
6031 "Spectrum image height (%d) is not a multiple of "
6032 "the number of traces (%d). Confused, bailing out",
6035 norders =
ny / n_traces;
6041 assure( gain > 0, CPL_ERROR_ILLEGAL_INPUT,
"Non-positive gain: %f", gain);
6042 assure( bin_size > 0, CPL_ERROR_ILLEGAL_INPUT,
"Illegal binning: %d", bin_size);
6046 check_msg(scaled=cpl_image_divide_scalar_create(spectrum,cor_fct),
6047 "Error correcting spectrum for gain, exposure time, binning");
6049 if (scaled_error != NULL)
6051 check_msg( *scaled_error=cpl_image_divide_scalar_create(spectrum_error,
6053 "Error correcting rebinned spectrum for gain, exposure time, binning");
6060 double dlambda, lambda_start;
6065 "Error reading bin width from header");
6078 "Error reading start wavelength from header");
6084 "Error reading start wavelength from header");
6087 for (trace = 1; trace <= n_traces; trace++)
6089 int spectrum_row = (
order - 1)*n_traces + trace;
6092 for (
x = 1;
x <=
nx;
x++)
6101 lambda = lambda_start + (
x-1) * dlambda;
6103 flux = cpl_image_get(scaled,
x, spectrum_row, &pis_rejected1);
6104 if (scaled_error != NULL)
6106 dflux = cpl_image_get(*scaled_error,
x,
6107 spectrum_row, &pis_rejected2);
6110 if (!pis_rejected1 && (scaled_error == NULL || !pis_rejected2))
6117 lambda, atm_extinction,
6118 "D_LAMBDA",
"D_EXTINCTION", &istart),
6119 "Error interpolating extinction coefficient");
6132 scaled,
x, spectrum_row,
6133 flux * pow(10, 0.4 * extinction * airmass));
6134 if (scaled_error != NULL)
6137 *scaled_error,
x, spectrum_row,
6138 dflux * pow(10, 0.4 * extinction * airmass));
6143 cpl_image_reject(scaled,
x, spectrum_row);
6144 if (scaled_error != NULL)
6146 cpl_image_reject(*scaled_error,
x, spectrum_row);
6157 if (cpl_error_get_code() != CPL_ERROR_NONE)
6160 if (scaled_error != NULL)
6171int* i_inf,
int* i_sup)
6177 double x1=data_x[*i_inf];
6178 double x2=data_x[*i_sup];
6179 double y1=data_y[*i_inf];
6180 double y2=data_y[*i_sup];
6186 for(i=1;i<ndata-1;i++) {
6199 y=(y2-y1)/(x2-x1)*(
x-x1)+y1;
6219 cpl_frame* result=NULL;
6220 cpl_table* table_i=NULL;
6221 cpl_table* table_o=NULL;
6222 const char* name_i=NULL;
6223 const char* tag_i=NULL;
6228 cpl_propertylist* plist=NULL;
6233 double* pwave_i=NULL;
6234 double* pflux_i=NULL;
6235 double* pwave_o=NULL;
6236 double* pflux_o=NULL;
6246 name_i=cpl_frame_get_filename(table_frame);
6247 tag_i=cpl_frame_get_tag(table_frame);
6249 check(table_i=cpl_table_load(name_i,1,0));
6250 nrows_i=cpl_table_get_nrow(table_i);
6251 plist=cpl_propertylist_load(name_i,0);
6252 nrows_o=(int)((wmax-wmin)/wstep+0.5);
6253 table_o=cpl_table_new(nrows_o);
6254 cpl_table_new_column(table_o,
"LAMBDA",CPL_TYPE_DOUBLE);
6255 cpl_table_new_column(table_o,
"FLUX",CPL_TYPE_DOUBLE);
6256 check(cpl_table_fill_column_window_double(table_o,
"LAMBDA",0,nrows_o,0.));
6257 check(cpl_table_fill_column_window_double(table_o,
"FLUX",0,nrows_o,0.));
6258 check(pwave_i=cpl_table_get_data_double(table_i,
"LAMBDA"));
6259 check(pflux_i=cpl_table_get_data_double(table_i,
"FLUX"));
6260 check(pwave_o=cpl_table_get_data_double(table_o,
"LAMBDA"));
6261 check(pflux_o=cpl_table_get_data_double(table_o,
"FLUX"));
6265 for (row = 0; row < nrows_o; row++)
6268 pwave_o[row]= wmin + row * wstep;
6274 tag_o=cpl_sprintf(
"INTERPOL_%s",tag_i);
6275 name_o=cpl_sprintf(
"INTERPOL_%s.fits",tag_i);
6277 check(cpl_table_save(table_o,plist,NULL,name_o,CPL_IO_DEFAULT));
6279 CPL_FRAME_GROUP_PRODUCT,
6280 CPL_FRAME_LEVEL_FINAL));
6312 cpl_frame* result=NULL;
6313 cpl_table* table_i=NULL;
6314 cpl_table* table_o=NULL;
6315 const char* name_i=NULL;
6316 const char* tag_i=NULL;
6321 cpl_propertylist* plist=NULL;
6335 name_i=cpl_frame_get_filename(table_frame);
6337 tag_i=cpl_frame_get_tag(table_frame);
6338 check(table_i=cpl_table_load(name_i,1,0));
6339 flux_med=cpl_table_get_column_median(table_i,
"FLUX");
6340 cpl_table_divide_scalar(table_i,
"FLUX",flux_med);
6342 plist=cpl_propertylist_load(name_i,0);
6343 nrows=(int)((wmax-wmin)/wstep+0.5);
6344 table_o=cpl_table_new(nrows);
6346 cpl_table_new_column(table_o,
"LAMBDA",CPL_TYPE_DOUBLE);
6348 cpl_table_new_column(table_o,
"FLUX",CPL_TYPE_DOUBLE);
6350 check(pwave=cpl_table_get_data_double(table_o,
"LAMBDA"));
6352 check(pflux=cpl_table_get_data_double(table_o,
"FLUX"));
6355 check(cpl_table_fill_column_window_double(table_o,
"LAMBDA",0,nrows,0.));
6356 check(cpl_table_fill_column_window_double(table_o,
"FLUX",0,nrows,0.));
6358 check(pwave=cpl_table_get_data_double(table_o,
"LAMBDA"));
6359 check(pflux=cpl_table_get_data_double(table_o,
"FLUX"));
6362 for (row = 0; row < nrows; row++)
6365 wave = wmin + row * wstep;
6368 "LAMBDA",
"FLUX", &istart),
6369 "Error interpolating curve at lambda = %f wlu", wave);
6376 cpl_table_multiply_scalar(table_i,
"FLUX",flux_med);
6377 cpl_table_multiply_scalar(table_o,
"FLUX",flux_med);
6379 tag_o=cpl_sprintf(
"INTERPOL_%s",tag_i);
6380 name_o=cpl_sprintf(
"INTERPOL_%s.fits",tag_i);
6382 check(cpl_table_save(table_o,plist,NULL,name_o,CPL_IO_DEFAULT));
6384 CPL_FRAME_GROUP_PRODUCT,
6385 CPL_FRAME_LEVEL_FINAL));
6426 int nb_rejected = 0, total_rejected=0;
6433 check( mean_val = cpl_array_get_mean( array));
6435 check(
size = cpl_array_get_size( array));
6443 check(pval=cpl_array_get_data_double(array));
6444 for( i=1; i<=
niter; i++) {
6447 for( j=0; j<
size; j++){
6449 if ( flags[j] == 0 && fabs( pval[j]-mean_val) >
kappa*
sigma){
6452 check( cpl_array_set_invalid( array, j));
6455 if ( nb_rejected == 0){
6456 xsh_msg(
"No more points are rejected. Iterations are stopped.");
6459 total_rejected += nb_rejected;
6460 frac = 1-(double)total_rejected/(
double)
size;
6461 if (frac < frac_min){
6462 xsh_msg(
"Minimal fraction of accepted points %f is reached (%f). Iterations are stopped",
6467 check( mean_val = cpl_array_get_mean( array));
6469 xsh_msg(
"Accepted fraction %f Mean %f sigma %f", frac, mean_val,
sigma);
6472 xsh_msg(
"End of clipping : Mean %f sigma %f", mean_val,
sigma);
6504 int nb_rejected = 0, total_rejected=0;
6511 check( median_val = cpl_array_get_median( array));
6513 check(
size = cpl_array_get_size( array));
6518 xsh_msg(
"Accepted fraction %f Median %f sigma %f", 1.0, median_val,
sigma);
6519 check(pval=cpl_array_get_data_double(array));
6520 for( i=1; i<=
niter; i++) {
6523 for( j=0; j<
size; j++){
6525 if ( flags[j] == 0 && fabs( pval[j]-median_val) >
kappa*
sigma){
6528 check( cpl_array_set_invalid( array, j));
6531 if ( nb_rejected == 0){
6532 xsh_msg(
"No more points are rejected. Iterations are stopped.");
6535 total_rejected += nb_rejected;
6536 frac = 1-(double)total_rejected/(
double)
size;
6537 if (frac < frac_min){
6538 xsh_msg(
"Minimal fraction of accepted points %f is reached (%f). Iterations are stopped",
6542 check( median_val = cpl_array_get_median( array));
6544 xsh_msg(
"Accepted fraction %f Median %f sigma %f", frac, median_val,
sigma);
6546 xsh_msg(
"End of clipping : Median %f sigma %f", median_val,
sigma);
6547 *median = median_val;
6569 cpl_vector *val_vect,
6574 cpl_polynomial **polyp,
6581 int nb_rejected = 0, total_rejected=0;
6582 cpl_polynomial *poly = NULL;
6583 cpl_vector *temp_pos = NULL, *temp_val = NULL;
6584 double *positions = NULL;
6585 double *values = NULL;
6596 check(
size = cpl_vector_get_size( pos_vect));
6603 sigma = sqrt(*chisq);
6609 for( i=1; i<=
niter; i++) {
6613 for( j=0; j<
size; j++){
6614 double pos=0.0, val=0.0, pol_val=0.0;
6616 check( pos = cpl_vector_get( pos_vect, j));
6617 check( val = cpl_vector_get( val_vect, j));
6618 check( pol_val = cpl_polynomial_eval_1d( poly, pos, NULL));
6620 if ( flags[j] == 0 && fabs( pol_val-val) >
kappa*
sigma){
6625 if ( nb_rejected == 0){
6629 total_rejected += nb_rejected;
6630 frac = 1-(double)total_rejected/(
double)
size;
6631 if (frac < frac_min){
6632 xsh_msg(
"Minimal fraction of accepted points %f is reached (%f). Iterations are stopped",
6638 for( j=0; j<
size; j++){
6640 positions[ngood] = cpl_vector_get( pos_vect, j);
6641 values[ngood] = cpl_vector_get( val_vect, j);
6648 check( temp_pos = cpl_vector_wrap( ngood, positions));
6649 check( temp_val = cpl_vector_wrap( ngood, values));
6656 sigma = sqrt(*chisq);
6664 for( j=0; j<
size; j++){
6686 cpl_parameter* p=NULL;
6689 if(cpl_parameter_get_double(p) <= 0) {
6702 if(cpl_parameter_get_double(p) <= 0) {
6717 return cpl_error_get_code();
6728 cpl_parameter* p=NULL;
6731 if (cpl_parameter_get_int(p) < 0) {
6744 return cpl_error_get_code();
6763 double *xtab = ((
struct data *)
data)->x;
6765 double area = gsl_vector_get (
x, 0);
6766 double a = gsl_vector_get (
x, 1);
6767 double b = gsl_vector_get(
x,2);
6768 double c = gsl_vector_get(
x,3);
6769 double x0 = gsl_vector_get(
x,4);
6770 double sigma = gsl_vector_get(
x,5);
6772 for (i = 0; i <
n; i++)
6779 double Yi = height*exp(-W)+a+b*
t+c*
t*
t;
6781 gsl_vector_set (f, i, Yi -
y[i]);
6793 double *xtab = ((
struct data *)
data)->x;
6795 double area = gsl_vector_get (
x, 0);
6796 double x0 = gsl_vector_get (
x, 4);
6797 double sigma = gsl_vector_get(
x,5);
6799 for (i = 0; i <
n; i++)
6813 gsl_matrix_set (J, i, 0, e);
6817 gsl_matrix_set (J, i, 1, e);
6826 gsl_matrix_set (J, i, 2, e);
6835 gsl_matrix_set (J, i, 3, e);
6839 gsl_matrix_set (J, i, 4, e);
6843 gsl_matrix_set (J, i, 5, e);
6851 gsl_vector * f, gsl_matrix * J)
6865 double flux_max, flux_min, intensity;
6867 double init_area, init_sigma, init_x0=0, init_offset;
6868 double flux_sum =0.0;
6869 double hflux, flux25, flux75;
6870 double init25=0, init75=0;
6875 size = cpl_vector_get_size( xpos_vect);
6876 flux_min = cpl_vector_get_min( ypos_vect);
6877 flux_max = cpl_vector_get_max( ypos_vect);
6878 intensity = flux_max-flux_min;
6880 init_offset = flux_min;
6882 for (i = 0; i <
size; i++){
6885 yval = cpl_vector_get( ypos_vect, i);
6886 flux_sum += yval-init_offset;
6889 hflux = flux_sum/2.0;
6890 flux25 = flux_sum*0.25;
6891 flux75 = flux_sum*0.75;
6894 for (i = 0; i <
size; i++){
6897 yval = cpl_vector_get( ypos_vect, i);
6899 flux_sum += yval-init_offset;
6901 if ( (init25 == 0) && flux_sum > flux25){
6902 init25 = (i+i-1)/2.0;
6905 if ( (init_x0 == 0) && flux_sum > hflux){
6906 init_x0 = (i+i-1)/2.0;
6909 if ( (init75 == 0) && flux_sum > flux75){
6910 init75 = (i+i-1)/2.0;
6916 init_sigma = (init75-init25)/(2*0.6744);
6917 init_area = intensity*sqrt(2*
M_PI*init_sigma*init_sigma);
6920 init_area, init_x0, init_sigma, init_offset);
6922 init_par[0] = init_area;
6923 init_par[1] = init_offset;
6926 init_par[4] = init_x0;
6927 init_par[5] = init_sigma;
6938 double *params,
double *errs,
int *status)
6940 const gsl_multifit_fdfsolver_type *T;
6941 gsl_multifit_fdfsolver *
s=NULL;
6944 gsl_matrix *covar = gsl_matrix_alloc (p, p);
6947#if defined GSL_MAJOR_VERSION && GSL_MAJOR_VERSION >= 2
6951 struct data d = {
n, NULL, NULL,
deg};
6952 gsl_multifit_function_fdf f;
6953 gsl_vector*
x = NULL;
6954 double area, offset, b, c, x0,
sigma;
6955 double chi, dof, cte;
6957 double *xpos = NULL;
6958 double *ypos = NULL;
6968 size = cpl_vector_get_size( xpos_vect);
6969 xpos = cpl_vector_get_data( xpos_vect);
6970 ypos = cpl_vector_get_data( ypos_vect);
6972 x = gsl_vector_calloc (p);
6981 gsl_vector_set(
x, 0, area);
6982 gsl_vector_set(
x, 1, offset);
6983 gsl_vector_set(
x, 2, b);
6984 gsl_vector_set(
x, 3, c);
6985 gsl_vector_set(
x, 4, x0);
6986 gsl_vector_set(
x, 5,
sigma);
7002 T = gsl_multifit_fdfsolver_lmsder;
7003 s = gsl_multifit_fdfsolver_alloc (T,
n, p);
7004 gsl_multifit_fdfsolver_set (
s, &f,
x);
7006 xsh_msg_dbg_high (
"iter: %3u area % 15.8f a % 15.8f b % 15.8f c % 15.8f x0 % 15.8f sigma % 15.8f |f(x)| = %g\n",
7008 gsl_vector_get (
s->
x, 0),
7009 gsl_vector_get (
s->
x, 1),
7010 gsl_vector_get (
s->
x, 2),
7011 gsl_vector_get (
s->
x, 3),
7012 gsl_vector_get (
s->
x, 4),
7013 gsl_vector_get (
s->
x, 5),
7014 gsl_blas_dnrm2 (
s->f));
7018 *status = gsl_multifit_fdfsolver_iterate (
s);
7020 xsh_msg_dbg_high (
"iter: %3u area % 15.8f a % 15.8f b % 15.8f c % 15.8f x0 % 15.8f sigma % 15.8f |f(x)| = %g\n",
7022 gsl_vector_get (
s->
x, 0),
7023 gsl_vector_get (
s->
x, 1),
7024 gsl_vector_get (
s->
x, 2),
7025 gsl_vector_get (
s->
x, 3),
7026 gsl_vector_get (
s->
x, 4),
7027 gsl_vector_get (
s->
x, 5),
7028 gsl_blas_dnrm2 (
s->f));
7033 *status = gsl_multifit_test_delta (
s->dx,
s->
x,
7037 while ( *status == GSL_CONTINUE && iter < 500);
7039#if defined GSL_MAJOR_VERSION && GSL_MAJOR_VERSION >= 2
7040 J = gsl_matrix_alloc(
n, p);
7041 gsl_multifit_fdfsolver_jac(
s, J);
7042 gsl_multifit_covar (J, 0.0, covar);
7043 gsl_matrix_free (J);
7045 gsl_multifit_covar (
s->J, 0.0, covar);
7049 params[0] = gsl_vector_get(
s->
x, 0);
7050 params[1] = gsl_vector_get(
s->
x, 1);
7051 params[2] = gsl_vector_get(
s->
x, 2);
7052 params[3] = gsl_vector_get(
s->
x, 3);
7053 params[4] = gsl_vector_get(
s->
x, 4);
7054 params[5] = gsl_vector_get(
s->
x, 5);
7056 chi = gsl_blas_dnrm2(
s->f);
7058 cte = GSL_MAX_DBL(1, chi / sqrt(dof));
7060 errs[0] = cte * sqrt(gsl_matrix_get(covar,0,0));
7061 errs[1] = cte * sqrt(gsl_matrix_get(covar,1,1));
7062 errs[2] = cte * sqrt(gsl_matrix_get(covar,2,2));
7063 errs[3] = cte * sqrt(gsl_matrix_get(covar,3,3));
7064 errs[4] = cte * sqrt(gsl_matrix_get(covar,4,4));
7065 errs[5] = cte * sqrt(gsl_matrix_get(covar,5,5));
7070 gsl_multifit_fdfsolver_free (
s);
7071 gsl_matrix_free (covar);
7072 gsl_vector_free(
x);
7113 double mean_i, mean_t ;
7114 double rms_i, rms_t ;
7122 int STEP_MIN=-half_search;
7123 int STEP_MAX=half_search;
7126 sum = sqsum = 0.00 ;
7127 for (i=0 ; i<width_i ; i++) {
7129 sqsum += line_i[i] * line_i[i];
7132 mean_i = sum / (double) width_i ;
7133 sqsum /= (double)width_i ;
7134 rms_i = sqsum - mean_i*mean_i ;
7136 sum = sqsum = 0.00 ;
7137 for (i=0 ; i<width_t ; i++) {
7139 sqsum += line_t[i] * line_t[i];
7141 mean_t = sum / (double)width_t ;
7142 sqsum /= (double)width_t ;
7143 rms_t = sqsum - mean_t*mean_t ;
7145 norm = 1.00 / sqrt(rms_i * rms_t);
7147 nsteps = (STEP_MAX - STEP_MIN) +1 ;
7148 xcorr = cpl_malloc(nsteps *
sizeof(
double));
7154 xcorr[
step-STEP_MIN] = 0.00 ;
7156 for (i=0 ; i<width_t ; i++) {
7158 (i+
step < width_i)) {
7159 xcorr[
step-STEP_MIN] += (line_t[i] - mean_t) *
7160 (line_i[i+
step] - mean_i) *
7165 xcorr[
step-STEP_MIN] /= (double) nval ;
7168 *xcorr_max = xcorr[0] ;
7170 for (i=0 ; i<nsteps ; i++) {
7171 if (xcorr[i]>(*xcorr_max)) {
7173 *xcorr_max = xcorr[i];
7177 cpl_vector* vcor=cpl_vector_wrap(nsteps,xcorr);
7178 double a=xcorr[maxpos-1];
7179 double b=xcorr[maxpos+1];
7180 double c=xcorr[maxpos];
7181 double fraction=(a-b)/(2.*a+2.*b-4.*c);
7183 cpl_vector_unwrap(vcor);
7187 (*delta) = (double)STEP_MIN + (
double)maxpos;
7198 cpl_propertylist* p1 = cpl_propertylist_load(cpl_frame_get_filename(f1),0);
7199 cpl_propertylist* p2 = cpl_propertylist_load(cpl_frame_get_filename(f2),0);
7200 if(p1 != NULL && p2 != NULL && cpl_propertylist_has(p1,
"MJD-OBS") && cpl_propertylist_has(p2,
"MJD-OBS")){
7201 double mjd1 = cpl_propertylist_get_double(p1,
"MJD-OBS");
7202 double mjd2 = cpl_propertylist_get_double(p2,
"MJD-OBS");
7205 }
else if (mjd1 > mjd2){
7210 cpl_propertylist_delete(p1);
7211 cpl_propertylist_delete(p2);
7218 cpl_frameset* fraws = cpl_frameset_duplicate(fset);
7221 int nraws = cpl_frameset_get_size(fraws);
7226 for (
int f = 0; f < nraws; f++) {
7227 cpl_frame* frm = cpl_frameset_get_frame(fset, f);
7228 const char* fname = cpl_frame_get_filename(frm);
7229 cpl_image* img = cpl_image_load(fname,CPL_TYPE_DOUBLE,0,0);
7230 cpl_propertylist* hdr = cpl_propertylist_load(fname,0);
7232 double thresh = 60000.0;
7237 double* idata = cpl_image_get_data_double(img);
7239 int nx = cpl_image_get_size_x(img);
7240 int ny = cpl_image_get_size_y(img);
7241 for(
int i=0;i<
nx*
ny;i++){
7242 if(idata[i] > thresh){
7246 if(nsat > nsat_max){
7250 cpl_image_delete(img);
7251 cpl_propertylist_delete(hdr);
7254 cpl_propertylist_update_int(qclist,
"ESO QC NUM SAT",nsat_max);
7257 cpl_frame* raw1 = cpl_frameset_get_frame(fraws,0);
7258 cpl_frame* raw2 = cpl_frameset_get_frame(fraws,nraws-1);
7259 cpl_propertylist* rhdr1 = cpl_propertylist_load(cpl_frame_get_filename(raw1),0);
7260 cpl_propertylist* rhdr2 = cpl_propertylist_load(cpl_frame_get_filename(raw2),0);
7261 if (rhdr1 != NULL && rhdr2 != NULL){
7262 if(cpl_propertylist_has(rhdr1,
"ESO TEL AIRM START") && cpl_propertylist_has(rhdr2,
"ESO TEL AIRM END")){
7263 cpl_propertylist_update_double(qclist,
"ESO QC AIRM AVG",
7264 0.5*(cpl_propertylist_get_double(rhdr1,
"ESO TEL AIRM START") + cpl_propertylist_get_double(rhdr2,
"ESO TEL AIRM END")));
7266 if(cpl_propertylist_has(rhdr1,
"ESO TEL AIRM START") && !cpl_propertylist_has(rhdr2,
"ESO TEL AIRM END")){
7267 cpl_propertylist_update_double(qclist,
"ESO QC AIRM AVG",cpl_propertylist_get_double(rhdr1,
"ESO TEL AIRM START"));
7269 if(!cpl_propertylist_has(rhdr1,
"ESO TEL AIRM START") && cpl_propertylist_has(rhdr2,
"ESO TEL AIRM END")){
7270 cpl_propertylist_update_double(qclist,
"ESO QC AIRM AVG",cpl_propertylist_get_double(rhdr2,
"ESO TEL AIRM END"));
7273 if(cpl_propertylist_has(rhdr1,
"ESO TEL AMBI IWV START") && cpl_propertylist_has(rhdr2,
"ESO TEL AMBI IWV END")){
7274 cpl_propertylist_update_double(qclist,
"ESO QC IWV AVG",
7275 0.5*(cpl_propertylist_get_double(rhdr1,
"ESO TEL AMBI IWV START") + cpl_propertylist_get_double(rhdr2,
"ESO TEL AMBI IWV END")));
7277 if(cpl_propertylist_has(rhdr1,
"ESO TEL AMBI IWV START") && !cpl_propertylist_has(rhdr2,
"ESO TEL AMBI IWV END")){
7278 cpl_propertylist_update_double(qclist,
"ESO QC IWV AVG",cpl_propertylist_get_double(rhdr1,
"ESO TEL AMBI IWV START"));
7280 if(!cpl_propertylist_has(rhdr1,
"ESO TEL AMBI IWV START") && cpl_propertylist_has(rhdr2,
"ESO TEL AMBI IWV END")){
7281 cpl_propertylist_update_double(qclist,
"ESO QC IWV AVG",cpl_propertylist_get_double(rhdr2,
"ESO TEL AMBI IWV END"));
7285 if(cpl_propertylist_has(rhdr1,
"ESO TEL AMBI FWHM START") && cpl_propertylist_has(rhdr2,
"ESO TEL AMBI FWHM END")){
7286 cpl_propertylist_update_double(qclist,
"ESO QC FWHM AVG",
7287 0.5*(cpl_propertylist_get_double(rhdr1,
"ESO TEL AMBI FWHM START") + cpl_propertylist_get_double(rhdr2,
"ESO TEL AMBI FWHM END")));
7289 if(cpl_propertylist_has(rhdr1,
"ESO TEL AMBI FWHM START") && !cpl_propertylist_has(rhdr2,
"ESO TEL AMBI FWHM END")){
7290 cpl_propertylist_update_double(qclist,
"ESO QC FWHM AVG",cpl_propertylist_get_double(rhdr1,
"ESO TEL AMBI FWHM START"));
7292 if(!cpl_propertylist_has(rhdr1,
"ESO TEL AMBI FWHM START") && cpl_propertylist_has(rhdr2,
"ESO TEL AMBI FWHM END")){
7293 cpl_propertylist_update_double(qclist,
"ESO QC FWHM AVG",cpl_propertylist_get_double(rhdr2,
"ESO TEL AMBI FWHM END"));
7298 cpl_propertylist_delete(rhdr1);
7299 cpl_propertylist_delete(rhdr2);
7301 cpl_frameset_delete(fraws);
7303 return cpl_error_get_code();
7308 cpl_propertylist* ehdr = cpl_propertylist_load(cpl_frame_get_filename(eff),0);
7309 if(ehdr != NULL && cpl_propertylist_has(ehdr,
"ESO QC EFF MED ORD 1") && cpl_propertylist_has(ehdr,
"ESO QC EFF MED ORD 9")){
7310 double ord1 = cpl_propertylist_get_double(ehdr,
"ESO QC EFF MED ORD 1");
7311 double ord9 = cpl_propertylist_get_double(ehdr,
"ESO QC EFF MED ORD 9");
7313 cpl_propertylist_update_double(qclist,
"ESO QC CHROMATIC EFF",ord9/ord1);
7316 cpl_propertylist_delete(ehdr);
7318 return cpl_error_get_code();
7329 int xpos[2] = {2000,12000};
7333 int dw = cpl_image_get_size_x(im);
7334 int dy = cpl_image_get_size_y(im);
7337 double xcen = dw*0.5;
7352 cpl_error_code err = CPL_ERROR_NONE;
7353 for (
int idx=0;idx < 2;idx++){
7354 llx = xpos[idx]-wx*0.5;
7355 urx = xpos[idx]+wx*0.5;
7358 lly = (int)(dy*0.5-15);
7359 ury = (int)(dy*0.5+15);
7362 cpl_image* windowed = cpl_image_extract(im,
llx,
lly,
urx,
ury);
7363 cpl_image* wim = cpl_image_collapse_median_create(windowed,1,0,0);
7364 cpl_image_delete(windowed);
7366 double* wdata = cpl_image_get_data_double(wim);
7368 cpl_msg_info(cpl_func,
"failed to load data from image window");
7372 int wdata_dy = cpl_image_get_size_y(wim);
7374 cpl_vector* sx = cpl_vector_new(wdata_dy);
7375 cpl_vector* sy = cpl_vector_wrap(wdata_dy,wdata);
7377 for (
int i=0;i<wdata_dy;i++){
7378 cpl_vector_set(sx,i,i);
7380 double cenpos=0.5*wdata_dy;
7385 err = cpl_vector_fit_gaussian(sx,NULL,sy,NULL,CPL_FIT_ALL,&cenpos,&
sigma,&area,&offset,NULL,NULL,NULL);
7388 ypos[idx] = cenpos+
lly;
7389 cpl_vector_delete(sx);
7390 cpl_vector_unwrap(sy);
7391 cpl_image_delete(wim);
7394 if(err == CPL_ERROR_NONE) {
7396 double b = (ypos[1]-ypos[0])/(xpos[1]-xpos[0]);
7397 double a = ypos[0]-b*(xpos[0]-xcen);
7398 cpl_propertylist_update_double(qclist,
"ESO QC CURVE 0",a);
7399 cpl_propertylist_update_double(qclist,
"ESO QC CURVE 1",b);
7400 cpl_propertylist_update_double(qclist,
"ESO QC CURVE X",xcen);
7404 return cpl_error_get_code();
7414 *f = p[0] + p[1]*(
x[0]-p[2]);
7431 int dy = cpl_image_get_size_y(im);
7433 int drop_ll = (dy*0.5)-10;
7434 int drop_ur = dy-((dy*0.5)+10);
7435 cpl_image* dup = cpl_image_duplicate(im);
7436 cpl_image* slit = cpl_image_collapse_median_create(dup,0,drop_ll,drop_ur);
7438 float * sdata = cpl_image_get_data_float(slit);
7439 int coord0 = (xa2-xa1)/2;
7440 int nvals = xa2-xa1;
7441 cpl_vector* sy = cpl_vector_new(nvals);
7442 cpl_vector* sye = cpl_vector_new(nvals);
7443 cpl_vector* sx = cpl_vector_new(nvals);
7446 for (
int i=xa1;i<xa2;i++){
7447 cpl_vector_set(sx,count,i);
7448 cpl_vector_set(sy,count,sdata[i]);
7450 cpl_vector_set(sye,count,1.0);
7454 cpl_vector* sycalc = cpl_vector_duplicate(sy);
7455 double mean = cpl_vector_get_mean(sycalc);
7456 cpl_vector_subtract_scalar(sycalc,mean);
7457 cpl_vector_multiply(sycalc,sycalc);
7458 double rms = sqrt(cpl_vector_get_mean(sycalc));
7460 cpl_propertylist_append_double(qclist,
"ESO QC FLAT SLIT RMS",rms);
7462 cpl_matrix* coeff = cpl_matrix_new(nvals,1);
7463 for (
int i=0;i<nvals;i++){
7464 cpl_matrix_set(coeff,i,0,cpl_vector_get(sx,i));
7467 cpl_matrix* covar = cpl_matrix_new(3,3);
7470 cpl_vector* guess = cpl_vector_new(3);
7471 cpl_vector_set(guess,0,0);
7472 cpl_vector_set(guess,1,0);
7473 cpl_vector_set(guess,2,coord0);
7475 int ia[3] = {1,1,0};
7476 cpl_error_code err= cpl_fit_lvmq(coeff,NULL,sy,sye,guess,ia,
qc_linfunc,
qc_dlinfunc,CPL_FIT_LVMQ_TOLERANCE,CPL_FIT_LVMQ_COUNT,CPL_FIT_LVMQ_MAXITER,NULL,&chi_sq,&covar);
7491 double grad_err_other = sqrt(cpl_matrix_get(covar,1,1)* (chi_sq));
7492 double grad = cpl_vector_get(guess,1);
7494 cpl_propertylist_append_double(qclist,
"ESO QC FLAT SLIT B",grad);
7495 cpl_propertylist_append_double(qclist,
"ESO QC FLAT SLIT DB",grad_err_other);
7497 cpl_image_delete(slit);
7500 return cpl_error_get_code();
7508 cpl_table* mtab2 = cpl_table_duplicate(mtab);
7509 cpl_table_new_column(mtab2,
"SIGMA",CPL_TYPE_DOUBLE);
7510 cpl_size mrows = cpl_table_get_nrow(mtab2);
7513 cpl_size rrows = cpl_table_get_nrow(rtab);
7517 return CPL_ERROR_NONE;
7522 float* tmplam = cpl_table_get_data_float(mtab2,
"LAMBDA");
7524 cpl_table_select_all(mtab2);
7525 cpl_table_select_all(rtab);
7529 for(
int i=0;i<mrows;i++){
7530 cpl_table_set_double(mtab2,
"SIGMA",i,1.0);
7533 if(i < lower || i > upper ){
7534 cpl_table_set_double(mtab2,
"SIGMA",i,1e20);
7535 cpl_table_unselect_row(mtab2,i);
7536 cpl_table_unselect_row(rtab,i);
7553 cpl_table_set_double(mtab2,
"SIGMA",i,1e20);
7555 cpl_table_unselect_row(mtab2,i);
7556 cpl_table_unselect_row(rtab,i);
7560 if( (tmplam[i] > 1350 && tmplam[i] < 1480) || (tmplam[i] > 1770 && tmplam[i] < 2080)){
7562 cpl_table_set_double(mtab2,
"SIGMA",i,1e20);
7570 mtab2 = cpl_table_extract_selected(mtab2);
7571 rtab = cpl_table_extract_selected(rtab);
7573 mrows = cpl_table_get_nrow(mtab2);
7575 float* mlam = cpl_table_get_data_float(mtab2,
"LAMBDA");
7576 double* msigma = cpl_table_get_data_double(mtab2,
"SIGMA");
7577 float* rlam = cpl_table_get_data_float(rtab,
"LAMBDA");
7578 float* mresp = cpl_table_get_data_float(mtab2,
"RESPONSE");
7579 float* rresp = cpl_table_get_data_float(rtab,
"RESPONSE");
7582 cpl_vector* vm_lam = cpl_vector_new(mrows);
7583 cpl_vector* vr_lam = cpl_vector_new(mrows);
7584 cpl_vector* vm_resp= cpl_vector_new(mrows);
7585 cpl_vector* vr_resp= cpl_vector_new(mrows);
7586 cpl_vector*
sigma = cpl_vector_new(mrows);
7588 cpl_vector*
cr = cpl_vector_new(mrows);
7589 for(
int i=0;i<mrows;i++){
7590 cpl_vector_set(vm_lam,i,mlam[i]);
7591 cpl_vector_set(
sigma,i,msigma[i]);
7592 cpl_vector_set(vr_lam,i,rlam[i]);
7593 cpl_vector_set(vm_resp,i,mresp[i]);
7594 cpl_vector_set(vr_resp,i,rresp[i]);
7595 cpl_vector_set(
cr,i,1.0);
7596 if(cpl_vector_get(
cr,i) > 0 && cpl_vector_get(vr_resp,i) > 0){
7597 cpl_vector_set(
cr,i,cpl_vector_get(vm_resp,i)/cpl_vector_get(vr_resp,i));
7600 cpl_vector_set(
cr,i,cpl_vector_get(
cr,i-1));
7602 cpl_vector_set(
sigma,i,1e20);
7606 double lam0 = cpl_vector_get(vm_lam,(
int)(mrows*0.5));
7607 cpl_msg_info(cpl_func,
"lam0: %e\n",lam0);
7610 cpl_vector* guess = cpl_vector_new(3);
7611 cpl_vector_set(guess,0,0);
7612 cpl_vector_set(guess,1,0);
7613 cpl_vector_set(guess,2,lam0);
7615 cpl_matrix* covar = cpl_matrix_new(3,3);
7619 int ia[3] = {1,1,0};
7621 cpl_matrix* coeff = cpl_matrix_new(mrows,1);
7622 for(
int i=0;i<mrows;i++){
7623 cpl_matrix_set(coeff,i,0,cpl_vector_get(vm_lam,i));
7628 cpl_error_code err= cpl_fit_lvmq(coeff,NULL,
cr,
sigma,guess,ia,
qc_linfunc,
qc_dlinfunc,CPL_FIT_LVMQ_TOLERANCE,CPL_FIT_LVMQ_COUNT,CPL_FIT_LVMQ_MAXITER,NULL,&chi_sq,&covar);
7629 double a = cpl_vector_get(guess,0);
7630 double b = cpl_vector_get(guess,1);
7631 double da= sqrt(cpl_matrix_get(covar,0,0)* (chi_sq));
7632 double db= sqrt(cpl_matrix_get(covar,1,1)* (chi_sq));
7636 kw = cpl_sprintf(
"ESO QC RESP SPEC A %s",label);
7637 cpl_propertylist_append_double(qclist,kw,a);
7638 kw = cpl_sprintf(
"ESO QC RESP SPEC B %s",label);
7639 cpl_propertylist_append_double(qclist,kw,b);
7640 kw = cpl_sprintf(
"ESO QC RESP SPEC DA %s",label);
7641 cpl_propertylist_append_double(qclist,kw,da);
7642 kw = cpl_sprintf(
"ESO QC RESP SPEC DB %s",label);
7643 cpl_propertylist_append_double(qclist,kw,db);
7646 cpl_propertylist_append_double(qclist,
"ESO QC RESP SPEC A",a);
7647 cpl_propertylist_append_double(qclist,
"ESO QC RESP SPEC B",b);
7648 cpl_propertylist_append_double(qclist,
"ESO QC RESP SPEC DA",da);
7649 cpl_propertylist_append_double(qclist,
"ESO QC RESP SPEC DB",db);
7651 cpl_table_delete(mtab2);
7653 return cpl_error_get_code();
static xsh_instrument * instrument
int xsh_grid_get_index(xsh_grid *grid)
get the number of elements in the grid
xsh_grid_point * xsh_grid_point_get(xsh_grid *grid, int i)
get x points from the grid
cpl_image * xsh_pre_get_data(xsh_pre *pre)
Get data.
xsh_pre * xsh_pre_duplicate(const xsh_pre *pre)
Copy a PRE structure.
xsh_pre * xsh_pre_load(cpl_frame *frame, xsh_instrument *instr)
Load a xsh_pre structure from a frame.
void xsh_pre_multiply_image(const xsh_pre *pre, cpl_image *img)
multiply a frame in PRE format by an image
cpl_image * xsh_pre_get_qual(xsh_pre *pre)
Get qual.
void xsh_pre_multiply_scalar(const xsh_pre *pre, double x)
multiply a frame in PRE format by a scalar
cpl_image * xsh_pre_abs(const xsh_pre *pre)
computes absolute value of a frame in PRE format
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
cpl_image * xsh_pre_get_errs(xsh_pre *pre)
Get errs.
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
xsh_spectrum * xsh_spectrum_load(cpl_frame *s1d_frame)
Load a 1D spectrum structure.
cpl_frame * xsh_spectrum_save(xsh_spectrum *s, const char *filename, const char *tag)
save a spectrum
void xsh_spectrum_free(xsh_spectrum **s)
free memory associated to an 1D spectrum
cpl_error_code xsh_print_cpl_frameset(cpl_frameset *frames)
Print a frame set.
const char * xsh_tostring_cpl_type(cpl_type t)
Convert a CPL type to a string.
cpl_error_code xsh_print_cpl_frame(const cpl_frame *f)
Print a frame.
#define XSH_ASSURE_NOT_NULL_MSG(pointer, msg)
#define XSH_ASSURE_NOT_ILLEGAL(cond)
#define assure(CONDITION, ERROR_CODE,...)
#define check_msg(COMMAND,...)
#define xsh_error_reset()
#define XSH_ASSURE_NOT_ILLEGAL_MSG(cond, msg)
#define XSH_ASSURE_NOT_NULL(pointer)
#define passure(CONDITION,...)
cpl_error_code xsh_instrument_nir_corr_if_spectral_format_is_JH(cpl_frameset *calib, xsh_instrument *instr)
cpl_error_code xsh_instrument_nir_corr_if_JH(cpl_frameset *raws, xsh_instrument *instr)
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
void xsh_instrument_set_recipe_id(xsh_instrument *instrument, const char *recipe_id)
Set the recipe_id into the instrument structure.
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
void xsh_instrument_set_decode_bp(xsh_instrument *i, const int decode_bp)
Set bad pixel code.
void xsh_msg_init(void)
Initialize messaging.
#define xsh_msg_warning(...)
Print an warning message.
#define xsh_msg_dbg_medium(...)
#define xsh_msg_debug(...)
Print a debug message.
#define xsh_msg_error(...)
Print an error message.
#define xsh_msg(...)
Print a message on info level.
int xsh_msg_get_warnings(void)
Get number of warnings printed so far.
#define xsh_msg_dbg_high(...)
void xsh_pfits_set_cd1(cpl_propertylist *plist, double value)
Write the CD1 value.
void xsh_pfits_set_cd13(cpl_propertylist *plist, double value)
Write the CD1_3 value.
cpl_error_code xsh_plist_set_extra_keys(cpl_propertylist *plist, const char *hduclas1, const char *hduclas2, const char *hduclas3, const char *scidata, const char *errdata, const char *qualdata, const int type)
set hdu keys
const char * xsh_pfits_get_dpr_type(const cpl_propertylist *plist)
find out the DPR TECH
void xsh_pfits_set_cd22(cpl_propertylist *plist, double value)
Write the CD2_2 value.
void xsh_pfits_set_pcatg(cpl_propertylist *plist, const char *value)
Write the PCATG value.
void xsh_pfits_set_cd21(cpl_propertylist *plist, double value)
Write the CD2_1 value.
double xsh_pfits_get_cdelt2(const cpl_propertylist *plist)
find out the cdelt2
double xsh_pfits_get_nod_jitterwidth(const cpl_propertylist *plist)
Get the Jitter Box size.
double xsh_pfits_get_cdelt3(const cpl_propertylist *plist)
find out the cdelt3
int xsh_pfits_get_binx(const cpl_propertylist *plist)
find out the BINX value
void xsh_pfits_set_extname(cpl_propertylist *plist, const char *value)
Write the EXTNAME value.
double xsh_pfits_get_nodthrow(const cpl_propertylist *plist)
Get the Nod Throw value.
void xsh_pfits_set_cd31(cpl_propertylist *plist, double value)
Write the CD3_2 value.
void xsh_pfits_set_cd32(cpl_propertylist *plist, double value)
Write the CD3_2 value.
void xsh_pfits_set_cd23(cpl_propertylist *plist, double value)
Write the CD2_3 value.
const char * xsh_pfits_get_dpr_catg(const cpl_propertylist *plist)
find out the DPR CATG
const char * xsh_pfits_get_date_obs(const cpl_propertylist *plist)
find out the DATE
double xsh_pfits_get_gain(const cpl_propertylist *plist)
find out the GAIN value
double xsh_pfits_get_cdelt1(const cpl_propertylist *plist)
find out the cdelt1
int xsh_pfits_get_biny(const cpl_propertylist *plist)
find out the BINY value
void xsh_pfits_set_bunit(cpl_propertylist *plist, const char *value)
Write the BUNIT value.
void xsh_pfits_set_cd33(cpl_propertylist *plist, double value)
Write the CD3_3 value.
double xsh_pfits_get_exptime(const cpl_propertylist *plist)
find out the exposure time
double xsh_pfits_get_airm_mean(const cpl_propertylist *plist)
find out the mean airmass value
double xsh_pfits_get_crval1(const cpl_propertylist *plist)
find out the crval1
void xsh_pfits_set_cd12(cpl_propertylist *plist, double value)
Write the CD1_2 value.
void xsh_pfits_set_cd11(cpl_propertylist *plist, double value)
Write the CD1_1 value.
int xsh_pfits_get_naxis(const cpl_propertylist *plist)
find out the NAXIS value
double xsh_pfits_get_cumoffsety(const cpl_propertylist *plist)
polynomial * xsh_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.
void xsh_polynomial_delete(polynomial **p)
Delete a polynomial.
double xsh_polynomial_evaluate_2d(const polynomial *p, double x1, double x2)
Evaluate a 2d polynomial.
polynomial * xsh_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_frame * xsh_normalize_spectrum(const cpl_frame *obj_frame, const cpl_frame *atm_ext_frame, cpl_boolean correct_binning, xsh_instrument *instrument, const char *tag_o)
Normalize a spectrum.
int xsh_max_int(int x, int y)
Maximum of two numbers.
#define XSH_FLOAT_SWAP(a, b)
void xsh_unwrap_vector(cpl_vector **v)
Unwrap a vector and set the pointer to NULL.
cpl_frame * xsh_frameset_average(cpl_frameset *set, const char *tag)
Dump propertylist.
void xsh_vector_fit_gaussian(cpl_vector *x, cpl_vector *y, XSH_GAUSSIAN_FIT *result)
set debug level
void xsh_image_fit_spline(cpl_image *img, xsh_grid *grid)
perform spline fit
static void xsh_property_dump(cpl_property *property)
Dump property.
void xsh_free_polynomial(cpl_polynomial **p)
Deallocate a polynomial and set the pointer to NULL.
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
void xsh_free_parameterlist(cpl_parameterlist **p)
Deallocate a parameter list and set the pointer to NULL.
#define XSH_INT_SWAP(a, b)
static int qc_dlinfunc(const double x[], const double p[], double f[])
const char * xsh_set_recipe_sky_file_prefix(char *rec_prefix)
Set recipe sky frames prefix.
static int expb_df(const gsl_vector *x, void *data, gsl_matrix *J)
static int expb_f(const gsl_vector *x, void *data, gsl_vector *f)
cpl_error_code xsh_check_input_is_unbinned(cpl_frame *in)
Check if an input frame is not binned.
cpl_image * xsh_normalize_spectrum_image(const cpl_image *spectrum, const cpl_image *spectrum_error, const cpl_propertylist *spectrum_header, const int bin_size, const double gain, const double exptime, const double airmass, const int n_traces, const cpl_table *atm_extinction, cpl_image **scaled_error)
Normalize a spectrum.
int xsh_time_stamp_set(int ts)
set timestamp
cpl_error_code get_average_qc_from_raws(cpl_frameset *fset, xsh_instrument *instrument, cpl_propertylist *qclist)
int xsh_fileutils_copy(const char *srcpath, const char *dstpath)
char * xsh_stringcat_4(const char *s1, const char *s2, const char *s3, const char *s4)
String concatenation.
const char * xsh_string_tolower(char *s)
Convert all uppercase characters in a string into lowercase characters.
int xsh_select_table_rows(cpl_table *t, const char *column, cpl_table_select_operator operator, double value)
Select table rows.
char * xsh_stringcat_5(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5)
String concatenation.
void xsh_tools_min_max(int size, double *tab, double *min, double *max)
computes min & max in ab array
static int expb_fdf(const gsl_vector *x, void *data, gsl_vector *f, gsl_matrix *J)
cpl_error_code xsh_frameset_dump_nod_info(cpl_frameset *set)
Dump frameset nod info.
const char * xsh_string_toupper(char *s)
Convert all lowercase characters in a string into uppercase characters.
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
int xsh_debug_level_get(void)
get debug level
int xsh_fileutils_move(const char *srcpath, const char *dstpath)
cpl_error_code xsh_update_pheader_in_image_multi(cpl_frame *frame, const cpl_propertylist *pheader)
Update FITS header.
double xsh_spline_hermite(double xp, const double *x, const double *y, int n, int *istart)
Spline interpolation based on Hermite polynomials.
void xsh_unwrap_bivector_vectors(cpl_bivector **b)
Unwrap a bi-vector and set the pointer to NULL.
#define CPL_IMAGE_GET_DATA_CONST
double convert_data_to_bin(double data, int binning)
cpl_error_code xsh_get_property_value(const cpl_propertylist *plist, const char *keyword, cpl_type keywordtype, void *result)
Read a property value from a property list.
int xsh_min_int(int x, int y)
Minimum of two numbers.
static cpl_error_code xsh_util_get_infsup(double *piw, double w, int i_start, int i_end, int *i_inf, int *i_sup)
cpl_frame * xsh_spectrum_interpolate_linear(cpl_frame *table_frame, const double wstep, const double wmin, const double wmax)
spectra interpolation
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
double xsh_vector_get_err_median(cpl_vector *vect)
Computes median error on a data set.
void xsh_free_frameset(cpl_frameset **f)
Deallocate a frame set and set the pointer to NULL.
int xsh_debug_level_set(int level)
set debug level
char * xsh_stringcat_6(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6)
String concatenation.
char * xsh_set_recipe_file_prefix(cpl_frameset *raw, const char *recipe)
Set recipe frames prefix.
char * xsh_stringcat_3(const char *s1, const char *s2, const char *s3)
String concatenation.
void xsh_free_temporary_files(void)
Free temprary files list.
cpl_frame * xsh_normalize_spectrum_ord(const cpl_frame *obj_frame, const cpl_frame *atm_ext_frame, cpl_boolean correct_binning, xsh_instrument *instrument, const char *tag_o)
Normalize a spectrum.
void xsh_free_array(cpl_array **m)
Deallocate an array and set the pointer to NULL.
double * xsh_function1d_xcorrelate(double *line_i, int width_i, double *line_t, int width_t, int half_search, int normalise, double *xcorr_max, double *delta)
double xsh_tools_tchebitchev_transform(double pos, double min, double max)
computes Tchebitchev transformation
cpl_frame * xsh_util_multiply_by_response_ord(cpl_frame *extracted_sci, cpl_frame *response, const char *tag_o)
Multiply input frame by response frame.
double xsh_min_double(double x, double y)
Minimum of two numbers.
double xsh_vector_get_err_mean(cpl_vector *vect)
Computes mean error on a data set.
const char * xsh_debug_level_tostring(void)
set debug level
void xsh_tools_get_statistics(double *tab, int size, double *median, double *mean, double *stdev)
Compute median, stdev and mean for the tab.
void xsh_mem_dump(const char *prompt)
void xsh_free_stats(cpl_stats **s)
Deallocate a stats object and set the pointer to NULL.
static int qc_linfunc(const double x[], const double p[], double *f)
void xsh_reindex_float(float *data, int *idx, int size)
TO BE DESCRIBED.
void xsh_frame_table_save(cpl_frame *frame, const char *name_o)
Save a table frame.
cpl_error_code xsh_monitor_flux(cpl_frame *frm_ima, const cpl_frame *frm_tab, xsh_instrument *instrument, const char *qc_key_prefix)
Monitor Flux level along the orders traces given by an input table
cpl_frame * xsh_frame_mult(cpl_frame *in, xsh_instrument *instr, cpl_frame *sign)
Computes product of two input frames.
void xsh_random_init(void)
cpl_error_code calc_flat_slit_qc(cpl_image *im, int xa1, int xa2, cpl_propertylist *qclist)
#define CPL_IMAGE_GET_DATA
cpl_vector * xsh_tools_tchebitchev_poly_eval(int n, double X)
Compute tchebitchev Tn(X) first coefficient for tchebitchev polynomial.
void xsh_plist_dump(cpl_propertylist *plist)
Dump propertylist.
int xsh_get_random_int_window(const int v1, const int v2)
generates random integer values in range [v1,v2]
double xsh_tools_tchebitchev_reverse_transform(double pos, double min, double max)
computes reverse Tchebitchev transformation
void xsh_gsl_init_gaussian_fit(cpl_vector *xpos_vect, cpl_vector *ypos_vect, double *init_par)
cpl_error_code xsh_set_cd_matrix3d(cpl_propertylist *plist)
Set CD matrix.
cpl_frame * xsh_frame_abs(cpl_frame *in, xsh_instrument *instr, cpl_frame **sign)
Computes absolute value of a frame.
void xsh_free_mask(cpl_mask **m)
Deallocate an image mask and set the pointer to NULL.
char * xsh_get_basename(const char *filename)
Return base filename.
int xsh_tools_running_median_1d_get_max(double *tab, int size, int wsize)
get max of a list of doubles after running median
cpl_error_code calc_curve_qc(cpl_image *im, xsh_instrument *instrument, cpl_propertylist *qclist)
void xsh_free_product_files(void)
Free temprary files list.
static int compare_frame_date(const void *one, const void *two)
int raw_mjd_frame_compare(const cpl_frame *f1, const cpl_frame *f2)
const char * xsh_get_license(void)
Get the pipeline copyright and license.
char * xsh_stringcat_any(const char *s,...)
Concatenate an arbitrary number of strings.
void xsh_reindex_int(int *data, int *idx, int size)
TO BE DESCRIBED.
cpl_error_code xsh_tools_sort_double(double *pix_arr, int n)
Sort a double array.
cpl_error_code xsh_frameset_dump(cpl_frameset *set)
Dump frameset.
cpl_error_code xsh_set_cd_matrix2d(cpl_propertylist *plist)
Set CD matrix.
#define XSH_PIX_STACK_SIZE
double xsh_hms2deg(const double hms)
Convert a double from hours minute seconds to deg:
int xsh_time_stamp_get(void)
get timestamp
double xsh_get_random_double_window(const double v1, const double v2)
generates random integer values in range [v1,v2]
cpl_frame * xsh_spectrum_resample(cpl_frame *frame_inp, const double wstep, const double wmin, const double wmax, xsh_instrument *instr)
resample a spectrum
void xsh_array_clip_poly1d(cpl_vector *pos_vect, cpl_vector *val_vect, double kappa, int niter, double frac_min, int deg, cpl_polynomial **polyp, double *chisq, int **flagsp)
clip outliers from a 1D poly fit
char * xsh_stringdup(const char *s)
String duplication.
int xsh_erase_table_rows(cpl_table *t, const char *column, cpl_table_select_operator operator, double value)
Erase table rows.
cpl_error_code xsh_tools_sort_int(int *pix_arr, int n)
Sort an integer array.
cpl_error_code get_chromatic_eff(cpl_frame *eff, cpl_propertylist *qclist)
void xsh_frame_image_save(cpl_frame *frm, const char *name_o)
save an image frame
cpl_frame * xsh_util_frameset_collapse_mean(cpl_frameset *set, xsh_instrument *instrument)
Compute mean frame from a list of (IMAGE) framesets.
cpl_vector * xsh_image_to_vector(cpl_image *spectrum)
cpl_frame * xsh_frame_inv(cpl_frame *in, const char *filename, xsh_instrument *instr)
Inverse the flux of a PRE frame.
void xsh_free_parameter(cpl_parameter **p)
Deallocate a parameter and set the pointer to NULL.
long xsh_round_double(double x)
Computes round(x)
cpl_error_code xsh_set_cd_matrix(cpl_propertylist *plist)
Set CD matrix.
cpl_frame * xsh_util_multiply_by_response(cpl_frame *merged_sci, cpl_frame *response, const char *tag_o)
Multiply input frame by response frame.
int * xsh_sort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *))
Sort an array and give is index table.
void xsh_reindex(double *data, int *idx, int size)
TO BE DESCRIBED.
void xsh_array_clip_median(cpl_array *array, double kappa, int niter, double frac_min, double *median, double *stdev)
median clip of an array
void xsh_init(void)
Reset library state.
double xsh_tools_get_median_double(double *array, int size)
Calculates the median value of an array of double.
cpl_frameset * xsh_order_frameset_by_date(cpl_frameset *frameset)
Order frameset by date.
char * xsh_sdate_utc(time_t *now)
void xsh_unwrap_image(cpl_image **i)
Unwrap an image and set the pointer to NULL.
cpl_image * xsh_imagelist_collapse_sigclip_iter_create(const cpl_imagelist *imlist, double sigma_low, double sigma_upp, const int niter)
Average with sigma-clipping rejection an imagelist to a single image.
static double date_to_double(const char *the_date)
void xsh_array_clip_mean(cpl_array *array, double kappa, int niter, double frac_min, double *mean, double *stdev)
mean clip of an array
cpl_error_code xsh_tools_sort_float(float *pix_arr, int n)
Sort a float array.
double xsh_sess2deg(const double sess)
Convert a double from ssessagesimal to deg: 203049.197= 20:30:49.197 = 20.5136658333.
void xsh_free_matrix(cpl_matrix **m)
Deallocate a matrix and set the pointer to NULL.
char * xsh_stringcat(const char *s1, const char *s2)
String concatenation.
void xsh_gsl_fit_gaussian(cpl_vector *xpos_vect, cpl_vector *ypos_vect, int deg, double *params, double *errs, int *status)
cpl_error_code xsh_remove_crh_single_params_set_defaults(cpl_parameterlist *pars, const char *rec_id, xsh_instrument *inst, xsh_remove_crh_single_param *crh_single_par)
cpl_error_code xsh_begin(cpl_frameset *frames, const cpl_parameterlist *parameters, xsh_instrument **instrument, cpl_frameset **raws, cpl_frameset **calib, const char *tag_list[], int tag_list_size, const char *recipe_id, unsigned int binary_version, const char *short_descr)
Recipe initialization.
cpl_error_code xsh_rectify_params_set_defaults(cpl_parameterlist *pars, const char *rec_id, xsh_instrument *inst, xsh_rectify_param *rectify_par)
double convert_bin_to_data(double bin_data, int binning)
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
cpl_error_code calc_resp_qc(cpl_table *mtab, cpl_table *rtab, xsh_instrument *instrument, int lower, int upper, const char *label, cpl_propertylist *qclist)
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
cpl_error_code xsh_end(const char *recipe_id, cpl_frameset *frames, cpl_parameterlist *parameters)
Recipe termination.
cpl_image * xsh_vector_to_image(const cpl_vector *vector, cpl_type type)
Convert a vector to a 1d image.
void xsh_add_product_file(const char *name)
Add temporary file to temprary files list.
static double xsh_spectrum_integrate(double *pif, double *piw, int i1_inf, int i1_sup, int i2_inf, int i2_sup, double wave, double wstep)
cpl_frame * xsh_spectrum_interpolate(cpl_frame *table_frame, const double wstep, const double wmin, const double wmax)
spectra interpolation
void xsh_frame_spectrum_save(cpl_frame *frm, const char *name_o)
save an spectrum frame
cpl_error_code xsh_normalize_spectrum_image_slice(const char *name_s, const char *tag_o, const int ext, const int bin_size, const double gain, const double exptime, const double airmass, const cpl_table *tbl_atm_ext)
Normalize spectrum slice.
cpl_frame * xsh_frameset_add(cpl_frameset *set, xsh_instrument *instr, const int decode_bp)
coadd frames in a frameset
double xsh_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.
static double xsh_iterpol_linear(double *data_x, double *data_y, int ndata, double x, int *i_inf, int *i_sup)
void xsh_tools_tchebitchev_transform_tab(int size, double *pos, double min, double max, double *tcheb_pos)
computes Tchebitchev transformation
void xsh_free_imagelist(cpl_imagelist **i)
Deallocate an image list and set the pointer to NULL.
double xsh_pow_int(double x, int y)
Computes x^y.
double xsh_max_double(double x, double y)
Maximum of two numbers.
void xsh_free(const void *mem)
Deallocate memory.
void xsh_unwrap_array(cpl_array **a)
Unwrap an array and set the pointer to NULL.
cpl_parameterlist * xsh_parameterlist_duplicate(const cpl_parameterlist *pin)
Extract frames with given tag from frameset.
#define XSH_DOUBLE_SWAP(a, b)
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
cpl_error_code xsh_set_cd_matrix1d(cpl_propertylist *plist)
Set CD matrix.
void xsh_show_time(const char *comment)
show time
#define XSH_ATMOS_EXT_LIST_COLNAME_OLD
#define XSH_ATMOS_EXT_LIST_COLNAME_K
#define XSH_PRE_DATA_TYPE
#define XSH_PRE_QUAL_TYPE
#define XSH_PRE_ERRS_TYPE
#define XSH_SPECTRUM_DATA_BPP
#define XSH_SPECTRUM_ERRS_BPP
cpl_error_code xsh_calib_nir_corr_if_JH(cpl_frameset *calib, xsh_instrument *instr, const char *recid)
void xsh_dfs_split_in_group(cpl_frameset *input, cpl_frameset *raws, cpl_frameset *calib)
split input sof in groups: raw and calib
int xsh_dfs_files_dont_exist(cpl_frameset *frameset)
Check if all SOF files exist.
xsh_instrument * xsh_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset and return the instrument detected.
void xsh_dfs_filter(cpl_frameset *set, const char *tags[], int size)
filter the frameset with the given tag list
cpl_frame * xsh_frame_product(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level)
Creates a frame with given characteristics.
#define XSH_SLIT_BIN_SIZE_PIPE_VIS
#define XSH_WAVE_BIN_SIZE_PIPE_VIS
#define XSH_SLIT_BIN_SIZE_PIPE_UVB
#define XSH_SLIT_BIN_SIZE_PIPE_NIR
#define XSH_REMOVE_CRH_SINGLE_NIR
#define XSH_REMOVE_CRH_SINGLE_UVB_VIS
#define XSH_WAVE_BIN_SIZE_PIPE_UVB
#define XSH_WAVE_BIN_SIZE_PIPE_NIR
cpl_parameter * xsh_parameters_find(cpl_parameterlist *list, const char *recipe_id, const char *name)
find a parameter
int xsh_parameters_get_temporary(const char *recipe_id, const cpl_parameterlist *list)
int xsh_parameters_debug_level_get(const char *recipe_id, const cpl_parameterlist *list)
#define XSH_NOD_CUMULATIVE_OFFSETY
#define XSH_NOD_JITTER_BOX
#define XSH_BUNIT_FLUX_ABS_C
#define XSH_QC_FLUX_MIN_C
#define XSH_QC_FLUX_MAX_C
#define XSH_ATM_EXT_UVB_WAV_MIN
#define XSH_FREE(POINTER)
#define XSH_MALLOC(POINTER, TYPE, SIZE)
#define XSH_CALLOC(POINTER, TYPE, SIZE)
#define XSH_NEW_FRAMESET(POINTER)
cpl_polynomial * xsh_polynomial_fit_1d_create(const cpl_vector *x_pos, const cpl_vector *values, int degree, double *mse)