Skip to content
Snippets Groups Projects
test_gridding.py 1.92 KiB
from toargridding.gridding import get_gridded_toar_data

from unittest import mock
from pathlib import Path

import xarray as xr
from compliance_checker.runner import ComplianceChecker, CheckSuite


def generate_compliance_report(dataset: xr.Dataset):
    checker_names = ["cf", "acdd"]
    verbose = 0
    criteria = "normal"
    output_filename = Path(__file__).parent / "results" / "report.html"
    output_format = "html"

    check_suite = CheckSuite()
    check_suite.load_all_available_checkers()

    ds_output_path = Path(__file__).parent / "data_sample.nc"
    dataset.to_netcdf(ds_output_path)

    return_value, errors = ComplianceChecker.run_checker(
        str(ds_output_path),
        checker_names,
        verbose,
        criteria,
        output_filename=str(output_filename),
        output_format=output_format,
    )

    ds_output_path.unlink()


def is_cf_compliant(dataset: xr.Dataset):
    generate_compliance_report(dataset)
    return True


def test_get_gridded_toar_data_cf_compliance(
    time, regular_grid, local_analysis_service
):
    datasets, metadatas = get_gridded_toar_data(
        local_analysis_service,
        regular_grid,
        time,
        variables=["mole_fraction_of_ozone_in_air"],
        stats=["mean"],
    )

    assert all(is_cf_compliant(ds) for ds in datasets)


@mock.patch("toargridding.gridding.AnalysisService", autospec=True, instance=True)
@mock.patch("toargridding.gridding.GridDefinition", autospec=True, instance=True)
def test_get_gridded_toar_data(mock_grid, mock_analysis_service, time):
    variables = [
        "mole_fraction_of_ozone_in_air",
        "mole_fraction_of_carbon_monoxide_in_air",
    ]
    stats = ["mean", "dma8epa"]

    datasets, metadatas = get_gridded_toar_data(
        mock_analysis_service, mock_grid, time, variables=variables, stats=stats
    )

    print(datasets)

    print(mock_analysis_service.call_args_list)
    print(mock_grid.call_args_list)

    assert False