Skip to content

Commit

Permalink
Allow truncation in other than time dimension
Browse files Browse the repository at this point in the history
  • Loading branch information
tukiains committed Apr 16, 2024
1 parent ead562a commit 61cd6dc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
10 changes: 6 additions & 4 deletions cloudnetpy/concat_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
from cloudnetpy.exceptions import InconsistentDataError


def truncate_netcdf_file(filename: str, output_file: str, n_profiles: int) -> None:
"""Truncates netcdf file in 'time' dimension taking only n_profiles.
def truncate_netcdf_file(
filename: str, output_file: str, n_profiles: int, dim_name: str = "time"
) -> None:
"""Truncates netcdf file in dim_name dimension taking only n_profiles.
Useful for creating small files for tests.
"""
with (
netCDF4.Dataset(filename, "r") as nc,
netCDF4.Dataset(output_file, "w", format=nc.data_model) as nc_new,
):
for dim in nc.dimensions:
dim_len = None if dim == "time" else nc.dimensions[dim].size
dim_len = None if dim == dim_name else nc.dimensions[dim].size
nc_new.createDimension(dim, dim_len)
for attr in nc.ncattrs():
value = getattr(nc, attr)
Expand All @@ -30,7 +32,7 @@ def truncate_netcdf_file(filename: str, output_file: str, n_profiles: int) -> No
zlib=True,
fill_value=fill_value,
)
if dimensions and "time" in dimensions[0]:
if dimensions and dim_name in dimensions[0]:
if array.ndim == 1:
var[:] = array[:n_profiles]
if array.ndim == 2:
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ pattern = ["MAJOR = (?P<major>\\d+)", "MINOR = (?P<minor>\\d+)", "PATCH = (?P<pa
changelog = "CHANGELOG.md"

[tool.ruff]
extend-select = ["I"]
exclude = ["cloudnetpy/model_evaluation/tests/", "tests/"]

[tool.ruff.lint]
Expand Down

0 comments on commit 61cd6dc

Please sign in to comment.