sinfo_utilities.c

00001 /*
00002  * This file is part of the ESO SINFONI Pipeline
00003  * Copyright (C) 2004,2005 European Southern Observatory
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00018  */
00019 #ifdef HAVE_CONFIG_H
00020 #  include <config.h>
00021 #endif
00022 #include "sinfo_utilities.h"
00023 #include "sinfo_error.h"
00024 #include "sinfo_utils_wrappers.h"
00025 #include "sinfo_function_1d.h"
00034 /*----------------------------------------------------------------------------*/
00044 /*----------------------------------------------------------------------------*/
00045 
00046 cpl_image*
00047 sinfo_vector_to_image(const cpl_vector* vector,cpl_type type)
00048 {
00049   int i=0;
00050   cpl_image* image=NULL;
00051   int size=0;
00052   const double* pv=NULL;
00053   int* pi=NULL;
00054   float* pf=NULL;
00055   double* pd=NULL;
00056 
00057 
00058       size=cpl_vector_get_size(vector);
00059       image=cpl_image_new(size,1,type);
00060       pv=cpl_vector_get_data_const(vector);
00061       if(type == CPL_TYPE_INT) {
00062         pi=cpl_image_get_data_int(image);
00063         for(i=0;i<size;i++) {
00064       pi[i]=pv[i];
00065     }
00066       } else if (type == CPL_TYPE_FLOAT) {
00067         pf=cpl_image_get_data_float(image);
00068         for(i=0;i<size;i++) {
00069       pf[i]=pv[i];
00070     }
00071       } else if (type == CPL_TYPE_DOUBLE) {
00072         pd=cpl_image_get_data_double(image);
00073         for(i=0;i<size;i++) {
00074       pd[i]=pv[i];
00075     }
00076       } else {
00077         assure( 0, CPL_ERROR_INVALID_TYPE,
00078             "No CPL type to represent BITPIX = %d", type);
00079       }
00080 
00081  cleanup:
00082     if (cpl_error_get_code() != CPL_ERROR_NONE){
00083       sinfo_free_image(&image);
00084     }
00085 
00086     return image;
00087 
00088 }
00089 
00090 
00091 cpl_error_code
00092 sinfo_ima_line_cor(cpl_parameterlist * parlist, cpl_frameset* in)
00093 {
00094   int n=0;
00095   int i=0;
00096   int kappa=0;
00097   int filt_rad=0;
00098   int width=3;
00099 
00100   cpl_frame* frm=NULL;
00101   const char* name=NULL;
00102   cpl_image * ima=NULL ;
00103   cpl_image * ima_out=NULL ;
00104   cpl_parameter* p=NULL;
00105   cpl_propertylist* plist=NULL;
00106 
00107   check_nomsg(p=cpl_parameterlist_find(parlist, "sinfoni.general.lc_kappa"));
00108   check_nomsg(kappa=cpl_parameter_get_int(p));
00109   check_nomsg(p=cpl_parameterlist_find(parlist,
00110                        "sinfoni.general.lc_filt_rad"));
00111   check_nomsg(filt_rad = cpl_parameter_get_int(p)) ;
00112 
00113   n=cpl_frameset_get_size(in);
00114 
00115   for(i=0;i<n;i++) {
00116     check_nomsg(frm=cpl_frameset_get_frame(in,0));
00117     check_nomsg(name=cpl_frame_get_filename(frm));
00118     check_nomsg(ima=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00119     check_nomsg(sinfo_image_line_corr(width,filt_rad,kappa,ima,&ima_out));
00120     check_nomsg(plist=cpl_propertylist_load(name,0));
00121     check(cpl_image_save(ima_out,
00122              name,
00123              CPL_BPP_IEEE_FLOAT,
00124              plist,
00125              CPL_IO_DEFAULT),
00126       "Could not save product");
00127     sinfo_free_image(&ima);
00128     sinfo_free_propertylist(&plist);
00129   }
00130 
00131  cleanup:
00132   sinfo_free_image(&ima);
00133   sinfo_free_propertylist(&plist);
00134 
00135   return cpl_error_get_code();
00136 
00137 }
00138 
00139 int
00140 sinfo_table_column_dump(cpl_table* t, const char* name, cpl_type type)
00141 {
00142   int nrow=0;
00143   int i=0;
00144   int* pi=NULL;
00145   float* pf=NULL;
00146   double* pd=NULL;
00147   char** ps=NULL;
00148 
00149   nrow=cpl_table_get_nrow(t);
00150 
00151   switch(type) {
00152 
00153   case CPL_TYPE_INT:
00154     pi=cpl_table_get_data_int(t,name);
00155     for(i=0;i<nrow;i++) {
00156       sinfo_msg("val=%d",pi[i]);
00157     }
00158     break;
00159   case CPL_TYPE_FLOAT:
00160     pf=cpl_table_get_data_float(t,name);
00161     for(i=0;i<nrow;i++) {
00162       sinfo_msg("val=%g",pf[i]);
00163     }
00164     break;
00165   case CPL_TYPE_DOUBLE:
00166     pd=cpl_table_get_data_double(t,name);
00167     for(i=0;i<nrow;i++) {
00168       sinfo_msg("val=%g",pd[i]);
00169     }
00170     break;
00171   case CPL_TYPE_STRING:
00172     ps=cpl_table_get_data_string(t,name);
00173     for(i=0;i<nrow;i++) {
00174       sinfo_msg("val=%s",ps[i]);
00175     }
00176     break;
00177   default:
00178     sinfo_msg_error("Wrong column type");
00179     cpl_error_set(cpl_func, CPL_ERROR_TYPE_MISMATCH);
00180     return 0;
00181 
00182   }
00183   return 0;
00184 }
00185 
00186 
00187 
00197 cpl_table*
00198 sinfo_table_shift_column_spline3(cpl_table* t,
00199                                  const char* col,
00200                                  const double shift)
00201 {
00202   cpl_table* out=NULL;
00203   int nrow=0;
00204   int i=0;
00205   int z=0;
00206 
00207   float sum=0;
00208   float new_sum=0;
00209 
00210   float* pi=NULL;
00211   float* po=NULL;
00212   float* eval=NULL;
00213   float* xnum=NULL;
00214   float* spec=NULL;
00215   float* corrected_spec=NULL;
00216 
00217   cknull(t,"null input table");
00218   out=cpl_table_duplicate(t);
00219 
00220   nrow=cpl_table_get_nrow(t);
00221   check_nomsg(cpl_table_cast_column(t,col,"FINT",CPL_TYPE_FLOAT));
00222   check_nomsg(cpl_table_cast_column(out,col,"FINT",CPL_TYPE_FLOAT));
00223   pi=cpl_table_get_data_float(t,"FINT");
00224   po=cpl_table_get_data_float(out,"FINT");
00225 
00226 
00227 
00228   xnum=cpl_calloc(nrow,sizeof(float)) ;
00229   /* fill the xa[] array for the spline function */
00230   for ( i = 0 ; i < nrow ; i++ ) {
00231     xnum[i] = i ;
00232   }
00233 
00234   spec=cpl_calloc(nrow,sizeof(float)) ;
00235   corrected_spec=cpl_calloc(nrow,sizeof(float)) ;
00236   eval=cpl_calloc(nrow,sizeof(float)) ;
00237 
00238   sum = 0. ;
00239   for ( z = 0 ; z < nrow ; z++ ) {
00240     spec[z] = pi[z] ;
00241     if (isnan(spec[z]) ) {
00242       for ( i = z-1 ; i <= z+1 ; i++ ) {
00243     if ( i < 0 ) continue ;
00244     if ( i >= nrow) continue ;
00245     corrected_spec[i] = ZERO ;
00246       }
00247       spec[z] = 0. ;
00248     }
00249     sum += spec[z] ;
00250     eval[z] = (float)shift+(float)z ;
00251   }
00252   /* now we do the spline interpolation*/
00253   if ( -1 == sinfo_function1d_natural_spline(xnum,spec, nrow,
00254                                              eval,corrected_spec, nrow))
00255     {
00256       sinfo_msg_error("error in spline interpolation!") ;
00257       goto cleanup;
00258     }
00259 
00260   new_sum = 0. ;
00261   for ( z = 0 ; z < nrow ; z++ ) {
00262     if ( isnan(corrected_spec[z]) ) {
00263       continue ;
00264     }
00265     new_sum += corrected_spec[z] ;
00266   }
00267   /* fill output imagelist */
00268   for ( z = 0 ; z < nrow ; z++ ) {
00269     if ( new_sum == 0. ) new_sum =1. ;
00270     {
00271       if ( isnan(corrected_spec[z]) ) {
00272     po[z] = ZERO ;
00273       } else {
00274     corrected_spec[z] *= sum / new_sum ;
00275     po[z] = corrected_spec[z] ;
00276       }
00277     }
00278   }
00279 
00280   sinfo_free_float(&xnum);
00281   sinfo_free_float(&spec) ;
00282   sinfo_free_float(&corrected_spec) ;
00283   sinfo_free_float(&eval) ;
00284 
00285   check_nomsg(cpl_table_erase_column(t,"FINT"));
00286   check_nomsg(cpl_table_erase_column(out,col));
00287   check_nomsg(cpl_table_cast_column(out,"FINT",col,CPL_TYPE_DOUBLE));
00288   check_nomsg(cpl_table_erase_column(out,"FINT"));
00289 
00290   return out;
00291  cleanup:
00292 
00293   sinfo_free_float(&xnum);
00294   sinfo_free_float(&spec) ;
00295   sinfo_free_float(&corrected_spec) ;
00296   sinfo_free_float(&eval) ;
00297   sinfo_free_table(&out);
00298   return NULL;
00299 
00300 
00301 }
00302 
00303 
00313 cpl_table*
00314 sinfo_table_shift_column_int(const cpl_table* t,
00315                              const char* col,
00316                              const double s,
00317                                    double* r)
00318 {
00319   cpl_table* out=NULL;
00320   int is=(int)s;
00321   int nrow=0;
00322   int i=0;
00323 
00324   const double* pi=NULL;
00325   double* po=NULL;
00326 
00327   cknull(t,"null input table");
00328   out=cpl_table_duplicate(t);
00329   *r=s-is;
00330   nrow=cpl_table_get_nrow(t);
00331   pi=cpl_table_get_data_double_const(t,col);
00332   po=cpl_table_get_data_double(out,col);
00333   for(i=0;i<nrow;i++) {
00334     if( ((i-is) >=0) && ((i-is) < nrow)) {
00335       po[i-is]=pi[i];
00336     }
00337   }
00338   return out;
00339  cleanup:
00340   sinfo_free_table(&out);
00341   return NULL;
00342 
00343 }
00344 
00345 
00356 cpl_table*
00357 sinfo_table_shift_column_poly(cpl_table* t,
00358                               const char* col,
00359                               const double shift,
00360                               const int order)
00361 {
00362   cpl_table* out=NULL;
00363   int nrow=0;
00364   int i=0;
00365   int flag=0;
00366   int n_points=0;
00367   int firstpos=0;
00368   int z=0;
00369   float eval=0;
00370   float sum=0;
00371   float new_sum=0;
00372   float* pi=NULL;
00373   float* po=NULL;
00374   float* spec=NULL ;
00375   float* corrected_spec=NULL ;
00376   float* xnum=NULL ;
00377   float* tableptr=NULL;
00378 
00379   cknull(t,"null input table");
00380   if ( order <= 0 ) {
00381     sinfo_msg_error("wrong order of interpolation polynom given!") ;
00382     goto cleanup;
00383   }
00384 
00385   out=cpl_table_duplicate(t);
00386 
00387   nrow=cpl_table_get_nrow(t);
00388   cpl_table_cast_column(t,col,"FINT",CPL_TYPE_FLOAT);
00389   cpl_table_cast_column(out,col,"FINT",CPL_TYPE_FLOAT);
00390   pi=cpl_table_get_data_float(t,"FINT");
00391   po=cpl_table_get_data_float(out,"FINT");
00392 
00393   n_points = order + 1 ;
00394   if ( n_points % 2 == 0 ) {
00395     firstpos = (int)(n_points/2) - 1 ;
00396   } else {
00397     firstpos = (int)(n_points/2) ;
00398   }
00399   spec=cpl_calloc(nrow,sizeof(float)) ;
00400   corrected_spec=cpl_calloc(nrow,sizeof(float)) ;
00401   xnum=cpl_calloc(order+1,sizeof(float)) ;
00402   /* fill the xa[] array for the polint function */
00403   for ( i = 0 ; i < n_points ; i++ ) {
00404     xnum[i] = i ;
00405   }
00406 
00407 
00408   for(i=0;i<nrow;i++) {
00409     corrected_spec[i] = 0. ;
00410   }
00411 
00412   sum = 0. ;
00413   for ( z = 0 ; z < nrow ; z++ ) {
00414     spec[z] = pi[z] ;
00415     if (isnan(spec[z]) ) {
00416       spec[z] = 0. ;
00417 
00418       for ( i = z - firstpos ; i < z-firstpos+n_points ; i++ ) {
00419     if ( i < 0 ) continue ;
00420     if ( i >= nrow) continue  ;
00421     corrected_spec[i] = ZERO ;
00422       }
00423     }
00424     if ( z != 0 && z != nrow - 1 ) {
00425       sum += spec[z] ;
00426     }
00427   }
00428 
00429   new_sum = 0. ;
00430   for ( z = 0 ; z < nrow ; z++ ) {
00431     /* ---------------------------------------------------------------
00432      * now determine the arrays of size n_points with which the
00433      * polynom is determined and determine the position eval
00434      * where the polynom is evaluated in polynomial interpolation.
00435      * Take care of the points near the row edges!
00436      */
00437     if (isnan(corrected_spec[z])) continue ;
00438     if ( z - firstpos < 0 ) {
00439       tableptr = &spec[0] ;
00440       eval     = shift + z ;
00441     } else if ( z - firstpos + n_points >= nrow ) {
00442       tableptr = &spec[nrow - n_points] ;
00443       eval     = shift + z + n_points - nrow ;
00444     } else {
00445       tableptr = &spec[z-firstpos] ;
00446       eval     = shift + firstpos ;
00447     }
00448 
00449     flag=0;
00450     corrected_spec[z]=sinfo_new_nev_ille(xnum,tableptr,order,eval,&flag);
00451     if ( z != 0 && z != nrow - 1 ) {
00452       new_sum += corrected_spec[z] ;
00453     }
00454   }
00455 
00456   /* fill the output spectrum */
00457   for (z = 0 ; z < nrow ; z++ ) {
00458     if ( new_sum == 0. ) {
00459       new_sum = 1. ;
00460     }
00461     if ( z == 0 ) {
00462       po[z] = ZERO ;
00463     } else if ( z == nrow - 1 ) {
00464       po[z] = ZERO ;
00465     } else if ( isnan(corrected_spec[z]) ) {
00466       po[z] = ZERO ;
00467     } else {
00468       corrected_spec[z] *= sum / new_sum ;
00469       po[z] = corrected_spec[z] ;
00470     }
00471   }
00472   check_nomsg(cpl_table_erase_column(t,"FINT"));
00473   check_nomsg(cpl_table_erase_column(out,col));
00474   check_nomsg(cpl_table_cast_column(out,"FINT",col,CPL_TYPE_DOUBLE));
00475   check_nomsg(cpl_table_erase_column(out,"FINT"));
00476 
00477   sinfo_free_float(&spec) ;
00478   sinfo_free_float(&corrected_spec) ;
00479   sinfo_free_float(&xnum) ;
00480 
00481   return out;
00482  cleanup:
00483 
00484 
00485   sinfo_free_float(&spec) ;
00486   sinfo_free_float(&corrected_spec) ;
00487   sinfo_free_float(&xnum) ;
00488   sinfo_free_table(&out);
00489   return NULL;
00490 
00491 
00492 }
00493 
00494 
00495 
00496 
00497     void sinfo_new_array_set_value( float * array, float value, int i )
00498     {
00499         array[i] = value ;
00500     }
00501     float sinfo_new_array_get_value( float * array, int i )
00502     {
00503         return array[i] ;
00504     }
00505 
00506 
00507 
00508     void sinfo_new_destroy_array(float ** array)
00509     {
00510       if(*array != NULL) {
00511         cpl_free( *array ) ;
00512         *array = NULL;
00513       }
00514     }
00515 
00516     void sinfo_new_destroy_stringarray(char ** array, int size_x)
00517     {
00518         int i ;
00519 
00520         for ( i = 0 ; i < size_x ; i++ )
00521         {
00522             cpl_free( array[i] ) ;
00523         }
00524         cpl_free( array ) ;
00525     }
00526 
00527     void sinfo_new_destroy_2Dintarray(int *** array, int size_x)
00528     {
00529         int i ;
00530 
00531         if((*array) != NULL) {
00532       for ( i = 0 ; i < size_x ; i++ ) {
00533         if((*array)[i] != NULL) {
00534           cpl_free( (*array)[i] );
00535           (*array)[i]=NULL;
00536         }
00537       }
00538       cpl_free( *array ) ;
00539       *array=NULL;
00540     }
00541 
00542     }
00543 
00544 
00545     void sinfo_new_intarray_set_value( int * array, int value, int i )
00546     {
00547         array[i] = value ;
00548     }
00549     float sinfo_new_array2D_get_value( float ** array, int x, int y )
00550     {
00551         return array[x][y] ;
00552     }
00553     int ** sinfo_new_2Dintarray( int size_x, int size_y)
00554     {
00555         int ** retVal ;
00556         int i ;
00557 
00558         retVal = (int **) cpl_calloc( size_x, sizeof (int*) ) ;
00559         for ( i = 0 ; i < size_x ; i++ )
00560         {
00561             retVal[i] = (int *) cpl_calloc( size_y, sizeof (int)) ;
00562         }
00563         return retVal ;
00564     }
00565 
00566     float * sinfo_new_floatarray( int size)
00567     {
00568         return (float *) cpl_calloc( size, sizeof (float) ) ;
00569     }
00570 
00571 
00572     void sinfo_new_destroy_2Dfloatarray(float *** array, int size_x)
00573     {
00574         int i ;
00575         if((*array) != NULL) {
00576       for ( i = 0 ; i < size_x ; i++ ) {
00577         if((*array)[i] != NULL) {
00578           cpl_free( (*array)[i] );
00579           (*array)[i]=NULL;
00580         }
00581       }
00582       cpl_free( *array ) ;
00583       *array=NULL;
00584     }
00585     }
00586 
00587     void sinfo_new_destroy_2Ddoublearray(double *** array, int size_x)
00588     {
00589         int i ;
00590 
00591         if((*array) != NULL) {
00592       for ( i = 0 ; i < size_x ; i++ ) {
00593         if((*array)[i] != NULL) {
00594           cpl_free( (*array)[i] );
00595           (*array)[i]=NULL;
00596         }
00597       }
00598       cpl_free( *array ) ;
00599       *array=NULL;
00600     }
00601 
00602     }
00603 
00604 
00605     void sinfo_new_array2D_set_value(float ** array,float value,int x,int y)
00606     {
00607         array[x][y] = value ;
00608     }
00609 
00610     double sinfo_new_doublearray_get_value( double * array, int i )
00611     {
00612         return array[i] ;
00613     }
00614     void sinfo_new_doublearray_set_value( double * array, double value, int i )
00615     {
00616         array[i] = value ;
00617     }
00618 
00619    void sinfo_new_destroy_doublearray(double * array)
00620     {
00621         cpl_free( array ) ;
00622     }
00623     double * sinfo_new_doublearray( int size)
00624     {
00625         return (double *) cpl_calloc( size, sizeof (double) ) ;
00626     }
00627 
00628     double ** sinfo_new_2Ddoublearray( int size_x, int size_y)
00629     {
00630         double ** retVal ;
00631         int i ;
00632 
00633         retVal = (double **) cpl_calloc( size_x, sizeof (double*) ) ;
00634         for ( i = 0 ; i < size_x ; i++ )
00635         {
00636             retVal[i] = (double *) cpl_calloc( size_y, sizeof (double)) ;
00637         }
00638         return retVal ;
00639     }
00640 
00641     float ** sinfo_new_2Dfloatarray( int size_x, int size_y)
00642     {
00643         float ** retVal ;
00644         int i ;
00645 
00646         retVal = (float **) cpl_calloc( size_x, sizeof (float*) ) ;
00647         for ( i = 0 ; i < size_x ; i++ )
00648         {
00649             retVal[i] = (float *) cpl_calloc( size_y, sizeof (float)) ;
00650         }
00651         return retVal ;
00652     }
00653 
00654 
00655     int * sinfo_new_intarray( int size)
00656     {
00657         return (int *) cpl_calloc( size, sizeof (int) ) ;
00658     }
00659     void sinfo_new_destroy_intarray(int ** array)
00660     {
00661         cpl_free( *array ) ;
00662         *array=NULL;
00663     }
00664 
00665     int sinfo_new_intarray_get_value( int * array, int i )
00666     {
00667         return array[i] ;
00668     }
00669 
00670     float sinfo_new_Stats_get_cleanstdev(Stats * stats)
00671     {
00672         return stats -> cleanstdev ;
00673     }
00674     float sinfo_new_Stats_get_cleanmean(Stats * stats)
00675     {
00676         return stats -> cleanmean ;
00677     }
00678 
00679 char * sinfo_new_get_basename(const char *filename)
00680 {
00681   char *p ;
00682   p = strrchr (filename, '/');
00683   return p ? p + 1 : (char *) filename;
00684 }
00685 
00686 
00687 
00688 char * sinfo_new_get_rootname(const char * filename)
00689 {
00690     static char path[MAX_NAME_SIZE+1];
00691     char * lastdot ;
00692 
00693     if (strlen(filename)>MAX_NAME_SIZE) return NULL ;
00694     memset(path, MAX_NAME_SIZE, 0);
00695     strcpy(path, filename);
00696     lastdot = strrchr(path, '.');
00697     if (lastdot == NULL) return path ;
00698     if ((!strcmp(lastdot, ".fits")) || (!strcmp(lastdot, ".FITS")) ||
00699         (!strcmp(lastdot, ".paf")) || (!strcmp(lastdot, ".PAF")) ||
00700         (!strcmp(lastdot, ".dat")) || (!strcmp(lastdot, ".DAT")) ||
00701         (!strcmp(lastdot, ".fits")) || (!strcmp(lastdot, ".TFITS")) ||
00702         (!strcmp(lastdot, ".ascii")) || (!strcmp(lastdot, ".ASCII")))
00703     {
00704         lastdot[0] = (char)0;
00705     }
00706     return path ;
00707 }
00708 
00709 
00710 
00711 /*----------------------------------------------------------------------------*/
00719 /*----------------------------------------------------------------------------*/
00720 cpl_imagelist * sinfo_new_frameset_to_iset(cpl_frameset * fset)
00721 {
00722     cpl_imagelist   *   iset=NULL ;
00723     char        **  filenames ;
00724     int             nfiles=0 ;
00725 
00726     /* Test entries */
00727     if (fset == NULL) return NULL ;
00728 
00729     /* Get the filenames */
00730     if ((filenames = sinfo_new_frameset_to_filenames(fset, &nfiles)) == NULL) {
00731         sinfo_msg_error( "Cannot get the files names") ;
00732         return NULL ;
00733     }
00734     /* Load image set */
00735     if ((iset = sinfo_new_imagelist_load_frameset(fset,
00736                                          CPL_TYPE_FLOAT, 0, 0)) == NULL) {
00737         sinfo_msg_error( "Cannot load *** the image set") ;
00738         sinfo_msg_error((char* ) cpl_error_get_message());
00739 
00740         cpl_free(filenames) ;
00741         return NULL ;
00742     }
00743 
00744     /* Free and Return  */
00745     cpl_free(filenames) ;
00746     return iset ;
00747 }
00748 #include "cpl_imagelist_io.h"
00749 
00750 /*----------------------------------------------------------------------------*/
00761 /*----------------------------------------------------------------------------*/
00762 cpl_imagelist *
00763 sinfo_new_imagelist_load_frameset(const cpl_frameset * frameset,
00764                                   cpl_type type,
00765                                   int pnum,
00766                                   int extnum)
00767 {
00768     cpl_image  * image     = NULL;
00769     cpl_imagelist  * imagelist = NULL;
00770     const cpl_frame  * frame     = cpl_frameset_get_first_const(frameset);
00771     const int nz = cpl_frameset_get_size(frameset);
00772     int       i;
00773 
00774     /* Require imagelist to contain at least one image */
00775     cpl_ensure(nz > 0, CPL_ERROR_DATA_NOT_FOUND, NULL);
00776 
00777     for (i = 0; frame != NULL;
00778          i++, frame = cpl_frameset_get_next_const(frameset)) {
00779 
00780         const char * name = cpl_frame_get_filename(frame);
00781         if (name == NULL) break; /* Error check */
00782 
00783 
00784         image = cpl_image_load(name, type, pnum, extnum);
00785 
00786         if (image == NULL) break; /* Error check */
00787 
00788         if (i == 0) {
00789             const int nx = cpl_image_get_size_x(image);
00790             const int ny = cpl_image_get_size_y(image);
00791 
00792             if (nx < 1 || ny < 1) break; /* Error check */
00793             imagelist = cpl_imagelist_new();
00794         if (imagelist == NULL) break; /* Error check */
00795         }
00796 
00797         if (cpl_imagelist_set(imagelist, image, i)) break;
00798         image = NULL; /* Image is now part of the imagelist */
00799 
00800     }
00801 
00802     if (i != nz) {
00803         /* Error handling */
00804         cpl_image_delete(image);
00805         cpl_imagelist_delete(imagelist);
00806         imagelist = NULL;
00807     }
00808     return imagelist;
00809 
00810 }
00811 
00822 char ** sinfo_new_frameset_to_filenames(cpl_frameset *set, int *nfiles)
00823 {
00824     char **filenames=NULL;
00825 
00826     int nbframes=0;
00827     int i=0;
00828 
00829     cpl_frame *curr_frame;
00830 
00831     if (set == NULL) {
00832         return NULL;
00833     }
00834 
00835     nbframes = cpl_frameset_get_size(set);
00836 
00837     if (nbframes < 1) {
00838         return NULL;
00839     }
00840     /*
00841      * Create the list of filenames and fill it
00842      */
00843     filenames = cpl_malloc(nbframes * sizeof(char *));
00844 
00845     curr_frame = cpl_frameset_get_first(set);
00846     for (i = 0; i < nbframes; i++) {
00847        filenames[i]=(char*) cpl_frame_get_filename(curr_frame);
00848         curr_frame = cpl_frameset_get_next(set);
00849     }
00850 
00851 
00852     /*
00853      * Set the number of files found
00854      */
00855     *nfiles = nbframes;
00856 
00857     return filenames;
00858 
00859 }
00860 
00861 
00862 /*---------------------------------------------------------------------------*/
00878 /*----------------------------------------------------------------------------*/
00879 double sinfo_spline_hermite(double xp,
00880                             const double *x,
00881                             const double *y,
00882                               int n,
00883                               int *istart )
00884 {
00885     double yp1, yp2, yp = 0;
00886     double xpi, xpi1, l1, l2, lp1, lp2;
00887     int i;
00888 
00889     if ( x[0] <= x[n-1] && (xp < x[0] || xp > x[n-1]) )    return 0.0;
00890     if ( x[0] >  x[n-1] && (xp > x[0] || xp < x[n-1]) )    return 0.0;
00891 
00892     if ( x[0] <= x[n-1] )
00893     {
00894         for ( i = (*istart)+1; i <= n && xp >= x[i-1]; i++ )
00895         ;
00896     }
00897     else
00898     {
00899         for ( i = (*istart)+1; i <= n && xp <= x[i-1]; i++ )
00900         ;
00901     }
00902 
00903     *istart = i;
00904     i--;
00905 
00906     lp1 = 1.0 / (x[i-1] - x[i]);
00907     lp2 = -lp1;
00908 
00909     if ( i == 1 )
00910     {
00911         yp1 = (y[1] - y[0]) / (x[1] - x[0]);
00912     }
00913     else
00914     {
00915         yp1 = (y[i] - y[i-2]) / (x[i] - x[i-2]);
00916     }
00917 
00918     if ( i >= n - 1 )
00919     {
00920         yp2 = (y[n-1] - y[n-2]) / (x[n-1] - x[n-2]);
00921     }
00922     else
00923     {
00924         yp2 = (y[i+1] - y[i-1]) / (x[i+1] - x[i-1]);
00925     }
00926 
00927     xpi1 = xp - x[i];
00928     xpi  = xp - x[i-1];
00929     l1   = xpi1*lp1;
00930     l2   = xpi*lp2;
00931 
00932     yp = y[i-1]*(1 - 2.0*lp1*xpi)*l1*l1 +
00933          y[i]*(1 - 2.0*lp2*xpi1)*l2*l2 +
00934          yp1*xpi*l1*l1 + yp2*xpi1*l2*l2;
00935 
00936     return yp;
00937 }
00938 
00944 cpl_error_code update_bad_pixel_map(cpl_image* im)
00945 {
00946     int szx = cpl_image_get_size_x(im);
00947     int szy = cpl_image_get_size_y(im);
00948     int x = 0;
00949     cpl_mask* bpm = cpl_image_get_bpm(im);
00950 
00951     for (x = 1; x <=szx; x++)
00952     {
00953         int y = 0;
00954         for(y = 1; y <= szy; y++)
00955         {
00956             int isnull = 0;
00957             double value = cpl_image_get(im, x, y, &isnull);
00958             if (isnan(value))
00959             {
00960                 cpl_mask_set(bpm, x, y, CPL_BINARY_1);
00961             }
00962         }
00963     }
00964     return cpl_error_get_code();
00965 }
00966 cpl_polynomial * sinfo_polynomial_fit_2d_create(cpl_bivector     *  xy_pos,
00967                                               cpl_vector       *  values,
00968                                               int                 degree,
00969                                               double           *  mse)
00970 {
00971     typedef double* (*get_data)(cpl_bivector*);
00972     get_data data_extractor[2] = { &cpl_bivector_get_x_data, &cpl_bivector_get_y_data};
00973     //samppos matrix must
00974     // have two rows with copies of the two vectors in the x_pos bivector.
00975 
00976     double rechisq = 0;
00977     int i, j;
00978     cpl_vector     * fitresidual = 0;
00979     cpl_matrix     * samppos2d = 0;
00980     cpl_polynomial * fit2d = cpl_polynomial_new(2);
00981     int xy_size = cpl_bivector_get_size(xy_pos);
00982 
00983     samppos2d = cpl_matrix_new(2, xy_size);
00984     for (i = 0; i < 2; i++)
00985     {
00986         for (j = 0; j < xy_size; j++)
00987         {
00988             double value = data_extractor[i](xy_pos)[j];
00989             cpl_matrix_set(samppos2d, i, j, value);
00990         }
00991     }
00992 
00993    cpl_polynomial_fit(fit2d, samppos2d, NULL, values, NULL, CPL_FALSE,
00994                              NULL, &degree);
00995 
00996     fitresidual = cpl_vector_new(xy_size);
00997     cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit2d,
00998                                             samppos2d, &rechisq);
00999     if (mse)
01000     {
01001         *mse = cpl_vector_product(fitresidual, fitresidual)
01002             / cpl_vector_get_size(fitresidual);
01003     }
01004     cpl_matrix_delete(samppos2d);
01005     cpl_vector_delete(fitresidual);
01006     return fit2d;
01007 }
01008 
01009 cpl_polynomial * sinfo_polynomial_fit_1d_create(
01010         const cpl_vector    *   x_pos,
01011         const cpl_vector    *   values,
01012         int                     degree,
01013         double              *   mse
01014         )
01015 {
01016     cpl_polynomial * fit1d = cpl_polynomial_new(1);
01017 //    cpl_vector* x_copy = cpl_vector_duplicate(x_pos);
01018 //    cpl_vector* values_copy = cpl_vector_duplicate(values);
01019     int x_size = cpl_vector_get_size(x_pos);
01020     double rechisq = 0;
01021     cpl_matrix     * samppos = cpl_matrix_wrap(1, x_size,
01022                                                (double*)cpl_vector_get_data_const(x_pos));
01023     cpl_vector     * fitresidual = cpl_vector_new(x_size);
01024 
01025     cpl_polynomial_fit(fit1d, samppos, NULL, values, NULL,
01026                        CPL_FALSE, NULL, &degree);
01027     cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
01028     cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit1d,
01029                                             samppos, &rechisq);
01030     cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
01031     if (mse)
01032     {
01033         *mse = cpl_vector_product(fitresidual, fitresidual)
01034             / cpl_vector_get_size(fitresidual);
01035     }
01036     cpl_matrix_unwrap(samppos);
01037     cpl_vector_delete(fitresidual);
01038     return fit1d;
01039 }
01040 
01041 static cpl_image * sinfo_image_filter_wrapper(const cpl_image *b, const cpl_matrix *k, cpl_filter_mode mode);
01042 cpl_image * sinfo_image_filter_median(const cpl_image * img, const cpl_matrix * mx)
01043 {
01044     return sinfo_image_filter_wrapper(img, mx, CPL_FILTER_MEDIAN);
01045 }
01046 
01047 cpl_image * sinfo_image_filter_linear(const cpl_image *img, const cpl_matrix * mx)
01048 {
01049     return sinfo_image_filter_wrapper(img, mx, CPL_FILTER_LINEAR);
01050 
01051 }
01052 //cpl_image * sinfo_image_filter_
01053 static cpl_image * sinfo_image_filter_wrapper(const cpl_image *b, const cpl_matrix *k, cpl_filter_mode mode)
01054 {
01055     const double EPSILON = 1E-5;
01056     int nx   = cpl_image_get_size_x(b);
01057     int ny   = cpl_image_get_size_y(b);
01058     int nrow = cpl_matrix_get_nrow(k);
01059     int ncol = cpl_matrix_get_ncol(k);
01060     int i, j;
01061     cpl_type type = cpl_image_get_type(b);
01062     cpl_image * a = cpl_image_new(nx, ny, type);
01063     // where m is a cpl_mask with a CPL_BINARY_1 whereever k has a 1.0.
01064     cpl_mask* m = cpl_mask_new(ncol, nrow);
01065     cpl_msg_warning(cpl_func, "nx[%d], ny[%d], ncol[%d], nrow[%d]", nx, ny, ncol, nrow);
01066     for (i = 0; i < ncol ; i++)
01067     {
01068         for (j = 0; j < nrow ; j++)
01069         {
01070             double value = cpl_matrix_get(k, j, i);
01071             if (fabs(value - 1.0) < EPSILON)
01072             {
01073                 cpl_mask_set(m, i + 1, j + 1, CPL_BINARY_1);
01074             }
01075         }
01076     }
01077 
01078     cpl_image_filter_mask(a, b, m, mode, CPL_BORDER_FILTER);
01079     cpl_mask_delete(m);
01080     return a;
01081  }

Generated on 8 Mar 2011 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1