22 #include "sinfo_utilities.h"
23 #include "sinfo_error.h"
24 #include "sinfo_dfs.h"
25 #include "sinfo_utils_wrappers.h"
26 #include "sinfo_function_1d.h"
48 sinfo_vector_to_image(
const cpl_vector* vector,cpl_type type)
51 cpl_image* image=NULL;
53 const double* pv=NULL;
59 size=cpl_vector_get_size(vector);
60 image=cpl_image_new(size,1,type);
61 pv=cpl_vector_get_data_const(vector);
62 if(type == CPL_TYPE_INT) {
63 pi=cpl_image_get_data_int(image);
67 }
else if (type == CPL_TYPE_FLOAT) {
68 pf=cpl_image_get_data_float(image);
72 }
else if (type == CPL_TYPE_DOUBLE) {
73 pd=cpl_image_get_data_double(image);
78 assure( 0, CPL_ERROR_INVALID_TYPE,
79 "No CPL type to represent BITPIX = %d", type);
83 if (cpl_error_get_code() != CPL_ERROR_NONE){
84 sinfo_free_image(&image);
93 sinfo_ima_line_cor(cpl_parameterlist * parlist, cpl_frameset* in)
102 const char* name=NULL;
103 const char* bname=NULL;
105 cpl_image * ima=NULL ;
106 cpl_image * ima_out=NULL ;
107 cpl_parameter* p=NULL;
108 cpl_propertylist* plist=NULL;
113 check_nomsg(p=cpl_parameterlist_find(parlist,
"sinfoni.general.lc_kappa"));
114 check_nomsg(kappa=cpl_parameter_get_int(p));
115 check_nomsg(p=cpl_parameterlist_find(parlist,
116 "sinfoni.general.lc_filt_rad"));
117 check_nomsg(filt_rad = cpl_parameter_get_int(p)) ;
119 n=cpl_frameset_get_size(in);
122 check_nomsg(frm=cpl_frameset_get_frame(in,i));
123 tag= (
char*) cpl_frame_get_tag(frm);
124 if(sinfo_frame_is_raw(tag) == 1) {
125 check_nomsg(name=cpl_frame_get_filename(frm));
127 bname=sinfo_new_get_basename(name);
128 check_nomsg(ima=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
129 check_nomsg(sinfo_image_line_corr(width,filt_rad,kappa,ima,
131 check_nomsg(plist=cpl_propertylist_load(name,0));
133 sprintf(name_deb,
"org_%2.2d_%s",i,bname);
135 check(cpl_image_save(ima,
140 "Could not save product");
143 check(cpl_image_save(ima_out,
148 "Could not save product");
150 cpl_frame_set_filename(frm,bname);
151 sinfo_free_image(&ima);
152 sinfo_free_propertylist(&plist);
158 sinfo_free_image(&ima);
159 sinfo_free_propertylist(&plist);
161 return cpl_error_get_code();
166 sinfo_table_column_dump(cpl_table* t,
const char* name, cpl_type type)
175 nrow=cpl_table_get_nrow(t);
180 pi=cpl_table_get_data_int(t,name);
181 for(i=0;i<nrow;i++) {
182 sinfo_msg(
"val=%d",pi[i]);
186 pf=cpl_table_get_data_float(t,name);
187 for(i=0;i<nrow;i++) {
188 sinfo_msg(
"val=%g",pf[i]);
191 case CPL_TYPE_DOUBLE:
192 pd=cpl_table_get_data_double(t,name);
193 for(i=0;i<nrow;i++) {
194 sinfo_msg(
"val=%g",pd[i]);
197 case CPL_TYPE_STRING:
198 ps=cpl_table_get_data_string(t,name);
199 for(i=0;i<nrow;i++) {
200 sinfo_msg(
"val=%s",ps[i]);
205 cpl_error_set(cpl_func, CPL_ERROR_TYPE_MISMATCH);
224 sinfo_table_shift_column_spline3(cpl_table* t,
241 float* corrected_spec=NULL;
243 cknull(t,
"null input table");
244 out=cpl_table_duplicate(t);
246 nrow=cpl_table_get_nrow(t);
247 check_nomsg(cpl_table_cast_column(t,col,
"FINT",CPL_TYPE_FLOAT));
248 check_nomsg(cpl_table_cast_column(out,col,
"FINT",CPL_TYPE_FLOAT));
249 pi=cpl_table_get_data_float(t,
"FINT");
250 po=cpl_table_get_data_float(out,
"FINT");
254 xnum=cpl_calloc(nrow,
sizeof(
float)) ;
256 for ( i = 0 ; i < nrow ; i++ ) {
260 spec=cpl_calloc(nrow,
sizeof(
float)) ;
261 corrected_spec=cpl_calloc(nrow,
sizeof(
float)) ;
262 eval=cpl_calloc(nrow,
sizeof(
float)) ;
265 for ( z = 0 ; z < nrow ; z++ ) {
267 if (isnan(spec[z]) ) {
268 for ( i = z-1 ; i <= z+1 ; i++ ) {
269 if ( i < 0 ) continue ;
270 if ( i >= nrow) continue ;
271 corrected_spec[i] = ZERO ;
276 eval[z] = (float)shift+(
float)z ;
279 if ( -1 == sinfo_function1d_natural_spline(xnum,spec, nrow,
280 eval,corrected_spec, nrow))
287 for ( z = 0 ; z < nrow ; z++ ) {
288 if ( isnan(corrected_spec[z]) ) {
291 new_sum += corrected_spec[z] ;
294 for ( z = 0 ; z < nrow ; z++ ) {
295 if ( new_sum == 0. ) new_sum =1. ;
297 if ( isnan(corrected_spec[z]) ) {
300 corrected_spec[z] *= sum / new_sum ;
301 po[z] = corrected_spec[z] ;
306 sinfo_free_float(&xnum);
307 sinfo_free_float(&spec) ;
308 sinfo_free_float(&corrected_spec) ;
309 sinfo_free_float(&eval) ;
311 check_nomsg(cpl_table_erase_column(t,
"FINT"));
312 check_nomsg(cpl_table_erase_column(out,col));
313 check_nomsg(cpl_table_cast_column(out,
"FINT",col,CPL_TYPE_DOUBLE));
314 check_nomsg(cpl_table_erase_column(out,
"FINT"));
319 sinfo_free_float(&xnum);
320 sinfo_free_float(&spec) ;
321 sinfo_free_float(&corrected_spec) ;
322 sinfo_free_float(&eval) ;
323 sinfo_free_table(&out);
340 sinfo_table_shift_column_int(
const cpl_table* t,
350 const double* pi=NULL;
353 cknull(t,
"null input table");
354 out=cpl_table_duplicate(t);
356 nrow=cpl_table_get_nrow(t);
357 pi=cpl_table_get_data_double_const(t,col);
358 po=cpl_table_get_data_double(out,col);
359 for(i=0;i<nrow;i++) {
360 if( ((i-is) >=0) && ((i-is) < nrow)) {
366 sinfo_free_table(&out);
383 sinfo_table_shift_column_poly(cpl_table* t,
401 float* corrected_spec=NULL ;
403 float* tableptr=NULL;
405 cknull(t,
"null input table");
411 out=cpl_table_duplicate(t);
413 nrow=cpl_table_get_nrow(t);
414 cpl_table_cast_column(t,col,
"FINT",CPL_TYPE_FLOAT);
415 cpl_table_cast_column(out,col,
"FINT",CPL_TYPE_FLOAT);
416 pi=cpl_table_get_data_float(t,
"FINT");
417 po=cpl_table_get_data_float(out,
"FINT");
419 n_points = order + 1 ;
420 if ( n_points % 2 == 0 ) {
421 firstpos = (int)(n_points/2) - 1 ;
423 firstpos = (int)(n_points/2) ;
425 spec=cpl_calloc(nrow,
sizeof(
float)) ;
426 corrected_spec=cpl_calloc(nrow,
sizeof(
float)) ;
427 xnum=cpl_calloc(order+1,
sizeof(
float)) ;
429 for ( i = 0 ; i < n_points ; i++ ) {
434 for(i=0;i<nrow;i++) {
435 corrected_spec[i] = 0. ;
439 for ( z = 0 ; z < nrow ; z++ ) {
441 if (isnan(spec[z]) ) {
444 for ( i = z - firstpos ; i < z-firstpos+n_points ; i++ ) {
445 if ( i < 0 ) continue ;
446 if ( i >= nrow) continue ;
447 corrected_spec[i] = ZERO ;
450 if ( z != 0 && z != nrow - 1 ) {
456 for ( z = 0 ; z < nrow ; z++ ) {
463 if (isnan(corrected_spec[z])) continue ;
464 if ( z - firstpos < 0 ) {
465 tableptr = &spec[0] ;
467 }
else if ( z - firstpos + n_points >= nrow ) {
468 tableptr = &spec[nrow - n_points] ;
469 eval = shift + z + n_points - nrow ;
471 tableptr = &spec[z-firstpos] ;
472 eval = shift + firstpos ;
476 corrected_spec[z]=sinfo_new_nev_ille(xnum,tableptr,order,eval,&flag);
477 if ( z != 0 && z != nrow - 1 ) {
478 new_sum += corrected_spec[z] ;
483 for (z = 0 ; z < nrow ; z++ ) {
484 if ( new_sum == 0. ) {
489 }
else if ( z == nrow - 1 ) {
491 }
else if ( isnan(corrected_spec[z]) ) {
494 corrected_spec[z] *= sum / new_sum ;
495 po[z] = corrected_spec[z] ;
498 check_nomsg(cpl_table_erase_column(t,
"FINT"));
499 check_nomsg(cpl_table_erase_column(out,col));
500 check_nomsg(cpl_table_cast_column(out,
"FINT",col,CPL_TYPE_DOUBLE));
501 check_nomsg(cpl_table_erase_column(out,
"FINT"));
503 sinfo_free_float(&spec) ;
504 sinfo_free_float(&corrected_spec) ;
505 sinfo_free_float(&xnum) ;
511 sinfo_free_float(&spec) ;
512 sinfo_free_float(&corrected_spec) ;
513 sinfo_free_float(&xnum) ;
514 sinfo_free_table(&out);
523 void sinfo_new_array_set_value(
float * array,
float value,
int i )
527 float sinfo_new_array_get_value(
float * array,
int i )
534 void sinfo_new_destroy_array(
float ** array)
542 void sinfo_new_destroy_stringarray(
char ** array,
int size_x)
546 for ( i = 0 ; i < size_x ; i++ )
548 cpl_free( array[i] ) ;
553 void sinfo_new_destroy_2Dintarray(
int *** array,
int size_x)
557 if((*array) != NULL) {
558 for ( i = 0 ; i < size_x ; i++ ) {
559 if((*array)[i] != NULL) {
560 cpl_free( (*array)[i] );
571 void sinfo_new_intarray_set_value(
int * array,
int value,
int i )
575 float sinfo_new_array2D_get_value(
float ** array,
int x,
int y )
579 int ** sinfo_new_2Dintarray(
int size_x,
int size_y)
584 retVal = (
int **) cpl_calloc( size_x,
sizeof (
int*) ) ;
585 for ( i = 0 ; i < size_x ; i++ )
587 retVal[i] = (
int *) cpl_calloc( size_y,
sizeof (
int)) ;
592 float * sinfo_new_floatarray(
int size)
594 return (
float *) cpl_calloc( size,
sizeof (
float) ) ;
598 void sinfo_new_destroy_2Dfloatarray(
float *** array,
int size_x)
601 if((*array) != NULL) {
602 for ( i = 0 ; i < size_x ; i++ ) {
603 if((*array)[i] != NULL) {
604 cpl_free( (*array)[i] );
613 void sinfo_new_destroy_2Ddoublearray(
double *** array,
int size_x)
617 if((*array) != NULL) {
618 for ( i = 0 ; i < size_x ; i++ ) {
619 if((*array)[i] != NULL) {
620 cpl_free( (*array)[i] );
631 void sinfo_new_array2D_set_value(
float ** array,
float value,
int x,
int y)
633 array[x][y] = value ;
636 double sinfo_new_doublearray_get_value(
double * array,
int i )
640 void sinfo_new_doublearray_set_value(
double * array,
double value,
int i )
645 void sinfo_new_destroy_doublearray(
double * array)
649 double * sinfo_new_doublearray(
int size)
651 return (
double *) cpl_calloc( size,
sizeof (
double) ) ;
654 double ** sinfo_new_2Ddoublearray(
int size_x,
int size_y)
659 retVal = (
double **) cpl_calloc( size_x,
sizeof (
double*) ) ;
660 for ( i = 0 ; i < size_x ; i++ )
662 retVal[i] = (
double *) cpl_calloc( size_y,
sizeof (
double)) ;
667 float ** sinfo_new_2Dfloatarray(
int size_x,
int size_y)
672 retVal = (
float **) cpl_calloc( size_x,
sizeof (
float*) ) ;
673 for ( i = 0 ; i < size_x ; i++ )
675 retVal[i] = (
float *) cpl_calloc( size_y,
sizeof (
float)) ;
681 int * sinfo_new_intarray(
int size)
683 return (
int *) cpl_calloc( size,
sizeof (
int) ) ;
685 void sinfo_new_destroy_intarray(
int ** array)
691 int sinfo_new_intarray_get_value(
int * array,
int i )
696 float sinfo_new_Stats_get_cleanstdev(Stats * stats)
698 return stats -> cleanstdev ;
700 float sinfo_new_Stats_get_cleanmean(Stats * stats)
702 return stats -> cleanmean ;
705 char * sinfo_new_get_basename(
const char *filename)
708 p = strrchr (filename,
'/');
709 return p ? p + 1 : (
char *) filename;
714 char * sinfo_new_get_rootname(
const char * filename)
716 static char path[MAX_NAME_SIZE+1];
719 if (strlen(filename)>MAX_NAME_SIZE)
return NULL ;
720 memset(path, MAX_NAME_SIZE, 0);
721 strcpy(path, filename);
722 lastdot = strrchr(path,
'.');
723 if (lastdot == NULL)
return path ;
724 if ((!strcmp(lastdot,
".fits")) || (!strcmp(lastdot,
".FITS"))
725 || (!strcmp(lastdot,
".paf")) || (!strcmp(lastdot,
".PAF"))
726 || (!strcmp(lastdot,
".dat")) || (!strcmp(lastdot,
".DAT"))
727 || (!strcmp(lastdot,
".fits")) || (!strcmp(lastdot,
".TFITS"))
728 || (!strcmp(lastdot,
".ascii"))
729 || (!strcmp(lastdot,
".ASCII")))
731 lastdot[0] = (char)0;
747 cpl_imagelist * sinfo_new_frameset_to_iset(cpl_frameset * fset)
749 cpl_imagelist * iset=NULL ;
754 if (fset == NULL)
return NULL ;
757 if ((filenames = sinfo_new_frameset_to_filenames(fset, &nfiles)) == NULL) {
762 if ((iset = sinfo_new_imagelist_load_frameset(fset,
763 CPL_TYPE_FLOAT, 0, 0)) == NULL) {
767 cpl_free(filenames) ;
772 cpl_free(filenames) ;
775 #include "cpl_imagelist_io.h"
790 sinfo_new_imagelist_load_frameset(
const cpl_frameset * frameset,
795 cpl_image * image = NULL;
796 cpl_imagelist * imagelist = NULL;
797 const cpl_frame * frame = cpl_frameset_get_first_const(frameset);
798 const int nz = cpl_frameset_get_size(frameset);
802 cpl_ensure(nz > 0, CPL_ERROR_DATA_NOT_FOUND, NULL);
804 for (i = 0; frame != NULL;
805 i++, frame = cpl_frameset_get_next_const(frameset)) {
807 const char * name = cpl_frame_get_filename(frame);
808 if (name == NULL)
break;
811 image = cpl_image_load(name, type, pnum, extnum);
813 if (image == NULL)
break;
816 const int nx = cpl_image_get_size_x(image);
817 const int ny = cpl_image_get_size_y(image);
819 if (nx < 1 || ny < 1)
break;
820 imagelist = cpl_imagelist_new();
821 if (imagelist == NULL)
break;
824 if (cpl_imagelist_set(imagelist, image, i))
break;
831 cpl_image_delete(image);
832 cpl_imagelist_delete(imagelist);
849 char ** sinfo_new_frameset_to_filenames(cpl_frameset *
set,
int *nfiles)
851 char **filenames=NULL;
856 cpl_frame *curr_frame;
862 nbframes = cpl_frameset_get_size(
set);
870 filenames = cpl_malloc(nbframes *
sizeof(
char *));
872 curr_frame = cpl_frameset_get_first(
set);
873 for (i = 0; i < nbframes; i++) {
874 filenames[i]=(
char*) cpl_frame_get_filename(curr_frame);
875 curr_frame = cpl_frameset_get_next(
set);
906 double sinfo_spline_hermite(
double xp,
912 double yp1, yp2, yp = 0;
913 double xpi, xpi1, l1, l2, lp1, lp2;
916 if ( x[0] <= x[n-1] && (xp < x[0] || xp > x[n-1]) )
return 0.0;
917 if ( x[0] > x[n-1] && (xp > x[0] || xp < x[n-1]) )
return 0.0;
919 if ( x[0] <= x[n-1] )
921 for ( i = (*istart)+1; i <= n && xp >= x[i-1]; i++ )
926 for ( i = (*istart)+1; i <= n && xp <= x[i-1]; i++ )
933 lp1 = 1.0 / (x[i-1] - x[i]);
938 yp1 = (y[1] - y[0]) / (x[1] - x[0]);
942 yp1 = (y[i] - y[i-2]) / (x[i] - x[i-2]);
947 yp2 = (y[n-1] - y[n-2]) / (x[n-1] - x[n-2]);
951 yp2 = (y[i+1] - y[i-1]) / (x[i+1] - x[i-1]);
959 yp = y[i-1]*(1 - 2.0*lp1*xpi)*l1*l1 +
960 y[i]*(1 - 2.0*lp2*xpi1)*l2*l2 +
961 yp1*xpi*l1*l1 + yp2*xpi1*l2*l2;
972 cpl_error_code update_bad_pixel_map(cpl_image* im)
974 int szx = cpl_image_get_size_x(im);
975 int szy = cpl_image_get_size_y(im);
977 cpl_mask* bpm = cpl_image_get_bpm(im);
979 for (x = 1; x <=szx; x++)
982 for(y = 1; y <= szy; y++)
985 double value = cpl_image_get(im, x, y, &isnull);
988 cpl_mask_set(bpm, x, y, CPL_BINARY_1);
992 return cpl_error_get_code();
994 cpl_polynomial * sinfo_polynomial_fit_2d_create(cpl_bivector * xy_pos,
999 typedef double* (*get_data)(cpl_bivector*);
1000 get_data data_extractor[2] = { &cpl_bivector_get_x_data,
1001 &cpl_bivector_get_y_data};
1007 cpl_vector * fitresidual = 0;
1008 cpl_matrix * samppos2d = 0;
1009 cpl_polynomial * fit2d = cpl_polynomial_new(2);
1010 int xy_size = cpl_bivector_get_size(xy_pos);
1012 samppos2d = cpl_matrix_new(2, xy_size);
1013 for (i = 0; i < 2; i++)
1015 for (j = 0; j < xy_size; j++)
1017 double value = data_extractor[i](xy_pos)[j];
1018 cpl_matrix_set(samppos2d, i, j, value);
1022 cpl_polynomial_fit(fit2d, samppos2d, NULL, values, NULL, CPL_FALSE,
1025 fitresidual = cpl_vector_new(xy_size);
1026 cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit2d,
1027 samppos2d, &rechisq);
1030 *mse = cpl_vector_product(fitresidual, fitresidual)
1031 / cpl_vector_get_size(fitresidual);
1033 cpl_matrix_delete(samppos2d);
1034 cpl_vector_delete(fitresidual);
1038 cpl_polynomial * sinfo_polynomial_fit_1d_create(
1039 const cpl_vector * x_pos,
1040 const cpl_vector * values,
1045 cpl_polynomial * fit1d = cpl_polynomial_new(1);
1048 int x_size = cpl_vector_get_size(x_pos);
1050 cpl_size loc_deg=(cpl_size)degree;
1051 cpl_matrix * samppos = cpl_matrix_wrap(1, x_size,
1052 (
double*)cpl_vector_get_data_const(x_pos));
1053 cpl_vector * fitresidual = cpl_vector_new(x_size);
1055 cpl_polynomial_fit(fit1d, samppos, NULL, values, NULL,
1056 CPL_FALSE, NULL, &loc_deg);
1057 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1058 cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit1d,
1060 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1063 *mse = cpl_vector_product(fitresidual, fitresidual)
1064 / cpl_vector_get_size(fitresidual);
1066 cpl_matrix_unwrap(samppos);
1067 cpl_vector_delete(fitresidual);
1072 static cpl_image * sinfo_image_filter_wrapper(
const cpl_image *b,
1073 const cpl_matrix *k,
1074 cpl_filter_mode mode)
1076 const double EPSILON = 1E-5;
1077 int nx = cpl_image_get_size_x(b);
1078 int ny = cpl_image_get_size_y(b);
1079 int nrow = cpl_matrix_get_nrow(k);
1080 int ncol = cpl_matrix_get_ncol(k);
1082 cpl_type type = cpl_image_get_type(b);
1083 cpl_image * a = cpl_image_new(nx, ny, type);
1085 cpl_mask* m = cpl_mask_new(ncol, nrow);
1086 cpl_msg_warning(cpl_func,
"nx[%d], ny[%d], ncol[%d], nrow[%d]",
1087 nx, ny, ncol, nrow);
1088 for (i = 0; i < ncol ; i++)
1090 for (j = 0; j < nrow ; j++)
1092 double value = cpl_matrix_get(k, j, i);
1093 if (fabs(value - 1.0) < EPSILON)
1095 cpl_mask_set(m, i + 1, j + 1, CPL_BINARY_1);
1100 cpl_image_filter_mask(a, b, m, mode, CPL_BORDER_FILTER);
1107 sinfo_image_filter_mode(
const cpl_image* b,
1108 const cpl_matrix * ker,
1109 cpl_filter_mode filter)
1111 int nx = cpl_image_get_size_x(b);
1112 int ny = cpl_image_get_size_y(b);
1113 int type = cpl_image_get_type(b);
1114 cpl_image * a = cpl_image_new(nx, ny, type);
1117 case CPL_FILTER_MEDIAN:
1118 check_nomsg(cpl_image_filter(a, b, ker, CPL_FILTER_MEDIAN,
1119 CPL_BORDER_FILTER));
1121 case CPL_FILTER_LINEAR:
1122 check_nomsg(cpl_image_filter(a, b, ker, CPL_FILTER_LINEAR,
1123 CPL_BORDER_FILTER));
1125 case CPL_FILTER_STDEV:
1126 cpl_image_filter(a, b, ker, CPL_FILTER_STDEV, CPL_BORDER_FILTER);
1128 case CPL_FILTER_MORPHO:
1129 cpl_image_filter(a, b, ker, CPL_FILTER_MORPHO, CPL_BORDER_FILTER);
1142 sinfo_image_filter_linear(
const cpl_image *img,
const cpl_matrix * mx)
1144 return sinfo_image_filter_mode(img, mx, CPL_FILTER_LINEAR);
1148 cpl_image * sinfo_image_filter_linear2(
const cpl_image *img,
1149 const cpl_matrix * mx)
1151 return sinfo_image_filter_wrapper(img, mx, CPL_FILTER_LINEAR);
1156 cpl_image * sinfo_image_filter_median(
const cpl_image * img,
1157 const cpl_matrix * mx)
1159 return sinfo_image_filter_wrapper(img, mx, CPL_FILTER_MEDIAN);