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 <cpl.h>
00037
00038 #include "sinfoni_tpl_utils.h"
00039 #include "sinfoni_pfits.h"
00040 #include "sinfoni_tpl_dfs.h"
00041 #include "sinfoni_key_names.h"
00042 #include "sinfoni_pro_types.h"
00043 #include "sinfoni_functions.h"
00044 #include <sinfoni_memory.h>
00045
00046
00047
00048
00049
00050 static int si_utl_ima_arith_create(cpl_plugin *) ;
00051 static int si_utl_ima_arith_exec(cpl_plugin *) ;
00052 static int si_utl_ima_arith_destroy(cpl_plugin *) ;
00053 static int si_utl_ima_arith(cpl_parameterlist *, cpl_frameset *) ;
00054
00055
00056
00057
00058
00059 static char si_utl_ima_arith_description[] =
00060 "This recipe performs image computation.\n"
00061 "The input files are 2 images\n"
00062 "their associated tags should be IMA.\n"
00063 "The output is an image resulting from the IMA op IMA where op indicates\n"
00064 "the operation to be performed specified by the parameter sinfoni.si_utl_ima_arith.op\n"
00065 " having alias 'op'\n"
00066 "Information on relevant parameters can be found with\n"
00067 "esorex --params si_utl_ima_arith\n"
00068 "esorex --help si_utl_ima_arith\n"
00069 "\n";
00070
00071
00072
00073
00074
00075
00083
00084 int cpl_plugin_get_info(cpl_pluginlist * list)
00085 {
00086 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
00087 cpl_plugin * plugin = &recipe->interface ;
00088
00089 cpl_plugin_init(plugin,
00090 CPL_PLUGIN_API,
00091 SINFONI_BINARY_VERSION,
00092 CPL_PLUGIN_TYPE_RECIPE,
00093 "si_utl_ima_arith",
00094 "Computes result of ima1 op ima2",
00095 si_utl_ima_arith_description,
00096 "Andrea Modigliani",
00097 "Andrea.Modigliani@eso.org",
00098 sinfoni_get_license(),
00099 si_utl_ima_arith_create,
00100 si_utl_ima_arith_exec,
00101 si_utl_ima_arith_destroy) ;
00102
00103 cpl_pluginlist_append(list, plugin) ;
00104
00105 return 0;
00106 }
00107
00108
00117
00118 static int si_utl_ima_arith_create(cpl_plugin * plugin)
00119 {
00120 cpl_recipe * recipe ;
00121 cpl_parameter * p ;
00122
00123
00124 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00125 recipe = (cpl_recipe *)plugin ;
00126 else return -1 ;
00127
00128
00129 recipe->parameters = cpl_parameterlist_new() ;
00130
00131
00132
00133 p = cpl_parameter_new_value("sinfoni.si_utl_ima_arith.op",
00134 CPL_TYPE_STRING, "A possible operation", "sinfoni.si_utl_ima_arith","+");
00135 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "op") ;
00136 cpl_parameterlist_append(recipe->parameters, p) ;
00137
00138
00139 p = cpl_parameter_new_value("sinfoni.si_utl_ima_arith.value",
00140 CPL_TYPE_DOUBLE, "a value", "sinfoni.si_utl_ima_arith", 9999.) ;
00141 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "value") ;
00142 cpl_parameterlist_append(recipe->parameters, p) ;
00143
00144
00145 return 0;
00146 }
00147
00148
00154
00155 static int si_utl_ima_arith_exec(cpl_plugin * plugin)
00156 {
00157 cpl_recipe * recipe ;
00158
00159
00160 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00161 recipe = (cpl_recipe *)plugin ;
00162 else return -1 ;
00163
00164 return si_utl_ima_arith(recipe->parameters, recipe->frames) ;
00165 }
00166
00167
00173
00174 static int si_utl_ima_arith_destroy(cpl_plugin * plugin)
00175 {
00176 cpl_recipe * recipe ;
00177
00178
00179 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00180 recipe = (cpl_recipe *)plugin ;
00181 else return -1 ;
00182
00183 cpl_parameterlist_delete(recipe->parameters) ;
00184 return 0 ;
00185 }
00186
00187
00194
00195 static int si_utl_ima_arith(
00196 cpl_parameterlist * parlist,
00197 cpl_frameset * framelist)
00198 {
00199 const char * fctid = "si_utl_ima_arith" ;
00200 cpl_parameter * param ;
00201 const char * operation ;
00202 double value ;
00203 cpl_frame * frm_ima1 ;
00204 cpl_frame * frm_ima2 ;
00205 cpl_image * ima1 ;
00206 cpl_image * ima2 ;
00207 int switch_ima2 = 0;
00208 const char * name_o ;
00209 cpl_propertylist * plist ;
00210 cpl_image * image ;
00211 cpl_frame * product_frame;
00212 cpl_frameset * raw_set=NULL;
00213 int nraw=0;
00214
00215
00216
00217 param = cpl_parameterlist_find(parlist, "sinfoni.si_utl_ima_arith.op");
00218 operation = cpl_parameter_get_string(param);
00219
00220
00221 param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_ima_arith.value");
00222 value = cpl_parameter_get_double(param) ;
00223
00224
00225 if (sinfoni_dfs_set_groups(framelist)) {
00226 cpl_msg_error(fctid, "Cannot identify RAW and CALIB frames") ;
00227 return -1 ;
00228 }
00229
00230
00231 raw_set=cpl_frameset_new();
00232 sinfoni_contains_frames_kind(framelist,raw_set,PRO_IMA);
00233 nraw=cpl_frameset_get_size(raw_set);
00234 if (nraw<1) {
00235 cpl_msg_error(fctid,"Found no input frames with tag %s",PRO_IMA);
00236 cpl_frameset_delete(raw_set);
00237 return -1;
00238 } else {
00239 frm_ima1=cpl_frameset_get_frame(framelist,0);
00240 ima1 = cpl_image_load(cpl_frame_get_filename(frm_ima1),CPL_TYPE_FLOAT,0,0);
00241 if (nraw>1) {
00242 frm_ima2=cpl_frameset_get_frame(framelist,1);
00243 ima2 = cpl_image_load(cpl_frame_get_filename(frm_ima2),CPL_TYPE_FLOAT,0,0);
00244 switch_ima2=1;
00245 } else if (value == 9999.) {
00246 cpl_msg_error(fctid,"Found only one input frames with tag %s",PRO_IMA);
00247 cpl_image_delete(ima1);
00248 cpl_frameset_delete(raw_set);
00249 return -1;
00250 } else {
00251 cpl_msg_info(fctid,"Perform image arithmetics on frame %s",PRO_IMA);
00252 }
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 cpl_frameset_delete(raw_set);
00271
00272
00273 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(frm_ima1),
00274 0)) == NULL) {
00275 cpl_msg_error(fctid, "Cannot read the FITS header") ;
00276 cpl_image_delete(ima1);
00277 cpl_image_delete(ima2);
00278 return -1 ;
00279 }
00280
00281
00282
00283 if (value == 9999.) {
00284 if(ima1 != NULL && ima2 != NULL) {
00285 cpl_msg_info(fctid,"ima1 %s ima2",operation);
00286 if (strcmp(operation,"+") == 0 ) {
00287 if ((image = cpl_image_add_create(ima1, ima2)) == NULL) {
00288 cpl_msg_error(fctid, "Cannot generate the %s image",operation) ;
00289 cpl_image_delete(ima1);
00290 cpl_image_delete(ima2);
00291 cpl_propertylist_delete(plist) ;
00292 return -1 ;
00293 }
00294 } else if (strcmp(operation,"-") == 0 ) {
00295 if ((image = cpl_image_subtract_create(ima1, ima2)) == NULL) {
00296 cpl_msg_error(fctid, "Cannot generate the %s image",operation) ;
00297 cpl_image_delete(ima1);
00298 cpl_image_delete(ima2);
00299 cpl_propertylist_delete(plist) ;
00300 return -1 ;
00301 }
00302 } else if (strcmp(operation,"*") == 0 ) {
00303 if ((image = cpl_image_multiply_create(ima1, ima2)) == NULL) {
00304 cpl_msg_error(fctid, "Cannot generate the %s image",operation) ;
00305 cpl_image_delete(ima1);
00306 cpl_image_delete(ima2);
00307 cpl_propertylist_delete(plist) ;
00308 return -1 ;
00309 }
00310 } else if (strcmp(operation,"/") == 0 ) {
00311 if ((image = cpl_image_divide_create(ima1, ima2)) == NULL) {
00312 cpl_msg_error(fctid, "Cannot generate the %s image",operation) ;
00313 cpl_image_delete(ima1);
00314 cpl_image_delete(ima2);
00315 cpl_propertylist_delete(plist) ;
00316 return -1 ;
00317 }
00318
00319 } else {
00320 cpl_msg_error(fctid,"Operation %s not supported",operation);
00321 cpl_image_delete(ima1);
00322 cpl_image_delete(ima2);
00323 cpl_propertylist_delete(plist) ;
00324 return -1;
00325 }
00326 cpl_image_delete(ima1);
00327 cpl_image_delete(ima2);
00328
00329 }
00330
00331 } else {
00332 cpl_msg_info(fctid,"ima1 %s %f",operation,value);
00333
00334 if(switch_ima2 == 1) {
00335 cpl_image_delete(ima2);
00336 }
00337
00338 if (strcmp(operation,"+") == 0 ) {
00339 if ((image = cpl_image_add_scalar_create(ima1, value)) == NULL) {
00340 cpl_msg_error(fctid, "Cannot apply the %s operator",operation) ;
00341 cpl_image_delete(ima1);
00342 cpl_propertylist_delete(plist) ;
00343 return -1 ;
00344 }
00345 } else if (strcmp(operation,"-") == 0 ) {
00346 if ((image = cpl_image_subtract_scalar_create(ima1, value)) == NULL) {
00347 cpl_msg_error(fctid, "Cannot apply the %s operator",operation) ;
00348 cpl_image_delete(ima1);
00349 cpl_propertylist_delete(plist) ;
00350 return -1 ;
00351 }
00352 } else if (strcmp(operation,"*") == 0 ) {
00353 if ((image = cpl_image_multiply_scalar_create(ima1, value)) == NULL) {
00354 cpl_msg_error(fctid, "Cannot apply the %s operator",operation) ;
00355 cpl_image_delete(ima1);
00356 cpl_propertylist_delete(plist) ;
00357 return -1 ;
00358 }
00359 } else if (strcmp(operation,"/") == 0 ) {
00360 if ((image = cpl_image_divide_scalar_create(ima1, value)) == NULL) {
00361 cpl_msg_error(fctid, "Cannot apply the %s operator",operation) ;
00362 cpl_image_delete(ima1);
00363 cpl_propertylist_delete(plist) ;
00364 return -1 ;
00365 }
00366
00367 } else {
00368 cpl_msg_error(fctid,"Operation %s not supported",operation);
00369 cpl_image_delete(ima1);
00370 cpl_propertylist_delete(plist) ;
00371 return -1;
00372 }
00373
00374 cpl_image_delete(ima1);
00375
00376
00377 }
00378
00379
00380
00381
00382 name_o = "ima_res.fits" ;
00383
00384
00385 product_frame = cpl_frame_new();
00386 cpl_frame_set_filename(product_frame, name_o) ;
00387 cpl_frame_set_tag(product_frame, SI_UTL_IMA_ARITH_PROIMA) ;
00388 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE) ;
00389 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT) ;
00390 cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL) ;
00391 if (cpl_error_get_code()) {
00392 cpl_msg_error(fctid, "Error while initialising the product frame") ;
00393 cpl_propertylist_delete(plist) ;
00394 cpl_frame_delete(product_frame) ;
00395 cpl_image_delete(image) ;
00396 return -1 ;
00397 }
00398
00399
00400 cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG");
00401 if (cpl_dfs_setup_product_header(plist, product_frame, framelist, parlist,
00402 "si_utl_ima_arith", "SINFONI", KEY_VALUE_HPRO_DID) != CPL_ERROR_NONE) {
00403 cpl_msg_error(fctid, "Problem in the product DFS-compliance") ;
00404 cpl_image_delete(image) ;
00405 cpl_propertylist_delete(plist) ;
00406 cpl_frame_delete(product_frame) ;
00407 return -1 ;
00408 }
00409
00410
00411
00412 if (cpl_image_save(image, name_o, CPL_BPP_DEFAULT, plist,
00413 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00414 cpl_msg_error(fctid, "Could not save product");
00415 cpl_propertylist_delete(plist) ;
00416 cpl_frame_delete(product_frame) ;
00417 cpl_image_delete(image) ;
00418 return -1 ;
00419 }
00420 cpl_propertylist_delete(plist) ;
00421 cpl_image_delete(image) ;
00422
00423
00424 cpl_frameset_insert(framelist, product_frame) ;
00425
00426
00427 if (cpl_error_get_code())
00428 return -1 ;
00429 else
00430 return 0 ;
00431 }