00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include "iiinstrument_utils.h"
00037 #include "iiinstrument_pfits.h"
00038 #include "iiinstrument_dfs.h"
00039 #include <cpl.h>
00040 #include <string.h>
00041
00042
00043
00044
00045
00046 int cpl_plugin_get_info(cpl_pluginlist * list);
00047
00048
00049
00050
00051
00052 static int rrrecipe_calib_create(cpl_plugin *);
00053 static int rrrecipe_calib_exec(cpl_plugin *);
00054 static int rrrecipe_calib_destroy(cpl_plugin *);
00055 static int rrrecipe_calib(cpl_frameset *, const cpl_parameterlist *);
00056
00057
00058
00059
00060
00061 static char rrrecipe_calib_description[] =
00062 "This example text is used to describe the recipe.\n"
00063 "The description should include the required FITS-files and\n"
00064 "their associated tags, e.g.\n"
00065 "IIINSTRUMENT-RRRECIPE-CALIB-raw-file.fits " RRRECIPE_CALIB_RAW "\n"
00066 "\n"
00067 "Additionally, it should describe functionality of the expected output."
00068 "\n";
00069
00070
00071
00072
00073
00074
00084
00085 int cpl_plugin_get_info(cpl_pluginlist * list)
00086 {
00087 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
00088 cpl_plugin * plugin = &recipe->interface;
00089
00090 if (cpl_plugin_init(plugin,
00091 CPL_PLUGIN_API,
00092 IIINSTRUMENT_BINARY_VERSION,
00093 CPL_PLUGIN_TYPE_RECIPE,
00094 "rrrecipe_calib",
00095 "Short description of rrrecipe_calib",
00096 rrrecipe_calib_description,
00097 "Firstname Lastname",
00098 PACKAGE_BUGREPORT,
00099 iiinstrument_get_license(),
00100 rrrecipe_calib_create,
00101 rrrecipe_calib_exec,
00102 rrrecipe_calib_destroy)) {
00103 cpl_msg_error(cpl_func, "Plugin initialization failed");
00104 (void)cpl_error_set_where(cpl_func);
00105 return 1;
00106 }
00107
00108 if (cpl_pluginlist_append(list, plugin)) {
00109 cpl_msg_error(cpl_func, "Error adding plugin to list");
00110 (void)cpl_error_set_where(cpl_func);
00111 return 1;
00112 }
00113
00114 return 0;
00115 }
00116
00117
00125
00126 static int rrrecipe_calib_create(cpl_plugin * plugin)
00127 {
00128 cpl_recipe * recipe;
00129 cpl_parameter * p;
00130
00131
00132 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00133 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00134 cpl_func, __LINE__, cpl_error_get_where());
00135 return (int)cpl_error_get_code();
00136 }
00137
00138 if (plugin == NULL) {
00139 cpl_msg_error(cpl_func, "Null plugin");
00140 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00141 }
00142
00143
00144 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00145 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00146 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00147 }
00148
00149
00150 recipe = (cpl_recipe *)plugin;
00151
00152
00153 recipe->parameters = cpl_parameterlist_new();
00154 if (recipe->parameters == NULL) {
00155 cpl_msg_error(cpl_func, "Parameter list allocation failed");
00156 cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
00157 }
00158
00159
00160
00161 p = cpl_parameter_new_value("iiinstrument.rrrecipe_calib.str_option",
00162 CPL_TYPE_STRING, "the string option",
00163 "iiinstrument.rrrecipe_calib",NULL);
00164 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "stropt");
00165 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00166 cpl_parameterlist_append(recipe->parameters, p);
00167
00168
00169 p = cpl_parameter_new_value("iiinstrument.rrrecipe_calib.bool_option",
00170 CPL_TYPE_BOOL, "a flag", "iiinstrument.rrrecipe_calib", TRUE);
00171 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "boolopt");
00172 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00173 cpl_parameterlist_append(recipe->parameters, p);
00174
00175 return 0;
00176 }
00177
00178
00184
00185 static int rrrecipe_calib_exec(cpl_plugin * plugin)
00186 {
00187
00188 cpl_recipe * recipe;
00189 int recipe_status;
00190 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00191
00192
00193 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00194 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00195 cpl_func, __LINE__, cpl_error_get_where());
00196 return (int)cpl_error_get_code();
00197 }
00198
00199 if (plugin == NULL) {
00200 cpl_msg_error(cpl_func, "Null plugin");
00201 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00202 }
00203
00204
00205 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00206 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00207 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00208 }
00209
00210
00211 recipe = (cpl_recipe *)plugin;
00212
00213
00214 if (recipe->parameters == NULL) {
00215 cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
00216 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00217 }
00218 if (recipe->frames == NULL) {
00219 cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
00220 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00221 }
00222
00223
00224 recipe_status = rrrecipe_calib(recipe->frames, recipe->parameters);
00225
00226
00227 if (cpl_dfs_update_product_header(recipe->frames)) {
00228 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
00229 }
00230
00231 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00232
00233
00234 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00235 }
00236
00237 return recipe_status;
00238 }
00239
00240
00246
00247 static int rrrecipe_calib_destroy(cpl_plugin * plugin)
00248 {
00249 cpl_recipe * recipe;
00250
00251 if (plugin == NULL) {
00252 cpl_msg_error(cpl_func, "Null plugin");
00253 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00254 }
00255
00256
00257 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00258 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00259 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00260 }
00261
00262
00263 recipe = (cpl_recipe *)plugin;
00264
00265 cpl_parameterlist_delete(recipe->parameters);
00266
00267 return 0;
00268 }
00269
00270
00277
00278 static int rrrecipe_calib(cpl_frameset * frameset,
00279 const cpl_parameterlist * parlist)
00280 {
00281 const cpl_parameter * param;
00282 const char * str_option;
00283 int bool_option;
00284 cpl_frameset * rawframes;
00285 const cpl_frame * firstframe;
00286 double qc_param;
00287 cpl_propertylist * plist;
00288 cpl_propertylist * applist;
00289 cpl_image * image;
00290 int nraw;
00291 int i;
00292
00293
00294
00295 cpl_errorstate prestate = cpl_errorstate_get();
00296
00297
00298
00299 param = cpl_parameterlist_find_const(parlist,
00300 "iiinstrument.rrrecipe_calib.str_option");
00301 str_option = cpl_parameter_get_string(param);
00302
00303
00304 param = cpl_parameterlist_find_const(parlist,
00305 "iiinstrument.rrrecipe_calib.bool_option");
00306 bool_option = cpl_parameter_get_bool(param);
00307
00308 if (!cpl_errorstate_is_equal(prestate)) {
00309 return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00310 "Could not retrieve the input "
00311 "parameters");
00312 }
00313
00314
00315 cpl_ensure_code(iiinstrument_dfs_set_groups(frameset) == CPL_ERROR_NONE,
00316 cpl_error_get_code());
00317
00318
00319
00320 rawframes = cpl_frameset_new();
00321 nraw = 0;
00322 for (i = 0; i<cpl_frameset_get_size(frameset); i++) {
00323 const cpl_frame * current_frame;
00324 current_frame = cpl_frameset_get_position_const(frameset, i);
00325 if(!strcmp(cpl_frame_get_tag(current_frame), RRRECIPE_CALIB_RAW)) {
00326 cpl_frame * new_frame = cpl_frame_duplicate(current_frame);
00327 cpl_frameset_insert(rawframes, new_frame);
00328 nraw++;
00329 }
00330 }
00331 if (nraw == 0) {
00332 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00333 "SOF does not have any file tagged "
00334 "with %s", RRRECIPE_CALIB_RAW);
00335 }
00336
00337
00338
00339 firstframe = cpl_frameset_get_first_const(rawframes);
00340
00341
00342
00343 plist = cpl_propertylist_load_regexp(cpl_frame_get_filename(firstframe),
00344 0, "ESO DET ", 0);
00345 if (plist == NULL) {
00346
00347 return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00348 "Could not read the FITS header");
00349 }
00350
00351 qc_param = iiinstrument_pfits_get_dit(plist);
00352 cpl_propertylist_delete(plist);
00353
00354
00355
00356 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00357
00358
00359
00360 image = cpl_image_load(cpl_frame_get_filename(firstframe), CPL_TYPE_FLOAT, 0,
00361 0);
00362 if (image == NULL) {
00363 return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00364 "Could not load the image");
00365 }
00366
00367 applist = cpl_propertylist_new();
00368
00369
00370 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
00371 RRRECIPE_OUT_CALIB_PROCATG);
00372
00373
00374 cpl_propertylist_append_double(applist, "ESO QC QCPARAM", qc_param);
00375
00376
00377 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image,
00378 CPL_BPP_IEEE_FLOAT, "rrrecipe_calib", applist,
00379 NULL, PACKAGE "/" PACKAGE_VERSION,
00380 "rrrecipe_calib.fits")) {
00381
00382 (void)cpl_error_set_where(cpl_func);
00383 }
00384
00385 cpl_image_delete(image);
00386 cpl_propertylist_delete(applist);
00387
00388 return (int)cpl_error_get_code();
00389 }