UVES Pipeline Reference Manual  5.4.6
uves_test_simulate.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: 2010-09-24 09:31:49 $
23  * $Revision: 1.14 $
24  * $Name: not supported by cvs2svn $
25  * $Log: not supported by cvs2svn $
26  * Revision 1.12 2007/09/11 17:10:49 amodigli
27  * added uves_utils_polynomial.h
28  *
29  * Revision 1.11 2007/08/30 07:56:05 amodigli
30  * fixed some doxygen warnings
31  *
32  * Revision 1.10 2007/06/26 14:51:00 jmlarsen
33  * Fixed bug in load_linetable
34  *
35  * Revision 1.9 2007/06/20 15:55:37 jmlarsen
36  * Parametrized assumption about MIDAS format
37  *
38  * Revision 1.8 2007/06/20 15:49:59 jmlarsen
39  * Use MIDAS compatible column names
40  *
41  * Revision 1.7 2007/06/20 08:28:53 amodigli
42  * updated interface to uves_polynomial_convert_from_plist_midas
43  *
44  * Revision 1.6 2007/05/22 11:28:14 jmlarsen
45  * Removed unused variables
46  *
47  * Revision 1.5 2007/04/24 12:50:29 jmlarsen
48  * Replaced cpl_propertylist -> uves_propertylist which is much faster
49  *
50  * Revision 1.4 2007/04/10 11:34:48 jmlarsen
51  * More points per order in order table
52  *
53  * Revision 1.3 2007/04/10 07:22:07 jmlarsen
54  * Changed interface of polynomial_regression_2d()
55  *
56  * Revision 1.2 2007/03/15 14:44:51 jmlarsen
57  * Removed debugging dump
58  *
59  * Revision 1.1 2007/03/15 12:47:32 jmlarsen
60  * Imported sources
61  *
62  *
63  */
64 
65 #ifdef HAVE_CONFIG_H
66 # include <config.h>
67 #endif
68 
69 /*----------------------------------------------------------------------------*/
73 /*----------------------------------------------------------------------------*/
76 /*-----------------------------------------------------------------------------
77  Includes
78  -----------------------------------------------------------------------------*/
79 
80 #include <uves_test_simulate.h>
81 //#include <uves_utils_polynomial.h>
82 #include <uves_utils_wrappers.h>
83 #include <uves_wavecal_utils.h>
84 #include <uves_error.h>
85 #include <uves_dfs.h>
86 
87 #include <cpl.h>
88 
89 /*-----------------------------------------------------------------------------
90  Functions prototypes
91  -----------------------------------------------------------------------------*/
92 
93 /*-----------------------------------------------------------------------------
94  Implementation
95  -----------------------------------------------------------------------------*/
96 
97 /*----------------------------------------------------------------------------*/
108 /*----------------------------------------------------------------------------*/
109 void
110 create_order_table(cpl_table **ordertable,
111  polynomial **order_locations,
112  cpl_table **tracetable,
113  int minorder,
114  int maxorder,
115  int nx)
116 {
117  uves_propertylist *header = NULL;
118 
119  /* Create polynomial */
120  assure_nomsg(order_locations != NULL, CPL_ERROR_NULL_INPUT);
121 
122  {
123  const char *data[] = {"",
124  "'COEFFI','I*4',1,7,'7I10'",
125  " 53889 2 3 2 1 4 5",
126  "",
127  "'COEFFR','R*4',1,5,'5E14.7'",
128  " 4.3300000E+02 4.0880000E+03 1.0000000E+00 2.1000000E+01 0.0000000E+00",
129  "",
130  "'COEFFD','R*8',1,30,'3E23.15'",
131  " -7.097005629698889E+01 4.050908371864904E-02 -2.886756545398909E-06",
132  " 5.504345508879626E-10 -5.583004967206025E-14 7.624532125635992E+01",
133  " -2.428213567964009E-03 1.819158447566360E-06 -5.090366383338846E-10",
134  " 5.198098506055602E-14 3.513177145982783E-01 5.570332137951829E-04",
135  " -3.876157463910250E-07 1.113253735718822E-10 -1.132455173423791E-14",
136  " 2.977232589499959E-02 -5.389240622889887E-05 3.777456726044612E-08",
137  " -1.083863050648735E-11 1.098450510939580E-15 -1.093309039442914E-03",
138  " 2.402609262989674E-06 -1.688416547941747E-09 4.839101712729582E-13",
139  " -4.884504488944702E-17 1.919853952642526E-05 -4.004133160220927E-08",
140  " 2.816206503824200E-11 -8.051313882805877E-15 8.090579180112579E-19",
141  " ",
142  "'TAB_IN_OUT_YSHIFT','R*8',1,1,'3E23.15'",
143  " 4.180818583555659E+01 ",
144  " "};
145 
146  header = uves_propertylist_new();
147  {
148  unsigned i;
149  for (i = 0; i < sizeof(data)/sizeof(char *); i++)
150  {
151  uves_propertylist_append_string(header, "HISTORY",
152  data[i]);
153  }
154  }
155 
156  check_nomsg( *order_locations = uves_polynomial_convert_from_plist_midas(header,
157  "COEFF",-1));
158  }
159 
160  /* Fill order table */
161  if (ordertable != NULL)
162  {
163  int order;
164  int row = 0;
165 
166  *ordertable = cpl_table_new(5*(maxorder - minorder + 1));
167  cpl_table_new_column(*ordertable, "Order", CPL_TYPE_INT);
168  cpl_table_new_column(*ordertable, "X", CPL_TYPE_INT);
169  cpl_table_new_column(*ordertable, "Yfit", CPL_TYPE_DOUBLE);
170 
171  for (order = minorder; order <= maxorder; order++)
172  {
173  int x[5];
174  int i;
175  x[0] = (1*nx)/6+1;
176  x[1] = (2*nx)/6+1;
177  x[2] = (3*nx)/6+1;
178  x[3] = (4*nx)/6+1;
179  x[4] = (5*nx)/6+1;
180 
181  for (i = 0; i < 5; i++)
182  {
183  cpl_table_set_int(*ordertable, "Order", row, order);
184  cpl_table_set_int(*ordertable, "X", row, x[i]);
185  cpl_table_set_double(*ordertable, "Yfit", row,
186  uves_polynomial_evaluate_2d(*order_locations,
187  x[i], order));
188  row++;
189  }
190  }
191  }
192 
193  /* Not implemented: */
194  /* Fill tracetable */
195  tracetable = tracetable; /* suppress warnings */
196 
197  cleanup:
198  uves_free_propertylist(&header);
199  return;
200 }
201 
202 /*----------------------------------------------------------------------------*/
216 /*----------------------------------------------------------------------------*/
217 void
218 create_line_table(cpl_table **linetable,
219  polynomial **dispersion,
220  polynomial **abs_orders,
221  int *firstabs,
222  int *lastabs,
223  int minorder,
224  int maxorder,
225  int nx)
226 {
227  polynomial *order_locations = NULL;
228  cpl_table *ordertable = NULL;
229  cpl_table *tracetable = NULL;
230  const bool midas_format = false;
231 
232  const char *ORDER = midas_format ? "ORDER" : "Order";
233  const char *IDENT = midas_format ? "IDENT" : "Ident";
234  const char *YNEW = midas_format ? "YNEW" : "Ynew";
235 
236  create_order_table(&ordertable, &order_locations, &tracetable,
237  minorder, maxorder, nx);
238 
239  assure_nomsg(linetable != NULL, CPL_ERROR_NULL_INPUT);
240 
241  {
242  int row = 0;
243  *linetable = cpl_table_new(cpl_table_get_nrow(ordertable));
244  cpl_table_new_column(*linetable, "X", CPL_TYPE_DOUBLE);
245  cpl_table_new_column(*linetable, YNEW, CPL_TYPE_DOUBLE);
246  cpl_table_new_column(*linetable, "Y", CPL_TYPE_INT);
247  cpl_table_new_column(*linetable, ORDER, CPL_TYPE_INT);
248  cpl_table_new_column(*linetable, LINETAB_LAMBDAC, CPL_TYPE_DOUBLE);
249  cpl_table_new_column(*linetable, IDENT, CPL_TYPE_DOUBLE);
250  cpl_table_new_column(*linetable, "Aux", CPL_TYPE_DOUBLE);
251  for (row = 0; row < cpl_table_get_nrow(ordertable); row++)
252  {
253  int order = cpl_table_get_int(ordertable, "Order", row, NULL);
254  double x = cpl_table_get_int(ordertable, "X", row, NULL);
255  double y = cpl_table_get_double(ordertable, "Yfit", row, NULL);
256  int m = 120 - order; /* absolute order number */
257  double lambda = 3000 + 50*(order - minorder) + 80*(x*1.0/nx);
258 
259  cpl_table_set_double(*linetable, "X", row, x);
260  cpl_table_set_double(*linetable, YNEW, row, y);
261  cpl_table_set_int(*linetable, "Y", row, order); /* it's correct! */
262  cpl_table_set_int(*linetable, ORDER, row, m);
263  cpl_table_set_double(*linetable, LINETAB_LAMBDAC, row, lambda);
264  cpl_table_set_double(*linetable, IDENT, row, lambda); /* exact! */
265  cpl_table_set_double(*linetable, "Aux", row, lambda*m);
266  }
267  }
268 
269  if (abs_orders != NULL)
270  {
271  /* Create polynomial
272  absorders(x, y) = m
273  */
274  int degree = 2;
275  check_nomsg(
276  *abs_orders = uves_polynomial_regression_2d(*linetable,
277  "X", YNEW, ORDER, NULL,
278  degree, degree,
279  NULL, NULL, NULL, /* new columns */
280  NULL, NULL, /* mse, red_chisq */
281  NULL, -1, -1)); /* variance, kappa */
282  if (firstabs != NULL) {
283  double x = nx/2;
284  double y =
285  uves_polynomial_evaluate_2d(order_locations, x, minorder);
286 
287  *firstabs = uves_round_double(
288  uves_polynomial_evaluate_2d(*abs_orders, x, y));
289  }
290  if (lastabs != NULL) {
291  double x = nx/2;
292  double y =
293  uves_polynomial_evaluate_2d(order_locations, x, maxorder);
294 
295  *lastabs = uves_round_double(
296  uves_polynomial_evaluate_2d(*abs_orders, x, y));
297  }
298 
299  }
300 
301  if (dispersion != NULL)
302  {
303  /* Create polynomial of this form
304  f(x, m) = lambda m
305  */
306  int degree = 2;
307  check_nomsg(
308  *dispersion = uves_polynomial_regression_2d(*linetable,
309  "X", ORDER, "Aux", NULL,
310  degree, degree,
311  NULL, NULL, NULL, /* new columns */
312  NULL, NULL, /* mse, red_chisq */
313  NULL, -1, -1)); /* variance, kappa */
314  }
315 
316  cleanup:
317  uves_polynomial_delete(&order_locations);
318  uves_free_table(&ordertable);
319  uves_free_table(&tracetable);
320  return;
321 }
void uves_polynomial_delete(polynomial **p)
Delete a polynomial.
void create_order_table(cpl_table **ordertable, polynomial **order_locations, cpl_table **tracetable, int minorder, int maxorder, int nx)
Create order table.
#define check_nomsg(CMD)
Definition: uves_error.h:204
uves_propertylist * uves_propertylist_new(void)
Create an empty property list.
polynomial * uves_polynomial_regression_2d(cpl_table *t, const char *X1, const char *X2, const char *Y, const char *sigmaY, int degree1, int degree2, const char *polynomial_fit, const char *residual_square, const char *variance_fit, double *mse, double *red_chisq, polynomial **variance, double kappa, double min_reject)
Fit a 2d polynomial to three table columns.
Definition: uves_utils.c:2869
double uves_polynomial_evaluate_2d(const polynomial *p, double x1, double x2)
Evaluate a 2d polynomial.
#define assure_nomsg(BOOL, CODE)
Definition: uves_error.h:177
void create_line_table(cpl_table **linetable, polynomial **dispersion, polynomial **abs_orders, int *firstabs, int *lastabs, int minorder, int maxorder, int nx)
Create line table.