HAWKI Pipeline Reference Manual  1.8.12
hawki_calib.c
1 /* $Id: hawki_calib.c,v 1.6 2010/11/03 18:50:38 cgarcia Exp $
2  *
3  * This file is part of the HAWKI Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: cgarcia $
23  * $Date: 2010/11/03 18:50:38 $
24  * $Revision: 1.6 $
25  * $Name: hawki-1_8_12 $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <float.h>
37 #include <string.h>
38 #include <math.h>
39 #include <cpl.h>
40 
41 #include "hawki_utils.h"
42 #include "hawki_calib.h"
43 #include "hawki_pfits.h"
44 #include "hawki_load.h"
45 
46 /*----------------------------------------------------------------------------*/
50 /*----------------------------------------------------------------------------*/
51 
54 /*----------------------------------------------------------------------------*/
71 /*----------------------------------------------------------------------------*/
73 (cpl_imagelist * ilist,
74  cpl_imagelist * flat,
75  cpl_imagelist * dark,
76  cpl_imagelist * bpm)
77 {
78  int idet;
79 
80  /* Test entries */
81  if (ilist == NULL) return -1 ;
82 
83  /* Dark correction */
84  if (dark != NULL)
85  {
86  cpl_msg_info(cpl_func, "Subtracting the dark to each chip image") ;
87  /* Apply the dark correction to the images */
88  for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
89  {
90  if (cpl_image_subtract(cpl_imagelist_get(ilist, idet),
91  cpl_imagelist_get(dark, idet))!=CPL_ERROR_NONE)
92  {
93  cpl_msg_error(cpl_func,"Cannot apply the dark to chip %d",
94  idet+1);
95  return -1 ;
96  }
97  }
98  }
99 
100  /* Flat-field correction */
101  if (flat != NULL)
102  {
103  cpl_msg_info(cpl_func, "Dividing the flat to each chip image") ;
104  /* Apply the flat correction to the images */
105  for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
106  {
107  if (cpl_image_divide(cpl_imagelist_get(ilist, idet),
108  cpl_imagelist_get(flat, idet))!=CPL_ERROR_NONE)
109  {
110  cpl_msg_error(__func__,"Cannot apply the flatfield to chip %d",
111  idet+1);
112  return -1 ;
113  }
114  }
115  }
116 
117  /* Correct the bad pixels if requested */
118  if (bpm != NULL)
119  {
120  cpl_msg_info(cpl_func, "Correct the bad pixels to each chip image");
121  /* Apply the flat correction to the images */
122  for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
123  {
124  cpl_mask * bpm_im_bin ;
125  /* Convert the map from integer to binary */
126  bpm_im_bin = cpl_mask_threshold_image_create
127  (cpl_imagelist_get(bpm, idet), -0.5, 0.5) ;
128  cpl_mask_not(bpm_im_bin) ;
129  cpl_image_reject_from_mask(cpl_imagelist_get(ilist, idet), bpm_im_bin);
130  if (cpl_detector_interpolate_rejected
131  (cpl_imagelist_get(ilist, idet)) != CPL_ERROR_NONE)
132  {
133  cpl_msg_error
134  (cpl_func, "Cannot clean the bad pixels in chip %d",
135  idet+1);
136  cpl_mask_delete(bpm_im_bin) ;
137  return -1 ;
138  }
139  cpl_mask_delete(bpm_im_bin) ;
140  }
141  }
142 
143  /* Return */
144  return 0 ;
145 }
146 
147 /*----------------------------------------------------------------------------*/
160 /*----------------------------------------------------------------------------*/
162  cpl_imagelist * ilist,
163  cpl_imagelist * flat,
164  cpl_imagelist * bpm)
165 {
166  int idet;
167 
168  /* Test entries */
169  if (ilist == NULL) return -1 ;
170 
171  /* Flat-field correction */
172  if (flat != NULL)
173  {
174  cpl_msg_info(cpl_func, "Divide the images by the flatfield") ;
175  /* Apply the flat correction to the images */
176  for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
177  {
178  if (cpl_image_divide(cpl_imagelist_get(ilist, idet),
179  cpl_imagelist_get(flat, idet))!=CPL_ERROR_NONE)
180  {
181  cpl_msg_error(cpl_func,"Cannot apply the flatfield to the images");
182  return -1 ;
183  }
184  }
185  }
186 
187  /* Correct the bad pixels if requested */
188  if (bpm != NULL)
189  {
190  cpl_msg_info(cpl_func, "Correct the bad pixels in the images") ;
191  /* Apply the flat correction to the images */
192  for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
193  {
194  cpl_mask * bpm_im_bin ;
195  /* Convert the map from integer to binary */
196  bpm_im_bin = cpl_mask_threshold_image_create
197  (cpl_imagelist_get(bpm, idet), -0.5, 0.5) ;
198  cpl_mask_not(bpm_im_bin) ;
199  cpl_image_reject_from_mask(cpl_imagelist_get(ilist, idet), bpm_im_bin);
200  if (cpl_detector_interpolate_rejected
201  (cpl_imagelist_get(ilist, idet)) != CPL_ERROR_NONE)
202  {
203  cpl_msg_error
204  (cpl_func, "Cannot clean the bad pixels in detector %d",
205  idet+1);
206  cpl_mask_delete(bpm_im_bin) ;
207  return -1 ;
208  }
209  cpl_mask_delete(bpm_im_bin) ;
210  }
211  }
212 
213  /* Return */
214  return 0 ;
215 }
216 
217 /*----------------------------------------------------------------------------*/
224 /*----------------------------------------------------------------------------*/
226 (cpl_imagelist * ilist,
227  cpl_imagelist * bkg)
228 {
229  int idet;
230 
231  /* Test entries */
232  if (ilist == NULL) return -1 ;
233 
234  /* Background correction */
235  if (bkg != NULL)
236  {
237  cpl_msg_info(cpl_func, "Subtract the images by the bkg") ;
238  /* Apply the bkg correction to the images */
239  for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
240  {
241  if (cpl_image_subtract(cpl_imagelist_get(ilist, idet),
242  cpl_imagelist_get(bkg, idet))!=CPL_ERROR_NONE)
243  {
244  cpl_msg_error(cpl_func,"Cannot apply the bkg to the images");
245  return -1 ;
246  }
247  }
248  }
249 
250  /* Return */
251  return 0 ;
252 
253 }
254 
255 /*----------------------------------------------------------------------------*/
275 /*----------------------------------------------------------------------------*/
277  cpl_imagelist * ilist,
278  cpl_image * flat,
279  cpl_image * dark,
280  cpl_image * bpm)
281 {
282  cpl_mask * bpm_im_bin ;
283  int i ;
284 
285  /* Test entries */
286  if (ilist == NULL) return -1 ;
287 
288  /* Dark correction */
289  if (dark != NULL)
290  {
291  cpl_msg_info(cpl_func, "Subtract the images by the dark") ;
292  if (cpl_imagelist_subtract_image(ilist, dark)!=CPL_ERROR_NONE)
293  {
294  cpl_msg_error(cpl_func,"Cannot apply the dark to the images");
295  return -1 ;
296  }
297  }
298 
299  /* Flat-field correction */
300  if (flat != NULL) {
301  cpl_msg_info(cpl_func, "Divide the images by the flatfield") ;
302 
303  /* Apply the flatfield correction to the images */
304  if (cpl_imagelist_divide_image(ilist, flat)!=CPL_ERROR_NONE) {
305  cpl_msg_error(cpl_func,"Cannot apply the flatfield to the images");
306  return -1 ;
307  }
308  }
309 
310  /* Correct the bad pixels if requested */
311  if (bpm != NULL) {
312  cpl_msg_info(cpl_func, "Correct the bad pixels in the images") ;
313 
314  /* Convert the map from integer to binary */
315  bpm_im_bin = cpl_mask_threshold_image_create(bpm, -0.5, 0.5) ;
316  cpl_mask_not(bpm_im_bin) ;
317  /* Apply the bad pixels cleaning */
318  for (i=0 ; i<cpl_imagelist_get_size(ilist) ; i++) {
319  cpl_image_reject_from_mask(cpl_imagelist_get(ilist, i), bpm_im_bin);
320  if (cpl_detector_interpolate_rejected(
321  cpl_imagelist_get(ilist, i)) != CPL_ERROR_NONE) {
322  cpl_msg_error(cpl_func, "Cannot clean the bad pixels in obj %d",
323  i+1);
324  cpl_mask_delete(bpm_im_bin) ;
325  return -1 ;
326  }
327  }
328  cpl_mask_delete(bpm_im_bin) ;
329  }
330 
331  /* Return */
332  return 0 ;
333 }
334 
335 /*----------------------------------------------------------------------------*/
353 /*----------------------------------------------------------------------------*/
355  cpl_imagelist * ilist,
356  cpl_image * flat,
357  cpl_image * bpm)
358 {
359  cpl_mask * bpm_im_bin ;
360  int i ;
361 
362  /* Test entries */
363  if (ilist == NULL) return -1 ;
364 
365  /* Flat-field correction */
366  if (flat != NULL) {
367  cpl_msg_info(cpl_func, "Divide the images by the flatfield") ;
368 
369  /* Apply the flatfield correction to the images */
370  if (cpl_imagelist_divide_image(ilist, flat)!=CPL_ERROR_NONE) {
371  cpl_msg_error(cpl_func,"Cannot apply the flatfield to the images");
372  return -1 ;
373  }
374  }
375 
376  /* Correct the bad pixels if requested */
377  if (bpm != NULL) {
378  cpl_msg_info(cpl_func, "Correct the bad pixels in the images") ;
379 
380  /* Convert the map from integer to binary */
381  bpm_im_bin = cpl_mask_threshold_image_create(bpm, -0.5, 0.5) ;
382  cpl_mask_not(bpm_im_bin) ;
383  /* Apply the bad pixels cleaning */
384  for (i=0 ; i<cpl_imagelist_get_size(ilist) ; i++) {
385  cpl_image_reject_from_mask(cpl_imagelist_get(ilist, i), bpm_im_bin);
386  if (cpl_detector_interpolate_rejected(
387  cpl_imagelist_get(ilist, i)) != CPL_ERROR_NONE) {
388  cpl_msg_error(cpl_func, "Cannot clean the bad pixels in obj %d",
389  i+1);
390  cpl_mask_delete(bpm_im_bin) ;
391  return -1 ;
392  }
393  }
394  cpl_mask_delete(bpm_im_bin) ;
395  }
396 
397  /* Return */
398  return 0 ;
399 }
400 
401 /*----------------------------------------------------------------------------*/
412 cpl_imagelist * hawki_trim_detector_calib
413 (cpl_imagelist * imalist,
414  int nborder)
415 {
416  int i = 0;
417  /* Discard the pixels on the sides */
418  cpl_imagelist * trimmed_images = cpl_imagelist_new();
419  while(cpl_imagelist_get_size(imalist) > 0)
420  {
421  cpl_image * non_trimmed;
422  cpl_image * trimmed;
423  int nx;
424  int ny;
425 
426  non_trimmed = cpl_imagelist_unset(imalist, 0);
427  nx = cpl_image_get_size_x(non_trimmed);
428  ny = cpl_image_get_size_y(non_trimmed);
429  trimmed = cpl_image_extract(non_trimmed,
430  nborder+1, nborder+1,
431  nx-nborder, ny-nborder);
432  cpl_imagelist_set(trimmed_images, trimmed, i);
433  ++i;
434  cpl_image_delete(non_trimmed);
435  }
436 
437  return trimmed_images;
438 }
439 
440 /*----------------------------------------------------------------------------*/
449 /*----------------------------------------------------------------------------*/
451  cpl_image * ima,
452  const char * bpm,
453  int idet)
454 {
455  cpl_mask * bpm_im_bin ;
456  cpl_image * bpm_im_int ;
457  int ext_nb ;
458 
459  /* Test entries */
460  if (ima == NULL) return -1 ;
461  if (idet < 1 || idet > HAWKI_NB_DETECTORS) return -1 ;
462  if (bpm == NULL) return -1 ;
463 
464  /* Get the extension number for this detector */
465  if ((ext_nb = hawki_get_ext_from_detector(bpm, idet)) == -1) {
466  cpl_msg_error(__func__, "Cannot get the extension with detector %d", idet) ;
467  return -1 ;
468  }
469  /* Load the bad pixels image */
470  if ((bpm_im_int = cpl_image_load(bpm, CPL_TYPE_INT, 0, ext_nb)) == NULL) {
471  cpl_msg_error(cpl_func, "Cannot load the bad pixel map %s", bpm) ;
472  return -1 ;
473  }
474  /* Convert the map from integer to binary */
475  bpm_im_bin = cpl_mask_threshold_image_create(bpm_im_int, -0.5, 0.5) ;
476  cpl_mask_not(bpm_im_bin) ;
477  cpl_image_delete(bpm_im_int) ;
478  /* Apply the bad pixels cleaning */
479  cpl_image_reject_from_mask(ima, bpm_im_bin);
480  if (cpl_detector_interpolate_rejected(ima) != CPL_ERROR_NONE) {
481  cpl_msg_error(cpl_func, "Cannot clean the bad pixels");
482  cpl_mask_delete(bpm_im_bin) ;
483  return -1 ;
484  }
485  cpl_mask_delete(bpm_im_bin) ;
486 
487  /* Return */
488  return 0 ;
489 }
490