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 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039
00040 #include "omega_dfs.h"
00041 #include "omega_stats.h"
00042 #include "omega_utils.h"
00043 #include "omega_bpm.h"
00044 #include "omega_trim.h"
00045
00062
00065
00083
00084 cpl_image * clean_deadpix_median(cpl_image *dirty, cpl_mask *dead, int boxsize)
00085 {
00086
00087 int i = 0;
00088 int j = 0;
00089 int k = 0;
00090 int l = 0;
00091 int nx = 0;
00092 int ny = 0;
00093 int lx = 0;
00094 int ly = 0;
00095 int pixelpos = 0;
00096 int numgood = 0;
00097 double replace = 0.0;
00098 float *goodpix;
00099 float *cleandata;
00100 float *dirtydata;
00101
00102 const char *_id = "clean_deadpix_median";
00103
00104 cpl_binary *deaddata;
00105 cpl_image *cleaned;
00106
00107
00108 if(dirty == NULL){
00109 cpl_msg_error(_id,"Null input image");
00110 return NULL;
00111 }
00112
00113 lx = cpl_image_get_size_x(dirty);
00114 ly = cpl_image_get_size_y(dirty);
00115
00116 dirtydata = (float *)cpl_image_get_data(dirty);
00117
00118 cleaned = cpl_image_new(lx,ly,CPL_TYPE_FLOAT);
00119 cleandata = (float *)cpl_image_get_data(cleaned);
00120
00121 goodpix = calloc((boxsize+1)*(boxsize+1), sizeof(float));
00122 if(goodpix == NULL){
00123 cpl_msg_error(_id,"Error in allocating memory");
00124 cpl_image_delete(cleaned);
00125 return NULL;
00126 }
00127
00128 boxsize = boxsize/2;
00129
00130 deaddata = cpl_mask_get_data(dead);
00131
00132
00133 for(j=0; j<ly; j++){
00134 for(i=0; i<lx; i++){
00135 pixelpos = i + j*lx;
00136
00137 if(deaddata[pixelpos] == CPL_BINARY_1){
00138 cleandata[pixelpos] = dirtydata[pixelpos];
00139 }
00140 else{
00141 numgood = 0;
00142 replace = 0.0;
00143 for(l=-boxsize; l<=boxsize; l++){
00144 ny = j+l;
00145 for(k=-boxsize; k<=boxsize; k++){
00146 nx = i+k;
00147
00148
00149
00150 if((nx >= 0) && (nx < lx) &&
00151 (ny >= 0) && (ny < ly) &&
00152 (deaddata[nx+ny*lx] == CPL_BINARY_1)){
00153
00154 goodpix[numgood] = (double)dirtydata[nx+ny*lx];
00155 numgood++;
00156 }
00157 }
00158 }
00159
00160 if(numgood <=1){
00161 cleandata[pixelpos] = (float)0;
00162 }
00163 else{
00164
00165 cleandata[pixelpos] = get_median_float(goodpix, numgood);
00166 }
00167 }
00168 }
00169 }
00170
00171 free(goodpix);
00172
00173 return cleaned;
00174
00175 }
00176
00177
00190
00191 cpl_mask *makebpm(const cpl_frame *hot, const cpl_frame *cold, int cxn)
00192 {
00193 cpl_image *hot_img;
00194 cpl_image *cold_img;
00195 cpl_mask *bpm;
00196 cpl_mask *mask;
00197
00198 if((hot == NULL) && (cold == NULL))
00199 return NULL;
00200
00201
00202 if((hot != NULL) && (cold != NULL)){
00203 hot_img = cpl_image_load(cpl_frame_get_filename(hot), CPL_TYPE_INT, 0, cxn);
00204 cold_img = cpl_image_load(cpl_frame_get_filename(cold), CPL_TYPE_INT, 0, cxn);
00205 bpm = cpl_mask_threshold_image_create(hot_img, 0.5, 1.5) ;
00206 mask = cpl_mask_threshold_image_create(cold_img, 0.5, 1.5) ;
00207
00208 cpl_mask_or(bpm, mask);
00209
00210 cpl_image_delete(hot_img);
00211 cpl_image_delete(cold_img);
00212 cpl_mask_delete(mask);
00213 }
00214 else if((hot == NULL) && (cold != NULL)){
00215 cold_img = cpl_image_load(cpl_frame_get_filename(cold), CPL_TYPE_INT, 0, cxn);
00216 bpm = cpl_mask_threshold_image_create(cold_img, 0.5, 1.5) ;
00217 cpl_image_delete(cold_img);
00218 }
00219 else if((hot != NULL) && (cold == NULL)){
00220 hot_img = cpl_image_load(cpl_frame_get_filename(hot), CPL_TYPE_INT, 0, cxn);
00221 bpm = cpl_mask_threshold_image_create(hot_img, 0.5, 1.5) ;
00222 cpl_image_delete(hot_img);
00223 }
00224 else {
00225 return NULL;
00226 }
00227
00228 return bpm;
00229
00230 }
00231
00244 cpl_mask * create_saturated_map(const cpl_frame *frame, int oc, int ext, cpl_parameterlist *spars)
00245 {
00246
00247 int i = 0;
00248 int npars = 0;
00249 double low = 0.0;
00250 double high = 0.0;
00251 const char *alias = NULL;
00252
00253 cpl_image *trim;
00254 cpl_mask *mask ;
00255 cpl_parameter *par;
00256
00257
00258 if((frame == NULL) || (spars == NULL)){
00259 cpl_msg_error(cpl_func,"Input image or parameter list is NULL");
00260 return NULL;
00261 }
00262
00263
00264 npars = cpl_parameterlist_get_size(spars);
00265 par = cpl_parameterlist_get_first(spars);
00266
00267 for(i=0; i<npars; i++) {
00268 alias = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
00269 if(strcmp ("lt-satu", alias) == 0){
00270 low = cpl_parameter_get_double(par);
00271 }
00272 else if(strcmp ("ht-satu", alias) == 0){
00273 high = cpl_parameter_get_double(par);
00274 }
00275 par = cpl_parameterlist_get_next(spars);
00276 }
00277
00278 trim = TrimOscanCorrect(frame, oc, ext);
00279 if (trim == NULL) {
00280 cpl_msg_error(cpl_func,"Cannot trim image");
00281 return NULL;
00282 }
00283 mask = cpl_mask_threshold_image_create(trim, low, high);
00284 cpl_mask_not(mask);
00285
00286 freeimage(trim);
00287
00288
00289
00290
00291 return mask;
00292 }
00293
00304 cpl_mask * omega_saturated_map(cpl_image *trim, int oc, cpl_parameterlist *spars)
00305 {
00306
00307 int i = 0;
00308 int npars = 0;
00309 double low = 0.0;
00310 double high = 0.0;
00311 const char *alias = NULL;
00312 double imsize=0.0;
00313 double badpixel=0.0;
00314 cpl_mask *mask ;
00315 cpl_parameter *par;
00316
00317
00318 if((trim == NULL) || (spars == NULL))
00319 return NULL;
00320
00321
00322 npars = cpl_parameterlist_get_size(spars);
00323 par = cpl_parameterlist_get_first(spars);
00324
00325 for(i=0; i<npars; i++) {
00326 alias = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
00327 if(strcmp ("lt-satu", alias) == 0){
00328 low = cpl_parameter_get_double(par);
00329 }
00330 else if(strcmp ("ht-satu", alias) == 0){
00331 high = cpl_parameter_get_double(par);
00332 }
00333 par = cpl_parameterlist_get_next(spars);
00334 }
00335
00336 mask = cpl_mask_threshold_image_create(trim, low, high);
00337 cpl_mask_not(mask);
00338
00339 imsize=cpl_image_get_size_x(trim)*cpl_image_get_size_y(trim);
00340 badpixel=cpl_mask_count(mask);
00341
00342 cpl_msg_debug(cpl_func,"Ratio of saturated pixel: %g", badpixel/imsize);
00343
00344 cpl_msg_debug(cpl_func,"Detected %g saturated pixels in image",badpixel);
00345
00346 if(badpixel/imsize > 0.95) {
00347 cpl_msg_warning(cpl_func," %g percent of the pixels are saturated!",
00348 badpixel/imsize*100.);
00349 }
00350
00351 return mask;
00352 }
00353
00354
00369
00370 cpl_image * create_weightframe(cpl_image *flat, cpl_image *bpmsatu,
00371 cpl_mask *cosmic,cpl_mask *satellite)
00372 {
00373
00374 int count = 0;
00375 int nx = 0;
00376 int ny = 0;
00377
00378 cpl_image *mask_img;
00379 cpl_image *weight;
00380 cpl_mask *bpm;
00381
00382
00383 if((flat == NULL) || (bpmsatu == NULL)) {
00384 cpl_msg_debug(cpl_func,"NULL input frame");
00385 return NULL;
00386 }
00387
00388
00389 bpm = cpl_mask_threshold_image_create(bpmsatu, 0.5, 1.5);
00390
00391 if(cosmic != NULL)
00392 cpl_mask_or(bpm, cosmic);
00393
00394 if(satellite != NULL)
00395 cpl_mask_or(bpm, satellite);
00396
00397 nx = cpl_mask_get_size_x(bpm);
00398 ny = cpl_mask_get_size_y(bpm);
00399
00400
00401 cpl_msg_debug(cpl_func,"Weight of % " CPL_SIZE_FORMAT " bad pixels is set to 0", cpl_mask_count(bpm));
00402
00403
00404
00405
00406
00407 cpl_mask_not(bpm);
00408 mask_img = cpl_image_new_from_mask(bpm);
00409
00410
00411 weight = cpl_image_cast(mask_img, CPL_TYPE_FLOAT);
00412
00413 freeimage(mask_img);
00414
00415 cpl_image_multiply(weight, flat);
00416
00417 freemask(bpm);
00418
00419 return weight;
00420
00421 }
00422
00423
00428
00429
00430 cpl_mask *omega_bpm_create(cpl_image *img, cpl_parameterlist *pars)
00431 {
00432 int i,n;
00433 double low,high;
00434 const char *name = "omega_temp.fits";
00435 const char *bname = "omega_back.fits";
00436 const char *text;
00437 cpl_parameter *par;
00438 cpl_image *norm, *back;
00439 cpl_mask *bpm;
00440
00441 if(img == NULL)
00442 return NULL;
00443
00444
00445 par = cpl_parameterlist_get_first(pars);
00446
00447 for(i=0; i<cpl_parameterlist_get_size(pars); i++) {
00448 text = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
00449 if(strcmp("low", text) == 0) {
00450 low = cpl_parameter_get_double(par) ;
00451 n++;
00452 }
00453 else if(strcmp("high", text) == 0){
00454 high = cpl_parameter_get_double(par);
00455 n++;
00456 }
00457 if (n == 2)
00458 break;
00459
00460 par = cpl_parameterlist_get_next(pars);
00461 }
00462
00463
00464
00465 cpl_image_save(img, name, CPL_BPP_IEEE_FLOAT, NULL, CPL_IO_CREATE);
00466
00467
00468 if(omega_create_background(pars, name, bname) != 0){
00469 cpl_msg_warning(cpl_func,"Error in creating background image");
00470 return NULL;
00471 }
00472
00473 if((back = cpl_image_load(bname, CPL_TYPE_FLOAT, 0, 0)) == NULL)
00474 return NULL;
00475
00476 norm = cpl_image_divide_create(img, back);
00477 freeimage(back);
00478
00479 bpm = cpl_mask_threshold_image_create(norm, low, high);
00480 freeimage(norm);
00481
00482
00483
00484 return bpm;
00485
00486 }
00487