Exemplo n.º 1
0
def test_calc_help_cells(helpm, output_file):
    """Test that the HelpManager is able to run water budget calculation."""
    cellnames = helpm.cellnames[:100]

    helpm.calc_help_cells(output_file, cellnames, tfsoil=-3)
    assert osp.exists(output_file)

    inputdir = osp.join(helpm.inputdir, 'd10d11_input_files')
    assert len(os.listdir(inputdir)) == 98 * 2
    inputdir = osp.join(helpm.inputdir, 'D4_input_files')
    assert len(os.listdir(inputdir)) == 2
    inputdir = osp.join(helpm.inputdir, 'D7_input_files')
    assert len(os.listdir(inputdir)) == 2
    inputdir = osp.join(helpm.inputdir, 'D13_input_files')
    assert len(os.listdir(inputdir)) == 1

    # Assert that the results are as expected.
    output = HelpOutput(output_file)
    area_yrly_avg = output.calc_area_yearly_avg()
    expected_results = {
        'precip': 11614.46,
        'perco': 2767.51,
        'evapo': 6034.42,
        'rechg': 1432.13,
        'runoff': 2334.89,
        'subrun1': 509.02,
        'subrun2': 1243.24
    }
    for key in list(expected_results.keys()):
        assert abs(np.sum(area_yrly_avg[key]) - expected_results[key]) < 1, key
Exemplo n.º 2
0
def test_plot_area_monthly_avg(output_dir, output_file, fig_title):
    """
    Test that the water budget plots are created and saved as expected.
    """
    output = HelpOutput(output_file)

    figfilename = osp.join(output_dir, 'area_monthly_avg.pdf')
    fig = output.plot_area_monthly_avg(figfilename,
                                       year_from=2003,
                                       year_to=2009,
                                       fig_title=fig_title)

    assert fig is not None
    assert osp.exists(figfilename)

    children = fig.axes[0].get_children()
    assert (children[0].get_ydata().sum() -
            1086.8448950125246) < 0.01  # precip
    assert (children[1].get_ydata().sum() - 136.12068516893297) < 0.01  # rechg
    assert (children[2].get_ydata().sum() - 226.9635476845988) < 0.01  # runoff
    assert (children[3].get_ydata().sum() - 550.11140519815) < 0.01  # evapo
    assert (children[4].get_ydata().sum() -
            47.97935577404126) < 0.01  # subrun1
    assert (children[5].get_ydata().sum() -
            121.66539490800443) < 0.01  # subrun2

    if fig_title is None:
        assert fig._suptitle is None
    else:
        assert fig._suptitle.get_text() == fig_title
Exemplo n.º 3
0
def test_plot_area_yearly_avg(output_dir, output_file, fig_title):
    """
    Test that plotting the yearly averages is working expected.
    """
    output = HelpOutput(output_file)

    figfilename = osp.join(output_dir, 'area_yearly_avg.pdf')
    fig = output.plot_area_yearly_avg(figfilename,
                                      year_from=2003,
                                      year_to=2009,
                                      fig_title=fig_title)

    assert fig is not None
    assert osp.exists(figfilename)

    children = fig.axes[0].get_children()
    assert (children[0].get_height() - 1086.8448950125246) < 0.01  # precip
    assert children[1].get_text() == '1087\nmm/an'
    assert (children[2].get_height() - 136.12068516893297) < 0.01  # rechg
    assert children[3].get_text() == '136\nmm/an'
    assert (children[4].get_height() - 226.9635476845988) < 0.01  # runoff
    assert children[5].get_text() == '227\nmm/an'
    assert (children[6].get_height() - 550.11140519815) < 0.01  # evapo
    assert children[7].get_text() == '550\nmm/an'
    assert (children[8].get_height() - 47.97935577404126) < 0.01  # subrun1
    assert children[9].get_text() == '48\nmm/an'
    assert (children[10].get_height() - 121.66539490800443) < 0.01  # subrun2
    assert children[11].get_text() == '122\nmm/an'

    if fig_title is None:
        assert fig._suptitle is None
    else:
        assert fig._suptitle.get_text() == fig_title
