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
|
/*============================================================================
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: tbth1.c,v 4.8.1.1 2011/08/15 08:07:06 cal103 Exp cal103 $
*=============================================================================
*
* tbth1 tests wcsbth(), the WCS FITS parser for binary table headers, and
* wcsfix(), which translates non-standard constructs. It reads a test header
* and uses wcsprt() to print the resulting wcsprm structs.
*
* Input comes from file "bth.fits" using either fits_hdr2str() from CFITSIO
* if the DO_CFITSIO preprocessor is defined, or read directly using fgets()
* otherwise.
*
*---------------------------------------------------------------------------*/
#include <wcsconfig_tests.h>
#include <stdio.h>
#include <string.h>
#if defined HAVE_CFITSIO && defined DO_CFITSIO
#include <fitsio.h>
#endif
#include <wcs.h>
#include <wcshdr.h>
#include <wcsfix.h>
int main()
{
char infile[] = "bth.fits";
char a, *hptr;
short alts[1000][28];
int colsel[8], ctrl, ialt, iblock, icol, ifix, ikeyrec, iwcs, keysel,
nkeyrec, nreject, nwcs, relax, stat[NWCSFIX], status;
struct wcsprm *wcs;
#if defined HAVE_CFITSIO && defined DO_CFITSIO
char *header;
fitsfile *fptr;
#else
char keyrec[81], header[288001];
int gotend, k;
FILE *fptr;
#endif
/* 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 WCSLIB parser for FITS binary table headers (tbth1.c)\n"
"-------------------------------------------------------------\n\n");
/* Read in the FITS header, excluding COMMENT and HISTORY keyrecords. */
#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, 1, NULL, 0, &header, &nkeyrec, &status)) {
fits_report_error(stderr, status);
return 1;
}
fits_close_file(fptr, &status);
#else
if ((fptr = fopen(infile, "r")) == 0) {
fprintf(stderr, "ERROR opening %s\n", infile);
return 1;
}
k = 0;
nkeyrec = 0;
gotend = 0;
for (iblock = 0; iblock < 100; iblock++) {
for (ikeyrec = 0; ikeyrec < 36; ikeyrec++) {
if (fgets(keyrec, 81, fptr) == 0) {
break;
}
if (strncmp(keyrec, " ", 8) == 0) continue;
if (strncmp(keyrec, "COMMENT ", 8) == 0) continue;
if (strncmp(keyrec, "HISTORY ", 8) == 0) continue;
strncpy(header+k, keyrec, 80);
k += 80;
nkeyrec++;
if (strncmp(keyrec, "END ", 8) == 0) {
/* An END keyrecord was read, but read the rest of the block. */
gotend = 1;
}
}
if (gotend) break;
}
fclose(fptr);
#endif
fprintf(stderr, "Found %d non-comment header keyrecords.\n\n", nkeyrec);
/* Parse the header, allowing all recognized non-standard WCS keywords and
* usage. WCS keyrecords that are used are culled from the header, illegal
* ones are reported. */
relax = WCSHDR_all;
ctrl = -2;
keysel = 0;
colsel[0] = 0;
fprintf(stderr, "\nIllegal-WCS header keyrecords rejected by wcsbth():\n");
if ((status = wcsbth(header, nkeyrec, relax, ctrl, keysel, colsel,
&nreject, &nwcs, &wcs))) {
fprintf(stderr, "wcsbth ERROR %d: %s.\n", status, wcs_errmsg[status]);
}
if (!nreject) fprintf(stderr, "(none)\n");
/* List the remaining keyrecords. */
printf("\n\nHeader keyrecords ignored by wcsbth():\n");
hptr = header;
while (*hptr) {
printf("%.80s\n", hptr);
hptr += 80;
}
#if defined HAVE_CFITSIO && defined DO_CFITSIO
free(header);
#endif
/* Summarize what was found. */
printf("\n\nExtracted %d coordinate description%s:\n", nwcs,
(nwcs == 1) ? "" : "s");
printf("\n Unattached image header(s)");
status = wcsbdx(nwcs, &wcs, 0, alts);
if (alts[0][27]) {
printf(" with indices:\n -");
for (a = 'A'; a <= 'Z'; a++) {
printf("%2c", a);
}
printf("\n ");
for (ialt = 0; ialt < 27; ialt++) {
if (alts[0][ialt] < 0) {
printf(" -");
} else {
printf("%2d", alts[0][ialt]);
}
}
printf("\n");
} else {
printf(": (none)\n");
}
printf("\n Binary table image array(s)");
for (icol = 1; icol <= 999; icol++) {
if (alts[icol][27]) {
printf(" with indices:\n Col. -");
for (a = 'A'; a <= 'Z'; a++) {
printf("%2c", a);
}
printf("\n");
for (icol = 1; icol <= 999; icol++) {
for (ialt = 0; ialt < 27; ialt++) {
if (alts[icol][ialt] >= 0) {
printf("%5d: ", icol);
for (ialt = 0; ialt < 27; ialt++) {
if (alts[icol][ialt] < 0) {
printf(" -");
} else {
printf("%2d", alts[icol][ialt]);
}
}
printf("\n");
break;
}
}
}
icol = 9999;
}
}
if (icol < 9999) {
printf(": (none)\n");
}
printf("\n Pixel list(s)");
status = wcsbdx(nwcs, &wcs, 1, alts);
for (icol = 1; icol <= 999; icol++) {
if (alts[icol][27]) {
printf(" with indices:\n Col. -");
for (a = 'A'; a <= 'Z'; a++) {
printf("%2c", a);
}
printf("\n");
for (icol = 1; icol <= 999; icol++) {
for (ialt = 0; ialt < 27; ialt++) {
if (alts[icol][ialt] >= 0) {
printf("%5d: ", icol);
for (ialt = 0; ialt < 27; ialt++) {
if (alts[icol][ialt] < 0) {
printf(" -");
} else {
printf("%2d", alts[icol][ialt]);
}
}
printf("\n");
break;
}
}
}
icol = 9999;
}
}
if (icol < 9999) {
printf(": (none)\n");
}
/* Fix non-standard usage and print each of the wcsprm structs. */
for (iwcs = 0; iwcs < nwcs; iwcs++) {
printf("\n------------------------------------"
"------------------------------------\n");
/* Fix non-standard WCS keyvalues. */
if ((status = wcsfix(7, 0, wcs+iwcs, stat))) {
printf("wcsfix ERROR, status returns: (");
for (ifix = 0; ifix < NWCSFIX; ifix++) {
printf(ifix ? ", %d" : "%d", stat[ifix]);
}
printf(")\n\n");
}
if ((status = wcsset(wcs+iwcs))) {
fprintf(stderr, "wcsset ERROR %d: %s.\n", status, wcs_errmsg[status]);
continue;
}
if ((status = wcsprt(wcs+iwcs))) {
fprintf(stderr, "wcsprt ERROR %d: %s.\n", status, wcs_errmsg[status]);
}
}
status = wcsvfree(&nwcs, &wcs);
/* Do it again to check that wcsbth() can handle multiple invokations. */
printf("\nInvoking wcsbth() a second time on the same header...\n");
ctrl = 0;
if ((status = wcsbth(header, nkeyrec, relax, 0, keysel, colsel, &nreject,
&nwcs, &wcs))) {
fprintf(stderr, "wcsbth ERROR %d: %s.\n", status, wcs_errmsg[status]);
} else {
printf("OK, extracted %d coordinate description%s.\n", nwcs,
(nwcs == 1) ? "" : "s");
}
return 0;
}
|