Skip to content

Commit

Permalink
Merge cac22c3 into 3ed555e
Browse files Browse the repository at this point in the history
  • Loading branch information
johanvdw committed Nov 21, 2022
2 parents 3ed555e + cac22c3 commit 845ca8d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
12 changes: 8 additions & 4 deletions niche_vlaanderen/niche.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,10 +709,11 @@ def write(self, folder, overwrite_files=False, detailed_files=False):

if detailed_files:
for vi in self._vegetation_detail:
with rasterio.open(files[vi], "w", **params) as dst:
filename = files["%02d_detail" % vi]
with rasterio.open(filename, "w", **params) as dst:
dst.write(self._vegetation_detail[vi], 1)
self._files_written["%02d_detail" % vi] = os.path.normpath(
files["%02d_detail" % vi]
self._files_written[filename] = os.path.normpath(
filename
)

# deviation
Expand Down Expand Up @@ -1249,7 +1250,10 @@ def plot(self, key, ax=None):
mpl_extent = (a, c, d, b)

im = plt.imshow(
self._delta[key], extent=mpl_extent, norm=Normalize(0, max(self._values)), interpolation="none"
self._delta[key],
extent=mpl_extent,
norm=Normalize(0, max(self._values)),
interpolation="none",
)

if self.name != "":
Expand Down
25 changes: 18 additions & 7 deletions tests/test_niche.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import shutil
import os
import sys
import yaml

import distutils.spawn
import subprocess
Expand Down Expand Up @@ -477,10 +478,20 @@ def test_overwrite_file(self):
myniche = self.create_small()
myniche.run()
tmpdir = tempfile.mkdtemp()
myniche.write(tmpdir)
myniche.write(tmpdir, detailed_files=True)
# should raise: file already exists
with pytest.raises(NicheException):
myniche.write(tmpdir)

# check that all necessary files are created
with open(tmpdir + "/log.txt") as log:
res = yaml.safe_load(log)
files_written = res["files_written"]
# assert all files written actually exist

for index in files_written:
assert os.path.exists(files_written[index])

shutil.rmtree(tmpdir)

def test_overwrite_codetable_nonexisting(self):
Expand Down Expand Up @@ -606,7 +617,7 @@ def testinvalidDelta(self):
with pytest.raises(NicheException):
niche_vlaanderen.NicheDelta(zwb, small)

def test_overwrite_file(self):
def test_overwrite_delta_file(self):
myniche = TestNiche.create_small()
myniche.run(full_model=False)
delta = niche_vlaanderen.NicheDelta(myniche, myniche)
Expand All @@ -617,24 +628,24 @@ def test_overwrite_file(self):
delta.write(tmpdir)
# should just warn
delta.write(tmpdir, overwrite_files=True)
# check that all files in log.txt actually exist

shutil.rmtree(tmpdir)


@pytest.mark.skipif(
distutils.spawn.find_executable("gdalinfo") is None,
reason="gdalinfo not available in the environment.")
def test_conductivity2minerality():
tmpdir = tempfile.mkdtemp()
def test_conductivity2minerality(tmpdir):
niche_vlaanderen.conductivity2minerality(
"tests/data/small/conductivity.asc", tmpdir + "/minerality.tif")
"tests/data/small/conductivity.asc", str(tmpdir / "minerality.tif"))

info = subprocess.check_output(
["gdalinfo", "-stats", tmpdir + "/minerality.tif"]
["gdalinfo", "-stats", str(tmpdir / "minerality.tif")]
).decode('utf-8')

assert ("STATISTICS_MAXIMUM=1" in info)
assert ("STATISTICS_MINIMUM=0" in info)
assert ("STATISTICS_STDDEV=0.5")
assert ("STATISTICS_MEAN=0.5")

shutil.rmtree(tmpdir)

0 comments on commit 845ca8d

Please sign in to comment.