#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "header.h"
#define MYIO_C

/*
 * functions in this file are related to output.
 * plot_...-functions print out files in gnuplot format.
 * and create_pdf is used in these functions to
 * process the gnuplot files into pdf-format and remove all temporary files.
 * */
void write_phi(char *out, double *data, int X, int Y){
	FILE *file;
	int x, y, ix;
	file=fopen(out, "w");
	for(x=0; x<X; x++){
		for(y=0; y<Y; y++){
			ix=x*X+y;
			fprintf(file, "%3d %3d %3d % .16e\n", x, y, ix, data[ix]);
		}
	}
	fclose(file);
}

void print_phi(double *data, int X, int Y){
	int x, y;
	for(y=0; y<Y; y++){
		for(x=0; x<X; x++){
			printf( "%d %d % .2e\t", x, y, data[y*X+x]);
		}
		printf("\n");
	}
	printf("\n");
	fflush(stdout);
}


void create_pdf(char *outdir, char *plot){
	FILE *file;
	char *output;
	output=malloc(1000*sizeof(char));
	sprintf(output, "%s/plot.sh", outdir);
	check_file(output, "w");
	file=fopen(output, "w");
	fprintf(file, "#!/bin/bash\n");
	fprintf(file, "cd %s\n", outdir);
	fprintf(file, "gnuplot %s.gp\n", plot);
	fprintf(file, "latex %s.tex\n", plot);
	fprintf(file, "dvipdf %s.dvi\n", plot);
	fprintf(file, "rm %s.gp\n", plot);
	fprintf(file, "rm %s.tex\n", plot);
	fprintf(file, "rm %s.dvi\n", plot);
	fprintf(file, "rm %s.aux\n", plot);
	fprintf(file, "rm %s-inc.eps\n", plot);
	fprintf(file, "rm %s.log\n", plot);
	fclose(file);

	sprintf(output, "sh %s/plot.sh\n", outdir);
	system(output);
	sprintf(output, "rm %s/plot.sh\n", outdir);
	system(output);
	free(output);
}

void plot_data_color(char *outdir, char *plot, char *title, char *xlabel, char *ylabel,
		double *range, int X, int Y, int npi, plotinfo *pi){
	FILE *file;
	char *out;
	int i, L;
	double xcm, ycm;
	if(X>Y)
		L=X;
	else
		L=Y;
	xcm=14*(double)X/L;
	ycm=14*(double)Y/L;

	out=malloc(1000*sizeof(char));
	sprintf(out, "%s/%s.gp", outdir, plot);
	check_file(out, "w");
	file=fopen(out, "w");
	fprintf(file, "reset\n");
	fprintf(file, "set term epslatex standalone color size %fcm,%fcm solid\n", xcm, ycm);
	fprintf(file, "set out sprintf(\'%s/%s.tex\')\n", outdir, plot);
	for(i=0; i<npi; i++)
		fprintf(file, "data%d = sprintf(\'%s/%s\')\n", i, outdir, pi[i].data);

	fprintf(file, "unset key\n");
	fprintf(file, "set title \'%s\'\n", title);
	fprintf(file, "set pm3d map\n");
	fprintf(file, "set samples %d; set isosamples %d\n", X*Y, X*Y);
	fprintf(file, "set ytics out\n");
	fprintf(file, "set xtics out rotate by -90\n");
	fprintf(file, "set xlabel \'%s\'\n", xlabel);
	fprintf(file, "set ylabel \'%s\' offset +1.5\n", ylabel);

	if(range[0]<range[1])
		fprintf(file, "set xrange[%f:%f]\n", range[0], range[1]);
	if(range[2]<range[3])
		fprintf(file, "set yrange[%f:%f]\n", range[2], range[3]);


	fprintf(file, "splot ");
	for(i=0; i<npi-1; i++){
		fprintf(file, "data%d,\\\n",i);
	}
	fprintf(file, "data%d",i);

	fclose(file);
	create_pdf(outdir, plot);
	free(out);
}