Exemplo n.º 4
0
def test_calc_cells_yearly_avg(output_file):
    """
    Test that the method to calculate cells yearly average values is
    working as expected.
    """
    output = HelpOutput(output_file)

    # Test calc_cells_yearly_avg without providing any value for the
    # year_from and year_to argument.
    yearly_avg = output.calc_cells_yearly_avg()
    expected_results = {
        'precip': 1055.8597373343132,
        'perco': 251.5843470406275,
        'evapo': 548.5954285729573,
        'rechg': 130.18263914385366,
        'runoff': 212.26214164981408,
        'subrun1': 46.26946108344004,
        'subrun2': 113.02721698440918
    }
    for varname in list(expected_results.keys()):
        result = np.sum(yearly_avg[varname]) / len(yearly_avg[varname])
        assert abs(result - expected_results[varname]) < 1, varname

    # Test calc_cells_yearly_avg with non null year_from and year_to argument.
    yearly_avg = output.calc_cells_yearly_avg(year_from=2003, year_to=2009)
    expected_results = {
        'precip': 1086.8448950125246,
        'perco': 259.85654385728577,
        'evapo': 550.11140519815,
        'rechg': 136.12068516893297,
        'runoff': 226.9635476845988,
        'subrun1': 47.97935577404126,
        'subrun2': 121.66539490800443
    }
    for varname in list(expected_results.keys()):
        result = np.sum(yearly_avg[varname]) / len(yearly_avg[varname])
        assert abs(result - expected_results[varname]) < 1, varname

    # Test calc_cells_yearly_avg with year_from == year_to.
    yearly_avg = output.calc_cells_yearly_avg(year_from=2003, year_to=2003)
    expected_results = {
        'precip': 1144.4142919267927,
        'perco': 324.15252048559057,
        'evapo': 492.4243657442988,
        'rechg': 148.72946963740077,
        'runoff': 164.32582637467374,
        'subrun1': 56.33706154407843,
        'subrun2': 140.96849990912418
    }
    for varname in list(expected_results.keys()):
        result = np.sum(yearly_avg[varname]) / len(yearly_avg[varname])
        assert abs(result - expected_results[varname]) < 1, varname
Exemplo n.º 5
0
def test_validate_results(output_file):
    """Test that the water budget results are as expected. """
    output = HelpOutput(output_file)
    area_yrly_avg = output.calc_area_yearly_avg()
    expected_results = {'precip': 11614.46,
                        'perco': 2767.51,
                        'evapo': 6034.42,
                        'rechg': 1432.13,
                        'runoff': 2334.89,
                        'subrun1': 509.02,
                        'subrun2': 1243.24}
    for key in list(expected_results.keys()):
        assert abs(np.sum(area_yrly_avg[key]) - expected_results[key]) < 1, key
Exemplo n.º 6
0
def test_save_output_to_csv(output_dir, output_file):
    """
    Test that saving yearly results to csv is working as expected.
    """
    output = HelpOutput(output_file)

    # Save yearly results to csv.
    csvfilename = osp.join(output_dir, 'test_help_yearly_results.csv')
    assert not osp.exists(csvfilename)
    output.save_to_csv(csvfilename)
    assert osp.exists(csvfilename)

    # Assert that the content of the csv is as expected.
    df = pd.read_csv(csvfilename, dtype={'cid': 'str'})
    df = df.set_index('cid', drop=True)
    assert list(df.columns) == ['lat_dd', 'lon_dd'] + VARNAMES
    assert df.index.name == 'cid'
    assert len(df) == 98

    assert df.index[0] == output.data['cid'][0]
    assert df.iloc[0]['lat_dd'] == output.data['lat_dd'][0]
    assert df.iloc[0]['lon_dd'] == output.data['lon_dd'][0]

    expected_results = {
        'precip': 1055.86,
        'perco': 251.59,
        'evapo': 548.58,
        'rechg': 130.19,
        'runoff': 212.26,
        'subrun1': 46.27,
        'subrun2': 113.02
    }
    for key in list(expected_results.keys()):
        result = df[key].sum() / len(df)
        expected_result = expected_results[key]
        assert abs(result - expected_result) < 1, key
Exemplo n.º 7
0
def test_plot_area_yearly_series(output_dir, output_file, fig_title):
    """
    Test that plotting the yearly values is working expected.
    """
    output = HelpOutput(output_file)

    figfilename = osp.join(output_dir, 'area_yearly_series.pdf')
    fig = output.plot_area_yearly_series(figfilename,
                                         year_from=2003,
                                         year_to=2009,
                                         fig_title=fig_title)

    assert fig is not None
    assert osp.exists(figfilename)

    expected_xdata = [2003, 2004, 2005, 2006, 2007, 2008, 2009]

    children = fig.axes[0].get_children()
    for i in range(12):
        assert list(children[i].get_xdata()) == expected_xdata
    assert (children[0].get_ydata().mean() -
            1086.8448950125246) < 0.01  # precip
    assert (children[2].get_ydata().mean() -
            136.12068516893297) < 0.01  # rechg
    assert (children[4].get_ydata().mean() -
            226.9635476845988) < 0.01  # runoff
    assert (children[6].get_ydata().mean() - 550.11140519815) < 0.01  # evapo
    assert (children[8].get_ydata().mean() -
            47.97935577404126) < 0.01  # subrun1
    assert (children[10].get_ydata().mean() -
            121.66539490800443) < 0.01  # subrun2

    if fig_title is None:
        assert fig._suptitle is None
    else:
        assert fig._suptitle.get_text() == fig_title
Exemplo n.º 8
0
def test_plot_water_budget(output_file, example_folder):
    """
    Test that the water budget plots are created and saved as expected.

    Regression test for Issue #29.
    """
    output = HelpOutput(output_file)

    figfilename = osp.join(example_folder, 'area_monthly_avg.pdf')
    output.plot_area_monthly_avg(figfilename)
    assert osp.exists(figfilename)

    figfilename = osp.join(example_folder, 'area_yearly_avg.pdf')
    output.plot_area_yearly_avg(figfilename)
    assert osp.exists(figfilename)

    figfilename = osp.join(example_folder, 'area_yearly_series.pdf')
    output.plot_area_yearly_series(figfilename)
    assert osp.exists(figfilename)
