load_text.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2009 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/Base/Formats/PSRFITS/load_text.h
10
11#ifndef __load_text_h
12#define __load_text_h
13
14#include "FITSError.h"
15#include "FilePtr.h"
16
17#include <fitsio.h>
18#include <string.h>
19#include <vector>
20#include <iostream>
21
22template<class T>
23void load_text (fitsfile* fptr, const char* table, const char* column,
24 T* instance, bool verbose = false)
25{
26 // Move to the HDU named table
27 int status = 0;
28 fits_movnam_hdu (fptr, BINARY_TBL, const_cast<char*>(table), 0, &status);
29
30 if (status)
31 throw FITSError (status, "unload_text",
32 "fits_movnam_hdu %s", table);
33
34 long numrows = 0;
35 fits_get_num_rows (fptr, &numrows, &status);
36
37 if (status)
38 throw FITSError (status, "load_text",
39 "fits_get_num_rows %s", table);
40
41 if (!numrows)
42 throw Error (InvalidParam, "load_text", "no rows in table");
43
44 int colnum = 0;
45 fits_get_colnum (fptr, CASEINSEN, const_cast<char*>(column),
46 &colnum, &status);
47
48 int typecode = 0;
49 long repeat = 0;
50 long width = 0;
51
52 fits_get_coltype (fptr, colnum, &typecode, &repeat, &width, &status);
53
54 if (typecode != TSTRING)
55 throw Error (InvalidState, "load_text",
56 "%s typecode != TSTRING", column);
57
58 std::vector<char> text (repeat);
59 char* temp = &(text[0]);
60
61 FilePtr stream = tmpfile();
62
63 for (int row=1; row <= numrows; row++)
64 {
65 int anynul = 0;
66 fits_read_col (fptr, TSTRING, colnum, row, 1, 1, 0,
67 &temp, &anynul, &status);
68
69 if (status)
70 throw FITSError (status, "load_text", "fits_read_col");
71
72 char* newline = strchr (temp, '\n');
73 if (newline)
74 *newline = '\0';
75
76 fprintf (stream, "%s\n", temp);
77 }
78
79 fseek (stream, 0, SEEK_SET);
80
81 instance->load (stream);
82
83 if (verbose)
84 {
85 std::cerr << "load_text loaded" << std::endl;
86 instance->unload (stderr);
87 }
88}
89
90#endif

Generated using doxygen 1.14.0