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
00029 #ifdef HAVE_CONFIG_H
00030 # include <config.h>
00031 #endif
00032
00033
00034
00035
00036
00037
00038
00039 #include "sinfo_utl_cube_arith.h"
00040 #include <sinfo_spectrum_ops.h>
00041 #include <sinfo_new_cube_ops.h>
00042 #include "sinfo_key_names.h"
00043 #include "sinfo_pro_types.h"
00044 #include <sinfo_error.h>
00045 #include <sinfo_utils_wrappers.h>
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00063
00064
00065
00066
00067
00074
00075 int sinfo_utl_cube_arith(
00076 cpl_parameterlist * parlist,
00077 cpl_frameset * framelist)
00078 {
00079 cpl_parameter * param =NULL;
00080 const char * operation =NULL;
00081 const char * name_c =NULL;
00082 const char * name_s =NULL;
00083 const char * name_o =NULL;
00084 const char * name_d ="start";
00085
00086
00087 double value=0 ;
00088 double def_value=0 ;
00089
00090 cpl_parameter * p=NULL;
00091 cpl_frame * frm_cube=NULL ;
00092 cpl_frame * frm_spct=NULL ;
00093
00094 cpl_propertylist * plist=NULL ;
00095 cpl_image * image=NULL ;
00096 cpl_frame * product_frame=NULL;
00097 cpl_imagelist * cub_ims=NULL;
00098 cpl_imagelist * cube_i=NULL;
00099 cpl_imagelist * cube_o=NULL;
00100 cpl_imagelist * cube_d=NULL;
00101 cpl_image * std_star=NULL;
00102 Vector * bb=NULL;
00103 Vector* spectrum=NULL;
00104
00105
00106 check(sinfo_dfs_set_groups(framelist),
00107 "Cannot identify RAW and CALIB frames") ;
00108
00109
00110
00111
00112 check_nomsg(param = cpl_parameterlist_find(parlist,
00113 "sinfoni.sinfo_utl_cube_arith.op"));
00114 check_nomsg(operation = cpl_parameter_get_string(param));
00115 name_o = "out_cube.fits";
00116
00117
00118
00119 check_nomsg(param = cpl_parameterlist_find(parlist,
00120 "sinfoni.sinfo_utl_cube_arith.value"));
00121 check_nomsg(value = cpl_parameter_get_double(param));
00122
00123
00124 check(frm_cube = cpl_frameset_find(framelist, SI_UTL_CUBE_ARITH_CUBE),
00125 "SOF does not have a file tagged as %s",SI_UTL_CUBE_ARITH_CUBE);
00126
00127 check_nomsg(name_c = cpl_frame_get_filename(frm_cube));
00128 check_nomsg(p = cpl_parameterlist_find(parlist,
00129 "sinfoni.sinfo_utl_cube_arith.value"));
00130 check_nomsg(def_value = cpl_parameter_get_default_double(p));
00131 if (value == def_value) {
00132 if ((frm_spct = cpl_frameset_find(framelist,
00133 SI_UTL_CUBE_ARITH_SPECTRUM))==NULL) {
00134 sinfo_msg( "SOF does not have a file tagged as %s",
00135 SI_UTL_CUBE_ARITH_SPECTRUM);
00136 cpl_error_reset();
00137 } else {
00138 sinfo_msg( "SOF contains a file tagged as %s",
00139 SI_UTL_CUBE_ARITH_SPECTRUM);
00140 name_s = cpl_frame_get_filename(frm_spct);
00141 name_d = "spectrum";
00142 }
00143 }
00144
00145 if (value == def_value) {
00146 if ((frm_spct = cpl_frameset_find(framelist, PRO_IMA))==NULL) {
00147 sinfo_msg( "SOF does not have a file tagged as %s",PRO_IMA);
00148 cpl_error_reset();
00149 } else {
00150 sinfo_msg( "SOF contains a file tagged as %s",PRO_IMA);
00151 name_s = cpl_frame_get_filename(frm_spct);
00152 name_d = "image";
00153 }
00154 }
00155
00156 if (frm_spct == NULL) {
00157 sinfo_msg("Assuming operation with a constant");
00158 }
00159 check(plist=cpl_propertylist_load(cpl_frame_get_filename(frm_cube),0),
00160 "Cannot read the FITS header") ;
00161
00162
00163
00164
00165 check_nomsg(cube_i= cpl_imagelist_load((char*)name_c,CPL_TYPE_FLOAT,0));
00166 if (value == def_value) {
00167 sinfo_msg("value equal to default");
00168 if( strcmp(name_d,"spectrum") == 0 ) {
00169 sinfo_msg("Operation by spectrum");
00170 check(std_star=cpl_image_load ((char*)name_s,CPL_TYPE_FLOAT,0,0),
00171 "Could not load input STD STAR spectrum");
00172
00173 spectrum = sinfo_new_image_to_vector(std_star);
00174
00175 if (strcmp(operation,"/") == 0) {
00176 cube_o = sinfo_new_div_cube_by_spectrum (cube_i, spectrum);
00177 } else if (strcmp(operation,"-") == 0) {
00178 cube_o = sinfo_new_sub_spectrum_from_cube(cube_i, spectrum);
00179 } else if (strcmp(operation,"+") == 0) {
00180 cube_o = sinfo_new_add_spectrum_to_cube(cube_i, spectrum);
00181 } else if (strcmp(operation,"*") == 0) {
00182 cube_o = sinfo_new_mul_spectrum_to_cube(cube_i, spectrum);
00183
00184
00185
00186
00187
00188
00189
00190 } else {
00191 sinfo_msg_error("operation %s not supported",operation);
00192 sinfo_msg("If an input spectrum is given and no value is set");
00193 sinfo_msg("operations supported are: `/`,`-`,`+`,'*'");
00194 goto cleanup;
00195 }
00196 if (strcmp(operation,"calib") == 0) {
00197 goto cleanup;
00198 }
00199 } else if ( strcmp(name_d,"image") == 0) {
00200 sinfo_msg("Operation by image");
00201 check_nomsg(std_star=cpl_image_load ((char*)name_s,
00202 CPL_TYPE_FLOAT,0,0));
00203 if(strcmp(operation,"+") == 0) {
00204 cknull(cube_o = sinfo_new_add_image_to_cube(cube_i,std_star),
00205 "operation %s failed",operation);
00206 } else if (strcmp(operation,"-") == 0) {
00207 cknull(cube_o = sinfo_new_sub_image_from_cube(cube_i,std_star),
00208 "operation %s failed",operation);
00209 } else if (strcmp(operation,"*") == 0) {
00210 cknull(cube_o = sinfo_new_mul_image_to_cube(cube_i,std_star),
00211 "operation %s failed",operation);
00212 } else if (strcmp(operation,"/") == 0) {
00213 cknull(cube_o = sinfo_new_div_cube_by_image(cube_i,std_star),
00214 "operation %s failed",operation);
00215 } else {
00216 sinfo_msg_error("operation %s not supported",operation);
00217 sinfo_msg("If an input image is set");
00218 sinfo_msg("operations supported are: `-`, `+`, `*`, `/`");
00219 goto cleanup;
00220 }
00221 } else {
00222 sinfo_msg("Operation %s by constant",operation);
00223
00224 check_nomsg(cube_o = cpl_imagelist_duplicate(cube_i));
00225
00226 if(strcmp(operation,"+") == 0) {
00227 check(cpl_imagelist_add_scalar(cube_o,value),
00228 "operation %s failed",operation);
00229 } else if (strcmp(operation,"-") == 0) {
00230 check(cpl_imagelist_subtract_scalar(cube_o,value),
00231 "operation %s failed",operation);
00232 } else if (strcmp(operation,"*") == 0) {
00233 check(cpl_imagelist_multiply_scalar(cube_o,value),
00234 "operation %s failed",operation);
00235 } else if (strcmp(operation,"/") == 0) {
00236 check(cpl_imagelist_divide_scalar(cube_o,value),
00237 "operation %s failed",operation);
00238 } else {
00239 sinfo_msg_error("operation %s not supported",operation);
00240 sinfo_msg("If an input value is set");
00241 sinfo_msg("operations supported are: `-`, `+`, `*`, `/`");
00242 goto cleanup;
00243 }
00244
00245 }
00246
00247 } else {
00248
00249 sinfo_msg("Operation %s by constant",operation);
00250
00251 check_nomsg(cube_o = cpl_imagelist_duplicate(cube_i));
00252
00253 if(strcmp(operation,"+") == 0) {
00254 check(cpl_imagelist_add_scalar(cube_o,value),
00255 "operation %s failed",operation);
00256 } else if (strcmp(operation,"-") == 0) {
00257 check(cpl_imagelist_subtract_scalar(cube_o,value),
00258 "operation %s failed",operation);
00259 } else if (strcmp(operation,"*") == 0) {
00260 check(cpl_imagelist_multiply_scalar(cube_o,value),
00261 "operation %s failed",operation);
00262 } else if (strcmp(operation,"/") == 0) {
00263 check(cpl_imagelist_divide_scalar(cube_o,value),
00264 "operation %s failed",operation);
00265 } else {
00266 sinfo_msg_error("operation %s not supported",operation);
00267 sinfo_msg("If an input value is set");
00268 sinfo_msg("operations supported are: `-`, `+`, `*`, `/`");
00269 goto cleanup;
00270 }
00271
00272 }
00273
00274
00275
00276 check_nomsg(product_frame = cpl_frame_new());
00277 check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
00278 check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_CUBE_ARITH_PROCUBE)) ;
00279 check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
00280 check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00281 check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
00282 "Error while initialising the product frame") ;
00283
00284
00285 check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
00286
00287
00288 check(cpl_dfs_setup_product_header(plist, product_frame, framelist,
00289 parlist,
00290 "si_sinfo_utl_cube_arith",
00291 "SINFONI", KEY_VALUE_HPRO_DID),
00292 "Problem in the product DFS-compliance") ;
00293
00294
00295
00296 check(cpl_imagelist_save(cube_o, name_o, CPL_BPP_DEFAULT, plist,
00297 CPL_IO_DEFAULT),
00298 "Could not save product");
00299
00300
00301 check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
00302
00303
00304
00305 cleanup:
00306
00307 sinfo_free_svector(&spectrum);
00308 sinfo_free_svector(&bb);
00309 sinfo_free_imagelist(&cube_d);
00310 sinfo_free_imagelist(&cube_i);
00311 sinfo_free_imagelist(&cube_o);
00312 sinfo_free_image(&std_star);
00313 sinfo_free_image(&image);
00314 sinfo_free_imagelist(&cub_ims);
00315 sinfo_free_propertylist(&plist);
00316
00317
00318 if (cpl_error_get_code())
00319 return -1 ;
00320 else
00321 return 0 ;
00322 }