Skip to content

Commit

Permalink
add sorting dymensions
Browse files Browse the repository at this point in the history
  • Loading branch information
mandresm committed Nov 15, 2024
1 parent 265fa59 commit 95add68
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/pymorize/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,18 @@ def trigger_compute(data, rule_spec, *args, **kwargs):
return data.compute()
# Data doesn't have a compute method, do nothing
return data


def sort_dimensions(data, rule_spec):
"""Sorts the dimensions of a DataArray based on the array_order in the rule_spec."""
dryrun = rule_spec.get("dryrun", False)

missing_dims = rule_spec.get("sort_dimensions_missing_dims", "raise")

logger.info(
f"Transposing dimensions of data from {data.dims} to {rule_spec.array_order}"
)
if not dryrun:
data = data.transpose(*rule_spec.array_order, missing_dims=missing_dims)

return data
44 changes: 44 additions & 0 deletions tests/fixtures/sample_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,47 @@ def rule_with_units():
)
r.data_request_variable = r.data_request_variables[0]
return r


@pytest.fixture
def rule_with_unsorted_data():
return Rule(
array_order=["time", "lat", "lon"],
inputs=[
{
"path": "/some/files/containing/",
"pattern": "var1.*.nc",
},
{
"path": "/some/other/files/containing/",
"pattern": r"var1_(?P<year>\d{4}).nc",
},
],
cmor_variable="var1",
pipelines=["pymorize.pipeline.TestingPipeline"],
data_request_variables=[
DataRequestVariable(
variable_id="var1",
unit="kg m-2 s-1",
description="Some description",
time_method="instant",
table="Some Table",
frequency="mon",
realms=["atmos"],
standard_name="some_standard_name",
cell_methods="time: mean",
cell_measures="area: areacella",
)
],
)


@pytest.fixture
def dummy_array():
import numpy
import xarray as xr

return xr.DataArray(
numpy.random.rand(10, 10, 10),
dims=["lat", "lon", "time"],
)
9 changes: 9 additions & 0 deletions tests/unit/template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import numpy as np
import pytest
import xarray as xr


# @pytest.mark.parametrize("test_input", a_list)
def test_name(test_input):
"""Docstrig"""
pass
13 changes: 13 additions & 0 deletions tests/unit/test_array_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import numpy as np
import pytest
import xarray as xr


# @pytest.mark.parametrize("test_input", a_list)
def test_sort_dimensions(dummy_array, rule_with_unsorted_data):
"""Test to check that dimensions are sorted correctly"""
from pymorize.generic import sort_dimensions

dummy_array = sort_dimensions(dummy_array, rule_with_unsorted_data)

assert dummy_array.dims == tuple(rule_with_unsorted_data.array_order)

0 comments on commit 95add68

Please sign in to comment.