Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pp save rules to handle time means #3132

Merged
merged 7 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
9 changes: 8 additions & 1 deletion lib/iris/fileformats/pp_save_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,14 @@ def _general_time_rules(cube, pp):
if (time_coord is not None and
time_coord.has_bounds() and
clim_season_coord is None and
(fp_coord is not None or frt_coord is not None) and
cm_time_mean is not None):
pp.lbtim.ib = 2
pp.t1 = time_coord.units.num2date(time_coord.bounds[0, 0])
pp.t2 = time_coord.units.num2date(time_coord.bounds[0, 1])

if (time_coord is not None and
time_coord.has_bounds() and
clim_season_coord is None and
cm_time_mean is not None and
cm_time_mean.intervals != () and
cm_time_mean.intervals[0].endswith('hour')):
Expand Down
7 changes: 4 additions & 3 deletions lib/iris/tests/test_cube_to_pp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2010 - 2017, Met Office
# (C) British Crown Copyright 2010 - 2018, Met Office
#
# This file is part of Iris.
#
Expand Down Expand Up @@ -26,6 +26,7 @@
import tempfile

import cf_units
import cftime

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F401 'cftime' imported but unused

import numpy as np

import iris.coords
Expand Down Expand Up @@ -294,8 +295,8 @@ def test_lbvc(self):
self.assertEqual(field.lbvc, lbvc)
self.assertEqual(field.lblev, lblev)
self.assertEqual(field.blev, blev)


def fields_from_cube(cubes):
"""
Return an iterator of PP fields generated from saving the given cube(s)
Expand Down
140 changes: 139 additions & 1 deletion lib/iris/tests/unit/fileformats/pp/test_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@
from __future__ import (absolute_import, division, print_function)
from six.moves import (filter, input, map, range, zip) # noqa

import cftime
import cf_units

# Import iris.tests first so that some things can be initialised before
# importing anything else.
import iris.tests as tests

from iris.coords import DimCoord, CellMethod
from iris.fileformats._ff_cross_references import STASH_TRANS
import iris.fileformats.pp as pp
from iris.fileformats.pp_save_rules import _lbproc_rules
from iris.fileformats.pp_save_rules import _lbproc_rules, verify
from iris.tests import mock
import iris.tests.stock as stock

Expand Down Expand Up @@ -198,5 +201,140 @@ def test_maximum(self):
self.assertEqual(lbproc, 8192)


class TestTimeMean(tests.IrisTest):
'''
Tests that time mean cell method is converted to pp appropriately.

Pattern is pairs of tests - one with time mean method, and one without, to
show divergent behaviour.

'''
def test_t1_time_mean(self):
cube = _get_single_time_cube(set_time_mean=True)
tc = cube.coord(axis='t')
expected = tc.units.num2date(0)

with mock.patch('iris.fileformats.pp.PPField3',
autospec=True) as pp_field:
verify(cube, pp_field)
actual = pp_field.t1

self.assertEqual(expected, actual)

def test_t1_no_time_mean(self):
cube = _get_single_time_cube()
tc = cube.coord(axis='t')
expected = tc.units.num2date(15)

with mock.patch('iris.fileformats.pp.PPField3',
autospec=True) as pp_field:
verify(cube, pp_field)
actual = pp_field.t1

self.assertEqual(expected, actual)

def test_t2_time_mean(self):
cube = _get_single_time_cube(set_time_mean=True)
tc = cube.coord(axis='t')
expected = tc.units.num2date(30)

with mock.patch('iris.fileformats.pp.PPField3',
autospec=True) as pp_field:
verify(cube, pp_field)
actual = pp_field.t2

self.assertEqual(expected, actual)

def test_t2_no_time_mean(self):
cube = _get_single_time_cube(set_time_mean=False)
expected = cftime.datetime(0, 0, 0)

with mock.patch('iris.fileformats.pp.PPField3',
autospec=True) as pp_field:
verify(cube, pp_field)
actual = pp_field.t2
self.assertEqual(expected, actual)

def test_lbft_no_forecast_time(self):
# Different pattern here: checking that lbft hasn't been changed from
# the default value.
cube = _get_single_time_cube()
mock_lbft = mock.sentinel.lbft

with mock.patch('iris.fileformats.pp.PPField3',
autospec=True) as pp_field:
pp_field.lbft = mock_lbft
verify(cube, pp_field)
actual = pp_field.lbft

assert(mock_lbft is actual)

def test_lbtim_no_time_mean(self):
cube = _get_single_time_cube()
expected_ib = 0
expected_ic = 2 # 360 day calendar

with mock.patch('iris.fileformats.pp.PPField3',
autospec=True) as pp_field:
verify(cube, pp_field)
actual_ib = pp_field.lbtim.ib
actual_ic = pp_field.lbtim.ic

self.assertEqual(expected_ib, actual_ib)
self.assertEqual(expected_ic, actual_ic)

def test_lbtim_time_mean(self):
cube = _get_single_time_cube(set_time_mean=True)
expected_ib = 2 # Time mean
expected_ic = 2 # 360 day calendar

with mock.patch('iris.fileformats.pp.PPField3',
autospec=True) as pp_field:
verify(cube, pp_field)
actual_ib = pp_field.lbtim.ib
actual_ic = pp_field.lbtim.ic

self.assertEqual(expected_ib, actual_ib)
self.assertEqual(expected_ic, actual_ic)

def test_lbproc_no_time_mean(self):
cube = _get_single_time_cube()
expected = 0

with mock.patch('iris.fileformats.pp.PPField3',
autospec=True) as pp_field:
verify(cube, pp_field)
actual = pp_field.lbproc

self.assertEqual(expected, actual)

def test_lbproc_time_mean(self):
cube = _get_single_time_cube(set_time_mean=True)
expected = 128

with mock.patch('iris.fileformats.pp.PPField3',
autospec=True) as pp_field:
verify(cube, pp_field)
actual = pp_field.lbproc

self.assertEqual(expected, actual)


def _get_single_time_cube(set_time_mean=False):
cube = stock.realistic_3d()[0:1, :, :]
cube.remove_coord('time')
cube.remove_coord('forecast_period')
tc = DimCoord(
points=[15, ],
standard_name='time',
units=cf_units.Unit('days since epoch', calendar='360_day'),
bounds=[[0, 30], ],
)
cube.add_dim_coord(tc, 0)
if set_time_mean:
cube.cell_methods = (CellMethod("mean", coords='time'), )
return cube


if __name__ == "__main__":
tests.main()