import numpy as np
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.dates as dts
import netCDF4 as nc
import os
import re
import pytz
import csv
import time
%matplotlib inline
def loadSOGHoff(fname):
data2=dict()
with open(fname, 'rt') as file_obj:
for index, line in enumerate(file_obj):
line = line.strip()
if line.startswith('*FieldNames:'):
field_names = line.split(': ', 1)[1].split(', ')
elif line.startswith('*FieldUnits:'):
field_units = line.split(': ', 1)[1].split(', ')
elif line.startswith('*HoffmuellerStartYr:'):
year_start = line.split(': ', 1)[1]
elif line.startswith('*HoffmuellerStartDay:'):
day_start = line.split(': ', 1)[1]
elif line.startswith('*HoffmuellerStartSec:'):
sec_start = line.split(': ', 1)[1]
elif line.startswith('*HoffmuellerInterval:'):
interval = line.split(': ', 1)[1]
elif line.startswith('*EndOfHeader'):
break
# Timestamp in matplotlib time
dt_start = dts.date2num(dt.datetime.strptime(year_start + ' ' + day_start, '%Y %j')) + float(sec_start)/86400
data = pd.read_csv(fname, delim_whitespace=True, header=0, names=field_names, skiprows=index, chunksize=82)
# Extract dataframe chunks into dictionary
times=list()
for index, chunk in enumerate(data):
times.append(dts.num2date(dt_start + index*float(interval), tz=pytz.timezone('UTC')))
if index==0:
da=chunk
else:
da=np.dstack((da,chunk))
data2['times']=np.array(times)
for iel in range(0,len(field_names)):
data2[field_names[iel]]=da[:,iel,:].transpose()
return data2
d=dict()
d['orig']=loadSOGHoff('/data/eolson/SOG/SOG-runs/temp/profiles/hoff-SOG.dat')
d['base']=loadSOGHoff('/data/eolson/SOG/SOG-runs/exp/base/profiles/hoff-SOG.dat')
d['NewBase']=loadSOGHoff('/data/eolson/SOG/SOG-runs/exp/NewBase/profiles/hoff-SOG.dat')
d['Q1']=loadSOGHoff('/data/eolson/SOG/SOG-runs/exp/Q1/profiles/hoff-SOG.dat')
d['Q2']=loadSOGHoff('/data/eolson/SOG/SOG-runs/exp/Q2/profiles/hoff-SOG.dat')
d['Q4']=loadSOGHoff('/data/eolson/SOG/SOG-runs/exp/Q4/profiles/hoff-SOG.dat')
d['Q5']=loadSOGHoff('/data/eolson/SOG/SOG-runs/exp/Q5/profiles/hoff-SOG.dat')
d['Q6']=loadSOGHoff('/data/eolson/SOG/SOG-runs/exp/Q6/profiles/hoff-SOG.dat')
d['Q7']=loadSOGHoff('/data/eolson/SOG/SOG-runs/exp/Q7/profiles/hoff-SOG.dat')
d['Q8']=loadSOGHoff('/data/eolson/SOG/SOG-runs/exp/Q8/profiles/hoff-SOG.dat')
d['Q9']=loadSOGHoff('/data/eolson/SOG/SOG-runs/exp/Q9/profiles/hoff-SOG.dat')
d.keys()
dict_keys(['orig', 'Q2', 'NewBase', 'Q7', 'Q6', 'Q9', 'Q8', 'Q1', 'Q4', 'Q5', 'base'])
d['Q1'].keys()
dict_keys(['pico phytoplankton', 'total momentum eddy diffusivity', 'salinity', 'dissolved inorganic carbon', 'total temperature eddy diffusivity', 'ammonium', 'DOC detritus', 'micro zooplankton', 'dissolved oxygen', 'times', 'depth', 'nano phytoplankton', 'biogenic Si detritus', 'PON detritus', 'photosynthetic available radiation', 'silicon', 'POC detritus', 'total salinity eddy diffusivity', 'alkalinity', 'temperature', 'sigma-t', 'refractory N detritus', 'u velocity', 'micro phytoplankton', 'nitrate', 'DON detritus', 'v velocity'])
style={'micro phytoplankton':'-','nano phytoplankton':'--','pico phytoplankton':'.-',
'nitrate':'-o','silicon':'->','ammonium':'-x','micro zooplankton':'+'}
col={'orig':'k','base':'maroon','NewBase':'orangered','Q1':'orchid','Q2':'teal',
'Q4':'green','Q5':'lime','Q6':'red','Q7':'aquamarine','Q8':'blue','Q9':'goldenrod'}
#'R1':'dodgerblue','A3':'lime','N3':'m','A5':'pink','C2':'gold','A6':'brown',
#'F1':'blue','C4':'turquoise','C5':'mediumseagreen','C6':'red','C7':'c','C8':'pink',
#'M1':'darkorange','R5':'purple','M3':'darkorange','M5':'limegreen','M6':'firebrick',
#'M7':'darkkhaki','N2':'aquamarine','M8':'peru','R2':'deeppink','R3':'darkkhaki','Si3':'orange'}
fig, axs = plt.subplots(1,1,figsize=(20,7))
for el in ('orig','base','NewBase','Q1','Q2','Q4','Q5','Q6'):
for var in ('micro phytoplankton','nano phytoplankton','pico phytoplankton'):
axs.plot(d[el]['times'],np.mean(d[el][var][:,0:6],1),style[var],color=col[el],label=el+' '+var)
axs.set_xlim(dt.datetime(2005,2,1),dt.datetime(2005,6,1))
axs.legend(loc=1)
<matplotlib.legend.Legend at 0x7f7350c9e3c8>
fig, axs = plt.subplots(1,1,figsize=(20,7))
for el in ('NewBase','Q8','Q9'):
for var in ('micro phytoplankton','nano phytoplankton','pico phytoplankton'):
axs.plot(d[el]['times'],np.mean(d[el][var][:,0:6],1),style[var],color=col[el],label=el+' '+var)
axs.set_xlim(dt.datetime(2005,2,1),dt.datetime(2005,6,1))
axs.legend(loc=1)
<matplotlib.legend.Legend at 0x7f7348ad79e8>
fig, axs = plt.subplots(2,1,figsize=(12,8))
for el in ('NewBase','Q9','Q8'):
axs[0].plot(np.mean(d[el]['nitrate'][:,0:6],1),np.mean(d[el]['silicon'][:,0:6],1),'s',alpha=.3,color=col[el],label=el+' Si:N')
#ts=d[el]['times']
#xs=np.mean(d[el]['silicon'][:,0:6]+.002,1)/np.mean(d[el]['nitrate'][:,0:6]+.001,1)
#ys=np.mean(d[el]['nitrate'][:,0:6]+.001,1)
#axs.plot(ts[ys>1],xs[ys>1],'s',color=col[el],label=el+' Si:N')
axs[1].plot(d[el]['times'],np.mean(d[el]['silicon'][:,0:6],1)-2*np.mean(d[el]['nitrate'][:,0:6],1),color=col[el])
#axs.set_xlim(dt.datetime(2005,2,1),dt.datetime(2005,10,1))
axs[0].legend(loc=4)
axs[0].set_aspect(.5)
fig, axs = plt.subplots(1,1,figsize=(20,7))
for el in ('NewBase','Q9','Q8'):
for var in ('nitrate','silicon','ammonium'):
axs.plot(d[el]['times'],np.mean(d[el][var][:,0:6],1),style[var],color=col[el],label=el+' '+var)
axs.set_xlim(dt.datetime(2005,2,1),dt.datetime(2005,8,1))
axs.legend(loc=1)
<matplotlib.legend.Legend at 0x7f7350ca8208>
fig, axs = plt.subplots(1,1,figsize=(20,7))
for el in ('NewBase','Q9','Q8'):
for var in ('micro phytoplankton','nano phytoplankton','pico phytoplankton','micro zooplankton'):
axs.plot(d[el]['times'],np.mean(d[el][var][:,0:6],1),style[var],color=col[el],label=el+' '+var)
#axs.plot(d['M5']['times'],np.mean(d['M5']['micro phytoplankton'][:,0:6],1),'g*')
axs.set_xlim(dt.datetime(2005,2,1),dt.datetime(2005,10,1))
axs.legend(loc=1)
# axs.plot(d['C5']['times'][128],10,'rs')
<matplotlib.legend.Legend at 0x7f7348ad7748>
fig, axs = plt.subplots(1,1,figsize=(20,7))
for el in ('NewBase','Q6','Q7','Q8'):
for var in ('pico phytoplankton',):
axs.plot(d[el]['times'],np.mean(d[el][var][:,0:6],1),style[var],color=col[el],label=el+' '+var)
#axs.plot(d['M5']['times'],np.mean(d['M5']['micro phytoplankton'][:,0:6],1),'g*')
axs.set_xlim(dt.datetime(2005,2,1),dt.datetime(2005,10,1))
axs.legend(loc=1)
# axs.plot(d['C5']['times'][128],10,'rs')
<matplotlib.legend.Legend at 0x7fb78e177cf8>
fig, axs = plt.subplots(1,1,figsize=(20,7))
for el in ('M5','R1','R2','R3','R5'):
for var in ('micro phytoplankton','nano phytoplankton','pico phytoplankton'):
axs.plot(d[el]['times'],np.mean(d[el][var][:,0:6],1),style[var],color=col[el],label=el+' '+var)
#axs.plot(d['M5']['times'],np.mean(d['M5']['micro phytoplankton'][:,0:6],1),'g*')
axs.set_xlim(dt.datetime(2005,2,1),dt.datetime(2005,10,1))
axs.legend(loc=1)
# axs.plot(d['C5']['times'][128],10,'rs')
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-12-da206d1b0805> in <module>() 3 for el in ('M5','R1','R2','R3','R5'): 4 for var in ('micro phytoplankton','nano phytoplankton','pico phytoplankton'): ----> 5 axs.plot(d[el]['times'],np.mean(d[el][var][:,0:6],1),style[var],color=col[el],label=el+' '+var) 6 7 #axs.plot(d['M5']['times'],np.mean(d['M5']['micro phytoplankton'][:,0:6],1),'g*') KeyError: 'M5'
fig, axs = plt.subplots(1,1,figsize=(20,7))
for el in ('M5','R1'):
for var in ('nitrate',):
axs.plot(d[el]['times'],np.mean(d[el][var][:,0:6],1),style[var],color=col[el],label=el+' '+var)
#axs.plot(d['M5']['times'],np.mean(d['M5']['micro phytoplankton'][:,0:6],1),'g*')
axs.set_xlim(dt.datetime(2005,2,1),dt.datetime(2005,10,1))
axs.legend(loc=1)
# axs.plot(d['C5']['times'][128],10,'rs')