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 #include <cpl.h>
00038
00039 #include "hawki_distortion.h"
00040 #include "hawki_bkg.h"
00041 #include "hawki_pfits.h"
00042 #include "hawki_load.h"
00043 #include "hawki_utils.h"
00044
00045
00049
00050
00053
00067
00068 int hawki_bkg_fill_assoc(cpl_frameset * objframes, cpl_propertylist * proplist)
00069 {
00070 int iframe;
00071 int nframes;
00072
00073
00074 nframes = cpl_frameset_get_size(objframes);
00075 for(iframe = 0 ; iframe < nframes ; ++iframe)
00076 {
00077 cpl_frame * this_frame;
00078 cpl_propertylist * this_proplist;
00079 const char * arcfile;
00080 char keystr[256];
00081
00082
00083 this_frame = cpl_frameset_get_frame(objframes, iframe);
00084 this_proplist = cpl_propertylist_load
00085 (cpl_frame_get_filename(this_frame),0);
00086 arcfile = hawki_pfits_get_arcfile(proplist);
00087 snprintf(keystr, 256, "ESO QC BKG ASSOC RAW%d",iframe+1);
00088 cpl_propertylist_append_string(proplist, keystr, arcfile);
00089 cpl_propertylist_delete(this_proplist);
00090 }
00091
00092 return 0;
00093 }
00094
00095
00107
00108 int hawki_bkg_from_objects_median
00109 (const cpl_frameset * objframes, cpl_imagelist * bkg)
00110 {
00111 int idet;
00112
00113
00114 cpl_errorstate prestate = cpl_errorstate_get();
00115
00116
00117 for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00118 {
00119 cpl_imagelist * img_serie;
00120 cpl_image * this_bkg_image;
00121
00122
00123 img_serie = hawki_load_detector(objframes, idet + 1, CPL_TYPE_FLOAT);
00124 if(img_serie== NULL)
00125 {
00126 cpl_msg_error(__func__, "Error reading object image") ;
00127 return -1;
00128 }
00129
00130
00131
00132
00133
00134 if ((this_bkg_image = cpl_imagelist_collapse_median_create(img_serie))
00135 == NULL)
00136 {
00137 cpl_msg_error(__func__, "Cannot compute the median of obj images");
00138 cpl_imagelist_delete(img_serie);
00139 return -1;
00140 }
00141
00142
00143 cpl_imagelist_set(bkg, this_bkg_image, idet);
00144
00145
00146 cpl_imagelist_delete(img_serie);
00147 }
00148
00149
00150 if(!cpl_errorstate_is_equal(prestate))
00151 return -1;
00152 return 0;
00153 }
00154
00155
00168
00169 int hawki_bkg_from_sky_median
00170 (const cpl_frameset * skyframes,
00171 cpl_imagelist * bkg)
00172 {
00173 int idet;
00174
00175
00176 cpl_errorstate prestate = cpl_errorstate_get();
00177
00178
00179 for(idet = 0; idet < HAWKI_NB_DETECTORS ; ++idet)
00180 {
00181 cpl_imagelist * img_serie;
00182 cpl_image * this_bkg_image;
00183
00184
00185 img_serie = hawki_load_detector(skyframes, idet + 1, CPL_TYPE_FLOAT);
00186 if(img_serie== NULL)
00187 {
00188 cpl_msg_error(__func__, "Error reading object image") ;
00189 return -1;
00190 }
00191
00192
00193 if ((this_bkg_image = cpl_imagelist_collapse_median_create(img_serie))
00194 == NULL)
00195 {
00196 cpl_msg_error(__func__, "Cannot compute the median of obj images");
00197 cpl_imagelist_delete(img_serie);
00198 return -1;
00199 }
00200
00201 cpl_imagelist_set(bkg, this_bkg_image, idet);
00202
00203
00204 cpl_imagelist_delete(img_serie);
00205 }
00206
00207
00208 if(!cpl_errorstate_is_equal(prestate))
00209 return -1;
00210 return 0;
00211 }
00212
00213
00229
00230 int hawki_bkg_from_running_mean_detector
00231 (cpl_imagelist * objimages,
00232 const cpl_vector * medians,
00233 int i_target,
00234 int half_width,
00235 int rejlow,
00236 int rejhigh,
00237 cpl_image * bkg)
00238 {
00239 float * bkg_p;
00240 float * curima_p;
00241 cpl_binary * curmask_p;
00242 float ** objimages_p;
00243 cpl_binary ** maskimages_p;
00244 const double * medians_p;
00245 double out;
00246 int from_ima;
00247 int to_ima;
00248 int nima;
00249 int nuse;
00250 int nx;
00251 int ny;
00252
00253 int pos_x;
00254 int pos_y;
00255 int pos;
00256 int i_ima;
00257 int i_win;
00258 int n_win;
00259
00260 cpl_vector * localwin;
00261
00262
00263 bkg_p = cpl_image_get_data_float(bkg);
00264
00265
00266 nima = cpl_imagelist_get_size(objimages);
00267 from_ima = i_target - half_width;
00268 to_ima = i_target + half_width;
00269 if (from_ima<0) from_ima = 0 ;
00270 if (to_ima>(nima-1)) to_ima=nima-1 ;
00271
00272
00273 nuse = to_ima - from_ima ;
00274
00275
00276 nx = cpl_image_get_size_x(bkg);
00277 ny = cpl_image_get_size_y(bkg);
00278
00279
00280 medians_p = cpl_vector_get_data_const(medians);
00281
00282
00283 localwin = cpl_vector_new(nuse) ;
00284
00285
00286 objimages_p = cpl_malloc(nima * sizeof(float *));
00287 maskimages_p = cpl_malloc(nima * sizeof(cpl_binary *));
00288 for(i_ima=from_ima ; i_ima<=to_ima ; i_ima++)
00289 {
00290 objimages_p[i_ima] = cpl_image_get_data_float
00291 (cpl_imagelist_get(objimages, i_ima));
00292 maskimages_p[i_ima] = cpl_mask_get_data
00293 (cpl_image_get_bpm(cpl_imagelist_get(objimages, i_ima)));
00294 }
00295
00296
00297 cpl_image_accept_all(bkg);
00298
00299
00300 for (pos_x=0 ; pos_x<nx ; pos_x++)
00301 {
00302 for (pos_y=0 ; pos_y<ny ; pos_y++)
00303 {
00304
00305 pos = pos_x + pos_y * nx;
00306
00307
00308 cpl_vector_set_size(localwin, nuse);
00309
00310
00311 i_win=0;
00312 for (i_ima=from_ima ; i_ima<=to_ima ; i_ima++)
00313 {
00314 if (i_ima != i_target)
00315 {
00316 curima_p = objimages_p[i_ima];
00317 curmask_p = maskimages_p[i_ima];
00318 if(!curmask_p[pos])
00319 {
00320 cpl_vector_set(localwin, i_win,
00321 (double)curima_p[pos]-medians_p[i_ima]);
00322 i_win++;
00323 }
00324 }
00325 }
00326 n_win = i_win;
00327 if(n_win - rejlow - rejhigh < 1)
00328 {
00329
00330 cpl_msg_debug(__func__,"Pixel %d %d added to the sky bpm",
00331 pos_x, pos_y);
00332 if(cpl_image_reject(bkg, pos_x + 1, pos_y + 1) != CPL_ERROR_NONE)
00333 {
00334 cpl_msg_error(__func__,"Cannot add pixel to sky bpm");
00335 cpl_vector_delete(localwin) ;
00336 cpl_free(objimages_p);
00337 cpl_free(maskimages_p);
00338 return(-1);
00339 }
00340 }
00341 else
00342 {
00343
00344 cpl_vector_set_size(localwin, n_win);
00345
00346 cpl_vector_sort(localwin, 1);
00347
00348 out = 0.0 ;
00349 for (i_win=rejlow ; i_win<n_win - rejhigh; i_win++)
00350 {
00351 out += cpl_vector_get(localwin, i_win);
00352 }
00353
00354 out /= (double)(n_win - rejlow - rejhigh);
00355
00356
00357 bkg_p[pos] = (out+medians_p[i_target]);
00358 }
00359 }
00360 }
00361
00362
00363 cpl_vector_delete(localwin) ;
00364 cpl_free(objimages_p);
00365 cpl_free(maskimages_p);
00366
00367 return 0;
00368 }
00369