diff --git a/.gitignore b/.gitignore index 906cba7446747e853abecc88d675085144b3bb1d..3b9cbf5865ffe95846d7396358ed7fded7ac02d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.ascii *.csv *.swp *.nc diff --git a/aosstower/tests/level_b1/__init__.py b/aosstower/tests/level_b1/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/aosstower/tests/level_b1/test_nc.py b/aosstower/tests/level_b1/test_nc.py new file mode 100644 index 0000000000000000000000000000000000000000..07149e93aa8fe737db0ed13e4b58f3a5d7178617 --- /dev/null +++ b/aosstower/tests/level_b1/test_nc.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# encoding: utf8 +"""Test basic NetCDF generation.""" + +import os +from datetime import datetime +import numpy as np + + +def get_nc_schema_database(fields=None): + """Get a version of the NetCDF schema that mimics the nc script.""" + from aosstower import schema + if fields is None: + fields = schema.met_vars + mini_database = {k: schema.database_dict[k] for k in fields} + return mini_database + + +def test_nc_basic1(tmpdir): + """Test basic usage of the NetCDF generation.""" + from aosstower.level_b1.nc import create_giant_netcdf + from aosstower.tests.utils import get_cached_level_00 + from netCDF4 import Dataset + + input_files = list(get_cached_level_00(num_files=2)) + nc_out = tmpdir.join('test.nc') + create_giant_netcdf( + input_files, str(nc_out), True, None, + start=datetime(2020, 1, 2, 0, 0, 0), + interval_width='1T', + database=get_nc_schema_database(), + ) + assert os.path.isfile(nc_out) + with Dataset(nc_out, 'r') as nc: + sflux = nc['solar_flux'][:] + assert np.count_nonzero(sflux.mask) == 2 diff --git a/aosstower/tests/test_data/.gitkeep b/aosstower/tests/test_data/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/aosstower/tests/utils.py b/aosstower/tests/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..d6fedde4af41d88b10b85599a78f9b17bebab856 --- /dev/null +++ b/aosstower/tests/utils.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python +# encoding: utf8 +"""Utilities for running tests.""" + +import os +import urllib.request + +CACHE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_data') + + +def get_cached_level_00(num_files=2): + """Get a couple real ASCII files to test against.""" + file_urls = ( + "https://metobs-test.ssec.wisc.edu/pub/cache/aoss/tower/level_00/version_00/2020/01/01/aoss_tower.2020-01-01.ascii", + "https://metobs-test.ssec.wisc.edu/pub/cache/aoss/tower/level_00/version_00/2020/01/02/aoss_tower.2020-01-02.ascii", + ) + if num_files > len(file_urls): + raise ValueError(f"Maximum of {len(file_urls)} can be loaded.") + file_urls = file_urls[:num_files] + + for u in file_urls: + fn = os.path.join(CACHE_DIR, os.path.basename(u)) + if not os.path.isfile(fn): + urllib.request.urlretrieve(u, fn) + yield fn