37 static int montecarlo_create(cpl_plugin *);
38 static int montecarlo_exec(cpl_plugin *);
39 static int montecarlo_destroy(cpl_plugin *);
40 static int montecarlo(cpl_parameterlist *, cpl_frameset *);
42 static char montecarlo_description[] =
43 "This recipe is used to test the mos_montecarlo_polyfit() function.\n"
44 "It accepts a table with columns x, y, y_err, derives the best polynomial\n"
45 "fit y = p(x), and produces a table with the polynomial 1-sigma accuracy\n"
46 "on the given set of x coordinates.\n"
48 " DO category: Type: Explanation: Required:\n"
49 " TABLE Raw Table to evaluate Y\n\n"
51 " DO category: Data type: Explanation:\n"
52 " MODEL_ERROR FITS image Model error at different x\n\n";
54 #define montecarlo_exit(message) \
56 if (message) cpl_msg_error(recipe, message); \
57 cpl_table_delete(table); \
58 cpl_table_delete(points); \
59 cpl_table_delete(model_error); \
60 cpl_polynomial_delete(p); \
61 cpl_msg_indent_less(); \
65 #define montecarlo_exit_memcheck(message) \
67 if (message) cpl_msg_info(recipe, message); \
68 cpl_table_delete(table); \
69 cpl_table_delete(points); \
70 cpl_table_delete(model_error); \
71 cpl_polynomial_delete(p); \
72 cpl_msg_indent_less(); \
90 cpl_recipe *recipe = cpl_calloc(1,
sizeof *recipe );
91 cpl_plugin *plugin = &recipe->interface;
93 cpl_plugin_init(plugin,
96 CPL_PLUGIN_TYPE_RECIPE,
98 "Test function mos_montecarlo_polyfit()",
99 montecarlo_description,
102 "This file is currently part of the FORS Instrument Pipeline\n"
103 "Copyright (C) 2002-2010 European Southern Observatory\n\n"
104 "This program is free software; you can redistribute it and/or modify\n"
105 "it under the terms of the GNU General Public License as published by\n"
106 "the Free Software Foundation; either version 2 of the License, or\n"
107 "(at your option) any later version.\n\n"
108 "This program is distributed in the hope that it will be useful,\n"
109 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
110 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
111 "GNU General Public License for more details.\n\n"
112 "You should have received a copy of the GNU General Public License\n"
113 "along with this program; if not, write to the Free Software Foundation,\n"
114 "Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n",
119 cpl_pluginlist_append(list, plugin);
135 static int montecarlo_create(cpl_plugin *plugin)
145 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
146 recipe = (cpl_recipe *)plugin;
154 recipe->parameters = cpl_parameterlist_new();
161 p = cpl_parameter_new_value(
"fors.montecarlo.x",
163 "Name of independent variable column",
166 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"x");
167 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
168 cpl_parameterlist_append(recipe->parameters, p);
170 p = cpl_parameter_new_value(
"fors.montecarlo.y",
172 "Name of dependent variable column",
175 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"y");
176 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
177 cpl_parameterlist_append(recipe->parameters, p);
179 p = cpl_parameter_new_value(
"fors.montecarlo.sigma",
181 "Name of error column on dependent variable",
184 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sigma");
185 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
186 cpl_parameterlist_append(recipe->parameters, p);
192 p = cpl_parameter_new_value(
"fors.montecarlo.order",
194 "Order of fitting polynomial",
197 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"order");
198 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
199 cpl_parameterlist_append(recipe->parameters, p);
205 p = cpl_parameter_new_value(
"fors.montecarlo.zero",
207 "Origin of x for fit",
210 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"zero");
211 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
212 cpl_parameterlist_append(recipe->parameters, p);
218 p = cpl_parameter_new_value(
"fors.montecarlo.start",
220 "Start x for evaluation",
223 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"start");
224 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
225 cpl_parameterlist_append(recipe->parameters, p);
231 p = cpl_parameter_new_value(
"fors.montecarlo.end",
233 "End x for evaluation",
236 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"end");
237 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
238 cpl_parameterlist_append(recipe->parameters, p);
244 p = cpl_parameter_new_value(
"fors.montecarlo.step",
246 "x sampling interval",
249 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"step");
250 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
251 cpl_parameterlist_append(recipe->parameters, p);
257 p = cpl_parameter_new_value(
"fors.montecarlo.trials",
259 "Size of statistical sample",
262 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"trials");
263 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
264 cpl_parameterlist_append(recipe->parameters, p);
278 static int montecarlo_exec(cpl_plugin *plugin)
282 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
283 recipe = (cpl_recipe *)plugin;
287 return montecarlo(recipe->parameters, recipe->frames);
299 static int montecarlo_destroy(cpl_plugin *plugin)
303 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
304 recipe = (cpl_recipe *)plugin;
308 cpl_parameterlist_delete(recipe->parameters);
323 static int montecarlo(cpl_parameterlist *parlist, cpl_frameset *frameset)
326 const char *recipe =
"montecarlo";
347 cpl_table *table = NULL;
348 cpl_table *model_error = NULL;
349 cpl_table *points = NULL;
350 cpl_polynomial *p = NULL;
356 char *version =
"0.1";
357 char *table_tag =
"TABLE";
358 char *model_error_tag =
"MODEL_ERROR";
364 cpl_msg_set_indentation(2);
371 cpl_msg_info(recipe,
"Recipe %s configuration parameters:", recipe);
372 cpl_msg_indent_more();
379 montecarlo_exit(
"Invalid polynomial order");
383 montecarlo_exit(
"Invalid interval");
385 if (step >= end - start || step <= 0.0)
386 montecarlo_exit(
"Invalid step");
389 montecarlo_exit(
"At least 2 trials should be performed");
392 if (cpl_error_get_code())
393 montecarlo_exit(
"Failure getting the configuration parameters");
395 ntables = cpl_frameset_count_tags(frameset, table_tag);
397 cpl_msg_error(recipe,
"Missing required input: %s", table_tag);
398 montecarlo_exit(NULL);
401 cpl_msg_error(recipe,
"Too many in input: %s", table_tag);
402 montecarlo_exit(NULL);
406 points = cpl_table_new(cpl_table_get_nrow(table));
408 if (cpl_table_has_column(table, x)) {
409 cpl_table_move_column(points, x, table);
410 if (!cpl_table_has_column(points,
"x")) {
411 cpl_table_name_column(points, x,
"x");
415 cpl_msg_error(recipe,
"Missing column: %s", x);
416 montecarlo_exit(NULL);
419 cpl_table_subtract_scalar(points,
"x", zero);
421 if (cpl_table_has_column(table, y)) {
422 cpl_table_move_column(points, y, table);
423 if (!cpl_table_has_column(points,
"y")) {
424 cpl_table_name_column(points, y,
"y");
428 cpl_msg_error(recipe,
"Missing column: %s", y);
429 montecarlo_exit(NULL);
432 if (sigma[0] !=
'\0') {
433 if (cpl_table_has_column(table, sigma)) {
434 cpl_table_move_column(points, sigma, table);
435 if (!cpl_table_has_column(points,
"y_err")) {
436 cpl_table_name_column(points, sigma,
"y_err");
440 cpl_msg_error(recipe,
"Missing column: %s", sigma);
441 montecarlo_exit(NULL);
445 cpl_table_delete(table); table = NULL;
446 cpl_table_erase_invalid(points);
449 while (start + step*count <= end)
452 model_error = cpl_table_new(count);
453 cpl_table_new_column(model_error,
"x", CPL_TYPE_DOUBLE);
454 cpl_table_fill_column_window_double(model_error,
"x", 0, count, 0.0);
455 xdata = cpl_table_get_data_double(model_error,
"x");
457 for (i = 0; i < count; i++)
458 xdata[i] = start + step*i - zero;
460 p = mos_montecarlo_polyfit(points, model_error, trials, order);
462 if (cpl_error_get_code() != CPL_ERROR_NONE) {
463 cpl_msg_error(recipe,
"%s", cpl_error_get_message());
464 montecarlo_exit(NULL);
467 for (i = 0; i < count; i++)
470 cpl_polynomial_delete(p); p = NULL;
471 cpl_table_delete(points); points = NULL;
474 parlist, recipe, version))
475 montecarlo_exit(NULL);
477 cpl_table_delete(model_error); model_error = NULL;
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
const char * dfs_get_parameter_string(cpl_parameterlist *parlist, const char *name, const cpl_table *defaults)
Reading a recipe string parameter value.
cpl_table * dfs_load_table(cpl_frameset *frameset, const char *category, int ext)
Loading table data of given category.
int dfs_get_parameter_int(cpl_parameterlist *parlist, const char *name, const cpl_table *defaults)
Reading a recipe integer parameter value.
int dfs_save_table(cpl_frameset *frameset, const cpl_table *table, const char *category, cpl_propertylist *header, const cpl_parameterlist *parlist, const char *recipename, const char *version)
Saving table data of given category.
double dfs_get_parameter_double(cpl_parameterlist *parlist, const char *name, const cpl_table *defaults)
Reading a recipe double parameter value.