SINFONI Pipeline Reference Manual  2.6.0
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 /* TODO: not used */
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 /* TODO: not used */
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 
222 /* TODO: not used */
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 /* TODO: not used */
340 cpl_table*
341 sinfo_table_shift_column_int(const cpl_table* t,
342  const char* col,
343  const double s,
344  double* r)
345 {
346  cpl_table* out=NULL;
347  int is=(int)s;
348  int nrow=0;
349  int i=0;
350 
351  const double* pi=NULL;
352  double* po=NULL;
353 
354  cknull(t,"null input table");
355  out=cpl_table_duplicate(t);
356  *r=s-is;
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)) {
362  po[i-is]=pi[i];
363  }
364  }
365  return out;
366  cleanup:
367  sinfo_free_table(&out);
368  return NULL;
369 
370 }
371 
372 
382 /* TODO: not used */
383 cpl_table*
384 sinfo_table_shift_column_poly(cpl_table* t,
385  const char* col,
386  const double shift,
387  const int order)
388 {
389  cpl_table* out=NULL;
390  int nrow=0;
391  int i=0;
392  int flag=0;
393  int n_points=0;
394  int firstpos=0;
395  int z=0;
396  float eval=0;
397  float sum=0;
398  float new_sum=0;
399  float* pi=NULL;
400  float* po=NULL;
401  float* spec=NULL ;
402  float* corrected_spec=NULL ;
403  float* xnum=NULL ;
404  float* tableptr=NULL;
405 
406  cknull(t,"null input table");
407  if ( order <= 0 ) {
408  sinfo_msg_error("wrong order of interpolation polynom given!") ;
409  goto cleanup;
410  }
411 
412  out=cpl_table_duplicate(t);
413 
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");
419 
420  n_points = order + 1 ;
421  if ( n_points % 2 == 0 ) {
422  firstpos = (int)(n_points/2) - 1 ;
423  } else {
424  firstpos = (int)(n_points/2) ;
425  }
426  spec=cpl_calloc(nrow,sizeof(float)) ;
427  corrected_spec=cpl_calloc(nrow,sizeof(float)) ;
428  xnum=cpl_calloc(order+1,sizeof(float)) ;
429  /* fill the xa[] array for the polint function */
430  for ( i = 0 ; i < n_points ; i++ ) {
431  xnum[i] = i ;
432  }
433 
434 
435  for(i=0;i<nrow;i++) {
436  corrected_spec[i] = 0. ;
437  }
438 
439  sum = 0. ;
440  for ( z = 0 ; z < nrow ; z++ ) {
441  spec[z] = pi[z] ;
442  if (isnan(spec[z]) ) {
443  spec[z] = 0. ;
444 
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 ;
449  }
450  }
451  if ( z != 0 && z != nrow - 1 ) {
452  sum += spec[z] ;
453  }
454  }
455 
456  new_sum = 0. ;
457  for ( z = 0 ; z < nrow ; z++ ) {
458  /* ---------------------------------------------------------------
459  * now determine the arrays of size n_points with which the
460  * polynom is determined and determine the position eval
461  * where the polynom is evaluated in polynomial interpolation.
462  * Take care of the points near the row edges!
463  */
464  if (isnan(corrected_spec[z])) continue ;
465  if ( z - firstpos < 0 ) {
466  tableptr = &spec[0] ;
467  eval = shift + z ;
468  } else if ( z - firstpos + n_points >= nrow ) {
469  tableptr = &spec[nrow - n_points] ;
470  eval = shift + z + n_points - nrow ;
471  } else {
472  tableptr = &spec[z-firstpos] ;
473  eval = shift + firstpos ;
474  }
475 
476  flag=0;
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] ;
480  }
481  }
482 
483  /* fill the output spectrum */
484  for (z = 0 ; z < nrow ; z++ ) {
485  if ( new_sum == 0. ) {
486  new_sum = 1. ;
487  }
488  if ( z == 0 ) {
489  po[z] = ZERO ;
490  } else if ( z == nrow - 1 ) {
491  po[z] = ZERO ;
492  } else if ( isnan(corrected_spec[z]) ) {
493  po[z] = ZERO ;
494  } else {
495  corrected_spec[z] *= sum / new_sum ;
496  po[z] = corrected_spec[z] ;
497  }
498  }
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"));
503 
504  sinfo_free_float(&spec) ;
505  sinfo_free_float(&corrected_spec) ;
506  sinfo_free_float(&xnum) ;
507 
508  return out;
509  cleanup:
510 
511 
512  sinfo_free_float(&spec) ;
513  sinfo_free_float(&corrected_spec) ;
514  sinfo_free_float(&xnum) ;
515  sinfo_free_table(&out);
516  return NULL;
517 
518 
519 }
520 
521 
522 
523 
524 void sinfo_new_array_set_value( float * array, float value, int i )
525 {
526  array[i] = value ;
527 }
528 float sinfo_new_array_get_value( float * array, int i )
529 {
530  return array[i] ;
531 }
532 
533 
534 
535 void sinfo_new_destroy_array(float ** array)
536 {
537  if(*array != NULL) {
538  cpl_free( *array ) ;
539  *array = NULL;
540  }
541 }
542 /* TODO: not used */
543 void sinfo_new_destroy_stringarray(char ** array, int size_x)
544 {
545  int i ;
546 
547  for ( i = 0 ; i < size_x ; i++ )
548  {
549  cpl_free( array[i] ) ;
550  }
551  cpl_free( array ) ;
552 }
553 
554 void sinfo_new_destroy_2Dintarray(int *** array, int size_x)
555 {
556 
557  if((*array) != NULL) {
558  for ( int 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  if((*array) != NULL) {
601  for ( int i = 0 ; i < size_x ; i++ ) {
602  if((*array)[i] != NULL) {
603  cpl_free( (*array)[i] );
604  (*array)[i]=NULL;
605  }
606  }
607  cpl_free( *array ) ;
608  *array=NULL;
609  }
610 }
611 
612 void sinfo_new_destroy_2Ddoublearray(double *** array, int size_x)
613 {
614 
615  if((*array) != NULL) {
616  for ( int i = 0 ; i < size_x ; i++ ) {
617  if((*array)[i] != NULL) {
618  cpl_free( (*array)[i] );
619  (*array)[i]=NULL;
620  }
621  }
622  cpl_free( *array ) ;
623  *array=NULL;
624  }
625 
626 }
627 
628 
629 void sinfo_new_array2D_set_value(float ** array,float value,int x,int y)
630 {
631  array[x][y] = value ;
632 }
633 
634 double sinfo_new_doublearray_get_value( double * array, int i )
635 {
636  return array[i] ;
637 }
638 void sinfo_new_doublearray_set_value( double * array, double value, int i )
639 {
640  array[i] = value ;
641 }
642 
643 void sinfo_new_destroy_doublearray(double * array)
644 {
645  cpl_free( array ) ;
646 }
647 double * sinfo_new_doublearray( int size)
648 {
649  return (double *) cpl_calloc( size, sizeof (double) ) ;
650 }
651 
652 double ** sinfo_new_2Ddoublearray( int size_x, int size_y)
653 {
654  double ** retVal ;
655  int i ;
656 
657  retVal = (double **) cpl_calloc( size_x, sizeof (double*) ) ;
658  for ( i = 0 ; i < size_x ; i++ )
659  {
660  retVal[i] = (double *) cpl_calloc( size_y, sizeof (double)) ;
661  }
662  return retVal ;
663 }
664 
665 float ** sinfo_new_2Dfloatarray( int size_x, int size_y)
666 {
667  float ** retVal ;
668  int i ;
669 
670  retVal = (float **) cpl_calloc( size_x, sizeof (float*) ) ;
671  for ( i = 0 ; i < size_x ; i++ )
672  {
673  retVal[i] = (float *) cpl_calloc( size_y, sizeof (float)) ;
674  }
675  return retVal ;
676 }
677 
678 
679 int * sinfo_new_intarray( int size)
680 {
681  return (int *) cpl_calloc( size, sizeof (int) ) ;
682 }
683 void sinfo_new_destroy_intarray(int ** array)
684 {
685  cpl_free( *array ) ;
686  *array=NULL;
687 }
688 /* TODO: not used */
689 int sinfo_new_intarray_get_value( int * array, int i )
690 {
691  return array[i] ;
692 }
693 /* TODO: not used */
694 float sinfo_new_Stats_get_cleanstdev(Stats * stats)
695 {
696  return stats -> cleanstdev ;
697 }
698 /* TODO: not used */
699 float sinfo_new_Stats_get_cleanmean(Stats * stats)
700 {
701  return stats -> cleanmean ;
702 }
703 
704 char * sinfo_new_get_basename(const char *filename)
705 {
706  char *p ;
707  p = strrchr (filename, '/');
708  return p ? p + 1 : (char *) filename;
709 }
710 
711 
712 
713 char * sinfo_new_get_rootname(const char * filename)
714 {
715  static char path[MAX_NAME_SIZE+1];
716  char * lastdot ;
717 
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")))
729  {
730  lastdot[0] = (char)0;
731  }
732  return path ;
733 }
734 
735 
736 
737 /*----------------------------------------------------------------------------*/
745 /*----------------------------------------------------------------------------*/
746 cpl_imagelist * sinfo_new_frameset_to_iset(cpl_frameset * fset)
747 {
748  cpl_imagelist * iset=NULL ;
749  char ** filenames ;
750  int nfiles=0 ;
751 
752  /* Test entries */
753  if (fset == NULL) return NULL ;
754 
755  /* Get the filenames */
756  if ((filenames = sinfo_new_frameset_to_filenames(fset, &nfiles)) == NULL) {
757  sinfo_msg_error( "Cannot get the files names") ;
758  return NULL ;
759  }
760  /* Load image set */
761  if ((iset = sinfo_new_imagelist_load_frameset(fset,
762  CPL_TYPE_FLOAT, 0, 0)) == NULL) {
763  sinfo_msg_error( "Cannot load *** the image set") ;
764  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
765 
766  cpl_free(filenames) ;
767  return NULL ;
768  }
769 
770  /* Free and Return */
771  cpl_free(filenames) ;
772  return iset ;
773 }
774 #include "cpl_imagelist_io.h"
775 
776 /*----------------------------------------------------------------------------*/
787 /*----------------------------------------------------------------------------*/
788 cpl_imagelist *
789 sinfo_new_imagelist_load_frameset(const cpl_frameset * frameset,
790  cpl_type type,
791  int pnum,
792  int extnum)
793 {
794  cpl_image * image = NULL;
795  cpl_imagelist * imagelist = NULL;
796 
797  cpl_frameset_iterator* it = cpl_frameset_iterator_new(frameset);
798  const cpl_frame *frame = cpl_frameset_iterator_get_const(it);
799 
800  const int nz = cpl_frameset_get_size(frameset);
801  int i;
802 
803  /* Require imagelist to contain at least one image */
804  cpl_ensure(nz > 0, CPL_ERROR_DATA_NOT_FOUND, NULL);
805 
806  for (i = 0; i<nz; i++) {
807 
808  const char * name = cpl_frame_get_filename(frame);
809  if (name == NULL) break; /* Error check */
810 
811 
812  image = cpl_image_load(name, type, pnum, extnum);
813 
814  if (image == NULL) break; /* Error check */
815 
816  if (i == 0) {
817  const int nx = cpl_image_get_size_x(image);
818  const int ny = cpl_image_get_size_y(image);
819 
820  if (nx < 1 || ny < 1) break; /* Error check */
821  imagelist = cpl_imagelist_new();
822  if (imagelist == NULL) break; /* Error check */
823  }
824 
825  if (cpl_imagelist_set(imagelist, image, i)) break;
826  image = NULL; /* Image is now part of the imagelist */
827  cpl_frameset_iterator_advance(it, 1);
828  frame = cpl_frameset_iterator_get_const(it);
829 
830  }
831 
832  if (i != nz) {
833  /* Error handling */
834  cpl_image_delete(image);
835  cpl_imagelist_delete(imagelist);
836  imagelist = NULL;
837  }
838  cpl_frameset_iterator_delete(it);
839  return imagelist;
840 
841 }
842 
853 char ** sinfo_new_frameset_to_filenames(cpl_frameset *set, int *nfiles)
854 {
855  char **filenames=NULL;
856 
857  int nbframes=0;
858  int i=0;
859 
860  //cpl_frame *curr_frame;
861 
862  if (set == NULL) {
863  return NULL;
864  }
865 
866  nbframes = cpl_frameset_get_size(set);
867 
868  if (nbframes < 1) {
869  return NULL;
870  }
871  /*
872  * Create the list of filenames and fill it
873  */
874  filenames = cpl_malloc(nbframes * sizeof(char *));
875 
876  cpl_frameset_iterator* it = cpl_frameset_iterator_new(set);
877  const cpl_frame *curr_frame = cpl_frameset_iterator_get(it);
878 
879  for (i = 0; i < nbframes; i++) {
880  filenames[i]=(char*) cpl_frame_get_filename(curr_frame);
881 
882  cpl_frameset_iterator_advance(it, 1);
883  curr_frame = cpl_frameset_iterator_get_const(it);
884  }
885  cpl_frameset_iterator_delete(it);
886 
887  /*
888  * Set the number of files found
889  */
890  *nfiles = nbframes;
891 
892  return filenames;
893 
894 }
895 
896 
897 /*---------------------------------------------------------------------------*/
913 /*----------------------------------------------------------------------------*/
914 /* TODO: not used */
915 double sinfo_spline_hermite(double xp,
916  const double *x,
917  const double *y,
918  int n,
919  int *istart )
920 {
921  double yp1, yp2, yp = 0;
922  double xpi, xpi1, l1, l2, lp1, lp2;
923  int i;
924 
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;
927 
928  if ( x[0] <= x[n-1] )
929  {
930  for ( i = (*istart)+1; i <= n && xp >= x[i-1]; i++ )
931  ;
932  }
933  else
934  {
935  for ( i = (*istart)+1; i <= n && xp <= x[i-1]; i++ )
936  ;
937  }
938 
939  *istart = i;
940  i--;
941 
942  lp1 = 1.0 / (x[i-1] - x[i]);
943  lp2 = -lp1;
944 
945  if ( i == 1 )
946  {
947  yp1 = (y[1] - y[0]) / (x[1] - x[0]);
948  }
949  else
950  {
951  yp1 = (y[i] - y[i-2]) / (x[i] - x[i-2]);
952  }
953 
954  if ( i >= n - 1 )
955  {
956  yp2 = (y[n-1] - y[n-2]) / (x[n-1] - x[n-2]);
957  }
958  else
959  {
960  yp2 = (y[i+1] - y[i-1]) / (x[i+1] - x[i-1]);
961  }
962 
963  xpi1 = xp - x[i];
964  xpi = xp - x[i-1];
965  l1 = xpi1*lp1;
966  l2 = xpi*lp2;
967 
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;
971 
972  return yp;
973 }
974 
981 cpl_error_code update_bad_pixel_map(cpl_image* im)
982 {
983  int szx = cpl_image_get_size_x(im);
984  int szy = cpl_image_get_size_y(im);
985  int x = 0;
986  cpl_mask* bpm = cpl_image_get_bpm(im);
987 
988  for (x = 1; x <=szx; x++)
989  {
990  int y = 0;
991  for(y = 1; y <= szy; y++)
992  {
993  int isnull = 0;
994  double value = cpl_image_get(im, x, y, &isnull);
995  if (isnan(value))
996  {
997  cpl_mask_set(bpm, x, y, CPL_BINARY_1);
998  }
999  }
1000  }
1001  return cpl_error_get_code();
1002 }
1003 cpl_polynomial * sinfo_polynomial_fit_2d_create(cpl_bivector * xy_pos,
1004  cpl_vector * values,
1005  cpl_size degree,
1006  double * mse)
1007 {
1008  typedef double* (*get_data)(cpl_bivector*);
1009  get_data data_extractor[2] = { &cpl_bivector_get_x_data,
1010  &cpl_bivector_get_y_data};
1011  //samppos matrix must
1012  // have two rows with copies of the two vectors in the x_pos bivector.
1013 
1014  double rechisq = 0;
1015  int i, j;
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);
1020 
1021  samppos2d = cpl_matrix_new(2, xy_size);
1022  for (i = 0; i < 2; i++)
1023  {
1024  for (j = 0; j < xy_size; j++)
1025  {
1026  double value = data_extractor[i](xy_pos)[j];
1027  cpl_matrix_set(samppos2d, i, j, value);
1028  }
1029  }
1030 
1031  cpl_polynomial_fit(fit2d, samppos2d, NULL, values, NULL, CPL_FALSE,
1032  NULL, &degree);
1033 
1034  fitresidual = cpl_vector_new(xy_size);
1035  cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit2d,
1036  samppos2d, &rechisq);
1037  if (mse)
1038  {
1039  *mse = cpl_vector_product(fitresidual, fitresidual)
1040  / cpl_vector_get_size(fitresidual);
1041  }
1042  cpl_matrix_delete(samppos2d);
1043  cpl_vector_delete(fitresidual);
1044  return fit2d;
1045 }
1046 
1047 cpl_polynomial * sinfo_polynomial_fit_1d_create(
1048  const cpl_vector * x_pos,
1049  const cpl_vector * values,
1050  int degree,
1051  double * mse
1052 )
1053 {
1054  cpl_polynomial * fit1d = cpl_polynomial_new(1);
1055  // cpl_vector* x_copy = cpl_vector_duplicate(x_pos);
1056  // cpl_vector* values_copy = cpl_vector_duplicate(values);
1057  int x_size = cpl_vector_get_size(x_pos);
1058  double rechisq = 0;
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);
1063 
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,
1068  samppos, &rechisq);
1069  cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1070  if (mse)
1071  {
1072  *mse = cpl_vector_product(fitresidual, fitresidual)
1073  / cpl_vector_get_size(fitresidual);
1074  }
1075  cpl_matrix_unwrap(samppos);
1076  cpl_vector_delete(fitresidual);
1077  return fit1d;
1078 }
1079 
1080 //cpl_image * sinfo_image_filter_
1081 static cpl_image * sinfo_image_filter_wrapper(const cpl_image *b,
1082  const cpl_matrix *k,
1083  cpl_filter_mode mode)
1084 {
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);
1090  int i, j;
1091  cpl_type type = cpl_image_get_type(b);
1092  cpl_image * a = cpl_image_new(nx, ny, type);
1093  // where m is a cpl_mask with a CPL_BINARY_1 whereever k has a 1.0.
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++)
1098  {
1099  for (j = 0; j < nrow ; j++)
1100  {
1101  double value = cpl_matrix_get(k, j, i);
1102  if (fabs(value - 1.0) < EPSILON)
1103  {
1104  cpl_mask_set(m, i + 1, j + 1, CPL_BINARY_1);
1105  }
1106  }
1107  }
1108 
1109  cpl_image_filter_mask(a, b, m, mode, CPL_BORDER_FILTER);
1110  cpl_mask_delete(m);
1111  return a;
1112 }
1113 
1114 
1115 static cpl_image*
1116 sinfo_image_filter_mode(const cpl_image* b,
1117  const cpl_matrix * ker,
1118  cpl_filter_mode filter)
1119 {
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);
1124 
1125  switch(filter) {
1126  case CPL_FILTER_MEDIAN:
1127  check_nomsg(cpl_image_filter(a, b, ker, CPL_FILTER_MEDIAN,
1128  CPL_BORDER_FILTER));
1129  break;
1130  case CPL_FILTER_LINEAR:
1131  check_nomsg(cpl_image_filter(a, b, ker, CPL_FILTER_LINEAR,
1132  CPL_BORDER_FILTER));
1133  break;
1134  case CPL_FILTER_STDEV:
1135  cpl_image_filter(a, b, ker, CPL_FILTER_STDEV, CPL_BORDER_FILTER);
1136  break;
1137  case CPL_FILTER_MORPHO:
1138  cpl_image_filter(a, b, ker, CPL_FILTER_MORPHO, CPL_BORDER_FILTER);
1139  break;
1140  default:
1141  sinfo_msg_error("Filter type not supported");
1142  return NULL;
1143  }
1144  cleanup:
1145 
1146  return a;
1147 
1148 }
1149 
1150 cpl_image *
1151 sinfo_image_filter_linear(const cpl_image *img, const cpl_matrix * mx)
1152 {
1153  return sinfo_image_filter_mode(img, mx, CPL_FILTER_LINEAR);
1154 
1155 }
1156 /* TODO: not used */
1157 cpl_image * sinfo_image_filter_linear2(const cpl_image *img,
1158  const cpl_matrix * mx)
1159 {
1160  return sinfo_image_filter_wrapper(img, mx, CPL_FILTER_LINEAR);
1161 
1162 }
1163 
1164 
1165 cpl_image * sinfo_image_filter_median(const cpl_image * img,
1166  const cpl_matrix * mx)
1167 {
1168  return sinfo_image_filter_wrapper(img, mx, CPL_FILTER_MEDIAN);
1169 }
1170 
1171 
#define sinfo_msg_error(...)
Print an error message.
Definition: sinfo_msg.h:69