Passed
Push — master ( e8dfc7...897815 )
by Stefan
07:20
created

test_level2.test_oldver_round_trip_nc()   A

Complexity

Conditions 2

Size

Total Lines 17
Code Lines 16

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 16
nop 2
dl 17
loc 17
rs 9.6
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# vim:fileencoding=utf-8
3
#
4
# Copyright (c) 2018 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 tests
12
"""
13
import os
14
15
import numpy as np
16
import pytest
17
import xarray as xr
18
19
import sciapy.level2
20
21
22
def test_module_structure():
23
	assert sciapy.level2
24
	assert sciapy.level2.binning
25
	assert sciapy.level2.binning.bin_lat_timeavg
26
	assert sciapy.level2.density
27
	assert sciapy.level2.density.scia_densities
28
29
30
@pytest.fixture(scope="module")
31
def ds():
32
	alts = np.r_[80:121:20.]
33
	lats = np.r_[-85:86:10.]
34
	lons = np.r_[15:186:10.]
35
	ts = np.linspace(3686, 3686.5, 2)
36
	lons_2d = np.vstack([lons + i * 180. for i in range(ts.size)])
37
	data = np.arange(ts.size * lats.size * alts.size).reshape(
38
		(ts.size, lats.size, alts.size)
39
	)
40
	ds = xr.Dataset(
41
		{
42
			"data": (
43
				["time", "latitude", "altitude"],
44
				data,
45
				{"units": "fir mfur μftn$^{-2}$ fth"}
46
			),
47
			"longitude": (
48
				["time", "latitude"],
49
				lons_2d,
50
				{"units": "degrees_east"}
51
			),
52
		},
53
		coords={
54
			"latitude": lats,
55
			"altitude": alts,
56
			"time": (
57
				["time"], ts, {"units": "days since 2000-01-01 00:00:00+00:00"}
58
			),
59
		},
60
	)
61
	return ds
62
63
64
def test_binning_aw(ds):
65
	from sciapy.level2.binning import bin_lat_timeavg
66
	data = ds.data.values
67
	lons = ds.longitude.values
68
	nts = ds.time.size
69
	wgts = np.cos(np.radians(ds.latitude.values))
70
	wgtbs = wgts[::2] + wgts[1::2]
71
	# divide each by the bin sum
72
	wgts[::2] /= wgtbs
73
	wgts[1::2] /= wgtbs
74
	data = data * wgts[None, :, None]
75
	lons = lons * wgts[None, :]
76
	# "manual" result
77
	data_avg = 1.0 / nts * (data[:, ::2, :] + data[:, 1::2, :]).sum(axis=0)
78
	lons_avg = 1.0 / nts * (lons[:, ::2] + lons[:, 1::2]).sum(axis=0)
79
	# binning function result
80
	dst_nw = bin_lat_timeavg(ds, bins=np.r_[-90:91:20])
81
	np.testing.assert_allclose(dst_nw.data.values, data_avg)
82
	np.testing.assert_allclose(dst_nw.longitude.values, lons_avg)
83
	assert dst_nw.data.attrs["units"] == "fir mfur μftn$^{-2}$ fth"
84
	assert dst_nw.longitude.attrs["units"] == "degrees_east"
85
86
87
def test_binning_nw(ds):
88
	from sciapy.level2.binning import bin_lat_timeavg
89
	data = ds.data.values
90
	lons = ds.longitude.values
91
	nts = ds.time.size
92
	# "manual" result
93
	data_avg = 0.5 / nts * (data[:, ::2, :] + data[:, 1::2, :]).sum(axis=0)
94
	lons_avg = 0.5 / nts * (lons[:, ::2] + lons[:, 1::2]).sum(axis=0)
95
	# binning function result
96
	dst_nw = bin_lat_timeavg(ds, bins=np.r_[-90:91:20], area_weighted=False)
97
	np.testing.assert_allclose(dst_nw.data.values, data_avg)
98
	np.testing.assert_allclose(dst_nw.longitude.values, lons_avg)
99
	assert dst_nw.data.attrs["units"] == "fir mfur μftn$^{-2}$ fth"
100
	assert dst_nw.longitude.attrs["units"] == "degrees_east"
101
102
103
DATADIR = os.path.join(".", "tests", "data", "l2")
104
IFILE = os.path.join(
105
	DATADIR,
106
	"000NO_orbit_41454_20100203_Dichten.txt",
107
)
108
109
110
def _assert_class_equal(l, r):
111
	for _k, _l in l.__dict__.items():
112
		_r = r.__dict__[_k]
113
		assert np.all(_l == _r), (_k, _l, _r)
114
115
116 View Code Duplication
@pytest.mark.parametrize(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
117
	"dirname, version",
118
	[["level2_v1.2.3", "1.2.3"], ["foo", None]],
119
)
120
def test_level2_round_trip_nc(tmpdir, dirname, version):
121
	from sciapy.level2.density import scia_densities
122
	odir = os.path.join(tmpdir, dirname)
123
	if not os.path.exists(odir):
124
		os.makedirs(odir)
125
	obase = os.path.join(odir, os.path.basename(IFILE))
126
	ofnc = obase + ".nc"
127
	l2_o = scia_densities(data_ver=version)
128
	l2_o.read_from_file(IFILE)
129
	l2_o.write_to_netcdf(ofnc)
130
	l2_t = scia_densities()
131
	l2_t.read_from_netcdf(ofnc)
132
	_assert_class_equal(l2_o, l2_t)
133
134
135 View Code Duplication
@pytest.mark.parametrize(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
136
	"dirname, version",
137
	[["level2_v1.2.3", "1.2.3"], ["foo", None]],
138
)
139
def test_level2_round_trip_txt(tmpdir, dirname, version):
140
	from sciapy.level2.density import scia_densities
141
	odir = os.path.join(tmpdir, dirname)
142
	if not os.path.exists(odir):
143
		os.makedirs(odir)
144
	obase = os.path.join(odir, os.path.basename(IFILE))
145
	oftxt = obase + ".txt"
146
	l2_o = scia_densities(data_ver=version)
147
	l2_o.read_from_file(IFILE)
148
	l2_o.write_to_textfile(oftxt)
149
	l2_t = scia_densities()
150
	l2_t.read_from_textfile(oftxt)
151
	_assert_class_equal(l2_o, l2_t)
152
153
154 View Code Duplication
@pytest.mark.parametrize(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
155
	"version",
156
	["0.8", "0.9"],
157
)
158
def test_oldver_round_trip_txt(tmpdir, version):
159
	from sciapy.level2.density import scia_densities
160
	odir = os.path.join(tmpdir, "level2_v{0}".format(version))
161
	if not os.path.exists(odir):
162
		os.makedirs(odir)
163
	obase = os.path.join(odir, os.path.basename(IFILE))
164
	oftxt = obase + ".txt"
165
	l2_o = scia_densities(data_ver=version)
166
	l2_o.read_from_file(IFILE[:-4] + "_v{0}.txt".format(version))
167
	l2_o.write_to_textfile(oftxt)
168
	l2_t = scia_densities()
169
	l2_t.read_from_textfile(oftxt)
170
	_assert_class_equal(l2_o, l2_t)
171
172
173 View Code Duplication
@pytest.mark.parametrize(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
174
	"version",
175
	["0.8", "0.9"],
176
)
177
def test_oldver_round_trip_nc(tmpdir, version):
178
	from sciapy.level2.density import scia_densities
179
	odir = os.path.join(tmpdir, "level2_v{0}".format(version))
180
	if not os.path.exists(odir):
181
		os.makedirs(odir)
182
	obase = os.path.join(odir, os.path.basename(IFILE))
183
	ofnc = obase + ".nc"
184
	l2_o = scia_densities(data_ver=version)
185
	l2_o.read_from_file(IFILE[:-4] + "_v{0}.txt".format(version))
186
	l2_o.write_to_netcdf(ofnc)
187
	l2_t = scia_densities()
188
	l2_t.read_from_netcdf(ofnc)
189
	_assert_class_equal(l2_o, l2_t)
190