From 26287efa052adb591e31664837143400a9b806c0 Mon Sep 17 00:00:00 2001 From: "Soroosh.Mani" Date: Mon, 13 Nov 2023 11:21:16 -0500 Subject: [PATCH] Use Python code for downloading test file --- tests/api/__init__.py | 27 +++++++++++++++++++++------ tests/api/hfun.py | 11 ++++++----- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/tests/api/__init__.py b/tests/api/__init__.py index 38c66fd8..21716b3a 100644 --- a/tests/api/__init__.py +++ b/tests/api/__init__.py @@ -1,7 +1,22 @@ import os -os.system(""" - ls /tmp/test_dem.tif > /dev/null - if [ $? -eq 0 ]; then exit; fi - wget https://coast.noaa.gov/htdata/raster2/elevation/NCEI_ninth_Topobathy_2014_8483/northeast_sandy/ncei19_n40x75_w073x75_2015v1.tif -O /tmp/fullsize_dem.tif - gdalwarp -tr 0.0005 0.0005 -r average -overwrite /tmp/fullsize_dem.tif /tmp/test_dem.tif -""") +import tempfile +import urllib.request +from pathlib import Path + +from rasterio.enums import Resampling + +from ocsmesh.raster import Raster + + +# Find a better way! +tif_url = ( + 'https://coast.noaa.gov/htdata/raster2/elevation/NCEI_ninth_Topobathy_2014_8483/northeast_sandy/ncei19_n40x75_w073x75_2015v1.tif' +) +TEST_FILE = os.path.join(tempfile.gettempdir(), 'test_dem.tif') +if not Path(TEST_FILE).exists(): + with tempfile.NamedTemporaryFile() as tfp: + urllib.request.urlretrieve(tif_url, filename=tfp.name) + r = Raster(tfp.name) + r.resampling_method = Resampling.average + r.resample(scaling_factor=0.01) + r.save(TEST_FILE) diff --git a/tests/api/hfun.py b/tests/api/hfun.py index 32bb40a3..65c39d4e 100755 --- a/tests/api/hfun.py +++ b/tests/api/hfun.py @@ -13,6 +13,7 @@ import ocsmesh +from tests.api import TEST_FILE from tests.api.common import ( topo_2rast_1mesh, ) @@ -492,7 +493,7 @@ def test_hfun_fast_extent(self): class SizeFromMesh(unittest.TestCase): def setUp(self): - rast = ocsmesh.raster.Raster('/tmp/test_dem.tif') + rast = ocsmesh.raster.Raster(TEST_FILE) hfun_orig = ocsmesh.hfun.hfun.Hfun(rast, hmin=100, hmax=1500) hfun_orig.add_contour(level=0, expansion_rate=0.001, target_size=100) @@ -530,7 +531,7 @@ def test_hfun_raster_cfl_constraint_support(self): courant_hi = 0.8 courant_lo = 0.2 - rast = ocsmesh.raster.Raster('/tmp/test_dem.tif') + rast = ocsmesh.raster.Raster(TEST_FILE) hfun_raster = ocsmesh.hfun.hfun.Hfun(rast, hmin=100, hmax=5000) hfun_raster.add_courant_num_constraint( @@ -586,7 +587,7 @@ def test_hfun_raster_cfl_constraint_support(self): ) def test_hfun_raster_cfl_constraint_io(self): - rast = ocsmesh.raster.Raster('/tmp/test_dem.tif') + rast = ocsmesh.raster.Raster(TEST_FILE) hfun_raster = ocsmesh.hfun.hfun.Hfun(rast, hmin=100, hmax=5000) self.assertRaises( ValueError, @@ -614,8 +615,8 @@ def test_hfun_coll_cfl_constraint(self): # TODO: Add subTest # Creating adjacent rasters from the test raster - rast1 = ocsmesh.raster.Raster('/tmp/test_dem.tif') - rast2 = ocsmesh.raster.Raster('/tmp/test_dem.tif') + rast1 = ocsmesh.raster.Raster(TEST_FILE) + rast2 = ocsmesh.raster.Raster(TEST_FILE) bounds = rast1.bbox.bounds bbox1 = geometry.box( bounds[0], bounds[1], (bounds[0] + bounds[2]) / 2, bounds[3]