SINFONI Pipeline Reference Manual  2.5.2
sinfo_utilities.c
1 /*
2  * This file is part of the ESO SINFONI Pipeline
3  * Copyright (C) 2004,2005 European Southern Observatory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18  */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
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"
35 /*----------------------------------------------------------------------------*/
45 /*----------------------------------------------------------------------------*/
46 
47 cpl_image*
48 sinfo_vector_to_image(const cpl_vector* vector,cpl_type type)
49 {
50  int i=0;
51  cpl_image* image=NULL;
52  int size=0;
53  const double* pv=NULL;
54  int* pi=NULL;
55  float* pf=NULL;
56  double* pd=NULL;
57 
58 
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);
64  for(i=0;i<size;i++) {
65  pi[i]=pv[i];
66  }
67  } else if (type == CPL_TYPE_FLOAT) {
68  pf=cpl_image_get_data_float(image);
69  for(i=0;i<size;i++) {
70  pf[i]=pv[i];
71  }
72  } else if (type == CPL_TYPE_DOUBLE) {
73  pd=cpl_image_get_data_double(image);
74  for(i=0;i<size;i++) {
75  pd[i]=pv[i];
76  }
77  } else {
78  assure( 0, CPL_ERROR_INVALID_TYPE,
79  "No CPL type to represent BITPIX = %d", type);
80  }
81 
82  cleanup:
83  if (cpl_error_get_code() != CPL_ERROR_NONE){
84  sinfo_free_image(&image);
85  }
86 
87  return image;
88 
89 }
90 
91 
92 cpl_error_code
93 sinfo_ima_line_cor(cpl_parameterlist * parlist, cpl_frameset* in)
94 {
95  int n=0;
96  int i=0;
97  int kappa=18;
98  int filt_rad=3;
99  int width=4;
100 
101  cpl_frame* frm=NULL;
102  const char* name=NULL;
103  const char* bname=NULL;
104 
105  cpl_image * ima=NULL ;
106  cpl_image * ima_out=NULL ;
107  cpl_parameter* p=NULL;
108  cpl_propertylist* plist=NULL;
109  char* tag=NULL;
110  char name_deb[80];
111 
112 
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)) ;
118 
119  n=cpl_frameset_get_size(in);
120 
121  for(i=0;i<n;i++) {
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));
126 
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,
130  &ima_out));
131  check_nomsg(plist=cpl_propertylist_load(name,0));
132  //sprintf(name_deb,"dark_org_%d.fits",i);
133  sprintf(name_deb,"org_%2.2d_%s",i,bname);
134 
135  check(cpl_image_save(ima,
136  name_deb,
137  CPL_BPP_IEEE_FLOAT,
138  plist,
139  CPL_IO_DEFAULT),
140  "Could not save product");
141 
142 
143  check(cpl_image_save(ima_out,
144  bname,
145  CPL_BPP_IEEE_FLOAT,
146  plist,
147  CPL_IO_DEFAULT),
148  "Could not save product");
149 
150  cpl_frame_set_filename(frm,bname);
151  sinfo_free_image(&ima);
152  sinfo_free_propertylist(&plist);
153  }
154 
155  }
156 
157  cleanup:
158  sinfo_free_image(&ima);
159  sinfo_free_propertylist(&plist);
160 
161  return cpl_error_get_code();
162 
163 }
164 
165 int
166 sinfo_table_column_dump(cpl_table* t, const char* name, cpl_type type)
167 {
168  int nrow=0;
169  int i=0;
170  int* pi=NULL;
171  float* pf=NULL;
172  double* pd=NULL;
173  char** ps=NULL;
174 
175  nrow=cpl_table_get_nrow(t);
176 
177  switch(type) {
178 
179  case CPL_TYPE_INT:
180  pi=cpl_table_get_data_int(t,name);
181  for(i=0;i<nrow;i++) {
182  sinfo_msg("val=%d",pi[i]);
183  }
184  break;
185  case CPL_TYPE_FLOAT:
186  pf=cpl_table_get_data_float(t,name);
187  for(i=0;i<nrow;i++) {
188  sinfo_msg("val=%g",pf[i]);
189  }
190  break;
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]);
195  }
196  break;
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]);
201  }
202  break;
203  default:
204  sinfo_msg_error("Wrong column type");
205  cpl_error_set(cpl_func, CPL_ERROR_TYPE_MISMATCH);
206  return 0;
207 
208  }
209  return 0;
210 }
211 
212 
213 
223 cpl_table*
224 sinfo_table_shift_column_spline3(cpl_table* t,
225  const char* col,
226  const double shift)
227 {
228  cpl_table* out=NULL;
229  int nrow=0;
230  int i=0;
231  int z=0;
232 
233  float sum=0;
234  float new_sum=0;
235 
236  float* pi=NULL;
237  float* po=NULL;
238  float* eval=NULL;
239  float* xnum=NULL;
240  float* spec=NULL;
241  float* corrected_spec=NULL;
242 
243  cknull(t,"null input table");
244  out=cpl_table_duplicate(t);
245 
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");
251 
252 
253 
254  xnum=cpl_calloc(nrow,sizeof(float)) ;
255  /* fill the xa[] array for the spline function */
256  for ( i = 0 ; i < nrow ; i++ ) {
257  xnum[i] = i ;
258  }
259 
260  spec=cpl_calloc(nrow,sizeof(float)) ;
261  corrected_spec=cpl_calloc(nrow,sizeof(float)) ;
262  eval=cpl_calloc(nrow,sizeof(float)) ;
263 
264  sum = 0. ;
265  for ( z = 0 ; z < nrow ; z++ ) {
266  spec[z] = pi[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 ;
272  }
273  spec[z] = 0. ;
274  }
275  sum += spec[z] ;
276  eval[z] = (float)shift+(float)z ;
277  }
278  /* now we do the spline interpolation*/
279  if ( -1 == sinfo_function1d_natural_spline(xnum,spec, nrow,
280  eval,corrected_spec, nrow))
281  {
282  sinfo_msg_error("error in spline interpolation!") ;
283  goto cleanup;
284  }
285 
286  new_sum = 0. ;
287  for ( z = 0 ; z < nrow ; z++ ) {
288  if ( isnan(corrected_spec[z]) ) {
289  continue ;
290  }
291  new_sum += corrected_spec[z] ;
292  }
293  /* fill output imagelist */
294  for ( z = 0 ; z < nrow ; z++ ) {
295  if ( new_sum == 0. ) new_sum =1. ;
296  {
297  if ( isnan(corrected_spec[z]) ) {
298  po[z] = ZERO ;
299  } else {
300  corrected_spec[z] *= sum / new_sum ;
301  po[z] = corrected_spec[z] ;
302  }
303  }
304  }
305 
306  sinfo_free_float(&xnum);
307  sinfo_free_float(&spec) ;
308  sinfo_free_float(&corrected_spec) ;
309  sinfo_free_float(&eval) ;
310 
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"));
315 
316  return out;
317  cleanup:
318 
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);
324  return NULL;
325 
326 
327 }
328 
329 
339 cpl_table*
340 sinfo_table_shift_column_int(const cpl_table* t,
341  const char* col,
342  const double s,
343  double* r)
344 {
345  cpl_table* out=NULL;
346  int is=(int)s;
347  int nrow=0;
348  int i=0;
349 
350  const double* pi=NULL;
351  double* po=NULL;
352 
353  cknull(t,"null input table");
354  out=cpl_table_duplicate(t);
355  *r=s-is;
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)) {
361  po[i-is]=pi[i];
362  }
363  }
364  return out;
365  cleanup:
366  sinfo_free_table(&out);
367  return NULL;
368 
369 }
370 
371 
382 cpl_table*
383 sinfo_table_shift_column_poly(cpl_table* t,
384  const char* col,
385  const double shift,
386  const int order)
387 {
388  cpl_table* out=NULL;
389  int nrow=0;
390  int i=0;
391  int flag=0;
392  int n_points=0;
393  int firstpos=0;
394  int z=0;
395  float eval=0;
396  float sum=0;
397  float new_sum=0;
398  float* pi=NULL;
399  float* po=NULL;
400  float* spec=NULL ;
401  float* corrected_spec=NULL ;
402  float* xnum=NULL ;
403  float* tableptr=NULL;
404 
405  cknull(t,"null input table");
406  if ( order <= 0 ) {
407  sinfo_msg_error("wrong order of interpolation polynom given!") ;
408  goto cleanup;
409  }
410 
411  out=cpl_table_duplicate(t);
412 
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");
418 
419  n_points = order + 1 ;
420  if ( n_points % 2 == 0 ) {
421  firstpos = (int)(n_points/2) - 1 ;
422  } else {
423  firstpos = (int)(n_points/2) ;
424  }
425  spec=cpl_calloc(nrow,sizeof(float)) ;
426  corrected_spec=cpl_calloc(nrow,sizeof(float)) ;
427  xnum=cpl_calloc(order+1,sizeof(float)) ;
428  /* fill the xa[] array for the polint function */
429  for ( i = 0 ; i < n_points ; i++ ) {
430  xnum[i] = i ;
431  }
432 
433 
434  for(i=0;i<nrow;i++) {
435  corrected_spec[i] = 0. ;
436  }
437 
438  sum = 0. ;
439  for ( z = 0 ; z < nrow ; z++ ) {
440  spec[z] = pi[z] ;
441  if (isnan(spec[z]) ) {
442  spec[z] = 0. ;
443 
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 ;
448  }
449  }
450  if ( z != 0 && z != nrow - 1 ) {
451  sum += spec[z] ;
452  }
453  }
454 
455  new_sum = 0. ;
456  for ( z = 0 ; z < nrow ; z++ ) {
457  /* ---------------------------------------------------------------
458  * now determine the arrays of size n_points with which the
459  * polynom is determined and determine the position eval
460  * where the polynom is evaluated in polynomial interpolation.
461  * Take care of the points near the row edges!
462  */
463  if (isnan(corrected_spec[z])) continue ;
464  if ( z - firstpos < 0 ) {
465  tableptr = &spec[0] ;
466  eval = shift + z ;
467  } else if ( z - firstpos + n_points >= nrow ) {
468  tableptr = &spec[nrow - n_points] ;
469  eval = shift + z + n_points - nrow ;
470  } else {
471  tableptr = &spec[z-firstpos] ;
472  eval = shift + firstpos ;
473  }
474 
475  flag=0;
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] ;
479  }
480  }
481 
482  /* fill the output spectrum */
483  for (z = 0 ; z < nrow ; z++ ) {
484  if ( new_sum == 0. ) {
485  new_sum = 1. ;
486  }
487  if ( z == 0 ) {
488  po[z] = ZERO ;
489  } else if ( z == nrow - 1 ) {
490  po[z] = ZERO ;
491  } else if ( isnan(corrected_spec[z]) ) {
492  po[z] = ZERO ;
493  } else {
494  corrected_spec[z] *= sum / new_sum ;
495  po[z] = corrected_spec[z] ;
496  }
497  }
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"));
502 
503  sinfo_free_float(&spec) ;
504  sinfo_free_float(&corrected_spec) ;
505  sinfo_free_float(&xnum) ;
506 
507  return out;
508  cleanup:
509 
510 
511  sinfo_free_float(&spec) ;
512  sinfo_free_float(&corrected_spec) ;
513  sinfo_free_float(&xnum) ;
514  sinfo_free_table(&out);
515  return NULL;
516 
517 
518 }
519 
520 
521 
522 
523 void sinfo_new_array_set_value( float * array, float value, int i )
524 {
525  array[i] = value ;
526 }
527 float sinfo_new_array_get_value( float * array, int i )
528 {
529  return array[i] ;
530 }
531 
532 
533 
534 void sinfo_new_destroy_array(float ** array)
535 {
536  if(*array != NULL) {
537  cpl_free( *array ) ;
538  *array = NULL;
539  }
540 }
541 
542 void sinfo_new_destroy_stringarray(char ** array, int size_x)
543 {
544  int i ;
545 
546  for ( i = 0 ; i < size_x ; i++ )
547  {
548  cpl_free( array[i] ) ;
549  }
550  cpl_free( array ) ;
551 }
552 
553 void sinfo_new_destroy_2Dintarray(int *** array, int size_x)
554 {
555  int i ;
556 
557  if((*array) != NULL) {
558  for ( i = 0 ; i < size_x ; i++ ) {
559  if((*array)[i] != NULL) {
560  cpl_free( (*array)[i] );
561  (*array)[i]=NULL;
562  }
563  }
564  cpl_free( *array ) ;
565  *array=NULL;
566  }
567 
568 }
569 
570 
571 void sinfo_new_intarray_set_value( int * array, int value, int i )
572 {
573  array[i] = value ;
574 }
575 float sinfo_new_array2D_get_value( float ** array, int x, int y )
576 {
577  return array[x][y] ;
578 }
579 int ** sinfo_new_2Dintarray( int size_x, int size_y)
580 {
581  int ** retVal ;
582  int i ;
583 
584  retVal = (int **) cpl_calloc( size_x, sizeof (int*) ) ;
585  for ( i = 0 ; i < size_x ; i++ )
586  {
587  retVal[i] = (int *) cpl_calloc( size_y, sizeof (int)) ;
588  }
589  return retVal ;
590 }
591 
592 float * sinfo_new_floatarray( int size)
593 {
594  return (float *) cpl_calloc( size, sizeof (float) ) ;
595 }
596 
597 
598 void sinfo_new_destroy_2Dfloatarray(float *** array, int size_x)
599 {
600  int i ;
601  if((*array) != NULL) {
602  for ( i = 0 ; i < size_x ; i++ ) {
603  if((*array)[i] != NULL) {
604  cpl_free( (*array)[i] );
605  (*array)[i]=NULL;
606  }
607  }
608  cpl_free( *array ) ;
609  *array=NULL;
610  }
611 }
612 
613 void sinfo_new_destroy_2Ddoublearray(double *** array, int size_x)
614 {
615  int i ;
616 
617  if((*array) != NULL) {
618  for ( i = 0 ; i < size_x ; i++ ) {
619  if((*array)[i] != NULL) {
620  cpl_free( (*array)[i] );
621  (*array)[i]=NULL;
622  }
623  }
624  cpl_free( *array ) ;
625  *array=NULL;
626  }
627 
628 }
629 
630 
631 void sinfo_new_array2D_set_value(float ** array,float value,int x,int y)
632 {
633  array[x][y] = value ;
634 }
635 
636 double sinfo_new_doublearray_get_value( double * array, int i )
637 {
638  return array[i] ;
639 }
640 void sinfo_new_doublearray_set_value( double * array, double value, int i )
641 {
642  array[i] = value ;
643 }
644 
645 void sinfo_new_destroy_doublearray(double * array)
646 {
647  cpl_free( array ) ;
648 }
649 double * sinfo_new_doublearray( int size)
650 {
651  return (double *) cpl_calloc( size, sizeof (double) ) ;
652 }
653 
654 double ** sinfo_new_2Ddoublearray( int size_x, int size_y)
655 {
656  double ** retVal ;
657  int i ;
658 
659  retVal = (double **) cpl_calloc( size_x, sizeof (double*) ) ;
660  for ( i = 0 ; i < size_x ; i++ )
661  {
662  retVal[i] = (double *) cpl_calloc( size_y, sizeof (double)) ;
663  }
664  return retVal ;
665 }
666 
667 float ** sinfo_new_2Dfloatarray( int size_x, int size_y)
668 {
669  float ** retVal ;
670  int i ;
671 
672  retVal = (float **) cpl_calloc( size_x, sizeof (float*) ) ;
673  for ( i = 0 ; i < size_x ; i++ )
674  {
675  retVal[i] = (float *) cpl_calloc( size_y, sizeof (float)) ;
676  }
677  return retVal ;
678 }
679 
680 
681 int * sinfo_new_intarray( int size)
682 {
683  return (int *) cpl_calloc( size, sizeof (int) ) ;
684 }
685 void sinfo_new_destroy_intarray(int ** array)
686 {
687  cpl_free( *array ) ;
688  *array=NULL;
689 }
690 
691 int sinfo_new_intarray_get_value( int * array, int i )
692 {
693  return array[i] ;
694 }
695 
696 float sinfo_new_Stats_get_cleanstdev(Stats * stats)
697 {
698  return stats -> cleanstdev ;
699 }
700 float sinfo_new_Stats_get_cleanmean(Stats * stats)
701 {
702  return stats -> cleanmean ;
703 }
704 
705 char * sinfo_new_get_basename(const char *filename)
706 {
707  char *p ;
708  p = strrchr (filename, '/');
709  return p ? p + 1 : (char *) filename;
710 }
711 
712 
713 
714 char * sinfo_new_get_rootname(const char * filename)
715 {
716  static char path[MAX_NAME_SIZE+1];
717  char * lastdot ;
718 
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")))
730  {
731  lastdot[0] = (char)0;
732  }
733  return path ;
734 }
735 
736 
737 
738 /*----------------------------------------------------------------------------*/
746 /*----------------------------------------------------------------------------*/
747 cpl_imagelist * sinfo_new_frameset_to_iset(cpl_frameset * fset)
748 {
749  cpl_imagelist * iset=NULL ;
750  char ** filenames ;
751  int nfiles=0 ;
752 
753  /* Test entries */
754  if (fset == NULL) return NULL ;
755 
756  /* Get the filenames */
757  if ((filenames = sinfo_new_frameset_to_filenames(fset, &nfiles)) == NULL) {
758  sinfo_msg_error( "Cannot get the files names") ;
759  return NULL ;
760  }
761  /* Load image set */
762  if ((iset = sinfo_new_imagelist_load_frameset(fset,
763  CPL_TYPE_FLOAT, 0, 0)) == NULL) {
764  sinfo_msg_error( "Cannot load *** the image set") ;
765  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
766 
767  cpl_free(filenames) ;
768  return NULL ;
769  }
770 
771  /* Free and Return */
772  cpl_free(filenames) ;
773  return iset ;
774 }
775 #include "cpl_imagelist_io.h"
776 
777 /*----------------------------------------------------------------------------*/
788 /*----------------------------------------------------------------------------*/
789 cpl_imagelist *
790 sinfo_new_imagelist_load_frameset(const cpl_frameset * frameset,
791  cpl_type type,
792  int pnum,
793  int extnum)
794 {
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);
799  int i;
800 
801  /* Require imagelist to contain at least one image */
802  cpl_ensure(nz > 0, CPL_ERROR_DATA_NOT_FOUND, NULL);
803 
804  for (i = 0; frame != NULL;
805  i++, frame = cpl_frameset_get_next_const(frameset)) {
806 
807  const char * name = cpl_frame_get_filename(frame);
808  if (name == NULL) break; /* Error check */
809 
810 
811  image = cpl_image_load(name, type, pnum, extnum);
812 
813  if (image == NULL) break; /* Error check */
814 
815  if (i == 0) {
816  const int nx = cpl_image_get_size_x(image);
817  const int ny = cpl_image_get_size_y(image);
818 
819  if (nx < 1 || ny < 1) break; /* Error check */
820  imagelist = cpl_imagelist_new();
821  if (imagelist == NULL) break; /* Error check */
822  }
823 
824  if (cpl_imagelist_set(imagelist, image, i)) break;
825  image = NULL; /* Image is now part of the imagelist */
826 
827  }
828 
829  if (i != nz) {
830  /* Error handling */
831  cpl_image_delete(image);
832  cpl_imagelist_delete(imagelist);
833  imagelist = NULL;
834  }
835  return imagelist;
836 
837 }
838 
849 char ** sinfo_new_frameset_to_filenames(cpl_frameset *set, int *nfiles)
850 {
851  char **filenames=NULL;
852 
853  int nbframes=0;
854  int i=0;
855 
856  cpl_frame *curr_frame;
857 
858  if (set == NULL) {
859  return NULL;
860  }
861 
862  nbframes = cpl_frameset_get_size(set);
863 
864  if (nbframes < 1) {
865  return NULL;
866  }
867  /*
868  * Create the list of filenames and fill it
869  */
870  filenames = cpl_malloc(nbframes * sizeof(char *));
871 
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);
876  }
877 
878 
879  /*
880  * Set the number of files found
881  */
882  *nfiles = nbframes;
883 
884  return filenames;
885 
886 }
887 
888 
889 /*---------------------------------------------------------------------------*/
905 /*----------------------------------------------------------------------------*/
906 double sinfo_spline_hermite(double xp,
907  const double *x,
908  const double *y,
909  int n,
910  int *istart )
911 {
912  double yp1, yp2, yp = 0;
913  double xpi, xpi1, l1, l2, lp1, lp2;
914  int i;
915 
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;
918 
919  if ( x[0] <= x[n-1] )
920  {
921  for ( i = (*istart)+1; i <= n && xp >= x[i-1]; i++ )
922  ;
923  }
924  else
925  {
926  for ( i = (*istart)+1; i <= n && xp <= x[i-1]; i++ )
927  ;
928  }
929 
930  *istart = i;
931  i--;
932 
933  lp1 = 1.0 / (x[i-1] - x[i]);
934  lp2 = -lp1;
935 
936  if ( i == 1 )
937  {
938  yp1 = (y[1] - y[0]) / (x[1] - x[0]);
939  }
940  else
941  {
942  yp1 = (y[i] - y[i-2]) / (x[i] - x[i-2]);
943  }
944 
945  if ( i >= n - 1 )
946  {
947  yp2 = (y[n-1] - y[n-2]) / (x[n-1] - x[n-2]);
948  }
949  else
950  {
951  yp2 = (y[i+1] - y[i-1]) / (x[i+1] - x[i-1]);
952  }
953 
954  xpi1 = xp - x[i];
955  xpi = xp - x[i-1];
956  l1 = xpi1*lp1;
957  l2 = xpi*lp2;
958 
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;
962 
963  return yp;
964 }
965 
972 cpl_error_code update_bad_pixel_map(cpl_image* im)
973 {
974  int szx = cpl_image_get_size_x(im);
975  int szy = cpl_image_get_size_y(im);
976  int x = 0;
977  cpl_mask* bpm = cpl_image_get_bpm(im);
978 
979  for (x = 1; x <=szx; x++)
980  {
981  int y = 0;
982  for(y = 1; y <= szy; y++)
983  {
984  int isnull = 0;
985  double value = cpl_image_get(im, x, y, &isnull);
986  if (isnan(value))
987  {
988  cpl_mask_set(bpm, x, y, CPL_BINARY_1);
989  }
990  }
991  }
992  return cpl_error_get_code();
993 }
994 cpl_polynomial * sinfo_polynomial_fit_2d_create(cpl_bivector * xy_pos,
995  cpl_vector * values,
996  cpl_size degree,
997  double * mse)
998 {
999  typedef double* (*get_data)(cpl_bivector*);
1000  get_data data_extractor[2] = { &cpl_bivector_get_x_data,
1001  &cpl_bivector_get_y_data};
1002  //samppos matrix must
1003  // have two rows with copies of the two vectors in the x_pos bivector.
1004 
1005  double rechisq = 0;
1006  int i, j;
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);
1011 
1012  samppos2d = cpl_matrix_new(2, xy_size);
1013  for (i = 0; i < 2; i++)
1014  {
1015  for (j = 0; j < xy_size; j++)
1016  {
1017  double value = data_extractor[i](xy_pos)[j];
1018  cpl_matrix_set(samppos2d, i, j, value);
1019  }
1020  }
1021 
1022  cpl_polynomial_fit(fit2d, samppos2d, NULL, values, NULL, CPL_FALSE,
1023  NULL, &degree);
1024 
1025  fitresidual = cpl_vector_new(xy_size);
1026  cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit2d,
1027  samppos2d, &rechisq);
1028  if (mse)
1029  {
1030  *mse = cpl_vector_product(fitresidual, fitresidual)
1031  / cpl_vector_get_size(fitresidual);
1032  }
1033  cpl_matrix_delete(samppos2d);
1034  cpl_vector_delete(fitresidual);
1035  return fit2d;
1036 }
1037 
1038 cpl_polynomial * sinfo_polynomial_fit_1d_create(
1039  const cpl_vector * x_pos,
1040  const cpl_vector * values,
1041  int degree,
1042  double * mse
1043 )
1044 {
1045  cpl_polynomial * fit1d = cpl_polynomial_new(1);
1046  // cpl_vector* x_copy = cpl_vector_duplicate(x_pos);
1047  // cpl_vector* values_copy = cpl_vector_duplicate(values);
1048  int x_size = cpl_vector_get_size(x_pos);
1049  double rechisq = 0;
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);
1054 
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,
1059  samppos, &rechisq);
1060  cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1061  if (mse)
1062  {
1063  *mse = cpl_vector_product(fitresidual, fitresidual)
1064  / cpl_vector_get_size(fitresidual);
1065  }
1066  cpl_matrix_unwrap(samppos);
1067  cpl_vector_delete(fitresidual);
1068  return fit1d;
1069 }
1070 
1071 //cpl_image * sinfo_image_filter_
1072 static cpl_image * sinfo_image_filter_wrapper(const cpl_image *b,
1073  const cpl_matrix *k,
1074  cpl_filter_mode mode)
1075 {
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);
1081  int i, j;
1082  cpl_type type = cpl_image_get_type(b);
1083  cpl_image * a = cpl_image_new(nx, ny, type);
1084  // where m is a cpl_mask with a CPL_BINARY_1 whereever k has a 1.0.
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++)
1089  {
1090  for (j = 0; j < nrow ; j++)
1091  {
1092  double value = cpl_matrix_get(k, j, i);
1093  if (fabs(value - 1.0) < EPSILON)
1094  {
1095  cpl_mask_set(m, i + 1, j + 1, CPL_BINARY_1);
1096  }
1097  }
1098  }
1099 
1100  cpl_image_filter_mask(a, b, m, mode, CPL_BORDER_FILTER);
1101  cpl_mask_delete(m);
1102  return a;
1103 }
1104 
1105 
1106 static cpl_image*
1107 sinfo_image_filter_mode(const cpl_image* b,
1108  const cpl_matrix * ker,
1109  cpl_filter_mode filter)
1110 {
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);
1115 
1116  switch(filter) {
1117  case CPL_FILTER_MEDIAN:
1118  check_nomsg(cpl_image_filter(a, b, ker, CPL_FILTER_MEDIAN,
1119  CPL_BORDER_FILTER));
1120  break;
1121  case CPL_FILTER_LINEAR:
1122  check_nomsg(cpl_image_filter(a, b, ker, CPL_FILTER_LINEAR,
1123  CPL_BORDER_FILTER));
1124  break;
1125  case CPL_FILTER_STDEV:
1126  cpl_image_filter(a, b, ker, CPL_FILTER_STDEV, CPL_BORDER_FILTER);
1127  break;
1128  case CPL_FILTER_MORPHO:
1129  cpl_image_filter(a, b, ker, CPL_FILTER_MORPHO, CPL_BORDER_FILTER);
1130  break;
1131  default:
1132  sinfo_msg_error("Filter type not supported");
1133  return NULL;
1134  }
1135  cleanup:
1136 
1137  return a;
1138 
1139 }
1140 
1141 cpl_image *
1142 sinfo_image_filter_linear(const cpl_image *img, const cpl_matrix * mx)
1143 {
1144  return sinfo_image_filter_mode(img, mx, CPL_FILTER_LINEAR);
1145 
1146 }
1147 
1148 cpl_image * sinfo_image_filter_linear2(const cpl_image *img,
1149  const cpl_matrix * mx)
1150 {
1151  return sinfo_image_filter_wrapper(img, mx, CPL_FILTER_LINEAR);
1152 
1153 }
1154 
1155 
1156 cpl_image * sinfo_image_filter_median(const cpl_image * img,
1157  const cpl_matrix * mx)
1158 {
1159  return sinfo_image_filter_wrapper(img, mx, CPL_FILTER_MEDIAN);
1160 }
1161 
1162