Echoregions Regions2D Plotting Demonstration#

Prior to running this notebook and all other notebooks, make sure you have installed the packages found in requirements.txt.

This notebook demonstrates some of the functionalities of echoregions to read Echoview region .evr files and visualize regions.

import matplotlib.pyplot as plt
import xarray as xr
from datetime import timedelta
import echoregions as er
import warnings
import gdown
import urllib.request
warnings.filterwarnings("ignore", category=DeprecationWarning)
# download an example file
urllib.request.urlretrieve("https://raw.githubusercontent.com/OSOceanAcoustics/echoregions/main/echoregions/test_data/x1.evr","x1.evr")
EVR_FILE = 'x1.evr'

Get a Regions2D object with read_evr#

r2d = er.read_evr(EVR_FILE)

Plotting#

# Display availible regions
print(r2d.data.region_id.values)
<IntegerArray>
[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 32, 33, 34, 35]
Length: 33, dtype: Int64
# let's select one id
region_id = 11
# Plot a region with a specific id with the `plot` function
r2d.plot(region_id)
_images/f4ca8a3e5f5bab4f4ac113aeeb49d886edd450b26c50b0827e80b9b42d2ea2fe.png
# Plot a closed region by using close_region=True.
# Optionally provide matplotlib kwargs for more customization.
r2d.plot(region_id, close_region=True, color='k', alpha=.5, marker='x', markeredgecolor='red', markersize=12)
_images/ccddf506d86fcd635c797a1202882de7225347e06a97b60d279fec6ee12a4c19.png

Plotting regions on an echogram#

Reading Preprocessed Sonar Files#

We have converted and calibrated a sample of echosounder files from the same transect and stored them in .nc. We can directly read them with the xarray library.

# mounting the google drive (uncomment if you have permission to read directly from Google Drive)
# from google.colab import drive
# drive.mount('/content/drive/') 
# Paths for Google Drive read  (uncomment if you have permission to read directly from Google Drive)
# SONAR_PATH_Sv = '/content/drive/Shareddrives/uw-echospace/shared_data/SH1707/sample/Sv/'
# SONAR_PATH_raw = '/content/drive/Shareddrives/uw-echospace/shared_data/SH1707/sample/raw_converted/'
# download a zipped sample folder from publicly available Google Drive
url = 'https://drive.google.com/uc?id=1OhYVcakCEgXEKA4R9za4jvBWQUFIOnE5'
output = 'x1.tar.gz'
gdown.download(url, output, quiet=False)
# unzip into a sample folder
!tar -xvzf  x1.tar.gz
ds = xr.open_dataset("x1.zarr")
c:\Users\cmtug\OneDrive\Documents\GitHub\echoregions\.conda\lib\site-packages\xarray\backends\plugins.py:139: RuntimeWarning: 'netcdf4' fails while guessing
  warnings.warn(f"{engine!r} fails while guessing", RuntimeWarning)
c:\Users\cmtug\OneDrive\Documents\GitHub\echoregions\.conda\lib\site-packages\xarray\backends\plugins.py:139: RuntimeWarning: 'h5netcdf' fails while guessing
  warnings.warn(f"{engine!r} fails while guessing", RuntimeWarning)
c:\Users\cmtug\OneDrive\Documents\GitHub\echoregions\.conda\lib\site-packages\xarray\backends\plugins.py:139: RuntimeWarning: 'scipy' fails while guessing
  warnings.warn(f"{engine!r} fails while guessing", RuntimeWarning)

The dataset has a range_sample dimension and instead we convert it to a depth dimension by adjusting the water level.

# create depth coordinate:
echo_range = ds.echo_range.isel(channel=0, ping_time=0)
# assuming water levels are same for different frequencies and location_time
depth = ds.water_level.isel(channel=0, ping_time=0) + echo_range
depth = depth.drop_vars('channel')
# creating a new depth dimension
ds['depth'] = depth
ds = ds.swap_dims({'range_sample': 'depth'})
ds.Sv
<xarray.DataArray 'Sv' (channel: 3, ping_time: 13192, depth: 3957)>
[156602232 values with dtype=float64]
Coordinates:
  * channel       (channel) object 'GPT  18 kHz 009072058c8d 1-1 ES18-11' ......
  * depth         (depth) float64 9.15 9.15 9.15 9.34 ... 758.1 758.3 758.5
  * ping_time     (ping_time) datetime64[ns] 2017-06-25T15:04:30.076000256 .....
    range_sample  (depth) int64 0 1 2 3 4 5 6 ... 3951 3952 3953 3954 3955 3956

Plotting#

# plot Sv
ds.Sv.isel(channel=0).plot(x='ping_time', vmax=-40, vmin=-100, yincrease=False, figsize=(20,8))
# plot region
r2d.plot(region_id, close_region=True, color='k')
plt.show()
_images/ebc1504bd7391403f2d7e758e5a4ddac6e121c464503407bdbbf9f69489045e4.png

Let’s limit the time extent of the echogram so that we can see better the region.

# we will create a 15 minute window around the bounding box of the region
bbox_right = r2d.data[r2d.data.region_id==region_id].region_bbox_right.iloc[0] + timedelta(minutes = 15)
bbox_left = r2d.data[r2d.data.region_id==region_id].region_bbox_left.iloc[0] - timedelta(minutes = 15)
# plot Sv
ds.Sv.isel(channel=0).sel(ping_time=slice(bbox_left, bbox_right)).plot(x='ping_time', vmax=-40, vmin=-100, yincrease=False, figsize=(20,8))
# plot region
r2d.plot(region_id, close_region=True, color='k')
plt.show()
_images/1bc8e5646455b6e795023c22734b9a524322900442670b5784628ac65998308d.png