UVES Pipeline Reference Manual  5.4.0
uves_physmod_msrawxy.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: 2011-12-08 14:05:02 $
23  * $Revision: 1.21 $
24  * $Name: not supported by cvs2svn $
25  */
26 
27 /*----------------------------------------------------------------------------*/
31 /*----------------------------------------------------------------------------*/
34 #ifdef HAVE_CONFIG_H
35 # include <config.h>
36 #endif
37 /*-----------------------------------------------------------------------------
38  Includes
39  -----------------------------------------------------------------------------*/
40 #include <uves_physmod_msrawxy.h>
41 
42 #include <uves_physmod_center_gauss.h>
43 #include <uves_pfits.h>
44 #include <uves_utils_polynomial.h>
45 #include <uves_utils_wrappers.h>
46 #include <uves_msg.h>
47 #include <uves_error.h>
48 
49 /*-----------------------------------------------------------------------------
50  Functions code
51  -----------------------------------------------------------------------------*/
52 
53 /*----------------------------------------------------------------------------*/
83 /*----------------------------------------------------------------------------*/
84 
85 
86 int uves_physmod_msrawxy(const cpl_image *raw_image,
87  const uves_propertylist *raw_header,
88  const char *recipe_id,
89  const cpl_parameterlist* parameters,
90  cpl_table* mod_tbl,
91  cpl_table** l_tbl,
92  cpl_table** m_tbl,
93  cpl_table** p_tbl,
94  cpl_table** rline_tbl,
95  cpl_table** mline_tbl,
96  cpl_table** npline_tbl)
97 
98 {
99  double scl_fct=0;
100  double add_fct=0;
101  double X_AXIS_SCALE=0;
102  double Y_AXIS_SCALE=0;
103  double OFFSET_X=0;
104  double OFFSET_Y=0;
105 
106 
107  int status=0;
108 
109 
110  double limit[4]={0.,0.,0.,0.};
111  int MBOX_X=0;
112  int MBOX_Y=0;
113 
114  double tmp_x=0;
115  double tmp_y=0;
116  double tmp_val_x=0;
117  double tmp_val_y=0;
118 
119 
120 
121  polynomial* poly2d_x=NULL;
122  polynomial* poly2d_y=NULL;
123  double mean_err_xdif=0;
124  double mean_err_ydif=0;
125 
126  double binx=0;
127  double biny=0;
128 
129  double mbox_x=0;
130  double mbox_y=0;
131  double outputd[3][3];
132  int k=0;
133 
134  cpl_table * tmp_tbl=NULL;
135  cpl_table * tmp_tbl1=NULL;
136  cpl_table * tmp_tbl2=NULL;
137  cpl_table * tmp_tbl3=NULL;
138  cpl_table * tmp_tbl4=NULL;
139 
140  uves_msg_debug("start msrawxy");
141 
142  check (biny = uves_pfits_get_binx(raw_header),
143  "Could not read x binning factor from input header");
144 
145  check (binx = uves_pfits_get_biny(raw_header),
146  "Could not read x binning factor from input header");
147 
148  check( uves_get_parameter(parameters, NULL, recipe_id,
149  "mbox_x", CPL_TYPE_INT, &MBOX_X ) ,
150  "Could not read parameter");
151 
152  check( uves_get_parameter(parameters, NULL, recipe_id,
153  "mbox_y", CPL_TYPE_INT, &MBOX_Y ) ,
154  "Could not read parameter");
155 
156  mbox_x=MBOX_X/binx;
157  mbox_y=MBOX_Y/biny;
158 
159  /* IN MIDAS we always use the default offset=0,0
160  check( uves_get_parameter(parameters, NULL, recipe_id,
161  "offset_x", CPL_TYPE_DOUBLE, &OFFSET_X ) ,
162  "Could not read parameter");
163 
164  check( uves_get_parameter(parameters, NULL, recipe_id,
165  "offset_y", CPL_TYPE_DOUBLE, &OFFSET_Y ) ,
166  "Could not read parameter");
167 
168  */
169  /* msrawxy specific pars */
170  /* Removed params x_axis_scale,y_axis_scale, always set to 0 */
171  /*
172  check( uves_get_parameter(parameters, NULL, recipe_id,
173  "x_axis_scale", CPL_TYPE_DOUBLE, &X_AXIS_SCALE ) ,
174  "Could not read parameter");
175 
176  check( uves_get_parameter(parameters, NULL, recipe_id,
177  "y_axis_scale", CPL_TYPE_DOUBLE, &Y_AXIS_SCALE ),
178  "Could not read parameter");
179  */
180 
181  check(cpl_table_and_selected_double(mod_tbl,"IDENT",CPL_GREATER_THAN,0),
182  "Error selecting IDENT");
183  *l_tbl=cpl_table_extract_selected(mod_tbl);
184 
185  uves_msg_debug("xs=%f ofx=%f bx=%f",(1.+X_AXIS_SCALE),OFFSET_X,mbox_x/2);
186  uves_msg_debug("ys=%f ofy=%f by=%f",(1.+Y_AXIS_SCALE),OFFSET_Y,mbox_y/2);
187 
188  scl_fct=(1.+X_AXIS_SCALE);
189  cpl_table_erase_column(*l_tbl,"XSTART");
190  cpl_table_duplicate_column(*l_tbl,"XSTART",*l_tbl,"XMOD");
191  cpl_table_multiply_scalar(*l_tbl,"XSTART",scl_fct);
192  add_fct=OFFSET_X-(double)mbox_x/2;
193 
194  uves_msg_debug("add_fct=%f",add_fct);
195  cpl_table_add_scalar(*l_tbl,"XSTART",add_fct);
196 
197  cpl_table_erase_column(*l_tbl,"XEND");
198  cpl_table_duplicate_column(*l_tbl,"XEND",*l_tbl,"XMOD");
199  cpl_table_multiply_scalar(*l_tbl,"XEND",scl_fct);
200  add_fct=OFFSET_X+(double)mbox_x/2;
201  uves_msg_debug("add_fct=%f",add_fct);
202  cpl_table_add_scalar(*l_tbl,"XEND",add_fct);
203 
204  scl_fct=(1.+Y_AXIS_SCALE);
205  cpl_table_erase_column(*l_tbl,"YSTART");
206  cpl_table_duplicate_column(*l_tbl,"YSTART",*l_tbl,"YMOD");
207  cpl_table_multiply_scalar(*l_tbl,"YSTART",scl_fct);
208  add_fct=OFFSET_Y-(double)mbox_y/2;
209  uves_msg_debug("add_fct=%f",add_fct);
210  cpl_table_add_scalar(*l_tbl,"YSTART",add_fct);
211 
212  cpl_table_erase_column(*l_tbl,"YEND");
213  cpl_table_duplicate_column(*l_tbl,"YEND",*l_tbl,"YMOD");
214  cpl_table_multiply_scalar(*l_tbl,"YEND",scl_fct);
215  add_fct=OFFSET_Y+(double)mbox_y/2;
216  uves_msg_debug("add_fct=%f",add_fct);
217  cpl_table_add_scalar(*l_tbl,"YEND",add_fct);
218 
219 
220 
221  limit[2] = mbox_x/2; /* limit(3) */
222  check(tmp_tbl1=uves_extract_table_rows(*l_tbl,"XSTART",CPL_GREATER_THAN,
223  limit[2]),"Error selecting XSTART");
224 
225  check_nomsg(limit[0] = cpl_image_get_size_x(raw_image)-mbox_x/2); /* limit(1) */
226 
227  check(tmp_tbl2=uves_extract_table_rows(tmp_tbl1,"XSTART",CPL_LESS_THAN,
228  limit[0]),"Error selecting XSTART");
229 
230  limit[3] = mbox_y/2; /* limit(4) */
231 
232  check(tmp_tbl3=uves_extract_table_rows(tmp_tbl2,"YSTART",CPL_GREATER_THAN,
233  limit[3]),"Error selecting YSTART");
234 
235  check_nomsg(limit[1] = cpl_image_get_size_y(raw_image)-mbox_y/2); /* limit(2) */
236 
237  check(tmp_tbl4=uves_extract_table_rows(tmp_tbl3,"YSTART",CPL_LESS_THAN,
238  limit[1]),"Error selecting YSTART");
239 
240  uves_msg_debug("limits %f %f %f %f",
241  limit[0],limit[1],limit[2],limit[3]);
242  check_nomsg(*m_tbl = cpl_table_extract_selected(tmp_tbl4));
243  check_nomsg(*rline_tbl = cpl_table_extract_selected(tmp_tbl4));
244 
245  /* this is to finally get in l_tbl the result of the selection */
246  check_nomsg(tmp_tbl=cpl_table_extract_selected(*l_tbl));
247  uves_free_table(l_tbl);
248  *l_tbl=cpl_table_duplicate(tmp_tbl);
249  uves_free_table(&tmp_tbl);
250 
251 
252  assure( !cpl_table_has_invalid(*m_tbl,"XMOD") &&
253  !cpl_table_has_invalid(*m_tbl,"YMOD"),
254  CPL_ERROR_ILLEGAL_INPUT,
255  "Invalid content in table m_tbl");
256 
257  uves_msg_debug("min=%f max=%f std=%f",
258  cpl_image_get_min(raw_image),
259  cpl_image_get_max(raw_image),
260  cpl_image_get_stdev(raw_image));
261 
262  check_nomsg( uves_physmod_center_gauss(raw_image,m_tbl) );
263  /*
264  cpl_table_dump(*m_tbl,0,20,stdout);
265  */
266 
267 
268  check_nomsg(
269  (cpl_table_duplicate_column(*rline_tbl,"XMES",*m_tbl,"XCEN"),
270  cpl_table_duplicate_column(*rline_tbl,"YMES",*m_tbl,"YCEN"),
271  cpl_table_duplicate_column(*rline_tbl,"STATUS",*m_tbl,"STATUS")));
272 
273  /* save measured positions without check for quality check */
274  *mline_tbl=cpl_table_duplicate(*rline_tbl);
275 
276  uves_msg_debug("nraw=%" CPL_SIZE_FORMAT "",
277  cpl_table_and_selected_int(*rline_tbl,"STATUS",CPL_EQUAL_TO,0.));
278 
279 
280  check_nomsg( *p_tbl=cpl_table_extract_selected(*rline_tbl) );
281  /* the following two lines to keep in *rline_tbl the result of selection */
282  uves_free_table(rline_tbl);
283  *rline_tbl=cpl_table_duplicate(*p_tbl);
284 
285  uves_msg_debug("middump nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*p_tbl));
286 
287  cpl_table_select_all(*rline_tbl);
288  uves_free_table(rline_tbl);
289  *rline_tbl=cpl_table_duplicate(*p_tbl);
290  uves_msg_debug("rline nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
291 
292 
293  check_nomsg( (cpl_table_duplicate_column(*rline_tbl,"XDIF",*rline_tbl,"XMES"),
294  cpl_table_subtract_columns(*rline_tbl,"XDIF","XMOD")) );
295  uves_msg_debug("rline1 nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
296 
297 
298  cpl_table_duplicate_column(*rline_tbl,"YDIF",*rline_tbl,"YMES");
299  cpl_table_subtract_columns(*rline_tbl,"YDIF","YMOD");
300 
301  uves_msg_debug("rline2 nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
302  /* Fits the residual differences */
303  /* Now we do the polynomial fit */
304  check(poly2d_x=uves_polynomial_regression_2d(*rline_tbl,"XMOD","YMOD","XDIF",
305  NULL,2,2,"XDIFREG",NULL,NULL,
306  &mean_err_xdif,NULL,NULL,-1,-1),
307  "2D poly regression to fit XDIF failed. Try to increase mbox_x value, "
308  "and possibly the mbox_y value");
309 
310  outputd[0][0]=uves_polynomial_get_coeff_2d(poly2d_x,0,0);
311  outputd[1][0]=uves_polynomial_get_coeff_2d(poly2d_x,1,0);
312  outputd[0][1]=uves_polynomial_get_coeff_2d(poly2d_x,0,1);
313  outputd[1][1]=uves_polynomial_get_coeff_2d(poly2d_x,1,1);
314  outputd[2][0]=uves_polynomial_get_coeff_2d(poly2d_x,2,0);
315  outputd[0][2]=uves_polynomial_get_coeff_2d(poly2d_x,0,2);
316  outputd[1][2]=uves_polynomial_get_coeff_2d(poly2d_x,1,2);
317  outputd[2][1]=uves_polynomial_get_coeff_2d(poly2d_x,2,1);
318  outputd[2][2]=uves_polynomial_get_coeff_2d(poly2d_x,2,2);
319 
320  check(poly2d_y=uves_polynomial_regression_2d(*rline_tbl,"XMOD","YMOD","YDIF",
321  NULL,2,2,"YDIFREG",NULL,NULL,
322  &mean_err_ydif,NULL,NULL,-1,-1),
323  "2D poly regression to fit YDIF failed. Try to increase mbox_y value");
324 
325 
326  outputd[0][0]=uves_polynomial_get_coeff_2d(poly2d_y,0,0);
327  outputd[1][0]=uves_polynomial_get_coeff_2d(poly2d_y,1,0);
328  outputd[0][1]=uves_polynomial_get_coeff_2d(poly2d_y,0,1);
329  outputd[1][1]=uves_polynomial_get_coeff_2d(poly2d_y,1,1);
330  outputd[2][0]=uves_polynomial_get_coeff_2d(poly2d_y,2,0);
331  outputd[0][2]=uves_polynomial_get_coeff_2d(poly2d_y,0,2);
332  outputd[1][2]=uves_polynomial_get_coeff_2d(poly2d_y,1,2);
333  outputd[2][1]=uves_polynomial_get_coeff_2d(poly2d_y,2,1);
334  outputd[2][2]=uves_polynomial_get_coeff_2d(poly2d_y,2,2);
335 
336 
337  uves_msg_debug("rline3 nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
338  cpl_table_duplicate_column(*rline_tbl,"XMODREG",*rline_tbl,"XMOD");
339  cpl_table_add_columns(*rline_tbl,"XMODREG","XDIFREG");
340 
341  cpl_table_duplicate_column(*rline_tbl,"YMODREG",*rline_tbl,"YMOD");
342  cpl_table_add_columns(*rline_tbl,"YMODREG","YDIFREG");
343 
344  uves_msg_debug("rline4 nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
345  /*
346  cpl_table_dump(*rline_tbl,0,10,stdout);
347  */
348 
349 
350  /* was */
351  *npline_tbl=cpl_table_extract_selected(mod_tbl);
352 
353  /* we now unselect */
354  cpl_table_select_all(mod_tbl);
355  /*
356  *npline_tbl=cpl_table_extract_selected(mod_tbl);
357  */
358  uves_msg_debug("npline nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*npline_tbl));
359  /* to prevent a problem of duplicating columns from tables
360  with different size we use a temporary table and extend
361  the size */
362  tmp_tbl=cpl_table_new(cpl_table_get_nrow(*rline_tbl));
363  cpl_table_duplicate_column(tmp_tbl,"XDIF",*rline_tbl,"XDIF");
364  cpl_table_duplicate_column(tmp_tbl,"YDIF",*rline_tbl,"YDIF");
365 
366  cpl_table_set_size(tmp_tbl,cpl_table_get_nrow(*npline_tbl));
367 
368  cpl_table_duplicate_column(*npline_tbl,"XDIF",tmp_tbl,"XDIF");
369  cpl_table_duplicate_column(*npline_tbl,"YDIF",tmp_tbl,"YDIF");
370  uves_free_table(&tmp_tbl);
371 
372  cpl_table_new_column(*npline_tbl,"XDIFREG",CPL_TYPE_DOUBLE);
373  cpl_table_new_column(*npline_tbl,"YDIFREG",CPL_TYPE_DOUBLE);
374 
375  for (k=0;k<cpl_table_get_nrow(*npline_tbl);k++) {
376 
377  tmp_x=cpl_table_get_double(*npline_tbl,"XMOD",k,&status);
378  tmp_y=cpl_table_get_double(*npline_tbl,"YMOD",k,&status);
379 
380  tmp_val_x=uves_polynomial_evaluate_2d(poly2d_x,tmp_x,tmp_y);
381  tmp_val_y=uves_polynomial_evaluate_2d(poly2d_y,tmp_x,tmp_y);
382 
383  cpl_table_set_double(*npline_tbl,"XDIFREG",k,tmp_val_x);
384  cpl_table_set_double(*npline_tbl,"YDIFREG",k,tmp_val_y);
385 
386  }
387 
388  cpl_table_add_columns(*npline_tbl,"XMOD","XDIFREG");
389  cpl_table_add_columns(*npline_tbl,"YMOD","YDIFREG");
390 
391 
392  cleanup:
393  uves_polynomial_delete(&poly2d_x);
394  uves_polynomial_delete(&poly2d_y);
395  uves_free_table(&tmp_tbl);
396  uves_free_table(&tmp_tbl1);
397  uves_free_table(&tmp_tbl2);
398  uves_free_table(&tmp_tbl3);
399  uves_free_table(&tmp_tbl4);
400 
401  uves_msg_debug("end msrawxy");
402  return 0;
403 }