#! /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:
    """
    Load all parameters for multiples classes in one object. 
    
    The object has one
    attribute (``.table``) that contains a ``pandas DataFrame`` with all paramters (rows)
    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
    
    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
    
    
    """

    def __init__(self, paramFile: str):
        """
        Create pandas table from the csv parameter file.

        ## Arguments
        1. paramFile: `str`
            path to csv parameter file
        """

        # Read csv file with Pandas
        csvFile = read_csv(paramFile)
        
        # Index file for correct conversion to dictionnary
        csvFile.index = csvFile.iloc[:,0]
        csvFile.drop(columns = ['ClassName'], inplace = True)
        
        # 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