hawki_calib.c

00001 /* $Id: hawki_calib.c,v 1.6 2010/11/03 18:50:38 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: 2010/11/03 18:50:38 $
00024  * $Revision: 1.6 $
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 <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 /*----------------------------------------------------------------------------*/
00071 /*----------------------------------------------------------------------------*/
00072 int hawki_flat_dark_bpm_imglist_calib
00073 (cpl_imagelist  *  ilist,
00074  cpl_imagelist  *  flat,
00075  cpl_imagelist  *  dark,
00076  cpl_imagelist  *  bpm)
00077 {
00078     int   idet;
00079 
00080     /* Test entries */
00081     if (ilist == NULL) return -1 ;
00082 
00083     /* Dark correction */
00084     if (dark != NULL)
00085     {
00086         cpl_msg_info(cpl_func, "Subtracting the dark to each chip image") ;
00087         /* Apply the dark correction to the images */
00088         for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00089         {
00090             if (cpl_image_subtract(cpl_imagelist_get(ilist, idet),
00091                                    cpl_imagelist_get(dark, idet))!=CPL_ERROR_NONE) 
00092             {
00093                 cpl_msg_error(cpl_func,"Cannot apply the dark to chip %d",
00094                               idet+1);
00095                 return -1 ;
00096             }
00097         }
00098     }
00099 
00100     /* Flat-field correction */
00101     if (flat != NULL) 
00102     {
00103         cpl_msg_info(cpl_func, "Dividing the flat to each chip image") ;
00104         /* Apply the flat correction to the images */
00105         for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00106         {
00107             if (cpl_image_divide(cpl_imagelist_get(ilist, idet),
00108                                  cpl_imagelist_get(flat, idet))!=CPL_ERROR_NONE) 
00109             {
00110                 cpl_msg_error(__func__,"Cannot apply the flatfield to chip %d",
00111                               idet+1);
00112                 return -1 ;
00113             }
00114         }
00115     }
00116 
00117     /* Correct the bad pixels if requested */
00118     if (bpm != NULL) 
00119     {
00120         cpl_msg_info(cpl_func, "Correct the bad pixels to each chip image");
00121         /* Apply the flat correction to the images */
00122         for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00123         {
00124             cpl_mask    * bpm_im_bin ;
00125             /* Convert the map from integer to binary */
00126             bpm_im_bin = cpl_mask_threshold_image_create
00127                 (cpl_imagelist_get(bpm, idet), -0.5, 0.5) ;
00128             cpl_mask_not(bpm_im_bin) ;
00129             cpl_image_reject_from_mask(cpl_imagelist_get(ilist, idet), bpm_im_bin);
00130             if (cpl_detector_interpolate_rejected
00131                     (cpl_imagelist_get(ilist, idet)) != CPL_ERROR_NONE) 
00132             {
00133                     cpl_msg_error
00134                         (cpl_func, "Cannot clean the bad pixels in chip %d",
00135                          idet+1);
00136                     cpl_mask_delete(bpm_im_bin) ;
00137                     return -1 ;
00138             }
00139             cpl_mask_delete(bpm_im_bin) ;
00140         }
00141     }
00142 
00143     /* Return */
00144     return 0 ;
00145 }
00146 
00147 /*----------------------------------------------------------------------------*/
00160 /*----------------------------------------------------------------------------*/
00161 int hawki_flat_bpm_imglist_calib(
00162         cpl_imagelist       *   ilist,
00163         cpl_imagelist       *   flat,
00164         cpl_imagelist       *   bpm)
00165 {
00166     int                 idet;
00167 
00168     /* Test entries */
00169     if (ilist == NULL) return -1 ;
00170 
00171     /* Flat-field correction */
00172     if (flat != NULL) 
00173     {
00174         cpl_msg_info(cpl_func, "Divide the images by the flatfield") ;
00175         /* Apply the flat correction to the images */
00176         for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00177         {
00178             if (cpl_image_divide(cpl_imagelist_get(ilist, idet),
00179                                  cpl_imagelist_get(flat, idet))!=CPL_ERROR_NONE) 
00180             {
00181                 cpl_msg_error(cpl_func,"Cannot apply the flatfield to the images");
00182                 return -1 ;
00183             }
00184         }
00185     }
00186 
00187     /* Correct the bad pixels if requested */
00188     if (bpm != NULL) 
00189     {
00190         cpl_msg_info(cpl_func, "Correct the bad pixels in the images") ;
00191         /* Apply the flat correction to the images */
00192         for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00193         {
00194             cpl_mask        *   bpm_im_bin ;
00195             /* Convert the map from integer to binary */
00196             bpm_im_bin = cpl_mask_threshold_image_create
00197                 (cpl_imagelist_get(bpm, idet), -0.5, 0.5) ;
00198             cpl_mask_not(bpm_im_bin) ;
00199             cpl_image_reject_from_mask(cpl_imagelist_get(ilist, idet), bpm_im_bin);
00200             if (cpl_detector_interpolate_rejected
00201                     (cpl_imagelist_get(ilist, idet)) != CPL_ERROR_NONE) 
00202             {
00203                     cpl_msg_error
00204                         (cpl_func, "Cannot clean the bad pixels in detector %d",
00205                          idet+1);
00206                     cpl_mask_delete(bpm_im_bin) ;
00207                     return -1 ;
00208             }
00209             cpl_mask_delete(bpm_im_bin) ;
00210         }
00211     }
00212 
00213     /* Return */
00214     return 0 ;
00215 }
00216 
00217 /*----------------------------------------------------------------------------*/
00224 /*----------------------------------------------------------------------------*/
00225 int hawki_bkg_imglist_calib
00226 (cpl_imagelist *       ilist,
00227  cpl_imagelist *       bkg)
00228 {
00229     int                 idet;
00230 
00231     /* Test entries */
00232     if (ilist == NULL) return -1 ;
00233 
00234     /* Background correction */
00235     if (bkg != NULL) 
00236     {
00237         cpl_msg_info(cpl_func, "Subtract the images by the bkg") ;
00238         /* Apply the bkg correction to the images */
00239         for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00240         {
00241             if (cpl_image_subtract(cpl_imagelist_get(ilist, idet),
00242                                    cpl_imagelist_get(bkg, idet))!=CPL_ERROR_NONE) 
00243             {
00244                 cpl_msg_error(cpl_func,"Cannot apply the bkg to the images");
00245                 return -1 ;
00246             }
00247         }
00248     }
00249 
00250     /* Return */
00251     return 0 ;
00252 
00253 }
00254 
00255 /*----------------------------------------------------------------------------*/
00275 /*----------------------------------------------------------------------------*/
00276 int hawki_flat_dark_bpm_detector_calib(
00277         cpl_imagelist       *   ilist,
00278         cpl_image           *   flat,
00279         cpl_image           *   dark,
00280         cpl_image           *   bpm)
00281 {
00282     cpl_mask        *   bpm_im_bin ;
00283     int                 i ;
00284 
00285     /* Test entries */
00286     if (ilist == NULL) return -1 ;
00287 
00288     /* Dark correction */
00289     if (dark != NULL) 
00290     {
00291         cpl_msg_info(cpl_func, "Subtract the images by the dark") ;
00292         if (cpl_imagelist_subtract_image(ilist, dark)!=CPL_ERROR_NONE) 
00293         {
00294             cpl_msg_error(cpl_func,"Cannot apply the dark to the images");
00295             return -1 ;
00296         }
00297     }
00298 
00299     /* Flat-field correction */
00300     if (flat != NULL) {
00301         cpl_msg_info(cpl_func, "Divide the images by the flatfield") ;
00302 
00303         /* Apply the flatfield correction to the images */
00304         if (cpl_imagelist_divide_image(ilist, flat)!=CPL_ERROR_NONE) {
00305             cpl_msg_error(cpl_func,"Cannot apply the flatfield to the images");
00306             return -1 ;
00307         }
00308     }
00309 
00310     /* Correct the bad pixels if requested */
00311     if (bpm != NULL) {
00312         cpl_msg_info(cpl_func, "Correct the bad pixels in the images") ;
00313 
00314         /* Convert the map from integer to binary */
00315         bpm_im_bin = cpl_mask_threshold_image_create(bpm, -0.5, 0.5) ;
00316         cpl_mask_not(bpm_im_bin) ;
00317         /* Apply the bad pixels cleaning */
00318         for (i=0 ; i<cpl_imagelist_get_size(ilist) ; i++) {
00319             cpl_image_reject_from_mask(cpl_imagelist_get(ilist, i), bpm_im_bin);
00320             if (cpl_detector_interpolate_rejected(
00321                         cpl_imagelist_get(ilist, i)) != CPL_ERROR_NONE) {
00322                 cpl_msg_error(cpl_func, "Cannot clean the bad pixels in obj %d",
00323                         i+1);
00324                 cpl_mask_delete(bpm_im_bin) ;
00325                 return -1 ;
00326             }
00327         }
00328         cpl_mask_delete(bpm_im_bin) ;
00329     }
00330 
00331     /* Return */
00332     return 0 ;
00333 }
00334 
00335 /*----------------------------------------------------------------------------*/
00353 /*----------------------------------------------------------------------------*/
00354 int hawki_flat_bpm_detector_calib(
00355         cpl_imagelist       *   ilist,
00356         cpl_image           *   flat,
00357         cpl_image           *   bpm)
00358 {
00359     cpl_mask        *   bpm_im_bin ;
00360     int                 i ;
00361 
00362     /* Test entries */
00363     if (ilist == NULL) return -1 ;
00364 
00365     /* Flat-field correction */
00366     if (flat != NULL) {
00367         cpl_msg_info(cpl_func, "Divide the images by the flatfield") ;
00368 
00369         /* Apply the flatfield correction to the images */
00370         if (cpl_imagelist_divide_image(ilist, flat)!=CPL_ERROR_NONE) {
00371             cpl_msg_error(cpl_func,"Cannot apply the flatfield to the images");
00372             return -1 ;
00373         }
00374     }
00375 
00376     /* Correct the bad pixels if requested */
00377     if (bpm != NULL) {
00378         cpl_msg_info(cpl_func, "Correct the bad pixels in the images") ;
00379 
00380         /* Convert the map from integer to binary */
00381         bpm_im_bin = cpl_mask_threshold_image_create(bpm, -0.5, 0.5) ;
00382         cpl_mask_not(bpm_im_bin) ;
00383         /* Apply the bad pixels cleaning */
00384         for (i=0 ; i<cpl_imagelist_get_size(ilist) ; i++) {
00385             cpl_image_reject_from_mask(cpl_imagelist_get(ilist, i), bpm_im_bin);
00386             if (cpl_detector_interpolate_rejected(
00387                         cpl_imagelist_get(ilist, i)) != CPL_ERROR_NONE) {
00388                 cpl_msg_error(cpl_func, "Cannot clean the bad pixels in obj %d",
00389                         i+1);
00390                 cpl_mask_delete(bpm_im_bin) ;
00391                 return -1 ;
00392             }
00393         }
00394         cpl_mask_delete(bpm_im_bin) ;
00395     }
00396 
00397     /* Return */
00398     return 0 ;
00399 }
00400 
00401 /*----------------------------------------------------------------------------*/
00412 cpl_imagelist *  hawki_trim_detector_calib
00413 (cpl_imagelist       *   imalist,
00414  int                     nborder)
00415 {
00416     int i = 0;
00417     /* Discard the pixels on the sides */
00418     cpl_imagelist * trimmed_images = cpl_imagelist_new();
00419     while(cpl_imagelist_get_size(imalist) > 0)
00420     {
00421         cpl_image * non_trimmed;
00422         cpl_image * trimmed;
00423         int         nx;
00424         int         ny;
00425         
00426         non_trimmed = cpl_imagelist_unset(imalist, 0);
00427         nx = cpl_image_get_size_x(non_trimmed);
00428         ny = cpl_image_get_size_y(non_trimmed);
00429         trimmed = cpl_image_extract(non_trimmed, 
00430                                     nborder+1, nborder+1, 
00431                                     nx-nborder, ny-nborder);
00432         cpl_imagelist_set(trimmed_images, trimmed, i);
00433         ++i;
00434         cpl_image_delete(non_trimmed);
00435     }
00436 
00437     return trimmed_images;
00438 }
00439 
00440 /*----------------------------------------------------------------------------*/
00449 /*----------------------------------------------------------------------------*/
00450 int hawki_bpm_calib(
00451         cpl_image           *   ima,
00452         const char          *   bpm,
00453         int                     idet)
00454 {
00455     cpl_mask        *   bpm_im_bin ;
00456     cpl_image       *   bpm_im_int ;
00457     int                 ext_nb ;
00458 
00459     /* Test entries */
00460     if (ima == NULL) return -1 ;
00461     if (idet < 1 || idet > HAWKI_NB_DETECTORS) return -1 ;
00462     if (bpm == NULL) return -1 ;
00463 
00464     /* Get the extension number for this detector */
00465     if ((ext_nb = hawki_get_ext_from_detector(bpm, idet)) == -1) {
00466         cpl_msg_error(__func__, "Cannot get the extension with detector %d", idet) ;
00467         return -1 ;
00468     }
00469      /* Load the bad pixels image */
00470     if ((bpm_im_int = cpl_image_load(bpm, CPL_TYPE_INT, 0, ext_nb)) == NULL) {
00471         cpl_msg_error(cpl_func, "Cannot load the bad pixel map %s", bpm) ;
00472         return -1 ;
00473     }
00474     /* Convert the map from integer to binary */
00475     bpm_im_bin = cpl_mask_threshold_image_create(bpm_im_int, -0.5, 0.5) ;
00476     cpl_mask_not(bpm_im_bin) ;
00477     cpl_image_delete(bpm_im_int) ;
00478     /* Apply the bad pixels cleaning */
00479     cpl_image_reject_from_mask(ima, bpm_im_bin);
00480     if (cpl_detector_interpolate_rejected(ima) != CPL_ERROR_NONE) {
00481         cpl_msg_error(cpl_func, "Cannot clean the bad pixels");
00482         cpl_mask_delete(bpm_im_bin) ;
00483         return -1 ;
00484     }
00485     cpl_mask_delete(bpm_im_bin) ;
00486 
00487     /* Return */
00488     return 0 ;
00489 }
00490 

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