UVES Pipeline Reference Manual  5.5.5b1
uves_dfs-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: 2009-06-05 05:49:02 $
23  * $Revision: 1.20 $
24  */
25 
26 /*-----------------------------------------------------------------------------
27  Includes
28  -----------------------------------------------------------------------------*/
29 
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33 
34 #include <uves_dfs.h>
35 #include <uves_parameters.h>
36 #include <uves_utils_wrappers.h>
37 #include <uves_test_simulate.h>
38 #include <uves_pfits.h>
39 #include <uves_error.h>
40 
41 #include <cpl_test.h>
42 #include <cpl.h>
43 
44 #include <float.h>
45 /*-----------------------------------------------------------------------------
46  Defines
47  -----------------------------------------------------------------------------*/
48 
49 /*-----------------------------------------------------------------------------
50  Functions prototypes
51  -----------------------------------------------------------------------------*/
52 
53 
54 /*----------------------------------------------------------------------------*/
58 /*----------------------------------------------------------------------------*/
62 /*----------------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------------*/
67 static void
69 {
71  polynomial *p = NULL;
72 
73  const char *data[] = {"",
74  "'COEFFI','I*4',1,7,'7I10'",
75  " 53889 2 3 2 1 4 5",
76  "",
77  "'COEFFR','R*4',1,5,'5E14.7'",
78  " 4.3300000E+02 4.0880000E+03 1.0000000E+00 2.1000000E+01 0.0000000E+00",
79  "",
80  "'COEFFD','R*8',1,30,'3E23.15'",
81  " -7.097005629698889E+01 4.050908371864904E-02 -2.886756545398909E-06",
82  " 5.504345508879626E-10 -5.583004967206025E-14 7.624532125635992E+01",
83  " -2.428213567964009E-03 1.819158447566360E-06 -5.090366383338846E-10",
84  " 5.198098506055602E-14 3.513177145982783E-01 5.570332137951829E-04",
85  " -3.876157463910250E-07 1.113253735718822E-10 -1.132455173423791E-14",
86  " 2.977232589499959E-02 -5.389240622889887E-05 3.777456726044612E-08",
87  " -1.083863050648735E-11 1.098450510939580E-15 -1.093309039442914E-03",
88  " 2.402609262989674E-06 -1.688416547941747E-09 4.839101712729582E-13",
89  " -4.884504488944702E-17 1.919853952642526E-05 -4.004133160220927E-08",
90  " 2.816206503824200E-11 -8.051313882805877E-15 8.090579180112579E-19",
91  " ",
92  "'TAB_IN_OUT_YSHIFT','R*8',1,1,'3E23.15'",
93  " 4.180818583555659E+01 ",
94  " "};
95  unsigned int i;
96 
97  /* Create typical FLAMES ordef table header */
98  for (i = 0; i < 8000; i++)
99  {
100  uves_propertylist_append_string(
101  header, "HISTORY",
102  " 35834 35835 35836 35837 35838 35839 35840");
103  }
104 
105  for (i = 0; i < sizeof(data)/sizeof(char *); i++)
106  {
107  uves_propertylist_append_string(
108  header, "HISTORY",
109  data[i]);
110  }
111 
112  check_nomsg( p = uves_polynomial_convert_from_plist_midas(header,
113  "COEFF",-1));
114 
115  assure( uves_polynomial_get_dimension(p) == 2, CPL_ERROR_ILLEGAL_OUTPUT,
116  "Dimension is %d, 2 expected", uves_polynomial_get_dimension(p));
117 
118  cleanup:
119  uves_free_propertylist(&header);
121 
122  return;
123 }
124 
125 /*----------------------------------------------------------------------------*/
129 /*----------------------------------------------------------------------------*/
130 static void
132 {
133  const char * const filename = "linetable.fits";
134  cpl_table *linetable_in = NULL;
135  polynomial *dispersion = NULL;
136  polynomial *absorder = NULL;
139 
140  cpl_frame *f = cpl_frame_new();
141  cpl_frameset *frames = cpl_frameset_new();
142  bool flames = false;
143  const char *const chip_id = "CCD42";
144  cpl_table *ordertable = NULL;
145  cpl_table *tracetable = NULL;
146  polynomial *order_locations = NULL;
147  int firstabs, lastabs;
148  enum uves_chip chip = UVES_CHIP_BLUE;
149  int minorder = 1;
150  int maxorder = 5;
151  int nx = 150;
152  /*int ny = 100;*/
153  int trace_id = 0;
154  int window = 1;
155 
156  /* output */
157  const char *linetable_filename;
158  cpl_table *linetable_out = NULL;
159  uves_propertylist *header_out = NULL;
160  polynomial *dispersion_out = NULL;
161  polynomial *absorder_out = NULL;
162 
163  /* build data */
164  check_nomsg( create_order_table(&ordertable, &order_locations, &tracetable,
165  minorder, maxorder, nx));
166  check_nomsg( create_line_table(&linetable_in, &dispersion, &absorder,
167  &firstabs, &lastabs,
168  minorder, maxorder, nx));
169  check_nomsg( uves_propertylist_append_string(header, UVES_CHIP_ID(chip), chip_id));
170  check_nomsg( uves_propertylist_append_string(header, UVES_DRS_ID, "CPL"));
171 
172  check_nomsg( uves_pfits_set_firstabsorder(eheader, firstabs) );
173  check_nomsg( uves_pfits_set_lastabsorder(eheader, lastabs) );
174 
175  check_nomsg( uves_pfits_set_traceid(eheader, trace_id) );
176  check_nomsg( uves_pfits_set_windownumber(eheader, window) );
177 
178  check_nomsg( uves_table_save(linetable_in, header, eheader, filename, CPL_IO_DEFAULT) );
179  check_nomsg( uves_save_polynomial(dispersion, filename, eheader) );
180  check_nomsg( uves_save_polynomial(absorder, filename, eheader) );
181 
182  cpl_test_eq(cpl_error_get_code(), CPL_ERROR_NONE);
183 
184  cpl_frame_set_filename(f, filename);
185  cpl_frame_set_tag(f, "LINE_TABLE_BLUE");
186  cpl_frameset_insert(frames, f);
187 
188  check_nomsg( uves_load_linetable(frames,
189  flames,
190  chip_id,
191  order_locations, minorder, maxorder,
192  &linetable_filename,
193  &linetable_out,
194  &header_out,
195  &dispersion_out,
196  &absorder_out,
197  chip, trace_id, window));
198 
199  cpl_test_eq(cpl_error_get_code(), CPL_ERROR_NONE);
200 
201  cpl_test( linetable_out != NULL );
202  cpl_test( header_out != NULL );
203  cpl_test( dispersion_out != NULL );
204  cpl_test( absorder_out != NULL );
205 
206  cpl_test_eq( cpl_table_get_nrow(linetable_in),
207  cpl_table_get_nrow(linetable_out) );
208 
209  /* not required: cpl_test_eq( cpl_table_get_ncol(linetable_in),
210  cpl_table_get_ncol(linetable_out) );
211  */
212 
213  cpl_test_eq_string( filename, linetable_filename );
214 
215  {
216  int order;
217  int x;
218 
219  for (order = minorder; order <= maxorder; order++) {
220  for (x = 1; x <= nx; x += nx/6) {
221  cpl_test_rel(
222  uves_polynomial_evaluate_2d(absorder , x, order),
223  uves_polynomial_evaluate_2d(absorder_out, x, order), 0.001);
224 
225  cpl_test_rel(
226  uves_polynomial_evaluate_2d(dispersion , x, order),
227  uves_polynomial_evaluate_2d(dispersion_out, x, order), 0.001);
228  }
229  }
230  }
231 
232  cleanup:
233  uves_free_frameset(&frames);
234  uves_free_table(&linetable_in);
235  uves_free_table(&linetable_out);
236  uves_polynomial_delete(&dispersion);
237  uves_polynomial_delete(&absorder);
238  uves_polynomial_delete(&dispersion_out);
239  uves_polynomial_delete(&absorder_out);
240  uves_free_propertylist(&header_out);
241  uves_free_propertylist(&header);
242  uves_free_propertylist(&eheader);
243  uves_free_table(&ordertable);
244  uves_free_table(&tracetable);
245  uves_polynomial_delete(&order_locations);
246 
247  return;
248 }
249 
250 
251 /*----------------------------------------------------------------------------*/
255 /*----------------------------------------------------------------------------*/
256 static void
258 {
259  const char *values[] = {"HISTORY", "",
260  "HISTORY", "'FIBREPOS','R*8',1,9,'3E23.15'",
261  "HISTORY", " -3.243571124678650E+01 -2.309646501161805E+01 -1.402902770375962E+01",
262  "HISTORY", " -4.772375924542811E+00 4.827040349175236E+00 1.378761244187003E+01",
263  "HISTORY", " 2.321337764943556E+01 3.243571124678650E+01 -3.552713678800501E-15",
264  "HISTORY", "",
265  "HISTORY", "'COEFFR','R*4',1,20,'5E14.7'",
266  "HISTORY", "9.4893160E+00 4.0716226E+03 0.0000000E+00 2.3000000E+01 1.8538159E-04",
267  "HISTORY", "0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00",
268  "HISTORY", "",
269  "HISTORY", "'INTVAL','I",
270  "HISTORY", "1 2 3 4 5 6",
271  "HISTORY", "7",
272  "HISTORY", "",
273  "HISTORY", "'LEGAL','C",
274  "HISTORY", " a sdfasdf",
275  "HISTORY", "",
276  "HISTORY", "'ILLEGAL','C",
277  "HISTORY", "1bsdf",
278  "HISTORY", "bsdf",
279  "HISTORY", "",
280  "HISTORY", "'CHIPCHOICE','C",
281  "HISTORY", "abcd",
282  "HISTORY", ""};
283 
284  int N = sizeof(values) / sizeof(const char *) / 2;
285 
286  uves_propertylist *header = NULL;
287  double *resultd = NULL;
288  int *resulti = NULL;
289  float *resultf = NULL;
290  const char *results = NULL;
291  int result_length, i;
292  int nkeys;
293  cpl_type result_type;
294 
295  header = uves_propertylist_new();
296  for (i = 0; i < N; i++)
297  {
298  uves_propertylist_append_string(header, values[i*2], values[i*2+1]);
299  }
300 
301  check_nomsg(resultd = uves_read_midas_array(header, "FIBREPOS", &result_length,
302  &result_type, &nkeys));
303 
304  cpl_test_eq(result_type, CPL_TYPE_DOUBLE);
305  cpl_test_eq(result_length, 9);
306  cpl_test_eq(nkeys, 5);
307 
308  /* Check numbers, see above */
309  cpl_test_rel(resultd[0], -32, 0.10);
310  cpl_test_rel(resultd[3], -4.7, 0.10);
311  cpl_test_rel(resultd[6], 23, 0.10);
312  cpl_test( fabs(resultd[8]) < 0.001);
313 
314  /* float */
315  check_nomsg(resultf = uves_read_midas_array(header, "COEFFR", &result_length,
316  &result_type, &nkeys));
317  cpl_test_eq(result_type, CPL_TYPE_FLOAT);
318  cpl_test_eq(result_length, 10);
319  cpl_test_eq(nkeys, 4);
320 
321  cpl_test_rel(resultf[0], 9.489, 0.01);
322  cpl_test_rel(resultf[1], 4071, 0.01);
323  cpl_test_abs(resultf[2], 0.000, 0.01);
324  cpl_test_rel(resultf[3], 23.00, 0.01);
325 
326  /* integer */
327  check_nomsg(resulti = uves_read_midas_array(header, "INTVAL", &result_length,
328  &result_type, &nkeys));
329 
330  cpl_test_eq(result_type, CPL_TYPE_INT);
331  cpl_test_eq(result_length, 7);
332  cpl_test_eq(nkeys, 4);
333  for (i = 1; i <= 7; i++)
334  {
335  cpl_test_eq(resulti[i-1], i);
336  }
337 
338 
339  /* string */
340  check_nomsg( results = uves_read_midas_array(header, "LEGAL", &result_length,
341  &result_type, &nkeys) );
342 
343  cpl_test_eq(result_type, CPL_TYPE_STRING);
344  cpl_test_eq(result_length, 10);
345  cpl_test_eq(nkeys, 3);
346  cpl_test_eq_string(results, " a sdfasdf");
347 
348  cpl_test(uves_read_midas_array(header, "ILLEGAL2", &result_length,
349  &result_type, &nkeys) == NULL);
351 
352  uves_free_string_const(&results);
353  check_nomsg(results = uves_read_midas_array(header, "CHIPCHOICE", &result_length,
354  &result_type, &nkeys));
355 
356  cpl_test_eq(result_type, CPL_TYPE_STRING);
357  cpl_test_eq(result_length, 4);
358  cpl_test_eq(nkeys, 3);
359  cpl_test_eq_string(results, "abcd");
360 
361  /* Performance test (relevant for long FLAMES FITS headers) */
362  N = 9000;
363  uves_free_propertylist(&header);
364  header = uves_propertylist_new();
365  uves_propertylist_append_string(header, "HISTORY", "'SELIDX','I*4',1,48389,'7I10'");
366  for (i = 0; i < N; i++)
367  {
368  uves_propertylist_append_string(
369  header, "HISTORY",
370  " 64605 64606 64607 64608 64609 64610 64611");
371  }
372  uves_propertylist_append_string(header, "HISTORY", "");
373 
374  uves_free_int(&resulti);
375  check_nomsg( resulti = uves_read_midas_array(header, "SELIDX", &result_length,
376  &result_type, &nkeys));
377 
378  cpl_test_eq(result_type, CPL_TYPE_INT);
379  cpl_test_eq(result_length, N*7);
380  cpl_test_eq(nkeys, 1+N+1);
381 
382  cleanup:
383  uves_free_propertylist(&header);
384  uves_free_double(&resultd);
385  uves_free_int(&resulti);
386  uves_free_float(&resultf);
387  uves_free_string_const(&results);
388  return;
389 }
390 
391 /*----------------------------------------------------------------------------*/
395 /*----------------------------------------------------------------------------*/
396 static void
398 {
399  const int N = 100;
400  int i;
401 
402  cpl_image *image = cpl_image_new(N, 1, CPL_TYPE_DOUBLE);
403 
404  double inf = DBL_MAX;
405  for (i = 1; i <= N; i++)
406  {
407  cpl_image_set(image, i, 1, -FLT_MAX*200);
408  inf *= 10;
409  }
410 
411 
412  cpl_image_set(image, 1, 1, inf);
413  cpl_image_set(image, 2, 1, inf/inf);
414 
415  uves_save_image(image, "dfs.fits", NULL, true, true);
416  uves_free_image(&image);
417 
418  return;
419 }
420 
421 /*----------------------------------------------------------------------------*/
425 /*----------------------------------------------------------------------------*/
426 static void
428 {
429  cpl_frameset *frames = cpl_frameset_new();
430  cpl_parameterlist *parameters = cpl_parameterlist_new();
431  int nx = 1500;
432  int ny = 1024;
433 
434  cpl_image *image = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
436  uves_propertylist *product_header = uves_propertylist_new();
437  const char *starttime;
438  const char *recipe_id = "uves_cal_phony";
439  const char *tag = "PHONY_TAG";
440 
441 
442  uves_define_global_parameters(parameters);
443  cpl_test_eq( cpl_error_get_code(), CPL_ERROR_NONE );
444 
445  /* Create raw image */
446  {
447  cpl_image *raw_image = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
448  cpl_frame *raw_frame = cpl_frame_new();
449 
450  {
451  int i;
452  int nkey = 360;
453  for (i = 0; i < nkey; i++)
454  {
455  const char *key_name = uves_sprintf("KEY%d", i);
456  uves_propertylist_append_int(raw_header, key_name, i);
457  uves_free_string_const(&key_name);
458  }
459  uves_propertylist_append_string(raw_header, "ORIGIN", "unknown...");
460  }
461  const char *raw_filename = "raw_file.fits";
462  uves_image_save(raw_image,
463  raw_filename,
464  CPL_BPP_IEEE_FLOAT,
465  raw_header,
466  CPL_IO_DEFAULT);
467  cpl_test_eq( cpl_error_get_code(), CPL_ERROR_NONE );
468 
469  uves_free_image(&raw_image);
470 
471  /* Wrap frame */
472  cpl_frame_set_tag(raw_frame, "BIAS_BLUE"); /* Use recognized tag,
473  so that FRAME_TYPE
474  is set to RAW */
475  cpl_frame_set_filename(raw_frame, raw_filename);
476  cpl_frameset_insert(frames, raw_frame);
477  }
478 
479  starttime = uves_initialize(frames, parameters, recipe_id,
480  "This recipe does not do anything");
481  cpl_test_eq( cpl_error_get_code(), CPL_ERROR_NONE );
482 
483  uves_frameset_insert(frames,
484  image,
485  CPL_FRAME_GROUP_PRODUCT,
486  CPL_FRAME_TYPE_IMAGE,
487  CPL_FRAME_LEVEL_INTERMEDIATE,
488  "dfs_product.fits",
489  tag,
490  raw_header,
491  product_header,
492  NULL, /* table header */
493  parameters,
494  recipe_id,
495  PACKAGE "/" PACKAGE_VERSION,
496  NULL, /* qc table */
497  starttime,
498  false, /* dump PAF */
499  0 /* stats_mask */);
500 
501  cpl_test_eq( cpl_error_get_code(), CPL_ERROR_NONE );
502  cpl_test( cpl_frameset_find(frames, tag) != NULL);
503 
504  uves_free_frameset(&frames);
505  uves_free_parameterlist(&parameters);
506  uves_free_image(&image);
507  uves_free_propertylist(&raw_header);
508  uves_free_propertylist(&product_header);
509  uves_free_string_const(&starttime);
510  return;
511 }
512 
513 
514 /*----------------------------------------------------------------------------*/
518 /*----------------------------------------------------------------------------*/
519 
520 int main(void)
521 {
522  /* Initialize CPL + UVES messaging */
523  cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
524  cpl_errorstate initial_errorstate = cpl_errorstate_get();
525 // cpl_msg_set_level(CPL_MSG_DEBUG);
526 
528  "Test of MIDAS array conversion");
529 
530 
532 
534  "Test of MIDAS array conversion failed");
535 
537 
539 
540  cleanup:
541  if (cpl_error_get_code() != CPL_ERROR_NONE)
542  {
543  cpl_errorstate_dump(initial_errorstate,CPL_FALSE,NULL);
544  }
545  return cpl_test_end(0);
546 }
547 
548 
int uves_polynomial_get_dimension(const polynomial *p)
Get the dimension of a polynomial.
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.
void uves_pfits_set_windownumber(uves_propertylist *plist, int window_number)
Write the window number.
Definition: uves_pfits.c:1944
#define check_nomsg(CMD)
Definition: uves_error.h:204
uves_propertylist * uves_propertylist_new(void)
Create an empty property list.
void uves_pfits_set_traceid(uves_propertylist *plist, int trace_id)
Write the trace ID.
Definition: uves_pfits.c:1888
double uves_polynomial_evaluate_2d(const polynomial *p, double x1, double x2)
Evaluate a 2d polynomial.
static void convert_midas_array(void)
this function tests reading MIDAS arrays
char * uves_initialize(cpl_frameset *frames, const cpl_parameterlist *parlist, const char *recipe_id, const char *short_descr)
Recipe initialization.
Definition: uves_utils.c:1910
#define uves_error_reset()
Definition: uves_error.h:215
void uves_pfits_set_lastabsorder(uves_propertylist *plist, int last_abs_order)
Write the last absolute order number.
Definition: uves_pfits.c:1750
static void test_save_frame(void)
test pipeline product creation
static void test_load_linetable(void)
line table input
static void test_save_image(void)
test image output
static void parse_midas_poly(void)
This function tests reading MIDAS polynomials.
Definition: uves_dfs-test.c:68
#define check(CMD,...)
Definition: uves_error.h:198
void uves_pfits_set_firstabsorder(uves_propertylist *plist, int first_abs_order)
Write the first absolute order number.
Definition: uves_pfits.c:1717
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.
int main(void)
Various tests of low-level library functions.
Definition: uves-test.c:393