Note
Go to the end to download the full example code.
Read SMC sensors observations to assimilate#
The notebook illustrate how to read SMC sensors dataset to be prepare for DA
Estimated time to run the notebook = 2min
import numpy as np
from pyCATHY.DA.cathy_DA import DA
import pandas as pd
import matplotlib.pyplot as plt
from pyCATHY.DA.cathy_DA import DA, dictObs_2pd
from pyCATHY.DA.observations import read_observations, prepare_observations, make_data_cov
from pathlib import Path
import pyvista as pv
simuWithDA = DA(
dirName='./DA_with_swc',
prj_name='import_SMC',
notebook=True,
)
🏁 Initiate CATHY object
date_range = pd.date_range(start='2023-01-01', end='2023-01-03', freq='H')
# Create smoother soil moisture data using cumulative sum of random changes
sm_data = {f'SM{i}': np.cumsum(np.random.normal(0, 1, len(date_range))) + 25 + 10*i for i in range(1, 4)}
# Create a dataframe with smoother data
sm_smooth = pd.DataFrame(sm_data, index=date_range)
# Plot the smoother soil moisture data
sm_smooth.plot(figsize=(12, 6), marker='o')
plt.xlabel('DateTime')
plt.ylabel('Soil Moisture (%)')
plt.title('Soil Moisture Content from SMC Sensors (Smoothed Data)')
plt.legend()
plt.xticks(rotation=45)
plt.grid(True)
plt.tight_layout()
plt.show()
sm_smooth['Seconds'] = (sm_smooth.index - sm_smooth.index[0]).total_seconds()
simuWithDA.create_mesh_vtk()
SMC_XY = [5,5]
SMC_depths = [0.05,0.25,0.75] # SMC depths
# find the altitudes of the nodes at the mesh position x, y = (0.05,0.25)
_ , closest = simuWithDA.find_nearest_node(SMC_XY)
nodes_SMC = []
closestPos = []
for d in SMC_depths:
SMC_XYZi = [5,5,closest[0][2]-d]
nodeId, closest = simuWithDA.find_nearest_node(SMC_XYZi)
nodes_SMC.append(nodeId)
closestPos.append(closest)
nodes_SMC = np.hstack(nodes_SMC)
SMC_XYZ = np.vstack(closestPos)
pl = pv.Plotter(notebook=True)
mesh = pv.read(
Path(simuWithDA.workdir) /
simuWithDA.project_name /
f'vtk/{simuWithDA.project_name}.vtk'
)
pl.add_mesh(mesh,
opacity=0.7
)
pl.add_points(SMC_XYZ,
color='red'
)
pl.show_grid()
pl.show()
Traceback (most recent call last):
File "/home/runner/work/pycathy_wrapper/pycathy_wrapper/examples/DA/plot_2_prepare_SMC_obs4DA (conflicted copy 2025-05-21 091532).py", line 52, in <module>
simuWithDA.create_mesh_vtk()
File "/home/runner/work/pycathy_wrapper/pycathy_wrapper/pyCATHY/cathy_tools.py", line 3807, in create_mesh_vtk
self.run_processor(IPRT1=3, verbose=verbose)
File "/home/runner/work/pycathy_wrapper/pycathy_wrapper/pyCATHY/cathy_tools.py", line 584, in run_processor
self.update_parm(**kwargs) # to uncomment
File "/home/runner/work/pycathy_wrapper/pycathy_wrapper/pyCATHY/cathy_tools.py", line 1858, in update_parm
self.update_cathyH(
File "/home/runner/work/pycathy_wrapper/pycathy_wrapper/pyCATHY/cathy_tools.py", line 776, in update_cathyH
self.update_veg_map()
File "/home/runner/work/pycathy_wrapper/pycathy_wrapper/pyCATHY/cathy_tools.py", line 3353, in update_veg_map
indice_veg, str_hd_rootmap = in_CT.read_root_map(os.path.join(
File "/home/runner/work/pycathy_wrapper/pycathy_wrapper/pyCATHY/importers/cathy_inputs.py", line 529, in read_root_map
with open(os.path.join(rootmapfile), "r") as f: # open the file for reading
FileNotFoundError: [Errno 2] No such file or directory: '/home/runner/work/pycathy_wrapper/pycathy_wrapper/examples/DA/./DA_with_swc/import_SMC/input/veg_map'
abs_data_err = 1e-1 # constant error does not vary with time
dict_obs = {} # initiate the dictionnary
for i in range(len(sm_smooth.columns)-1):
for j, assimilation_time_sec in enumerate(sm_smooth['Seconds']):
dict_obs = read_observations(
dict_obs,
obs_2_add=sm_smooth[sm_smooth.columns[i]].iloc[j],
tA=assimilation_time_sec,
mesh_nodes = nodes_SMC[i],
data_type='swc',
data_err=abs_data_err,
colname=' m³/m³ Water Content',
datetime=sm_smooth.index[j]
)
data_measure_df = dictObs_2pd(dict_obs)
data_measure_df
By default, there is no correlation between sensors Therefore, the covariance matrices are diagonal with the error values on the diagonals
_,_, stacked_data_cov = make_data_cov(
simuWithDA,
dict_obs,
list_assimilated_obs = 'swc',
)
print(np.shape(stacked_data_cov))
simuWithDA.stacked_data_cov = stacked_data_cov
import pickle
with open('obs_prepared_SMC.pkl', 'wb') as fp:
pickle.dump(dict_obs, fp)
Total running time of the script: (0 minutes 6.305 seconds)