UVES Pipeline Reference Manual  5.4.0
uves_response-test.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: 2012-01-17 07:53:20 $
23  * $Revision: 1.8 $
24  * $Name: not supported by cvs2svn $
25  * $Log: not supported by cvs2svn $
26  * Revision 1.7 2010/02/08 07:11:39 amodigli
27  * added test_load_3dtable
28  *
29  * Revision 1.6 2009/06/05 05:49:02 amodigli
30  * updated init/end to cpl5
31  *
32  * Revision 1.5 2008/09/29 07:02:05 amodigli
33  * add #include <string.h>
34  *
35  * Revision 1.4 2007/05/23 06:43:23 jmlarsen
36  * Removed unused variables
37  *
38  * Revision 1.3 2007/04/24 12:50:29 jmlarsen
39  * Replaced cpl_propertylist -> uves_propertylist which is much faster
40  *
41  * Revision 1.2 2007/03/20 07:28:02 jmlarsen
42  * Test TYPE = 'NULL'
43  *
44  * Revision 1.1 2007/03/15 12:27:18 jmlarsen
45  * Moved unit tests to ./uves/tests and ./flames/tests
46  *
47  * Revision 1.2 2007/02/27 14:04:14 jmlarsen
48  * Move unit test infrastructure to IRPLIB
49  *
50  * Revision 1.1 2007/02/21 12:38:26 jmlarsen
51  * Renamed _test -> -test
52  *
53  * Revision 1.22 2007/01/29 12:17:54 jmlarsen
54  * Support setting verbosity from command line
55  *
56  * Revision 1.21 2006/11/24 09:39:35 jmlarsen
57  * Factored out termination code
58  *
59  * Revision 1.20 2006/11/16 09:49:25 jmlarsen
60  * Fixed doxygen bug
61  *
62  * Revision 1.19 2006/11/08 14:03:59 jmlarsen
63  * Doxybugfix
64  *
65  * Revision 1.18 2006/11/07 13:59:19 jmlarsen
66  * Removed memory leaks
67  *
68  * Revision 1.17 2006/11/06 15:31:52 jmlarsen
69  * Added check for memory leak
70  *
71  * Revision 1.16 2006/11/06 15:30:54 jmlarsen
72  * Added missing includes
73  *
74  * Revision 1.15 2006/11/06 15:19:41 jmlarsen
75  * Removed unused include directives
76  *
77  * Revision 1.14 2006/11/03 15:15:46 jmlarsen
78  * Added test of uves_align
79  *
80  * Revision 1.16 2006/09/11 13:59:01 jmlarsen
81  * Renamed identifier reserved by POSIX
82  *
83  */
84 
85 /*-----------------------------------------------------------------------------
86  Includes
87  -----------------------------------------------------------------------------*/
88 
89 #ifdef HAVE_CONFIG_H
90 # include <config.h>
91 #endif
92 
93 #include <uves_response_utils.h>
94 #include <uves_utils_wrappers.h>
95 #include <uves_pfits.h>
96 #include <uves_error.h>
97 #include <uves_msg.h>
98 
99 #include <cpl_test.h>
100 #include <cpl.h>
101 #include <string.h>
102 /*-----------------------------------------------------------------------------
103  Defines
104  -----------------------------------------------------------------------------*/
105 
106 /*-----------------------------------------------------------------------------
107  Functions prototypes
108  -----------------------------------------------------------------------------*/
109 
110 /*----------------------------------------------------------------------------*/
114 /*----------------------------------------------------------------------------*/
116 /*----------------------------------------------------------------------------*/
120 /*----------------------------------------------------------------------------*/
121 static void
123 {
124  cpl_table *flux_table = NULL;
125  const char* dir_name="/media/VERBATIM/data7/flames/flames_uves_demo/ref/";
126  const char* tab_name="flxstd.fits";
127  char res_name[80];
128  char ref_name[25];
129  char ref_type[25];
130 
131  char full_name[256];
132  int nraws=0;
133  int i=0;
134  double ref_ra=0;
135  double ref_dec=0;
136  uves_propertylist* header=NULL;
137  const char *columns[3] = {"LAMBDA", "BIN_WIDTH", "F_LAMBDA"};
138  int ndata; /* Number of elements in column */
139  int j=0;
140  cpl_table* result=NULL;
141  sprintf(full_name,"%s%s",dir_name,tab_name);
142  uves_msg("full name: %s",full_name);
143  flux_table=cpl_table_load(full_name,1,0);
144  nraws=cpl_table_get_nrow(flux_table);
145  header=uves_propertylist_load(full_name,0);
146 
147  uves_msg("std: name \t type \t ra \t dec");
148  for(i=0;i<nraws; i++) {
149 
150  check( ref_ra = cpl_table_get_double(flux_table, "RA_DEG", i, NULL),
151  "Could not read catalogue star right ascension");
152 
153  check( ref_dec = cpl_table_get_double(flux_table, "DEC_DEG", i, NULL),
154  "Could not read catalogue star declination");
155 
156  check( sprintf(ref_name, "%s", cpl_table_get_string(flux_table, "OBJECT", i)),
157  "Could not read reference object name");
158 
159  check( sprintf(ref_type, "%s", cpl_table_get_string(flux_table, "TYPE", i)),
160  "Could not read reference object type");
161 
162  uves_msg("std: %s \t %s \t %f \t %f",ref_name,ref_type,ref_ra,ref_dec);
163 
164  sprintf(res_name,"%s.fits",ref_name);
165  check( ndata = cpl_table_get_int(flux_table, "NDATA", i, NULL),
166  "Error reading length of flux array");
167 
168  result=cpl_table_new(ndata);
169  for(j = 0; j < 3; j++)
170  {
171  const cpl_array *data;
172  int indx;
173 
174  cpl_table_new_column(result, columns[j], CPL_TYPE_DOUBLE);
175 
176  data = cpl_table_get_array(flux_table, columns[j], i);
177 
178  /* Only the 'ndata' first elements of the array are used,
179  and the array may be longer than this */
180  uves_msg_debug("3d table array size = %" CPL_SIZE_FORMAT ", ndata = %d",
181  cpl_array_get_size(data), ndata);
182 
183  assure( cpl_array_get_size(data) >= ndata,
184  CPL_ERROR_ILLEGAL_INPUT,
185  "Flux table row %d: column '%s' depth (%" CPL_SIZE_FORMAT ") "
186  "is less than NDATA (%d)",
187  i, columns[j], cpl_array_get_size(data), ndata);
188 
189  for (indx = 0; indx < ndata; indx++)
190  {
191  /* 3d columns are float */
192  cpl_table_set_double(result, columns[j], indx,
193  cpl_array_get_float(data, indx, NULL));
194  }
195  }
196 
197  uves_table_save(result, header, NULL,res_name, CPL_IO_DEFAULT);
198  uves_free_table(&result);
199  }
200 
201 
202  cleanup:
203  uves_free_table(&flux_table);
204  uves_free_propertylist(&header);
205 
206  return;
207 }
208 
209 /*----------------------------------------------------------------------------*/
213 /*----------------------------------------------------------------------------*/
214 static void
216 {
217  /* Create flux table + header */
218  cpl_table *flux_table = NULL;
219  uves_propertylist *raw_header = NULL;
220  cpl_array *values = NULL;
221  int depth = 10;
222  int nrow = 2;
223  const char *object[] = {"first std", "another standard star"};
224  const char *type [] = {"NULL", "type 2"};
225  const double lambda[] = {8000, 2000};
226  const double fluxes[] = {1000000, 200};
227  const double bin_width[] = {0.8, 0.0003};
228  const double ra[] = {10, 80};
229  const double dec[] = {-8, 0};
230 
231  cpl_table *cat_flux = NULL;
232  char *ref_name = NULL;
233 
234  int i;
235 
236  /* Create flux table */
237  flux_table = cpl_table_new(nrow);
238 
239  cpl_table_new_column(flux_table, "RA_DEG", CPL_TYPE_DOUBLE);
240  cpl_table_new_column(flux_table, "DEC_DEG", CPL_TYPE_DOUBLE);
241 
242  cpl_table_new_column(flux_table, "OBJECT", CPL_TYPE_STRING);
243  cpl_table_new_column(flux_table, "TYPE", CPL_TYPE_STRING);
244 
245  cpl_table_new_column(flux_table, "NDATA", CPL_TYPE_INT); /* depth of arrays */
246 
247  cpl_table_new_column_array(flux_table, "LAMBDA", CPL_TYPE_FLOAT, depth);
248  cpl_table_new_column_array(flux_table, "BIN_WIDTH", CPL_TYPE_FLOAT, depth);
249  cpl_table_new_column_array(flux_table, "F_LAMBDA", CPL_TYPE_FLOAT, depth);
250 
251  values = cpl_array_new(depth, CPL_TYPE_FLOAT);
252 
253  for (i = 0; i < nrow; i++)
254  {
255  cpl_table_set_double(flux_table, "RA_DEG", i, ra[i]);
256  cpl_table_set_double(flux_table, "DEC_DEG", i, dec[i]);
257  cpl_table_set_string(flux_table, "OBJECT", i, object[i]);
258  cpl_table_set_string(flux_table, "TYPE", i, type[i]);
259  cpl_table_set_int (flux_table, "NDATA", i, depth);
260 
261  cpl_array_fill_window_float(values, 0, depth, lambda[i]);
262  cpl_table_set_array(flux_table, "LAMBDA", i, values);
263 
264  cpl_array_fill_window_float(values, 0, depth, bin_width[i]);
265  cpl_table_set_array(flux_table, "BIN_WIDTH", i, values);
266 
267  cpl_array_fill_window_float(values, 0, depth, fluxes[i]);
268  cpl_table_set_array(flux_table, "F_LAMBDA", i, values);
269  }
270 
271  /* Create header */
272  raw_header = uves_propertylist_new();
273 
274  /* Set RA, DEC to match each row of flux table, and verify that
275  we got the corresponding std star */
276  for (i = 0; i < nrow; i++)
277  {
278  double paccuracy = 60; /* arcsecs */
279  double residual = 30;
280 
281  uves_pfits_set_ra (raw_header, ra[i]+residual/3600);
282  uves_pfits_set_dec(raw_header, dec[i]-residual/3600);
283 
284  uves_free_table(&cat_flux);
285  cpl_free(ref_name); ref_name = NULL;
286  check_nomsg( cat_flux = uves_align(raw_header,
287  flux_table,
288  paccuracy,
289  &ref_name));
290 
291  /* Saving table string values to FITS might add trailing blanks;
292  replace with end-of-string */
293  while(ref_name[strlen(ref_name)-1] == ' ')
294  {
295  /* O(n^2) but that is okay */
296  ref_name[strlen(ref_name)-1] = '\0';
297  }
298 
299  /* cpl_table_dump(cat_flux, 0, cpl_table_get_nrow(cat_flux), stdout); */
300 
301  assure( cpl_table_get_nrow(cat_flux) == depth, CPL_ERROR_ILLEGAL_OUTPUT,
302  "Flux table has %" CPL_SIZE_FORMAT " rows, %d expected",
303  cpl_table_get_nrow(cat_flux), depth);
304 
305  assure( strcmp(ref_name, object[i]) == 0, CPL_ERROR_ILLEGAL_OUTPUT,
306  "Found '%s'; Expected '%s'", ref_name, object[i]);
307 
308 #if 0
309  assure( float_equal(cpl_table_get_column_mean(cat_flux, "LAMBDA"),
310  lambda[i], 0.001),
311  CPL_ERROR_ILLEGAL_OUTPUT,
312  "LAMBDA = %f; Expected = %f",
313  cpl_table_get_column_mean(cat_flux, "LAMBDA"), lambda[i]);
314 
315  assure( float_equal(cpl_table_get_column_mean(cat_flux, "BIN_WIDTH"),
316  bin_width[i], 0.001),
317  CPL_ERROR_ILLEGAL_OUTPUT,
318  "BIN_WIDTH = %f; Expected = %f",
319  cpl_table_get_column_mean(cat_flux, "BIN_WIDTH"), bin_width[i]);
320 
321  assure( float_equal(cpl_table_get_column_mean(cat_flux, "F_LAMBDA"),
322  fluxes[i], 0.001),
323  CPL_ERROR_ILLEGAL_OUTPUT,
324  "F_LAMBDA = %f; Expected = %f",
325  cpl_table_get_column_mean(cat_flux, "F_LAMBDA"), fluxes[i]);
326 #endif
327  cpl_test_rel(cpl_table_get_column_mean(cat_flux, "LAMBDA"),
328  lambda[i], 0.001);
329 
330  cpl_test_rel(cpl_table_get_column_mean(cat_flux, "BIN_WIDTH"),
331  bin_width[i], 0.001);
332 
333  cpl_test_rel(cpl_table_get_column_mean(cat_flux, "F_LAMBDA"),
334  fluxes[i], 0.001);
335  }
336 
337  cleanup:
338  uves_free_table(&flux_table);
339  uves_free_table(&cat_flux);
340  uves_free_array(&values);
341  uves_free_propertylist(&raw_header);
342  cpl_free(ref_name);
343 
344 
345  return;
346 }
347 
348 /*----------------------------------------------------------------------------*/
352 /*----------------------------------------------------------------------------*/
353 
354 int main(void)
355 {
356  cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
357 
359  //check_nomsg( test_load_3dtable() );
360 
361  cleanup:
362  return cpl_test_end(0);
363 }
364