Skip to main content

A modern C++ header only cdf library

Project description

License: GPL v3 Documentation Status CPP17 PyPi Coverage Discover on MyBinder

Python packages

Linux x86_64 Windows x86_64 MacOs x86_64 MacOs ARM64
linux_x86_64 windows_x86_64 macos_x86_64 macos_arm64

Unit Tests

Linux x86_64 Windows x86_64 MacOs x86_64
linux_x86_64 windows_x86_64 macos_x86_64

CDFpp (CDF++)

A NASA's CDF modern C++ library. This is not a C++ wrapper but a full C++ implementation. Why? CDF files are still used for space physics missions but few implementations are available. The main one is NASA's C implementation available here but it lacks multi-threads support (global shared state), has an old C interface and has a license which isn't compatible with most Linux distributions policy. There are also Java and Python implementations which are not usable in C++.

List of features and roadmap:

  • CDF reading
    • read uncompressed file headers
    • read uncompressed attributes
    • read uncompressed variables
    • read variable attributes
    • loads cdf files from memory (std::vector or char*)
    • handles both row and column major files
    • read variables with nested VXRs
    • read compressed files (GZip, RLE)
    • read compressed variables (GZip, RLE)
    • read UTF-8 encoded files
    • read ISO 8859-1(Latin-1) encoded files (converts to UTF-8 on the fly)
    • variables values lazy loading
    • decode DEC's floating point encoding (Itanium, ALPHA and VAX)
    • pad values
  • CDF writing
    • write uncompressed headers
    • write uncompressed attributes
    • write uncompressed variables
    • write compressed variables
    • write compressed files
    • pad values
  • General features
    • uses libdeflate for faster GZip decompression
    • highly optimized CDF reads (up to ~4GB/s read speed from disk)
    • handle leap seconds
    • Python wrappers
    • Documentation
    • Examples (see below)
    • Benchmarks

If you want to understand how it works, how to use the code or what works, you may have to read tests.

Installing

From PyPi

python3 -m pip install --user pycdfpp

From sources

meson build
cd build
ninja
sudo ninja install

Or if youl want to build a Python wheel:

python -m build . 
# resulting wheel will be located into dist folder

Basic usage

Python

Reading CDF files

Basic example from a local file:

import pycdfpp
cdf = pycdfpp.load("some_cdf.cdf")
cdf_var_data = cdf["var_name"].values #builds a numpy view or a list of strings
attribute_name_first_value = cdf.attributes['attribute_name'][0]

Note that you can also load in memory files:

import pycdfpp
import requests
import matplotlib.pyplot as plt
tha_l2_fgm = pycdfpp.load(requests.get("https://spdf.gsfc.nasa.gov/pub/data/themis/tha/l2/fgm/2016/tha_l2_fgm_20160101_v01.cdf").content)
plt.plot(tha_l2_fgm["tha_fgl_gsm"])
plt.show()

Buffer protocol support:

import pycdfpp
import requests
import xarray as xr
import matplotlib.pyplot as plt

tha_l2_fgm = pycdfpp.load(requests.get("https://spdf.gsfc.nasa.gov/pub/data/themis/tha/l2/fgm/2016/tha_l2_fgm_20160101_v01.cdf").content)
xr.DataArray(tha_l2_fgm['tha_fgl_gsm'], dims=['time', 'components'], coords={'time':tha_l2_fgm['tha_fgl_time'].values, 'components':['x', 'y', 'z']}).plot.line(x='time')
plt.show()

# Works with matplotlib directly too

plt.plot(tha_l2_fgm['tha_fgl_time'], tha_l2_fgm['tha_fgl_gsm'])
plt.show()

Datetimes handling:

import pycdfpp
import os
# Due to an issue with pybind11 you have to force your timezone to UTC for 
# datetime conversion (not necessary for numpy datetime64)
os.environ['TZ']='UTC'

mms2_fgm_srvy = pycdfpp.load("mms2_fgm_srvy_l2_20200201_v5.230.0.cdf")

# to convert any CDF variable holding any time type to python datetime:
epoch_dt = pycdfpp.to_datetime(mms2_fgm_srvy["Epoch"])

# same with numpy datetime64:
epoch_dt64 = pycdfpp.to_datetime64(mms2_fgm_srvy["Epoch"])

# note that using datetime64 is ~100x faster than datetime (~2ns/element on an average laptop)

Writing CDF files

Creating a basic CDF file:

import pycdfpp
import numpy as np
from datetime import datetime

cdf = pycdfpp.CDF()
cdf.add_attribute("some attribute", [[1,2,3], [datetime(2018,1,1), datetime(2018,1,2)], "hello\nworld"])
cdf.add_variable(f"some variable", values=np.ones((10),dtype=np.float64))
pycdfpp.save(cdf, "some_cdf.cdf")

C++

#include "cdf-io/cdf-io.hpp"
#include <iostream>

std::ostream& operator<<(std::ostream& os, const cdf::Variable::shape_t& shape)
{
    os << "(";
    for (auto i = 0; i < static_cast<int>(std::size(shape)) - 1; i++)
        os << shape[i] << ',';
    if (std::size(shape) >= 1)
        os << shape[std::size(shape) - 1];
    os << ")";
    return os;
}

