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


/*	
	Combines overlapping data 

	FOLD filename 

*/

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

#define NBUF	80

int main(int, char **);
static double pmod(double,double);

#define USAGE "usage: fold file [ycol=] [freq=] [nbin=] [verbose] [help]"

struct store{
	double sum;
	int count;
};

int verbose;

int 
main(argc, argv)
int argc;
char *argv[];
{
	int i,gotfile=0;
	int y_column=1;
	double freq,period=1.0;
	double step=.1;
	double val;
	double t=0.;
	int nbin=10;
	char *gotit,buf[NBUF],fname[NBUF];
	struct store *dat;	
	FILE *fin;


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

	/* placate compiler */
	fin = stdin;
	freq = 0.0;


	for (i = 1; i < argc; i++) {
		if (keyword("help", argv[i])) {
			printf("%s\n",USAGE);
			exit(0);
		}
		if ((gotit = find("ycol", argv[i])) != NULL) {
			y_column = (int)(evaluate(gotit) + 0.5);
			continue;
		}
		if ((gotit = find("nbin", argv[i])) != NULL) {
			nbin = (int)(evaluate(gotit) + 0.5);
			continue;
		}
		if ((gotit = find("freq", argv[i])) != NULL) {
			freq = evaluate(gotit) ;
			if(freq < SMALL)
				error(1,"fold: frequency too small");
			period=1.0/freq;
			continue;
		}
		if ((gotit = find("step", argv[i])) != NULL) {
			step = evaluate(gotit);
			if (step == 0.0)
				error(-1, "fold: step time cannot be zero");
			continue;
		}
		if (keyword("verbose", argv[i])) {
			verbose = 1;
			continue;
		}
		if (gotfile) {
			fprintf(stderr,"fold: unknown option '%s'\n", argv[i]);
			exit(1);
		}
		strcpy(fname, argv[i]);
		gotfile = 1;
	}

	if(gotfile){
		if(strcmp(fname,"-") == 0){
			fin=stdin;
		}else{
			if ((fin = fopen(fname, "r")) == NULL){
				fprintf(stderr, "fold:can't open input file '%s'\n",fname);
				exit(1);
			}	
		}
	}else{
		error(1,"fold: no file specified");
	}
	
	dat=(struct store *)calloc(nbin,sizeof(struct store));
	if (dat == NULL)
		error(1,"fold: malloc failure");

	if(verbose)
		fprintf(stderr,"nbin=%d freq=%f step=%f\n",nbin,freq,step); 

	while (fgets(buf, NBUF, fin) != NULL) {
		if(buf[0] == '#')	/* skip comment characters */
			continue;
		val = get_col(buf,y_column);
		i=nbin*pmod(t,period)/period;
		dat[i].sum+=val;
		dat[i].count++;
		t+=step;
	}

	for (i = 0; i < nbin; i ++) {
		if(dat[i].count)
			printf("%d %.3f %d\n", i, dat[i].sum/dat[i].count,dat[i].count);
		else
			printf("%d -----\n",i);
	}
	exit(0);
}

static double pmod(arg,mod)        /* positive modulus function */
double arg,mod;             /* returns 0<value<mod */
{
	return(arg-floor(arg/mod)*mod);
}

