X-shooter Pipeline Reference Manual 3.8.15
xsh_model_anneal.c
Go to the documentation of this file.
1/* $Id: xsh_model_anneal.c,v 1.20 2011-12-02 14:15:28 amodigli Exp $
2 *
3 * This file is part of the ESO X-shooter Pipeline
4 * Copyright (C) 2006 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * $Author: amodigli $
23 * $Date: 2011-12-02 14:15:28 $
24 * $Revision: 1.20 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32/*-----------------------------------------------------------------------------
33 Includes
34 -----------------------------------------------------------------------------*/
35
36#include <cpl.h>
37
38#include <xsh_msg.h>
39#include <xsh_error.h>
40
41#include "xsh_model_anneal.h"
42#include "xsh_model_kernel.h"
44#include "xsh_model_io.h"
45#include "xsh_model_metric.h"
46#include "xsh_model_sa.h"
47
48#define MAXIT 25000
49
50/*----------------------------------------------------------------------------*/
54/*----------------------------------------------------------------------------*/
55
58/*----------------------------------------------------------------------------*/
66/*----------------------------------------------------------------------------*/
68 const char * xsh_config_file,
69 const char * xsh_config_tag,
70 const char * meas_coord_file)
71{
72 cpl_table * conf_tab = NULL;
73 int ii;
74 int morder,sizearraywavelengths, DS_size,adim;
75 int morder_it;
76 int morder_min, morder_max;
77 struct xs_3 xs_model, *p_xs_3;
78 ann_all_par all_par[300], *p_all_par;
79 double abest[300];
80 double amin[300];
81 double amax[300];
82 int aname[300];
83
84 DOUBLE flux_tot;
85 int tot;
86 coord *msp_coord = NULL;
87 DOUBLE ** ref_ind = NULL;
88 DOUBLE *p_wlarray = NULL;
89
90
91 p_xs_3=&xs_model;
92 p_all_par=&all_par[0];
93 sizearraywavelengths=countlines(meas_coord_file);
94 XSH_CALLOC( msp_coord, coord, sizearraywavelengths+5);
95
96 xsh_msg("%d \n",sizearraywavelengths);
97 /*wavelength = (DOUBLE *)cpl_malloc(sizearraywavelengths*sizeof(DOUBLE));*/
98 xsh_msg("tag=%s",xsh_config_tag);
99 adim=xsh_model_readfits(abest,
100 amin,
101 amax,
102 aname,
103 xsh_config_file,
104 xsh_config_tag,
105 p_xs_3,
106 p_all_par);
107
108 xsh_msg("arm %d \n",p_xs_3->arm);
109
110 ref_ind=xsh_alloc2Darray(8,7);
111
112 if (p_xs_3->arm==0) {
113 p_xs_3->BSIZE=UVB_BSIZE;
114 p_xs_3->ASIZE=UVB_ASIZE;
115 p_xs_3->chipxpix=UVB_chipxpix;
116 p_xs_3->chipypix=UVB_chipypix;
117 xsh_ref_ind_read(0,ref_ind,p_xs_3->temper);
119 morder_it=5;
122 }
123 else if (p_xs_3->arm==1) {
124 p_xs_3->BSIZE=VIS_BSIZE;
125 p_xs_3->ASIZE=VIS_ASIZE;
126 p_xs_3->chipypix=VIS_chipypix;
127 p_xs_3->chipxpix=VIS_chipxpix;
128 xsh_ref_ind_read(1,ref_ind,p_xs_3->temper);
130 morder_it=7;
133 }
134 else if (p_xs_3->arm==2) {
135 p_xs_3->ASIZE=NIR_ASIZE;
136 p_xs_3->BSIZE=NIR_BSIZE;
137 p_xs_3->chipxpix=NIR_chipxpix;
138 p_xs_3->chipypix=NIR_chipypix;
139 xsh_ref_ind_read(2,ref_ind,p_xs_3->t_ir_p2);
141 morder_it=8;
144 }
145 else {
146 printf("Arm not set. \n");
147 return NULL;
148 }
149
150 xsh_showmatrix(p_xs_3->e_slit);
151 /*We now need to update several matrices (that were set in the
152 xsh_3_init routine outside of the current loop) for this prism angle */
153 xsh_3_init(p_xs_3);
154
155 /* Size of the data structure is simply the length of the coords file, since
156 wavelengths matched in more than one order are lister each time (unlike
157 the x-dispersion for stisanneal)*/
158 DS_size = sizearraywavelengths;
159
160 /* now allocate memory for the data structure*/
161 p_wlarray=xsh_alloc1Darray(DS_size);
162
163 get_meas_coordinates(sizearraywavelengths, msp_coord,(char*)meas_coord_file);
164 flux_tot=0.0;
165 tot=0;
166 /* get total flux */
167 for(ii=0;ii<sizearraywavelengths;ii++) {
168 p_wlarray[ii]=msp_coord[ii].wave*1e-6;
169 /* below is a quick hack to allow an offset in the
170 co-ordinates eg 0.5 for pixel centres*/
171/* if (p_xs_3->arm==0) { */
172/* msp_coord[ii].x=2144.0-msp_coord[ii].x-0.0; */
173/* msp_coord[ii].y=3000.0-msp_coord[ii].y-0.0; */
174/* } */
175 flux_tot+=msp_coord[ii].flux;
176 tot+=1;
177 }
178 /* Normalise fluxes */
179 for(ii=0;ii<sizearraywavelengths;ii++) {
180 msp_coord[ii].flux=msp_coord[ii].flux*(double)(tot)/flux_tot;
181 }
182#ifdef DEBUG
183 printf ("Before anneal: \n");
184 for (ii=0;ii<adim;ii++) {
185 printf("%d %s %lf \n", aname[ii], (p_all_par+aname[ii])->name, abest[ii]);
186 }
187
188 xsh_showmatrix(p_xs_3->e_slit);
189 /*Call the model kernel with the initial values*/
190 int jj=0;
191
192 for (jj=0;jj<DS_size;jj++) {
193 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;
194 morder=msp_coord[jj].order;
195 xsh_3_init(p_xs_3);
196 xsh_3_eval(p_wlarray[jj],morder,ref_ind,p_xs_3);
197 xsh_3_detpix(p_xs_3);
198 printf("check %d %lf %lf %d %d %lf %lf %lf %lf %d\n",
199 jj,p_wlarray[jj],p_xs_3->es_y_tot,p_xs_3->chippix[0],
200 msp_coord[jj].arm,p_xs_3->xpospix,p_xs_3->ypospix,
201 msp_coord[jj].x-p_xs_3->xpospix,msp_coord[jj].y-p_xs_3->ypospix,
202 msp_coord[jj].order);
203 }
204#endif
205 /*Call the main anneal algorithm in crires_model_metric.c*/
206 conf_tab = xsh_model_anneal_comp( p_all_par,
207 adim,
208 abest,
209 amin,
210 amax,
211 aname,
212 p_xs_3,
213 DS_size,
214 msp_coord,
215 p_wlarray,
216 ref_ind,
217 MAXIT);
218 cleanup:
219 xsh_free2Darray(ref_ind,8);
220 cpl_free(p_wlarray);
221 return conf_tab ;
222}
225/*The number of measured co-ordinates must equal the number of wavelengths in meas_coord_file (and they should also correspond)*/
226void get_meas_coordinates(int sizearraywavelengths,
227 coord* meas_coord,
228 char* datname)
229{
230 int feat_cnt=0;
231
232 FILE* measfile;
233 measfile=fopen(datname,"r");
234 /* FILE * matchfile=fopen("match.dat","r");*/
235 if(measfile ==NULL ){
236 printf("Cannot open the measured coords file\n");
237 abort();}
238
239 while(fscanf(measfile, "%64d %64lf %64lf %64d %64lf %64d %64lf %64d\n",
240 &meas_coord[feat_cnt].counter,
241 &meas_coord[feat_cnt].y,
242 &meas_coord[feat_cnt].x,
243 &meas_coord[feat_cnt].arm,
244 &meas_coord[feat_cnt].flux,
245 &meas_coord[feat_cnt].slit_pos,
246 &meas_coord[feat_cnt].wave,
247 &meas_coord[feat_cnt].order) != EOF ){
248 feat_cnt+=1;
249 }
250 fclose(measfile);
251 if (feat_cnt != sizearraywavelengths) {
252 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);
253 abort();}
254}
255
256/*count lines of the calibration files in order to dimension the array's size.*/
257int countlines (const char * filename)
258 {
259
260 /* count lines ignore Comments (lines start with a %)*/
261 char oneline[200];
262 char *c;
263 char firstchar[]="";
264 char comment[]="%";
265 FILE * myfile;
266 int linecounter=0;
267 if((myfile=fopen((char*)filename,"r"))==NULL){
268 printf("Cannot open file %s for reading.\n",filename);
269 abort();
270 }
271
272 do
273 {
274 c = fgets(oneline, 200, myfile); /* get one line from the file */
275 if (c != NULL)
276 {
277 strncpy(firstchar,oneline,1);
278 if(strncmp(firstchar,comment,1))
279 /*if is not a comment increment linecounter*/
280 linecounter++;
281 }
282
283 }
284 while (c != NULL); /* repeat until NULL */
285 fclose(myfile);
286 return linecounter;
287 }
288
289
290
291
292
293
cpl_table * xsh_model_anneal_reduce(const char *xsh_config_file, const char *xsh_config_tag, const char *meas_coord_file)
The actual anneal computation takes place here.
int xsh_model_readfits(double *abest, double *amin, double *amax, int *aname, const char *xs_config_file, const char *xs_config_tag, struct xs_3 *p_xs_3, ann_all_par *p_all_par)
Definition: xsh_model_io.c:570
void xsh_ref_ind_read(int arm, DOUBLE **ref_ind_T, DOUBLE temper)
Load the refractive index coeffs for a given arm and temperature.
void xsh_3_detpix(struct xs_3 *p_xs_3)
Takes the physical x,y position at the detector array and converts this to a pixel position.
void xsh_3_eval(DOUBLE lambda, int morder, DOUBLE **ref_ind, struct xs_3 *p_xs_3)
Compute the physical x,y position at the detector array for a given wavelength, order and parameter c...
void xsh_showmatrix(mat A)
Show matrix.
DOUBLE * xsh_alloc1Darray(int asize)
allocates memory for an array of doubles
int xsh_free2Darray(double **ccdtemp, int asize)
free memory for a 2D array of doubles
void xsh_3_init(struct xs_3 *p_xs_3)
Pre-compute a number of non-wavelength dependent secondary parameters required by the model.
double ** xsh_alloc2Darray(int asize, int bsize)
allocates memory for a 2D array of doubles
int * y
cpl_table * xsh_model_anneal_comp(ann_all_par *p_all_par, int nparam, double *abest, double *amin, double *amax, int *aname, struct xs_3 *p_xs_3, int DS_size, coord *msp_coord, DOUBLE *p_wlarray, DOUBLE **ref_ind, int maxit)
Use the simulated annealing algorithm to adjust the model parameters so that the metric (mean Euclide...
int * x
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
int slit_pos
double x
double y
double flux
double wave
DOUBLE xpospix
int morder_max
double chipypix
DOUBLE t_ir_p2
DOUBLE slit[10]
DOUBLE slit_scale
DOUBLE ypospix
DOUBLE es_y
DOUBLE temper
int morder_min
DOUBLE es_y_tot
detloc chippix
int morder
double chipxpix
void get_meas_coordinates(int sizearraywavelengths, coord *meas_coord, char *datname)
int countlines(const char *filename)
#define MAXIT
int order
Definition: xsh_detmon_lg.c:80
#define NIR_chipypix
#define NIR_morder_max
#define UVB_chipypix
#define NIR_morder_min
#define VIS_chipxpix
#define VIS_morder_max
#define VIS_chipypix
#define UVB_morder
#define UVB_chipxpix
#define UVB_morder_max
#define NIR_BSIZE
#define NIR_morder
#define UVB_morder_min
#define VIS_morder
#define UVB_ASIZE
#define VIS_BSIZE
#define UVB_BSIZE
#define NIR_chipxpix
#define NIR_ASIZE
#define VIS_ASIZE
#define VIS_morder_min
double DOUBLE
#define XSH_CALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:56