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 #ifdef HAVE_CONFIG_H
00108 # include <config.h>
00109 #endif
00110
00111
00118
00121
00122
00123
00124
00125 #include <uves_flatfield.h>
00126 #include <uves_utils.h>
00127 #include <uves_utils_wrappers.h>
00128 #include <uves_error.h>
00129
00130 #include <cpl.h>
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00162
00163 cpl_error_code
00164 uves_flatfielding(cpl_image *image, cpl_image *noise,
00165 const cpl_image *master_flat, const cpl_image *mflat_noise)
00166 {
00167 double *image_data = NULL;
00168 cpl_mask *image_mask = NULL;
00169 cpl_binary *image_bad = NULL;
00170
00171 double *noise_data = NULL;
00172 cpl_mask *noise_mask = NULL;
00173 cpl_binary *noise_bad = NULL;
00174
00175 const double *mf_data = NULL;
00176 const cpl_mask *mf_mask = NULL;
00177 const cpl_binary *mf_bad = NULL;
00178
00179 const double *mfnoise_data = NULL;
00180 const cpl_mask *mfnoise_mask = NULL;
00181 const cpl_binary *mfnoise_bad = NULL;
00182
00183
00184 double ff_mean;
00185 int nx, ny;
00186 int x, y;
00187
00188 passure( image != NULL, " ");
00189 passure( master_flat != NULL, " ");
00190 passure( noise == NULL || mflat_noise != NULL, " ");
00191
00192 passure( cpl_image_get_type(image) == CPL_TYPE_DOUBLE,
00193 "Image must be double");
00194 passure( noise == NULL || cpl_image_get_type(noise) == CPL_TYPE_DOUBLE,
00195 "Image must be double");
00196 passure( cpl_image_get_type(master_flat) == CPL_TYPE_DOUBLE,
00197 "Image must be double");
00198 passure( mflat_noise == NULL || cpl_image_get_type(mflat_noise) == CPL_TYPE_DOUBLE,
00199 "Image must be double");
00200
00201 nx = cpl_image_get_size_x(image);
00202 ny = cpl_image_get_size_y(image);
00203
00204 assure( nx == cpl_image_get_size_x(master_flat),
00205 CPL_ERROR_INCOMPATIBLE_INPUT,
00206 "Input image and master flat field image have different widths: "
00207 "%d and %d (pixels)",
00208 nx, cpl_image_get_size_x(master_flat));
00209
00210 assure( ny == cpl_image_get_size_y(master_flat),
00211 CPL_ERROR_INCOMPATIBLE_INPUT,
00212 "Input image and master flat field image have different heights: "
00213 "%d and %d (pixels)",
00214 ny, cpl_image_get_size_y(master_flat));
00215
00216
00217 image_data = cpl_image_get_data(image);
00218 image_mask = cpl_image_get_bpm(image);
00219 image_bad = cpl_mask_get_data(image_mask);
00220
00221 mf_data = cpl_image_get_data_const(master_flat);
00222 mf_mask = cpl_image_get_bpm_const(master_flat);
00223 mf_bad = cpl_mask_get_data_const(mf_mask);
00224
00225 if (noise != NULL)
00226 {
00227 noise_data = cpl_image_get_data(noise);
00228 noise_mask = cpl_image_get_bpm(noise);
00229 noise_bad = cpl_mask_get_data(noise_mask);
00230
00231 mfnoise_data = cpl_image_get_data_const(mflat_noise);
00232 mfnoise_mask = cpl_image_get_bpm_const(mflat_noise);
00233 mfnoise_bad = cpl_mask_get_data_const(mfnoise_mask);
00234 }
00235
00236 if (false)
00237 {
00238
00239
00240
00241
00242
00243
00244
00245
00246 check( ff_mean = cpl_image_get_mean(master_flat),
00247 "Could not read average flux of master flat image");
00248 }
00249 else
00250 {
00251
00252
00253
00254
00255 check( ff_mean = cpl_image_get_flux(master_flat) / (nx * ny),
00256 "Could not read average flux of master flat image");
00257 }
00258
00259 assure( ff_mean != 0 && !irplib_isnan(ff_mean) &&
00260 !irplib_isinf(ff_mean), CPL_ERROR_ILLEGAL_INPUT,
00261 "Flat-field mean value is %g! Please provide a better flat-field",
00262 ff_mean );
00263
00264
00265 for (y = 0; y < ny; y++)
00266 {
00267 for (x = 0; x < nx; x++)
00268 {
00269 double mf, mf_noise = 0;
00270 double flux, flux_noise = 0, flux_corrected = 0;
00271 double noise_corrected = 0;
00272 cpl_binary pis_rejected;
00273 bool is_bad = false;
00274
00275 mf = mf_data[x + y*nx];
00276 pis_rejected = mf_bad [x + y*nx];
00277 is_bad = is_bad || (pis_rejected == CPL_BINARY_1);
00278
00279
00280
00281 if (noise != NULL)
00282 {
00283 flux_noise = noise_data[x + y*nx];
00284 pis_rejected = noise_bad [x + y*nx];
00285 is_bad = is_bad || (pis_rejected == CPL_BINARY_1);
00286
00287
00288
00289
00290 mf_noise = mfnoise_data[x + y*nx];
00291 pis_rejected = mfnoise_bad [x + y*nx];
00292 is_bad = is_bad || (pis_rejected == CPL_BINARY_1);
00293
00294
00295
00296 }
00297
00298 flux = image_data[x + y*nx];
00299 pis_rejected = image_bad [x + y*nx];
00300 is_bad = is_bad || (pis_rejected == CPL_BINARY_1);
00301
00302
00303
00304
00305
00306
00307 if (mf > 0)
00308 {
00309 flux_corrected = (flux / mf) * ff_mean;
00310 }
00311 else
00312 {
00313
00314
00315
00316 is_bad = true;
00317 }
00318
00319 if (noise != NULL)
00320 {
00321 noise_corrected = uves_error_fraction(
00322 flux, mf, flux_noise, mf_noise)
00323 * ff_mean;
00324 }
00325
00326 if (is_bad)
00327 {
00328 image_bad[x + nx*y] = CPL_BINARY_1;
00329
00330 if (noise != NULL)
00331 {
00332 noise_bad[x + nx*y] = CPL_BINARY_1;
00333
00334 }
00335 }
00336 else
00337 {
00338 image_data[x + nx*y] = flux_corrected;
00339
00340 if (noise != NULL)
00341 {
00342 noise_data[x + nx*y] = noise_corrected;
00343
00344 }
00345 }
00346 }
00347 }
00348
00349 cleanup:
00350 return cpl_error_get_code();
00351 }
00352
00353
00363
00364 flatfielding_method
00365 uves_get_flatfield_method(const cpl_parameterlist *parameters,
00366 const char *context, const char *subcontext)
00367 {
00368 const char *ff = "";
00369 flatfielding_method result = 0;
00370
00371 check( uves_get_parameter(parameters, context, subcontext, "ffmethod", CPL_TYPE_STRING, &ff),
00372 "Could not read parameter");
00373
00374 if (strcmp(ff, "pixel" ) == 0) result = FF_PIXEL;
00375 else if (strcmp(ff, "extract") == 0) result = FF_EXTRACT;
00376 else if (strcmp(ff, "no" ) == 0) result = FF_NO;
00377 else
00378 {
00379 assure(false, CPL_ERROR_ILLEGAL_INPUT, "No such flat-fielding method: '%s'", ff);
00380 }
00381
00382 cleanup:
00383 return result;
00384 }
00385