# -*- 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 calculating the probability of specific precipitation phases."""
import operator
import iris
import numpy as np
from cf_units import Unit
from improver import BasePlugin
from improver.nbhood.nbhood import GeneratePercentilesFromANeighbourhood
from improver.metadata.utilities import (
create_new_diagnostic_cube, generate_mandatory_attributes)
from improver.utilities.cube_checker import spatial_coords_match
[docs]class PrecipPhaseProbability(BasePlugin):
"""
This plugin converts a falling-phase-change-level cube into the
probability of a specific precipitation phase being found at the surface.
For snow; the 80th percentile is taken from a neighbourhood around
each point and is compared with the orography. Where the orography is
higher, the returned probability-of-snow is 1, else 0.
For rain, the above method is modified to get the 20th percentile
and where the orography is lower than the percentile value, the returned
probability-of-rain is 1, else 0.
"""
[docs] def __init__(self, radius=10000.):
"""
Initialise plugin
Args:
radius (float):
Neighbourhood radius from which 80th percentile is found (m)
"""
self.percentile_plugin = GeneratePercentilesFromANeighbourhood
self._nbhood_shape = 'circular'
self.radius = radius
[docs] def process(self, cubes):
"""
Derives the probability of a precipitation phase at the surface. If
the snow-sleet falling-level is supplied, this is the probability of
snow at (or below) the surface. If the sleet-rain falling-level is
supplied, this is the probability of rain at (or above) the surface.
Args:
cubes (iris.cube.CubeList or list):
Contains cubes of the altitude of the phase-change level (this
can be snow->sleet, or sleet->rain) and the altitude of the
orography.
Returns:
iris.cube.Cube:
Contains the probability of a specific precipitation phase
reaching the surface orography. If the falling_level_cube was
snow->sleet, then this will be the probability of snow at the
surface. If the falling_level_cube was sleet->rain, then this
will be the probability of rain at the surface.
The probabilities are categorical (1 or 0) allowing
precipitation to be divided uniquely between snow, sleet and
rain phases.
"""
self._extract_input_cubes(cubes)
processed_falling_level = iris.util.squeeze(
self.get_discriminating_percentile(
self.falling_level_cube))
result_data = np.where(
self.comparator(
self.orography_cube.data,
processed_falling_level.data),
1, 0).astype('float32')
mandatory_attributes = generate_mandatory_attributes(
[self.falling_level_cube])
cube = create_new_diagnostic_cube(
f'probability_of_{self.param}_at_surface',
Unit('1'),
self.falling_level_cube,
mandatory_attributes,
data=result_data)
return cube