1. Robert Massaioli
  2. peru-averager

Commits

Robert Massaioli  committed 381f96f

Initial Import of the code.

I think it works but it pays to be safe.

  • Participants
  • Branches master

Comments (0)

Files changed (9)

File .classpath

View file
  • Ignore whitespace
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="lib" path="libs/commons-io-2.4.jar"/>
+	<classpathentry kind="lib" path="libs/guava-12.0.jar"/>
+	<classpathentry kind="lib" path="libs/commons-lang3-3.1.jar"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

File .gitignore

View file
  • Ignore whitespace
+*~
+.*.swp
+
+bin

File .project

View file
  • Ignore whitespace
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>peru-averager</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

File libs/commons-io-2.4.jar

  • Ignore whitespace
Binary file added.

File libs/commons-lang3-3.1.jar

  • Ignore whitespace
Binary file added.

File libs/guava-12.0.jar

  • Ignore whitespace
Binary file added.

File src/peru/averages/AverageRow.java

View file
  • Ignore whitespace
+package peru.averages;
+
+import java.math.BigDecimal;
+
+public class AverageRow {
+	private BigDecimal	dt, tr, windSpeed, temperature, rh, dp, bp;
+	private BigDecimal	count;
+
+	public AverageRow() {
+		dt = new BigDecimal(0);
+		tr = new BigDecimal(0);
+		windSpeed = new BigDecimal(0);
+		temperature = new BigDecimal(0);
+		rh = new BigDecimal(0);
+		dp = new BigDecimal(0);
+		bp = new BigDecimal(0);
+		count = new BigDecimal(0);
+	}
+
+	public void addDataRow(DataRow row) {
+		dt = dt.add(row.getDt());
+		tr = tr.add(row.getTr());
+		windSpeed = windSpeed.add(row.getWindSpeed());
+		temperature = temperature.add(row.getTemperature());
+		rh = rh.add(row.getRh());
+		dp = dp.add(row.getDp());
+		bp = bp.add(row.getBp());
+
+		count = count.add(BigDecimal.ONE);
+	}
+
+	public BigDecimal getDt() {
+		if (count.equals(BigDecimal.ZERO)) return BigDecimal.ZERO;
+		return new BigDecimal(dt.doubleValue() / count.doubleValue());
+	}
+
+	public BigDecimal getTr() {
+		if (count.equals(BigDecimal.ZERO)) return BigDecimal.ZERO;
+		return new BigDecimal(tr.doubleValue() / count.doubleValue());
+	}
+
+	public BigDecimal getWindSpeed() {
+		if (count.equals(BigDecimal.ZERO)) return BigDecimal.ZERO;
+		return new BigDecimal(windSpeed.doubleValue() / count.doubleValue());
+	}
+
+	public BigDecimal getTemperature() {
+		if (count.equals(BigDecimal.ZERO)) return BigDecimal.ZERO;
+		return new BigDecimal(temperature.doubleValue() / count.doubleValue());
+	}
+
+	public BigDecimal getRh() {
+		if (count.equals(BigDecimal.ZERO)) return BigDecimal.ZERO;
+		return new BigDecimal(rh.doubleValue() / count.doubleValue());
+	}
+
+	public BigDecimal getDp() {
+		if (count.equals(BigDecimal.ZERO)) return BigDecimal.ZERO;
+		return new BigDecimal(dp.doubleValue() / count.doubleValue());
+	}
+
+	public BigDecimal getBp() {
+		if (count.equals(BigDecimal.ZERO)) return BigDecimal.ZERO;
+		return new BigDecimal(bp.doubleValue() / count.doubleValue());
+	}
+
+}

File src/peru/averages/DataConverter.java

View file
  • Ignore whitespace
