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


/***************************************************************************
 * Add a "vector" (an image with one dimension equal to 1) to an image.
 *
 *    addvec file vector row|col [norm] [outfile=]
 *
 */

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



static char usage[] = "addvec file vector row|col [norm] [outfile=]";

int
main(argc, argv)
int argc;
char *argv[];
{
	FITS_HANDLE fh, fvec, fout;
	static int i, norm_flag, nother;
	static unsigned int nr, nc, sr, sc, nrow, ncol, row, vec_nrow, vec_ncol;
	static int16 data[NMAX], vecdata[NMAX], datum;
	static long sum[NMAX], lresult[NMAX];
	static int do_row, do_col;
	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);
	strcpy(outfile, argv[1]);
	fvec = -1;
	nother = 0;
	nr = nrow;
	nc = ncol;	
	sr = 0;
	sc = 0;
	do_row = 0;
	do_col = 0;

	for(i = 2; i < argc; i++) {
		if (keyword(argv[i], "help")) {
			printf("%s\n", usage);
			exit(0);
		}
		if ((gotit = find("outfile", argv[i])) != NULL) {
			strcpy(outfile,gotit);
			new_file=1;
			continue;
		}
		if (keyword("norm", argv[i])){
			norm_flag = 1;
			continue;
		}
		if (keyword("row", argv[i])){
			do_row = 1;
			continue;
		}
		if (keyword("col", argv[i])){
			do_col = 1;
			continue;
		}
		/* arg must be vector's file name if we got here */
		if (nother++ > 1)
			error(-1, "too many files given on command line");
		fvec = fits_open(argv[i], "r", &vec_nrow, &vec_ncol);
		if ((vec_nrow > 1) && (vec_ncol > 1)) {
			error(0, "addvec: warning: vector has > 1 row and col");
		}
	}
	if (nother == 0) {
		error(1, usage);
	}

	if (do_row + do_col != 1) {
		error(1, "addvec: you must specify exactly one of 'row' or 'col'");
	}
	if (do_row) {
		if (ncol != vec_ncol) {
			error(-1, "number of columns not the same");
		}
	}
	if (do_col) {
		if (nrow != vec_nrow) {
			error(-1, "number of rows not the same");
		}
	}

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

	if(new_file)
		fits_copyheader(fh, fout, FITS_CHECK);
			
	if (do_row) {
		fits_get_data(fvec, 0, sc, vecdata, nc);
	}

	if (norm_flag) {
		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];		
			if (do_col) {
				fits_get_data(fvec, row, sc, &datum, 1);
				for (i = 0; i < nc; i++) {
					sum[i] += datum;
				}
			}
			else { /* dorow */
				for (i = 0; i < nc; i++) {
					sum[i] += vecdata[i];
				}
			}
			for (i = 0; i < nc; i++) {
				if ((lresult[i] = (sum[i]/2)) >= 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];
			}
			if (do_col) {
				fits_get_data(fvec, row, sc, &datum, 1);
				for (i = 0; i < nc; i++) {
					lresult[i] += datum;
				}
			}
			else { /* dorow */
				for (i = 0; i < nc; i++) {
					lresult[i] += vecdata[i];
				}
			}
			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(fvec);
	fits_close(fout);
	exit(0);
}
