Skip to content

Commit 6d74c78

Browse files
authored
Merge pull request #166 from knutfrode/dev
Dev
2 parents d5f4e0b + 385d8a7 commit 6d74c78

File tree

7 files changed

+38
-18
lines changed

7 files changed

+38
-18
lines changed

examples/create_test_dataset.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
'16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc')
1515
o.add_reader([reader_norkyst, reader_arome])
1616

17-
# Seeding some particles
18-
o.seed_elements(lon=4.4, lat=60.1, radius=1000, number=1000,
19-
time=reader_arome.start_time)
17+
# Seeding some particles, continuous spill
18+
o.seed_elements(lon=4.2, lat=60.1, radius=1000, number=1000,
19+
time=[reader_arome.start_time, reader_arome.end_time])
2020

2121
# Running model
2222
o.run(end_time=reader_norkyst.end_time,
23-
export_variables=['mass_oil'], outfile='openoil.nc')
23+
export_variables=['viscosity', 'z'], outfile='openoil.nc')
24+
25+
o.plot(fast=True, linecolor='viscosity')

examples/example_opendrift.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,17 @@
1313

1414
#%%
1515
# Importing a trajectory dataset from a simulation with OpenDrift.
16-
# decode_coords is needed so that lon and lat are not interpreted as coordinate variables.
17-
with lzma.open('openoil.nc.xz') as oil:
18-
ds = xr.open_dataset(oil, decode_coords=False)
19-
ds.load()
20-
# Requirement that status>=0 is needed since non-valid points are not masked in OpenDrift output
21-
ds = ds.where(ds.status>=0) # only active particles
16+
ds = xr.open_dataset('openoil.nc')
2217

2318
#%%
2419
# Displaying some basic information about this dataset
2520
print(ds.traj)
2621

2722
#%%
28-
# Making a basic plot of trajectories
29-
ds.traj.plot()
30-
plt.title('Basic trajectory plot')
23+
# Plotting trajectories, colored by oil viscosity
24+
mappable = ds.traj.plot(color=ds.viscosity, alpha=1)
25+
plt.title('Oil trajectories')
26+
plt.colorbar(mappable, orientation='horizontal', label=f'Oil viscosity [{ds.viscosity.units}]')
3127
plt.show()
3228

3329
#%%

examples/example_parcels.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,25 @@
1919
#%%
2020
# Basic plot
2121
ds.traj.plot(land='mask', margin=1)
22+
2223
# TODO: we must allow no time dimension for the below to work
2324
#ds.mean('trajectory', skipna=True).traj.plot(color='r', label='Mean trajectory')
25+
# In the meantime, we regrid to a regular 1D dataset to allow plotting a mean trajectory
26+
ds = ds.traj.gridtime('1h')
27+
ds.mean('trajectory').traj.plot(color='r', label='Mean trajectory')
28+
plt.legend()
29+
plt.show()
2430

31+
#%%
32+
# Calculating skillscore
33+
# Defining the first trajectory to be the "true"
34+
ds_true = ds.isel(trajectory=0)
35+
skillscore = ds.traj.skill(expected=ds_true, method='liu-weissberg', tolerance_threshold=1)
36+
37+
#%%
38+
# Plotting trajectories, colored by their skillscore, compared to the "true" trajectory (black)
39+
mappable = ds.traj.plot(land='mask', color=skillscore)
40+
ds_true.traj.plot(color='k', linewidth=3, label='"True" trajectory')
41+
plt.colorbar(mappable=mappable, orientation='horizontal', label='Skillscore per trajectory')
42+
plt.legend()
2543
plt.show()

examples/openoil.nc

297 KB
Binary file not shown.

examples/openoil.nc.xz

-332 KB
Binary file not shown.

tests/fixtures.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ def plot(pytestconfig):
99

1010
@pytest.fixture
1111
def opendrift_sim():
12-
oil = Path(__file__).parent.parent / 'examples' / 'openoil.nc.xz'
13-
with lzma.open(oil) as oil:
14-
ds = xr.open_dataset(oil)
15-
ds.load()
16-
return ds
12+
oil = Path(__file__).parent.parent / 'examples' / 'openoil.nc'
13+
ds = xr.open_dataset(oil)
14+
return ds
1715

1816
@pytest.fixture
1917
def barents():

trajan/plot/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ def lines(self, *args, **kwargs):
168168
y = self.ds.traj.tlat.values.T
169169

170170
if hasattr(kwargs['color'], 'shape'):
171+
if isinstance(kwargs['color'], xr.DataArray):
172+
kwargs['color'] = kwargs['color'].values
173+
# TODO: it should be possible to make this much faster, especially for fixed color per trajectory
174+
if kwargs['color'].ndim == 1: # Fixed color per line
175+
assert len(kwargs['color']) == x.shape[1]
176+
kwargs['color'] = kwargs['color'][:, np.newaxis] * np.ones(x.shape).T
171177
from matplotlib.collections import LineCollection
172178
c = kwargs.pop('color').T
173179
if hasattr(c, 'values'):

0 commit comments

Comments
 (0)