Skip to content

Commit

Permalink
Merge pull request #4 from psc-code/pr/build
Browse files Browse the repository at this point in the history
build bits
  • Loading branch information
germasch authored Jun 5, 2024
2 parents b706d0d + beedfda commit 216d246
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 73 deletions.
12 changes: 12 additions & 0 deletions .copier-answers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changes here will be overwritten by Copier; NEVER EDIT MANUALLY
_commit: 2024.03.10
_src_path: gh:scientific-python/cookie
backend: skbuild
email: [email protected]
full_name: Kai Germaschewski
license: BSD
org: psc-code
project_name: xarray-pschdf5
project_short_description: XArray reader for PSC HDF5 data
url: https://github.com/psc-code/xarray-pschdf5
vcs: true
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ jobs:
- uses: pre-commit/[email protected]
with:
extra_args: --hook-stage manual --all-files
- name: Run PyLint
run: |
echo "::add-matcher::$GITHUB_WORKSPACE/.github/matchers/pylint.json"
pipx run nox -s pylint
# - name: Run PyLint
# run: |
# echo "::add-matcher::$GITHUB_WORKSPACE/.github/matchers/pylint.json"
# pipx run nox -s pylint

checks:
name: Check Python ${{ matrix.python-version }} on ${{ matrix.runs-on }}
Expand Down
16 changes: 8 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ repos:
- id: clang-format
types_or: [c++, c, cuda]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.8.0"
hooks:
- id: mypy
files: src|tests
args: []
additional_dependencies:
- pytest
# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: "v1.8.0"
# hooks:
# - id: mypy
# files: src|tests
# args: []
# additional_dependencies:
# - pytest

- repo: https://github.com/codespell-project/codespell
rev: "v2.2.6"
Expand Down
6 changes: 0 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
cmake_minimum_required(VERSION 3.15...3.26)
project(${SKBUILD_PROJECT_NAME} LANGUAGES CXX)

set(PYBIND11_FINDPYTHON ON)
find_package(pybind11 CONFIG REQUIRED)

pybind11_add_module(_core MODULE src/main.cpp)
install(TARGETS _core DESTINATION ${SKBUILD_PROJECT_NAME})
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["pybind11", "scikit-build-core"]
requires = ["scikit-build-core"]
build-backend = "scikit_build_core.build"


Expand Down Expand Up @@ -33,6 +33,7 @@ dynamic = ["version"]
dependencies = [
"pugixml >= 0.5.0",
"h5py",
"xarray",
]

[project.optional-dependencies]
Expand Down Expand Up @@ -145,6 +146,8 @@ ignore = [
"PLR09", # Too many <...>
"PLR2004", # Magic value used in comparison
"ISC001", # Conflicts with formatter
"C408", # like my dict() calls
"RET504",
]
isort.required-imports = ["from __future__ import annotations"]
# Uncomment if using a _compat.typing backport
Expand Down
2 changes: 0 additions & 2 deletions src/xarray_pschdf5/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@
from ._version import version as __version__

__all__ = ["__version__"]

from .pschdf5_backend import PscHdf5Entrypoint
119 changes: 67 additions & 52 deletions src/xarray_pschdf5/pschdf5_backend.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from __future__ import annotations

import os
from collections import OrderedDict
from pugixml import pugi
from typing import Any, ClassVar

import h5py
import numpy as np
import xarray as xr
from pugixml import pugi
from xarray.backends import BackendEntrypoint
import numpy as np
import os


class PscHdf5Entrypoint(BackendEntrypoint):
Expand All @@ -18,7 +22,7 @@ def open_dataset(
):
return pschdf5_open_dataset(filename_or_obj, drop_variables=drop_variables)

open_dataset_parameters = ["filename_or_obj", "drop_variables"]
open_dataset_parameters: ClassVar[Any] = ["filename_or_obj", "drop_variables"]

def guess_can_open(self, filename_or_obj):
if filename_or_obj.endswith(".xdmf"):
Expand All @@ -39,90 +43,101 @@ def pschdf5_open_dataset(filename_or_obj, *, drop_variables=None):

vars = dict()
assert len(grids) == 1
for gridname, grid in grids.items():
for _, grid in grids.items():
for fldname, fld in grid["fields"].items():
data_dims = fld["dims"]
if fldname in drop_variables:
continue

# data_dims = fld["dims"]
data_path = fld["path"]
h5_filename, h5_path = fld['path'].split(':')
h5_file = h5py.File(dirname + '/' + h5_filename)
h5_filename, h5_path = fld["path"].split(":")
h5_file = h5py.File(dirname + "/" + h5_filename)
data = h5_file[h5_path][:].T

