UVES Pipeline Reference Manual  5.4.0
uves_physmod_necregr.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:32:07 $
23  * $Revision: 1.7 $
24  * $Name: not supported by cvs2svn $
25  * $Log: not supported by cvs2svn $
26  * Revision 1.5 2007/06/06 08:17:33 amodigli
27  * replace tab with 4 spaces
28  *
29  * Revision 1.4 2006/11/06 15:19:41 jmlarsen
30  * Removed unused include directives
31  *
32  * Revision 1.3 2006/08/23 09:33:03 jmlarsen
33  * Renamed local variables shadowing POSIX reserved names
34  *
35  * Revision 1.2 2006/06/20 10:56:56 amodigli
36  * cleaned output, added units
37  *
38  * Revision 1.1 2006/02/03 07:46:30 jmlarsen
39  * Moved recipe implementations to ./uves directory
40  *
41  * Revision 1.8 2006/01/20 10:05:49 jmlarsen
42  * Inserted missing doxygen end tag
43  *
44  * Revision 1.7 2006/01/16 08:01:57 amodigli
45  *
46  * Added stability check
47  *
48  * Revision 1.6 2006/01/05 14:29:59 jmlarsen
49  * Removed newline characters from output strings
50  *
51  * Revision 1.5 2005/12/20 08:11:44 jmlarsen
52  * Added CVS entry
53  *
54  */
55 
56 /*----------------------------------------------------------------------------*/
60 /*----------------------------------------------------------------------------*/
63 #ifdef HAVE_CONFIG_H
64 # include <config.h>
65 #endif
66 
67 /*-----------------------------------------------------------------------------
68  Includes
69  -----------------------------------------------------------------------------*/
70 #include <uves_physmod_necregr.h>
71 
72 #include <uves_error.h>
73 #include <uves_msg.h>
74 
75 /*-----------------------------------------------------------------------------
76  Defines
77  -----------------------------------------------------------------------------*/
78 /*-----------------------------------------------------------------------------
79  Functions prototypes
80  ----------------------------------------------------------------------------*/
81 /*-----------------------------------------------------------------------------
82  Static variables
83  -----------------------------------------------------------------------------*/
84 
85 /*-----------------------------------------------------------------------------
86  Functions code
87  -----------------------------------------------------------------------------*/
88 
89 /*----------------------------------------------------------------------------*/
102 /*----------------------------------------------------------------------------*/
103 
104 int
105 uves_physmod_necregr(cpl_table** ord_tbl, cpl_table** reg_tbl)
106 
107 {
108 
109  int order=0;
110  int nb_order=0;
111  int present_order=0;
112  int order_nb=0;
113  int row=0;
114  int ncol=0;
115  int nrow=0;
116 
117  int selected=0;
118  int null=0;
119  int ord_min=0;
120  int ord_max=0;
121 
122  double x=0., y=0., cnt=0., sx=0., sy=0., sx2=0., sxy=0., sy2=0.;
123  double det=0., a=0., b=0., rms=0.;
124 
125  uves_msg_debug("start %s",__func__);
126  nrow=cpl_table_get_nrow(*ord_tbl);
127  ncol=cpl_table_get_ncol(*ord_tbl);
128 
129  uves_msg_debug("nrow=%d ncol=%d",nrow,ncol);
130  ord_min=cpl_table_get_column_min(*ord_tbl,"ORDER");
131  ord_max=cpl_table_get_column_max(*ord_tbl,"ORDER");
132  nb_order=ord_max-ord_min+1;
133  *reg_tbl=cpl_table_new(100);
134  cpl_table_new_column(*reg_tbl,"ORDER",CPL_TYPE_INT);
135  cpl_table_new_column(*reg_tbl,"RMS",CPL_TYPE_DOUBLE);
136 
137  row = 0;
138  selected=1;
139 
140  for (order=0; order<nb_order; order++) {
141 
142  cnt=0., sx=0., sy=0., sx2=0., sxy=0., sy2 = 0.;
143  order_nb=cpl_table_get_int(*ord_tbl,"ORDER",row,&null);
144 
145  present_order = order_nb;
146 
147  while (present_order == order_nb) {
148 
149  if (selected) {
150 
151  x=cpl_table_get_double(*ord_tbl,"X",row,&null);
152  y=cpl_table_get_double(*ord_tbl,"Y",row,&null);
153 
154  cnt += 1., sx += x, sy += y, sx2 += x*x, sy2 += y*y, sxy += x*y;
155  }
156 
157  if (row >= (nrow-1)) break;
158  row++;
159  present_order=cpl_table_get_int(*ord_tbl,"ORDER",row,&null);
160 
161  }
162 
163 
164  if (cnt >= 3) {
165  det = cnt*sx2 - sx*sx;
166  a = (sy*sx2 - sx*sxy)/det;
167  b = (cnt*sxy - sx*sy)/det;
168  rms = (sy2 - a*a*cnt - 2.*b*a*sx - b*b*sx2)/cnt;
169  if (rms < 0. && rms > -0.05) rms = 0.;
170  rms = sqrt(rms);
171  }
172  else rms = 99999.;
173 
174  cpl_table_set_int(*reg_tbl,"ORDER",order,order_nb);
175  cpl_table_set_double(*reg_tbl,"RMS",order,rms);
176 
177  }
178  cpl_table_erase_invalid_rows(*reg_tbl);
179  uves_msg_debug("end %s",__func__);
180  return 0;
181 }