Newer
Older
Jeremy Auclair
committed
#! /usr/bin/env python
#-*- coding: utf-8 -*-
"""
11-07-2023 adapted from modspa-parcel code
@author: jeremy auclair
Classes to load and store SAMIR parameters.
"""
from pandas import read_csv # to read csv parameter files
class samir_parameters:
"""
Jeremy Auclair
committed
Load all parameters for multiples classes in one object.
The object has one
attribute (``.table``) that contains a ``pandas DataFrame`` with all paramters (rows)
Jeremy Auclair
committed
for all classes (columns).
It also contains :
- a ``scale_factor`` column (first column) that allows to convert all parameters to integer values for reduced memory usage
- a ``Default`` column (second column) that contains default values to fill in missing values
Jeremy Auclair
committed
Example of the parameter table:
.. code-block:: python
scale_factor Default class1 class2 class3
ClassName
ClassNumber 1 0.00 1.00 2.000 3.000
FminNDVI 1000 0.20 0.20 0.100 0.100
FmaxNDVI 1000 0.90 0.90 0.900 0.900
FminFC 1000 0.90 0.90 0.900 0.900
FmaxFC 1000 1.00 0.90 1.000 1.000
Fslope 1000 1.40 1.50 1.500 1.500
Foffset 1000 -0.10 -0.10 -0.100 -0.100
Plateau 1 70.00 70.00 70.000 70.000
KminNDVI 1000 0.10 0.10 0.100 0.100
KmaxNDVI 1000 0.90 0.90 0.900 0.900
KminKcb 1000 0.00 0.20 0.000 0.000
KmaxKcb 1000 0.98 1.00 1.100 1.100
Kslope 1000 1.60 1.60 1.600 1.600
Koffset 1000 -0.10 -0.10 -0.100 -0.100
Zsoil 1 2000.00 1600.00 1550.000 1550.000
... ... ... ... ... ...
Kcmax 1000 1.15 1.15 1.15 1.15
Fc_stop 1000 0.15 0.15 0.15 0.15
Start_date_Irr 1 0.00 0.00 0.00 0.00
p_trigger 1 0.00 0.00 0.00 0.00
Jeremy Auclair
committed
Jeremy Auclair
committed
"""
def __init__(self, paramFile: str):
"""
Create pandas table from the csv parameter file.
## Arguments
1. paramFile: `str`
path to csv parameter file
"""
Jeremy Auclair
committed
# Read csv file with Pandas
Jeremy Auclair
committed
csvFile = read_csv(paramFile)
Jeremy Auclair
committed
# Index file for correct conversion to dictionnary
csvFile.index = csvFile.iloc[:,0]
Jeremy Auclair
committed
csvFile.drop(columns = ['ClassName'], inplace = True)
Jeremy Auclair
committed
Jeremy Auclair
committed
# Replace missing parameters by their default values
csvFile[csvFile.columns[1:]] = csvFile[csvFile.columns[1:]].astype('float32')
for col in csvFile.columns[2:]:
mask = csvFile[col].isna()
csvFile.loc[mask, col] = csvFile.loc[mask, 'Default']
# Store dataframe in attribute table
self.table = csvFile