#include "peg_pic.h"

#ifdef WITH_XGRAFIX
#include "xgrafix/xgrafix.h"
#endif

/**  xwindows with diagnostics  **/

void InitWindows(int argc, char *argv[]) {
	int i, s;
	char **buffer1, **buffer2, **buffer3, **buffer4;   

	XGInit(argc, argv, &t);
	XGSet2D("linlin", "Time", "Number(t)", "iconic", 100, 400, 1.0, 1.0, True, True);
	
	for (i=nsp-1; i>=0; i--) {
		XGCurve(t_array, np_hist[i], &hist_hi, i);
	}

	XGSet2D("linlin", "X", "Density n(x)", "iconic", 600, 500, 1.0, nc2p/area/dx, False, True, 0.0, length);

	for (i=nsp-1; i>=0; i--) {
		XGCurve(x_array, srho[i], &ng, i);
	}

	if (field) {
		XGSet2D("linlin", "X", "E field(x)", "iconic", 500, 10, 1.0, 1.0, False, True,0.0, length);
		XGCurve(x_array, e, &ng, 3);

		XGSet2D("linlin", "X", "Potential(x)", "iconic", 400, 300, 1.0, 1.0, False, True,  0.0, length); 
		XGCurve(x_array, phi, &ng, 4);

		XGSet2D("linlin", "Time", "LHS Potential(t)", "iconic", 700, 400, 1.0, 1.0, True, True);
		XGCurve(t_array, com_phi_hist[0], &hist_hi, 4);

		XGSet2D("linlin", "Time", "RHS Potential(t)", "iconic", 500, 400, 1.0, 1.0, True, True);
		XGCurve(t_array, com_phi_hist[1], &hist_hi, 4);
	}

	if (slow) {
		/*  x is not a float any more */
		XGSet2D("linlin", "X", "Vx-X Phase Space", "iconic", 300, 10, dx, dx/dt,
				False, False, 0.0, length, -vxscale, vxscale);
		for (i=nsp-1; i>=0; i--) {
			XGScat2D(xd[i], vx[i], &np[i], i);
		}

		XGSet2D("linlin", "Time", "LHS Flux(t)", "iconic", 100, 400, 1.0, 1.0, True, True);
		for (i=nsp-1; i>=0; i--) {
			XGCurve(t_array, F_hist[i], &hist_hi, i);
		}

		XGSet2D("linlin", "Time", "LHS Energy Flux(t)", "iconic", 100, 400, 1.0, 1.0, True, True);
		for (i=nsp-1; i>=0; i--) {
			XGCurve(t_array, Eflhs_hist[i], &hist_hi, i);
		}

		XGSet2D("linlin", "Time", "RHS Flux(t)", "iconic", 100, 400, 1.0, 1.0, True, True);
		for (i=nsp-1; i>=0; i--) {
			XGCurve(t_array, F_histRHS[i], &hist_hi, i);
		}

		XGSet2D("linlin", "Time", "RHS Energy Flux(t)", "iconic", 100, 400, 1.0, 1.0, True, True);
		for (i=nsp-1; i>=0; i--) {
			XGCurve(t_array, Efrhs_hist[i], &hist_hi, i);
		}

		if (T_diag) {
			XGSet2D("linlin", "Time", "T(t)", "iconic", 100, 400, 1.0, 1.0, True, True);
			for (i=nsp-1; i>=0; i--) {
				XGCurve(t_array, Tx_hist[i], &hist_hi, i);
			}

			XGSet2D("linlin", "Time", "Tpar-Tperp(t)", "iconic", 100, 400, 1.0, 1.0, True, True);
			for (i=nsp-1; i>=0; i--) {
				XGCurve(t_array, T_dif_hist[i], &hist_hi, i);
			}

			XGSet2D("linlin", "Time", "Vx(t)", "iconic", 100, 500, 1.0, dx/dt, True, True);
			for (i=nsp-1; i>=0; i--) {
				XGCurve(t_array, Vxt[i], &hist_hi, i);
			}

			XGSet2D("linlin", "X", "T_par-T_perp(x)", "iconic", 600, 500, 1.0,  1.0,
					False, True, 0.0, length);
			for (i=nsp-1; i>=0; i--) {
				XGCurve(x_array, T_dif_ave_show[i], &ng, i);
			}


			if (ccc > 1) { 
				XGSet2D("linlin", "Time", "T_01(t)", "iconic", 100, 400, 1.0, 1.0, True, True);
				XGCurve(t_array, Teq, &hist_hi, 2); 
			}
		}

		XGSet2D("linlin", "X", "rho(x)", "iconic", 400, 100, 1.0, 1.0, False, True, 0.0, length);
		XGCurve(x_array, rho, &ng, 2);

		XGSet2D("linlin", "X", "Vx(x)", "iconic", 600, 500, 1.0,  dx/dt, False, True, 0.0, length);
		for (i=nsp-1; i>=0; i--) {
			XGCurve(x_array, Vx[i], &ng, i);
		}

		XGSet2D("linlin", "X", "T_simult.(x)", "iconic", 600, 500, 1.0,  1.0, False, True, 0.0, length);
		for (i=nsp-1; i>=0; i--) {
			XGCurve(x_array, Tx[i], &ng, i);
		}

		if (extc) {
			XGSet2D("linlin", "Time", "Current(t)", "iconic", 600, 400, 1.0, 1.0, True, True);
			XGCurve(t_array, com_cur_hist, &hist_hi, 4);
		}

		XGSet2D("linlin", "Time", "Wall Charge(t)", "iconic", 600, 200, 1.0, 1.0, True, True);
		for (i=nsp-1; i>=0; i--) {
			XGCurve(t_array, jwall_hist[i], &hist_hi, i);
		}

		buffer1 = (char **)malloc(nsp*sizeof(char *));

		for(i=0; i<nsp; i++) {
			buffer1[i]= (char *)malloc(10*sizeof(char));
			sprintf(buffer1[i], "f(E)_L %d", i+1);

			XGSet2D("linlin", "E", buffer1[i], "iconic", 650, 350, Escale[i], 1.0,
					False, True, e_array[i][0]*Escale[i], e_array[i][nbin[i]-1]*Escale[i]);
			XGCurve(e_array[i], fe[i], &nbin[i], i);
		}

		buffer2 = (char **)malloc(nsp*sizeof(char *));

		for(i=0; i<nsp; i++) {
			buffer2[i]= (char *)malloc(10*sizeof(char));
			sprintf(buffer2[i], "f(E)_R %d", i+1);

			XGSet2D("linlin", "E", buffer2[i], "iconic", 650, 350, Escale[i], 1.0,
					False, True, e_array[i][0]*Escale[i], e_array[i][nbin[i]-1]*Escale[i]);
			XGCurve(e_array[i], feR[i], &nbin[i], i);
		}

		buffer3 = (char **)malloc(nsp*sizeof(char *));

		for(i=0; i<nsp; i++) {
			buffer3[i]= (char *)malloc(13*sizeof(char));

			for (s=0; s<NDP; s++) {
				sprintf(buffer3[i], "f(E) Mid %d", i+1);
				XGSet2D("linlin", "E", buffer3[i], "iconic", 650, 350, Escale[i], 1.0,
						False, True, emin[i]*Escale[i], e_array[i][nbin[i]-1]*Escale[i]);
				XGCurve(e_array[i], fe_mid_show[i][s], &nbin[i], i);
			}
		}


		buffer4 = (char **)malloc(nsp*sizeof(char *));

		for(i=0; i<nsp; i++) {
			buffer4[i]= (char *)malloc(13*sizeof(char));

			for (s=0; s<NDP; s++) {
				sprintf(buffer4[i], "f(v) Mid %d", i+1);
				XGSet2D("linlin", "v", buffer4[i], "iconic", 650, 350, vscale, 1.0,
						False, True, vx_min[i]*vscale, fvx_array[i][nbin_fvx[i]-1]*vscale);
				XGCurve(fvx_array[i], f_vpar_show[i][s], &nbin_fvx[i], i);
			}
		}

		XGSet2D("linlin", "Theta", "f(theta)", "iconic", 600, 350, 180/PI, 1.0,
				False, True, 0.0, 90.0);
		for (i=nsp-1; i>=0; i--) {
			XGCurve(th_array[i], ftheta[i], &nbin[i], i);
		}

		XGSet2D("linlog", "Time", "Ave. Kinetic Energy(t) [eV]", "iconic", 500, 200,
				1.0, 1.0, True, True);
		for (i=nsp-1; i>=0; i--) {
			XGCurve(t_array, kes_hist[i], &hist_hi, i);
		}

		if (field) {
			XGSet2D("linlog", "Time", "Field Energy(t) [eV]", "iconic", 650, 400, 1.0, 1.0, True, True);
			XGCurve(t_array, ese_hist, &hist_hi, 4);
		}


		if((bmag>0.) || Nnum) {
			XGSet2D("linlin", "Vx", "Vy-Vx Phase Space", "iconic", 400, 10, dx/dt, dx/dt,
					False, False, -vxscale, vxscale, -vxscale, vxscale);
			for (i=nsp-1; i>=0; i--) {
				XGScat2D(vx[i], vy[i], &np[i], i);
			}
		}


		if(nfft) {
			if (field) {
				XGSet2D("linlin", "X", "Time Ave. E field(x)", "iconic", 10, 500, 1.0, 1.0,
						False, True, 0.0, length);
				XGCurve(x_array, e_ave_show, &ng, 3);

				XGSet2D("linlin", "X", "Time Ave. Potential(x)", "iconic", 400, 300, 1.0, 1.0,
						False, True, 0.0, length);
				XGCurve(x_array, phi_ave_show, &ng, 4);
			}

			XGSet2D("linlin", "X", "Time Ave. Density n(x)", "iconic", 600, 500, 1.0, nc2p/area/dx,
					False, True, 0.0, length);
			for (i=nsp-1; i>=0; i--) {
				XGCurve(x_array, srho_ave_show[i], &ng, i);
			}

			XGSet2D("linlin", "X", "Time Ave. Vx(x)", "iconic", 600, 500, 1.0, dx/dt, 
					False, True, 0.0, length);
			for (i=nsp-1; i>=0; i--) {
				XGCurve(x_array, Vx_ave_show[i], &ng, i);
			}

			if (bmag > 0.) {
				XGSet2D("linlin", "X", "Time Ave. Vpar(x)", "iconic", 600, 500, 1.0, dx/dt,
						False, True, 0.0, length);
				for (i=nsp-1; i>=0; i--) {
					XGCurve(x_array, Vpar_ave_show[i], &ng, i);
				}
			}

			XGSet2D("linlin", "X", "T(x)", "iconic", 600, 500, 1.0, 1.0, False, True, 0.0, length);
			for (i=nsp-1; i>=0; i--) {
				XGCurve(x_array, Tx_ave_show[i], &ng, i);
			}

			XGSet2D("linlin", "X", "q_x_heat(x)", "iconic", 600, 500, 1.0, 1.0, False, True, 0.0, length);
			for (i=nsp-1; i>=0; i--) {
				XGCurve(x_array, Qh_ave_show[i], &ng, i);
			}

			XGSet2D("linlin", "X", "q_x_total(x)", "iconic", 600, 500, 1.0, 1.0, False, True, 0.0, length);
			for (i=nsp-1; i>=0; i--) {
				XGCurve(x_array, Qtot_ave_show[i], &ng, i);
			}

			if (Nnum) {
				XGSet2D("linlin", "X", "Neutral density", "iconic", 600, 500, 1.0, 1.0, False, True, 0.0, length);
				for (i=Nnum-1; i>=0; i--) {
					XGCurve(x_array, Nneut[i], &ng, i);
				}

				XGSet2D("linlin", "X", "Neutral temperature", "iconic", 600, 500, 1.0, 1.0, False, True, 0.0, length);
				for (i=Nnum-1; i>=0; i--) {
					XGCurve(x_array, Tneutshow[i], &ng, i);
				}

				if(ecollisional) {
					XGSet2D("linlin", "X", "Ionization Profile(x)", "iconic", 100, 500, 1.0, 1.0, False, True, 0.0, length);
					for (i=Nnum-1; i>=0; i--) {
						XGCurve(x_array, ionrate[i], &ng, i);
					}

					XGSet2D("linlin", "X", "Excitation Profile(x)", "iconic", 200, 450, 1.0, 1.0, False, True, 0.0, length);
					for (i=Nnum-1; i>=0; i--) {
						XGCurve(x_array, extrate[i], &ng, i);
					}
				}

				if(icollns) {
					XGSet2D("linlin", "X", "Charge X Profile(x)", "iconic", 300, 550, 1.0, 1.0, False, True, 0.0, length);
					for (i=Nnum-1; i>=0; i--) {
						XGCurve(x_array, chrgxrate[i], &ng, i);
					}
				}
			}  /* end of if (Nnum) */

			XGSet2D("linlin", "Time", "Local RHS Potential(t)", "iconic", 300, 550, 1.0, 1.0, True, True);
			XGCurve(Local_t_array, phi_hist[1], &thist_hi, 3);

			XGSet2D("linlin", "Time", "Local LHS Potential(t)", "iconic", 300, 550, 1.0, 1.0, True, True);
			XGCurve(Local_t_array, phi_hist[0], &thist_hi, 3);

			if (field) {
				XGSet2D("linlog", "Freq", "Mag of RHS V(f)", "iconic", 100, 250, 1.0, 1.0, True, True);
				XGCurve(f_array, mphi_fft, &freq_hi, 4);

				XGSet2D("linlog", "Freq", "Mag of LHS V(f)", "iconic", 100, 250, 1.0, 1.0, True, True);
				XGCurve(f_array, phi_fft, &freq_hi, 4);
			}

			if (extc) {
				XGSet2D("linlin", "Time", "Local Current(t)", "iconic", 300, 550, 1.0, 1.0, True, True);
				XGCurve(Local_t_array, cur_hist, &thist_hi, 3);

				XGSet2D("linlog", "Freq", "Mag of I(f)", "iconic", 100, 250, 1.0, 1.0, True, True);
				XGCurve(f_array, cur_fft, &freq_hi, 4);
			}
		}  /* end of if (nfft) */
	}   /* end of if (slow) */
}    /* End of InitWindows */   

