CDIPpy Example - Compendium Plot¶
The following example runs an application of the CDIPpy python library to create plots of significant wave height (Hs), peak period (Tp) and peak direction (Dp) for a single station for a given month on 3 separate plots. The format of this plot is consistent with the original USACE wave parameters plot known as the wave compendium plot. Plotting all 3 parameters simultaneously gives a concise display of the general wave environment for the month.
- read in CDIP station metadata,
- access wave data for time period of interest,
- plot timeseries,
Import Libraries¶
Start by importing the necessary python packages and CDIPPY module
In [1]:
Copied!
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import time
import calendar
# CDIP imports
import cdippy
from cdippy.stndata import StnData
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import time
import calendar
# CDIP imports
import cdippy
from cdippy.stndata import StnData
Initialize CDIPpy input parameters¶
- Supply CDIP station id
- Start date (YYYYMMDDHH)
- End date
- Station parameters
In [2]:
Copied!
##- Initialize station id, start/end date, and parameters
stn = '100p1'
sdate = '20210201040201'
edate = '20220601040201'
params = ['waveHs','waveTp','waveDp']
##- Initialize station id, start/end date, and parameters
stn = '100p1'
sdate = '20210201040201'
edate = '20220601040201'
params = ['waveHs','waveTp','waveDp']
Data Access¶
- Use cdippy.stndata function StnData(stn)
- Returns StnData object
- Access station metadata about individual stations deployments
In [3]:
Copied!
##- Get Station Dataset object
stn_data = StnData(stn)
##- Get metadata (i.e. information about individual deployments)
meta = stn_data.get_stn_meta()
stn_name = meta['metaStationName']
print(stn_name)
##- Get Station Dataset object
stn_data = StnData(stn)
##- Get metadata (i.e. information about individual deployments)
meta = stn_data.get_stn_meta()
stn_name = meta['metaStationName']
print(stn_name)
TORREY PINES OUTER, CA BUOY - 100p1
Grab data using 'get_series' function¶
Will return a dictionary of arrays for each parameter as keys
stn_data.get_series(start, end, params)
- start: start datetime (datetime obj)
- end: end datetime (datetime obj)
- params: list of parameters
In [4]:
Copied!
##- Use CDIPPY to convert input start/end date strings to datetime objects
start = cdippy.utils.utils.cdip_datetime(sdate)
end = cdippy.utils.utils.cdip_datetime(edate)
##- Grab data using 'get_series' function
data = stn_data.get_series(start, end, params)
data.keys()
##- Use CDIPPY to convert input start/end date strings to datetime objects
start = cdippy.utils.utils.cdip_datetime(sdate)
end = cdippy.utils.utils.cdip_datetime(edate)
##- Grab data using 'get_series' function
data = stn_data.get_series(start, end, params)
data.keys()
Out[4]:
dict_keys(['waveDp', 'waveTime', 'waveTp', 'waveHs'])
Convert waveTimes to Datetime object¶
In [5]:
Copied!
## Convert wave times to datetime objects (currently integers)
##- Convert times to datetime objects
wT = [cdippy.utils.utils.timestamp_to_datetime(x) for x in data['waveTime']]
## Convert wave times to datetime objects (currently integers)
##- Convert times to datetime objects
wT = [cdippy.utils.utils.timestamp_to_datetime(x) for x in data['waveTime']]
Create a Pandas Dataframe indexed by time¶
In [6]:
Copied!
## Create pandas dataframe from data
df = pd.DataFrame({'datetime': wT, 'Hs': data['waveHs'].data})
df.set_index('datetime', inplace=True)
# Add a "Month" column for grouping
df['Month'] = df.index.month
df.head()
## Create pandas dataframe from data
df = pd.DataFrame({'datetime': wT, 'Hs': data['waveHs'].data})
df.set_index('datetime', inplace=True)
# Add a "Month" column for grouping
df['Month'] = df.index.month
df.head()
Out[6]:
Hs | Month | |
---|---|---|
datetime | ||
2021-02-01 04:30:00 | 0.65 | 2 |
2021-02-01 05:00:00 | 0.64 | 2 |
2021-02-01 05:30:00 | 0.66 | 2 |
2021-02-01 06:00:00 | 0.65 | 2 |
2021-02-01 06:30:00 | 0.68 | 2 |
Create Compendium plot¶
In [7]:
Copied!
# Crete figure and specify subplot orientation (3 rows, 1 column), shared x-axis, and figure size
f, (pHs, pTp, pDp) = plt.subplots(3, 1, sharex=True, figsize=(15,10))
# Create 3 stacked subplots for three PARAMETERS (Hs, Tp, Dp)
pHs.plot(wT,data['waveHs'],'b', linewidth="0.5")
pTp.plot(wT,data['waveTp'],'b', linewidth="0.5")
pDp.scatter(wT,data['waveDp'],color='blue',s=3) # Plot Dp variable as a scatterplot, rather than line
# Set Titles
plt.suptitle(stn_name, fontsize=30, y=0.99)
plt.title(start.strftime("%Y%m%d")+'-'+end.strftime("%Y%m%d"), fontsize=20, y=3.45)
# Set tick parameters
#pHs.set_xticklabels(['1','6','11','16','21','26','31'])
pHs.tick_params(axis='y', which='major', labelsize=12, right='off')
pHs.tick_params(axis='x', which='major', labelsize=12, top='off')
# Make a second y-axis for the Hs plot, to show values in both meters and feet
pHs2 = pHs.twinx()
# Set y-axis limits for each plot
pHs.set_ylim(0,8)
pHs2.set_ylim(0,25)
pTp.set_ylim(0,28)
pDp.set_ylim(0,360)
# Label each y-axis
pHs.set_ylabel('Hs, m', fontsize=18)
pHs2.set_ylabel('Hs, ft', fontsize=18)
pTp.set_ylabel('Tp, s', fontsize=18)
pDp.set_ylabel('Dp, deg', fontsize=18)
# Plot dashed gridlines
pHs.grid(linestyle='--')
pTp.grid(linestyle='--')
pDp.grid(linestyle='--')
# Crete figure and specify subplot orientation (3 rows, 1 column), shared x-axis, and figure size
f, (pHs, pTp, pDp) = plt.subplots(3, 1, sharex=True, figsize=(15,10))
# Create 3 stacked subplots for three PARAMETERS (Hs, Tp, Dp)
pHs.plot(wT,data['waveHs'],'b', linewidth="0.5")
pTp.plot(wT,data['waveTp'],'b', linewidth="0.5")
pDp.scatter(wT,data['waveDp'],color='blue',s=3) # Plot Dp variable as a scatterplot, rather than line
# Set Titles
plt.suptitle(stn_name, fontsize=30, y=0.99)
plt.title(start.strftime("%Y%m%d")+'-'+end.strftime("%Y%m%d"), fontsize=20, y=3.45)
# Set tick parameters
#pHs.set_xticklabels(['1','6','11','16','21','26','31'])
pHs.tick_params(axis='y', which='major', labelsize=12, right='off')
pHs.tick_params(axis='x', which='major', labelsize=12, top='off')
# Make a second y-axis for the Hs plot, to show values in both meters and feet
pHs2 = pHs.twinx()
# Set y-axis limits for each plot
pHs.set_ylim(0,8)
pHs2.set_ylim(0,25)
pTp.set_ylim(0,28)
pDp.set_ylim(0,360)
# Label each y-axis
pHs.set_ylabel('Hs, m', fontsize=18)
pHs2.set_ylabel('Hs, ft', fontsize=18)
pTp.set_ylabel('Tp, s', fontsize=18)
pDp.set_ylabel('Dp, deg', fontsize=18)
# Plot dashed gridlines
pHs.grid(linestyle='--')
pTp.grid(linestyle='--')
pDp.grid(linestyle='--')
In [ ]:
Copied!