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 #ifdef HAVE_CONFIG_H
00090 # include <config.h>
00091 #endif
00092
00093
00099
00102
00103
00104
00105
00106 #include "uves_corrbadpix.h"
00107
00108 #include <uves_pfits.h>
00109 #include <uves_dump.h>
00110 #include <uves_error.h>
00111 #include <uves_msg.h>
00112
00113 #include <irplib_access.h>
00114 #include <cpl.h>
00115
00116 #include <stdbool.h>
00117
00118
00119
00120 static int
00121 uves_correct_badpix(cpl_image *master_bias, uves_propertylist *header, int **badmap,
00122 bool mark_bad);
00123
00124
00125
00126
00127
00128
00129
00146
00147 int
00148 uves_correct_badpix_all(cpl_image *master_bias, uves_propertylist *mbias_header,
00149 enum uves_chip chip,
00150 int binx, int biny, int mark_bad)
00151 {
00152 int badpixels_cleaned = -1;
00153
00154 int **badmap = NULL;
00155
00156 check( badmap = uves_get_badpix(chip, binx, biny, mark_bad),
00157 "Could not get bad pixel map");
00158
00159 check( badpixels_cleaned =
00160 uves_correct_badpix(master_bias, mbias_header, badmap, mark_bad),
00161 "Error cleaning bad pixels");
00162
00163 cleanup:
00164 uves_badmap_free(&badmap);
00165 return badpixels_cleaned;
00166 }
00167
00168
00169
00174
00175 void
00176 uves_badmap_free(int ***badmap)
00177 {
00178 if (badmap != NULL)
00179 {
00180 if (*badmap != NULL)
00181 {
00182 int row;
00183 for (row = 0;
00184 (*badmap)[row][0] != -1;
00185 row++)
00186 {
00187 cpl_free((*badmap)[row]);
00188 }
00189 cpl_free((*badmap)[row]);
00190
00191 cpl_free(*badmap);
00192 *badmap = NULL;
00193 }
00194 }
00195 }
00196
00197
00203
00204 static int **
00205 dup_map(int badmap[][4])
00206 {
00207 int **map = NULL;
00208 int row;
00209 bool finished = false;
00210
00211
00212
00213
00214 for (row = 0; !finished; row++)
00215 {
00216 map = cpl_realloc(map, (row+1)*sizeof(int *));
00217
00218
00219 map[row] = cpl_calloc(4, sizeof(int));
00220 map[row][0] = badmap[row][0];
00221 map[row][1] = badmap[row][1];
00222 map[row][2] = badmap[row][2];
00223 map[row][3] = badmap[row][3];
00224
00225 finished = (badmap[row][0] == -1);
00226 }
00227
00228 return map;
00229 }
00230
00231
00232
00246
00247 int **
00248 uves_get_badpix(enum uves_chip chip,
00249 int binx, int biny, int mark_bad)
00250 {
00251 int **map = NULL;
00252
00253 if (chip == UVES_CHIP_REDL)
00254 {
00255 if (binx == 1 && biny == 1)
00256 {
00257 int badmap[][4] = {{1,4,2088,4},
00258 {1,63,2282,63},
00259 {1,108,1778,108},
00260 {1,176,2443,176},
00261 {1,196,2021,196},
00262 {1,285,1974,285},
00263 {1,352,1942,352},
00264 {-1,-1,-1,-1}};
00265
00266 map = dup_map(badmap);
00267 }
00268 else if (binx == 1 && biny == 2)
00269 {
00270 int badmap[][4] = {{1,4,1044,4},
00271 {1,63,1141,63},
00272 {1,108,894,108},
00273 {1,176,1222,176},
00274 {1,196,1011,196},
00275 {1,285,988,285},
00276 {1,352,971,352},
00277 {-1,-1,-1,-1}};
00278
00279 map = dup_map(badmap);
00280 }
00281 else if (binx == 2 && biny == 2)
00282 {
00283 int badmap[][4] = {{1,3,1044,3},
00284 {1,33,1141,33},
00285 {1,55,894,56},
00286 {1,89,1222,90},
00287 {1,99,1011,100},
00288 {1,144,988,145},
00289 {1,177,971,178},
00290 {-1,-1,-1,-1}};
00291 map = dup_map(badmap);
00292 }
00293 else if (binx == 2 && biny == 3)
00294 {
00295 int badmap[][4] = {{1,3,696,3},
00296 {1,33,761,33},
00297 {1,55,592,56},
00298 {1,89,814,90},
00299 {1,99,674,100},
00300 {1,144,658,144},
00301 {1,177,647,178},
00302 {-1,-1,-1,-1}};
00303 map = dup_map(badmap);
00304 }
00305 else
00306 {
00307 assure( false, CPL_ERROR_ILLEGAL_INPUT,
00308 "Don't know bad pixel map for %dx%d binning, red, lower chip",
00309 binx, biny);
00310 }
00311 }
00312 else if (chip == UVES_CHIP_REDU)
00313 {
00314
00315
00316
00317 if (binx == 1 && biny ==1)
00318 {
00319 int badmap[][4] = {{1,2030,1268,2033},
00320 {1269,2033,4096,2033},
00321 {1201, 491, 3271, 492},
00322 {-1,-1,-1,-1}};
00323
00324 if (!mark_bad)
00325 {
00326 badmap[2][0] = -1;
00327 badmap[2][1] = -1;
00328 badmap[2][2] = -1;
00329 badmap[2][3] = -1;
00330 }
00331 map = dup_map(badmap);
00332 }
00333 else if (binx == 1 && biny == 2)
00334 {
00335 int badmap[][4] = {{1,2030,634,2033},
00336 {635,2033,2048,2033},
00337 {600, 491,1635, 492},
00338 {-1,-1,-1,-1}};
00339 if (!mark_bad)
00340 {
00341 badmap[2][0] = -1;
00342 badmap[2][1] = -1;
00343 badmap[2][2] = -1;
00344 badmap[2][3] = -1;
00345 }
00346 map = dup_map(badmap);
00347 }
00348 else if (binx == 2 && biny == 2)
00349 {
00350 int badmap[][4] = {{1,1013,634,1016},
00351 {635,1015,2048,1016},
00352 {600, 244,1635, 245},
00353 {-1,-1,-1,-1}};
00354 if (!mark_bad)
00355 {
00356 badmap[2][0] = -1;
00357 badmap[2][1] = -1;
00358 badmap[2][2] = -1;
00359 badmap[2][3] = -1;
00360 }
00361 map = dup_map(badmap);
00362 }
00363 else if (binx == 2 && biny == 3)
00364 {
00365 int badmap[][4] = {{1,1013,423,1016},
00366 {424,1015,1365,1016},
00367 {400, 244,1090, 245},
00368 {-1,-1,-1,-1}};
00369 if (!mark_bad)
00370 {
00371 badmap[2][0] = -1;
00372 badmap[2][1] = -1;
00373 badmap[2][2] = -1;
00374 badmap[2][3] = -1;
00375 }
00376 map = dup_map(badmap);
00377 }
00378 else
00379 {
00380 assure( false, CPL_ERROR_ILLEGAL_INPUT,
00381 "Don't know bad pixel map for %dx%d binning, red, upper chip",
00382 binx, biny);
00383 }
00384 }
00385 else
00386 {
00387
00388 int badmap[][4] = {{-1,-1,-1,-1}};
00389
00390 map = dup_map(badmap);
00391 }
00392
00393 cleanup:
00394 return map;
00395 }
00396
00397
00398
00399
00414
00415 static int
00416 uves_correct_badpix(cpl_image *master_bias, uves_propertylist *header, int **badmap,
00417 bool mark_bad)
00418 {
00419 int ncorrect = 0;
00420 int xstart, ystart, xend, yend;
00421 int row;
00422 bool finished = false;
00423 int nx, ny;
00424 cpl_mask *image_bad = NULL;
00425 cpl_binary*image_bpm = NULL;
00426
00427 assure( cpl_image_get_type(master_bias) == CPL_TYPE_DOUBLE,
00428 CPL_ERROR_UNSUPPORTED_MODE,
00429 "Image type must be double. It is %s",
00430 uves_tostring_cpl_type(cpl_image_get_type(master_bias)));
00431
00432 image_bad = irplib_image_get_bpm(master_bias);
00433 image_bpm = irplib_mask_get_data(image_bad);
00434
00435 nx = cpl_image_get_size_x(master_bias);
00436 ny = cpl_image_get_size_y(master_bias);
00437
00438 row = 0;
00439 while (!finished)
00440 {
00441 xstart = badmap[row][0];
00442 ystart = badmap[row][1];
00443 xend = badmap[row][2];
00444 yend = badmap[row][3];
00445
00446 if (xstart > 0)
00447 {
00448 int ylow, yhigh;
00449 int x, y;
00450
00451 assure( 1 <= xstart && xstart <= nx &&
00452 1 <= xend && xend <= nx &&
00453 1 <= ystart && ystart <= ny &&
00454 1 <= yend && yend <= ny, CPL_ERROR_ILLEGAL_INPUT,
00455 "Illegal window (%d, %d) - (%d, %d). Image size = %dx%d",
00456 xstart, ystart, xend, yend, nx, ny);
00457
00458 if ( ystart < 3 )
00459 {
00460 assure( yend + 2 <= ny, CPL_ERROR_ILLEGAL_INPUT,
00461 "Too large range in y: %d - %d", ystart, yend);
00462
00463 ylow = yend + 1;
00464 yhigh = yend + 2;
00465 }
00466 else if (yend > ny - 3 )
00467 {
00468 assure( ystart - 2 >= 1, CPL_ERROR_ILLEGAL_INPUT,
00469 "Too large range in y: %d - %d", ystart, yend);
00470
00471 ylow = ystart - 2;
00472 yhigh = ystart - 1;
00473 }
00474 else
00475 {
00476 ylow = ystart - 2;
00477 yhigh = yend + 2;
00478 }
00479
00480 uves_msg("Correcting window (%d, %d)-(%d, %d)", xstart, ystart, xend, yend);
00481
00482 for (x = xstart; x <= xend; x++) {
00483 for (y = ystart; y <= yend; y++) {
00484 int pis_rejected;
00485 double i1, i2;
00486
00487 if (mark_bad)
00488 {
00489
00490
00491
00492 image_bpm[(x-1) + (y-1)*nx] = CPL_BINARY_1;
00493 }
00494 else
00495
00496 {
00497 double *master_bias_data;
00498
00499 i1 = cpl_image_get(master_bias, x, ylow , &pis_rejected);
00500 i2 = cpl_image_get(master_bias, x, yhigh, &pis_rejected);
00501
00502
00503
00504
00505
00506
00507 master_bias_data = irplib_image_get_data_double(master_bias);
00508 master_bias_data[(x-1) + (y-1)*nx] = (i1+i2)/2;
00509 }
00510 ncorrect += 1;
00511 }
00512 }
00513 }
00514 else
00515 {
00516 finished = true;
00517 }
00518 row++;
00519 }
00520
00521
00522 if (ncorrect > 0)
00523 {
00524 check( uves_pfits_set_badpixcorr(header, "true"),
00525 "Error updating product header");
00526 }
00527
00528 cleanup:
00529 return ncorrect;
00530 }