File: vmextincttable.c

package info (click to toggle)
cpl-plugin-vimos 4.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 28,228 kB
  • sloc: ansic: 169,271; cpp: 16,177; sh: 4,344; python: 3,678; makefile: 1,138; perl: 10
file content (321 lines) | stat: -rw-r--r-- 7,276 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* $Id: vmextincttable.c,v 1.2 2013-03-25 11:43:04 cgarcia Exp $
 *
 * This file is part of the VIMOS Pipeline
 * Copyright (C) 2002-2004 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/*
 * $Author: cgarcia $
 * $Date: 2013-03-25 11:43:04 $
 * $Revision: 1.2 $
 * $Name: not supported by cvs2svn $
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <string.h>

#include <fitsio.h>

#include <pilmessages.h>
#include <cpl_msg.h>
#include <piltranslator.h>

#include "vmtable.h"
#include "vmextincttable.h"


/*
 * Column definitions for a spectral atmospheric extinction table.
 */

static const char *extinct_table_columns[] = {
  "WAVE",                /* Angstrom                                    */
  "EXTINCTION"           /* Atmospheric extinction in mag/airmass       */
};

static size_t nc = sizeof(extinct_table_columns) / sizeof(const char *);

/**
 * @name vmextinctttable Spectral Atmospheric Extinction Table
 *
 * @doc
 *   The module provides constructors for the creation of Spectral Atmospheric 
 *   Extinction table objects. These constructors are specialized versions 
 *   derived from the generic table constructor. No specialized version of a
 *   destructor is provided, since the generic table destructor is sufficient.
 */
 
/**@{*/

/**
 * @memo
 *   Create an empty spectral atmospheric extinction table object.
 *
 * @return The function returns a pointer to the created table object if
 *   no error occured, otherwise a #NULL# pointer is returned.
 *
 * @doc
 *   The function uses the generic table constructor to create a generic
 *   table object. The table name is set to #VM_ATMEXT# and this value is
 *   added to the list of descriptors with the descriptor name referenced
 *   by the alias 'Table'.
 *
 * @author R. Palsa, C. Izzo
 */

VimosTable *newExtinctTableEmpty()
{

  VimosTable *table = newTable();

  if (table) {

    /*
     * Set the table tag
     */

    strcpy(table->name, VM_ATMEXT);
    table->descs = newStringDescriptor(pilTrnGetKeyword("Table"), VM_ATMEXT,
				       "Type of table");
  }

  return table;

}


/**
 * @memo
 *   Create a spectral atmospheric extinction table object.
 *
 * @return The function returns a pointer to the created table object if
 *   no error occured, otherwise a #NULL# pointer is returned.
 *
 * @doc
 *   The function creates an empty spectral atmospheric extinction table using
 *   \textbf{newExtinctTableEmpty()} and adds the columns
 *   \begin{itemize}
 *     \item #WAVE#,
 *     \item #EXTINCTION#,
 *   \end{itemize}
 *   which define a spectral atmospheric extinction table, to this object. 
 *   The columns are created with \textbf{numRows} rows.
 *
 *
 * @see newExtinctTableEmpty
 *
 * @author R. Palsa
 */

VimosTable *newExtinctTable(size_t numRows)
{
 
  register size_t i;
 
  VimosTable *table = newExtinctTableEmpty();
 
  if (table) {
 
    /*
     * Create the spectral atmospheric extinction table columns.
     */
 
    for (i = 0; i < nc; i++) {
      if ((tblAppendColumn(table, newDoubleColumn(numRows, 
		           extinct_table_columns[i]))) == EXIT_FAILURE) {
        deleteTable(table);
        return 0;
      }
    }
    
  }
  
  return table;
 
}


/** 
 * @memo 
 *  Read spectral atmospheric extinction Table from FITS file
 *
 * @return VM_TRUE or VM_FALSE.
 *
 * @param extTable        Spectral Atmospheric Extinction Table
 * @param fptr            Pointer to FITS file 
 *
 * @doc 
 *   Read spectral atmospheric extinction Table from FITS file 
 *
 * @author R. Palsa, C. Izzo
 */

