Skip to content

Commit

Permalink
Unit handling and round-trip .nc conversion fixes (#13)
Browse files Browse the repository at this point in the history
* Fix roundtrip conversion of named_options

* Fix unit-conversion error ("" as ureg.dimensionless, not skip conversion)

* Pre-commit fixes

* Remove unnecessary string catch in fileio

* Add test to check conversion to dimensionless

* Add netcdf4 to environment

* Add back in fix for str in netcdf

* Mypy fixes

* Add default value for netcdf engine

* Update cfspopcon/file_io.py

Co-authored-by: Christoph Hasse <[email protected]>

* fix pre-commit error

---------

Co-authored-by: Christoph Hasse <[email protected]>
  • Loading branch information
tbody-cfs and hassec authored Nov 30, 2023
1 parent 2b7f8ee commit dad2abb
Show file tree
Hide file tree
Showing 13 changed files with 777 additions and 721 deletions.
2 changes: 1 addition & 1 deletion cfspopcon/atomic_data/read_radas_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np
import xarray as xr
from scipy.interpolate import RectBivariateSpline, RegularGridInterpolator # type: ignore[import]
from scipy.interpolate import RectBivariateSpline, RegularGridInterpolator # type: ignore[import-untyped]

from ..named_options import Impurity
from ..unit_handling import convert_units, magnitude, ureg
Expand Down
4 changes: 2 additions & 2 deletions cfspopcon/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from pathlib import Path

import click
import matplotlib.pyplot as plt # type:ignore[import]
import matplotlib.pyplot as plt
import xarray as xr
from ipdb import launch_ipdb_on_exception # type:ignore[import]
from ipdb import launch_ipdb_on_exception # type:ignore[import-untyped]

from cfspopcon import file_io
from cfspopcon.input_file_handling import read_case
Expand Down
12 changes: 8 additions & 4 deletions cfspopcon/file_io.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Functions for saving results to file and loading those files."""
import json
from pathlib import Path
from typing import Any
from typing import Any, Literal

import numpy as np
import xarray as xr

from .helpers import convert_named_options
Expand All @@ -26,7 +27,9 @@ def sanitize_variable(val: xr.DataArray, key: str) -> xr.DataArray:
return val


def write_dataset_to_netcdf(dataset: xr.Dataset, filepath: Path) -> None:
def write_dataset_to_netcdf(
dataset: xr.Dataset, filepath: Path, netcdf_writer: Literal["netcdf4", "scipy", "h5netcdf"] = "netcdf4"
) -> None:
"""Write a dataset to a NetCDF file."""
serialized_dataset = dataset.copy()
for key in dataset.keys():
Expand All @@ -37,7 +40,7 @@ def write_dataset_to_netcdf(dataset: xr.Dataset, filepath: Path) -> None:
assert isinstance(key, str) # because hashable type of key is broader str but we know it's str
serialized_dataset[key] = sanitize_variable(dataset[key], key)

serialized_dataset.to_netcdf(filepath)
serialized_dataset.to_netcdf(filepath, engine=netcdf_writer)


def promote_variable(val: xr.DataArray, key: str) -> Any:
Expand All @@ -47,7 +50,8 @@ def promote_variable(val: xr.DataArray, key: str) -> Any:
except KeyError:
pass

if val.dtype == object:
# scipy i/o yields objects for strings while netcdf4 yields np.str_
if val.dtype == object or val.dtype.type == np.str_:
if val.size == 1:
return convert_named_options(key, val.item())
else:
Expand Down
2 changes: 1 addition & 1 deletion cfspopcon/formulas/divertor_metrics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Divertor loading and functions to calculate OMP pitch (for q_parallel calculation)."""
import numpy as np
from scipy import constants # type: ignore[import]
from scipy import constants # type: ignore[import-untyped]

from ..unit_handling import ureg, wraps_ufunc

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from numpy import float64
from numpy.typing import NDArray
from scipy import constants # type: ignore[import]
from scipy import constants # type: ignore[import-untyped]


def reaction_energy_DT(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import pandas as pd
import yaml
from numpy.typing import NDArray
from scipy.interpolate import RectBivariateSpline # type: ignore[import]
from scipy.interpolate import RectBivariateSpline # type: ignore[import-untyped]

plasma_profiles_directory = Path(__file__).parent

Expand Down
2 changes: 1 addition & 1 deletion cfspopcon/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np
import xarray as xr
from scipy.interpolate import griddata # type:ignore[import]
from scipy.interpolate import griddata # type:ignore[import-untyped]

from cfspopcon.unit_handling import Unit, magnitude

Expand Down
2 changes: 1 addition & 1 deletion cfspopcon/unit_handling/default_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def convert_to_default_units(value: Quantity, key: str) -> Quantity:
def convert_to_default_units(value: Union[float, Quantity, xr.DataArray], key: str) -> Union[float, Quantity, xr.DataArray]:
"""Convert an array or scalar to default units."""
unit = DEFAULT_UNITS[key]
if unit is None or unit == "":
if unit is None:
return value
elif isinstance(value, (xr.DataArray, Quantity)):
return convert_units(value, unit)
Expand Down
2 changes: 1 addition & 1 deletion cfspopcon/unit_handling/setup_unit_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np
import numpy.typing as npt
import pint
import pint_xarray # type:ignore[import]
import pint_xarray # type:ignore[import-untyped]
import xarray as xr
from pint.errors import DimensionalityError
from typing_extensions import ParamSpec
Expand Down
1,452 changes: 744 additions & 708 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ xarray = "^2023.4.1"
pint-xarray = "^0.3"
ipdb = "^0.13.13"
click = "^8.1.0"
netcdf4 = "^1.6.5"

[tool.poetry.group.dev.dependencies]
pre-commit = "^2.20.0"
Expand Down
Binary file modified tests/regression_results/SPARC_PRD_result.nc
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/test_unit_handling/test_conversions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import warnings

import numpy as np
import pytest
import xarray as xr

from cfspopcon.unit_handling import dimensionless_magnitude, ureg, Quantity, ureg, convert_to_default_units


def test_conversion_of_dimensionless():

val = Quantity(2.0, ureg.percent)

assert np.isclose(dimensionless_magnitude(val), 2e-2)
assert np.isclose(convert_to_default_units(val, key="beta"), 2e-2)

0 comments on commit dad2abb

Please sign in to comment.