MOONS Pipeline Reference Manual 0.13.1
moo_loc.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 <hdrl.h>
30#include "moo_pfits.h"
31#include "moo_loc.h"
32#include "moo_fibres_table.h"
33/*----------------------------------------------------------------------------*/
48/*----------------------------------------------------------------------------*/
49
52/*-----------------------------------------------------------------------------
53 Function codes
54 -----------------------------------------------------------------------------*/
55
56/*----------------------------------------------------------------------------*/
64/*----------------------------------------------------------------------------*/
65moo_loc *
67{
68 moo_loc *res = cpl_calloc(1, sizeof(moo_loc));
69 return res;
70}
71
72/*----------------------------------------------------------------------------*/
81/*----------------------------------------------------------------------------*/
82moo_loc *
83moo_loc_create(const char *filename)
84{
85 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
86 cpl_errorstate prev_state = cpl_errorstate_get();
87 moo_loc *res = moo_loc_new();
88 res->primary_header = cpl_propertylist_new();
89 res->filename = cpl_strdup(filename);
90 cpl_propertylist_save(res->primary_header, filename, CPL_IO_CREATE);
91 if (!cpl_errorstate_is_equal(prev_state)) {
92 cpl_msg_error("moo_loc", "Can't load filename %s (%s)", filename,
93 cpl_error_get_message());
94 }
95 return res;
96}
97
98/*----------------------------------------------------------------------------*/
107/*----------------------------------------------------------------------------*/
108moo_loc *
109moo_loc_load(const cpl_frame *locframe)
110{
111 cpl_ensure(locframe != NULL, CPL_ERROR_NULL_INPUT, NULL);
112
113 const char *filename = cpl_frame_get_filename(locframe);
114 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
115 cpl_errorstate prev_state = cpl_errorstate_get();
116 moo_loc *res = moo_loc_new();
117
118 res->filename = cpl_strdup(filename);
119 res->primary_header = cpl_propertylist_load(filename, 0);
120
121 for (int i = 1; i <= 2; i++) {
122 res->ri[i - 1] =
123 moo_loc_single_create(res->filename,
125 res->yj[i - 1] =
126 moo_loc_single_create(res->filename,
128 res->h[i - 1] =
129 moo_loc_single_create(res->filename,
131 }
132 if (!cpl_errorstate_is_equal(prev_state)) {
133 const char *tag = cpl_frame_get_tag(locframe);
134 cpl_msg_error("moo_loc", "Cant' load %s (%s) error code : %d", filename,
135 tag, cpl_error_get_code());
136 }
137 return res;
138}
139
140/*----------------------------------------------------------------------------*/
153/*----------------------------------------------------------------------------*/
154moo_loc_single *
155moo_loc_get_single(moo_loc *self, moo_detector_type type, int ntas)
156{
157 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
158 cpl_ensure(ntas >= 1 && ntas <= 2, CPL_ERROR_ILLEGAL_INPUT, NULL);
159
160 switch (type) {
161 case MOO_TYPE_RI:
162 return self->ri[ntas - 1];
163 break;
164 case MOO_TYPE_YJ:
165 return self->yj[ntas - 1];
166 break;
167 case MOO_TYPE_H:
168 return self->h[ntas - 1];
169 break;
170 default:
171 return NULL;
172 }
173}
174/*----------------------------------------------------------------------------*/
186/*----------------------------------------------------------------------------*/
187cpl_error_code
188moo_loc_set_single(moo_loc *self,
190 int ntas,
191 moo_loc_single *single)
192{
193 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
194 cpl_ensure_code(single != NULL, CPL_ERROR_NULL_INPUT);
195
196 cpl_error_code status = CPL_ERROR_NONE;
197 cpl_errorstate prev_state = cpl_errorstate_get();
198
199 switch (type) {
200 case MOO_TYPE_RI:
201 self->ri[ntas - 1] = single;
202 break;
203 case MOO_TYPE_YJ:
204 self->yj[ntas - 1] = single;
205 break;
206 case MOO_TYPE_H:
207 self->h[ntas - 1] = single;
208 break;
209 }
210 if (!cpl_errorstate_is_equal(prev_state)) {
211 cpl_msg_error("moo_loc", "Error for adding fibre table to %s (%d)",
212 self->filename, cpl_error_get_code());
213 status = cpl_error_get_code();
214 cpl_errorstate_set(prev_state);
215 }
216 return status;
217}
218
219/*----------------------------------------------------------------------------*/
232/*----------------------------------------------------------------------------*/
233cpl_error_code
234moo_loc_add_single(moo_loc *self,
235 moo_loc_single *single,
237 int ntas,
238 int keep_points)
239{
240 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
241 cpl_ensure_code(self->filename != NULL, CPL_ERROR_NULL_INPUT);
242 cpl_ensure_code(single != NULL, CPL_ERROR_NULL_INPUT);
243
244 cpl_error_code status = CPL_ERROR_NONE;
245 cpl_errorstate prev_state = cpl_errorstate_get();
246
247 moo_loc_set_single(self, type, ntas, single);
248 moo_loc_single_save(single, self->filename, keep_points);
249 if (!cpl_errorstate_is_equal(prev_state)) {
250 cpl_errorstate_dump(prev_state, CPL_FALSE, cpl_errorstate_dump_one);
251 cpl_msg_error("moo_loc", "Error for adding LOC single table to %s (%d)",
252 self->filename, cpl_error_get_code());
253 status = cpl_error_get_code();
254 cpl_errorstate_set(prev_state);
255 }
256 return status;
257}
258
259/*----------------------------------------------------------------------------*/
269/*----------------------------------------------------------------------------*/
270cpl_error_code
271moo_loc_add_fibre_table(moo_loc *self, cpl_table *fibre_table)
272{
273 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
274 cpl_ensure_code(self->filename != NULL, CPL_ERROR_NULL_INPUT);
275 cpl_ensure_code(fibre_table != NULL, CPL_ERROR_NULL_INPUT);
276
277 cpl_error_code status = CPL_ERROR_NONE;
278 cpl_errorstate prev_state = cpl_errorstate_get();
279 self->fibre_table = fibre_table;
280
281 cpl_propertylist *h = cpl_propertylist_new();
282 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
283 MOO_FIBRES_TABLE_EXTNAME);
284 cpl_table_save(self->fibre_table, NULL, h, self->filename, CPL_IO_EXTEND);
285 cpl_propertylist_delete(h);
286
287 if (!cpl_errorstate_is_equal(prev_state)) {
288 cpl_msg_error("moo_loc", "Error for adding fibre table to %s (%d)",
289 self->filename, cpl_error_get_code());
290 status = cpl_error_get_code();
291 cpl_errorstate_set(prev_state);
292 }
293 return status;
294}
295/*----------------------------------------------------------------------------*/
304/*----------------------------------------------------------------------------*/
305cpl_table *
307{
308 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
309
310 if (self->fibre_table == NULL && self->filename != NULL) {
311 cpl_size qnum =
312 cpl_fits_find_extension(self->filename, MOO_FIBRES_TABLE_EXTNAME);
313 if (qnum > 0) {
314 self->fibre_table = cpl_table_load(self->filename, qnum, 0);
315 }
316 }
317 return self->fibre_table;
318}
319
320/*----------------------------------------------------------------------------*/
329/*----------------------------------------------------------------------------*/
330
331void
332moo_loc_delete(moo_loc *self)
333{
334 if (self != NULL) {
335 int i;
336 if (self->filename != NULL) {
337 cpl_free(self->filename);
338 }
339 if (self->primary_header != NULL) {
340 cpl_propertylist_delete(self->primary_header);
341 }
342 for (i = 0; i < 2; i++) {
343 if (self->ri[i] != NULL) {
344 moo_loc_single_delete(self->ri[i]);
345 }
346 if (self->yj[i] != NULL) {
347 moo_loc_single_delete(self->yj[i]);
348 }
349 if (self->h[i] != NULL) {
350 moo_loc_single_delete(self->h[i]);
351 }
352 }
353 if (self->fibre_table != NULL) {
354 cpl_table_delete(self->fibre_table);
355 }
356 cpl_free(self);
357 }
358}
359/*----------------------------------------------------------------------------*/
370/*----------------------------------------------------------------------------*/
371void
372moo_loc_save(moo_loc *self, const char *filename, int keep_points)
373{
374 if (self != NULL) {
375 cpl_propertylist_save(self->primary_header, filename, CPL_IO_CREATE);
376
377 int i;
378 for (i = 0; i < 2; i++) {
379 if (self->ri[i] != NULL) {
380 moo_loc_single_save(self->ri[i], filename, keep_points);
381 }
382 if (self->yj[i] != NULL) {
383 moo_loc_single_save(self->yj[i], filename, keep_points);
384 }
385 if (self->h[i] != NULL) {
386 moo_loc_single_save(self->h[i], filename, keep_points);
387 }
388 }
389
390 if (self->fibre_table != NULL) {
391 cpl_propertylist *h = cpl_propertylist_new();
392 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
393 MOO_FIBRES_TABLE_EXTNAME);
394 cpl_table_save(self->fibre_table, h, h, filename, CPL_IO_EXTEND);
395 cpl_propertylist_delete(h);
396 }
397 }
398}
399
400/*----------------------------------------------------------------------------*/
411/*----------------------------------------------------------------------------*/
412cpl_error_code
413moo_loc_dump(const moo_loc *self, FILE *stream)
414{
415 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
416 cpl_ensure_code(stream != NULL, CPL_ERROR_NULL_INPUT);
417
418 fprintf(stream, "---MOO_LOC\n");
419 fprintf(stream, "filename %s\n", self->filename);
420 int i;
421 for (i = 0; i < 2; i++) {
422 moo_loc_single_dump(self->ri[i], stream);
423 moo_loc_single_dump(self->yj[i], stream);
424 moo_loc_single_dump(self->h[i], stream);
425 }
426
427 return CPL_ERROR_NONE;
428}
429
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
void moo_loc_single_delete(moo_loc_single *self)
Delete a moo_loc_single.
void moo_loc_single_save(const moo_loc_single *self, const char *filename, int keep_points)
Save a moo_loc_single to a FITS file.
cpl_error_code moo_loc_single_dump(const moo_loc_single *self, FILE *stream)
Dump structural information of LOC_SINGLE.
moo_loc_single * moo_loc_single_create(const char *filename, const char *extname)
Create a new moo_loc_single from the given LOC filename.
moo_loc * moo_loc_create(const char *filename)
Create a new empty LOC filename.
Definition: moo_loc.c:83
moo_loc_single * moo_loc_get_single(moo_loc *self, moo_detector_type type, int ntas)
Get the type part in LOC and return it.
Definition: moo_loc.c:155
cpl_error_code moo_loc_add_fibre_table(moo_loc *self, cpl_table *fibre_table)
Add fibre table to LOC filename and update moo_loc structure.
Definition: moo_loc.c:271
moo_loc * moo_loc_new(void)
Create a new moo_loc.
Definition: moo_loc.c:66
cpl_error_code moo_loc_set_single(moo_loc *self, moo_detector_type type, int ntas, moo_loc_single *single)
assign moo_loc_single structure in moo_loc structure
Definition: moo_loc.c:188
moo_loc * moo_loc_load(const cpl_frame *locframe)
Load a LOC frame and create a moo_loc.
Definition: moo_loc.c:109
cpl_error_code moo_loc_add_single(moo_loc *self, moo_loc_single *single, moo_detector_type type, int ntas, int keep_points)
Add LOC_SINGLE extension to LOC filename and update moo_loc structure.
Definition: moo_loc.c:234
cpl_error_code moo_loc_dump(const moo_loc *self, FILE *stream)
Dump structural information of LOC.
Definition: moo_loc.c:413
cpl_table * moo_loc_get_fibre_table(moo_loc *self)
Get the FIBRE TABLE in LOC.
Definition: moo_loc.c:306
void moo_loc_save(moo_loc *self, const char *filename, int keep_points)
Save a moo_loc to a FITS file.
Definition: moo_loc.c:372
void moo_loc_delete(moo_loc *self)
Delete a moo_loc.
Definition: moo_loc.c:332