File: dump.cpp

package info (click to toggle)
munipack 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 33,104 kB
  • sloc: cpp: 29,677; sh: 4,909; f90: 2,872; makefile: 278; python: 140; xml: 72; awk: 12
file content (214 lines) | stat: -rw-r--r-- 4,856 bytes parent folder | download | duplicates (2)
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
/*

  FITS dump utility


  Copyright © 2012, 2022 F.Hroch (hroch@physics.muni.cz)

  This file is part of Munipack.

  Munipack 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 3 of the License, or
  (at your option) any later version.

  Munipack 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 Munipack.  If not, see <http://www.gnu.org/licenses/>.


*/

#include "fits.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <stdlib.h>
#include <fitsio.h>

#include <cassert>

using namespace std;

int dump(const string& filename, const string& outname)
{
  fitsfile *f;
  int status = 0;
  int nhdu = 0;

  ofstream fout(outname.c_str());
  if( ! fout.good() ) {
    cerr << "Failed to write to `" << filename << "'" << endl;
    return 1;
  }

  status = 0;
  fits_open_file(&f, filename.c_str(), READONLY, &status);
  fits_get_num_hdus(f,&nhdu,&status);

  if( status ) goto hell;

  for(int k = 0; k < nhdu; k++) {

    fout << "# BEGIN HDU " << k << endl;

    int htype, dummy;

    fits_movabs_hdu(f,k+1,&htype,&status);
    if( status ) goto hell;

    int nhead;
    char record[FLEN_CARD];
    fits_get_hdrspace(f,&nhead,&dummy,&status);
    for(int n = 0; status == 0 && n < nhead; n++) {
      if( fits_read_record(f,n+1,record,&status) == 0 )
	fout << record << endl;
    }
    if( status ) goto hell;

    fout << "END" << endl;

    if( htype == IMAGE_HDU ) {

      int bitpix, naxis;

      fits_get_img_type(f,&bitpix,&status);
      fits_get_img_dim(f,&naxis,&status);

      if( naxis > 0 ) {

	long *naxes = new long[naxis];
	fits_get_img_size(f,naxis,naxes,&status);
	if( status ) { delete[] naxes; goto hell; }

	long ndata = 1; for(int i = 1; i < naxis; i++ ) ndata = ndata*naxes[i];
	assert(ndata > 0);

	if( bitpix > 0 ) {

	  int nullval = 0;
	  int *data = new int[ndata];
	  long fpixel = 1;
	  for(int j = 0; j < naxes[0] && status == 0; j++) {
	    fits_read_img(f,TINT,fpixel,ndata,&nullval,data,&dummy,&status);
	    if( status == 0 ) {
	      fpixel = fpixel + ndata;
	      for(int i = 0; i < ndata; i++)
		fout << " " << data[i];
	      fout << endl;
	    }
	  }
	  delete[] data;
	  if( status ) goto hell;
	}
	else {

	  double nullval = 0;
	  double *data = new double[ndata];
	  long fpixel = 1;
	  for(int j = 0; j < naxes[0] && status == 0; j++) {
	    fits_read_img(f,TDOUBLE,fpixel,ndata,&nullval,data,&dummy,&status);
	    if( status == 0 ) {
	      fpixel = fpixel + ndata;
	      for(int i = 0; i < ndata; i++)
		fout << " " << data[i];
	      fout << endl;
	    }
	  }
	  delete[] data;
	  if( status ) goto hell;

	}


	delete[] naxes;
      }

    }
    else if( htype == ASCII_TBL || htype == BINARY_TBL ) {

      long nrows;
      int ncols;
      fits_get_num_rows(f,&nrows,&status);
      fits_get_num_cols(f,&ncols,&status);

      if( status ) goto hell;

      int *widths = new int[ncols];
      int *types = new int[ncols];
      long repeat, widths2;
      for(int k = 0; k < ncols && status == 0; k++) {
	fits_get_coltype(f,k+1,&types[k],&repeat,&widths2,&status);
	fits_get_col_display_width(f,k+1,&widths[k],&status);
      }


      if( status ) { delete[] widths; delete[] types; goto hell;}

      char **table = new char*[ncols*nrows];
      for(int k = 0; k < ncols; k++)
	for(int i = 0; i < nrows; i++)
	  table[i+k*nrows] = new char[widths[k]];


      long frow = 1, felem = 1; char *nullval = 0; int dummy;
      for(int k = 0; k < ncols && status == 0; k++ )
	fits_read_col(f, TSTRING, k+1, frow, felem, nrows, &nullval,
		      table + k*nrows,&dummy,&status);

      if( status == 0 ) {

	for(int i = 0; i < nrows; i++) {
	  for(int k = 0; k < ncols; k++) {
	    int b = widths[k];
	    char *cell = table[i+nrows*k];
	    char c[2*b+3];
	    if( types[k] == TSTRING ) {
	      c[0] = '\'';
	      int l = 0;
	      for(l = 0; l != b && cell[l] != '\0'; l++) {
		if( cell[l] == '\'' ) {
		  c[l++] = '\'';
		  c[l+1] = '\'';
		}
		else
		  c[l+1] = cell[l];
	      }
	      c[l+1] = '\'';
	      c[l+2] = '\0';
	    }
	    else {
	      strncpy(c,cell,b);
	      c[b] = '\0';
	    }
	    fout << c << " ";

	  }
	  fout << endl;
	}
      }

      for(int i = 0; i < ncols*nrows; i++)
	delete[] table[i];
      delete[] table;
      delete[] widths;
      delete[] types;

    }

    fout << "# END HDU " << k << endl;
  }

 hell:

  fits_report_error(stderr, status);
  fits_close_file(f, &status);
  return status;
}