MOONS Pipeline Reference Manual 0.13.1
moo_spectral_format.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_spectral_format.h"
30/*----------------------------------------------------------------------------*/
45/*----------------------------------------------------------------------------*/
46
49/*-----------------------------------------------------------------------------
50 Function codes
51 -----------------------------------------------------------------------------*/
52
53/*----------------------------------------------------------------------------*/
61/*----------------------------------------------------------------------------*/
62moo_spectral_format *
64{
65 moo_spectral_format *res = cpl_calloc(1, sizeof(moo_spectral_format));
66 return res;
67}
68
69/*----------------------------------------------------------------------------*/
77/*----------------------------------------------------------------------------*/
78moo_spectral_format_info *
80{
81 moo_spectral_format_info *res =
82 cpl_calloc(1, sizeof(moo_spectral_format_info));
83 return res;
84}
85
86/*----------------------------------------------------------------------------*/
97/*----------------------------------------------------------------------------*/
98moo_spectral_format *
99moo_spectral_format_load(const cpl_frame *frame, moo_mode_type m)
100{
101 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
102 const char *filename = cpl_frame_get_filename(frame);
103 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
104
105 cpl_errorstate prev_state = cpl_errorstate_get();
106 moo_spectral_format *res = moo_spectral_format_new();
107
108 cpl_table *full_table = cpl_table_load(filename, 1, 0);
109 const char *mode = moo_mode_get_name(m);
110 res->mode = m;
111 cpl_table_select_all(full_table);
112 cpl_table_and_selected_string(full_table, MODE_COLNAME, CPL_EQUAL_TO, mode);
113 res->table = cpl_table_extract_selected(full_table);
114 cpl_table_delete(full_table);
115
116 if (!cpl_errorstate_is_equal(prev_state)) {
117 cpl_errorstate_set(prev_state);
118 cpl_msg_error("moo_spectral_format",
119 "Error in loading spectral_format %s", filename);
121 res = NULL;
122 }
123 return res;
124}
125
126static moo_spectral_format_info *
127_moo_spectral_format_get(moo_spectral_format *self, int nrow)
128{
129 moo_spectral_format_info *info = NULL;
130 int flag;
131 double wmin = cpl_table_get_double(self->table, WMIN_COLNAME, nrow, &flag);
132 double wmax = cpl_table_get_double(self->table, WMAX_COLNAME, nrow, &flag);
133 double resolution =
134 cpl_table_get_double(self->table, RES_COLNAME, nrow, &flag);
135 double dispmin =
136 cpl_table_get_double(self->table, DISPMIN_COLNAME, nrow, &flag);
137 double dispmax =
138 cpl_table_get_double(self->table, DISPMAX_COLNAME, nrow, &flag);
139 int dir = cpl_table_get_int(self->table, DIRECTION_COLNAME, nrow, &flag);
141 info->dispmin = dispmin;
142 info->dispmax = dispmax;
143 info->resolution = resolution;
144 info->wmin = wmin;
145 info->wmax = wmax;
146 info->direction = dir;
147 return info;
148}
149/*----------------------------------------------------------------------------*/
158/*----------------------------------------------------------------------------*/
159moo_spectral_format_info *
160moo_spectral_format_get(moo_spectral_format *self,
162 int ntas)
163{
164 moo_spectral_format_info *info = NULL;
165
166 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
167 cpl_ensure(ntas >= 1 && ntas <= 2, CPL_ERROR_ILLEGAL_INPUT, NULL);
168
169 char *arm = cpl_sprintf("%s%d", moo_detector_get_name(type), ntas);
170 cpl_table_select_all(self->table);
171 cpl_table_and_selected_string(self->table, ARM_COLNAME, CPL_EQUAL_TO, arm);
172 cpl_array *idx_array = cpl_table_where_selected(self->table);
173 double idx = cpl_array_get(idx_array, 0, NULL);
174
175 info = _moo_spectral_format_get(self, idx);
176
177 cpl_array_delete(idx_array);
178
179 cpl_free(arm);
180 return info;
181}
182
183/*----------------------------------------------------------------------------*/
193/*----------------------------------------------------------------------------*/
194
195cpl_error_code
196moo_spectral_format_get_wave_range(moo_spectral_format *self,
198 double *min,
199 double *max)
200{
201 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
202 cpl_ensure_code(min != NULL, CPL_ERROR_NULL_INPUT);
203 cpl_ensure_code(max != NULL, CPL_ERROR_NULL_INPUT);
204
205 moo_spectral_format_info *info = moo_spectral_format_get(self, type, 1);
206 *min = info->wmin;
207 *max = info->wmax;
208
210
211 return cpl_error_get_code();
212}
213/*----------------------------------------------------------------------------*/
222/*----------------------------------------------------------------------------*/
223void
224moo_spectral_format_delete(moo_spectral_format *self)
225{
226 if (self != NULL) {
227 if (self->table != NULL) {
228 cpl_table_delete(self->table);
229 }
230 cpl_free(self);
231 }
232}
233
234/*----------------------------------------------------------------------------*/
243/*----------------------------------------------------------------------------*/
244void
245moo_spectral_format_info_delete(moo_spectral_format_info *self)
246{
247 if (self != NULL) {
248 cpl_free(self);
249 }
250}
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
const char * moo_mode_get_name(moo_mode_type type)
Get the name of a mode.
Definition: moo_detector.c:204
void moo_spectral_format_delete(moo_spectral_format *self)
Delete a moo_spectral_format.
void moo_spectral_format_info_delete(moo_spectral_format_info *self)
Delete a moo_spectral_format_info.
moo_spectral_format * moo_spectral_format_load(const cpl_frame *frame, moo_mode_type m)
Load a SPECTRAL FORMAT frame and create a moo_spectral_format.
moo_spectral_format_info * moo_spectral_format_info_new(void)
Create a new moo_spectral_format_info.
moo_spectral_format_info * moo_spectral_format_get(moo_spectral_format *self, moo_detector_type type, int ntas)
Get the spectral format information for a given ARM.
moo_spectral_format * moo_spectral_format_new(void)
Create a new moo_spectral_format.
cpl_error_code moo_spectral_format_get_wave_range(moo_spectral_format *self, moo_detector_type type, double *min, double *max)
Get the spectral format wave range for a given detector.