int main(int argc, char** argv)
{
    auto path = std::string(DATA_PATH) + "/a_cdf.cdf";
    // cdf::io::load returns a optional<CDF>
    if (const auto my_cdf = cdf::io::load(path); my_cdf)
    {
        std::cout << "Attribute list:" << std::endl;
        for (const auto& [name, attribute] : my_cdf->attributes)
        {
            std::cout << "\t" << name << std::endl;
        }
        std::cout << "Variable list:" << std::endl;
        for (const auto& [name, variable] : my_cdf->variables)
        {
            std::cout << "\t" << name << " shape:" << variable.shape() << std::endl;
        }
        return 0;
    }
    return -1;
}

caveats

  • NRV variables shape, in order to expose a consistent shape, PyCDFpp exposes the reccord count as first dimension and thus its value will be either 0 or 1 (0 mean empty variable).

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

pycdfpp-0.6.4.tar.gz (1.2 MB view hashes)

Uploaded Source

Built Distributions

pycdfpp-0.6.4-pp310-pypy310_pp73-win_amd64.whl (388.8 kB view hashes)

Uploaded PyPy Windows x86-64

pycdfpp-0.6.4-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (816.7 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pycdfpp-0.6.4-pp310-pypy310_pp73-macosx_11_0_arm64.whl (605.8 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

pycdfpp-0.6.4-pp310-pypy310_pp73-macosx_10_15_x86_64.whl (654.8 kB view hashes)

Uploaded PyPy macOS 10.15+ x86-64

pycdfpp-0.6.4-pp39-pypy39_pp73-win_amd64.whl (388.8 kB view hashes)

Uploaded PyPy Windows x86-64

pycdfpp-0.6.4-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (816.4 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pycdfpp-0.6.4-pp39-pypy39_pp73-macosx_11_0_arm64.whl (605.8 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

pycdfpp-0.6.4-pp39-pypy39_pp73-macosx_10_15_x86_64.whl (654.7 kB view hashes)

Uploaded PyPy macOS 10.15+ x86-64

pycdfpp-0.6.4-pp38-pypy38_pp73-win_amd64.whl (388.8 kB view hashes)

Uploaded PyPy Windows x86-64

pycdfpp-0.6.4-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (815.7 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pycdfpp-0.6.4-pp38-pypy38_pp73-macosx_11_0_arm64.whl (605.9 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

pycdfpp-0.6.4-pp38-pypy38_pp73-macosx_10_15_x86_64.whl (654.8 kB view hashes)

Uploaded PyPy macOS 10.15+ x86-64

pycdfpp-0.6.4-cp312-cp312-win_amd64.whl (390.5 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

pycdfpp-0.6.4-cp312-cp312-musllinux_1_1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

pycdfpp-0.6.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (816.4 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

pycdfpp-0.6.4-cp312-cp312-macosx_11_0_arm64.whl (609.4 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

pycdfpp-0.6.4-cp312-cp312-macosx_10_15_x86_64.whl (658.8 kB view hashes)

Uploaded CPython 3.12 macOS 10.15+ x86-64

pycdfpp-0.6.4-cp311-cp311-win_amd64.whl (390.4 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

pycdfpp-0.6.4-cp311-cp311-musllinux_1_1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

pycdfpp-0.6.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (814.7 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pycdfpp-0.6.4-cp311-cp311-macosx_11_0_arm64.whl (607.2 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

pycdfpp-0.6.4-cp311-cp311-macosx_10_15_x86_64.whl (655.6 kB view hashes)

Uploaded CPython 3.11 macOS 10.15+ x86-64

pycdfpp-0.6.4-cp310-cp310-win_amd64.whl (389.3 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

pycdfpp-0.6.4-cp310-cp310-musllinux_1_1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

pycdfpp-0.6.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (814.5 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pycdfpp-0.6.4-cp310-cp310-macosx_11_0_arm64.whl (605.7 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

pycdfpp-0.6.4-cp310-cp310-macosx_10_15_x86_64.whl (654.6 kB view hashes)

Uploaded CPython 3.10 macOS 10.15+ x86-64

pycdfpp-0.6.4-cp39-cp39-win_amd64.whl (382.0 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

pycdfpp-0.6.4-cp39-cp39-musllinux_1_1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

pycdfpp-0.6.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (814.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pycdfpp-0.6.4-cp39-cp39-macosx_11_0_arm64.whl (605.9 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

pycdfpp-0.6.4-cp39-cp39-macosx_10_15_x86_64.whl (654.6 kB view hashes)

Uploaded CPython 3.9 macOS 10.15+ x86-64

pycdfpp-0.6.4-cp38-cp38-win_amd64.whl (389.3 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

pycdfpp-0.6.4-cp38-cp38-musllinux_1_1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

pycdfpp-0.6.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (841.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pycdfpp-0.6.4-cp38-cp38-macosx_11_0_arm64.whl (613.8 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

pycdfpp-0.6.4-cp38-cp38-macosx_10_15_x86_64.whl (678.7 kB view hashes)

Uploaded CPython 3.8 macOS 10.15+ x86-64

Supported by

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