ImperialCollegeLondon / virtual_ecosystem

This repository is the home for the codebase for the Virtual Ecosystem project.
https://virtual-ecosystem.readthedocs.io
BSD 3-Clause "New" or "Revised" License
9 stars 1 forks source link

Simplify and unify concatenating layers #411

Closed vgro closed 1 month ago

vgro commented 5 months ago

Most variables in the data object have the full vertical dimension/coordinates, including rows of NaN, for example the soil layers in air_temperature and the canopy layers that are not filled. We run a lot of functions by selecting the relevant layers and converting them to numpy arrays and then concatenating everything back together. This is often clunky and very hard to read, see comments in #406

We would like shapes stored somewhere that we can use to select and replace layers from the data object. For example like:

true_data_rows = list(range(0, true_canopy_layers_n + 1)) + list(
            range(
                true_canopy_layers_n + empty_canopy_layers + 1,
                true_canopy_layers_n + empty_canopy_layers + 3,
            )
        )
        for var in ["wind_speed_canopy", "friction_velocity"]:
            # Might make sense to store the shape somewhere and just use np.full(shape, np.nan)
            var_data = np.full_like(self.data['leaf_area_index'], np.nan)
            var_data[true_data_rows, :] = wind_update[var]
            var_out = DataArray(
                var_data,
                dims=self.data["layer_heights"].dims,
                coords=self.data["layer_heights"].coords,
            )
            wind_output[var] = var_out

For tests, we would like something like

def fill_expected_data_layers(shape: tuple[int, ...], data_layers: list[int], data: NDArray) -> NDArray:
    array = np.full(shape, np.nan)
    array[data_layers, :] = data
    return array
davidorme commented 3 months ago

Maybe add this to the LayerStructure as a method get_empty_layer_array that returns the expected np.nan filled cell_id by layer_structure canopy layer. Might need to be a stand alone util actually because it needs access to Grid properties?

vgro commented 1 month ago

resolved with new layer structure #421