#include "peg_pic.h"


/*********** Calculate nfft time steps averaged values *********/
/*************   These are different profiles     **************/

void avt(int *nrf) {
	int i, isp, s;

	if (*nrf == nfft) {
		for(isp=0; isp<nsp; isp++) {
			for(i=0; i<ng; i++) {
				srho_ave_show[isp][i]= srho_ave[isp][i]/nfft;
				srho_ave[isp][i]= srho[isp][i];

				Vx_ave_show[isp][i]= Vx_ave[isp][i]/nfft;
				Vx_ave[isp][i]= Vx[isp][i];

				if (bmag>0) {
					Vpar_ave_show[isp][i]= Vpar_ave[isp][i]/nfft;
					Vpar_ave[isp][i]= Vpar[isp][i];
				}

				Tx_ave_show[isp][i]= Tx_ave[isp][i]/nfft;
				Tx_ave[isp][i]= Tx[isp][i];

				Qh_ave_show[isp][i]= Qh_ave[isp][i]/nfft;
				Qh_ave[isp][i]= Qh[isp][i];

				Qtot_ave_show[isp][i]= Qtot_ave[isp][i]/nfft;
				Qtot_ave[isp][i]= Qtot[isp][i];

				if (T_diag) {
					T_dif_ave_show[isp][i]= T_dif_ave[isp][i]/nfft;
					T_dif_ave[isp][i]= T_dif[isp][i];
				}
			}

			for (s=0; s<NDP; s++) {
				for (i=0; i<nbin[isp] ; i++) {
					fe_mid_show[isp][s][i] = fe_mid_ave[isp][s][i]/nfft;
					fe_mid_ave[isp][s][i] = fe_mid[isp][s][i];
				}

				for (i=0; i<nbin_fvx[isp] ; i++) {
					f_vpar_show[isp][s][i] = f_vpar_ave[isp][s][i]/nfft;
					f_vpar_ave[isp][s][i] = f_vpar[isp][s][i];
				}
			}
		}             /* end of for(isp=0 ... */

		for (i=0; i<ng; i++) {
			e_ave_show[i]= e_ave[i]/nfft;
			e_ave[i]  = e[i];
			phi_ave_show[i]= phi_ave[i]/nfft;
			phi_ave[i]= phi[i];
		}

		*nrf =1;
	/* end of if (*nrf == nfft) loop */
	} else {
		for(isp=0; isp<nsp; isp++) {
			for(i=0; i<ng; i++) {
				srho_ave[isp][i]+= srho[isp][i];
				Vx_ave[isp][i]+= Vx[isp][i];
				
				if (bmag>0) {
					Vpar_ave[isp][i]+= Vpar[isp][i];
				}
				
				Tx_ave[isp][i]+= Tx[isp][i];
				Qh_ave[isp][i]+= Qh[isp][i];
				Qtot_ave[isp][i]+= Qtot[isp][i];
				
				if (T_diag) {
					T_dif_ave[isp][i]+= T_dif[isp][i];
				}
			}

			for (s=0; s<NDP; s++) {
				for (i=0; i<nbin[isp] ; i++) {
					fe_mid_ave[isp][s][i] += fe_mid[isp][s][i];
				}

				for (i=0; i<nbin_fvx[isp] ; i++) {
					f_vpar_ave[isp][s][i] +=f_vpar[isp][s][i];
				}
			}
		}

		for (i=0; i<ng; i++) {
			e_ave[i]   += e[i];
			phi_ave[i] += phi[i];
		}

		*nrf +=1;
	}
}    /* END of avt() */

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