hawki_bkg.c

00001 /* $Id: hawki_bkg.c,v 1.15 2009/10/23 15:13:06 cgarcia Exp $
00002  *
00003  * This file is part of the HAWKI Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: cgarcia $
00023  * $Date: 2009/10/23 15:13:06 $
00024  * $Revision: 1.15 $
00025  * $Name: hawki-1_8_0 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                    Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <string.h>
00037 #include <cpl.h>
00038 
00039 #include "hawki_distortion.h"
00040 #include "hawki_bkg.h"
00041 #include "hawki_pfits.h"
00042 #include "hawki_load.h"
00043 #include "hawki_utils.h"
00044 
00045 /*----------------------------------------------------------------------------*/
00049 /*----------------------------------------------------------------------------*/
00050 
00053 /*----------------------------------------------------------------------------*/
00067 /*----------------------------------------------------------------------------*/
00068 int hawki_bkg_fill_assoc(cpl_frameset * objframes, cpl_propertylist * proplist)
00069 {
00070     int   iframe;
00071     int   nframes;
00072     
00073     /* Loop on the frames */
00074     nframes = cpl_frameset_get_size(objframes);
00075     for(iframe = 0 ; iframe < nframes ; ++iframe)
00076     {
00077         cpl_frame         * this_frame;
00078         cpl_propertylist  * this_proplist;
00079         const char        * arcfile;
00080         char                keystr[256];
00081 
00082         /* Getting the proplist for this frame */
00083         this_frame     = cpl_frameset_get_frame(objframes, iframe);
00084         this_proplist  = cpl_propertylist_load
00085             (cpl_frame_get_filename(this_frame),0);
00086         arcfile        = hawki_pfits_get_arcfile(proplist);
00087         snprintf(keystr, 256, "ESO QC BKG ASSOC RAW%d",iframe+1);
00088         cpl_propertylist_append_string(proplist, keystr, arcfile);
00089         cpl_propertylist_delete(this_proplist);
00090     }
00091     
00092     return 0;
00093 }
00094 
00095 /*----------------------------------------------------------------------------*/
00107 /*----------------------------------------------------------------------------*/
00108 int hawki_bkg_from_objects_median
00109 (const cpl_frameset  *  objframes, cpl_imagelist * bkg)
00110 {
00111     int             idet;
00112 
00113     /* Error state variables */
00114     cpl_errorstate  prestate = cpl_errorstate_get();
00115     
00116     /* Loop on detectors */
00117     for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00118     {
00119         cpl_imagelist * img_serie;
00120         cpl_image     * this_bkg_image;
00121         
00122         /* Loading the object frame */
00123         img_serie = hawki_load_detector(objframes, idet + 1, CPL_TYPE_FLOAT);
00124         if(img_serie== NULL)
00125         {
00126             cpl_msg_error(__func__, "Error reading object image") ;
00127             return -1;
00128         }
00129         
00130         /* Averaging */
00131         /* TODO: Comprobar que cpl_imagelist_collapse_median_create
00132          * admite las mascaras, para que cuando implemente lo de la mascara 
00133          * de objetos siga funcionando */
00134         if ((this_bkg_image = cpl_imagelist_collapse_median_create(img_serie))
00135                 == NULL)
00136         {
00137             cpl_msg_error(__func__, "Cannot compute the median of obj images");
00138             cpl_imagelist_delete(img_serie);
00139             return -1;
00140         }
00141 
00142         /* Fill the corresponding chip */
00143         cpl_imagelist_set(bkg, this_bkg_image, idet);
00144         
00145         /* Freeing */
00146         cpl_imagelist_delete(img_serie);
00147     }
00148 
00149     /* Check error status and exit */
00150     if(!cpl_errorstate_is_equal(prestate))
00151         return -1;
00152     return 0;
00153 }
00154 
00155 /*----------------------------------------------------------------------------*/
00168 /*----------------------------------------------------------------------------*/
00169 int hawki_bkg_from_sky_median
00170 (const cpl_frameset  *  skyframes, 
00171  cpl_imagelist       *  bkg)
00172 {
00173     int             idet;
00174 
00175     /* Error state variables */
00176     cpl_errorstate  prestate = cpl_errorstate_get();
00177     
00178     /* Loop on detectors */
00179     for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00180     {
00181         cpl_imagelist * img_serie;
00182         cpl_image     * this_bkg_image;
00183         
00184         /* Loading the object frame */
00185         img_serie = hawki_load_detector(skyframes, idet + 1, CPL_TYPE_FLOAT);
00186         if(img_serie== NULL)
00187         {
00188             cpl_msg_error(__func__, "Error reading object image") ;
00189             return -1;
00190         }
00191         
00192         /* Averaging */
00193         if ((this_bkg_image = cpl_imagelist_collapse_median_create(img_serie))
00194                 == NULL) 
00195         {
00196             cpl_msg_error(__func__, "Cannot compute the median of obj images");
00197             cpl_imagelist_delete(img_serie);
00198             return -1;
00199         }
00200         /* Fill the corresponding chip */
00201         cpl_imagelist_set(bkg, this_bkg_image, idet);
00202         
00203         /* Freeing */
00204         cpl_imagelist_delete(img_serie);
00205     }
00206 
00207     /* Check error status and exit */
00208     if(!cpl_errorstate_is_equal(prestate))
00209         return -1;
00210     return 0;
00211 }
00212 
00213 /*----------------------------------------------------------------------------*/
00229 /*----------------------------------------------------------------------------*/
00230 int hawki_bkg_from_running_mean_detector
00231 (cpl_imagelist       * objimages,
00232  const cpl_vector    * medians,
00233  int                   i_target,
00234  int                   half_width,
00235  int                   rejlow,
00236  int                   rejhigh,
00237  cpl_image          *  bkg)
00238 {
00239     float        *  bkg_p;
00240     float        *  curima_p;
00241     cpl_binary   *  curmask_p;
00242     float        ** objimages_p;
00243     cpl_binary   ** maskimages_p;
00244     const double *  medians_p;
00245     double          out;
00246     int             from_ima;
00247     int             to_ima;
00248     int             nima;
00249     int             nuse;
00250     int             nx;
00251     int             ny;
00252     
00253     int             pos_x;
00254     int             pos_y;
00255     int             pos;
00256     int             i_ima;
00257     int             i_win;
00258     int             n_win;
00259     
00260     cpl_vector * localwin;
00261 
00262     /* Get the pointer to the background image */
00263     bkg_p = cpl_image_get_data_float(bkg);
00264     
00265     /* Compute border indices */
00266     nima = cpl_imagelist_get_size(objimages);
00267     from_ima = i_target - half_width;
00268     to_ima   = i_target + half_width;
00269     if (from_ima<0) from_ima = 0 ;
00270     if (to_ima>(nima-1)) to_ima=nima-1 ;
00271 
00272     /* Number of valid planes to consider after edge effects */
00273     nuse = to_ima - from_ima ;
00274 
00275     /* Get the size of images */
00276     nx = cpl_image_get_size_x(bkg);
00277     ny = cpl_image_get_size_y(bkg);
00278     
00279     /* Get the pointers for better performance */
00280     medians_p = cpl_vector_get_data_const(medians);
00281 
00282     /* Allocate local window with the maximum number of possible used values*/
00283     localwin = cpl_vector_new(nuse) ;
00284     
00285     /* Get fast accesors to input image pointers */
00286     objimages_p  = cpl_malloc(nima * sizeof(float *));
00287     maskimages_p = cpl_malloc(nima * sizeof(cpl_binary *));
00288     for(i_ima=from_ima ; i_ima<=to_ima ; i_ima++)
00289     {
00290         objimages_p[i_ima]  = cpl_image_get_data_float
00291             (cpl_imagelist_get(objimages, i_ima));
00292         maskimages_p[i_ima] = cpl_mask_get_data
00293             (cpl_image_get_bpm(cpl_imagelist_get(objimages, i_ima)));
00294     }
00295     
00296     /* Initialize the bad pixel mask */
00297     cpl_image_accept_all(bkg);
00298 
00299     /* Loop over all pixels */
00300     for (pos_x=0 ; pos_x<nx ; pos_x++)
00301     {
00302         for (pos_y=0 ; pos_y<ny ; pos_y++)
00303         {
00304             /* Get the position in the array */
00305             pos = pos_x + pos_y * nx;
00306             
00307             /* Resize again to maximum number of elements */
00308             cpl_vector_set_size(localwin, nuse);
00309         
00310             /* Fill up local window */
00311             i_win=0;
00312             for (i_ima=from_ima ; i_ima<=to_ima ; i_ima++) 
00313             {
00314                 if (i_ima != i_target) 
00315                 {
00316                     curima_p = objimages_p[i_ima];
00317                     curmask_p = maskimages_p[i_ima];
00318                     if(!curmask_p[pos])
00319                     {
00320                         cpl_vector_set(localwin, i_win,
00321                                        (double)curima_p[pos]-medians_p[i_ima]);
00322                         i_win++;
00323                     }
00324                 }
00325             }
00326             n_win = i_win;
00327             if(n_win - rejlow - rejhigh < 1)
00328             {
00329                 /* Add it to the bad pixel mask */
00330                 cpl_msg_debug(__func__,"Pixel %d %d added to the sky bpm",
00331                         pos_x, pos_y);
00332                 if(cpl_image_reject(bkg, pos_x + 1, pos_y + 1) != CPL_ERROR_NONE)
00333                 {
00334                     cpl_msg_error(__func__,"Cannot add pixel to sky bpm");
00335                     cpl_vector_delete(localwin) ;
00336                     cpl_free(objimages_p);
00337                     cpl_free(maskimages_p);
00338                     return(-1);
00339                 }
00340             }
00341             else
00342             {
00343                 /* Resize to current number of elements */
00344                 cpl_vector_set_size(localwin, n_win);
00345                 /* Sort window */
00346                 cpl_vector_sort(localwin, 1);
00347                 /* Reject min and max, accumulate other pixels */
00348                 out = 0.0 ;
00349                 for (i_win=rejlow ; i_win<n_win - rejhigh; i_win++) 
00350                 {
00351                     out += cpl_vector_get(localwin, i_win);
00352                 }
00353                 /* Take the mean */
00354                 out /= (double)(n_win - rejlow - rejhigh);
00355         
00356                 /* Assign value */
00357                 bkg_p[pos] = (out+medians_p[i_target]);
00358             }
00359         }
00360     }
00361 
00362     /* Free */
00363     cpl_vector_delete(localwin) ;
00364     cpl_free(objimages_p);
00365     cpl_free(maskimages_p);
00366 
00367     return 0;
00368 }
00369 

Generated on Thu Feb 17 17:13:07 2011 for HAWKI Pipeline Reference Manual by  doxygen 1.4.7