MOONS Pipeline Reference Manual 0.13.2
moo_molectable.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 <string.h>
30#include "moo_pfits.h"
31#include "moo_fits.h"
32#include "moo_qc.h"
33#include "moo_molectable.h"
34/*----------------------------------------------------------------------------*/
49/*----------------------------------------------------------------------------*/
50
53/*-----------------------------------------------------------------------------
54 Function codes
55 -----------------------------------------------------------------------------*/
56
57/*----------------------------------------------------------------------------*/
65/*----------------------------------------------------------------------------*/
66moo_molectable *
68{
69 moo_molectable *res = cpl_calloc(1, sizeof(moo_molectable));
70 return res;
71}
72/*----------------------------------------------------------------------------*/
73
74/*----------------------------------------------------------------------------*/
84/*----------------------------------------------------------------------------*/
85moo_molectable *
86moo_molectable_load(const cpl_frame *frame, const char *modename)
87{
88 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
89 const char *filename = cpl_frame_get_filename(frame);
90 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
91
92 char *extname = NULL;
93
94 cpl_errorstate prev_state = cpl_errorstate_get();
95
96 moo_molectable *res = moo_molectable_new();
97 res->filename = cpl_strdup(filename);
98 res->primary_header = cpl_propertylist_load(filename, 0);
99
100 for (int i = 0; i < 3; i++) {
101 const char *bandname = moo_detector_get_name(i);
102
103 if (modename == NULL || strcmp(bandname, "YJ") == 0) {
104 extname = cpl_sprintf("%s", bandname);
105 }
106 else {
107 extname = cpl_sprintf("%s_%s", bandname, modename);
108 }
109 cpl_size qnum = cpl_fits_find_extension(filename, extname);
110
111 if (qnum > 0) {
112 cpl_propertylist *header = NULL;
113 header = moo_fits_load_extension_header(filename, NULL, extname);
114 res->data_header[i] = header;
115 int naxis = moo_pfits_get_naxis(header);
116 if (naxis > 0) {
117 res->data[i] = cpl_table_load(filename, qnum, 0);
118 }
119 }
120 cpl_free(extname);
121 extname = NULL;
122 }
123
124 if (!cpl_errorstate_is_equal(prev_state)) {
125 cpl_free(extname);
126 cpl_errorstate_set(prev_state);
127 cpl_msg_error("moo_molectable", "Error in loading table %s", filename);
129 res = NULL;
130 }
131
132 return res;
133}
134/*----------------------------------------------------------------------------*/
135
136/*----------------------------------------------------------------------------*/
143/*----------------------------------------------------------------------------*/
144cpl_table *
146{
147 cpl_table *result = NULL;
148
149 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
150
151 result = self->data[type];
152
153 return result;
154}
155
156/*----------------------------------------------------------------------------*/
164/*----------------------------------------------------------------------------*/
165cpl_error_code
166moo_molectable_set_data(moo_molectable *self,
168 cpl_table *data)
169{
170 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
171 if (data != NULL) {
172 self->data[type] = cpl_table_duplicate(data);
173 }
174 return CPL_ERROR_NONE;
175}
176/*----------------------------------------------------------------------------*/
186/*----------------------------------------------------------------------------*/
187void
188moo_molectable_save(moo_molectable *self, const char *filename)
189{
190 if (self != NULL) {
191 cpl_propertylist_save(self->primary_header, filename, CPL_IO_CREATE);
192
193 for (int i = 0; i < 3; i++) {
194 const char *extname = moo_detector_get_name(i);
195
196 moo_fits_write_extension_table(self->data[i], filename, NULL,
197 extname, self->data_header[i]);
198 }
199 }
200}
201
202/*----------------------------------------------------------------------------*/
211/*----------------------------------------------------------------------------*/
212
213void
214moo_molectable_delete(moo_molectable *self)
215{
216 if (self != NULL) {
217 int i;
218 if (self->primary_header != NULL) {
219 cpl_propertylist_delete(self->primary_header);
220 }
221
222 if (self->filename != NULL) {
223 cpl_free(self->filename);
224 }
225 for (i = 0; i < 3; i++) {
226 if (self->data[i] != NULL) {
227 cpl_table_delete(self->data[i]);
228 }
229 if (self->data_header[i] != NULL) {
230 cpl_propertylist_delete(self->data_header[i]);
231 }
232 }
233 cpl_free(self);
234 }
235}
236
237
238/*----------------------------------------------------------------------------*/
250/*----------------------------------------------------------------------------*/
251cpl_error_code
252moo_molectable_dump(const moo_molectable *self, FILE *stream)
253{
254 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
255 cpl_ensure_code(stream != NULL, CPL_ERROR_NULL_INPUT);
256
257 fprintf(stream, "---MOO_MAP\n");
258
259 int i;
260 for (i = 0; i < 3; i++) {
261 if (self->data[i] != NULL) {
262 fprintf(stream, "data[%d] %p\n", i, (void *)self->data[i]);
263 if (self->data[i] != NULL) {
264 cpl_table_dump_structure(self->data[i], stream);
265 }
266 }
267 }
268 return CPL_ERROR_NONE;
269}
const char * moo_detector_get_name(moo_detector_type type)
Get the extension name of a detector.
Definition: moo_detector.c:183
enum _moo_detector_type_ moo_detector_type
The type code type.
Definition: moo_detector.h:64
cpl_error_code moo_fits_write_extension_table(cpl_table *table, const char *filename, const char *name, const char *detectorname, cpl_propertylist *header)
Write an image as extension in FITS file.
Definition: moo_fits.c:187
cpl_error_code moo_molectable_set_data(moo_molectable *self, moo_detector_type type, cpl_table *data)
Set table in molectable.
cpl_error_code moo_molectable_dump(const moo_molectable *self, FILE *stream)
Dump structural information of MOLECTABLE.
moo_molectable * moo_molectable_load(const cpl_frame *frame, const char *modename)
Load a moo_molectable.
void moo_molectable_save(moo_molectable *self, const char *filename)
Save a moo_molectable to a FITS file.
cpl_table * moo_molectable_get_data(moo_molectable *self, moo_detector_type type)
Get table from molec_table.
void moo_molectable_delete(moo_molectable *self)
Delete a moo_molectable.
moo_molectable * moo_molectable_new(void)
Create a new moo_molectable.
int moo_pfits_get_naxis(const cpl_propertylist *plist)
find out the NAXIS value
Definition: moo_pfits.c:980