MOONS Pipeline Reference Manual 0.13.2
moo_psf.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_psf.h"
32/*----------------------------------------------------------------------------*/
47/*----------------------------------------------------------------------------*/
48
51/*-----------------------------------------------------------------------------
52 Function codes
53 -----------------------------------------------------------------------------*/
54
55/*----------------------------------------------------------------------------*/
63/*----------------------------------------------------------------------------*/
64moo_psf *
66{
67 moo_psf *res = cpl_calloc(1, sizeof(moo_psf));
68 return res;
69}
70
71/*----------------------------------------------------------------------------*/
80/*----------------------------------------------------------------------------*/
81moo_psf *
82moo_psf_create(const char *filename)
83{
84 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
85 cpl_errorstate prev_state = cpl_errorstate_get();
86 moo_psf *res = moo_psf_new();
87 res->filename = cpl_strdup(filename);
88 res->primary_header = cpl_propertylist_new();
89 moo_fits_create(filename);
90
91 if (!cpl_errorstate_is_equal(prev_state)) {
92 cpl_msg_info("moo_psf", "psf load %d", cpl_error_get_code());
93 cpl_errorstate_set(prev_state);
94 }
95 return res;
96}
97
98/*----------------------------------------------------------------------------*/
107/*----------------------------------------------------------------------------*/
108moo_psf *
109moo_psf_load(const cpl_frame *psfframe)
110{
111 cpl_ensure(psfframe != NULL, CPL_ERROR_NULL_INPUT, NULL);
112
113 const char *filename = cpl_frame_get_filename(psfframe);
114 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
115 cpl_errorstate prev_state = cpl_errorstate_get();
116 moo_psf *res = moo_psf_new();
117 res->filename = cpl_strdup(filename);
118 res->primary_header = cpl_propertylist_load(filename, 0);
119 for (int i = 1; i <= 2; i++) {
120 res->ri[i - 1] =
121 moo_psf_single_create(filename,
123 res->yj[i - 1] =
124 moo_psf_single_create(filename,
126 res->h[i - 1] =
127 moo_psf_single_create(filename,
129 }
130 if (!cpl_errorstate_is_equal(prev_state)) {
131 cpl_msg_info("moo_psf", "psf load %d", cpl_error_get_code());
132 cpl_errorstate_set(prev_state);
133 }
134 return res;
135}
136
137/*----------------------------------------------------------------------------*/
149/*----------------------------------------------------------------------------*/
150moo_psf_single *
151moo_psf_get_single(moo_psf *self, moo_detector_type type, int ntas)
152{
153 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
154
155 switch (type) {
156 case MOO_TYPE_RI:
157 return self->ri[ntas - 1];
158 break;
159 case MOO_TYPE_YJ:
160 return self->yj[ntas - 1];
161 break;
162 case MOO_TYPE_H:
163 return self->h[ntas - 1];
164 break;
165 default:
166 return NULL;
167 }
168}
169/*----------------------------------------------------------------------------*/
181/*----------------------------------------------------------------------------*/
182cpl_error_code
183moo_psf_set_single(moo_psf *self,
185 int ntas,
186 moo_psf_single *single)
187{
188 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
189 cpl_ensure_code(single != NULL, CPL_ERROR_NULL_INPUT);
190
191 cpl_error_code status = CPL_ERROR_NONE;
192 cpl_errorstate prev_state = cpl_errorstate_get();
193
194 switch (type) {
195 case MOO_TYPE_RI:
196 self->ri[ntas - 1] = single;
197 break;
198 case MOO_TYPE_YJ:
199 self->yj[ntas - 1] = single;
200 break;
201 case MOO_TYPE_H:
202 self->h[ntas - 1] = single;
203 break;
204 }
205 if (!cpl_errorstate_is_equal(prev_state)) {
206 cpl_msg_error("moo_psf", "Error for adding ext to %s (%d)",
207 self->filename, cpl_error_get_code());
208 status = cpl_error_get_code();
209 cpl_errorstate_set(prev_state);
210 }
211 return status;
212}
213
214/*----------------------------------------------------------------------------*/
226/*----------------------------------------------------------------------------*/
227cpl_error_code
228moo_psf_add_single(moo_psf *self,
229 moo_psf_single *single,
231 int ntas)
232{
233 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
234 cpl_ensure_code(self->filename != NULL, CPL_ERROR_NULL_INPUT);
235 cpl_ensure_code(single != NULL, CPL_ERROR_NULL_INPUT);
236
237 cpl_error_code status = CPL_ERROR_NONE;
238 cpl_errorstate prev_state = cpl_errorstate_get();
239
240 moo_psf_set_single(self, type, ntas, single);
241 moo_psf_single_save(single, self->filename);
242
243 if (!cpl_errorstate_is_equal(prev_state)) {
244 cpl_msg_error("moo_psf", "Error for adding single to %s (%d)",
245 self->filename, cpl_error_get_code());
246 status = cpl_error_get_code();
247 cpl_errorstate_set(prev_state);
248 }
249 return status;
250}
251
252/*----------------------------------------------------------------------------*/
261/*----------------------------------------------------------------------------*/
262
263void
264moo_psf_delete(moo_psf *self)
265{
266 if (self != NULL) {
267 int i;
268 if (self->filename != NULL) {
269 cpl_free(self->filename);
270 }
271 if (self->primary_header != NULL) {
272 cpl_propertylist_delete(self->primary_header);
273 }
274 for (i = 0; i < 2; i++) {
275 if (self->ri[i] != NULL) {
276 moo_psf_single_delete(self->ri[i]);
277 }
278 if (self->yj[i] != NULL) {
279 moo_psf_single_delete(self->yj[i]);
280 }
281 if (self->h[i] != NULL) {
282 moo_psf_single_delete(self->h[i]);
283 }
284 }
285 cpl_free(self);
286 }
287}
288/*----------------------------------------------------------------------------*/
298/*----------------------------------------------------------------------------*/
299void
300moo_psf_save(moo_psf *self, const char *filename)
301{
302 if (self != NULL) {
303 cpl_propertylist_save(self->primary_header, filename, CPL_IO_CREATE);
304
305 int i;
306 for (i = 0; i < 2; i++) {
307 if (self->ri[i] != NULL) {
308 moo_psf_single_save(self->ri[i], filename);
309 }
310 if (self->yj[i] != NULL) {
311 moo_psf_single_save(self->yj[i], filename);
312 }
313 if (self->h[i] != NULL) {
314 moo_psf_single_save(self->h[i], filename);
315 }
316 }
317 }
318}
319
320/*----------------------------------------------------------------------------*/
331/*----------------------------------------------------------------------------*/
332cpl_error_code
333moo_psf_dump(const moo_psf *self, FILE *stream)
334{
335 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
336 cpl_ensure_code(stream != NULL, CPL_ERROR_NULL_INPUT);
337
338 fprintf(stream, "---MOO_PSF\n");
339 fprintf(stream, "filename %s\n", self->filename);
340
341 return CPL_ERROR_NONE;
342}
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
@ MOO_TYPE_YJ
Definition: moo_detector.h:50
@ MOO_TYPE_H
Definition: moo_detector.h:54
@ MOO_TYPE_RI
Definition: moo_detector.h:46
cpl_error_code moo_fits_create(const char *filename)
Create a new fits file with empty propertylist.
Definition: moo_fits.c:533
void moo_psf_single_delete(moo_psf_single *self)
Delete a moo_psf_single.
void moo_psf_single_save(const moo_psf_single *self, const char *filename)
Save a moo_psf_single to a FITS file.
moo_psf_single * moo_psf_single_create(const char *filename, const char *extname)
Create a new moo_psf_single from the given PSF filename.
moo_psf_single * moo_psf_get_single(moo_psf *self, moo_detector_type type, int ntas)
Get a PSF single from PSF.
Definition: moo_psf.c:151
moo_psf * moo_psf_new(void)
Create a new moo_psf
Definition: moo_psf.c:65
void moo_psf_save(moo_psf *self, const char *filename)
Save a moo_psf to a FITS file.
Definition: moo_psf.c:300
void moo_psf_delete(moo_psf *self)
Delete a moo_psf.
Definition: moo_psf.c:264
cpl_error_code moo_psf_set_single(moo_psf *self, moo_detector_type type, int ntas, moo_psf_single *single)
assign moo_psf_single structure in moo_psf structure
Definition: moo_psf.c:183
cpl_error_code moo_psf_add_single(moo_psf *self, moo_psf_single *single, moo_detector_type type, int ntas)
Add PSF_SINGLE extension to PSF filename and update moo_psf structure.
Definition: moo_psf.c:228
moo_psf * moo_psf_create(const char *filename)
Create a new empty PSF filename.
Definition: moo_psf.c:82
moo_psf * moo_psf_load(const cpl_frame *psfframe)
Load a PSF frame and create a moo_psf.
Definition: moo_psf.c:109
cpl_error_code moo_psf_dump(const moo_psf *self, FILE *stream)
Dump structural information of PSF.
Definition: moo_psf.c:333