38#include <libdap/DDS.h>
39#include <libdap/Structure.h>
40#include <libdap/Str.h>
41#include <libdap/Array.h>
42#include <libdap/Byte.h>
43#include <libdap/Int16.h>
44#include <libdap/Int32.h>
45#include <libdap/Float32.h>
46#include <libdap/Float64.h>
47#include <libdap/Error.h>
48#include <libdap/mime_util.h>
54#include "fits_read_descriptors.h"
55#include "BESAutoPtr.h"
60void fits_handler::process_status(
int status,
string &error)
63 fits_report_error(stderr, status);
65 char error_description[30] =
"";
66 fits_get_errstatus(status, error_description);
67 error = error_description;
73fits_handler::ltoa(
long val,
79 if (base > 36 || base < 2)
86 r = ldiv(labs(val), base);
91 buf = ltoa(r.quot, buf, base);
94 *buf++ =
"0123456789abcdefghijklmnopqrstuvwxyz"[(
int) r.rem];
100bool fits_handler::fits_read_descriptors(DDS &dds,
const string &filename,
string &error)
104 if (fits_open_file(&fptr, filename.c_str(), READONLY, &status)) {
105 error =
"Can not open fits file ";
110 dds.set_dataset_name(name_path(filename));
113 for (
int ii = 1; !(fits_movabs_hdu(fptr, ii, &hdutype, &status)); ii++) {
120 status = process_hdu_image(fptr, dds, hdu.str(), hdu.str() +
"_IMAGE");
121 process_status(status, error);
124 status = process_hdu_ascii_table(fptr, dds, hdu.str(), hdu.str() +
"_ASCII");
125 process_status(status, error);
133 process_status(status, error);
137 process_status(1, error);
142 if (status == END_OF_FILE)
145 process_status(status, error);
146 fits_close_file(fptr, &status);
149 if (fits_close_file(fptr, &status)) {
150 process_status(status, error);
157template<
class T,
class U>
158static int read_hdu_image_data(
int fits_type, fitsfile *fptr,
const string &varname,
const string &datasetname,
int number_axes,
const vector<long> &naxes, Structure *container)
160 unique_ptr<T> in(
new T(varname, datasetname));
161 unique_ptr<Array> arr(
new Array(varname, datasetname, in.get()));
163 for (
register int w = 0; w < number_axes; w++) {
166 arr->append_dim(naxes[w], oss.str());
168 npixels = npixels * naxes[w];
170 dods_byte nullval = 0;
171 vector<U> buffer(npixels);
172 int anynull, status = 0;
175 fits_read_img(fptr, fits_type, 1 , npixels, &nullval, buffer.data(), &anynull, &status);
176 arr->set_value(buffer.data(), npixels);
177 container->add_var(arr.get());
182int fits_handler::process_hdu_image(fitsfile *fptr, DDS &dds,
const string &hdu,
const string &str)
184 string datasetname = dds.get_dataset_name();
186 unique_ptr<Structure> container(
new Structure(hdu, datasetname));
190 if (fits_get_hdrpos(fptr, &nkeys, &keypos, &status))
193 for (
int jj = 1; jj <= nkeys; jj++) {
194 char name[FLEN_KEYWORD];
195 char value[FLEN_VALUE];
196 char comment[FLEN_COMMENT];
197 if (fits_read_keyn(fptr, jj, name, value, comment, &status))
return status;
200 keya <<
"key_" << jj;
201 unique_ptr<Structure> st(
new Structure(keya.str(), datasetname));
203 unique_ptr<Str> s1(
new Str(
"name", datasetname));
207 unique_ptr<Str> s2(
new Str(
"value", datasetname));
211 unique_ptr<Str> s3(
new Str(
"comment", datasetname));
215 st->add_var(s1.get());
216 st->add_var(s2.get());
217 st->add_var(s3.get());
218 container->add_var(st.get());
221 char value[FLEN_VALUE];
222 if (fits_read_keyword(fptr,
"BITPIX", value, NULL, &status))
224 int bitpix = atoi(value);
226 if (fits_read_keyword(fptr,
"NAXIS", value, NULL, &status))
228 int number_axes = atoi(value);
230 vector<long> naxes(number_axes);
232 if (fits_read_keys_lng(fptr,
"NAXIS", 1, number_axes, naxes.data(), &nfound, &status))
237 status = read_hdu_image_data<Byte,dods_byte>(TBYTE, fptr, str, datasetname, number_axes, naxes, container.get());
241 status = read_hdu_image_data<Int16,dods_int16>(TSHORT, fptr, str, datasetname, number_axes, naxes, container.get());
263 status = read_hdu_image_data<Int32,dods_int32>(TINT, fptr, str, datasetname, number_axes, naxes, container.get());
267 status = read_hdu_image_data<Float32,dods_float32>(TFLOAT, fptr, str, datasetname, number_axes, naxes, container.get());
271 status = read_hdu_image_data<Float64,dods_float64>(TDOUBLE, fptr, str, datasetname, number_axes, naxes, container.get());
279 dds.add_var(container.get());
286int fits_handler::process_hdu_ascii_table(fitsfile *fptr, DDS &dds,
const string &hdu,
const string &str)
288 string datasetname = dds.get_dataset_name();
289 unique_ptr<Structure> container(
new Structure(hdu, datasetname));
295 char name[FLEN_KEYWORD];
296 char value[FLEN_VALUE];
297 char comment[FLEN_COMMENT];
300 if (fits_get_hdrpos(fptr, &nkeys, &keypos, &status))
303 for (
int jj = 1; jj <= nkeys; jj++) {
304 if (fits_read_keyn(fptr, jj, name, value, comment, &status))
308 unique_ptr<Structure> st(
new Structure(oss.str(), datasetname));
309 unique_ptr<Str> s1(
new Str(
"name", datasetname));
312 unique_ptr<Str> s2(
new Str(
"value", datasetname));
315 unique_ptr<Str> s3(
new Str(
"comment", datasetname));
318 st->add_var(s1.get());
319 st->add_var(s2.get());
320 st->add_var(s3.get());
321 container->add_var(st.get());
324 fits_get_num_rows(fptr, &nrows, &status);
325 fits_get_num_cols(fptr, &ncols, &status);
334 for (
int j = 0; j < ncols; j++) {
335 (ttype_autoptr.get())[j].reset();
336 (ttype_autoptr.get())[j].set(
new char[FLEN_VALUE],
true);
344 char **ttype =
new char*[ncols];
354 for (
int j = 0; j < ncols; j++)
355 ttype[j] = (ttype_autoptr.get())[j].get();
359 if (fits_read_keys_str(fptr,
"TTYPE", 1, ncols, ttype, &nfound, &status))
365 unique_ptr<Structure> table(
new Structure(str, datasetname));
367 for (
int h = 0; h < ncols; h++) {
370 fits_get_coltype(fptr, h + 1, &typecode, &repeat, &width, &status);
375 unique_ptr<Str> in(
new Str(ttype[h], datasetname));
376 unique_ptr<Array> arr(
new Array(ttype[h], datasetname, in.get()));
377 arr->append_dim(nrows);
378 char strnull[10] =
"";
379 char **name =
new char*[nrows];
384 for (p = 0; p < nrows; p++) {
386 name[p] =
new char[width + 1];
388 (sa3.get())[p].reset();
390 (sa3.get())[p].set(name[p],
true);
392 fits_read_col(fptr, typecode, h + 1, 1, 1, nrows, strnull, name, &anynull, &status);
394 string *strings =
new string[nrows];
398 for (
int p = 0; p < nrows; p++)
399 strings[p] = name[p];
400 arr->set_value(strings, nrows);
402 table->add_var(arr.get());
408 arr->append_dim(nrows);
409 dods_int16 nullval = 0;
411 fits_read_col(fptr, typecode, h + 1, 1, 1, nrows, &nullval, buffer.get(), &anynull, &status);
412 arr->set_value(buffer.get(), nrows);
413 table->add_var(arr.get());
419 arr->append_dim(nrows);
420 dods_int32 nullval = 0;
422 fits_read_col(fptr, TINT, h + 1, 1, 1, nrows, &nullval, buffer.get(), &anynull, &status);
423 arr->set_value(buffer.get(), nrows);
424 table->add_var(arr.get());
430 arr->append_dim(nrows);
431 dods_float32 nullval = 0;
433 fits_read_col(fptr, typecode, h + 1, 1, 1, nrows, &nullval, buffer.get(), &anynull, &status);
434 arr->set_value(buffer.get(), nrows);
435 table->add_var(arr.get());
441 arr->append_dim(nrows);
442 dods_float64 nullval = 0;
444 fits_read_col(fptr, typecode, h + 1, 1, 1, nrows, &nullval, buffer.get(), &anynull, &status);
445 arr->set_value(buffer.get(), nrows);
446 table->add_var(arr.get());
451 container->add_var(table.get());
452 dds.add_var(container.get());
457int fits_handler::process_hdu_binary_table(fitsfile *, DDS &)