forked from OpenDrift/trajan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_model_along_trajectory.py
More file actions
58 lines (48 loc) · 1.41 KB
/
Copy pathexample_model_along_trajectory.py
File metadata and controls
58 lines (48 loc) · 1.41 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
"""
Read variables from a model along the trajectories of drifters.
=============================================================================
"""
import xarray as xr
import numpy as np
import cf_xarray as _
import pyproj
import trajan as ta
import matplotlib.pyplot as plt
#%%
# Open drifter dataset from a CSV file
ds = ta.read_csv('bug05_pos.csv.xz',
lon='Longitude',
lat='Latitude',
time='Time',
name='Device')
print(ds)
#%%
# Open the Norkyst800 model
nk = xr.open_dataset(
'https://thredds.met.no/thredds/dodsC/sea/norkyst800m/1h/aggregate_be')
#%%
# Use cf-xarray to get the CRS
gm = nk.cf['grid_mapping']
nk_crs = pyproj.CRS.from_cf(gm.attrs)
print(nk_crs)
#%%
# Grid the drifter dataset to the timesteps of the model.
times = nk.sel(time=slice('2022-05-10', '2022-05-20')).time.values
ds = ds.traj.gridtime(times)
ds = ds.dropna('time', how='all')
print(ds)
#%%
# Transform the drifter dataset to the CRS of the model
dst = ds.traj.transform(nk_crs)
#%%
# Extract the values of a variable for the trajectory
temp = nk.isel(depth=0).sel(time=dst.time,
X=dst.x,
Y=dst.y,
method='nearest').temperature
print(temp)
#%%
# Notice that the `lat` and `lon` variables from Norkyst match the original `lat` and `lon` from the dataset.
plt.figure()
temp.plot()
plt.show()