Notebook to play with WOD data using the wodpy package
WOD data
wodpy package
from wodpy import wod
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import netCDF4 as nc
from salishsea_tools import viz_tools, tidetools
import numpy as np
import glob
import os
import datetime
from salishsea_tools.nowcast import analyze
import comparisons
%matplotlib inline
#make plots pretty
sns.set_style('darkgrid')
f=nc.Dataset('/data/nsoontie/MEOPAR/NEMO-forcing/grid/bathy_meter_SalishSea2.nc')
bathy=f.variables['Bathymetry'][:]
X=f.variables['nav_lon'][:]
Y=f.variables['nav_lat'][:]
Experiment with functions to manipulate the data.
def read_file_to_dataframe(filename):
"""Reads a WOD file (filename) and returns data as a dataframe.
data inlcudes columns Temperature, Salinity, Depth, Year, Month, Day, Longitude, Latitude, Datetime"""
file = open(filename)
#empty list for gatherting profiles.
list_data=[]
#loop through profiles
profile = wod.WodProfile(file)
while not profile.is_last_profile_in_file(file):
year = profile.year()
lat = profile.latitude()
lon = profile.longitude()
s=profile.s()
d=profile.z()
t=profile.t()
month = profile.month()
day = profile.day()
date = datetime.datetime(year, month, day)
tmp={'Year': year, 'Month': month, 'Day': day, 'Longitude': lon, 'Latitude': lat,
'Salinity': s, 'Temperature': t, 'Depth': d, 'Datetime': date}
list_data.append(tmp)
profile = wod.WodProfile(file)
#again for last profile
year = profile.year()
lat = profile.latitude()
lon = profile.longitude()
s=profile.s()
d=profile.z()
t=profile.t()
month = profile.month()
day = profile.day()
tmp={'Year': year, 'Month': month, 'Day': day, 'Longitude': lon, 'Latitude': lat,
'Salinity': s, 'Temperature': t, 'Depth': d}
list_data.append(tmp)
#convert to data frame
data = pd.DataFrame(list_data)
return data
data = read_file_to_dataframe('/ocean/nsoontie/MEOPAR/WOD/CTDS7412')
Isolate region and plot
#define region and isolate
lon_min=-123.4; lat_min=48.3;
lon_max=-123; lat_max=48.85;
data_sog = comparisons.isolate_region(data, lon_min, lon_max, lat_min, lat_max)
Isolate time period
#define time period and isolate
sdt = datetime.datetime(2000,1,1)
edt = datetime.datetime(2015,12,31)
data_recent = comparisons.isolate_time_period(data_sog, sdt, edt)
Examine data by plotting
fig,axm = plt.subplots(1,figsize=(5,5))
data_recent.plot(x='Longitude',y='Latitude',kind='scatter', marker='o',ax=axm)
viz_tools.plot_coastline(axm,f,coords='map')
axm.set_xlim([lon_min,lon_max])
axm.set_ylim([lat_min, lat_max])
(48.3, 48.85)
data_recent.hist('Month',bins=np.arange(0.5,13.5))
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7f516ff3a8d0>]], dtype=object)
data_recent.hist('Year')
ax=plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
Plan:
paths = {'nowcast': '/data/dlatorne/MEOPAR/SalishSea/nowcast/',
'spinup': '/ocean/dlatorne/MEOPAR/SalishSea/results/spin-up/'}
zmax=200
November
month=11
fig=comparisons.compare_model_obs(month,2003,'Salinity',data_recent,paths['spinup'],zmax=zmax,vmin=24,vmax=34)
fig=comparisons.compare_model_obs(month,2014,'Salinity',data_recent,paths['nowcast'],zmax=zmax,vmin=24,vmax=34)
data_month=data_recent[data_recent['Month']==month]
data_month.hist('Year')
ax=plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
December
month=12
fig=comparisons.compare_model_obs(month,2003,'Salinity',data_recent,paths['spinup'],zmax=zmax,vmin=24,vmax=34)
fig=comparisons.compare_model_obs(month,2014,'Salinity',data_recent,paths['nowcast'],zmax=zmax,vmin=24,vmax=34)
data_month=data_recent[data_recent['Month']==month]
data_month.hist('Year')
ax=plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
January
month=1
fig=comparisons.compare_model_obs(month,2003,'Salinity',data_recent,paths['spinup'],zmax=zmax,vmin=24,vmax=34)
fig=comparisons.compare_model_obs(month,2015,'Salinity',data_recent,paths['nowcast'],zmax=zmax,vmin=24,vmax=34)
data_month=data_recent[data_recent['Month']==month]
data_month.hist('Year')
ax=plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
February
month=2
fig=comparisons.compare_model_obs(month,2003,'Salinity',data_recent,paths['spinup'],zmax=zmax, vmin=24,vmax=34)
fig=comparisons.compare_model_obs(month,2015,'Salinity',data_recent,paths['nowcast'],zmax=zmax, vmin=24,vmax=34)
data_month=data_recent[data_recent['Month']==month]
data_month.hist('Year')
ax=plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
March
month=3
fig=comparisons.compare_model_obs(month,2003,'Salinity',data_recent,paths['spinup'],zmax=zmax,vmin=24,vmax=34)
fig=comparisons.compare_model_obs(month,2015,'Salinity',data_recent,paths['nowcast'],zmax=zmax,vmin=24,vmax=34)
data_month=data_recent[data_recent['Month']==month]
data_month.hist('Year')
ax=plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
April
month=4
fig=comparisons.compare_model_obs(month,2003,'Salinity',data_recent,paths['spinup'],zmax=zmax,vmin=24,vmax=34)
fig=comparisons.compare_model_obs(month,2015,'Salinity',data_recent,paths['nowcast'],zmax=zmax, vmin=24,vmax=34)
data_month=data_recent[data_recent['Month']==month]
data_month.hist('Year')
ax=plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
Note: large April discharge in 2015 so expecting model nowcasts to be much fresher. But why is spinup so fresh?
May
month=5
fig=comparisons.compare_model_obs(month,2003,'Salinity',data_recent,paths['spinup'],zmax=zmax, vmin=24,vmax=34)
fig=comparisons.compare_model_obs(month,2015,'Salinity',data_recent,paths['nowcast'],zmax=zmax, vmin=24,vmax=34)
data_month=data_recent[data_recent['Month']==month]
data_month.hist('Year')
ax=plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
June
month=6
fig=comparisons.compare_model_obs(month,2003,'Salinity',data_recent,paths['spinup'],vmin=10,zmax=zmax,vmax=33)
fig=comparisons.compare_model_obs(month,2015,'Salinity',data_recent,paths['nowcast'],vmin=10,zmax=zmax,vmax=33)
data_month=data_recent[data_recent['Month']==month]
data_month.hist('Year')
ax=plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
Starting to get into the large discharge time. It might be harder to make fair comparisons here because there is so much interannual variability in the discharge. Will think about this...
July
month=7
fig=comparisons.compare_model_obs(month,2003,'Salinity',data_recent,paths['spinup'],vmin=10,zmax=zmax,vmax=34)
fig=comparisons.compare_model_obs(month,2015,'Salinity',data_recent,paths['nowcast'],vmin=10,zmax=zmax,vmax=34)
data_month=data_recent[data_recent['Month']==month]
data_month.hist('Year')
ax=plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
I think that some of the data overlaps with the IOS data.