-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlotting_data_from_one_netcdf_file.py
executable file
·50 lines (40 loc) · 1.36 KB
/
Plotting_data_from_one_netcdf_file.py
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
#!/usr/bin/env python3
import xarray as xr
import matplotlib.pyplot as plt
import numpy as np
import cmocean
import sys
# Traverse THREDDS server, isolate 'N' for Greenland.
url = 'https://www.ngdc.noaa.gov/thredds/dodsC/global/ETOPO2022/15s/15s_bed_elev_netcdf/ETOPO_2022_v1_15s_N75W030_bed.nc'
ds = xr.open_dataset(url)
bathymetry = ds['z']
# Subset the data to the area of interest
lat_min = 72
lat_max = 74
lon_min = -17
lon_max = -16
# Elevation range for colour scale
vmax = 3000
vmin = vmax * -1
# Contour interval
contour_interval = 500
contour_levels = np.arange(vmin, vmax + contour_interval, contour_interval)
# Plot only every nth sample in both lat and lon to speed up processing
sampling_factor = 10
bathymetry_zoomed = bathymetry.sel(lat=slice(lat_min, lat_max), lon=slice(lon_min, lon_max))
# Check if there is data in the subset
if bathymetry_zoomed.size == 0:
print("No data in the specified range for file")
sys.exit()
else:
bathymetry = bathymetry_zoomed
# Select every nth sample for faster resampling
bathymetry_resampled = bathymetry.isel(
lat=slice(None, None, sampling_factor),
lon=slice(None, None, sampling_factor)
)
# Plot the data with a colormap
bathymetry_resampled.plot(cmap=cmocean.cm.topo, vmin=vmin, vmax=vmax)
# Plot contours
bathymetry_resampled.plot.contour(levels=contour_levels, colors='black', linewidths=0.5)
plt.show()