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_create(cpl_plugin *);
53 static int rrrecipe_exec(cpl_plugin *);
54 static int rrrecipe_destroy(cpl_plugin *);
55 static int rrrecipe(cpl_frameset *,
const cpl_parameterlist *);
61 static char rrrecipe_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-raw-file.fits " RRRECIPE_RAW
"\n"
66 "and any optional files, e.g.\n"
67 "IIINSTRUMENT-RRRECIPE-flat-file.fits " IIINSTRUMENT_CALIB_FLAT
"\n"
69 "Additionally, it should describe functionality of the expected output."
87 int cpl_plugin_get_info(cpl_pluginlist * list)
89 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
90 cpl_plugin * plugin = &recipe->interface;
92 if (cpl_plugin_init(plugin,
94 IIINSTRUMENT_BINARY_VERSION,
95 CPL_PLUGIN_TYPE_RECIPE,
97 "Short description of rrrecipe",
101 iiinstrument_get_license(),
105 cpl_msg_error(cpl_func,
"Plugin initialization failed");
106 (void)cpl_error_set_where(cpl_func);
110 if (cpl_pluginlist_append(list, plugin)) {
111 cpl_msg_error(cpl_func,
"Error adding plugin to list");
112 (void)cpl_error_set_where(cpl_func);
128 static int rrrecipe_create(cpl_plugin * plugin)
134 if (cpl_error_get_code() != CPL_ERROR_NONE) {
135 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
136 cpl_func, __LINE__, cpl_error_get_where());
137 return (
int)cpl_error_get_code();
140 if (plugin == NULL) {
141 cpl_msg_error(cpl_func,
"Null plugin");
142 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
146 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
147 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
148 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
152 recipe = (cpl_recipe *)plugin;
155 recipe->parameters = cpl_parameterlist_new();
156 if (recipe->parameters == NULL) {
157 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
158 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
163 p = cpl_parameter_new_value(
"iiinstrument.rrrecipe.str_option",
164 CPL_TYPE_STRING,
"the string option",
165 "iiinstrument.rrrecipe",
"NONE");
166 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"stropt");
167 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
168 cpl_parameterlist_append(recipe->parameters, p);
170 p = cpl_parameter_new_value(
"iiinstrument.rrrecipe.file_option",
171 CPL_TYPE_STRING,
"the string option",
172 "iiinstrument.rrrecipe",
"NONE");
173 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"fileopt");
174 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
175 cpl_parameterlist_append(recipe->parameters, p);
178 p = cpl_parameter_new_value(
"iiinstrument.rrrecipe.bool_option",
179 CPL_TYPE_BOOL,
"a flag",
180 "iiinstrument.rrrecipe", TRUE);
181 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"boolopt");
182 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
183 cpl_parameterlist_append(recipe->parameters, p);
185 p = cpl_parameter_new_value(
"iiinstrument.rrrecipe.int_option",
186 CPL_TYPE_INT,
"a flag",
187 "iiinstrument.rrrecipe", 3);
188 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"intopt");
189 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
190 cpl_parameterlist_append(recipe->parameters, p);
192 p = cpl_parameter_new_value(
"iiinstrument.rrrecipe.float_option", CPL_TYPE_DOUBLE,
195 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"floatopt");
196 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
197 cpl_parameterlist_append(recipe->parameters, p);
199 p = cpl_parameter_new_range(
"iiinstrument.rrrecipe.range_option", CPL_TYPE_INT,
200 "This is a value range of type int",
201 "Example", 3, 0, 10);
202 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"rangeopt");
203 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
204 cpl_parameterlist_append(recipe->parameters, p);
206 p = cpl_parameter_new_enum(
"iiinstrument.rrrecipe.enum_option", CPL_TYPE_STRING,
207 "This is an enumeration of type " "string",
208 "Example",
"first", 3,
"first",
"second",
"third");
209 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"enumopt");
210 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
211 cpl_parameterlist_append(recipe->parameters, p);
213 p = cpl_parameter_new_range(
"iiinstrument.rrrecipe.float_range_option", CPL_TYPE_DOUBLE,
214 "This is a value range of type float."
215 " Valid ragne is [-5.5, 5.5]",
216 "Example", 3.5, -5.5, 5.5);
217 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"floatrangeopt");
218 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
219 cpl_parameterlist_append(recipe->parameters, p);
231 static int rrrecipe_exec(cpl_plugin * plugin)
236 cpl_errorstate initial_errorstate = cpl_errorstate_get();
239 if (cpl_error_get_code() != CPL_ERROR_NONE) {
240 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
241 cpl_func, __LINE__, cpl_error_get_where());
242 return (
int)cpl_error_get_code();
245 if (plugin == NULL) {
246 cpl_msg_error(cpl_func,
"Null plugin");
247 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
251 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
252 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
253 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
257 recipe = (cpl_recipe *)plugin;
260 if (recipe->parameters == NULL) {
261 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
262 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
264 if (recipe->frames == NULL) {
265 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
266 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
270 recipe_status = rrrecipe(recipe->frames, recipe->parameters);
273 if (cpl_dfs_update_product_header(recipe->frames)) {
274 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
277 if (!cpl_errorstate_is_equal(initial_errorstate)) {
280 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
283 return recipe_status;
293 static int rrrecipe_destroy(cpl_plugin * plugin)
297 if (plugin == NULL) {
298 cpl_msg_error(cpl_func,
"Null plugin");
299 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
303 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
304 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
305 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
309 recipe = (cpl_recipe *)plugin;
311 cpl_parameterlist_delete(recipe->parameters);
324 static int rrrecipe(cpl_frameset * frameset,
325 const cpl_parameterlist * parlist)
327 const cpl_parameter * param;
328 const char * str_option;
330 const cpl_frame * rawframe;
331 const cpl_frame * flat;
333 cpl_propertylist * plist;
334 cpl_propertylist * applist;
339 cpl_errorstate prestate = cpl_errorstate_get();
343 param = cpl_parameterlist_find_const(parlist,
344 "iiinstrument.rrrecipe.str_option");
345 str_option = cpl_parameter_get_string(param);
348 param = cpl_parameterlist_find_const(parlist,
349 "iiinstrument.rrrecipe.bool_option");
350 bool_option = cpl_parameter_get_bool(param);
352 if (!cpl_errorstate_is_equal(prestate)) {
353 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
354 "Could not retrieve the input "
359 cpl_ensure_code(iiinstrument_dfs_set_groups(frameset) == CPL_ERROR_NONE,
360 cpl_error_get_code());
364 rawframe = cpl_frameset_find_const(frameset, RRRECIPE_RAW);
365 if (rawframe == NULL) {
368 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
369 "SOF does not have any file tagged "
370 "with %s", RRRECIPE_RAW);
373 flat = cpl_frameset_find(frameset, IIINSTRUMENT_CALIB_FLAT);
375 cpl_msg_warning(cpl_func,
"SOF does not have any file tagged with %s",
376 IIINSTRUMENT_CALIB_FLAT);
381 plist = cpl_propertylist_load_regexp(cpl_frame_get_filename(rawframe),
385 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
386 "Could not read the FITS header");
389 qc_param = iiinstrument_pfits_get_dit(plist);
390 cpl_propertylist_delete(plist);
394 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
398 image = cpl_image_load(cpl_frame_get_filename(rawframe), CPL_TYPE_FLOAT, 0,
401 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
402 "Could not load the image");
405 applist = cpl_propertylist_new();
408 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
409 RRRECIPE_OUT_PROCATG);
412 cpl_propertylist_append_double(applist,
"ESO QC QCPARAM", qc_param);
415 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image,
416 CPL_BPP_IEEE_FLOAT,
"rrrecipe", applist,
417 NULL, PACKAGE
"/" PACKAGE_VERSION,
420 (void)cpl_error_set_where(cpl_func);
423 cpl_image_delete(image);
424 cpl_propertylist_delete(applist);
426 return (
int)cpl_error_get_code();