36 #include "omega_recipe.h"
37 #include "omega_utils.h"
62 static int omega_illumination_create(cpl_plugin *) ;
63 static int omega_illumination_exec(cpl_plugin *) ;
64 static int omega_illumination_destroy(cpl_plugin *) ;
65 static int omega_illumination(cpl_frameset *, cpl_parameterlist *) ;
66 static cpl_image *omega_illum_process(cpl_frame *frame, cpl_parameterlist *pars,
int ext);
67 static void omega_illum_init(
void);
68 static void omega_illum_tidy(
void);
87 #define RECIPE "omega_illumination"
101 cpl_recipe * recipe = cpl_calloc(1,
sizeof(*recipe)) ;
102 cpl_plugin * plugin = &recipe->interface ;
104 cpl_plugin_init(plugin,
106 OMEGA_BINARY_VERSION,
107 CPL_PLUGIN_TYPE_RECIPE,
108 "omega_illumination",
109 "OMEGA - Generates a valid illumination correction frame (Calfile 548).",
110 "This recipe is used to derive an illumination correction frame for one \n"
111 "particular chip and filter. The recipe always takes as input a fit of \n"
112 "the illumination correction over the full focal plane for a given filter. \n"
113 "A chip name should also be specified.",
117 omega_illumination_create,
118 omega_illumination_exec,
119 omega_illumination_destroy) ;
121 cpl_pluginlist_append(list, plugin) ;
136 static int omega_illumination_create(cpl_plugin * plugin)
142 if (cpl_error_get_code() != CPL_ERROR_NONE) {
143 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
144 cpl_func, __LINE__, cpl_error_get_where());
145 return (
int)cpl_error_get_code();
148 if (plugin == NULL) {
149 cpl_msg_error(cpl_func,
"Null plugin");
150 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
154 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
155 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
156 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
160 recipe = (cpl_recipe *)plugin;
163 recipe->parameters = cpl_parameterlist_new() ;
164 if (recipe->parameters == NULL) {
165 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
166 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
170 p = cpl_parameter_new_value(
"omega.omega_illumination.ExtensionNumber",
172 "FITS extension number to load (1 to 32). (-1 == all)",
173 "omega_nightsky_flat",
176 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"ext") ;
177 cpl_parameterlist_append(recipe->parameters, p) ;
190 static int omega_illumination_exec(cpl_plugin * plugin)
196 if (cpl_error_get_code() != CPL_ERROR_NONE) {
197 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
198 cpl_func, __LINE__, cpl_error_get_where());
199 return (
int)cpl_error_get_code();
202 if (plugin == NULL) {
203 cpl_msg_error(cpl_func,
"Null plugin");
204 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
208 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
209 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
210 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
214 recipe = (cpl_recipe *)plugin;
217 if (recipe->parameters == NULL) {
218 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
219 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
221 if (recipe->frames == NULL) {
222 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
223 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
227 recipe_status = omega_illumination(recipe->frames, recipe->parameters);
230 if (cpl_dfs_update_product_header(recipe->frames)) {
231 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
234 return recipe_status;
245 static int omega_illumination_destroy(cpl_plugin * plugin)
249 if (plugin == NULL) {
250 cpl_msg_error(cpl_func,
"Null plugin");
251 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
255 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
256 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
257 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
261 recipe = (cpl_recipe *)plugin;
263 cpl_parameterlist_delete(recipe->parameters) ;
276 static int omega_illumination(cpl_frameset *
set, cpl_parameterlist *pars)
278 int j,jst,jfn,isfirst;
279 char *outfile = NULL;
281 cpl_frame *frame, *product_frame;
283 cpl_propertylist *qclist;
287 cpl_msg_error (cpl_func,
"Parameters list not found");
291 if (cpl_frameset_is_empty(
set) == 1) {
292 cpl_msg_error (cpl_func,
"Frameset not found");
297 par = cpl_parameterlist_find(pars,
"omega.omega_illumination.ExtensionNumber") ;
298 omega_illum_config.extnum = cpl_parameter_get_int(par) ;
301 if (oc_dfs_set_groups(
set)) {
302 cpl_msg_error(cpl_func,
"Cannot identify RAW and CALIB frames") ;
310 frame = cpl_frameset_find(
set,OMEGA_CALIB_ILLFIT);
312 cpl_msg_error(cpl_func,
"Cannot find coefficients table in input frameset");
319 if(omega_illum_config.extnum == 0){
320 cpl_msg_error(cpl_func,
"Unsupported extension request, %d",omega_illum_config.extnum);
325 for (j = jst; j <= jfn; j++) {
326 isfirst = (j == jst);
327 cpl_msg_indent_more();
328 cpl_msg_info(cpl_func,
".....Working on extension %d.....",j);
329 cpl_msg_indent_less();
332 ps.illum = omega_illum_process(frame, pars, j);
333 if(ps.illum == NULL){
334 cpl_msg_error(cpl_func,
"Cannot create illumination correction frame");
342 outfile = cpl_sprintf(
"%s_%s.fits", INSTRUME,ILLUM_PROCATG);
346 cpl_msg_info(cpl_func,
"Saving Illumination frame");
350 RECIPE,product_frame,NULL,isfirst) == -1){
351 cpl_msg_error(cpl_func,
"Cannot save product %s",ILLUM_PROCATG);
386 static cpl_image *omega_illum_process(cpl_frame *frame, cpl_parameterlist *pars,
int ext)
399 cpl_polynomial *poly;
414 cpl_msg_error(cpl_func,
"CHIP ID not found");
418 cpl_msg_info(cpl_func,
"CHIP ID for this extension is %s",chipid);
420 if((objects = cpl_table_load(cpl_frame_get_filename(frame), 1, 0)) == NULL){
421 cpl_msg_error(cpl_func,
"NULL input OBJECTS table. %s",cpl_error_get_message());
426 for(i = 0; i < cpl_table_get_nrow(objects); i++){
427 const char *name = cpl_table_get_string(objects,
"chip_name",i);
428 status = strcmp(chipid,name);
430 cfs[0] = cpl_table_get_float(objects,
"C_o", i, NULL);
431 cfs[1] = cpl_table_get_float(objects,
"C_x", i, NULL);
432 cfs[2] = cpl_table_get_float(objects,
"C_y", i, NULL);
433 cfs[3] = cpl_table_get_float(objects,
"C_xx", i, NULL);
434 cfs[4] = cpl_table_get_float(objects,
"C_xy", i, NULL);
435 cfs[5] = cpl_table_get_float(objects,
"C_yy", i, NULL);
441 cpl_msg_error(cpl_func,
"Cannot find CHIP ID in table");
450 poly = cpl_polynomial_new(dim);
453 cpl_polynomial_set_coeff(poly, pows, cfs[0]);
456 cpl_polynomial_set_coeff(poly, pows, cfs[1]);
459 cpl_polynomial_set_coeff(poly, pows, cfs[2]);
462 cpl_polynomial_set_coeff(poly, pows, cfs[3]);
465 cpl_polynomial_set_coeff(poly, pows, cfs[4]);
468 cpl_polynomial_set_coeff(poly, pows, cfs[5]);
470 illum = cpl_image_new(xsize, ysize, CPL_TYPE_FLOAT);
471 if(cpl_image_fill_polynomial(illum, poly,1,1,1,1) != CPL_ERROR_NONE){
472 cpl_msg_error(cpl_func,
"Cannot fill polynomial. %s",cpl_error_get_message());
473 cpl_polynomial_delete(poly);
476 cpl_polynomial_delete(poly);
478 cpl_image_multiply_scalar(illum, -0.4);
479 cpl_image_exponential(illum, 10);
486 static void omega_illum_init(
void)
494 static void omega_illum_tidy(
void)