You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
6.0 KiB
178 lines
6.0 KiB
package seng302.DataInput;
|
|
|
|
/**
|
|
* Created by hba56 on 10/05/17.
|
|
*/
|
|
|
|
import seng302.Exceptions.InvalidPolarFileException;
|
|
import seng302.Model.Polar;
|
|
|
|
import java.io.*;
|
|
import java.util.ArrayList;
|
|
|
|
|
|
/**
|
|
* Responsible for parsing a polar data file, and creating a polar data object.
|
|
*/
|
|
public class PolarParser {
|
|
|
|
|
|
/**
|
|
* Given a filename, this function parses it and generates a PolarTable object, which can be queried for polar information.
|
|
* @param filename
|
|
* @return
|
|
*/
|
|
/*
|
|
///TEMP PolarTable = ArrayList<Polar>
|
|
public static ArrayList<Polar> parse(String filename) throws InvalidPolarFileException {
|
|
//Temporary table to return later.
|
|
ArrayList<Polar> polarTable = new ArrayList<Polar>();
|
|
|
|
|
|
//Open the file for reading.
|
|
InputStream fileStream = PolarParser.class.getClassLoader().getResourceAsStream(filename);
|
|
if (fileStream == null) {
|
|
throw new InvalidPolarFileException("Could not open polar data file: " + filename);
|
|
}
|
|
//Wrap it with buffered input stream to set encoding and buffer.
|
|
InputStreamReader in = null;
|
|
try {
|
|
in = new InputStreamReader(fileStream, "UTF-8");
|
|
} catch (UnsupportedEncodingException e) {
|
|
throw new InvalidPolarFileException("Unsupported encoding: UTF-8", e);
|
|
}
|
|
BufferedReader inputStream = new BufferedReader(in);
|
|
|
|
|
|
//We expect the polar data file to have the column headings:
|
|
// Tws, Twa0, Bsp0, Twa1, Bsp1, UpTwa, UpBsp, Twa2, Bsp2, Twa3, Bsp3, Twa4, Bsp4, Twa5, Bsp5, Twa6, Bsp6, DnTwa, DnBsp, Twa7, Bsp7
|
|
//and to have 7 rows of data.
|
|
//Angles are expected to be in degrees, and velocities in knots.
|
|
|
|
|
|
//We read the heading and data rows, and split them into arrays of elements.
|
|
String[] headings;
|
|
ArrayList<String[]> dataRows = new ArrayList<>(7);
|
|
try {
|
|
//Heading.
|
|
//Read heading row.
|
|
String headingRow = inputStream.readLine();
|
|
|
|
//Split it into individual headings.
|
|
headings = headingRow.split(",");
|
|
|
|
|
|
//Data rows.
|
|
while (inputStream.ready()) {
|
|
//Read line.
|
|
String dataRow = inputStream.readLine();
|
|
|
|
//Split line.
|
|
String[] dataElements = dataRow.split(",");
|
|
|
|
//Add to collection.
|
|
dataRows.add(dataElements);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
throw new InvalidPolarFileException("Could not read from polar data file: " + filename, e);
|
|
}
|
|
|
|
//Finished reading in data, now we need to construct polar rows and table from it.
|
|
//For each row...
|
|
int rowNumber = 0;
|
|
for (String[] row : dataRows) {
|
|
//Create Polar row object.
|
|
Polar polarRow = new Polar();
|
|
|
|
//For each column...
|
|
for (int i = 0; i < row.length; i++) {
|
|
|
|
//Convert value to a double.
|
|
Double value;
|
|
try {
|
|
value = Double.parseDouble(row[i]);
|
|
}
|
|
catch (NumberFormatException e) {
|
|
throw new InvalidPolarFileException("Could not convert (Row,Col): (" + rowNumber + "," + i +") = " + row[i] + "to a double.", e);
|
|
}
|
|
|
|
//Set the values of the row.
|
|
//For reference:
|
|
//Tws, Twa0, Bsp0, Twa1, Bsp1, UpTwa, UpBsp, Twa2, Bsp2, Twa3, Bsp3, Twa4, Bsp4, Twa5, Bsp5, Twa6, Bsp6, DnTwa, DnBsp, Twa7, Bsp7
|
|
if (headings[i] == "Tws") {
|
|
polarRow.setTrueWindSpeed(value);
|
|
}
|
|
else if (headings[i] == "Twa0") {
|
|
polarRow.setTrueWindAngle0(value);
|
|
}
|
|
else if (headings[i] == "Bsp0") {
|
|
polarRow.setBoatSpeed0(value);
|
|
}
|
|
else if (headings[i] == "Twa1") {
|
|
polarRow.setTrueWindAngle1(value);
|
|
}
|
|
else if (headings[i] == "Bsp1") {
|
|
polarRow.setBoatSpeed1(value);
|
|
}
|
|
else if (headings[i] == "UpTwa") {
|
|
polarRow.setUpTrueWindAngel(value);
|
|
}
|
|
else if (headings[i] == "UpBsp") {
|
|
polarRow.setUpBoatSpeed(value);
|
|
}
|
|
else if (headings[i] == "Twa2") {
|
|
polarRow.setTrueWindAngle2(value);
|
|
}
|
|
else if (headings[i] == "Bsp2") {
|
|
polarRow.setBoatSpeed2(value);
|
|
}
|
|
else if (headings[i] == "Twa3") {
|
|
polarRow.setTrueWindAngle3(value);
|
|
}
|
|
else if (headings[i] == "Bsp3") {
|
|
polarRow.setBoatSpeed3(value);
|
|
}
|
|
else if (headings[i] == "Twa4") {
|
|
polarRow.setTrueWindAngle4(value);
|
|
}
|
|
else if (headings[i] == "Bsp4") {
|
|
polarRow.setBoatSpeed4(value);
|
|
}
|
|
else if (headings[i] == "Twa5") {
|
|
polarRow.setTrueWindAngle5(value);
|
|
}
|
|
else if (headings[i] == "Bsp5") {
|
|
polarRow.setBoatSpeed5(value);
|
|
}
|
|
else if (headings[i] == "Twa6") {
|
|
polarRow.setTrueWindAngle6(value);
|
|
}
|
|
else if (headings[i] == "Bsp6") {
|
|
polarRow.setBoatSpeed6(value);
|
|
}
|
|
else if (headings[i] == "Twa7") {
|
|
polarRow.setTrueWindAngle7(value);
|
|
}
|
|
else if (headings[i] == "Bsp7") {
|
|
polarRow.setBoatSpeed7(value);
|
|
}
|
|
|
|
//Add the polar row to the polar table.
|
|
polarTable.add(polarRow);
|
|
|
|
}
|
|
|
|
//Increment row number.
|
|
rowNumber++;
|
|
|
|
}
|
|
|
|
|
|
return polarTable;
|
|
}
|
|
*/
|
|
|
|
}
|