00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
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
00078 if (ilist == NULL) return -1 ;
00079
00080
00081 if (dark != NULL)
00082 {
00083 cpl_msg_info(cpl_func, "Subtracting the dark to each chip image") ;
00084
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
00098 if (flat != NULL)
00099 {
00100 cpl_msg_info(cpl_func, "Dividing the flat to each chip image") ;
00101
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
00115 if (bpm != NULL)
00116 {
00117 cpl_msg_info(cpl_func, "Correct the bad pixels to each chip image");
00118
00119 for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00120 {
00121 cpl_mask * bpm_im_bin ;
00122
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
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
00166 if (ilist == NULL) return -1 ;
00167
00168
00169 if (flat != NULL)
00170 {
00171 cpl_msg_info(cpl_func, "Divide the images by the flatfield") ;
00172
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
00185 if (bpm != NULL)
00186 {
00187 cpl_msg_info(cpl_func, "Correct the bad pixels in the images") ;
00188
00189 for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00190 {
00191 cpl_mask * bpm_im_bin ;
00192
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
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
00229 if (ilist == NULL) return -1 ;
00230
00231
00232 if (bkg != NULL)
00233 {
00234 cpl_msg_info(cpl_func, "Subtract the images by the bkg") ;
00235
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
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
00281 if (ilist == NULL) return -1 ;
00282
00283
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
00295 if (flat != NULL) {
00296 cpl_msg_info(cpl_func, "Divide the images by the flatfield") ;
00297
00298
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
00306 if (bpm != NULL) {
00307 cpl_msg_info(cpl_func, "Correct the bad pixels in the images") ;
00308
00309
00310 bpm_im_bin = cpl_mask_threshold_image_create(bpm, -0.5, 0.5) ;
00311 cpl_mask_not(bpm_im_bin) ;
00312
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
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
00355 if (ilist == NULL) return -1 ;
00356
00357
00358 if (flat != NULL) {
00359 cpl_msg_info(cpl_func, "Divide the images by the flatfield") ;
00360
00361
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
00369 if (bpm != NULL) {
00370 cpl_msg_info(cpl_func, "Correct the bad pixels in the images") ;
00371
00372
00373 bpm_im_bin = cpl_mask_threshold_image_create(bpm, -0.5, 0.5) ;
00374 cpl_mask_not(bpm_im_bin) ;
00375
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
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
00413 if (ima == NULL) return -1 ;
00414 if (idet < 1 || idet > HAWKI_NB_DETECTORS) return -1 ;
00415 if (bpm == NULL) return -1 ;
00416
00417
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
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
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
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
00441 return 0 ;
00442 }
00443