Skip to content

Commit bd1f2a8

Browse files
committed
formatting etc
1 parent 7f311ad commit bd1f2a8

File tree

2 files changed

+204
-139
lines changed

2 files changed

+204
-139
lines changed

docs/tutorials/ADCP_data_tutorial.ipynb

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"source": [
77
"## Example ADCP data handling \n",
88
"### Upwelling near Peru\n",
9-
"The ocean region near Peru is known for its upwelling, causing the rise of nutrient-rich bottom water to the surface and a subsequently flourishing ecosystem. Hence, it is an important region for wildlife and for the fishery industry. \n",
9+
"The ocean region near Peru is known for its upwelling, causing the rise of nutrient-rich bottom water to the surface and a subsequently flourishing ecosystem. Hence, it is an important region for wildlife and the fishery industry. \n",
1010
"\n",
1111
"We explored this area with an ADCP device onboard the Virtualship. It measured zonal and meridional velocities up to 500 metres depth. In this tutorial you can see how we process the data in the context of wind-induced effects on ocean circulation."
1212
]
@@ -44,19 +44,20 @@
4444
"import requests\n",
4545
"\n",
4646
"files = {\n",
47-
" \"Peru_adcp.zip\":\"https://surfdrive.surf.nl/files/index.php/s/z1qAN6VP7zbbC3z/download\"}\n",
47+
" \"Peru_adcp.zip\": \"https://surfdrive.surf.nl/files/index.php/s/z1qAN6VP7zbbC3z/download\"\n",
48+
"}\n",
4849
"\n",
4950
"for filename, url in files.items():\n",
50-
" response = requests.get(url, allow_redirects=True)\n",
51+
" response = requests.get(url, allow_redirects=True)\n",
5152
"\n",
52-
" if response.status_code == 200:\n",
53-
" with open(filename, \"wb\") as f:\n",
54-
" f.write(response.content)\n",
55-
" else:\n",
56-
" print(\"Failed to download\", url)\n",
57-
"print('Download ready')\n",
53+
" if response.status_code == 200:\n",
54+
" with open(filename, \"wb\") as f:\n",
55+
" f.write(response.content)\n",
56+
" else:\n",
57+
" print(\"Failed to download\", url)\n",
58+
"print(\"Download ready\")\n",
5859
"\n",
59-
"#unpack the downloaded data\n",
60+
"# unpack the downloaded data\n",
6061
"!unzip -q Peru_adcp.zip -d . "
6162
]
6263
},
@@ -88,19 +89,19 @@
8889
],
8990
"source": [
9091
"# Make a plot of the ship's trajectory. To check that it runs perpendicular to the coast of Peru.\n",
91-
"peru = xr.open_zarr('Peru_adcp.zarr').compute()\n",
92+
"peru = xr.open_zarr(\"Peru_adcp.zarr\").compute()\n",
9293
"\n",
9394
"# for convenience, define z positive upward\n",
94-
"zu = np.unique(-peru.z, axis=1).squeeze() # convert z to 1D and positive upward\n",
95-
"peru = peru.assign_coords({'z':('trajectory',zu)}).sortby('z')\n",
95+
"zu = np.unique(-peru.z, axis=1).squeeze() # convert z to 1D and positive upward\n",
96+
"peru = peru.assign_coords({\"z\": (\"trajectory\", zu)}).sortby(\"z\")\n",
9697
"\n",
9798
"# plot track\n",
9899
"ax = plt.axes(projection=ccrs.PlateCarree())\n",
99100
"ax.scatter(peru.lon, peru.lat, transform=ccrs.PlateCarree())\n",
100101
"ax.coastlines()\n",
101102
"ax.gridlines(draw_labels=True)\n",
102-
"ax.set_extent((-85,-75,-15,-5))\n",
103-
"ax.set_title('Peru ADCP data')"
103+
"ax.set_extent((-85, -75, -15, -5))\n",
104+
"ax.set_title(\"Peru ADCP data\")"
104105
]
105106
},
106107
{
@@ -120,12 +121,13 @@
120121
"# Calculate the distance from each measurement to the coast and add it as variable to the dataset.\n",
121122
"peru_surf = peru.isel(trajectory=0).compute()\n",
122123
"d = xr.zeros_like(peru_surf.lon)\n",
123-
"lon0,lat0 = peru_surf.lon.data[-1], peru_surf.lat.data[-1]\n",
124-
"for ob,(lon,lat) in enumerate(zip(peru_surf.lon.data,peru_surf.lat.data)):\n",
125-
" d[ob] = distance.distance((lon,lat),(lon0,lat0)).m\n",
126-
"peru['s'] = -d.assign_attrs({\n",
127-
" 'long_name':'distance to coast','units':'m','positive':'shoreward'})\n",
128-
"peru = peru.set_coords('s').sortby('s')\n",
124+
"lon0, lat0 = peru_surf.lon.data[-1], peru_surf.lat.data[-1]\n",
125+
"for ob, (lon, lat) in enumerate(zip(peru_surf.lon.data, peru_surf.lat.data)):\n",
126+
" d[ob] = distance.distance((lon, lat), (lon0, lat0)).m\n",
127+
"peru[\"s\"] = -d.assign_attrs(\n",
128+
" {\"long_name\": \"distance to coast\", \"units\": \"m\", \"positive\": \"shoreward\"}\n",
129+
")\n",
130+
"peru = peru.set_coords(\"s\").sortby(\"s\")\n",
129131
"print(f\"max distance from coast: {abs(peru.s.min()).data/1000:.2f} km\")"
130132
]
131133
},
@@ -159,42 +161,48 @@
159161
"# Calculate and plot the velocity components parallel and perpendicular to the coast.\n",
160162
"\n",
161163
"# calculations\n",
162-
"dlon = np.deg2rad(peru_surf.lon.differentiate('obs'))\n",
163-
"dlat = np.deg2rad(peru_surf.lat.differentiate('obs'))\n",
164+
"dlon = np.deg2rad(peru_surf.lon.differentiate(\"obs\"))\n",
165+
"dlat = np.deg2rad(peru_surf.lat.differentiate(\"obs\"))\n",
164166
"lat = np.deg2rad(peru_surf.lat)\n",
165-
"alpha = np.arctan(dlat/(dlon*np.cos(lat))).mean('obs') # cruise direction angle\n",
166-
"Ucross = np.cos(alpha)*peru.U + np.sin(alpha)*peru.V # cross-shore vel\n",
167-
"Ulong = -np.sin(alpha)*peru.U + np.cos(alpha)*peru.V # long-shore vel\n",
168-
"peru['Ucross'] = Ucross.assign_attrs({'long_name':'cross-shore velocity','units':'m s-1'})\n",
169-
"peru['Ulong'] = Ulong.assign_attrs({'long_name':'longshore velocity','units':'m s-1'})\n",
167+
"alpha = np.arctan(dlat / (dlon * np.cos(lat))).mean(\"obs\") # cruise direction angle\n",
168+
"Ucross = np.cos(alpha) * peru.U + np.sin(alpha) * peru.V # cross-shore vel\n",
169+
"Ulong = -np.sin(alpha) * peru.U + np.cos(alpha) * peru.V # long-shore vel\n",
170+
"peru[\"Ucross\"] = Ucross.assign_attrs(\n",
171+
" {\"long_name\": \"cross-shore velocity\", \"units\": \"m s-1\"}\n",
172+
")\n",
173+
"peru[\"Ulong\"] = Ulong.assign_attrs(\n",
174+
" {\"long_name\": \"longshore velocity\", \"units\": \"m s-1\"}\n",
175+
")\n",
170176
"\n",
171177
"# figure settings\n",
172-
"fig = plt.figure(figsize=(10,6))\n",
178+
"fig = plt.figure(figsize=(10, 6))\n",
173179
"norm = mcolors.CenteredNorm(halfrange=0.35)\n",
174180
"cmap = cmocean.cm.balance\n",
175-
"landmask = xr.where(((peru.U==0) & (peru.V==0)), 1, np.nan)\n",
181+
"landmask = xr.where(((peru.U == 0) & (peru.V == 0)), 1, np.nan)\n",
176182
"S = peru.s.broadcast_like(peru.lon)\n",
177183
"\n",
178184
"# cross-shore velocity plot\n",
179185
"ax1 = fig.add_subplot(121)\n",
180-
"p = ax1.pcolormesh(S/1000, peru.z, peru.Ucross, norm=norm, cmap=cmap)\n",
181-
"ax1.pcolormesh(S/1000, peru.z, landmask, cmap='binary_r')\n",
182-
"ax1.set_ylabel('depth [m]')\n",
183-
"ax1.set_xlabel('distance from coast [km]')\n",
184-
"ax1.set_title('cross-shore velocity (positive shoreward)')\n",
186+
"p = ax1.pcolormesh(S / 1000, peru.z, peru.Ucross, norm=norm, cmap=cmap)\n",
187+
"ax1.pcolormesh(S / 1000, peru.z, landmask, cmap=\"binary_r\")\n",
188+
"ax1.set_ylabel(\"depth [m]\")\n",
189+
"ax1.set_xlabel(\"distance from coast [km]\")\n",
190+
"ax1.set_title(\"cross-shore velocity (positive shoreward)\")\n",
185191
"ax1.grid()\n",
186192
"\n",
187193
"# alongshore velocity plot\n",
188194
"ax2 = fig.add_subplot(122, sharey=ax1)\n",
189195
"ax2.yaxis.tick_right()\n",
190-
"p2 = ax2.pcolormesh(S/1000, peru.z, peru.Ulong, norm=norm, cmap=cmap)\n",
191-
"ax2.pcolormesh(S/1000, peru.z, landmask, cmap='binary_r')\n",
192-
"ax2.set_ylabel('depth [m]', rotation=270, labelpad=15)\n",
193-
"ax2.set_xlabel('distance from coast [km]')\n",
194-
"ax2.set_title('longshore velocity (positive with coast on r.h.s.)')\n",
195-
"ax2.yaxis.set_label_position('right')\n",
196+
"p2 = ax2.pcolormesh(S / 1000, peru.z, peru.Ulong, norm=norm, cmap=cmap)\n",
197+
"ax2.pcolormesh(S / 1000, peru.z, landmask, cmap=\"binary_r\")\n",
198+
"ax2.set_ylabel(\"depth [m]\", rotation=270, labelpad=15)\n",
199+
"ax2.set_xlabel(\"distance from coast [km]\")\n",
200+
"ax2.set_title(\"longshore velocity (positive with coast on r.h.s.)\")\n",
201+
"ax2.yaxis.set_label_position(\"right\")\n",
196202
"ax2.grid()\n",
197-
"fig.colorbar(p, ax=[ax1,ax2], shrink=0.7, orientation='horizontal', label=r'm s$^{-1}$')"
203+
"fig.colorbar(\n",
204+
" p, ax=[ax1, ax2], shrink=0.7, orientation=\"horizontal\", label=r\"m s$^{-1}$\"\n",
205+
")"
198206
]
199207
},
200208
{
@@ -234,32 +242,36 @@
234242
],
235243
"source": [
236244
"# Integrate the continuity equation to find the vertical velocity, starting from the surface downward.\n",
237-
"peru_d = peru.sortby('z', ascending=False) # sort downward\n",
245+
"peru_d = peru.sortby(\"z\", ascending=False) # sort downward\n",
238246
"\n",
239247
"# # compute dwdz - easy central difference method\n",
240248
"# dwdz = -peru_d.Ucross.differentiate('s')\n",
241249
"\n",
242250
"# compute dwdz - central difference using interpolated values at cell edges\n",
243251
"# slightly more accurate\n",
244-
"landfilter = xr.where(((peru_d.U==0) & (peru_d.V==0)), 0, 1) # 0 in land, 1 in ocean\n",
245-
"Ucrossi = peru_d.Ucross.interp(obs=peru_d.obs-0.5) # U at cell boundaries (toward ocean)\n",
252+
"landfilter = xr.where(\n",
253+
" ((peru_d.U == 0) & (peru_d.V == 0)), 0, 1\n",
254+
") # 0 in land, 1 in ocean\n",
255+
"Ucrossi = peru_d.Ucross.interp(\n",
256+
" obs=peru_d.obs - 0.5\n",
257+
") # U at cell boundaries (toward ocean)\n",
246258
"Ucrossi = Ucrossi * landfilter.data\n",
247-
"dUcrossds = Ucrossi.diff('obs') / Ucrossi.s.diff('obs')\n",
259+
"dUcrossds = Ucrossi.diff(\"obs\") / Ucrossi.s.diff(\"obs\")\n",
248260
"dwdz = -dUcrossds\n",
249261
"\n",
250262
"# integrate dwdz\n",
251-
"peru['w'] = dwdz.cumulative_integrate('z')\n",
263+
"peru[\"w\"] = dwdz.cumulative_integrate(\"z\")\n",
252264
"\n",
253265
"# plot the vertical velocity\n",
254266
"ax = plt.axes()\n",
255-
"S,Z = xr.broadcast(peru.s/1000, peru.z)\n",
267+
"S, Z = xr.broadcast(peru.s / 1000, peru.z)\n",
256268
"w = ax.pcolormesh(S, Z, peru.w.T, norm=mcolors.CenteredNorm(), cmap=cmap)\n",
257-
"ax.pcolormesh(S, Z, landmask.T, cmap='binary_r')\n",
258-
"plt.title('vertical velocity')\n",
259-
"plt.ylabel('depth [m]')\n",
260-
"plt.xlabel('shoreward distance [km]')\n",
261-
"plt.xlim([-350,0])\n",
262-
"plt.colorbar(w, label=r'm s$^{-1}$')"
269+
"ax.pcolormesh(S, Z, landmask.T, cmap=\"binary_r\")\n",
270+
"plt.title(\"vertical velocity\")\n",
271+
"plt.ylabel(\"depth [m]\")\n",
272+
"plt.xlabel(\"shoreward distance [km]\")\n",
273+
"plt.xlim([-350, 0])\n",
274+
"plt.colorbar(w, label=r\"m s$^{-1}$\")"
263275
]
264276
},
265277
{

0 commit comments

Comments
 (0)