package com.planetmayo.debrief.satc.zigdetector;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import au.com.bytecode.opencsv.CSVReader;

public class Track
{

	public static Track read(final String path) throws IOException
	{
		Track res = null;
		// check it exists
		final File theFile = new File(path);
		if (theFile.exists())
		{
			res = new Track(path);
		}

		return res;
	}

	final private long[] dates;
	final private double[] x;
	final private double[] y;
	final private double[] courses;
	final private double[] speeds;
//	public double[] averageCourses;
//	public double[] averageSpeeds;

	public Track(final String path) throws IOException
	{

		final DateTimeFormatter formatter = DateTimeFormat
				.forPattern("yyMMdd HHmmss");

		// Filename containing the data
		// String csvFilename = "data/ArcTan_Data.csv";

		// schema
		// 100112 120000,SENSOR,12000.00 ,24000.00 ,0.00 ,205.00 ,4.12 ,100.00
		// ,inactive

		// Reading the csv file but ignoring the first column since it contains
		// headings the result is stored in a list
		CSVReader csvReader = null;

		try
		{
			final FileReader fReader = new FileReader(path);
			csvReader = new CSVReader(fReader, ',', '\'', 1);
			final List<String[]> content = csvReader.readAll();
			// variable to hold each row of the List while iterating through it
			String[] row = null;

			// counter used to populate the variables with data from the csv
			// file
			int counter = 0;

			// initializing the variables to hold data in the csv file
			x = new double[content.size()];
			y = new double[content.size()];
			speeds = new double[content.size()];
			courses = new double[content.size()];
			dates = new long[content.size()];

			for (final Object object : content)
			{
				row = (String[]) object;
				/* parsing data from the list to the variables */
				final String thisDate = row[0].toString();
				dates[counter] = formatter.parseDateTime(thisDate).getMillis();
				x[counter] = (Double.parseDouble(row[2].toString()));
				y[counter] = (Double.parseDouble(row[3].toString()));
				courses[counter] = (Double.parseDouble(row[5].toString()));
				speeds[counter] = (Double.parseDouble(row[6].toString()));
				counter++;
			}
		}
		finally
		{
			if (csvReader != null)
			{
				csvReader.close();
			}
		}
	}

	public double[] getCourses()
	{
		return courses;
	}

	public long[] getDates()
	{
		return dates;
	}

	public double[] getSpeeds()
	{
		return speeds;
	}

	public double[] getX()
	{
		return x;
	}

	public double[] getY()
	{
		return y;
	}

}

@@ -42,16 +42,17 @@
 tector;%0A
+%0A
 import j
@@ -672,19 +672,21 @@
 speeds;%0A
+%0A%09
 //
-%09
+ 
 public d
@@ -713,11 +713,12 @@
 es;%0A
+%09
 //
-%09
+ 
 publ
@@ -884,16 +884,20 @@
 d HHmmss
+.SSS
 %22);%0A%0A%09%09/
@@ -1229,16 +1229,113 @@
  null;%0A%0A
+%09%09// counter used to populate the variables with data from the csv%0A%09%09// file%0A%09%09int counter = 0;%0A%0A
 %09%09try%0A%09%09
@@ -1596,108 +1596,8 @@
 l;%0A%0A
-%09%09%09// counter used to populate the variables with data from the csv%0A%09%09%09// file%0A%09%09%09int counter = 0;%0A%0A
 %09%09%09/
@@ -2013,24 +2013,103 @@
 toString();%0A
+%09%09%09%09if (thisDate == null %7C%7C thisDate.length() == 0)%0A%09%09%09%09%7B%0A%09%09%09%09%09continue;%0A%09%09%09%09%7D%0A
 %09%09%09%09dates%5Bco
@@ -2403,24 +2403,25 @@
 String()));%0A
+%0A
 %09%09%09%09counter+
@@ -2446,16 +2446,70 @@
 lly%0A%09%09%7B%0A
+//%09%09%09System.out.println(%22Counter got to:%22 + counter);%0A
 %09%09%09if (c
@@ -2559,20 +2559,21 @@
 );%0A%09%09%09%7D%0A
-
 %09%09%7D%0A
+%0A
 %09%7D%0A%0A%09pub

make Track reader more robust, also handle data with millisecond resolution