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


/*	
	converts ASCII data into an image 
	usage:

		MAKEIM filename 

	9/22/92 - exit(0) added
	9/27/94 - Solaris
	11/26/95 - re-coded main loop so that data is read AND written,
	               one row at a time.  Requires the user to specify
	               both ncol AND nrow on command line.  MWR
	1/24/96 - added "help" option.  MWR
	2/21/97 - fixed error message to give first arg, not command name.  MWR
*/

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

int main(int,char **);

#define USAGE "usage: makeim filename nrow= ncol= [scale=] [outfile=] [help]"

int
main(argc, argv)
int argc;
char *argv[];
{
	FILE *fin;
	FITS_HANDLE fout;
	int i, row, nrow, ncol;
	int done;
	double scale = 1.0, test;
	int16 *data;
	char *gotit;
	char err_msg[200];
	char outname[NBUF];

	/* placate compiler */
	gotit = NULL;
	nrow = -1;
	ncol = -1;

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

	strcpy(outname, "makeim");

	if (strcmp("argv[1]", "-") == 0) {
		fin=stdin;
	}
	else {
		if ((fin = fopen(argv[1], "r")) == NULL) {
			sprintf(err_msg, "makeim: cannot open '%s'\n", argv[1]);
			error(1, err_msg);
		}
	}

	for (i = 2; i < argc; i++) {
		if (keyword("help", argv[i])) {
			printf("%s\n", USAGE);
			exit(0);
		}
		if ((gotit=find("nrow",argv[i])) != NULL) {
			nrow=atoi(gotit);
			continue;
		}
		if ((gotit=find("ncol",argv[i])) != NULL) {
			ncol=atoi(gotit);
			continue;
		}
		if ((gotit=find("scale",argv[i])) != NULL) {
			scale=atof(gotit);
			continue;
		}
		if ((gotit=find("outfile",argv[i])) != NULL) {
			strcpy(outname,gotit);
			continue;
		}
		sprintf(err_msg,"makeim: unknown argument '%s'\n", argv[i]);
		error(1, err_msg);
	}

	if ((nrow <= 0) || (ncol <= 0)) {
		error(1, "makeim: nrow and ncol must be specified and > 0\n");
	}
	if ((data = malloc(ncol*sizeof(int16))) == NULL) {
		error(1, "makeim: malloc failure");
	}

	/* here we open the new FITS file for output ... */
#ifdef DEBUG
	printf("makeim opening '%s' nrow=%d ncol=%d\n", outname, nrow, ncol);
#endif
	fout = fits_open(outname, "w", &nrow, &ncol);


	/* 
	 * now, we enter a loop where we keep reading 'ncol' values
	 * from the input file.  Each time, we create a single row of output 
	 * values, and write that row to the output FITS file.
	 * We keep going until we run out of input values.
	 */
	done = 0;
	for (row = 0; row < nrow; row++) {
		for (i = 0; i < ncol; i++) {
			data[i] = 0;
		}
		if (done == 1) {
			fits_put_data(fout, row, 0, data, ncol);
			continue;
		}
		for (i = 0; i < ncol; i++) {
			if (fscanf(fin, "%lf", &test) != 1) {
				done = 1;
				error(0, "makeim: ran out of data, padding with zeroes");
				break;
			}
			test = test*scale + 0.5;
			if (test > (double)MAX_DATA_VAL)
				error(0, "makeim: warning.. data overflow\n");
			if (test < (double) MIN_DATA_VAL)
				error(0, "makeim: warning.. data underflow\n");
			data[i] = (int16)test;
		}
		fits_put_data(fout, row, 0, data, ncol);
	}

	fits_close(fout);

	exit(0);
}
