Commit 69794411 authored by Oscar Corvi's avatar Oscar Corvi
Browse files

"investigating the weir behavior of the PM equation to wind speed"

parent ca10fddc
Pipeline #235931 passed with stage
in 35 seconds
%% Cell type:markdown id:declared-dealer tags:
%% Cell type:markdown id:dangerous-indie tags:
# **Sensitivity of the PM model to wind speed: Sympy study**
%% Cell type:markdown id:heavy-plastic tags:
%% Cell type:markdown id:superb-progressive tags:
# Part I - Methodology
## <u> Motivation </u>
### Theoretical background
When assessing the models sensitivity to changes in atmospheric forcings in notebook simpler_model.ipynb, we found out that the potential evapo-transpiration models had a counter intuitive sensitivity to wind speed:
<p align="center">
<img width="460" height="300" src="Influence_atmo_rel_dry.png">
</p>
This otebook aims at investigating the mathematical structure of the Penman-Monteith model to investigate the variation behavior of the equation to wind speed.
### Modelling experiements
Sympy is used in this notebook in order to compute the derivative of the Penman-Monteith equation to wind speed and to study its sign
%% Cell type:markdown id:sonic-harrison tags:
%% Cell type:markdown id:aquatic-uruguay tags:
# Part II - Functions set up
%% Cell type:markdown id:specialized-chest tags:
%% Cell type:markdown id:limiting-mason tags:
## Importing relevant packages
%% Cell type:code id:gothic-native tags:
%% Cell type:code id:realistic-biotechnology tags:
``` python
# data manipulation and plotting
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib._layoutgrid import plot_children
from collections import OrderedDict
from IPython.display import display
import os # to look into the other folders of the project
import importlib.util # to open the .py files written somewhere else
#sns.set_theme(style="whitegrid")
# Sympy and sympbolic mathematics
from sympy import (asin, cos, diff, Eq, exp, init_printing, log, pi, sin,
solve, sqrt, Symbol, symbols, tan, Abs)
from sympy.physics.units import convert_to
init_printing()
from sympy.printing import StrPrinter
from sympy import Piecewise
StrPrinter._print_Quantity = lambda self, expr: str(expr.abbrev) # displays short units (m instead of meter)
from sympy.printing.aesaracode import aesara_function
from sympy.physics.units import * # Import all units and dimensions from sympy
from sympy.physics.units.systems.si import dimsys_SI, SI
# for ESSM, environmental science for symbolic math, see https://github.com/environmentalscience/essm
from essm.variables._core import BaseVariable, Variable
from essm.equations import Equation
from essm.variables.units import derive_unit, SI, Quantity
from essm.variables.utils import (extract_variables, generate_metadata_table, markdown,
replace_defaults, replace_variables, subs_eq)
from essm.variables.units import (SI_BASE_DIMENSIONS, SI_EXTENDED_DIMENSIONS, SI_EXTENDED_UNITS,
derive_unit, derive_baseunit, derive_base_dimension)
# For netCDF
import netCDF4
import numpy as np
import xarray as xr
import warnings
from netCDF4 import Dataset
# For regressions
from sklearn.linear_model import LinearRegression
# Deactivate unncessary warning messages related to a bug in Numpy
warnings.simplefilter(action='ignore', category=FutureWarning)
# for calibration
from scipy import optimize
from random import random
```
%% Cell type:markdown id:structured-judge tags:
%%%% Output: stream
WARNING (aesara.link.c.cmodule): install mkl with `conda install mkl-service`: No module named 'mkl'
WARNING (aesara.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
%% Cell type:markdown id:fewer-essex tags:
## Path of the different files (pre-defined python functions, sympy equations, sympy variables)
%% Cell type:code id:initial-marina tags:parameters
%% Cell type:code id:olympic-wichita tags:parameters
``` python
path_variable = '../../theory/pyFile_storage/theory_variable.py'
path_equation = '../../theory/pyFile_storage/theory_equation.py'
path_analysis_functions = '../../theory/pyFile_storage/analysis_functions.py'
path_data = '../../../data/eddycovdata/'
dates_fPAR = '../../../data/fpar_howard_spring/dates_v5'
tex_file_whole = "latex_files/whole_year.tex"
tex_file_dry = "latex_files/dry_season.tex"
tex_file_wet = "latex_files/wet_season.tex"
timeSerie_oneSite_oneYear = 'timeSerie_oneSite_oneYear.png'
inverseModelling = "inverseModelling.png"
Influence_atmo_E_dry = "Influence_atmo_E_dry.png"
Influence_atmo_E_wet = "Influence_atmo_E_wet.png"
Influence_atmo_rel_dry = "Influence_atmo_rel_dry.png"
Influence_atmo_rel_wet = "Influence_atmo_rel_wet.png"
sensitivity_parameters = "sensitivity_parameters.png"
statistical_assessment = "statistical_assessment.png"
different_sites = "different_sites.png"
```
%% Cell type:markdown id:cooperative-trinidad tags:
%% Cell type:markdown id:manufactured-knight tags:
## Importing the sympy variables and equations defined in the theory.ipynb notebook
%% Cell type:code id:accepted-heart tags:
%% Cell type:code id:waiting-pension tags:
``` python
for code in [path_variable,path_equation]:
name_code = code[-20:-3]
spec = importlib.util.spec_from_file_location(name_code, code)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
names = getattr(mod, '__all__', [n for n in dir(mod) if not n.startswith('_')])
glob = globals()
for name in names:
print(name)
glob[name] = getattr(mod, name)
```
%%%% Output: stream
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:theta_sat" will be overridden by "e/theory_variable:<class 'e/theory_variable.theta_sat'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:theta_res" will be overridden by "e/theory_variable:<class 'e/theory_variable.theta_res'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:alpha" will be overridden by "e/theory_variable:<class 'e/theory_variable.alpha'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:n" will be overridden by "e/theory_variable:<class 'e/theory_variable.n'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:m" will be overridden by "e/theory_variable:<class 'e/theory_variable.m'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:S_mvg" will be overridden by "e/theory_variable:<class 'e/theory_variable.S_mvg'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:theta" will be overridden by "e/theory_variable:<class 'e/theory_variable.theta'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:h" will be overridden by "e/theory_variable:<class 'e/theory_variable.h'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:S" will be overridden by "e/theory_variable:<class 'e/theory_variable.S'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:theta_4" will be overridden by "e/theory_variable:<class 'e/theory_variable.theta_4'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:theta_3" will be overridden by "e/theory_variable:<class 'e/theory_variable.theta_3'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:theta_2" will be overridden by "e/theory_variable:<class 'e/theory_variable.theta_2'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:theta_1" will be overridden by "e/theory_variable:<class 'e/theory_variable.theta_1'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:L" will be overridden by "e/theory_variable:<class 'e/theory_variable.L'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:Mw" will be overridden by "e/theory_variable:<class 'e/theory_variable.Mw'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:Pv" will be overridden by "e/theory_variable:<class 'e/theory_variable.Pv'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:Pvs" will be overridden by "e/theory_variable:<class 'e/theory_variable.Pvs'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:R" will be overridden by "e/theory_variable:<class 'e/theory_variable.R'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:T" will be overridden by "e/theory_variable:<class 'e/theory_variable.T'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:c1" will be overridden by "e/theory_variable:<class 'e/theory_variable.c1'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:T0" will be overridden by "e/theory_variable:<class 'e/theory_variable.T0'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:Delta" will be overridden by "e/theory_variable:<class 'e/theory_variable.Delta'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:E" will be overridden by "e/theory_variable:<class 'e/theory_variable.E'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:G" will be overridden by "e/theory_variable:<class 'e/theory_variable.G'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:H" will be overridden by "e/theory_variable:<class 'e/theory_variable.H'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:Rn" will be overridden by "e/theory_variable:<class 'e/theory_variable.Rn'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:LE" will be overridden by "e/theory_variable:<class 'e/theory_variable.LE'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:gamma" will be overridden by "e/theory_variable:<class 'e/theory_variable.gamma'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:alpha_PT" will be overridden by "e/theory_variable:<class 'e/theory_variable.alpha_PT'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:c_p" will be overridden by "e/theory_variable:<class 'e/theory_variable.c_p'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:w" will be overridden by "e/theory_variable:<class 'e/theory_variable.w'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:kappa" will be overridden by "e/theory_variable:<class 'e/theory_variable.kappa'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:z" will be overridden by "e/theory_variable:<class 'e/theory_variable.z'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:u_star" will be overridden by "e/theory_variable:<class 'e/theory_variable.u_star'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:VH" will be overridden by "e/theory_variable:<class 'e/theory_variable.VH'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:d" will be overridden by "e/theory_variable:<class 'e/theory_variable.d'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:z_om" will be overridden by "e/theory_variable:<class 'e/theory_variable.z_om'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:z_oh" will be overridden by "e/theory_variable:<class 'e/theory_variable.z_oh'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:r_a" will be overridden by "e/theory_variable:<class 'e/theory_variable.r_a'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:g_a" will be overridden by "e/theory_variable:<class 'e/theory_variable.g_a'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:r_s" will be overridden by "e/theory_variable:<class 'e/theory_variable.r_s'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:g_s" will be overridden by "e/theory_variable:<class 'e/theory_variable.g_s'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:c1_e" will be overridden by "e/theory_variable:<class 'e/theory_variable.c1_e'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:c2_e" will be overridden by "e/theory_variable:<class 'e/theory_variable.c2_e'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:e" will be overridden by "e/theory_variable:<class 'e/theory_variable.e'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:T_min" will be overridden by "e/theory_variable:<class 'e/theory_variable.T_min'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:T_max" will be overridden by "e/theory_variable:<class 'e/theory_variable.T_max'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:RH_max" will be overridden by "e/theory_variable:<class 'e/theory_variable.RH_max'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:RH_min" will be overridden by "e/theory_variable:<class 'e/theory_variable.RH_min'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:e_a" will be overridden by "e/theory_variable:<class 'e/theory_variable.e_a'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:e_s" will be overridden by "e/theory_variable:<class 'e/theory_variable.e_s'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:iv_T" will be overridden by "e/theory_variable:<class 'e/theory_variable.iv_T'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:T_kv" will be overridden by "e/theory_variable:<class 'e/theory_variable.T_kv'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:P" will be overridden by "e/theory_variable:<class 'e/theory_variable.P'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:rho_a" will be overridden by "e/theory_variable:<class 'e/theory_variable.rho_a'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/variables/_core.py:89: UserWarning: "e/theory_variable:VPD" will be overridden by "e/theory_variable:<class 'e/theory_variable.VPD'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_m_n" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_m_n'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_MVG_neg_case" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_MVG_neg_case'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_MVG" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_MVG'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_sat_degree" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_sat_degree'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_MVG_h" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_MVG_h'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_h_FC" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_h_FC'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_theta_4_3" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_theta_4_3'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_theta_2_1" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_theta_2_1'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_water_stress_simple" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_water_stress_simple'>"
instance[expr] = instance
%%%% Output: stream
theta_sat
theta_res
alpha
n
m
S_mvg
theta
h
S
theta_4
theta_3
theta_2
theta_1
L
Mw
Pv
Pvs
R
T
c1
T0
Delta
E
G
H
Rn
LE
gamma
alpha_PT
c_p
w
kappa
z
u_star
VH
d
z_om
z_oh
r_a
g_a
r_s
g_s
c1_e
c2_e
e
T_min
T_max
RH_max
RH_min
e_a
e_s
iv_T
T_kv
P
rho_a
VPD
%%%% Output: stream
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_Pvs_T" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_Pvs_T'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_Delta" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_Delta'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_PT" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_PT'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_PM" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_PM'>"
instance[expr] = instance
%%%% Output: stream
eq_m_n
eq_MVG_neg_case
eq_MVG
eq_sat_degree
eq_MVG_h
eq_h_FC
eq_theta_4_3
eq_theta_2_1
eq_water_stress_simple
eq_Pvs_T
eq_Delta
eq_PT
eq_PM
eq_PM_VPD
eq_PM_g
eq_PM_inv
%%%% Output: stream
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_PM_VPD" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_PM_VPD'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_PM_g" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_PM_g'>"
instance[expr] = instance
/opt/conda/lib/python3.8/site-packages/essm/equations/_core.py:107: UserWarning: "e/theory_equation:eq_PM_inv" will be overridden by "e/theory_equation:<class 'e/theory_equation.eq_PM_inv'>"
instance[expr] = instance
%% Cell type:markdown id:fixed-lodging tags:
%% Cell type:markdown id:particular-shakespeare tags:
## Importing the performance assessment functions defined in the analysis_function.py file
%% Cell type:code id:distinct-lighting tags:
%% Cell type:code id:noble-guatemala tags:
``` python
for code in [path_analysis_functions]:
name_code = code[-20:-3]
spec = importlib.util.spec_from_file_location(name_code, code)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
names = getattr(mod, '__all__', [n for n in dir(mod) if not n.startswith('_')])
glob = globals()
for name in names:
print(name)
glob[name] = getattr(mod, name)
```
%%%% Output: stream
AIC
AME
BIC
CD
CP
IoA
KGE
MAE
MARE
ME
MRE
MSRE
MdAPE
NR4MS4E
NRMSE
NS
NSC
PDIFF
PEP
R4MS4E
RAE
RMSE
RMedSE
RVE
bias
np
nt
%% Cell type:markdown id:buried-malaysia tags:
%% Cell type:markdown id:helpful-thinking tags:
## Data import, preprocess and shape for the computations
%% Cell type:markdown id:deadly-drove tags:
%% Cell type:markdown id:express-acoustic tags:
### Get the different files where data are stored
Eddy-covariance data from the OzFlux network are stored in **.nc** files (NetCDF4 files) which is roughly a panda data frame with meta-data (see https://www.unidata.ucar.edu/software/netcdf/ for more details about NetCDF4 file format). fPAR data are stored in **.txt** files
%% Cell type:code id:imperial-victim tags:
%% Cell type:code id:monetary-length tags:
``` python
fPAR_files = []
eddy_files = []
for file in os.listdir(path_data):
if file.endswith(".txt"):
fPAR_files.append(os.path.join(path_data, file))
elif file.endswith(".nc"):
eddy_files.append(os.path.join(path_data, file))
fPAR_files.sort()
print(fPAR_files)
eddy_files.sort()
print(eddy_files)
```
%%%% Output: stream
['../../../data/eddycovdata/fpar_adelaide_v5.txt', '../../../data/eddycovdata/fpar_daly_v5.txt', '../../../data/eddycovdata/fpar_dry_v5.txt', '../../../data/eddycovdata/fpar_howard_v5.txt', '../../../data/eddycovdata/fpar_sturt_v5.txt']
['../../../data/eddycovdata/AdelaideRiver_L4.nc', '../../../data/eddycovdata/DalyUncleared_L4.nc', '../../../data/eddycovdata/DryRiver_L4.nc', '../../../data/eddycovdata/HowardSprings_L4.nc', '../../../data/eddycovdata/SturtPlains_L4.nc']
%% Cell type:markdown id:lyric-bookmark tags:
%% Cell type:markdown id:wicked-possession tags:
### Define and test a function that process the fPAR data
In the **.txt** files, only one value per month is given for the fPAR. The following function takes one .txt file containing data about the fPAR coefficients, and the related dates, stored in the a seperate file. The fPAR data (date and coefficients) are cleaned (good string formatting), mapped together and averaged to output one value per month (the fPAR measurement period doesn't spans the measurement period of the eddy covariance data)
%% Cell type:code id:nominated-oxide tags:
%% Cell type:code id:universal-pixel tags:
``` python
def fPAR_data_process(fPAR_file,dates_fPAR):
fparv5_dates = np.genfromtxt(dates_fPAR, dtype='str', delimiter=',')
fparv5_dates = pd.to_datetime(fparv5_dates[:,1], format="%Y%m")
dates_pd = pd.date_range(fparv5_dates[0], fparv5_dates[-1], freq='MS')
fparv5_howard = np.loadtxt(fPAR_file,delimiter=',', usecols=3 )
fparv5_howard[fparv5_howard == -999] = np.nan
fparv5_howard_pd = pd.Series(fparv5_howard, index = fparv5_dates)
fparv5_howard_pd = fparv5_howard_pd.resample('MS').max()
# convert fparv5_howard_pd to dataframe
fPAR_pd = pd.DataFrame(fparv5_howard_pd)
fPAR_pd = fPAR_pd.rename(columns={0:"fPAR"})
fPAR_pd.index = fPAR_pd.index.rename("time")
# convert fPAR_pd to xarray to aggregate the data
fPAR_xr = fPAR_pd.to_xarray()
fPAR_agg = fPAR_xr.fPAR.groupby('time.month').max()
# convert back to dataframe
fPAR_pd = fPAR_agg.to_dataframe()
Month = np.arange(1,13)
Month_df = pd.DataFrame(Month)
Month_df.index = fPAR_pd.index
Month_df = Month_df.rename(columns={0:"Month"})
fPAR_mon = pd.concat([fPAR_pd,Month_df], axis = 1)
return(fPAR_mon)
```
%% Cell type:code id:orange-spectacular tags:
%% Cell type:code id:structural-mediterranean tags:
``` python
fPAR_data_process(fPAR_files[3],dates_fPAR)
```
%%%% Output: execute_result
fPAR Month
month
1 0.78 1
2 0.84 2
3 0.79 3
4 0.84 4
5 0.71 5
6 0.75 6
7 0.60 7
8 0.54 8
9 0.52 9
10 0.67 10
11 0.73 11
12 0.78 12
%% Cell type:markdown id:behind-brother tags:
%% Cell type:markdown id:antique-queen tags:
### fPARSet function
Map the fPAR time serie to the given eddy-covariance data. Takes two dataframes as input (one containing the fPAR data, the other containing the eddy-covariance data) and returns a data frame where the fPAR monthly values have been scaled to the time scale of the eddy covariance data
%% Cell type:code id:widespread-subsection tags:
%% Cell type:code id:increasing-franchise tags:
``` python
def fPARSet(df_add, fPAR_pd):
# construct the time serie of the fPAR coefficients
dummy_len = df_add["Fe"].size
fPAR_val = np.zeros((dummy_len,))
dummy_pd = df_add
dummy_pd.reset_index(inplace=True)
dummy_pd.index=dummy_pd.time
month_pd = dummy_pd['time'].dt.month
for i in range(dummy_len):
current_month = month_pd.iloc[i]
line_fPAR = fPAR_pd[fPAR_pd['Month'] == current_month]
fPAR_val[i] = line_fPAR['fPAR']
# transform fPAR_val into dataframe to concatenate to df:
fPAR = pd.DataFrame(fPAR_val, index = df_add.index)
df_add = pd.concat([df_add,fPAR], axis = 1)
df_add = df_add.rename(columns = {0:"fPAR"})
return(df_add)
```
%% Cell type:markdown id:southern-excuse tags:
%% Cell type:markdown id:swedish-certification tags:
### DataChose function
Function taking the raw netcdf4 data file from the eddy covariance measurement and shape it such that it can be used for the computations. Only relevant variables are kept (latent heat flux, net radiation, ground heat flux, soil water content, wind speed, air temperature, VPD, bed shear stress). The desired data period is selected and is reshaped at the desired time scale (daily by default). Uses the fPARSet function defined above
List of variable abbreviation :
* `Rn` : Net radiation flux
* `G` : Ground heat flux
* `Sws` : soil moisture
* `Ta` : Air temperature
* `RH` : Relative humidity
* `W` : Wind speed
* `E` : measured evaporation
* `VPD` : Vapour pressure deficit
%% Cell type:code id:agreed-holocaust tags:
%% Cell type:code id:middle-antique tags:
``` python
def DataChose(ds_ref, period_sel, fPAR_given, Freq = "D", sel_period_flag = True):
"""Take subset of dataset if Flag == True, entire dataset else