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