File: vmlinecatalog.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 (425 lines) | stat: -rw-r--r-- 12,594 bytes parent folder | download | duplicates (4)
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* $Id: vmlinecatalog.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 <pilmemory.h>
#include <pilmessages.h>
#include <cpl_msg.h>

#include "vmtable.h"
#include "vmlinecatalog.h"
#include "cpl.h"


/*
  Allocate a new line Catalog. This is a VimosTable with name VM_LIN (which
  should have value "IN").
  No descriptors or columns are allocated (but this may change in a later
  version).
*/
VimosTable *newLineCatalog()
{
  VimosTable *newTab;
  
  /* allocate new VimosTable */
  newTab = newTable();

  /* check if new VimosTable was allocated */
  if (newTab == NULL) {
    cpl_msg_error("newLineCatalog","The function newTable has returned NULL");
    return(NULL);
  }
  
  /* copy "LIN" into name of table */
  strcpy(newTab->name, VM_LIN);
  newTab->descs = newStringDescriptor("ESO PRO TABLE", VM_LIN, "");
  if (newTab->descs == NULL) {
    /* cleanup */
    cpl_free(newTab);
    cpl_msg_error("newLineCatalog","The function newStringDescriptor has returned NULL");
    return(NULL);
  }

  /* should create all the descriptors and columns here ? */
  
  /* should create all the descriptors and columns here ?*/
  
  /* return address of new Line Catalog */
  return(newTab);
  
}

/*
  Delete a Line Catalog. This is just an esthetic wrapper for 
  deleteTable(lCat)
*/
void deleteLineCatalog(VimosTable *lCat)
{
  /* just a wrapper for deleteTabel() */
  deleteTable(lCat);
}

