import cmocean.cm as cm
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
#from salishsea_tools import viz_tools
First lets open the file that has the physical tracers in (salinity called vosaline and temperature called votemper)
phys = xr.open_dataset('/results2/SalishSea/nowcast-green.201905/29sep22/SalishSea_1h_20220929_20220929_grid_T.nc')
phys
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) File ~/opt/anaconda3/envs/analysis-camryn/lib/python3.10/site-packages/xarray/backends/file_manager.py:201, in CachingFileManager._acquire_with_cache_info(self, needs_lock) 200 try: --> 201 file = self._cache[self._key] 202 except KeyError: File ~/opt/anaconda3/envs/analysis-camryn/lib/python3.10/site-packages/xarray/backends/lru_cache.py:55, in LRUCache.__getitem__(self, key) 54 with self._lock: ---> 55 value = self._cache[key] 56 self._cache.move_to_end(key) KeyError: [<class 'netCDF4._netCDF4.Dataset'>, ('/results2/SalishSea/nowcast-green.201905/29sep22/SalishSea_1h_20220929_20220929_grid_T.nc',), 'r', (('clobber', True), ('diskless', False), ('format', 'NETCDF4'), ('persist', False))] During handling of the above exception, another exception occurred: FileNotFoundError Traceback (most recent call last) Cell In [3], line 1 ----> 1 phys = xr.open_dataset('/results2/SalishSea/nowcast-green.201905/29sep22/SalishSea_1h_20220929_20220929_grid_T.nc') 2 phys File ~/opt/anaconda3/envs/analysis-camryn/lib/python3.10/site-packages/xarray/backends/api.py:531, in open_dataset(filename_or_obj, engine, chunks, cache, decode_cf, mask_and_scale, decode_times, decode_timedelta, use_cftime, concat_characters, decode_coords, drop_variables, inline_array, backend_kwargs, **kwargs) 519 decoders = _resolve_decoders_kwargs( 520 decode_cf, 521 open_backend_dataset_parameters=backend.open_dataset_parameters, (...) 527 decode_coords=decode_coords, 528 ) 530 overwrite_encoded_chunks = kwargs.pop("overwrite_encoded_chunks", None) --> 531 backend_ds = backend.open_dataset( 532 filename_or_obj, 533 drop_variables=drop_variables, 534 **decoders, 535 **kwargs, 536 ) 537 ds = _dataset_from_backend_dataset( 538 backend_ds, 539 filename_or_obj, (...) 547 **kwargs, 548 ) 549 return ds File ~/opt/anaconda3/envs/analysis-camryn/lib/python3.10/site-packages/xarray/backends/netCDF4_.py:555, in NetCDF4BackendEntrypoint.open_dataset(self, filename_or_obj, mask_and_scale, decode_times, concat_characters, decode_coords, drop_variables, use_cftime, decode_timedelta, group, mode, format, clobber, diskless, persist, lock, autoclose) 534 def open_dataset( 535 self, 536 filename_or_obj, (...) 551 autoclose=False, 552 ): 554 filename_or_obj = _normalize_path(filename_or_obj) --> 555 store = NetCDF4DataStore.open( 556 filename_or_obj, 557 mode=mode, 558 format=format, 559 group=group, 560 clobber=clobber, 561 diskless=diskless, 562 persist=persist, 563 lock=lock, 564 autoclose=autoclose, 565 ) 567 store_entrypoint = StoreBackendEntrypoint() 568 with close_on_error(store): File ~/opt/anaconda3/envs/analysis-camryn/lib/python3.10/site-packages/xarray/backends/netCDF4_.py:384, in NetCDF4DataStore.open(cls, filename, mode, format, group, clobber, diskless, persist, lock, lock_maker, autoclose) 378 kwargs = dict( 379 clobber=clobber, diskless=diskless, persist=persist, format=format 380 ) 381 manager = CachingFileManager( 382 netCDF4.Dataset, filename, mode=mode, kwargs=kwargs 383 ) --> 384 return cls(manager, group=group, mode=mode, lock=lock, autoclose=autoclose) File ~/opt/anaconda3/envs/analysis-camryn/lib/python3.10/site-packages/xarray/backends/netCDF4_.py:332, in NetCDF4DataStore.__init__(self, manager, group, mode, lock, autoclose) 330 self._group = group 331 self._mode = mode --> 332 self.format = self.ds.data_model 333 self._filename = self.ds.filepath() 334 self.is_remote = is_remote_uri(self._filename) File ~/opt/anaconda3/envs/analysis-camryn/lib/python3.10/site-packages/xarray/backends/netCDF4_.py:393, in NetCDF4DataStore.ds(self) 391 @property 392 def ds(self): --> 393 return self._acquire() File ~/opt/anaconda3/envs/analysis-camryn/lib/python3.10/site-packages/xarray/backends/netCDF4_.py:387, in NetCDF4DataStore._acquire(self, needs_lock) 386 def _acquire(self, needs_lock=True): --> 387 with self._manager.acquire_context(needs_lock) as root: 388 ds = _nc4_require_group(root, self._group, self._mode) 389 return ds File ~/opt/anaconda3/envs/analysis-camryn/lib/python3.10/contextlib.py:135, in _GeneratorContextManager.__enter__(self) 133 del self.args, self.kwds, self.func 134 try: --> 135 return next(self.gen) 136 except StopIteration: 137 raise RuntimeError("generator didn't yield") from None File ~/opt/anaconda3/envs/analysis-camryn/lib/python3.10/site-packages/xarray/backends/file_manager.py:189, in CachingFileManager.acquire_context(self, needs_lock) 186 @contextlib.contextmanager 187 def acquire_context(self, needs_lock=True): 188 """Context manager for acquiring a file.""" --> 189 file, cached = self._acquire_with_cache_info(needs_lock) 190 try: 191 yield file File ~/opt/anaconda3/envs/analysis-camryn/lib/python3.10/site-packages/xarray/backends/file_manager.py:207, in CachingFileManager._acquire_with_cache_info(self, needs_lock) 205 kwargs = kwargs.copy() 206 kwargs["mode"] = self._mode --> 207 file = self._opener(*self._args, **kwargs) 208 if self._mode == "w": 209 # ensure file doesn't get overridden when opened again 210 self._mode = "a" File src/netCDF4/_netCDF4.pyx:2463, in netCDF4._netCDF4.Dataset.__init__() File src/netCDF4/_netCDF4.pyx:2026, in netCDF4._netCDF4._ensure_nc_success() FileNotFoundError: [Errno 2] No such file or directory: b'/results2/SalishSea/nowcast-green.201905/29sep22/SalishSea_1h_20220929_20220929_grid_T.nc'
And look at the specific variables. Note the full names and the units
phys.vosaline
<xarray.DataArray 'vosaline' (time_counter: 24, deptht: 40, y: 898, x: 398)> [343107840 values with dtype=float32] Coordinates: nav_lat (y, x) float32 ... nav_lon (y, x) float32 ... * deptht (deptht) float32 0.5 1.5 2.5 3.5 ... 360.7 387.6 414.5 441.5 time_centered (time_counter) datetime64[ns] 2022-09-29T00:30:00 ... 2022... * time_counter (time_counter) datetime64[ns] 2022-09-29T00:30:00 ... 2022... Dimensions without coordinates: y, x Attributes: standard_name: sea_water_reference_salinity long_name: salinity units: g kg-1 online_operation: average interval_operation: 40 s interval_write: 1 h cell_methods: time: mean (interval: 40 s) cell_measures: area: area
[343107840 values with dtype=float32]
[357404 values with dtype=float32]
[357404 values with dtype=float32]
array([ 0.5 , 1.500003, 2.500011, 3.500031, 4.500071, 5.500151, 6.50031 , 7.500623, 8.501236, 9.502433, 10.504766, 11.509312, 12.518167, 13.535412, 14.568982, 15.634288, 16.761173, 18.007135, 19.481785, 21.389978, 24.100256, 28.229916, 34.685757, 44.517723, 58.484333, 76.58559 , 98.06296 , 121.866516, 147.08946 , 173.11449 , 199.57304 , 226.2603 , 253.06664 , 279.93454 , 306.8342 , 333.75018 , 360.67453 , 387.6032 , 414.5341 , 441.4661 ], dtype=float32)
array(['2022-09-29T00:30:00.000000000', '2022-09-29T01:30:00.000000000', '2022-09-29T02:30:00.000000000', '2022-09-29T03:30:00.000000000', '2022-09-29T04:30:00.000000000', '2022-09-29T05:30:00.000000000', '2022-09-29T06:30:00.000000000', '2022-09-29T07:30:00.000000000', '2022-09-29T08:30:00.000000000', '2022-09-29T09:30:00.000000000', '2022-09-29T10:30:00.000000000', '2022-09-29T11:30:00.000000000', '2022-09-29T12:30:00.000000000', '2022-09-29T13:30:00.000000000', '2022-09-29T14:30:00.000000000', '2022-09-29T15:30:00.000000000', '2022-09-29T16:30:00.000000000', '2022-09-29T17:30:00.000000000', '2022-09-29T18:30:00.000000000', '2022-09-29T19:30:00.000000000', '2022-09-29T20:30:00.000000000', '2022-09-29T21:30:00.000000000', '2022-09-29T22:30:00.000000000', '2022-09-29T23:30:00.000000000'], dtype='datetime64[ns]')
array(['2022-09-29T00:30:00.000000000', '2022-09-29T01:30:00.000000000', '2022-09-29T02:30:00.000000000', '2022-09-29T03:30:00.000000000', '2022-09-29T04:30:00.000000000', '2022-09-29T05:30:00.000000000', '2022-09-29T06:30:00.000000000', '2022-09-29T07:30:00.000000000', '2022-09-29T08:30:00.000000000', '2022-09-29T09:30:00.000000000', '2022-09-29T10:30:00.000000000', '2022-09-29T11:30:00.000000000', '2022-09-29T12:30:00.000000000', '2022-09-29T13:30:00.000000000', '2022-09-29T14:30:00.000000000', '2022-09-29T15:30:00.000000000', '2022-09-29T16:30:00.000000000', '2022-09-29T17:30:00.000000000', '2022-09-29T18:30:00.000000000', '2022-09-29T19:30:00.000000000', '2022-09-29T20:30:00.000000000', '2022-09-29T21:30:00.000000000', '2022-09-29T22:30:00.000000000', '2022-09-29T23:30:00.000000000'], dtype='datetime64[ns]')
phys.votemper
<xarray.DataArray 'votemper' (time_counter: 24, deptht: 40, y: 898, x: 398)> [343107840 values with dtype=float32] Coordinates: nav_lat (y, x) float32 ... nav_lon (y, x) float32 ... * deptht (deptht) float32 0.5 1.5 2.5 3.5 ... 360.7 387.6 414.5 441.5 time_centered (time_counter) datetime64[ns] 2022-09-29T00:30:00 ... 2022... * time_counter (time_counter) datetime64[ns] 2022-09-29T00:30:00 ... 2022... Dimensions without coordinates: y, x Attributes: standard_name: sea_water_conservative_temperature long_name: temperature units: degC online_operation: average interval_operation: 40 s interval_write: 1 h cell_methods: time: mean (interval: 40 s) cell_measures: area: area
[343107840 values with dtype=float32]
[357404 values with dtype=float32]
[357404 values with dtype=float32]
array([ 0.5 , 1.500003, 2.500011, 3.500031, 4.500071, 5.500151, 6.50031 , 7.500623, 8.501236, 9.502433, 10.504766, 11.509312, 12.518167, 13.535412, 14.568982, 15.634288, 16.761173, 18.007135, 19.481785, 21.389978, 24.100256, 28.229916, 34.685757, 44.517723, 58.484333, 76.58559 , 98.06296 , 121.866516, 147.08946 , 173.11449 , 199.57304 , 226.2603 , 253.06664 , 279.93454 , 306.8342 , 333.75018 , 360.67453 , 387.6032 , 414.5341 , 441.4661 ], dtype=float32)
array(['2022-09-29T00:30:00.000000000', '2022-09-29T01:30:00.000000000', '2022-09-29T02:30:00.000000000', '2022-09-29T03:30:00.000000000', '2022-09-29T04:30:00.000000000', '2022-09-29T05:30:00.000000000', '2022-09-29T06:30:00.000000000', '2022-09-29T07:30:00.000000000', '2022-09-29T08:30:00.000000000', '2022-09-29T09:30:00.000000000', '2022-09-29T10:30:00.000000000', '2022-09-29T11:30:00.000000000', '2022-09-29T12:30:00.000000000', '2022-09-29T13:30:00.000000000', '2022-09-29T14:30:00.000000000', '2022-09-29T15:30:00.000000000', '2022-09-29T16:30:00.000000000', '2022-09-29T17:30:00.000000000', '2022-09-29T18:30:00.000000000', '2022-09-29T19:30:00.000000000', '2022-09-29T20:30:00.000000000', '2022-09-29T21:30:00.000000000', '2022-09-29T22:30:00.000000000', '2022-09-29T23:30:00.000000000'], dtype='datetime64[ns]')
array(['2022-09-29T00:30:00.000000000', '2022-09-29T01:30:00.000000000', '2022-09-29T02:30:00.000000000', '2022-09-29T03:30:00.000000000', '2022-09-29T04:30:00.000000000', '2022-09-29T05:30:00.000000000', '2022-09-29T06:30:00.000000000', '2022-09-29T07:30:00.000000000', '2022-09-29T08:30:00.000000000', '2022-09-29T09:30:00.000000000', '2022-09-29T10:30:00.000000000', '2022-09-29T11:30:00.000000000', '2022-09-29T12:30:00.000000000', '2022-09-29T13:30:00.000000000', '2022-09-29T14:30:00.000000000', '2022-09-29T15:30:00.000000000', '2022-09-29T16:30:00.000000000', '2022-09-29T17:30:00.000000000', '2022-09-29T18:30:00.000000000', '2022-09-29T19:30:00.000000000', '2022-09-29T20:30:00.000000000', '2022-09-29T21:30:00.000000000', '2022-09-29T22:30:00.000000000', '2022-09-29T23:30:00.000000000'], dtype='datetime64[ns]')
A naive plot, note that we have chosen the second time step (1 == 01:30 UTC) and the near surface (0 == 0.5 m depth)
phys.vosaline[1, 0].plot();
Okay but we could do better.
# set up a proper set of axes and chose a better size
fig, ax = plt.subplots(1, 1, figsize=(5, 9))
phys.vosaline[1, 0].plot(ax=ax);
# fix the aspect ratio
viz_tools.set_aspect(ax);
# open the mesh mask and use it to mask the land
mesh = xr.open_dataset('/home/sallen/MEOPAR/grid/mesh_mask202108.nc')
# the mask is 1 where there is water, we want the opposite. The meshmask has an extra dimension, hence the [0]
tmask = 1 - mesh.tmask[0]
fig, ax = plt.subplots(1, 1, figsize=(5, 9))
salinity = np.ma.masked_array(phys.vosaline[1, 0], mask=tmask[0])
ax.pcolormesh(salinity)
viz_tools.set_aspect(ax);
# note that switching to pcolormesh from xarray we lost our colorbar and our title
# Here I will also improve the colormap
cmap = cm.haline
cmap.set_bad('gray')
fig, ax = plt.subplots(1, 1, figsize=(5, 9))
salinity = np.ma.masked_array(phys.vosaline[1, 0], mask=tmask[0])
colours = ax.pcolormesh(salinity, cmap=cmap)
cb = fig.colorbar(colours, ax=ax)
cb.set_label('Salinity (g/kg)')
viz_tools.set_aspect(ax);
# Zoom in on Rosario Strait and the Nooksack River
fig, ax = plt.subplots(1, 1, figsize=(7, 7))
colours = ax.pcolormesh(salinity[280:380, 280:380], cmap=cmap)
cb = fig.colorbar(colours, ax=ax)
cb.set_label('Salinity (g/kg)')
viz_tools.set_aspect(ax);
# Now do a vertical cross-section through Rosario Strait and into Bellingham Bay
fig, ax = plt.subplots(1, 1, figsize=(15, 5))
salinity = np.ma.masked_array(phys.vosaline[1, :, 305, 300:380], mask=tmask[:, 305, 300:380])
colours = ax.pcolormesh(phys.nav_lon[305, 300:380+1], phys.deptht, salinity[:39], cmap=cmap)
ax.set_ylim(0, 150)
ax.invert_yaxis();
cb = fig.colorbar(colours, ax=ax)
cb.set_label('Salinity (g/kg)');
# Velocity across the cross-section
vvel = xr.open_dataset('/results2/SalishSea/nowcast-green.201905/29sep22/SalishSea_1h_20220929_20220929_grid_V.nc')
vmask = 1 - mesh.vmask[0]
fig, ax = plt.subplots(1, 1, figsize=(15, 5))
cmap = cm.curl
cmap.set_bad('grey')
northvel = np.ma.masked_array(vvel.vomecrty[1, :, 305, 300:380], mask=vmask[:, 305, 300:380])
colours = ax.pcolormesh(vvel.nav_lon[305, 300:380+1], vvel.depthv, northvel[:39], cmap=cmap, vmax=1.0, vmin=-1.0)
ax.set_ylim(0, 150)
ax.invert_yaxis();
cb = fig.colorbar(colours, ax=ax)
cb.set_label('Velocity (m/s)');
# and six hours later
fig, ax = plt.subplots(1, 1, figsize=(15, 5))
northvel = np.ma.masked_array(vvel.vomecrty[7, :, 305, 300:380], mask=vmask[:, 305, 300:380])
colours = ax.pcolormesh(vvel.nav_lon[305, 300:380+1], vvel.depthv, northvel[:39], cmap=cmap, vmax=1.0, vmin=-1.0)
ax.set_ylim(0, 150)
ax.invert_yaxis();
cb = fig.colorbar(colours, ax=ax)
cb.set_label('Velocity (m/s)');
# file for 2017 Sep 29 (Sorry, its in my space not on /results)
river = xr.open_dataset('/home/sallen/MEOPAR/tools/I_ForcingFiles/Rivers/ncfiles/R202108Dailies_y2017m09d29.nc')
# get the bathymetry file
bathy = xr.open_dataset('/home/sallen/MEOPAR/grid/bathymetry_202108.nc')
# exclude Fraser off the edge
jmax = 394
jj = range(jmax)
ii = range(898)
jjm, iim = np.meshgrid(jj, ii)
fluxarray = np.array(river.rorunoff[0, :, :jmax])
fig, ax = plt.subplots(1, 1, figsize=(4, 9))
ax.contourf(bathy.Bathymetry[:, :jmax], cmap='winter_r')
ax.scatter(jjm[fluxarray>0], iim[fluxarray>0], s=fluxarray[fluxarray>0]*1000, color='tab:orange')
fig.suptitle(f'September 29, 2017 River Flux Excluding the Fraser River');