MOONS Pipeline Reference Manual 0.13.2
moo_bpm.c
1/*
2 * This file is part of the MOONS Pipeline
3 * Copyright (C) 2002-2016 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27#include <math.h>
28#include <cpl.h>
29#include "moo_pfits.h"
30#include "moo_fits.h"
31#include "moo_qc.h"
32#include "moo_bpm.h"
33/*----------------------------------------------------------------------------*/
48/*----------------------------------------------------------------------------*/
49
52/*-----------------------------------------------------------------------------
53 Function codes
54 -----------------------------------------------------------------------------*/
55
56/*----------------------------------------------------------------------------*/
64/*----------------------------------------------------------------------------*/
65moo_bpm *
67{
68 moo_bpm *res = cpl_calloc(1, sizeof(moo_bpm));
69 return res;
70}
71
72/*----------------------------------------------------------------------------*/
83/*----------------------------------------------------------------------------*/
84cpl_error_code
85moo_bpm_set_data(moo_bpm *self,
87 int ntas,
88 cpl_image *data,
89 cpl_propertylist *header)
90{
91 cpl_ensure_code(ntas >= 1 && ntas <= 2, CPL_ERROR_ILLEGAL_INPUT);
92 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
93 cpl_ensure_code(header != NULL, CPL_ERROR_NULL_INPUT);
94
95 self->data[(ntas - 1) * 3 + type] = data;
96 self->data_header[(ntas - 1) * 3 + type] = header;
97
98 return CPL_ERROR_NONE;
99}
100/*----------------------------------------------------------------------------*/
111/*----------------------------------------------------------------------------*/
112cpl_error_code
113moo_bpm_add_data(moo_bpm *self,
114 cpl_image *img,
116 int ntas)
117{
118 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
119 cpl_ensure_code(self->filename != NULL, CPL_ERROR_NULL_INPUT);
120 cpl_error_code status = CPL_ERROR_NONE;
121 cpl_errorstate prev_state = cpl_errorstate_get();
122 cpl_propertylist *header = cpl_propertylist_new();
123 char *extname = NULL;
124
125 extname = cpl_sprintf("QUAL_%s", moo_detector_get_extname(type, ntas));
126 cpl_propertylist_append_string(header, MOO_PFITS_EXTNAME, extname);
127 cpl_free(extname);
128
129 if (img != NULL) {
130 cpl_image_save(img, self->filename, CPL_TYPE_INT, header,
131 CPL_IO_EXTEND);
132 }
133 else {
134 cpl_propertylist_save(header, self->filename, CPL_IO_EXTEND);
135 }
136 moo_bpm_set_data(self, type, ntas, img, header);
137
138 if (!cpl_errorstate_is_equal(prev_state)) {
139 cpl_msg_error("moo_bpm", "Error for adding data to %s %s (%s)",
140 self->filename, extname,
141 cpl_error_get_message_default(cpl_error_get_code()));
142 status = cpl_error_get_code();
143 }
144 return status;
145}
146
147/*----------------------------------------------------------------------------*/
156/*----------------------------------------------------------------------------*/
157cpl_error_code
158moo_bpm_merge(moo_bpm *a, moo_bpm *b)
159{
160 cpl_ensure_code(a != NULL, CPL_ERROR_NULL_INPUT);
161 cpl_ensure_code(b != NULL, CPL_ERROR_NULL_INPUT);
162
163 cpl_error_code status = CPL_ERROR_NONE;
164 for (int i = 0; i < 6; i++) {
165 cpl_image *im1 = a->data[i];
166 cpl_image *im2 = b->data[i];
167 if (im1 != NULL && im2 != NULL) {
168 status = cpl_image_or(im1, NULL, im2);
169 }
170 }
171 return status;
172}
173
174/*----------------------------------------------------------------------------*/
184/*----------------------------------------------------------------------------*/
185void
186moo_bpm_save(moo_bpm *self, const char *filename)
187{
188 if (self != NULL) {
189 cpl_propertylist_save(self->primary_header, filename, CPL_IO_CREATE);
190 for (int i = 0; i < 2; i++) {
191 for (int j = 0; j < 3; j++) {
192 const char *extname = moo_detector_get_extname(j, i + 1);
193 moo_fits_write_extension_image(self->data[i * 3 + j], filename,
194 "QUAL", extname, CPL_TYPE_INT,
195 self->data_header[i * 3 + j]);
196 }
197 }
198 }
199}
200
201/*----------------------------------------------------------------------------*/
210/*----------------------------------------------------------------------------*/
211
212void
213moo_bpm_delete(moo_bpm *self)
214{
215 if (self != NULL) {
216 int i;
217 if (self->primary_header != NULL) {
218 cpl_propertylist_delete(self->primary_header);
219 }
220 for (i = 0; i < 6; i++) {
221 if (self->data[i] != NULL) {
222 cpl_image_delete(self->data[i]);
223 }
224 if (self->data_header[i] != NULL) {
225 cpl_propertylist_delete(self->data_header[i]);
226 }
227 }
228 cpl_free(self->filename);
229 cpl_free(self);
230 }
231}
void moo_bpm_save(moo_bpm *self, const char *filename)
Save a moo_bpm to a FITS file.
Definition: moo_bpm.c:186
cpl_error_code moo_bpm_set_data(moo_bpm *self, moo_detector_type type, int ntas, cpl_image *data, cpl_propertylist *header)
set bpm data for relevant extension
Definition: moo_bpm.c:85
cpl_error_code moo_bpm_merge(moo_bpm *a, moo_bpm *b)
Merge to moo_bpm structure in the first.
Definition: moo_bpm.c:158
void moo_bpm_delete(moo_bpm *self)
Delete a moo_bpm.
Definition: moo_bpm.c:213
cpl_error_code moo_bpm_add_data(moo_bpm *self, cpl_image *img, moo_detector_type type, int ntas)
Add CPL_IMAGE extension to BPM filename and update moo_bpm structure.
Definition: moo_bpm.c:113
moo_bpm * moo_bpm_new(void)
Create a new moo_bpm.
Definition: moo_bpm.c:66
const char * moo_detector_get_extname(moo_detector_type type, int ntas)
Get the extension name of a detector.
Definition: moo_detector.c:137
enum _moo_detector_type_ moo_detector_type
The type code type.
Definition: moo_detector.h:64
cpl_error_code moo_fits_write_extension_image(cpl_image *image, const char *filename, const char *name, const char *detectorname, cpl_type type, cpl_propertylist *header)
Write an image as extension in FITS file.
Definition: moo_fits.c:123