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
|
/* File i2f.c
* May 11 2009
* By Jessica Mink, Harvard-Smithsonian Center for Astrophysics
* Send bug reports to jmink@cfa.harvard.edu
Copyright (C) 1996-2009
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
*/
#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"
static void usage();
static void IRAFtoFITS ();
static int verbose = 0; /* verbose/debugging flag */
static char *RevMsg = "I2F WCSTools 3.9.7, 26 April 2022, Jessica Mink (jmink@cfa.harvard.edu)";
static int version = 0; /* If 1, print only program name and version */
static int delirafkey = 0; /* If 1, delete IRAF .imh keywords */
static int deliraffile = 0; /* If 1, delete IRAF .imh files */
static char outname[128]; /* Name for output image */
static char outdir[256]; /* Output directory pathname */
static int first = 1;
int
main (ac, av)
int ac;
char **av;
{
char *str;
char *listfile;
char filename[256];
int ifile, nfile;
FILE *flist;
outname[0] = 0;
outdir[0] = 0;
/* 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 && *(str = *av) == '-'; av++) {
char c;
while ((c = *++str)) {
switch (c) {
case 'v': /* more verbosity */
verbose++;
break;
case 'd': /* output directory */
if (ac < 2)
usage ();
strcpy (outdir, *++av);
ac--;
break;
case 'x': /* delete IRAF imh files */
deliraffile = 1;
break;
case 'i': /* delete IRAF imh keywords */
delirafkey = 1;
break;
case 'o': /* output file name */
if (ac < 2)
usage ();
strcpy (outname, *++av);
ac--;
break;
case 's': /* output to stdout */
strcpy (outname, "stdout");
break;
default:
usage();
break;
}
}
}
/* now there are ac remaining file names starting at av[0] */
if (ac == 0)
usage ();
else if (*av[0] == '@') {
listfile = *av + 1;
if (isimlist (listfile)) {
nfile = getfilelines (listfile);
if ((flist = fopen (listfile, "r")) == NULL) {
fprintf (stderr,"I2F: Image list file %s cannot be read\n",
listfile);
usage ();
}
for (ifile = 0; ifile < nfile; ifile++) {
first_token (flist, 254, filename);
IRAFtoFITS (filename);
}
fclose (flist);
}
}
else {
while (ac-- > 0) {
char *fn = *av++;
if (verbose)
fprintf (stderr,"%s", fn);
IRAFtoFITS (fn);
}
}
return (0);
}
static void
usage ()
{
fprintf (stderr,"%s\n",RevMsg);
if (version)
exit (-1);
fprintf (stderr,"Write FITS files from IRAF image files\n");
fprintf(stderr,"Usage: i2f [-isvx] [-o name] [-d path] file.imh ...\n");
fprintf(stderr," or : i2f [-isvx] [-o name] [-d path] @imhlist\n");
fprintf(stderr," -d: write FITS file(s) to this directory\n");
fprintf(stderr," -i: delete unnecessary IRAF keywords\n");
fprintf(stderr," -o: output name for one file\n");
fprintf(stderr," -s: write output to standard output\n");
fprintf(stderr," -v: verbose\n");
fprintf(stderr," -x: delete IRAF image file\n");
exit (1);
}
static void
IRAFtoFITS (name)
char *name;
{
char *image; /* FITS image */
char *header; /* FITS header */
int lhead; /* Maximum number of bytes in FITS header */
int nbhead; /* Actual number of bytes in FITS header */
char *irafheader; /* IRAF image header */
char pixname[256]; /* Pixel file name */
char history[128]; /* for HISTORY line */
char *filename; /* Pointer to start of file name */
char irafname[256]; /* Name of IRAF file */
char hdrfile[256]; /* Name of IRAF header file */
char hdrback[256]; /* Name of backup IRAF header file */
char pixfile[256]; /* Name of IRAF pixel file */
char fitsname[256]; /* Name of FITS file */
char fitspath[256]; /* Pathname of FITS file */
char *hdrfile1;
char *fitsfile;
char command[256];
char *ext; /* Pointer to start of extension */
char *endchar;
char *ltime;
int nc, ier;
/* Open IRAF image if .imh extension is present */
if (strsrch (name,".imh") != NULL) {
if ((irafheader = irafrhead (name, &lhead)) != NULL) {
header = iraf2fits (name, irafheader, lhead, &nbhead);
free (irafheader);
if (header == NULL) {
fprintf (stderr, "Cannot translate IRAF header %s/n",name);
return;
}
if ((image = irafrimage (header)) == NULL) {
hgetm (header,"PIXFIL", 255, pixname);
fprintf (stderr, "Cannot read IRAF pixel file %s\n", pixname);
free (header);
return;
}
}
else {
fprintf (stderr,"Cannot read IRAF header file %s\n", name);
return;
}
strcpy (fitsname, name);
ext = strsrch (fitsname,".imh");
strcpy (ext,".fits");
if (verbose && first) {
fprintf (stderr,"%s\n",RevMsg);
if (outname[0] == (char) 0)
fprintf (stderr, "Write FITS file from IRAF image file %s\n",
name);
else
fprintf (stderr,"Write FITS file %s from IRAF image file %s\n",
outname, name);
}
}
/* Add .imh extension to make IRAF header file name if not present */
else {
strcpy (irafname, name);
strcat (irafname,".imh");
if ((irafheader = irafrhead (irafname, &lhead)) != NULL) {
header = iraf2fits (irafname, irafheader, lhead, &nbhead);
free (irafheader);
if (header == NULL) {
fprintf (stderr, "Cannot translate IRAF header %s/n",irafname);
return;
}
if ((image = irafrimage (header)) == NULL) {
hgetm (header,"PIXFIL", 255, pixname);
fprintf (stderr, "Cannot read IRAF pixel file %s\n", pixname);
free (irafheader);
free (header);
return;
}
}
else {
fprintf (stderr,"Cannot read IRAF header file %s\n", irafname);
return;
}
strcpy (fitsname, name);
strcat (fitsname,".fits");
if (verbose && first) {
fprintf (stderr,"%s\n",RevMsg);
fprintf (stderr,"Write FITS files from IRAF image file %s\n", irafname);
}
}
/* Add HISTORY notice of this conversion */
filename = strrchr (name,'/');
if (filename)
filename = filename + 1;
else
filename = name;
strcpy (history, RevMsg);
endchar = strchr (history, ',');
*endchar = (char) 0;
strcat (history, " ");
ltime = lt2fd ();
strcat (history, ltime);
endchar = strrchr (history,':');
*endchar = (char) 0;
strcat (history, " FITS from ");
strcat (history, filename);
if (strlen (history) > 72)
history[72] = 0;
hputc (header, "HISTORY", history);
/* If assigned output name, use it */
if (outname[0] != (char) 0)
strcpy (fitsname, outname);
/* Save name of header file and pixel file */
hgetm (header, "IMHFIL", 256, hdrfile);
hgetm (header, "PIXFIL", 256, pixfile);
/* Delete IRAF header information */
if (delirafkey) {
hdel (header, "IMHFIL_1");
hdel (header, "PIXFIL_1");
hdel (header, "IMHVER");
hdel (header, "PIXOFF");
hdel (header, "PIXSWAP");
hdel (header, "HEADSWAP");
hdel (header, "DATE-MOD");
hdel (header, "PIXSWAP");
hdel (header, "DATE-MOD");
hdel (header, "IRAFMIN");
hdel (header, "IRAFMAX");
hdel (header, "IRAFTYPE");
hdel (header, "IRAF-BPX");
}
/* Write FITS file to a specified directory */
if (outdir[0] > 0) {
strcpy (fitspath, outdir);
strcat (fitspath, "/");
fitsfile = strrchr (fitsname,'/');
if (fitsfile == NULL)
strcat (fitspath, fitsname);
else
strcat (fitspath, fitsfile+1);
}
else
strcpy (fitspath, fitsname);
first = 0;
/* Write FITS image */
if (fitswimage (fitspath, header, image) > 0) {
if (verbose)
fprintf (stderr, " to %s\n", fitspath);
/* Delete IRAF .imh, .pix files */
if (deliraffile) {
hdrfile1 = strrchr (hdrfile, '/');
if (hdrfile1 == NULL) {
strcpy (hdrback, "..");
strcat (hdrback, hdrfile);
}
else {
nc = hdrfile1 - hdrfile;
strncpy (hdrback, hdrfile, nc+1);
strcat (hdrback, "..");
strcat (hdrback, hdrfile1+1);
}
if (verbose)
fprintf (stderr, "deleting %s, %s, and %s\n",
hdrfile, pixfile, hdrback);
strcpy (command, "rm ");
strcat (command, hdrfile);
ier = system (command);
if (ier)
(void)fprintf(stderr,"Command %s failed %d\n",command,ier);
strcpy (command, "rm ");
strcat (command, pixfile);
ier = system (command);
if (ier)
(void)fprintf(stderr,"Command %s failed %d\n",command,ier);
strcpy (command, "rm ");
strcat (command, hdrback);
ier = system (command);
if (ier)
(void)fprintf(stderr,"Command %s failed %d\n",command,ier);
}
}
else if (verbose)
fprintf (stderr, "%s: not written.\n", fitspath);
free (header);
free (image);
return;
}
/* Jun 6 1996 New program
* Jul 16 1996 Update header input
* Aug 16 1996 Clean up code
* Aug 26 1996 Change HGETC call to HGETS
* Aug 27 1996 Drop unused variables after lint
* Oct 17 1996 Clean up after lint
*
* Feb 21 1997 Check pointers against NULL explicitly for Linux
*
* Jan 14 1998 Version 1.3 to handle IRAF 2.11 .imh files
* May 27 1998 Include fitsio.h instead of fitshead.h
* Jun 2 1998 Fix bug in hput()
* Jul 24 1998 Make irafheader char instead of int
* Aug 6 1998 Change fitsio.h to fitsfile.h
* Aug 14 1998 Write file.fits instead of file.fit
* Aug 17 1998 Add HISTORY to header
* Nov 30 1998 Add version and help commands for consistency
* Sep 28 1999 Add standard output option
* Oct 22 1999 Drop unused variables after lint
*
* Mar 22 2000 Use lt2fd() instead of getltime()
* Mar 23 2000 Use hgetm() to get the IRAF pixel file name, not hgets()
* May 30 2000 Add option to delete IRAF keywords
* Jun 6 2000 Add options to delete IRAF files and to write FITS elsewhere
* Jul 6 2000 Implement conversion of file list
*
* Jun 21 2006 Clean up code
*
* May 11 2006 Drop extra free of irafheader when pixel file is missing
*/
|