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


/*	adds data files
	ADD file1 [file2] [fileN] [const=c] [box=] [norm] [help]
	adds constant if const is specified

	1/29/91 - added 'floor' to expression for "const", and included
	          <math.h>  - MWR
	9/9/91  - added check for values > MAX_DATA_VAL or < MIN_DATA_VAL
	          after 32-bit addition/subtraction.  Probably slows things
	          down quite a bit.  MWR
	8/15/92 - call to exit(0) -rrt
	9/22/92 - usage -rrt
	11/20/92 - add outfile option -rrt
	7/7/94 - prototypes checked -rrt
	1/22/96 - added "help" option.  MWR
*/
#include "pcvista.h"
#include "fits.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>

#define MAXADD    	100		/* maximum number of files to add together */



#define USAGE \
  "usage: add file1 [file2] [fileN] [const=] [box=] [norm] [outfile=] [help]"


int
main(argc,argv)
int argc;
char *argv[];
{
	FITS_HANDLE fh, fn[MAXADD],fout;
	static int i, k, constant, boxno, norm_flag, nfiles, nother;
	static int nr, nc, sr, sc, nrow, ncol, row, other_nrow, other_ncol;
	static int16 data[NMAX], other[NMAX];
	static long sum[NMAX], lresult[NMAX];
	char *gotit,outfile[NBUF];
	int new_file=0;

	if (argc == 1) {
		fprintf(stderr, "%s\n", USAGE);
		exit(1);
	}
	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;	
	sr = 0;
	sc = 0;

	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);
			if (getbox(boxno, &sr, &sc, &nr, &nc))
				error(-1, "can't find box");
			check_box(boxno, nrow, ncol);			
			continue;
		}
		if ((gotit = find("outfile", argv[i])) != NULL) {
			strcpy(outfile,gotit);
			new_file=1;
			continue;
		}
		if ((gotit = find("const", argv[i])) != NULL) {
			constant = (int)floor(evaluate(gotit) + 0.5);
			continue;
		}
		if (keyword("norm", argv[i])){
			norm_flag = 1;
			continue;
		}
		/* arg must be another file name if we got here */
		if (nother++ > MAXADD)
			error(-1, "too many files given on command line");
		fn[nother-1] = fits_open(argv[i], "r", &other_nrow, &other_ncol);
		if (nrow != other_nrow)
			error(-1, "number of rows not the same");
		if (ncol != other_ncol)
			error(-1, "number of columns not the same");
	}

	fout = fits_open(outfile, new_file?"w":"x", &nrow, &ncol);

	if(new_file)
		fits_copyheader(fh,fout, FITS_CHECK);
			
	if (norm_flag) {
		nfiles = nother + 1;
		for (row = sr; row < (sr+nr); row++) {
		    fits_get_data(fh, row, sc, data, nc);
			for (i = 0; i < nc; i++)
				sum[i] = data[i];		
			for (k = 0; k < nother; k++) {
				fits_get_data(fn[k], row, sc, data, nc);
				for (i = 0; i < nc; i++)
					sum[i] += data[i];		
			}
			for (i = 0; i < nc; i++) {
				if ((lresult[i] = (sum[i]/nfiles + constant)) >= MAX_DATA_VAL)
					data[i] = MAX_DATA_VAL;
				else if (lresult[i] <= MIN_DATA_VAL)
					data[i] = MIN_DATA_VAL;
				else
					data[i] = (int16) lresult[i];
				sum[i] = 0;
			}
			fits_put_data(fout, row, sc, data, nc);
		}
	} else {
		for (row = sr; row < (sr+nr); row++) {
			fits_get_data(fh, row, sc, data, nc);
			for (i = 0; i < nc ;i++) {
				lresult[i] = data[i];
			}
			for (k = 0; k < nother; k++) {
				fits_get_data(fn[k], row, sc, other, nc);
				for (i = 0; i < nc ;i++) {
					lresult[i] += other[i];
				}
			}
			if (constant) {
				for (i = 0; i < nc; i++) {
					lresult[i] += constant;
				}
			}
			for (i = 0; i < nc ;i++) {
				if (lresult[i] >= MAX_DATA_VAL)
					data[i] = MAX_DATA_VAL;
				else if (lresult[i] <= MIN_DATA_VAL)
					data[i] = MIN_DATA_VAL;
				else
					data[i] = (int16) lresult[i];
			}
			fits_put_data(fout, row, sc, data, nc);
		}
	}
	fits_close(fout);
	exit(0);
}
