ERIS Pipeline Reference Manual 1.9.2
eris_nix_master_bpm.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_bpm.h"
39#include "eris_nix_utils.h"
40
41/*----------------------------------------------------------------------------*/
45/*----------------------------------------------------------------------------*/
46
49/*----------------------------------------------------------------------------*/
56/*----------------------------------------------------------------------------*/
57
58void en_master_bpm_delete(master_bpm * target) {
59
60 if (target) {
61 cpl_image_delete((cpl_image *) target->bpm);
62 cpl_free((char *) target->filename);
63 cpl_propertylist_delete((cpl_propertylist *) target->plist);
64 cpl_free(target);
65 }
66}
67
72/*----------------------------------------------------------------------------*/
79/*----------------------------------------------------------------------------*/
80
81master_bpm * en_master_bpm_load_from_frameset(const cpl_frameset * frameset,
82 const char * tag,
83 cpl_frameset * used,
84 const int required) {
85 master_bpm * result = NULL;
86 cpl_image * bpm = NULL;
87 cpl_image * full_bpm = NULL;
88 cpl_propertylist * plist = NULL;
89
90 if (cpl_error_get_code() != CPL_ERROR_NONE) return NULL;
91 cpl_ensure(frameset, CPL_ERROR_NULL_INPUT, NULL);
92 cpl_ensure(tag, CPL_ERROR_NULL_INPUT, NULL);
93
94 /* Look for tagged file in frameset */
95
96 const cpl_frame * target_frame = cpl_frameset_find_const(frameset, tag);
97 enu_check(target_frame != NULL, CPL_ERROR_DATA_NOT_FOUND,
98 "SoF has no file tagged %s", tag);
99
100 /* Read the data in as a cpl image */
101
102 const char * filename = cpl_frame_get_filename(target_frame);
103 bpm = cpl_image_load(filename, CPL_TYPE_INT, 0, 0);
104 plist = cpl_propertylist_load(filename, 0);
105
106 /* If the bpm is windowed then create a full-chip version */
107
108 cpl_size nx = 0;
109 cpl_size ny = 0;
110 int rot = 0;
111 cpl_size strx = 0;
112 cpl_size stry = 0;
113 cpl_size nx_chip = 0;
114 cpl_size ny_chip = 0;
115 cpl_boolean windowed = 0;
117 &ny,
118 &rot,
119 &strx,
120 &stry,
121 &nx_chip,
122 &ny_chip,
123 &windowed,
124 plist);
125 enu_check_error_code("Failed to read bpm window information");
126
127 if (nx != nx_chip || ny != ny_chip) {
128 cpl_msg_info(cpl_func, "windowed bpm, extending to cover full chip");
129 full_bpm = cpl_image_new(nx_chip,
130 ny_chip,
131 CPL_TYPE_INT);
132 cpl_image_copy(full_bpm,
133 bpm,
134 strx,
135 stry);
136 } else {
137 full_bpm = bpm; bpm = NULL;
138 }
139
140 result = en_master_bpm_create_from_image(filename, full_bpm, plist);
141
142 cleanup:
143
144 cpl_image_delete(bpm);
145 cpl_image_delete(full_bpm);
146 cpl_propertylist_delete(plist);
147
148 if (cpl_error_get_code() == CPL_ERROR_NONE) {
149 cpl_frame * dup_bpmframe = cpl_frame_duplicate(target_frame);
150 cpl_frameset_insert(used, dup_bpmframe);
151 } else {
152 en_master_bpm_delete(result);
153 result = NULL;
154
155 if (!required) {
156
157 /* reset the error code if something has gone wrong but the
158 master_flat is not definitely required */
159
160 cpl_msg_warning(cpl_func, "no file tagged %s in SoF", tag);
161 cpl_error_reset();
162 }
163 }
164 return result;
165}
166
171/*----------------------------------------------------------------------------*/
180/*----------------------------------------------------------------------------*/
181
182master_bpm * en_master_bpm_create(const char * filename,
183 const cpl_mask * mask,
184 const bpm_code flag_code,
185 const cpl_propertylist * plist) {
186
187 if (cpl_error_get_code() != CPL_ERROR_NONE) return NULL;
188 cpl_ensure(mask && flag_code, CPL_ERROR_NULL_INPUT, NULL);
189
190 master_bpm * result = cpl_malloc(sizeof(master_bpm));
191
192 cpl_size nx = cpl_mask_get_size_x(mask);
193 cpl_size ny = cpl_mask_get_size_y(mask);
194 result->bpm = cpl_image_new(nx, ny, CPL_TYPE_INT);
195
196 /* Set or clear the specified bit in the master_bpm */
197
198 int * bpm_data = cpl_image_get_data_int((cpl_image *) result->bpm);
199 const cpl_binary * mask_data = cpl_mask_get_data_const(mask);
200
201 int bit_mask = (1 << (flag_code-1));
202 for (cpl_size i=0; i < nx * ny; i++) {
203 if (mask_data[i]) {
204 bpm_data[i] |= bit_mask;
205 } else {
206 bpm_data[i] &= (~bit_mask);
207 }
208 }
209
210 if (filename) {
211 result->filename = cpl_strdup(filename);
212 } else {
213 result->filename = NULL;
214 }
215 if (plist) {
216 result->plist = cpl_propertylist_duplicate(plist);
217 } else {
218 result->plist = NULL;
219 }
220
221 return result;
222}
223
228/*----------------------------------------------------------------------------*/
238/*----------------------------------------------------------------------------*/
239
240master_bpm * en_master_bpm_create_from_image(const char * filename,
241 const cpl_image * image,
242 const cpl_propertylist * plist) {
243
244 if (cpl_error_get_code() != CPL_ERROR_NONE) return NULL;
245 cpl_ensure(image, CPL_ERROR_NULL_INPUT, NULL);
246
247 master_bpm * result = cpl_malloc(sizeof(master_bpm));
248
249 result->bpm = cpl_image_duplicate(image);
250
251 if (filename) {
252 result->filename = cpl_strdup(filename);
253 } else {
254 result->filename = NULL;
255 }
256 if (plist) {
257 result->plist = cpl_propertylist_duplicate(plist);
258 } else {
259 result->plist = NULL;
260 }
261
262 return result;
263}
264
269/*----------------------------------------------------------------------------*/
280/*----------------------------------------------------------------------------*/
281
282cpl_mask * en_master_bpm_get_mask(const master_bpm * master,
283 const int flag_mask) {
284
285 if (cpl_error_get_code() != CPL_ERROR_NONE) return NULL;
286 cpl_ensure(master, CPL_ERROR_NULL_INPUT, NULL);
287
288 cpl_size nx = cpl_image_get_size_x(master->bpm);
289 cpl_size ny = cpl_image_get_size_y(master->bpm);
290
291 cpl_mask * result = cpl_mask_new(nx, ny);
292 cpl_binary * result_data = cpl_mask_get_data(result);
293 const int * bpm_data = cpl_image_get_data_int_const(master->bpm);
294
295 for (cpl_size j=0; j<ny; j++) {
296 for (cpl_size i=0; i<nx; i++) {
297 result_data[nx*j+i] = (cpl_binary) ((bpm_data[nx*j+i] & flag_mask) > 0);
298 }
299 }
300
301 if (cpl_error_get_code() != CPL_ERROR_NONE) {
302 cpl_mask_delete(result);
303 result = NULL;
304 }
305
306 return result;
307}
308
313/*----------------------------------------------------------------------------*/
321/*----------------------------------------------------------------------------*/
322
323cpl_error_code en_master_bpm_set(master_bpm * master,
324 const cpl_mask * mask,
325 bpm_code flag_code) {
326
327 if (cpl_error_get_code() != CPL_ERROR_NONE) return cpl_error_get_code();
328 cpl_ensure_code(master && mask, CPL_ERROR_NULL_INPUT);
329
330 /* Check dimensions match */
331
332 cpl_size nx = cpl_mask_get_size_x(mask);
333 cpl_size ny = cpl_mask_get_size_y(mask);
334 cpl_size nx_image = cpl_image_get_size_x(master->bpm);
335 cpl_size ny_image = cpl_image_get_size_y(master->bpm);
336 cpl_ensure_code((nx == nx_image) && (ny == ny_image),
337 CPL_ERROR_INCOMPATIBLE_INPUT);
338
339 /* Set or clear the specified bit in the master_bpm */
340
341 int * bpm_data = cpl_image_get_data_int((cpl_image *) master->bpm);
342 const cpl_binary * mask_data = cpl_mask_get_data_const(mask);
343
344 int bit_mask = (1 << (flag_code-1));
345 for (cpl_size i=0; i < nx * ny; i++) {
346 if (mask_data[i]) {
347 bpm_data[i] |= bit_mask;
348 } else {
349 bpm_data[i] &= (~bit_mask);
350 }
351 }
352
353 return cpl_error_get_code();
354}
355
void en_master_bpm_delete(master_bpm *target)
Delete a 'master_bpm' struct.
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.