38 #include "gravi_utils.h"
39 #include "gravi_pfits.h"
40 #include "gravi_dfs.h"
41 #include "gravi_image.h"
47 static int gravi_image_reconstruct_create(cpl_plugin *);
48 static int gravi_image_reconstruct_exec(cpl_plugin *);
49 static int gravi_image_reconstruct_destroy(cpl_plugin *);
50 static int gravi_image_reconstruct(cpl_frameset *,
const cpl_parameterlist *);
56 static char gravi_image_reconstruct_description[] =
57 "This recipe launch a Yorick batch executing mira-script.i to process an\n"
58 "input OIFITS file to produce an image in fits file.\n"
59 "The input frame is an OIFITS file :\n"
60 "GRAVI-GRAVI_MIRA-input-file.oifits " GRAVI_MIRA_INPUT_PROCATG
"\n"
61 "and the recipe generates a fits image\n"
62 "GRAVI-GRAVI_MIRA-image-file.fits " GRAVI_MIRA_OUTPUT_PROCATG
"\n";
79 int cpl_plugin_get_info(cpl_pluginlist * list)
81 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
82 cpl_plugin * plugin = &recipe->interface;
84 if (cpl_plugin_init(plugin,
87 CPL_PLUGIN_TYPE_RECIPE,
88 "gravi_image_reconstruct",
89 "Processess an OIFITS file to reconstruct an image",
90 gravi_image_reconstruct_description,
94 gravi_image_reconstruct_create,
95 gravi_image_reconstruct_exec,
96 gravi_image_reconstruct_destroy)) {
97 cpl_msg_error(cpl_func,
"Plugin initialization failed");
98 (void)cpl_error_set_where(cpl_func);
102 if (cpl_pluginlist_append(list, plugin)) {
103 cpl_msg_error(cpl_func,
"Error adding plugin to list");
104 (void)cpl_error_set_where(cpl_func);
120 static int gravi_image_reconstruct_create(cpl_plugin * plugin)
126 if (cpl_error_get_code() != CPL_ERROR_NONE) {
127 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
128 cpl_func, __LINE__, cpl_error_get_where());
129 return (
int)cpl_error_get_code();
132 if (plugin == NULL) {
133 cpl_msg_error(cpl_func,
"Null plugin");
134 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
138 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
139 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
140 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
144 recipe = (cpl_recipe *)plugin;
147 recipe->parameters = cpl_parameterlist_new();
148 if (recipe->parameters == NULL) {
149 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
150 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
163 p = cpl_parameter_new_value(
"gravi.gravi_image_reconstruct.pixelsize",
164 CPL_TYPE_DOUBLE,
"size of the pixel (milliarcseconds)",
165 "gravi.gravi_image_reconstruct", 0.2);
166 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"pixelsize");
167 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
168 cpl_parameterlist_append(recipe->parameters, p);
171 p = cpl_parameter_new_value(
"gravi.gravi_image_reconstruct.dim",
172 CPL_TYPE_INT,
"number of pixels per side of the image",
173 "gravi.gravi_image_reconstruct", 100);
174 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"dim");
175 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
176 cpl_parameterlist_append(recipe->parameters, p);
179 p = cpl_parameter_new_value(
"gravi.gravi_image_reconstruct.regul",
180 CPL_TYPE_STRING,
"name of regularization method",
181 "gravi.gravi_image_reconstruct",
"totvar");
182 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"regul");
183 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
184 cpl_parameterlist_append(recipe->parameters, p);
187 p = cpl_parameter_new_value(
"gravi.gravi_image_reconstruct.regul_mu",
188 CPL_TYPE_DOUBLE,
"global regularization weight",
189 "gravi.gravi_image_reconstruct", 1E4);
190 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"regul_mu");
191 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
192 cpl_parameterlist_append(recipe->parameters, p);
195 p = cpl_parameter_new_value(
"gravi.gravi_image_reconstruct.maxeval",
196 CPL_TYPE_INT,
"maximum number of evaluations of the objective function",
197 "gravi.gravi_image_reconstruct", 2000);
198 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"maxeval");
199 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
200 cpl_parameterlist_append(recipe->parameters, p);
203 p = cpl_parameter_new_value(
"gravi.gravi_image_reconstruct.timeout",
204 CPL_TYPE_DOUBLE,
"Maximum execution time of Mira process (s)",
205 "gravi.gravi_image_reconstruct", 60.);
206 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"timeout");
207 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
208 cpl_parameterlist_append(recipe->parameters, p);
220 static int gravi_image_reconstruct_exec(cpl_plugin * plugin)
225 cpl_errorstate initial_errorstate = cpl_errorstate_get();
228 if (cpl_error_get_code() != CPL_ERROR_NONE) {
229 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
230 cpl_func, __LINE__, cpl_error_get_where());
231 return (
int)cpl_error_get_code();
234 if (plugin == NULL) {
235 cpl_msg_error(cpl_func,
"Null plugin");
236 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
240 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
241 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
242 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
246 recipe = (cpl_recipe *)plugin;
249 if (recipe->parameters == NULL) {
250 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
251 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
253 if (recipe->frames == NULL) {
254 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
255 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
259 recipe_status = gravi_image_reconstruct(recipe->frames, recipe->parameters);
262 if (cpl_dfs_update_product_header(recipe->frames)) {
263 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
266 if (!cpl_errorstate_is_equal(initial_errorstate)) {
269 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
272 return recipe_status;
282 static int gravi_image_reconstruct_destroy(cpl_plugin * plugin)
286 if (plugin == NULL) {
287 cpl_msg_error(cpl_func,
"Null plugin");
288 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
292 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
293 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
294 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
298 recipe = (cpl_recipe *)plugin;
300 cpl_parameterlist_delete(recipe->parameters);
313 static int gravi_image_reconstruct(cpl_frameset * frameset,
314 const cpl_parameterlist * parlist)
316 const cpl_frame * rawframe;
318 cpl_propertylist * plist;
319 cpl_propertylist * applist;
321 cpl_frameset * usedframes;
325 cpl_errorstate prestate = cpl_errorstate_get();
329 if (!cpl_errorstate_is_equal(prestate)) {
330 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
331 "Could not retrieve the input "
336 cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
337 cpl_error_get_code());
341 rawframe = cpl_frameset_find_const(frameset, GRAVI_MIRA_INPUT_PROCATG);
342 if (rawframe == NULL) {
345 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
346 "SOF does not have any file tagged "
347 "with %s", GRAVI_MIRA_INPUT_PROCATG);
352 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
354 cpl_msg_set_component_on();
358 image=gravi_image(rawframe, parlist);
362 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_NULL_INPUT,
363 "The gravi_image function return NULL pointer");
366 applist = cpl_propertylist_new();
369 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
370 GRAVI_MIRA_OUTPUT_PROCATG);
373 cpl_propertylist_append_double(applist,
"ESO QC QCPARAM", qc_param);
376 gravi_dfs_set_groups(frameset);
379 usedframes = cpl_frameset_new();
380 cpl_frameset_insert(usedframes, cpl_frame_duplicate(rawframe));
382 cpl_msg_info(cpl_func,
"Writing image_out.fits");
407 cpl_dfs_save_image(frameset, NULL, parlist, usedframes,
408 rawframe, image, CPL_BPP_IEEE_DOUBLE,
409 "gravi_image_reconstruct", applist, NULL,
410 PACKAGE
"/" PACKAGE_VERSION ,
"image_out.fits" );
411 cpl_msg_info(cpl_func,
"Reconstructed image saved in image_out.fits");
414 cpl_image_delete(image);
415 cpl_propertylist_delete(applist);
416 cpl_frameset_delete(usedframes);
418 return (
int)cpl_error_get_code();