File: tfitshdr.c

package info (click to toggle)
wcslib 7.12%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,612 kB
  • sloc: ansic: 34,618; lex: 9,328; fortran: 6,731; sh: 3,367; sed: 497; pascal: 190; makefile: 15
file content (285 lines) | stat: -r--r--r-- 7,840 bytes parent folder | download
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
/*============================================================================
  WCSLIB 7.12 - an implementation of the FITS WCS standard.
  Copyright (C) 1995-2022, Mark Calabretta

  This file is part of WCSLIB.

  WCSLIB is free software: you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free
  Software Foundation, either version 3 of the License, or (at your option)
  any later version.

  WCSLIB 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 Lesser General Public License for
  more details.

  You should have received a copy of the GNU Lesser General Public License
  along with WCSLIB.  If not, see http://www.gnu.org/licenses.

  Author: Mark Calabretta, Australia Telescope National Facility, CSIRO.
  http://www.atnf.csiro.au/people/Mark.Calabretta
  $Id: tfitshdr.c,v 7.12 2022/09/09 04:57:58 mcalabre Exp $
*=============================================================================
*
* tfitshdr tests fitshdr(), the FITS parser for image headers, by reading a
* test header and printing the resulting fitskey structs.
*
* Input comes from file 'fitshdr.fits' using either fits_hdr2str() from
* CFITSIO if the DO_CFITSIO preprocessor is defined, or read directly using
* fgets() otherwise.
*
* wcshdr() is called first to extract all WCS-related keyrecords from the
* input header before passing it on to fitshdr().
*
*---------------------------------------------------------------------------*/

#include <wcsconfig_tests.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined HAVE_CFITSIO && defined DO_CFITSIO
#include <fitsio.h>
#endif

#include <fitshdr.h>
#include <wcshdr.h>
#include <wcsutil.h>

int main()

