hawki_calib.c

00001 /* $Id: hawki_calib.c,v 1.5 2009/04/30 10:40:21 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/04/30 10:40:21 $
00024  * $Revision: 1.5 $
00025  * $Name: hawki-1_7_2 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                    Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <float.h>
00037 #include <string.h>
00038 #include <math.h>
00039 #include <cpl.h>
00040 
00041 #include "hawki_utils.h"
00042 #include "hawki_calib.h"
00043 #include "hawki_pfits.h"
00044 #include "hawki_load.h"
00045 
00046 /*----------------------------------------------------------------------------*/
00050 /*----------------------------------------------------------------------------*/
00051 
00054 /*----------------------------------------------------------------------------*/
00068 /*----------------------------------------------------------------------------*/
00069 int hawki_flat_dark_bpm_imglist_calib(
00070         cpl_imagelist       *   ilist,
00071         cpl_imagelist       *   flat,
00072         cpl_imagelist       *   dark,
00073         cpl_imagelist       *   bpm)
00074 {
00075     int                 idet;
00076 
00077     /* Test entries */
00078     if (ilist == NULL) return -1 ;
00079 
00080     /* Dark correction */
00081     if (dark != NULL) 
00082     {
00083         cpl_msg_info(cpl_func, "Subtracting the dark to each chip image") ;
00084         /* Apply the dark correction to the images */
00085         for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00086         {
00087             if (cpl_image_subtract(cpl_imagelist_get(ilist, idet),
00088                                    cpl_imagelist_get(dark, idet))!=CPL_ERROR_NONE) 
00089             {
00090                 cpl_msg_error(cpl_func,"Cannot apply the dark to chip %d",
00091                               idet+1);
00092                 return -1 ;
00093             }
00094         }
00095     }
00096 
00097     /* Flat-field correction */
00098     if (flat != NULL) 
00099     {
00100         cpl_msg_info(cpl_func, "Dividing the flat to each chip image") ;
00101         /* Apply the flat correction to the images */
00102         for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00103         {
00104             if (cpl_image_divide(cpl_imagelist_get(ilist, idet),
00105                                  cpl_imagelist_get(flat, idet))!=CPL_ERROR_NONE) 
00106             {
00107                 cpl_msg_error(__func__,"Cannot apply the flatfield to chip %d",
00108                               idet+1);
00109                 return -1 ;
00110             }
00111         }
00112     }
00113 
00114     /* Correct the bad pixels if requested */
00115     if (bpm != NULL) 
00116     {
00117         cpl_msg_info(cpl_func, "Correct the bad pixels to each chip image");
00118         /* Apply the flat correction to the images */
00119         for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00120         {
00121             cpl_mask    * bpm_im_bin ;
00122             /* Convert the map from integer to binary */
00123             bpm_im_bin = cpl_mask_threshold_image_create
00124                 (cpl_imagelist_get(bpm, idet), -0.5, 0.5) ;
00125             cpl_mask_not(bpm_im_bin) ;
00126             cpl_image_reject_from_mask(cpl_imagelist_get(ilist, idet), bpm_im_bin);
00127             if (cpl_detector_interpolate_rejected
00128                     (cpl_imagelist_get(ilist, idet)) != CPL_ERROR_NONE) 
00129             {
00130                     cpl_msg_error
00131                         (cpl_func, "Cannot clean the bad pixels in chip %d",
00132                          idet+1);
00133                     cpl_mask_delete(bpm_im_bin) ;
00134                     return -1 ;
00135             }
00136             cpl_mask_delete(bpm_im_bin) ;
00137         }
00138     }
00139 
00140     /* Return */
00141     return 0 ;
00142 }
00143 
00144 /*----------------------------------------------------------------------------*/
00157 /*----------------------------------------------------------------------------*/
00158 int hawki_flat_bpm_imglist_calib(
00159         cpl_imagelist       *   ilist,
00160         cpl_imagelist       *   flat,
00161         cpl_imagelist       *   bpm)
00162 {
00163     int                 idet;
00164 
00165     /* Test entries */
00166     if (ilist == NULL) return -1 ;
00167 
00168     /* Flat-field correction */
00169     if (flat != NULL) 
00170     {
00171         cpl_msg_info(cpl_func, "Divide the images by the flatfield") ;
00172         /* Apply the flat correction to the images */
00173         for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00174         {
00175             if (cpl_image_divide(cpl_imagelist_get(ilist, idet),
00176                                  cpl_imagelist_get(flat, idet))!=CPL_ERROR_NONE) 
00177             {
00178                 cpl_msg_error(cpl_func,"Cannot apply the flatfield to the images");
00179                 return -1 ;
00180             }
00181         }
00182     }
00183 
00184     /* Correct the bad pixels if requested */
00185     if (bpm != NULL) 
00186     {
00187         cpl_msg_info(cpl_func, "Correct the bad pixels in the images") ;
00188         /* Apply the flat correction to the images */
00189         for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00190         {
00191             cpl_mask        *   bpm_im_bin ;
00192             /* Convert the map from integer to binary */
00193             bpm_im_bin = cpl_mask_threshold_image_create
00194                 (cpl_imagelist_get(bpm, idet), -0.5, 0.5) ;
00195             cpl_mask_not(bpm_im_bin) ;
00196             cpl_image_reject_from_mask(cpl_imagelist_get(ilist, idet), bpm_im_bin);
00197             if (cpl_detector_interpolate_rejected
00198                     (cpl_imagelist_get(ilist, idet)) != CPL_ERROR_NONE) 
00199             {
00200                     cpl_msg_error
00201                         (cpl_func, "Cannot clean the bad pixels in detector %d",
00202                          idet+1);
00203                     cpl_mask_delete(bpm_im_bin) ;
00204                     return -1 ;
00205             }
00206             cpl_mask_delete(bpm_im_bin) ;
00207         }
00208     }
00209 
00210     /* Return */
00211     return 0 ;
00212 }
00213 
00214 /*----------------------------------------------------------------------------*/
00221 /*----------------------------------------------------------------------------*/
00222 int hawki_bkg_imglist_calib
00223 (cpl_imagelist *       ilist,
00224  cpl_imagelist *       bkg)
00225 {
00226     int                 idet;
00227 
00228     /* Test entries */
00229     if (ilist == NULL) return -1 ;
00230 
00231     /* Background correction */
00232     if (bkg != NULL) 
00233     {
00234         cpl_msg_info(cpl_func, "Subtract the images by the bkg") ;
00235         /* Apply the bkg correction to the images */
00236         for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00237         {
00238             if (cpl_image_subtract(cpl_imagelist_get(ilist, idet),
00239                                    cpl_imagelist_get(bkg, idet))!=CPL_ERROR_NONE) 
00240             {
00241                 cpl_msg_error(cpl_func,"Cannot apply the bkg to the images");
00242                 return -1 ;
00243             }
00244         }
00245     }
00246 
00247     /* Return */
00248     return 0 ;
00249 
00250 }
00251 
00252 /*----------------------------------------------------------------------------*/
00270 /*----------------------------------------------------------------------------*/
00271 int hawki_flat_dark_bpm_detector_calib(
00272         cpl_imagelist       *   ilist,
00273         cpl_image           *   flat,
00274         cpl_image           *   dark,
00275         cpl_image           *   bpm)
00276 {
00277     cpl_mask        *   bpm_im_bin ;
00278     int                 i ;
00279 
00280     /* Test entries */
00281     if (ilist == NULL) return -1 ;
00282 
00283     /* Dark correction */
00284     if (dark != NULL) 
00285     {
00286         cpl_msg_info(cpl_func, "Subtract the images by the dark") ;
00287         if (cpl_imagelist_subtract_image(ilist, dark)!=CPL_ERROR_NONE) 
00288         {
00289             cpl_msg_error(cpl_func,"Cannot apply the dark to the images");
00290             return -1 ;
00291         }
00292     }
00293 
00294     /* Flat-field correction */
00295     if (flat != NULL) {
00296         cpl_msg_info(cpl_func, "Divide the images by the flatfield") ;
00297 
00298         /* Apply the flatfield correction to the images */
00299         if (cpl_imagelist_divide_image(ilist, flat)!=CPL_ERROR_NONE) {
00300             cpl_msg_error(cpl_func,"Cannot apply the flatfield to the images");
00301             return -1 ;
00302         }
00303     }
00304 
00305     /* Correct the bad pixels if requested */
00306     if (bpm != NULL) {
00307         cpl_msg_info(cpl_func, "Correct the bad pixels in the images") ;
00308 
00309         /* Convert the map from integer to binary */
00310         bpm_im_bin = cpl_mask_threshold_image_create(bpm, -0.5, 0.5) ;
00311         cpl_mask_not(bpm_im_bin) ;
00312         /* Apply the bad pixels cleaning */
00313         for (i=0 ; i<cpl_imagelist_get_size(ilist) ; i++) {
00314             cpl_image_reject_from_mask(cpl_imagelist_get(ilist, i), bpm_im_bin);
00315             if (cpl_detector_interpolate_rejected(
00316                         cpl_imagelist_get(ilist, i)) != CPL_ERROR_NONE) {
00317                 cpl_msg_error(cpl_func, "Cannot clean the bad pixels in obj %d",
00318                         i+1);
00319                 cpl_mask_delete(bpm_im_bin) ;
00320                 return -1 ;
00321             }
00322         }
00323         cpl_mask_delete(bpm_im_bin) ;
00324     }
00325 
00326     /* Return */
00327     return 0 ;
00328 }
00329 
00330 /*----------------------------------------------------------------------------*/
00345 /*----------------------------------------------------------------------------*/
00346 int hawki_flat_bpm_detector_calib(
00347         cpl_imagelist       *   ilist,
00348         cpl_image           *   flat,
00349         cpl_image           *   bpm)
00350 {
00351     cpl_mask        *   bpm_im_bin ;
00352     int                 i ;
00353 
00354     /* Test entries */
00355     if (ilist == NULL) return -1 ;
00356 
00357     /* Flat-field correction */
00358     if (flat != NULL) {
00359         cpl_msg_info(cpl_func, "Divide the images by the flatfield") ;
00360 
00361         /* Apply the flatfield correction to the images */
00362         if (cpl_imagelist_divide_image(ilist, flat)!=CPL_ERROR_NONE) {
00363             cpl_msg_error(cpl_func,"Cannot apply the flatfield to the images");
00364             return -1 ;
00365         }
00366     }
00367 
00368     /* Correct the bad pixels if requested */
00369     if (bpm != NULL) {
00370         cpl_msg_info(cpl_func, "Correct the bad pixels in the images") ;
00371 
00372         /* Convert the map from integer to binary */
00373         bpm_im_bin = cpl_mask_threshold_image_create(bpm, -0.5, 0.5) ;
00374         cpl_mask_not(bpm_im_bin) ;
00375         /* Apply the bad pixels cleaning */
00376         for (i=0 ; i<cpl_imagelist_get_size(ilist) ; i++) {
00377             cpl_image_reject_from_mask(cpl_imagelist_get(ilist, i), bpm_im_bin);
00378             if (cpl_detector_interpolate_rejected(
00379                         cpl_imagelist_get(ilist, i)) != CPL_ERROR_NONE) {
00380                 cpl_msg_error(cpl_func, "Cannot clean the bad pixels in obj %d",
00381                         i+1);
00382                 cpl_mask_delete(bpm_im_bin) ;
00383                 return -1 ;
00384             }
00385         }
00386         cpl_mask_delete(bpm_im_bin) ;
00387     }
00388 
00389     /* Return */
00390     return 0 ;
00391 }
00392 
00393 /*----------------------------------------------------------------------------*/
00402 /*----------------------------------------------------------------------------*/
00403 int hawki_bpm_calib(
00404         cpl_image           *   ima,
00405         const char          *   bpm,
00406         int                     idet)
00407 {
00408     cpl_mask        *   bpm_im_bin ;
00409     cpl_image       *   bpm_im_int ;
00410     int                 ext_nb ;
00411 
00412     /* Test entries */
00413     if (ima == NULL) return -1 ;
00414     if (idet < 1 || idet > HAWKI_NB_DETECTORS) return -1 ;
00415     if (bpm == NULL) return -1 ;
00416 
00417     /* Get the extension number for this detector */
00418     if ((ext_nb = hawki_get_ext_from_detector(bpm, idet)) == -1) {
00419         cpl_msg_error(__func__, "Cannot get the extension with detector %d", idet) ;
00420         return -1 ;
00421     }
00422      /* Load the bad pixels image */
00423     if ((bpm_im_int = cpl_image_load(bpm, CPL_TYPE_INT, 0, ext_nb)) == NULL) {
00424         cpl_msg_error(cpl_func, "Cannot load the bad pixel map %s", bpm) ;
00425         return -1 ;
00426     }
00427     /* Convert the map from integer to binary */
00428     bpm_im_bin = cpl_mask_threshold_image_create(bpm_im_int, -0.5, 0.5) ;
00429     cpl_mask_not(bpm_im_bin) ;
00430     cpl_image_delete(bpm_im_int) ;
00431     /* Apply the bad pixels cleaning */
00432     cpl_image_reject_from_mask(ima, bpm_im_bin);
00433     if (cpl_detector_interpolate_rejected(ima) != CPL_ERROR_NONE) {
00434         cpl_msg_error(cpl_func, "Cannot clean the bad pixels");
00435         cpl_mask_delete(bpm_im_bin) ;
00436         return -1 ;
00437     }
00438     cpl_mask_delete(bpm_im_bin) ;
00439 
00440     /* Return */
00441     return 0 ;
00442 }
00443 

Generated on 10 Jun 2010 for HAWKI Pipeline Reference Manual by  doxygen 1.6.1