UVES Pipeline Reference Manual  5.4.0
uves_physmod_calmap.c
1 /* *
2  * This file is part of the ESO UVES Pipeline *
3  * Copyright (C) 2004,2005 European Southern Observatory *
4  * *
5  * This library is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18  * */
19 
20 /*
21  * $Author: amodigli $
22  * $Date: 2012-03-02 16:38:18 $
23  * $Revision: 1.22 $
24  * $Name: not supported by cvs2svn $
25  */
26 /*----------------------------------------------------------------------------*/
30 /*----------------------------------------------------------------------------*/
33 #ifdef HAVE_CONFIG_H
34 # include <config.h>
35 #endif
36 
37 #include <uves_physmod_calmap.h>
38 
39 #include <uves_physmod_regress_echelle.h>
40 #include <uves_wavecal_utils.h>
41 #include <uves_utils_wrappers.h>
42 #include <uves_msg.h>
43 #include <uves_error.h>
44 
68 int uves_physmod_calmap(const uves_propertylist *raw_header,
69  enum uves_chip chip,
70  const char *recipe_id,
71  const cpl_parameterlist* parameters,
72  cpl_table* npline_tbl,
73  cpl_table** ord_tbl,
74  cpl_table** lin_tbl,
75  cpl_table** w_tbl,
76  cpl_table** s_tbl,
77  int* abs_ord_min,
78  int* abs_ord_max,
79  polynomial** poly2d)
80 {
81  int rel_ord=0;
82  int ord_min=0;
83  int ord_max=0;
84  int ord_ech=0;
85  double dis_avg=0;
86 
87  uves_propertylist* plist=NULL;
88  polynomial* poly1d=NULL;
89 
90  double mean_err_aux=0;
91  double mean_err_order=0;
92 
93  int num_outliers=0;
94  double tol=0;
95  double kappa=0.;
96  double outputd[3];
97  uves_msg_debug("start calmap");
98 
99 
100 
101  check(uves_get_parameter(parameters,NULL,recipe_id,"kappa",CPL_TYPE_DOUBLE,&kappa),
102  "Could not read parameter");
103  check(uves_get_parameter(parameters,NULL,recipe_id,"tol",CPL_TYPE_DOUBLE,&tol),
104  "Could not read parameter");
105 
106  cpl_table_erase_invalid_rows(npline_tbl);
107  uves_msg_debug("nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(npline_tbl));
108  *ord_tbl=cpl_table_new(cpl_table_get_nrow(npline_tbl));
109 
110  cpl_table_duplicate_column(*ord_tbl,"ABS_ORDER",npline_tbl,"ORDER");
111  cpl_table_duplicate_column(*ord_tbl,"ORDER",npline_tbl,"ORDER");
112  cpl_table_duplicate_column(*ord_tbl,"X",npline_tbl,"XMOD");
113  cpl_table_duplicate_column(*ord_tbl,"Y",npline_tbl,"YMOD");
114 
115  /*
116  cpl_table_dump(*ord_tbl,0,20,stdout);
117  OLD VERSION: (WRONG)
118  rel_ord = cpl_table_get_int(*ord_tbl,"ORDER",0,&status);
119  */
120  rel_ord = cpl_table_get_column_max(*ord_tbl,"ORDER");
121 
122  uves_msg_debug("relative order=%d",rel_ord);
123  cpl_table_multiply_scalar(*ord_tbl,"ORDER",-1);
124  cpl_table_add_scalar(*ord_tbl,"ORDER",rel_ord);
125  cpl_table_add_scalar(*ord_tbl,"ORDER",+1);
126 
127 
128  plist=uves_propertylist_new();
129  uves_propertylist_append_bool(plist,"ORDER",0); /* 0 for ascending order */
130  uves_propertylist_append_bool(plist,"X",0); /* 1 for descending order */
131  uves_table_sort(*ord_tbl,plist);
132  uves_free_propertylist(&plist);
133  *abs_ord_min=cpl_table_get_column_min(*ord_tbl,"ORDER");
134  *abs_ord_max=cpl_table_get_column_max(*ord_tbl,"ORDER");
135 
136  ord_ech=*abs_ord_max-*abs_ord_min+1;
137  uves_msg_debug("Orders: max %d min %d No %d",
138  *abs_ord_max,*abs_ord_min,(*abs_ord_max-*abs_ord_min+1));
139 
140 
141 
142  uves_physmod_regress_echelle(raw_header,chip,
143  recipe_id,parameters,
144  ord_tbl,num_outliers,
145  tol, kappa,s_tbl,w_tbl);
146 
147 
148  /* we skip prepare/background */
149 
150 
151 
152  uves_free_table(lin_tbl);
153  *lin_tbl=cpl_table_new(cpl_table_get_nrow(npline_tbl));
154 
155 
156  cpl_table_duplicate_column(*lin_tbl,"X",npline_tbl,"XMOD");
157  cpl_table_duplicate_column(*lin_tbl,"Y",npline_tbl,"ORDER");
158  cpl_table_duplicate_column(*lin_tbl,"PEAK",npline_tbl,"ORDER");
159  cpl_table_duplicate_column(*lin_tbl,"Ident",npline_tbl,"IDENT");
160  cpl_table_duplicate_column(*lin_tbl,"YNEW",npline_tbl,"YMOD");
161  cpl_table_duplicate_column(*lin_tbl,"Order",npline_tbl,"ORDER");
162  cpl_table_duplicate_column(*lin_tbl,"WAVEC",npline_tbl,"IDENT");
163 
164  cpl_table_duplicate_column(*lin_tbl,"AUX",npline_tbl,"ORDER");
165  cpl_table_cast_column(*lin_tbl,"AUX","Aux",CPL_TYPE_DOUBLE);
166  cpl_table_multiply_columns(*lin_tbl,"Aux","Ident");
167  cpl_table_erase_column(*lin_tbl,"AUX");
168 
169  cpl_table_set_column_unit(*lin_tbl,"WAVEC","Ang");
170  cpl_table_set_column_unit(*lin_tbl,"YNEW","pix");
171  cpl_table_set_column_unit(*lin_tbl,"Ident","Ang");
172  /* we check that the product Aux=ORDER*Ident=m*lambda=d*sin(alpha) is a
173  constant as is expected in a grating. A non constant product is an
174  indication of detector rotation (this is the case: the product is
175  not exacly constant).
176  */
177  check(poly1d=uves_polynomial_regression_1d(*lin_tbl,"X","Aux",NULL,
178  3,"XREG",NULL,
179  &mean_err_aux,-1),
180  "Fitting YDIF failed");
181 
182  cpl_table_set_column_unit(*lin_tbl,"XREG","pix");
183  cpl_table_set_column_unit(*lin_tbl,"X","pix");
184 
185  /* NOTE THAT XREG is NOT an output column from MIDAS: should we remove it? */
186 
187  outputd[0]=uves_polynomial_get_coeff_1d(poly1d,0);
188  outputd[1]=uves_polynomial_get_coeff_1d(poly1d,1);
189  outputd[2]=uves_polynomial_get_coeff_1d(poly1d,2);
190 
191  ord_min=cpl_table_get_column_min(*lin_tbl,"Order");
192  ord_max=cpl_table_get_column_max(*lin_tbl,"Order");
193 
194  uves_msg_debug("output0=%f output1=%f output2=%f",
195  outputd[0],outputd[1],outputd[2]);
196 
197  uves_msg_debug("ord_max=%d ord_min=%d",ord_max,ord_min);
198 
199  cpl_table_duplicate_column(*lin_tbl,LINETAB_PIXELSIZE,*lin_tbl,"X");
200  cpl_table_multiply_scalar(*lin_tbl,LINETAB_PIXELSIZE,2*outputd[2]);
201  cpl_table_add_scalar(*lin_tbl,LINETAB_PIXELSIZE,outputd[1]);
202  cpl_table_divide_columns(*lin_tbl,LINETAB_PIXELSIZE,"Order");
203 
204 
205  dis_avg=cpl_table_get_column_mean(*lin_tbl,LINETAB_PIXELSIZE);
206  uves_msg("Average pixel size: %f wav. units",dis_avg);
207 
208 
209  check(*poly2d=uves_polynomial_regression_2d(*lin_tbl,"X","YNEW","Order",
210  NULL,4,4,"RORD",NULL,NULL,
211  &mean_err_order,NULL,NULL,-1,-1),
212  "Fitting Order failed");
213 
214 
215 
216  /* NOTE THAT RORD is NOT an output column from MIDAS: should we remove it? */
217 
218 
219  /*
220 save/regr {LINTAB} RORD KEYLONG
221  */
222 
223  /* SHOULD WE SAVE RORD somewhere? */
224 
225  /* This saves the current section and parameters and tables: generates
226  {P3}LINE.tbl {P3}ORDE.tbl {P3}back.tbl
227 
228 save/echelle {P3} >Null
229  */
230 
231  /*
232 set/echelle wlcmtd=guess guess={P3}
233  */
234 
235 
236  uves_msg_debug("Line Table: ncol=%" CPL_SIZE_FORMAT "",cpl_table_get_ncol(*lin_tbl));
237  cleanup:
238  uves_free_propertylist(&plist);
239  uves_polynomial_delete(&poly1d);
240 
241  uves_msg_debug("end calmap");
242  return 0;
243 }