39 #include "irplib_utils.h"
41 #include "hawki_utils.h"
42 #include "hawki_pfits.h"
43 #include "hawki_dfs.h"
44 #include "hawki_load.h"
45 #include "hawki_save.h"
46 #include "hawki_distortion.h"
52 static int hawki_step_apply_dist_create(cpl_plugin *) ;
53 static int hawki_step_apply_dist_exec(cpl_plugin *) ;
54 static int hawki_step_apply_dist_destroy(cpl_plugin *) ;
55 static int hawki_step_apply_dist(cpl_parameterlist *, cpl_frameset *) ;
56 static int hawki_step_apply_dist_save
57 (cpl_imagelist * images,
59 cpl_parameterlist * recipe_parlist,
60 cpl_frameset * used_frameset,
61 cpl_frameset * recipe_frameset);
63 int hawki_step_apply_dist_compute_and_save
64 (cpl_frameset * objects,
65 cpl_frameset * distortion_x,
66 cpl_frameset * distortion_y,
67 cpl_parameterlist * parlist,
68 cpl_frameset * recipe_frameset);
74 static char hawki_step_apply_dist_description[] =
75 "hawki_step_apply_dist -- Distortion correction utility\n"
76 "This recipe accepts three types of frames:\n"
77 "object.fits Images to correct (PRO.CATG = "HAWKI_CALPRO_BKG_SUBTRACTED
")\n"
78 "distmap_x.fits The image with the distortion in X.\n"
79 " (PRO CATG = "HAWKI_CALPRO_DISTORTION_X
")\n"
80 "distmap_y.fits The image with the distortion in Y.\n"
81 " (PRO CATG = "HAWKI_CALPRO_DISTORTION_Y
")\n"
83 "This recipe produces:\n"
84 "hawki_step_apply_dist.fits: the corrected image.\n"
85 " (PRO CATG = "HAWKI_CALPRO_DIST_CORRECTED
")\n" ;
101 int cpl_plugin_get_info(cpl_pluginlist * list)
103 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe ) ;
104 cpl_plugin * plugin = &recipe->interface ;
106 cpl_plugin_init(plugin,
108 HAWKI_BINARY_VERSION,
109 CPL_PLUGIN_TYPE_RECIPE,
110 "hawki_step_apply_dist",
111 "Distortion correction utility",
112 hawki_step_apply_dist_description,
113 "Cesar Enrique Garcia Dabo",
116 hawki_step_apply_dist_create,
117 hawki_step_apply_dist_exec,
118 hawki_step_apply_dist_destroy) ;
120 cpl_pluginlist_append(list, plugin) ;
134 static int hawki_step_apply_dist_create(cpl_plugin * plugin)
136 cpl_recipe * recipe ;
139 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
140 recipe = (cpl_recipe *)plugin ;
144 recipe->parameters = cpl_parameterlist_new() ;
157 static int hawki_step_apply_dist_exec(cpl_plugin * plugin)
159 cpl_recipe * recipe ;
162 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
163 recipe = (cpl_recipe *)plugin ;
169 return hawki_step_apply_dist(recipe->parameters, recipe->frames) ;
179 static int hawki_step_apply_dist_destroy(cpl_plugin * plugin)
181 cpl_recipe * recipe ;
184 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
185 recipe = (cpl_recipe *)plugin ;
188 cpl_parameterlist_delete(recipe->parameters) ;
200 static int hawki_step_apply_dist(
201 cpl_parameterlist * parlist,
202 cpl_frameset * frameset)
204 cpl_frameset * objframes;
205 cpl_frameset * distortion_x;
206 cpl_frameset * distortion_y;
212 cpl_msg_error(__func__,
"Cannot identify RAW and CALIB frames") ;
217 cpl_msg_info(__func__,
"Identifying objects");
219 (frameset, HAWKI_CALPRO_BKG_SUBTRACTED);
220 if (objframes == NULL)
222 cpl_msg_error(__func__,
"No object frames provided (%s)",
223 HAWKI_CALPRO_BKG_SUBTRACTED);
228 cpl_msg_info(__func__,
"Identifying distortion maps");
230 (frameset, HAWKI_CALPRO_DISTORTION_X);
232 (frameset, HAWKI_CALPRO_DISTORTION_Y);
233 if(cpl_frameset_get_size(distortion_x) != 1 &&
234 cpl_frameset_get_size(distortion_y) != 1 )
236 cpl_msg_error(__func__,
"One X-distortion frame and one Y-distortion "
237 "must be provided (%s, %s)",
238 HAWKI_CALPRO_DISTORTION_X, HAWKI_CALPRO_DISTORTION_Y);
239 cpl_frameset_delete(objframes);
240 cpl_frameset_delete(distortion_x);
241 cpl_frameset_delete(distortion_y);
246 if(hawki_step_apply_dist_compute_and_save
247 (objframes, distortion_x, distortion_y, parlist, frameset) == -1)
249 cpl_msg_error(__func__,
"Could not correct the frames");
250 cpl_frameset_delete(objframes);
251 cpl_frameset_delete(distortion_x);
252 cpl_frameset_delete(distortion_y);
257 cpl_frameset_delete(objframes);
258 cpl_frameset_delete(distortion_x);
259 cpl_frameset_delete(distortion_y);
262 if (cpl_error_get_code())
264 cpl_msg_error(__func__,
265 "HAWK-I pipeline could not recover from previous errors");
281 int hawki_step_apply_dist_compute_and_save
282 (cpl_frameset * objects,
283 cpl_frameset * distortion_x,
284 cpl_frameset * distortion_y,
285 cpl_parameterlist * parlist,
286 cpl_frameset * recipe_frameset)
288 const cpl_frame * distframe_x;
289 const cpl_frame * distframe_y;
292 cpl_image * first_image;
299 cpl_errorstate error_prevstate = cpl_errorstate_get();
302 cpl_msg_info(__func__,
"Creating the distortion maps");
303 distframe_x = cpl_frameset_get_first_const(distortion_x);
304 distframe_y = cpl_frameset_get_first_const(distortion_y);
306 nx = cpl_image_get_size_x(first_image);
307 ny = cpl_image_get_size_y(first_image);
308 cpl_image_delete(first_image);
309 dist_x = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_image *));
310 dist_y = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_image *));
311 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
313 hawki_distortion * distortion;
314 dist_x[idet] = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
315 dist_y[idet] = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE) ;
319 (distframe_x, distframe_y , idet+1)) == NULL)
321 cpl_msg_error(__func__,
"Cannot load the distortion for chip %d: %s",
322 idet+1, cpl_error_get_message());
323 for (jdet=0 ; jdet<=idet; jdet++)
325 cpl_image_delete(dist_x[jdet]);
326 cpl_image_delete(dist_y[jdet]);
332 if (hawki_distortion_create_maps_detector
333 (distortion, dist_x[idet], dist_y[idet]))
335 cpl_msg_error(__func__,
"Cannot create the distortion maps") ;
336 for (jdet=0 ; jdet<=idet; jdet++)
338 cpl_image_delete(dist_x[jdet]);
339 cpl_image_delete(dist_y[jdet]);
350 nframes = cpl_frameset_get_size(objects);
351 cpl_msg_info(__func__,
"Number of frames to process: %d", nframes);
352 cpl_msg_indent_more();
353 for(iframe = 0 ; iframe < nframes; ++iframe)
355 cpl_imagelist * object_images;
356 cpl_frame * this_frame;
357 cpl_frameset * used_frameset;
360 cpl_msg_info(__func__,
"Correcting distortion in frame %d",iframe+1);
363 this_frame = cpl_frameset_get_frame(objects, iframe);
366 cpl_msg_error(__func__,
"Could not load input object images");
367 cpl_msg_indent_less();
368 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
370 cpl_image_delete(dist_x[idet]);
371 cpl_image_delete(dist_y[idet]);
381 cpl_msg_error(__func__,
"Cannot correct distortion") ;
382 cpl_msg_indent_less();
383 cpl_imagelist_delete(object_images);
384 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
386 cpl_image_delete(dist_x[idet]);
387 cpl_image_delete(dist_y[idet]);
395 used_frameset = cpl_frameset_new();
396 cpl_frameset_insert(used_frameset, cpl_frame_duplicate(this_frame));
398 (used_frameset,cpl_frame_duplicate
399 (cpl_frameset_get_first_const((distortion_x))));
401 (used_frameset,cpl_frame_duplicate
402 (cpl_frameset_get_first_const((distortion_y))));
405 if (hawki_step_apply_dist_save
406 (object_images, iframe,
407 parlist, used_frameset, recipe_frameset) == -1)
409 cpl_errorstate_set(CPL_ERROR_NONE);
413 cpl_frameset_delete(used_frameset);
414 cpl_imagelist_delete(object_images);
417 cpl_msg_indent_less();
419 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
421 cpl_image_delete(dist_x[idet]);
422 cpl_image_delete(dist_y[idet]);
426 if(!cpl_errorstate_is_equal(error_prevstate))
428 cpl_msg_warning(__func__,
"Probably some data could not be saved. "
429 "Check permisions or disk space");
444 static int hawki_step_apply_dist_save
445 (cpl_imagelist * images,
447 cpl_parameterlist * recipe_parlist,
448 cpl_frameset * used_frameset,
449 cpl_frameset * recipe_frameset)
451 const cpl_frame * raw_reference;
452 cpl_propertylist ** extproplists;
454 cpl_propertylist * inputlist ;
456 const char * recipe_name =
"hawki_step_apply_dist";
460 raw_reference = irplib_frameset_get_first_from_group
461 (used_frameset, CPL_FRAME_GROUP_RAW);
464 cpl_msg_indent_more();
465 extproplists = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_propertylist*));
466 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
470 (cpl_frame_get_filename(raw_reference), idet+1);
473 extproplists[idet] = cpl_propertylist_new();
476 inputlist = cpl_propertylist_load_regexp(
477 cpl_frame_get_filename(raw_reference), ext_nb,
478 HAWKI_HEADER_EXT_FORWARD, 0);
479 cpl_propertylist_append(extproplists[idet], inputlist);
480 cpl_propertylist_delete(inputlist);
481 inputlist = cpl_propertylist_load_regexp(
482 cpl_frame_get_filename(raw_reference), ext_nb,
483 HAWKI_HEADER_WCS, 0);
484 cpl_propertylist_append(extproplists[idet], inputlist);
485 cpl_propertylist_delete(inputlist);
489 snprintf(filename, 256,
"hawki_step_apply_dist_%03d.fits", iserie+1);
496 HAWKI_CALPRO_DIST_CORRECTED,
497 HAWKI_PROTYPE_DIST_CORRECTED,
499 (
const cpl_propertylist**)extproplists,
502 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
503 cpl_propertylist_delete(extproplists[idet]) ;
504 cpl_free(extproplists) ;
505 cpl_msg_indent_less();
510 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
511 cpl_propertylist_delete(extproplists[idet]) ;
513 cpl_free(extproplists) ;
514 cpl_msg_indent_less();