package test;

import java.io.InputStream;
import java.io.InputStreamReader;

import oneLiners.AsciiMatrixFile;
import oneLiners.OneLiners;

import algorithmrepository.contouring.BoundedContouring;
import algorithmrepository.contouring.ContourCollector;
import algorithmrepository.contouring.Contouring;

public class TestContouring {
	
	public static void main(String[] args) throws Exception {
		new TestContouring().testContouring();			
	}
	
	public void testContouring() throws Exception {
		InputStream in = this.getClass().getResourceAsStream("/test/fw2.txt");		
		double[][] fw = AsciiMatrixFile.load(in, "fw2", false);
		
		in = this.getClass().getResourceAsStream("/test/psi.txt");		
		double[] psi = AsciiMatrixFile.load(in, "psi", false)[0];
		
		System.out.println("Psi 50x60");
		OneLiners.dumpArray(psi);

		double R0 = 0.624;
		double R1 = 1.136;
		double Z0 = -0.75;
		double Z1 = 0.75;
		int contourNR = 50;
		int contourNZ = 60;
		
		Contouring.GridFunction psiContour = null;
		BoundedContouring bCntr = new BoundedContouring(null, R0, R1, contourNR, 
				Z0, Z1, contourNZ,
				psiContour, fw[0], fw[1] );
		
		
		psiContour = new Contouring.FlatArrayGrid(psi, 50);		
		bCntr.setF(psiContour);
		
		ContourCollector c = new ContourCollector();
		bCntr.setCallbacks(c);
		
		bCntr.getAllGridContours(OneLiners.linSpace(OneLiners.min(psi), OneLiners.max(psi), 20));
		System.out.println(c);
		c.writeSVGAllContours("/tmp/allContours.svg", new double[]{ R0, Z0, R1, Z1 }, true);
		
		
		double magr = 0.895673469387755;
		double magz = 0.26694915254237284;
		int lcfsNumBisects = 50;
		double targetPsiLCFSToFOFS = 1.0e-4;
		
		c.clear();
		
		double[] ret = bCntr.getLastClosedContour(magr, magz,
				lcfsNumBisects, targetPsiLCFSToFOFS, false);
		
		System.out.println("bracket = " + ret[0] + "\t" + ret[1]);
				
		System.out.println(c);
		c.writeSVGAllContours("/tmp/lcfs.svg", new double[]{ R0, Z0, R1, Z1 }, true);
		
		
	}
}
