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.
Attributes
----------
Jeremy Auclair
committed
1. .table: ``pd.DataFrame``
``pandas DataFrame`` with all paramters (rows) for all classes (columns)
Jeremy Auclair
committed
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
Example of the parameter table:
Jeremy Auclair
committed
.. code-block:: python
Jeremy Auclair
committed
ClassName scale_factor Default no_sim straw_cereal oilseed soy sunflower corn
ClassNumber 1 0.000 1.000 2.000 3.000 4.000 5.000 6.000
NDVIsol 1 0.150 0.150 0.150 0.150 0.150 0.150 0.150
NDVImax 1 0.850 0.850 0.850 0.850 0.850 0.850 0.850
FCmax 1000 1.000 0.000 1.000 1.000 1.000 1.000 1.000
Fslope 1000 1.400 0.000 1.180 1.220 1.400 1.400 1.400
Foffset 1000 -0.075 0.000 -0.165 -0.170 -0.075 -0.075 -0.075
Kcmax 1000 1.150 0.000 1.150 0.700 1.150 1.150 1.150
Kslope 1000 1.200 0.000 1.470 1.350 1.200 1.200 1.200
Koffset 1000 -0.240 0.000 -0.170 -0.160 -0.240 -0.240 -0.240
Zsoil 1 2000.000 2000.000 2000.000 2000.000 2000.000 2000.000 2000.000
Ze 1 300.000 300.000 125.000 125.000 300.000 300.000 300.000
Init_RU 1000 0.870 0.870 0.500 0.500 0.870 0.870 0.870
DiffE 1 1.000 0.000 0.000 0.000 1.000 1.000 1.000
DiffR 1 5.000 0.000 0.000 0.000 5.000 5.000 5.000
REW 1 0.000 0.000 9.000 9.000 0.000 0.000 0.000
minZr 1 150.000 0.000 125.000 125.000 150.000 150.000 150.000
maxZr 1 600.000 0.000 1250.000 1450.000 600.000 600.000 600.000
p 1000 0.550 0.000 0.550 0.650 0.550 0.550 0.550
FW 1000 1.000 0.000 1.000 1.000 1.000 1.000 1.000
Irrig_auto 1 1.000 0.000 1.000 1.000 1.000 1.000 1.000
Irrig_man 1 0.000 0.000 0.000 0.000 0.000 0.000 0.000
Kcb_stop_irrig 1 0.500 0.000 0.500 0.500 0.500 0.500 0.500
Jeremy Auclair
committed
Jeremy Auclair
committed
"""
def __init__(self, paramFile: str) -> None:
"""
Create pandas table from the csv parameter file.
Arguments
=========
1. paramFile: ``str``
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