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
00042
00043
00044
00045
00046
00047
00048
00049 #include <xsh_dfs.h>
00050 #include <xsh_data_pre.h>
00051 #include <xsh_parameters.h>
00052 #include <xsh_drl.h>
00053 #include <xsh_msg.h>
00054 #include <xsh_pfits.h>
00055 #include <xsh_error.h>
00056
00057
00058
00059
00060
00061
00062
00063 #define XSH_UTL_IMA_ARITH_RECIPE_ID "xsh_util_ima_arith"
00064 #define XSH_UTL_IMA_ARITH_RECIPE_AUTHOR "A.Modigliani"
00065 #define XSH_UTL_IMA_ARITH_RECIPE_CONTACT "Andrea.Modigliani@eso.org"
00066 #define PRO_IMA "PRO_IMA_UVB"
00067 #define KEY_VALUE_HPRO_DID "PRO-1.15"
00068
00069
00070
00071
00072 static int xsh_util_ima_arith_create(cpl_plugin *) ;
00073 static int xsh_util_ima_arith_exec(cpl_plugin *) ;
00074 static int xsh_util_ima_arith_destroy(cpl_plugin *) ;
00075 static int xsh_util_ima_arith(cpl_parameterlist *, cpl_frameset *) ;
00076
00077
00078
00079
00080
00081 static char
00082 xsh_util_ima_arith_description_short[] = "Computes result of ima1 op ima2";
00083 static char xsh_util_ima_arith_description[] =
00084 "This recipe performs image computation.\n"
00085 "The input files are 2 images\n"
00086 "their associated tags should be IMA.\n"
00087 "The output is an image resulting from the IMA op IMA where op indicates\n"
00088 "the operation to be performed specified by the parameter \n"
00089 "xsh.xsh_util_ima_arith.op having alias 'op'\n"
00090 "Information on relevant parameters can be found with\n"
00091 "esorex --params xsh_util_ima_arith\n"
00092 "esorex --help xsh_util_ima_arith\n"
00093 "\n";
00094
00095
00096
00097
00098
00103
00104
00106
00114
00115 int cpl_plugin_get_info(cpl_pluginlist * list)
00116 {
00117 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
00118 cpl_plugin * plugin = &recipe->interface ;
00119
00120 cpl_plugin_init(plugin,
00121 CPL_PLUGIN_API,
00122 XSH_BINARY_VERSION,
00123 CPL_PLUGIN_TYPE_RECIPE,
00124 XSH_UTL_IMA_ARITH_RECIPE_ID,
00125 xsh_util_ima_arith_description_short,
00126 xsh_util_ima_arith_description,
00127 XSH_UTL_IMA_ARITH_RECIPE_AUTHOR,
00128 XSH_UTL_IMA_ARITH_RECIPE_CONTACT,
00129 xsh_get_license(),
00130 xsh_util_ima_arith_create,
00131 xsh_util_ima_arith_exec,
00132 xsh_util_ima_arith_destroy) ;
00133
00134 cpl_pluginlist_append(list, plugin) ;
00135
00136 return 0;
00137 }
00138
00139
00148
00149 static int xsh_util_ima_arith_create(cpl_plugin * plugin)
00150 {
00151 cpl_recipe * recipe ;
00152 cpl_parameter * p ;
00153
00154
00155 xsh_init();
00156
00157
00158 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00159 recipe = (cpl_recipe *)plugin ;
00160 else return -1 ;
00161 cpl_error_reset();
00162
00163
00164
00165 recipe->parameters = cpl_parameterlist_new() ;
00166
00167
00168
00169 check( xsh_parameters_generic(XSH_UTL_IMA_ARITH_RECIPE_ID,
00170 recipe->parameters ) ) ;
00171 xsh_parameters_decode_bp(XSH_UTL_IMA_ARITH_RECIPE_ID,recipe->parameters,-1);
00172
00173
00174 p = cpl_parameter_new_value("xsh.xsh_util_ima_arith.op",
00175 CPL_TYPE_STRING,
00176 "A possible operation",
00177 "xsh.xsh_util_ima_arith","+");
00178 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "op") ;
00179 cpl_parameterlist_append(recipe->parameters, p) ;
00180
00181
00182 p = cpl_parameter_new_value("xsh.xsh_util_ima_arith.value",
00183 CPL_TYPE_DOUBLE, "a value", "xsh.xsh_util_ima_arith", 9999.) ;
00184 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "value") ;
00185 cpl_parameterlist_append(recipe->parameters, p) ;
00186 cleanup:
00187
00188
00189 return 0;
00190 }
00191
00192
00198
00199 static int xsh_util_ima_arith_exec(cpl_plugin * plugin)
00200 {
00201 cpl_recipe * recipe ;
00202 int code=0;
00203 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00204
00205
00206 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00207 recipe = (cpl_recipe *)plugin ;
00208 else return -1 ;
00209 cpl_error_reset();
00210
00211 code = xsh_util_ima_arith(recipe->parameters, recipe->frames) ;
00212
00213
00214 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00215
00216
00217 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00218 }
00219
00220 return code ;
00221 }
00222
00223
00229
00230 static int xsh_util_ima_arith_destroy(cpl_plugin * plugin)
00231 {
00232 cpl_recipe * recipe ;
00233
00234
00235 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00236 recipe = (cpl_recipe *)plugin ;
00237 else return -1 ;
00238
00239 cpl_parameterlist_delete(recipe->parameters) ;
00240 return 0 ;
00241 }
00242
00243
00250
00251 static int
00252 xsh_util_ima_arith( cpl_parameterlist * parlist,
00253 cpl_frameset * framelist)
00254 {
00255 const char* recipe_tags[1] = { XSH_RAW_IMA_SLIT_UVB};
00256 int recipe_tags_size = 1;
00257 cpl_parameter * param= NULL ;
00258 const char * operation=NULL;
00259 double value=1 ;
00260 cpl_image * ima1=NULL ;
00261 cpl_image * ima2=NULL ;
00262 int switch_ima2 = 0;
00263 const char * name_o=NULL ;
00264 cpl_propertylist * plist=NULL ;
00265 cpl_image * image=NULL ;
00266 cpl_frame * product_frame=NULL;
00267 cpl_frameset * raw_set=NULL;
00268 int nraw=0;
00269 int n=0;
00270 xsh_instrument* instrument=NULL;
00271 cpl_frameset* raws=NULL;
00272 cpl_frameset* calib=NULL;
00273 xsh_pre* pre = NULL;
00274 int pre_overscan_corr=0;
00275 xsh_msg("Welcome to XSHOOTER Pipeline release %d.%d.%d",
00276 XSH_MAJOR_VERSION,XSH_MINOR_VERSION,XSH_MICRO_VERSION);
00277
00278
00279
00280 check(param=cpl_parameterlist_find(parlist,
00281 "xsh.xsh_util_ima_arith.op"));
00282 check(operation=cpl_parameter_get_string(param));
00283
00284
00285 check(param=cpl_parameterlist_find(parlist,
00286 "xsh.xsh_util_ima_arith.value"));
00287 check(value = cpl_parameter_get_double(param)) ;
00288
00289
00290
00291
00292
00293
00294 check( xsh_begin( framelist, parlist, &instrument, &raws, &calib,
00295 recipe_tags, recipe_tags_size,
00296 XSH_UTL_IMA_ARITH_RECIPE_ID,
00297 XSH_UTL_IMA_ARITH, XSH_BINARY_VERSION,
00298 xsh_util_ima_arith_description_short ) ) ;
00299
00300
00301
00302 check_msg(xsh_dfs_set_groups(framelist),
00303 "Cannot identify RAW and CALIB frames") ;
00304
00305
00306 n=cpl_frameset_get_size(framelist);
00307 if(n<1) {
00308 xsh_msg_error("Empty input frame list!");
00309 goto cleanup ;
00310 }
00311
00312
00313 check_msg(raw_set=xsh_frameset_extract(framelist,XSH_RAW_IMA_SLIT_UVB),
00314 "Found no input frames with tag %s",XSH_RAW_IMA_SLIT_UVB);
00315 check(nraw=cpl_frameset_get_size(raw_set));
00316 if (nraw<1) {
00317 xsh_msg_error("Found no input frames with tag %s",XSH_RAW_IMA_SLIT_UVB);
00318 goto cleanup;
00319 } else {
00320 check(xsh_prepare(raw_set,NULL, NULL,"RAW_IMA", instrument,pre_overscan_corr,CPL_TRUE));
00321 check(ima1=cpl_image_load("RAW_IMA_PRE_0.fits",CPL_TYPE_FLOAT,0,0));
00322 if (nraw>1) {
00323 check(ima2 = cpl_image_load("RAW_IMA_PRE_1.fits",CPL_TYPE_FLOAT,0,0));
00324 switch_ima2=1;
00325 } else if (value == 9999.) {
00326 xsh_msg_error("Found only one input frames with tag %s",
00327 XSH_RAW_IMA_SLIT_UVB);
00328 goto cleanup;
00329 } else {
00330 xsh_msg("Perform image arithmetics on frame %s",
00331 XSH_RAW_IMA_SLIT_UVB);
00332 }
00333 }
00334
00335 xsh_free_frameset(&raw_set);
00336
00337
00338 check_msg(plist=cpl_propertylist_load("RAW_IMA_PRE_0.fits",0),
00339 "Cannot read the FITS header") ;
00340
00341
00342
00343 if (value == 9999.) {
00344
00345 if(ima1 != NULL && ima2 != NULL) {
00346 xsh_msg("ima1 %s ima2",operation);
00347 if (strcmp(operation,"+") == 0 ) {
00348 check_msg(image = cpl_image_add_create(ima1, ima2),
00349 "Cannot generate the %s image",operation) ;
00350 } else if (strcmp(operation,"-") == 0 ) {
00351 check_msg(image = cpl_image_subtract_create(ima1, ima2),
00352 "Cannot generate the %s image",operation) ;
00353 } else if (strcmp(operation,"*") == 0 ) {
00354 check_msg(image = cpl_image_multiply_create(ima1, ima2),
00355 "Cannot generate the %s image",operation) ;
00356 } else if (strcmp(operation,"/") == 0 ) {
00357 check_msg(image = cpl_image_divide_create(ima1, ima2),
00358 "Cannot generate the %s image",operation) ;
00359 } else {
00360 xsh_msg_error("Operation %s not supported",operation);
00361 goto cleanup;
00362 }
00363 xsh_free_image(&ima1);
00364 xsh_free_image(&ima2);
00365
00366 }
00367
00368 } else {
00369 xsh_msg("ima1 %s %f",operation,value);
00370
00371 if(switch_ima2 == 1) {
00372 xsh_free_image(&ima2);
00373 }
00374
00375 if (strcmp(operation,"+") == 0 ) {
00376 check_msg(image = cpl_image_add_scalar_create(ima1, value),
00377 "Cannot apply the %s operator",operation) ;
00378 } else if (strcmp(operation,"-") == 0 ) {
00379 check_msg(image = cpl_image_subtract_scalar_create(ima1, value),
00380 "Cannot apply the %s operator",operation) ;
00381 } else if (strcmp(operation,"*") == 0 ) {
00382 check_msg(image = cpl_image_multiply_scalar_create(ima1, value),
00383 "Cannot apply the %s operator",operation) ;
00384 } else if (strcmp(operation,"/") == 0 ) {
00385 check_msg(image = cpl_image_divide_scalar_create(ima1, value),
00386 "Cannot apply the %s operator",operation) ;
00387 } else {
00388 xsh_msg_error("Operation %s not supported",operation);
00389 goto cleanup;
00390 }
00391
00392 xsh_free_image(&ima1);
00393
00394 }
00395
00396
00397
00398
00399 name_o = "ima_res.fits" ;
00400
00401
00402 check(product_frame = cpl_frame_new());
00403 check(cpl_frame_set_filename(product_frame, name_o)) ;
00404 check(cpl_frame_set_tag(product_frame, PRO_IMA)) ;
00405 check(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
00406 check(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00407 check_msg(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
00408 "Error while initialising the product frame") ;
00409
00410
00411 check(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
00412 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)
00413 check_msg(cpl_dfs_setup_product_header(plist,
00414 product_frame,
00415 framelist,
00416 parlist,
00417 "xsh_util_ima_arith",
00418 "XSHOOTER",
00419 KEY_VALUE_HPRO_DID,NULL),
00420 "Problem in the product DFS-compliance") ;
00421 #else
00422 check_msg(cpl_dfs_setup_product_header(plist,
00423 product_frame,
00424 framelist,
00425 parlist,
00426 "xsh_util_ima_arith",
00427 "XSHOOTER",
00428 KEY_VALUE_HPRO_DID),
00429 "Problem in the product DFS-compliance") ;
00430 #endif
00431 check_msg(cpl_image_save(image,
00432 name_o,
00433 CPL_BPP_IEEE_FLOAT,
00434 plist,
00435 CPL_IO_DEFAULT),
00436 "Could not save product");
00437 xsh_free_propertylist(&plist) ;
00438 check(plist=cpl_propertylist_new());
00439 check( xsh_pfits_set_extname(plist, "ERRS"));
00440
00441 cpl_image_power(image,0.5);
00442 check_msg(cpl_image_save(image,
00443 name_o,
00444 CPL_BPP_IEEE_FLOAT,
00445 plist,
00446 CPL_IO_EXTEND),
00447 "Could not save product");
00448 xsh_free_propertylist(&plist) ;
00449 check(plist=cpl_propertylist_new());
00450 check( xsh_pfits_set_extname(plist, "QUAL"));
00451
00452
00453 cpl_image_divide(image,image);
00454 check_msg(cpl_image_save(image,
00455 name_o,
00456 CPL_BPP_IEEE_FLOAT,
00457 plist,
00458 CPL_IO_EXTEND),
00459 "Could not save product");
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469 xsh_pre_free(&pre);
00470
00471 xsh_free_propertylist(&plist) ;
00472 xsh_free_image(&image);
00473
00474
00475 check(cpl_frameset_insert(framelist, product_frame)) ;
00476
00477
00478 cleanup:
00479
00480 xsh_pre_free(&pre);
00481 xsh_free_image(&ima1);
00482 xsh_free_image(&ima2);
00483 xsh_free_frameset(&raw_set);
00484 xsh_free_propertylist(&plist) ;
00485
00486
00487
00488 xsh_free_image(&image) ;
00489
00490 if (cpl_error_get_code()) {
00491 return -1 ;
00492 } else {
00493 return 0 ;
00494 }
00495
00496 }