|
1 | | -public class DataWriterODF { |
2 | | - private PrintWriter output; |
3 | | - private String fname; |
4 | | - private int rowsWritten; |
5 | | - private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); |
6 | | - protected String fileNamePrependString = "OpenBCI-RAW-"; |
7 | | - protected String headerFirstLineString = "%OpenBCI Raw EXG Data"; |
8 | | - |
9 | | - //variation on constructor to have custom name |
10 | | - DataWriterODF(String _sessionName, String _fileName) { |
11 | | - settings.setSessionPath(directoryManager.getRecordingsPath() + "OpenBCISession_" + _sessionName + File.separator); |
12 | | - fname = settings.getSessionPath(); |
13 | | - fname += fileNamePrependString; |
14 | | - fname += _fileName; |
15 | | - fname += ".txt"; |
16 | | - output = createWriter(fname); //open the file |
17 | | - writeHeader(); //add the header |
18 | | - rowsWritten = 0; //init the counter |
19 | | - } |
20 | | - |
21 | | - public void writeHeader() { |
22 | | - output.println(headerFirstLineString); |
23 | | - output.println("%Number of channels = " + getNumberOfChannels()); |
24 | | - output.println("%Sample Rate = " + getSamplingRate() + " Hz"); |
25 | | - output.println("%Board = " + getUnderlyingBoardClass()); |
26 | | - |
27 | | - String[] colNames = getChannelNames(); |
28 | | - |
29 | | - for (int i=0; i<colNames.length; i++) { |
30 | | - output.print(colNames[i]); |
31 | | - output.print(", "); |
32 | | - } |
33 | | - output.print("Timestamp (Formatted)"); |
34 | | - output.println(); |
35 | | - output.flush(); |
36 | | - } |
37 | | - |
38 | | - public void append(double[][] data) { |
39 | | - //get current date time with Date() |
40 | | - for (int iSample=0; iSample<data[0].length; iSample++) { |
41 | | - for (int iChan=0; iChan<data.length; iChan++) { |
42 | | - output.print(data[iChan][iSample]); |
43 | | - output.print(", "); |
44 | | - } |
45 | | - |
46 | | - int timestampChan = getTimestampChannel(); |
47 | | - // *1000 to convert from seconds to milliserconds |
48 | | - long timestampMS = (long)(data[timestampChan][iSample] * 1000.0); |
49 | | - |
50 | | - output.print(dateFormat.format(new Date(timestampMS))); |
51 | | - output.println(); |
52 | | - |
53 | | - rowsWritten++; |
54 | | - } |
55 | | - } |
56 | | - |
57 | | - public void closeFile() { |
58 | | - output.close(); |
59 | | - } |
60 | | - |
61 | | - public int getRowsWritten() { |
62 | | - return rowsWritten; |
63 | | - } |
64 | | - |
65 | | - protected int getNumberOfChannels() { |
66 | | - return nchan; |
67 | | - } |
68 | | - |
69 | | - protected int getSamplingRate() { |
70 | | - return ((Board)currentBoard).getSampleRate(); |
71 | | - } |
72 | | - |
73 | | - protected String getUnderlyingBoardClass() { |
74 | | - return ((Board)currentBoard).getClass().getName(); |
75 | | - } |
76 | | - |
77 | | - protected String[] getChannelNames() { |
78 | | - return ((Board)currentBoard).getChannelNames(); |
79 | | - } |
80 | | - |
81 | | - protected int getTimestampChannel() { |
82 | | - return ((Board)currentBoard).getTimestampChannel(); |
83 | | - } |
84 | | - |
85 | | -}; |
| 1 | +public class DataWriterODF { |
| 2 | + private PrintWriter output; |
| 3 | + private String fname; |
| 4 | + private int rowsWritten; |
| 5 | + private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); |
| 6 | + protected String fileNamePrependString = "OpenBCI-RAW-"; |
| 7 | + protected String headerFirstLineString = "%OpenBCI Raw EXG Data"; |
| 8 | + |
| 9 | + //variation on constructor to have custom name |
| 10 | + DataWriterODF(String _sessionName, String _fileName) { |
| 11 | + settings.setSessionPath(directoryManager.getRecordingsPath() + "OpenBCISession_" + _sessionName + File.separator); |
| 12 | + fname = settings.getSessionPath(); |
| 13 | + fname += fileNamePrependString; |
| 14 | + fname += _fileName; |
| 15 | + fname += ".txt"; |
| 16 | + output = createWriter(fname); //open the file |
| 17 | + writeHeader(); //add the header |
| 18 | + rowsWritten = 0; //init the counter |
| 19 | + } |
| 20 | + |
| 21 | + public void writeHeader() { |
| 22 | + output.println(headerFirstLineString); |
| 23 | + output.println("%Number of channels = " + getNumberOfChannels()); |
| 24 | + output.println("%Sample Rate = " + getSamplingRate() + " Hz"); |
| 25 | + output.println("%Board = " + getUnderlyingBoardClass()); |
| 26 | + |
| 27 | + String[] colNames = getChannelNames(); |
| 28 | + |
| 29 | + for (int i=0; i<colNames.length; i++) { |
| 30 | + output.print(colNames[i]); |
| 31 | + output.print(", "); |
| 32 | + } |
| 33 | + output.print("Timestamp (Formatted)"); |
| 34 | + output.println(); |
| 35 | + output.flush(); |
| 36 | + } |
| 37 | + |
| 38 | + public void append(double[][] data) { |
| 39 | + //get current date time with Date() |
| 40 | + for (int iSample=0; iSample<data[0].length; iSample++) { |
| 41 | + for (int iChan=0; iChan<data.length; iChan++) { |
| 42 | + output.print(data[iChan][iSample]); |
| 43 | + output.print(", "); |
| 44 | + } |
| 45 | + |
| 46 | + int timestampChan = getTimestampChannel(); |
| 47 | + // *1000 to convert from seconds to milliserconds |
| 48 | + long timestampMS = (long)(data[timestampChan][iSample] * 1000.0); |
| 49 | + |
| 50 | + output.print(dateFormat.format(new Date(timestampMS))); |
| 51 | + output.println(); |
| 52 | + |
| 53 | + rowsWritten++; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public void closeFile() { |
| 58 | + output.close(); |
| 59 | + } |
| 60 | + |
| 61 | + public int getRowsWritten() { |
| 62 | + return rowsWritten; |
| 63 | + } |
| 64 | + |
| 65 | + protected int getNumberOfChannels() { |
| 66 | + return nchan; |
| 67 | + } |
| 68 | + |
| 69 | + protected int getSamplingRate() { |
| 70 | + return ((Board)currentBoard).getSampleRate(); |
| 71 | + } |
| 72 | + |
| 73 | + protected String getUnderlyingBoardClass() { |
| 74 | + return ((Board)currentBoard).getClass().getName(); |
| 75 | + } |
| 76 | + |
| 77 | + protected String[] getChannelNames() { |
| 78 | + return ((Board)currentBoard).getChannelNames(); |
| 79 | + } |
| 80 | + |
| 81 | + protected int getTimestampChannel() { |
| 82 | + return ((Board)currentBoard).getTimestampChannel(); |
| 83 | + } |
| 84 | + |
| 85 | +}; |
0 commit comments