{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from salishsea_tools import evaltools as et, viz_tools\n", "import cmocean as cmo\n", "import datetime as dt\n", "import netCDF4 as nc\n", "import matplotlib.dates as mdates\n", "import matplotlib as mpl\n", "import matplotlib.gridspec as gridspec\n", "from matplotlib.colors import LogNorm\n", "import cmocean\n", "import pandas as pd\n", "import pickle\n", "import os\n", "import gsw\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### definitions for matching model output" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# path to model files:\n", "PATH= '/results/SalishSea/nowcast-green.201812/'\n", "\n", "# start and end dates for analysis:\n", "start_date = dt.datetime(2015,1,1)\n", "end_date = dt.datetime(2016,1,1)\n", "\n", "# number of days per model file:\n", "flen=1\n", "\n", "# dictionary mapping desired model variables to the file types where they are found\n", "filemap={'nitrate':'ptrc_T','silicon':'ptrc_T','ammonium':'ptrc_T','diatoms':'ptrc_T','ciliates':'ptrc_T',\n", " 'flagellates':'ptrc_T','vosaline':'grid_T','votemper':'grid_T'}\n", "\n", "# dictionary mapping model file types to their time resolution in hours (1 is hourly files, 24 is daily)\n", "fdict={'ptrc_T':1,'grid_T':1}\n", "\n", "# results format\n", "# -- nowcast: files like 01jan15/SalishSea_1h_20150101_20150101_ptrc_T.nc\n", "# -- long: files like SalishSea_1h_20150206_20150804_ptrc_T_20150427-20150506.nc, all in one directory\n", "namfmt='nowcast'\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### load WADE nutrient data and create csv file" ] }, { "cell_type": "code", "execution_count": 3, "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": 4, "metadata": {}, "outputs": [], "source": [ "# load new datetime info\n", "dfTime=pd.read_excel('/ocean/eolson/MEOPAR/obs/WADE/WDE_Data/OlsonSuchyAllen_UBC_PDR_P003790-010721.xlsx',\n", " engine='openpyxl',sheet_name='EventDateTime')\n", "# drop duplicate rows, taking first time\n", "dfTime.drop_duplicates(subset=['FlightDate','SiteCode'],keep='first',inplace=True)\n", "dfTime['dtPac']=[dt.datetime.combine(idate, itime) for idate, itime \\\n", " in zip(dfTime['FlightDate'],dfTime['TimeDown \\n(Local - PST or PDT)'])]\n", "dfTime['dtUTC']=[et.pac_to_utc(ii) for ii in dfTime['dtPac']]\n", "# join to date/time\n", "dfTime['dtUTC']=[et.pac_to_utc(dt.datetime.combine(idate,itime)) for idate,itime in \\\n", " zip(dfTime['FlightDate'],dfTime['TimeDown \\n(Local - PST or PDT)'])]\n", "dfTime2=dfTime.loc[:,['FlightDate','SiteCode','dtUTC']]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "dfs=dict()\n", "for year in range(2015,2019):\n", " PATH= '/results2/SalishSea/nowcast-green.201905/'\n", " datadir='/ocean/eolson/MEOPAR/obs/WADE/ptools_data/ecology'\n", " dfSta=pickle.load(open(os.path.join(datadir,'sta_df.p'),'rb'))\n", " dfCTD0=pickle.load(open(os.path.join(datadir,f'Casts_{str(year)}.p'),'rb'))\n", " dfs0=pd.merge(left=dfSta,right=dfCTD0,how='right',\n", " left_on='Station',right_on='Station')\n", " dfs0.rename(columns={'Latitude':'Lat','Longitude':'Lon'},inplace=True)\n", " # Calculate Absolute (Reference) Salinity (g/kg) and Conservative Temperature (deg C) from \n", " # Salinity (psu) and Temperature (deg C):\n", " dfs0['Z']=-1*dfs0['Z']\n", " press=gsw.p_from_z(-1*dfs0['Z'],dfs0['Lat'])\n", " dfs0['SA']=gsw.SA_from_SP(dfs0['Salinity'],press,\n", " dfs0['Lon'],dfs0['Lat'])\n", " dfs0['CT']=gsw.CT_from_t(dfs0['SA'],dfs0['Temperature'],press)\n", " # merge time\n", " dfs[year]=pd.merge(left=dfs0,right=dfTime2,how='left',\n", " left_on=['Date','Station'],right_on=['FlightDate','SiteCode'])\n", " # if no time available, guess midday\n", " dfs[year]['dtUTC']=[iiD+dt.timedelta(hours=20) if pd.isnull(iiU) else iiU \\\n", " for iiU,iiD in zip(dfs[year]['dtUTC'],dfs[year]['Date'])]\n", " #dfs[year]['Year']=[ii.year for ii in dfs[year]['dtUTC']]\n", " #dfs[year]['YD']=et.datetimeToYD(dfs[year]['dtUTC'])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Station | \n", "Basin | \n", "Lat | \n", "Lon | \n", "Sigma | \n", "Chl | \n", "Z | \n", "SA | \n", "CT | \n", "SiteCode | \n", "dtUTC | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "ADM001 | \n", "Admiralty Inlet | \n", "48.029813 | \n", "-122.617933 | \n", "23.311800 | \n", "0.809025 | \n", "118.5 | \n", "30.307274 | \n", "9.191911 | \n", "ADM001 | \n", "2015-01-20 19:14:00 | \n", "
1 | \n", "ADM001 | \n", "Admiralty Inlet | \n", "48.029813 | \n", "-122.617933 | \n", "23.310699 | \n", "0.829323 | \n", "118.0 | \n", "30.305957 | \n", "9.192287 | \n", "ADM001 | \n", "2015-01-20 19:14:00 | \n", "
2 | \n", "ADM001 | \n", "Admiralty Inlet | \n", "48.029813 | \n", "-122.617933 | \n", "23.306700 | \n", "0.791626 | \n", "117.5 | \n", "30.301120 | \n", "9.193727 | \n", "ADM001 | \n", "2015-01-20 19:14:00 | \n", "
3 | \n", "ADM001 | \n", "Admiralty Inlet | \n", "48.029813 | \n", "-122.617933 | \n", "23.304600 | \n", "0.791626 | \n", "117.0 | \n", "30.298496 | \n", "9.194526 | \n", "ADM001 | \n", "2015-01-20 19:14:00 | \n", "
4 | \n", "ADM001 | \n", "Admiralty Inlet | \n", "48.029813 | \n", "-122.617933 | \n", "23.306299 | \n", "0.817724 | \n", "116.5 | \n", "30.300392 | \n", "9.193442 | \n", "ADM001 | \n", "2015-01-20 19:14:00 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
55880 | \n", "WPA113 | \n", "Willapa Bay | \n", "46.644000 | \n", "-123.993000 | \n", "22.316300 | \n", "NaN | \n", "3.0 | \n", "29.307299 | \n", "10.628845 | \n", "NaN | \n", "2018-11-19 20:00:00 | \n", "
55881 | \n", "WPA113 | \n", "Willapa Bay | \n", "46.644000 | \n", "-123.993000 | \n", "22.249900 | \n", "NaN | \n", "2.5 | \n", "29.220695 | \n", "10.627059 | \n", "NaN | \n", "2018-11-19 20:00:00 | \n", "
55882 | \n", "WPA113 | \n", "Willapa Bay | \n", "46.644000 | \n", "-123.993000 | \n", "22.152100 | \n", "NaN | \n", "2.0 | \n", "29.092593 | \n", "10.620385 | \n", "NaN | \n", "2018-11-19 20:00:00 | \n", "
55883 | \n", "WPA113 | \n", "Willapa Bay | \n", "46.644000 | \n", "-123.993000 | \n", "22.115000 | \n", "NaN | \n", "1.5 | \n", "29.043971 | \n", "10.617901 | \n", "NaN | \n", "2018-11-19 20:00:00 | \n", "
55884 | \n", "WPA113 | \n", "Willapa Bay | \n", "46.644000 | \n", "-123.993000 | \n", "22.094800 | \n", "NaN | \n", "1.0 | \n", "29.017655 | \n", "10.617328 | \n", "NaN | \n", "2018-11-19 20:00:00 | \n", "
211283 rows × 11 columns
\n", "\n", " | Station | \n", "Basin | \n", "Lat | \n", "Lon | \n", "Sigma | \n", "Chl | \n", "Z | \n", "SA | \n", "CT | \n", "SiteCode | \n", "dtUTC | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "ADM001 | \n", "Admiralty Inlet | \n", "48.029813 | \n", "-122.617933 | \n", "23.311800 | \n", "0.809025 | \n", "118.5 | \n", "30.307274 | \n", "9.191911 | \n", "ADM001 | \n", "2015-01-20 19:14:00 | \n", "
1 | \n", "ADM001 | \n", "Admiralty Inlet | \n", "48.029813 | \n", "-122.617933 | \n", "23.310699 | \n", "0.829323 | \n", "118.0 | \n", "30.305957 | \n", "9.192287 | \n", "ADM001 | \n", "2015-01-20 19:14:00 | \n", "
2 | \n", "ADM001 | \n", "Admiralty Inlet | \n", "48.029813 | \n", "-122.617933 | \n", "23.306700 | \n", "0.791626 | \n", "117.5 | \n", "30.301120 | \n", "9.193727 | \n", "ADM001 | \n", "2015-01-20 19:14:00 | \n", "
3 | \n", "ADM001 | \n", "Admiralty Inlet | \n", "48.029813 | \n", "-122.617933 | \n", "23.304600 | \n", "0.791626 | \n", "117.0 | \n", "30.298496 | \n", "9.194526 | \n", "ADM001 | \n", "2015-01-20 19:14:00 | \n", "
4 | \n", "ADM001 | \n", "Admiralty Inlet | \n", "48.029813 | \n", "-122.617933 | \n", "23.306299 | \n", "0.817724 | \n", "116.5 | \n", "30.300392 | \n", "9.193442 | \n", "ADM001 | \n", "2015-01-20 19:14:00 | \n", "