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);
341 sinfo_table_shift_column_int(
const cpl_table* t,
351 const double* pi=NULL;
354 cknull(t,
"null input table");
355 out=cpl_table_duplicate(t);
357 nrow=cpl_table_get_nrow(t);
358 pi=cpl_table_get_data_double_const(t,col);
359 po=cpl_table_get_data_double(out,col);
360 for(i=0;i<nrow;i++) {
361 if( ((i-is) >=0) && ((i-is) < nrow)) {
367 sinfo_free_table(&out);
384 sinfo_table_shift_column_poly(cpl_table* t,
402 float* corrected_spec=NULL ;
404 float* tableptr=NULL;
406 cknull(t,
"null input table");
412 out=cpl_table_duplicate(t);
414 nrow=cpl_table_get_nrow(t);
415 cpl_table_cast_column(t,col,
"FINT",CPL_TYPE_FLOAT);
416 cpl_table_cast_column(out,col,
"FINT",CPL_TYPE_FLOAT);
417 pi=cpl_table_get_data_float(t,
"FINT");
418 po=cpl_table_get_data_float(out,
"FINT");
420 n_points = order + 1 ;
421 if ( n_points % 2 == 0 ) {
422 firstpos = (int)(n_points/2) - 1 ;
424 firstpos = (int)(n_points/2) ;
426 spec=cpl_calloc(nrow,
sizeof(
float)) ;
427 corrected_spec=cpl_calloc(nrow,
sizeof(
float)) ;
428 xnum=cpl_calloc(order+1,
sizeof(
float)) ;
430 for ( i = 0 ; i < n_points ; i++ ) {
435 for(i=0;i<nrow;i++) {
436 corrected_spec[i] = 0. ;
440 for ( z = 0 ; z < nrow ; z++ ) {
442 if (isnan(spec[z]) ) {
445 for ( i = z - firstpos ; i < z-firstpos+n_points ; i++ ) {
446 if ( i < 0 ) continue ;
447 if ( i >= nrow) continue ;
448 corrected_spec[i] = ZERO ;
451 if ( z != 0 && z != nrow - 1 ) {
457 for ( z = 0 ; z < nrow ; z++ ) {
464 if (isnan(corrected_spec[z])) continue ;
465 if ( z - firstpos < 0 ) {
466 tableptr = &spec[0] ;
468 }
else if ( z - firstpos + n_points >= nrow ) {
469 tableptr = &spec[nrow - n_points] ;
470 eval = shift + z + n_points - nrow ;
472 tableptr = &spec[z-firstpos] ;
473 eval = shift + firstpos ;
477 corrected_spec[z]=sinfo_new_nev_ille(xnum,tableptr,order,eval,&flag);
478 if ( z != 0 && z != nrow - 1 ) {
479 new_sum += corrected_spec[z] ;
484 for (z = 0 ; z < nrow ; z++ ) {
485 if ( new_sum == 0. ) {
490 }
else if ( z == nrow - 1 ) {
492 }
else if ( isnan(corrected_spec[z]) ) {
495 corrected_spec[z] *= sum / new_sum ;
496 po[z] = corrected_spec[z] ;
499 check_nomsg(cpl_table_erase_column(t,
"FINT"));
500 check_nomsg(cpl_table_erase_column(out,col));
501 check_nomsg(cpl_table_cast_column(out,
"FINT",col,CPL_TYPE_DOUBLE));
502 check_nomsg(cpl_table_erase_column(out,
"FINT"));
504 sinfo_free_float(&spec) ;
505 sinfo_free_float(&corrected_spec) ;
506 sinfo_free_float(&xnum) ;
512 sinfo_free_float(&spec) ;
513 sinfo_free_float(&corrected_spec) ;
514 sinfo_free_float(&xnum) ;
515 sinfo_free_table(&out);
524 void sinfo_new_array_set_value(
float * array,
float value,
int i )
528 float sinfo_new_array_get_value(
float * array,
int i )
535 void sinfo_new_destroy_array(
float ** array)
543 void sinfo_new_destroy_stringarray(
char ** array,
int size_x)
547 for ( i = 0 ; i < size_x ; i++ )
549 cpl_free( array[i] ) ;
554 void sinfo_new_destroy_2Dintarray(
int *** array,
int size_x)
557 if((*array) != NULL) {
558 for (
int 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)
600 if((*array) != NULL) {
601 for (
int i = 0 ; i < size_x ; i++ ) {
602 if((*array)[i] != NULL) {
603 cpl_free( (*array)[i] );
612 void sinfo_new_destroy_2Ddoublearray(
double *** array,
int size_x)
615 if((*array) != NULL) {
616 for (
int i = 0 ; i < size_x ; i++ ) {
617 if((*array)[i] != NULL) {
618 cpl_free( (*array)[i] );
629 void sinfo_new_array2D_set_value(
float ** array,
float value,
int x,
int y)
631 array[x][y] = value ;
634 double sinfo_new_doublearray_get_value(
double * array,
int i )
638 void sinfo_new_doublearray_set_value(
double * array,
double value,
int i )
643 void sinfo_new_destroy_doublearray(
double * array)
647 double * sinfo_new_doublearray(
int size)
649 return (
double *) cpl_calloc( size,
sizeof (
double) ) ;
652 double ** sinfo_new_2Ddoublearray(
int size_x,
int size_y)
657 retVal = (
double **) cpl_calloc( size_x,
sizeof (
double*) ) ;
658 for ( i = 0 ; i < size_x ; i++ )
660 retVal[i] = (
double *) cpl_calloc( size_y,
sizeof (
double)) ;
665 float ** sinfo_new_2Dfloatarray(
int size_x,
int size_y)
670 retVal = (
float **) cpl_calloc( size_x,
sizeof (
float*) ) ;
671 for ( i = 0 ; i < size_x ; i++ )
673 retVal[i] = (
float *) cpl_calloc( size_y,
sizeof (
float)) ;
679 int * sinfo_new_intarray(
int size)
681 return (
int *) cpl_calloc( size,
sizeof (
int) ) ;
683 void sinfo_new_destroy_intarray(
int ** array)
689 int sinfo_new_intarray_get_value(
int * array,
int i )
694 float sinfo_new_Stats_get_cleanstdev(Stats * stats)
696 return stats -> cleanstdev ;
699 float sinfo_new_Stats_get_cleanmean(Stats * stats)
701 return stats -> cleanmean ;
704 char * sinfo_new_get_basename(
const char *filename)
707 p = strrchr (filename,
'/');
708 return p ? p + 1 : (
char *) filename;
713 char * sinfo_new_get_rootname(
const char * filename)
715 static char path[MAX_NAME_SIZE+1];
718 if (strlen(filename)>MAX_NAME_SIZE)
return NULL ;
719 memset(path, MAX_NAME_SIZE, 0);
720 strcpy(path, filename);
721 lastdot = strrchr(path,
'.');
722 if (lastdot == NULL)
return path ;
723 if ((!strcmp(lastdot,
".fits")) || (!strcmp(lastdot,
".FITS"))
724 || (!strcmp(lastdot,
".paf")) || (!strcmp(lastdot,
".PAF"))
725 || (!strcmp(lastdot,
".dat")) || (!strcmp(lastdot,
".DAT"))
726 || (!strcmp(lastdot,
".fits")) || (!strcmp(lastdot,
".TFITS"))
727 || (!strcmp(lastdot,
".ascii"))
728 || (!strcmp(lastdot,
".ASCII")))
730 lastdot[0] = (char)0;
746 cpl_imagelist * sinfo_new_frameset_to_iset(cpl_frameset * fset)
748 cpl_imagelist * iset=NULL ;
753 if (fset == NULL)
return NULL ;
756 if ((filenames = sinfo_new_frameset_to_filenames(fset, &nfiles)) == NULL) {
761 if ((iset = sinfo_new_imagelist_load_frameset(fset,
762 CPL_TYPE_FLOAT, 0, 0)) == NULL) {
766 cpl_free(filenames) ;
771 cpl_free(filenames) ;
774 #include "cpl_imagelist_io.h"
789 sinfo_new_imagelist_load_frameset(
const cpl_frameset * frameset,
794 cpl_image * image = NULL;
795 cpl_imagelist * imagelist = NULL;
797 cpl_frameset_iterator* it = cpl_frameset_iterator_new(frameset);
798 const cpl_frame *frame = cpl_frameset_iterator_get_const(it);
800 const int nz = cpl_frameset_get_size(frameset);
804 cpl_ensure(nz > 0, CPL_ERROR_DATA_NOT_FOUND, NULL);
806 for (i = 0; i<nz; i++) {
808 const char * name = cpl_frame_get_filename(frame);
809 if (name == NULL)
break;
812 image = cpl_image_load(name, type, pnum, extnum);
814 if (image == NULL)
break;
817 const int nx = cpl_image_get_size_x(image);
818 const int ny = cpl_image_get_size_y(image);
820 if (nx < 1 || ny < 1)
break;
821 imagelist = cpl_imagelist_new();
822 if (imagelist == NULL)
break;
825 if (cpl_imagelist_set(imagelist, image, i))
break;
827 cpl_frameset_iterator_advance(it, 1);
828 frame = cpl_frameset_iterator_get_const(it);
834 cpl_image_delete(image);
835 cpl_imagelist_delete(imagelist);
838 cpl_frameset_iterator_delete(it);
853 char ** sinfo_new_frameset_to_filenames(cpl_frameset *set,
int *nfiles)
855 char **filenames=NULL;
866 nbframes = cpl_frameset_get_size(set);
874 filenames = cpl_malloc(nbframes *
sizeof(
char *));
876 cpl_frameset_iterator* it = cpl_frameset_iterator_new(set);
877 const cpl_frame *curr_frame = cpl_frameset_iterator_get(it);
879 for (i = 0; i < nbframes; i++) {
880 filenames[i]=(
char*) cpl_frame_get_filename(curr_frame);
882 cpl_frameset_iterator_advance(it, 1);
883 curr_frame = cpl_frameset_iterator_get_const(it);
885 cpl_frameset_iterator_delete(it);
915 double sinfo_spline_hermite(
double xp,
921 double yp1, yp2, yp = 0;
922 double xpi, xpi1, l1, l2, lp1, lp2;
925 if ( x[0] <= x[n-1] && (xp < x[0] || xp > x[n-1]) )
return 0.0;
926 if ( x[0] > x[n-1] && (xp > x[0] || xp < x[n-1]) )
return 0.0;
928 if ( x[0] <= x[n-1] )
930 for ( i = (*istart)+1; i <= n && xp >= x[i-1]; i++ )
935 for ( i = (*istart)+1; i <= n && xp <= x[i-1]; i++ )
942 lp1 = 1.0 / (x[i-1] - x[i]);
947 yp1 = (y[1] - y[0]) / (x[1] - x[0]);
951 yp1 = (y[i] - y[i-2]) / (x[i] - x[i-2]);
956 yp2 = (y[n-1] - y[n-2]) / (x[n-1] - x[n-2]);
960 yp2 = (y[i+1] - y[i-1]) / (x[i+1] - x[i-1]);
968 yp = y[i-1]*(1 - 2.0*lp1*xpi)*l1*l1 +
969 y[i]*(1 - 2.0*lp2*xpi1)*l2*l2 +
970 yp1*xpi*l1*l1 + yp2*xpi1*l2*l2;
981 cpl_error_code update_bad_pixel_map(cpl_image* im)
983 int szx = cpl_image_get_size_x(im);
984 int szy = cpl_image_get_size_y(im);
986 cpl_mask* bpm = cpl_image_get_bpm(im);
988 for (x = 1; x <=szx; x++)
991 for(y = 1; y <= szy; y++)
994 double value = cpl_image_get(im, x, y, &isnull);
997 cpl_mask_set(bpm, x, y, CPL_BINARY_1);
1001 return cpl_error_get_code();
1003 cpl_polynomial * sinfo_polynomial_fit_2d_create(cpl_bivector * xy_pos,
1004 cpl_vector * values,
1008 typedef double* (*get_data)(cpl_bivector*);
1009 get_data data_extractor[2] = { &cpl_bivector_get_x_data,
1010 &cpl_bivector_get_y_data};
1016 cpl_vector * fitresidual = 0;
1017 cpl_matrix * samppos2d = 0;
1018 cpl_polynomial * fit2d = cpl_polynomial_new(2);
1019 int xy_size = cpl_bivector_get_size(xy_pos);
1021 samppos2d = cpl_matrix_new(2, xy_size);
1022 for (i = 0; i < 2; i++)
1024 for (j = 0; j < xy_size; j++)
1026 double value = data_extractor[i](xy_pos)[j];
1027 cpl_matrix_set(samppos2d, i, j, value);
1031 cpl_polynomial_fit(fit2d, samppos2d, NULL, values, NULL, CPL_FALSE,
1034 fitresidual = cpl_vector_new(xy_size);
1035 cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit2d,
1036 samppos2d, &rechisq);
1039 *mse = cpl_vector_product(fitresidual, fitresidual)
1040 / cpl_vector_get_size(fitresidual);
1042 cpl_matrix_delete(samppos2d);
1043 cpl_vector_delete(fitresidual);
1047 cpl_polynomial * sinfo_polynomial_fit_1d_create(
1048 const cpl_vector * x_pos,
1049 const cpl_vector * values,
1054 cpl_polynomial * fit1d = cpl_polynomial_new(1);
1057 int x_size = cpl_vector_get_size(x_pos);
1059 cpl_size loc_deg=(cpl_size)degree;
1060 cpl_matrix * samppos = cpl_matrix_wrap(1, x_size,
1061 (
double*)cpl_vector_get_data_const(x_pos));
1062 cpl_vector * fitresidual = cpl_vector_new(x_size);
1064 cpl_polynomial_fit(fit1d, samppos, NULL, values, NULL,
1065 CPL_FALSE, NULL, &loc_deg);
1066 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1067 cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit1d,
1069 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1072 *mse = cpl_vector_product(fitresidual, fitresidual)
1073 / cpl_vector_get_size(fitresidual);
1075 cpl_matrix_unwrap(samppos);
1076 cpl_vector_delete(fitresidual);
1081 static cpl_image * sinfo_image_filter_wrapper(
const cpl_image *b,
1082 const cpl_matrix *k,
1083 cpl_filter_mode mode)
1085 const double EPSILON = 1E-5;
1086 int nx = cpl_image_get_size_x(b);
1087 int ny = cpl_image_get_size_y(b);
1088 int nrow = cpl_matrix_get_nrow(k);
1089 int ncol = cpl_matrix_get_ncol(k);
1091 cpl_type type = cpl_image_get_type(b);
1092 cpl_image * a = cpl_image_new(nx, ny, type);
1094 cpl_mask* m = cpl_mask_new(ncol, nrow);
1095 cpl_msg_warning(cpl_func,
"nx[%d], ny[%d], ncol[%d], nrow[%d]",
1096 nx, ny, ncol, nrow);
1097 for (i = 0; i < ncol ; i++)
1099 for (j = 0; j < nrow ; j++)
1101 double value = cpl_matrix_get(k, j, i);
1102 if (fabs(value - 1.0) < EPSILON)
1104 cpl_mask_set(m, i + 1, j + 1, CPL_BINARY_1);
1109 cpl_image_filter_mask(a, b, m, mode, CPL_BORDER_FILTER);
1116 sinfo_image_filter_mode(
const cpl_image* b,
1117 const cpl_matrix * ker,
1118 cpl_filter_mode filter)
1120 int nx = cpl_image_get_size_x(b);
1121 int ny = cpl_image_get_size_y(b);
1122 int type = cpl_image_get_type(b);
1123 cpl_image * a = cpl_image_new(nx, ny, type);
1126 case CPL_FILTER_MEDIAN:
1127 check_nomsg(cpl_image_filter(a, b, ker, CPL_FILTER_MEDIAN,
1128 CPL_BORDER_FILTER));
1130 case CPL_FILTER_LINEAR:
1131 check_nomsg(cpl_image_filter(a, b, ker, CPL_FILTER_LINEAR,
1132 CPL_BORDER_FILTER));
1134 case CPL_FILTER_STDEV:
1135 cpl_image_filter(a, b, ker, CPL_FILTER_STDEV, CPL_BORDER_FILTER);
1137 case CPL_FILTER_MORPHO:
1138 cpl_image_filter(a, b, ker, CPL_FILTER_MORPHO, CPL_BORDER_FILTER);
1151 sinfo_image_filter_linear(
const cpl_image *img,
const cpl_matrix * mx)
1153 return sinfo_image_filter_mode(img, mx, CPL_FILTER_LINEAR);
1157 cpl_image * sinfo_image_filter_linear2(
const cpl_image *img,
1158 const cpl_matrix * mx)
1160 return sinfo_image_filter_wrapper(img, mx, CPL_FILTER_LINEAR);
1165 cpl_image * sinfo_image_filter_median(
const cpl_image * img,
1166 const cpl_matrix * mx)
1168 return sinfo_image_filter_wrapper(img, mx, CPL_FILTER_MEDIAN);
#define sinfo_msg_error(...)
Print an error message.