📄 seedreader.java
字号:
*/
/*
float pt[] = new float[header.sampleCount];
// Decode the X0 & Xn in the 1st frame, these are
// the "forward" and "reverse integration constants" (first and last values)
// Note that the first sample will be a repeat of the last sample of the
// previous SEED packet.
firstValue = Bits.byteToInt4 (buff, header.dataOffset+4); // the first value
lastValue = Bits.byteToInt4 (buff, header.dataOffset+8);
// Decode the remaining frames
// Doing this in two steps simplifies the logic because the first sample is given
// both explicity and as a difference
int offset;
int nword;
int bytePtr;
int idx = -1;
int lastIndex = header.sampleCount - 1; // nSamples -1;
// Step 1: Load differences into the 'pt[]' array
int frameNo = 0;
// NOTE: the total samples in the Seed record may be less then what it can hold.
// :. we must believe the value given in the header and passed as nSamples.
try {
// while (idx < lastIndex) {
while (frameNo < header.framesInRecord) {
offset = header.dataOffset + (frameNo * FrameSize); // byte offset to frame
// decode the compression map that tells us how may bytes are used
// for each data value here. It always has 16 values.
map = compressionMap (buff, offset);
// handle bytes in groups of 4-bytes (1 longword)
for (nword=0; nword<map.length; nword++) {
bytePtr = offset + (nword*4); // step 4 bytes each time
if (map[nword] == 0) { // not compressed data, skip it
} else if (map[nword] == 1) { // four 1-byte samples
pt[++idx] = buff[bytePtr];
pt[++idx] = buff[bytePtr+1];
pt[++idx] = buff[bytePtr+2];
pt[++idx] = buff[bytePtr+3];
} else if (map[nword] == 2) { // two 2-byte samples
pt[++idx] = Bits.byteToInt2 (buff, bytePtr);
pt[++idx] = Bits.byteToInt2 (buff, bytePtr+2);
} else if (map[nword] == 3) { // one 4-byte sample
pt[++idx] = Bits.byteToInt4 (buff, bytePtr);
}
} // end of for (nword=0...
frameNo++; // next frame
} // end of while ....
// catch attempt to write more samples than there should be
} catch (IndexOutOfBoundsException ex) {
System.out.println (ex.toString());
idx--;
}
// Step 2: Calculate actual values from differences
pt[0] = firstValue;
for (int i=1; i<=idx; i++) {
pt[i] = pt[i-1] + pt[i];
}
// sanity checks
if (idx+1 != header.sampleCount) {
System.out.println (" * Sample count mismatch: header said = "
+ header.sampleCount + " decompressed count = " + (idx+1));
}
if (pt[idx] != lastValue) {
System.out.println (" * Last value mismatch: Xn = " + lastValue
+ " last decompressed value = " + pt[idx]);
}
return pt;
} // end of decompress()
*/
/*
static float[] decompressSteim1 (int dataOffset, int sampleCount,
byte[] buff) {
boolean swap = false;
int[] samps = new int[sampleCount];
try {
int dataBytes = buff.length-dataOffset;
byte[] data = new byte[dataBytes];
System.arraycopy(buff, dataOffset, data, 0, dataBytes);
samps = Steim1.decode(data, sampleCount, swap);
} catch (Exception ex) {
System.out.println (ex.toString());
return new float[0];
}
float pt[] = new float[sampleCount];
// convert int to float
for (int i = 0; i < sampleCount; i++) {
pt[i] = (float) samps[i];
}
return pt;
}
*/
/**
* Decompress one SEED packet (Only handles Steim1 or 2 compression at this time)
* Assumes you've already parsed the header. Byte-swaps if necessary. Returns
* float[0] array on error.
*/
static float[] decompress (SeedHeader header, byte[] buff) {
// determine if you must byte-swap the timeseries
boolean swap = false;
if (header.wordOrder == 0) swap = true;
int[] samps = new int[header.sampleCount];
try {
int dataBytes = buff.length-header.dataOffset;
byte[] data = new byte[dataBytes];
System.arraycopy(buff, header.dataOffset, data, 0, dataBytes);
if (header.encodingFormat == SeedEncodingFormat.STEIM1) {
samps = Steim1.decode(data, header.sampleCount, swap);
} else if (header.encodingFormat == SeedEncodingFormat.STEIM2) {
samps = Steim2.decode(data, header.sampleCount, swap);
} else {
throw new SeedReaderException("Can't decode data format: "+header.encodingFormat);
}
} catch (Exception ex) {
System.out.println (ex.toString());
return new float[0];
}
// convert int to float
float pt[] = new float[header.sampleCount];
for (int i = 0; i < header.sampleCount; i++) {
pt[i] = (float) samps[i];
}
return pt;
}
/**
* Decompress one SEED packet (Only handles Steim1 compression at this time)
* Assumes you've already parsed the header because
*/
/*
static float[] decompress (int dataOffset, int sampleCount,
int framesInRecord, byte[] buff) {
return decompressSteim1(dataOffset, sampleCount,
buff);
}
*/
/*
float pt[] = new float[sampleCount];
// Decode the X0 & Xn in the 1st frame, these are
// the "forward" and "reverse integration constants" (first and last values)
// Note that the first sample will be a repeat of the last sample of the
// previous SEED packet.
float firstValue = Bits.byteToInt4 (buff, dataOffset+4); // the first value
float lastValue = Bits.byteToInt4 (buff, dataOffset+8);
// Decode the remaining frames
// Doing this in two steps simplifies the logic because the first sample is given
// both explicity and as a difference
int offset;
int nword;
int bytePtr;
int idx = -1;
//int lastIndex = header.sampleCount - 1; // nSamples -1;
int lastIndex = sampleCount - 1; // nSamples -1;
// Step 1: Load differences into the 'pt[]' array
int frameNo = 0;
// NOTE: the total samples in the Seed record may be less then what it can hold.
// :. we must believe the value given in the header and passed as nSamples.
try {
while (idx < lastIndex) {
// while (frameNo < framesInRecord) {
offset = dataOffset + (frameNo * FrameSize); // byte offset to frame
// decode the compression map that tells us how may bytes are used
// for each data value here. It always has 16 values.
map = compressionMap (buff, offset);
// handle bytes in groups of 4-bytes (1 longword)
for (nword=0; nword<map.length; nword++) {
bytePtr = offset + (nword*4); // step 4 bytes each time
if (map[nword] == 0) { // not compressed data, skip it
} else if (map[nword] == 1) { // four 1-byte samples
pt[++idx] = buff[bytePtr];
pt[++idx] = buff[bytePtr+1];
pt[++idx] = buff[bytePtr+2];
pt[++idx] = buff[bytePtr+3];
} else if (map[nword] == 2) { // two 2-byte samples
pt[++idx] = Bits.byteToInt2 (buff, bytePtr);
pt[++idx] = Bits.byteToInt2 (buff, bytePtr+2);
} else if (map[nword] == 3) { // one 4-byte sample
pt[++idx] = Bits.byteToInt4 (buff, bytePtr);
}
} // end of for (nword=0...
frameNo++; // next frame
} // end of while ....
// catch attempt to write more samples than there should be
} catch (IndexOutOfBoundsException ex) {
System.out.println (ex.toString());
idx--;
}
// Step 2: Calculate actual values from differences
pt[0] = firstValue;
for (int i=1; i<=idx; i++) {
pt[i] = pt[i-1] + pt[i];
}
// sanity checks
if (idx+1 != sampleCount) {
System.out.println (" * Sample count mismatch: header said = "
+ sampleCount + " decompressed count = " + (idx+1));
}
if (pt[idx] != lastValue) {
System.out.println (" * Last value mismatch: Xn = " + lastValue
+ " last decompressed value = " + pt[idx]);
}
return pt;
} // end of decompress()
*/
/**
* Decode the "nibbles" in the first 32 bits of the frame that encode how
* many bytes are in each difference. the arg
* 'offset' is the address of the first byte of the first frame. Bytes are counted
* from 0 :. this value will typically be 64. <p>
The compression map is a "longword" (4 bytes) and is interpreted as 16 2-byte
nibbles. Each nibble (C00 - C15) tells you the amount of compression in the
corrisponding 4-byte longword in the data portion of the frame.
<tt>
| byte #0 | byte #1 | byte #2 | byte #3 |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|C00|C01|C02|C03|C04|C05|C06|C07|C08|C09|C10|C11|C12|C13|C14|C15|
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
16 nibbles = 32 bits = 4 bytes
Cxx = 0 - special, the matching longword is not compressed data but
something else (like a compression map value)
Cxx = 1 - four 1-byte differences
Cxx = 2 - two 2-byte differences
Cxx = 3 - one 4-byte difference
</tt>
See Seed Manual Pg. 121-122.
*/
/*
public static int[] compressionMap (byte[] buff, int offset)
{
imap = -1;
for (int nbyte=offset; nbyte<offset+4; nbyte++)
{
for (int nib=0; nib<4; nib++) // for each nibble
{
map[++imap] = buff[nbyte] >> (6-(nib*2)) & 0x03;
// System.out.println (" imap=" + imap + " val=" + map[imap]);
}
}
return (map);
}
public final void finalize() {
try {
if (csStmt != null) csStmt.close();
} catch (SQLException ex) {}
csStmt = null;
}
*/
/// --------------------------------------------------------------------------
/**
* Main for testing
*/
public static void main (String args[])
{
Vector segVector = new Vector(); // ref to vector of WF segments
int evid;
if (args.length > 0) // translate epoch second on command-line value
{
Integer val = Integer.valueOf(args[0]); // convert arg String to 'double'
evid = val.intValue();
} else {
// evid = 9500724; // test event
//evid = 9749085;
evid = 2218477;
System.out.println("Syntax: SeedReader <evid> <mode> <dbhost> <doublestart> <doubleend>");
}
System.out.println ("Making connection... ");
DataSource jc = new TestDataSource();
// Make a persistent instance so the FTP connection can be reused
SeedReader reader = new SeedReader();
// reader.setFileAccess();
Waveform wfi = Waveform.create();
System.out.println ("evid = " +evid+ " filesAreLocal= "+ wfi.filesAreLocal());
ArrayList wfList = (ArrayList) wfi.getBySolutionId(evid);
System.out.println( "wf list length = "+ wfList.size());
Waveform wf [] = new Waveform[wfList.size()];
wfList.toArray(wf);
if (wf == null)
{
System.out.println ("No waveforms in dbase for evid "+evid);
System.exit(0);
}
// local read
int totalBytes = 0;
int count=0;
int status = args.length;
// String mode = "LOCAL";
String mode = "DB";
if (status > 0) mode = args[1].toUpperCase();
System.out.println("Getting waveforms source input mode: " + mode);
double wfStartTime = 0.;
double wfEndTime = 0.;
double startTime = 0.;
double endTime = 99999999999.;
if (args.length > 3) {
startTime = Double.parseDouble(args[3]);
}
if (args.length > 4) {
endTime = Double.parseDouble(args[4]);
}
// boolean verbose = false;
boolean verbose = true;
if (args.length > 3) {
verbose = true;
//verbose = Boolean.valueOf(args[5]).booleanValue();
}
BenchMark bm = new BenchMark();
BenchMark bm2 = new BenchMark();
for (int i = 0; i<wf.length; i++)
{
if (verbose) System.out.print ("Count = "+ ++count + " reading... "+ wf[i].toString() );
wfStartTime = wf[i].getStart().doubleValue();
wfEndTime = wf[i].getEnd().doubleValue();
System.out.println("Requested window: " +
EpochTime.epochToString(wfStartTime) + " to " +
EpochTime.epochToString(wfEndTime));
bm.reset();
if (mode.equals("LOCAL")) {
status = reader.getDataLocal(wf[i]);
// else if (mode.equals("REMOTE")) status = reader.getDataRemote(wf[i]);
// else if (mode.equals("DB")) {
} else if (mode.equals("REMOTE") || mode.equals("DB")) {
//startTime = Math.min(startTime+20.,endTime);
//endTime = Math.max(endTime-20.,startTime);
double sTime = Math.max(wfStartTime,startTime);
double eTime = Math.min(wfEndTime, endTime);
if (eTime > sTime) {
if (verbose) System.out.println(" requested time window data len secs: " + (eTime - sTime)) ;
status = reader.getDataFromDataSource(wf[i],sTime,eTime);
}
else if (verbose) System.out.println("** Note requested start time > end time of waveform.");
}
else status = getData(wf[i]);
if (status > 0)
if (verbose) System.out.println ("Read " + wf[i].getSegmentList().size() + " segments for " + wf[i].toString() );
if (verbose) bm.print("BenchMark: ");
// Dump segments
if (args.length > 5) {
WFSegment [] wfseg = wf[i].getArray();
for (int j = 0; j < wfseg.length; j++) {
if (verbose) System.out.println ( wfseg[j].toString() );
}
}
wfseg = null;
wf[i] = null;
}
bm2.print("TOTAL BenchMark: ");
bm2.reset();
}
} // end of class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -