Package constants
Run this notebook
Read the guide on setting up your computer to run Jupyter notebooks
Download
this notebookas a Jupyter notebook.
The models presented in this package rely on a relatively large number of underlying
constants. In order to keep the argument lists of functions and classes as simple as
possible, the pyrealm package discriminates between two kinds of variables.
Arguments: Class and function arguments cover the variables that a user is likely to want to vary within a particular study, such as temperature or primary productivity.
Model Constants: These are the underlying values that are likely to be constant for a particular study. These may be true constants (such as the universal gas constant \(R\)) but can also be experimental estimates of coefficients for functions describing plant physiology or geometry, which a user might want to alter to update with a new estimate or explore sensitivity to variation.
For this reason, the pyrealm package provides data classes that contain sets
of default model constants. The core API and details for each class can be seen
in the constants API documentation..
Creating constant class instances
These can be used to generate the default set of model parameters:
from pyrealm.constants import CoreConst, TModelTraits
core_const = CoreConst()
tmodel_const = TModelTraits()
print(core_const)
print(tmodel_const)
CoreConst(k_R=8.3145, k_co=209476.0, k_c_molmass=12.0107, k_water_molmass=18.01258, k_Po=101325.0, k_To=298.15, k_L=0.0065, k_G=9.80665, k_Ma=0.028963, k_Mv=0.01802, k_CtoK=273.15, visible_light_albedo=0.03, swdown_to_ppfd_factor=2.04, transmissivity_coef=(0.25, 0.5, 2.67e-05), net_longwave_radiation_coef=(0.2, 107.0), shortwave_albedo=0.17, solar_constant=1360.8, day_seconds=86400, equation_of_time_coef=(7.5e-05, 0.001868, -0.032077, -0.014615, -0.04089, 229.18), solar_eccentricity=0.0167, solar_obliquity=23.44, solar_perihelion=283.0, magnus_coef=array([611.2 , 17.62, 243.12]), mwr=0.622, magnus_option=None, water_density_method='jones_harris_eq6', fisher_dial_lambda=array([ 1.788316e+03, 2.155053e+01, -4.695911e-01, 3.096363e-03,
-7.341182e-06]), fisher_dial_Po=array([ 5.9184990e+03, 5.8052670e+01, -1.1253317e+00, 6.6123869e-03,
-1.4661625e-05]), fisher_dial_Vinf=array([ 6.980547e-01, -7.435626e-04, 3.704258e-05, -6.315724e-07,
9.829576e-09, -1.197269e-10, 1.005461e-12, -5.437898e-15,
1.699460e-17, -2.295063e-20]), chen_po=array([ 9.9983952e-01, 6.7882600e-05, -9.0865900e-06, 1.0221300e-07,
-1.3543900e-09, 1.4711500e-11, -1.1166300e-13, 5.0440700e-16,
-1.0065900e-18]), chen_ko=array([ 1.965217e+04, 1.481830e+02, -2.299950e+00, 1.281000e-02,
-4.915640e-05, 1.035530e-07]), chen_ca=array([ 3.26138e+00, 5.22300e-04, 1.32400e-04, -7.65500e-07,
8.58400e-10]), chen_cb=array([ 7.2061e-05, -5.8948e-06, 8.6990e-08, -1.0100e-09, 4.3220e-12]), density_kell=((999.83952, 16.945176, -0.0079870401, -4.6170461e-05, 1.0556302e-07, -2.8054253e-10), 0.01689785), density_jones_harris_rho=(999.84847, 0.06337563, -0.008523829, 6.943248e-05, -3.821216e-07), density_jones_harris_kappa=(5.083101e-07, -3.68293e-09, 7.263725e-11, -6.597702e-13, 2.87767e-15), water_viscosity_method='vogel', huber_tk_ast=647.096, huber_rho_ast=322.0, huber_mu_ast=1e-06, huber_H_i=array([ 1.67752 , 2.20462 , 0.6366564, -0.241605 ]), huber_H_ij=array([[ 5.20094e-01, 8.50895e-02, -1.08374e+00, -2.89555e-01,
0.00000e+00, 0.00000e+00],
[ 2.22531e-01, 9.99115e-01, 1.88797e+00, 1.26613e+00,
0.00000e+00, 1.20573e-01],
[-2.81378e-01, -9.06851e-01, -7.72479e-01, -4.89837e-01,
-2.57040e-01, 0.00000e+00],
[ 1.61913e-01, 2.57399e-01, 0.00000e+00, 0.00000e+00,
0.00000e+00, 0.00000e+00],
[-3.25372e-02, 0.00000e+00, 0.00000e+00, 6.98452e-02,
0.00000e+00, 0.00000e+00],
[ 0.00000e+00, 0.00000e+00, 0.00000e+00, 0.00000e+00,
8.72102e-03, 0.00000e+00],
[ 0.00000e+00, 0.00000e+00, 0.00000e+00, -4.35673e-03,
0.00000e+00, -5.93264e-04]]))
TModelTraits(a_hd=116.0, ca_ratio=390.43, h_max=25.33, rho_s=200.0, lai=1.8, sla=14.0, tau_f=4.0, tau_r=1.04, par_ext=0.5, yld=0.6, zeta=0.17, resp_r=0.913, resp_s=0.044, resp_f=0.1)
And individual values can be altered using the parameter arguments:
# Estimate processes under the moon's gravity...
core_const_moon = CoreConst(k_G=1.62)
# ... allowing a much greater maximum height
tmodel_const_moon = TModelTraits(h_max=200)
print(core_const_moon.k_G)
print(tmodel_const_moon.h_max)
1.62
200
In order to ensure that a set of parameters cannot change while models are being run, instances of these parameter classes are frozen. You cannot edit an existing instance and will need to create a new instance to use different parameters.
core_const_moon.k_G = 9.80665
---------------------------------------------------------------------------
FrozenInstanceError Traceback (most recent call last)
Cell In[3], line 1
----> 1 core_const_moon.k_G = 9.80665
File <string>:4, in __setattr__(self, name, value)
FrozenInstanceError: cannot assign to field 'k_G'
Exporting and reloading parameter sets
All parameter classes inherit methods from the base
ConstantsClass class that provides bulk import and
export of parameter settings to dictionaries and to JSON formatted files. The code below
shows these methods working. First, a trait definition in a JSON file is read into a
dictionary:
import json
import pprint
trt_dict = json.load(open("../files/traits.json", "r"))
pprint.pprint(trt_dict)
{'a_hd': 116.0,
'ca_ratio': 390.43,
'h_max': 5.0,
'lai': 1.8,
'par_ext': 0.5,
'resp_f': 0.1,
'resp_r': 0.913,
'resp_s': 0.044,
'rho_s': 200.0,
'sla': 14.0,
'tau_f': 4.0,
'tau_r': 1.04,
'yld': 0.17,
'zeta': 0.17}
That dictionary can then be used to create a TModelTraits instance using
the from_dict() method. The
from_json() method allows this to
be done more directly and the resulting instances are identical.
traits1 = TModelTraits.from_dict(trt_dict)
traits2 = TModelTraits.from_json("../files/traits.json")
print(traits1)
print(traits2)
traits1 == traits2
TModelTraits(a_hd=116.0, ca_ratio=390.43, h_max=5.0, rho_s=200.0, lai=1.8, sla=14.0, tau_f=4.0, tau_r=1.04, par_ext=0.5, yld=0.17, zeta=0.17, resp_r=0.913, resp_s=0.044, resp_f=0.1)
TModelTraits(a_hd=116.0, ca_ratio=390.43, h_max=5.0, rho_s=200.0, lai=1.8, sla=14.0, tau_f=4.0, tau_r=1.04, par_ext=0.5, yld=0.17, zeta=0.17, resp_r=0.913, resp_s=0.044, resp_f=0.1)
True