"""Copyright (c) 2024 MPI-M, Clara Bayley----- Microphysics Test Cases -----File: calcs.pyProject: thermoCreated Date: Friday 1st March 2024Author: Clara Bayley (CB)Additional Contributors:-----Last Modified: Monday 2nd September 2024Modified By: CB-----License: BSD 3-Clause "New" or "Revised" Licensehttps://opensource.org/licenses/BSD-3-Clause-----File Description:functions for calculations of some quantities e.g. potential temperature(s)"""importnumpyasnpfromtypingimportOptional
[docs]defdry_potential_temperature(temp:np.ndarray,press:np.ndarray):r"""Calculate the potential temperature for dry air. This function calculates the potential temperature for dry air given the temperature and pressure. .. math:: \theta_{\rm{dry}} = T \left( \frac{P_{\rm ref}}{P} \right) ^{ \frac{R_{\rm{dry}}}{c_{\rm{p, dry}}} } where :math:`P_{\rm ref}` is the first pressure in press array. Args: temp (array-like): Temperature values (K). press (array-like): Pressure values (Pa). Returns: array-like: The dry potential temperature (K). """frommetpy.unitsimportunitsfrommetpyimportcalctheta_dry=calc.potential_temperature(press*units.Pa,temp*units.kelvin)returntheta_dry.magnitude# Kelvin
[docs]defmoist_equiv_potential_temperature(temp:np.ndarray,press:np.ndarray,qvap:np.ndarray):""" Calculate the moist potential temperature. .. math:: \theta_e = \theta \cdot \exp\left(\frac{L_v \cdot q}{c_p \cdot T}\right) Args: temp (array-like): Temperature values (K). press (array-like): Pressure values (Pa). qvap (array-like): Mixing ratio of water vapour (kg/kg) Returns: array-like: The moist potential temperature (K). """frommetpy.unitsimportunitsfrommetpyimportcalcrelh=calc.relative_humidity_from_mixing_ratio(press*units.Pa,temp*units.kelvin,qvap)dewpoint=calc.dewpoint_from_relative_humidity(temp*units.kelvin,relh)theta_equiv=calc.equivalent_potential_temperature(press*units.Pa,temp*units.kelvin,dewpoint)returntheta_equiv.magnitude# Kelvin
[docs]defmoist_static_energy(temp:np.ndarray,qvap:np.ndarray,height:Optional[np.ndarray]=None):""" Calculate the moist static energy [kilojoule / kilogram] .. math:: \theta_e = \theta \cdot \exp\left(\frac{L_v \cdot q}{c_p \cdot T}\right) Args: temp (array-like): Temperature values (K). press (array-like): Pressure values (Pa). qvap (array-like): Mixing ratio of water vapour (kg/kg) Returns: array-like: The moist potential temperature (K). """frommetpy.unitsimportunitsfrommetpyimportcalcifheightisNone:height=np.zeros(temp.shape)specific_humidity=calc.specific_humidity_from_mixing_ratio(qvap)mse=calc.moist_static_energy(height*units.meters,temp*units.kelvin,specific_humidity)returnmse.magnitude# [kilojoule / kilogram]
[docs]defsupersaturation(temp:np.ndarray,press:np.ndarray,qvap:np.ndarray):""" Calculate supersaturation based on the method described in PyMPDATA-examples This function uses the calculations in the Shipway and Hill (2012) example from PyMPDATA-examples library to compute the supersaturation. Parameters: temp (float): Temperature in Kelvin. press (float): Pressure in Pascals. qvap (float): Specific humidity (kg/kg). Returns: float: Supersaturation value. """fromPyMPDATA_examplesimportShipway_and_Hill_2012askidpvs=kid.formulae.pvs_Celsius(temp-kid.const.T0)relh=kid.formulae.pv(press,qvap)/pvsreturnrelh-1