00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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_dfs.h"
00025 #include "sinfo_utils_wrappers.h"
00026 #include "sinfo_function_1d.h"
00035
00045
00046
00047 cpl_image*
00048 sinfo_vector_to_image(const cpl_vector* vector,cpl_type type)
00049 {
00050 int i=0;
00051 cpl_image* image=NULL;
00052 int size=0;
00053 const double* pv=NULL;
00054 int* pi=NULL;
00055 float* pf=NULL;
00056 double* pd=NULL;
00057
00058
00059 size=cpl_vector_get_size(vector);
00060 image=cpl_image_new(size,1,type);
00061 pv=cpl_vector_get_data_const(vector);
00062 if(type == CPL_TYPE_INT) {
00063 pi=cpl_image_get_data_int(image);
00064 for(i=0;i<size;i++) {
00065 pi[i]=pv[i];
00066 }
00067 } else if (type == CPL_TYPE_FLOAT) {
00068 pf=cpl_image_get_data_float(image);
00069 for(i=0;i<size;i++) {
00070 pf[i]=pv[i];
00071 }
00072 } else if (type == CPL_TYPE_DOUBLE) {
00073 pd=cpl_image_get_data_double(image);
00074 for(i=0;i<size;i++) {
00075 pd[i]=pv[i];
00076 }
00077 } else {
00078 assure( 0, CPL_ERROR_INVALID_TYPE,
00079 "No CPL type to represent BITPIX = %d", type);
00080 }
00081
00082 cleanup:
00083 if (cpl_error_get_code() != CPL_ERROR_NONE){
00084 sinfo_free_image(&image);
00085 }
00086
00087 return image;
00088
00089 }
00090
00091
00092 cpl_error_code
00093 sinfo_ima_line_cor(cpl_parameterlist * parlist, cpl_frameset* in)
00094 {
00095 int n=0;
00096 int i=0;
00097 int kappa=18;
00098 int filt_rad=3;
00099 int width=4;
00100
00101 cpl_frame* frm=NULL;
00102 const char* name=NULL;
00103 const char* bname=NULL;
00104
00105 cpl_image * ima=NULL ;
00106 cpl_image * ima_out=NULL ;
00107 cpl_parameter* p=NULL;
00108 cpl_propertylist* plist=NULL;
00109 char* tag=NULL;
00110 char name_deb[80];
00111
00112
00113 check_nomsg(p=cpl_parameterlist_find(parlist, "sinfoni.general.lc_kappa"));
00114 check_nomsg(kappa=cpl_parameter_get_int(p));
00115 check_nomsg(p=cpl_parameterlist_find(parlist,
00116 "sinfoni.general.lc_filt_rad"));
00117 check_nomsg(filt_rad = cpl_parameter_get_int(p)) ;
00118
00119 n=cpl_frameset_get_size(in);
00120
00121 for(i=0;i<n;i++) {
00122 check_nomsg(frm=cpl_frameset_get_frame(in,i));
00123 tag= (char*) cpl_frame_get_tag(frm);
00124 if(sinfo_frame_is_raw(tag) == 1) {
00125 check_nomsg(name=cpl_frame_get_filename(frm));
00126
00127 bname=sinfo_new_get_basename(name);
00128 check_nomsg(ima=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00129 check_nomsg(sinfo_image_line_corr(width,filt_rad,kappa,ima,&ima_out));
00130 check_nomsg(plist=cpl_propertylist_load(name,0));
00131
00132 sprintf(name_deb,"org_%2.2d_%s",i,bname);
00133
00134 check(cpl_image_save(ima,
00135 name_deb,
00136 CPL_BPP_IEEE_FLOAT,
00137 plist,
00138 CPL_IO_DEFAULT),
00139 "Could not save product");
00140
00141
00142 check(cpl_image_save(ima_out,
00143 bname,
00144 CPL_BPP_IEEE_FLOAT,
00145 plist,
00146 CPL_IO_DEFAULT),
00147 "Could not save product");
00148
00149 cpl_frame_set_filename(frm,bname);
00150 sinfo_free_image(&ima);
00151 sinfo_free_propertylist(&plist);
00152 }
00153
00154 }
00155
00156 cleanup:
00157 sinfo_free_image(&ima);
00158 sinfo_free_propertylist(&plist);
00159
00160 return cpl_error_get_code();
00161
00162 }
00163
00164 int
00165 sinfo_table_column_dump(cpl_table* t, const char* name, cpl_type type)
00166 {
00167 int nrow=0;
00168 int i=0;
00169 int* pi=NULL;
00170 float* pf=NULL;
00171 double* pd=NULL;
00172 char** ps=NULL;
00173
00174 nrow=cpl_table_get_nrow(t);
00175
00176 switch(type) {
00177
00178 case CPL_TYPE_INT:
00179 pi=cpl_table_get_data_int(t,name);
00180 for(i=0;i<nrow;i++) {
00181 sinfo_msg("val=%d",pi[i]);
00182 }
00183 break;
00184 case CPL_TYPE_FLOAT:
00185 pf=cpl_table_get_data_float(t,name);
00186 for(i=0;i<nrow;i++) {
00187 sinfo_msg("val=%g",pf[i]);
00188 }
00189 break;
00190 case CPL_TYPE_DOUBLE:
00191 pd=cpl_table_get_data_double(t,name);
00192 for(i=0;i<nrow;i++) {
00193 sinfo_msg("val=%g",pd[i]);
00194 }
00195 break;
00196 case CPL_TYPE_STRING:
00197 ps=cpl_table_get_data_string(t,name);
00198 for(i=0;i<nrow;i++) {
00199 sinfo_msg("val=%s",ps[i]);
00200 }
00201 break;
00202 default:
00203 sinfo_msg_error("Wrong column type");
00204 cpl_error_set(cpl_func, CPL_ERROR_TYPE_MISMATCH);
00205 return 0;
00206
00207 }
00208 return 0;
00209 }
00210
00211
00212
00222 cpl_table*
00223 sinfo_table_shift_column_spline3(cpl_table* t,
00224 const char* col,
00225 const double shift)
00226 {
00227 cpl_table* out=NULL;
00228 int nrow=0;
00229 int i=0;
00230 int z=0;
00231
00232 float sum=0;
00233 float new_sum=0;
00234
00235 float* pi=NULL;
00236 float* po=NULL;
00237 float* eval=NULL;
00238 float* xnum=NULL;
00239 float* spec=NULL;
00240 float* corrected_spec=NULL;
00241
00242 cknull(t,"null input table");
00243 out=cpl_table_duplicate(t);
00244
00245 nrow=cpl_table_get_nrow(t);
00246 check_nomsg(cpl_table_cast_column(t,col,"FINT",CPL_TYPE_FLOAT));
00247 check_nomsg(cpl_table_cast_column(out,col,"FINT",CPL_TYPE_FLOAT));
00248 pi=cpl_table_get_data_float(t,"FINT");
00249 po=cpl_table_get_data_float(out,"FINT");
00250
00251
00252
00253 xnum=cpl_calloc(nrow,sizeof(float)) ;
00254
00255 for ( i = 0 ; i < nrow ; i++ ) {
00256 xnum[i] = i ;
00257 }
00258
00259 spec=cpl_calloc(nrow,sizeof(float)) ;
00260 corrected_spec=cpl_calloc(nrow,sizeof(float)) ;
00261 eval=cpl_calloc(nrow,sizeof(float)) ;
00262
00263 sum = 0. ;
00264 for ( z = 0 ; z < nrow ; z++ ) {
00265 spec[z] = pi[z] ;
00266 if (isnan(spec[z]) ) {
00267 for ( i = z-1 ; i <= z+1 ; i++ ) {
00268 if ( i < 0 ) continue ;
00269 if ( i >= nrow) continue ;
00270 corrected_spec[i] = ZERO ;
00271 }
00272 spec[z] = 0. ;
00273 }
00274 sum += spec[z] ;
00275 eval[z] = (float)shift+(float)z ;
00276 }
00277
00278 if ( -1 == sinfo_function1d_natural_spline(xnum,spec, nrow,
00279 eval,corrected_spec, nrow))
00280 {
00281 sinfo_msg_error("error in spline interpolation!") ;
00282 goto cleanup;
00283 }
00284
00285 new_sum = 0. ;
00286 for ( z = 0 ; z < nrow ; z++ ) {
00287 if ( isnan(corrected_spec[z]) ) {
00288 continue ;
00289 }
00290 new_sum += corrected_spec[z] ;
00291 }
00292
00293 for ( z = 0 ; z < nrow ; z++ ) {
00294 if ( new_sum == 0. ) new_sum =1. ;
00295 {
00296 if ( isnan(corrected_spec[z]) ) {
00297 po[z] = ZERO ;
00298 } else {
00299 corrected_spec[z] *= sum / new_sum ;
00300 po[z] = corrected_spec[z] ;
00301 }
00302 }
00303 }
00304
00305 sinfo_free_float(&xnum);
00306 sinfo_free_float(&spec) ;
00307 sinfo_free_float(&corrected_spec) ;
00308 sinfo_free_float(&eval) ;
00309
00310 check_nomsg(cpl_table_erase_column(t,"FINT"));
00311 check_nomsg(cpl_table_erase_column(out,col));
00312 check_nomsg(cpl_table_cast_column(out,"FINT",col,CPL_TYPE_DOUBLE));
00313 check_nomsg(cpl_table_erase_column(out,"FINT"));
00314
00315 return out;
00316 cleanup:
00317
00318 sinfo_free_float(&xnum);
00319 sinfo_free_float(&spec) ;
00320 sinfo_free_float(&corrected_spec) ;
00321 sinfo_free_float(&eval) ;
00322 sinfo_free_table(&out);
00323 return NULL;
00324
00325
00326 }
00327
00328
00338 cpl_table*
00339 sinfo_table_shift_column_int(const cpl_table* t,
00340 const char* col,
00341 const double s,
00342 double* r)
00343 {
00344 cpl_table* out=NULL;
00345 int is=(int)s;
00346 int nrow=0;
00347 int i=0;
00348
00349 const double* pi=NULL;
00350 double* po=NULL;
00351
00352 cknull(t,"null input table");
00353 out=cpl_table_duplicate(t);
00354 *r=s-is;
00355 nrow=cpl_table_get_nrow(t);
00356 pi=cpl_table_get_data_double_const(t,col);
00357 po=cpl_table_get_data_double(out,col);
00358 for(i=0;i<nrow;i++) {
00359 if( ((i-is) >=0) && ((i-is) < nrow)) {
00360 po[i-is]=pi[i];
00361 }
00362 }
00363 return out;
00364 cleanup:
00365 sinfo_free_table(&out);
00366 return NULL;
00367
00368 }
00369
00370
00381 cpl_table*
00382 sinfo_table_shift_column_poly(cpl_table* t,
00383 const char* col,
00384 const double shift,
00385 const int order)
00386 {
00387 cpl_table* out=NULL;
00388 int nrow=0;
00389 int i=0;
00390 int flag=0;
00391 int n_points=0;
00392 int firstpos=0;
00393 int z=0;
00394 float eval=0;
00395 float sum=0;
00396 float new_sum=0;
00397 float* pi=NULL;
00398 float* po=NULL;
00399 float* spec=NULL ;
00400 float* corrected_spec=NULL ;
00401 float* xnum=NULL ;
00402 float* tableptr=NULL;
00403
00404 cknull(t,"null input table");
00405 if ( order <= 0 ) {
00406 sinfo_msg_error("wrong order of interpolation polynom given!") ;
00407 goto cleanup;
00408 }
00409
00410 out=cpl_table_duplicate(t);
00411
00412 nrow=cpl_table_get_nrow(t);
00413 cpl_table_cast_column(t,col,"FINT",CPL_TYPE_FLOAT);
00414 cpl_table_cast_column(out,col,"FINT",CPL_TYPE_FLOAT);
00415 pi=cpl_table_get_data_float(t,"FINT");
00416 po=cpl_table_get_data_float(out,"FINT");
00417
00418 n_points = order + 1 ;
00419 if ( n_points % 2 == 0 ) {
00420 firstpos = (int)(n_points/2) - 1 ;
00421 } else {
00422 firstpos = (int)(n_points/2) ;
00423 }
00424 spec=cpl_calloc(nrow,sizeof(float)) ;
00425 corrected_spec=cpl_calloc(nrow,sizeof(float)) ;
00426 xnum=cpl_calloc(order+1,sizeof(float)) ;
00427
00428 for ( i = 0 ; i < n_points ; i++ ) {
00429 xnum[i] = i ;
00430 }
00431
00432
00433 for(i=0;i<nrow;i++) {
00434 corrected_spec[i] = 0. ;
00435 }
00436
00437 sum = 0. ;
00438 for ( z = 0 ; z < nrow ; z++ ) {
00439 spec[z] = pi[z] ;
00440 if (isnan(spec[z]) ) {
00441 spec[z] = 0. ;
00442
00443 for ( i = z - firstpos ; i < z-firstpos+n_points ; i++ ) {
00444 if ( i < 0 ) continue ;
00445 if ( i >= nrow) continue ;
00446 corrected_spec[i] = ZERO ;
00447 }
00448 }
00449 if ( z != 0 && z != nrow - 1 ) {
00450 sum += spec[z] ;
00451 }
00452 }
00453
00454 new_sum = 0. ;
00455 for ( z = 0 ; z < nrow ; z++ ) {
00456
00457
00458
00459
00460
00461
00462 if (isnan(corrected_spec[z])) continue ;
00463 if ( z - firstpos < 0 ) {
00464 tableptr = &spec[0] ;
00465 eval = shift + z ;
00466 } else if ( z - firstpos + n_points >= nrow ) {
00467 tableptr = &spec[nrow - n_points] ;
00468 eval = shift + z + n_points - nrow ;
00469 } else {
00470 tableptr = &spec[z-firstpos] ;
00471 eval = shift + firstpos ;
00472 }
00473
00474 flag=0;
00475 corrected_spec[z]=sinfo_new_nev_ille(xnum,tableptr,order,eval,&flag);
00476 if ( z != 0 && z != nrow - 1 ) {
00477 new_sum += corrected_spec[z] ;
00478 }
00479 }
00480
00481
00482 for (z = 0 ; z < nrow ; z++ ) {
00483 if ( new_sum == 0. ) {
00484 new_sum = 1. ;
00485 }
00486 if ( z == 0 ) {
00487 po[z] = ZERO ;
00488 } else if ( z == nrow - 1 ) {
00489 po[z] = ZERO ;
00490 } else if ( isnan(corrected_spec[z]) ) {
00491 po[z] = ZERO ;
00492 } else {
00493 corrected_spec[z] *= sum / new_sum ;
00494 po[z] = corrected_spec[z] ;
00495 }
00496 }
00497 check_nomsg(cpl_table_erase_column(t,"FINT"));
00498 check_nomsg(cpl_table_erase_column(out,col));
00499 check_nomsg(cpl_table_cast_column(out,"FINT",col,CPL_TYPE_DOUBLE));
00500 check_nomsg(cpl_table_erase_column(out,"FINT"));
00501
00502 sinfo_free_float(&spec) ;
00503 sinfo_free_float(&corrected_spec) ;
00504 sinfo_free_float(&xnum) ;
00505
00506 return out;
00507 cleanup:
00508
00509
00510 sinfo_free_float(&spec) ;
00511 sinfo_free_float(&corrected_spec) ;
00512 sinfo_free_float(&xnum) ;
00513 sinfo_free_table(&out);
00514 return NULL;
00515
00516
00517 }
00518
00519
00520
00521
00522 void sinfo_new_array_set_value( float * array, float value, int i )
00523 {
00524 array[i] = value ;
00525 }
00526 float sinfo_new_array_get_value( float * array, int i )
00527 {
00528 return array[i] ;
00529 }
00530
00531
00532
00533 void sinfo_new_destroy_array(float ** array)
00534 {
00535 if(*array != NULL) {
00536 cpl_free( *array ) ;
00537 *array = NULL;
00538 }
00539 }
00540
00541 void sinfo_new_destroy_stringarray(char ** array, int size_x)
00542 {
00543 int i ;
00544
00545 for ( i = 0 ; i < size_x ; i++ )
00546 {
00547 cpl_free( array[i] ) ;
00548 }
00549 cpl_free( array ) ;
00550 }
00551
00552 void sinfo_new_destroy_2Dintarray(int *** array, int size_x)
00553 {
00554 int i ;
00555
00556 if((*array) != NULL) {
00557 for ( i = 0 ; i < size_x ; i++ ) {
00558 if((*array)[i] != NULL) {
00559 cpl_free( (*array)[i] );
00560 (*array)[i]=NULL;
00561 }
00562 }
00563 cpl_free( *array ) ;
00564 *array=NULL;
00565 }
00566
00567 }
00568
00569
00570 void sinfo_new_intarray_set_value( int * array, int value, int i )
00571 {
00572 array[i] = value ;
00573 }
00574 float sinfo_new_array2D_get_value( float ** array, int x, int y )
00575 {
00576 return array[x][y] ;
00577 }
00578 int ** sinfo_new_2Dintarray( int size_x, int size_y)
00579 {
00580 int ** retVal ;
00581 int i ;
00582
00583 retVal = (int **) cpl_calloc( size_x, sizeof (int*) ) ;
00584 for ( i = 0 ; i < size_x ; i++ )
00585 {
00586 retVal[i] = (int *) cpl_calloc( size_y, sizeof (int)) ;
00587 }
00588 return retVal ;
00589 }
00590
00591 float * sinfo_new_floatarray( int size)
00592 {
00593 return (float *) cpl_calloc( size, sizeof (float) ) ;
00594 }
00595
00596
00597 void sinfo_new_destroy_2Dfloatarray(float *** array, int size_x)
00598 {
00599 int i ;
00600 if((*array) != NULL) {
00601 for ( i = 0 ; i < size_x ; i++ ) {
00602 if((*array)[i] != NULL) {
00603 cpl_free( (*array)[i] );
00604 (*array)[i]=NULL;
00605 }
00606 }
00607 cpl_free( *array ) ;
00608 *array=NULL;
00609 }
00610 }
00611
00612 void sinfo_new_destroy_2Ddoublearray(double *** array, int size_x)
00613 {
00614 int i ;
00615
00616 if((*array) != NULL) {
00617 for ( i = 0 ; i < size_x ; i++ ) {
00618 if((*array)[i] != NULL) {
00619 cpl_free( (*array)[i] );
00620 (*array)[i]=NULL;
00621 }
00622 }
00623 cpl_free( *array ) ;
00624 *array=NULL;
00625 }
00626
00627 }
00628
00629
00630 void sinfo_new_array2D_set_value(float ** array,float value,int x,int y)
00631 {
00632 array[x][y] = value ;
00633 }
00634
00635 double sinfo_new_doublearray_get_value( double * array, int i )
00636 {
00637 return array[i] ;
00638 }
00639 void sinfo_new_doublearray_set_value( double * array, double value, int i )
00640 {
00641 array[i] = value ;
00642 }
00643
00644 void sinfo_new_destroy_doublearray(double * array)
00645 {
00646 cpl_free( array ) ;
00647 }
00648 double * sinfo_new_doublearray( int size)
00649 {
00650 return (double *) cpl_calloc( size, sizeof (double) ) ;
00651 }
00652
00653 double ** sinfo_new_2Ddoublearray( int size_x, int size_y)
00654 {
00655 double ** retVal ;
00656 int i ;
00657
00658 retVal = (double **) cpl_calloc( size_x, sizeof (double*) ) ;
00659 for ( i = 0 ; i < size_x ; i++ )
00660 {
00661 retVal[i] = (double *) cpl_calloc( size_y, sizeof (double)) ;
00662 }
00663 return retVal ;
00664 }
00665
00666 float ** sinfo_new_2Dfloatarray( int size_x, int size_y)
00667 {
00668 float ** retVal ;
00669 int i ;
00670
00671 retVal = (float **) cpl_calloc( size_x, sizeof (float*) ) ;
00672 for ( i = 0 ; i < size_x ; i++ )
00673 {
00674 retVal[i] = (float *) cpl_calloc( size_y, sizeof (float)) ;
00675 }
00676 return retVal ;
00677 }
00678
00679
00680 int * sinfo_new_intarray( int size)
00681 {
00682 return (int *) cpl_calloc( size, sizeof (int) ) ;
00683 }
00684 void sinfo_new_destroy_intarray(int ** array)
00685 {
00686 cpl_free( *array ) ;
00687 *array=NULL;
00688 }
00689
00690 int sinfo_new_intarray_get_value( int * array, int i )
00691 {
00692 return array[i] ;
00693 }
00694
00695 float sinfo_new_Stats_get_cleanstdev(Stats * stats)
00696 {
00697 return stats -> cleanstdev ;
00698 }
00699 float sinfo_new_Stats_get_cleanmean(Stats * stats)
00700 {
00701 return stats -> cleanmean ;
00702 }
00703
00704 char * sinfo_new_get_basename(const char *filename)
00705 {
00706 char *p ;
00707 p = strrchr (filename, '/');
00708 return p ? p + 1 : (char *) filename;
00709 }
00710
00711
00712
00713 char * sinfo_new_get_rootname(const char * filename)
00714 {
00715 static char path[MAX_NAME_SIZE+1];
00716 char * lastdot ;
00717
00718 if (strlen(filename)>MAX_NAME_SIZE) return NULL ;
00719 memset(path, MAX_NAME_SIZE, 0);
00720 strcpy(path, filename);
00721 lastdot = strrchr(path, '.');
00722 if (lastdot == NULL) return path ;
00723 if ((!strcmp(lastdot, ".fits")) || (!strcmp(lastdot, ".FITS")) ||
00724 (!strcmp(lastdot, ".paf")) || (!strcmp(lastdot, ".PAF")) ||
00725 (!strcmp(lastdot, ".dat")) || (!strcmp(lastdot, ".DAT")) ||
00726 (!strcmp(lastdot, ".fits")) || (!strcmp(lastdot, ".TFITS")) ||
00727 (!strcmp(lastdot, ".ascii")) || (!strcmp(lastdot, ".ASCII")))
00728 {
00729 lastdot[0] = (char)0;
00730 }
00731 return path ;
00732 }
00733
00734
00735
00736
00744
00745 cpl_imagelist * sinfo_new_frameset_to_iset(cpl_frameset * fset)
00746 {
00747 cpl_imagelist * iset=NULL ;
00748 char ** filenames ;
00749 int nfiles=0 ;
00750
00751
00752 if (fset == NULL) return NULL ;
00753
00754
00755 if ((filenames = sinfo_new_frameset_to_filenames(fset, &nfiles)) == NULL) {
00756 sinfo_msg_error( "Cannot get the files names") ;
00757 return NULL ;
00758 }
00759
00760 if ((iset = sinfo_new_imagelist_load_frameset(fset,
00761 CPL_TYPE_FLOAT, 0, 0)) == NULL) {
00762 sinfo_msg_error( "Cannot load *** the image set") ;
00763 sinfo_msg_error((char* ) cpl_error_get_message());
00764
00765 cpl_free(filenames) ;
00766 return NULL ;
00767 }
00768
00769
00770 cpl_free(filenames) ;
00771 return iset ;
00772 }
00773 #include "cpl_imagelist_io.h"
00774
00775
00786
00787 cpl_imagelist *
00788 sinfo_new_imagelist_load_frameset(const cpl_frameset * frameset,
00789 cpl_type type,
00790 int pnum,
00791 int extnum)
00792 {
00793 cpl_image * image = NULL;
00794 cpl_imagelist * imagelist = NULL;
00795 const cpl_frame * frame = cpl_frameset_get_first_const(frameset);
00796 const int nz = cpl_frameset_get_size(frameset);
00797 int i;
00798
00799
00800 cpl_ensure(nz > 0, CPL_ERROR_DATA_NOT_FOUND, NULL);
00801
00802 for (i = 0; frame != NULL;
00803 i++, frame = cpl_frameset_get_next_const(frameset)) {
00804
00805 const char * name = cpl_frame_get_filename(frame);
00806 if (name == NULL) break;
00807
00808
00809 image = cpl_image_load(name, type, pnum, extnum);
00810
00811 if (image == NULL) break;
00812
00813 if (i == 0) {
00814 const int nx = cpl_image_get_size_x(image);
00815 const int ny = cpl_image_get_size_y(image);
00816
00817 if (nx < 1 || ny < 1) break;
00818 imagelist = cpl_imagelist_new();
00819 if (imagelist == NULL) break;
00820 }
00821
00822 if (cpl_imagelist_set(imagelist, image, i)) break;
00823 image = NULL;
00824
00825 }
00826
00827 if (i != nz) {
00828
00829 cpl_image_delete(image);
00830 cpl_imagelist_delete(imagelist);
00831 imagelist = NULL;
00832 }
00833 return imagelist;
00834
00835 }
00836
00847 char ** sinfo_new_frameset_to_filenames(cpl_frameset *set, int *nfiles)
00848 {
00849 char **filenames=NULL;
00850
00851 int nbframes=0;
00852 int i=0;
00853
00854 cpl_frame *curr_frame;
00855
00856 if (set == NULL) {
00857 return NULL;
00858 }
00859
00860 nbframes = cpl_frameset_get_size(set);
00861
00862 if (nbframes < 1) {
00863 return NULL;
00864 }
00865
00866
00867
00868 filenames = cpl_malloc(nbframes * sizeof(char *));
00869
00870 curr_frame = cpl_frameset_get_first(set);
00871 for (i = 0; i < nbframes; i++) {
00872 filenames[i]=(char*) cpl_frame_get_filename(curr_frame);
00873 curr_frame = cpl_frameset_get_next(set);
00874 }
00875
00876
00877
00878
00879
00880 *nfiles = nbframes;
00881
00882 return filenames;
00883
00884 }
00885
00886
00887
00903
00904 double sinfo_spline_hermite(double xp,
00905 const double *x,
00906 const double *y,
00907 int n,
00908 int *istart )
00909 {
00910 double yp1, yp2, yp = 0;
00911 double xpi, xpi1, l1, l2, lp1, lp2;
00912 int i;
00913
00914 if ( x[0] <= x[n-1] && (xp < x[0] || xp > x[n-1]) ) return 0.0;
00915 if ( x[0] > x[n-1] && (xp > x[0] || xp < x[n-1]) ) return 0.0;
00916
00917 if ( x[0] <= x[n-1] )
00918 {
00919 for ( i = (*istart)+1; i <= n && xp >= x[i-1]; i++ )
00920 ;
00921 }
00922 else
00923 {
00924 for ( i = (*istart)+1; i <= n && xp <= x[i-1]; i++ )
00925 ;
00926 }
00927
00928 *istart = i;
00929 i--;
00930
00931 lp1 = 1.0 / (x[i-1] - x[i]);
00932 lp2 = -lp1;
00933
00934 if ( i == 1 )
00935 {
00936 yp1 = (y[1] - y[0]) / (x[1] - x[0]);
00937 }
00938 else
00939 {
00940 yp1 = (y[i] - y[i-2]) / (x[i] - x[i-2]);
00941 }
00942
00943 if ( i >= n - 1 )
00944 {
00945 yp2 = (y[n-1] - y[n-2]) / (x[n-1] - x[n-2]);
00946 }
00947 else
00948 {
00949 yp2 = (y[i+1] - y[i-1]) / (x[i+1] - x[i-1]);
00950 }
00951
00952 xpi1 = xp - x[i];
00953 xpi = xp - x[i-1];
00954 l1 = xpi1*lp1;
00955 l2 = xpi*lp2;
00956
00957 yp = y[i-1]*(1 - 2.0*lp1*xpi)*l1*l1 +
00958 y[i]*(1 - 2.0*lp2*xpi1)*l2*l2 +
00959 yp1*xpi*l1*l1 + yp2*xpi1*l2*l2;
00960
00961 return yp;
00962 }
00963
00969 cpl_error_code update_bad_pixel_map(cpl_image* im)
00970 {
00971 int szx = cpl_image_get_size_x(im);
00972 int szy = cpl_image_get_size_y(im);
00973 int x = 0;
00974 cpl_mask* bpm = cpl_image_get_bpm(im);
00975
00976 for (x = 1; x <=szx; x++)
00977 {
00978 int y = 0;
00979 for(y = 1; y <= szy; y++)
00980 {
00981 int isnull = 0;
00982 double value = cpl_image_get(im, x, y, &isnull);
00983 if (isnan(value))
00984 {
00985 cpl_mask_set(bpm, x, y, CPL_BINARY_1);
00986 }
00987 }
00988 }
00989 return cpl_error_get_code();
00990 }
00991 cpl_polynomial * sinfo_polynomial_fit_2d_create(cpl_bivector * xy_pos,
00992 cpl_vector * values,
00993 cpl_size degree,
00994 double * mse)
00995 {
00996 typedef double* (*get_data)(cpl_bivector*);
00997 get_data data_extractor[2] = { &cpl_bivector_get_x_data, &cpl_bivector_get_y_data};
00998
00999
01000
01001 double rechisq = 0;
01002 int i, j;
01003 cpl_vector * fitresidual = 0;
01004 cpl_matrix * samppos2d = 0;
01005 cpl_polynomial * fit2d = cpl_polynomial_new(2);
01006 int xy_size = cpl_bivector_get_size(xy_pos);
01007
01008 samppos2d = cpl_matrix_new(2, xy_size);
01009 for (i = 0; i < 2; i++)
01010 {
01011 for (j = 0; j < xy_size; j++)
01012 {
01013 double value = data_extractor[i](xy_pos)[j];
01014 cpl_matrix_set(samppos2d, i, j, value);
01015 }
01016 }
01017
01018 cpl_polynomial_fit(fit2d, samppos2d, NULL, values, NULL, CPL_FALSE,
01019 NULL, °ree);
01020
01021 fitresidual = cpl_vector_new(xy_size);
01022 cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit2d,
01023 samppos2d, &rechisq);
01024 if (mse)
01025 {
01026 *mse = cpl_vector_product(fitresidual, fitresidual)
01027 / cpl_vector_get_size(fitresidual);
01028 }
01029 cpl_matrix_delete(samppos2d);
01030 cpl_vector_delete(fitresidual);
01031 return fit2d;
01032 }
01033
01034 cpl_polynomial * sinfo_polynomial_fit_1d_create(
01035 const cpl_vector * x_pos,
01036 const cpl_vector * values,
01037 int degree,
01038 double * mse
01039 )
01040 {
01041 cpl_polynomial * fit1d = cpl_polynomial_new(1);
01042
01043
01044 int x_size = cpl_vector_get_size(x_pos);
01045 double rechisq = 0;
01046 cpl_size loc_deg=(cpl_size)degree;
01047 cpl_matrix * samppos = cpl_matrix_wrap(1, x_size,
01048 (double*)cpl_vector_get_data_const(x_pos));
01049 cpl_vector * fitresidual = cpl_vector_new(x_size);
01050
01051 cpl_polynomial_fit(fit1d, samppos, NULL, values, NULL,
01052 CPL_FALSE, NULL, &loc_deg);
01053 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
01054 cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit1d,
01055 samppos, &rechisq);
01056 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
01057 if (mse)
01058 {
01059 *mse = cpl_vector_product(fitresidual, fitresidual)
01060 / cpl_vector_get_size(fitresidual);
01061 }
01062 cpl_matrix_unwrap(samppos);
01063 cpl_vector_delete(fitresidual);
01064 return fit1d;
01065 }
01066
01067
01068 static cpl_image * sinfo_image_filter_wrapper(const cpl_image *b, const cpl_matrix *k, cpl_filter_mode mode)
01069 {
01070 const double EPSILON = 1E-5;
01071 int nx = cpl_image_get_size_x(b);
01072 int ny = cpl_image_get_size_y(b);
01073 int nrow = cpl_matrix_get_nrow(k);
01074 int ncol = cpl_matrix_get_ncol(k);
01075 int i, j;
01076 cpl_type type = cpl_image_get_type(b);
01077 cpl_image * a = cpl_image_new(nx, ny, type);
01078
01079 cpl_mask* m = cpl_mask_new(ncol, nrow);
01080 cpl_msg_warning(cpl_func, "nx[%d], ny[%d], ncol[%d], nrow[%d]", nx, ny, ncol, nrow);
01081 for (i = 0; i < ncol ; i++)
01082 {
01083 for (j = 0; j < nrow ; j++)
01084 {
01085 double value = cpl_matrix_get(k, j, i);
01086 if (fabs(value - 1.0) < EPSILON)
01087 {
01088 cpl_mask_set(m, i + 1, j + 1, CPL_BINARY_1);
01089 }
01090 }
01091 }
01092
01093 cpl_image_filter_mask(a, b, m, mode, CPL_BORDER_FILTER);
01094 cpl_mask_delete(m);
01095 return a;
01096 }
01097
01098
01099 static cpl_image*
01100 sinfo_image_filter_mode(const cpl_image* b,
01101 const cpl_matrix * ker,
01102 cpl_filter_mode filter)
01103 {
01104 int nx = cpl_image_get_size_x(b);
01105 int ny = cpl_image_get_size_y(b);
01106 int type = cpl_image_get_type(b);
01107 cpl_image * a = cpl_image_new(nx, ny, type);
01108
01109 switch(filter) {
01110 case CPL_FILTER_MEDIAN:
01111 check_nomsg(cpl_image_filter(a, b, ker, CPL_FILTER_MEDIAN, CPL_BORDER_FILTER));
01112 break;
01113 case CPL_FILTER_LINEAR:
01114 check_nomsg(cpl_image_filter(a, b, ker, CPL_FILTER_LINEAR, CPL_BORDER_FILTER));
01115 break;
01116 case CPL_FILTER_STDEV:
01117 cpl_image_filter(a, b, ker, CPL_FILTER_STDEV, CPL_BORDER_FILTER);
01118 break;
01119 case CPL_FILTER_MORPHO:
01120 cpl_image_filter(a, b, ker, CPL_FILTER_MORPHO, CPL_BORDER_FILTER);
01121 break;
01122 default:
01123 sinfo_msg_error("Filter type not supported");
01124 return NULL;
01125 }
01126 cleanup:
01127
01128 return a;
01129
01130 }
01131
01132 cpl_image *
01133 sinfo_image_filter_linear(const cpl_image *img, const cpl_matrix * mx)
01134 {
01135 return sinfo_image_filter_mode(img, mx, CPL_FILTER_LINEAR);
01136
01137 }
01138
01139 cpl_image * sinfo_image_filter_linear2(const cpl_image *img, const cpl_matrix * mx)
01140 {
01141 return sinfo_image_filter_wrapper(img, mx, CPL_FILTER_LINEAR);
01142
01143 }
01144
01145
01146 cpl_image * sinfo_image_filter_median(const cpl_image * img, const cpl_matrix * mx)
01147 {
01148 return sinfo_image_filter_wrapper(img, mx, CPL_FILTER_MEDIAN);
01149 }
01150
01151