Skip to main content

SuperDARN DMAP file format I/O

Project description

A library for SuperDARN DMAP file I/O

github

The SuperDARN DMAP file formats are all supported (IQDAT, RAWACF, FITACF, GRID, MAP, and SND) as well as a generic DMAP format that is unaware of any required fields or types (e.g. char, int32) for any fields. For more information on DMAP please see RST Documentation.

Installation

Package manager

This package is registered on PyPI as darn-dmap, you can install the package with your package manager, e.g. pip install darn-dmap.

From source

If you want to build from source, you first need to have Rust installed on your machine. Then:

  1. Clone the repository: git clone https://github.com/SuperDARNCanada/dmap
  2. Create a virtual environment and source it, then install maturin
  3. In the project directory, run maturin develop to build and install the Python bindings. This will make a wheel file based on your operating system and architecture that you can install directly on any compatible machine.

Usage

The basics

The basic code to read and write a DMAP file is:

import dmap

file = "path/to/rawacf_file"
data, _ = dmap.read_rawacf(file)  # returns `tuple[list[dict], Optional[int]]`
outfile = "path/to/outfile.rawacf"
dmap.write_rawacf(data, outfile)  # writes binary data to `outfile`
raw_bytes = dmap.write_rawacf(data)  # returns a `bytes` object

dmap.read_rawacf(...) reads the file into a list of dictionaries, returning the list as well as the byte where any corrupted records start.

The supported reading functions are:

  • read_iqdat,
  • read_rawacf,
  • read_fitacf,
  • read_grid,
  • read_map,
  • read_snd, and
  • read_dmap.

The supported writing functions are:

  • write_iqdat,
  • write_rawacf,
  • write_fitacf,
  • write_grid,
  • write_map,
  • write_snd, and
  • write_dmap.

Accessing data fields

To see the names of the variables you've loaded in and now have access to, try using the keys() method:

print(data[0].keys())

which will tell you all the variables in the first (zeroth) record.

Let's say you loaded in a MAP file, and wanted to grab the cross polar-cap potentials for each record:

import dmap
file = "20150302.n.map"
map_data, _ = dmap.read_map(file)

cpcps=[rec['pot.drop'] for rec in map_data]

I/O on a bz2 compressed file

dmap will handle compressing and decompressing .bz2 files seamlessly, detecting the compression automatically. E.g.

import dmap
fitacf_file = "path/to/file.bz2"
data, _ = dmap.read_fitacf(fitacf_file)
dmap.write_fitacf(data, "temp.fitacf.bz2")

will read in the compressed file, then also write out a new compressed file. You can also pass the argument bz2=True to compress with bzip2 regardless of file extension, or even to return compressed byte objects.

Generic I/O

dmap supports generic DMAP I/O, without verifying the field names and types. The file must still be properly formatted as a DMAP file, but otherwise no checks are conducted.

NOTE: When using the generic writing function write_dmap, scalar fields will possibly be resized; e.g., the stid field may be stored as an 8-bit integer, as opposed to a 16-bit integer as usual. As such, reading with a specific method (e.g. read_fitacf) on a file written using write_dmap will likely not pass the FITACF format checks.

import dmap
generic_file = "path/to/file"  # can be iqdat, rawacf, fitacf, grid, map, snd, and optionally .bz2 compressed
data, _ = dmap.read_dmap(generic_file)
dmap.write_dmap(data, "temp.generic.fitacf")  # fitacf as an example
data2, bad_byte = dmap.read_rawacf("temp.generic.fitacf")  # This will fail due to different types for scalar fields
assert bad_byte == 0  # The first record should be corrupted, i.e. not be a valid FITACF record
assert len(data2) == 0  # No valid records encountered

Handling corrupted data files

The self-describing data format of DMAP files makes it susceptible to corruption. The metadata fields which describe how to interpret the following bytes are very important, and so any corruption will lead to the remainder of the file being effectively useless. dmap is able to handle corruption in two ways. The keyword argument mode of the read_rawacf, etc. functions allows you to choose how to handle corrupt records.

In "lax" mode (the default), no error is raised if a corrupt file is read, and the byte where the corrupted records start is returned along with the non-corrupted records. In "strict" mode, the I/O functions will raise an error if a corrupted record is encountered.

import dmap

corrupted_file = "path/to/file"
data, bad_byte = dmap.read_dmap(corrupted_file, mode="lax")
assert bad_byte > 0

good_file = "path/to/file"
data, bad_byte = dmap.read_dmap(good_file, mode="lax")
assert bad_byte is None

In both uses of the above example, data will be a list of all records extracted from the file, but may be considerably smaller than the file.

import dmap