VimosBool readFitsLineCatalog(VimosTable *linCat, fitsfile *fptr)
{
  int    i;
  int    nCols;
  int    nRows;
  int    null;
  int    wlenColNo, nameColNo, fluxColNo, commColNo;
  int    status, nfound, ii;
  long   lenName, lenComm, width;
  int    typecode;
  char **ttype;
  char   comment[80];
  char   nstr[] = {'\0'};

  VimosColumn *wlenCol;
  VimosColumn *nameCol;
  VimosColumn *fluxCol;
  VimosColumn *commCol;

  char         modName[] = "readFitsLineCatalog";

  status = 0;

  /* validate input */
  if (linCat == NULL) {
    cpl_msg_debug(modName, "NULL input table");
    return(VM_FALSE);
  }
  /* validate input */
  if ( strcmp(linCat->name, VM_LIN) ) {
    cpl_msg_debug(modName, "Invalid input table");
    return(VM_FALSE);
  }
  
  /* open Table */
  if (fits_movnam_hdu(fptr, BINARY_TBL, "LIN", 0, &status)) {
    cpl_msg_debug(modName, 
    "The function fits_movnam_hdu returned error code %d", status);
    return(VM_FALSE);
  }
  
  linCat->fptr = fptr;
  
  /* read Table */
  if (!readDescsFromFitsTable(&(linCat->descs), linCat->fptr)) {
    cpl_msg_debug(modName, "Function readDescsFromFitsTable returned an error");
    return(VM_FALSE);
  }

  if (!readIntDescriptor(linCat->descs, "TFIELDS", &nCols, comment)) {
    cpl_msg_debug(modName, "The function readIntDescriptor returned an error");
    return(VM_FALSE);
  }

  if (!readIntDescriptor(linCat->descs, "NAXIS2", &nRows, comment)) {
    cpl_msg_debug(modName, "The function readIntDescriptor returned an error");
    return(VM_FALSE);
  }

  /* allocate space for the column labels */
  ttype = (char **) cpl_malloc(nCols*sizeof(char *));
  for (ii = 0; ii < nCols; ii++) {
    ttype[ii] = (char *) cpl_malloc(FLEN_VALUE*sizeof(char));
    /* check if space was allocated */
    if (ttype[ii] == NULL) {
      cpl_msg_debug(modName, "Allocation Error");
      return(VM_FALSE);
    }
  }

  /* read the column names from the TTYPEn keywords */
  if (fits_read_keys_str(linCat->fptr, "TTYPE", 1, nCols, ttype, &nfound, 
			 &status)) {
    cpl_msg_debug(modName, 
    "Function fits_read_keys_str returned error code %d", status);
    return(VM_FALSE);
  }
  if (fits_get_colnum(linCat->fptr,CASEINSEN,"WLEN",&wlenColNo,&status)) {
    cpl_msg_debug(modName, 
    "The function fits_get_colnum returned error code %d", status);
    return(VM_FALSE);
  }
  if (fits_get_colnum(linCat->fptr,CASEINSEN,"NAME",&nameColNo,&status)) {
    cpl_msg_debug(modName, 
    "The function fits_get_colnum returned error code %d", status);
    return(VM_FALSE);
  }
  if (fits_get_coltype(linCat->fptr, nameColNo, 
                       &typecode, &lenName, &width, &status)) {
    cpl_msg_debug(modName,
    "The function fits_get_coltype returned error code %d", status);
    return(VM_FALSE);
  }
  if (fits_get_colnum(linCat->fptr,CASEINSEN,"FLUX",&fluxColNo,&status)) {
    cpl_msg_debug(modName, 
    "The function fits_get_colnum returned error code %d", status);
    return(VM_FALSE);
  }
  if (fits_get_colnum(linCat->fptr,CASEINSEN,"COMMENT",&commColNo,&status)) {
    cpl_msg_debug(modName, 
    "Function fits_get_colnum returned error code %d", status);
    return(VM_FALSE);
  }
  if (fits_get_coltype(linCat->fptr, commColNo, 
                       &typecode, &lenComm, &width, &status)) {
    cpl_msg_debug(modName,
    "The function fits_get_coltype returned error code %d", status);
    return(VM_FALSE);
  }
  
  /* create column for wavelengths */
  wlenCol = newFloatColumn(nRows, "WLEN");
  if (wlenCol == NULL) {
    cpl_msg_debug(modName, "The function newFloatColumn has returned NULL");
    return(VM_FALSE);
  }

  /* put column in Table */
  linCat->cols = wlenCol;
  
  /* create column for NAME */
  nameCol = newStringColumn(nRows, "NAME");
  if (nameCol == NULL) {
    cpl_msg_debug(modName, "The function newStringColumn has returned NULL");
    return(VM_FALSE);
  }

  /* link column into table */
  wlenCol->next = nameCol;
  nameCol->prev = wlenCol;
  
  /* create column for FLUX */
  fluxCol = newFloatColumn(nRows, "FLUX");
  if (fluxCol == NULL) {
    cpl_msg_debug(modName, "The function newFloatColumn has returned NULL");
    return(VM_FALSE);
  }
  /* link column into table */
  nameCol->next = fluxCol;
  fluxCol->prev = nameCol;
  
  /* create column for COMMENT */
  commCol = newStringColumn(nRows, "COMMENT");
  if (commCol == NULL) {
    cpl_msg_debug(modName, "The function newStringColumn has returned NULL");
    return(VM_FALSE);
  }
  /* link column into table */
  fluxCol->next = commCol;
  commCol->prev = fluxCol;

  for (i = 1; i <= nRows; i++) {
      if (fits_read_col_flt(linCat->fptr,wlenColNo,i,1,1,null,
			    &(wlenCol->colValue->fArray[i-1]),&null,&status)){
	cpl_msg_debug(modName, 
        "Function fits_read_col_flt returned error code %d", status);
	return(VM_FALSE);
      } 
      
      nameCol->colValue->sArray[i-1] = 
                           (char *) cpl_malloc(((int)lenName + 1)*sizeof(char));

      if (fits_read_col_str(linCat->fptr,nameColNo,i,1,1,nstr,
			    &(nameCol->colValue->sArray[i-1]),&null,&status)){
	cpl_msg_debug(modName, 
        "Function fits_read_col_str returned error code %d", status);
	return(VM_FALSE);
      }  

    /* just to be sure ... */
    nameCol->colValue->sArray[i-1][lenName] = '\0';   
    if (fits_read_col_flt(linCat->fptr,fluxColNo,i,1,1,null,
			  &(fluxCol->colValue->fArray[i-1]),&null,&status)) {
      cpl_msg_debug(modName, 
      "Function fits_read_col_flt returned error code %d", status);
      return(VM_FALSE);
    }   

    commCol->colValue->sArray[i-1] = 
                         (char *) cpl_malloc(((int)lenComm + 1)*sizeof(char));

    if (fits_read_col_str(linCat->fptr,commColNo,i,1,1,nstr,
			  &(commCol->colValue->sArray[i-1]),&null,&status)) {
      cpl_msg_debug(modName, 
      "The function fits_read_col_str returned error code %d", status);
      return(VM_FALSE);
    }    
 
    commCol->colValue->sArray[i-1][lenComm] = '\0';
  }
   
  for (i = 0; i < 3; i++){     /* free the memory for the column labels */
    cpl_free( ttype[i] );
  }

  return (VM_TRUE);  
}



