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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
|
/* $Id: vmcpl.c,v 1.5 2013-08-23 10:22:37 cgarcia Exp $
*
* This file is part of the VIMOS Pipeline
* Copyright (C) 2002-2004 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* $Author: cgarcia $
* $Date: 2013-08-23 10:22:37 $
* $Revision: 1.5 $
* $Name: not supported by cvs2svn $
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <cxstring.h>
#include <vmutils.h>
#include <piltranslator.h>
#include <pilrecipe.h>
#include "vmcpl.h"
/* FIXME: This is used to compensate for a deficiency in CPL 1.0. Change
* request has been submitted, so this symbol must be removed when
* we switch to the next version of CPL (1.0.x or 1.x)!
*/
#ifndef CPL_FRAME_TYPE_PAF
# define CPL_FRAME_TYPE_PAF (1 << 4)
#endif
/**
* @defgroup vmcpl CPL interface
*
* This module provides the necessary adaptor functionality in order
* to execute data reduction tasks using the CPL recipe interface.
*/
/**@{*/
/*
* Transform a CPL frame into a PilFrame. This function does not handle
* product specific frame attributes because this function should only
* be called for the transformation of raw and calibration frames.
*
* This relies on the fact that VIMOS does not have recipe chains, so that
* product frames from previous data reduction tasks should not appear
* in a set of frames passed to a recipe. Set of frames are 'always'
* created from a set of frames file, which provides only the filename,
* the tag (frame category) and the frame group information.
*/
inline static PilFrame *
_vmCplFrameExport(const cpl_frame *self)
{
PilFrame *frame = NULL;
if (self != NULL) {
const char *name = cpl_frame_get_filename(self);
const char *tag = cpl_frame_get_tag(self);
cpl_frame_group group = cpl_frame_get_group(self);
if (name != NULL && tag != NULL) {
frame = newPilFrame(name, tag);
switch (group) {
case CPL_FRAME_GROUP_NONE:
pilFrmSetType(frame, PIL_FRAME_TYPE_UNDEF);
break;
case CPL_FRAME_GROUP_RAW:
pilFrmSetType(frame, PIL_FRAME_TYPE_RAW);
break;
case CPL_FRAME_GROUP_CALIB:
pilFrmSetType(frame, PIL_FRAME_TYPE_CALIB);
break;
case CPL_FRAME_GROUP_PRODUCT:
pilFrmSetType(frame, PIL_FRAME_TYPE_PRODUCT);
break;
default:
deletePilFrame(frame);
return NULL;
break;
}
}
}
return frame;
}
inline static cpl_frame *
_vmCplFrameImport(const PilFrame *frame)
{
cpl_frame *self = NULL;
if (frame != NULL) {
const char *name = pilFrmGetName(frame);
const char *tag = pilFrmGetCategory(frame);
PilFrameType group = pilFrmGetType(frame);
PilFrameFormat type = pilFrmGetFormat(frame);
PilProductLevel level = pilFrmGetProductLevel(frame);
self = cpl_frame_new();
if (cpl_frame_set_filename(self, name) != 0) {
cpl_frame_delete(self);
return NULL;
}
if (cpl_frame_set_tag(self, tag) != 0) {
cpl_frame_delete(self);
return NULL;
}
switch (group) {
case PIL_FRAME_TYPE_UNDEF:
cpl_frame_set_group(self, CPL_FRAME_GROUP_NONE);
break;
case PIL_FRAME_TYPE_RAW:
cpl_frame_set_group(self, CPL_FRAME_GROUP_RAW);
break;
case PIL_FRAME_TYPE_CALIB:
cpl_frame_set_group(self, CPL_FRAME_GROUP_CALIB);
break;
case PIL_FRAME_TYPE_PRODUCT:
cpl_frame_set_group(self, CPL_FRAME_GROUP_PRODUCT);
break;
default:
cpl_frame_delete(self);
return NULL;
break;
}
switch (type) {
case PIL_FRAME_FORMAT_UNDEF:
cpl_frame_set_type(self, CPL_FRAME_TYPE_NONE);
break;
case PIL_FRAME_FORMAT_IMAGE:
cpl_frame_set_type(self, CPL_FRAME_TYPE_IMAGE);
break;
case PIL_FRAME_FORMAT_TABLE:
cpl_frame_set_type(self, CPL_FRAME_TYPE_TABLE);
break;
case PIL_FRAME_FORMAT_PAF:
cpl_frame_set_type(self, CPL_FRAME_TYPE_PAF);
break;
default:
cpl_frame_delete(self);
return NULL;
break;
}
switch (level) {
case PIL_PRODUCT_LEVEL_UNDEF:
cpl_frame_set_level(self, CPL_FRAME_LEVEL_NONE);
break;
case PIL_PRODUCT_LEVEL_TEMPORARY:
cpl_frame_set_level(self, CPL_FRAME_LEVEL_TEMPORARY);
break;
case PIL_PRODUCT_LEVEL_INTERMEDIATE:
cpl_frame_set_level(self, CPL_FRAME_LEVEL_INTERMEDIATE);
break;
case PIL_PRODUCT_LEVEL_SECONDARY:
case PIL_PRODUCT_LEVEL_PRIMARY:
cpl_frame_set_level(self, CPL_FRAME_LEVEL_FINAL);
break;
default:
cpl_frame_delete(self);
return NULL;
break;
}
}
return self;
}
/**
* @brief
* Export a CPL frameset.
*
* @param set The CPL frameset to export.
* @param sof The set of frames to fill.
*
* @return The function returns 0 on success or a non-zero value otherwise.
*
* The function fills an existing set of frames object @em sof with the
* converted contents of the source CPL frame set @em set. If set is empty,
* or if @em set is @c NULL, the target set of frames is left unchanged.
*
* The intended use of this function is to convert an input CPL frameset
* into a set of frames structure. The function therefore does not deal
* with other frame and product attributes than the frame group. Calling
* this function for a frameset containing products with extended
* attributes will propagate only the frame group to the created set of
* frames.
*
* In most case this is exactly what is needed, since the product attributes
* are only used to help the calling application to write the frames to
* the disk. On input to a subsequent data reduction task the file name,
* the tag and the frame group are sufficient.
*
* @see vmCplImportSof()
*/
int
vmCplFramesetExport(const cpl_frameset *set, PilSetOfFrames *sof)
{
if (sof == NULL) {
return 1;
}
if (set != NULL || !cpl_frameset_is_empty(set)) {
for (int i =0; i< cpl_frameset_get_size(set); i ++)
{
const cpl_frame * frame = cpl_frameset_get_position_const(set, i);
if (cpl_frame_get_group(frame) != CPL_FRAME_GROUP_PRODUCT) {
PilFrame *_frame = _vmCplFrameExport(frame);
if (_frame == NULL) {
return 2;
}
if (pilSofInsert(sof, _frame) == 0) {
return 3;
}
}
}
}
return 0;
}
/**
* @brief
* Imports products to a CPL frameset.
*
* @param set The CPL frameset to be updated.
* @param sof The set of frames from which products are imported.
*
* @return The function returns 0 on success or a non-zero value otherwise.
*
* The function updates an existing CPL frame set @em set with the product
* frames found in the set of frames @em sof. The target CPL frame set must
* exist. If @em set is @c NULL the function returns an error. If the source
* set of frames @em sof is empty, or if it is @c NULL the target frame set
* is left unchanged, otherwise all product frames found in @em sof are
* converted to CPL frames and inserted in @em set.
*
* @see vmCplExportFrameset()
*/
int
vmCplFramesetImport(cpl_frameset *set, const PilSetOfFrames *sof)
{
if (set == NULL) {
return 1;
}
if (sof != NULL || !pilSofIsEmpty(sof)) {
PilFrame *frame = pilSofFirst((PilSetOfFrames *)sof);
while (frame != NULL) {
PilFrameType group = pilFrmGetType(frame);
const char *name = pilFrmGetName(frame);
const char *_name;
cpl_frame *_frame;
cpl_frame_group _group;
int nframes = cpl_frameset_get_size(set);
int i;
switch (group) {
case PIL_FRAME_TYPE_UNDEF:
_group = CPL_FRAME_GROUP_NONE;
break;
case PIL_FRAME_TYPE_RAW:
_group = CPL_FRAME_GROUP_RAW;
break;
case PIL_FRAME_TYPE_CALIB:
_group = CPL_FRAME_GROUP_CALIB;
break;
case PIL_FRAME_TYPE_PRODUCT:
_group = CPL_FRAME_GROUP_PRODUCT;
break;
default:
break;
}
for (i = 0; i < nframes; i++) {
_frame = cpl_frameset_get_position(set, i);
_name = cpl_frame_get_filename(_frame);
if (strcmp(name, _name) == 0) {
cpl_frame_set_group(_frame, _group);
break;
}
}
frame = pilSofNext((PilSetOfFrames *)sof, frame);
}
frame = pilSofFirst((PilSetOfFrames *)sof);
while (frame != NULL) {
if (pilFrmGetType(frame) == PIL_FRAME_TYPE_PRODUCT) {
cpl_frame *_frame = _vmCplFrameImport(frame);
if (_frame == NULL) {
return 2;
}
cpl_frameset_insert(set, _frame);
}
frame = pilSofNext((PilSetOfFrames *)sof, frame);
}
}
return 0;
}
/**
* @brief
* Export a CPL parameter list.
*
* @param list The CPL parameter list to export.
*
* @return The function returns 0 on success or a non-zero value otherwise.
*
* The function fills the recipe configuration database with the contents
* of the CPL parameter list @em list. The recipe configuration database
* must be initialized before this function is called. If the parameter
* list is empty, or if @em list is @c NULL, the recipe configuration
* database is left unchanged.
*/
int
vmCplParlistExport(const cpl_parameterlist *list)
{
if (list != NULL
|| cpl_parameterlist_get_size((cpl_parameterlist *)list) == 0) {
cpl_parameter *parameter =
cpl_parameterlist_get_first((cpl_parameterlist *)list);
while (parameter) {
const char *name;
const char *group;
char *context = (char *)cpl_parameter_get_context(parameter);
int status;
cx_string *value;
/*
* Verify that the parameter context starts with 'vimos.'
*/
if (strstr(context, "vimos.") != context) {
return -1;
}
else {
group = context + strlen("vimos.");
}
/*
* The parameter's command line alias is used as the name
* of the corresponding recipe configuration database entry.
*/
name = cpl_parameter_get_alias(parameter, CPL_PARAMETER_MODE_CLI);
if (name == NULL) {
return -2;
}
else {
char *s = strrchr(name, (int)'.');
if (s != NULL) {
name = s + 1;
}
}
/*
* Create a string representation of the parameter value to
* be inserted into the recipe configuration data base.
*/
value = cx_string_new();
switch (cpl_parameter_get_type(parameter)) {
case CPL_TYPE_BOOL:
if (cpl_parameter_get_bool(parameter) != 0) {
cx_string_set(value, "true");
}
else {
cx_string_set(value, "false");
}
break;
case CPL_TYPE_INT:
{
int i = cpl_parameter_get_int(parameter);
cx_string_sprintf(value, "%i", i);
break;
}
case CPL_TYPE_DOUBLE:
{
double d = cpl_parameter_get_double(parameter);
cx_string_sprintf(value, "%g", d);
break;
}
case CPL_TYPE_STRING:
cx_string_set(value, cpl_parameter_get_string(parameter));
break;
default:
return -3;
break;
}
/*
* Create the new recipe configuration database entry.
*/
status = pilDfsDbCreateEntry(group, name, cx_string_get(value),
READ_WRITE);
if (status != EXIT_SUCCESS) {
cx_string_delete(value);
return 1;
}
cx_string_delete(value);
parameter = cpl_parameterlist_get_next((cpl_parameterlist *)list);
}
}
return 0;
}
/**
* @brief
* Convert the messaging severity level.
*
* @param level Message severity level to convert.
*
* @return The converted message severity level.
*
* The function translates a CPL message severity level to a VIMOS message
* severity level.
*/
PilMsgSeverity
vmCplMsgSeverityExport(cpl_msg_severity level)
{
PilMsgSeverity _level;
switch (level) {
case CPL_MSG_DEBUG:
_level = PIL_MSG_DEBUG;
break;
case CPL_MSG_INFO:
_level = PIL_MSG_INFO;
break;
case CPL_MSG_WARNING:
_level = PIL_MSG_WARNING;
break;
case CPL_MSG_ERROR:
_level = PIL_MSG_ERROR;
break;
case CPL_MSG_OFF:
_level = PIL_MSG_OFF;
break;
default:
break;
}
return _level;
}
/**
* @brief
* Start the recipe execution timer
*
* @param time Address of the variable where the start time will be stored.
*
* @return The function returns 0 on success and a non-zero values in case of
* an error.
*
* The function starts the recipe's execution timer and saves the start time
* to the recipe information structure. In addition, if @em time is not
* @c NULL, the recipe's start time is stored in the variable pointed to by
* @em time.
*/
int
vmCplRecipeTimerStart(PilTime *time)
{
PilTime start;
PilTimer *timer = pilRecGetTimer();
if (timer == NULL) {
timer = newPilTimer();
if (timer == NULL) {
return 1;
}
pilRecSetTimer(timer);
}
else {
if (pilTimerIsActive(timer)) {
pilTimerStop(timer, NULL);
}
}
start = pilTimerStart(timer, NULL);
pilRecSetTimeStart(start);
if (time != NULL) {
*time = start;
}
return 0;
}
/**
* @brief
* Stop the recipe execution timer
*
* @param time Address of the variable where the stop time will be stored.
*
* @return The function returns 0 on success and a non-zero values in case of
* an error.
*
* The function stops the recipe's execution timer and saves the stop time
* to the recipe information structure. In addition, if @em time is not
* @c NULL, the recipe's stop time is stored in the variable pointed to by
* @em time.
*/
int
vmCplRecipeTimerStop(PilTime *time)
{
PilTimer *timer = pilRecGetTimer();
if (timer == NULL) {
return 1;
}
if (pilTimerIsActive(timer)) {
PilTime elapsed;
PilTime start = pilRecGetTimeStart();
pilTimerStop(timer, NULL);
elapsed = pilTimerElapsed(timer, NULL);
pilRecSetTimeStop(start + elapsed);
if (time != NULL) {
*time = start + elapsed;
}
return 0;
}
return 2;
}
/**
* @brief
* Initialize the VIMOS recipe subsystems.
*
* @param recipe The name of the recipe to initialize.
*
* @return The function returns 0 on success or a non-zero value otherwise.
*
* The function uses the current CPL subsystem settings to initialize
* the corresponding VIMOS recipe subsystems in the same way.
*/
int
vmCplRecipeStart(const char *recipe, const char *version)
{
PilMsgSeverity level;
cx_print_func printer;
/*
* Initialize the recipe information structure
*/
if (pilRecSetName(recipe) == EXIT_FAILURE) {
return 1;
}
if (pilRecSetVersion(version) == EXIT_FAILURE) {
return 1;
}
if (pilRecSetInstrument("vimos") == EXIT_FAILURE) {
return 1;
}
/*
* Initialize the messaging subsystem
*/
pilMsgStart();
pilMsgSetRecipeName(recipe);
printer = cx_print_set_handler(NULL);
cx_print_set_handler(printer);
pilMsgSetPrintHandler(printer);
printer = cx_printerr_set_handler(NULL);
cx_printerr_set_handler(printer);
pilMsgSetErrorHandler(printer);
level = PIL_MSG_OFF; /* vmCplMsgSeverityExport(cpl_msg_get_log_level()); */
pilMsgEnableLog(level);
if (level == PIL_MSG_DEBUG) {
pilMsgEnableTimeTag();
pilMsgEnableComponentTag();
}
/* level = vmCplMsgSeverityExport(cpl_msg_get_level()); */
pilMsgEnableTerminal(level);
if (level == PIL_MSG_DEBUG) {
pilMsgEnableTimeTag();
pilMsgEnableComponentTag();
}
/*
* Initialize the recipe configuration database
*/
if (pilDfsCreateDB('.', USE_CASE) == EXIT_FAILURE) {
return 2;
}
/*
* Initialize keyword and category translation tables
*/
/* FIXME: Add suport for loading translation tables from files
* since the old system supports it too. (RP)
*/
if (pilTrnInitKeywordMap() == EXIT_FAILURE) {
return 3;
}
if (pilTrnInitCategoryMap() == EXIT_FAILURE) {
return 4;
}
return 0;
}
/**
* @brief
* Shutdown the VIMOS recipe subsystems.
*
* @return The function returns 0 on success or a non-zero value otherwise.
*
* The function stops the VIMOS recipe subsystems initialized by a call to
* @b vmCplRecipeStart().
* the corresponding VIMOS recipe subsystems in the same way.
*/
int
vmCplRecipeStop(void)
{
/*
* Services should be disabled in the reverse order of
* initialization. See vmCplRecipeStart() for details.
*/
pilTrnClearCategoryMap();
pilTrnClearKeywordMap();
pilDfsFreeDB();
if (pilMsgCloseLog() != EXIT_SUCCESS) {
return 1;
}
pilMsgStop();
pilRecInfoClear();
return 0;
}
/**
* @brief
* Apply post processing steps to all frames in a set of frames.
*
* @param sof Set of frames to process.
*
* @return The function returns 0 on success, or a non-zero value otherwise.
*
* The function finalizes the set of frames after the data reduction task
* has completed. For registered product frames, for instance, the FITS
* header entries are completed with generic product information.
*/
int
vmCplPostProcessFrames(PilSetOfFrames *sof, const char *recipename)
{
// const char *fctid = "vmCplPostProcessFrames";
PilFrame *frame = NULL;
if (sof == NULL) {
return 1;
}
/*
* Process all frames in the set. Apply the type specific
* post processing task (if any).
*/
frame = pilSofFirst(sof);
if (frame == NULL) {
return 2;
}
while (frame) {
if (pilFrmGetType(frame) == PIL_FRAME_TYPE_PRODUCT) {
if (pilFrmGetFormat(frame) != PIL_FRAME_FORMAT_PAF) {
/* if (pilRecUpdateProductInfo(frame, NULL, sof) != 0) { */
if (vm_dfs_setup_product_header(frame, recipename, sof) != 0) {
return 3;
}
}
}
frame = pilSofNext(sof, frame);
}
return 0;
}
/**@}*/
|