{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This Script only exists as a place to load up all of the pickle files files while I am busy doing other things."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import os\n",
"import pandas as pd\n",
"import netCDF4 as nc\n",
"import datetime as dt\n",
"from salishsea_tools import evaltools as et, viz_tools\n",
"import gsw \n",
"import matplotlib.gridspec as gridspec\n",
"import matplotlib as mpl\n",
"import matplotlib.dates as mdates\n",
"import cmocean as cmo\n",
"import scipy.interpolate as sinterp\n",
"import pickle\n",
"import cmocean\n",
"import json\n",
"import f90nml\n",
"from collections import OrderedDict\n",
"from matplotlib.colors import LogNorm\n",
"\n",
"fs=16\n",
"mpl.rc('xtick', labelsize=fs)\n",
"mpl.rc('ytick', labelsize=fs)\n",
"mpl.rc('legend', fontsize=fs)\n",
"mpl.rc('axes', titlesize=fs)\n",
"mpl.rc('axes', labelsize=fs)\n",
"mpl.rc('figure', titlesize=fs)\n",
"mpl.rc('font', size=fs)\n",
"mpl.rc('font', family='sans-serif', weight='normal', style='normal')\n",
"\n",
"import warnings\n",
"#warnings.filterwarnings('ignore')\n",
"from IPython.display import Markdown, display\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"from IPython.display import HTML\n",
"\n",
"HTML('''\n",
"\n",
"
''')\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"year=2019\n",
"modelversion='nowcast-green.201905'\n",
"PATH= '/results2/SalishSea/nowcast-green.201905/'\n",
"datadir='/ocean/eolson/MEOPAR/obs/WADE/ptools_data/ecology'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### load observations"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"dfSta=pickle.load(open(os.path.join(datadir,'sta_df.p'),'rb'))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Desig | \n",
" Descrip | \n",
" Basin | \n",
" Max_Depth | \n",
" Latitude | \n",
" Longitude | \n",
"
\n",
" \n",
" Station | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" ADM001 | \n",
" C | \n",
" Admiralty Inlet - Bush Pt. | \n",
" Admiralty Inlet | \n",
" 114 | \n",
" 48.029813 | \n",
" -122.617933 | \n",
"
\n",
" \n",
" ADM002 | \n",
" C | \n",
" Admiralty Inlet (north) - Quimper Pn. | \n",
" Admiralty Inlet | \n",
" 79 | \n",
" 48.187318 | \n",
" -122.842950 | \n",
"
\n",
" \n",
" ADM003 | \n",
" C | \n",
" Admiralty Inlet (south) | \n",
" Admiralty Inlet | \n",
" 118 | \n",
" 47.878983 | \n",
" -122.483195 | \n",
"
\n",
" \n",
" BLL009 | \n",
" C | \n",
" Bellingham Bay - Pt. Frances | \n",
" Strait of Georgia | \n",
" 31 | \n",
" 48.685940 | \n",
" -122.599618 | \n",
"
\n",
" \n",
" BUD005 | \n",
" C | \n",
" Budd Inlet - Olympia Shoal | \n",
" South Basin | \n",
" 22 | \n",
" 47.092040 | \n",
" -122.918197 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Desig Descrip Basin \\\n",
"Station \n",
"ADM001 C Admiralty Inlet - Bush Pt. Admiralty Inlet \n",
"ADM002 C Admiralty Inlet (north) - Quimper Pn. Admiralty Inlet \n",
"ADM003 C Admiralty Inlet (south) Admiralty Inlet \n",
"BLL009 C Bellingham Bay - Pt. Frances Strait of Georgia \n",
"BUD005 C Budd Inlet - Olympia Shoal South Basin \n",
"\n",
" Max_Depth Latitude Longitude \n",
"Station \n",
"ADM001 114 48.029813 -122.617933 \n",
"ADM002 79 48.187318 -122.842950 \n",
"ADM003 118 47.878983 -122.483195 \n",
"BLL009 31 48.685940 -122.599618 \n",
"BUD005 22 47.092040 -122.918197 "
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dfSta.head()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"dfBot=pickle.load(open(os.path.join(datadir,f'Bottles_{str(year)}.p'),'rb'))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Station | \n",
" Date | \n",
" UTCDateTime | \n",
" Z | \n",
" PO4(uM)D | \n",
" SiOH4(uM)D | \n",
" NO3(uM)D | \n",
" NO2(uM)D | \n",
" NH4(uM)D | \n",
" DIN | \n",
" Znom | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" ADM001 | \n",
" 2019-01-08 | \n",
" 2019-01-08 19:27:00 | \n",
" -30.119 | \n",
" 2.2138 | \n",
" 52.2913 | \n",
" 26.2228 | \n",
" 0.1347 | \n",
" 0.0000 | \n",
" 26.3575 | \n",
" -30.0 | \n",
"
\n",
" \n",
" 1 | \n",
" ADM001 | \n",
" 2019-01-08 | \n",
" 2019-01-08 19:27:00 | \n",
" -10.262 | \n",
" 2.2413 | \n",
" 53.5360 | \n",
" 26.3984 | \n",
" 0.1256 | \n",
" 0.0203 | \n",
" 26.5443 | \n",
" -10.0 | \n",
"
\n",
" \n",
" 2 | \n",
" ADM001 | \n",
" 2019-01-08 | \n",
" 2019-01-08 19:27:00 | \n",
" -0.587 | \n",
" 2.2755 | \n",
" 56.0817 | \n",
" 26.6102 | \n",
" 0.1552 | \n",
" 0.0347 | \n",
" 26.8001 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 3 | \n",
" ADM001 | \n",
" 2019-02-06 | \n",
" 2019-02-06 19:04:00 | \n",
" -29.444 | \n",
" 2.2109 | \n",
" 52.4005 | \n",
" 25.5163 | \n",
" 0.1996 | \n",
" 0.0000 | \n",
" 25.7159 | \n",
" -30.0 | \n",
"
\n",
" \n",
" 4 | \n",
" ADM001 | \n",
" 2019-02-06 | \n",
" 2019-02-06 19:04:00 | \n",
" -9.722 | \n",
" 2.2208 | \n",
" 51.9906 | \n",
" 25.6077 | \n",
" 0.1979 | \n",
" 0.0000 | \n",
" 25.8056 | \n",
" -10.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Station Date UTCDateTime Z PO4(uM)D SiOH4(uM)D \\\n",
"0 ADM001 2019-01-08 2019-01-08 19:27:00 -30.119 2.2138 52.2913 \n",
"1 ADM001 2019-01-08 2019-01-08 19:27:00 -10.262 2.2413 53.5360 \n",
"2 ADM001 2019-01-08 2019-01-08 19:27:00 -0.587 2.2755 56.0817 \n",
"3 ADM001 2019-02-06 2019-02-06 19:04:00 -29.444 2.2109 52.4005 \n",
"4 ADM001 2019-02-06 2019-02-06 19:04:00 -9.722 2.2208 51.9906 \n",
"\n",
" NO3(uM)D NO2(uM)D NH4(uM)D DIN Znom \n",
"0 26.2228 0.1347 0.0000 26.3575 -30.0 \n",
"1 26.3984 0.1256 0.0203 26.5443 -10.0 \n",
"2 26.6102 0.1552 0.0347 26.8001 0.0 \n",
"3 25.5163 0.1996 0.0000 25.7159 -30.0 \n",
"4 25.6077 0.1979 0.0000 25.8056 -10.0 "
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dfBot.head()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Station | \n",
" Desig | \n",
" Descrip | \n",
" Basin | \n",
" Max_Depth | \n",
" Latitude | \n",
" Longitude | \n",
" Date | \n",
" UTCDateTime | \n",
" Z | \n",
" PO4(uM)D | \n",
" SiOH4(uM)D | \n",
" NO3(uM)D | \n",
" NO2(uM)D | \n",
" NH4(uM)D | \n",
" DIN | \n",
" Znom | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" ADM001 | \n",
" C | \n",
" Admiralty Inlet - Bush Pt. | \n",
" Admiralty Inlet | \n",
" 114 | \n",
" 48.029813 | \n",
" -122.617933 | \n",
" 2019-01-08 | \n",
" 2019-01-08 19:27:00 | \n",
" -30.119 | \n",
" 2.2138 | \n",
" 52.2913 | \n",
" 26.2228 | \n",
" 0.1347 | \n",
" 0.0000 | \n",
" 26.3575 | \n",
" -30.0 | \n",
"
\n",
" \n",
" 1 | \n",
" ADM001 | \n",
" C | \n",
" Admiralty Inlet - Bush Pt. | \n",
" Admiralty Inlet | \n",
" 114 | \n",
" 48.029813 | \n",
" -122.617933 | \n",
" 2019-01-08 | \n",
" 2019-01-08 19:27:00 | \n",
" -10.262 | \n",
" 2.2413 | \n",
" 53.5360 | \n",
" 26.3984 | \n",
" 0.1256 | \n",
" 0.0203 | \n",
" 26.5443 | \n",
" -10.0 | \n",
"
\n",
" \n",
" 2 | \n",
" ADM001 | \n",
" C | \n",
" Admiralty Inlet - Bush Pt. | \n",
" Admiralty Inlet | \n",
" 114 | \n",
" 48.029813 | \n",
" -122.617933 | \n",
" 2019-01-08 | \n",
" 2019-01-08 19:27:00 | \n",
" -0.587 | \n",
" 2.2755 | \n",
" 56.0817 | \n",
" 26.6102 | \n",
" 0.1552 | \n",
" 0.0347 | \n",
" 26.8001 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 3 | \n",
" ADM001 | \n",
" C | \n",
" Admiralty Inlet - Bush Pt. | \n",
" Admiralty Inlet | \n",
" 114 | \n",
" 48.029813 | \n",
" -122.617933 | \n",
" 2019-02-06 | \n",
" 2019-02-06 19:04:00 | \n",
" -29.444 | \n",
" 2.2109 | \n",
" 52.4005 | \n",
" 25.5163 | \n",
" 0.1996 | \n",
" 0.0000 | \n",
" 25.7159 | \n",
" -30.0 | \n",
"
\n",
" \n",
" 4 | \n",
" ADM001 | \n",
" C | \n",
" Admiralty Inlet - Bush Pt. | \n",
" Admiralty Inlet | \n",
" 114 | \n",
" 48.029813 | \n",
" -122.617933 | \n",
" 2019-02-06 | \n",
" 2019-02-06 19:04:00 | \n",
" -9.722 | \n",
" 2.2208 | \n",
" 51.9906 | \n",
" 25.6077 | \n",
" 0.1979 | \n",
" 0.0000 | \n",
" 25.8056 | \n",
" -10.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Station Desig Descrip Basin Max_Depth \\\n",
"0 ADM001 C Admiralty Inlet - Bush Pt. Admiralty Inlet 114 \n",
"1 ADM001 C Admiralty Inlet - Bush Pt. Admiralty Inlet 114 \n",
"2 ADM001 C Admiralty Inlet - Bush Pt. Admiralty Inlet 114 \n",
"3 ADM001 C Admiralty Inlet - Bush Pt. Admiralty Inlet 114 \n",
"4 ADM001 C Admiralty Inlet - Bush Pt. Admiralty Inlet 114 \n",
"\n",
" Latitude Longitude Date UTCDateTime Z PO4(uM)D \\\n",
"0 48.029813 -122.617933 2019-01-08 2019-01-08 19:27:00 -30.119 2.2138 \n",
"1 48.029813 -122.617933 2019-01-08 2019-01-08 19:27:00 -10.262 2.2413 \n",
"2 48.029813 -122.617933 2019-01-08 2019-01-08 19:27:00 -0.587 2.2755 \n",
"3 48.029813 -122.617933 2019-02-06 2019-02-06 19:04:00 -29.444 2.2109 \n",
"4 48.029813 -122.617933 2019-02-06 2019-02-06 19:04:00 -9.722 2.2208 \n",
"\n",
" SiOH4(uM)D NO3(uM)D NO2(uM)D NH4(uM)D DIN Znom \n",
"0 52.2913 26.2228 0.1347 0.0000 26.3575 -30.0 \n",
"1 53.5360 26.3984 0.1256 0.0203 26.5443 -10.0 \n",
"2 56.0817 26.6102 0.1552 0.0347 26.8001 0.0 \n",
"3 52.4005 25.5163 0.1996 0.0000 25.7159 -30.0 \n",
"4 51.9906 25.6077 0.1979 0.0000 25.8056 -10.0 "
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df1=pd.merge(left=dfSta,right=dfBot,how='right',\n",
" left_on='Station',right_on='Station')\n",
"#right join means all rows in right table (dfBot) are included in output\n",
"df1.head()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Station | \n",
" Desig | \n",
" Descrip | \n",
" Basin | \n",
" Max_Depth | \n",
" Latitude | \n",
" Longitude | \n",
" Date | \n",
" UTCDateTime | \n",
" Z | \n",
" PO4(uM)D | \n",
" SiOH4(uM)D | \n",
" NO3(uM)D | \n",
" NO2(uM)D | \n",
" NH4(uM)D | \n",
" DIN | \n",
" Znom | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: [Station, Desig, Descrip, Basin, Max_Depth, Latitude, Longitude, Date, UTCDateTime, Z, PO4(uM)D, SiOH4(uM)D, NO3(uM)D, NO2(uM)D, NH4(uM)D, DIN, Znom]\n",
"Index: []"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# check that there are no stations without lat and lon:\n",
"df1.loc[pd.isnull(df1['Latitude'])]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1171, 1171, 39)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# check one to one matches:\n",
"len(df1),len(dfBot), len(dfSta)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# where no time is provided, set time to midday Pacific time = ~ 20:00 UTC for now\n",
"# (most sampling takes place during the day)\n",
"# accurate times will be provided at a later date\n",
"# the code below takes advantage of all elements in 'Date' having a time component \n",
"# set to midnight\n",
"df1['UTCDateTime']=[iiD+dt.timedelta(hours=20) if pd.isnull(iiU) \\\n",
" else iiU for iiU,iiD in \\\n",
" zip(df1['UTCDateTime'],df1['Date'])]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Station | \n",
" Desig | \n",
" Descrip | \n",
" Basin | \n",
" Max_Depth | \n",
" Lat | \n",
" Lon | \n",
" Date | \n",
" dtUTC | \n",
" Z | \n",
" PO4(uM)D | \n",
" SiOH4(uM)D | \n",
" NO3(uM)D | \n",
" NO2(uM)D | \n",
" NH4(uM)D | \n",
" DIN | \n",
" Znom | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" ADM001 | \n",
" C | \n",
" Admiralty Inlet - Bush Pt. | \n",
" Admiralty Inlet | \n",
" 114 | \n",
" 48.029813 | \n",
" -122.617933 | \n",
" 2019-01-08 | \n",
" 2019-01-08 19:27:00 | \n",
" 30.119 | \n",
" 2.2138 | \n",
" 52.2913 | \n",
" 26.2228 | \n",
" 0.1347 | \n",
" 0.0000 | \n",
" 26.3575 | \n",
" -30.0 | \n",
"
\n",
" \n",
" 1 | \n",
" ADM001 | \n",
" C | \n",
" Admiralty Inlet - Bush Pt. | \n",
" Admiralty Inlet | \n",
" 114 | \n",
" 48.029813 | \n",
" -122.617933 | \n",
" 2019-01-08 | \n",
" 2019-01-08 19:27:00 | \n",
" 10.262 | \n",
" 2.2413 | \n",
" 53.5360 | \n",
" 26.3984 | \n",
" 0.1256 | \n",
" 0.0203 | \n",
" 26.5443 | \n",
" -10.0 | \n",
"
\n",
" \n",
" 2 | \n",
" ADM001 | \n",
" C | \n",
" Admiralty Inlet - Bush Pt. | \n",
" Admiralty Inlet | \n",
" 114 | \n",
" 48.029813 | \n",
" -122.617933 | \n",
" 2019-01-08 | \n",
" 2019-01-08 19:27:00 | \n",
" 0.587 | \n",
" 2.2755 | \n",
" 56.0817 | \n",
" 26.6102 | \n",
" 0.1552 | \n",
" 0.0347 | \n",
" 26.8001 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 3 | \n",
" ADM001 | \n",
" C | \n",
" Admiralty Inlet - Bush Pt. | \n",
" Admiralty Inlet | \n",
" 114 | \n",
" 48.029813 | \n",
" -122.617933 | \n",
" 2019-02-06 | \n",
" 2019-02-06 19:04:00 | \n",
" 29.444 | \n",
" 2.2109 | \n",
" 52.4005 | \n",
" 25.5163 | \n",
" 0.1996 | \n",
" 0.0000 | \n",
" 25.7159 | \n",
" -30.0 | \n",
"
\n",
" \n",
" 4 | \n",
" ADM001 | \n",
" C | \n",
" Admiralty Inlet - Bush Pt. | \n",
" Admiralty Inlet | \n",
" 114 | \n",
" 48.029813 | \n",
" -122.617933 | \n",
" 2019-02-06 | \n",
" 2019-02-06 19:04:00 | \n",
" 9.722 | \n",
" 2.2208 | \n",
" 51.9906 | \n",
" 25.6077 | \n",
" 0.1979 | \n",
" 0.0000 | \n",
" 25.8056 | \n",
" -10.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Station Desig Descrip Basin Max_Depth \\\n",
"0 ADM001 C Admiralty Inlet - Bush Pt. Admiralty Inlet 114 \n",
"1 ADM001 C Admiralty Inlet - Bush Pt. Admiralty Inlet 114 \n",
"2 ADM001 C Admiralty Inlet - Bush Pt. Admiralty Inlet 114 \n",
"3 ADM001 C Admiralty Inlet - Bush Pt. Admiralty Inlet 114 \n",
"4 ADM001 C Admiralty Inlet - Bush Pt. Admiralty Inlet 114 \n",
"\n",
" Lat Lon Date dtUTC Z PO4(uM)D \\\n",
"0 48.029813 -122.617933 2019-01-08 2019-01-08 19:27:00 30.119 2.2138 \n",
"1 48.029813 -122.617933 2019-01-08 2019-01-08 19:27:00 10.262 2.2413 \n",
"2 48.029813 -122.617933 2019-01-08 2019-01-08 19:27:00 0.587 2.2755 \n",
"3 48.029813 -122.617933 2019-02-06 2019-02-06 19:04:00 29.444 2.2109 \n",
"4 48.029813 -122.617933 2019-02-06 2019-02-06 19:04:00 9.722 2.2208 \n",
"\n",
" SiOH4(uM)D NO3(uM)D NO2(uM)D NH4(uM)D DIN Znom \n",
"0 52.2913 26.2228 0.1347 0.0000 26.3575 -30.0 \n",
"1 53.5360 26.3984 0.1256 0.0203 26.5443 -10.0 \n",
"2 56.0817 26.6102 0.1552 0.0347 26.8001 0.0 \n",
"3 52.4005 25.5163 0.1996 0.0000 25.7159 -30.0 \n",
"4 51.9906 25.6077 0.1979 0.0000 25.8056 -10.0 "
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# We require the following columns:\n",
"# dtUTC datetime\n",
"# Lat Latitude\n",
"# Lon Longitude\n",
"# Z Depth, increasing downward (positive)\n",
"df1.rename(columns={'UTCDateTime':'dtUTC','Latitude':'Lat','Longitude':'Lon'},inplace=True)\n",
"df1['Z']=-1*df1['Z']\n",
"df1.head()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# It will also be useful to add/rename the following columns:\n",
"df1['NO23']=df1['NO3(uM)D']+df1['NO2(uM)D'] # the model does not distinguish between NO2 and NO3\n",
"df1['Amm']=df1['NH4(uM)D']\n",
"df1['Si']=df1['SiOH4(uM)D']\n",
"df1['Year']=[ii.year for ii in df1['dtUTC']]\n",
"df1['YD']=et.datetimeToYD(df1['dtUTC'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## load CTD data"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"dfCTD0=pickle.load(open(os.path.join(datadir,f'Casts_{str(year)}.p'),'rb'))\n",
"dfCTD=pd.merge(left=dfSta,right=dfCTD0,how='right',\n",
" left_on='Station',right_on='Station')\n",
"dfCTD['dtUTC']=[iiD+dt.timedelta(hours=20) for iiD in dfCTD['Date']] #Does this mean it also has that flaw where we are not sure when the data was collected?\n",
"dfCTD.rename(columns={'Latitude':'Lat','Longitude':'Lon'},inplace=True)\n",
"dfCTD['Z']=-1*dfCTD['Z']\n",
"# Calculate Absolute (Reference) Salinity (g/kg) and Conservative Temperature (deg C) from \n",
"# Salinity (psu) and Temperature (deg C):\n",
"press=gsw.p_from_z(-1*dfCTD['Z'],dfCTD['Lat'])\n",
"dfCTD['SA']=gsw.SA_from_SP(dfCTD['Salinity'],press,\n",
" dfCTD['Lon'],dfCTD['Lat'])\n",
"dfCTD['CT']=gsw.CT_from_t(dfCTD['SA'],dfCTD['Temperature'],press)\n",
"\n",
"dfCTD['Year']=[ii.year for ii in dfCTD['dtUTC']]\n",
"dfCTD['YD']=et.datetimeToYD(dfCTD['dtUTC'])"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['Station', 'Desig', 'Descrip', 'Basin', 'Max_Depth', 'Lat', 'Lon',\n",
" 'Salinity', 'Temperature', 'Sigma', 'DO', 'Turb', 'Z', 'Date', 'dtUTC',\n",
" 'SA', 'CT', 'Year', 'YD'],\n",
" dtype='object')"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dfCTD.keys()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"this should be 1: [1]\n"
]
}
],
"source": [
"# check that there is never more than one ctd cast per station per day:\n",
"test=dfCTD.groupby(['Station','Year','YD','Z']).count()\n",
"print('this should be 1: ',test['Date'].unique())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Creating CTD exclusive matched dataset"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"start_date = dt.datetime(year,1,1)\n",
"end_date = dt.datetime(year,12,31)\n",
"flen=1 # number of days per model output file. always 1 for 201905 and 201812 model runs\n",
"namfmt='nowcast' # for 201905 and 201812 model runs, this should always be 'nowcast'\n",
"filemap={'vosaline':'grid_T','votemper':'grid_T','diatoms':'ptrc_T','ciliates':'ptrc_T','flagellates':'ptrc_T'}\n",
"fdict={'ptrc_T':1,'grid_T':1}"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(Lat,Lon)= 46.453155 -124.00960333333333 not matched to domain\n",
"(Lat,Lon)= 46.463155 -123.94126833333334 not matched to domain\n",
"(Lat,Lon)= 46.54537666666667 -123.98016166666666 not matched to domain\n",
"(Lat,Lon)= 46.644 -123.993 not matched to domain\n",
"(Lat,Lon)= 46.68676333333333 -123.9735 not matched to domain\n",
"(Lat,Lon)= 46.703986666666665 -123.837385 not matched to domain\n",
"(Lat,Lon)= 46.937313333333336 -123.91322333333333 not matched to domain\n",
"(Lat,Lon)= 46.953421666666664 -124.09295 not matched to domain\n",
"(Lat,Lon)= 47.21342666666666 -123.07765 not matched to domain\n",
"progress: 0.0%\n",
"progress: 9.350863084662715%\n",
"progress: 18.70172616932543%\n",
"progress: 28.052589253988142%\n",
"progress: 37.40345233865086%\n",
"progress: 46.75431542331357%\n",
"progress: 56.105178507976284%\n",
"progress: 65.456041592639%\n",
"progress: 74.80690467730172%\n",
"progress: 84.15776776196444%\n",
"progress: 93.50863084662714%\n"
]
}
],
"source": [
"data_CTD=et.matchData(dfCTD,filemap,fdict,start_date,end_date,'nowcast',PATH,1,quiet=False);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Creating Nutrients matched dataset with added CT and SA from CTD"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"def interpCTDvar(sta,yr,yd,ztarget,ctdvar):\n",
" ctdlocs=(dfCTD.Station==sta)&(dfCTD.Year==yr)&(dfCTD.YD==yd)\n",
" if np.sum(ctdlocs)==0:\n",
" print(f'Warning: Station {sta}, Year {yr}, year day {yd} not found in dfCTD')\n",
" return np.nan\n",
" else:\n",
" val=np.interp(ztarget,dfCTD.loc[ctdlocs,['Z']].values.flatten(),\n",
" dfCTD.loc[ctdlocs,[ctdvar]].values.flatten())\n",
" return val"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 8, 37, 72, 92, 127, 157, 197, 225, 267, 303, 318])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dfCTD.loc[dfCTD.Station=='PSS019']['YD'].unique()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 8, 37, 72, 92, 127, 157, 197, 225, 267, 303, 318])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df1.loc[df1.Station=='PSS019']['YD'].unique()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Warning: Station GYS008, Year 2019, year day 120 not found in dfCTD\n",
"Warning: Station GYS008, Year 2019, year day 302 not found in dfCTD\n",
"Warning: Station WPA008, Year 2019, year day 87 not found in dfCTD\n"
]
}
],
"source": [
"df1['SA']=[interpCTDvar(sta,yr,yd,ztarget,'SA') for sta, yr, yd, ztarget \\\n",
" in zip(df1['Station'],df1['Year'],df1['YD'],df1['Z'])]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Warning: Station GYS008, Year 2019, year day 120 not found in dfCTD\n",
"Warning: Station GYS008, Year 2019, year day 302 not found in dfCTD\n",
"Warning: Station WPA008, Year 2019, year day 87 not found in dfCTD\n"
]
}
],
"source": [
"df1['CT']=[interpCTDvar(sta,yr,yd,ztarget,'CT') for sta, yr, yd, ztarget \\\n",
" in zip(df1['Station'],df1['Year'],df1['YD'],df1['Z'])]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### set up variables for model-data matching"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"fdict={'ptrc_T':1,'grid_T':1}\n",
"# start_date and end_date are the first and last dates that will \n",
"# be included in the matched data set\n",
"start_date = dt.datetime(year,1,1)\n",
"end_date = dt.datetime(year,12,31)\n",
"flen=1 # number of days per model output file. always 1 for 201905 and 201812 model runs\n",
"namfmt='nowcast' # for 201905 and 201812 model runs, this should always be 'nowcast'\n",
"# filemap is dictionary of the form variableName: fileType, where variableName is the name\n",
"# of the variable you want to extract and fileType designates the type of \n",
"# model output file it can be found in (usually ptrc_T for biology, grid_T for temperature and \n",
"# salinity)\n",
"filemap={'nitrate':'ptrc_T','silicon':'ptrc_T','ammonium':'ptrc_T','diatoms':'ptrc_T',\n",
" 'ciliates':'ptrc_T','flagellates':'ptrc_T','votemper':'grid_T','vosaline':'grid_T'}\n",
"# fdict is a dictionary mappy file type to its time resolution. Here, 1 means hourly output\n",
"# (1h file) and 24 means daily output (1d file). In certain runs, multiple time resolutions \n",
"# are available\n",
"fdict={'ptrc_T':1,'grid_T':1}\n",
"# Note: to switch between 201812 and 201905 model results, change PATH\n",
"# to switch from hourly to daily model output, change fdict values from 1 to 24 (but daily \n",
"# files are not available for some runs and file types)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(Lat,Lon)= 46.453155 -124.00960333333333 not matched to domain\n",
"(Lat,Lon)= 46.463155 -123.94126833333334 not matched to domain\n",
"(Lat,Lon)= 46.54537666666667 -123.98016166666666 not matched to domain\n",
"(Lat,Lon)= 46.644 -123.993 not matched to domain\n",
"(Lat,Lon)= 46.68676333333333 -123.9735 not matched to domain\n",
"(Lat,Lon)= 46.703986666666665 -123.837385 not matched to domain\n",
"(Lat,Lon)= 46.937313333333336 -123.91322333333333 not matched to domain\n",
"(Lat,Lon)= 46.953421666666664 -124.09295 not matched to domain\n",
"(Lat,Lon)= 47.21342666666666 -123.07765 not matched to domain\n"
]
}
],
"source": [
"data=et.matchData(df1,filemap,fdict,start_date,end_date,'nowcast',PATH,1,quiet=False);"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"cm1=cmocean.cm.thermal\n",
"with nc.Dataset('/data/eolson/results/MEOPAR/NEMO-forcing-new/grid/bathymetry_201702.nc') as bathy:\n",
" bathylon=np.copy(bathy.variables['nav_lon'][:,:])\n",
" bathylat=np.copy(bathy.variables['nav_lat'][:,:])\n",
" bathyZ=np.copy(bathy.variables['Bathymetry'][:,:])"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"##### Saving data as Pickle files to be used in the summary file\n",
"saveloc='/ocean/kflanaga/MEOPAR/savedData'\n",
"with open(os.path.join(saveloc,f'data_WADE_{modelversion}_{year}.pkl'),'wb') as hh:\n",
" pickle.dump(data,hh)\n",
"with open(os.path.join(saveloc,f'data_CTD_{modelversion}_{year}.pkl'),'wb') as hh:\n",
" pickle.dump(data_CTD,hh)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (py39)",
"language": "python",
"name": "py39"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}