Skip to content

Commit

Permalink
Fix endianess
Browse files Browse the repository at this point in the history
  • Loading branch information
zadeviggers committed Oct 8, 2023
1 parent 5255345 commit ede3ce8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "miniseed",
"version": "0.2.0",
"version": "0.2.1",
"type": "module",
"scripts": {
"build": "vite build",
Expand Down
19 changes: 11 additions & 8 deletions src/miniseed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,24 @@ export function serialiseToMiniSEEDBuffer<T extends keyof typeof encodingTypes>(
view.writeString("MS");

// https://docs.fdsn.org/projects/miniseed3/en/latest/definition.html#field-2
view.writeUint8(3);
// NOTE: The spec specifies little-endian=true for the others, and leaves it
// unspecified for Uint8's, so I'm assuming false
view.writeUint8(3, false);

// https://docs.fdsn.org/projects/miniseed3/en/latest/definition.html#field-3
view.writeUint8(metadata.flags);
view.writeUint8(metadata.flags, false);

// https://docs.fdsn.org/projects/miniseed3/en/latest/definition.html#field-4
view.writeUint32(metadata.startTime.nanoSecond);
view.writeUint16(metadata.startTime.year);
view.writeUint16(metadata.startTime.dayOfYear);
view.writeUint8(metadata.startTime.hour);
view.writeUint8(metadata.startTime.minute);
view.writeUint8(metadata.startTime.second);

view.writeUint8(metadata.startTime.hour, false);
view.writeUint8(metadata.startTime.minute, false);
view.writeUint8(metadata.startTime.second, false);

// https://docs.fdsn.org/projects/miniseed3/en/latest/definition.html#field-5
view.writeUint8(encodingInfo.code);
view.writeUint8(encodingInfo.code, false);

// https://docs.fdsn.org/projects/miniseed3/en/latest/definition.html#field-6
view.writeFloat64(metadata.sampleRatePeriod);
Expand All @@ -132,10 +135,10 @@ export function serialiseToMiniSEEDBuffer<T extends keyof typeof encodingTypes>(
view.writeUint32(0);

// https://docs.fdsn.org/projects/miniseed3/en/latest/definition.html#field-9
view.writeUint8(metadata.dataPublicationVersion);
view.writeUint8(metadata.dataPublicationVersion, false);

// https://docs.fdsn.org/projects/miniseed3/en/latest/definition.html#field-10
view.writeUint8(sourceIdentifier.length);
view.writeUint8(sourceIdentifier.length, false);

// https://docs.fdsn.org/projects/miniseed3/en/latest/definition.html#field-11
view.writeUint16(extraHeaders.length);
Expand Down
27 changes: 26 additions & 1 deletion tests/date-utils.test.ts → tests/dates.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect } from "vitest";
import { startTimeFromDate } from "../src/miniseed";
import { startTimeFromDate, serialiseToMiniSEEDBuffer } from "../src/miniseed";
import jDataView from "z-jdataview-temp-publish";

describe("Date utilities", () => {
it("Works with a set date", () => {
Expand Down Expand Up @@ -37,3 +38,27 @@ describe("Date utilities", () => {
expect(startTime2.dayOfYear).toEqual(61);
});
});

describe("Date serialisation", () => {
it("Sets date fields correctly", () => {
const date = new Date("21 September 1978 21:45:30");
const view = new jDataView(
serialiseToMiniSEEDBuffer([1, 2, 3], {
sourceIdentifier: "https://zade.viggers.net/example",
startTime: startTimeFromDate(date),
}),
undefined,
undefined,
true
);

console.log(view.buffer);

expect(view.getUint32(4)).toEqual(0); // nanosecond
expect(view.getUint16(8)).toEqual(1978); // year
expect(view.getUint16(10)).toEqual(264); // day of year
expect(view.getUint8(12, false)).toEqual(21); // hour
expect(view.getUint8(13, false)).toEqual(45); // minute
expect(view.getUint8(14, false)).toEqual(30); // second
});
});
14 changes: 0 additions & 14 deletions tests/vaid-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,6 @@ describe("Date validity", () => {
});
await checkData(serialised, "date-0-auto");
});
// it("Works with a manually set 0 date", async () => {
// const serialised = serialiseToMiniSEEDUint8Array([], {
// sourceIdentifier: "https://zade.viggers.net/example",
// startTime: {
// dayOfYear: 0,
// year: 0,
// hour: 0,
// minute: 0,
// second: 0,
// nanoSecond: 0,
// },
// });
// await checkData(serialised, "date-0-manual");
// });
it("Works with a new date right now", async () => {
const serialised = serialiseToMiniSEEDUint8Array([], {
sourceIdentifier: "https://zade.viggers.net/example",
Expand Down

0 comments on commit ede3ce8

Please sign in to comment.