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
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 #ifdef HAVE_CONFIG_H
00129 # include <config.h>
00130 #endif
00131
00132
00139
00142
00143
00144
00145 #include <string.h>
00146 #include <uves_flatfield.h>
00147 #include <uves_utils.h>
00148 #include <uves_utils_wrappers.h>
00149 #include <uves_error.h>
00150
00151 #include <cpl.h>
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00183
00184 cpl_error_code
00185 uves_flatfielding(cpl_image *image, cpl_image *noise,
00186 const cpl_image *master_flat, const cpl_image *mflat_noise)
00187 {
00188 double *image_data = NULL;
00189 cpl_mask *image_mask = NULL;
00190 cpl_binary *image_bad = NULL;
00191
00192 double *noise_data = NULL;
00193 cpl_mask *noise_mask = NULL;
00194 cpl_binary *noise_bad = NULL;
00195
00196 const double *mf_data = NULL;
00197 const cpl_mask *mf_mask = NULL;
00198 const cpl_binary *mf_bad = NULL;
00199
00200 const double *mfnoise_data = NULL;
00201 const cpl_mask *mfnoise_mask = NULL;
00202 cpl_mask *mfnoise_mask_own = NULL;
00203 cpl_mask *mf_mask_own = NULL;
00204 const cpl_binary *mfnoise_bad = NULL;
00205
00206
00207 double ff_mean;
00208 int nx, ny;
00209 int x, y;
00210
00211 passure( image != NULL, " ");
00212 passure( master_flat != NULL, " ");
00213 passure( noise == NULL || mflat_noise != NULL, " ");
00214
00215 passure( cpl_image_get_type(image) == CPL_TYPE_DOUBLE,
00216 "Image must be double");
00217 passure( noise == NULL || cpl_image_get_type(noise) == CPL_TYPE_DOUBLE,
00218 "Image must be double");
00219 passure( cpl_image_get_type(master_flat) == CPL_TYPE_DOUBLE,
00220 "Image must be double");
00221 passure( mflat_noise == NULL || cpl_image_get_type(mflat_noise) == CPL_TYPE_DOUBLE,
00222 "Image must be double");
00223
00224 nx = cpl_image_get_size_x(image);
00225 ny = cpl_image_get_size_y(image);
00226
00227 assure( nx == cpl_image_get_size_x(master_flat),
00228 CPL_ERROR_INCOMPATIBLE_INPUT,
00229 "Input image and master flat field image have different widths: "
00230 "%" CPL_SIZE_FORMAT " and %" CPL_SIZE_FORMAT " (pixels)",
00231 nx, cpl_image_get_size_x(master_flat));
00232
00233 assure( ny == cpl_image_get_size_y(master_flat),
00234 CPL_ERROR_INCOMPATIBLE_INPUT,
00235 "Input image and master flat field image have different heights: "
00236 "%" CPL_SIZE_FORMAT " and %" CPL_SIZE_FORMAT " (pixels)",
00237 ny, cpl_image_get_size_y(master_flat));
00238
00239
00240 check_nomsg(image_data = cpl_image_get_data(image));
00241 check_nomsg(image_mask = cpl_image_get_bpm(image));
00242 check_nomsg(image_bad = cpl_mask_get_data(image_mask));
00243
00244 check_nomsg(mf_data = cpl_image_get_data_const(master_flat));
00245 check_nomsg(mf_mask = cpl_image_get_bpm_const(master_flat));
00246 if(mf_mask==NULL) {
00247 mf_mask_own = cpl_mask_new(nx,ny);
00248 mf_mask = mf_mask_own ;
00249 }
00250 check_nomsg(mf_bad = cpl_mask_get_data_const(mf_mask));
00251
00252 if (noise != NULL)
00253 {
00254 check_nomsg(noise_data = cpl_image_get_data(noise));
00255 check_nomsg(noise_mask = cpl_image_get_bpm(noise));
00256 check_nomsg(noise_bad = cpl_mask_get_data(noise_mask));
00257
00258 check_nomsg(mfnoise_data = cpl_image_get_data_const(mflat_noise));
00259 check_nomsg(mfnoise_mask = cpl_image_get_bpm_const(mflat_noise));
00260 if(mfnoise_mask==NULL) {
00261 mfnoise_mask_own = cpl_mask_new(nx,ny);
00262 mfnoise_mask = mfnoise_mask_own ;
00263 }
00264 check_nomsg(mfnoise_bad = cpl_mask_get_data_const(mfnoise_mask));
00265 }
00266
00267 if (false)
00268 {
00269
00270
00271
00272
00273
00274
00275
00276
00277 check( ff_mean = cpl_image_get_mean(master_flat),
00278 "Could not read average flux of master flat image");
00279 }
00280 else
00281 {
00282
00283
00284
00285
00286 check( ff_mean = cpl_image_get_flux(master_flat) / (nx * ny),
00287 "Could not read average flux of master flat image");
00288 }
00289
00290 assure( ff_mean != 0 && !irplib_isnan(ff_mean) &&
00291 !irplib_isinf(ff_mean), CPL_ERROR_ILLEGAL_INPUT,
00292 "Flat-field mean value is %g! Please provide a better flat-field",
00293 ff_mean );
00294
00295
00296 for (y = 0; y < ny; y++)
00297 {
00298 for (x = 0; x < nx; x++)
00299 {
00300 double mf, mf_noise = 0;
00301 double flux, flux_noise = 0, flux_corrected = 0;
00302 double noise_corrected = 0;
00303 cpl_binary pis_rejected;
00304 bool is_bad = false;
00305
00306 mf = mf_data[x + y*nx];
00307 pis_rejected = mf_bad [x + y*nx];
00308 is_bad = is_bad || (pis_rejected == CPL_BINARY_1);
00309
00310
00311
00312 if (noise != NULL)
00313 {
00314 flux_noise = noise_data[x + y*nx];
00315 pis_rejected = noise_bad [x + y*nx];
00316 is_bad = is_bad || (pis_rejected == CPL_BINARY_1);
00317
00318
00319
00320
00321 mf_noise = mfnoise_data[x + y*nx];
00322 pis_rejected = mfnoise_bad [x + y*nx];
00323 is_bad = is_bad || (pis_rejected == CPL_BINARY_1);
00324
00325
00326
00327 }
00328
00329 flux = image_data[x + y*nx];
00330 pis_rejected = image_bad [x + y*nx];
00331 is_bad = is_bad || (pis_rejected == CPL_BINARY_1);
00332
00333
00334
00335
00336
00337
00338 if (mf > 0)
00339 {
00340 flux_corrected = (flux / mf) * ff_mean;
00341 }
00342 else
00343 {
00344
00345
00346
00347 is_bad = true;
00348 }
00349
00350 if (noise != NULL)
00351 {
00352 noise_corrected = uves_error_fraction(
00353 flux, mf, flux_noise, mf_noise)
00354 * ff_mean;
00355 }
00356
00357 if (is_bad)
00358 {
00359 image_bad[x + nx*y] = CPL_BINARY_1;
00360
00361 if (noise != NULL)
00362 {
00363 noise_bad[x + nx*y] = CPL_BINARY_1;
00364
00365 }
00366 }
00367 else
00368 {
00369 image_data[x + nx*y] = flux_corrected;
00370
00371 if (noise != NULL)
00372 {
00373 noise_data[x + nx*y] = noise_corrected;
00374
00375 }
00376 }
00377 }
00378 }
00379
00380 cleanup:
00381 if(mf_mask_own) uves_free_mask(&mf_mask_own);
00382 if(mfnoise_mask_own) uves_free_mask(&mfnoise_mask_own);
00383 return cpl_error_get_code();
00384 }
00385
00386
00396
00397 flatfielding_method
00398 uves_get_flatfield_method(const cpl_parameterlist *parameters,
00399 const char *context, const char *subcontext)
00400 {
00401 const char *ff = "";
00402 flatfielding_method result = 0;
00403
00404 check( uves_get_parameter(parameters, context, subcontext, "ffmethod", CPL_TYPE_STRING, &ff),
00405 "Could not read parameter");
00406
00407 if (strcmp(ff, "pixel" ) == 0) result = FF_PIXEL;
00408 else if (strcmp(ff, "extract") == 0) result = FF_EXTRACT;
00409 else if (strcmp(ff, "no" ) == 0) result = FF_NO;
00410 else
00411 {
00412 assure(false, CPL_ERROR_ILLEGAL_INPUT, "No such flat-fielding method: '%s'", ff);
00413 }
00414
00415 cleanup:
00416 return result;
00417 }
00418