Skip to content

Commit

Permalink
Use ruff formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bouweandela committed Sep 26, 2024
1 parent d92982a commit 801e5f8
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions esmvalcore/preprocessor/_derive/vegfrac.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ def required(project):
"""Declare the variables needed for derivation."""
required = [
{
'short_name': 'baresoilFrac',
"short_name": "baresoilFrac",
},
{
'short_name': 'residualFrac',
"short_name": "residualFrac",
},
{
'short_name': 'sftlf',
'mip': 'fx',
"short_name": "sftlf",
"mip": "fx",
},
]
return required
Expand All @@ -32,17 +32,23 @@ def required(project):
def calculate(cubes):
"""Compute vegetation fraction from bare soil fraction."""
baresoilfrac_cube = cubes.extract_cube(
NameConstraint(var_name='baresoilFrac'))
NameConstraint(var_name="baresoilFrac")
)
residualfrac_cube = cubes.extract_cube(
NameConstraint(var_name='residualFrac'))
sftlf_cube = cubes.extract_cube(NameConstraint(var_name='sftlf'))
NameConstraint(var_name="residualFrac")
)
sftlf_cube = cubes.extract_cube(NameConstraint(var_name="sftlf"))

# Add time dimension to sftlf
target_shape_sftlf = (baresoilfrac_cube.shape[0], *sftlf_cube.shape)
target_chunks_sftlf = tuple(
baresoilfrac_cube.lazy_data().chunks[0],
*sftlf_cube.lazy_data().chunks,
) if baresoilfrac_cube.has_lazy_data() else None
target_chunks_sftlf = (
tuple(
baresoilfrac_cube.lazy_data().chunks[0],
*sftlf_cube.lazy_data().chunks,
)
if baresoilfrac_cube.has_lazy_data()
else None
)
sftlf_data = broadcast_to_shape(
sftlf_cube.core_data(),
target_shape_sftlf,
Expand All @@ -53,13 +59,16 @@ def calculate(cubes):

# Regrid sftlf if necessary and adapt mask
if sftlf_cube.shape != baresoilfrac_cube.shape:
sftlf_cube = regrid(sftlf_cube, baresoilfrac_cube, 'linear')
sftlf_cube = regrid(sftlf_cube, baresoilfrac_cube, "linear")
sftlf_cube.data = da.ma.masked_array(
sftlf_cube.core_data(),
mask=da.ma.getmaskarray(baresoilfrac_cube.core_data()))
mask=da.ma.getmaskarray(baresoilfrac_cube.core_data()),
)

# Calculate vegetation fraction
baresoilfrac_cube.data = (sftlf_cube.core_data() -
baresoilfrac_cube.core_data() -
residualfrac_cube.core_data())
baresoilfrac_cube.data = (
sftlf_cube.core_data()
- baresoilfrac_cube.core_data()
- residualfrac_cube.core_data()
)
return baresoilfrac_cube

0 comments on commit 801e5f8

Please sign in to comment.