From 3d04ee98217e2e0124fc815c4ac614350f096723 Mon Sep 17 00:00:00 2001
From: Vladimir Onoprienko <vonopr@yandex.ru>
Date: Wed, 2 Sep 2020 19:40:29 +0300
Subject: [PATCH] Add unit tests
---
.gitignore | 1 +
inmplot.py | 2 ++
plot_ctl.py | 12 +++++++-----
pytest.ini | 5 +++++
setup.py | 2 +-
tests/data/mmsst.dat | 4 ++--
tests/data/mmsst.nc | 3 +++
tests/data/sst.png | 4 ++--
tests/tests.py | 33 +++++++++++++++++++++++++++++++++
9 files changed, 56 insertions(+), 10 deletions(-)
create mode 100644 inmplot.py
create mode 100644 pytest.ini
create mode 100644 tests/data/mmsst.nc
create mode 100644 tests/tests.py
diff --git a/.gitignore b/.gitignore
index 33414d6..6996cf0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
__pycache__*
+inmplot.egg-info*
ctler.egg-info*
diff --git a/inmplot.py b/inmplot.py
new file mode 100644
index 0000000..31b3cc1
--- /dev/null
+++ b/inmplot.py
@@ -0,0 +1,2 @@
+from ctler import ctl_to_netcdf
+from plot_ctl import plot_ctl
diff --git a/plot_ctl.py b/plot_ctl.py
index d929d36..39f507e 100644
--- a/plot_ctl.py
+++ b/plot_ctl.py
@@ -2,7 +2,7 @@ import matplotlib.pyplot as plt
import matplotlib.cm as cm
import ctler
import argparse
-
+
def cmdargs_wide_help_column():
formatter = lambda prog: argparse.HelpFormatter(prog,max_help_position=40)
return formatter
@@ -15,21 +15,23 @@ def cmdargs():
parser.add_argument("--znum", "-z", default=0 , help="level number to plot")
parser.add_argument("--cmap", "-c", default="coolwarm" , help="color map palette")
parser.add_argument("--output", "-o", default="pic.png" , help="output image file")
+ parser.add_argument("--dpi", "-q", default="600" , help="output image dpi resolution")
return parser.parse_args()
-def plot_ctl(ctl_file, varname, tnum, znum, cmap, out_file):
+def plot_ctl(ctl_file, varname, tnum, znum, cmap, out_file, dpi):
ds = ctler.ctl_read(ctl_file)
lon = ds.variables['longitude'][:]
lat = ds.variables['latitude'][:]
var = ds.variables[varname]
-
+ fig = plt.figure()
plt.contourf(lon, lat, var[tnum,znum,:,:], cmap=cm.get_cmap(name=cmap) )
plt.colorbar()
- plt.savefig(out_file, dpi=300)
+ plt.savefig(out_file, dpi=dpi)
+ return fig
if __name__ == '__main__':
args = cmdargs()
- plot_ctl(args.file, args.var, args.tnum, args.znum, args.cmap, args.output)
+ plot_ctl(args.file, args.var, args.tnum, args.znum, args.cmap, args.output, int(args.dpi))
diff --git a/pytest.ini b/pytest.ini
new file mode 100644
index 0000000..ca55c81
--- /dev/null
+++ b/pytest.ini
@@ -0,0 +1,5 @@
+[pytest]
+addopts = --mpl
+
+testpaths =
+ tests/tests.py
diff --git a/setup.py b/setup.py
index 50267d0..7fe67c6 100644
--- a/setup.py
+++ b/setup.py
@@ -15,7 +15,7 @@ setup(
description="Plotting tools for INMCM model",
classifiers=filter(None, classifiers.split("\n")),
license='MIT',
- py_modules=['ctler, ctlinm, plot_ctl'],
+ py_modules=['ctler, ctlinm, plot_ctl, inmplot'],
include_package_data=True,
install_requires=[
diff --git a/tests/data/mmsst.dat b/tests/data/mmsst.dat
index be539f8..37c96c4 100644
--- a/tests/data/mmsst.dat
+++ b/tests/data/mmsst.dat
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
-oid sha256:54d1a83b3aae94b0f080ba4127e8c8cffcffe5dcbc0559dcbb3e3a2035adaa05
-size 4147200
+oid sha256:8407281e492115af89e967478973ffa4db2b8ac15e1ff831bdcab3e667e72cce
+size 49766400
diff --git a/tests/data/mmsst.nc b/tests/data/mmsst.nc
new file mode 100644
index 0000000..1b02e9e
--- /dev/null
+++ b/tests/data/mmsst.nc
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:ff4a9f21153f30200dfc2c1a9184ec1d349ef30441b9b41229472da34e2e7e2e
+size 49782468
diff --git a/tests/data/sst.png b/tests/data/sst.png
index 9c5e909..2fc107f 100644
--- a/tests/data/sst.png
+++ b/tests/data/sst.png
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
-oid sha256:b2fc198ef1c54c924067d4cc3f9001877e323610d570b5213dc806494e898790
-size 110844
+oid sha256:714a333ef5aaf88dcb0e6bcb83061c0d43eedcea1dd675b6f634875128cb43f6
+size 30508
diff --git a/tests/tests.py b/tests/tests.py
new file mode 100644
index 0000000..021e91d
--- /dev/null
+++ b/tests/tests.py
@@ -0,0 +1,33 @@
+import inmplot
+
+import pytest
+import filecmp
+import os
+
+
+@pytest.fixture
+def convert_ctl_to_netcdf():
+ inmplot.ctl_to_netcdf(os.path.join('tests','data','mmsst.ctl'), 'test.nc')
+ yield
+ os.remove('test.nc')
+
+def test_ctl_to_netcdf(convert_ctl_to_netcdf):
+ assert filecmp.cmp('test.nc', os.path.join('tests','data','mmsst.nc'))
+
+
+
+
+
+def teardown_module():
+ os.remove('test.png')
+
+@pytest.mark.mpl_image_compare(baseline_dir="data", filename='sst.png', savefig_kwargs={'dpi': 100})
+def test_plot_ctl():
+ return inmplot.plot_ctl(
+ ctl_file=os.path.join('tests','data','mmsst.ctl'),
+ varname='sst',
+ tnum=0,
+ znum=0,
+ cmap='coolwarm',
+ out_file="test.png",
+ dpi=100)
--
GitLab