+package peru.averages;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.nio.charset.Charset;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.List;
+import java.util.Locale;
+import java.util.Vector;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Lists;
+
+public class DataConverter {
+	public static void main(String[] args) {
+		System.out.println("== Attempting to process the data ==");
+
+		List<DataRow> rows = null;
+		try {
+			rows = parseData(new File("peru_data.csv"));
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+
+		generateOutput(rows, new File("output.csv"));
+
+		System.out.println("== Finished Processing the Data ==");
+	}
+
+	private static final int	HOURS_PER_DAY		= 24;
+	private static final int	SECTIONS_PER_HOUR	= 4;
+	private static final int	TOTAL_SECTIONS		= HOURS_PER_DAY * SECTIONS_PER_HOUR;
+
+	private static void generateOutput(List<DataRow> rows, File output) {
+		final Vector<AverageRow> averages = new Vector<AverageRow>();
+
+		for (int i = 0; i < TOTAL_SECTIONS; ++i) {
+			averages.add(new AverageRow());
+		}
+
+		int rowCount = 0;
+		final Calendar currentDate = new GregorianCalendar();
+		for (DataRow row : rows) {
+			System.out.println(rowCount);
+			currentDate.setTime(row.getDate());
+			final int currentHour = currentDate.get(Calendar.HOUR_OF_DAY);
+			final int currentMinute = currentDate.get(Calendar.MINUTE);
+			final int currentSection = currentMinute * SECTIONS_PER_HOUR / 60;
+			final int offset = currentHour * SECTIONS_PER_HOUR + currentSection;
+
+			averages.get(offset).addDataRow(row);
+			rowCount++;
+		}
+
+		final List<String> outputLines = new ArrayList<String>(TOTAL_SECTIONS);
+		for (int offset = 0; offset < TOTAL_SECTIONS; ++offset) {
+			final AverageRow averageRow = averages.get(offset);
+			final int hour = offset / SECTIONS_PER_HOUR;
+			final int section = offset % SECTIONS_PER_HOUR;
+			final int minutes = 60 * section / SECTIONS_PER_HOUR;
+
+			final List<String> data = new ArrayList<String>();
+			data.add(hour + ":" + minutes);
+			// dt, tr, windSpeed, temperature, rh, dp, bp;
+			data.add(averageRow.getDt().toPlainString());
+			data.add(averageRow.getTr().toPlainString());
+			data.add(averageRow.getWindSpeed().toPlainString());
+			data.add(averageRow.getTemperature().toPlainString());
+			data.add(averageRow.getRh().toPlainString());
+			data.add(averageRow.getDp().toPlainString());
+			data.add(averageRow.getBp().toPlainString());
+			outputLines.add(StringUtils.join(data, ","));
+		}
+
+		try {
+			final FileOutputStream outputStream = new FileOutputStream(output);
+			IOUtils.writeLines(outputLines, IOUtils.LINE_SEPARATOR_WINDOWS, outputStream, Charset.forName("UTF-8"));
+			IOUtils.closeQuietly(outputStream);
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+
+		}
+	}
+
+	private static List<DataRow> parseData(File inputFile) throws IOException {
+		final BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(inputFile));
+
+		List<DataRow> elements = null;
+		try {
+			// TODO assume that you can use apache utils to get a list of Strings
+			elements = convertData(IOUtils.readLines(inputStream));
+		} finally {
+			if (inputStream != null) inputStream.close();
+		}
+
+		return elements;
+	}
+
+	private static final SimpleDateFormat	dateFormat	= new SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.US);
+
+	private static List<DataRow> convertData(List<String> lines) {
+		return Lists.transform(lines, new Function<String, DataRow>() {
+			@Override
+			public DataRow apply(String line) {
+				return convertLine(line);
+			}
+
+		});
+
+	}
+
+	private static DataRow convertLine(String line) {
+		final String[] sections = line.split(",");
+
+		Date sampleDate = null;
+		try {
+			sampleDate = dateFormat.parse(sections[0]);
+		} catch (ParseException e) {
+			System.out.println(sections[0]);
+			e.printStackTrace();
+		}
+		final BigDecimal dt = BigDecimal.valueOf(Double.parseDouble(sections[1]));
+		final BigDecimal tr = BigDecimal.valueOf(Double.parseDouble(sections[2]));
+		final BigDecimal windSpeed = BigDecimal.valueOf(Double.parseDouble(sections[3]));
+		final BigDecimal temperature = BigDecimal.valueOf(Double.parseDouble(sections[4]));
+		final BigDecimal rh = BigDecimal.valueOf(Double.parseDouble(sections[5]));
+		final BigDecimal dp = BigDecimal.valueOf(Double.parseDouble(sections[6]));
+		final BigDecimal bp = BigDecimal.valueOf(Double.parseDouble(sections[7]));
+
+		return new DataRow(sampleDate, dt, tr, windSpeed, temperature, rh, dp, bp);
+	}
+}

File src/peru/averages/DataRow.java

View file
  • Ignore whitespace
+package peru.averages;
+
+import java.math.BigDecimal;
+import java.util.Date;
+
+public class DataRow {
+	private final Date	date;
+	private final BigDecimal	dt, tr, windSpeed, temperature, rh, dp, bp;
+
+	public DataRow(Date date, BigDecimal dt, BigDecimal tr, BigDecimal windSpeed, BigDecimal temperature, BigDecimal rh, BigDecimal dp, BigDecimal bp) {
+		super();
+		this.date = date;
+		this.dt = dt;
+		this.tr = tr;
+		this.windSpeed = windSpeed;
+		this.temperature = temperature;
+		this.rh = rh;
+		this.dp = dp;
+		this.bp = bp;
+	}
+
+	public Date getDate() {
+		return date;
+	}
+
+	public BigDecimal getDt() {
+		return dt;
+	}
+
+	public BigDecimal getTr() {
+		return tr;
+	}
+
+	public BigDecimal getWindSpeed() {
+		return windSpeed;
+	}
+
+	public BigDecimal getTemperature() {
+		return temperature;
+	}
+
+	public BigDecimal getRh() {
+		return rh;
+	}
+
+	public BigDecimal getDp() {
+		return dp;
+	}
+
+	public BigDecimal getBp() {
+		return bp;
+	}
+
+}