{
  char infile[] = "fitshdr.fits";
  char text[80];
  int  i, j, k, nkeyrec, nkeyids, nreject, status;
  struct fitskey *keys, *kptr;
  struct fitskeyid keyids[8];
#if defined HAVE_CFITSIO && defined DO_CFITSIO
  char *header;
  fitsfile *fptr;
#else
  char keyrec[81], header[288001];
  int  end;
  FILE *fptr;
#endif

  struct wcsprm *wcs;
  int  ctrl, nwcs, relax;


  // Set line buffering in case stdout is redirected to a file, otherwise
  // stdout and stderr messages will be jumbled (stderr is unbuffered).
  setvbuf(stdout, NULL, _IOLBF, 0);

  printf("Testing FITS image header parser (tfitshdr.c)\n"
         "---------------------------------------------\n\n");

  // Read in the FITS header.
#if defined HAVE_CFITSIO && defined DO_CFITSIO
  status = 0;

  if (fits_open_file(&fptr, infile, READONLY, &status)) {
    fits_report_error(stderr, status);
    return 1;
  }

  if (fits_hdr2str(fptr, 0, NULL, 0, &header, &nkeyrec, &status)) {
    fits_report_error(stderr, status);
    return 1;
  }

  fits_close_file(fptr, &status);
#else
  if ((fptr = fopen(infile, "r")) == 0) {
    printf("ERROR opening %s\n", infile);
    return 1;
  }

  k = 0;
  end = 0;
  nkeyrec = 0;
  for (j = 0; j < 100; j++) {
    for (i = 0; i < 36; i++) {
      if (fgets(keyrec, 81, fptr) == 0) {
        break;
      }

      memcpy(header+k, keyrec, 80);
      k += 80;
      nkeyrec++;

      if (strncmp(keyrec, "END       ", 10) == 0) {
        // An END keyrecord was read, but read the rest of the block.
        end = 1;
      }
    }

    if (end) break;
  }
  fclose(fptr);
#endif

  printf("Found %d header keyrecords.\n", nkeyrec);


  // Cull recognized, syntactically valid WCS keyrecords from the header.
  relax = WCSHDR_all;
  ctrl = -1;
  if ((status = wcspih(header, nkeyrec, relax, ctrl, &nreject, &nwcs,
                       &wcs))) {
    fprintf(stderr, "wcspih ERROR %d: %s.\n", status, wcs_errmsg[status]);
    return 1;
  }
  wcsvfree(&nwcs, &wcs);

  // Number remaining.
  nkeyrec = strlen(header) / 80;


  // Specific keywords to be located or culled.
  strcpy(keyids[0].name, "SIMPLE  ");
  strcpy(keyids[1].name, "BITPIX  ");
  strcpy(keyids[2].name, "NAXIS   ");
  strcpy(keyids[3].name, "COMMENT ");
  strcpy(keyids[4].name, "HISTORY ");
  strcpy(keyids[5].name, "        ");
  strcpy(keyids[6].name, "END     ");
  nkeyids = 7;

  if (nkeyids) {
    printf("\nThe following keyrecords will not be listed:\n");
    for (i = 0; i < nkeyids; i++) {
      printf("  \"%8s\"\n", keyids[i].name);
    }
  }


  // Parse the header.
  if ((status = fitshdr(header, nkeyrec, nkeyids, keyids, &nreject, &keys))) {
    printf("fitskey ERROR %d: %s.\n", status, fitshdr_errmsg[status]);
  }
#if defined HAVE_CFITSIO && defined DO_CFITSIO
  fits_free_memory(header, &status);
#endif

  // Report the results.
  printf("\n%d header keyrecords parsed by fitshdr(), %d rejected:\n\n",
    nkeyrec, nreject);
  kptr = keys;
  for (i = 0; i < nkeyrec; i++, kptr++) {
    // Skip syntactically valid keyrecords that were indexed.
    if (kptr->keyno < 0 && !kptr->status) continue;

    // Basic keyrecord info.
    printf("%4d%5d  %-8s%3d", kptr->keyno, kptr->status, kptr->keyword,
                              kptr->type);

    // Format the keyvalue for output.
    switch (abs(kptr->type)%10) {
    case 1:
      // Logical.
      sprintf(text, "%c", kptr->keyvalue.i?'T':'F');
      break;
    case 2:
      // 32-bit signed integer.
      sprintf(text, "%d", kptr->keyvalue.i);
      break;
    case 3:
      // 64-bit signed integer.
#ifdef WCSLIB_INT64
         sprintf(text, "%+lld", kptr->keyvalue.k);
#else
         if (kptr->keyvalue.k[2]) {
           sprintf(text, "%+d%09d%09d", kptr->keyvalue.k[2],
                                    abs(kptr->keyvalue.k[1]),
                                    abs(kptr->keyvalue.k[0]));
         } else {
           sprintf(text, "%+d%09d",     kptr->keyvalue.k[1],
                                    abs(kptr->keyvalue.k[0]));
         }
#endif
       break;
    case 4:
      // Very long integer.
      k = 0;
      for (j = 7; j > 0; j--) {
        if (kptr->keyvalue.l[j]) {
          k = j;
          break;
        }
      }

      sprintf(text, "%+d", kptr->keyvalue.l[k]);
      for (j = k-1; j >= 0; j--) {
        sprintf(text+strlen(text), "%09d", abs(kptr->keyvalue.l[j]));
      }

      break;
    case 5:
      // Float.
      sprintf(text, "%+13.6e", kptr->keyvalue.f);
      break;
    case 6:
      // Int complex.
      sprintf(text, "%.0f  %.0f", kptr->keyvalue.c[0],
                                  kptr->keyvalue.c[1]);
      break;
    case 7:
      // Float complex.
      sprintf(text, "%+13.6e  %+13.6e", kptr->keyvalue.c[0],
                                        kptr->keyvalue.c[1]);
      break;
    case 8:
      // String.
      sprintf(text, "\"%s\"", kptr->keyvalue.s);
      break;
    default:
      // No value.
      *text = '\0';
      break;
    }

    if (kptr->type > 0) {
      // Keyvalue successfully extracted.
      printf("  %s", text);
    } else if (kptr->type < 0) {
      // Syntax error of some type while extracting the keyvalue.
      printf("  (%s)", text);
    }

    // Units?
    if (kptr->ulen) {
      printf(" %.*s", kptr->ulen-2, kptr->comment+1);
    }

    // Comment text or reject keyrecord.
    printf("\n%s\n", kptr->comment);
  }


  // Print indexes.
  printf("\n\nIndexes of selected keywords:\n");
  for (i = 0; i < nkeyids; i++) {
    printf("%-8s%5d%5d%5d", keyids[i].name, keyids[i].count, keyids[i].idx[0],
      keyids[i].idx[1]);

    // Print logical (SIMPLE) and integer (BITPIX, NAXIS) values.
    if (keyids[i].count) {
      kptr = keys + keyids[i].idx[0];
      printf("%4d", kptr->type);

      if (kptr->type == 1) {
        printf("%5c", kptr->keyvalue.i?'T':'F');
      } else if (kptr->type == 2) {
        printf("%5d", kptr->keyvalue.i);
      }
    }
    printf("\n");
  }

  wcsdealloc(keys);

  return 0;
}