From 72a6c24aec862f88caa51f40b0b81a4b233f12c3 Mon Sep 17 00:00:00 2001
From: David Hoese <david.hoese@ssec.wisc.edu>
Date: Tue, 24 Mar 2020 10:30:23 -0500
Subject: [PATCH] Add basic netcdf creation test

---
 .gitignore                           |  1 +
 aosstower/tests/level_b1/__init__.py |  0
 aosstower/tests/level_b1/test_nc.py  | 36 ++++++++++++++++++++++++++++
 aosstower/tests/test_data/.gitkeep   |  0
 aosstower/tests/utils.py             | 25 +++++++++++++++++++
 5 files changed, 62 insertions(+)
 create mode 100644 aosstower/tests/level_b1/__init__.py
 create mode 100644 aosstower/tests/level_b1/test_nc.py
 create mode 100644 aosstower/tests/test_data/.gitkeep
 create mode 100644 aosstower/tests/utils.py

diff --git a/.gitignore b/.gitignore
index 906cba7..3b9cbf5 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 0000000..e69de29
diff --git a/aosstower/tests/level_b1/test_nc.py b/aosstower/tests/level_b1/test_nc.py
new file mode 100644
index 0000000..07149e9
--- /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 0000000..e69de29
diff --git a/aosstower/tests/utils.py b/aosstower/tests/utils.py
new file mode 100644
index 0000000..d6fedde
--- /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
-- 
GitLab