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 <string.h>
00037
00038
00039 #include "omega_flats.h"
00040 #include "omega_fftw.h"
00041 #include "omega_dfs.h"
00042 #include "omega_pfits.h"
00043 #include "omega_utils.h"
00044 #include "omega_stats.h"
00045
00073 cpl_image *omega_make_mflat(cpl_image *dome, cpl_image *twil,
00074 cpl_mask *bpmmap, cpl_parameterlist *pars)
00075 {
00076
00077 int dx = 0;
00078 int dy = 0;
00079
00080 double gausfilt = 9.0;
00081
00082 cpl_image *dome_comp;
00083 cpl_image *dome_low_image;
00084 cpl_image *twil_comp;
00085 cpl_parameter *par;
00086
00087 if((dome == NULL) || (twil == NULL) || (pars == NULL))
00088 return NULL;
00089
00090
00091
00092 par = cpl_parameterlist_find(pars, "omega.omega_mflat.GaussianFiltSize") ;
00093 gausfilt = cpl_parameter_get_double(par) ;
00094
00095 par = cpl_parameterlist_find(pars, "omega.omega_mflat.MirrorXpix") ;
00096 dx = cpl_parameter_get_int(par) ;
00097
00098 par = cpl_parameterlist_find(pars, "omega.omega_mflat.MirrorYpix") ;
00099 dy = cpl_parameter_get_int(par) ;
00100
00101
00102 cpl_msg_info (cpl_func,"Working on low spatial frequency component of flat.");
00103 twil_comp = omega_get_spatial_freq(twil, bpmmap, gausfilt, dx, dy);
00104
00105
00106 if (twil_comp == NULL) {
00107 cpl_msg_error (cpl_func, "Error in low spatial frequency calculation");
00108 return NULL;
00109 }
00110
00111
00112 cpl_msg_info (cpl_func,"Working on high spatial frequency component of flat");
00113 dome_low_image = omega_get_spatial_freq(dome, bpmmap, gausfilt, dx, dy);
00114 if (dome_low_image == NULL) {
00115 cpl_msg_error (cpl_func, "Error in high spatial frequency calculation");
00116 freeimage(twil_comp);
00117 return NULL;
00118 }
00119
00120 dome_comp = cpl_image_divide_create(dome, dome_low_image);
00121 if (dome_comp == NULL) {
00122 cpl_msg_error(cpl_func,"Error in division. <%s>",cpl_error_get_message());
00123 freeimage(twil_comp);
00124 freeimage(dome_low_image);
00125 return NULL;
00126 }
00127 freeimage(dome_low_image);
00128
00129
00130 cpl_image_multiply(twil_comp, dome_comp);
00131 freeimage(dome_comp);
00132
00133 if(bpmmap != NULL)
00134 cpl_image_reject_from_mask(twil_comp, bpmmap);
00135
00136
00137
00138 if (cpl_image_normalise(twil_comp, CPL_NORM_MEAN) != CPL_ERROR_NONE){
00139 cpl_msg_error (cpl_func, "Image is NULL in normalization step. <%s>",cpl_error_get_message());
00140 freeimage(twil_comp);
00141 return NULL;
00142 }
00143
00144 return twil_comp;
00145
00146 }
00147
00148
00161 int omega_fringecor(cpl_image *sci, const cpl_image *fringe, const cpl_image *bpm,
00162 const cpl_parameterlist *fpars)
00163 {
00164
00165 int i = 0;
00166 int N = 0;
00167 int npars = 0;
00168 double a = 0.0;
00169 double FF = 0.0;
00170 double hthre = 5.0;
00171 double lthre = 1.5;
00172 double mean_F = 0.0;
00173 double mean_R = 0.0;
00174 double median = 0.0;
00175 double RF = 0.0;
00176 double scale = 5.0;
00177 double stdev = 0.0;
00178 const char *text = NULL;
00179
00180 const cpl_parameter *par;
00181 cpl_stats *stats;
00182 cpl_stats *stats_F;
00183 cpl_stats *stats_R;
00184 cpl_image *image;
00185 cpl_image *ff;
00186
00187 cpl_mask *map, *bpmap;
00188 cpl_mask *bin1;
00189 cpl_mask *bin2;
00190 cpl_error_code code;
00191
00192 if((sci == NULL) || (fringe == NULL) || (bpm == NULL) || (fpars == NULL))
00193 return -1;
00194
00195
00196 par = cpl_parameterlist_get_first_const(fpars);
00197
00198 for(i=0; i<npars; i++) {
00199 text = cpl_parameter_get_alias(par,CPL_PARAMETER_MODE_CLI);
00200 if(strcmp("sigma-fringes", text) == 0) {
00201 scale = cpl_parameter_get_double(par) ;
00202 }
00203 else if(strcmp("lfringes", text) == 0) {
00204 lthre = cpl_parameter_get_double(par) ;
00205 }
00206 else if(strcmp("hfringes", text) == 0) {
00207 hthre = cpl_parameter_get_double(par) ;
00208 }
00209
00210 par = cpl_parameterlist_get_next_const(fpars);
00211
00212 }
00213
00214
00215 stats = cpl_stats_new_from_image(sci, CPL_STATS_ALL);
00216 if(stats == NULL)
00217 return -1;
00218
00219 median = cpl_stats_get_median(stats);
00220 stdev = cpl_stats_get_stdev(stats);
00221
00222
00223 bpmap = cpl_mask_threshold_image_create(bpm, 0.5, 1.5);
00224 map = cpl_mask_threshold_image_create(sci,median-(scale*stdev),median+(scale*stdev));
00225 cpl_mask_and(bpmap, map);
00226 cpl_mask_delete(map);
00227 freestats(stats);
00228
00229
00230
00231 ff = cpl_image_duplicate(fringe);
00232 stats = cpl_stats_new_from_image(ff, CPL_STATS_ALL);
00233 if(stats == NULL)
00234 return -1;
00235
00236 median = cpl_stats_get_median(stats);
00237 stdev = cpl_stats_get_stdev(stats);
00238
00239
00240 bin1 = cpl_mask_threshold_image_create(ff, median-(hthre*stdev), median-(lthre*stdev));
00241 bin2 = cpl_mask_threshold_image_create(ff, median+(lthre*stdev), median+(hthre*stdev));
00242
00243 code = cpl_mask_or(bin1, bin2);
00244 if(code != CPL_ERROR_NONE) {
00245 cpl_msg_debug(cpl_func,"Error in bit-wise OR operation");
00246 freemask(bin1);
00247 freemask(bin2);
00248 freemask(bpmap);
00249 freestats(stats);
00250 return -1;
00251 }
00252
00253 code = cpl_mask_and(bpmap, bin1);
00254 if(code != CPL_ERROR_NONE) {
00255 cpl_msg_debug(cpl_func,"Error in bit-wise AND operation");
00256 freemask(bin1);
00257 freemask(bin2);
00258 freestats(stats);
00259 return -1;
00260 }
00261
00262 freemask(bin1);
00263 freemask(bin2);
00264 freestats(stats);
00265
00266
00267
00268
00269
00270
00271 code = cpl_image_reject_from_mask(ff, bpmap);
00272 if(code != CPL_ERROR_NONE)
00273 cpl_msg_warning(cpl_func,"Cannot set BP in fringe image");
00274
00275 stats_F = cpl_stats_new_from_image(ff, CPL_STATS_ALL);
00276 mean_F = cpl_stats_get_mean(stats_F);
00277
00278
00279 code = cpl_image_reject_from_mask(sci, bpmap);
00280 if(code != CPL_ERROR_NONE)
00281 cpl_msg_warning(cpl_func,"Cannot set BP in image");
00282
00283 stats_R = cpl_stats_new_from_image(sci, CPL_STATS_ALL);
00284 mean_R = cpl_stats_get_mean(stats_R);
00285
00286 N = cpl_stats_get_npix(stats_F);
00287
00288 image = cpl_image_multiply_create(sci, ff);
00289 stats = cpl_stats_new_from_image(image, CPL_STATS_ALL);
00290 RF = cpl_stats_get_flux(stats);
00291
00292 freeimage(image);
00293 freestats(stats);
00294
00295 image = cpl_image_multiply_create(ff, ff);
00296 stats = cpl_stats_new_from_image(image, CPL_STATS_ALL);
00297 FF = cpl_stats_get_flux(stats);
00298
00299 a = (RF - N*mean_R*mean_F) / (FF - N*mean_F*mean_F);
00300
00301
00302 cpl_image_multiply_scalar(ff, a);
00303
00304 cpl_image_subtract(sci, ff);
00305
00306
00307
00308 freeimage(ff);
00309 freeimage(image);
00310 freestats(stats);
00311 freestats(stats_F);
00312 freestats(stats_R);
00313
00314 return 0;
00315 }
00316
00326 int omega_flatcor(cpl_image *image, const cpl_image *flat, const cpl_image *nsky)
00327 {
00328 cpl_image *mflat;
00329
00330 if((image == NULL) || (flat == NULL))
00331 return -1;
00332
00333
00334 if(nsky != NULL){
00335 mflat = cpl_image_multiply_create(flat, nsky);
00336 cpl_image_divide(image, mflat);
00337 freeimage(mflat);
00338 }
00339 else {
00340 cpl_image_divide(image, flat);
00341 }
00342
00343 return 0;
00344
00345 }
00346
00358 cpl_image *omega_nsky_create(cpl_imagelist *ilist,cpl_image *bpm, int n)
00359 {
00360
00361 int i;
00362 cpl_image *nsky;
00363
00364 if(ilist == NULL)
00365 return NULL;
00366
00367 for(i = 0; i < n; i++){
00368 cpl_image *img = cpl_imagelist_get(ilist, i);
00369 if(bpm != NULL){
00370 cpl_mask *mask = cpl_mask_threshold_image_create(img, 0.5, 1.5);
00371 cpl_image_reject_from_mask(img, mask);
00372 freemask(mask);
00373 }
00374 double median = cpl_image_get_median(img);
00375 cpl_image_divide_scalar(img, median);
00376 }
00377
00378
00379 nsky = cpl_imagelist_collapse_median_create(ilist);
00380
00381
00382 cpl_image_normalise(nsky,CPL_NORM_MEAN);
00383
00384 if(bpm != NULL)
00385 cpl_image_multiply(nsky, bpm);
00386
00387 return nsky;
00388 }
00389
00401 cpl_image *omega_fringes_create(cpl_imagelist *ilist, cpl_image *bpm, int n)
00402 {
00403 cpl_image *fringes;
00404
00405 if(ilist == NULL)
00406 return NULL;
00407
00408
00409 fringes = omega_nsky_create(ilist, bpm, n);
00410 cpl_image_subtract_scalar(fringes, 1.0);
00411
00412 return fringes;
00413
00414 }
00415