
/**************************************************************************
 *                                                                        *
 * Copyright (c) 1996 Michael Richmond and Richard Treffers               *
 *                                                                        *
 *                    This software may be copied and distributed for     *
 *                    educational, research and not for profit services   *
 *                    provided that this copyright and statement are      *
 *                    included in all such copies.                        *
 *                                                                        *
 **************************************************************************/


/*	
	program to inspect fits header

		BUFFERS fname [FULL] [FITS[=param]

	2/21/92 - exit(0) code added -rrt
	9/22/92 - usage changed -rrt
	10/6/93 - keywords changed to upper case
*/

#include "pcvista.h"
#include "fits.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>

#ifdef PROTO
int main(int,char **);
static void strupr(char *);
#else
static void strupr();
#endif

#define NPARAM_MAX	10

#define USAGE "usage: buffers fname [full] [fits=[param]] [help]"

int
main(argc,argv)
int argc;
char *argv[];
{
	int i, nrow, ncol;
	FITS_HANDLE fh;
	static char *gotit, param[NPARAM_MAX][NBUF], buf[BIG_BUF];
	static int full_flag, n_params;

	if (argc == 1) {
		error(-1, USAGE);
	}
	if (strcmp(argv[1], "help") == 0) {
		printf("%s\n", USAGE);
		exit(0);
	}

	if (argc == 2)
		full_flag = 1;
	fh = fits_open(argv[1], "r", &nrow, &ncol);
	
	for (i = 2; i < argc; i++) {
		if (keyword("help", argv[i])) {
			printf("%s\n", USAGE);
			exit(0);
		}
		if ((gotit = find("fits", argv[i])) != NULL) {
			strupr(gotit);
			strcpy(param[n_params++], gotit);
			if (n_params == NPARAM_MAX)
				error(-1, "too many parameters specified");
			continue;
		}
		if (keyword("fits", argv[i]) || keyword("full", argv[i])) {
			full_flag = 1;
			continue;
		}
		sprintf(buf,"'%s' unknown keyword", argv[i]);
		error(-1, buf);
	}
	for (i = 0; i < n_params; i++) {
		if (fits_get_symbol(fh, param[i], buf) == FITS_PASS)
			printf("%s = %s\n", param[i], buf);
		else
			printf("%s - not found\n", param[i]);
	}
	if (full_flag) {
		int card;
		char *gotit;
		for (card = 1; (gotit = fits_get_headerline(fh, card)) != NULL; card++) {
			if (gotit[0] == ' ')
				break;
			strncpy(buf, gotit, 79);
			buf[79] = 0;
			for (i = 78; (buf[i] == ' ') && i > 0; i--)	/* remove trailing blanks */
				buf[i] = 0;
			printf("%s\n", buf);
		}
	}
	exit(0);
}



/* the damn unix library should have this baby 
*/

static void strupr(in)
char *in;
{
	while(*in){
		*in=toupper(*in);
		in++;
	}
}
