Skip to content
ZisIsNotZisPublic

About

Python JSON conversion for a tested subset of uncompressed PDA2 DAT files. Native ibaAnalyzer interoperability is pending.

Resources

Stars

5 stars

Watchers

1 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

Repository files navigation

iba.py: Read and Write PDA2 .dat Files in Python

Status (2026-11-29): the local entry points work and are tested; opening a generated file in the real IBA Analyzer is still unverified.

What is verified here, on this machine, offline and with synthetic data only:

  • python -m iba_py converts JSON to a PDA2 3.2 .dat and back, using the documented default names and an optional explicit output path.
  • The reader accepts files written by the previous release and by this one; analog channels are compared as float32, digital channels as booleans.
  • Bad, truncated, mislabelled, or compressed inputs produce one readable error line and leave any existing output file untouched.
  • python -m unittest discover -s tests passes all 28 tests.
  • tools/verify_export.py turns an Analyzer CSV export into an auditable pass/fail against the source JSON, so the remaining manual step is a single command.

What is not verified: that a real ibaAnalyzer installation opens the generated .dat with the expected channel names, start time, sample interval, frame count, and sample values. No Analyzer or official sample file was available, so round-tripping this project's own files is not evidence of Analyzer compatibility. See Compatibility for the exact pending check.

Installation

From this checkout, no dependencies are needed:

pip install -e .
# or simply run it in place:
python -m iba_py --help

Requires Python 3.10 or newer (the code uses X | Y type syntax).

Usage

1. Command-Line Interface

JSON → .dat

python -m iba_py your_data.json              # writes your_data.json.dat
python -m iba_py your_data.json -o plant.dat # explicit output name

.dat → JSON

python -m iba_py your_existing.dat              # writes your_existing.dat.json
python -m iba_py your_existing.dat -o data.json

The input name decides the direction: .json is converted to .dat, .dat is converted to .json (case-insensitive). Without -o, the output name is the input name with the other extension appended, so both files stay distinguishable.

Behaviour worth knowing:

  • An existing output file is replaced only after the whole conversion succeeded. A failure leaves it byte-for-byte unchanged and leaves no temporary files behind.
  • The input file (also via a symbolic or hard link) is never used as the output.
  • Decoded JSON uses "YYYY-MM-DD HH:MM:SS" for the start time, plus .ffffff when the time is not a whole second (a space, not the ISO T used on input; the fraction is truncated to the 0.1 ms the DAT stores, so .123456 comes back as .123400). That JSON is valid input again.
  • gzip- or ZIP-wrapped input is rejected with ...-wrapped input is unsupported; extract the DAT first; unknown signatures are rejected as not a PDA2 DAT file.
  • Exit status is 0 on success, 1 on any conversion error, 2 on a usage error.

2. Python

from iba_py import FormatError, UnsupportedFormatError, dec, enc

begin = "2024-05-20T09:00:00"
interval = 0.2  # seconds between samples
channels = {
    "Temperature C": [22.5, 22.6, 22.7],
    "Pump on": [False, False, True],   # bools become a digital channel
}

enc("plant.dat", begin, interval, channels)
start, step, data = dec("plant.dat")

enc and dec accept either a path or a binary file object. All bytes are prepared before a path is opened, so an invalid write never truncates an existing file. dec requires a seekable stream. Both raise FormatError for bad input and data; UnsupportedFormatError (a subclass) for a layout this tool does not implement. Both derive from ValueError.

JSON Format

A three-element list:

[
  "2024-05-20T09:00:00",
  0.2,
  {
    "Temperature C": [22.5, 22.6, 22.7],
    "Pump on": [false, false, true]
  }
]
  • Start time: ISO 8601 date and time, no time zone. Written with 0.1 ms resolution; extra digits are truncated. Read back as "YYYY-MM-DD HH:MM:SS", or with .ffffff appended when the stored time is not a whole second.
  • Interval: positive, finite number of seconds, written in the shortest text that reads back to the same float (0.1, 1, 5e-07 all round-trip exactly).
  • Channels: a non-empty object. Every channel needs at least one sample and all channels need the same number of samples. A list must be all booleans (digital) or all numbers (analog); mixing is an error. Keep channel names non-blank and free of line breaks. A JSON object cannot repeat a key, so build channel names that are unique.
  • Values are stored as float32, so 0.1 reads back as 0.10000000149011612. Values outside the float32 range and NaN/Infinity are rejected.

A ready-to-run sample is in examples/sample.json. EVIDENCE.md records what was measured for this release and how to rerun it.

Compatibility

