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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/*============================================================================
WCSLIB 4.8 - an implementation of the FITS WCS standard.
Copyright (C) 1995-2011, 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/>.
Correspondence concerning WCSLIB may be directed to:
Internet email: mcalabre@atnf.csiro.au
Postal address: Dr. Mark Calabretta
Australia Telescope National Facility, CSIRO
PO Box 76
Epping NSW 1710
AUSTRALIA
Author: Mark Calabretta, Australia Telescope National Facility
http://www.atnf.csiro.au/~mcalabre/index.html
$Id: fitshdr.c,v 4.8.1.1 2011/08/15 08:07:07 cal103 Exp cal103 $
*=============================================================================
* Usage: fitshdr [infile]
*-----------------------------------------------------------------------------
* List headers from a FITS file specified on the command line, or else on
* stdin, printing them as 80-character keyrecords without trailing blanks.
*
* If invoked as 'rpfhdr' rather than 'fitshdr' it also handles RPFITS format
* which has a block size of 2560 (rather than 2880) but otherwise looks like
* FITS.
*
* Handles large files (>2GiB) via macros in wcsconfig_utils.h.
*---------------------------------------------------------------------------*/
char usage[] =
"List headers from a FITS file specified on the command line, or else on\n"
"stdin, printing them as 80-character keyrecords without trailing blanks.\n"
"\n"
"Options:\n"
" -q N Quit after reading the Nth header, where N is an integer\n"
" (optional space between -q and N).\n";
/* Get LFS definitions for stdio.h. */
#include <wcsconfig_utils.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char **argv)
{
char cbuff[2880], *cptr;
char dashes[84] = "----------------------------------------"
"----------------------------------------";
char equals[84] = "========================================"
"========================================";
char spaces[84] = " "
" ";
char *format, rpfits[8] = "RPFITS";
int i, len;
unsigned int blksiz = 2880, ihdr = 0, inhdr = 0, nhdr = 0, seekable = 1;
unsigned long long int iblock = 0, nblock = 0, nbyte;
struct stat instat;
/* Parse options. */
for (i = 1; i < argc && argv[i][0] == '-'; i++) {
switch (argv[i][1]) {
case 'q':
if (strlen(argv[i]) == 2) {
nhdr = atoi(&argv[++i][0]);
} else {
nhdr = atoi(&argv[i][2]);
}
break;
default:
fprintf(stderr, "\nUsage: fitshdr [-q N] [<infile>]\n\n%s\n", usage);
return 1;
}
}
/* If an input file name was specified then reopen it as stdin */
/* (doesn't affect seekability). */
if (i < argc) {
if (access(argv[i], R_OK) == -1) {
perror(argv[i]);
return 2;
}
if (freopen(argv[i], "r", stdin) == NULL) {
perror(argv[i]);
return 2;
}
}
/* Check for standard FITS or RPFITS. */
if (!fread(cbuff, (size_t)80, (size_t)1, stdin)) {
perror(argv[i]);
return 2;
}
if (strncmp(cbuff, "SIMPLE = ", 10) == 0) {
if (!fread(cbuff+80, (size_t)80, (size_t)1, stdin)) {
perror(argv[i]);
return 2;
}
/* Assume FITS by default. */
format = rpfits + 2;
blksiz = 2880;
/* Check for RPFITS. */
if (strncmp(cbuff+80, "FORMAT = RPFITS", 30) == 0 ||
strncmp(cbuff+80, "FORMAT = 'RPFITS'", 30) == 0) {
if (strcmp(*argv, "rpfhdr") == 0) {
/* If invoked as 'rpfhdr' then allow RPFITS. */
format = rpfits;
blksiz = 2560;
} else {
/* Otherwise disallow RPFITS but issue a warning. */
printf("WARNING: Input appears to be RPFITS, continuing anyway using "
"2880-byte blocks.\n" );
}
}
/* Read the rest of the first block. */
if (!fread(cbuff+160, (size_t)(blksiz-160), (size_t)1, stdin)) {
perror(argv[i]);
return 2;
}
inhdr = 1;
} else {
/* If we have not been invoked as 'rpfhdr' then bail out now. */
if (strcmp(*argv, "rpfhdr") != 0) {
fprintf(stderr, "Input file does not appear to be standard FITS.\n" );
return 1;
}
/* RPFITS may have a block or two of rubbish before the first header. */
format = rpfits;
blksiz = 2560;
if (!fread(cbuff+80, (size_t)(blksiz-80), (size_t)1, stdin)) {
perror(argv[i]);
return 2;
}
while (iblock < 4) {
if (!fread(cbuff, (size_t)blksiz, (size_t)1, stdin)) {
perror(argv[i]);
return 2;
}
iblock++;
if (strncmp(cbuff, "SIMPLE = ", 10) == 0) {
inhdr = 1;
break;
}
}
if (!inhdr) {
fprintf(stderr, "Input does not appear to be FITS or RPFITS.\n" );
return 1;
}
if (iblock) {
nbyte = blksiz * iblock;
printf("Skipped %lld block%s of rubbish of size %d bytes (%lld "
"bytes).\n", iblock, (iblock > 1)?"s":"", blksiz, nbyte);
}
}
printf("%s\n%s header number %d at block number %lld.\n%s\n", equals,
format, ++ihdr, ++iblock, dashes);
/* Scan through the file. */
while (1) {
if (!inhdr) {
/* Searching for a header. */
if (!fread(cbuff, (size_t)10, (size_t)1, stdin)) break;
if (strncmp(cbuff, "SIMPLE = ", 10) == 0 ||
strncmp(cbuff, "XTENSION= ", 10) == 0) {
/* Found a header. */
if (iblock) {
nbyte = blksiz * nblock;
printf("Skipped %lld block%s of data of size %d bytes (%lld "
"bytes).\n", nblock, (nblock == 1)?"":"s", blksiz, nbyte);
}
if (!fread(cbuff+10, (size_t)(blksiz-10), (size_t)1, stdin)) break;
printf("%s\n%s header number %d at block number %lld.\n%s\n",
equals, format, ++ihdr, ++iblock, dashes);
inhdr = 1;
nblock = 0;
} else {
/* Seek past it if possible. */
if (seekable) {
#ifdef HAVE_FSEEKO
if (fseeko(stdin, (off_t)(blksiz-10), SEEK_CUR)) {
#else
if (fseek(stdin, (long)(blksiz-10), SEEK_CUR)) {
#endif
if (errno == ESPIPE || errno == EBADF) {
seekable = 0;
} else {
break;
}
}
}
if (!seekable) {
if (!fread(cbuff+10, (size_t)(blksiz-10), (size_t)1, stdin)) break;
}
iblock++;
}
}
if (inhdr) {
for (cptr = cbuff; cptr < cbuff + blksiz; cptr += 80) {
/* Write out a keyrecord without trailing blanks. */
for (len = 80; len > 0; len--) {
if (cptr[len-1] != ' ') break;
}
printf("%.*s\n", len, cptr);
/* Check for end-of-header. */
if (strncmp(cptr, "END ", 8) == 0) {
inhdr = 0;
printf("%s\n", dashes);
fflush(stdout);
break;
}
}
/* Get the next header block. */
if (inhdr) {
if (!fread(cbuff, (size_t)blksiz, (size_t)1, stdin)) break;
iblock++;
}
} else {
if (!nblock) {
if (nhdr && ihdr == nhdr) {
printf("Stopping at data section number %d which begins at block "
"number %lld.\n", ihdr, iblock);
return 0;
}
printf("Data section number %d beginning at block number %lld.\n",
ihdr, iblock);
}
nblock++;
if (nblock%1000 == 0) {
/* Report progress on stderr in case it's saved to file. */
nbyte = blksiz * nblock;
fprintf(stderr, "Skipping %lld blocks of data of size %d bytes "
"(%lld bytes). \r", nblock, blksiz, nbyte);
fflush(stderr);
}
}
}
if (feof(stdin)) {
nbyte = blksiz * nblock;
printf("Skipped %lld block%s of data of size %d bytes (%lld bytes). \n",
nblock, (nblock == 1)?"":"s", blksiz, nbyte);
nbyte = blksiz * iblock;
printf("%s\nEnd-of-file after %d HDU%s in %lld x %d-byte blocks (%lld "
"bytes).\n", equals, ihdr, (ihdr == 1)?"":"s", iblock, blksiz, nbyte);
if (argc > 1 && !stat(argv[i], &instat)) {
if (nbyte != instat.st_size) {
printf("WARNING: File is too short by %lld bytes.\n",
nbyte - instat.st_size);
}
}
printf("%s\n", dashes);
fprintf(stderr, "%s\r", spaces);
} else {
perror(argv[i]);
return 2;
}
return 0;
}
|