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_wavelength_shift.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_wavelength_shift(
00061 cpl_parameterlist * parlist,
00062 cpl_frameset * framelist)
00063 {
00064 const char * fctid = "si_utl_spectrum_wavelength_shift" ;
00065 cpl_parameter * param =NULL;
00066 const char * method =NULL;
00067 const char * name_i =NULL;
00068 const char * name_o =NULL;
00069
00070 double shift=0 ;
00071
00072 cpl_frame * frm_spct=NULL ;
00073
00074 cpl_propertylist * plist=NULL ;
00075 cpl_image * image =NULL;
00076 cpl_image * image_w =NULL;
00077
00078 cpl_frame * product_frame=NULL;
00079 OneImage * image_o=NULL;
00080 OneImage * image_i=NULL;
00081 OneImage * image_s=NULL;
00082
00083 double* sub_shift=NULL;
00084
00085
00086
00087
00088 name_o = "out_ima.fits";
00089
00090
00091 param = cpl_parameterlist_find(parlist, "sinfoni.si_utl_spectrum_arith.method");
00092 method = cpl_parameter_get_string(param);
00093
00094
00095 param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_spectrum_wavelength_shift.shift");
00096 shift = cpl_parameter_get_double(param) ;
00097
00098
00099
00100 if ((frm_spct = cpl_frameset_find(framelist, SI_UTL_SPECTRUM_WAVELENGTH_SHIFT_SPECTRUM))==NULL) {
00101 cpl_msg_error(fctid, "SOF does not have a file tagged as %s",SI_UTL_SPECTRUM_WAVELENGTH_SHIFT_SPECTRUM);
00102 return -1 ;
00103 }
00104
00105
00106 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(frm_spct),
00107 0)) == NULL) {
00108 cpl_msg_error(fctid, "Cannot read the FITS header") ;
00109 return -1 ;
00110 }
00111
00112
00113 name_i = cpl_frame_get_filename(frm_spct);
00114 image_i= load_image ((char*)name_i);
00115
00116 sub_shift = new_double_array(1);
00117 doublearray_set_value(sub_shift, 0., 0);
00118 image_s = shiftImageInSpec (image_i, shift, sub_shift);
00119
00120 if (image_s == NULL){
00121 cpl_msg_error(fctid,"error in shiftImageInSpec()");
00122 return -1;
00123 }
00124 shift = doublearray_get_value(sub_shift, 0);
00125
00126 if (strcmp(method,"S")==0) {
00127 image_o = fineShiftImageInSpecCubicspline ( image_s, shift );
00128 if (image_o == NULL){
00129 cpl_msg_error(fctid,"error in fineShiftImageInSpecCubicSpline()");
00130 cpl_propertylist_delete(plist);
00131 return -1;
00132 }
00133 } else if (strcmp(method,"P")==0) {
00134 image_o = fineShiftImageInSpecPoly( image_s, shift, 2 );
00135 if (image_o == NULL) {
00136 cpl_msg_error(fctid,"error in fineShiftImageInSpecPoly()");
00137 destroy_doublearray(sub_shift);
00138 cpl_propertylist_delete(plist);
00139 return -1;
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 } else {
00154 cpl_msg_error(fctid,"wrong method %s",method);
00155 destroy_doublearray(sub_shift);
00156 cpl_propertylist_delete(plist);
00157 return -1;
00158 }
00159
00160
00161
00162 image_w=cpl_image_wrap_float(image_o->lx,image_o->ly,image_o->data);
00163 image=cpl_image_duplicate(image_w);
00164 cpl_image_unwrap(image_w);
00165
00166
00167 product_frame = cpl_frame_new();
00168 cpl_frame_set_filename(product_frame, name_o) ;
00169 cpl_frame_set_tag(product_frame, SI_UTL_SPECTRUM_WAVELENGTH_SHIFT_PROSPECTRUM) ;
00170 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE) ;
00171 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT) ;
00172 cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL) ;
00173 if (cpl_error_get_code()) {
00174 cpl_msg_error(fctid, "Error while initialising the product frame") ;
00175 cpl_propertylist_delete(plist) ;
00176 cpl_frame_delete(product_frame) ;
00177 cpl_image_delete(image) ;
00178 destroy_image(image_i);
00179 destroy_image(image_o);
00180 destroy_image(image_s);
00181 destroy_doublearray(sub_shift);
00182 sinfoni_memory_status();
00183 return -1 ;
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 if (cpl_image_save(image, name_o, CPL_BPP_DEFAULT, plist,
00201 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00202 cpl_msg_error(fctid, "Could not save product");
00203 cpl_propertylist_delete(plist) ;
00204 cpl_frame_delete(product_frame) ;
00205 cpl_image_delete(image) ;
00206 destroy_image(image_i);
00207 destroy_image(image_o);
00208 destroy_image(image_s);
00209 destroy_doublearray(sub_shift);
00210 sinfoni_memory_status();
00211 return -1 ;
00212 }
00213 cpl_propertylist_delete(plist) ;
00214 cpl_image_delete(image) ;
00215
00216
00217
00218
00219 cpl_frameset_insert(framelist, product_frame) ;
00220
00221 destroy_image(image_i);
00222 destroy_image(image_o);
00223
00224 destroy_image(image_s);
00225 destroy_doublearray(sub_shift);
00226 sinfoni_memory_status();
00227
00228 if (cpl_error_get_code())
00229 return -1 ;
00230 else
00231 return 0 ;
00232 }