NACO Pipeline Reference Manual  4.4.0
irplib_pfits.c
1 /* $Id: irplib_pfits.c,v 1.16 2008-04-11 22:06:03 llundin Exp $
2  *
3  * This file is part of the IRPLIB Package
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: llundin $
23  * $Date: 2008-04-11 22:06:03 $
24  * $Revision: 1.16 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <string.h>
37 #include <sys/types.h>
38 #include <regex.h>
39 
40 #include <assert.h>
41 #include <cpl.h>
42 
43 #include "irplib_utils.h"
44 #include "irplib_pfits.h"
45 
46 /*-----------------------------------------------------------------------------
47  Private funcions
48  -----------------------------------------------------------------------------*/
49 
50 static cpl_error_code irplib_dfs_check_frame_tag(const cpl_frame *,
51  const cpl_propertylist *,
52  const char* (*)
53  (const char *,
54  const char *,
55  const char *));
56 
57 
58 /*----------------------------------------------------------------------------*/
63 /*----------------------------------------------------------------------------*/
64 
67 /*-----------------------------------------------------------------------------
68  Function codes
69  -----------------------------------------------------------------------------*/
70 
71 
72 /*----------------------------------------------------------------------------*/
78 /*----------------------------------------------------------------------------*/
79 const char * irplib_pfits_get_dpr_catg(const cpl_propertylist * self)
80 {
81  return irplib_pfits_get_string(self, "ESO DPR CATG");
82 }
83 
84 /*----------------------------------------------------------------------------*/
90 /*----------------------------------------------------------------------------*/
91 const char * irplib_pfits_get_dpr_tech(const cpl_propertylist * self)
92 {
93  return irplib_pfits_get_string(self, "ESO DPR TECH");
94 }
95 
96 /*----------------------------------------------------------------------------*/
102 /*----------------------------------------------------------------------------*/
103 const char * irplib_pfits_get_dpr_type(const cpl_propertylist * self)
104 {
105  return irplib_pfits_get_string(self, "ESO DPR TYPE");
106 }
107 
108 
109 /*----------------------------------------------------------------------------*/
120 /*----------------------------------------------------------------------------*/
121 cpl_boolean irplib_pfits_get_bool_macro(const cpl_propertylist * self,
122  const char * key, const char * function,
123  const char * file, unsigned line)
124 {
125  cpl_boolean value;
126  cpl_errorstate prestate = cpl_errorstate_get();
127 
128  value = cpl_propertylist_get_bool(self, key);
129 
130  if (cpl_errorstate_is_equal(prestate)) {
131  cpl_msg_debug(function, "FITS card '%s' [bool]: %c", key,
132  value ? 'T' : 'F');
133  } else {
134  /* Set the error location to that of the caller */
135  (void)cpl_error_set_message_macro(function, cpl_error_get_code(), file,
136  line, "Missing FITS card "
137  " [bool]: '%s' ", key);
138  }
139 
140  return value;
141 }
142 
143 /*----------------------------------------------------------------------------*/
154 /*----------------------------------------------------------------------------*/
155 double irplib_pfits_get_double_macro(const cpl_propertylist * self,
156  const char * key,
157  const char * function,
158  const char * file,
159  unsigned line)
160 {
161  double value;
162  cpl_errorstate prestate = cpl_errorstate_get();
163 
164  value = cpl_propertylist_get_double(self, key);
165 
166  if (cpl_errorstate_is_equal(prestate)) {
167  cpl_msg_debug(function, "FITS card '%s' [double]: %g", key, value);
168  } else {
169  /* Set the error location to that of the caller */
170  (void)cpl_error_set_message_macro(function, cpl_error_get_code(), file,
171  line, "Missing FITS card "
172  " [double]: '%s' ", key);
173  }
174 
175  return value;
176 }
177 
178 
179 
180 /*----------------------------------------------------------------------------*/
191 /*----------------------------------------------------------------------------*/
192 int irplib_pfits_get_int_macro(const cpl_propertylist * self,
193  const char * key, const char * function,
194  const char * file, unsigned line)
195 {
196  int value;
197  cpl_errorstate prestate = cpl_errorstate_get();
198 
199  value = cpl_propertylist_get_int(self, key);
200 
201  if (cpl_errorstate_is_equal(prestate)) {
202  cpl_msg_debug(function, "FITS card '%s' [int]: %d", key, value);
203  } else {
204  /* Set the error location to that of the caller */
205  (void)cpl_error_set_message_macro(function, cpl_error_get_code(), file,
206  line, "Missing FITS card "
207  " [int]: '%s' ", key);
208  }
209 
210  return value;
211 }
212 
213 
214 /*----------------------------------------------------------------------------*/
225 /*----------------------------------------------------------------------------*/
226 const char * irplib_pfits_get_string_macro(const cpl_propertylist * self,
227  const char * key,
228  const char * function,
229  const char * file,
230  unsigned line)
231 {
232  const char * value;
233  cpl_errorstate prestate = cpl_errorstate_get();
234 
235  value = cpl_propertylist_get_string(self, key);
236 
237  if (cpl_errorstate_is_equal(prestate)) {
238  cpl_msg_debug(function, "FITS card '%s' [string]: %s", key, value);
239  } else {
240  /* Set the error location to that of the caller */
241  (void)cpl_error_set_message_macro(function, cpl_error_get_code(), file,
242  line, "Missing FITS card "
243  " [string]: '%s' ", key);
244  }
245 
246  return value;
247 }
248 
249 
250 
251 
252 /*----------------------------------------------------------------------------*/
259 /*----------------------------------------------------------------------------*/
260 cpl_error_code irplib_dfs_check_framelist_tag(const irplib_framelist * self,
261  const char* (*pfind)(const char *,
262  const char *,
263  const char *))
264 {
265 
266  int i;
267 
268  if (cpl_error_get_code()) return cpl_error_get_code();
269 
270  cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
271  cpl_ensure_code(pfind != NULL, CPL_ERROR_NULL_INPUT);
272 
273  for (i = 0; i < irplib_framelist_get_size(self); i++) {
274  const cpl_frame * frame = irplib_framelist_get_const(self, i);
275  const cpl_propertylist * plist
277 
278  cpl_ensure_code(frame != NULL, cpl_error_get_code());
279  cpl_ensure_code(plist != NULL, cpl_error_get_code());
280 
281  cpl_ensure_code(!irplib_dfs_check_frame_tag(frame, plist, pfind),
282  cpl_error_get_code());
283  }
284 
285  return cpl_error_get_code();
286 
287 }
288 
289 
290 /*----------------------------------------------------------------------------*/
301 /*----------------------------------------------------------------------------*/
302 int irplib_dfs_find_words(const char * words, const char * format, ...)
303 {
304 
305  regex_t re;
306  va_list ap;
307  int error, status;
308 
309 
310  if (cpl_error_get_code()) return -1;
311 
312  cpl_ensure(words != NULL, CPL_ERROR_NULL_INPUT, -2);
313  cpl_ensure(format != NULL, CPL_ERROR_NULL_INPUT, -3);
314 
315  /* format must consist of space separated %s */
316  error = regcomp(&re, "^ *%s( +%s)* *$", REG_EXTENDED | REG_NOSUB);
317 
318  /* Should really be assert() */
319  cpl_ensure(!error, CPL_ERROR_ILLEGAL_INPUT, -4);
320 
321  status = regexec(&re, format, (size_t)0, NULL, 0);
322 
323  regfree(&re);
324 
325  if (status != 0) {
326  cpl_msg_error(cpl_func, "Regexp counter must consist of space-separated"
327  " %%s, not: %s", format);
328  cpl_ensure(0, CPL_ERROR_ILLEGAL_INPUT, -5);
329  }
330 
331  va_start(ap, format);
332 
333  /* Iterate through arguments, by searching for the '%' */
334  for (; format != NULL; format = strchr(++format, '%')) {
335 
336  const char * regexp = va_arg(ap, const char *);
337 
338  if (regexp == NULL) {
339  va_end(ap);
340  cpl_ensure(0, CPL_ERROR_ILLEGAL_INPUT, -6);
341  }
342 
343  error = regcomp(&re, regexp, REG_EXTENDED | REG_NOSUB);
344 
345  if (error) {
346  va_end(ap);
347  cpl_ensure(0, CPL_ERROR_ILLEGAL_INPUT, -7);
348  }
349 
350  status = regexec(&re, words, (size_t)0, NULL, 0);
351 
352  regfree(&re);
353 
354  if (status != 0) break; /* Not matched */
355 
356  }
357 
358  va_end(ap);
359 
360  return format == NULL ? 0 : 1;
361 
362 }
363 
364 /*----------------------------------------------------------------------------*/
372 /*----------------------------------------------------------------------------*/
373 cpl_error_code irplib_pfits_set_airmass(cpl_propertylist * self,
374  const irplib_framelist * rawframes)
375 {
376 
377  char * newcomment = NULL;
378  const int nframes = irplib_framelist_get_size(rawframes);
379  int iframe;
380  int nmass = 0;
381  double astart0 = -1.0;
382  double aend0 = -1.0;
383  double airmass = 0.0;
384  cpl_errorstate prestate = cpl_errorstate_get();
385 
386  skip_if(0);
387  skip_if(self == NULL);
388 
389  for (iframe = 0; iframe < nframes; iframe++) {
390  const cpl_propertylist * plist
391  = irplib_framelist_get_propertylist_const(rawframes, iframe);
392  double astart = DBL_MAX; /* Avoid (false) uninit warning */
393  double aend = DBL_MAX; /* Avoid (false) uninit warning */
394  double airmi;
395 
396  if (!cpl_errorstate_is_equal(prestate)) {
397  irplib_error_recover(prestate, "No propertylist found for frame %d:",
398  iframe);
399  continue;
400  }
401 
402  if (iframe == 0) {
403  astart = irplib_pfits_get_double(plist, "ESO TEL AIRM START");
404  if (cpl_errorstate_is_equal(prestate)) {
405  astart0 = astart;
406  aend = irplib_pfits_get_double(plist, "ESO TEL AIRM END");
407  }
408  } else {
409  aend = irplib_pfits_get_double(plist, "ESO TEL AIRM END");
410  if (cpl_errorstate_is_equal(prestate)) {
411  if (iframe == nframes - 1) aend0 = aend;
412  astart = irplib_pfits_get_double(plist, "ESO TEL AIRM START");
413  }
414  }
415 
416  if (cpl_errorstate_is_equal(prestate)) {
417  airmi = 0.5 * (astart + aend);
418  } else {
419  const char * filename = cpl_frame_get_filename(
420  irplib_framelist_get_const(rawframes, iframe));
421  irplib_error_recover(prestate, "Could not get FITS key from %s",
422  filename);
423 
424  airmi = irplib_pfits_get_double(plist, "AIRMASS");
425 
426  if (!cpl_errorstate_is_equal(prestate)) {
427  irplib_error_recover(prestate, "Could not get FITS key from %s",
428  filename);
429  continue;
430  }
431  }
432 
433  airmass += airmi;
434  nmass++;
435  }
436 
437  bug_if(0);
438 
439  if (nmass == 0 && astart0 > 0.0 && aend0 > 0.0) {
440  airmass = 0.5 * (astart0 + aend0);
441  nmass = 1;
442  }
443  if (nmass > 0) {
444  const char * key = "AIRMASS";
445  const char * comment = cpl_propertylist_get_comment(self, key);
446 
447  irplib_error_recover(prestate, "Could not get FITS key:");
448 
449  airmass /= (double)nmass;
450 
451  bug_if(cpl_propertylist_update_double(self, key, airmass));
452 
453  if (comment == NULL) {
454  bug_if(cpl_propertylist_set_comment(self, key, "Averaged air mass "
455  "(Recalculated)"));
456  } else {
457  newcomment = cpl_sprintf("%s (Recalculated)",
458  comment);
459  bug_if(cpl_propertylist_set_comment(self, key, newcomment));
460  }
461 
462  }
463 
464  end_skip;
465 
466  cpl_free(newcomment);
467 
468  return cpl_error_get_code();
469 
470 }
471 
475 /*----------------------------------------------------------------------------*/
483 /*----------------------------------------------------------------------------*/
484 static cpl_error_code irplib_dfs_check_frame_tag(const cpl_frame * self,
485  const cpl_propertylist * plist,
486  const char* (*pfind)
487  (const char *,
488  const char *,
489  const char *))
490 {
491 
492  const char * file;
493  const char * tag;
494  const char * docatg;
495  const char * catg;
496  const char * type;
497  const char * tech;
498  cpl_errorstate prestate = cpl_errorstate_get();
499 
500 
501  cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
502 
503  file = cpl_frame_get_filename(self);
504 
505  cpl_ensure_code(file != NULL, cpl_error_get_code());
506 
507  tag = cpl_frame_get_tag(self);
508 
509  cpl_ensure_code(tag != NULL, cpl_error_get_code());
510 
511  catg = irplib_pfits_get_dpr_catg(plist);
512  type = irplib_pfits_get_dpr_type(plist);
513  tech = irplib_pfits_get_dpr_tech(plist);
514 
515  if (!cpl_errorstate_is_equal(prestate)) {
516  if (cpl_msg_get_level() <= CPL_MSG_DEBUG) {
517  cpl_msg_warning(cpl_func, "File %s has missing or incomplete DPR "
518  "triplet", file);
519  cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
520  }
521  cpl_errorstate_set(prestate);
522  } else {
523 
524  cpl_ensure_code(pfind != NULL, CPL_ERROR_NULL_INPUT);
525 
526  docatg = (*pfind)(catg, type, tech);
527 
528  if (docatg == NULL) {
529  if (cpl_msg_get_level() <= CPL_MSG_DEBUG)
530  cpl_msg_warning(cpl_func, "File %s has tag %s but unknown DPR "
531  "triplet: (CATG;TYPE;TECH)=(%s;%s;%s)", file, tag,
532  catg, type, tech);
533  } else if (strcmp(tag, docatg) != 0) {
534  if (cpl_msg_get_level() <= CPL_MSG_DEBUG)
535  cpl_msg_warning(cpl_func, "File %s has tag %s but DPR triplet of "
536  "%s: (CATG;TYPE;TECH)=(%s;%s;%s)", file, tag,
537  docatg, catg, type, tech);
538  }
539  }
540 
541  return CPL_ERROR_NONE;
542 
543 }
544 
545 
const char * irplib_pfits_get_dpr_catg(const cpl_propertylist *self)
The data category.
Definition: irplib_pfits.c:79
const char * irplib_pfits_get_string_macro(const cpl_propertylist *self, const char *key, const char *function, const char *file, unsigned line)
Get the value of a property of type string.
Definition: irplib_pfits.c:226
const char * irplib_pfits_get_dpr_type(const cpl_propertylist *self)
The data type.
Definition: irplib_pfits.c:103
cpl_boolean irplib_pfits_get_bool_macro(const cpl_propertylist *self, const char *key, const char *function, const char *file, unsigned line)
Get the value of a property of type boolean.
Definition: irplib_pfits.c:121
cpl_error_code irplib_pfits_set_airmass(cpl_propertylist *self, const irplib_framelist *rawframes)
Update/Set the AIRMASS property.
Definition: irplib_pfits.c:373
const char * irplib_pfits_get_dpr_tech(const cpl_propertylist *self)
The data technique.
Definition: irplib_pfits.c:91
const cpl_propertylist * irplib_framelist_get_propertylist_const(const irplib_framelist *self, int pos)
Get the propertylist of the specified frame in the framelist.
double irplib_pfits_get_double_macro(const cpl_propertylist *self, const char *key, const char *function, const char *file, unsigned line)
Get the value of a property of type double.
Definition: irplib_pfits.c:155
int irplib_pfits_get_int_macro(const cpl_propertylist *self, const char *key, const char *function, const char *file, unsigned line)
Get the value of a property of type int.
Definition: irplib_pfits.c:192
cpl_error_code irplib_dfs_check_framelist_tag(const irplib_framelist *self, const char *(*pfind)(const char *, const char *, const char *))
Check the tags in a frameset (group raw only)
Definition: irplib_pfits.c:260
const cpl_frame * irplib_framelist_get_const(const irplib_framelist *self, int pos)
Get the specified frame from the framelist.
int irplib_dfs_find_words(const char *words, const char *format,...)
Match a string with word(s) against a list of 1-word-regexps.
Definition: irplib_pfits.c:302
int irplib_framelist_get_size(const irplib_framelist *self)
Get the size of a framelist.