corrupted_file = "path/to/file"
try:
    data = dmap.read_dmap(corrupted_file, mode="strict")
    had_error = False
except:
    had_error = True
assert had_error

good_file = "path/to/file"
try:
    data = dmap.read_dmap(good_file, mode="strict")
    had_error = False
except:
    had_error = True
assert had_error is False

Stream I/O

dmap also can conduct read/write operations from/to Python bytes objects directly. These bytes must be formatted in accordance with the DMAP format. Simply pass in a bytes object to any of the read_[type] functions instead of a path and the input will be parsed.

While not the recommended way to read data from a DMAP file, the following example shows the use of these byte I/O functions:

import dmap
file = "path/to/file.fitacf"
with open(file, 'rb') as f:  # 'rb' specifies to open the binary (b) file as read-only (r)
    raw_bytes = f.read()  # reads the file in its entirety
data, _ = dmap.read_dmap(raw_bytes)
binary_data = dmap.write_fitacf(data)
assert binary_data == raw_bytes

As a note, this binary data can be compressed ~2x typically using zlib, or with another compression utility. This is quite useful if sending data over a network where speed and bandwidth must be considered. Note that the binary writing functions can compress with bzip2 by passing bz2=True as an argument.

File "sniffing"

If you only want to inspect a file, without actually needing access to all the data, you can use the read_[type] functions in with the optional indices argument. This will only read in the specified records from a file, and works in all modes.

import dmap
path = "path/to/file"
recs = dmap.read_dmap(path, mode="...", indices=[0, 1, -2, -1])

Reading only metadata fields

Each DMAP format consists of metadata and data fields. You can read only the metadata fields by passing mode="metadata" to any of the writing functions. Note that the generic read function read_dmap will return all fields, as it by nature has no knowledge of the underlying fields. Note also that the read functions operating on a file still read the entire file into memory first, so reading metadata only may not significantly decrease read times. Also, bzipped files are fully decompressed before reading begins, so again read times may not be significantly quicker in this mode under certain circumstances.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

