/*
outputNear1d.c

plot and log of near2d (2D)
*/

#include "oth.h"
#include "complex.h"
#include "oth_prototype.h"

void outputNear1d(void)
{
	// log
	FILE *fp;
	if ((fp = fopen(FN_near1d, "w")) == NULL) {
		printf("*** %s open error.\n", FN_near1d);
		return;
	}

	const char fmt[] = "%4d%12.3e%12.3e%12.3e%12.4e%12.4e%9.3f%12.4e%9.3f%12.4e%9.3f\n";

	double *amp[3], *deg[3], pos[2][3];
	double *lng = NULL;

	for (int n = 0; n < NNear1d; n++) {
		// division and position
		int div = 0;
		if      (Near1d[n].dir == 'X') {
			lng = Xn;
			div = Nx;
			pos[0][0] = lng[0];
			pos[1][0] = lng[div];
			pos[0][1] = pos[1][1] = Yn[Near1d[n].id1];
			pos[0][2] = pos[1][2] = Zn[Near1d[n].id2];
		}
		else if (Near1d[n].dir == 'Y') {
			lng = Yn;
			div = Ny;
			pos[0][1] = lng[0];
			pos[1][1] = lng[div];
			pos[0][2] = pos[1][2] = Zn[Near1d[n].id1];
			pos[0][0] = pos[1][0] = Xn[Near1d[n].id2];
		}
		else if (Near1d[n].dir == 'Z') {
			lng = Zn;
			div = Nz;
			pos[0][2] = lng[0];
			pos[1][2] = lng[div];
			pos[0][0] = pos[1][0] = Xn[Near1d[n].id1];
			pos[0][1] = pos[1][1] = Yn[Near1d[n].id2];
		}

		// alloc
		for (int m = 0; m < 3; m++) {
			amp[m] = (double *)malloc((div + 1) * sizeof(double));
			deg[m] = (double *)malloc((div + 1) * sizeof(double));
		}

		for (int ifreq = 0; ifreq < NFrequency; ifreq++) {
			// log
			const int cmp = toupper(Near1d[n].cmp[0]);
			fprintf(fp, "#%d : frequency[Hz] = %.3e\n", (n + 1), Frequency[ifreq]);
			fprintf(fp, "%s", " No.     X[m]        Y[m]        Z[m]       ");
			fprintf(fp, "%s\n",
				(cmp == 'E') ? "E[V/m]      Ex[V/m]   Ex[deg]    Ey[V/m]   Ey[deg]    Ez[V/m]   Ez[deg]" :
				(cmp == 'H') ? "H[A/m]      Hx[A/m]   Hx[deg]    Hy[A/m]   Hy[deg]    Hz[A/m]   Hz[deg]" :
				(cmp == 'I') ? "I[A]        Ix[A]     Ix[deg]    Iy[A]     Iy[deg]    Iz[A]     Iz[deg]" : "");

			// field
			for (int l = 0; l <= div; l++) {
				// i, j, k
				int i = 0, j = 0, k = 0;
				if      (Near1d[n].dir == 'X') {
					i = l;
					j = Near1d[n].id1;
					k = Near1d[n].id2;
				}
				else if (Near1d[n].dir == 'Y') {
					i = Near1d[n].id2;
					j = l;
					k = Near1d[n].id1;
				}
				else if (Near1d[n].dir == 'Z') {
					i = Near1d[n].id1;
					j = Near1d[n].id2;
					k = l;
				}

				// field
				d_complex_t f[3];
				if      (Near1d[n].cmp[0] == 'E') {
					enode(ifreq, i, j, k, Near1dExclude, f);
				}
				else if (Near1d[n].cmp[0] == 'H') {
					hnode(ifreq, i, j, k, Near1dExclude, f);
					for (int m = 0; m < 3; m++) {
						f[m] = d_rmul(1 / ETA0, f[m]);  // to A/m
					}
				}
				else if (Near1d[n].cmp[0] == 'I') {
					cnode(ifreq, i, j, k, f);
				}
				for (int m = 0; m < 3; m++) {
					amp[m][l] = d_abs(f[m]);
					deg[m][l] = d_deg(f[m]);
				}

				// log
				fprintf(fp, fmt,
					l, Xn[i], Yn[j], Zn[k],
					sqrt(d_norm(f[0]) + d_norm(f[1]) + d_norm(f[2])),
					d_abs(f[0]), d_deg(f[0]),
					d_abs(f[1]), d_deg(f[1]),
					d_abs(f[2]), d_deg(f[2]));
			}

			// plot
			plot2dNear1d0(
				Near1d[n].cmp, div, amp, deg, lng,
				Near1dScale.db, Near1dScale.user, Near1dScale.min, Near1dScale.max, Near1dScale.div,
				Title, Frequency[ifreq], pos,
				Width2d, Height2d, Fontsize2d);
		}

		// free
		for (int m = 0; m < 3; m++) {
			free(amp[m]);
			free(deg[m]);
		}
	}
}
