bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
fits_read_attributes.cc
1// fits_read_attributes.cc
2
3// This file is part of fits_handler, a data handler for the OPeNDAP data
4// server.
5
6// Copyright (c) 2004,2005 University Corporation for Atmospheric Research
7// Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or (at your option) any later version.
13//
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// You can contact University Corporation for Atmospheric Research at
24// 3080 Center Green Drive, Boulder, CO 80301
25
26// (c) COPYRIGHT University Corporation for Atmostpheric Research 2004-2005
27// Please read the full copyright statement in the file COPYRIGHT_UCAR.
28//
29// Authors:
30// pwest Patrick West <pwest@ucar.edu>
31// jgarcia Jose Garcia <jgarcia@ucar.edu>
32
33// #include <stdlib.h>
34// #include <ctype.h>
35
36#include <string>
37#include <sstream>
38
39#include <fitsio.h>
40
41#include <libdap/AttrTable.h>
42#include <libdap/DAS.h>
43
44#include "fits_read_attributes.h"
45
46using namespace libdap;
47using namespace std;
48
49bool fits_handler::fits_read_attributes(DAS &das, const string &filename, string &error)
50{
51 fitsfile *fptr;
52 int status = 0;
53 if (fits_open_file(&fptr, filename.c_str(), READONLY, &status)) {
54 error = "Can not open fits file ";
55 error += filename;
56 return false;
57 }
58
59 int hdutype;
60 for (int ii = 1; !(fits_movabs_hdu(fptr, ii, &hdutype, &status)); ii++) {
61 AttrTable *at = new AttrTable();
62
63 /* get no. of keywords */
64 int nkeys, keypos;
65 if (fits_get_hdrpos(fptr, &nkeys, &keypos, &status)) {
66 fits_close_file(fptr, &status);
67 return false;
68 }
69
70 for (int jj = 1; jj <= nkeys; jj++) {
71 char name[FLEN_KEYWORD];
72 char value[FLEN_VALUE];
73 char comment[FLEN_COMMENT];
74
75 if (fits_read_keyn(fptr, jj, name, value, comment, &status)) {
76 fits_close_file(fptr, &status);
77 return false;
78 }
79#if 0
80 // name, ..., will always be true
81 string s_name = "";
82 if (name)
83 s_name = name;
84
85 string s_value = "";
86 if (value)
87 s_value = value;
88
89 string s_comment = "";
90 if (comment)
91 s_comment = comment;
92#endif
93 string s_name = name;
94 if (s_name.empty()/* == ""*/) {
95 ostringstream oss;
96 oss << "key_" << jj;
97 s_name = oss.str();
98#if 0
99 char tmp[100];
100 ltoa(jj, tmp, 10);
101 s_name = (string) "key_" + tmp;
102#endif
103 }
104
105 {
106 string com ;
107 // No need to add quote here.
108 // libdap4 will add the quote when generating DAS.
109 // KY 2022-08-25
110#if 0
111 //string com = "\"";
112#endif
113 com.append(value);
114 com.append(" / ");
115 com.append(comment);
116#if 0
117 //com.append("\"");
118#endif
119 at->append_attr(s_name, "String"/*type*/, com);
120 }
121
122 }
123#if 0
124 string str = "HDU_";
125 char tmp[100];
126 ltoa(ii, tmp, 10);
127 str.append(tmp);
128#endif
129 ostringstream oss;
130 oss << "HDU_" << ii;
131 das.add_table(oss.str(), at);
132 }
133
134 if (status == END_OF_FILE) /* status values are defined in fitsioc.h */
135 status = 0; /* got the expected EOF error; reset = 0 */
136 else {
137 /* got an unexpected error */
138 fits_close_file(fptr, &status);
139 return false;
140 }
141
142 if (fits_close_file(fptr, &status))
143 return false;
144
145 return true;
146}
147