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 #include <string.h>
00036
00037
00038 #include <cpl.h>
00039
00040
00041 #include <irplib_utils.h>
00042
00043 #include <sinfo_tpl_utils.h>
00044 #include <sinfo_pfits.h>
00045 #include <sinfo_tpl_dfs.h>
00046 #include <sinfo_key_names.h>
00047 #include <sinfo_pro_types.h>
00048 #include <sinfo_functions.h>
00049 #include <sinfo_msg.h>
00050 #include <sinfo_error.h>
00051 #include <sinfo_utils_wrappers.h>
00052
00053
00054
00055
00056
00057 static int sinfo_utl_ima_arith_create(cpl_plugin *) ;
00058 static int sinfo_utl_ima_arith_exec(cpl_plugin *) ;
00059 static int sinfo_utl_ima_arith_destroy(cpl_plugin *) ;
00060 static int sinfo_utl_ima_arith(cpl_parameterlist *, cpl_frameset *) ;
00061
00062
00063
00064
00065
00066 static char sinfo_utl_ima_arith_description[] =
00067 "This recipe performs image computation.\n"
00068 "The input files are 2 images\n"
00069 "their associated tags should be IMA.\n"
00070 "The output is an image resulting from the IMA op IMA where op indicates\n"
00071 "the operation to be performed specified by the parameter \n"
00072 "sinfoni.sinfo_utl_ima_arith.op having alias 'op'\n"
00073 "Information on relevant parameters can be found with\n"
00074 "esorex --params sinfo_utl_ima_arith\n"
00075 "esorex --help sinfo_utl_ima_arith\n"
00076 "\n";
00077
00078
00079
00080
00081
00085
00086
00088
00096
00097 int cpl_plugin_get_info(cpl_pluginlist * list)
00098 {
00099 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
00100 cpl_plugin * plugin = &recipe->interface ;
00101
00102 cpl_plugin_init(plugin,
00103 CPL_PLUGIN_API,
00104 SINFONI_BINARY_VERSION,
00105 CPL_PLUGIN_TYPE_RECIPE,
00106 "sinfo_utl_ima_arith",
00107 "Computes result of ima1 op ima2",
00108 sinfo_utl_ima_arith_description,
00109 "Andrea Modigliani",
00110 "Andrea.Modigliani@eso.org",
00111 sinfo_get_license(),
00112 sinfo_utl_ima_arith_create,
00113 sinfo_utl_ima_arith_exec,
00114 sinfo_utl_ima_arith_destroy) ;
00115
00116 cpl_pluginlist_append(list, plugin) ;
00117
00118 return 0;
00119 }
00120
00121
00130
00131 static int sinfo_utl_ima_arith_create(cpl_plugin * plugin)
00132 {
00133 cpl_recipe * recipe ;
00134 cpl_parameter * p ;
00135
00136
00137 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00138 recipe = (cpl_recipe *)plugin ;
00139 else return -1 ;
00140 cpl_error_reset();
00141 irplib_reset();
00142
00143
00144 recipe->parameters = cpl_parameterlist_new() ;
00145
00146
00147
00148 p = cpl_parameter_new_value("sinfoni.sinfo_utl_ima_arith.op",
00149 CPL_TYPE_STRING,
00150 "A possible operation",
00151 "sinfoni.sinfo_utl_ima_arith","+");
00152 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "op") ;
00153 cpl_parameterlist_append(recipe->parameters, p) ;
00154
00155
00156 p = cpl_parameter_new_value("sinfoni.sinfo_utl_ima_arith.value",
00157 CPL_TYPE_DOUBLE, "a value", "sinfoni.sinfo_utl_ima_arith", 9999.) ;
00158 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "value") ;
00159 cpl_parameterlist_append(recipe->parameters, p) ;
00160
00161
00162 return 0;
00163 }
00164
00165
00171
00172 static int sinfo_utl_ima_arith_exec(cpl_plugin * plugin)
00173 {
00174 cpl_recipe * recipe ;
00175 int code=0;
00176 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00177
00178
00179 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00180 recipe = (cpl_recipe *)plugin ;
00181 else return -1 ;
00182 cpl_error_reset();
00183 irplib_reset();
00184 code = sinfo_utl_ima_arith(recipe->parameters, recipe->frames) ;
00185
00186
00187 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00188
00189
00190 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00191 }
00192
00193 return code ;
00194 }
00195
00196
00202
00203 static int sinfo_utl_ima_arith_destroy(cpl_plugin * plugin)
00204 {
00205 cpl_recipe * recipe ;
00206
00207
00208 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00209 recipe = (cpl_recipe *)plugin ;
00210 else return -1 ;
00211
00212 cpl_parameterlist_delete(recipe->parameters) ;
00213 return 0 ;
00214 }
00215
00216
00223
00224 static int
00225 sinfo_utl_ima_arith( cpl_parameterlist * parlist,
00226 cpl_frameset * framelist)
00227 {
00228 cpl_parameter * param= NULL ;
00229 const char * operation=NULL;
00230 double value=1 ;
00231 cpl_frame * frm_ima1=NULL ;
00232 cpl_frame * frm_ima2=NULL ;
00233 cpl_image * ima1=NULL ;
00234 cpl_image * ima2=NULL ;
00235 int switch_ima2 = 0;
00236 const char * name_o=NULL ;
00237 cpl_propertylist * plist=NULL ;
00238 cpl_image * image=NULL ;
00239 cpl_frame * product_frame=NULL;
00240 cpl_frameset * raw_set=NULL;
00241 int nraw=0;
00242 int n=0;
00243 sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
00244 SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
00245
00246
00247
00248 check_nomsg(param=cpl_parameterlist_find(parlist,
00249 "sinfoni.sinfo_utl_ima_arith.op"));
00250 check_nomsg(operation=cpl_parameter_get_string(param));
00251
00252
00253 check_nomsg(param=cpl_parameterlist_find(parlist,
00254 "sinfoni.sinfo_utl_ima_arith.value"));
00255 check_nomsg(value = cpl_parameter_get_double(param)) ;
00256
00257
00258 check(sinfo_dfs_set_groups(framelist),
00259 "Cannot identify RAW and CALIB frames") ;
00260
00261
00262 n=cpl_frameset_get_size(framelist);
00263 if(n<1) {
00264 sinfo_msg_error("Empty input frame list!");
00265 goto cleanup ;
00266 }
00267
00268
00269 check_nomsg(raw_set=cpl_frameset_new());
00270
00271 check(sinfo_contains_frames_kind(framelist,raw_set,PRO_IMA),
00272 "Found no input frames with tag %s",PRO_IMA);
00273 check_nomsg(nraw=cpl_frameset_get_size(raw_set));
00274 if (nraw<1) {
00275 sinfo_msg_error("Found no input frames with tag %s",PRO_IMA);
00276 goto cleanup;
00277 } else {
00278 check_nomsg(frm_ima1=cpl_frameset_get_frame(framelist,0));
00279 check_nomsg(ima1=cpl_image_load(cpl_frame_get_filename(frm_ima1),
00280 CPL_TYPE_FLOAT,0,0));
00281 if (nraw>1) {
00282 check_nomsg(frm_ima2=cpl_frameset_get_frame(framelist,1));
00283 check_nomsg(ima2 = cpl_image_load(cpl_frame_get_filename(frm_ima2),
00284 CPL_TYPE_FLOAT,0,0));
00285 switch_ima2=1;
00286 } else if (value == 9999.) {
00287 sinfo_msg_error("Found only one input frames with tag %s",PRO_IMA);
00288 goto cleanup;
00289 } else {
00290 sinfo_msg("Perform image arithmetics on frame %s",PRO_IMA);
00291 }
00292 }
00293
00294 sinfo_free_frameset(&raw_set);
00295
00296
00297 check(plist=cpl_propertylist_load(cpl_frame_get_filename(frm_ima1),0),
00298 "Cannot read the FITS header") ;
00299
00300
00301
00302 if (value == 9999.) {
00303
00304 if(ima1 != NULL && ima2 != NULL) {
00305 sinfo_msg("ima1 %s ima2",operation);
00306 if (strcmp(operation,"+") == 0 ) {
00307 check(image = cpl_image_add_create(ima1, ima2),
00308 "Cannot generate the %s image",operation) ;
00309 } else if (strcmp(operation,"-") == 0 ) {
00310 check(image = cpl_image_subtract_create(ima1, ima2),
00311 "Cannot generate the %s image",operation) ;
00312 } else if (strcmp(operation,"*") == 0 ) {
00313 check(image = cpl_image_multiply_create(ima1, ima2),
00314 "Cannot generate the %s image",operation) ;
00315 } else if (strcmp(operation,"/") == 0 ) {
00316 check(image = cpl_image_divide_create(ima1, ima2),
00317 "Cannot generate the %s image",operation) ;
00318 } else {
00319 sinfo_msg_error("Operation %s not supported",operation);
00320 goto cleanup;
00321 }
00322 sinfo_free_image(&ima1);
00323 sinfo_free_image(&ima2);
00324
00325 }
00326
00327 } else {
00328 sinfo_msg("ima1 %s %f",operation,value);
00329
00330 if(switch_ima2 == 1) {
00331 sinfo_free_image(&ima2);
00332 }
00333
00334 if (strcmp(operation,"+") == 0 ) {
00335 check(image = cpl_image_add_scalar_create(ima1, value),
00336 "Cannot apply the %s operator",operation) ;
00337 } else if (strcmp(operation,"-") == 0 ) {
00338 check(image = cpl_image_subtract_scalar_create(ima1, value),
00339 "Cannot apply the %s operator",operation) ;
00340 } else if (strcmp(operation,"*") == 0 ) {
00341 check(image = cpl_image_multiply_scalar_create(ima1, value),
00342 "Cannot apply the %s operator",operation) ;
00343 } else if (strcmp(operation,"/") == 0 ) {
00344 check(image = cpl_image_divide_scalar_create(ima1, value),
00345 "Cannot apply the %s operator",operation) ;
00346 } else {
00347 sinfo_msg_error("Operation %s not supported",operation);
00348 goto cleanup;
00349 }
00350
00351 sinfo_free_image(&ima1);
00352
00353 }
00354
00355
00356
00357
00358 name_o = "ima_res.fits" ;
00359
00360
00361 check_nomsg(product_frame = cpl_frame_new());
00362 check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
00363 check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_IMA_ARITH_PROIMA)) ;
00364 check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
00365 check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00366 check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
00367 "Error while initialising the product frame") ;
00368
00369
00370 check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
00371 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)
00372 check(cpl_dfs_setup_product_header(plist,
00373 product_frame,
00374 framelist,
00375 parlist,
00376 "sinfo_utl_ima_arith",
00377 "SINFONI",
00378 KEY_VALUE_HPRO_DID,NULL),
00379 "Problem in the product DFS-compliance") ;
00380 #else
00381 check(cpl_dfs_setup_product_header(plist,
00382 product_frame,
00383 framelist,
00384 parlist,
00385 "sinfo_utl_ima_arith",
00386 "SINFONI",
00387 KEY_VALUE_HPRO_DID),
00388 "Problem in the product DFS-compliance") ;
00389 #endif
00390
00391
00392 check(cpl_image_save(image,
00393 name_o,
00394 CPL_BPP_IEEE_FLOAT,
00395 plist,
00396 CPL_IO_DEFAULT),
00397 "Could not save product");
00398 sinfo_free_propertylist(&plist) ;
00399 sinfo_free_image(&image);
00400
00401
00402 check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
00403
00404
00405 cleanup:
00406
00407 sinfo_free_image(&ima1);
00408 sinfo_free_image(&ima2);
00409 sinfo_free_frameset(&raw_set);
00410 sinfo_free_propertylist(&plist) ;
00411
00412
00413
00414 sinfo_free_image(&image) ;
00415
00416 if (cpl_error_get_code()) {
00417 return -1 ;
00418 } else {
00419 return 0 ;
00420 }
00421
00422 }