/*
*	io,example.c
*	Example use of my IO routines and macros.
*
*	To compile this in linux, use (with appropriate io.o path):
*		gcc -DNeed_time_h io,example.c ../give/code/io.o -lm
*
*	To test this routine, type:
*		op point in.fld
*		a.out out.fld in.fld
*		op range out.fld in.fld
*
*	Copyright 2002-8-19	Jeff Fessler	The University of Michigan
*/
#include "../give/code/def.h"	/* replace path to point to your def.h */

/* define a few globals that may be needed */

GlobalsDefinedHere

/*
*	io_example_work()
*	here is where the actual computation (trivial in this case) occurs.
*	notice that there is no file IO in this routine!
*/
bool io_example_work(float *po, cfloat *pi, cdouble val,
cint n1, cint n2, cint n3, cint n4, cint chat)
{
	int ii;
	cint nelem = n1 * n2 * Max(n3, 1) * Max(n4, 1);

	if (chat)	Note1("scaling data by %d", val)

	for (ii=0; ii < nelem; ++ii)
		po[ii] = pi[ii] * val;
	Ok
}


/*
*	io_example_file()
*/
bool io_example_file(cchar *file_out, cchar *file_in, cint chat)
{
	int n1, n2, n3, n4, nelem;
	float *data;

	if (chat)
		printf("running io_example() to %s from %s\n",
			file_out, file_in);

	/* get dimensions of array in file */
	I4dim(&n1, &n2, &n3, &n4, file_in)

	/* allocate memory data */
	nelem = n1 * n2 * Max(n3, 1) * Max(n4, 1);
	Mem0(data, nelem)

	/* read input file */
	Iread4float(data, n1, n2, n3, n4, 0, Max(n4,1)-1, file_in)

	/* do the actual data processing */
	Call(io_example_work, (data, data, 10.0, n1, n2, n3, n4, Chat))

	/* save to output file */
	Iwrite4float(file_out, data, n1, n2, n3, n4)

	/* free allocated memory */
	Free0(data)

	Ok
}


/*
*	main_io_example()
*/
bool main_io_example(int argc, cchar **argv)
{
	if (argc < 2)
		Fail1("Usage: %s out.file in.file [chat]", argv[0])
	Call(io_example_file, (argv[1], argv[2], Argint(3, 9)))
	Ok
}


/*
*	main()
*/
int main(int argc, cchar **argv)
{
	if (!main_io_example(argc, argv))
		return -1;
	return 0;
}
