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


/*	
	MULTIPLIES data files

		MUL filea [fileb] [const=c] [div=] [box=] [help]

	multiplies by constant if const is specified
	scales by div if specified (to get non-integral multipliers)
	if BOX= is used, only multiplies over area of box

	1/29/91 - added 'floor' to expression for "const" and "div", 
	          and included <math.h>  - MWR
	9/9/92  - added check for valid data value before assigning result
	          of calculation (in 32 bits) to 16-bit output value MWR 
	9/15/92 - exit(0) added -rrt
	11/17/92 - added outfile option -rrt
	1/7/96   - added 'help' option.  MWR.

*/

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

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

char *usage = "mul file [file2] [const=] [div=] [box=] [outfile=] [help]";

int
main(argc, argv)
int argc;
char *argv[];
{
	int i, constant = 1, nrow, ncol, div = 1, other_nrow, other_ncol;
	int row, boxno;
	static int nc, nr, sr, sc;
	long lresult;
	FITS_HANDLE fh, fother = 0,fout;
	static int16 data[NMAX], other[NMAX];
	char *gotit,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);
	nr = nrow; 	
	nc = ncol;

	strcpy(outfile,argv[1]);

	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("const", argv[i])) != NULL) {
			constant = (int) floor(evaluate(gotit) + 0.5);
			continue;
		}
		if ((gotit = find("outfile", argv[i])) != NULL) {
			strcpy(outfile,gotit);
			new_file=1;
			continue;
		}
		if ((gotit = find("div", argv[i])) != NULL) {
			div = (int) floor(evaluate(gotit) + 0.5);
			if (div == 0)
				error(-1, "can't divide by zero");
			continue;
		}
		if (fother)
			error(-1, "file already open");
		fother = fits_open(argv[i], "r", &other_nrow, &other_ncol);
	}

	if (fother) {
		if (other_nrow != (sr + nr))
			error(-1, "multiplier file does not have same number of rows");
		if (other_ncol != (sc + nc))
			error(-1, "multiplier file does not have same number of columns");
	}

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

	if(new_file)
		fits_copyheader(fh,fout, FITS_CHECK);

	for (i = 0; i < nc; i++)
		other[i] = 1;

	for (row = 0; row < nr; row++) {
		fits_get_data(fh, row, sr, data, nc);
		if (fother)
			fits_get_data(fother, row, sc, other, nc);
		for (i = 0; i < ncol; i++) {
			lresult = (((long)constant*data[i]*other[i])/div);		
			if (lresult >= MAX_DATA_VAL)
				data[i] = MAX_DATA_VAL;
			else if (lresult <= MIN_DATA_VAL)
				data[i] = MIN_DATA_VAL;
			else
				data[i] = (int16) lresult;
		}
		fits_put_data(fout, row, sr, data, nc);
	}
	fits_close(fout);
	exit(0);
}
