-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathtest_reader_shape.py
More file actions
107 lines (89 loc) · 3.95 KB
/
Copy pathtest_reader_shape.py
File metadata and controls
107 lines (89 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import numpy as np
import pytest
from . import *
from opendrift.readers import reader_ROMS_native
from opendrift.models.oceandrift import OceanDrift
from opendrift.readers import reader_shape
import cartopy.io.shapereader as shpreader
import urllib.error
has_ne_shapes = False
try:
shpreader.natural_earth(resolution='110m',
category='cultural',
name='admin_0_countries')
has_ne_shapes = True
except urllib.error.URLError:
has_ne_shapes = False
need_ne_shapes = pytest.mark.skipif(has_ne_shapes == False,
reason='Natural Earth shapes are not available.')
@need_ne_shapes
def test_on_land():
shpfilename = shpreader.natural_earth(resolution='110m',
category='cultural',
name='admin_0_countries')
r = reader_shape.Reader.from_shpfiles(shpfilename)
assert r.__on_land__(np.array([10]), np.array([60]), r.variables[0]) == [True]
assert r.__on_land__(np.array([5]), np.array([60]), r.variables[0]) == [False]
@need_ne_shapes
def test_global_array(test_data):
shpfilename = shpreader.natural_earth(resolution='110m',
category='cultural',
name='admin_0_countries')
reader_landmask = reader_shape.Reader.from_shpfiles(shpfilename)
reader_nordic = reader_ROMS_native.Reader(
test_data +
'2Feb2016_Nordic_sigma_3d/Nordic-4km_SLEVELS_avg_00_subset2Feb2016.nc')
lon = np.array([15., 5.])
lat = np.array([65.6, 65.6])
# global
oc = OceanDrift(loglevel=00)
oc.set_config('general:use_auto_landmask', False)
oc.add_reader([reader_nordic, reader_landmask])
oc.env.finalize()
en, en_prof, missing = oc.env.get_environment(['land_binary_mask'],
reader_nordic.start_time, lon,
lat, np.array([0, 0]), None)
np.testing.assert_array_equal(en.land_binary_mask, np.array([True, False]))
print(oc.env.readers)
assert len(
oc.env.readers) == 2 # make sure opendrift doesn't add default basemap
def test_get_shape_variable():
oc = OceanDrift(loglevel=30)
shpfilename = [
oc.test_data_folder() + 'shp/test_vars.shp',
oc.test_data_folder() + 'shp/test_more_vars.shp',
]
reader = reader_shape.Reader.from_shpfiles(shpfilename)
test = reader.get_variables(['var0', 'var1'],
x=14.267009, y=68.528457)
assert test['var0'] == False and test['var1'] == True, \
"Failed on reading test_vars.shp"
test = reader.get_variables(['var0', 'var1'],
x=13.979286, y=68.164405)
assert test['var0'] == True and test['var1'] == False, \
"Failed on reading test_more_vars.shp"
def test_global_var_array():
oc = OceanDrift(loglevel=30)
shpfilename = [
oc.test_data_folder() + 'shp/test_vars.shp',
oc.test_data_folder() + 'shp/test_vars_landmask.shp',
oc.test_data_folder() + 'shp/test_more_vars.shp',
]
reader_landmask = reader_shape.Reader.from_shpfiles(shpfilename)
reader_nordic = reader_ROMS_native.Reader(
oc.test_data_folder() + \
'2Feb2016_Nordic_sigma_3d/Nordic-4km_SLEVELS_avg_00_subset2Feb2016.nc')
lon = np.array([15., 5.])
lat = np.array([65.6, 65.6])
# global
oc = OceanDrift(loglevel=00)
oc.set_config('general:use_auto_landmask', False)
oc.add_reader([reader_nordic, reader_landmask])
oc.env.finalize()
en, en_prof, missing = oc.env.get_environment(['land_binary_mask'],
reader_nordic.start_time, lon,
lat, np.array([0, 0]), None)
np.testing.assert_array_equal(en.land_binary_mask, np.array([True, False]))
print(oc.env.readers)
assert len(
oc.env.readers) == 2 # make sure opendrift doesn't add default basemap