
/**************************************************************************
 *                                                                        *
 * 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.                        *
 *                                                                        *
 **************************************************************************/


/*	
	Clips data files

		CLIP file [MAX=] [VMAX=] [MIN=] [VMIN=] [box=d]

	sets values > max to VMAX and/or values < VMIN to VMIN
	operates over box if specified

	3/20/92 - nr=ncol bug fixed -RRT
	9/22/92 - usage 
			- exit(0) added -rrt
	7/28/93 - outfile added as well as verbose -rrt
*/

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

#ifdef PROTO
int main(int, char **);
#endif

#define USAGE \
  "usage: clip file [max=] [min=] [vmax=] [vmin=] [box=] [outfile=] \n       [verbose] [help]"

int verbose;

int
main(argc, argv)
int argc;
char *argv[];
{
	FITS_HANDLE fh,fout;
	int i, boxno;
	static int sr, sc, nr, nc;
	int nrow, ncol, row;
	static int min = MIN_DATA_VAL, max = MAX_DATA_VAL, vmin = 0, vmax = 0, val;
	static int16 data[NMAX], clip_hi, clip_lo, got_vmin, got_vmax;
	char *gotit;
	char outfile[NBUF];
	int new_file=0;

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

	fh = fits_open(argv[1], "r", &nrow, &ncol);
	strcpy(outfile,argv[1]);
	nr = nrow;	
	nc = ncol;

	for (i = 2; i < argc; i++) {
		if (keyword("help", argv[i])) {
			printf("%s\n", USAGE);
			exit(0);
		}
		if ((gotit = find("box", argv[i])) != NULL) {
			boxno = (int)(evaluate(gotit) + 0.5);
			if (getbox(boxno, &sr, &sc, &nr, &nc))
				error(-1, "can't find box");		
			check_box(boxno, nrow, ncol);
			continue;
		}
		if ((gotit = find("min", argv[i])) != NULL) {
			min = (int) evaluate(gotit);
			clip_lo = 1;
			continue;
		}
		if ((gotit = find("vmin", argv[i])) != NULL) {
			vmin = (int) evaluate(gotit);
			got_vmin = 1;
			continue;
		}
		if ((gotit = find("max", argv[i])) != NULL) {
			max = (int) evaluate(gotit);
			clip_hi = 1;
			continue;
		}
		if ((gotit = find("outfile", argv[i])) != NULL) {
				strcpy(outfile,gotit);
				new_file=1;
				continue;
		}

		if ((gotit = find("vmax", argv[i])) != NULL) {
			vmax = (int) evaluate(gotit);
			got_vmax = 1;
			continue;
		}
		if(keyword("verbose",argv[i])){
			verbose=1;
			continue;
		}
	}
	fout = fits_open(outfile, new_file?"w":"x", &nrow, &ncol);
		 
	 if(new_file)
		 fits_copyheader(fh,fout,FITS_CHECK);


	if (!got_vmax)
		vmax = max;
	if (!got_vmin)
		vmin = min;

	if (clip_hi && verbose)
		printf("Clipping values greater than %10d to %10d\n", max, vmax);
	if (clip_lo && verbose)
		printf("Clipping values less than    %10d to %10d\n", min, vmin);

	if (!clip_hi && !clip_lo)
		error(-1, "clip: no action taken");
	for (row = sr; row < (sr + nr); row++) {
		fits_get_data(fh, row, sc, data, nc);
		for (i = 0; i < nc; i++) {
			val = data[i];
			if (clip_hi && (val > max)) {
				data[i] = vmax;
				continue;
			}
			if (clip_lo && (val < min)) {
				val = vmin;
				data[i] = val;		
			}
		}
		fits_put_data(fout, row, sc, data, nc);
	}
	fits_close(fout);
	exit(0);
}
