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
00030
00031
00032
00033
00034 #include "utl_spectrum_arith.h"
00035 #include <eclipse.h>
00036 #include <spectrum_ops.h>
00037 #include "sinfoni_key_names.h"
00038 #include <sinfoni_memory.h>
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00059
00060 int si_utl_spectrum_arith(
00061 cpl_parameterlist * parlist,
00062 cpl_frameset * framelist)
00063 {
00064 const char * fctid = "si_utl_spectrum_arith" ;
00065 cpl_parameter * param =NULL;
00066 const char * operation =NULL;
00067 const char * method =NULL;
00068 const char * name_i =NULL;
00069 const char * name_o =NULL;
00070
00071 double temp=0 ;
00072 double shift=0 ;
00073
00074 cpl_frame * frm_spct=NULL ;
00075
00076 cpl_propertylist * plist=NULL ;
00077 cpl_image * image =NULL;
00078 cpl_image * image_w =NULL;
00079
00080 cpl_frame * product_frame=NULL;
00081 OneImage * bb_img=NULL;
00082 OneImage * image_o=NULL;
00083 OneImage * image_i=NULL;
00084 OneImage * image_s=NULL;
00085
00086 Vector* bb=NULL;
00087 double* sub_shift=NULL;
00088
00089
00090
00091
00092 param = cpl_parameterlist_find(parlist, "sinfoni.si_utl_spectrum_arith.op");
00093 operation = cpl_parameter_get_string(param);
00094
00095 name_o = "out_ima.fits";
00096
00097 param = cpl_parameterlist_find(parlist, "sinfoni.si_utl_spectrum_arith.method");
00098 method = cpl_parameter_get_string(param);
00099
00100
00101 param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_spectrum_arith.temperature");
00102 temp = cpl_parameter_get_double(param) ;
00103
00104 param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_spectrum_arith.shift");
00105 shift = cpl_parameter_get_double(param) ;
00106
00107
00108
00109 if ((frm_spct = cpl_frameset_find(framelist, SI_UTL_SPECTRUM_ARITH_SPECTRUM))==NULL) {
00110 cpl_msg_error(fctid, "SOF does not have a file tagged as %s",SI_UTL_SPECTRUM_ARITH_SPECTRUM);
00111 return -1 ;
00112 }
00113
00114
00115 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(frm_spct),
00116 0)) == NULL) {
00117 cpl_msg_error(fctid, "Cannot read the FITS header") ;
00118 return -1 ;
00119 }
00120
00121
00122 name_i = cpl_frame_get_filename(frm_spct);
00123 image_i= load_image ((char*)name_i);
00124 if(strcmp(operation,"division") == 0) {
00125 bb = blackbodySpectrum ((char*)name_i, temp);
00126 bb_img = vectorToImage(bb);
00127 image_o = divImageBySpectrum (image_i,bb_img);
00128 } else if (strcmp(operation,"shift") == 0) {
00129
00130 sub_shift = new_double_array(1);
00131 doublearray_set_value(sub_shift, 0., 0);
00132 image_s = shiftImageInSpec (image_i, shift, sub_shift);
00133
00134 if (image_s == NULL){
00135 cpl_msg_error(fctid,"error in shiftImageInSpec()");
00136 return -1;
00137 }
00138 shift = doublearray_get_value(sub_shift, 0);
00139
00140 if (strcmp(method,"S")==0) {
00141 image_o = fineShiftImageInSpecCubicspline ( image_s, shift );
00142 if (image_o == NULL){
00143 cpl_msg_error(fctid,"error in fineShiftImageInSpecCubicSpline()");
00144 cpl_propertylist_delete(plist);
00145 return -1;
00146 }
00147 } else if (strcmp(method,"P")==0) {
00148 image_o = fineShiftImageInSpecPoly( image_s, shift, 2 );
00149 if (image_o == NULL) {
00150 cpl_msg_error(fctid,"error in fineShiftImageInSpecPoly()");
00151 destroy_doublearray(sub_shift);
00152 cpl_propertylist_delete(plist);
00153 return -1;
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 } else {
00168 cpl_msg_error(fctid,"wrong method %s",method);
00169 destroy_doublearray(sub_shift);
00170 cpl_propertylist_delete(plist);
00171 return -1;
00172 }
00173 } else {
00174 cpl_msg_error(fctid,"Operation %s not supported",operation);
00175 cpl_propertylist_delete(plist);
00176 return -1;
00177 }
00178
00179
00180
00181 image_w=cpl_image_wrap_float(image_o->lx,image_o->ly,image_o->data);
00182 image=cpl_image_duplicate(image_w);
00183 cpl_image_unwrap(image_w);
00184
00185
00186 product_frame = cpl_frame_new();
00187 cpl_frame_set_filename(product_frame, name_o) ;
00188 cpl_frame_set_tag(product_frame, SI_UTL_SPECTRUM_ARITH_PROSPECTRUM) ;
00189 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE) ;
00190 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT) ;
00191 cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL) ;
00192 if (cpl_error_get_code()) {
00193 cpl_msg_error(fctid, "Error while initialising the product frame") ;
00194 cpl_propertylist_delete(plist) ;
00195 cpl_frame_delete(product_frame) ;
00196 cpl_image_delete(image) ;
00197 destroy_image(image_i);
00198 destroy_image(image_o);
00199 if(strcmp(operation,"division") == 0) {
00200 if(bb != NULL) {
00201
00202 }
00203 if(bb_img != NULL) {
00204 destroy_image(bb_img);
00205 }
00206 } else {
00207 destroy_image(image_s);
00208 destroy_doublearray(sub_shift);
00209 }
00210 sinfoni_memory_status();
00211 return -1 ;
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 if (cpl_image_save(image, name_o, CPL_BPP_DEFAULT, plist,
00228 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00229 cpl_msg_error(fctid, "Could not save product");
00230 cpl_propertylist_delete(plist) ;
00231 cpl_frame_delete(product_frame) ;
00232 cpl_image_delete(image) ;
00233 return -1 ;
00234 }
00235 cpl_propertylist_delete(plist) ;
00236 cpl_image_delete(image) ;
00237
00238
00239
00240
00241 cpl_frameset_insert(framelist, product_frame) ;
00242
00243 destroy_image(image_i);
00244 destroy_image(image_o);
00245
00246 if(strcmp(operation,"division") == 0) {
00247 if(bb != NULL) {
00248
00249 }
00250 if(bb_img != NULL) {
00251 destroy_image(bb_img);
00252 }
00253 } else {
00254 destroy_image(image_s);
00255 destroy_doublearray(sub_shift);
00256 }
00257 sinfoni_memory_status();
00258
00259 if (cpl_error_get_code())
00260 return -1 ;
00261 else
00262 return 0 ;
00263 }