MUSE Pipeline Reference Manual  2.1.1
muse_products_merge.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) 2015 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 /*----------------------------------------------------------------------------*/
49 /*----------------------------------------------------------------------------*/
50 
53 #define PRINT_USAGE(rc) \
54  fprintf(stderr, "Usage: %s [ -s ] [ -d ] FILENAMES\n", argv[0]); \
55  cpl_end(); return (rc);
56 
57 int main(int argc, char **argv)
58 {
59  cpl_init(CPL_INIT_DEFAULT);
61 
62  if (argc <= 3) {
63  /* filename and two input files are needed at least */
64  PRINT_USAGE(1);
65  }
66 
67  /* argument processing */
68  cpl_boolean dodelete = CPL_FALSE, /* do not delete inputs by default */
69  dosign = CPL_FALSE; /* do not sign by default */
70  cpl_array *inames = cpl_array_new(0, CPL_TYPE_STRING); /* input filenames */
71  int nin = cpl_array_get_size(inames); /* number of input filenames */
72  int i;
73  for (i = 1; i < argc; i++) {
74  if (strncmp(argv[i], "-s", 3) == 0) {
75  dosign = CPL_TRUE;
76  } else if (strncmp(argv[i], "-d", 3) == 0) {
77  dodelete = CPL_TRUE;
78  } else if (strncmp(argv[i], "-", 1) == 0) { /* unallowed options */
79  PRINT_USAGE(9);
80  } else {
81  cpl_array_set_size(inames, ++nin); /* space for the new name */
82  cpl_array_set_string(inames, nin - 1, argv[i]);
83  }
84  } /* for i (all arguments) */
85 
86  cpl_errorstate state = cpl_errorstate_get();
87 
88  /* create frameset from the list of input names */
89  printf("Separate input product%s:\n", nin == 0 ? "" : "s");
90  cpl_frameset *fset = cpl_frameset_new();
91  for (i = 0; i < nin; i++) {
92  cpl_frame *frame = cpl_frame_new();
93  const char *fn = cpl_array_get_string(inames, i);
94  cpl_frame_set_filename(frame, fn);
95 
96  cpl_propertylist *header = cpl_propertylist_load(fn, 0);
97  const char *tag = muse_pfits_get_pro_catg(header);
98  /* check input files for PRO.CATG and ensure that they are not pixel tables */
99  if (tag && !strncmp(tag, "PIXTABLE_", 9)) {
100  fprintf(stderr, "\tWARNING: \"%s\" (PRO.CATG %s), cannot merge this type!\n", fn, tag);
101  cpl_frame_delete(frame);
102  cpl_propertylist_delete(header);
103  continue;
104  }
105  cpl_frame_set_tag(frame, tag);
106  cpl_frame_set_group(frame, CPL_FRAME_GROUP_NONE);
107  /* these frame properties have no consequences here */
108  cpl_frame_set_type(frame, CPL_FRAME_TYPE_NONE);
109  cpl_frame_set_level(frame, CPL_FRAME_LEVEL_NONE);
110  printf("\t\"%s\" (PRO.CATG %s).\n", fn, tag);
111  cpl_propertylist_delete(header);
112 #if 0
113  printf("frame!\n");
114  cpl_frame_dump(frame, stdout);
115  fflush(stdout);
116 #endif
117  cpl_frameset_insert(fset, frame);
118  } /* for i (all input filenames) */
119  cpl_array_delete(inames);
120 
121  /* check that there is something to be merged */
122  nin = cpl_frameset_get_size(fset);
123  if (nin <= 1) {
124  cpl_frameset_delete(fset);
125  PRINT_USAGE(10);
126  }
127 
128  printf("Merging %d products, %s the input files...\n", nin,
129  dodelete ? "deleting" : "keeping");
130  muse_utils_frameset_merge_frames(fset, dodelete);
131 #if 0
132  printf("merged frameset:\n");
133  cpl_frameset_dump(fset, stdout);
134  fflush(stdout);
135 #endif
136 
137  int nout = cpl_frameset_get_size(fset),
138  nmerged = nout - (dodelete ? 0 : nin); /* subtract input files */
139  if (dosign) {
140  printf("Signing %d output product%s...\n", nmerged, nmerged == 0 ? "" : "s");
141  cpl_dfs_sign_products(fset, CPL_DFS_SIGNATURE_DATAMD5 | CPL_DFS_SIGNATURE_CHECKSUM);
142  }
143 
144  printf("Merged output product%s:\n", nout == 0 ? "" : "s");
145  for (i = 0; i < nout; i++) {
146  const cpl_frame *frame = cpl_frameset_get_position_const(fset, i);
147  if (cpl_frame_get_group(frame) != CPL_FRAME_GROUP_PRODUCT) {
148  continue; /* ignore (input) file */
149  }
150  const char *fn = cpl_frame_get_filename(frame);
151  cpl_propertylist *header = cpl_propertylist_load(fn, 0);
152  const char *tag = muse_pfits_get_pro_catg(header);
153  int next = cpl_fits_count_extensions(fn);
154  printf("\t\"%s\" (PRO.CATG %s, %d FITS extensions)\n", fn, tag, next);
155  cpl_propertylist_delete(header);
156  } /* for i (all output frames) */
157  cpl_frameset_delete(fset);
158 
159  int rc = 0;
160  if (!cpl_errorstate_is_equal(state)) {
161  cpl_errorstate_dump(state, CPL_FALSE, muse_cplerrorstate_dump_some);
162  rc = 50;
163  }
164  cpl_memory_dump(); /* only active when CPL_MEMORY_MODE != 0 */
165  cpl_end();
166  return rc;
167 }
168 
void muse_cplerrorstate_dump_some(unsigned aCurrent, unsigned aFirst, unsigned aLast)
Dump some CPL errors.
const char * muse_pfits_get_pro_catg(const cpl_propertylist *aHeaders)
find out the PRO category
Definition: muse_pfits.c:159
void muse_processing_recipeinfo(cpl_plugin *)
Output main pipeline configuration, inputs, and parameters.
cpl_error_code muse_utils_frameset_merge_frames(cpl_frameset *aFrames, cpl_boolean aDelete)
Merge IFU-specific files from a frameset to create multi-IFU outputs.
Definition: muse_utils.c:589