import xarray as xr
ds = xr.open_mfdataset("local_folder/oscar_vel2022.nc")
# pull out data for all of 2022-June
ds.sel(time="2022-06")
<xarray.Dataset> Dimensions: (time: 6, year: 72, depth: 1, latitude: 481, longitude: 1201) Coordinates: * time (time) datetime64[ns] 2022-06-02 2022-06-07 ... 2022-06-27 * year (year) float32 2.022e+03 2.022e+03 ... 2.023e+03 2.023e+03 * depth (depth) float32 15.0 * latitude (latitude) float64 80.0 79.67 79.33 79.0 ... -79.33 -79.67 -80.0 * longitude (longitude) float64 20.0 20.33 20.67 21.0 ... 419.3 419.7 420.0 Data variables: u (time, depth, latitude, longitude) float64 dask.array<chunksize=(6, 1, 481, 1201), meta=np.ndarray> v (time, depth, latitude, longitude) float64 dask.array<chunksize=(6, 1, 481, 1201), meta=np.ndarray> um (time, depth, latitude, longitude) float64 dask.array<chunksize=(6, 1, 481, 1201), meta=np.ndarray> vm (time, depth, latitude, longitude) float64 dask.array<chunksize=(6, 1, 481, 1201), meta=np.ndarray> Attributes: (12/15) VARIABLE: Ocean Surface Currents DATATYPE: 1/72 YEAR Interval DATASUBTYPE: unfiltered GEORANGE: 20 to 420 -80 to 80 PERIOD: Jan.01,2022 to Dec.26,2022 year: 2022 ... ... source: Gary Lagerloef, ESR ([email protected]) and Kathleen Dohan, E... contact: Kathleen Dohan ([email protected]) or John T. Gunn (gunn@esr... company: Earth & Space Research, Seattle, WA reference: Bonjean F. and G.S.E. Lagerloef, 2002 ,Diagnostic model a... note1: Maximum Mask velocity is the geostrophic component at all... note2: Longitude extends from 20 E to 420 E to avoid a break in ...
# demonstrate "nearest" indexing
ds.sel(longitude=240.2, method="nearest")
<xarray.Dataset> Dimensions: (time: 72, year: 72, depth: 1, latitude: 481) Coordinates: * time (time) datetime64[ns] 2022-01-01 2022-01-06 ... 2022-12-26 * year (year) float32 2.022e+03 2.022e+03 ... 2.023e+03 2.023e+03 * depth (depth) float32 15.0 * latitude (latitude) float64 80.0 79.67 79.33 79.0 ... -79.33 -79.67 -80.0 longitude float64 240.3 Data variables: u (time, depth, latitude) float64 dask.array<chunksize=(72, 1, 481), meta=np.ndarray> v (time, depth, latitude) float64 dask.array<chunksize=(72, 1, 481), meta=np.ndarray> um (time, depth, latitude) float64 dask.array<chunksize=(72, 1, 481), meta=np.ndarray> vm (time, depth, latitude) float64 dask.array<chunksize=(72, 1, 481), meta=np.ndarray> Attributes: (12/15) VARIABLE: Ocean Surface Currents DATATYPE: 1/72 YEAR Interval DATASUBTYPE: unfiltered GEORANGE: 20 to 420 -80 to 80 PERIOD: Jan.01,2022 to Dec.26,2022 year: 2022 ... ... source: Gary Lagerloef, ESR ([email protected]) and Kathleen Dohan, E... contact: Kathleen Dohan ([email protected]) or John T. Gunn (gunn@esr... company: Earth & Space Research, Seattle, WA reference: Bonjean F. and G.S.E. Lagerloef, 2002 ,Diagnostic model a... note1: Maximum Mask velocity is the geostrophic component at all... note2: Longitude extends from 20 E to 420 E to avoid a break in ...
import numpy as np
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
plt.figure(figsize=(18, 9))
ax = plt.axes(projection=ccrs.PlateCarree()) # Plate Carrée projection
dec = 2
lon = ds.longitude.values[::dec]
lon[lon > 180] = lon[lon > 180] - 360
mymap = plt.streamplot(
lon,
ds.latitude.values[::dec],
ds.u.values[0, 0, ::dec, ::dec],
ds.v.values[0, 0, ::dec, ::dec],
8,
transform=ccrs.PlateCarree()
)
ax.coastlines()
# Adjust the longitude range for the entire world map
ax.set_extent([20, 420, -80, 80], crs=ccrs.PlateCarree())
plt.title('Sea surface currents derived from oscar_vel2022.nc')
plt.savefig("oscar_vel2022.png", dpi=150)
plt.show()