MUSE Pipeline Reference Manual  2.5.2
muse_cube_filter.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 /*----------------------------------------------------------------------------*/
64 /*----------------------------------------------------------------------------*/
65 
68 #define PRINT_USAGE(rc) \
69  fprintf(stderr, "Usage: %s [ -x ] [ -f filter,names ] [ -l <lambda1> <lambda"\
70  "2> ] CUBE FILTER_LIST\n", argv[0]); \
71  cpl_end(); return (rc);
72 
73 int main(int argc, char **argv)
74 {
75  const char *idstring = "muse_cube_filter";
76  cpl_init(CPL_INIT_DEFAULT);
77  cpl_msg_set_time_on();
79  cpl_msg_set_level(CPL_MSG_DEBUG);
80  cpl_msg_set_component_on();
81  cpl_errorstate state = cpl_errorstate_get();
82 
83  if (argc < 3) {
84  /* two filenames are needed at least */
85  PRINT_USAGE(1);
86  }
87 
88  cpl_array *filters = NULL; /* filter names */
89  char *iname = NULL, /* input name */
90  *list = NULL; /* filter list names */
91  double lambda1 = NAN,
92  lambda2 = NAN;
93  cpl_boolean extended = CPL_FALSE;
94  int i;
95 
96  /* argument processing */
97  for (i = 1; i < argc; i++) {
98  if (strncmp(argv[i], "-f", 3) == 0) {
99  /* skip to next arg to get start value */
100  i++;
101  if (i < argc) {
102  filters = muse_cplarray_new_from_delimited_string(argv[i], ",");
103  } else {
104  PRINT_USAGE(2);
105  }
106  } else if (strncmp(argv[i], "-x", 3) == 0) {
107  extended = CPL_TRUE;
108  } else if (strncmp(argv[i], "-l", 3) == 0) {
109  /* skip to next arg to get start value */
110  if (i+2 < argc) {
111  lambda1 = atof(argv[++i]);
112  lambda2 = atof(argv[++i]);
113  } else {
114  PRINT_USAGE(3);
115  }
116  } else if (strncmp(argv[i], "-", 1) == 0) { /* unallowed options */
117  PRINT_USAGE(9);
118  } else {
119  if (iname && list) {
120  break; /* we have the required names, skip the rest */
121  }
122  if (!iname) {
123  iname = argv[i]; /* set the name for the input cube */
124  } else {
125  list = argv[i]; /* set the name for the filter list table */
126  }
127  }
128  } /* for i (all arguments) */
129  if (!iname || !list) {
130  PRINT_USAGE(10);
131  }
132  int iext = 0;
133  if (!filters) {
134  cpl_msg_info(idstring, "No filter names given, will use all from \"%s\"",
135  list);
136  /* create the array from the filter list file extension names */
137  filters = cpl_array_new(0, CPL_TYPE_STRING);
138  cpl_errorstate es = cpl_errorstate_get();
139  do {
140  iext++;
141  cpl_propertylist *header = cpl_propertylist_load(list, iext);
142  if (!header) {
143  break;
144  }
145  if (!cpl_propertylist_has(header, "EXTNAME")) {
146  continue;
147  }
148  const char *extname = muse_pfits_get_extname(header);
149  cpl_array_set_size(filters, cpl_array_get_size(filters) + 1);
150  cpl_array_set_string(filters, cpl_array_get_size(filters) - 1, extname);
151  cpl_msg_info(idstring, "Added filter \"%s\"", extname);
152  cpl_propertylist_delete(header);
153  } while (cpl_errorstate_is_equal(es));
154  cpl_errorstate_set(es);
155  } /* if no filters given */
156 
157  muse_table *fcustom = NULL;
158  if (isnan(lambda1) && isnan(lambda2)) {
159  /* do nothing, this is expected if the -l parameter was not used! */
160  } else if (!isfinite(lambda1) || !isfinite(lambda2) || lambda1 >= lambda2) {
161  cpl_msg_error(idstring, "Bad wavelength range given %f ... %f Angstrom!",
162  lambda1, lambda2);
163  cpl_array_delete(filters);
164  PRINT_USAGE(12); /* better stop, before time is spent to load the cube */
165  } else {
166  /* add custom filter */
167  char *fname = cpl_sprintf("CUSTOM_%.3f_%.3f", lambda1, lambda2);
168  cpl_array_set_size(filters, cpl_array_get_size(filters) + 1);
169  cpl_array_set_string(filters, cpl_array_get_size(filters) - 1, fname);
170  /* create ad-hoc filter, using minimal rectangular filter function table */
172  cpl_table *ftable = muse_cpltable_new(muse_filtertable_def, 4);
173  cpl_table_set(ftable, "lambda", 0, lambda1 - 1e-5);
174  cpl_table_set(ftable, "lambda", 1, lambda1);
175  cpl_table_set(ftable, "lambda", 2, lambda2);
176  cpl_table_set(ftable, "lambda", 3, lambda2 + 1e-5);
177  cpl_table_set(ftable, "throughput", 0, 0.);
178  cpl_table_set(ftable, "throughput", 1, 1.);
179  cpl_table_set(ftable, "throughput", 2, 1.);
180  cpl_table_set(ftable, "throughput", 3, 0.);
181  /* now convert into MUSE table with header */
182  fcustom = muse_table_new();
183  fcustom->table = ftable;
184  fcustom->header = cpl_propertylist_new();
185  cpl_propertylist_append_string(fcustom->header, "EXTNAME", fname);
186  cpl_free(fname);
187  } /* else: valid wavelengths */
188 #if 0
189  cpl_array_dump(filters, 0, 10000, stdout);
190  fflush(stdout);
191 #endif
192 
193  cpl_msg_info(idstring, "Loading cube \"%s\"", iname);
194  muse_datacube *cube = muse_datacube_load(iname);
195  if (!cube) {
196  cpl_msg_error(idstring, "MUSE cube could not be loaded from \"%s\"!", iname);
197  PRINT_USAGE(11);
198  }
199 
200  /* get data sizes, mainly for display */
201  int nx = cpl_image_get_size_x(cpl_imagelist_get(cube->data, 0)),
202  ny = cpl_image_get_size_y(cpl_imagelist_get(cube->data, 0)),
203  nl = cpl_imagelist_get_size(cube->data);
204  cpl_msg_info(idstring, "Loaded cube \"%s\" (%dx%dx%d)", iname, nx, ny, nl);
205 
207  cpl_array *names = cpl_array_new(0, CPL_TYPE_STRING);
208  int idx, n = cpl_array_get_size(filters);
209  for (i = 0, idx = 0; i < n; i++) {
210  const char *fname = cpl_array_get_string(filters, i);
211  muse_table *ftable = NULL;
212  if (fname && strncmp(fname, "white", 6) && strncmp(fname, "CUSTOM_", 7)) {
213  iext = cpl_fits_find_extension(list, fname);
214  if (iext < 1) {
215  cpl_msg_warning(idstring, "No filter \"%s\" found in table \"%s\"", fname,
216  list);
217  continue;
218  }
219  cpl_msg_info(idstring, "Integrating over filter \"%s\" (from %s)", fname,
220  MUSE_TAG_FILTER_LIST);
221  ftable = muse_table_new();
222  ftable->table = cpl_table_load(list, iext, 1);
223  ftable->header = cpl_propertylist_load(list, 0);
224  cpl_propertylist *hext = cpl_propertylist_load(list, iext);
225  cpl_propertylist_copy_property_regexp(ftable->header, hext,
226  "^EXTNAME$|^Z|^COMMENT", 0);
227  cpl_propertylist_delete(hext);
228  } else if (fname && !strncmp(fname, "CUSTOM_", 7)) {
229  cpl_msg_info(idstring, "Integrating over \"%s\" filter (wavelength range "
230  "%.3f ... %.3f Angstrom)", fname, lambda1, lambda2);
231  ftable = fcustom;
232  } else {
233  cpl_msg_info(idstring, "Integrating over filter \"%s\" (builtin)", fname);
234  ftable = muse_table_load_filter(NULL, fname);
235  }
236  muse_image *fov = muse_datacube_collapse(cube, ftable);
237  muse_table_delete(ftable);
238  /* we want NANs instead of DQ in the output images */
240  muse_imagelist_set(images, fov, idx++);
241  cpl_array_set_size(names, cpl_array_get_size(names) + 1);
242  cpl_array_set_string(names, cpl_array_get_size(names) - 1, fname);
243  } /* for i (all filters) */
244  cpl_array_delete(filters);
245  muse_datacube_delete(cube);
246 
247 #if 0
248  cpl_array_dump(names, 0, n, stdout);
249  fflush(stdout);
250 #endif
251 
252  /* extended format: append the new images to the original cube file */
253  n = muse_imagelist_get_size(images);
254  if (extended) {
255  cpl_errorstate es = cpl_errorstate_get();
256  cpl_error_code rc = muse_datacube_save_recimages(iname, images, names);
257  if (rc == CPL_ERROR_NONE && cpl_errorstate_is_equal(es)) {
258  cpl_msg_info(idstring, "Appended %d images into cube \"%s\"", n, iname);
259  } else {
260  cpl_msg_error(idstring, "Appending %d images into cube \"%s\" failed: %s",
261  n, iname, cpl_error_get_message());
262  }
263  } else { /* not extended */
264  /* normal case: save new images for each filtered image */
265  char *fn = cpl_sprintf("%s", iname),
266  *basename = strstr(fn, ".fits");
267  basename[0] = '\0'; /* clip off the .fits extension to get the base name */
268  for (i = 0; i < n; i++) {
269  const char *fname = cpl_array_get_string(names, i);
270  muse_image *image = muse_imagelist_get(images, i);
271  char *outname = cpl_sprintf("%s_%s.fits", fn, fname);
272  // XXX treat the FITS header!!
273  muse_image_save(image, outname);
274  cpl_msg_info(idstring, "Saved image for filter \"%s\" into \"%s\"", fname,
275  outname);
276  cpl_free(outname);
277  } /* for i (all images) */
278  cpl_free(fn);
279  } /* else: not extended */
280  cpl_array_delete(names);
281  muse_imagelist_delete(images);
282 
283  if (!cpl_errorstate_is_equal(state)) {
284  cpl_errorstate_dump(state, CPL_FALSE, muse_cplerrorstate_dump_some);
285  }
286  cpl_memory_dump();
287  cpl_end();
288  return 0;
289 }
290 
Structure definition of a MUSE datacube.
Definition: muse_datacube.h:48
const muse_cpltable_def muse_filtertable_def[]
MUSE filter table definition.
Definition: muse_utils.c:768
Structure definition for a collection of muse_images.
const char * muse_pfits_get_extname(const cpl_propertylist *aHeaders)
find out the extension name
Definition: muse_pfits.c:205
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.
void muse_imagelist_delete(muse_imagelist *aList)
Free the memory of the MUSE image list.
muse_table * muse_table_new(void)
Allocate memory for a new muse_table object.
Definition: muse_table.c:63
Structure definition of MUSE three extension FITS file.
Definition: muse_image.h:40
unsigned int muse_imagelist_get_size(muse_imagelist *aList)
Return the number of stored images.
cpl_table * muse_cpltable_new(const muse_cpltable_def *aDef, cpl_size aLength)
Create an empty table according to the specified definition.
cpl_array * muse_cplarray_new_from_delimited_string(const char *aString, const char *aDelim)
Convert a delimited string into an array of strings.
muse_image * muse_imagelist_get(muse_imagelist *aList, unsigned int aIdx)
Get the muse_image of given list index.
cpl_table * table
The table.
Definition: muse_table.h:49
cpl_error_code muse_datacube_save_recimages(const char *aFilename, muse_imagelist *aImages, cpl_array *aNames)
Save reconstructed images of a cube in extra extensions.
void muse_cplerrorstate_dump_some(unsigned aCurrent, unsigned aFirst, unsigned aLast)
Dump some CPL errors.
muse_table * muse_table_load_filter(muse_processing *aProcessing, const char *aFilterName)
Load a table for a given filter name.
Definition: muse_utils.c:799
cpl_imagelist * data
the cube containing the actual data values
Definition: muse_datacube.h:76
Structure to store a table together with a property list.
Definition: muse_table.h:43
void muse_table_delete(muse_table *aTable)
Deallocate memory associated to a muse_table object.
Definition: muse_table.c:80
cpl_propertylist * header
the header
Definition: muse_table.h:56
cpl_error_code muse_image_save(muse_image *aImage, const char *aFilename)
Save the three image extensions and the FITS headers of a MUSE image to a file.
Definition: muse_image.c:405
muse_imagelist * muse_imagelist_new(void)
Create a new (empty) MUSE image list.
Definition of a cpl table structure.
muse_image * muse_datacube_collapse(muse_datacube *aCube, const muse_table *aFilter)
Integrate a FITS NAXIS=3 datacube along the wavelength direction.
void muse_processing_recipeinfo(cpl_plugin *)
Output main pipeline configuration, inputs, and parameters.
cpl_error_code muse_imagelist_set(muse_imagelist *aList, muse_image *aImage, unsigned int aIdx)
Set the muse_image of given list index.
cpl_error_code muse_image_dq_to_nan(muse_image *aImage)
Convert pixels flagged in the DQ extension to NANs in DATA (and STAT, if present).
Definition: muse_image.c:904