-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSCFileReader.java
146 lines (130 loc) · 5.61 KB
/
SCFileReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
/**
* SCFileReader - Steady course file reader. Collection of methods and tools
* for reading Debrief file and populating corresponding data structures.
* @see rep replay file format: http://www.debrief.info/tutorial/index.html#reference.html#replay_track_format
*/
public class SCFileReader {
public static final Charset ENCODING = StandardCharsets.UTF_8;
//-------------------------------------------------------------------------
/**
* Reads Debrief file and populates corresponding data structure
* @param path a path to the (.rep) file which should be read and processed
* @param num_lines
* @return ArrayList<Tote> arrayl_totes - array list of created Totes
*/
public List<Tote> process(String path) {
File file = new File(path);
List<String> lines = readFile(file);
ArrayList<Tote> arrayl_totes = splitLines(lines);
return arrayl_totes;
}
//-------------------------------------------------------------------------
/**
* Creates list of strings (lines in a file) from a given file
* @param file given file
* @return List<String> lines - list of string lines in the file
* @see rep replay file format: http://www.debrief.info/tutorial/index.html#reference.html#replay_track_format
*/
public List<String> readFile(File file) {
List<String> lines = new ArrayList<>();
try {
lines = Files.readAllLines(file.toPath(), ENCODING);
}
catch (IOException e) {
System.out.println("Exception while reading file!?");
}
return lines;
}
//-------------------------------------------------------------------------
/**
* Create ArrayList of Tote objects from a list of strings (string lines in a Debrief replay format file)
* @param lines List of strings
* @return ArrayList<Tote> arrayl_totes - array list of created Totes
*/
public ArrayList<Tote> splitLines(List<String> lines) {
ArrayList<Tote> arrayl_totes = new ArrayList<>();
for (String line : lines) {
String sline = line.trim();
if (sline.isEmpty() || sline.startsWith(";;")) continue;
arrayl_totes.add(createTote(sline));
}
return arrayl_totes;
}
static int global_index = 0;
//-------------------------------------------------------------------------
/**
* Create Tote structure from a string line (a line in a Debrief replay format file)
* @param line string line in .rep file
* @return Tote tote - the tote created from a line in the file
*/
double previousHeading = 180.0; // suitable initial value
public Tote createTote(String line) {
Tote tote = new Tote();
try {
String[] parts = line.split("\\s+"); //split na white space
if (parts.length >= 14) { // for this particular format, there are 16 substrings in a string line. But, we don't need the last two.
tote.index = global_index++;
tote.sdate = parts[0].trim();
tote.stime = parts[1].trim();
tote.dheading = Double.parseDouble(parts[12].trim());
if(previousHeading - tote.dheading > 180.0)
tote.dheading += 360.0;
else if(tote.dheading - previousHeading > 180.0)
tote.dheading -= 360.0;
previousHeading = tote.dheading;
tote.dspeed = Double.parseDouble(parts[13].trim());
tote.dabsolute_time = toAbsoluteTime(tote.sdate, tote.stime);
}
}
catch (NumberFormatException e) {
System.out.println("Reading specified file caused " + e);
}
return tote;
}
//-------------------------------------------------------------------------
/**
* Returns elapsed seconds from midnight 1970-01-01
* @param sdate date in YYYYMMDD or YYMMDD format
* @param stime time in HHMMSS.SSS or HMMSS.SSS format
* @return double desconds - seconds from midnight 1970-01-01
*/
public double toAbsoluteTime(String sdate, String stime) {
if(sdate.length() == 6) {
if(sdate.startsWith("0") || sdate.startsWith("1"))
sdate = "20" + sdate;
else
sdate = "19" + sdate;
}
LocalDate date = LocalDate.parse(sdate, DateTimeFormatter.BASIC_ISO_DATE);
long iepoch_days = date.toEpochDay();
double dseconds = 24 * 3600 * iepoch_days;
String shours, smins, ssecs;
int pospoint = stime.indexOf('.');
String sinttime = pospoint != -1 ? stime.substring(0, pospoint) : stime;
if(sinttime.length() == 6) {
shours = stime.substring(0, 2);
smins = stime.substring(2, 4);
ssecs = stime.substring(4);
}
else {
shours = "0" + stime.substring(0, 1);
smins = stime.substring(1, 3);
ssecs = stime.substring(3);
}
String stime_iso = shours + ":" + smins + ":" + ssecs;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_TIME;
LocalTime time = LocalTime.parse(stime_iso, dateTimeFormatter);
dseconds += (double)(time.toNanoOfDay()) * 1e-9;
return dseconds;
}
}