MUSE Pipeline Reference Manual  2.1.1
muse_cube_concatenate.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-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 <muse.h>
23 #include <string.h>
24 
25 /*----------------------------------------------------------------------------*/
56 /*----------------------------------------------------------------------------*/
57 
60 #define PRINT_USAGE(rc) \
61  fprintf(stderr, "Usage: %s CUBE_OUT CUBE_IN_1 CUBE_IN_2 [ CUBE_IN_3 ... ]\n",\
62  argv[0]); \
63  cpl_end(); return (rc);
64 
65 int main(int argc, char **argv)
66 {
67  const char *idstring = "muse_cube_concatenate";
68  cpl_init(CPL_INIT_DEFAULT);
69  cpl_msg_set_time_on();
71  cpl_msg_set_level(CPL_MSG_DEBUG);
72  cpl_msg_set_component_on();
73 
74  if (argc < 4) {
75  /* three filenames are needed at least */
76  PRINT_USAGE(1);
77  }
78 
79  char *oname = NULL; /* output cube */
80  /* argument processing */
81  int i;
82  for (i = 1; i < argc; i++) {
83  if (strncmp(argv[i], "-", 1) == 0) { /* unallowed options */
84  PRINT_USAGE(9);
85  } else {
86  if (oname) {
87  break; /* we have the required name, skip the rest */
88  }
89  oname = argv[i];
90  }
91  } /* for i (all arguments) */
92  if (!oname) {
93  PRINT_USAGE(10);
94  }
95  FILE *fp = fopen(oname, "r");
96  if (fp) {
97  cpl_msg_error(idstring, "Output cube \"%s\" is already present!", oname);
98  cpl_msg_error(idstring, "Please specify another output name or rename the "
99  "existing file with this name.");
100  fclose(fp);
101  PRINT_USAGE(11);
102  }
103 
104  int ncubes = argc - i;
105  cpl_msg_info(idstring, "Will write concatenation of %d cubes to \"%s\".",
106  ncubes, oname);
107 
108  cpl_errorstate state = cpl_errorstate_get();
109 
110  /* now start the processing by loading the first cube *
111  * and then appending the other one(s) in turn */
112  cpl_msg_info(idstring, "Loading cube 1 from \"%s\"", argv[2]);
113  muse_datacube *cube1 = muse_datacube_load(argv[2]);
114  int icube = 3; /* index of cube in the argv[] array */
115  for (icube = 3; icube < ncubes + 2; icube++) {
116  cpl_msg_info(idstring, "Loading cube %d from \"%s\"", icube - 1, argv[icube]);
117  muse_datacube *cube2 = muse_datacube_load(argv[icube]);
118  if (!cube2) {
119  cpl_msg_warning(idstring, "Could not load MUSE cube from \"%s\": %s",
120  argv[icube], cpl_error_get_message());
121  continue;
122  }
123  cpl_error_code rc = muse_datacube_concat(cube1, cube2);
124  if (rc != CPL_ERROR_NONE) {
125  cpl_msg_warning(idstring, "Error while concatenating \"%s\": %s",
126  argv[icube], cpl_error_get_message());
127  }
128  muse_datacube_delete(cube2);
129  } /* for icube */
130 
131  int rc = 0;
132  if (!cpl_errorstate_is_equal(state)) {
133  cpl_msg_error(idstring, "Some errors occurred while concatenating the "
134  "cubes (not saving an output cube):");
135  cpl_errorstate_dump(state, CPL_FALSE, muse_cplerrorstate_dump_some);
136  rc = 50;
137  } else {
138  cpl_msg_info(idstring, "Saving concatenated cube as \"%s\".", oname);
139  muse_datacube_save(cube1, oname);
140  cpl_msg_info(idstring, "...done");
141  }
142  muse_datacube_delete(cube1);
143 
144  cpl_memory_dump(); /* only active when CPL_MEMORY_MODE != 0 */
145  cpl_end();
146  return rc;
147 }
148 
Structure definition of a MUSE datacube.
Definition: muse_datacube.h:48
cpl_error_code muse_datacube_concat(muse_datacube *aCube, const muse_datacube *aAppend)
Concatenate one datacube at the end of another one.
muse_datacube * muse_datacube_load(const char *aFilename)
Load header, DATA and optionally STAT and DQ extensions as well as the reconstructed images of a MUSE...
void muse_datacube_delete(muse_datacube *aCube)
Deallocate memory associated to a muse_datacube object.
cpl_error_code muse_datacube_save(muse_datacube *aCube, const char *aFilename)
Save the three cube extensions and the FITS headers of a MUSE datacube to a file. ...
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.