39 #include "irplib_utils.h"
41 #include "sofi_utils.h"
42 #include "sofi_pfits.h"
49 static int sofi_img_domeflat_create(cpl_plugin *);
50 static int sofi_img_domeflat_exec(cpl_plugin *);
51 static int sofi_img_domeflat_destroy(cpl_plugin *);
52 static int sofi_img_domeflat(cpl_parameterlist *, cpl_frameset *);
53 static cpl_image * sofi_img_domeflat_compute(cpl_frameset *);
54 static cpl_image * sofi_img_domeflat_bias(cpl_image *, cpl_image *,
56 static int sofi_img_domeflat_save(cpl_image *, cpl_parameterlist *,
63 static char sofi_img_domeflat_description[] =
64 "sofi_img_domeflat -- SOFI imaging flat-field creation.\n"
65 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
66 "raw-file.fits "SOFI_IMG_DOMEFLAT_RAW
"\n";
81 int cpl_plugin_get_info(cpl_pluginlist * list)
83 cpl_recipe * recipe = cpl_calloc(1,
sizeof(*recipe));
84 cpl_plugin * plugin = &recipe->interface;
86 cpl_plugin_init(plugin,
89 CPL_PLUGIN_TYPE_RECIPE,
92 sofi_img_domeflat_description,
96 sofi_img_domeflat_create,
97 sofi_img_domeflat_exec,
98 sofi_img_domeflat_destroy);
100 cpl_pluginlist_append(list, plugin);
115 static int sofi_img_domeflat_create(cpl_plugin * plugin)
120 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
121 recipe = (cpl_recipe *)plugin;
122 else return CPL_ERROR_UNSPECIFIED;
125 recipe->parameters = cpl_parameterlist_new();
140 static int sofi_img_domeflat_exec(cpl_plugin * plugin)
145 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
146 recipe = (cpl_recipe *)plugin;
147 else return CPL_ERROR_UNSPECIFIED;
149 return sofi_img_domeflat(recipe->parameters, recipe->frames);
159 static int sofi_img_domeflat_destroy(cpl_plugin * plugin)
164 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
165 recipe = (cpl_recipe *)plugin;
166 else return CPL_ERROR_UNSPECIFIED;
168 cpl_parameterlist_delete(recipe->parameters);
180 static int sofi_img_domeflat(
181 cpl_parameterlist * parlist,
182 cpl_frameset * framelist)
184 cpl_frameset * flatframes;
189 cpl_msg_error(cpl_func,
"Cannot identify RAW and CALIB frames");
190 return CPL_ERROR_UNSPECIFIED;
195 SOFI_IMG_DOMEFLAT_RAW)) == NULL) {
196 cpl_msg_error(cpl_func,
"Cannot find flat frames in the input list");
197 return CPL_ERROR_UNSPECIFIED;
201 cpl_msg_info(cpl_func,
"Compute dome flat");
202 if ((flat = sofi_img_domeflat_compute(flatframes)) == NULL) {
203 cpl_msg_error(cpl_func,
"Cannot compute dome flat");
204 cpl_frameset_delete(flatframes);
205 return CPL_ERROR_UNSPECIFIED;
207 cpl_frameset_delete(flatframes);
210 cpl_msg_info(cpl_func,
"Save the product");
211 if (sofi_img_domeflat_save(flat, parlist, framelist) == -1) {
212 cpl_msg_error(cpl_func,
"Cannot save the product");
213 cpl_image_delete(flat);
214 return CPL_ERROR_UNSPECIFIED;
216 cpl_image_delete(flat);
229 static cpl_image * sofi_img_domeflat_compute(cpl_frameset * fset)
231 cpl_imagelist * ilist;
240 if (fset == NULL)
return NULL;
241 if (cpl_frameset_get_size(fset) != 8)
return NULL;
250 cpl_msg_info(cpl_func,
"Load the frames");
251 if ((ilist = cpl_imagelist_load_frameset(fset, CPL_TYPE_FLOAT, 0,
253 cpl_msg_error(cpl_func,
"Cannot load the frames");
258 on = cpl_image_add_create( cpl_imagelist_get(ilist, 3),
259 cpl_imagelist_get(ilist, 4));
260 cpl_image_divide_scalar(on, 2);
261 on_ma = cpl_image_add_create( cpl_imagelist_get(ilist, 2),
262 cpl_imagelist_get(ilist, 5));
263 cpl_image_divide_scalar(on_ma, 2);
266 if ((bias = sofi_img_domeflat_bias(on, on_ma, x1, x2, x3, x4)) == NULL) {
267 cpl_msg_error(cpl_func,
"Cannot compute the bias of the on frames");
268 cpl_image_delete(on);
269 cpl_image_delete(on_ma);
270 cpl_imagelist_delete(ilist);
273 cpl_image_delete(on_ma);
276 cpl_image_subtract(on, bias);
277 cpl_image_delete(bias);
280 off = cpl_image_add_create( cpl_imagelist_get(ilist, 0),
281 cpl_imagelist_get(ilist, 7));
282 cpl_image_divide_scalar(off, 2);
283 off_ma =cpl_image_add_create( cpl_imagelist_get(ilist, 1),
284 cpl_imagelist_get(ilist, 6));
285 cpl_image_divide_scalar(off_ma, 2);
286 cpl_imagelist_delete(ilist);
289 if ((bias = sofi_img_domeflat_bias(off, off_ma, x1, x2, x3,
291 cpl_msg_error(cpl_func,
"Cannot compute the bias of the off frames");
292 cpl_image_delete(on);
293 cpl_image_delete(off);
294 cpl_image_delete(off_ma);
297 cpl_image_delete(off_ma);
300 cpl_image_subtract(off, bias);
301 cpl_image_delete(bias);
304 cpl_image_subtract(on, off);
305 cpl_image_delete(off);
308 cpl_image_normalise(on, CPL_NORM_MEAN);
326 static cpl_image * sofi_img_domeflat_bias(
344 if (in == NULL)
return NULL;
345 if (in_mask == NULL)
return NULL;
348 nx = cpl_image_get_size_x(in);
349 ny = cpl_image_get_size_y(in);
352 a_1d = cpl_image_collapse_window_create(in, x1, 1, x2, ny, 1);
353 cpl_image_divide_scalar(a_1d, x2-x1+1);
354 c_1d = cpl_image_collapse_window_create(in_mask, x1, 1, x2, ny, 1);
355 cpl_image_divide_scalar(c_1d, x2-x1+1);
356 b_1d = cpl_image_collapse_window_create(in_mask, x3, 1, x4, ny, 1);
357 cpl_image_divide_scalar(b_1d, x4-x3+1);
358 cpl_image_subtract(a_1d, c_1d);
359 cpl_image_delete(c_1d);
360 cpl_image_add(a_1d, b_1d);
361 cpl_image_delete(b_1d);
364 a_2d = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
365 pa_2d = cpl_image_get_data_float(a_2d);
366 pa_1d = cpl_image_get_data_float(a_1d);
367 for (j=0 ; j<ny ; j++) {
368 for (i=0 ; i<nx ; i++) {
369 pa_2d[i+j*nx] = pa_1d[j];
372 cpl_image_delete(a_1d);
386 static int sofi_img_domeflat_save(
388 cpl_parameterlist * parlist,
391 cpl_propertylist * plist;
392 cpl_propertylist * paflist;
393 cpl_propertylist * qclist;
394 const cpl_frame * ref_frame;
398 qclist = cpl_propertylist_new();
401 irplib_dfs_save_image(set,
407 SOFI_IMG_DOMEFLAT_RES,
410 PACKAGE
"/" PACKAGE_VERSION,
411 "sofi_img_domeflat.fits");
414 ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
417 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
419 cpl_msg_error(cpl_func,
"getting header from reference frame");
420 cpl_propertylist_delete(qclist);
421 return CPL_ERROR_UNSPECIFIED;
425 paflist = cpl_propertylist_new();
426 cpl_propertylist_copy_property_regexp(paflist, plist,
427 "^(DATE-OBS|ESO DET CHIP NAME|ARCFILE|ESO TPL ID|ESO DET MODE NAME|"
428 "ESO DET NCORRS NAME|ESO DET RSPEED|ESO DET DIT)$", 0);
429 cpl_propertylist_delete(plist);
433 cpl_propertylist_delete(qclist);
436 cpl_dfs_save_paf(
"SOFI",
439 "sofi_img_domeflat.paf");
440 cpl_propertylist_delete(paflist);