Exemplo n.º 9
0
Created on Tue Apr 26 09:21:49 2022
@author: Jean-Sébastien Gosselin
"""

from pyhelp.output import HelpOutput
import matplotlib.pyplot as plt

plt.close('all')
dirname = "D:/Projets/pyhelp/example/"

# On charge en mémoire les résultats PyHELP d'une simulation antérieure
# dont nous avions sauvegarder les résultats sur le disque.

# À noter que cela revient au même que d'utiliser l'output de la commande
# "output_help = helpm.calc_help_cells()" directement.
output_help = HelpOutput(dirname + "help_example.out")

# On peut ensuite exporter, dans un fichier CSV, les moyennes annuelles du
# bilan hydrologique.

# On peut également spécifier (disponible dans pyhelp 0.3) les années pour
# lesquelles ont souhaite calculer les moyennes annuelles du bilan. Par
# exemple, pour calculer les moyennes entre 2003 et 2009 inclusivement, on
# écrirait :
output_help.save_to_csv(dirname + "help_example_yearly_2003-2009.csv",
                        year_from=2003,
                        year_to=2007)

# On peut également produires des graphiques des moyennes annuelles et
# ensuelles du bilan hydrologique de même que les valeurs annuelles du
# bilan hydrologique à l'aide des 3 fonctions ci-dessous. Ici encore,
Exemplo n.º 10
0
    def calc_help_cells(self,
                        path_to_hdf5=None,
                        cellnames=None,
                        tfsoil=0,
                        sf_edepth: float = 1,
                        sf_ulai: float = 1,
                        sf_cn: float = 1,
                        build_help_input_files: bool = True) -> HelpOutput:
        """
        Calcul the water budget for all eligible cells with HELP.

        Run HELP to compute the monthly water budget for the cells listed in
        "cellnames". If a file name is provided in _path_outfile_, the results
        are also saved to disk in a HDF5 file.

        Parameters
        ----------
        path_to_hdf5: str, optional
            File path where to save the results to disk in a HDF5 file.
        tfsoil: float, optional
            The average air temperature, in Celcius degrees,
            below which the soil is assumed to be freezing. The default is 0.
        sf_edepth : float, optional
            Global scale factor for the Evaporative Zone Depth (applied to
            the whole grid). The default is 1.
        sf_ulai : float, optional
            Global scale factor for the Maximum Leaf Area Index (applied to
            the whole grid). The default is 1.
        sf_cn : float, optional
            Global scale factor for the Curve Number (applied to
            the whole grid). The default is 1.
        build_help_input_files: bool
            A flag to indicate whether to generate the basic HELP input
            files before running the simulation.
        """
        if build_help_input_files:
            self.build_help_input_files(cellnames, sf_edepth, sf_ulai, sf_cn)

        # Convert from Celcius to Farenheight
        tfsoil = (tfsoil * 1.8) + 32

        tempdir = osp.join(self.inputdir, ".temp")
        if not osp.exists(tempdir):
            os.makedirs(tempdir)

        run_cellnames = self.get_run_cellnames(cellnames)
        cellparams = {}
        skipped_cells = []
        for cellname in run_cellnames:
            fpath_d4 = self.connect_tables['D4'][cellname]
            fpath_d7 = self.connect_tables['D7'][cellname]
            fpath_d13 = self.connect_tables['D13'][cellname]
            fpath_d10 = self.connect_tables['D10'][cellname]
            fpath_d11 = self.connect_tables['D11'][cellname]
            fpath_out = osp.abspath(osp.join(tempdir, str(cellname) + '.OUT'))

            if fpath_d10 is None or fpath_d11 is None:
                skipped_cells.append(cellname)
                continue

            daily_out = 0
            monthly_out = 1
            yearly_out = 0
            summary_out = 0

            unit_system = 2  # IP if 1 else SI

            year_start = self.precip_data.index.year.min()
            year_end = self.precip_data.index.year.max()
            simu_nyear = year_end - year_start + 1

            cellparams[cellname] = (fpath_d4, fpath_d7, fpath_d13, fpath_d11,
                                    fpath_d10, fpath_out, daily_out,
                                    monthly_out, yearly_out, summary_out,
                                    unit_system, simu_nyear, tfsoil)

        skipped_cells = list(set(skipped_cells))
        if skipped_cells:
            print('-' * 25)
            msg = "Warning: calcul for "
            msg += "cell " if len(skipped_cells) == 1 else "cells "
            msg += ", ".join(skipped_cells)
            msg += " will be skipped due to problems with the input data."
            print(msg)
            print('-' * 25)

        output_data = run_help_allcells(cellparams)
        output_data = self._post_process_output(output_data)
        help_output = HelpOutput(output_data)
        if path_to_hdf5:
            help_output.save_to_hdf5(path_to_hdf5)

        return help_output