MUSE Pipeline Reference Manual  2.1.1
muse_product_sign.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set sw=2 sts=2 et cin: */
3 /*
4  * This file is part of the MUSE Instrument Pipeline
5  * Copyright (C) 2014 European Southern Observatory
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #include <string.h>
23 #include <muse.h>
24 
25 /*----------------------------------------------------------------------------*/
50 /*----------------------------------------------------------------------------*/
51 
54 #ifdef MUSE_EXPERT /* extra -s flag for the maintainer! */
55  #define PRINT_USAGE(rc) \
56  fprintf(stderr, "Usage: %s [ -s ] FILENAME TAG\n", argv[0]); \
57  cpl_end(); return (rc);
58 #else /* normal compilation, when not maintainer */
59  #define PRINT_USAGE(rc) \
60  fprintf(stderr, "Usage: %s FILENAME TAG\n", argv[0]); \
61  cpl_end(); return (rc);
62 #endif
63 
64 int main(int argc, char **argv)
65 {
66  cpl_init(CPL_INIT_DEFAULT);
68 
69  if (argc <= 2) {
70  /* filename and tag are needed */
71  PRINT_USAGE(1);
72  }
73 
74  /* argument processing */
75  char *iname = NULL, /* filename */
76  *tag = NULL; /* tag */
77  cpl_boolean statcal = CPL_FALSE;
78  int i;
79  for (i = 1; i < argc; i++) {
80 #ifdef MUSE_EXPERT /* extra -s flag for the maintainer! */
81  if (strncmp(argv[i], "-s", 3) == 0) {
82  statcal = CPL_TRUE;
83  } else /* if follows below*/
84 #endif
85  if (strncmp(argv[i], "-", 1) == 0) { /* unallowed options */
86  PRINT_USAGE(9);
87  } else {
88  if (iname && tag) {
89  break; /* we have the required name, skip the rest */
90  }
91  if (!iname) {
92  iname = argv[i]; /* set the filename */
93  } else {
94  tag = argv[i]; /* set the tag */
95  }
96  }
97  } /* for i (all arguments) */
98 
99  cpl_errorstate state = cpl_errorstate_get();
100 
101  cpl_frameset *fset = cpl_frameset_new();
102  cpl_frame *frame = cpl_frame_new();
103  cpl_frame_set_filename(frame, iname);
104  cpl_frame_set_tag(frame, tag);
105  cpl_frame_set_type(frame, CPL_FRAME_TYPE_TABLE);
106  cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
107  cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
108  printf("Working on \"%s\" (tag %s).\n", iname, tag);
109 #if 0
110  printf("frame!\n");
111  cpl_frame_dump(frame, stdout);
112  fflush(stdout);
113 #endif
114  cpl_frameset_insert(fset, frame);
115 #if 0
116  printf("fset unsigned!\n");
117  cpl_frameset_dump(fset, stdout);
118  fflush(stdout);
119 #endif
120  unsigned int flags = CPL_DFS_SIGNATURE_DATAMD5;
121  if (!statcal) {
122  flags |= CPL_DFS_SIGNATURE_CHECKSUM;
123  }
124  cpl_dfs_sign_products(fset, flags);
125 #if 0
126  printf("fset signed!\n");
127  cpl_frameset_dump(fset, stdout);
128  fflush(stdout);
129 #endif
130  cpl_frameset_delete(fset);
131 
132 #ifdef MUSE_EXPERT /* special compilation for the maintainer! */
133  #include <fitsio.h>
134  /* special handling for static calibrations, since these cannot contain *
135  * PIPEFILE keywords which the CPL function above adds unconditionally; *
136  * since there is no way in CPL to rewrite the header with the correct *
137  * CHECKSUM keyword but without changing the DATE, do it with CFITSIO *
138  * directly */
139  if (statcal) {
140  printf("Handling \"%s\" as STATIC calibration!\n", iname);
141  int status;
142  fitsfile *fptr;
143  fits_open_file(&fptr, iname, READWRITE, &status);
144  if (status) {
145  fits_report_error(stderr, status);
146  }
147  /* primary HDU, no need to call fits_movabs_hdu() */
148  fits_delete_key(fptr, "PIPEFILE", &status);
149  if (status) {
150  fits_report_error(stderr, status);
151  }
152  /* add/overwrite key ESO.OBS.NAME */
153  fits_update_key_str(fptr, "HIERARCH ESO OBS NAME", "STATIC",
154  "Static calibration for the MUSE pipeline.", &status);
155  // really insert at position before PRO.CATG, i.e. using
156  // fits_insert_record(unit,key_no,card, &status);
157  // but this needs the position of PRO.CATG first
158  if (status) {
159  fits_report_error(stderr, status);
160  }
161  /* now finally recompute the CHECKSUM */
162  fits_write_chksum(fptr, &status);
163  if (status) {
164  fits_report_error(stderr, status);
165  }
166  fits_close_file(fptr, &status);
167  if (status) {
168  fits_report_error(stderr, status);
169  }
170  } /* if static calibration */
171 #endif /* end of maintainer-only section */
172 
173  int rc = 0;
174  if (!cpl_errorstate_is_equal(state)) {
175  cpl_errorstate_dump(state, CPL_FALSE, muse_cplerrorstate_dump_some);
176  rc = 50;
177  }
178 #if 0
179  cpl_memory_dump();
180 #endif
181  cpl_end();
182  return rc;
183 }
184 
void muse_cplerrorstate_dump_some(unsigned aCurrent, unsigned aFirst, unsigned aLast)
Dump some CPL errors.
void muse_processing_recipeinfo(cpl_plugin *)
Output main pipeline configuration, inputs, and parameters.