00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <string.h>
00037 #include <sys/types.h>
00038 #include <regex.h>
00039
00040 #include <assert.h>
00041 #include <cpl.h>
00042
00043 #include "irplib_utils.h"
00044 #include "irplib_tools.h"
00045 #include "irplib_pfits.h"
00046
00047
00048
00049
00050
00051 static cpl_error_code irplib_dfs_check_frame_tag(const cpl_frame *,
00052 const cpl_propertylist *,
00053 const char* (*)
00054 (const char *,
00055 const char *,
00056 const char *));
00057
00058
00059
00064
00065
00068
00069
00070
00071
00072
00073
00079
00080 const char * irplib_pfits_get_dpr_catg(const cpl_propertylist * self)
00081 {
00082 return irplib_pfits_get_string(self, "ESO DPR CATG");
00083 }
00084
00085
00091
00092 const char * irplib_pfits_get_dpr_tech(const cpl_propertylist * self)
00093 {
00094 return irplib_pfits_get_string(self, "ESO DPR TECH");
00095 }
00096
00097
00103
00104 const char * irplib_pfits_get_dpr_type(const cpl_propertylist * self)
00105 {
00106 return irplib_pfits_get_string(self, "ESO DPR TYPE");
00107 }
00108
00109
00120
00121 double irplib_pfits_get_double_macro(const cpl_propertylist * self,
00122 const char * key,
00123 const char * function,
00124 const char * file,
00125 unsigned line)
00126 {
00127 double value;
00128 cpl_errorstate prestate = cpl_errorstate_get();
00129
00130 value = cpl_propertylist_get_double(self, key);
00131
00132 if (cpl_errorstate_is_equal(prestate)) {
00133 cpl_msg_debug(function, "FITS card '%s' [double]: %g", key, value);
00134 } else {
00135
00136 (void)cpl_error_set_message_macro(function, cpl_error_get_code(), file,
00137 line, "Missing FITS card "
00138 " [double]: '%s' ", key);
00139 }
00140
00141 return value;
00142 }
00143
00144
00145
00146
00157
00158 int irplib_pfits_get_int_macro(const cpl_propertylist * self,
00159 const char * key, const char * function,
00160 const char * file, unsigned line)
00161 {
00162 int value;
00163 cpl_errorstate prestate = cpl_errorstate_get();
00164
00165 value = cpl_propertylist_get_int(self, key);
00166
00167 if (cpl_errorstate_is_equal(prestate)) {
00168 cpl_msg_debug(function, "FITS card '%s' [int]: %d", key, value);
00169 } else {
00170
00171 (void)cpl_error_set_message_macro(function, cpl_error_get_code(), file,
00172 line, "Missing FITS card "
00173 " [int]: '%s' ", key);
00174 }
00175
00176 return value;
00177 }
00178
00179
00180
00191
00192 const char * irplib_pfits_get_string_macro(const cpl_propertylist * self,
00193 const char * key,
00194 const char * function,
00195 const char * file,
00196 unsigned line)
00197 {
00198 const char * value;
00199 cpl_errorstate prestate = cpl_errorstate_get();
00200
00201 value = cpl_propertylist_get_string(self, key);
00202
00203 if (cpl_errorstate_is_equal(prestate)) {
00204 cpl_msg_debug(function, "FITS card '%s' [string]: %s", key, value);
00205 } else {
00206
00207 (void)cpl_error_set_message_macro(function, cpl_error_get_code(), file,
00208 line, "Missing FITS card "
00209 " [string]: '%s' ", key);
00210 }
00211
00212 return value;
00213 }
00214
00215
00216
00217
00218
00225
00226 cpl_error_code irplib_dfs_check_framelist_tag(const irplib_framelist * self,
00227 const char* (*pfind)(const char *,
00228 const char *,
00229 const char *))
00230 {
00231
00232 int i;
00233
00234 if (cpl_error_get_code()) return cpl_error_get_code();
00235
00236 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
00237 cpl_ensure_code(pfind != NULL, CPL_ERROR_NULL_INPUT);
00238
00239 for (i = 0; i < irplib_framelist_get_size(self); i++) {
00240 const cpl_frame * frame = irplib_framelist_get_const(self, i);
00241 const cpl_propertylist * plist
00242 = irplib_framelist_get_propertylist_const(self, i);
00243
00244 cpl_ensure_code(frame != NULL, cpl_error_get_code());
00245 cpl_ensure_code(plist != NULL, cpl_error_get_code());
00246
00247 cpl_ensure_code(!irplib_dfs_check_frame_tag(frame, plist, pfind),
00248 cpl_error_get_code());
00249 }
00250
00251 return cpl_error_get_code();
00252
00253 }
00254
00255
00256
00267
00268 int irplib_dfs_find_words(const char * words, const char * format, ...)
00269 {
00270
00271 regex_t re;
00272 va_list ap;
00273 int error, status;
00274
00275
00276 if (cpl_error_get_code()) return -1;
00277
00278 cpl_ensure(words != NULL, CPL_ERROR_NULL_INPUT, -2);
00279 cpl_ensure(format != NULL, CPL_ERROR_NULL_INPUT, -3);
00280
00281
00282 error = regcomp(&re, "^ *%s( +%s)* *$", REG_EXTENDED | REG_NOSUB);
00283
00284
00285 cpl_ensure(!error, CPL_ERROR_ILLEGAL_INPUT, -4);
00286
00287 status = regexec(&re, format, (size_t)0, NULL, 0);
00288
00289 regfree(&re);
00290
00291 if (status != 0) {
00292 cpl_msg_error(cpl_func, "Regexp counter must consist of space-separated"
00293 " %%s, not: %s", format);
00294 cpl_ensure(0, CPL_ERROR_ILLEGAL_INPUT, -5);
00295 }
00296
00297 va_start(ap, format);
00298
00299
00300 for (; format != NULL; format = strchr(++format, '%')) {
00301
00302 const char * regexp = va_arg(ap, const char *);
00303
00304 if (regexp == NULL) {
00305 va_end(ap);
00306 cpl_ensure(0, CPL_ERROR_ILLEGAL_INPUT, -6);
00307 }
00308
00309 error = regcomp(&re, regexp, REG_EXTENDED | REG_NOSUB);
00310
00311 if (error) {
00312 va_end(ap);
00313 cpl_ensure(0, CPL_ERROR_ILLEGAL_INPUT, -7);
00314 }
00315
00316 status = regexec(&re, words, (size_t)0, NULL, 0);
00317
00318 regfree(&re);
00319
00320 if (status != 0) break;
00321
00322 }
00323
00324 va_end(ap);
00325
00326 return format == NULL ? 0 : 1;
00327
00328 }
00329
00330
00338
00339 cpl_error_code irplib_pfits_set_airmass(cpl_propertylist * self,
00340 const irplib_framelist * rawframes)
00341 {
00342
00343 char * newcomment = NULL;
00344 const int nframes = irplib_framelist_get_size(rawframes);
00345 int iframe;
00346 int nmass = 0;
00347 double astart0 = -1.0;
00348 double aend0 = -1.0;
00349 double airmass = 0.0;
00350 cpl_errorstate prestate = cpl_errorstate_get();
00351
00352 skip_if(0);
00353 skip_if(self == NULL);
00354
00355 for (iframe = 0; iframe < nframes; iframe++) {
00356 const cpl_propertylist * plist
00357 = irplib_framelist_get_propertylist_const(rawframes, iframe);
00358 double astart = DBL_MAX;
00359 double aend = DBL_MAX;
00360 double airmi;
00361
00362 if (!cpl_errorstate_is_equal(prestate)) {
00363 irplib_error_recover(prestate, "No propertylist found for frame %d:",
00364 iframe);
00365 continue;
00366 }
00367
00368 if (iframe == 0) {
00369 astart = irplib_pfits_get_double(plist, "ESO TEL AIRM START");
00370 if (cpl_errorstate_is_equal(prestate)) {
00371 astart0 = astart;
00372 aend = irplib_pfits_get_double(plist, "ESO TEL AIRM END");
00373 }
00374 } else {
00375 aend = irplib_pfits_get_double(plist, "ESO TEL AIRM END");
00376 if (cpl_errorstate_is_equal(prestate)) {
00377 if (iframe == nframes - 1) aend0 = aend;
00378 astart = irplib_pfits_get_double(plist, "ESO TEL AIRM START");
00379 }
00380 }
00381
00382 if (cpl_errorstate_is_equal(prestate)) {
00383 airmi = 0.5 * (astart + aend);
00384 } else {
00385 const char * filename = cpl_frame_get_filename(
00386 irplib_framelist_get_const(rawframes, iframe));
00387 irplib_error_recover(prestate, "Could not get FITS key from %s",
00388 filename);
00389
00390 airmi = irplib_pfits_get_double(plist, "AIRMASS");
00391
00392 if (!cpl_errorstate_is_equal(prestate)) {
00393 irplib_error_recover(prestate, "Could not get FITS key from %s",
00394 filename);
00395 continue;
00396 }
00397 }
00398
00399 airmass += airmi;
00400 nmass++;
00401 }
00402
00403 bug_if(0);
00404
00405 if (nmass == 0 && astart0 > 0.0 && aend0 > 0.0) {
00406 airmass = 0.5 * (astart0 + aend0);
00407 nmass = 1;
00408 }
00409 if (nmass > 0) {
00410 const char * key = "AIRMASS";
00411 const char * comment = cpl_propertylist_get_comment(self, key);
00412
00413 irplib_error_recover(prestate, "Could not get FITS key:");
00414
00415 airmass /= (double)nmass;
00416
00417 bug_if(cpl_propertylist_update_double(self, key, airmass));
00418
00419 if (comment == NULL) {
00420 bug_if(cpl_propertylist_set_comment(self, key, "Averaged air mass "
00421 "(Recalculated)"));
00422 } else {
00423 newcomment = cpl_sprintf("%s (Recalculated)",
00424 comment);
00425 bug_if(cpl_propertylist_set_comment(self, key, newcomment));
00426 }
00427
00428 }
00429
00430 end_skip;
00431
00432 cpl_free(newcomment);
00433
00434 return cpl_error_get_code();
00435
00436 }
00437
00441
00449
00450 static cpl_error_code irplib_dfs_check_frame_tag(const cpl_frame * self,
00451 const cpl_propertylist * plist,
00452 const char* (*pfind)
00453 (const char *,
00454 const char *,
00455 const char *))
00456 {
00457
00458 const char * file;
00459 const char * tag;
00460 const char * docatg;
00461 const char * catg;
00462 const char * type;
00463 const char * tech;
00464 cpl_errorstate prestate = cpl_errorstate_get();
00465
00466
00467 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
00468
00469 file = cpl_frame_get_filename(self);
00470
00471 cpl_ensure_code(file != NULL, cpl_error_get_code());
00472
00473 tag = cpl_frame_get_tag(self);
00474
00475 cpl_ensure_code(tag != NULL, cpl_error_get_code());
00476
00477 catg = irplib_pfits_get_dpr_catg(plist);
00478 type = irplib_pfits_get_dpr_type(plist);
00479 tech = irplib_pfits_get_dpr_tech(plist);
00480
00481 if (!cpl_errorstate_is_equal(prestate)) {
00482 if (cpl_msg_get_level() <= CPL_MSG_DEBUG) {
00483 cpl_msg_warning(cpl_func, "File %s has missing or incomplete DPR "
00484 "triplet", file);
00485 cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
00486 }
00487 cpl_errorstate_set(prestate);
00488 } else {
00489
00490 cpl_ensure_code(pfind != NULL, CPL_ERROR_NULL_INPUT);
00491
00492 docatg = (*pfind)(catg, type, tech);
00493
00494 if (docatg == NULL) {
00495 if (cpl_msg_get_level() <= CPL_MSG_DEBUG)
00496 cpl_msg_warning(cpl_func, "File %s has tag %s but unknown DPR "
00497 "triplet: (CATG;TYPE;TECH)=(%s;%s;%s)", file, tag,
00498 catg, type, tech);
00499 } else if (strcmp(tag, docatg) != 0) {
00500 if (cpl_msg_get_level() <= CPL_MSG_DEBUG)
00501 cpl_msg_warning(cpl_func, "File %s has tag %s but DPR triplet of "
00502 "%s: (CATG;TYPE;TECH)=(%s;%s;%s)", file, tag,
00503 docatg, catg, type, tech);
00504 }
00505 }
00506
00507 return CPL_ERROR_NONE;
00508
00509 }
00510
00511