simrad_utils β Kongsberg Simrad .all Byte-Order & Sonar Model Detection
Overview & Purpose
The simrad_utils module (code/subr/simrad_utils.c and code/subr/simrad_utils.h) provides non-destructive file-header inspection utilities for Kongsberg Maritime (Simrad) .all multibeam raw datagram files.
Because Kongsberg EM-series multibeam echosounders have historically logged .all datagrams in both big-endian (older Sun/Motorola operator stations such as EM12, EM950, EM1000) and little-endian (x86 Windows/Linux SIS operator stations such as EM300, EM1002, EM3000, EM710, EM2040, EM302, EM122) byte orders, downstream parsers must determine the fileβs endianness and EM model number before unpacking telegrams. Both guess_simrad_byte_order() and get_model() preserve and restore the callerβs FILE * stream offset via ftell() and fseek().
Key Constants
Defined in code/subr/simrad_utils.h:
| Macro | Value | Description |
|---|---|---|
SIMRAD_UNKNOWN_BYTE_ORDER | -1 | Endianness could not be determined from the datagram header |
SIMRAD_BIG_ENDIAN | 0 | File datagrams are stored in big-endian (MSB-first) byte order |
SIMRAD_LITTLE_ENDIAN | 1 | File datagrams are stored in little-endian (LSB-first) byte order |
Public Functions & Signatures
guess_simrad_byte_order
int guess_simrad_byte_order(FILE *infile);
- Purpose: Determines whether a Kongsberg
.allfile is encoded in little-endian or big-endian byte order without altering the file position ofinfile. - Algorithm:
- Saves the current stream offset (
initial_file_position = ftell(infile)). - Seeks to byte offset
8from the start of the file (SEEK_SET), which corresponds to the 4-byte integerdatefield (YYYYMMDD) inside the first Kongsberg datagram header (following the 4-byte telegram length, 1-byteSTX0x02, 1-byte datagram type, and 2-byte EM model number). - Reads the 4-byte date field as both a little-endian integer (
fread_LE_int) and a big-endian integer (fread_BE_int), then restoresinitial_file_position. - Checks which interpretation falls within the valid calendar date range
[19700000, 20500000](1970-00-00to2050-00-00):- Returns
SIMRAD_LITTLE_ENDIAN(1) if onlydatefield_LEis within bounds. - Returns
SIMRAD_BIG_ENDIAN(0) if onlydatefield_BEis within bounds. - Returns
SIMRAD_UNKNOWN_BYTE_ORDER(-1) otherwise.
- Returns
- Saves the current stream offset (
get_model
int get_model(FILE *infile, int byte_order);
- Purpose: Reads and returns the 16-bit unsigned Kongsberg EM model number (e.g.,
300,1002,3000,710,2040,122,302) from the first datagram header ininfile. - Algorithm:
- Saves
initial_file_position = ftell(infile). - Seeks to byte offset
6from the start ofinfile(SEEK_SET). - Reads a 2-byte
unsigned shortusingfread_BE_short()ifbyte_order == SIMRAD_BIG_ENDIANorfread_LE_short()otherwise. - Restores
initial_file_positionand returnsmodel.
- Saves
old_guess_simrad_byte_order
int old_guess_simrad_byte_order(FILE *infile);
- Purpose: Legacy heuristic byte-order detector retained in
simrad_utils.c. Scans up to 10 consecutive datagrams under bothSIMRAD_BIG_ENDIANandSIMRAD_LITTLE_ENDIANhypotheses by reading the 4-bytetelegram_lengthpreceding each0x02(STX) byte and checking whether advancing bytelegram_length + 4lands on the next datagramβs0x02(STX) byte.
How It Fits Into the OMG Architecture
- Kongsberg
.allConverters (RT/RT.c,RTLite/RTlite.c,RTLite/RTlite_unravel.c): Callguess_simrad_byte_order()andget_model()immediately upon opening a.allfile to configure endian-swapping and sonar-specific beam/attitude handling. - XTF & Simrad Utilities (
xtf/xtfTool.c,xtf/xtf2omg_simrad.c,subr/xtf_utils.c,jonnyb/simrad_code/*): Used acrosssimrad_reader,EM_unpack,km2nav,km2orientation,km2report,get_watercolumn,compress_watercolumn,get_stave,get_seeps, andget_centralbeamsto auto-detect.allendianness and sonar model numbers.