MOONS Pipeline Reference Manual 0.13.1
moo_raw.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 <string.h>
29#include <cpl.h>
30#include <hdrl.h>
31#include "moo_utils.h"
32#include "moo_dfs.h"
33#include "moo_pfits.h"
34#include "moo_raw.h"
35/*----------------------------------------------------------------------------*/
41/*----------------------------------------------------------------------------*/
42
45/*-----------------------------------------------------------------------------
46 Function codes
47 -----------------------------------------------------------------------------*/
48moo_raw_ext *
49moo_raw_ext_new(void)
50{
51 moo_raw_ext *raw_ext = cpl_calloc(1, sizeof(moo_raw_ext));
52 return raw_ext;
53}
54
55void
56moo_raw_ext_delete(moo_raw_ext *self)
57{
58 if (self != NULL) {
59 if (self->header != NULL) {
60 cpl_propertylist_delete(self->header);
61 }
62 if (self->data != NULL) {
63 cpl_image_delete(self->data);
64 }
65 cpl_free(self);
66 }
67}
68
69moo_raw_ext *
70moo_raw_ext_create(const char *filename, const char *extname, cpl_type type)
71{
72 moo_raw_ext *res = NULL;
73
74 cpl_size qnum = cpl_fits_find_extension(filename, extname);
75
76 if (qnum > 0) {
77 res = moo_raw_ext_new();
78 res->header = cpl_propertylist_load(filename, qnum);
79 int naxis = moo_pfits_get_naxis(res->header);
80 if (naxis > 0) {
81 res->data = cpl_image_load(filename, type, 0, qnum);
82 }
83 }
84
85 return res;
86}
87
88/*----------------------------------------------------------------------------*/
96/*----------------------------------------------------------------------------*/
97moo_raw *
99{
100 moo_raw *raw = cpl_calloc(1, sizeof(moo_raw));
101 return raw;
102}
103/*----------------------------------------------------------------------------*/
112/*----------------------------------------------------------------------------*/
113void
114moo_raw_delete(moo_raw *self)
115{
116 if (self != NULL) {
117 int i;
118 for (i = 0; i < 2; i++) {
119 moo_raw_ext_delete(self->ri[i]);
120 moo_raw_ext_delete(self->yj[i]);
121 moo_raw_ext_delete(self->h[i]);
122 }
123 if (self->primary_header != NULL) {
124 cpl_propertylist_delete(self->primary_header);
125 }
126 cpl_free(self);
127 }
128}
129
130cpl_error_code
131moo_raw_ext_avg(moo_raw_ext *a, moo_raw_ext *b)
132{
133 cpl_ensure_code(a != NULL, CPL_ERROR_NULL_INPUT);
134 cpl_ensure_code(b != NULL, CPL_ERROR_NULL_INPUT);
135
136 if (a->data != NULL && b->data != NULL) {
137 cpl_image_add(a->data, b->data);
138 cpl_image_divide_scalar(a->data, 2.0);
139 }
140 return cpl_error_get_code();
141}
142
143cpl_error_code
144moo_raw_ext_save(moo_raw_ext *self, const char *filename, cpl_type type)
145{
146 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
147 cpl_ensure_code(filename != NULL, CPL_ERROR_NULL_INPUT);
148
149 if (self->data != NULL) {
150 cpl_image_save(self->data, filename, type, self->header, CPL_IO_EXTEND);
151 }
152 else {
153 cpl_propertylist_save(self->header, filename, CPL_IO_EXTEND);
154 }
155 return cpl_error_get_code();
156}
157/*----------------------------------------------------------------------------*/
168/*----------------------------------------------------------------------------*/
169moo_raw *
170moo_raw_create(const cpl_frame *rawframe)
171{
172 cpl_ensure(rawframe != NULL, CPL_ERROR_NULL_INPUT, NULL);
173
174 const char *filename = cpl_frame_get_filename(rawframe);
175 const char *tag = cpl_frame_get_tag(rawframe);
176 cpl_frame_group group = cpl_frame_get_group(rawframe);
177
178 // message par defaut en pile et return
179 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
180 cpl_ensure(tag != NULL, CPL_ERROR_NULL_INPUT, NULL);
181 cpl_ensure(group == CPL_FRAME_GROUP_RAW, CPL_ERROR_ILLEGAL_INPUT, NULL);
182
183
184 moo_raw *res = moo_raw_new();
185
186 res->primary_header = cpl_propertylist_load(filename, 0);
187
188 res->ri[0] = moo_raw_ext_create(filename, "RI_1", CPL_TYPE_FLOAT);
189 res->ri[1] = moo_raw_ext_create(filename, "RI_2", CPL_TYPE_FLOAT);
190
191 res->yj[0] = moo_raw_ext_create(filename, "YJ_1", CPL_TYPE_DOUBLE);
192 res->yj[1] = moo_raw_ext_create(filename, "YJ_2", CPL_TYPE_DOUBLE);
193
194 res->h[0] = moo_raw_ext_create(filename, "H_1", CPL_TYPE_DOUBLE);
195 res->h[1] = moo_raw_ext_create(filename, "H_2", CPL_TYPE_DOUBLE);
196
197 return res;
198}
199
200/*----------------------------------------------------------------------------*/
211/*----------------------------------------------------------------------------*/
212cpl_error_code
213moo_raw_avg(moo_raw *a, moo_raw *b)
214{
215 cpl_ensure_code(a != NULL, CPL_ERROR_NULL_INPUT);
216 cpl_ensure_code(b != NULL, CPL_ERROR_NULL_INPUT);
217
218 int i;
219 for (i = 0; i < 2; i++) {
220 if (a->ri[i] != NULL && b->ri[i] != NULL) {
221 moo_try_check(moo_raw_ext_avg(a->ri[i], b->ri[i]),
222 "Error adding extension RI %d", i);
223 }
224 if (a->yj[i] != NULL && b->yj[i] != NULL) {
225 moo_try_check(moo_raw_ext_avg(a->yj[i], b->yj[i]),
226 "Error adding extension YJ %d", i);
227 }
228 if (a->h[i] != NULL && b->h[i] != NULL) {
229 moo_try_check(moo_raw_ext_avg(a->h[i], b->h[i]),
230 "Error adding extension H %d", i);
231 }
232 }
233moo_try_cleanup:
234 return cpl_error_get_code();
235}
236
237/*----------------------------------------------------------------------------*/
252/*----------------------------------------------------------------------------*/
253cpl_error_code
254moo_raw_save(moo_raw *self, const char *filename)
255{
256 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
257 cpl_ensure_code(filename != NULL, CPL_ERROR_NULL_INPUT);
258 cpl_ensure_code(self->primary_header != NULL, CPL_ERROR_NULL_INPUT);
259
260 cpl_propertylist_save(self->primary_header, filename, CPL_IO_CREATE);
261
262 int i;
263 for (i = 0; i < 2; i++) {
264 if (self->ri[i] != NULL) {
265 moo_try_check(moo_raw_ext_save(self->ri[i], filename,
266 CPL_TYPE_FLOAT),
267 "Can't save RI %d", i);
268 }
269 if (self->yj[i] != NULL) {
270 moo_try_check(moo_raw_ext_save(self->yj[i], filename,
271 CPL_TYPE_FLOAT),
272 "Can't save YJ %d", i);
273 }
274 if (self->h[i] != NULL) {
275 moo_try_check(moo_raw_ext_save(self->h[i], filename,
276 CPL_TYPE_FLOAT),
277 "Can't save H %d", i);
278 }
279 }
280moo_try_cleanup:
281 return cpl_error_get_code();
282}
int moo_pfits_get_naxis(const cpl_propertylist *plist)
find out the NAXIS value
Definition: moo_pfits.c:980
moo_raw * moo_raw_new(void)
Create a new moo_raw
Definition: moo_raw.c:98
cpl_error_code moo_raw_save(moo_raw *self, const char *filename)
Save a moo_raw to a FITS file.
Definition: moo_raw.c:254
void moo_raw_delete(moo_raw *self)
Delete a moo_raw.
Definition: moo_raw.c:114
moo_raw * moo_raw_create(const cpl_frame *rawframe)
prepare moons raw frames
Definition: moo_raw.c:170
cpl_error_code moo_raw_avg(moo_raw *a, moo_raw *b)
Add to RAW frames (result in a)
Definition: moo_raw.c:213