Added polar data file.

Added PolarParse class - currently commented out as it depends on polar table class.

#story[900]
main
fjc40 9 years ago
parent a4480f3ccf
commit aeee8ca748

@ -3,5 +3,175 @@ 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;
}
*/
}

@ -0,0 +1,28 @@
package seng302.Exceptions;
/**
* Created by f123 on 10-May-17.
*/
/**
* An exception thrown when we cannot parse a polar data file.
*/
public class InvalidPolarFileException extends RuntimeException {
/**
* Constructs the exception with a given message.
* @param message Message to store.
*/
public InvalidPolarFileException(String message) {
super(message);
}
/**
* Constructs the exception with a given message and cause.
* @param message Message to store.
* @param cause Cause to store.
*/
public InvalidPolarFileException(String message, Throwable cause) {
super(message, cause);
}
}

@ -0,0 +1,8 @@
Tws,Twa0,Bsp0,Twa1,Bsp1,UpTwa,UpBsp,Twa2,Bsp2,Twa3,Bsp3,Twa4,Bsp4,Twa5,Bsp5,Twa6,Bsp6,DnTwa,DnBsp,Twa7,Bsp7
4,0,0,30,4,45,8,60,9,75,10,90,10,115,10,145,10,155,10,175,4
8,0,0,30,7,43,10,60,11,75,11,90,11,115,12,145,12,153,12,175,10
12,0,0,30,11,43,14.4,60,16,75,20,90,23,115,24,145,23,153,21.6,175,14
16,0,0,30,12,42,19.2,60,25,75,27,90,31,115,32,145,30,153,28.8,175,20
20,0,0,30,13,41,24,60,29,75,37,90,39,115,40,145,38,153,36,175,24
25,0,0,30,15,40,30,60,38,75,44,90,49,115,50,145,49,151,47,175,30
30,0,0,30,15,42,30,60,37,75,42,90,48,115,49,145,48,150,46,175,32
1 Tws Twa0 Bsp0 Twa1 Bsp1 UpTwa UpBsp Twa2 Bsp2 Twa3 Bsp3 Twa4 Bsp4 Twa5 Bsp5 Twa6 Bsp6 DnTwa DnBsp Twa7 Bsp7
2 4 0 0 30 4 45 8 60 9 75 10 90 10 115 10 145 10 155 10 175 4
3 8 0 0 30 7 43 10 60 11 75 11 90 11 115 12 145 12 153 12 175 10
4 12 0 0 30 11 43 14.4 60 16 75 20 90 23 115 24 145 23 153 21.6 175 14
5 16 0 0 30 12 42 19.2 60 25 75 27 90 31 115 32 145 30 153 28.8 175 20
6 20 0 0 30 13 41 24 60 29 75 37 90 39 115 40 145 38 153 36 175 24
7 25 0 0 30 15 40 30 60 38 75 44 90 49 115 50 145 49 151 47 175 30
8 30 0 0 30 15 42 30 60 37 75 42 90 48 115 49 145 48 150 46 175 32

@ -0,0 +1,26 @@
package seng302.DataInput;
import org.testng.annotations.Test;
import java.io.File;
import static org.testng.Assert.*;
/**
* Created by f123 on 10-May-17.
*/
public class PolarParserTest {
@Test
/**
* Tests if we can parse a polar data file (stored in a string), and create a polar table.
*/
public void testParse() throws Exception {
//Polars = PolarParser.parse("polars/acc_polars.csv");
}
}
Loading…
Cancel
Save