MUSE Pipeline Reference Manual  2.5.2
muse_illum.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) 2005-2018 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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 /*-------------------------------------------------------------------*
27  * Includes *
28  *-------------------------------------------------------------------*/
29 #include <string.h>
30 #include <cpl.h>
31 #include <muse.h>
32 #include "muse_illum_z.h"
33 
34 
35 typedef struct muse_illumflat muse_illumflat;
36 
37 struct muse_illumflat
38 {
39  cpl_propertylist *header;
40  cpl_image *image;
41 };
42 
43 typedef void (*(*muse_allocator_func)(void));
44 typedef void (*muse_free_func)(void *);
45 
46 
47 /*-------------------------------------------------------------------*
48  * Functions code *
49  *-------------------------------------------------------------------*/
50 
51 static void **
52 muse_valloc(cpl_size size, muse_allocator_func constructor)
53 {
54  void **self = cpl_calloc(size, sizeof *self);
55 
56  cpl_size pos;
57  for (pos = 0; pos < size; ++pos) {
58  self[pos] = constructor();
59  }
60 
61  return self;
62 }
63 
64 
65 static void
66 muse_vfree(void **array, cpl_size size, muse_free_func deallocator)
67 {
68  if (array) {
69  cpl_size idx;
70  for (idx = 0; idx < size; ++idx) {
71  if (deallocator) {
72  deallocator(array[idx]);
73  }
74  }
75  cpl_free(array);
76  }
77  return;
78 }
79 
80 
81 static muse_illumflat *
82 muse_illumflat_new(void)
83 {
84  muse_illumflat *self = cpl_malloc(sizeof *self);
85 
86  self->header = NULL;
87  self->image = NULL;
88 
89  return self;
90 }
91 
92 
93 static void
94 muse_illumflat_delete(muse_illumflat *self)
95 {
96 
97  if (self) {
98  if (self->header) {
99  cpl_propertylist_delete(self->header);
100  }
101  if (self->image) {
102  cpl_image_delete(self->image);
103  }
104  cpl_free(self);
105  }
106 
107  return;
108 }
109 
110 
111 /*-------------------------------------------------------------------*/
118 /*-------------------------------------------------------------------*/
119 int
120 muse_illum_compute(muse_processing *aProcessing, muse_illum_params_t *aParams)
121 {
122 
123  CPL_UNUSED(aParams);
124 
125  cpl_size nframes = cpl_frameset_get_size(aProcessing->inframes);
126  cpl_ensure(nframes == 1, CPL_ERROR_ILLEGAL_INPUT, -1);
127 
128 
129  const cpl_frame *frame = cpl_frameset_find(aProcessing->inframes,
130  "ILLUMFLAT");
131  const char *filename = cpl_frame_get_filename(frame);
132 
133  cpl_msg_info(__func__, "Loading raw illumination flat field '%s'",
134  filename);
135 
136  /* Find the channel data extensions */
137 
138  cpl_size *extensions = cpl_malloc(kMuseNumIFUs * sizeof(cpl_size));
139 
140  unsigned char kifu = 0;
141  unsigned char nifu;
142  for (nifu = 0; nifu < kMuseNumIFUs; ++nifu) {
143  char *extname = cpl_sprintf("CHAN%02hhu", nifu + 1);
144  cpl_size extension = cpl_fits_find_extension(filename, extname);
145  if (extension < 0) {
146  cpl_msg_error(__func__, "Cannot read extension '%s' from input "
147  "file '%s'", extname, filename);
148  cpl_free(extensions);
149  cpl_free(extname);
150  return -1;
151  } else if (extension > 0) {
152  extensions[kifu] = extension;
153  ++kifu;
154  } else {
155  cpl_msg_warning(__func__, "Input file '%s' does not contain data "
156  "for IFU '%s'!", filename, extname);
157  }
158  cpl_free(extname);
159  }
160 
161  if (kifu == 0) {
162  cpl_msg_error(__func__, "Input file '%s' does not contain any IFU "
163  "data", filename);
164  cpl_free(extensions);
165  return -1;
166  }
167 
168  /* Create container for the illumination flat field extensions and *
169  * also account for the primary data unit */
170 
171  cpl_size nextensions = kifu + 1;
172 
173  muse_illumflat **illumflats = (muse_illumflat **)
174  muse_valloc(nextensions, (muse_allocator_func)muse_illumflat_new);
175 
176  for (nifu = 0; nifu <= kifu; ++nifu) {
177  muse_illumflat *illumflat = illumflats[nifu];
178 
179  cpl_size iext = (nifu == 0) ? 0 : extensions[nifu - 1];
180 
181  illumflat->header = cpl_propertylist_load(filename, iext);
182  if (!illumflat->header) {
183  cpl_msg_error(__func__, "Cannot not read metadata of extension '%"
184  CPL_SIZE_FORMAT "' of '%s'!", iext, filename);
185  muse_vfree((void **)illumflats, nextensions,
186  (muse_free_func)muse_illumflat_delete);
187  cpl_free(extensions);
188 
189  return -1;
190  }
191 
192  illumflat->image = NULL;
193  if (nifu > 0) {
194  illumflat->image = cpl_image_load(filename, CPL_TYPE_INT, 0, iext);
195  if (!illumflat->image) {
196  cpl_msg_error(__func__, "Cannot not read image data of extension '%"
197  CPL_SIZE_FORMAT "' of '%s'!", iext, filename);
198  muse_vfree((void **)illumflats, nextensions,
199  (muse_free_func)muse_illumflat_delete);
200  cpl_free(extensions);
201 
202  return -1;
203  }
204  }
205  }
206 
207  cpl_free(extensions);
208 
209  muse_processing_append_used(aProcessing, (cpl_frame *)frame,
210  CPL_FRAME_GROUP_RAW, 1);
211 
212  cpl_msg_info(__func__, " Converting raw ilumination flat-field to product");
213 
214  /* Create a DFS compliant product header for the unprocessed *
215  * illumination flat field */
216 
217  cpl_propertylist *header = cpl_propertylist_duplicate(illumflats[0]->header);
218  cpl_frame *product_frame =
219  muse_processing_new_frame(aProcessing, -1, header, "ILLUM",
220  CPL_FRAME_TYPE_IMAGE);
221  if (!product_frame) {
222  cpl_msg_error(__func__, "Could not create product frame for input file "
223  "'%s'", filename);
224  cpl_propertylist_delete(header);
225 
226  muse_vfree((void **)illumflats, nextensions,
227  (muse_free_func)muse_illumflat_delete);
228  return -1;
229  }
230 
231  /* Save the product illumination flat field */
232 
233  cpl_propertylist_save(header, cpl_frame_get_filename(product_frame),
234  CPL_IO_CREATE);
235 
236  cpl_size iextension;
237  for (iextension = 1; iextension < nextensions; ++iextension) {
238  muse_illumflat *illumflat = illumflats[iextension];
239 
240  cpl_errorstate status = cpl_errorstate_get();
241 
242  cpl_image_save(illumflat->image, cpl_frame_get_filename(product_frame),
243  CPL_TYPE_INT, illumflat->header, CPL_IO_EXTEND);
244 
245  if (!cpl_errorstate_is_equal(status)) {
246  const char *extname = muse_pfits_get_extname(illumflat->header);
247  if (!extname) {
248  extname = "UNKNOWN";
249  }
250  cpl_msg_error(__func__, "Cannot save extension '%s' to recipe product "
251  "file %s", extname, cpl_frame_get_filename(product_frame));
252 
253  cpl_propertylist_delete(header);
254 
255  muse_vfree((void **)illumflats, nextensions,
256  (muse_free_func)muse_illumflat_delete);
257  return -1;
258  }
259  }
260 
261  cpl_propertylist_delete(header);
262 
263  muse_vfree((void **)illumflats, nextensions,
264  (muse_free_func)muse_illumflat_delete);
265 
266 
267  cpl_frameset_insert(aProcessing->outframes, product_frame);
268 
269  return 0;
270 } /* muse_illum_compute() */
Structure to hold the parameters of the muse_illum recipe.
Definition: muse_illum_z.h:48
const char * muse_pfits_get_extname(const cpl_propertylist *aHeaders)
find out the extension name
Definition: muse_pfits.c:205
cpl_frame * muse_processing_new_frame(muse_processing *aProcessing, int aIFU, cpl_propertylist *aHeader, const char *aTag, cpl_frame_type aType)
Create a new frame for a result file.
cpl_frameset * outframes
void muse_processing_append_used(muse_processing *aProcessing, cpl_frame *aFrame, cpl_frame_group aGroup, int aDuplicate)
Add a frame to the set of used frames.
cpl_frameset * inframes