VimosBool writeFitsLineCatalog(VimosTable *linCat, fitsfile *fptr)
{
  int nRows;
  int i;
  int status,nbytes;
  char *ttype[84], *tform[84];

  VimosColumn *wlenCol;
  VimosColumn *nameCol;
  VimosColumn *fluxCol;
  VimosColumn *commCol;

  char         modName[] = "writeFitsLineCatalog";
  
  /* validate input */
  if (linCat == NULL) {
    cpl_msg_debug(modName, "NULL input table");
    return (VM_FALSE);
  }
  if ( strcmp(linCat->name, VM_LIN) ) {
    cpl_msg_debug(modName, "Invalid input table");
    return (VM_FALSE);
  }

  nRows = linCat->cols->len;
  linCat->fptr = fptr;
  status = 0;

  /* if Table is already present, first remove it  */
  if (!fits_movnam_hdu(fptr, BINARY_TBL, "LIN", 0, &status) ) {
    if(fits_delete_hdu(fptr, NULL, &status)) {
      cpl_msg_debug(modName, 
      "Function fits_delete_hdu returned error code %d", status);
      return (VM_FALSE);
    }
  } else {
    status = 0;
  }

  for (i = 0; i <= 3; i++){      /* allocate space for the column labels */
    ttype[i] = (char *) cpl_malloc(FLEN_VALUE);  /* max label length = 69 */
    /* check if space was allocated */
    if (ttype[i] == NULL) {
      cpl_msg_debug(modName, "Allocation error");
      return(VM_FALSE);
    }
    tform[i] = (char *) cpl_malloc(FLEN_VALUE);
    /* check if space was allocated */
    if (tform[i] == NULL) {
      cpl_msg_debug(modName, "Allocation error");
      return(VM_FALSE);
    }
  }

  ttype[0] = "WLEN";
  tform[0] = "1E";
  ttype[1]="NAME";
  tform[1] = "9A";
  ttype[2]="FLUX"; 
  tform[2] = "1E";
  ttype[3] = "COMMENT";
  tform[3] = "20A";

  /* append a new empty binary table onto the FITS file */
  if (fits_create_tbl(fptr, BINARY_TBL,0, 4, ttype, tform,NULL, 
		      "LIN", &status)) {
    cpl_msg_debug(modName, 
    "Function fits_create_tbl returned error code %d", status);
    return(VM_FALSE);
  }
  if (fits_movnam_hdu(fptr, BINARY_TBL, "LIN", 0, &status)) {
    cpl_msg_debug(modName, 
    "Function fits_movnam_hdu returned error code %d", status);
    return(VM_FALSE);
  }

  /* write the table descriptors to the Fits file */
  
  if (fits_read_key (linCat->fptr,TINT,"NAXIS1",&nbytes,NULL,&status)) {
    cpl_msg_debug(modName, 
    "Function fits_read_key returned error code %d", status);
    return (VM_FALSE);
  }
  if (!writeIntDescriptor(&(linCat->descs), "NAXIS1", nbytes, "")) {
    cpl_msg_debug(modName, "Function writeIntDescriptor has returned an error");
    return (VM_FALSE);
  }
  if (!writeIntDescriptor(&(linCat->descs), "NAXIS2", nRows, "")) {
    cpl_msg_debug(modName, "Function writeIntDescriptor has returned an error");
    return (VM_FALSE);
  }
  if (!writeIntDescriptor(&(linCat->descs),"TFIELDS",4, "")) {
    cpl_msg_debug(modName, "Function writeIntDescriptor has returned an error");
    return (VM_FALSE);
  } 
  if (!writeDescsToFitsTable(linCat->descs, linCat->fptr)) {
    cpl_msg_debug(modName, "Function writeDescsToFitsTable returned an error");
    return(VM_FALSE);
  }

  wlenCol = linCat->cols;
  nameCol = wlenCol->next;
  fluxCol = nameCol->next;
  commCol = fluxCol->next;
  
  for (i = 1; i <= nRows; i++) {
    if (fits_write_col_flt(linCat->fptr,1,i,1,1,
		&(wlenCol->colValue->fArray[i-1]),&status)) {
      cpl_msg_debug(modName, 
      "Function fits_write_col_flt returned error code %d", status);
      return(VM_FALSE);
    }
    if (fits_write_col_str(linCat->fptr,2,i,1,1,
		&(nameCol->colValue->sArray[i-1]),&status)) {
      cpl_msg_debug(modName, 
      "Function fits_write_col_str returned error code %d", status);
      return(VM_FALSE);
    }
    if (fits_write_col_flt(linCat->fptr,3,i,1,1,
		&(fluxCol->colValue->fArray[i-1]),&status)) {
      cpl_msg_debug(modName, 
      "Function fits_write_col_flt returned error code %d", status);
      return(VM_FALSE);
    }
    if(fits_write_col_str(linCat->fptr,4,i,1,1,
		&(commCol->colValue->sArray[i-1]),&status)) {
      cpl_msg_debug(modName, 
      "Function fits_write_col_str returned error code %d", status);
      return(VM_FALSE);
    }
  }  
  return(VM_TRUE);
}