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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
|
/* File newfits.c
* May 10, 2006
* By Jessica Mink, Harvard-Smithsonian Center for Astrophysics
* Send bug reports to jmink@cfa.harvard.edu
Copyright (C) 2006
Smithsonian Astrophysical Observatory, Cambridge, MA USA
This program 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 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/* Write a FITS header without any data or a blank FITS image.
* Add information using edhead or sethead */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <math.h>
#include "libwcs/fitsfile.h"
#include "libwcs/wcs.h"
static void usage();
static void MakeFITS ();
extern void setcenter();
extern void setsys();
extern void setrot();
extern void setsecpix();
extern void setsecpix2();
extern void setrefpix();
extern void setcdelt();
extern void setproj();
extern struct WorldCoor *GetFITSWCS();
static char *RevMsg = "NEWFITS WCSTools 3.9.7, 26 April 2022, Jessica Mink (jmink@cfa.harvard.edu)";
static int verbose = 0; /* verbose/debugging flag */
static int bitpix = 0; /* number of bits per pixel (FITS code, 0=no image) */
static int version = 0; /* If 1, print only program name and version */
static int wcshead = 0; /* If 1, add WCS information from command line */
static int nx = 0; /* width of image in pixels */
static int ny = 0; /* height of image in pixels */
static int extend = 0; /* If 1, write primary header, add other files as ext */
static char *pixfile; /* Pixel file name */
static char *wcsfile; /* FITS WCS file name */
static char *newname; /* FITS extension file name */
int
main (ac, av)
int ac;
char **av;
{
char *str;
int readlist = 0;
char *lastchar;
char filename[128];
FILE *flist;
char *listfile = NULL;
char rastr[32], decstr[32];
double x, y;
pixfile = NULL;
newname = NULL;
/* Check for help or version command first */
str = *(av+1);
if (!str || !strcmp (str, "help") || !strcmp (str, "-help"))
usage();
if (!strcmp (str, "version") || !strcmp (str, "-version")) {
version = 1;
usage();
}
/* crack arguments */
for (av++; --ac > 0; av++) {
/* Set RA, Dec, and equinox if WCS-generated argument */
if (strsrch (*av,":") != NULL) {
if (ac < 3)
usage();
else {
strcpy (rastr, *av);
ac--;
strcpy (decstr, *++av);
setcenter (rastr, decstr);
ac--;
setsys (wcscsys (*++av));
}
}
/* Set decimal degree center */
else if (isnum (*av)) {
if (ac < 3)
usage();
else {
strcpy (rastr, *++av);
ac--;
strcpy (decstr, *++av);
setcenter (rastr, decstr);
ac--;
setsys (wcscsys (*++av));
}
}
/* Read list of files to create from file */
else if (*(str = *av) == '@') {
readlist = 1;
listfile = *av + 1;
}
/* Otherwise, read command */
else if (*(str = *av) == '-') {
char c;
while ((c = *++str))
switch (c) {
case 'a': /* Initial rotation angle in degrees */
if (ac < 2)
usage();
setrot (atof (*++av));
ac--;
wcshead++;
break;
case 'b': /* Reference pixel coordinates in B1950 */
if (ac < 3)
usage();
setsys (WCS_B1950);
strcpy (rastr, *++av);
ac--;
strcpy (decstr, *++av);
ac--;
setcenter (rastr, decstr);
wcshead++;
break;
case 'd': /* Set CDELTn, CROTAn instead of CD matrix */
setcdelt();
break;
case 'e': /* Make an extended FITS file */
extend = 1;
break;
case 'g': /* Reference pixel coordinates in Galactic */
if (ac < 3)
usage();
setsys (WCS_GALACTIC);
strcpy (rastr, *++av);
ac--;
strcpy (decstr, *++av);
ac--;
setcenter (rastr, decstr);
wcshead++;
break;
case 'i': /* Input pixel file */
if (ac < 2)
usage();
pixfile = *++av;
ac--;
break;
case 'j': /* Reference pixel coordinates in J2000 */
if (ac < 3)
usage();
setsys (WCS_J2000);
strcpy (rastr, *++av);
ac--;
strcpy (decstr, *++av);
ac--;
setcenter (rastr, decstr);
wcshead++;
break;
case 'o': /* Number of bits per pixel */
if (ac < 2)
usage ();
bitpix = (int) atof (*++av);
ac--;
break;
case 'p': /* Initial plate scale in arcseconds / pixel */
if (ac < 2)
usage();
setsecpix (atof (*++av));
ac--;
if (ac > 1 && isnum (*(av+1))) {
setsecpix2 (atof (*++av));
ac--;
}
wcshead++;
break;
case 's': /* Size of image in X and Y pixels */
if (ac < 3)
usage();
nx = atoi (*++av);
ac--;
ny = atoi (*++av);
ac--;
break;
case 't': /* FITS projection code for output image */
if (ac < 2)
usage(c, "needs projection type, such as TAN");
setproj (*++av);
ac--;
break;
case 'v': /* More verbosity */
verbose++;
break;
case 'w': /* Input FITS WCS file */
if (ac < 2)
usage();
wcsfile = *++av;
ac--;
wcshead++;
break;
case 'x': /* Reference pixel X and Y coordinates */
if (ac < 3)
usage();
x = atof (*++av);
ac--;
y = atof (*++av);
ac--;
setrefpix (x, y);
wcshead++;
break;
case 'z': /* Use AIPS classic WCS */
setdefwcs (WCS_ALT);
break;
default:
usage();
break;
}
}
else {
MakeFITS (*av);
if (verbose)
printf ("\n");
}
}
/* Process files in file of filenames */
if (readlist) {
if ((flist = fopen (listfile, "r")) == NULL) {
fprintf (stderr,"IMROT: List file %s cannot be read\n",
listfile);
usage ();
}
while (fgets (filename, 128, flist) != NULL) {
lastchar = filename + strlen (filename) - 1;
if (*lastchar < 32) *lastchar = 0;
MakeFITS (filename);
if (verbose)
printf ("\n");
}
fclose (flist);
}
return (0);
}
static void
usage ()
{
fprintf (stderr,"%s\n",RevMsg);
if (version)
exit (-1);
fprintf (stderr,"Make dataless FITS image header files\n");
fprintf(stderr,"Usage: [-v][-a degrees][-p scale][-b ra dec][-j ra dec][-s nx ny][-i file]\n");
fprintf(stderr," [-x x y] file.fits ...\n");
fprintf(stderr," -a num: initial rotation angle in degrees (default 0)\n");
fprintf(stderr," -b ra dec: initial center in B1950 (FK4) RA and Dec\n");
fprintf(stderr," -d: set CDELTn, CROTAn instead of CD matrix\n");
fprintf(stderr," -g lon lat: initial center in Galactic longitude and latitude\n");
fprintf(stderr," -i file : read image from a binary file\n");
fprintf(stderr," -j ra dec: initial center in J2000 (FK5) RA and Dec\n");
fprintf(stderr," -o num: output pixel size in bits (FITS code, default=0)\n");
fprintf(stderr," -p arcsec: initial plate scale in arcsec per pixel (default 0)\n");
fprintf(stderr," -s num num: size of image in x and y pixels (default 100x100)\n");
fprintf(stderr," -t proj: set FITS CTYPE projection (default TAN)\n");
fprintf(stderr," -v: verbose\n");
fprintf(stderr," -w file: Read FITS WCS from this file\n");
fprintf(stderr," -x num num: X and Y coordinates of reference pixel (default is center)\n");
exit (1);
}
static void
MakeFITS (name)
char *name;
{
char *image = NULL; /* FITS image */
char *header; /* FITS header */
int lhead; /* Maximum number of bytes in FITS header */
double cra; /* Center right ascension in degrees (returned) */
double cdec; /* Center declination in degrees (returned) */
double dra; /* Right ascension half-width in degrees (returned) */
double ddec; /* Declination half-width in degrees (returned) */
double secpix; /* Arcseconds per pixel (returned) */
int wp; /* Image width in pixels (returned) */
int hp; /* Image height in pixels (returned) */
int sysout=0; /* Coordinate system to return (0=image, returned) */
double eqout=0.0; /* Equinox to return (0=image, returned) */
int i;
int nbimage; /* Number of bytes in image */
int nbskip; /* Number of bytes to skip in file before image */
int nbfile; /* Number of bytes in file */
int nbread; /* Number of bytes actually read from file */
int nbhead;
struct WorldCoor *wcs;
FILE *diskfile;
char *cplus;
char history[256];
char extname[16];
if (verbose) {
fprintf (stderr,"%s\n",RevMsg);
if (bitpix != 0)
fprintf (stderr,"Create ");
else
fprintf (stderr,"Create header as ");
fprintf (stderr,"FITS file %s\n", name);
}
/* Make sure that no existing file is overwritten */
if ((diskfile = fopen (name, "r")) != NULL) {
fprintf (stderr,"NEWFITS: FITS file %s exists, no new file written\n",
name);
fclose (diskfile);
return;
}
/* Write primary header for FITS extension file */
if (extend == 1) {
lhead = 2880;
header = (char *) calloc (1, lhead);
strcpy (header, "END ");
hlength (header, 2880);
hputl (header, "SIMPLE", 1);
hputi4 (header, "BITPIX", 16);
hputi4 (header, "NAXIS", 0);
hputl (header, "EXTEND", 1);
image = NULL;
hputc (header, "COMMENT", "FITS (Flexible Image Transport System) format is defined in 'Astronomy");
hputc (header, "COMMENT", "and Astrophysics' vol 376, page 359; bibcode: 2001A&A...376..359H.");
if (fitswimage (name, header, image) == 0)
printf ("%s: dataless FITS image header not written.\n", name);
else {
if (verbose)
printf ("%s: dataless FITS image header written.\n", name);
newname = name;
}
extend++;
return;
}
else if (extend > 1) {
if (newname == NULL)
return;
diskfile = fopen (newname, "a");
if ((header = fitsrhead (name, &lhead, &nbhead)) != NULL) {
if ((image = fitsrimage (name, nbhead, header)) == NULL) {
fprintf (stderr, "Cannot read FITS image %s\n", name);
free (header);
return;
}
}
else {
fprintf (stderr, "Cannot read FITS file %s\n", name);
return;
}
sprintf (extname, "EXT%d", extend-1);
hputs (header, "EXTNAME", extname);
hputi4 (header, "EXTVER", extend-1);
hputc (header, "COMMENT", "FITS (Flexible Image Transport System) format is defined in 'Astronomy");
hputc (header, "COMMENT", "and Astrophysics' vol 376, page 359; bibcode 2001A&A...376..359H.");
if (fitswimage (name, header, image) == 0)
printf ("%s: FITS image extension %s not written.\n",name,extname);
else {
if (verbose)
printf ("%s: FITS image extension %s written.\n",name,extname);
}
extend++;
return;
}
if (wcsfile) {
header = fitsrhead (wcsfile, &lhead, &nbhead);
hputi4 (header, "BITPIX", bitpix);
}
else {
lhead = 14400;
header = (char *) calloc (1, lhead);
strcpy (header, "END ");
for (i = 4; i < lhead; i++)
header[i] = ' ';
hlength (header, 14400);
hputl (header, "SIMPLE", 1);
hputi4 (header, "BITPIX", bitpix);
hputi4 (header, "NAXIS", 2);
}
if (nx > 0)
hputi4 (header, "NAXIS1", nx);
else if (wcsfile)
hgeti4 (header, "NAXIS1", &nx);
else {
nx = 100;
hputi4 (header, "NAXIS1", nx);
}
if (ny > 0)
hputi4 (header, "NAXIS2", ny);
else if (wcsfile)
hgeti4 (header, "NAXIS2", &ny);
else {
ny = 100;
hputi4 (header, "NAXIS2", ny);
}
if (bitpix < 0)
nbimage = (-bitpix / 8) * nx * ny;
else
nbimage = (bitpix / 8) * nx * ny;
/* Read image file, if there is one */
nbskip = 0;
if (pixfile != NULL) {
if ((cplus = strchr (pixfile,'+')) != NULL) {
*cplus = (char) 0;
nbskip = atoi (cplus+1);
}
nbfile = getfilesize (pixfile);
if (nbfile > 0) {
nbfile = nbfile - nbskip;
if ((image = calloc (nbimage, 1)) != NULL) {
if ((diskfile = fopen (pixfile, "r")) != NULL) {
fseek (diskfile, nbskip, 0);
nbread = fread (image, 1, nbimage, diskfile);
if (nbread < nbimage)
printf ("*** NEWFITS: %d / %d bytes read from %s\n",
nbread, nbimage, pixfile);
else if (verbose)
printf ("NEWFITS: %d bytes read from %s\n",
nbread, pixfile);
fclose (diskfile);
sprintf (history, "Pixels from file %s", pixfile);
hputc (header, "HISTORY", history);
}
else
printf ("*** NEWFITS: Could not open file %s\n", pixfile);
}
else
printf ("*** NEWFITS: Could not allocate %d bytes for image\n",
nbimage);
}
}
/* Set up blank image, if bitpix is non-zero */
else if (bitpix != 0) {
if ((image = calloc (nbimage, 1)) == NULL)
printf ("*** NEWFITS: Could not allocate %d bytes for image\n",
nbimage);
if (verbose)
fprintf (stderr, "NEWFITS: %d bits/pixel\n", bitpix);
}
else
image = NULL;
/* Initialize header */
if (wcshead) {
if (wcsfile)
wcs = GetFITSWCS (wcsfile,header,verbose,&cra,&cdec,&dra,&ddec,&secpix,
&wp,&hp,&sysout,&eqout);
else
wcs = GetFITSWCS (name,header,verbose,&cra,&cdec,&dra,&ddec,&secpix,
&wp,&hp,&sysout,&eqout);
wcsfree (wcs);
}
if (!wcsfile) {
hputc (header, "COMMENT", "FITS (Flexible Image Transport System) format is defined in 'Astronomy");
hputc (header, "COMMENT", "and Astrophysics' vol 376, page 359; bibcode 2001A&A...376..359H.");
}
if (fitswimage (name, header, image) > 0 && verbose) {
if (image == NULL)
printf ("%s: dataless FITS image header written successfully.\n",
name);
else
printf ("%s: %d-byte FITS image written successfully.\n",
name, nbimage);
}
free (header);
if (image != NULL)
free (image);
return;
}
/* Jan 4 1999 New program
* Apr 7 1999 Add filename argument to GetFITSWCS
* May 13 1999 Change name to NEWFITS and add option to write blank image
* Jun 3 1999 Allow center to be set as standard WCSTools coordinate string
* Jun 3 1999 Move command line filenames into processing loop
* Oct 15 1999 Free wcs using wcsfree()
* Oct 22 1999 Drop unused variables after lint
* Nov 1 1999 Set header length after creating it; add option for CDELTn
*
* Aug 1 2000 Add -i option to add image from binary file
*
* Jan 18 2001 Add -e option to build FITS extension file
* Oct 11 2001 Add COMMENT with FITS reference to each header that is written
*
* Apr 9 2002 Fix bug in final print statement
*
* May 28 2003 Add -g for image with galactic coordinate WCS
* Sep 25 2003 Add -t to set projection
*
* Aug 30 2004 Declare undeclared setproj() subroutine
* Sep 29 2004 Add option to read WCS from another file
*
* May 10 2006 Always set BITPIX before NAXIS
*/
|