#include "peg_pic.h"

/* time history accumulator; calculates and stores all history values,  */
/* and performs combing on history values when low on memory            */

void history() {
	int isp;
	static float *jtemp;
	static int count=1, nrftcount=0;

	jtemp= (float *)malloc(nsp*sizeof(float));

	if(nfft) {
		if (thist_hi >= nfft) {
			freqanalysis(); 
			thist_hi=0;
		}

		/******** for frequency analyzis *************/

		cur_hist[thist_hi]= exti;
		
		for (isp=0; isp<2; isp++) {
			phi_hist[isp][thist_hi] = phi[potpos[isp]];
		}
		
		Local_t_array[thist_hi] = t;
		thist_hi++;

		/*********************************************/

		avt1();             /* Calcul. of nfft1 time steps averaged histories */
		avt(&nrftcount);     /* Calcul. of nfft time steps averaged profiles  */

		for (isp=0; isp<nsp; isp++) {
			jtemp[isp] += dt*jwall[isp];
		}

		/* end of if(nfft) */
	} else {         
		avt2();  /*Calc. of nfft1 time steps averaged histories (the faster one)*/
	}

	if (--count) {
		return;		  /* accum. only every "interval" time steps */
	}

	if (hist_hi >= HISTMAX) {	
		newhist();  	           /* rescale time of histories: t-->t/4 */
	}
	
	chist(jtemp);	                         /* calculate  histories */

	hist_hi++; 
	count = interval;

}  /* END of HISTORY() */

/***************************************************************/