void plot_data(char *outdir, char *plot, char *title, char *xlabel, char *ylabel,
		double *range, int npi, plotinfo *pi){
	FILE *file;
	char *out;
	int i;

	out=malloc(1000*sizeof(char));
	sprintf(out, "%s/%s.gp", outdir, plot);
	check_file(out, "w");
	file=fopen(out, "w");
	fprintf(file, "reset\n");
	fprintf(file, "set term epslatex standalone color size 24cm,14cm solid\n");
	fprintf(file, "set out sprintf(\'%s/%s.tex\')\n", outdir, plot);
	for(i=0; i<npi; i++)
		fprintf(file, "data%d = sprintf(\'%s/%s\')\n", i, outdir, pi[i].data);

	fprintf(file, "set key below\n");
	fprintf(file, "set title \'%s\'\n", title);

	if(range[0]<range[1])
		fprintf(file, "set xrange[%f:%f]\n", range[0], range[1]);
	if(range[2]<range[3])
		fprintf(file, "set yrange[%f:%f]\n", range[2], range[3]);

	fprintf(file, "set xlabel \'%s\'\n", xlabel);
	fprintf(file, "set ylabel \'%s\'\n", ylabel);
	fprintf(file, "plot ");
	for(i=0; i<npi-1; i++){
		fprintf(file, "data%d using %d:%d title \'%s\' lc rgb \"%s\" lw %d pt %d,\\\n",
				i, pi[i].x, pi[i].y,  pi[i].label, pi[i].color, pi[i].lw, pi[i].pt);
	}
	fprintf(file, "data%d using %d:%d title \'%s\' lc rgb \"%s\" lw %d pt %d",
					i, pi[i].x, pi[i].y, pi[i].label, pi[i].color, pi[i].lw, pi[i].pt);

	fclose(file);
	create_pdf(outdir, plot);
	free(out);
}

void plot_data_vecfield(char *outdir, char *plot, char *title, char *xlabel, char *ylabel,
		double *range, int X, int Y, int npi, plotinfo *pi){
	FILE *file;
	char *out;
	int i, L;
	double xcm, ycm;
	if(X>Y)
		L=X;
	else
		L=Y;
	xcm=14*(double)X/L;
	ycm=14*(double)Y/L;

	out=malloc(1000*sizeof(char));
	sprintf(out, "%s/%s.gp", outdir, plot);
	check_file(out, "w");
	file=fopen(out, "w");
	fprintf(file, "reset\n");
	fprintf(file, "set term epslatex standalone color size %fcm,%fcm solid\n", xcm, ycm);
	fprintf(file, "set out sprintf(\'%s/%s.tex\')\n", outdir, plot);
	for(i=0; i<npi; i++)
		fprintf(file, "data%d = sprintf(\'%s/%s\')\n", i, outdir, pi[i].data);

	fprintf(file, "unset key\n");
	fprintf(file, "set title \'%s\'\n", title);

	if(range[0]<range[1])
		fprintf(file, "set xrange[%f:%f]\n", range[0], range[1]);
	if(range[2]<range[3])
		fprintf(file, "set yrange[%f:%f]\n", range[2], range[3]);

	fprintf(file, "set xlabel \'%s\'\n", xlabel);
	fprintf(file, "set ylabel \'%s\'\n", ylabel);
	fprintf(file, "plot ");
	for(i=0; i<npi-1; i++){
		fprintf(file, "data%d using %d:%d:%d:%d w vec every %d:%d lc rgb \"%s\" lw %d,\\\n",
				i, pi[i].x, pi[i].y, pi[i].u, pi[i].v,(int)L/14,(int)L/14, pi[i].color, pi[i].lw);
	}
	fprintf(file, "data%d using %d:%d:%d:%d w vec every %d:%d lc rgb \"%s\" lw %d",
					i, pi[i].x, pi[i].y, pi[i].u, pi[i].v,(int)L/14,(int)L/14, pi[i].color, pi[i].lw);
	fclose(file);
	create_pdf(outdir, plot);
	free(out);
}