Implemented and tested subset:

  • Signature PDA2 followed by 3e c2 8f 9d, a 32-bit header offset, 20 reserved bytes, then run-length-encoded channel chunks (uint16 run count, uint32 next-chunk offset, then 1-byte count + float32 per analog run or 1-byte count + 40/c0 per digital run).
  • ibaFiles:3.2 with typ:real, where analog and digchannel channels mix freely (the analyzer numbering is reproduced). typ:int16 is read for all-digital files; int16 analog samples are not implemented.
  • Every byte of the data section ([32, header offset)) must belong to some channel block. Unreferenced bytes make the file be reported instead of read as a shorter, valid-looking one; this is what catches a file cut at endchannel:. Two channels may point at the same block, and a file missing only its final line break still reads.
  • Runs are capped at 255 samples and chunks at 5000 runs, matching the original implementation.
  • Channel offsets use the original project's O… token encoding, which is only 24 bits wide. A file whose channel data would start beyond 16 MiB is refused instead of written incorrectly.

Not supported, reported as UnsupportedFormatError:

  • Other PDA2 signatures, ibaFiles versions other than 3.2, typ values other than real and int16, and channel offsets that are not the O… form.
  • gzip and ZIP wrappers. The previous release did not support these either; there is no evidence of an official compressed variant, so no decompression is attempted.

Unknown key:value header fields (unit:…, comment:…) are ignored, so files with extra metadata still read. A line without a colon is not metadata but damage, and is reported.

Interoperability with the previous release (0.1.0)

The compatibility tests show which tested inputs produce the same bytes as 0.1.0. These four differences matter when reading or writing files next to an older installation:

  • A run of exactly 255 equal samples. 0.1.0 wrote 256 samples into the file and its own reader returned all 256. This writer writes and returns 255.
  • A digital channel whose first sample is false. 0.1.0 computed a wrong header offset and typ:real, and its reader then returned bogus floats instead of booleans. This writer emits the correct offset and typ:int16.
  • Sub-second start times are kept. 0.1.0 always wrote .0000 and parsed only the whole second. This tool stores 09:00:00.123456 as 09:00:00.1234 and reads it back as 09:00:00.123400; the 0.1.0 reader raises ValueError on such a file. A whole-second start time is written as .0000, exactly as 0.1.0 wrote it. Dropping the fraction to stay readable by 0.1.0 would lose information the format holds, so it is not done and there is no switch for it.
  • Channel names are read in full. 0.1.0's writer stored the whole name but its reader stopped at the first space, so Temperature °C appeared as Temperature. The bytes are unchanged; only this reader returns the full name.

The pending acceptance check needs an ibaAnalyzer installation of a recorded version in an environment where testing third-party generated files is permitted (iba AG's own terms tie free standard functionality to native-data conditions and list third-party generated files separately; check the licence that applies to you). The procedure is:

  1. python -m iba_py examples/sample.json -o sample.json.dat
  2. Open sample.json.dat in the Analyzer; confirm the three channels, the start time 20.05.2024 09:00:00, and an interval of 0.2 s.
  3. Export those channels to CSV.
  4. python tools/verify_export.py examples/sample.json sample.csv

The script compares channel names, sample count, start time, interval, and every sample (booleans as 0/1, analog within --tolerance), and prints what mismatched. The reverse direction needs a small file exported from the same Analyzer so the reader can be checked against real output.

Until such a run is recorded, treat the writer as "produces a valid PDA2 3.2 RLE subset", not as "opens in ibaAnalyzer".

Tests

python -m unittest discover -s tests -v

tests/test_format.py checks a hand-assembled file (independent of this writer), byte-level writer output, every truncation offset of a three-channel file, the interval values that are written and read back, that the channel blocks tile the data section, the documented 0.1.0 differences, the 255-run and 5000-chunk boundaries, float32 tolerance, rejection of corrupt files, and files produced by the previous release, including a known offset bug that is now reported instead of misread. tests/fixtures/legacy_*.dat were generated by the 0.1.0 code and are kept as read-only regressions. tests/test_cli.py drives the real python -m iba_py entry point. tests/test_verify_export.py checks the Analyzer-export comparison, including semicolon/decimal-comma exports, a trailing unit row, missing or extra channels, wrong counts, and tolerance control.

Troubleshooting

  1. input filename must end in .dat or .json — the extension picks the direction.
  2. not a PDA2 DAT file (invalid signature) — the file is not an iba DAT, or is gzip/ZIP-wrapped.
  3. unsupported ibaFiles version … — the file uses a layout this tool does not implement.
  4. channel 'X' must contain only numbers or only booleans — a channel mixes true/false with numbers, or contains null/strings.
  5. all channels must contain the same number of samples — pad or trim the lists.

About

Python JSON conversion for a tested subset of uncompressed PDA2 DAT files. Native ibaAnalyzer interoperability is pending.

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages