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