from dqc_wrapper.utils.utils_grib import *
from dqc_wrapper.utils.utils_bokeh import BokehPlot
from dqc_wrapper.unit_tests.conftest import dirs
from dqc_wrapper.test06_valid_ranges import ValidRangesTest
import pandas as pd


def prepare():
    vr = ValidRangesTest(dirs.log_dir, 'testname.ini', [dirs.nc_file],
                           'tas', '', '',  0, '',  dirs.test_dir, 'tas')
    vr.check_valid_ranges()
    return BokehPlot('test-valid-ranges.html', dirs.stats_csv, dirs.hist_csv,
                     'tas', '', 0, 50, {'varname': 'tas', 'unit': 'degC'})


def test_load_grib_metadata():
    metadata = load_grib_metadata()
    assert 'grib_codes' in metadata[list(metadata.keys())[0]]


def test_get_grib_version():
    test = GribFileCheck(dirs.grib_cds)
    res = test.get_grib_version()
    assert res == 2


def test_check_mandatory_keys():
    test = GribFileCheck(dirs.grib_cds)
    err_msgs = test.check_mandatory_keys({'GRIBEditionNumber': '2'})
    assert not len(err_msgs)
    err_msgs = test.check_mandatory_keys({'GRIBEditionNumber': '27'})
    assert len(err_msgs)


def test_get_grib_first_values():
    test = GribFileCheck(dirs.grib_cds)
    res = test.get_grib_first_values()
    assert len(res)


def test_get_grib_hadley_regions():
    prepare()
    test = GribFileCheck(dirs.grib_cds)
    hbins = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
    df = pd.read_csv(dirs.hist_csv)
    res = test.get_grib_hadley_regions(0, hbins, df)
    assert len(res[0]) == 15