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 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <cpl.h>
00037
00038 #include <xsh_msg.h>
00039 #include <xsh_error.h>
00040
00041 #include "xsh_model_anneal.h"
00042 #include "xsh_model_kernel.h"
00043 #include "xsh_model_arm_constants.h"
00044 #include "xsh_model_io.h"
00045 #include "xsh_model_metric.h"
00046 #include "xsh_model_sa.h"
00047
00048 #define MAXIT 25000
00049
00050
00054
00055
00058
00066
00067 cpl_table * xsh_model_anneal_reduce(
00068 const char * xsh_config_file,
00069 const char * xsh_config_tag,
00070 const char * meas_coord_file)
00071 {
00072 cpl_table * conf_tab = NULL;
00073 int ii;
00074 int morder,sizearraywavelengths, DS_size,adim;
00075 int morder_it, morder_min, morder_max;
00076 struct xs_3 xs_model, *p_xs_3;
00077 ann_all_par all_par[300], *p_all_par;
00078 double abest[300];
00079 double amin[300];
00080 double amax[300];
00081 int aname[300];
00082
00083 DOUBLE flux_tot;
00084 int tot;
00085 coord *msp_coord = NULL;
00086 DOUBLE ** ref_ind = NULL;
00087 DOUBLE *p_wlarray = NULL;
00088
00089
00090 p_xs_3=&xs_model;
00091 p_all_par=&all_par[0];
00092 sizearraywavelengths=countlines(meas_coord_file);
00093 XSH_CALLOC( msp_coord, coord, sizearraywavelengths+5);
00094
00095 xsh_msg("%d \n",sizearraywavelengths);
00096
00097 xsh_msg("tag=%s",xsh_config_tag);
00098 adim=xsh_model_readfits(abest,
00099 amin,
00100 amax,
00101 aname,
00102 xsh_config_file,
00103 xsh_config_tag,
00104 p_xs_3,
00105 p_all_par);
00106
00107 xsh_msg("arm %d \n",p_xs_3->arm);
00108
00109 ref_ind=xsh_alloc2Darray(8,7);
00110
00111 if (p_xs_3->arm==0) {
00112 p_xs_3->BSIZE=UVB_BSIZE;
00113 p_xs_3->ASIZE=UVB_ASIZE;
00114 p_xs_3->chipxpix=UVB_chipxpix;
00115 p_xs_3->chipypix=UVB_chipypix;
00116 xsh_ref_ind_read(0,ref_ind,p_xs_3->temper);
00117 morder=UVB_morder;
00118 morder_it=5;
00119 morder_min=UVB_morder_min;
00120 morder_max=UVB_morder_max;
00121 }
00122 else if (p_xs_3->arm==1) {
00123 p_xs_3->BSIZE=VIS_BSIZE;
00124 p_xs_3->ASIZE=VIS_ASIZE;
00125 p_xs_3->chipypix=VIS_chipypix;
00126 p_xs_3->chipxpix=VIS_chipxpix;
00127 xsh_ref_ind_read(1,ref_ind,p_xs_3->temper);
00128 morder=VIS_morder;
00129 morder_it=7;
00130 morder_min=VIS_morder_min;
00131 morder_max=VIS_morder_max;
00132 }
00133 else if (p_xs_3->arm==2) {
00134 p_xs_3->ASIZE=NIR_ASIZE;
00135 p_xs_3->BSIZE=NIR_BSIZE;
00136 p_xs_3->chipxpix=NIR_chipxpix;
00137 p_xs_3->chipypix=NIR_chipypix;
00138 xsh_ref_ind_read(2,ref_ind,p_xs_3->t_ir_p2);
00139 morder=NIR_morder;
00140 morder_it=8;
00141 morder_min=NIR_morder_min;
00142 morder_max=NIR_morder_max;
00143 }
00144 else {
00145 printf("Arm not set. \n");
00146 return NULL;
00147 }
00148
00149 xsh_showmatrix(p_xs_3->e_slit);
00150
00151
00152 xsh_3_init(p_xs_3);
00153
00154
00155
00156
00157 DS_size = sizearraywavelengths;
00158
00159
00160 p_wlarray=xsh_alloc1Darray(DS_size);
00161
00162 get_meas_coordinates(sizearraywavelengths, msp_coord,(char*)meas_coord_file);
00163 flux_tot=0.0;
00164 tot=0;
00165
00166 for(ii=0;ii<sizearraywavelengths;ii++) {
00167 p_wlarray[ii]=msp_coord[ii].wave*1e-6;
00168
00169
00170
00171
00172
00173
00174 flux_tot+=msp_coord[ii].flux;
00175 tot+=1;
00176 }
00177
00178 for(ii=0;ii<sizearraywavelengths;ii++) {
00179 msp_coord[ii].flux=msp_coord[ii].flux*(double)(tot)/flux_tot;
00180 }
00181 #ifdef DEBUG
00182 printf ("Before anneal: \n");
00183 for (ii=0;ii<adim;ii++) {
00184 printf("%d %s %lf \n", aname[ii], (p_all_par+aname[ii])->name, abest[ii]);
00185 }
00186
00187 xsh_showmatrix(p_xs_3->e_slit);
00188
00189 int jj=0;
00190
00191 for (jj=0;jj<DS_size;jj++) {
00192 p_xs_3->es_y_tot=p_xs_3->es_y+p_xs_3->slit[msp_coord[jj].slit_pos]*p_xs_3->slit_scale;
00193 morder=msp_coord[jj].order;
00194 xsh_3_init(p_xs_3);
00195 xsh_3_eval(p_wlarray[jj],morder,ref_ind,p_xs_3);
00196 xsh_3_detpix(p_xs_3);
00197 printf("check %d %lf %lf %d %d %lf %lf %lf %lf %d\n",
00198 jj,p_wlarray[jj],p_xs_3->es_y_tot,p_xs_3->chippix[0],
00199 msp_coord[jj].arm,p_xs_3->xpospix,p_xs_3->ypospix,
00200 msp_coord[jj].x-p_xs_3->xpospix,msp_coord[jj].y-p_xs_3->ypospix,
00201 msp_coord[jj].order);
00202 }
00203 #endif
00204
00205 conf_tab = xsh_model_anneal_comp( p_all_par,
00206 adim,
00207 abest,
00208 amin,
00209 amax,
00210 aname,
00211 p_xs_3,
00212 DS_size,
00213 msp_coord,
00214 p_wlarray,
00215 ref_ind,
00216 MAXIT);
00217 cleanup:
00218 xsh_free2Darray(ref_ind,8);
00219 cpl_free(p_wlarray);
00220 return conf_tab ;
00221 }
00224
00225 void get_meas_coordinates(int sizearraywavelengths,
00226 coord* meas_coord,
00227 char* datname)
00228 {
00229 int feat_cnt=0;
00230
00231 FILE* measfile;
00232 measfile=fopen(datname,"r");
00233
00234 if(measfile ==NULL ){
00235 printf("Cannot open the measured coords file\n");
00236 exit(EXIT_FAILURE);}
00237
00238 while(fscanf(measfile, "%d %lf %lf %d %lf %d %lf %d\n",
00239 &meas_coord[feat_cnt].counter,
00240 &meas_coord[feat_cnt].y,
00241 &meas_coord[feat_cnt].x,
00242 &meas_coord[feat_cnt].arm,
00243 &meas_coord[feat_cnt].flux,
00244 &meas_coord[feat_cnt].slit_pos,
00245 &meas_coord[feat_cnt].wave,
00246 &meas_coord[feat_cnt].order) != EOF ){
00247 feat_cnt+=1;
00248 }
00249 fclose(measfile);
00250 if (feat_cnt != sizearraywavelengths) {
00251 printf("Number of spectral features is not equal to the number of wavelengths and slit posn's supplied! \n %d != %d \n",feat_cnt, sizearraywavelengths);
00252 exit(EXIT_FAILURE);}
00253 }
00254
00255
00256 int countlines (const char * filename)
00257 {
00258
00259
00260 char oneline[200];
00261 char *c;
00262 char firstchar[]="";
00263 char comment[]="%";
00264 FILE * myfile;
00265 int linecounter=0;
00266 if((myfile=fopen((char*)filename,"r"))==NULL){
00267 printf("Cannot open file %s for reading.\n",filename);
00268 exit(EXIT_FAILURE);
00269 }
00270
00271 do
00272 {
00273 c = fgets(oneline, 200, myfile);
00274 if (c != NULL)
00275 {
00276 strncpy(firstchar,oneline,1);
00277 if(strncmp(firstchar,comment,1))
00278
00279 linecounter++;
00280 }
00281
00282 }
00283 while (c != NULL);
00284 fclose(myfile);
00285 return linecounter;
00286 }
00287
00288
00289
00290
00291
00292