Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions esmvalcore/cmor/tables/custom/CMOR_hfns.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
SOURCE: CMIP6
!============
variable_entry: hfns
!============
modeling_realm: atmos
!----------------------------------
! Variable attributes:
!----------------------------------
standard_name:
units: W m-2
cell_methods: time: mean
cell_measures: area: areacella
long_name: Surface Net Heat Flux
!----------------------------------
! Additional variable information:
!----------------------------------
dimensions: longitude latitude time
out_name: hfns
type: real
positive: up
!----------------------------------
33 changes: 33 additions & 0 deletions esmvalcore/preprocessor/_derive/hfns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Derivation of variable `hfns`."""

from iris import NameConstraint

from ._baseclass import DerivedVariableBase


class DerivedVariable(DerivedVariableBase):
"""Derivation of variable `hfns`."""

@staticmethod
def required(project):
"""Declare the variables needed for derivation."""
required = [
{
'short_name': 'hfls',
},
{
'short_name': 'hfss',
},
]
return required

@staticmethod
def calculate(cubes):
"""Compute surface net heat flux."""
hfls_cube = cubes.extract_cube(NameConstraint(var_name='hfls'))
hfss_cube = cubes.extract_cube(NameConstraint(var_name='hfss'))

hfns_cube = hfls_cube + hfss_cube
hfns_cube.units = hfls_cube.units

return hfns_cube
45 changes: 45 additions & 0 deletions tests/unit/preprocessor/_derive/test_hfns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Test derivation of ``hfns``."""
import numpy as np
import pytest
from iris.cube import CubeList

from esmvalcore.preprocessor._derive import hfns

from .test_shared import get_cube


@pytest.fixture
def cubes():
"""Input cubes for derivation of ``xch4``."""
hfls_cube = get_cube([[[1.0]]], air_pressure_coord=False,
standard_name='surface_upward_latent_heat_flux',
var_name='hfls', units='W m-2')
hfss_cube = get_cube([[[1.0]]], air_pressure_coord=False,
standard_name='surface_upward_sensible_heat_flux',
var_name='hfss', units='W m-2')
return CubeList([hfls_cube, hfss_cube])


def test_hfns_calculate(cubes):
"""Test function ``calculate``."""
derived_var = hfns.DerivedVariable()
out_cube = derived_var.calculate(cubes)
assert out_cube.shape == (1, 1, 1)
assert out_cube.units == 'W m-2'
assert out_cube.coords('time')
assert out_cube.coords('latitude')
assert out_cube.coords('longitude')
np.testing.assert_allclose(out_cube.data, [[[2.0]]])
np.testing.assert_allclose(out_cube.coord('time').points, [0.0])
np.testing.assert_allclose(out_cube.coord('latitude').points, [45.0])
np.testing.assert_allclose(out_cube.coord('longitude').points, [10.0])


def test_hfns_required():
"""Test function ``required``."""
derived_var = hfns.DerivedVariable()
output = derived_var.required(None)
assert output == [
{'short_name': 'hfls'},
{'short_name': 'hfss'},
]