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