-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathCPODBinaryStore.java
More file actions
269 lines (207 loc) · 6.6 KB
/
CPODBinaryStore.java
File metadata and controls
269 lines (207 loc) · 6.6 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package cpod;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import PamguardMVC.DataUnitBaseData;
import PamguardMVC.PamDataUnit;
import binaryFileStorage.BinaryDataSource;
import binaryFileStorage.BinaryHeader;
import binaryFileStorage.BinaryObjectData;
import binaryFileStorage.ModuleFooter;
import binaryFileStorage.ModuleHeader;
import cpod.CPODClick.CPODWaveTransforms;
/**
* Binary storage for CPOD data.
* <p>
* Seems a little silly to re create a new file format for CPOD data but by storing
* as binary files we can take advantage of the PAMGuard datagram, UID manager, super detections, a
* annotations etc. without creating a very hacky module.
*
* @author Jamie Macauly
*
*/
public class CPODBinaryStore extends BinaryDataSource {
/**
* <p>Module version changes</p>
* Version 2: Moved start sample, channel bitmap, duration to DataUnitBaseData general data structure<br>
* Version 3: Implemented FPOD data which includes waveforms<br>
*/
private static final int currentVersion = 3;
/**
* Reference to the CPOD control.
*/
private CPODControl2 cpodControl;
private CPODClickDataBlock cpodDataBlock;
public CPODBinaryStore(CPODControl2 cpodControl2, CPODClickDataBlock clipDataBlock) {
super(clipDataBlock);
this.cpodControl = cpodControl2;
this.cpodDataBlock = clipDataBlock;
}
@Override
public byte[] getModuleHeaderData() {
// TODO Auto-generated method stub
return null;
}
@Override
public int getModuleVersion() {
return currentVersion;
}
@Override
public String getStreamName() {
return "CPOD_clicks";
}
@Override
public int getStreamVersion() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void newFileOpened(File outputFile) {
// TODO Auto-generated method stub
}
private ByteArrayOutputStream bos;
private DataOutputStream dos;
@Override
public BinaryObjectData getPackedData(PamDataUnit pamDataUnit) {
CPODClick cd = (CPODClick) pamDataUnit;
//System.out.println("DLDetecitonBinarySource: packed: " + pamDataUnit.getBasicData().getMeasuredAmplitudeType());
// make a byte array output stream and write the data to that,
// then dump that down to the main storage stream
if (dos == null || bos == null) {
dos = new DataOutputStream(bos = new ByteArrayOutputStream());
}
else {
bos.reset();
}
try {
writeCPODdata3(dos, cd);
}
catch (IOException e) {
e.printStackTrace();
return null;
}
// getBinaryStorageStream().storeData(1, cd.getTimeMilliseconds(), bos.toByteArray());
return new BinaryObjectData(1, bos.toByteArray());
}
private void writeCPODdata3(DataOutputStream dos2, CPODClick cd) throws IOException {
// data[3]=nCyc;
// data[4]=bw;
// data[5]=kHz;
// data[6]=endF;
// data[7]=spl;
// data[8]=slope;
dos.writeShort(cd.getnCyc());
dos.writeShort(cd.getBw());
dos.writeShort(cd.getkHz());
dos.writeShort(cd.getEndF());
dos.writeShort(cd.getSpl());
dos.writeShort(cd.getSlope());
boolean hasWav = cd.getWaveData()!=null;
dos.writeBoolean(cd.getWaveData()!=null);
if (hasWav) {
//the number of samples - note FPODs only have one sample
dos.writeShort(cd.getWaveData()[0].length);
//convert back to shorts using scale factor
for (int i=0; i<cd.getWaveData()[0].length; i++) {
dos.writeShort((short) (cd.getWaveData()[0][i]*FPODReader.WAV_SCALE_FACTOR));
}
}
}
@Deprecated
private void writeCPODData2(DataOutputStream dos2, CPODClick cd) throws IOException {
//CPOD has 8 bytes - FPOD has more
dos.writeInt(cd.getRawData().length);
for (int i=0; i<cd.getRawData().length; i++) {
dos.writeShort(cd.getRawData()[i]);
}
}
@Override
public PamDataUnit sinkData(BinaryObjectData binaryObjectData,
BinaryHeader bh, int moduleVersion) {
ByteArrayInputStream bis = new ByteArrayInputStream(binaryObjectData.getData(),
0, binaryObjectData.getDataLength());
DataInputStream dis = new DataInputStream(bis);
DataUnitBaseData baseData = binaryObjectData.getDataUnitBaseData();
if (moduleVersion==2)
{
//This is not stored in the base data. Probably sensible because it's the same number for every data unit.
//baseData.setMeasuredAmplitudeType(DataUnitBaseData.AMPLITUDE_SCALE_LINREFSD); <- now in constructor.
//System.out.println("DLDetecitonBinarySource: sink: " + baseData.getMeasuredAmplitudeType());
//model results are loaded as annotations.
try {
short[] cpodClick = readCPODData(dis);
if (baseData.getChannelBitmap()==0) {
baseData.setChannelBitmap(1);
}
CPODClick newUnit = CPODClick.makeClick(baseData, cpodClick);
bis.close();
return newUnit;
} catch (IOException e) {
// e.printStackTrace();
System.err.println(e.getMessage());
return null;
}
}
else {
try {
//TODO - maybe include original time data?|
//Convenient way to make a click
short[] data = new short[9];
data[3]=dis.readShort();
data[4]=dis.readShort();
data[5]=dis.readShort();
data[6]=dis.readShort();
data[7]=dis.readShort();
data[8]=dis.readShort();
//make a basic click
CPODClick click = CPODClick.makeClick(baseData, data);
//now add more info if needed.
boolean wav = dis.readBoolean();
if (wav) {
int len = dis.readShort();
int[] arr = new int[len];
for (int i=0; i<arr.length ; i++) {
arr[i]=dis.readShort();
}
double[] wavData = FPODReader.scaleWavData(arr);
click.setWavData(new double[][] {wavData});
click.setRawDataTransfroms(new CPODWaveTransforms(click));
}
bis.close();
return click;
} catch (IOException e) {
// e.printStackTrace();
System.err.println(e.getMessage());
return null;
}
}
}
/**
* Read raw data from a CPOD detection from version 2 of the binary file format.
* @param dis - the data input stream.
* @return
* @throws IOException
*/
private short[] readCPODData(DataInputStream dis) throws IOException {
short[] cpodRawData = new short[dis.readInt()];
for (int i=0; i<cpodRawData.length; i++) {
cpodRawData[i] =dis.readShort();
}
return cpodRawData;
}
@Override
public ModuleFooter sinkModuleFooter(BinaryObjectData binaryObjectData,
BinaryHeader bh, ModuleHeader moduleHeader) {
// TODO Auto-generated method stub
return null;
}
@Override
public ModuleHeader sinkModuleHeader(BinaryObjectData binaryObjectData,
BinaryHeader bh) {
// TODO Auto-generated method stub
return null;
}
}