146 #include <uves_flatfield.h>
147 #include <uves_utils.h>
148 #include <uves_utils_wrappers.h>
149 #include <uves_error.h>
186 const cpl_image *master_flat,
const cpl_image *mflat_noise)
188 double *image_data = NULL;
189 cpl_mask *image_mask = NULL;
190 cpl_binary *image_bad = NULL;
192 double *noise_data = NULL;
193 cpl_mask *noise_mask = NULL;
194 cpl_binary *noise_bad = NULL;
196 const double *mf_data = NULL;
197 const cpl_mask *mf_mask = NULL;
198 const cpl_binary *mf_bad = NULL;
200 const double *mfnoise_data = NULL;
201 const cpl_mask *mfnoise_mask = NULL;
202 cpl_mask *mfnoise_mask_own = NULL;
203 cpl_mask *mf_mask_own = NULL;
204 const cpl_binary *mfnoise_bad = NULL;
212 passure( master_flat != NULL,
" ");
213 passure( noise == NULL || mflat_noise != NULL,
" ");
215 passure( cpl_image_get_type(image) == CPL_TYPE_DOUBLE,
216 "Image must be double");
217 passure( noise == NULL || cpl_image_get_type(noise) == CPL_TYPE_DOUBLE,
218 "Image must be double");
219 passure( cpl_image_get_type(master_flat) == CPL_TYPE_DOUBLE,
220 "Image must be double");
221 passure( mflat_noise == NULL || cpl_image_get_type(mflat_noise) == CPL_TYPE_DOUBLE,
222 "Image must be double");
224 nx = cpl_image_get_size_x(image);
225 ny = cpl_image_get_size_y(image);
227 assure( nx == cpl_image_get_size_x(master_flat),
228 CPL_ERROR_INCOMPATIBLE_INPUT,
229 "Input image and master flat field image have different widths: "
230 "%d and %" CPL_SIZE_FORMAT
" (pixels)",
231 nx, cpl_image_get_size_x(master_flat));
233 assure( ny == cpl_image_get_size_y(master_flat),
234 CPL_ERROR_INCOMPATIBLE_INPUT,
235 "Input image and master flat field image have different heights: "
236 "%d and %" CPL_SIZE_FORMAT
" (pixels)",
237 ny, cpl_image_get_size_y(master_flat));
240 check_nomsg(image_data = cpl_image_get_data(image));
241 check_nomsg(image_mask = cpl_image_get_bpm(image));
242 check_nomsg(image_bad = cpl_mask_get_data(image_mask));
244 check_nomsg(mf_data = cpl_image_get_data_const(master_flat));
245 check_nomsg(mf_mask = cpl_image_get_bpm_const(master_flat));
247 mf_mask_own = cpl_mask_new(nx,ny);
248 mf_mask = mf_mask_own ;
250 check_nomsg(mf_bad = cpl_mask_get_data_const(mf_mask));
254 check_nomsg(noise_data = cpl_image_get_data(noise));
255 check_nomsg(noise_mask = cpl_image_get_bpm(noise));
256 check_nomsg(noise_bad = cpl_mask_get_data(noise_mask));
258 check_nomsg(mfnoise_data = cpl_image_get_data_const(mflat_noise));
259 check_nomsg(mfnoise_mask = cpl_image_get_bpm_const(mflat_noise));
260 if(mfnoise_mask==NULL) {
261 mfnoise_mask_own = cpl_mask_new(nx,ny);
262 mfnoise_mask = mfnoise_mask_own ;
264 check_nomsg(mfnoise_bad = cpl_mask_get_data_const(mfnoise_mask));
277 check( ff_mean = cpl_image_get_mean(master_flat),
278 "Could not read average flux of master flat image");
286 check( ff_mean = cpl_image_get_flux(master_flat) / (nx * ny),
287 "Could not read average flux of master flat image");
290 assure( ff_mean != 0 && !irplib_isnan(ff_mean) &&
291 !irplib_isinf(ff_mean), CPL_ERROR_ILLEGAL_INPUT,
292 "Flat-field mean value is %g! Please provide a better flat-field",
296 for (y = 0; y < ny; y++)
298 for (x = 0; x < nx; x++)
300 double mf, mf_noise = 0;
301 double flux, flux_noise = 0, flux_corrected = 0;
302 double noise_corrected = 0;
303 cpl_binary pis_rejected;
306 mf = mf_data[x + y*nx];
307 pis_rejected = mf_bad [x + y*nx];
308 is_bad = is_bad || (pis_rejected == CPL_BINARY_1);
314 flux_noise = noise_data[x + y*nx];
315 pis_rejected = noise_bad [x + y*nx];
316 is_bad = is_bad || (pis_rejected == CPL_BINARY_1);
321 mf_noise = mfnoise_data[x + y*nx];
322 pis_rejected = mfnoise_bad [x + y*nx];
323 is_bad = is_bad || (pis_rejected == CPL_BINARY_1);
329 flux = image_data[x + y*nx];
330 pis_rejected = image_bad [x + y*nx];
331 is_bad = is_bad || (pis_rejected == CPL_BINARY_1);
340 flux_corrected = (flux / mf) * ff_mean;
352 noise_corrected = uves_error_fraction(
353 flux, mf, flux_noise, mf_noise)
359 image_bad[x + nx*y] = CPL_BINARY_1;
363 noise_bad[x + nx*y] = CPL_BINARY_1;
369 image_data[x + nx*y] = flux_corrected;
373 noise_data[x + nx*y] = noise_corrected;
381 if(mf_mask_own) uves_free_mask(&mf_mask_own);
382 if(mfnoise_mask_own) uves_free_mask(&mfnoise_mask_own);
383 return cpl_error_get_code();
399 const char *context,
const char *subcontext)
402 flatfielding_method result = 0;
404 check( uves_get_parameter(parameters, context, subcontext,
"ffmethod", CPL_TYPE_STRING, &ff),
405 "Could not read parameter");
407 if (strcmp(ff,
"pixel" ) == 0) result = FF_PIXEL;
408 else if (strcmp(ff,
"extract") == 0) result = FF_EXTRACT;
409 else if (strcmp(ff,
"no" ) == 0) result = FF_NO;
412 assure(
false, CPL_ERROR_ILLEGAL_INPUT,
"No such flat-fielding method: '%s'", ff);