00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "create_resampled.h"
00020 #include "sinfoni_dfs.h"
00021 #include "cubecreate_ini_by_cpl.h"
00022
00023
00024
00025
00026
00027
00028
00029
00030 void
00031 change_plist_create_resampled(cpl_propertylist * plist,
00032 char* ini_name,
00033 char* outName,
00034 float cenLambda,
00035 float dispersion,
00036 int cenpix) {
00037
00038
00039 char blank[FILENAMESZ] ;
00040 char firsttext[2*FILENAMESZ] ;
00041 char * date ;
00042
00043 strcpy(firsttext, "spiffi_cubecreate_resampled -f \0") ;
00044 sprintf(blank, "%f", ZERO) ;
00045
00046
00047 cpl_propertylist_insert_after_string(plist, "EXPTIME", "CTYPE2", "WAVE" ) ;
00048 cpl_propertylist_set_comment(plist, "WAVE", "wavelength axis in microns" ) ;
00049
00050 sprintf(blank, "%d", cenpix+1) ;
00051 cpl_propertylist_insert_after_string(plist, "EXPTIME", "CTYPE2", "CRPIX2" ) ;
00052 cpl_propertylist_set_comment(plist, "CRPIX2", "Reference pixel in z" ) ;
00053
00054
00055 sprintf(blank, "%f", cenLambda) ;
00056 cpl_propertylist_insert_after_string(plist, "EXPTIME", "CRPIX2", "CRVAL2" ) ;
00057 cpl_propertylist_set_comment(plist, "CRVAL2", "central wavelength" ) ;
00058
00059 sprintf(blank, "%f", dispersion) ;
00060 cpl_propertylist_insert_after_string(plist, "EXPTIME", "CRVAL2", "CDELT2" ) ;
00061 cpl_propertylist_set_comment(plist, "CDELT2", "microns per pixel" ) ;
00062
00063 cpl_propertylist_insert_after_string(plist, "EXPTIME", "CDELT2", "CUNIT2" ) ;
00064 cpl_propertylist_set_comment(plist, "CUNIT2", "spectral unit" ) ;
00065
00066 date = get_datetime_iso8601() ;
00067 strcat (firsttext , ini_name) ;
00068
00069
00070 strcat(firsttext,ini_name);
00071
00072 if (strlen(firsttext) > 30) {
00073
00074
00075
00076
00077
00078 cpl_propertylist_set_string(plist,KEY_NAME_PIPEFILE, outName ) ;
00079 cpl_propertylist_set_string(plist,KEY_NAME_HPRO_TYPE, "REDUCED" ) ;
00080 cpl_propertylist_set_string(plist,KEY_NAME_HPRO_CATG, "Data Cube creation" ) ;
00081 cpl_propertylist_set_string(plist,KEY_NAME_HPRO_STATUS, "OK" ) ;
00082 cpl_propertylist_set_string(plist,KEY_NAME_HPRO_DATE, date ) ;
00083 cpl_propertylist_set_string(plist,KEY_NAME_HPRO_RECID, firsttext ) ;
00084 cpl_propertylist_set_string(plist,KEY_NAME_HPRO_DRSID, PACKAGE_VERSION ) ;
00085
00086 }
00087
00088 }
00089
00090 int create_resampled (cpl_parameterlist* config, cpl_frameset* sof)
00091 {
00092
00093 const char* _id = "create_resampled";
00094 cube_config * cfg=NULL ;
00095 OneImage * im=NULL ;
00096 OneImage * wavemapim=NULL ;
00097 OneImage * resampledImage=NULL ;
00098 OneImage * rawresampledImage=NULL ;
00099 fits_header * head=NULL ;
00100 double dis=0 ;
00101 float mi=0 ;
00102 float ma=0 ;
00103 double centralLambda=0 ;
00104 float* distances=NULL;
00105
00106
00107
00108 double disp=0;
00109 float min=0;
00110 float max=0;
00111 double centralLam=0;
00112
00113 int cenpix=0 ;
00114 int centralpix=0 ;
00115 int i=0;
00116 float tmp_float=0;
00117 int status=0;
00118 char* tbl_distances_name=NULL;
00119 cpl_table* tbl_distances=NULL;
00120 cpl_image* rimg=NULL;
00121 cpl_propertylist* cplist=NULL;
00122 cpl_frameset* stk=NULL;
00123
00124
00125
00126
00127
00128
00129 cfg = parse_cpl_input_cube(config,sof,&stk) ;
00130 if (cfg == NULL)
00131 {
00132 cpl_msg_error(_id," could not parse cpl input!") ;
00133 return -1 ;
00134 }
00135
00136
00137
00138 if(is_fits_file (cfg->inFrame) != 1) {
00139 cpl_msg_error(_id,"Input file %s is not FITS",cfg->inFrame);
00140 return -1;
00141 }
00142
00143 if(is_fits_file (cfg->wavemap) != 1) {
00144 cpl_msg_error(_id,"Input file %s is not FITS",cfg->wavemap);
00145 return -1;
00146 }
00147
00148 if (cfg->halocorrectInd == 1) {
00149 if(is_fits_file (cfg->halospectrum) != 1) {
00150 cpl_msg_error(_id,"Input file %s is not FITS",cfg->halospectrum);
00151 return -1;
00152 }
00153 }
00154
00155 im = load_image(cfg->inFrame);
00156 if (im == NULL) {
00157 printf ( "Error: could not load inFrame\n" );
00158 return -1;
00159 }
00160 wavemapim = load_image(cfg->wavemap);
00161 if (wavemapim == NULL) {
00162 printf ( "Error: could not load wavemap\n" );
00163 return -1;
00164 }
00165 head = fits_read_header(cfg->inFrame);
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 rawresampledImage =
00188 definedResampling( im, wavemapim, cfg->ncoeffs, &cfg->nrows,
00189 &dis, &mi, &ma, ¢ralLambda, ¢ralpix);
00190
00191 if (resampledImage == NULL) {
00192 printf ( "Error: definedResampling() failed\n" );
00193 return -1;
00194 }
00195
00196 distances = new_float_array(cfg->nslits-1) ;
00197
00198
00199
00200 distances = (float*) cpl_calloc (cfg->nslits - 1, sizeof (float));
00201 if ( NULL == distances)
00202 {
00203 cpl_msg_error(_id," could allocate memory!") ;
00204 return -1 ;
00205 }
00206
00207
00208
00209 tbl_distances_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
00210 strcpy(tbl_distances_name,cfg->poslist);
00211 tbl_distances = cpl_table_load(tbl_distances_name,1,0);
00212 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00213 cpl_msg_error(_id,"error loading tbl %s",tbl_distances_name);
00214 cpl_msg_error(_id,(char* ) cpl_error_get_message());
00215 return -1;
00216 }
00217 for (i =0 ; i< cfg->nslits-1; i++){
00218 tmp_float=cpl_table_get_float(tbl_distances,
00219 "slitlet_distance",i,&status);
00220 array_set_value(distances,tmp_float,i);
00221
00222 }
00223 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00224 cpl_msg_error(_id,"error reading tbl %s",tbl_distances_name);
00225 cpl_msg_error(_id,(char* ) cpl_error_get_message());
00226 return -1;
00227 }
00228
00229 cpl_table_delete(tbl_distances);
00230 cpl_free(tbl_distances_name);
00231
00232
00233 resampledImage=maketrueResamp(rawresampledImage, distances, wavemapim);
00234 disp = 0;
00235 min = 0;
00236 max = 0;
00237 centralLam = 0;
00238 cenpix = 0;
00239
00240 printf( "dispersion%f\n", disp);
00241 printf( "minimal lambda %f\n", min);
00242 printf( "maximal lambda %f\n", max);
00243 printf( "central lambda %f\n", centralLam);
00244 printf( "central pixel %d\n", cenpix);
00245
00246
00247
00248 cplist = cpl_propertylist_new() ;
00249 if ((cpl_error_code)((cplist = cpl_propertylist_load(cfg->outName, 0)) == NULL)) {
00250 cpl_msg_error(_id, "getting header from frame %s",cfg->outName);
00251 cpl_propertylist_delete(cplist) ;
00252 return -1 ;
00253 }
00254
00255 change_plist_create_resampled(cplist, cfg->inFrame, cfg->outName,
00256 centralLam, disp, cenpix);
00257
00258 rimg=cpl_image_wrap_float(resampledImage->lx,resampledImage->ly,resampledImage->data);
00259 if (cpl_image_save(rimg, cfg->outName, CPL_BPP_DEFAULT, cplist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00260 cpl_msg_error(_id, "Cannot save the product %s",cfg->outName);
00261 cpl_propertylist_delete(cplist) ;
00262 return -1 ;
00263 }
00264
00265 cpl_propertylist_delete(cplist) ;
00266 destroy_image(im);
00267 destroy_image(wavemapim);
00268
00269
00270
00271 fits_header_destroy(head);
00272 destroy_image(resampledImage);
00273 cube_cfg_destroy (cfg);
00274 return 0;
00275 }
00276