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 "omega_recipe.h"
00037 #include "omega_utils.h"
00038
00058
00059
00060
00061
00062 static int omega_illumination_create(cpl_plugin *) ;
00063 static int omega_illumination_exec(cpl_plugin *) ;
00064 static int omega_illumination_destroy(cpl_plugin *) ;
00065 static int omega_illumination(cpl_frameset *, cpl_parameterlist *) ;
00066 static cpl_image *omega_illum_process(cpl_frame *frame, cpl_parameterlist *pars, int ext);
00067 static void omega_illum_init(void);
00068 static void omega_illum_tidy(void);
00069
00070
00071
00072 static struct {
00073
00074 int extnum;
00075
00076 }omega_illum_config;
00077
00078 static struct {
00079
00080
00081
00082
00083 cpl_image *illum;
00084
00085 }ps;
00086
00087 #define RECIPE "omega_illumination"
00088
00089
00098
00099 int cpl_plugin_get_info(cpl_pluginlist * list)
00100 {
00101 cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe)) ;
00102 cpl_plugin * plugin = &recipe->interface ;
00103
00104 cpl_plugin_init(plugin,
00105 CPL_PLUGIN_API,
00106 OMEGA_BINARY_VERSION,
00107 CPL_PLUGIN_TYPE_RECIPE,
00108 "omega_illumination",
00109 "OMEGA - Generates a valid illumination correction frame (Calfile 548).",
00110 "This recipe is used to derive an illumination correction frame for one \n"
00111 "particular chip and filter. The recipe always takes as input a fit of \n"
00112 "the illumination correction over the full focal plane for a given filter. \n"
00113 "A chip name should also be specified.",
00114 "Sandra Castro",
00115 "scastro@eso.org",
00116 omega_get_license(),
00117 omega_illumination_create,
00118 omega_illumination_exec,
00119 omega_illumination_destroy) ;
00120
00121 cpl_pluginlist_append(list, plugin) ;
00122
00123 return 0;
00124 }
00125
00126
00135
00136 static int omega_illumination_create(cpl_plugin * plugin)
00137 {
00138 cpl_recipe * recipe;
00139 cpl_parameter * p ;
00140
00141
00142 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00143 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00144 cpl_func, __LINE__, cpl_error_get_where());
00145 return (int)cpl_error_get_code();
00146 }
00147
00148 if (plugin == NULL) {
00149 cpl_msg_error(cpl_func, "Null plugin");
00150 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00151 }
00152
00153
00154 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00155 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00156 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00157 }
00158
00159
00160 recipe = (cpl_recipe *)plugin;
00161
00162
00163 recipe->parameters = cpl_parameterlist_new() ;
00164 if (recipe->parameters == NULL) {
00165 cpl_msg_error(cpl_func, "Parameter list allocation failed");
00166 cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
00167 }
00168
00169
00170 p = cpl_parameter_new_value("omega.omega_illumination.ExtensionNumber",
00171 CPL_TYPE_INT,
00172 "FITS extension number to load (1 to 32). (-1 == all)",
00173 "omega_nightsky_flat",
00174 -1) ;
00175
00176 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"ext") ;
00177 cpl_parameterlist_append(recipe->parameters, p) ;
00178
00179
00180 return 0;
00181 }
00182
00183
00189
00190 static int omega_illumination_exec(cpl_plugin * plugin)
00191 {
00192 cpl_recipe * recipe;
00193 int recipe_status;
00194
00195
00196 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00197 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00198 cpl_func, __LINE__, cpl_error_get_where());
00199 return (int)cpl_error_get_code();
00200 }
00201
00202 if (plugin == NULL) {
00203 cpl_msg_error(cpl_func, "Null plugin");
00204 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00205 }
00206
00207
00208 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00209 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00210 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00211 }
00212
00213
00214 recipe = (cpl_recipe *)plugin;
00215
00216
00217 if (recipe->parameters == NULL) {
00218 cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
00219 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00220 }
00221 if (recipe->frames == NULL) {
00222 cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
00223 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00224 }
00225
00226
00227 recipe_status = omega_illumination(recipe->frames, recipe->parameters);
00228
00229
00230 if (cpl_dfs_update_product_header(recipe->frames)) {
00231 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
00232 }
00233
00234 return recipe_status;
00235
00236 }
00237
00238
00244
00245 static int omega_illumination_destroy(cpl_plugin * plugin)
00246 {
00247 cpl_recipe * recipe;
00248
00249 if (plugin == NULL) {
00250 cpl_msg_error(cpl_func, "Null plugin");
00251 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00252 }
00253
00254
00255 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00256 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00257 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00258 }
00259
00260
00261 recipe = (cpl_recipe *)plugin;
00262
00263 cpl_parameterlist_delete(recipe->parameters) ;
00264
00265 return 0 ;
00266 }
00267
00268
00275
00276 static int omega_illumination(cpl_frameset *set, cpl_parameterlist *pars)
00277 {
00278 int j,jst,jfn,isfirst;
00279 char *outfile = NULL;
00280
00281 cpl_frame *frame, *product_frame;
00282 cpl_parameter *par;
00283 cpl_propertylist *qclist;
00284
00285
00286 if (!pars) {
00287 cpl_msg_error (cpl_func, "Parameters list not found");
00288 return -1;
00289 }
00290
00291 if (cpl_frameset_is_empty(set) == 1) {
00292 cpl_msg_error (cpl_func, "Frameset not found");
00293 return -1;
00294 }
00295
00296
00297 par = cpl_parameterlist_find(pars, "omega.omega_illumination.ExtensionNumber") ;
00298 omega_illum_config.extnum = cpl_parameter_get_int(par) ;
00299
00300
00301 if (oc_dfs_set_groups(set)) {
00302 cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames") ;
00303 return -1 ;
00304 }
00305
00306
00307 omega_illum_init();
00308
00309
00310 frame = cpl_frameset_find(set,OMEGA_CALIB_ILLFIT);
00311 if(frame == NULL){
00312 cpl_msg_error(cpl_func,"Cannot find coefficients table in input frameset");
00313 omega_illum_tidy();
00314 return -1;
00315 }
00316
00317
00318 omega_exten_range(omega_illum_config.extnum,&jst,&jfn);
00319 if(omega_illum_config.extnum == 0){
00320 cpl_msg_error(cpl_func,"Unsupported extension request, %d",omega_illum_config.extnum);
00321 omega_illum_tidy();
00322 return -1;
00323 }
00324
00325 for (j = jst; j <= jfn; j++) {
00326 isfirst = (j == jst);
00327 cpl_msg_indent_more();
00328 cpl_msg_info(cpl_func,".....Working on extension %d.....",j);
00329 cpl_msg_indent_less();
00330
00331
00332 ps.illum = omega_illum_process(frame, pars, j);
00333 if(ps.illum == NULL){
00334 cpl_msg_error(cpl_func,"Cannot create illumination correction frame");
00335 freespace(outfile);
00336 omega_illum_tidy();
00337 return -1;
00338 }
00339
00340
00341 if(isfirst){
00342 outfile = cpl_sprintf("%s_%s.fits", INSTRUME,ILLUM_PROCATG);
00343 product_frame = omega_product_frame(outfile, ILLUM_PROCATG, CPL_FRAME_TYPE_IMAGE);
00344 }
00345
00346 cpl_msg_info(cpl_func,"Saving Illumination frame");
00347
00348
00349 if(omega_save_image(ps.illum,set,pars,NULL,NULL,CPL_BPP_IEEE_FLOAT,outfile,
00350 RECIPE,product_frame,NULL,isfirst) == -1){
00351 cpl_msg_error(cpl_func,"Cannot save product %s",ILLUM_PROCATG);
00352 freespace(outfile);
00353 omega_illum_tidy();
00354 return -1;
00355 }
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 freeimage(ps.illum);
00366 ps.illum = NULL;
00367
00368 }
00369
00370 freespace(outfile);
00371 omega_illum_tidy();
00372
00373 return 0;
00374 }
00375
00386 static cpl_image *omega_illum_process(cpl_frame *frame, cpl_parameterlist *pars, int ext)
00387 {
00388
00389 int i = 0;
00390 int status = 0;
00391 int xsize, ysize;
00392 cpl_size pows[2];
00393 int dim = 2;
00394 float cfs[6];
00395 const char *chipid;
00396
00397 cpl_table *objects;
00398 cpl_image *illum;
00399 cpl_polynomial *poly;
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 if((chipid = omega_get_extension_chipid(ext)) == NULL){
00414 cpl_msg_error(cpl_func,"CHIP ID not found");
00415
00416 return NULL;
00417 }
00418 cpl_msg_info(cpl_func,"CHIP ID for this extension is %s",chipid);
00419
00420 if((objects = cpl_table_load(cpl_frame_get_filename(frame), 1, 0)) == NULL){
00421 cpl_msg_error(cpl_func,"NULL input OBJECTS table. %s",cpl_error_get_message());
00422
00423 return NULL;
00424 }
00425
00426 for(i = 0; i < cpl_table_get_nrow(objects); i++){
00427 const char *name = cpl_table_get_string(objects,"chip_name",i);
00428 status = strcmp(chipid,name);
00429 if(status == 0){
00430 cfs[0] = cpl_table_get_float(objects,"C_o", i, NULL);
00431 cfs[1] = cpl_table_get_float(objects,"C_x", i, NULL);
00432 cfs[2] = cpl_table_get_float(objects,"C_y", i, NULL);
00433 cfs[3] = cpl_table_get_float(objects,"C_xx", i, NULL);
00434 cfs[4] = cpl_table_get_float(objects,"C_xy", i, NULL);
00435 cfs[5] = cpl_table_get_float(objects,"C_yy", i, NULL);
00436 break;
00437 }
00438 }
00439
00440 if(status != 0){
00441 cpl_msg_error(cpl_func,"Cannot find CHIP ID in table");
00442 freetable(objects);
00443 return NULL;
00444 }
00445 freetable(objects);
00446
00447 omega_pfits_get_chip_size(NULL, &xsize, &ysize);
00448
00449
00450 poly = cpl_polynomial_new(dim);
00451 pows[0] = 0;
00452 pows[1] = 0;
00453 cpl_polynomial_set_coeff(poly, pows, cfs[0]);
00454 pows[0] = 1;
00455 pows[1] = 0;
00456 cpl_polynomial_set_coeff(poly, pows, cfs[1]);
00457 pows[0] = 0;
00458 pows[1] = 1;
00459 cpl_polynomial_set_coeff(poly, pows, cfs[2]);
00460 pows[0] = 2;
00461 pows[1] = 0;
00462 cpl_polynomial_set_coeff(poly, pows, cfs[3]);
00463 pows[0] = 1;
00464 pows[1] = 1;
00465 cpl_polynomial_set_coeff(poly, pows, cfs[4]);
00466 pows[0] = 0;
00467 pows[1] = 2;
00468 cpl_polynomial_set_coeff(poly, pows, cfs[5]);
00469
00470 illum = cpl_image_new(xsize, ysize, CPL_TYPE_FLOAT);
00471 if(cpl_image_fill_polynomial(illum, poly,1,1,1,1) != CPL_ERROR_NONE){
00472 cpl_msg_error(cpl_func,"Cannot fill polynomial. %s",cpl_error_get_message());
00473 cpl_polynomial_delete(poly);
00474 }
00475
00476 cpl_polynomial_delete(poly);
00477
00478 cpl_image_multiply_scalar(illum, -0.4);
00479 cpl_image_exponential(illum, 10);
00480
00481 return illum;
00482 }
00483
00484
00485
00486 static void omega_illum_init(void)
00487 {
00488 ps.illum = NULL;
00489 }
00490
00491
00492
00493
00494 static void omega_illum_tidy(void)
00495 {
00496 freeimage(ps.illum);
00497 }
00498