UVES Pipeline Reference Manual  5.5.5b2
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  */
26 
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30 
31 /*----------------------------------------------------------------------------*/
35 /*----------------------------------------------------------------------------*/
38 /*-----------------------------------------------------------------------------
39  Includes
40  -----------------------------------------------------------------------------*/
41 
42 #include <uves_test_simulate.h>
43 //#include <uves_utils_polynomial.h>
44 #include <uves_utils_wrappers.h>
45 #include <uves_wavecal_utils.h>
46 #include <uves_error.h>
47 #include <uves_dfs.h>
48 
49 #include <cpl.h>
50 
51 /*-----------------------------------------------------------------------------
52  Functions prototypes
53  -----------------------------------------------------------------------------*/
54 
55 /*-----------------------------------------------------------------------------
56  Implementation
57  -----------------------------------------------------------------------------*/
58 
59 /*----------------------------------------------------------------------------*/
70 /*----------------------------------------------------------------------------*/
71 void
72 create_order_table(cpl_table **ordertable,
73  polynomial **order_locations,
74  cpl_table **tracetable,
75  int minorder,
76  int maxorder,
77  int nx)
78 {
79  uves_propertylist *header = NULL;
80 
81  /* Create polynomial */
82  assure_nomsg(order_locations != NULL, CPL_ERROR_NULL_INPUT);
83 
84  {
85  const char *data[] = {"",
86  "'COEFFI','I*4',1,7,'7I10'",
87  " 53889 2 3 2 1 4 5",
88  "",
89  "'COEFFR','R*4',1,5,'5E14.7'",
90  " 4.3300000E+02 4.0880000E+03 1.0000000E+00 2.1000000E+01 0.0000000E+00",
91  "",
92  "'COEFFD','R*8',1,30,'3E23.15'",
93  " -7.097005629698889E+01 4.050908371864904E-02 -2.886756545398909E-06",
94  " 5.504345508879626E-10 -5.583004967206025E-14 7.624532125635992E+01",
95  " -2.428213567964009E-03 1.819158447566360E-06 -5.090366383338846E-10",
96  " 5.198098506055602E-14 3.513177145982783E-01 5.570332137951829E-04",
97  " -3.876157463910250E-07 1.113253735718822E-10 -1.132455173423791E-14",
98  " 2.977232589499959E-02 -5.389240622889887E-05 3.777456726044612E-08",
99  " -1.083863050648735E-11 1.098450510939580E-15 -1.093309039442914E-03",
100  " 2.402609262989674E-06 -1.688416547941747E-09 4.839101712729582E-13",
101  " -4.884504488944702E-17 1.919853952642526E-05 -4.004133160220927E-08",
102  " 2.816206503824200E-11 -8.051313882805877E-15 8.090579180112579E-19",
103  " ",
104  "'TAB_IN_OUT_YSHIFT','R*8',1,1,'3E23.15'",
105  " 4.180818583555659E+01 ",
106  " "};
107 
108  header = uves_propertylist_new();
109  {
110  unsigned i;
111  for (i = 0; i < sizeof(data)/sizeof(char *); i++)
112  {
113  uves_propertylist_append_string(header, "HISTORY",
114  data[i]);
115  }
116  }
117 
118  check_nomsg( *order_locations = uves_polynomial_convert_from_plist_midas(header,
119  "COEFF",-1));
120  }
121 
122  /* Fill order table */
123  if (ordertable != NULL)
124  {
125  int order;
126  int row = 0;
127 
128  *ordertable = cpl_table_new(5*(maxorder - minorder + 1));
129  cpl_table_new_column(*ordertable, "Order", CPL_TYPE_INT);
130  cpl_table_new_column(*ordertable, "X", CPL_TYPE_INT);
131  cpl_table_new_column(*ordertable, "Yfit", CPL_TYPE_DOUBLE);
132 
133  for (order = minorder; order <= maxorder; order++)
134  {
135  int x[5];
136  int i;
137  x[0] = (1*nx)/6+1;
138  x[1] = (2*nx)/6+1;
139  x[2] = (3*nx)/6+1;
140  x[3] = (4*nx)/6+1;
141  x[4] = (5*nx)/6+1;
142 
143  for (i = 0; i < 5; i++)
144  {
145  cpl_table_set_int(*ordertable, "Order", row, order);
146  cpl_table_set_int(*ordertable, "X", row, x[i]);
147  cpl_table_set_double(*ordertable, "Yfit", row,
148  uves_polynomial_evaluate_2d(*order_locations,
149  x[i], order));
150  row++;
151  }
152  }
153  }
154 
155  /* Not implemented: */
156  /* Fill tracetable */
157  tracetable = tracetable; /* suppress warnings */
158 
159  cleanup:
160  uves_free_propertylist(&header);
161  return;
162 }
163 
164 /*----------------------------------------------------------------------------*/
178 /*----------------------------------------------------------------------------*/
179 void
180 create_line_table(cpl_table **linetable,
181  polynomial **dispersion,
182  polynomial **abs_orders,
183  int *firstabs,
184  int *lastabs,
185  int minorder,
186  int maxorder,
187  int nx)
188 {
189  polynomial *order_locations = NULL;
190  cpl_table *ordertable = NULL;
191  cpl_table *tracetable = NULL;
192  const bool midas_format = false;
193 
194  const char *ORDER = midas_format ? "ORDER" : "Order";
195  const char *IDENT = midas_format ? "IDENT" : "Ident";
196  const char *YNEW = midas_format ? "YNEW" : "Ynew";
197 
198  create_order_table(&ordertable, &order_locations, &tracetable,
199  minorder, maxorder, nx);
200 
201  assure_nomsg(linetable != NULL, CPL_ERROR_NULL_INPUT);
202 
203  {
204  int row = 0;
205  *linetable = cpl_table_new(cpl_table_get_nrow(ordertable));
206  cpl_table_new_column(*linetable, "X", CPL_TYPE_DOUBLE);
207  cpl_table_new_column(*linetable, YNEW, CPL_TYPE_DOUBLE);
208  cpl_table_new_column(*linetable, "Y", CPL_TYPE_INT);
209  cpl_table_new_column(*linetable, ORDER, CPL_TYPE_INT);
210  cpl_table_new_column(*linetable, LINETAB_LAMBDAC, CPL_TYPE_DOUBLE);
211  cpl_table_new_column(*linetable, IDENT, CPL_TYPE_DOUBLE);
212  cpl_table_new_column(*linetable, "Aux", CPL_TYPE_DOUBLE);
213  for (row = 0; row < cpl_table_get_nrow(ordertable); row++)
214  {
215  int order = cpl_table_get_int(ordertable, "Order", row, NULL);
216  double x = cpl_table_get_int(ordertable, "X", row, NULL);
217  double y = cpl_table_get_double(ordertable, "Yfit", row, NULL);
218  int m = 120 - order; /* absolute order number */
219  double lambda = 3000 + 50*(order - minorder) + 80*(x*1.0/nx);
220 
221  cpl_table_set_double(*linetable, "X", row, x);
222  cpl_table_set_double(*linetable, YNEW, row, y);
223  cpl_table_set_int(*linetable, "Y", row, order); /* it's correct! */
224  cpl_table_set_int(*linetable, ORDER, row, m);
225  cpl_table_set_double(*linetable, LINETAB_LAMBDAC, row, lambda);
226  cpl_table_set_double(*linetable, IDENT, row, lambda); /* exact! */
227  cpl_table_set_double(*linetable, "Aux", row, lambda*m);
228  }
229  }
230 
231  if (abs_orders != NULL)
232  {
233  /* Create polynomial
234  absorders(x, y) = m
235  */
236  int degree = 2;
237  check_nomsg(
238  *abs_orders = uves_polynomial_regression_2d(*linetable,
239  "X", YNEW, ORDER, NULL,
240  degree, degree,
241  NULL, NULL, NULL, /* new columns */
242  NULL, NULL, /* mse, red_chisq */
243  NULL, -1, -1)); /* variance, kappa */
244  if (firstabs != NULL) {
245  double x = nx/2;
246  double y =
247  uves_polynomial_evaluate_2d(order_locations, x, minorder);
248 
249  *firstabs = uves_round_double(
250  uves_polynomial_evaluate_2d(*abs_orders, x, y));
251  }
252  if (lastabs != NULL) {
253  double x = nx/2;
254  double y =
255  uves_polynomial_evaluate_2d(order_locations, x, maxorder);
256 
257  *lastabs = uves_round_double(
258  uves_polynomial_evaluate_2d(*abs_orders, x, y));
259  }
260 
261  }
262 
263  if (dispersion != NULL)
264  {
265  /* Create polynomial of this form
266  f(x, m) = lambda m
267  */
268  int degree = 2;
269  check_nomsg(
270  *dispersion = uves_polynomial_regression_2d(*linetable,
271  "X", ORDER, "Aux", NULL,
272  degree, degree,
273  NULL, NULL, NULL, /* new columns */
274  NULL, NULL, /* mse, red_chisq */
275  NULL, -1, -1)); /* variance, kappa */
276  }
277 
278  cleanup:
279  uves_polynomial_delete(&order_locations);
280  uves_free_table(&ordertable);
281  uves_free_table(&tracetable);
282  return;
283 }
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:2870
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.