MUSE Pipeline Reference Manual  2.1.1
muse_table.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set sw=2 sts=2 et cin: */
3 /*
4  * This file is part of the MUSE Instrument Pipeline
5  * Copyright (C) 2016 European Southern Observatory
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 /*----------------------------------------------------------------------------*
28  * Includes *
29  *----------------------------------------------------------------------------*/
30 #include <cpl.h>
31 
32 #include "muse_table.h"
33 
34 #include "muse_pfits.h"
35 #include "muse_utils.h"
36 
37 /*----------------------------------------------------------------------------*/
45 /*----------------------------------------------------------------------------*/
48 /*----------------------------------------------------------------------------*/
61 /*----------------------------------------------------------------------------*/
62 muse_table *
64 {
65  return (muse_table *)cpl_calloc(1, sizeof(muse_table));
66 } /* muse_table_new() */
67 
68 /*----------------------------------------------------------------------------*/
78 /*----------------------------------------------------------------------------*/
79 void
81 {
82  if (!aTable) {
83  return;
84  }
85  cpl_table_delete(aTable->table);
86  cpl_propertylist_delete(aTable->header);
87  cpl_free(aTable);
88 } /* muse_table_delete() */
89 
90 /*----------------------------------------------------------------------------*/
104 /*----------------------------------------------------------------------------*/
105 muse_table *
106 muse_table_load(const char *aFilename, unsigned char aIFU)
107 {
108  cpl_ensure(aFilename, CPL_ERROR_NULL_INPUT, NULL);
109 
110  /* try to load the extension table for the given IFU */
111  int extension = muse_utils_get_extension_for_ifu(aFilename, aIFU);
112  /* other tables hopefully always have the data in the first extension */
113  if (extension <= 0) {
114  if (aIFU > 0) {
115  cpl_msg_debug(__func__, "Didn't find a specific extension for IFU %hhu, "
116  "will just use the first one.", aIFU);
117  }
118  extension = 1;
119  }
120  /* load the table, checking for invalid entries */
121  cpl_errorstate state = cpl_errorstate_get();
122  muse_table *table = muse_table_new();
123  table->table = cpl_table_load(aFilename, extension, 1);
124  if (!cpl_errorstate_is_equal(state) || !table->table ||
125  !cpl_table_get_nrow(table->table)) {
126  cpl_msg_info(__func__, "Loading table from file \"%s\" (ext %d) failed: %s",
127  aFilename, extension, cpl_error_get_message());
128  muse_table_delete(table);
129  return NULL;
130  }
131  /* load the primary FITS header; *
132  * If loading of the table above works, this is extremely unlikely to fail, so do ) */
133  table->header = cpl_propertylist_load(aFilename, 0);
134 
135  /* now output a message, use the relevant EXTNAME, if it exists */
136  cpl_propertylist *hext = cpl_propertylist_load(aFilename, extension);
137  state = cpl_errorstate_get();
138  const char *en = muse_pfits_get_extname(hext);
139  char *extname = NULL;
140  if (en && cpl_errorstate_is_equal(state)) {
141  extname = cpl_sprintf("[%s]", en);
142  /* add the EXTNAME in the table header component, if not existing yet */
143  if (!cpl_propertylist_has(table->header, "EXTNAME")) {
144  cpl_propertylist_append_string(table->header, "EXTNAME", en);
145  cpl_propertylist_set_comment(table->header, "EXTNAME",
146  cpl_propertylist_get_comment(hext, "EXTNAME"));
147  } /* if no extname yet */
148  } else {
149  cpl_errorstate_set(state);
150  extname = cpl_sprintf("%c", '\0');
151  }
152  cpl_msg_info(__func__, "Loaded table from file \"%s%s\" (ext %d).", aFilename,
153  extname, extension);
154  cpl_free(extname);
155  cpl_propertylist_delete(hext);
156 
157  return table;
158 } /* muse_table_load() */
159 
160 /*----------------------------------------------------------------------------*/
172 /*----------------------------------------------------------------------------*/
173 cpl_error_code muse_table_save(muse_table *aTable, const char *aFilename)
174 {
175  cpl_ensure_code(aTable && aFilename, CPL_ERROR_NULL_INPUT);
176 
177  cpl_error_code rc = cpl_table_save(aTable->table, aTable->header,
178  NULL, aFilename, CPL_IO_CREATE);
179  return rc;
180 } /* muse_table_save */
181 
const char * muse_pfits_get_extname(const cpl_propertylist *aHeaders)
find out the extension name
Definition: muse_pfits.c:205
int muse_utils_get_extension_for_ifu(const char *aFilename, unsigned char aIFU)
Return extension number that corresponds to this IFU/channel number.
Definition: muse_utils.c:118
cpl_error_code muse_table_save(muse_table *aTable, const char *aFilename)
Save the data and the FITS headers of a MUSE table to a file.
Definition: muse_table.c:173
muse_table * muse_table_new(void)
Allocate memory for a new muse_table object.
Definition: muse_table.c:63
cpl_table * table
The table.
Definition: muse_table.h:49
Structure to store a table together with a property list.
Definition: muse_table.h:43
void muse_table_delete(muse_table *aTable)
Deallocate memory associated to a muse_table object.
Definition: muse_table.c:80
cpl_propertylist * header
the header
Definition: muse_table.h:56
muse_table * muse_table_load(const char *aFilename, unsigned char aIFU)
Load a table file and its primary FITS header.
Definition: muse_table.c:106