sinfo_new_bezier.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 /*****************************************************************************
00020 * M.P.E. - SPIFFI project
00021 *
00022 *
00023 *
00024 * who       when      what
00025 
00026 * --------  --------  ----------------------------------------------
00027 * rabuter 2004-11-04 Fixed error on Find Cosmic when no valid neighboors
00028                      where found in the subcube
00029 * rabuter 10/07/03  created
00030 */
00031 
00032 /************************************************************************
00033 *   NAME
00034 *        sinfo_new_bezier.c -
00035 *        procedures to correct for bad pixels using bezier splines
00036 *
00037 *   SYNOPSIS
00038 *
00039 *   DESCRIPTION
00040 *
00041 *   FILES
00042 *
00043 *   ENVIRONMENT
00044 *
00045 *   RETURN VALUES
00046 *
00047 *   CAUTIONS
00048 *
00049 *   EXAMPLES
00050 *
00051 *   SEE ALSO
00052 *
00053 *   BUGS
00054 *
00055 *------------------------------------------------------------------------
00056 */
00057 
00058 #ifdef HAVE_CONFIG_H
00059 #  include <config.h>
00060 #endif
00061 #define POSIX_SOURCE 1
00062 #include "sinfo_vltPort.h"
00063 
00064 /*
00065  * System Headers
00066  */
00067 
00068 /*
00069  * Local Headers
00070  */
00071 
00072 #include "sinfo_new_bezier.h"
00073 #include "sinfo_msg.h"
00081 /*----------------------------------------------------------------------------
00082  *                            Function codes
00083  *--------------------------------------------------------------------------*/
00084 
00093 int sinfo_im_xy(cpl_image* im, int X, int Y)
00094 {
00095   int res=0;
00096 
00097    res=X+Y*cpl_image_get_size_x(im);
00098   return res;
00099 }
00108 int sinfo_im_xyz(cpl_image* im, int X, int Y, int Z)
00109 {
00110   int res=0;
00111    res = X+
00112          Y*cpl_image_get_size_x(im)+
00113          Z*cpl_image_get_size_x(im)*
00114            cpl_image_get_size_y(im);
00115   return res;
00116 }
00117 
00118 
00119 
00128 int sinfo_cu_xy(cpl_imagelist* cu, int X, int Y)
00129 {
00130   int res=0;
00131 
00132    res=X+Y*cpl_image_get_size_x(cpl_imagelist_get(cu,0));
00133   return res;
00134 }
00145 int sinfo_cu_xyz(cpl_imagelist* cu, int X, int Y, int Z)
00146 {
00147   int res=0;
00148    res = X+
00149          Y*cpl_image_get_size_x(cpl_imagelist_get(cu,0))+
00150          Z*cpl_image_get_size_x(cpl_imagelist_get(cu,0))*
00151            cpl_image_get_size_y(cpl_imagelist_get(cu,0));
00152   return res;
00153 }
00154 
00169 cpl_image *
00170 sinfo_new_c_bezier_interpolate_image(cpl_image *im,
00171                                      cpl_image *mask,
00172                                      new_Lookup *look,
00173                                            short rx,
00174                                            short ry,
00175                                            short rz,
00176                            int max_rad ,
00177                                            float   ** slit_edges )
00178 {
00179 
00180     int i,j,count;
00181     cpl_imagelist * sc_im,* drs_sc_mask;
00182     cpl_image *auxImage;
00183     cpl_image *tempMask;
00184     short szx,szy,szz;
00185     short rx_loop, ry_loop, rz_loop;
00186     /*float ant,new,dif;*/
00187 
00188     int ilx=0;
00189     int ily=0;
00190     int mlx=0;
00191     int mly=0;
00192 
00193     float* pidata=NULL;
00194     float* pmdata=NULL;
00195     float* ptdata=NULL;
00196     float* padata=NULL;
00197 
00198     cpl_image* sc_img=NULL;
00199     cpl_image* drs_img=NULL;
00200 
00201     mlx=cpl_image_get_size_x(mask);
00202     mly=cpl_image_get_size_y(mask);
00203     ilx=cpl_image_get_size_x(im);
00204     ily=cpl_image_get_size_y(im);
00205 
00206     pmdata=cpl_image_get_data_float(mask);
00207     pidata=cpl_image_get_data_float(im);
00208 
00209     if ( mlx != ilx || mly != ily )
00210     {
00211         sinfo_msg_error(" data & mask images not compatible in size\n") ;
00212         return NULL ;
00213     }
00214 
00215     /* allocate memory for sub cubes*/
00216     szx = (rx * 2 ) + 1;
00217     szy = (ry * 2 ) + 1;
00218     szz = (rz * 2 ) + 1;
00219 
00220     if ( NULL == ( sc_im = cpl_imagelist_new() ) )
00221     {
00222         sinfo_msg_error(" could not allocate memory for data subcube\n") ;
00223         return NULL ;
00224     }
00225 
00226     for(i=0;i<szz;i++) {
00227       sc_img=cpl_image_new(szx,szy,CPL_TYPE_FLOAT);
00228       cpl_imagelist_set(sc_im,sc_img,i);
00229     }
00230 
00231     if ( NULL == ( drs_sc_mask = cpl_imagelist_new() ) )
00232     {
00233         sinfo_msg_error(" could not allocate memory for mask subcube\n") ;
00234         return NULL ;
00235     }
00236     for(i=0;i<szz;i++) {
00237       drs_img=cpl_image_new(szx,szy,CPL_TYPE_FLOAT);
00238       cpl_imagelist_set(drs_sc_mask,drs_img,i);
00239     }
00240 
00241     if ( NULL == ( tempMask = cpl_image_new(mlx, mly, CPL_TYPE_FLOAT) ) )
00242     {
00243         sinfo_msg_error("could not allocate memory for temporary "
00244                         "dead pixel mask\n") ;
00245         return NULL ;
00246     }
00247     ptdata=cpl_image_get_data_float(tempMask);
00248 
00249     count=0;
00250     for ( i = 0 ; i < mlx; i++ )
00251     {
00252         for ( j = 0 ; j < mly ; j++ )
00253         {
00254         if ( pmdata[sinfo_im_xy(im,i,j)] ==  cubePT_BADPIXEL )
00255         {
00256           rx_loop = 1 ; ry_loop = 1 ; rz_loop = 1 ;
00257           pidata[sinfo_im_xy(im,i,j)] =
00258                    sinfo_new_c_bezier_correct_pixel( i, j, im, mask, sc_im,
00259                                drs_sc_mask, look, rx_loop, ry_loop, rz_loop );
00260           /* if not enough neighbors found, increase size of sub
00261                      cube until max radius is reached */
00262           while ( pidata[sinfo_im_xy(im,i,j)] == cubeNONEIGHBOR &&
00263                           rx_loop < rx && ry_loop < ry && rz_loop < rz )
00264             {
00265               rx_loop++ ; ry_loop++; rz_loop++;
00266               /* sinfo_msg_warning("Increasing radius to %d, in %d %d",
00267                                             rx_loop, i, j) ; */
00268 
00269               pidata[sinfo_im_xy(im,i,j)] =
00270                         sinfo_new_c_bezier_correct_pixel( i, j, im, mask, sc_im,
00271                                 drs_sc_mask, look, rx_loop, ry_loop, rz_loop );
00272             }
00273           /* If still not enough neighbors, make result NaN = ZERO
00274                      in spred convention */
00275           if ( pidata[sinfo_im_xy(im,i,j)] == cubeNONEIGHBOR )
00276             {
00277               pidata[sinfo_im_xy(im,i,j)] = ZERO ;
00278             }
00279         count++;
00280         }
00281         if ( pidata[sinfo_im_xy(im,i,j)] == ZERO )
00282           {
00283         ptdata[sinfo_im_xy(tempMask,i,j)] = 0 ;
00284           }
00285          else
00286           {
00287         ptdata[sinfo_im_xy(tempMask,i,j)] = 1 ;
00288           }
00289         }
00290     }
00291 
00292 
00293     sinfo_msg("Replacing NaN\n");
00294     auxImage=sinfo_interpol_source_image( im, tempMask, max_rad, slit_edges );
00295     padata=cpl_image_get_data_float(auxImage);
00296     for ( i = 0 ; i < mlx; i++ )
00297     {
00298         for ( j = 0 ; j < mly ; j++ )
00299         {
00300 
00301         if ( isnan(pidata[sinfo_im_xy(im,i,j)])) /*<= -2e10ZERO )*/
00302         {
00303           /* sinfo_msg_warning("Replacing NaN -> %d %d %f\n",
00304                                        i,j, padata[sinfo_im_xy(im,i,j)] ); */
00305         pidata[sinfo_im_xy(im,i,j)] = padata[sinfo_im_xy(im,i,j)];
00306         }
00307         }
00308     }
00309     cpl_image_delete(auxImage);
00310     cpl_imagelist_delete(sc_im);
00311     cpl_imagelist_delete(drs_sc_mask);
00312 
00313     sinfo_msg("bad pixels count: %d\n",count);
00314 
00315 
00316     return im;
00317 }
00318 
00319 cpl_image *
00320 sinfo_new_c_bezier_find_bad( cpl_image *im,
00321                                    cpl_image *mask,
00322                                    /* Lookup *look,*/
00323                                    short rx,
00324                                    short ry,
00325                                    short rz,
00326                        short lowerI,
00327                                    short highI,
00328                                    short lowerJ,
00329                                    short highJ,
00330                                    float factor )
00331 {
00332 
00333     int i,j,count;
00334     cpl_imagelist * sc_im,* drs_sc_mask;
00335     short szx,szy,szz;
00336     float /*ant,*/newValue,old/*,dif,porcentage,distance*/;
00337     double med, stdev;
00338     /*cpl_image *out;*/
00339     short rx_loop, ry_loop, rz_loop;
00340 
00341     int ilx=0;
00342     int ily=0;
00343     int mlx=0;
00344     int mly=0;
00345 
00346     float* pidata=NULL;
00347     float* pmdata=NULL;
00348 
00349     cpl_image* sc_img=NULL;
00350     cpl_image* drs_img=NULL;
00351 
00352 
00353     mlx=cpl_image_get_size_x(mask);
00354     mly=cpl_image_get_size_y(mask);
00355     ilx=cpl_image_get_size_x(im);
00356     ily=cpl_image_get_size_y(im);
00357 
00358     pmdata=cpl_image_get_data_float(mask);
00359     pidata=cpl_image_get_data_float(im);
00360 
00361 
00362     if ( mlx != ilx || mly != ily )
00363     {
00364         sinfo_msg_error(" data & mask images not compatible in size\n") ;
00365         return NULL ;
00366     }
00367 
00368     /* allocate memory for sub cubes*/
00369     szx = (rx * 2 ) + 1;
00370     szy = (ry * 2 ) + 1;
00371     szz = (rz * 2 ) + 1;
00372 
00373     if ( NULL == ( sc_im = cpl_imagelist_new() ) )
00374     {
00375         sinfo_msg_error(" could not allocate memory for data subcube\n") ;
00376         return NULL ;
00377     }
00378     for(i=0;i<szz;i++) {
00379       sc_img=cpl_image_new(szx,szy,CPL_TYPE_FLOAT);
00380       cpl_imagelist_set(sc_im,sc_img,i);
00381     }
00382 
00383     if ( NULL == ( drs_sc_mask = cpl_imagelist_new() ) )
00384     {
00385         sinfo_msg_error(" could not allocate memory for mask subcube\n") ;
00386         return NULL ;
00387     }
00388     for(i=0;i<szz;i++) {
00389       drs_img=cpl_image_new(szx,szy,CPL_TYPE_FLOAT);
00390       cpl_imagelist_set(drs_sc_mask,drs_img,i);
00391     }
00392 
00393     count=0;
00394     for ( i = 0 ; i < mlx; i++ )
00395     {
00396         for ( j = 0 ; j < mly ; j++ )
00397         {
00398         if ( i >= lowerI && i < highI && j >= lowerJ && j < highJ )
00399         {
00400 
00401         rx_loop = 1 ; ry_loop = 1 ; rz_loop = 1 ;
00402         newValue = sinfo_new_c_bezier_correct_pixel_2D( i, j, im,
00403                                                                 mask,
00404                                                                 sc_im,
00405                                                                 drs_sc_mask,
00406                                                                 /* look,*/
00407                                                                 rx_loop,
00408                                                                 ry_loop,
00409                                                                 rz_loop,
00410                                                                 &med,
00411                                                                 &stdev,
00412                                                                 factor );
00413             /* if NaN returned, increase size of sub cube
00414                    until max radius is reached */
00415         while ( newValue == ZERO && rx_loop < rx &&
00416                         ry_loop < ry && rz_loop < rz )
00417             {
00418               rx_loop++ ; ry_loop++; rz_loop++;
00419               /*sinfo_msg_warning("Increasing radius to %d,
00420                         in %d %d", rx_loop, i, j) ;  */
00421                    newValue = sinfo_new_c_bezier_correct_pixel_2D( i, j, im,
00422                                                                       mask,
00423                                                                       sc_im,
00424                                                                     drs_sc_mask,
00425                                                                     /*, look*/
00426                                                                     rx_loop,
00427                                                                     ry_loop,
00428                                                                     rz_loop,
00429                                                                     &med,
00430                                                                     &stdev,
00431                                                                     factor );
00432             }
00433         if ( isnan(newValue)) /*<= -3.e10 ZERO )*/
00434             continue;
00435 
00436         old = pidata[sinfo_im_xy(im,i,j)];
00437         if ( newValue != old )
00438             {
00439             pidata[sinfo_im_xy(im,i,j)] = newValue;
00440             /*sinfo_msg_warning("[%d,%d]=%f -> %f, med= %f,
00441                       stdev=%f\n",i,j, old, newValue, med, stdev );*/
00442             count++;
00443             }
00444         }
00445         }
00446     }
00447 
00448 
00449     sinfo_msg("bad pixels count: %d\n",count);
00450 
00451 
00452     cpl_imagelist_delete(sc_im);
00453     cpl_imagelist_delete(drs_sc_mask);
00454     return im;
00455 }
00456 
00457 float
00458 sinfo_new_c_bezier_correct_pixel(int ipos,
00459                                  int jpos,
00460                                  cpl_image * im,
00461                                  cpl_image * mask,
00462                      cpl_imagelist * sc_im,
00463                                  cpl_imagelist * drs_sc_mask,
00464                                  new_Lookup * look,
00465                                  short rx,
00466                                  short ry,
00467                                  short rz )
00468 {
00469     short ic, jc, kc, ii, jj, kk/*, sjj, skk*/,is,js,ks;
00470     short i, j, k, indexJ, indexI, lx, ly, lz, szx, szy, szz;
00471     /*float indexIf,indexJf,sp;*/
00472     cpl_image * X, * Y, * Z, * hX;
00473     cpl_imagelist  * id, * jd;
00474 
00475     int idlx=0;
00476     int idly=0;
00477     int idnp=0;
00478 
00479     int drslx=0;
00480     int drsly=0;
00481     int drsnp=0;
00482 
00483 
00484     float* pXdata=NULL;
00485     float* pYdata=NULL;
00486     float* pZdata=NULL;
00487     float* phXdata=NULL;
00488     float* pidata=NULL;
00489     float* pmdata=NULL;
00490     float* piddata=NULL;
00491     float* pjddata=NULL;
00492     float* pscdata=NULL;
00493     float* pdrsdata=NULL;
00494 
00495     cpl_image* id_img=NULL;
00496     cpl_image* jd_img=NULL;
00497     cpl_image* sc_img=NULL;
00498     cpl_image* drs_img=NULL;
00499 
00500     X  = look -> X;
00501     Y  = look -> Y;
00502     Z  = look -> Z;
00503     hX = look -> hX;
00504     id = look -> id;
00505     jd = look -> jd;
00506 
00507     /*
00508       phXdata=cpl_image_get_data_float(hX);
00509       if ( phXdata[sinfo_im_xy( hX, ipos, jpos)] > 1 )
00510     {
00511     sinfo_msg_error(" double hit in position [%d,%d]=%f, "
00512                         "can not correct\n",
00513         ipos,jpos,phXdata[sinfo_im_xy(hX,ipos,jpos)]) ;
00514     return ( -2e10 );
00515     }*/
00516 
00517     pidata=cpl_image_get_data_float(im);
00518     pmdata=cpl_image_get_data_float(mask);
00519 
00520     phXdata=cpl_image_get_data_float(hX);
00521     if ( phXdata[sinfo_im_xy( hX, ipos, jpos)] < 1 )
00522     {
00523     /*sinfo_msg_error("no lookup in position [%d,%d]=%f, can not correct",
00524       ipos,jpos,phXdata[sinfo_im_xy(hX,ipos,jpos)]) ;*/
00525     return ( ZERO );
00526     }
00527     pXdata=cpl_image_get_data_float(X);
00528     pYdata=cpl_image_get_data_float(Y);
00529     pZdata=cpl_image_get_data_float(Z);
00530 
00531 
00532     ic = pXdata[sinfo_im_xy( X, ipos, jpos)];
00533     jc = pYdata[sinfo_im_xy( Y, ipos, jpos)];
00534     kc = pZdata[sinfo_im_xy( Z, ipos, jpos)];
00535     /*if ( !(ipos % 16 )  )*/
00536 #ifdef DEBUG
00537     sinfo_msg_debug("Correcting bad pixel : ipos=%d,jpos=%d, "
00538                     "in Cube -> ic=%d, jc=%d, kc=%d\n",
00539                     ipos,jpos, ic, jc, kc );
00540 #endif
00541     /*limit to start not before the beginning of the cube*/
00542     ii = ic - rx; if ( ii < 0 ) ii = 0;
00543     jj = jc - ry; if ( jj < 0 ) jj = 0;
00544     kk = kc - rz; if ( kk < 0 ) kk = 0;
00545 
00546 #ifdef DEBUG
00547     sinfo_msg_debug("Start Point in Cube -> ii=%d,jj=%d,kk=%d\n", ii, jj, kk );
00548 #endif
00549 
00550     /*limit to end not outside of the cube */
00551     szx = (rx * 2 ) + 1;
00552     szy = (ry * 2 ) + 1;
00553     szz = (rz * 2 ) + 1;
00554 
00555     idlx=cpl_image_get_size_x(cpl_imagelist_get(id,0));
00556     idly=cpl_image_get_size_y(cpl_imagelist_get(id,0));
00557     idnp=cpl_imagelist_get_size(id);
00558 
00559     lx = idlx;
00560     ly = idly;
00561     lz = idnp;
00562 
00563     if ( ( ic + rx ) >= idlx )
00564     szx = szx - ( (ic+rx)-(lx-1) );
00565 
00566     if ( ( jc + ry ) >= idly )
00567     szy = szy - ( (jc+ry)-(ly-1) );
00568 
00569     if ( ( kc + rz ) >= idnp )
00570     szz = szz - ( (kc+rz)-(lz-1) );
00571 
00572     drslx=cpl_image_get_size_x(cpl_imagelist_get(drs_sc_mask,0));
00573     drsly=cpl_image_get_size_y(cpl_imagelist_get(drs_sc_mask,0));
00574     drsnp=cpl_imagelist_get_size(drs_sc_mask);
00575 #ifdef DEBUG
00576 
00577 
00578     sinfo_msg_error("Size of subcube: szx=%d,szy=%d,szz=%d\n", szx, szy, szz );
00579     /*fill whole mask with not available*/
00580     sinfo_msg_error("Fill Mask subcube of size: %d,%d,%d, with NOINFO\n",
00581                      drslx, drsly,  drsnp);
00582 #endif
00583     for( i = 0; i < drslx; i++) {
00584       for( j = 0; j < drsly; j++) {
00585     for( k = 0; k < drsnp; k++) {
00586       drs_img=cpl_imagelist_get(drs_sc_mask,k);
00587           pdrsdata=cpl_image_get_data_float(drs_img);
00588       pdrsdata[sinfo_cu_xy(drs_sc_mask,i,j)] = cubePT_NOINFO;
00589     }
00590       }
00591     }
00592 
00593      for( i = ii,is=0;  i < ii+szx; i++,is++)
00594      {
00595      for( j = jj,js=0;  j < jj+szy; j++,js++)
00596          {
00597          for( k = kk,ks=0;  k < kk+szz; k++,ks++)
00598          {
00599 #ifdef DEBUG
00600          sinfo_msg_debug("i=%d j=%d k=%d is=%d ij=%d ik=%d",
00601                                   i,j,k,is,js,ks);
00602 #endif
00603                  id_img=cpl_imagelist_get(id,k);
00604                  jd_img=cpl_imagelist_get(jd,k);
00605                  piddata=cpl_image_get_data_float(id_img);
00606                  pjddata=cpl_image_get_data_float(jd_img);
00607 
00608                  drs_img=cpl_imagelist_get(drs_sc_mask,ks);
00609                  pdrsdata=cpl_image_get_data_float(drs_img);
00610                  sc_img=cpl_imagelist_get(sc_im,ks);
00611                  pscdata=cpl_image_get_data_float(sc_img);
00612 
00613          indexI = sinfo_new_nint( piddata[sinfo_cu_xy(id,i,j)] );
00614          indexJ = sinfo_new_nint( pjddata[sinfo_cu_xy(jd,i,j)] );
00615          if ( indexJ <= -1 || indexJ>=2048 || indexI == -1 )
00616            {
00617              pdrsdata[sinfo_cu_xy(drs_sc_mask,is,js)] = cubePT_NOINFO;
00618              continue;
00619            }
00620          pscdata[sinfo_cu_xy(sc_im,is,js)]  =
00621                          pidata[sinfo_im_xy(im,indexI,indexJ)];
00622          pdrsdata[sinfo_cu_xy(drs_sc_mask,is,js)]  =
00623                          pmdata[sinfo_im_xy(mask,indexI,indexJ)];
00624 #ifdef DEBUG
00625         sinfo_msg_debug("Cube i=%d, j=%d, k=%d  ;"
00626                                 "  Sub is=%d, js=%d, ks=%d  ;"
00627                                 "  Plane I=%d,J=%d ; mask %f ; im %f",
00628                         i, j, k, is, js, ks, indexI, indexJ,
00629                                 mask -> data[sinfo_im_xy(mask,indexI,indexJ)],
00630                                 im   -> data[sinfo_im_xy(im,indexI,indexJ)]);
00631 #endif
00632 
00633          }
00634          }
00635      }
00636 
00637 
00638     /*signal to correct this pixel*/
00639      drs_img=cpl_imagelist_get(drs_sc_mask,rz);
00640      pdrsdata=cpl_image_get_data_float(drs_img);
00641     pdrsdata[sinfo_cu_xy(drs_sc_mask,rx,ry)] = cubePT_FIND;
00642     return ( sinfo_new_c_bezier_interpol( sc_im, drs_sc_mask ) );
00643 }
00644 
00645 float
00646 sinfo_new_c_bezier_correct_pixel_2D(int ipos,
00647                                     int jpos,
00648                                     cpl_image * im,
00649                                     cpl_image * mask,
00650                         cpl_imagelist * sc_im,
00651                                     cpl_imagelist * drs_sc_mask,
00652                                     /* Lookup * look,*/
00653                                     short rx, short ry,
00654                                     short rz ,
00655                                     double *med ,
00656                         double *stdev,
00657                                     float factor )
00658 {
00659     short ic, jc, kc, ii, jj, kk/*, sjj, skk*/,is,js,ks;
00660     short i, j, k, indexJ, indexI, lx, ly, lz, szx, szy, szz;
00661     double sum,aux;
00662     int counter;
00663     float sumarr[100];
00664 
00665 
00666     int ilx=0;
00667     int ily=0;
00668 
00669     int drslx=0;
00670     int drsly=0;
00671     int drsnp=0;
00672 
00673 
00674     float* pidata=0;
00675     float* pmdata=0;
00676     float* pscdata=0;
00677     float* pdrsdata=0;
00678 
00679     cpl_image* drs_img=NULL;
00680     cpl_image* sc_img=NULL;
00681 
00682     jc = 0;
00683     ic = ipos;
00684     kc = jpos;
00685     sinfo_msg_debug("Correcting bad pixel : ipos=%d,jpos=%d, "
00686                     "in Cube -> ic=%d, jc=%d, kc=%d", ipos,jpos, ic, jc, kc );
00687     /*limit to start not before the beginning of the cube*/
00688     ii = ic - rx; if ( ii < 0 ) ii = 0;
00689     jj = jc - ry; if ( jj < 0 ) jj = 0;
00690     kk = kc - rz; if ( kk < 0 ) kk = 0;
00691 
00692     sinfo_msg_debug("Start Point in Cube -> ii=%d,jj=%d,kk=%d", ii, jj, kk );
00693 
00694     ilx=cpl_image_get_size_x(im);
00695     ily=cpl_image_get_size_y(im);
00696 
00697     /*limit to end not outside of the cube */
00698     szx = (rx * 2 ) + 1;
00699     szy = (ry * 2 ) + 1;
00700     szz = (rz * 2 ) + 1;
00701     lx = ilx;
00702     ly = ily;
00703     lz = ily;
00704     if ( ( ic + rx ) >= ilx )
00705     szx = szx - ( (ic+rx)-(lx-1) );
00706 
00707     if ( ( jc + ry ) >= ily )
00708     szy = szy - ( (jc+ry)-(ly-1) );
00709 
00710     if ( ( kc + rz ) >= ily )
00711     szz = szz - ( (kc+rz)-(lz-1) );
00712 
00713 #ifdef DEBUG
00714     drslx=cpl_image_get_size_x(cpl_imagelist_get(drs_sc_mask,0));
00715     drsly=cpl_image_get_size_y(cpl_imagelist_get(drs_sc_mask,0));
00716     drsnp=cpl_imagelist_get_size(drs_sc_mask);
00717     sinfo_msg_debug("Size of subcube : szx=%d,szy=%d,szz=%d", szx, szy, szz );
00718     /*fill whole mask with not available*/
00719     sinfo_msg_debug("Fill Mask subcube of size:%d,%d,%d, with NOINFO",
00720                     drslx, drsly,  drsnp);
00721 #endif
00722     for( i = 0; i < drslx; i++) {
00723       for( j = 0; j < drsly; j++) {
00724     for( k = 0; k < drsnp; k++) {
00725       drs_img=cpl_imagelist_get(drs_sc_mask,k);
00726       pdrsdata=cpl_image_get_data_float(drs_img);
00727       pdrsdata[sinfo_cu_xy(drs_sc_mask,i,j)] = cubePT_NOINFO;
00728     }
00729       }
00730     }
00731     counter = 0;
00732     sum=0;
00733     memset(sumarr,0x00,sizeof(sumarr));
00734     pidata=cpl_image_get_data(im);
00735     pmdata=cpl_image_get_data(mask);
00736 
00737      for( i = ii,is=0;  i < ii+szx; i++,is++)
00738      {
00739      for( j = jj,js=0;  j < jj+szy; j++,js++)
00740          {
00741          for( k = kk,ks=0;  k < kk+szz; k++,ks++)
00742          {
00743 #ifdef DEBUG
00744          sinfo_msg_debug("i=%d j=%d k=%d is=%d ij=%d ik=%d",
00745                                   i,j,k,is,js,ks);
00746 #endif
00747          indexI = i;
00748          indexJ = k;
00749          if ( isnan(pidata[sinfo_im_xy(mask,indexI,indexJ)]) )
00750            pmdata[sinfo_im_xy(mask,indexI,indexJ)] = 0;
00751 
00752          if ( pmdata[sinfo_im_xy(mask,indexI,indexJ)] == 1 &&
00753                             ( indexI != ipos || indexJ != jpos) )
00754            {
00755              /*sumarr[counter] = pidata[sinfo_im_xy(im,indexI,indexJ)];*/
00756              sum = sum + pidata[sinfo_im_xy(im,indexI,indexJ)];
00757              counter++;
00758            }
00759          sc_img=cpl_imagelist_get(sc_im,ks);
00760          pscdata[sinfo_cu_xy(sc_im,is,js)]=
00761                         pidata[sinfo_im_xy(im,indexI,indexJ)];
00762                  drs_img=cpl_imagelist_get(drs_sc_mask,ks);
00763                  pdrsdata=cpl_image_get_data_float(drs_img);
00764          pdrsdata[sinfo_cu_xy(drs_sc_mask,is,js)]=
00765                          pmdata[sinfo_im_xy(mask,indexI,indexJ)];
00766 #ifdef DEBUG
00767          sinfo_msg_debug("Cube i=%d, j=%d, k=%d  ;  "
00768                                  "Sub is=%d, js=%d, ks=%d  ; "
00769                                 " Plane I=%d,J=%d ; mask %f ; im %f",
00770                         i, j, k, is, js, ks, indexI, indexJ,
00771                                 pmdata[sinfo_im_xy(mask,indexI,indexJ)],
00772                                 pidata[sinfo_im_xy(im,indexI,indexJ)]);
00773 #endif
00774 
00775          }
00776          }
00777      }
00778 
00779 
00780     /*signal to correct this pixel*/
00781     drs_img=cpl_imagelist_get(drs_sc_mask,rz);
00782     pdrsdata=cpl_image_get_data_float(drs_img);
00783     pdrsdata[sinfo_cu_xy(drs_sc_mask,rx,ry)] = cubePT_FIND;
00784     if ( counter )
00786     *med = sum/counter;
00787     else
00788     return(pidata[sinfo_im_xy(im,ipos,jpos)]);
00789 
00790     /*sinfo_msg_debug("%f %f %d\n",
00791                       sum ,pidata[sinfo_im_xy(im,ipos,jpos)], counter);*/
00792 
00793 
00794 
00795     sum =0;
00796     counter=0;
00797     for( i = ii,is=0;  i < ii+szx; i++,is++)
00798       {
00799     for( j = jj,js=0;  j < jj+szy; j++,js++)
00800       {
00801         for( k = kk,ks=0;  k < kk+szz; k++,ks++)
00802           {
00803         drs_img=cpl_imagelist_get(drs_sc_mask,ks);
00804         pdrsdata=cpl_image_get_data_float(drs_img);
00805         indexI = i;
00806         indexJ = k;
00807         if ( pdrsdata[sinfo_cu_xy(drs_sc_mask,is,js)] == 1 &&
00808                          ( indexI != ipos || indexJ != jpos) )
00809           {
00810               sc_img=cpl_imagelist_get(sc_im,ks);
00811               pscdata=cpl_image_get_data_float(sc_img);
00812 
00813             sum=sum+((pscdata[sinfo_cu_xy(drs_sc_mask,is,js)]- *med) *
00814              (pscdata[sinfo_cu_xy(drs_sc_mask,is,js)] - *med ) );
00815             counter++;
00816           }
00817           }
00818       }
00819       }
00820 
00821     aux = sum;
00822     sum   = sum / (counter - 1);
00823     *stdev = sqrt( sum );
00824 
00825     if ( (fabs( pidata[sinfo_im_xy(im,ipos,jpos)] - *med ) >
00826          factor * *stdev) ||
00827      isnan(pidata[sinfo_im_xy(im,ipos,jpos)]) )
00828     {
00829     /*sinfo_msg_debug("[%d,%d]: distance to mean = %f,"
00830                           " thres =%f sum=%f, stdev=%f, counter=%d, aux= %f",
00831                 ipos,jpos, fabs( pidata[sinfo_im_xy(im,ipos,jpos)] - *med),
00832                     factor * *stdev, sum,*stdev, counter,aux );
00833     pmdata[sinfo_im_xy(mask,ipos,jpos)] = 0;*/
00834     return ( sinfo_new_c_bezier_interpol( sc_im, drs_sc_mask ) );
00835     }
00836     return(pidata[sinfo_im_xy(im,ipos,jpos)]);
00837 }
00838 
00839 
00840 
00841 float
00842 sinfo_new_c_bezier_interpol( cpl_imagelist * im, cpl_imagelist * action )
00843 {
00844     short pos;
00845     unsigned short i,j,k;
00846     new_XYZW  indata[1000];
00847     new_XYZW  res;
00848     new_XYZW  selected;
00849     float step,cumstep,distance,selected_distance;
00850     new_Dim *point=NULL;
00851     double munk;
00852     int ilx=0;
00853     int ily=0;
00854     int inp=0;
00855 
00856     float* padata=NULL;
00857     float* pidata=NULL;
00858     cpl_image* i_img=NULL;
00859     cpl_image* a_img=NULL;
00860 
00861     selected.w = 0;
00862     memset(indata,0x00,1000*sizeof(new_XYZW));
00863     ilx=cpl_image_get_size_x(cpl_imagelist_get(im,0));
00864     ily=cpl_image_get_size_y(cpl_imagelist_get(im,0));
00865     inp=cpl_imagelist_get_size(im);
00866 
00867     pos=0;
00868     for( i=0; i < ilx; i++)
00869     {
00870     for( j=0; j < ily; j++)
00871         {
00872         for( k=0; k < inp; k++)
00873         {
00874           a_img=cpl_imagelist_get(action,k);
00875                   padata=cpl_image_get_data_float(a_img);
00876           i_img=cpl_imagelist_get(action,k);
00877                   pidata=cpl_image_get_data_float(i_img);
00878         if ( padata[sinfo_cu_xy(action,i,j)] == cubePT_USE )
00879             {
00880 #ifdef DEBUG
00881             sinfo_msg_debug("Used im[%d,%d,%d]=%lf\n",
00882                                       i,j,k,pidata[sinfo_im_xy(im,i,j)]);
00883 #endif
00884             indata[pos].x = i;
00885             indata[pos].y = j;
00886             indata[pos].z = k;
00887             indata[pos].w = pidata[sinfo_cu_xy(im,i,j)];
00888             pos++;
00889             }
00890         else
00891             {
00892             if ( padata[sinfo_cu_xy(action,i,j)] == cubePT_FIND )
00893             {
00894                (*point).x = i;
00895                (*point).y = j;
00896                (*point).z = k;
00897 #ifdef DEBUG
00898             sinfo_msg_debug("Find for im[%d,%d,%d]=%lf reason:%f",
00899                         i,j,k,pidata[sinfo_im_xy(im,i,j)],
00900                         padata[sinfo_cu_xy(action,i,j)]);
00901 #endif
00902             }
00903             else
00904             {
00905 #ifdef DEBUG
00906             sinfo_msg_debug("Ignored im[%d,%d,%d]=%lf reason:%f",
00907                         i,j,k,pidata[sinfo_im_xy(im,i,j)],
00908                         padata[sinfo_im_xy(action,i,j)]);
00909 #endif
00910             }
00911             }
00912         }
00913         }
00914     }
00915 
00916     
00917     if ( pos < 2 )
00918     {
00919 #ifdef DEBUG
00920        sinfo_msg_debug("subcube contains no valid pixels "
00921                        "to use in iterpolation");
00922 #endif
00923     /*i_img=cpl_imagelist_get((*point).z);
00924           pidata=cpl_image_get_data_float(i_img);
00925           return( pidata[sinfo_im_xy(im,(*point).x,(*point).y)] );*/
00926     return( cubeNONEIGHBOR );
00927 
00928     }
00929 
00930 
00931     step    = 0.01;
00932     cumstep = 0.0;
00933     selected_distance=1000;
00934     munk = pow( 1.0-cumstep, (double)pos - 1 );
00935     for ( i = 0 ; ( i < 100 ) && ( munk != 0.0 ); i++ )
00936     {
00937     memset( &res, 0x00, sizeof(new_XYZW) );
00938     sinfo_new_bezier( indata, pos-1, cumstep, munk, &res);
00939     distance = sqrt( pow( ((*point).x-res.x), 2)+
00940                      pow( ((*point).y-res.y), 2)+
00941                      pow( ((*point).z-res.z), 2) );
00942     /*sinfo_msg_debug("%lf %lf %lf %lf %lf\n",
00943                           res.x,res.y,res.z,res.w,distance);*/
00944     if ( distance < selected_distance )
00945         {
00946         selected_distance = distance;
00947         selected.x = res.x;
00948         selected.y = res.y;
00949         selected.z = res.z;
00950         selected.w = res.w;
00951         }
00952     cumstep = cumstep + step;
00953     munk = pow( 1.0 - cumstep, (double)pos - 1 );
00954 
00955     }
00956 
00957 #ifdef DEBUG
00958    sinfo_msg_debug("Selected %lf %lf %lf %lf, distance=%lf",
00959                     selected.x,selected.y,selected.z,
00960                     selected.w,selected_distance);
00961 #endif
00962    i_img=cpl_imagelist_get(im,(*point).z);
00963     pidata=cpl_image_get_data_float(i_img);
00964     pidata[sinfo_cu_xy(im,(*point).x,(*point).y)] = selected.w;
00965 
00966     return selected.w;
00967 }
00968 
00969 
00970 
00971 int
00972 sinfo_new_bezier(new_XYZW *p,int n,double mu,double munk,new_XYZW *res )
00973 {
00974    int k, kn, nn, nkn;
00975    double blend, muk;
00976 
00977    muk = 1;
00978    for ( k = 0; k <= n; k++ ) {
00979       nn = n;
00980       kn = k;
00981       nkn = n - k;
00982       blend = muk * munk;
00983       muk *= mu;
00984       munk /= ( 1.0 - mu );
00985       while ( nn >= 1 ) {
00986          blend *= (double)nn;
00987          nn--;
00988          if ( kn > 1 ) {
00989             blend /= (double)kn;
00990             kn--;
00991          }
00992          if ( nkn > 1 ) {
00993             blend /= (double)nkn;
00994             nkn--;
00995          }
00996       }
00997       res -> x += p[k].x * blend;
00998       res -> y += p[k].y * blend;
00999       res -> z += p[k].z * blend;
01000       res -> w += p[k].w * blend;
01001    }
01002    return( 0 );
01003 }
01004 
01005 int
01006 sinfo_new_c_create_XYZ( new_Lookup *l )
01007 {
01008     cpl_image *imX,*imY,*imZ,*imcX;
01009     short i,j,k,indexI,indexJ,x,y,z;
01010     int size;
01011     int idlx=0;
01012     int idly=0;
01013     int idnp=0;
01014     float* piddata=NULL;
01015     float* pjddata=NULL;
01016     float* pXdata=NULL;
01017     float* pYdata=NULL;
01018     float* pZdata=NULL;
01019     float* phXdata=NULL;
01020 
01021     cpl_image* i_img=NULL;
01022     cpl_image* j_img=NULL;
01023 
01024     idlx=cpl_image_get_size_x(cpl_imagelist_get(l->id,0));
01025     idly=cpl_image_get_size_y(cpl_imagelist_get(l->id,0));
01026     idnp=cpl_imagelist_get_size(l->id);
01027 
01028     size = idlx*idly;
01029     /* allocate memory */
01030     if ( NULL == (imX = cpl_image_new(size, size, CPL_TYPE_FLOAT)) )
01031         {
01032     sinfo_msg_error(" could not allocate memory for X !\n") ;
01033     return -1 ;
01034         }
01035     if ( NULL == (imY = cpl_image_new(size, size, CPL_TYPE_FLOAT)) )
01036         {
01037     sinfo_msg_error(" could not allocate memory for Y !\n") ;
01038     return -1 ;
01039         }
01040     if ( NULL == (imZ = cpl_image_new(size, size, CPL_TYPE_FLOAT)) )
01041         {
01042     sinfo_msg_error(" could not allocate memory for Z !\n") ;
01043     return -1 ;
01044         }
01045     if ( NULL == (imcX = cpl_image_new(size, size, CPL_TYPE_FLOAT)) )
01046         {
01047     sinfo_msg_error(" could not allocate memory for cX !\n") ;
01048     return -1 ;
01049         }
01050 
01051     l -> X  = imX;
01052     l -> Y  = imY;
01053     l -> Z  = imZ;
01054     l -> hX = imcX;
01055 
01056     /*Round id*/
01057     for( i = 0; i < idlx; i++)
01058     {
01059     for( j = 0; j < idly; j++)
01060         {
01061         for( k = 0; k < idnp; k++)
01062         {
01063           i_img=cpl_imagelist_get(l->id,k);
01064           piddata=cpl_image_get_data_float(i_img);
01065           piddata[sinfo_cu_xy(l->id,i,j)] =
01066                   (float)sinfo_new_nint(piddata[sinfo_cu_xy(l->id,i,j)]);
01067         }
01068         }
01069     }
01070 
01071     /*Round jd*/
01072     for( i = 0; i < idlx; i++)
01073     {
01074     for( j = 0; j < idly; j++)
01075         {
01076         for( k = 0; k < idnp; k++)
01077         {
01078           j_img=cpl_imagelist_get(l->jd,k);
01079           pjddata=cpl_image_get_data_float(j_img);
01080           pjddata[sinfo_cu_xy(l->jd,i,j)] =
01081                  (float)sinfo_new_nint(pjddata[sinfo_cu_xy(l->jd,i,j)]);
01082         }
01083         }
01084     }
01085 
01086     /*Fill with -1 X Y Z*/
01087     for( i = 0; i < cpl_image_get_size_x(l -> X); i++)
01088     {
01089     for( j = 0; j < cpl_image_get_size_y(l -> X); j++)
01090         {
01091           pXdata=cpl_image_get_data_float(l->X);
01092           pYdata=cpl_image_get_data_float(l->Y);
01093           pZdata=cpl_image_get_data_float(l->Z);
01094 
01095           pXdata[sinfo_im_xy(l->X,i,j)] = ZERO;
01096           pYdata[sinfo_im_xy(l->Y,i,j)] = ZERO;
01097           pZdata[sinfo_im_xy(l->Z,i,j)] = ZERO;
01098         }
01099     }
01100 #define  FORW
01101 #ifdef BACK
01102     for( x = idlx - 1;x>=0;x--)
01103     {
01104     for( y = idly - 1;y>=0;y--)
01105         {
01106         for( z = idnp - 1;z>=0;z--)
01107 #endif
01108 #ifdef FORW
01109     for( x = 0; x <  idlx; x++)
01110     {
01111     for( y = 0; y < idly; y++)
01112         {
01113         for( z = 0; z < idnp; z++)
01114 #endif
01115           {
01116         i_img=cpl_imagelist_get(l->id,z);
01117         piddata=cpl_image_get_data_float(i_img);
01118         j_img=cpl_imagelist_get(l->jd,z);
01119         pjddata=cpl_image_get_data_float(j_img);
01120         indexI = piddata [sinfo_cu_xy(l->id,x,y)];
01121         indexJ = pjddata [sinfo_cu_xy(l->jd,x,y)];
01122         if ( indexI > 0.0  && indexI < size  &&
01123              indexJ > 0.0  && indexJ < size )
01124             {
01125             /*sinfo_msg_debug("%d %d %d = %f, %f\n",
01126                       x,y,z,(float)ICube(x,y,z),(float)JCube(x,y,z));*/
01127               pXdata=cpl_image_get_data_float(l->X);
01128               pYdata=cpl_image_get_data_float(l->Y);
01129               pZdata=cpl_image_get_data_float(l->Z);
01130               phXdata=cpl_image_get_data_float(l->hX);
01131 
01132               pXdata[sinfo_im_xy(l->X ,indexI,indexJ)] = x;
01133               phXdata[sinfo_im_xy(l->hX,indexI,indexJ)] =
01134                          phXdata[sinfo_im_xy(l->hX,indexI,indexJ)] + 1;
01135 
01136             pYdata[sinfo_im_xy(l->Y ,indexI,indexJ)] = y;
01137             /*phXdata[sinfo_im_xy(l->hX,indexI,indexJ)] =
01138                       phXdata[sinfo_im_xy(l->hX,indexI,indexJ)] + 1;*/
01139 
01140             pZdata[sinfo_im_xy(l->Z ,indexI,indexJ)] = z;
01141             /*phXdata[sinfo_im_xy(l->hX,indexI,indexJ)] =
01142                       phXdata[sinfo_im_xy(l->hX,indexI,indexJ)] + 1;*/
01143             }
01144         }
01145         }
01146     }
01147 
01148 
01149     sinfo_msg("Filled X Y Z , cX cY cZ 2D frames\n");
01150     return(0);
01151 }
01152 
01158 new_Lookup *
01159 sinfo_new_lookup( void )
01160 {
01161     new_Lookup *l;
01162     l = (new_Lookup*)cpl_calloc(1, sizeof(new_Lookup));
01163     return (l);
01164 }
01171 void
01172 sinfo_new_destroy_lookup( new_Lookup *l )
01173 {
01174     if ( l )
01175     cpl_free(l);
01176 }
01184 int
01185 sinfo_new_change_mask (cpl_image * mask, cpl_image * im)
01186 {
01187     int i ;
01188     int mlx=0;
01189     int mly=0;
01190     int ilx=0;
01191     int ily=0;
01192     float* pidata=NULL;
01193     float* pmdata=NULL;
01194 
01195     if (mask == NULL || im == NULL) return -1 ;
01196     ilx=cpl_image_get_size_x(im);
01197     ily=cpl_image_get_size_y(im);
01198     pidata=cpl_image_get_data_float(im);
01199 
01200     mlx=cpl_image_get_size_x(mask);
01201     mly=cpl_image_get_size_y(mask);
01202     pmdata=cpl_image_get_data_float(mask);
01203 
01204     for ( i = 0 ; i < (int) ilx*ily ; i++ )
01205     {
01206         if (isnan(pidata[i]))
01207         {
01208             pmdata[i] = 0. ;
01209         }
01210     }
01211     return 0 ;
01212 }
01213 
01214 
01231 cpl_image *
01232 sinfo_new_c_bezier_find_cosmic( cpl_image *im,
01233                                       cpl_image *mask,
01234                                       new_Lookup *look,
01235                                       short rx,
01236                                       short ry,
01237                                       short rz,
01238                           short lowerI,
01239                                       short highI,
01240                                       short lowerJ,
01241                                       short highJ,
01242                                       float factor )
01243 {
01244 
01245     int i,j,count;
01246     cpl_imagelist * sc_im,* drs_sc_mask;
01247     short szx,szy,szz;
01248     float /*ant,*/newValue,old/*,dif,porcentage,distance*/;
01249     double med, stdev;
01250     /*cpl_image *out;*/
01251     short rx_loop, ry_loop, rz_loop;
01252 
01253 
01254     cpl_image* o_img=NULL;
01255     float* pmdata=NULL;
01256     float* pidata=NULL;
01257 
01258 
01259 
01260     int ilx=0;
01261     int ily=0;
01262 
01263     int mlx=0;
01264     int mly=0;
01265 
01266 
01267 
01268 
01269     mlx=cpl_image_get_size_x(mask);
01270     mly=cpl_image_get_size_y(mask);
01271     pmdata=cpl_image_get_data_float(mask);
01272 
01273     ilx=cpl_image_get_size_x(im);
01274     ily=cpl_image_get_size_y(im);
01275     pidata=cpl_image_get_data_float(im);
01276 
01277     if ( mlx != ilx || mly != ily )
01278     {
01279         sinfo_msg_error(" data & mask images not compatible in size\n") ;
01280         return NULL ;
01281     }
01282 
01283     /* allocate memory for sub cubes*/
01284     szx = (rx * 2 ) + 1;
01285     szy = (ry * 2 ) + 1;
01286     szz = (rz * 2 ) + 1;
01287 
01288     if ( NULL == ( sc_im = cpl_imagelist_new() ) )
01289     {
01290         sinfo_msg_error(" could not allocate memory for data subcube\n") ;
01291         return NULL ;
01292     }
01293     for(i=0;i<szz;i++){
01294       o_img=cpl_image_new(szx,szy,CPL_TYPE_FLOAT);
01295       cpl_imagelist_set(sc_im,o_img,i);
01296     }
01297 
01298     if ( NULL == ( drs_sc_mask = cpl_imagelist_new() ) )
01299     {
01300         sinfo_msg_error(" could not allocate memory for mask subcube\n") ;
01301         return NULL ;
01302     }
01303 
01304     for(i=0;i<szz;i++){
01305       o_img=cpl_image_new(szx,szy,CPL_TYPE_FLOAT);
01306       cpl_imagelist_set(drs_sc_mask,o_img,i);
01307     }
01308 
01309 
01310 
01311 
01312     count=0;
01313     for ( i = 0 ; i < mlx; i++ )
01314     {
01315         for ( j = 0 ; j < mly ; j++ )
01316         {
01317         if ( i >= lowerI && i < highI && j >= lowerJ && j < highJ )
01318         {
01319 
01320         rx_loop = 1 ; ry_loop = 1 ; rz_loop = 1 ;
01321         newValue = sinfo_new_c_bezier_correct_cosmic( i, j, im,
01322                                                               mask, sc_im,
01323                                                               drs_sc_mask,
01324                                                               look,
01325                                                               rx_loop,
01326                                                               ry_loop,
01327                                                               rz_loop,
01328                                                               &med,
01329                                                               &stdev,
01330                                                               factor );
01331             /* if no valid neighboors are found, increase size of
01332                    sub cube until max radius is reached */
01333         while ( newValue == cubeNONEIGHBOR && rx_loop < rx &&
01334                         ry_loop < ry && rz_loop < rz )
01335             {
01336               rx_loop++ ; ry_loop++; rz_loop++;
01337               /*sinfo_msg_debug("Increasing radius to %d, in %d %d",
01338                           rx_loop, i, j) ;  */
01339                    newValue = sinfo_new_c_bezier_correct_cosmic( i, j, im,
01340                                                                     mask,
01341                                                                     sc_im,
01342                                                                     drs_sc_mask,
01343                                                                     look,
01344                                                                     rx_loop,
01345                                                                     ry_loop,
01346                                                                     rz_loop,
01347                                                                     &med,
01348                                                                     &stdev,
01349                                                                      factor );
01350             }
01351         /*give up on increasing the size*/
01352         if ( isnan(newValue) || newValue == cubeNONEIGHBOR )
01353                 /*<= -3.e10 ZERO )*/
01354             continue;
01355 
01356         old = pidata[sinfo_im_xy(im,i,j)];
01357         if ( newValue != old )
01358             {
01359             pidata[sinfo_im_xy(im,i,j)] = newValue;
01360             /*sinfo_msg_debug("[%d,%d]=%f -> %f, med= %f, stdev=%f",
01361                       i,j, old, newValue, med, stdev ); */
01362             count++;
01363             }
01364         }
01365         }
01366     }
01367 
01368 
01369     sinfo_msg_debug("bad pixels count: %d",count);
01370 
01371 
01372     cpl_imagelist_delete(sc_im);
01373     cpl_imagelist_delete(drs_sc_mask);
01374     return im;
01375 }
01376 
01377 
01395 float
01396 sinfo_new_c_bezier_correct_cosmic(int ipos,
01397                                   int jpos,
01398                                   cpl_image * im,
01399                                   cpl_image * mask,
01400                       cpl_imagelist * sc_im,
01401                                   cpl_imagelist * drs_sc_mask,
01402                                   new_Lookup * look,
01403                                   short rx,
01404                                   short ry,
01405                                   short rz ,
01406                                   double *med ,
01407                       double *stdev,
01408                                   float factor )
01409 {
01410     short ic, jc, kc, ii, jj, kk/*, sjj, skk*/,is,js,ks;
01411     short i, j, k, indexJ, indexI, lx, ly, lz, szx, szy, szz;
01412     /*float indexIf,indexJf,sp;*/
01413     cpl_image * X, * Y, * Z, * hX;
01414     cpl_imagelist  * id, * jd;
01415     short counter;
01416     double sum;
01417     float* phXdata=NULL;
01418     float* pXdata=NULL;
01419     float* pYdata=NULL;
01420     float* pZdata=NULL;
01421 
01422     float* pimdata=NULL;
01423     float* pscdata=NULL;
01424     float* pdrsdata=NULL;
01425     float* piddata=NULL;
01426     float* pjddata=NULL;
01427     float* pmaskdata=NULL;
01428 
01429 
01430     int idlx=0;
01431     int idly=0;
01432     int idnp=0;
01433 
01434     int drslx=0;
01435     int drsly=0;
01436     int drsnp=0;
01437 
01438 
01439     X  = look -> X;
01440     Y  = look -> Y;
01441     Z  = look -> Z;
01442     hX = look -> hX;
01443     id = look -> id;
01444     jd = look -> jd;
01445 
01446     phXdata=cpl_image_get_data_float(hX);
01447     /*if ( phXdata[sinfo_im_xy( hX, ipos, jpos)] > 1 )
01448     {
01449     sinfo_msg_error("double hit in position [%d,%d]=%f, can not correct",
01450         ipos,jpos,phXdata[sinfo_im_xy(hX,ipos,jpos)]) ;
01451     return ( -2e10 );
01452     }*/
01453     if ( phXdata[sinfo_im_xy( hX, ipos, jpos)] < 1 )
01454     {
01455     /*sinfo_msg_error("no lookup  in position [%d,%d]=%f, can not correct",
01456       ipos,jpos,phXdata[sinfo_im_xy(hX,ipos,jpos)]) ;*/
01457     return ( ZERO );
01458     }
01459 
01460     pXdata=cpl_image_get_data_float(X);
01461     pYdata=cpl_image_get_data_float(Y);
01462     pZdata=cpl_image_get_data_float(Z);
01463 
01464     ic = pXdata[sinfo_im_xy( X, ipos, jpos)];
01465     jc = pYdata[sinfo_im_xy( Y, ipos, jpos)];
01466     kc = pZdata[sinfo_im_xy( Z, ipos, jpos)];
01467     /*if ( !(ipos % 16 )  )*/
01468 #ifdef DEBUG
01469     sinfo_msg_debug("Correcting bad pixel : ipos=%d,jpos=%d, "
01470                     "in Cube -> ic=%d, jc=%d, kc=%d", ipos,jpos, ic, jc, kc );
01471 #endif
01472     /*limit to start not before the beginning of the cube*/
01473     ii = ic - rx; if ( ii < 0 ) ii = 0;
01474     jj = jc - ry; if ( jj < 0 ) jj = 0;
01475     kk = kc - rz; if ( kk < 0 ) kk = 0;
01476 
01477 #ifdef DEBUG
01478     sinfo_msg_debug("Start Point in Cube -> ii=%d,jj=%d,kk=%d", ii, jj, kk );
01479 #endif
01480 
01481     /*limit to end not outside of the cube */
01482     szx = (rx * 2 ) + 1;
01483     szy = (ry * 2 ) + 1;
01484     szz = (rz * 2 ) + 1;
01485 
01486     idlx = cpl_image_get_size_x(cpl_imagelist_get(id,0));
01487     idly = cpl_image_get_size_y(cpl_imagelist_get(id,0));
01488     idnp = cpl_imagelist_get_size(id);
01489 
01490     lx = idlx;
01491     ly = idly;
01492     lz = idnp;
01493     if ( ( ic + rx ) >= idlx )
01494     szx = szx - ( (ic+rx)-(lx-1) );
01495 
01496     if ( ( jc + ry ) >= idly )
01497     szy = szy - ( (jc+ry)-(ly-1) );
01498 
01499     if ( ( kc + rz ) >= idnp )
01500     szz = szz - ( (kc+rz)-(lz-1) );
01501 
01502 #ifdef DEBUG
01503     sinfo_msg_error("Size of subcube : szx=%d,szy=%d,szz=%d\n", szx, szy, szz );
01504     /*fill whole mask with not available*/
01505     drsnp=cpl_imagelist_get_size(drs_sc_mask);
01506     drslx=cpl_image_get_size_x(cpl_imagelist_get(drs_sc_mask,0));
01507     drsly=cpl_image_get_size_y(cpl_imagelist_get(drs_sc_mask,0));
01508 
01509     sinfo_msg_error("Fill Mask subcube of size: %d,%d,%d, with NOINFO",
01510                      drslx, drsly,  drsnp);
01511 #endif
01512     for( i = 0; i < drslx; i++) {
01513       for( j = 0; j < drsly; j++) {
01514     for( k = 0; k < drsnp; k++) {
01515       pdrsdata=cpl_image_get_data_float(cpl_imagelist_get(drs_sc_mask,k));
01516       pdrsdata[sinfo_cu_xy(drs_sc_mask,i,j)] = cubePT_NOINFO;
01517     }
01518       }
01519     }
01520     pimdata=cpl_image_get_data_float(im);
01521     pmaskdata=cpl_image_get_data_float(mask);
01522     for( i = ii,is=0;  i < ii+szx; i++,is++)
01523       {
01524     for( j = jj,js=0;  j < jj+szy; j++,js++)
01525       {
01526         for( k = kk,ks=0;  k < kk+szz; k++,ks++)
01527           {
01528 #ifdef DEBUG
01529         sinfo_msg_debug("i=%d j=%d k=%d is=%d ij=%d ik=%d",
01530                 i,j,k,is,js,ks);
01531 #endif
01532         piddata=cpl_image_get_data_float(cpl_imagelist_get(id,k));
01533         pjddata=cpl_image_get_data_float(cpl_imagelist_get(id,k));
01534           pdrsdata=cpl_image_get_data_float(cpl_imagelist_get(drs_sc_mask,ks));
01535                 pscdata=cpl_image_get_data_float(cpl_imagelist_get(sc_im,ks));
01536 
01537 
01538         indexI = sinfo_new_nint( piddata[sinfo_cu_xy(id,i,j)] );
01539         indexJ = sinfo_new_nint( pjddata[sinfo_cu_xy(jd,i,j)] );
01540         if ( indexJ <= -1 || indexJ>=2048 || indexI == -1 )
01541           {
01542             pdrsdata[sinfo_cu_xy(drs_sc_mask,is,js)] = cubePT_NOINFO;
01543             continue;
01544           }
01545         pscdata[sinfo_cu_xy(sc_im,is,js)]=
01546                          pimdata[sinfo_im_xy(im,indexI,indexJ)];
01547         pdrsdata[sinfo_cu_xy(drs_sc_mask,is,js)]=
01548                          pmaskdata[sinfo_im_xy(mask,indexI,indexJ)];
01549 #ifdef DEBUG
01550         sinfo_msg_debug("Cube i=%d, j=%d, k=%d  ; "
01551                                 " Sub is=%d, js=%d, ks=%d  ; "
01552                                 " Plane I=%d,J=%d ; mask %f ; im %f\n",
01553                         i, j, k, is, js, ks, indexI, indexJ,
01554                                 pmaskdata[sinfo_im_xy(mask,indexI,indexJ)],
01555                                 pimdata[sinfo_im_xy(im,indexI,indexJ)]);
01556 #endif
01557 
01558           }
01559       }
01560       }
01561 
01562     /* ignoring the elements in the slitlet of the tested pixel */
01563 
01564     for( i = 0; i < szx; i++) {
01565       for( k = 0;  k < szz; k++) {
01566     pdrsdata=cpl_image_get_data_float(cpl_imagelist_get(drs_sc_mask,k));
01567     pdrsdata[sinfo_cu_xy(drs_sc_mask,i,ry)] = cubePT_NOINFO;
01568       }
01569     }
01570 /* now calculate mean and stdev in subcube */
01571 
01572     counter = 0;
01573     sum=0;
01574     for( i = 0; i < szx; i++)
01575       {
01576     for( j = 0;  j < szy; j++)
01577       {
01578         for( k = 0;  k < szz; k++)
01579           {
01580         pdrsdata=cpl_image_get_data_float(cpl_imagelist_get(drs_sc_mask,k));
01581         pscdata=cpl_image_get_data_float(cpl_imagelist_get(sc_im,k));
01582         if (pscdata[sinfo_cu_xy(sc_im ,i ,j)] != ZERO &&
01583                     pdrsdata[sinfo_cu_xy(drs_sc_mask,i ,j)] != cubePT_NOINFO)
01584           {
01585             sum = sum + pscdata[sinfo_cu_xy(sc_im ,i ,j)];
01586             counter++;
01587           }
01588           }
01589       }
01590       }
01591 
01592     *med = sum / counter ;
01593 
01594     counter = 0;
01595     sum=0;
01596     for( i = 0; i < szx; i++)
01597       {
01598     for( j = 0;  j < szy; j++)
01599       {
01600         for( k = 0;  k < szz; k++)
01601           {
01602         pscdata=cpl_image_get_data_float(cpl_imagelist_get(sc_im,k));
01603         pdrsdata=cpl_image_get_data_float(cpl_imagelist_get(drs_sc_mask,k));
01604         if (pscdata[sinfo_cu_xy(sc_im ,i ,j)] != ZERO &&
01605                     pdrsdata[sinfo_cu_xy(drs_sc_mask,i ,j)] != cubePT_NOINFO)
01606           {
01607                sum = sum + (pscdata[sinfo_cu_xy(sc_im ,i ,j)] - *med) *
01608                                    (pscdata[sinfo_cu_xy(sc_im ,i ,j)] - *med) ;
01609                counter++;
01610           }
01611           }
01612       }
01613       }
01614 
01615     *stdev = sqrt( sum / ( counter - 1 ) );
01616 
01617 
01618     if ( (fabs( pimdata[sinfo_im_xy(im,ipos,jpos)] - *med ) >
01619           factor * *stdev) ||
01620           isnan(pimdata[sinfo_im_xy(im,ipos,jpos)]) )
01621     {
01622     pdrsdata=cpl_image_get_data_float(cpl_imagelist_get(drs_sc_mask,rz));
01623     pdrsdata[sinfo_cu_xy(drs_sc_mask,rx,ry)] = cubePT_FIND;
01624     return ( sinfo_new_c_bezier_interpol( sc_im, drs_sc_mask ) );
01625     }
01626     else
01627         return(pimdata[sinfo_im_xy(im,ipos,jpos)]);
01628 
01629 
01630 }
01631 

Generated on 3 Mar 2013 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1