def east_west_joint_run_nee_err_no_r(xb, f_name, nee_scale=0, clma_er=1., lai_er=1., need_er=1., neen_er=1., cr_er=1., cw_er=1.): f_name += 'clmaer%r_laier%r_needer%r_neener%r_crer%r_cwer%r' % ( clma_er, lai_er, need_er, neen_er, cr_er, cw_er) # Construct B b_cor = pickle.load(open('b_edc_cor.p', 'r')) b_std = np.sqrt(np.diag(pickle.load(open('b_edc.p', 'r')))) b_std[10] = 0.25 * b_std[10] b_std[1] = 0.25 * b_std[1] b_std[0:17] = 0.5 * b_std[0:17] D = np.zeros_like(b_cor) np.fill_diagonal(D, b_std) b = 0.6 * np.dot(np.dot(D, b_cor), D) # east data de = dc.DalecData( 2015, 2016, 'nee_day_east, nee_night_east, c_roo_east, c_woo_east, clma, lai_east', nc_file='../../alice_holt_data/ah_data_daily_test_nee.nc', scale_nee=nee_scale) de.B = b # obs err scaling de.ob_err_dict['clma'] = clma_er * de.ob_err_dict['clma'] de.ob_err_dict['lai'] = lai_er * de.ob_err_dict['lai'] de.ob_err_dict['nee_day'] = need_er * de.ob_err_dict['nee_day'] de.ob_err_dict['nee_night'] = neen_er * de.ob_err_dict['nee_night'] de.ob_err_dict['c_roo'] = cr_er * de.ob_err_dict['c_roo'] de.ob_err_dict['c_woo'] = cw_er * de.ob_err_dict['c_woo'] # west data dw = dc.DalecData( 2015, 2016, 'nee_day_west, nee_night_west, c_roo_west, c_woo_west, clma, lai_west', nc_file='../../alice_holt_data/ah_data_daily_test_nee.nc', scale_nee=nee_scale) dw.B = b # obs err scaling dw.ob_err_dict['clma'] = clma_er * dw.ob_err_dict['clma'] dw.ob_err_dict['lai'] = lai_er * dw.ob_err_dict['lai'] dw.ob_err_dict['nee_day'] = need_er * dw.ob_err_dict['nee_day'] dw.ob_err_dict['nee_night'] = neen_er * dw.ob_err_dict['nee_night'] dw.ob_err_dict['c_roo'] = cr_er * dw.ob_err_dict['c_roo'] dw.ob_err_dict['c_woo'] = cw_er * dw.ob_err_dict['c_woo'] # setup model me = mc.DalecModel(de) mw = mc.DalecModel(dw) # run DA scheme xa_e = me.find_min_tnc_cvt(xb, f_name + 'east_assim') xa_w = mw.find_min_tnc_cvt(xb, f_name + 'west_assim') # save plots save_plots(f_name, xb, xa_e[1], xa_w[1], de, dw, me, mw) return 'done'
def east_west_joint_run_nee_err_r_errs(xb, f_name): # Construct B b_cor = pickle.load(open('b_edc_cor.p', 'r')) b_std = np.sqrt(np.diag(pickle.load(open('b_edc.p', 'r')))) b_std[10] = 0.25 * b_std[10] b_std[1] = 0.25 * b_std[1] #b_std[2] = 0.1*b_std[2] b_std[0:17] = b_std[0:17] * 0.5 D = np.zeros_like(b_cor) np.fill_diagonal(D, b_std) b = 0.6 * np.dot(np.dot(D, b_cor), D) #*0.6 # east data de = dc.DalecData( 2015, 2016, 'nee_east, nee_night_east, c_roo_east, c_woo_east, clma, lai_east', nc_file='../../alice_holt_data/ah_data_daily_test_nee2.nc', scale_nee=1) de.B = b # obs err scaling # west data dw = dc.DalecData( 2015, 2016, 'nee_west, nee_night_west, c_roo_west, c_woo_west, clma, lai_west', nc_file='../../alice_holt_data/ah_data_daily_test_nee2.nc', scale_nee=1) dw.B = b # obs err scaling # setup model me = mc.DalecModel(de) me.rmatrix = r_mat_corr(me.yerroblist, me.ytimestep, me.y_strlst, me.rmatrix, corr=0.3, tau=2.)[1] mw = mc.DalecModel(dw) mw.rmatrix = r_mat_corr(mw.yerroblist, mw.ytimestep, mw.y_strlst, mw.rmatrix, corr=0.3, tau=2.)[1] # run DA scheme xa_e = me.find_min_tnc_cvt(xb, f_name + 'east_assim') xa_w = mw.find_min_tnc_cvt(xb, f_name + 'west_assim') # save plots save_plots(f_name, xb, xa_e[1], xa_w[1], de, dw, me, mw) return 'done'
def east_west_joint_run_prior(xb, f_name, obs_str, b_mat, rm='None', end_yr=2014, start_yr=2012): if not os.path.exists(f_name): os.makedirs(f_name) # east data obs_east = ob_str_east_west(obs_str, 'east', rm_obs=rm) de = dc.DalecData( start_yr, end_yr, obs_east, nc_file='../../alice_holt_data/ah_data_daily_test_nee3.nc', scale_nee=0) de.B = b_mat # obs err scaling # west data obs_west = ob_str_east_west(obs_str, 'west', rm_obs=rm) dw = dc.DalecData( start_yr, end_yr, obs_west, nc_file='../../alice_holt_data/ah_data_daily_test_nee3.nc', scale_nee=0) dw.B = b_mat # obs err scaling # setup model me = mc.DalecModel(de) me.rmatrix = r_mat_corr(me.yerroblist, me.ytimestep, me.y_strlst, me.rmatrix, corr=0.3, tau=2.)[1] mw = mc.DalecModel(dw) mw.rmatrix = r_mat_corr(mw.yerroblist, mw.ytimestep, mw.y_strlst, mw.rmatrix, corr=0.3, tau=2.)[1] # run DA scheme xa_e = me.find_min_tnc_cvt(xb, f_name + 'east_assim') xa_w = mw.find_min_tnc_cvt(xb, f_name + 'west_assim') # save plots save_plots(f_name, xb, xa_e[1], xa_w[1], de, dw, me, mw) return 'done'
def calc_exp_vals(exp_name): east = pickle.load(open(exp_name + 'east_assim', 'r')) west = pickle.load(open(exp_name + 'west_assim', 'r')) b = 1.5 * east['b_mat'] # east data de = dc.DalecData( 2015, 2016, 'clma', nc_file='../../alice_holt_data/ah_data_daily_test_nee2.nc', scale_nee=1) de.B = b de.ob_dict = east['obs'] de.ob_err_dict = east['obs_err'] # obs err scaling # west data dw = dc.DalecData( 2015, 2016, 'clma', nc_file='../../alice_holt_data/ah_data_daily_test_nee2.nc', scale_nee=1) dw.B = b dw.ob_dict = west['obs'] dw.ob_err_dict = west['obs_err'] # obs err scaling # setup model me = mc.DalecModel(de) me.rmatrix = r_mat_corr(me.yerroblist, me.ytimestep, me.y_strlst, me.rmatrix, corr=0.3, tau=2.)[1] mw = mc.DalecModel(dw) mw.rmatrix = r_mat_corr(mw.yerroblist, mw.ytimestep, mw.y_strlst, mw.rmatrix, corr=0.3, tau=2.)[1] # a_east = pickle.load(open('a_east.p', 'r')) # a_west = pickle.load(open('a_west.p', 'r')) a_east = me.acovmat(east['xa']) a_west = mw.acovmat(west['xa']) e_ens = p.create_ensemble(de, a_east, east['xa']) w_ens = p.create_ensemble(de, a_west, west['xa']) p_e_ens = p.plist_ens(de, e_ens) p_w_ens = p.plist_ens(dw, w_ens) return east, west, de, dw, p_e_ens, p_w_ens
def plotcostone_ens(vect=1, sizee=20): """Using test_cost plots convergance of cost fn gradient for decreasing value of alpha. """ sns.set_context('poster', font_scale=1.5, rc={ 'lines.linewidth': 1, 'lines.markersize': 6 }) fig, ax = plt.subplots( nrows=1, ncols=1, ) #figsize=(10,10)) sns.set_style('ticks') power = np.arange(1, 14, 1) xlist = [10**(-x) for x in power] d = dC.DalecDataTwin( 1999, 2010, 'nee', nc_file='../../alice_holt_data/ah_data_daily_test_nee3.nc', scale_nee=0) m = mc.DalecModel(d, size_ens=sizee) tstlist = [test_cost_ens(m, x, vect) for x in xlist] ax.semilogx(xlist, tstlist, 'k', marker='x', mew=1, ms=8) #ax.semilogx(xlist, tstlist, 'k', 'x') plt.xlabel(r'$\alpha$') plt.ylabel(r'$f(\alpha)$') #plt.title('test of the gradient of the cost function') print tstlist return ax, fig
def test_cost_cvt(alph=1e-8, vect=0): """Test for cost and gradcost functions. """ d = dC.DalecDataTwin( 1999, 2000, 'nee', nc_file='../../alice_holt_data/ah_data_daily_test_nee3.nc', scale_nee=1) m = mc.DalecModel(d, size_ens=1) pvals = d.edinburgh_mean zvals = m.pvals2zvals(pvals) gradj = m.gradcost_cvt(zvals) print gradj.shape if vect == 0: h = zvals * (np.linalg.norm(zvals))**(-1) elif vect == 1: h = gradj * (np.linalg.norm(gradj))**(-1) elif vect == 2: h = np.ones(23) * (np.sqrt(23)**-1) j = m.cost_cvt(zvals) jalph = m.cost_cvt(zvals + alph * h) print jalph - j print np.dot(alph * h, gradj) print(jalph - j) / (np.dot(alph * h, gradj)) return abs(jalph - j) / (np.dot(alph * h, gradj))
def day_night_twin_run(start, end, obs, f_name, obs_loc): d = dc.DalecDataTwin(start, end, obs, nc_file='../../alice_holt_data/ah_data_daily_test.nc') pik_obs = pickle.load(open(obs_loc, 'r')) d.ob_dict = pik_obs m = mc.DalecModel(d) assim_results, xa = m.find_min_tnc_cvt(d.xb, f_name + '_assim_res') d2 = dc.DalecDataTwin(start, 2013, obs) # Plot 4dvar time series ax, fig = p.plot_4dvar_twin('nee', d2, xa=xa) fig.savefig(f_name + '_nee.pdf', bbox_inches='tight') ax, fig = p.plot_4dvar_twin('nee_day', d2, xa=xa) fig.savefig(f_name + '_need.pdf', bbox_inches='tight') ax, fig = p.plot_4dvar_twin('nee_night', d2, xa=xa) fig.savefig(f_name + '_neen.pdf', bbox_inches='tight') ax, fig = p.plot_4dvar_twin('lai', d2, xa=xa) fig.savefig(f_name + '_lai.pdf', bbox_inches='tight') # Plot scatter plots of obs ax, fig = p.plot_scatter_twin('nee', xa, d2, len(d.I), 'f') fig.savefig(f_name + '_nee_scat.pdf', bbox_inches='tight') ax, fig = p.plot_scatter_twin('nee_day', xa, d2, len(d.I), 'f') fig.savefig(f_name + '_need_scat.pdf', bbox_inches='tight') ax, fig = p.plot_scatter_twin('nee_night', xa, d2, len(d.I), 'f') fig.savefig(f_name + '_neen_scat.pdf', bbox_inches='tight') ax, fig = p.plot_scatter_twin('lai', xa, d2, len(d.I), 'f') fig.savefig(f_name + '_lai_scat.pdf', bbox_inches='tight') # Plot error in analysis and background ax, fig = p.plottwinerr(d.x_truth, d.xb, xa) fig.savefig(f_name + '_twin_err.pdf', bbox_inches='tight') return 'all experimented'
def __init__(self, start_date, end_date, ob_str, dat_file="../../aliceholtdata/ahdat99_13.csv"): DalecData.__init__(self, start_date, end_date, ob_str, dat_file) # self.d = DalecData(start_date, end_date, ob_str, nc_file) self.m = mc.DalecModel(self) # Define truth and background self.x_truth = self.edinburgh_median self.st_dev = 0.10 * self.x_truth self.B = self.make_b(self.st_dev) # self.xb = self.random_pert(self.random_pert(self.x_truth)) self.xb = np.array([ 2.53533992e-04, 5.85073161e-01, 7.43127332e-02, 4.99707798e-01, 1.38993876e+00, 6.11913792e-05, 2.58484324e-03, 2.79379720e-03, 8.72422101e-05, 4.35144260e-02, 8.73669864e+01, 1.29813051e+02, 3.87867223e-01, 4.69894281e+01, 2.78080852e+02, 9.15080347e+01, 1.36269157e+02, 1.44176657e+02, 6.71153814e+01, 2.42199267e+02, 4.96249386e+03, 4.15128028e+02, 1.90797697e+03 ]) # Extract observations for assimilation self.ob_dict, self.ob_err_dict = self.create_twin_data(ob_str)
def __init__(self, start_date, end_date, ob_str, err_scale=0.25, nc_file='../../alice_holt_data/ah_data_daily_test.nc'): DalecData.__init__(self, start_date, end_date, ob_str, nc_file) self.m = mc.DalecModel(self) # Define truth and background self.x_truth = self.edinburgh_median self.st_dev = 0.1*self.x_truth # self.B = self.make_b(self.st_dev) # Make EDC B b_cor = pickle.load(open('b_edc_cor.p', 'r')) # load correlation matrix from 2016 paper b_std = self.make_b(np.sqrt(self.st_dev)) # Create diagonal matrix of standard deviations self.B = np.dot(np.dot(b_std, b_cor), b_std) # Create correlated B # self.xb = self.random_pert(self.random_pert(self.x_truth)) self.xb = np.array([2.53533992e-04, 5.85073161e-01, 7.43127332e-02, 4.99707798e-01, 1.38993876e+00, 6.11913792e-05, 2.58484324e-03, 2.79379720e-03, 8.72422101e-05, 4.35144260e-02, 8.73669864e+01, 1.29813051e+02, 3.87867223e-01, 4.69894281e+01, 2.78080852e+02, 9.15080347e+01, 1.36269157e+02, 1.44176657e+02, 6.71153814e+01, 2.42199267e+02, 4.96249386e+03, 4.15128028e+02, 1.90797697e+03]) # Extract observations for assimilation self.ob_dict, self.ob_err_dict = self.create_twin_data(ob_str, err_scale)
def day_night_twin_run_no_obs_error(start, end, obs, f_name, its=1000): d = dc.DalecDataTwin(start, end, obs, err_scale=0.0) m = mc.DalecModel(d) assim_results, xa = m.find_min_tnc_cvt(d.xb, f_name + '_assim_res', maxits=its) d2 = dc.DalecDataTwin(start, 2013, obs) # Plot 4dvar time series ax, fig = p.plot_4dvar_twin('nee', d2, xa=xa) fig.savefig(f_name + '_nee.pdf', bbox_inches='tight') ax, fig = p.plot_4dvar_twin('nee_day', d2, xa=xa) fig.savefig(f_name + '_need.pdf', bbox_inches='tight') ax, fig = p.plot_4dvar_twin('nee_night', d2, xa=xa) fig.savefig(f_name + '_neen.pdf', bbox_inches='tight') ax, fig = p.plot_4dvar_twin('lai', d2, xa=xa) fig.savefig(f_name + '_lai.pdf', bbox_inches='tight') # Plot scatter plots of obs ax, fig = p.plot_scatter_twin('nee', xa, d2, len(d.I), 'f') fig.savefig(f_name + '_nee_scat.pdf', bbox_inches='tight') ax, fig = p.plot_scatter_twin('nee_day', xa, d2, len(d.I), 'f') fig.savefig(f_name + '_need_scat.pdf', bbox_inches='tight') ax, fig = p.plot_scatter_twin('nee_night', xa, d2, len(d.I), 'f') fig.savefig(f_name + '_neen_scat.pdf', bbox_inches='tight') ax, fig = p.plot_scatter_twin('lai', xa, d2, len(d.I), 'f') fig.savefig(f_name + '_lai_scat.pdf', bbox_inches='tight') # Plot error in analysis and background ax, fig = p.plottwinerr(d.x_truth, d.xb, xa) fig.savefig(f_name + '_twin_err.pdf', bbox_inches='tight') return 'all experimented'
def plotscatterobs(ob, pvals, dC, awindl, bfa='a'): """Plots scatter plot of obs vs model predicted values. :param ob: observation string corresponding to an observation in mod_class modobdict :param pvals: parameter and initial state values for generating modelled observations to judge against actual obs :param dC: DalecData class from data_class.py corresponding to length of data required :param awindl: Length of assimilation window in days as int :param bfa: string either 'b', 'f' or 'a'. Will compare observations to modelled observations in assimilation window ('b' or 'a') or in forecast period ('f'). :return: ax and fig object for plot """ sns.set_context('poster', font_scale=1.5, rc={ 'lines.linewidth': 1., 'lines.markersize': 6. }) fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 10)) sns.set_style('ticks') palette = sns.color_palette("colorblind", 11) m = mc.DalecModel(dC) mod_lst = m.mod_list(pvals) obs_lst = m.oblist(ob, mod_lst) y_obs = dC.ob_dict[ob] plt_ob_lst = (y_obs / y_obs) * obs_lst if bfa == 'b' or bfa == 'a': selection = xrange(0, awindl) elif bfa == 'f': selection = xrange(awindl, len(obs_lst)) else: raise Exception('Please check function input for bfa variable') ob_lst = plt_ob_lst[selection][np.isnan(y_obs[selection]) != True] y_obs = y_obs[selection][np.isnan(y_obs[selection]) != True] one_one = np.arange(int(min(min(y_obs), min(ob_lst))), int(max(max(y_obs), max(ob_lst)))) plt.plot(one_one, one_one, color=palette[0]) ax.plot(y_obs, ob_lst, 'o', color=palette[1]) error = np.sqrt(np.sum((y_obs - ob_lst)**2) / len(y_obs)) yhx = np.mean(y_obs - ob_lst) mod_obs_bar = np.mean(ob_lst) std_mod_obs = np.nanstd(ob_lst) obs_bar = np.mean(y_obs) std_obs = np.std(y_obs) rms = np.sqrt( np.sum([((ob_lst[x] - mod_obs_bar) - (y_obs[x] - obs_bar))**2 for x in range(len(y_obs))]) / len(y_obs)) corr_coef = (np.sum([((ob_lst[x]-mod_obs_bar)*(y_obs[x]-obs_bar)) for x in range(len(y_obs))]) / len(y_obs)) / \ (std_mod_obs*std_obs) plt.xlabel(ob.upper() + r' observations (g C m$^{-2}$ day$^{-1}$)') plt.ylabel(ob.upper() + ' model (g C m$^{-2}$ day$^{-1}$)') plt.title('mean(y-hx)=%.2f, rms=%.2f, corr_coef=%.2f' % (yhx, rms, corr_coef)) print bfa + '_error=%f, mean(y-hx)=%f, rms=%f, corr_coef=%f' % ( error, yhx, rms, corr_coef) #plt.xlim((-20, 15)) #plt.ylim((-20, 15)) return ax, fig
def plot_4dvar(ob, dC, xb=None, xa=None, erbars=1, awindl=None, obdict_a=None): """Plots a model predicted observation value for two initial states (xb and xa) and also the actual observations taken of the physical quantity (if available). :param ob: observation string corresponding to an observation in mod_class modobdict :param dC: DalecData class from data_class.py corresponding to length of data to be plotted :param xb: prior parameter and initial state values to plot initial model trajectory with :param xa: posterior parameter and initial state values to plot model trajectory after assimilation :param erbars: true or false value, to turn error bars on or off :param awindl: length of assimilation window as int in days, will plot a vertical line to show assimilation and forecast period :param obdict_a: If a different observation dictionary is required this can be specified here. :return: ax and fig object for plot """ sns.set_context(rc={'lines.linewidth': 0.8, 'lines.markersize': 6}) fig, ax = plt.subplots(nrows=1, ncols=1) m = mc.DalecModel(dC) palette = sns.color_palette("colorblind", 11) if xb != None: mod_lst = m.mod_list(xb) obs_lst = m.oblist(ob, mod_lst) ax.plot(dC.dates, obs_lst, color=palette[0]) if xa != None: mod_lst = m.mod_list(xa) obs_lst = m.oblist(ob, mod_lst) ax.plot(dC.dates, obs_lst, color=palette[1]) ob_dict = dC.ob_dict ob_err_dict = dC.ob_err_dict if ob in ob_dict.keys(): if erbars == True: ax.errorbar(dC.dates, ob_dict[ob], yerr=ob_err_dict[ob], fmt='o', label=ob + '_o', color=palette[2], alpha=0.7) else: ax.plot(dC.dates, ob_dict[ob], 'o', label=ob + '_o', color=palette[2]) if obdict_a != None: ax.plt.plot(dC.dates, obdict_a[ob], 'o') if awindl != None: ax.axvline(x=dC.dates[awindl], color='k', ls='dashed') ax.set_xlabel('Year') ax.set_ylabel(ob) plt.gcf().autofmt_xdate() return ax, fig
def test_costfn(alph=1e-9): """Test for cost and gradcost functions. """ d = dC.DalecData(50, 'nee') m = mc.DalecModel(d) gradj = m.gradcost(d.pvals) h = gradj * (np.linalg.norm(gradj))**(-1) j = m.cost(d.pvals) jalph = m.cost(d.pvals + alph * h) print(jalph - j) / (np.dot(alph * h, gradj)) assert (jalph - j) / (np.dot(alph * h, gradj)) < 1.0001
def run_4dvar_desroziers(pvals, east_west): d = dc.DalecData(2015, 2016, 'nee_day_'+east_west+', nee_night_'+east_west+', c_roo_'+east_west+',' ' c_woo_'+east_west+', clma, lai_'+east_west, nc_file='../../alice_holt_data/ah_data_daily_test_nee.nc') d.B = b d.ob_err_dict['clma'] = 0.33 * d.ob_err_dict['clma'] d.ob_err_dict['lai'] = 0.33 * d.ob_err_dict['lai'] m = mc.DalecModel(d) m.yoblist = perturb_obs(m.yoblist, m.yerroblist) out = m.find_min_tnc_cvt(pvals, dispp=0) return m.yoblist, pvals, out
def r_estimate(yoblist, pvals, out, east_west): d = dc.DalecData(2015, 2016, 'nee_day_'+east_west+', nee_night_'+east_west+', c_roo_'+east_west+',' ' c_woo_'+east_west+', clma, lai_'+east_west, nc_file='../../alice_holt_data/ah_data_daily_test_nee.nc') d.B = b m = mc.DalecModel(d) m.yoblist = yoblist pvallistxb = m.mod_list(pvals) pvallistxa = m.mod_list(out) yhxb = m.yoblist - m.hxcost(pvallistxb) yhxa = m.yoblist - m.hxcost(pvallistxa) r_estimate = np.dot(np.matrix(yhxa).T, np.matrix(yhxb)) return r_estimate
def plot_4dvar_twin(ob, dC, xb=None, xa=None, erbars=1, awindl=None, obdict_a=None): """Plots a model predicted observation value for two initial states (xb,xa) and also the actual observations taken of the physical quantity. Takes a ob string, two initial states (xb,xa), a dataClass and a start and finish time step. """ sns.set_context(rc={'lines.linewidth': .8, 'lines.markersize': 6}) fig, ax = plt.subplots(nrows=1, ncols=1) palette = sns.color_palette("colorblind", 11) m = mc.DalecModel(dC) mod_lst = m.mod_list(dC.x_truth) obs_lst = m.oblist(ob, mod_lst) ax.plot(dC.dates, obs_lst, color=palette[3]) if xb != None: mod_lst = m.mod_list(xb) obs_lst = m.oblist(ob, mod_lst) ax.plot(dC.dates, obs_lst, ':', color=palette[0]) if xa != None: mod_lst = m.mod_list(xa) obs_lst = m.oblist(ob, mod_lst) ax.plot(dC.dates, obs_lst, color=palette[1]) mod_lst = m.mod_list(dC.x_truth) obs_lst = m.oblist(ob, mod_lst) ax.plot(dC.dates, obs_lst, '--', color=palette[3]) ob_dict = obdict_a ob_err_dict = dC.ob_err_dict #if ob in ob_dict.keys(): # if erbars == True: ## ax.errorbar(dC.dates, ob_dict[ob], yerr=ob_err_dict[ob], # fmt='o', label=ob+'_o', color=palette[2], alpha=0.7) # else: # ax.plot(dC.dates, ob_dict[ob], 'o', label=ob+'_o', color=palette[2]) if obdict_a != None: ax.plot(dC.dates[0:awindl], obdict_a[ob], 'o', color=palette[2]) if awindl != None: ax.axvline(x=dC.dates[awindl], color='k', ls='dashed') ax.set_xlabel('Year') ax.set_ylabel(ob) plt.gcf().autofmt_xdate() return ax, fig
def __init__(self, start_date, end_date, ob_str, scale_nee=0, err_scale=0.25, nc_file='../../alice_holt_data/ah_data_daily_test.nc', params4opt=(10, 11, 14)): DalecData.__init__(self, start_date, end_date, ob_str, nc_file, scale_nee) self.m = mc.DalecModel(self) # Define truth and background self.x_truth = self.xb_ew_lai # self.edinburgh_median self.st_dev = 0.1 * cp.copy(self.x_truth) self.B = self.make_b(self.st_dev) # Make EDC B # b_cor = pickle.load(open('b_edc_cor.p', 'r')) # load correlation matrix from 2016 paper # b_std = self.make_b(np.sqrt(self.st_dev)) # Create diagonal matrix of standard deviations # self.B = np.dot(np.dot(b_std, b_cor), b_std) # Create correlated B # self.xb = self.random_pert(self.random_pert(self.x_truth)) #self.xb = np.array([2.53533992e-04, 5.85073161e-01, 7.43127332e-02, # 4.99707798e-01, 1.38993876e+00, 6.11913792e-05, # 2.58484324e-03, 2.79379720e-03, 8.72422101e-05, # 4.35144260e-02, 8.73669864e+01, 1.29813051e+02, # 3.87867223e-01, 4.69894281e+01, 2.78080852e+02, # 9.15080347e+01, 1.36269157e+02, 1.44176657e+02, # 6.71153814e+01, 2.42199267e+02, 4.96249386e+03, # 4.15128028e+02, 1.90797697e+03]) # Extract observations for assimilation self.ob_dict, self.ob_err_dict = self.create_twin_data( ob_str, err_scale) self.params4opt = np.array(params4opt) self.opt_bnds = tuple([self.bnds_tst[x] for x in params4opt]) self.opt_xb = np.array( [60.3, 149.17, 276.1] ) # np.array([0.0966, 59.3, 142.17, 276.1])# np.array([self.xb_ew_lai[x] for x in params4opt]) self.xb = cp.copy(self.xb_ew_lai) self.xb[self.params4opt] = self.opt_xb # self.B = pickle.load(open('b_edc.p', 'r')) self.opt_B = np.zeros((len(self.params4opt), len(self.params4opt))) for i in range(len(self.params4opt)): for j in range(len(self.params4opt)): self.opt_B[i, j] = self.B[self.params4opt[i], self.params4opt[j]]
def plotscatterobs(ob, pvals, dC, awindl, bfa='a'): """Plots scatter plot of obs vs model predicted values. Takes an initial parameter set, a dataClass (must have only desired ob for comparison specified in dC), assimilation window length and whether a comparison of background 'b', forecast 'f' or analysis 'a' is desired. """ sns.set_context('poster', font_scale=1.5, rc={ 'lines.linewidth': 1., 'lines.markersize': 6. }) fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 10)) sns.set_style('ticks') palette = sns.color_palette("colorblind", 11) m = mc.DalecModel(dC) mod_lst = m.mod_list(pvals) obs_lst = m.oblist(ob, mod_lst) y_obs = dC.ob_dict[ob] plt_ob_lst = (y_obs / y_obs) * obs_lst one_one = np.arange( int( min(min(y_obs[np.isnan(y_obs) != True]), min(plt_ob_lst[np.isnan(y_obs) != True]))), int( max(max(y_obs[np.isnan(y_obs) != True]), max(plt_ob_lst[np.isnan(y_obs) != True])))) plt.plot(one_one, one_one, color=palette[0]) if bfa == 'b' or bfa == 'a': ax.plot(y_obs[0:awindl], plt_ob_lst[0:awindl], 'o', color=palette[1]) error = np.sqrt( np.nansum((y_obs[0:awindl] - plt_ob_lst[0:awindl])**2) / len(y_obs[0:awindl])) yhx = np.nanmean(y_obs[0:awindl] - plt_ob_lst[0:awindl]) elif bfa == 'f': ax.plot(y_obs[awindl:], plt_ob_lst[awindl:], 'o', color=palette[1]) error = np.sqrt( np.nansum((y_obs[awindl:] - plt_ob_lst[awindl:])**2) / len(y_obs[awindl:])) yhx = np.nanmean(y_obs[awindl:] - plt_ob_lst[awindl:]) else: raise Exception('Please check function input for bfa variable') plt.xlabel(ob.upper() + r' observations (g C m$^{-2}$ day$^{-1}$)') plt.ylabel(ob.upper() + ' model (g C m$^{-2}$ day$^{-1}$)') #plt.title(bfa+'_error=%f, mean(y-hx)=%f' %(error,yhx)) print bfa + '_error=%f, mean(y-hx)=%f' % (error, yhx) #plt.xlim((-20, 15)) #plt.ylim((-20, 15)) return ax, fig
def test_linmod(gamma=1e1): """ Test from TLM to check it converges. """ d = dC.DalecData(731, 'nee') pvals = d.pvals m = mc.DalecModel(d) cx, matlist = m.linmod_list(pvals) pvals2 = pvals * (1 + 0.3 * gamma) cxdx = m.mod_list(pvals2)[-1] pvals3 = pvals * (0.3 * gamma) dxl = np.linalg.norm(np.dot(m.mfac(matlist, 730), pvals3.T)) dxn = np.linalg.norm(cxdx - cx - dxl) return dxl / dxn
def test_cost(alph=1e-8, vect=0): """Test for cost and gradcost functions. """ d = dC.DalecData(365, 'nee') m = mc.DalecModel(d) pvals = d.edinburghmean gradj = m.gradcost2(pvals) if vect == True: h = pvals * (np.linalg.norm(pvals))**(-1) else: h = gradj * (np.linalg.norm(gradj))**(-1) j = m.cost(pvals) jalph = m.cost(pvals + alph * h) print jalph - j print np.dot(alph * h, gradj) return (jalph - j) / (np.dot(alph * h, gradj))
def plot_obs(ob, pvals, dC): """Plots a specified observation using obs eqn in mod_class module. Takes an observation string, a dataClass (dC). """ sns.set_context(rc={'lines.linewidth': .8, 'lines.markersize': 6}) fig, ax = plt.subplots(nrows=1, ncols=1) m = mc.DalecModel(dC) mod_lst = m.mod_list(pvals) obs_lst = m.oblist(ob, mod_lst) palette = sns.color_palette("colorblind", 11) ax.plot(dC.dates, obs_lst, color=palette[0]) ax.set_xlabel('Year') ax.set_ylabel(ob) plt.gcf().autofmt_xdate() return ax, fig
def run_assimilation(yr): d = ahd.DalecData( yr, yr + 1, 'nee, nee_day, nee_night', nc_file='../../alice_holt_data/ah_data_daily_test_nee3.nc') m = mc.DalecModel(d) find_min, xa = m.find_min_tnc_cvt(d.xb_ew_lai_hi) result_dic = {} result_dic['time'] = [t.strftime('%m/%d/%Y') for t in d.dates] result_dic['incident_radiation'] = d.I.tolist() result_dic['t_mean'] = d.t_mean.tolist() result_dic['doy'] = d.D.tolist() mod_lst_xb = m.mod_list(d.xb_ew_lai_hi) result_dic['nee_day_xb'] = m.oblist('nee_day', mod_lst_xb).tolist() result_dic['nee_xb'] = m.oblist('nee', mod_lst_xb).tolist() mod_lst_xa = m.mod_list(xa) result_dic['nee_day_xa'] = m.oblist('nee_day', mod_lst_xa).tolist() result_dic['nee_xa'] = m.oblist('nee', mod_lst_xa).tolist() return result_dic
def return_a_mat(pick_dict): b = pick_dict['b_mat'] #1.5*pick_dict['b_mat'] # east data d = dc.DalecData( 2015, 2016, 'clma', nc_file='../../alice_holt_data/ah_data_daily_test_nee2.nc', scale_nee=1) d.B = b d.ob_dict = pick_dict['obs'] d.ob_err_dict = pick_dict['obs_err'] # obs err scaling # west data # obs err scaling # setup model m = mc.DalecModel(d) m.rmatrix = pick_dict['rmat'] a_cov = m.acovmat(pick_dict['xa']) return a_cov
def twin_exps(exp, f_name): if not os.path.exists(f_name): os.makedirs(f_name) if exp == 'a': obs = 'nee_day_east, nee_night_east' lab = '/twin_a' ob_dic = pickle.load(open('twin_obs_a025')) elif exp == 'b': obs = 'nee_day_east, nee_night_east, lai_east, clma' ob_dic = pickle.load(open('twin_obs_b025')) lab = '/twin_b' elif exp == 'c': obs = 'nee_day_east, nee_night_east, lai_east, clma, c_woo_east' ob_dic = pickle.load(open('twin_obs_c025')) lab = '/twin_c' d = dc.DalecDataTwin( 2015, 2016, obs, nc_file='../../alice_holt_data/ah_data_daily_test_nee2.nc', scale_nee=1, err_scale=0.25) d.ob_dict = ob_dic m = mc.DalecModel(d) m.rmatrix = r_mat_corr(m.yerroblist, m.ytimestep, m.y_strlst, m.rmatrix, corr=0.3, tau=2.)[1] # run DA scheme xa = m.find_min_tnc_cvt(d.xb, f_name + lab) ax, fig = p.plottwinerr(d.x_truth, d.xb, xa[1]) fig.savefig(f_name + lab + 'twin_err.pdf') ax, fig = p.plot_twinerr_red(d.x_truth, d.xb, xa[1]) fig.savefig(f_name + lab + 'twin_err_red.pdf') ax, fig = p.plot_twin_err(d.x_truth, d.xb, xa[1]) fig.savefig(f_name + lab + 'twin_err2.pdf', bbox_inches='tight') return xa
def plot_obs(ob, pvals, dC): """Plots a specified observation using obs eqn in mod_class module for a data class dC. :param ob: observation string corresponding to an observation in mod_class modobdict :param pvals: initial state and parameter values for which to run the model and generate modelled observations :param dC: DalecData class from data_class.py :return: ax and fig object for plot """ """Plots a specified observation using obs eqn in mod_class module. Takes an observation string, a dataClass (dC). """ sns.set_context(rc={'lines.linewidth': .8, 'lines.markersize': 6}) fig, ax = plt.subplots(nrows=1, ncols=1) m = mc.DalecModel(dC) mod_lst = m.mod_list(pvals) obs_lst = m.oblist(ob, mod_lst) palette = sns.color_palette("colorblind", 11) ax.plot(dC.dates, obs_lst, color=palette[0]) ax.set_xlabel('Year') ax.set_ylabel(ob) plt.gcf().autofmt_xdate() return ax, fig
def east_west_run(f_name, ob_list, east_west): ob_str = '' for ob in ob_list: if ob == 'clma': ob_str += ob + ',' else: ob_str += ob + '_' + east_west + ',' d = dc.DalecData(2015, 2016, ob_str) d.B = d.make_b(d.edinburgh_std) m = mc.DalecModel(d) assim_results, xa = m.find_min_tnc_cvt(d.edinburgh_mean, f_name + '_assim_res') # Plot 4dvar time series ax, fig = p.plot_4dvar('nee', d, xb=d.edinburgh_mean, xa=xa) fig.savefig(f_name + '_nee.png', bbox_inches='tight') ax, fig = p.plot_4dvar('nee_day', d, xb=d.edinburgh_mean, xa=xa) fig.savefig(f_name + '_need.png', bbox_inches='tight') ax, fig = p.plot_4dvar('nee_night', d, xb=d.edinburgh_mean, xa=xa) fig.savefig(f_name + '_neen.png', bbox_inches='tight') ax, fig = p.plot_4dvar('lai', d, xb=d.edinburgh_mean, xa=xa) fig.savefig(f_name + '_lai.png', bbox_inches='tight') ax, fig = p.plot_4dvar('c_woo', d, xb=d.edinburgh_mean, xa=xa) fig.savefig(f_name + '_cwoo.png', bbox_inches='tight') ax, fig = p.plot_4dvar('c_roo', d, xb=d.edinburgh_mean, xa=xa) fig.savefig(f_name + '_croo.png', bbox_inches='tight') # Plot scatter plots of obs ax, fig = p.plot_scatter('nee_day', xa, d, len(d.I), 'a') fig.savefig(f_name + '_need_scat.png', bbox_inches='tight') ax, fig = p.plot_scatter('nee_night', xa, d, len(d.I), 'a') fig.savefig(f_name + '_neen_scat.png', bbox_inches='tight') # Plot error in analysis and background ax, fig = p.plot_a_inc(d.edinburgh_mean, xa, east_west) fig.savefig(f_name + '_xa_inc.png', bbox_inches='tight') return 'all experimented'
def east_west_run_b(f_name, east_west, net_file="None"): if east_west == 'east': obs = 'nee_day_east, nee_night_east, clma, lai_east, c_woo_east, c_roo_east' elif east_west == 'west': obs = 'nee_day_west, nee_night_west, clma, lai_west, c_woo_west, c_roo_west' if net_file != "None": d = dc.DalecData(2015, 2016, obs, nc_file=net_file) else: d = dc.DalecData(2015, 2016, obs) m = mc.DalecModel(d) assim_results, xa = m.find_min_tnc_cvt(d.edinburgh_mean, f_name + '_assim_res') # Plot 4dvar time series ax, fig = p.plot_4dvar('nee', d, xb=d.edinburgh_mean, xa=xa) fig.savefig(f_name + '_nee.png', bbox_inches='tight') ax, fig = p.plot_4dvar('nee_day', d, xb=d.edinburgh_mean, xa=xa) fig.savefig(f_name + '_need.png', bbox_inches='tight') ax, fig = p.plot_4dvar('nee_night', d, xb=d.edinburgh_mean, xa=xa) fig.savefig(f_name + '_neen.png', bbox_inches='tight') ax, fig = p.plot_4dvar('lai', d, xb=d.edinburgh_mean, xa=xa) fig.savefig(f_name + '_lai.png', bbox_inches='tight') ax, fig = p.plot_4dvar('c_woo', d, xb=d.edinburgh_mean, xa=xa) fig.savefig(f_name + '_cwoo.png', bbox_inches='tight') ax, fig = p.plot_4dvar('c_roo', d, xb=d.edinburgh_mean, xa=xa) fig.savefig(f_name + '_croo.png', bbox_inches='tight') # Plot scatter plots of obs ax, fig = p.plot_scatter('nee_day', xa, d, len(d.I), 'a') fig.savefig(f_name + '_need_scat.png', bbox_inches='tight') ax, fig = p.plot_scatter('nee_night', xa, d, len(d.I), 'a') fig.savefig(f_name + '_neen_scat.png', bbox_inches='tight') # Plot error in analysis and background ax, fig = p.plot_a_inc(d.edinburgh_mean, xa, east_west) fig.savefig(f_name + '_xa_inc.png', bbox_inches='tight') return 'all experimented'
def save_paper_plots(f_name, exp_name): east = pickle.load(open(exp_name + 'east_assim', 'r')) west = pickle.load(open(exp_name + 'west_assim', 'r')) #b_cor = pickle.load(open('b_edc_cor.p', 'r')) #b_std = np.sqrt(np.diag(pickle.load(open('b_edc.p', 'r')))) #b_std[10] = 0.2*b_std[10] #b_std[1] = 0.2*b_std[1] #b_std[2] = 0.1*b_std[2] #b_std[0:17] = b_std[0:17]*0.5 #D = np.zeros_like(b_cor) #np.fill_diagonal(D, b_std) #b = 0.6*np.dot(np.dot(D, b_cor), D) #*0.6 b = east['b_mat'] # east data de = dc.DalecData( 2015, 2016, 'clma', nc_file='../../alice_holt_data/ah_data_daily_test_nee2.nc', scale_nee=1) de.B = b de.ob_dict = east['obs'] de.ob_err_dict = east['obs_err'] # obs err scaling # west data dw = dc.DalecData( 2015, 2016, 'clma', nc_file='../../alice_holt_data/ah_data_daily_test_nee2.nc', scale_nee=1) dw.B = b dw.ob_dict = west['obs'] dw.ob_err_dict = west['obs_err'] # obs err scaling # setup model me = mc.DalecModel(de) me.rmatrix = r_mat_corr(me.yerroblist, me.ytimestep, me.y_strlst, me.rmatrix, corr=0.3, tau=2.)[1] mw = mc.DalecModel(dw) mw.rmatrix = r_mat_corr(mw.yerroblist, mw.ytimestep, mw.y_strlst, mw.rmatrix, corr=0.3, tau=2.)[1] # a_east = pickle.load(open('a_east.p', 'r')) # a_west = pickle.load(open('a_west.p', 'r')) a_east = me.acovmat(east['xa']) a_west = mw.acovmat(west['xa']) ax, fig = p.plot_east_west_paper( 'rh', east['xa'], west['xa'], de, dw, a_east, a_west, y_label=r'Heterotrophic respiration (g C m$^{-2}$ day$^{-1}$)') fig.savefig(f_name + 'rh.pdf', bbox_inches='tight') ax, fig = p.plot_east_west_paper( 'nee_day', east['xa'], west['xa'], de, dw, a_east, a_west, y_label=r'NEE$_{day}$ (g C m$^{-2}$ day$^{-1}$)', y_lim=[-15, 5]) fig.savefig(f_name + 'nee_day.pdf', bbox_inches='tight') ax, fig = p.plot_east_west_paper_cum( 'nee_day', east['xa'], west['xa'], de, dw, a_east, a_west, y_label='Cumulative NEE (g C m$^{-2}$ day$^{-1}$)') fig.savefig(f_name + 'nee_day_cum.pdf', bbox_inches='tight') ax, fig = p.plot_east_west_paper_cum( 'nee', east['xa'], west['xa'], de, dw, a_east, a_west, y_label='Cumulative NEE (g C m$^{-2}$ day$^{-1}$)') fig.savefig(f_name + 'nee_cum.pdf', bbox_inches='tight') ax, fig = p.plot_east_west_paper( 'nee_night', east['xa'], west['xa'], de, dw, a_east, a_west, y_label=r'NEE$_{night}$ (g C m$^{-2}$ day$^{-1}$)') fig.savefig(f_name + 'nee_night.pdf', bbox_inches='tight') ax, fig = p.plot_east_west_paper( 'gpp', east['xa'], west['xa'], de, dw, a_east, a_west, y_label=r'Gross primary production (g C m$^{-2}$ day$^{-1}$)') fig.savefig(f_name + 'gpp.pdf', bbox_inches='tight') ax, fig = p.plot_east_west_paper_cum( 'gpp', east['xa'], west['xa'], de, dw, a_east, a_west, y_label='Cumulative GPP (g C m$^{-2}$ day$^{-1}$)') fig.savefig(f_name + 'gpp_cum.pdf', bbox_inches='tight') ax, fig = p.plot_east_west_paper('lai', east['xa'], west['xa'], de, dw, a_east, a_west, y_label=r'Leaf area index') fig.savefig(f_name + 'lai.pdf', bbox_inches='tight') ax, fig = p.plot_east_west_paper( 'c_woo', east['xa'], west['xa'], de, dw, a_east, a_west, y_label=r'Woody biomass and coarse root carbon (g C m$^{-2}$)', y_lim=[9000, 14500]) fig.savefig(f_name + 'c_woo.pdf', bbox_inches='tight') ax, fig = p.plot_east_west_paper( 'ra', east['xa'], west['xa'], de, dw, a_east, a_west, y_label=r'Autotrophic respiration (g C m$^{-2}$ day$^{-1}$)') fig.savefig(f_name + 'ra.pdf', bbox_inches='tight') ax, fig = p.plot_east_west_paper( 'rt', east['xa'], west['xa'], de, dw, a_east, a_west, y_label=r'Total ecosystem respiration (g C m$^{-2}$ day$^{-1}$)') fig.savefig(f_name + 'rt.pdf', bbox_inches='tight') ax, fig = p.plot_east_west_paper_cum( 'rt', east['xa'], west['xa'], de, dw, a_east, a_west, y_label='Cumulative ecosystem respiration (g C m$^{-2}$ day$^{-1}$)') fig.savefig(f_name + 'rt_cum.pdf', bbox_inches='tight') ax, fig = p.plot_inc_east_west(east['xb'], east['xa'], west['xa']) fig.savefig(f_name + 'xa_inc.pdf', bbox_inches='tight') return 'done!'
def east_west_joint_run_nee_err_a(xb, f_name, nee_scale=0, clma_er=1., lai_er=1., need_er=1., neen_er=1., cr_er=1., cw_er=1.): f_name += 'clmaer%r_laier%r_needer%r_neener%r_crer%r_cwer%r' % ( clma_er, lai_er, need_er, neen_er, cr_er, cw_er) # Construct B b = 0.6 * pickle.load(open('a_cov.p', 'r')) # b = pickle.load(open('b_edc.p', 'r')) # east data de = dc.DalecData( 2015, 2016, 'nee_day_east, nee_night_east, c_woo_east, clma, lai_east', nc_file='../../alice_holt_data/ah_data_daily_test_nee.nc', scale_nee=nee_scale) de.B = b # obs err scaling de.ob_err_dict['clma'] = clma_er * de.ob_err_dict['clma'] de.ob_err_dict['lai'] = lai_er * de.ob_err_dict['lai'] de.ob_err_dict['nee_day'] = need_er * de.ob_err_dict['nee_day'] de.ob_err_dict['nee_night'] = neen_er * de.ob_err_dict['nee_night'] # de.ob_err_dict['c_roo'] = cr_er * de.ob_err_dict['c_roo'] de.ob_err_dict['c_woo'] = cw_er * de.ob_err_dict['c_woo'] # west data dw = dc.DalecData( 2015, 2016, 'nee_day_west, nee_night_west, c_woo_west, clma, lai_west', nc_file='../../alice_holt_data/ah_data_daily_test_nee.nc', scale_nee=nee_scale) dw.B = b # obs err scaling dw.ob_err_dict['clma'] = clma_er * dw.ob_err_dict['clma'] dw.ob_err_dict['lai'] = lai_er * dw.ob_err_dict['lai'] dw.ob_err_dict['nee_day'] = need_er * dw.ob_err_dict['nee_day'] dw.ob_err_dict['nee_night'] = neen_er * dw.ob_err_dict['nee_night'] # dw.ob_err_dict['c_roo'] = cr_er * dw.ob_err_dict['c_roo'] dw.ob_err_dict['c_woo'] = cw_er * dw.ob_err_dict['c_woo'] # setup model me = mc.DalecModel(de) me.rmatrix = r_mat_corr(me.yerroblist, me.ytimestep, me.y_strlst, me.rmatrix, corr=0.3, tau=2.)[1] mw = mc.DalecModel(dw) mw.rmatrix = r_mat_corr(mw.yerroblist, mw.ytimestep, mw.y_strlst, mw.rmatrix, corr=0.3, tau=2.)[1] # run DA scheme xa_e = me.find_min_tnc_cvt(xb, f_name + 'east_assim') xa_w = mw.find_min_tnc_cvt(xb, f_name + 'west_assim') # save plots save_plots(f_name, xb, xa_e[1], xa_w[1], de, dw, me, mw) return 'done'
def plot_scatter_twin(ob, pvals, dC, awindl, bfa='a'): """Plots scatter plot of obs vs model predicted values. Takes an initial parameter set, a dataClass (must have only desired ob for comparison specified in dC), assimilation window length and whether a comparison of background 'b', forecast 'f' or analysis 'a' is desired. """ sns.set_context('poster', font_scale=1.5, rc={ 'lines.linewidth': 1., 'lines.markersize': 6. }) fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 10)) sns.set_style('ticks') palette = sns.color_palette("colorblind", 11) m = mc.DalecModel(dC) mod_lst = m.mod_list(pvals) mod_lst_truth = m.mod_list(dC.x_truth) obs_lst = m.oblist(ob, mod_lst) y_obs = m.oblist(ob, mod_lst_truth) one_one = np.arange(int(min(min(y_obs), min(obs_lst))), int(max(max(y_obs), max(obs_lst)))) plt.plot(one_one, one_one, color=palette[0]) if bfa == 'b' or bfa == 'a': ax.plot(y_obs[0:awindl], obs_lst[0:awindl], 'o', color=palette[1]) error = np.sqrt( np.nansum((y_obs[0:awindl] - obs_lst[0:awindl])**2) / len(y_obs[0:awindl])) yhx = np.nanmean(y_obs[0:awindl] - obs_lst[0:awindl]) mod_obs_bar = np.mean(obs_lst[0:awindl]) std_mod_obs = np.std(obs_lst[0:awindl]) obs_bar = np.mean(y_obs[0:awindl]) std_obs = np.std(y_obs[0:awindl]) rms = np.sqrt( np.sum([((obs_lst[x] - mod_obs_bar) - (y_obs[x] - obs_bar))**2 for x in xrange(awindl)]) / len(awindl)) corr_coef = (np.sum([((obs_lst[x]-mod_obs_bar)*(y_obs[x]-obs_bar)) for x in xrange(awindl)])/len(awindl))\ /(std_mod_obs*std_obs) elif bfa == 'f': ax.plot(y_obs[awindl:], obs_lst[awindl:], 'o', color=palette[1]) error = np.sqrt( np.nansum( (y_obs[awindl:] - obs_lst[awindl:])**2) / len(y_obs[awindl:])) yhx = np.nanmean(y_obs[awindl:] - obs_lst[awindl:]) mod_obs_bar = np.mean(obs_lst[awindl:]) std_mod_obs = np.std(obs_lst[awindl:]) obs_bar = np.mean(y_obs[awindl:]) std_obs = np.std(y_obs[awindl:]) rms = np.sqrt( np.sum([((obs_lst[x] - mod_obs_bar) - (y_obs[x] - obs_bar))**2 for x in xrange(awindl, len(y_obs))]) / len(y_obs[awindl:])) corr_coef = (np.sum([((obs_lst[x] - mod_obs_bar) * (y_obs[x] - obs_bar)) for x in xrange(awindl, len(y_obs))]) / len(y_obs[awindl:])) / (std_mod_obs * std_obs) else: raise Exception('Please check function input for bfa variable') plt.xlabel(ob.upper() + r' observations (g C m$^{-2}$ day$^{-1}$)') plt.ylabel(ob.upper() + ' model (g C m$^{-2}$ day$^{-1}$)') plt.title('mean(y-hx)=%.2f, rms=%.2f, corr_coef=%.2f' % (yhx, rms, corr_coef)) print bfa + '_error=%f, mean(y-hx)=%f, rms=%f, corr_coef=%f' % ( error, yhx, rms, corr_coef) #plt.xlim((-20, 15)) #plt.ylim((-20, 15)) return ax, fig