/* This program is the front-end to my cftp thingy.  */
#include "head.h"
#include "cftpmain.h"
#include "fgetword.h"
#include "getp.h"
#include "init.h"
#include "dom.h"
#include "rnorm.h"
#include "process.h"

int main(int argc, 
	 char *argv[]
	 )
{
  double time;       /* Number of iterations to run the process. */

  long length;     /* Number of data points. */

  int i,j,k;


  char dataloc[50];     /* Full path name of file containing data points */

  double sigma;         /* Value of standard deviation of noise */

  double tau;           /* Scale param for standard deviation of signal */

  double gamma;         /* gamma - the value of gamma in the prior */

  double lambda;        /* lambda - the value of lambda in the prior */

  double exponent;      /* The exponent of $J_{jk}$ in the prior */

  double mG;            /* Size of one point plus disc $G$. */
                        /* Used for minimum process. */

  double mult;          /* $J_{jk}/(\sigma^2+J_{jk})$ */

  double stddev;        /* $\sigma*\sqrt{\texttt{mult}}$ */

  location *points;

  FILE *fopen(), *datafile;

  char word[MAXWORD];

  int stop;

  long seed, seedtwo;

  if(argc <= 1){
    fprintf(stderr,"There was no specified input!\n");
    return 1;
  }

/* getparameter obtains the values of the parameters */
  else if (getparameters(dataloc,&sigma,&tau,&lambda,&gamma,&time,
			 &exponent,&seed,&seedtwo,argv[1])){

/* Open datafile to find out how many numbers are in it. */
    datafile = fopen(dataloc,"r");  
    j=0;
    for(i=0;
	i<=MAXLEN && (fgetword(datafile,MAXWORD,word)!=0);
	++i)
      if(*word!='[')
	j++;
    if(i>=MAXLEN){
      printf("Too much data.  ");
      printf("Please restrict data in file to a maximum of ");
      printf("%ld items.\n",(long) MAXLEN);
    }
    else{
      length = j;
      fclose(datafile);             /* Don't forget to close the file! */
    
/* Allocate memory for the locations */
      points = (location *) calloc(length, sizeof(location));

      if(points==NULL){
	printf("Out of memory");
	return 1;
      }
      else{
/* Now read the datafile */
	datafile = fopen(dataloc,"r");  /* Open datafile to read the */
					/* numbers this time */
	j=0;
	for(i=0; (fgetword(datafile,MAXWORD,word)!=0); ++i)
	  if(*word!='[')
	    sscanf(word,"%lf",&(points[j++].data));
	fclose(datafile);           /* Don't forget to close the file! */

/* \texttt{initialise} calculates the values of the birth rate at each 
   location (the $\lambda$s), $m(G)$ and $p_{\min}$. */
	initialise(sigma,tau,lambda,gamma,&mG,length,points,
		   seed,seedtwo);
      
      
	stop = 0;
	for(k=1;!stop;k*=2){
/* dominating generates the dominating process */
	  dominating(length,k*time,points);

/* Finally we get to the actual process.  This function determines which 
   points will be born and which will not in the true process and 
   determines whether the algorithm has converged. */
	  stop = process(k*time,length,gamma,
			 sigma,tau,exponent,points);
	}

	for(i=0;i<length;++i){
	  if(points[i].life.max>0){
	    if(points[i].continuous && points[i].life.max==1)
	      printf("%g\n",points[i].data);
	    else{
	      mult=tau*pow((double) points[i].life.max,exponent)/
		(sigma*sigma + 
		 tau*pow((double) points[i].life.max,exponent));
	      stddev=sigma*sqrt(mult);
	      printf("%g\n",
		     mult*points[i].data+rnorm(&seed)*stddev);
	    }
	  }
	  else
	    printf("0\n");
	}
	free(points);
/* Output the length of time taken to converge to standard error: */
	fprintf(stderr,"-M=%g\n",k*time/2);
      }
    }
  }
  return 0;
}
