File: fixhead.c

package info (click to toggle)
wcstools 3.9.7-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,684 kB
  • sloc: ansic: 113,336; sh: 553; makefile: 245; lisp: 86; sed: 1
file content (394 lines) | stat: -rw-r--r-- 9,874 bytes parent folder | download | duplicates (2)
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
/* File fixhead.c
 * October 14, 2021
 * By Jessica Mink, SAO Telescope Data Center
 * Send bug reports to jmink@cfa.harvard.edu

   Copyright (C) 2021
   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"
#include "libwcs/wcslib.h"

#define MAXKWD 100
#define MAXFILES 2000
static int maxnfile = MAXFILES;

static void usage();
static void CopyValues();
extern char *GetFITShead();

static char *RevMsg = "FIXHEAD WCSTools 3.9.7, 26 April 2022, Jessica Mink (jmink@cfa.harvard.edu)";
static int verbose = 0;		/* verbose/debugging flag */
static int nblank = 10;		/* Number of blank lines to leave before END */
static int nfile = 0;
static int ndec0 = -9;
static int listpath = 0;
static int newimage0 = 0;
static int keyset = 0;
static int histset = 0;
static int version = 0;		/* If 1, print only program name and version */
static char *rootdir=NULL;	/* Root directory for input files */

int
main (ac, av)
int ac;
char **av;
{
    char *str;
    char **fp, **newfp;
    int ifile;
    int nbytes;
    char filepath[256];
    char *name;
    FILE *flist = NULL;
    FILE *fdk;
    char *listfile;
    char *ilistfile;
    int  i;
    void FixHeader();

    ilistfile = NULL;
    nfile = 0;
    fp = (char **)calloc (maxnfile, sizeof(char *));

    /* 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++) {
	if ((*(str = *av))=='-') {
	    char c;
	    while ((c = *++str))
	    switch (c) {

		case 'b': /* Number of blank header lines to keep */
		    if (ac < 2)
			usage();
		    nblank = atoi (*++av);
		    ac--;

		case 'd': /* Root directory for input */
		    if (ac < 2)
			usage();
		    rootdir = *++av;
		    ac--;
		    break;

		case 'h':	/* Group HISTORY keywords at end of header */
		    histset++;
		    break;
	
		case 'v': /* More verbosity */
		    verbose++;
		    break;
	
		default:
		    usage();
		    break;
		}
	    }

	/* File containing a list of image files */
	else if (*av[0] == '@') {
	    listfile = *av + 1;
	    if (isimlist (listfile)) {
		ilistfile = listfile;
		nfile = getfilelines (ilistfile);
		}
	    else {
		printf ("FIXHEAD: %s is not an image list file\n", listfile);
		}
	    }

	/* Image file */
	else if (isfits (*av) || isiraf (*av)) {
	    if (nfile >= maxnfile) {
		maxnfile = maxnfile * 2;
		nbytes = maxnfile * sizeof (char *);
		newfp = (char **) calloc (maxnfile, sizeof (char *));
		for (i = 0; i < nfile; i++)
		newfp[i] = fp[i];
		free (fp);
		fp = newfp;
		}
	    fp[nfile] = *av;
	    nfile++;
	    }

	}

    if (nfile < 1 ) {
	printf ("FIXHEAD: no files specified\n");
	usage ();
	}

    /* Open file containing a list of images, if there is one */
    if (ilistfile != NULL) {
	if ((flist = fopen (ilistfile, "r")) == NULL) {
	    printf ("FIXHEAD: Image list file %s cannot be read\n", ilistfile);
	    usage ();
	    }
	}

    /* Read through headers of images */
    for (ifile = 0; ifile < nfile; ifile++) {
	if (ilistfile != NULL) {
	    first_token (flist, 254, filepath);
	    FixHeader (filepath);
	    }
	else
	    FixHeader (fp[ifile]);

	if (verbose)
	    printf ("\n");
	}
    if (ilistfile != NULL)
	fclose (flist);

    return (0);
}

static void
usage ()
{
    printf ("%s\n",RevMsg);
    if (version)
	exit (-1);
    printf ("Clean up FITS or IRAF by removing excessive blank lines\n");
    printf ("Usage: fixhead [-v][-d dir][-b num] file1.fit ... filen.fits\n");
    printf("  or : fixhead [-v][-d dir][-b num] file1.fit @filelist\n");
    printf("  -b num: Remove all but num blank lines from end of header\n");
    printf("  -d: Root directory for input files (default is cwd)\n");
    printf("  -h: Move HISTORY lines to end of header\n");
    printf("  -k: Write FIXHEAD keyword giving the number of lines removed\n");
    printf("  -v: Verbose\n");
    exit (1);
}


void
FixHeader (filepath)

char	*filepath;	/* FITS or IRAF file to process */

