32 #include "muse_illum_z.h" 35 typedef struct muse_illumflat muse_illumflat;
39 cpl_propertylist *header;
43 typedef void (*(*muse_allocator_func)(
void));
44 typedef void (*muse_free_func)(
void *);
52 muse_valloc(cpl_size size, muse_allocator_func constructor)
54 void **
self = cpl_calloc(size,
sizeof *
self);
57 for (pos = 0; pos < size; ++pos) {
58 self[pos] = constructor();
66 muse_vfree(
void **array, cpl_size size, muse_free_func deallocator)
70 for (idx = 0; idx < size; ++idx) {
72 deallocator(array[idx]);
81 static muse_illumflat *
82 muse_illumflat_new(
void)
84 muse_illumflat *
self = cpl_malloc(
sizeof *
self);
94 muse_illumflat_delete(muse_illumflat *
self)
99 cpl_propertylist_delete(self->header);
102 cpl_image_delete(self->image);
125 cpl_size nframes = cpl_frameset_get_size(aProcessing->
inframes);
126 cpl_ensure(nframes == 1, CPL_ERROR_ILLEGAL_INPUT, -1);
129 const cpl_frame *frame = cpl_frameset_find(aProcessing->
inframes,
131 const char *filename = cpl_frame_get_filename(frame);
133 cpl_msg_info(__func__,
"Loading raw illumination flat field '%s'",
138 cpl_size *extensions = cpl_malloc(kMuseNumIFUs *
sizeof(cpl_size));
140 unsigned char kifu = 0;
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);
146 cpl_msg_error(__func__,
"Cannot read extension '%s' from input " 147 "file '%s'", extname, filename);
148 cpl_free(extensions);
151 }
else if (extension > 0) {
152 extensions[kifu] = extension;
155 cpl_msg_warning(__func__,
"Input file '%s' does not contain data " 156 "for IFU '%s'!", filename, extname);
162 cpl_msg_error(__func__,
"Input file '%s' does not contain any IFU " 164 cpl_free(extensions);
171 cpl_size nextensions = kifu + 1;
173 muse_illumflat **illumflats = (muse_illumflat **)
174 muse_valloc(nextensions, (muse_allocator_func)muse_illumflat_new);
176 for (nifu = 0; nifu <= kifu; ++nifu) {
177 muse_illumflat *illumflat = illumflats[nifu];
179 cpl_size iext = (nifu == 0) ? 0 : extensions[nifu - 1];
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);
192 illumflat->image = NULL;
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);
207 cpl_free(extensions);
210 CPL_FRAME_GROUP_RAW, 1);
212 cpl_msg_info(__func__,
" Converting raw ilumination flat-field to product");
217 cpl_propertylist *header = cpl_propertylist_duplicate(illumflats[0]->header);
218 cpl_frame *product_frame =
220 CPL_FRAME_TYPE_IMAGE);
221 if (!product_frame) {
222 cpl_msg_error(__func__,
"Could not create product frame for input file " 224 cpl_propertylist_delete(header);
226 muse_vfree((
void **)illumflats, nextensions,
227 (muse_free_func)muse_illumflat_delete);
233 cpl_propertylist_save(header, cpl_frame_get_filename(product_frame),
237 for (iextension = 1; iextension < nextensions; ++iextension) {
238 muse_illumflat *illumflat = illumflats[iextension];
240 cpl_errorstate status = cpl_errorstate_get();
242 cpl_image_save(illumflat->image, cpl_frame_get_filename(product_frame),
243 CPL_TYPE_INT, illumflat->header, CPL_IO_EXTEND);
245 if (!cpl_errorstate_is_equal(status)) {
250 cpl_msg_error(__func__,
"Cannot save extension '%s' to recipe product " 251 "file %s", extname, cpl_frame_get_filename(product_frame));
253 cpl_propertylist_delete(header);
255 muse_vfree((
void **)illumflats, nextensions,
256 (muse_free_func)muse_illumflat_delete);
261 cpl_propertylist_delete(header);
263 muse_vfree((
void **)illumflats, nextensions,
264 (muse_free_func)muse_illumflat_delete);
267 cpl_frameset_insert(aProcessing->
outframes, product_frame);
Structure to hold the parameters of the muse_illum recipe.
const char * muse_pfits_get_extname(const cpl_propertylist *aHeaders)
find out the extension name
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.
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.