36 #include "sinfo_utl_cube_arith.h"
37 #include <sinfo_spectrum_ops.h>
38 #include <sinfo_new_cube_ops.h>
39 #include "sinfo_key_names.h"
40 #include "sinfo_pro_types.h"
41 #include <sinfo_error.h>
42 #include <sinfo_utilities.h>
43 #include <sinfo_utils_wrappers.h>
73 int sinfo_utl_cube_arith(
74 cpl_parameterlist * parlist,
75 cpl_frameset * framelist)
77 cpl_parameter * param =NULL;
78 const char * operation =NULL;
79 const char * name_c =NULL;
80 const char * name_s =NULL;
81 const char * name_o =NULL;
82 const char * name_d =
"start";
88 cpl_parameter * p=NULL;
89 cpl_frame * frm_cube=NULL ;
90 cpl_frame * frm_spct=NULL ;
92 cpl_propertylist * plist=NULL ;
93 cpl_image * image=NULL ;
94 cpl_frame * product_frame=NULL;
95 cpl_imagelist * cub_ims=NULL;
96 cpl_imagelist * cube_i=NULL;
97 cpl_imagelist * cube_o=NULL;
98 cpl_imagelist * cube_d=NULL;
99 cpl_image * std_star=NULL;
101 Vector* spectrum=NULL;
102 cpl_vector* vec=NULL;
104 check(sinfo_dfs_set_groups(framelist),
105 "Cannot identify RAW and CALIB frames") ;
110 check_nomsg(param = cpl_parameterlist_find(parlist,
111 "sinfoni.sinfo_utl_cube_arith.op"));
112 check_nomsg(operation = cpl_parameter_get_string(param));
113 name_o =
"out_cube.fits";
117 check_nomsg(param = cpl_parameterlist_find(parlist,
118 "sinfoni.sinfo_utl_cube_arith.value"));
119 check_nomsg(value = cpl_parameter_get_double(param));
122 check(frm_cube = cpl_frameset_find(framelist, SI_UTL_CUBE_ARITH_CUBE),
123 "SOF does not have a file tagged as %s",SI_UTL_CUBE_ARITH_CUBE);
125 check_nomsg(name_c = cpl_frame_get_filename(frm_cube));
126 check_nomsg(p = cpl_parameterlist_find(parlist,
127 "sinfoni.sinfo_utl_cube_arith.value"));
128 check_nomsg(def_value = cpl_parameter_get_default_double(p));
129 if (value == def_value) {
130 if ((frm_spct = cpl_frameset_find(framelist,
131 SI_UTL_CUBE_ARITH_SPECTRUM))==NULL) {
132 sinfo_msg(
"SOF does not have a file tagged as %s",
133 SI_UTL_CUBE_ARITH_SPECTRUM);
136 sinfo_msg(
"SOF contains a file tagged as %s",
137 SI_UTL_CUBE_ARITH_SPECTRUM);
138 name_s = cpl_frame_get_filename(frm_spct);
143 if (value == def_value) {
144 if ((frm_spct = cpl_frameset_find(framelist, PRO_SPECTRUM))!=NULL) {
145 sinfo_msg(
"SOF contains a file tagged as %s",PRO_SPECTRUM);
146 name_s = cpl_frame_get_filename(frm_spct);
148 }
else if ((frm_spct = cpl_frameset_find(framelist, PRO_IMA))!=NULL) {
149 sinfo_msg(
"SOF contains a file tagged as %s",PRO_IMA);
150 name_s = cpl_frame_get_filename(frm_spct);
153 sinfo_msg(
"SOF does not contains a file tagged as %s nor as %s",
154 PRO_SPECTRUM,PRO_IMA);
155 sinfo_msg(
"Assume operation by a constant");
159 check(plist=cpl_propertylist_load(cpl_frame_get_filename(frm_cube),0),
160 "Cannot read the FITS header") ;
165 check_nomsg(cube_i= cpl_imagelist_load((
char*)name_c,CPL_TYPE_FLOAT,0));
166 if (value == def_value) {
167 sinfo_msg(
"value equal to default");
168 if( strcmp(name_d,
"spectrum") == 0 ) {
169 sinfo_msg(
"Operation by spectrum");
170 sinfo_msg(
"loading spectrum image file %s",name_s);
171 check_nomsg(std_star=cpl_image_load((
char*)name_s,CPL_TYPE_FLOAT,0,0));
174 check_nomsg(spectrum = sinfo_new_image_to_vector(std_star));
175 if (strcmp(operation,
"/") == 0) {
176 cube_o = sinfo_new_div_cube_by_spectrum (cube_i, spectrum);
177 }
else if (strcmp(operation,
"-") == 0) {
178 cube_o = sinfo_new_sub_spectrum_from_cube(cube_i, spectrum);
179 }
else if (strcmp(operation,
"+") == 0) {
180 cube_o = sinfo_new_add_spectrum_to_cube(cube_i, spectrum);
181 }
else if (strcmp(operation,
"*") == 0) {
182 cube_o = sinfo_new_mul_spectrum_to_cube(cube_i, spectrum);
192 sinfo_msg(
"If an input spectrum is given and no value is set");
193 sinfo_msg(
"operations supported are: `/`,`-`,`+`,'*'");
196 if (strcmp(operation,
"calib") == 0) {
199 }
else if ( strcmp(name_d,
"image") == 0) {
200 sinfo_msg(
"Operation by image");
201 check_nomsg(std_star=cpl_image_load ((
char*)name_s,
202 CPL_TYPE_FLOAT,0,0));
203 if(strcmp(operation,
"+") == 0) {
204 cknull(cube_o = sinfo_new_add_image_to_cube(cube_i,std_star),
205 "operation %s failed",operation);
206 }
else if (strcmp(operation,
"-") == 0) {
207 cknull(cube_o = sinfo_new_sub_image_from_cube(cube_i,std_star),
208 "operation %s failed",operation);
209 }
else if (strcmp(operation,
"*") == 0) {
210 cknull(cube_o = sinfo_new_mul_image_to_cube(cube_i,std_star),
211 "operation %s failed",operation);
212 }
else if (strcmp(operation,
"/") == 0) {
213 cknull(cube_o = sinfo_new_div_cube_by_image(cube_i,std_star),
214 "operation %s failed",operation);
217 sinfo_msg(
"If an input image is set");
218 sinfo_msg(
"operations supported are: `-`, `+`, `*`, `/`");
222 sinfo_msg(
"Operation %s by constant",operation);
224 check_nomsg(cube_o = cpl_imagelist_duplicate(cube_i));
226 if(strcmp(operation,
"+") == 0) {
227 check(cpl_imagelist_add_scalar(cube_o,value),
228 "operation %s failed",operation);
229 }
else if (strcmp(operation,
"-") == 0) {
230 check(cpl_imagelist_subtract_scalar(cube_o,value),
231 "operation %s failed",operation);
232 }
else if (strcmp(operation,
"*") == 0) {
233 check(cpl_imagelist_multiply_scalar(cube_o,value),
234 "operation %s failed",operation);
235 }
else if (strcmp(operation,
"/") == 0) {
236 check(cpl_imagelist_divide_scalar(cube_o,value),
237 "operation %s failed",operation);
240 sinfo_msg(
"If an input value is set");
241 sinfo_msg(
"operations supported are: `-`, `+`, `*`, `/`");
249 sinfo_msg(
"Operation %s by constant",operation);
251 check_nomsg(cube_o = cpl_imagelist_duplicate(cube_i));
253 if(strcmp(operation,
"+") == 0) {
254 check(cpl_imagelist_add_scalar(cube_o,value),
255 "operation %s failed",operation);
256 }
else if (strcmp(operation,
"-") == 0) {
257 check(cpl_imagelist_subtract_scalar(cube_o,value),
258 "operation %s failed",operation);
259 }
else if (strcmp(operation,
"*") == 0) {
260 check(cpl_imagelist_multiply_scalar(cube_o,value),
261 "operation %s failed",operation);
262 }
else if (strcmp(operation,
"/") == 0) {
263 check(cpl_imagelist_divide_scalar(cube_o,value),
264 "operation %s failed",operation);
267 sinfo_msg(
"If an input value is set");
268 sinfo_msg(
"operations supported are: `-`, `+`, `*`, `/`");
276 check_nomsg(product_frame = cpl_frame_new());
277 check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
278 check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_CUBE_ARITH_PROCUBE)) ;
279 check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
280 check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
281 check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
282 "Error while initialising the product frame") ;
284 check_nomsg(cpl_propertylist_erase_regexp(plist,
"^ESO PRO CATG",0));
286 check(cpl_dfs_setup_product_header(plist, product_frame, framelist,
288 "si_sinfo_utl_cube_arith",
289 "SINFONI", KEY_VALUE_HPRO_DID,NULL),
290 "Problem in the product DFS-compliance") ;
293 check(cpl_imagelist_save(cube_o, name_o, CPL_BPP_IEEE_FLOAT, plist,
295 "Could not save product");
299 check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
304 sinfoni_free_vector(&vec);
305 sinfo_free_svector(&spectrum);
306 sinfo_free_svector(&bb);
308 sinfo_free_imagelist(&cube_d);
309 sinfo_free_imagelist(&cube_i);
310 sinfo_free_imagelist(&cube_o);
313 sinfo_free_image(&image);
314 sinfo_free_imagelist(&cub_ims);
315 sinfo_free_propertylist(&plist);
318 if (cpl_error_get_code())