36 #include "iiinstrument_utils.h"
37 #include "iiinstrument_pfits.h"
38 #include "iiinstrument_dfs.h"
46 int cpl_plugin_get_info(cpl_pluginlist * list);
52 static int rrrecipe_calib_create(cpl_plugin *);
53 static int rrrecipe_calib_exec(cpl_plugin *);
54 static int rrrecipe_calib_destroy(cpl_plugin *);
55 static int rrrecipe_calib(cpl_frameset *,
const cpl_parameterlist *);
61 static char rrrecipe_calib_description[] =
62 "This example text is used to describe the recipe.\n"
63 "The description should include the required FITS-files and\n"
64 "their associated tags, e.g.\n"
65 "IIINSTRUMENT-RRRECIPE-CALIB-raw-file.fits " RRRECIPE_CALIB_RAW
"\n"
67 "Additionally, it should describe functionality of the expected output."
85 int cpl_plugin_get_info(cpl_pluginlist * list)
87 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
88 cpl_plugin * plugin = &recipe->interface;
90 if (cpl_plugin_init(plugin,
92 IIINSTRUMENT_BINARY_VERSION,
93 CPL_PLUGIN_TYPE_RECIPE,
95 "Short description of rrrecipe_calib",
96 rrrecipe_calib_description,
99 iiinstrument_get_license(),
100 rrrecipe_calib_create,
102 rrrecipe_calib_destroy)) {
103 cpl_msg_error(cpl_func,
"Plugin initialization failed");
104 (void)cpl_error_set_where(cpl_func);
108 if (cpl_pluginlist_append(list, plugin)) {
109 cpl_msg_error(cpl_func,
"Error adding plugin to list");
110 (void)cpl_error_set_where(cpl_func);
126 static int rrrecipe_calib_create(cpl_plugin * plugin)
132 if (cpl_error_get_code() != CPL_ERROR_NONE) {
133 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
134 cpl_func, __LINE__, cpl_error_get_where());
135 return (
int)cpl_error_get_code();
138 if (plugin == NULL) {
139 cpl_msg_error(cpl_func,
"Null plugin");
140 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
144 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
145 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
146 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
150 recipe = (cpl_recipe *)plugin;
153 recipe->parameters = cpl_parameterlist_new();
154 if (recipe->parameters == NULL) {
155 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
156 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
161 p = cpl_parameter_new_value(
"iiinstrument.rrrecipe_calib.str_option",
162 CPL_TYPE_STRING,
"the string option",
163 "iiinstrument.rrrecipe_calib",NULL);
164 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"stropt");
165 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
166 cpl_parameterlist_append(recipe->parameters, p);
169 p = cpl_parameter_new_value(
"iiinstrument.rrrecipe_calib.bool_option",
170 CPL_TYPE_BOOL,
"a flag",
"iiinstrument.rrrecipe_calib", TRUE);
171 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"boolopt");
172 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
173 cpl_parameterlist_append(recipe->parameters, p);
185 static int rrrecipe_calib_exec(cpl_plugin * plugin)
190 cpl_errorstate initial_errorstate = cpl_errorstate_get();
193 if (cpl_error_get_code() != CPL_ERROR_NONE) {
194 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
195 cpl_func, __LINE__, cpl_error_get_where());
196 return (
int)cpl_error_get_code();
199 if (plugin == NULL) {
200 cpl_msg_error(cpl_func,
"Null plugin");
201 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
205 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
206 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
207 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
211 recipe = (cpl_recipe *)plugin;
214 if (recipe->parameters == NULL) {
215 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
216 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
218 if (recipe->frames == NULL) {
219 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
220 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
224 recipe_status = rrrecipe_calib(recipe->frames, recipe->parameters);
227 if (cpl_dfs_update_product_header(recipe->frames)) {
228 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
231 if (!cpl_errorstate_is_equal(initial_errorstate)) {
234 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
237 return recipe_status;
247 static int rrrecipe_calib_destroy(cpl_plugin * plugin)
251 if (plugin == NULL) {
252 cpl_msg_error(cpl_func,
"Null plugin");
253 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
257 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
258 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
259 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
263 recipe = (cpl_recipe *)plugin;
265 cpl_parameterlist_delete(recipe->parameters);
278 static int rrrecipe_calib(cpl_frameset * frameset,
279 const cpl_parameterlist * parlist)
281 const cpl_parameter * param;
282 const char * str_option;
284 cpl_frameset * rawframes;
285 const cpl_frame * firstframe;
287 cpl_propertylist * plist;
288 cpl_propertylist * applist;
295 cpl_errorstate prestate = cpl_errorstate_get();
299 param = cpl_parameterlist_find_const(parlist,
300 "iiinstrument.rrrecipe_calib.str_option");
301 str_option = cpl_parameter_get_string(param);
304 param = cpl_parameterlist_find_const(parlist,
305 "iiinstrument.rrrecipe_calib.bool_option");
306 bool_option = cpl_parameter_get_bool(param);
308 if (!cpl_errorstate_is_equal(prestate)) {
309 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
310 "Could not retrieve the input "
315 cpl_ensure_code(iiinstrument_dfs_set_groups(frameset) == CPL_ERROR_NONE,
316 cpl_error_get_code());
320 rawframes = cpl_frameset_new();
322 for (i = 0; i<cpl_frameset_get_size(frameset); i++) {
323 const cpl_frame * current_frame;
324 current_frame = cpl_frameset_get_position_const(frameset, i);
325 if(!strcmp(cpl_frame_get_tag(current_frame), RRRECIPE_CALIB_RAW)) {
326 cpl_frame * new_frame = cpl_frame_duplicate(current_frame);
327 cpl_frameset_insert(rawframes, new_frame);
332 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
333 "SOF does not have any file tagged "
334 "with %s", RRRECIPE_CALIB_RAW);
339 firstframe = cpl_frameset_get_first_const(rawframes);
343 plist = cpl_propertylist_load_regexp(cpl_frame_get_filename(firstframe),
347 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
348 "Could not read the FITS header");
351 qc_param = iiinstrument_pfits_get_dit(plist);
352 cpl_propertylist_delete(plist);
356 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
360 image = cpl_image_load(cpl_frame_get_filename(firstframe), CPL_TYPE_FLOAT, 0,
363 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
364 "Could not load the image");
367 applist = cpl_propertylist_new();
370 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
371 RRRECIPE_OUT_CALIB_PROCATG);
374 cpl_propertylist_append_double(applist,
"ESO QC QCPARAM", qc_param);
377 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image,
378 CPL_BPP_IEEE_FLOAT,
"rrrecipe_calib", applist,
379 NULL, PACKAGE
"/" PACKAGE_VERSION,
380 "rrrecipe_calib.fits")) {
382 (void)cpl_error_set_where(cpl_func);
385 cpl_image_delete(image);
386 cpl_propertylist_delete(applist);
388 return (
int)cpl_error_get_code();