VimosBool readFitsExtinctTable(VimosTable *extTable, fitsfile *fptr)
{

  int status = 0;
  char     modName[] = "readFitsExtinctTable";
 

  /* 
   * Validate input 
   */

  if (extTable == NULL) {
    cpl_msg_error(modName, "NULL input table");
    return VM_FALSE;
  }

  if (fptr == NULL) {
    cpl_msg_error(modName, "NULL pointer to FITS file");
    return VM_FALSE;
  }
  
  if (strcmp(extTable->name, VM_ATMEXT) ) {
    cpl_msg_error(modName,"Invalid input table");
    return (VM_FALSE);
  }


  /* 
   * Open Table 
   */

  if (fits_movnam_hdu(fptr, BINARY_TBL, VM_ATMEXT, 0, &status)) {
    cpl_msg_error(modName, "The function fits_movnam_hdu has returned an "
		  "error (code %d)", status);
    return(VM_FALSE);
  }

  extTable->fptr = fptr;

   
  /* 
   * Read Table 
   */

  if (!readFitsTable(extTable, extTable->fptr)) {
    cpl_msg_info(modName, "Error in reading the FITS file");
    return VM_FALSE;
  }

  if (!checkExtinctTable(extTable)) {
    cpl_msg_error(modName, "Invalid spectral atmospheric extinction table");
    return VM_FALSE;
  }

  return VM_TRUE;
}


/** 
 * @memo 
 *  Check a spectral atmospheric extinction Table 
 *
 * @return VM_TRUE or VM_FALSE. 
 *
 * @param extTable    Spectral atmospheric extinction Table
 *
 * @doc 
 *   Check if all necessary descriptors and columns of the spectral 
 *   atmospheric extinction Table are present.
 *
 * @author P. Sartoretti
 */

VimosBool checkExtinctTable(VimosTable *extTable) 
{

  char     modName[]  = "checkExtinctTable";
  size_t   i;

  
  if (extTable == NULL) {
    cpl_msg_error(modName, "Null input table");
    return VM_FALSE;
  }

  if (strcmp(extTable->name, VM_ATMEXT)) {
    cpl_msg_error(modName, "Invalid input table");
    return (VM_FALSE);
  }
 
  for (i = 0; i < nc; i++) {
    if (findColInTab(extTable, extinct_table_columns[i]) == NULL) { 
      cpl_msg_error(modName, "Column %s not found", extinct_table_columns[i]);
      return VM_FALSE;
    }
  }

  return VM_TRUE;

}


/** 
 * @memo 
 *  Write spectral atmospheric extinction Table to FITS file 
 *
 * @return VM_TRUE or VM_FALSE.
 * 
 * @param filename   Name of the FITS file 
 * @param extTable   Spectral atmospheric extinction Table
 *
 * @doc 
 *   Write spectral atmospheric extinction Table to FITS file
 *
 * @author P. Sartoretti
 */

VimosBool writeFitsExtinctTable(char *filename, VimosTable *extTable)
{

  char modName[] = "writeFitsExtinctTable";

  
  if (extTable == NULL) {
    cpl_msg_error(modName, "Null input Table");
    return VM_FALSE;
  }
  
  if (strcmp(extTable->name, VM_ATMEXT)) {
    cpl_msg_error(modName, "Invalid input table");
    return VM_FALSE;
  }

  /* 
   * Check if descriptors and columns are there 
   */

  if (!checkExtinctTable(extTable)) {
    cpl_msg_error(modName, "Check on table failed: incomplete table");
    return VM_FALSE;
  }

  if (!createFitsTable(filename, extTable, VM_ATMEXT)) {
    cpl_msg_error(modName, "Error in writing FITS table");
    return VM_FALSE;
  }

  return VM_TRUE;

}

/**@}*/