UVES Pipeline Reference Manual  5.4.0
uves_physmod_qc1pmtbl.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.13 $
24  * $Name: not supported by cvs2svn $
25  * $Log: not supported by cvs2svn $
26  * Revision 1.11 2010/06/08 09:45:58 amodigli
27  * removed XMODREG_X_RES YMODREG_YNEW_RES columns from result of selplot=1 selection
28  *
29  * Revision 1.10 2010/06/07 16:49:56 amodigli
30  * added columns XMODREG_X_RES,YMODREG_YNEW_RES and now the XDIF,YDIF are computed for any of the points that do not satisfy the SELPLOT=1 condition
31  *
32  * Revision 1.9 2010/06/02 15:04:10 amodigli
33  * fixed bug in cross correlating line and model predicted tables
34  *
35  * Revision 1.8 2010/06/01 13:28:57 amodigli
36  * added column units to line table columns
37  *
38  * Revision 1.7 2008/09/29 06:55:33 amodigli
39  * add #include <string.h>
40  *
41  * Revision 1.6 2007/06/06 08:17:33 amodigli
42  * replace tab with 4 spaces
43  *
44  * Revision 1.5 2006/11/06 15:19:41 jmlarsen
45  * Removed unused include directives
46  *
47  * Revision 1.4 2006/10/05 11:17:49 jmlarsen
48  * Removed debugging call of uves_error_dump
49  *
50  * Revision 1.3 2006/06/20 10:56:56 amodigli
51  * cleaned output, added units
52  *
53  * Revision 1.2 2006/03/08 08:55:37 amodigli
54  * fixed garbage in SELPLOT column of line table
55  *
56  * Revision 1.1 2006/02/03 07:46:30 jmlarsen
57  * Moved recipe implementations to ./uves directory
58  *
59  * Revision 1.7 2006/01/19 08:47:24 jmlarsen
60  * Inserted missing doxygen end tag
61  *
62  * Revision 1.6 2006/01/16 08:01:57 amodigli
63  *
64  * Added stability check
65  *
66  * Revision 1.5 2006/01/05 14:29:59 jmlarsen
67  * Removed newline characters from output strings
68  *
69  * Revision 1.4 2006/01/05 09:30:48 amodigli
70  * Col SELPLOT initialized to NULL
71  *
72  * Revision 1.3 2005/12/19 16:17:55 jmlarsen
73  * Replaced bool -> int
74  *
75  */
76 
77 /*----------------------------------------------------------------------------*/
81 /*----------------------------------------------------------------------------*/
84 #ifdef HAVE_CONFIG_H
85 # include <config.h>
86 #endif
87 
88 /*-----------------------------------------------------------------------------
89  Includes
90  -----------------------------------------------------------------------------*/
91 #include <uves_physmod_qc1pmtbl.h>
92 
93 #include <uves_error.h>
94 #include <uves_msg.h>
95 #include <string.h>
96 /*-----------------------------------------------------------------------------
97  Defines
98  -----------------------------------------------------------------------------*/
99 #define FILESIZE 200
100 /*-----------------------------------------------------------------------------
101  Functions prototypes
102  ----------------------------------------------------------------------------*/
103 /*-----------------------------------------------------------------------------
104  Static variables
105  -----------------------------------------------------------------------------*/
106 
107 /*-----------------------------------------------------------------------------
108  Functions code
109  -----------------------------------------------------------------------------*/
110 
111 /*----------------------------------------------------------------------------*/
120 /*----------------------------------------------------------------------------*/
121 int uves_physmod_qc1pmtbl(cpl_table** rline_tbl,cpl_table** lin_tbl)
122 {
123 
124  int nCol = 0; /* No of columns in tbl */
125  int rlineRowNumber=0; /* number of rows in rline table */
126  int lineRowNo=0; /* number of Rows in line table */
127  int null=0; /* number of null values in keyword */
128  int i=0; /* loop variable */
129  int l=0; /* loop variable */
130 
131 
132  int selIdx=0; /* actual value */
133 
134  char lineTbl[FILESIZE]; /* buffer for data values */
135  char rLineTbl[FILESIZE]; /* buffer for data values */
136  char aHeading[FILESIZE]; /* column reference */
137  char tmps[FILESIZE]; /* for messout */
138 
139 
140 
141  double xDif=0.0; /* actual value */
142  double yDif=0.0; /* actual value */
143  double xModReg=0.0; /* actual value */
144  double yModReg=0.0; /* actual value */
145  double xPred=0.0; /* actual value */
146  double yPred=0.0; /* actual value */
147  double x=0.0; /* actual value */
148  double yNew=0.0; /* actual value */
149  double diffX=0.0; /* difference between x and y */
150  double diffY=0.0; /* difference between x and y */
151  /* (rounding error threshold) */
152  double waveReg=0.0;
153  double wavec=0.0;
154  int match=1;
155  int order=0;
156  int orderReg=0;
157 
158 
159 /* initializes chars */
160  memset(lineTbl, 0, FILESIZE);
161  memset(rLineTbl, 0, FILESIZE);
162  memset(aHeading, 0, FILESIZE);
163  memset(tmps, 0, FILESIZE);
164 
165 
166  uves_msg_debug("start %s",__func__);
167 
168  check(nCol=cpl_table_get_ncol(*rline_tbl),"Error getting ncol");
169  rlineRowNumber=cpl_table_get_nrow(*rline_tbl);
170  uves_msg_debug("Opened table %s nCol=%d nrow=%d",
171  "rline.tbl" ,nCol,rlineRowNumber);
172 
173  nCol=cpl_table_get_ncol(*lin_tbl);
174  lineRowNo=cpl_table_get_nrow(*lin_tbl);
175 
176  uves_msg_debug("Opened table %s nCol=%d nrow=%d",
177  lineTbl ,nCol,lineRowNo);
178 
179 
180  if(cpl_table_has_column(*lin_tbl,"XDIF")){
181  cpl_table_erase_column(*lin_tbl,"XDIF");
182  uves_msg_debug("removed column XDIF");
183  }
184 
185  if(cpl_table_has_column(*lin_tbl,"YDIF")){
186  cpl_table_erase_column(*lin_tbl,"YDIF");
187  uves_msg_debug("removed column YDIF");
188  }
189 
190  if(cpl_table_has_column(*lin_tbl,"SELPLOT")){
191  cpl_table_erase_column(*lin_tbl,"SELPLOT");
192  uves_msg_debug("removed column SELPLOT");
193  }
194 
195  if(cpl_table_has_column(*lin_tbl,"XPRED")){
196  cpl_table_erase_column(*lin_tbl,"XPRED");
197  uves_msg_debug("removed column XPRED");
198  }
199 
200  if(cpl_table_has_column(*lin_tbl,"YPRED")){
201  cpl_table_erase_column(*lin_tbl,"YPRED");
202  uves_msg_debug("removed column YPRED");
203  }
204 
205  /* Then add those columns */
206 
207  cpl_table_new_column(*lin_tbl,"XPRED",CPL_TYPE_DOUBLE);
208  cpl_table_set_column_unit(*lin_tbl,"XPRED","pix");
209 
210  cpl_table_new_column(*lin_tbl,"YPRED",CPL_TYPE_DOUBLE);
211  cpl_table_set_column_unit(*lin_tbl,"YPRED","pix");
212 
213  cpl_table_new_column(*lin_tbl,"XDIF",CPL_TYPE_DOUBLE);
214  cpl_table_set_column_unit(*lin_tbl,"XDIF","pix");
215 
216  cpl_table_new_column(*lin_tbl,"YDIF",CPL_TYPE_DOUBLE);
217  cpl_table_set_column_unit(*lin_tbl,"YDIF","pix");
218 
219  cpl_table_new_column(*lin_tbl,"SELPLOT",CPL_TYPE_INT);
220  cpl_table_set_column_unit(*lin_tbl,"YDIF","pix");
221 
222 
223 /*
224  ***************************************************************************
225  * Now does the actual ordering of the elements writing in the right place *
226  ***************************************************************************
227  */
228 
229  cpl_table_set_column_invalid(*lin_tbl,"SELPLOT",0,
230  cpl_table_get_nrow(*lin_tbl));
231 
232 
233 /*
234  uves_msg("rlineRowNumber=%d",rlineRowNumber);
235  cpl_table_dump(*rline_tbl,1,2,stdout);
236  cpl_table_dump(*lin_tbl,1,2,stdout);
237 */
238  for (l=0; l< rlineRowNumber; l++) {
239 
240 
241  xModReg=cpl_table_get_double(*rline_tbl,"XMODREG",l,&null);
242  yModReg=cpl_table_get_double(*rline_tbl,"YMODREG",l,&null);
243  xDif=cpl_table_get_double(*rline_tbl,"XDIF",l,&null);
244  yDif=cpl_table_get_double(*rline_tbl,"YDIF",l,&null);
245  xPred=cpl_table_get_double(*rline_tbl,"XMOD",l,&null);
246  yPred=cpl_table_get_double(*rline_tbl,"YMOD",l,&null);
247  waveReg=cpl_table_get_double(*rline_tbl,"WAVE",l,&null);
248  check_nomsg(orderReg=cpl_table_get_int(*rline_tbl,"ORDER",l,&null));
249  //uves_msg("i=%d xDif=%f yDif=%f lineRowNo=%d",i,xDif,yDif,lineRowNo);
250 
251  selIdx = 1;
252  match=0;
253  for (i=0; i< lineRowNo; i++) {
254  //uves_msg("i=%d",i);
255  x=cpl_table_get_double(*lin_tbl,"X",i,&null);
256  yNew=cpl_table_get_double(*lin_tbl,"YNEW",i,&null);
257  wavec=cpl_table_get_double(*lin_tbl,"WAVEC",i,&null);
258  check_nomsg(order=cpl_table_get_int(*lin_tbl,"Order",i,&null));
259  diffX=fabs(x-xModReg);
260  diffY=fabs(yNew-yModReg);
261 /*
262  uves_msg("waveReg=%f wavec=%f diff=%f",
263  10.*waveReg, wavec, fabs(waveReg - wavec));
264 */
265  if ( (fabs(10.*waveReg - wavec) <= 0.01) &&
266  match == 0 &&
267  (fabs(orderReg-order)<0.1) ) {
268  //uves_msg("match wave l=%d i=%d order=%d",l,i,order);
269  cpl_table_set_double(*lin_tbl,"XPRED",i,xPred);
270  cpl_table_set_double(*lin_tbl,"YPRED",i,yPred);
271  cpl_table_set_double(*lin_tbl,"XDIF",i,xDif);
272  cpl_table_set_double(*lin_tbl,"YDIF",i,yDif);
273  }
274  if ((diffX <= 0.001) && (diffY <= 0.001)) {
275  match=1;
276  cpl_table_set_double(*lin_tbl,"XPRED",i,xPred);
277  cpl_table_set_double(*lin_tbl,"YPRED",i,yPred);
278  cpl_table_set_double(*lin_tbl,"XDIF",i,xDif);
279  cpl_table_set_double(*lin_tbl,"YDIF",i,yDif);
280 
281  cpl_table_set_int(*lin_tbl,"SELPLOT",i,selIdx);
282 
283  break;
284  }
285  }
286  }
287 
288 
289  /* second check to add only those lines that are not matching selplot=1
290  criterium */
291 
292  for (l=0; l< rlineRowNumber; l++) {
293  xModReg=cpl_table_get_double(*rline_tbl,"XMODREG",l,&null);
294  yModReg=cpl_table_get_double(*rline_tbl,"YMODREG",l,&null);
295  xDif=cpl_table_get_double(*rline_tbl,"XDIF",l,&null);
296  yDif=cpl_table_get_double(*rline_tbl,"YDIF",l,&null);
297  xPred=cpl_table_get_double(*rline_tbl,"XMOD",l,&null);
298  yPred=cpl_table_get_double(*rline_tbl,"YMOD",l,&null);
299  waveReg=cpl_table_get_double(*rline_tbl,"WAVE",l,&null);
300  check_nomsg(orderReg=cpl_table_get_int(*rline_tbl,"ORDER",l,&null));
301  //uves_msg("i=%d xDif=%f yDif=%f lineRowNo=%d",i,xDif,yDif,lineRowNo);
302 
303  for (i=0; i< lineRowNo; i++) {
304  //uves_msg("i=%d",i);
305  x=cpl_table_get_double(*lin_tbl,"X",i,&null);
306  yNew=cpl_table_get_double(*lin_tbl,"YNEW",i,&null);
307  wavec=cpl_table_get_double(*lin_tbl,"WAVEC",i,&null);
308  check_nomsg(order=cpl_table_get_int(*lin_tbl,"Order",i,&null));
309  check_nomsg(selIdx=cpl_table_get_int(*lin_tbl,"SELPLOT",i,&null));
310  diffX=fabs(x-xModReg);
311  diffY=fabs(yNew-yModReg);
312 
313  if ( selIdx != 1 &&
314  (fabs(orderReg-order)<0.1) ) {
315  cpl_table_set_double(*lin_tbl,"XPRED",i,xPred);
316  cpl_table_set_double(*lin_tbl,"YPRED",i,yPred);
317  cpl_table_set_double(*lin_tbl,"XDIF",i,xDif);
318  cpl_table_set_double(*lin_tbl,"YDIF",i,yDif);
319  break;
320  }
321  }
322  }
323 
324 
325 
326 
327  /*
328  cpl_table_dump(*lin_tbl,0,20,stdout);
329  */
330  uves_msg_debug("end %s",__func__);
331  cpl_table_fill_invalid_int(*lin_tbl,"SELPLOT",-1);
332 
333  cleanup:
334  return 0;
335 
336 }