# SA1 raw data file (SA_DATA_FILE v1.0) :id=top

------

> _Doc Ver:_ 1.1  
_App Ver:_ 2.6.6.53  
_Date:_ 09/07/2026

------

> On-device raw save written by **sa-di**. The file container, record layout, and names match SA2 `SA_DATA_FILE` v1.0. Writer is SA1-only: UDP **8190** payload, path `persist/data/raw` (`/var/sa/data/raw`). This is **not** `sa_daq` / `sa_rdplay`.

Inner `data` bytes are the demodulator UDP datagram as received. Frame meaning is in [wavelength](data-wl.md) / [physical value](data-pv.md). Do not confuse this container with standard `wl/` / `pv/` files (those are 0x00 / 0x04 / 0x05 frames, no 15-byte wrapper).

------

## 1. When SA1 writes raw

* Setting → System → Data save, or Data → Realtime save → Setting.
* **1.6 Data format = 0**, or check **1.11 Save raw**.
* Check **1.1 Enable**, then Save (`:SYS:RELOAD`).
* Data page tree **Raw**. Status `open` is `*.dat.temp`.
* `:PLAY:*` does **not** play raw (no `sa-rdplay`; UDP inject would fight sa-di). CSV uses `sa-file csv --kind raw`.

## 2. Name and path

Same template as wl/pv (kind is `raw`):

```
{idn}_{sn}-raw-{start_date}_{start_time}_{end_date}_{end_time}.dat[.gz]
```

While writing:

```
/var/sa/data/raw/{idn}_{sn}-raw-{YYYYMMDD}_{HHMMSSNNN}.dat.temp
```

After close:

```
{idn}_{sn}-raw-{start_date}_{start_time}_{end_date}_{end_time}.dat
{idn}_{sn}-raw-{start_date}_{start_time}_{end_date}_{end_time}.dat.gz
```

| Field | Format | Notes |
|-------|--------|--------|
| `idn` / `sn` | ASCII | From equipment IDN / SN |
| `start_*` / `end_*` | local time | Date `YYYYMMDD`, time `HHMMSSNNN` (milliseconds) |
| Record `sec` | UTC Unix | Not the same clock as the filename |

Compress on (`DS_ZIP`) gzips the **whole** file (header + records) to `.dat.gz`. That is **not** a `.tar.gz` (wl/pv still use tar). Skip `*.temp`.

Example:

```
FT-16_10600001-raw-20260906_235959123.dat.temp
FT-16_10600001-raw-20260906_235959123_20260907_005959456.dat.gz
```

Rotate: planned **60** minutes (header `Duration`), or earlier when the row cap is hit.

## 3. File layout

```
[A] ASCII header, LF lines, ends with EndOfHeader\n
[B] records: 15-byte LE header + optional IP + data
```

No end marker. EOF + `total_len` locate records. `.dat.gz` is gzip of `[A]+[B]`, not a tar member.

### 3.1 ASCII header

```
SA_DATA_FILE
Version: 1.0
IDN: FT-16
SN: 10600001
Type: raw
Extension: dat
Created: 2026-09-06T15:59:59.123Z
Compression: true
CompressionType: gz
Duration: 60
EndOfHeader
```

First line must be `SA_DATA_FILE`. Then `Key: Value`. Stop at `EndOfHeader`; next byte is [B]. `Created` is UTC with milliseconds and `Z`. `CompressionType` is `gz` or `none`.

### 3.2 Record (little-endian)

| Off | Field | Type | Meaning |
|----:|-------|------|---------|
| 0 | `total_len` | uint32 | `15 + ip_len + data_len` |
| 4 | `sec` | uint32 | UTC Unix seconds |
| 8 | `nsec` | uint32 | nanoseconds 0–999999999 |
| 12 | `src_port` | uint16 | UDP source port (often 8190) |
| 14 | `ip_len` | uint8 | 0–64; 0 means no IP field |
| 15 | `ip` | char[ip_len] | dotted IPv4 if `ip_len` > 0 |
| 15+ip_len | `data` | bytes | UDP payload, unchanged |

`ip_len = 0` is valid. `data_len = total_len - 15 - ip_len`. Reject `total_len < 15` or `ip_len > 64`. Do not resync after a bad record.

sa-di does not parse `data`. Magic `0xFFFF` / CRC belong to the demodulator wire format, not this container.

## 4. sa-file

```
sa-file info FILE
sa-file csv -s FILE -o DIR --kind raw
sa-file extract FILE.dat.gz -o DIR
```

CSV columns: `sec,nsec,src_port,ip,data_len,data_hex32`. `FILE` may be `.dat` or `.dat.gz`.

## 5. Read one record (Python)

```python
import struct

def iter_records(f):
    while True:
        h = f.read(15)
        if len(h) < 15:
            return
        total, sec, nsec, port, ip_len = struct.unpack("<IIIHB", h)
        if ip_len > 64 or total < 15 + ip_len:
            raise ValueError("bad record")
        ip = f.read(ip_len).decode("ascii") if ip_len else ""
        data = f.read(total - 15 - ip_len)
        yield sec, nsec, port, ip, data
```

------

[Top](#top)
