Passed
Push — master ( d361c5...72d69e )
by Stefan
09:40
created

test_level2_pp_cli.test_pp_module()   A

Complexity

Conditions 1

Size

Total Lines 31
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nop 3
dl 0
loc 31
rs 9.232
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# vim:fileencoding=utf-8
3
#
4
# Copyright (c) 2019 Stefan Bender
5
#
6
# This module is part of sciapy.
7
# sciapy is free software: you can redistribute it or modify
8
# it under the terms of the GNU General Public License as published
9
# by the Free Software Foundation, version 2.
10
# See accompanying LICENSE file or http://www.gnu.org/licenses/gpl-2.0.html.
11
"""SCIAMACHY level 2 post processing command line interface tests
12
13
Test functions to assure that the command line interface works in
14
most of the cases.
15
"""
16
import os
17
import sys
18
from subprocess import Popen
19
20
import pytest
21
from nccmpx import ncallclose, nccmpattrs
22
# If netCDF4 is available, the produced files will be
23
# netcdf4 files, otherwise netcdf3 files.
24
# Sets the `ncgen` option to use the same format.
25
try:
26
	import netCDF4
27
	NC_FMT = "-4"
28
	del netCDF4
29
except ImportError:
30
	NC_FMT = "-3"
31
32
CDL_EXT = ".cdl"
33
NC_EXT = ".nc"
34
DATADIR = os.path.join(".", "tests", "data")
35
IFILE1 = os.path.join(DATADIR, "test_v{0}" + NC_EXT)
36
IFILE2 = os.path.join(DATADIR, "test_v{0}x" + NC_EXT)
37
TEST_REVISIONS = ["2.1", "2.2"]
38
39
40
def _gentestfile(tfile, tmpdir):
41
	tpath = os.path.join(
42
		tmpdir,
43
		os.path.basename(tfile)
44
	)
45
	p = Popen([
46
		"ncgen",
47
		NC_FMT,
48
		"-o",
49
		tpath,
50
		tfile[:-len(NC_EXT)] + CDL_EXT,
51
	])
52
	p.communicate()
53
	p.wait()
54
	assert p.returncode == 0
55
	return tpath
56
57
58
def test_pp_help():
59
	p = Popen(["scia_post_process_l2.py", "-h"])
60
	p.communicate()
61
	p.wait()
62
	assert p.returncode == 0
63
64
65 View Code Duplication
@pytest.mark.xfail(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
66
	sys.version_info[:2] == (3, 4),
67
	reason="netcdf file attributes don't work with Python 3.4 compatible xarray.",
68
)
69
@pytest.mark.parametrize("revision", TEST_REVISIONS)
70
def test_pp_netcdf(revision, tmpdir):
71
	ifile = _gentestfile(IFILE1.format(revision), tmpdir)
72
	ofile = os.path.join(tmpdir, "test_v{0}_t.nc".format(revision))
73
	p = Popen([
74
		"scia_post_process_l2.py",
75
		"-A", "The Dude",
76
		"-M", "2010-02",
77
		"-R", revision,
78
		"-p", os.path.join(DATADIR, "l2"),
79
		"-s", os.path.join(DATADIR, "l1c"),
80
		"--mlt",
81
		ofile,
82
	])
83
	p.communicate()
84
	p.wait()
85
	assert p.returncode == 0
86
	ncallclose(ifile, ofile)
87
	nccmpattrs(ifile, ofile, ignore=["creation_time", "software"])
88
89
90 View Code Duplication
@pytest.mark.xfail(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
91
	sys.version_info[:2] == (3, 4),
92
	reason="netcdf file attributes don't work with Python 3.4 compatible xarray.",
93
)
94
@pytest.mark.parametrize("revision", TEST_REVISIONS)
95
def test_pp_xarray(revision, tmpdir):
96
	ifile = _gentestfile(IFILE2.format(revision), tmpdir)
97
	ofile = os.path.join(tmpdir, "test_v{0}x_t.nc".format(revision))
98
	p = Popen([
99
		"scia_post_process_l2.py",
100
		"-A", "The Dude",
101
		"-M", "2010-02",
102
		"-R", revision,
103
		"-p", os.path.join(DATADIR, "l2"),
104
		"-s", os.path.join(DATADIR, "l1c"),
105
		"--mlt",
106
		"-X",
107
		ofile,
108
	])
109
	p.communicate()
110
	p.wait()
111
	assert p.returncode == 0
112
	ncallclose(ifile, ofile)
113
	nccmpattrs(ifile, ofile, ignore=["creation_time", "software"])
114
115
116
@pytest.mark.xfail(
117
	sys.version_info[:2] == (3, 4),
118
	reason="netcdf file attributes don't work with Python 3.4 compatible xarray.",
119
)
120
@pytest.mark.parametrize("revision", TEST_REVISIONS)
121
@pytest.mark.parametrize("binary",
122
	[
123
		"scia_post_process_l2.py",
124
		"scia_post_process_l2",
125
		"python -m sciapy.level2.post_process",
126
	],
127
)
128
def test_pp_module(binary, revision, tmpdir):
129
	ifile = _gentestfile(IFILE2.format(revision), tmpdir)
130
	ofile = os.path.join(tmpdir, "test_v{0}x_t.nc".format(revision))
131
	p = Popen([
132
		*(binary.split(" ")),
133
		"-A", "The Dude",
134
		"-M", "2010-02",
135
		"-R", revision,
136
		"-p", os.path.join(DATADIR, "l2"),
137
		"-s", os.path.join(DATADIR, "l1c"),
138
		"--mlt",
139
		"-X",
140
		ofile,
141
	])
142
	p.communicate()
143
	p.wait()
144
	assert p.returncode == 0
145
	ncallclose(ifile, ofile)
146
	nccmpattrs(ifile, ofile, ignore=["creation_time", "software"])
147