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 "eclipse.h"
00035 #include "fitshead.h"
00036 #include "cube_ops.h"
00037 #include "utl_cube2ima.h"
00038 #include "sinfoni_key_names.h"
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00060
00061 int si_utl_cube2ima(
00062 cpl_parameterlist * parlist,
00063 cpl_frameset * framelist)
00064 {
00065 const char * fctid = "si_utl_cube2ima" ;
00066 cpl_parameter * param =NULL ;
00067 const char * name_i =NULL;
00068 const char * name_o =NULL;
00069
00070 double ws=0 ;
00071 double we=0 ;
00072 double wc=0 ;
00073 double wd=0 ;
00074
00075
00076 int naxis3=0;
00077 double lams=0;
00078 double lame=0;
00079
00080 cpl_frame * frm_cub=NULL ;
00081
00082 cpl_propertylist * plist=NULL ;
00083 cpl_image * image=NULL ;
00084 cpl_image * imagew=NULL ;
00085 cpl_frame * product_frame=NULL;
00086
00087 OneCube * cube=NULL;
00088 OneImage * img=NULL;
00089 fits_header * header=NULL;
00090
00091
00092
00093
00094 name_o ="out_ima.fits";
00095
00096
00097 param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_cube2ima.ws");
00098 ws = cpl_parameter_get_double(param) ;
00099
00100
00101 param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_cube2ima.we");
00102 we = cpl_parameter_get_double(param) ;
00103
00104
00105 if (sinfoni_dfs_set_groups(framelist)) {
00106 cpl_msg_error(fctid, "Cannot identify RAW and CALIB frames") ;
00107 return -1 ;
00108 }
00109
00110
00111 if ((frm_cub = cpl_frameset_find(framelist, SI_UTL_CUBE2IMA_CUBE))==NULL) {
00112 cpl_msg_error(fctid, "SOF does not have a file tagged as %s",SI_UTL_CUBE2IMA_CUBE);
00113 return -1 ;
00114 }
00115 name_i = cpl_frame_get_filename(frm_cub);
00116 naxis3=spiffi_get_naxis3((char*)name_i);
00117 wd=spiffi_get_dispersion((char*)name_i);
00118 wc=spiffi_get_cenLambda((char*)name_i);
00119 lams=wc-naxis3*wd/2+wd;
00120 lame=lams+(naxis3-2)*wd;
00121 param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_cube2ima.ws");
00122 if(ws != cpl_parameter_get_default_double(param)) {
00123 if (ws < lams) {
00124 cpl_msg_warning(fctid,"ws =%f too low set it to %f",ws,lams);
00125 ws=lams;
00126 }
00127 } else {
00128 ws=lams;
00129 }
00130
00131 param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_cube2ima.we");
00132 if(we != cpl_parameter_get_default_double(param)) {
00133 if (we > lame) {
00134 cpl_msg_warning(fctid,"we =%f too low set it to %f",we,lame);
00135 we=lame;
00136 }
00137 } else {
00138 we=lame;
00139 }
00140
00141 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(frm_cub),
00142 0)) == NULL) {
00143 cpl_msg_error(fctid, "Cannot read the FITS header") ;
00144 return -1 ;
00145 }
00146
00147
00148 cube = load_cube((char*)name_i);
00149 header = fits_read_header((char*)name_i);
00150
00151
00152 if(NULL == (img = averageCubeToImageBetweenWaves(cube,wd,wc,ws,we)) ) {
00153 cpl_msg_error(fctid, "Cannot average cube to image in wavelength range [%f,%f] um",ws,we) ;
00154 destroy_cube(cube);
00155 fits_header_destroy(header);
00156 cpl_propertylist_delete(plist);
00157 return -1 ;
00158 }
00159
00160
00161
00162 imagew=cpl_image_wrap_float(img->lx,img->ly,img->data);
00163 image=cpl_image_duplicate(imagew);
00164 cpl_image_unwrap(imagew);
00165
00166
00167
00168
00169
00170
00171 product_frame = cpl_frame_new();
00172 cpl_frame_set_filename(product_frame, name_o) ;
00173 cpl_frame_set_tag(product_frame, SI_UTL_CUBE2IMA_PROIMA) ;
00174 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE) ;
00175 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT) ;
00176 cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL) ;
00177
00178 if (cpl_error_get_code()) {
00179
00180 cpl_msg_error(fctid, "Error while initialising the product frame") ;
00181 cpl_propertylist_delete(plist) ;
00182 cpl_frame_delete(product_frame) ;
00183 cpl_image_delete(image) ;
00184 return -1 ;
00185 }
00186
00187
00188 cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG");
00189 if (cpl_dfs_setup_product_header(plist, product_frame, framelist, parlist,
00190 "si_utl_cube2ima", "SINFONI", KEY_VALUE_HPRO_DID) != CPL_ERROR_NONE) {
00191 cpl_msg_error(fctid, "Problem in the product DFS-compliance") ;
00192 cpl_propertylist_delete(plist) ;
00193 cpl_frame_delete(product_frame) ;
00194 cpl_image_delete(image) ;
00195 return -1 ;
00196 }
00197
00198
00199 cpl_propertylist_erase_regexp(plist, "^CTYPE3");
00200 cpl_propertylist_erase_regexp(plist, "^CRPIX3");
00201 cpl_propertylist_erase_regexp(plist, "^CRVAL3");
00202 cpl_propertylist_erase_regexp(plist, "^CDELT3");
00203 cpl_propertylist_erase_regexp(plist, "^CUNIT3");
00204
00205 if (cpl_image_save(image, name_o, CPL_BPP_DEFAULT, plist,
00206 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00207 cpl_msg_error(fctid, "Could not save product");
00208 cpl_propertylist_delete(plist) ;
00209 cpl_frame_delete(product_frame) ;
00210 cpl_image_delete(image) ;
00211 return -1 ;
00212 }
00213 cpl_propertylist_delete(plist) ;
00214
00215
00216 cpl_frameset_insert(framelist, product_frame) ;
00217 destroy_cube(cube);
00218 destroy_image(img);
00219 fits_header_destroy(header);
00220 cpl_image_delete(image) ;
00221
00222
00223 if (cpl_error_get_code())
00224 return -1 ;
00225 else
00226 return 0 ;
00227 }