{
    char *headin;	/* FITS image header to which to add */
    char *headout;	/* FITS image header to output */
    char *irafheader = NULL;	/* IRAF image header */
    char *image = NULL;	/* Input and output image buffer */
    double dval;
    int ival, nch, inum;
    int iraffile;
    int ndec, nbheadout, nbheadin, nbheader;
    char oldfilepath[128];
    char string[80];
    char *fext;
    char *fullpath;
    int lhist, lhead;
    char *ltime;
    int naxis, ipos, nbhead, nbr, nbw;
    int fdw;
    int lfp;
    int nblines, nlrem;
    char history[128];
    char echar;
    char *lblanks, *lend;
    int imageread = 0;
    char *fblanks;	/* Position of new END in output header */

    if (rootdir) {
	nch = strlen (rootdir) + strlen (filepath) + 2;
	fullpath = (char *) calloc (1, nch);
	strcat (fullpath, rootdir);
	strcat (fullpath, "/");
	strcat (fullpath, filepath);
	strcpy (filepath, fullpath);
	}
    else
	filepath = filepath;

/* Retrieve FITS header from FITS or IRAF .imh file */
    if ((headin = GetFITShead (filepath, verbose)) == NULL)
	return;

/* Open IRAF image if .imh extension is present */
    if (isiraf (filepath)) {
	iraffile = 1;
	if ((irafheader = irafrhead (filepath, &lhead)) != NULL) {
	    if ((headin = iraf2fits (filepath, irafheader, lhead, &nbhead)) == NULL) {
		printf ("Cannot translate IRAF header %s/n",filepath);
		free (irafheader);
		return;
		}
	    }
	else {
	    printf ("Cannot read IRAF file %s\n", filepath);
	    return;
	    }
	}

/* Open FITS file if .imh extension is not present */
    else {
	iraffile = 0;
	if ((headin = fitsrhead (filepath, &lhead, &nbhead)) != NULL) {
	    hgeti4 (headin,"NAXIS",&naxis);
	    if (naxis > 0) {
		if ((image = fitsrfull (filepath, nbhead, headin)) == NULL) {
		    if (verbose)
			printf ("No image with FITS header in %s\n", filepath);
		    imageread = 0;
		    }
		else
		    imageread = 1;
		}
	    else {
		if (verbose)
		    printf ("Writing new primary header only\n");
		}
	    }
	else {
	    printf ("Cannot read FITS file %s\n", filepath);
	    return;
	    }
	}

/* Allocate output FITS header and copy original header into it */
    nbheadin = nbhead;
    headout = (char *) calloc (nbheadout, 1);
    strncpy (headout, headin, nbheadin);
    headout[nbheadin+1] = (char) 0;
    nbheadout = nbheadin;

/* Find first and last blank line */
    lblanks = blsearch (headout, "END");
    lend = ksearch (headout, "END");
    nblines = (lend - lblanks) / 80;
    nlrem = nblines - nblank;
    hgeti4 (headout, "IMHVER", &iraffile );

    if (verbose) {
	printf ("%s\n",RevMsg);
	if (nlrem > 1)
	    printf ("Remove %d blank lines from header of ", nlrem);
	else if (nlrem > 0)
	    printf ("Remove %d blank line from header of ", nlrem);
	else
	    printf ("Removing no blank lines from header of ");
	if (iraffile)
	    printf ("IRAF image file %s\n", filepath);
	else
	    printf ("FITS image file %s\n", filepath);
	}

/* Return here if no changes are being made to the header */
    if (nlrem < 1)
	return;

/* If space for more than nblank blank lines, move END up to new end of header */
    fblanks = lblanks + (nblank * 80);
    if (fblanks < lend) {
	strcpy (fblanks, "END");
	strcpy (lend, "   ");
	}

/* If no space for nblank blank lines, exit */
    else
	return;

/* Remove directory path and extension from file name */
    strcpy (oldfilepath, filepath);
    fext = strrchr (oldfilepath, '.');
    if (fext)
	fext++;
    else {
	lfp = strlen (oldfilepath);
	fext = filepath + lfp;
	fext[0] = '.';
	fext++;
	}

/* Move original FITS or IRAF file to .fitx or .imx file */
    if (iraffile) {
	fext[0] = 'i';
	fext[1] = 'm';
	fext[2] = 'x';
	fext[3] = (char) 0;
	if (verbose)
	    printf ("Moving old IRAF image file to %s\n", oldfilepath);
	}
    else {
	fext[0] = 'f';
	fext[1] = 'i';
	fext[2] = 't';
	fext[3] = 'x';
	fext[4] = (char) 0;
	if (verbose)
	    printf ("Moving old FITS image file to %s\n", oldfilepath);
	}
    rename (filepath, oldfilepath);

/* Write fixed header to output file */
    if (iraffile) {
	if (irafwhead (filepath, lhead, irafheader, headout) > 0 && verbose)
	    printf ("%s rewritten successfully.\n", filepath);
	else if (verbose)
	    printf ("%s could not be written.\n", filepath);
	free (irafheader);
	}
    else if (naxis > 0 && imageread) {
	if (fitswimage (filepath, headout, image) > 0 && verbose)
	    printf ("%s: rewritten successfully.\n", filepath);
	else if (verbose)
	    printf ("%s could not be written.\n", filepath);
	free (image);
	}
    else {
	if ((fdw = fitswhead (filepath, headout)) > 0 ) {
	    if (verbose)
		printf ("%s: rewritten successfully.\n", filepath);
	    close (fdw);
	    }
	else if (verbose)
            printf ("%s could not be written.\n", filepath);
	}

    if (headin == headout) {
	free (headin);
	}
    else {
	free (headin);
	free (headout);
	}
    return;
}

/* Oct 14 2021	New program based on cphead
 */