ERIS Pipeline Reference Manual 1.8.15
eris_nix_master_dark.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_dfs.h"
39#include "eris_nix_master_dark.h"
40#include "eris_nix_utils.h"
41
42/*----------------------------------------------------------------------------*/
46/*----------------------------------------------------------------------------*/
47
50/*----------------------------------------------------------------------------*/
64/*----------------------------------------------------------------------------*/
65
66master_dark * en_master_dark_create(const hdrl_image * dark,
67 const cpl_mask * hot_bpm,
68 const char * filename,
69 const cpl_propertylist * plist) {
70
71 if (cpl_error_get_code() != CPL_ERROR_NONE) return NULL;
72
73 /* check for NULL inputs */
74
75 cpl_ensure(dark && hot_bpm, CPL_ERROR_NULL_INPUT, NULL);
76
77 master_dark * result = cpl_malloc(sizeof(master_dark));
78
79 result->dark = hdrl_image_duplicate(dark);
80 result->hot_bpm = cpl_mask_duplicate(hot_bpm);
81 if (filename) {
82 result->filename = cpl_strdup(filename);
83 } else {
84 result->filename = NULL;
85 }
86 if (plist) {
87 result->plist = cpl_propertylist_duplicate(plist);
88 } else {
89 result->plist = NULL;
90 }
91
92 return result;
93}
94
95
96/*----------------------------------------------------------------------------*/
104/*----------------------------------------------------------------------------*/
105
106void en_master_dark_delete(master_dark * target) {
107
108 if (target) {
109 cpl_free(target->filename);
110 hdrl_image_delete(target->dark);
111 cpl_mask_delete(target->hot_bpm);
112 cpl_propertylist_delete(target->plist);
113 cpl_free(target);
114 }
115}
116
117
118/*----------------------------------------------------------------------------*/
128/*----------------------------------------------------------------------------*/
129
130master_dark * en_master_dark_load_from_frameset(const cpl_frameset * frameset,
131 const char * tag,
132 cpl_frameset * used) {
133 cpl_mask * bpm = NULL;
134 hdrl_image * dark = NULL;
135 const char * filename = NULL;
136 mef_extension_list * mef_extensions = NULL;
137 cpl_propertylist * plist = NULL;
138 master_dark * result = NULL;
139
140 if (cpl_error_get_code() != CPL_ERROR_NONE) return NULL;
141
142 /* check parameters */
143
144 cpl_ensure(frameset, CPL_ERROR_NULL_INPUT, NULL);
145 cpl_ensure(tag, CPL_ERROR_NULL_INPUT, NULL);
146
147 /* Load elements from tagged file in frameset */
148
149 const cpl_frame * target_frame = cpl_frameset_find_const(frameset, tag);
150 enu_check(target_frame != NULL, CPL_ERROR_DATA_NOT_FOUND,
151 "SoF has no file tagged %s", tag);
152
153 filename = cpl_frame_get_filename(target_frame);
154
155 /* Read the data in as an hdrl image, any mef extensions present */
156
157 enu_himage_load_from_fits(filename, &dark, &mef_extensions,
158 &plist);
159 enu_check_error_code("failed to read file %s", filename);
160
161 bpm = enu_mef_extension_list_get_mask(mef_extensions,
162 ERIS_NIX_HOT_BPM_EXTENSION);
163 enu_check_error_code("failed to find bpm extension %s in %s",
164 ERIS_NIX_HOT_BPM_EXTENSION, filename);
165
166 cpl_frameset_insert(used, cpl_frame_duplicate(target_frame));
167
168 result = en_master_dark_create(dark, bpm, filename, plist);
169
170 cleanup:
171
172 if (cpl_error_get_code() != CPL_ERROR_NONE) {
173 en_master_dark_delete(result);
174 result = NULL;
175 }
176
177 cpl_mask_delete(bpm);
178 hdrl_image_delete(dark);
179 enu_mef_extension_list_delete(mef_extensions);
180 cpl_propertylist_delete(plist);
181
182 return result;
183}
184
185
186/*----------------------------------------------------------------------------*/
201/*----------------------------------------------------------------------------*/
202
203master_dark * en_master_dark_test(const cpl_size nx,
204 const cpl_size ny,
205 const double dit,
206 const int ndit,
207 const char * mode) {
208
209 if (cpl_error_get_code() != CPL_ERROR_NONE) return NULL;
210
211 cpl_image * conf = NULL;
212 hdrl_image * dark = NULL;
213 cpl_image * dark_data = NULL;
214 cpl_image * dark_error = NULL;
215 cpl_mask * hotbpm = NULL;
216 cpl_propertylist * plist = NULL;
217 master_dark * result = NULL;
218
219 /* make test master dark */
220
221 dark_data = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
222 cpl_image_fill_window(dark_data, 1, 1, nx, ny, 7.0);
223 dark_error = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
224 cpl_image_fill_window(dark_error, 1, 1, nx, ny, 3.0);
225 dark = hdrl_image_create(dark_data, dark_error);
226 hotbpm = cpl_mask_new(nx, ny);
227 conf = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
228 cpl_image_fill_window(conf, 1, 1, nx, ny, 5.0);
229
230 plist = cpl_propertylist_new();
231 cpl_propertylist_update_string(plist, CPL_DFS_PRO_CATG,
232 ERIS_NIX_MASTER_DARK_IMG_PRO_CATG);
233 cpl_propertylist_update_string(plist, "PRODCATG", "ANCILLARY.IMAGE");
234
235 cpl_propertylist_update_double(plist, "ESO DET DIT", dit);
236 cpl_propertylist_update_double(plist, "ESO DET NDIT", ndit);
237 cpl_propertylist_update_string(plist, "ESO DET READ CURNAME", mode);
238 cpl_propertylist_update_int(plist, "ESO DET SEQ1 WIN ROT", 0);
239 cpl_propertylist_update_int(plist, "ESO DET SEQ1 WIN STRX", 1);
240 cpl_propertylist_update_int(plist, "ESO DET SEQ1 WIN STRY", 1);
241 cpl_propertylist_update_int(plist, "ESO DET SEQ1 WIN NX", nx);
242 cpl_propertylist_update_int(plist, "ESO DET SEQ1 WIN NY", ny);
243
244 cpl_propertylist_append_double(plist, "ESO QC READ NOISE", 6.2);
245 cpl_propertylist_set_comment(plist, "ESO QC READ NOISE",
246 "Detector read out noise");
247 cpl_propertylist_append_double(plist, "ESO QC READ NOISE VAR", 6.3);
248 cpl_propertylist_set_comment(plist, "ESO QC READ NOISE VAR",
249 "variance on Detector read out noise");
250
251 hdrl_value dark_median = hdrl_image_get_median(dark);
252 cpl_propertylist_append_double(plist, "ESO QC DARK MED",
253 (double) dark_median.data);
254 cpl_propertylist_set_comment(plist, "ESO QC DARK MED",
255 "Dark median");
256
257 hdrl_value dark_mean = hdrl_image_get_mean(dark);
258 cpl_propertylist_append_double(plist, "ESO QC DARK MEAN",
259 (double) dark_mean.data);
260 cpl_propertylist_set_comment(plist, "ESO QC DARK MEAN",
261 "Dark mean");
262 double dark_rms = hdrl_image_get_stdev(dark);
263 cpl_propertylist_append_double(plist, "ESO QC DARK RMS", dark_rms);
264 cpl_propertylist_set_comment(plist, "ESO QC DARK RMS",
265 "Dark RMS");
266 cpl_size nhot = cpl_mask_count(hotbpm);
267 cpl_propertylist_append_int(plist, "ESO QC NUMBER HOT PIXEL", (int)nhot);
268 cpl_propertylist_set_comment(plist, "ESO QC NUMBER HOT PIXEL",
269 "Number of detected hot pixels");
270 cpl_propertylist_append_double(plist, "ESO QC PARTICLE_RATE", -1);
271 cpl_propertylist_set_comment(plist, "ESO QC PARTICLE_RATE",
272 "Particle rate");
273 cpl_propertylist_append_double(plist, "ESO QC GAIN", 8.4);
274 cpl_propertylist_set_comment(plist, "ESO QC GAIN",
275 "Detector gain");
276
277 result = en_master_dark_create(dark, hotbpm, NULL, plist);
278
279 /* tidy up */
280
281 cpl_image_delete(conf);
282 hdrl_image_delete(dark);
283 cpl_image_delete(dark_data);
284 cpl_image_delete(dark_error);
285 cpl_mask_delete(hotbpm);
286 cpl_propertylist_delete(plist);
287
288 if (cpl_error_get_code() != CPL_ERROR_NONE) {
289 en_master_dark_delete(result);
290 result = NULL;
291 }
292
293 return result;
294}
295
master_dark * en_master_dark_create(const hdrl_image *dark, const cpl_mask *hot_bpm, const char *filename, const cpl_propertylist *plist)
Create a new master_dark struct and initialise the contents.
master_dark * en_master_dark_load_from_frameset(const cpl_frameset *frameset, const char *tag, cpl_frameset *used)
Load a 'master_dark' struct from a frameset.
void en_master_dark_delete(master_dark *target)
Delete a 'master_dark' struct.
master_dark * en_master_dark_test(const cpl_size nx, const cpl_size ny, const double dit, const int ndit, const char *mode)
Create a test master_dark struct.
cpl_mask * enu_mef_extension_list_get_mask(mef_extension_list *meflist, const char *target)
Get a cpl_mask from a named mef_extension in a list.
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_value hdrl_image_get_median(const hdrl_image *self)
computes the median and associated error of an image.
hdrl_image * hdrl_image_duplicate(const hdrl_image *himg)
copy hdrl_image
Definition: hdrl_image.c:391
double hdrl_image_get_stdev(const hdrl_image *self)
computes the standard deviation of the data of an image
hdrl_value hdrl_image_get_mean(const hdrl_image *self)
computes mean pixel value and associated error of an image.
hdrl_image * hdrl_image_create(const cpl_image *image, const cpl_image *error)
create a new hdrl_image from to existing images by copying them
Definition: hdrl_image.c:295
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
Definition: hdrl_image.c:379