ERIS Pipeline Reference Manual 1.8.15
eris_nix_master_flat.c
1/* $Id$
2 *
3 * This file is part of the ERIS/NIX Pipeline
4 * Copyright (C) 2017 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /*
22 * $Author$
23 * $Date$
24 * $Rev$
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*-----------------------------------------------------------------------------
32 Includes
33 -----------------------------------------------------------------------------*/
34
35#include <cpl.h>
36#include <string.h>
37
38#include "eris_nix_master_flat.h"
39#include "eris_nix_utils.h"
40
41/*----------------------------------------------------------------------------*/
46/*----------------------------------------------------------------------------*/
47
50/*----------------------------------------------------------------------------*/
66/*----------------------------------------------------------------------------*/
67
68master_flat * en_master_flat_create(const hdrl_image * flat,
69 const cpl_mask * cold_bpm,
70 const cpl_image * confidence,
71 const char * filename,
72 const cpl_propertylist * plist) {
73
74 if (cpl_error_get_code() != CPL_ERROR_NONE) return NULL;
75
76 /* check for NULL inputs */
77
78 cpl_ensure(flat, CPL_ERROR_NULL_INPUT, NULL);
79 cpl_ensure(confidence, CPL_ERROR_NULL_INPUT, NULL);
80
81 master_flat * result = cpl_malloc(sizeof(master_flat));
82
83 result->flat = hdrl_image_duplicate(flat);
84 if (cold_bpm) {
85 result->cold_bpm = cpl_mask_duplicate(cold_bpm);
86 } else {
87 result->cold_bpm = NULL;
88 }
89 result->confidence = cpl_image_duplicate(confidence);
90 if (filename) {
91 result->filename = cpl_strdup(filename);
92 } else {
93 result->filename = NULL;
94 }
95 if (plist) {
96 result->plist = cpl_propertylist_duplicate(plist);
97 } else {
98 result->plist = NULL;
99 }
100
101 return result;
102}
103
108/*----------------------------------------------------------------------------*/
116/*----------------------------------------------------------------------------*/
117
118void en_master_flat_delete(master_flat * target) {
119
120 if (target) {
121 cpl_free(target->filename);
122 hdrl_image_delete(target->flat);
123 cpl_mask_delete(target->cold_bpm);
124 cpl_image_delete(target->confidence);
125 cpl_propertylist_delete(target->plist);
126 cpl_free(target);
127 }
128}
129
134/*----------------------------------------------------------------------------*/
141/*----------------------------------------------------------------------------*/
142
143master_flat * en_master_flat_load_from_frameset(const cpl_frameset * frameset,
144 const char * tag,
145 cpl_frameset * used,
146 const int required) {
147
148 if (cpl_error_get_code() != CPL_ERROR_NONE) return NULL;
149
150 cpl_mask * cold_bpm = NULL;
151 cpl_image * confidence = NULL;
152 const char * filename = NULL;
153 hdrl_image * flat = NULL;
154 cpl_mask * full_cold_bpm = NULL;
155 cpl_image * full_conf = NULL;
156 hdrl_image * full_flat = NULL;
157 mef_extension_list * mefs = NULL;
158 cpl_propertylist * plist = NULL;
159 master_flat * result = NULL;
160
161 /* check parameters */
162
163 cpl_ensure(frameset, CPL_ERROR_NULL_INPUT, NULL);
164 cpl_ensure(tag, CPL_ERROR_NULL_INPUT, NULL);
165
166 /* Look for tagged file in frameset */
167
168 const cpl_frame * target_frame = cpl_frameset_find_const(frameset, tag);
169 enu_check(target_frame != NULL, CPL_ERROR_DATA_NOT_FOUND,
170 "SoF has no file tagged %s", tag);
171 filename = cpl_frame_get_filename(target_frame);
172
173 /* Read the data in as an hdrl image, any mef extensions present */
174
175 enu_himage_load_from_fits(filename, &flat, &mefs, &plist);
176 enu_check_error_code("failed to read file %s", filename);
177
178 /* Obtain the cold pixel mask from its mef extension, if present */
179
180 for (int i=0; i < mefs->size; i++) {
181 if (!strcmp(mefs->mef[i]->name, "COLD_BPM")) {
182 cold_bpm = cpl_mask_duplicate(
183 (const cpl_mask *) (mefs->mef[i]->data));
184 break;
185 }
186 }
187
188 /* Obtain the confidence array from its mef extension, if present */
189
190 for (int i=0; i<mefs->size; i++) {
191 if (!strcmp(mefs->mef[i]->name, "CONFIDENCE")) {
192 confidence = cpl_image_duplicate(
193 (const cpl_image *) (mefs->mef[i]->data));
194 break;
195 }
196 }
197 if (!confidence) cpl_msg_warning(cpl_func, "confidence array not found");
198
199 /* If the flat is windowed then create a full-chip version */
200
201 cpl_size nx = 0;
202 cpl_size ny = 0;
203 int rot = 0;
204 cpl_size strx = 0;
205 cpl_size stry = 0;
206 cpl_size nx_chip = 0;
207 cpl_size ny_chip = 0;
208 cpl_boolean windowed = 0;
210 &ny,
211 &rot,
212 &strx,
213 &stry,
214 &nx_chip,
215 &ny_chip,
216 &windowed,
217 plist);
218 enu_check_error_code("Failed to read flatfield window information");
219
220 if (nx != nx_chip || ny != ny_chip) {
221 cpl_msg_info(cpl_func, "windowed flatfield, extending to cover full chip");
222 full_flat = hdrl_image_new(nx_chip,
223 ny_chip);
224 hdrl_image_copy(full_flat,
225 flat,
226 strx,
227 stry);
228
229 if (confidence) {
230 full_conf = cpl_image_new(nx_chip,
231 ny_chip,
232 cpl_image_get_type(confidence));
233 cpl_image_copy(full_conf,
234 confidence,
235 strx,
236 stry);
237
238 }
239
240 if (cold_bpm) {
241 full_cold_bpm = cpl_mask_new(nx_chip,
242 ny_chip);
243 cpl_mask_copy(full_cold_bpm,
244 cold_bpm,
245 strx,
246 stry);
247 }
248
249 } else {
250 full_flat = flat; flat = NULL;
251 full_conf = confidence; confidence = NULL;
252 full_cold_bpm = cold_bpm; cold_bpm = NULL;
253 }
254
255 result = en_master_flat_create(full_flat,
256 full_cold_bpm,
257 full_conf,
258 filename,
259 plist);
260
261 cleanup:
262
263 if (cpl_error_get_code() == CPL_ERROR_NONE) {
264 cpl_frame * dup_frame = cpl_frame_duplicate(target_frame);
265 cpl_frameset_insert(used, dup_frame);
266 } else {
267 en_master_flat_delete(result);
268 result = NULL;
269
270 if (!required) {
271
272 /* reset the error code if something has gone wrong but the
273 master_flat is not definitely required */
274
275 cpl_msg_warning(cpl_func, "no file tagged %s in SoF", tag);
276 cpl_error_reset();
277 }
278 }
279
280 cpl_mask_delete(full_cold_bpm);
281 cpl_mask_delete(cold_bpm);
282 cpl_image_delete(full_conf);
283 cpl_image_delete(confidence);
284 hdrl_image_delete(full_flat);
285 hdrl_image_delete(flat);
287 cpl_propertylist_delete(plist);
288
289 return result;
290}
291
master_flat * en_master_flat_create(const hdrl_image *flat, const cpl_mask *cold_bpm, const cpl_image *confidence, const char *filename, const cpl_propertylist *plist)
Create a new master_flat struct and initialise the contents.
cpl_error_code enu_get_window_info(cpl_size *nx, cpl_size *ny, int *rot, cpl_size *strx, cpl_size *stry, cpl_size *nx_chip, cpl_size *ny_chip, cpl_boolean *windowed, const cpl_propertylist *plist)
Get the detector 'window' information.
cpl_error_code enu_himage_load_from_fits(const char *filename, hdrl_image **result, mef_extension_list **mef_extensions, cpl_propertylist **plist)
Load an hdrl_image from a multi-extension FITS file.
void enu_mef_extension_list_delete(mef_extension_list *list)
Delete a mef_extension_list and its contents.
hdrl_image * hdrl_image_duplicate(const hdrl_image *himg)
copy hdrl_image
Definition: hdrl_image.c:391
cpl_error_code hdrl_image_copy(hdrl_image *dst, const hdrl_image *src, cpl_size xpos, cpl_size ypos)
Copy one image into another.
Definition: hdrl_image.c:686
hdrl_image * hdrl_image_new(cpl_size nx, cpl_size ny)
create new zero filled hdrl image
Definition: hdrl_image.c:311
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
Definition: hdrl_image.c:379