UVES Pipeline Reference Manual  5.4.6
uves_response_utils.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-03-02 16:38:14 $
23  * $Revision: 1.19 $
24  * $Name: not supported by cvs2svn $
25  * $Log: not supported by cvs2svn $
26  * Revision 1.18 2011/12/08 14:02:17 amodigli
27  * Fix warnings with CPL6
28  *
29  * Revision 1.17 2010/09/24 09:32:07 amodigli
30  * put back QFITS dependency to fix problem spot by NRI on FIBER mode (with MIDAS calibs) data
31  *
32  * Revision 1.15 2007/08/21 13:08:26 jmlarsen
33  * Removed irplib_access module, largely deprecated by CPL-4
34  *
35  * Revision 1.14 2007/06/06 08:17:33 amodigli
36  * replace tab with 4 spaces
37  *
38  * Revision 1.13 2007/04/24 12:50:29 jmlarsen
39  * Replaced cpl_propertylist -> uves_propertylist which is much faster
40  *
41  * Revision 1.12 2007/04/10 07:09:48 jmlarsen
42  * Changed interface of uves_spline_hermite()
43  *
44  * Revision 1.11 2006/11/15 15:02:15 jmlarsen
45  * Implemented const safe workarounds for CPL functions
46  *
47  * Revision 1.9 2006/11/15 14:04:08 jmlarsen
48  * Removed non-const version of parameterlist_get_first/last/next which is
49  * already in CPL, added const-safe wrapper, unwrapper and deallocator functions
50  *
51  * Revision 1.8 2006/11/07 14:04:45 jmlarsen
52  * Fixed memory error by setting pointer to NULL after cpl_free
53  *
54  * Revision 1.7 2006/11/06 15:19:42 jmlarsen
55  * Removed unused include directives
56  *
57  * Revision 1.6 2006/11/03 15:01:21 jmlarsen
58  * Killed UVES 3d table module and use CPL 3d tables
59  *
60  * Revision 1.5 2006/08/17 13:56:53 jmlarsen
61  * Reduced max line length
62  *
63  * Revision 1.4 2006/08/17 09:20:43 jmlarsen
64  * Get reference object ID from flux table, not raw header
65  *
66  * Revision 1.3 2006/06/01 14:43:17 jmlarsen
67  * Added missing documentation
68  *
69  * Revision 1.2 2006/03/24 13:56:13 jmlarsen
70  * Changed ambigous text message
71  *
72  * Revision 1.1 2006/02/03 07:51:04 jmlarsen
73  * Moved recipe implementations to ./uves directory
74  *
75  * Revision 1.2 2005/12/19 16:17:55 jmlarsen
76  * Replaced bool -> int
77  *
78  * Revision 1.1 2005/11/11 13:18:54 jmlarsen
79  * Reorganized code, renamed source files
80  *
81  */
82 
83 #ifdef HAVE_CONFIG_H
84 # include <config.h>
85 #endif
86 
87 /*----------------------------------------------------------------------------*/
91 /*----------------------------------------------------------------------------*/
94 /*-----------------------------------------------------------------------------
95  Includes
96  -----------------------------------------------------------------------------*/
97 
98 #include <uves_response_utils.h>
99 
100 #include <uves_utils.h>
101 #include <uves_utils_wrappers.h>
102 #include <uves_pfits.h>
103 #include <uves_error.h>
104 #include <uves_msg.h>
105 
106 #include <cpl.h>
107 
108 #include <stdbool.h>
109 
110 /*-----------------------------------------------------------------------------
111  Defines
112  -----------------------------------------------------------------------------*/
113 /*-----------------------------------------------------------------------------
114  Functions prototypes
115  -----------------------------------------------------------------------------*/
116 
117 /*-----------------------------------------------------------------------------
118  Implementation
119  -----------------------------------------------------------------------------*/
120 
121 /*----------------------------------------------------------------------------*/
138 /*----------------------------------------------------------------------------*/
139 cpl_image *
140 uves_calculate_response(const cpl_image *spectrum, const uves_propertylist *spectrum_header,
141  const cpl_table *flux_table,
142  const uves_propertylist *raw_header,
143  double PACCURACY,
144  bool inverse,
145  char **ref_obj_id)
146 {
147  cpl_image *response = NULL; /* Result */
148  cpl_table *catalogue_flux = NULL;
149  int nx, norders;
150 
151  nx = cpl_image_get_size_x(spectrum);
152  norders = cpl_image_get_size_y(spectrum);
153 
154  response = cpl_image_new(nx, norders, CPL_TYPE_DOUBLE);
155 
156  check( catalogue_flux = uves_align(raw_header, flux_table, PACCURACY, ref_obj_id),
157  "Cannot read catalogue flux");
158 
159  /* Correct for atmospheric extinction, and calculate response */
160  {
161  double dlambda;
162  int order;
163 
164  check( dlambda = uves_pfits_get_cdelt1(spectrum_header),
165  "Error reading bin width from header");
166 
167  for (order = 1; order <= norders; order++)
168  {
169  double lambda_start;
170  int x;
171 
172  /* If spectrum was already merged, then read cdelt1,
173  * otherwise read wstart for each order
174  */
175  if (norders == 1)
176  {
177  check( lambda_start = uves_pfits_get_crval1(spectrum_header),
178  "Error reading start wavelength from header");
179  }
180  else
181  {
182  check( lambda_start = uves_pfits_get_wstart(spectrum_header, order),
183  "Error reading start wavelength from header");
184  }
185 
186  for (x = 1; x <= nx; x++)
187  {
188  int pis_rejected;
189  double lambda;
190  double flux, std_flux, resp;
191  int istart = 0;
192 
193  lambda = lambda_start + (x-1) * dlambda;
194 
195  check( flux = cpl_image_get(spectrum, x, order, &pis_rejected),
196  "Error reading flux");
197 
198  if (!pis_rejected)
199  {
200  /* Get interpolated catalogue flux */
201  check( std_flux = uves_spline_hermite_table(
202  lambda, catalogue_flux,
203  "LAMBDA", "F_LAMBDA", &istart),
204  "Error interpolating catalogue flux");
205 
206  if (inverse)
207  {
208  resp = (flux == 0) ? 0 : std_flux / flux;
209  }
210  else
211  {
212  resp = (std_flux == 0) ? 0 : flux / std_flux;
213  }
214 
215  check( cpl_image_set(response, x, order, resp),
216  "Error writing response image");
217  }
218  else
219  {
220  cpl_image_reject(response, x, order);
221  }
222  }
223  }
224  }
225 
226  cleanup:
227  uves_free_table(&catalogue_flux);
228  if (cpl_error_get_code() != CPL_ERROR_NONE)
229  {
230  uves_free_image(&response);
231  }
232  return response;
233 }
234 
235 
236 /*----------------------------------------------------------------------------*/
255 /*----------------------------------------------------------------------------*/
256 cpl_table *
257 uves_align(const uves_propertylist *object_header, const cpl_table *flux_table,
258  double accuracy,
259  char **ref_name_dynamic)
260 {
261  double obj_ra, obj_dec; /* Object position in degrees */
262  int nident = 0; /* Number of identifications */
263  int match_row = 0; /* Catalogue row number of match */
264  double min_dist = 0; /* Accuracy of match */
265  double std_ra = 0, std_dec = 0; /* Catalogue position */
266  const char *ref_type = NULL;
267 
268  cpl_table *result = NULL;
269 
270  int i;
271 
272  assure_nomsg( ref_name_dynamic != NULL, CPL_ERROR_NULL_INPUT );
273  *ref_name_dynamic = NULL;
274 
275  check( obj_ra = uves_pfits_get_ra (object_header), "Could not read right ascension");
276  check( obj_dec = uves_pfits_get_dec(object_header), "Could not read declination");
277 
278  uves_msg("Object RA, DEC = (%e, %e)", obj_ra, obj_dec);
279 
280  nident = 0;
281  for (i = 0; i < cpl_table_get_nrow(flux_table); i++)
282  {
283  double ref_ra, ref_dec;
284  double dist;
285 
286  check( ref_ra = cpl_table_get_double(flux_table, "RA_DEG", i, NULL),
287  "Could not read catalogue star right ascension");
288  check( ref_dec = cpl_table_get_double(flux_table, "DEC_DEG", i, NULL),
289  "Could not read catalogue star declination");
290 
291  /* Calculate angular separation in arcsecs
292  * cos(sep) = sin(d1)sin(d2) + cos(d1)cos(d2)cos(ra1-ra2)
293  *
294  * All input angles are in degrees.
295  */
296 
297  dist =
298  SIN_DEG(obj_dec)*SIN_DEG(ref_dec) +
299  COS_DEG(obj_dec)*COS_DEG(ref_dec)*COS_DEG(obj_ra - ref_ra);
300 
301  dist = uves_max_double(dist, -1);
302  dist = uves_min_double(dist, 1);
303 
304  dist = ACOS_DEG(dist) * 3600;
305 
306  uves_msg_debug("Angular separation = %f arcseconds", dist);
307 
308 
309  /* Keep track of best match also if it is not within the pointing accuracy */
310  if (i == 0 || dist < min_dist)
311  {
312  min_dist = dist;
313  std_ra = ref_ra;
314  std_dec = ref_dec;
315  }
316 
317 
318  /* Does it match? */
319  if (dist < accuracy)
320  {
321  nident += 1;
322  match_row = i;
323  min_dist = dist;
324  std_ra = ref_ra;
325  std_dec = ref_dec;
326  }
327  }
328 
329  assure( nident >= 1, CPL_ERROR_INCOMPATIBLE_INPUT,
330  "No catalogue object within %f arcsecs. "
331  "Nearest object is %f arcsecs away at (RA, DEC) = (%f, %f)",
332  accuracy, min_dist, std_ra, std_dec);
333 
334  assure( nident <= 1, CPL_ERROR_INCOMPATIBLE_INPUT,
335  "%d matching catalogue objects found. Confused. "
336  "Decrease pointing accuracy (currently %f arcsecs) to get fewer matches",
337  nident, accuracy);
338 
339  check( *ref_name_dynamic = cpl_strdup(
340  cpl_table_get_string(flux_table, "OBJECT", match_row)),
341  "Could not read reference object name");
342 
343  check( ref_type = cpl_table_get_string(flux_table, "TYPE", match_row),
344  "Could not read reference object type");
345 
346  uves_msg("Object ID is '%s', type = '%s'. Residual between header/catalogue = %f arcsecs",
347  *ref_name_dynamic, ref_type, min_dist);
348 
349 
350  /* Create (2d) flux table from catalogue table row number 'match_row' */
351  {
352  const char *columns[3] = {"LAMBDA", "BIN_WIDTH", "F_LAMBDA"};
353  int ndata; /* Number of elements in column */
354 
355  check( ndata = cpl_table_get_int(flux_table, "NDATA", match_row, NULL),
356  "Error reading length of flux array");
357 
358  result = cpl_table_new(ndata);
359 
360  for(i = 0; i < 3; i++)
361  {
362  const cpl_array *data;
363  int indx;
364 
365  cpl_table_new_column(result, columns[i], CPL_TYPE_DOUBLE);
366 
367  data = cpl_table_get_array(flux_table, columns[i], match_row);
368 
369  /* Only the 'ndata' first elements of the array are used,
370  and the array may be longer than this */
371  uves_msg_debug("3d table array size = %" CPL_SIZE_FORMAT ", ndata = %d",
372  cpl_array_get_size(data), ndata);
373 
374  assure( cpl_array_get_size(data) >= ndata,
375  CPL_ERROR_ILLEGAL_INPUT,
376  "Flux table row %d: column '%s' depth (%" CPL_SIZE_FORMAT ") "
377  "is less than NDATA (%d)",
378  match_row, columns[i], cpl_array_get_size(data), ndata);
379 
380  for (indx = 0; indx < ndata; indx++)
381  {
382  /* 3d columns are float */
383  cpl_table_set_double(result, columns[i], indx,
384  cpl_array_get_float(data, indx, NULL));
385  }
386  }
387  }
388 
389  cleanup:
390  if (cpl_error_get_code() != CPL_ERROR_NONE)
391  {
392  uves_free_table(&result);
393  if (ref_name_dynamic != NULL)
394  {
395  cpl_free(*ref_name_dynamic);
396  *ref_name_dynamic = NULL;
397  }
398  }
399 
400  return result;
401 }
402 
cpl_table * uves_align(const uves_propertylist *object_header, const cpl_table *flux_table, double accuracy, char **ref_name_dynamic)
Match a star against a catalogue of stars.
double uves_pfits_get_wstart(const uves_propertylist *plist, int order)
Read the wstart keyword.
Definition: uves_pfits.c:3004
double uves_spline_hermite_table(double xp, const cpl_table *t, const char *column_x, const char *column_y, int *istart)
Spline interpolation based on Hermite polynomials.
Definition: uves_utils.c:3683
#define uves_msg(...)
Print a message on 'info' or 'debug' level.
Definition: uves_msg.h:119
double uves_pfits_get_ra(const uves_propertylist *plist)
Find out the right ascension.
Definition: uves_pfits.c:1140
double uves_pfits_get_dec(const uves_propertylist *plist)
Find out the declination.
Definition: uves_pfits.c:1158
#define uves_msg_debug(...)
Print a debug message.
Definition: uves_msg.h:97
double uves_pfits_get_cdelt1(const uves_propertylist *plist)
Find out the cdelt1.
Definition: uves_pfits.c:2465
double uves_pfits_get_crval1(const uves_propertylist *plist)
Find out the crval1.
Definition: uves_pfits.c:2393
#define assure_nomsg(BOOL, CODE)
Definition: uves_error.h:177
cpl_image * uves_calculate_response(const cpl_image *spectrum, const uves_propertylist *spectrum_header, const cpl_table *flux_table, const uves_propertylist *raw_header, double PACCURACY, bool inverse, char **ref_obj_id)
Calculate response.
#define check(CMD,...)
Definition: uves_error.h:198