|||

tfv 1.0.7 documentation

Quick search

  • API Reference
  • Examples
    • Tutorial 1. TUFLOW FV Post-Processing Using Xarray
    • Tutorial 2. Introductory Matplotlib Plot Composition
    • Tutorial 3: Working With Gridded Boundary Condition Data
    • Gallery 1: Timeseries Plots
    • Gallery 2: Profile Plots
    • Gallery 3: Working with Sheets
    • Gallery 4: Curtain Plots
    • Gallery 5: Combined Plots
    • Gallery 6: Particle Tracking Plots
    • Gallery 7: Miscellaneous

Gallery 1: Timeseries Plots¶

This notebook provides a series of timeseries examples used in combination with the TUFLOW FV Python Toolbox (tfv) package. To follow along on your own computer, please download the demonstration notebooks from the TUFLOW Downloads Page. Look for the TUFLOW FV Python Toolbox download. Installation instructions are provided on our TUFLOW FV Python Toolbox Wiki Page.

import xarray as xr  # We utilise xarray to do all the heavy lifting 
import tfv.xarray
from pathlib import Path # We'll also make use of the `pathlib` module to assist with managing file-paths, although this is entirely optional! 
import matplotlib.pyplot as plt

Open TUFLOW FV Model Result¶

model_folder = Path(r'..\..\data')
model_file = 'HYD_002.nc'

fv = xr.open_dataset(model_folder / model_file, decode_times=False).tfv
#fv  # Uncomment this to display the data variables

Extract Time Series Data¶

locs = {
    'P1' : (159.0758, -31.3638),
    'P2' : (159.0845,  -31.3727),
    'P3' : (159.0906,  -31.3814),
    'P4' : (159.1001,  -31.3948),
    'P5' : (159.1154,  -31.4032),
    'P6' : (159.1266,  -31.4105),
    'P7' : (159.1202,  -31.4165),
    'P8' : (159.1178,  -31.4236),
}

ts = fv.get_timeseries(['H', 'V', 'TEMP'],locs)
#ts  # Uncomment this to display the data variables
Extracting timeseries, please wait: 100%|████████████████████████████████████████████| 145/145 [00:01<00:00, 87.48it/s]

Plot Single Time Series¶

Plot the depth averaged velocity at the point P2.

ts['V'].sel(Location='P2').plot()
[<matplotlib.lines.Line2D at 0x277a513c1d0>]
../../../_images/3b04d0b6395cd51ab1d9330f2fd1052f723b5c32b9b51a624ce98fd2194bd235.png

Trim the Time to Plot¶

ts['V'].sel(Time=slice('2011-05-01','2011-05-03'), Location='P2').plot()
[<matplotlib.lines.Line2D at 0x277a796a710>]
../../../_images/c15e34410291b8d0fa319256a828eab16e5d3f654c7488f11db4f9a41d05d30d.png

Plot Time Series at Multiple Locations¶

# Plot the time series 
ts['V'].sel(Location='P1').plot(color='red', linestyle='--')
ts['V'].sel(Location='P2').plot(color='blue')

# Tidy up the plot
plt.title('Velocity at P1 and P2')
plt.grid()
plt.ylim(0,0.2)
plt.show()

# Or you can try this for something quick
ts['V'].sel(Location=['P1','P2']).plot.line(x='Time', hue='Location')
../../../_images/a0c7bde618e5e8d64b7c3048f6a2c3b55b25856fb90febb976ceb061021a8782.png
[<matplotlib.lines.Line2D at 0x277a7ce7f50>,
 <matplotlib.lines.Line2D at 0x277a7cf8390>]
../../../_images/405b0589f24b78783422e9a74de9f2c144fe3da76a8c0e57d7425afee408fa1d.png

Plot Time Series at All Locations¶

ts['V'].plot.line(x='Time', col='Location',col_wrap=4)
<xarray.plot.facetgrid.FacetGrid at 0x277a7d07d90>
../../../_images/86d2585b05ffb183fd08edf74599fd7e9f3f32d1982cc1154d29f2c57abd513e.png

Plot Times Series at Multiple Depths and Save to Disk¶

# Extract the data at different depths (see also height, elevation and depth datum methods)
ts_dave = fv.get_timeseries(['V', 'TEMP','SAL'],locs,datum='sigma',limits=(0,1))  #   Full depth averaged
ts_bot_10pct = fv.get_timeseries(['V', 'TEMP','SAL'],locs,datum='sigma',limits=(0,0.1))  # 10% of water depth near bed
ts_top_10pct = fv.get_timeseries(['V', 'TEMP','SAL'],locs,datum='sigma',limits=(0.9,1))  # 10% of water depth near surface
Extracting timeseries, please wait: 100%|████████████████████████████████████████████| 145/145 [00:01<00:00, 81.87it/s]
Extracting timeseries, please wait: 100%|████████████████████████████████████████████| 145/145 [00:01<00:00, 92.42it/s]
Extracting timeseries, please wait: 100%|████████████████████████████████████████████| 145/145 [00:01<00:00, 91.38it/s]
# Plot the salinity timeseries in three colours on a plot
fig, ax = plt.subplots(figsize=(12,6))
ts_dave['SAL'].sel(Location='P2').plot(ax=ax, label='Full depth averaged')
ts_bot_10pct['SAL'].sel(Location='P2').plot(ax=ax, label='10% of water depth near bed')
ts_top_10pct['SAL'].sel(Location='P2').plot(ax=ax, label='10% of water depth near surface')

ax.legend()
ax.set_ylabel('Salinity (psu)')
ax.set_xlabel('Time')
ax.set_title('Salinity Timeseries at Different Depths')
ax.grid(color='grey', linestyle='--', linewidth=0.5)

plt.show()

# Save the plot to a jpg file
fig.savefig(Path('./plots') / 'salinity_timeseries.jpg', dpi=300)
../../../_images/a2a7355457b3852f5beb99da9038d427eac77afc0bc7aaff3fbfe2d51168810b.png

Convert to Pandas Dataframe¶

ts_df = ts.to_dataframe()
# ts_df.head()  # Uncomment if you'd like to see a description of the pandas DataFrame

This concludes the examples on time series plotting.

<Page contents

>Page contents:

  • Gallery 1: Timeseries Plots
    • Open TUFLOW FV Model Result
    • Extract Time Series Data
    • Plot Single Time Series
    • Trim the Time to Plot
    • Plot Time Series at Multiple Locations
    • Plot Time Series at All Locations
    • Plot Times Series at Multiple Depths and Save to Disk
    • Convert to Pandas Dataframe
<Tutorial 3: Working With Gridded Boundary Condition Data
Gallery 2: Profile Plots>
© Copyright 2023 BMT. Created using Sphinx 7.2.6.

Styled using the Piccolo Theme