data_attrs = dict(path=data_path)
vars[fldname] = xr.DataArray(
data=data,
dims=["x", "y", "z"],
attrs=data_attrs)

coords = {"xyz"[d]: make_crd(grid['topology']['dims'][d], grid['geometry']
['origin'][d], grid['geometry']['spacing'][d]) for d in range(3)}
data=data, dims=["x", "y", "z"], attrs=data_attrs
)

coords = {
"xyz"[d]: make_crd(
grid["topology"]["dims"][d],
grid["geometry"]["origin"][d],
grid["geometry"]["spacing"][d],
)
for d in range(3)
}

attrs = dict(run=meta["run"], time=meta["time"])

ds = xr.Dataset(vars, coords=coords, attrs=attrs)
# ds.set_close(my_close_method)
# ds.set_close(my_close_method)

return ds


def make_crd(dim, origin, spacing):
return origin + np.arange(.5, dim) * spacing
return origin + np.arange(0.5, dim) * spacing


def read_xdmf(filename):
doc = pugi.XMLDocument()
result = doc.load_file(filename)
if not result:
print('parse error: status=%r description=%r' %
(result.status, result.description()))
assert False

grid_collection = doc.child('Xdmf').child('Domain').child('Grid')
assert grid_collection.attribute('GridType').value() == 'Collection'
grid_time = grid_collection.child('Time')
assert grid_time.attribute('Type').value() == 'Single'
time = grid_time.attribute('Value').value()
raise f"parse error: status={result.status} description={result.description()}"

grid_collection = doc.child("Xdmf").child("Domain").child("Grid")
assert grid_collection.attribute("GridType").value() == "Collection"
grid_time = grid_collection.child("Time")
assert grid_time.attribute("Type").value() == "Single"
time = grid_time.attribute("Value").value()
rv = {}
rv['time'] = time
rv['grids'] = {}
rv["time"] = time
rv["grids"] = {}
for node in grid_collection.children():
if node.name() == 'Grid':
if node.name() == "Grid":
grid = {}
grid_name = node.attribute('Name').value()
topology = node.child('Topology')
grid_name = node.attribute("Name").value()
topology = node.child("Topology")
# assert topology.attribute('TopologyType').value() == '3DCoRectMesh'
dims = topology.attribute('Dimensions').value()
dims = topology.attribute("Dimensions").value()
dims = np.asarray([int(d) - 1 for d in dims.split(" ")])[::-1]
grid['topology'] = {'type': topology.attribute(
'TopologyType').value(), 'dims': dims}
grid["topology"] = {
"type": topology.attribute("TopologyType").value(),
"dims": dims,
}

geometry = node.child("Geometry")
assert geometry.attribute("GeometryType").value() == "Origin_DxDyDz"

geometry = node.child('Geometry')
assert geometry.attribute(
'GeometryType').value() == 'Origin_DxDyDz'
grid["geometry"] = dict()
for child in geometry.children():
if child.attribute('Name').value() == 'Origin':
origin = np.asarray(
[float(x) for x in child.text().as_string().split(" ")])[::-1]
if child.attribute('Name').value() == 'Spacing':
spacing = np.asarray(
[float(x) for x in child.text().as_string().split(" ")])[::-1]
if child.attribute("Name").value() == "Origin":
grid["geometry"]["origin"] = np.asarray(
[float(x) for x in child.text().as_string().split(" ")]
)[::-1]

grid['geometry'] = {'origin': origin, 'spacing': spacing}
if child.attribute("Name").value() == "Spacing":
grid["geometry"]["spacing"] = np.asarray(
[float(x) for x in child.text().as_string().split(" ")]
)[::-1]

flds = OrderedDict()
for child in node.children():
if child.name() != 'Attribute':
if child.name() != "Attribute":
continue

fld = child.attribute('Name').value()
item = child.child('DataItem')
fld_dims = np.asarray([int(d) for d in item.attribute(
'Dimensions').value().split(" ")])[::-1]
fld = child.attribute("Name").value()
item = child.child("DataItem")
fld_dims = np.asarray(
[int(d) for d in item.attribute("Dimensions").value().split(" ")]
)[::-1]
assert np.all(fld_dims == dims)
assert item.attribute('Format').value() == 'HDF'
assert item.attribute("Format").value() == "HDF"
path = item.text().as_string()
flds[fld] = {'path': path, 'dims': dims}
flds[fld] = {"path": path, "dims": dims}

grid['fields'] = flds
rv['grids'][grid_name] = grid
grid["fields"] = flds
rv["grids"][grid_name] = grid

return rv

0 comments on commit 216d246

Please sign in to comment.