pydsstools.core.gridinfo.v6.conversion
Conversion functions between DSS v6 and v7 grid metadata.
This module provides functions to convert between the legacy v6 format (ctypes structures) and the modern v7 format (Pydantic models), as well as string encoding/decoding utilities for v6 binary I/O.
String Encoding in v6
DSS v6 stores strings as arrays of int32 values. Each int32 holds 4 ASCII characters packed in little-endian byte order:
“HRAP” → [1347571272] “MM” → [19789]
Each int32 holds 4 bytes: H(72), R(82), A(65), P(80) → 0x50415248 = 1347571272
Functions
|
Reinterpret float32 as int32 bits. |
|
Convert DSS v6 GridInfo to v7-compatible dictionary. |
|
Convert DSS v7 GridInfo to DSS v6 format. |
|
Reinterpret int32 bits as float32. |
|
Convert array of int32 values to ASCII string. |
|
Convert ASCII string to array of int32 values. |
- pydsstools.core.gridinfo.v6.conversion.str_to_ints(s, endian='little', signed=True)[source]
Convert ASCII string to array of int32 values.
Strings in DSS v6 are stored as arrays of int32, with 4 ASCII characters packed into each int32 value. This function performs that encoding.
- Parameters:
s (str) – ASCII string to encode.
endian (str, optional) – Byte order: “little” or “big”, default “little”.
signed (bool, optional) – If True, use signed int32; if False, use unsigned, default True.
- Returns:
List of int32 values representing the string.
- Return type:
list[int]
Examples
>>> str_to_ints("HRAP") [1347571272] >>> str_to_ints("MM") [19789] >>> str_to_ints("PRECIPITATION") [1346457936, 1414676528, 1413830225, 78]
Notes
Empty strings are treated as a single null character for padding. Strings are padded to 4-byte boundaries with null bytes.
- pydsstools.core.gridinfo.v6.conversion.ints_to_str(ints, endian='little', encoding='ascii', strip_trailing_nulls=True, stop_at_first_null=True)[source]
Convert array of int32 values to ASCII string.
Reverse of str_to_ints(). Unpacks 4 ASCII characters from each int32 and assembles them into a string.
- Parameters:
ints (list[int]) – List of int32 values representing a string.
endian (str, optional) – Byte order: “little” or “big”, default “little”.
encoding (str, optional) – Character encoding, default “ascii”.
strip_trailing_nulls (bool, optional) – If True, remove null padding at end, default True.
stop_at_first_null (bool, optional) – If True, stop at first null (C-string semantics), default True.
- Returns:
Decoded ASCII string.
- Return type:
str
Examples
>>> ints_to_str([1347571272]) 'HRAP' >>> ints_to_str([19789]) 'MM' >>> ints_to_str([1346457936, 1414676528, 1413830225, 78]) 'PRECIPITATION'
Notes
Applies C-string semantics by default (stops at first null)
Removes non-ASCII characters (values >= 128)
Handles both signed and unsigned int32 values
- pydsstools.core.gridinfo.v6.conversion.float32_to_int32(value, endian='<')[source]
Reinterpret float32 as int32 bits.
- Parameters:
value (float) – 32-bit floating point value.
endian (str, optional) – Endianness: ‘=’ native, ‘<’ little-endian, ‘>’ big-endian. Default is ‘<’.
- Returns:
Integer with same bit pattern as float.
- Return type:
int
- pydsstools.core.gridinfo.v6.conversion.int32_to_float32(value, endian='<')[source]
Reinterpret int32 bits as float32.
- Parameters:
value (int) – 32-bit integer value.
endian (str, optional) – Endianness: ‘=’ native, ‘<’ little-endian, ‘>’ big-endian. Default is ‘<’.
- Returns:
Float with same bit pattern as integer.
- Return type:
float
- pydsstools.core.gridinfo.v6.conversion.gridinfo7_to_gridinfo6(gridinfo7, pathname)[source]
Convert DSS v7 GridInfo to DSS v6 format.
- Parameters:
gridinfo7 (GridInfo or subclass) – DSS v7 grid info object (from gridinfo package).
pathname (str) – DSS pathname (used to extract start/end times from D and E parts).
- Returns:
DSS v6 grid info structure (ctypes object).
- Return type:
GridInfo6 or subclass
Notes
Handles conversion of: - Pydantic models to ctypes structures - GridInfo enum types to integer codes - Tuple coordinates to separate X/Y fields - String units to int32 arrays - Python bools to int32 values
Some fields may be truncated if they exceed DSS v6 limits: - data_units: 12 characters max - data_source: 12 characters max (HRAP) - proj_units: 12 characters max (Albers) - range_vals/counts: 20 bins max
Examples
>>> from pydsstools.core.gridinfo import GridInfoCreate, GridType, DataType >>> from pydsstools.core.gridinfo.v6 import gridinfo7_to_gridinfo6 >>> >>> info7 = GridInfoCreate( ... grid_type=GridType.hrap, ... data_type=DataType.per_aver, ... shape=(100, 150), ... cell_size=4762.5 ... ) >>> pathname = "/GRID/LOC/PRECIP/01JAN2020:0000//" >>> info6 = gridinfo7_to_gridinfo6(info7, pathname)
- pydsstools.core.gridinfo.v6.conversion.gridinfo6_to_gridinfo7_dict(gridinfo6)[source]
Convert DSS v6 GridInfo to v7-compatible dictionary.
- Parameters:
gridinfo6 (GridInfo6 or subclass) – DSS v6 grid info structure (ctypes object).
- Returns:
Dictionary compatible with GridInfoCreate() factory function.
- Return type:
dict
Notes
Handles conversion of: - Separate X/Y fields to tuple coordinates - Integer codes to GridType enum values - int32 arrays to Python strings - CRS inference from grid parameters
Examples
>>> from pydsstools.core.gridinfo.v6 import gridinfo6_to_gridinfo7_dict >>> from pydsstools.core.gridinfo import GridInfoCreate >>> >>> # Assume info6 is a GridInfo6 from C library >>> info7_dict = gridinfo6_to_gridinfo7_dict(info6) >>> info7 = GridInfoCreate(**info7_dict)