package endu;
import vtk.*;

import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;

public class ReadCSV {
	public static String fileName = "endurance.csv";
	
	static {
		System.loadLibrary("vtkCommonJava");
		System.loadLibrary("vtkIOJava");
		System.loadLibrary("vtkImagingJava");
		System.loadLibrary("vtkGraphicsJava");
		System.loadLibrary("vtkRenderingJava");
	}
	public static int numRow = 0;
	
	public static vtkFloatArray[] arrayMeasure = new vtkFloatArray[8];
	public static vtkFloatArray[][] arrayMeasure2 = new vtkFloatArray[8][3];
	public static int [][] arrayMeasure2Lenght = new int[8][3];
	public static vtkFloatArray arrayX;
	public static vtkFloatArray arrayY; 
	public static vtkFloatArray arrayDepth;
	public static vtkFloatArray arrayConductivity = new vtkFloatArray();
	public static vtkFloatArray arrayTemperature = new vtkFloatArray();
	public static vtkFloatArray arrayCDOM = new vtkFloatArray();
	public static vtkFloatArray arrayChl = new vtkFloatArray();
	public static vtkFloatArray arrayREDOX = new vtkFloatArray();
	public static vtkFloatArray arrayPAR = new vtkFloatArray();
	public static vtkFloatArray arrayPH = new vtkFloatArray();
	public static vtkFloatArray arrayTurbidity = new vtkFloatArray();
	public static vtkFloatArray arrayScanNumber = new vtkFloatArray();
	public static Date[] arrayDate;
	public static String[] strDate =new String[100000];
	public static String[] strTime =new String[100000];
	

	public final static String X = "X";
	public final static String Y = "Y";
	public final static String DEPTH = "Depth (m)";
	public final static String COND = "Conductivity";
	public final static String TEMP= "Temperature";
	public final static String CDOM = "CDOM";
	public final static String CHL = "Chl-a";
	public final static String REDOX = "REDOX";
	public final static String PAR = "PAR";
	public final static String PH = "pH";
	public final static String TURB = "Turbidity";
	public final static String SCAN = "Scan number";
	public final static String[] MEASURE = {COND, TEMP, CDOM,CHL,REDOX,PAR,PH,TURB,SCAN};
	

	public static void readEndurance() {
		numRow = 0;
		File file = new File(fileName);
		arrayX = new vtkFloatArray();
		arrayY = new vtkFloatArray();
		arrayDepth = new vtkFloatArray();
		for(int i=0;i<8;i++){
			arrayMeasure[i] = new vtkFloatArray();	
		}
		
		arrayX.SetName(X);
		arrayY.SetName(Y);
		arrayDepth.SetName(DEPTH);
		arrayConductivity.SetName(COND);
		arrayTemperature.SetName(TEMP);
		arrayCDOM.SetName(CDOM);
		arrayChl.SetName(CHL);
		arrayREDOX.SetName(REDOX);
		arrayPAR.SetName(PAR);
		arrayPH.SetName(PH);
		arrayTurbidity.SetName(TURB);
		arrayScanNumber.SetName(SCAN);

		
		try {
			BufferedReader bufRdr = new BufferedReader(new FileReader(file));
			String line = null;
			int col = 0;
			try {
				line = bufRdr.readLine();
			
				while ((line = bufRdr.readLine()) != null) {
					// System.out.println(line + " Line: " +numRow);
					StringTokenizer st = new StringTokenizer(line, ",");
					while (st.hasMoreTokens()) {
						String s = st.nextToken();
						col++;
						switch (col) {
						case 1:
							arrayX.InsertNextValue(Double.parseDouble(s));
					//		System.out.println("Reading: " +Double.parseDouble(s));
							break;
						case 2:
							arrayY.InsertNextValue(Double.parseDouble(s));
//							System.out.println("arrayY:   " +Double.parseDouble(s));
							break;
						case 3:
							arrayDepth.InsertNextValue(Double.parseDouble(s));
							break;
						case 4:
							arrayMeasure[0].InsertNextValue(Double.parseDouble(s));
							arrayConductivity.InsertNextValue(Double.parseDouble(s));
							break;
						case 5:
							arrayMeasure[1].InsertNextValue(Double.parseDouble(s));
							arrayTemperature.InsertNextValue(Double.parseDouble(s));
							break;
						case 6:
							arrayMeasure[2].InsertNextValue(Double.parseDouble(s));
							arrayCDOM.InsertNextValue(Double.parseDouble(s));
							break;
						case 7:
							arrayMeasure[3].InsertNextValue(Double.parseDouble(s));
							arrayChl.InsertNextValue(Double.parseDouble(s));
							break;
						case 8:
							arrayMeasure[4].InsertNextValue(Double.parseDouble(s));
							arrayREDOX.InsertNextValue(Double.parseDouble(s));
							break;
						case 9:
							arrayMeasure[5].InsertNextValue(Double.parseDouble(s));
							arrayPAR.InsertNextValue(Double.parseDouble(s));
							break;
						case 10:
							arrayMeasure[6].InsertNextValue(Double.parseDouble(s));
							arrayPH.InsertNextValue(Double.parseDouble(s));
							break;
						case 11:
							arrayMeasure[7].InsertNextValue(Double.parseDouble(s));
							arrayTurbidity.InsertNextValue(Double.parseDouble(s));
							break;
						case 12:
							arrayScanNumber.InsertNextValue(Double.parseDouble(s));
							break;
						case 13:
							strDate[numRow]=s;
							break;
						case 14:
							strTime[numRow]=s;
							break;
						}
					}
					col = 0; numRow++;
				}
				System.out.println("numRow: " +numRow);
				EnduModel.actorArray = new vtkActor[8][ReadCSV.numRow];

				
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static Date parseDate(String s) {
		Date d = new Date();
		try {
			d = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss").parse(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		;
		// System.out.println(d);
		return d;
	}

}
