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


/*	
	selects row or column from image and prints out data 
	from input file 						
	
		ROWCOL file [BOX=] [ROW=n] [COL=n] ...... [help]

	to select the relevant row or column			
	writes file ROW## or COL## in same directory as input data file

	11/18/91 - added BOX= option, and changed printed values from unsigned
	           to signed integers.  - MWR

	9/22/92  - modified so that many columns /or rows can
				be written at the same time
				also output sent to stdout instead of a file -rrt

	1/8/96   - added 'help' option
	           modified error message slightly.  MWR
*/

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

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

#define USAGE	"usage: rowcol file [row=] [col=] [box=] [help]"
#define MAXCUT 100 	/* maximum number of row or col entries */

int
main(argc, argv)
int argc;
char *argv[];
{
	FITS_HANDLE fh;
	int i = 0, j;
	int nrow, ncol;
	int sr, sc, nr, nc, row[MAXCUT], col[MAXCUT], boxno;
	int rowcount=0, colcount=0, boxflag,index;
	static int16 val;
	char *gotit;
	char err_msg[200];

	if (argc < 3)
		error(-1, USAGE);

	/* placate compiler */
	boxno = 0;

	sr = 0;
	sc = 0;
	fh = fits_open(argv[1], "r", &nrow, &ncol);
	nr = nrow;
	nc = ncol;

	for (j = 2; j < argc; j++) {
		if ((gotit = find("col", argv[j])) != NULL) {
			index = (int) floor(evaluate(gotit) + 0.5);
			if ((index < 0) || (index >= ncol)) {
				fprintf(stderr, 
				"bad column number %d; valid columns are 0 to %d\n",
				index, nc - 1);
				exit(1);
			}
			col[colcount++]=index;
			continue;
		}
		if ((gotit = find("row", argv[j])) != NULL) {
			index = (int) floor(evaluate(gotit) + 0.5);
			if ((index < 0) || (index >= nrow)) {
				fprintf(stderr, "bad row number %d; valid rows are 0 to %d", 
				                index, nr - 1);
				exit(-1);
			}
			row[rowcount++]=index;
			continue;
		}	
		if ((gotit = find("box", argv[j])) != NULL) {
			boxno = (int)(evaluate(gotit) + 0.5);
			if (getbox(boxno, &sr, &sc, &nr, &nc))
				error(-1,"box not found");
			check_box(boxno, (int) nrow, (int) ncol);
			boxflag = 1;
			continue;
		}	
		if (keyword("help", argv[i])) {
			printf("%s\n", USAGE);
			exit(0);
		}
		error(-1, USAGE);
	}
	if ((rowcount == 0) && (colcount == 0)) 
		error(-1, "rowcol: neither rows nor columns were specified");

	if ((rowcount != 0) && (colcount != 0)) 
		error(-1, "rowcol: you may not mix rows and columns ");

	if (colcount) {
		for(i=0; i<colcount; i++){
			if ((col[i] < sc) || (col[i] > sc + nc)) {
				fprintf(stderr, "col %d is not inside box %d\n", col[i], boxno);
				exit(1);
			}
		}
		for (index = sr; index < sr + nr; index++) {
			printf("%d",index);
			for(i=0; i<colcount; i++){
				fits_get_data(fh, index, col[i], &val, 1);
				printf(" %d", val);
			}
			printf("\n");
		}
	}
	if (rowcount) {
		for(i=0; i<rowcount; i++){
			if ((row[i] < sr) || (row[i] > sr + nr)) {
				sprintf(err_msg, "row %d is outside valid range\n", row[i]);
				error(1, err_msg);
			}
		}
		for(index=sc; index < sc+nc; index ++){
			printf("%d",index);
			for(i=0; i<rowcount; i++){
				fits_get_data(fh, row[i], index, &val, 1);
				printf(" %d", val);
			}
			printf("\n");
		}
	}
	exit(0);
}
