# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# (C) British Crown copyright. The 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.
"""This module contains the VerticalUpdraught plugin"""
from typing import List
import numpy as np
from iris.cube import Cube, CubeList
from improver import BasePlugin
from improver.metadata.utilities import (
create_new_diagnostic_cube,
generate_mandatory_attributes,
)
from improver.utilities.cube_checker import spatial_coords_match
[docs]class VerticalUpdraught(BasePlugin):
"""
Methods to calculate the maximum vertical updraught from CAPE and precipitation rate as
defined in Hand (2002) and Golding (1998) with the precipitation rate modifier found in
the UKPP CDP code.
Hand, W. 2002. "The Met Office Convection Diagnosis Scheme." Meteorological Applications
9(1): 69-83. doi:10.1017/S1350482702001081.
Golding, B.W. 1998. "Nimrod: A system for generating automated very short range forecasts."
Meteorol. Appl. 5: 1-16. doi:https://doi.org/10.1017/S1350482798000577.
"""
[docs] def __init__(self, model_id_attr: str = None):
"""
Set up class
Args:
model_id_attr:
Name of model ID attribute to be copied from source cubes to output cube
"""
self.model_id_attr = model_id_attr
self.cape = Cube(None)
self.precip = Cube(None)
self.cube_names = [
"atmosphere_convective_available_potential_energy",
"lwe_precipitation_rate_max",
]
self._minimum_cape = 10.0 # J kg-1. Minimum value to diagnose updraught from
self._minimum_precip = 5.0 # mm h-1. Minimum value to diagnose updraught from
[docs] def _updraught_from_cape(self) -> np.ndarray:
"""
Calculate the updraught from CAPE data
Calculation is 0.25 * sqrt(2 * cape)
Returns zero where CAPE < 10 J kg-1
"""
updraught = 0.25 * (2 * self.cape.data) ** 0.5
updraught[self.cape.data < self._minimum_cape] = 0.0
return updraught.astype(np.float32)
[docs] def _updraught_increment_from_precip(self) -> np.ndarray:
"""
Calculate the updraught increment from the precipitation rate.
Calculation is 7.33 * (precip / 28.7)^0.22
Where precipitation rate < 5 mm h-1, increment is zero.
"""
increment = 7.33 * (self.precip.data / 28.7) ** 0.22
increment[self.precip.data < self._minimum_precip] = 0.0
return increment.astype(np.float32)
[docs] def _make_updraught_cube(self, data: np.ndarray) -> Cube:
"""Puts the data array into a CF-compliant cube"""
attributes = {}
if self.model_id_attr:
attributes[self.model_id_attr] = self.precip.attributes[self.model_id_attr]
cube = create_new_diagnostic_cube(
"maximum_vertical_updraught",
"m s-1",
self.precip,
mandatory_attributes=generate_mandatory_attributes(
[self.precip, self.cape]
),
optional_attributes=attributes,
data=data,
)
return cube
[docs] def process(self, inputs: List[Cube]) -> Cube:
"""Executes methods to calculate updraught from CAPE and precipitation rate
and packages this as a Cube with appropriate metadata.
Args:
inputs:
List of CAPE and precipitation rate cubes (any order)
Returns:
Cube:
Containing maximum vertical updraught
"""
self._parse_inputs(inputs)
return self._make_updraught_cube(
self._updraught_from_cape() + self._updraught_increment_from_precip()
)