darn_dmap-0.8.0.tar.gz (43.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

darn_dmap-0.8.0-cp38-abi3-win_amd64.whl (576.6 kB view details)

Uploaded CPython 3.8+Windows x86-64

darn_dmap-0.8.0-cp38-abi3-win32.whl (518.5 kB view details)

Uploaded CPython 3.8+Windows x86

darn_dmap-0.8.0-cp38-abi3-musllinux_1_2_x86_64.whl (880.0 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ x86-64

darn_dmap-0.8.0-cp38-abi3-musllinux_1_2_i686.whl (913.2 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

darn_dmap-0.8.0-cp38-abi3-musllinux_1_2_armv7l.whl (898.2 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

darn_dmap-0.8.0-cp38-abi3-musllinux_1_2_aarch64.whl (806.8 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (681.2 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (682.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (747.1 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ i686

darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (622.2 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (642.2 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

darn_dmap-0.8.0-cp38-abi3-macosx_11_0_arm64.whl (579.8 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

darn_dmap-0.8.0-cp38-abi3-macosx_10_12_x86_64.whl (611.2 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file darn_dmap-0.8.0.tar.gz.

File metadata

  • Download URL: darn_dmap-0.8.0.tar.gz
  • Upload date:
  • Size: 43.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for darn_dmap-0.8.0.tar.gz
Algorithm Hash digest
SHA256 39f2f05d8d8c13e62c54c5656e01054a25dc158b393e363dc99ee8bc6078f1bb
MD5 b6a4fcfef0716ed3e82c830ed7d5bea7
BLAKE2b-256 0868187ae0aba9dbce3a1be3a25fef7e97c21383525a4095cd6eedc49c06a173

See more details on using hashes here.

File details

Details for the file darn_dmap-0.8.0-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: darn_dmap-0.8.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 576.6 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for darn_dmap-0.8.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4e54ebc84608ce87072744c53f925e8ba9d50a4f608d57259f0a29aa611e9194
MD5 c97e89a851c2acdbc96369b580b0f080
BLAKE2b-256 7615e18e1b9d73df9bc4c79c5f0658176bde08de248eda6d9e5c15f8770c5713

See more details on using hashes here.

File details

Details for the file darn_dmap-0.8.0-cp38-abi3-win32.whl.

File metadata

  • Download URL: darn_dmap-0.8.0-cp38-abi3-win32.whl
  • Upload date:
  • Size: 518.5 kB
  • Tags: CPython 3.8+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for darn_dmap-0.8.0-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 34a1e4aff8c4e17dbcc0a8c7a1155586ce8a687808b6f77342779fb2155c94bc
MD5 223a50c0267747f1856ee298c7121d92
BLAKE2b-256 35355bda293a616ba4d29655bfebe63b27944d50a6c65e0fd0dae9fe318ac663

See more details on using hashes here.

File details

Details for the file darn_dmap-0.8.0-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for darn_dmap-0.8.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c69d4a001172bda1060331f89452320be0c7c6ae594a4367bed3b0aa1930a1f7
MD5 3e312730e4c546fc0f89d71b2ea72b93
BLAKE2b-256 441badeed24317b23426ef200434b5014f82cd98c26057c2b8df0412daef356e

See more details on using hashes here.

File details

Details for the file darn_dmap-0.8.0-cp38-abi3-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for darn_dmap-0.8.0-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 dbf25d15df6437925200195ccea454a8968dbadea9f3caae7b0a9c49c6c34bff
MD5 51c2c0af7a6a5bfb2f3b51020f01c552
BLAKE2b-256 fe6b4fc584b7de698d4608d8d0edfc8cecb9a38bfc889c3a64f24069203d7f39

See more details on using hashes here.

File details

Details for the file darn_dmap-0.8.0-cp38-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for darn_dmap-0.8.0-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 1b336d68f4ffbeef886b7ca27995ca307d3efb5cf29cd118013cd0d8f8e253f7
MD5 f36087423b2f73d82db3e6928cc05fc5
BLAKE2b-256 27f2a6e253b976694728ebe228ced80e03815baf88942a17b850d83fe799fb7c

See more details on using hashes here.

File details

Details for the file darn_dmap-0.8.0-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for darn_dmap-0.8.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 cf84c3382420c2ed293ea39b1e720ced161fdfaeb09de576a9f4559672e39567
MD5 6f05411e60c5f62a2f55dbf488d871aa
BLAKE2b-256 fbad2579da5c9d2c1be9f5ed002469ced5c37dbab19fae8790ccbc06c7efff69

See more details on using hashes here.

File details

Details for the file darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fb2b2dc9615b25f63d93e5ae78512e95665f52aa05695a9d657da3f43c16241c
MD5 f9ae7057d6705e1449fd9125e558f62e
BLAKE2b-256 8525ff28e69af9d973fae9dd3606e14b01a878546d496d6f53f8034814ae6b33

See more details on using hashes here.

File details

Details for the file darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 d334527f2e8426a2f8e7d57efa77ceab7dd6c499499f21dba378297f9b8dc2f7
MD5 a8243f5081c50a35d37c56fd17eb84f8
BLAKE2b-256 342a612dcc00675c2984677a76b547f9c31e6ba9e8916b19cf5c827974be8499

See more details on using hashes here.

File details

Details for the file darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 4cbef012f78791d62a6039db9772552f9e54a7795ff0f3c25723697821cae1d6
MD5 ab7b030fa3bb69e9546de3dce0d4f72a
BLAKE2b-256 5e8a201a5fd93094ccf370320d766ede686e7c232cee1e4a6278f5cd54f1e2fd

See more details on using hashes here.

File details

Details for the file darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 b4daf092a708930a84f9c20e5363edf113b16c39e876d6122b0cd78632aabd4e
MD5 433e8367daed3a091c0b76673997b181
BLAKE2b-256 7e497572402cd7ebd635f21718efe2d4b9d92cb0b3eefd448726affb67b766ab

See more details on using hashes here.

File details

Details for the file darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for darn_dmap-0.8.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fe279dc040fbb2886684286032fe0f72314ce6a73f8b98a29a74d218d7e8ccb8
MD5 48c1159b7e666f604ff11d966cb2f9bf
BLAKE2b-256 43dc305dfc2ebfd72a716c49e19fa86613bfa99e1b87c19d94b3fd4d929792b3

See more details on using hashes here.

File details

Details for the file darn_dmap-0.8.0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for darn_dmap-0.8.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ccfb88b16e8d1e2d9246391f93de166e63cfdf7eeb56dca188ab8e9177289155
MD5 ab3b389abb8602ce640aeeab0c29b773
BLAKE2b-256 f2a70774b255e44996f1f14a3761b9c5c1b8665c4e6678b0babafb1b4ee000e2

See more details on using hashes here.

File details

Details for the file darn_dmap-0.8.0-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for darn_dmap-0.8.0-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8560b9ce23cecbd91490e445ed23f6cae7ad18a7d4d7edfdb9786c09dd36c3f8
MD5 2dfd1ade42f30fb3806f75a5e66d9a77
BLAKE2b-256 9a411985cf34a337ae16dd465fb4c80e3ed03c3811c19f8f45c94fbb0a7f2ef3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page