# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# (C) British Crown Copyright 2017-2019 Met Office.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""Module for saving netcdf cubes with desired attribute types."""
import os
import warnings
import cf_units
import iris
from improver.metadata.check_datatypes import (
check_datatypes, check_time_coordinate_metadata)
[docs]def _order_cell_methods(cube):
"""
Sorts the cell methods on a cube such that if there are multiple methods
they are always written in a consistent order in the output cube. The
input cube is modified.
Args:
cube (iris.cube.Cube):
The cube on which the cell methods are to be sorted.
"""
cell_methods = tuple(sorted(cube.cell_methods))
cube.cell_methods = cell_methods
[docs]def save_netcdf(cubelist, filename):
"""Save the input Cube or CubeList as a NetCDF file and check metadata
where required for integrity.
Uses the functionality provided by iris.fileformats.netcdf.save with
local_keys to record non-global attributes as data attributes rather than
global attributes.
Args:
cubelist (iris.cube.Cube or iris.cube.CubeList):
Cube or list of cubes to be saved
filename (str):
Filename to save input cube(s)
Raises:
warning if cubelist contains cubes of varying dimensions.
"""
if isinstance(cubelist, iris.cube.Cube):
cubelist = iris.cube.CubeList([cubelist])
elif not isinstance(cubelist, iris.cube.CubeList):
cubelist = iris.cube.CubeList(cubelist)
for cube in cubelist:
_order_cell_methods(cube)
_check_metadata(cube)
# If all xy slices are the same shape, use this to determine
# the chunksize for the netCDF (eg. 1, 1, 970, 1042)
chunksizes = None
if len(set([cube.shape[:2] for cube in cubelist])) == 1:
cube = cubelist[0]
if cube.ndim >= 2:
xy_chunksizes = [cube.shape[-2], cube.shape[-1]]
chunksizes = tuple([1] * (cube.ndim - 2) + xy_chunksizes)
else:
msg = ("Chunksize not set as cubelist "
"contains cubes of varying dimensions")
warnings.warn(msg)
global_keys = ['title', 'um_version', 'grid_id', 'source', 'Conventions',
'mosg__grid_type', 'mosg__model_configuration',
'mosg__grid_domain', 'mosg__grid_version',
'institution', 'history', 'bald__isPrefixedBy']
local_keys = {key for cube in cubelist
for key in cube.attributes.keys()
if key not in global_keys}
cubelist = _append_metadata_cube(cubelist, global_keys)
# save atomically by writing to a temporary file and then renaming
ftmp = str(filename) + '.tmp'
iris.fileformats.netcdf.save(cubelist, ftmp, local_keys=local_keys,
complevel=1, shuffle=True, zlib=True,
chunksizes=chunksizes)
os.rename(ftmp, filename)