def modelPlots(self, m): # some of the plots can be generated using the standard # routine, with some modifications super(ConfRunoff, self).modelPlots(m) # bname = os.path.join(self.output_path, "%s_Benchmark.nc" % (self.name)) fname = os.path.join(self.output_path, "%s_%s.nc" % (self.name, m.name)) # get the HTML page page = [ page for page in self.layout.pages if "MeanState" in page.name ][0] if not os.path.isfile(bname): return if not os.path.isfile(fname): return obs = Variable(filename=bname, variable_name="runoff", groupname="MeanState") mod = Variable(filename=fname, variable_name="runoff", groupname="MeanState") for i, basin in enumerate(self.basins): page.addFigure("Spatially integrated regional mean", basin, "MNAME_global_%s.png" % basin, basin, False, longname=basin) fig, ax = plt.subplots(figsize=(6.8, 2.8), tight_layout=True) ax.plot(obs.time / 365 + 1850, obs.data[:, i], lw=2, color='k', alpha=0.5) ax.plot(mod.time / 365 + 1850, mod.data[:, i], lw=2, color=m.color) ax.grid() ax.set_ylabel(post.UnitStringToMatplotlib(obs.unit)) fig.savefig( os.path.join(self.output_path, "%s_global_%s.png" % (m.name, basin))) plt.close()
def determinePlotLimits(self): # Pick limit type max_str = "up99" min_str = "dn99" if self.keywords.get("limit_type", "99per") == "minmax": max_str = "max" min_str = "min" # Determine the min/max of variables over all models limits = {} for fname in glob.glob("%s/*.nc" % self.output_path): with Dataset(fname) as dataset: if "MeanState" not in dataset.groups: continue group = dataset.groups["MeanState"] variables = [ v for v in group.variables.keys() if (v not in group.dimensions.keys() and "_bnds" not in v and group.variables[v][...].size > 1) ] for vname in variables: var = group.variables[vname] pname = vname.split("_")[0] if "_over_" in vname: region = vname.split("_over_")[-1] if not limits.has_key(pname): limits[pname] = {} if not limits[pname].has_key(region): limits[pname][region] = {} limits[pname][region]["min"] = +1e20 limits[pname][region]["max"] = -1e20 limits[pname][region][ "unit"] = post.UnitStringToMatplotlib( var.getncattr("units")) limits[pname][region]["min"] = min( limits[pname][region]["min"], var.getncattr("min")) limits[pname][region]["max"] = max( limits[pname][region]["max"], var.getncattr("max")) else: if not limits.has_key(pname): limits[pname] = {} limits[pname]["min"] = +1e20 limits[pname]["max"] = -1e20 limits[pname][ "unit"] = post.UnitStringToMatplotlib( var.getncattr("units")) limits[pname]["min"] = min(limits[pname]["min"], var.getncattr(min_str)) limits[pname]["max"] = max(limits[pname]["max"], var.getncattr(max_str)) # Another pass to fix score limits for pname in limits.keys(): if "score" in pname: if "min" in limits[pname].keys(): limits[pname]["min"] = 0. limits[pname]["max"] = 1. else: for region in limits[pname].keys(): limits[pname][region]["min"] = 0. limits[pname][region]["max"] = 1. self.limits = limits # Second pass to plot legends cmaps = {"bias": "seismic", "rmse": "YlOrRd"} for pname in limits.keys(): # Pick colormap cmap = self.cmap if cmaps.has_key(pname): cmap = cmaps[pname] elif "score" in pname: cmap = "RdYlGn" # Need to symetrize? if pname in ["bias"]: vabs = max(abs(limits[pname]["min"]), abs(limits[pname]["min"])) limits[pname]["min"] = -vabs limits[pname]["max"] = vabs # Some plots need legends if pname in [ "timeint", "bias", "biasscore", "rmse", "rmsescore", "timelonint" ]: if limits[pname].has_key("min"): fig, ax = plt.subplots(figsize=(6.8, 1.0), tight_layout=True) post.ColorBar(ax, vmin=limits[pname]["min"], vmax=limits[pname]["max"], label=limits[pname]["unit"], cmap=cmap) fig.savefig("%s/legend_%s.png" % (self.output_path, pname)) plt.close() else: fig, ax = plt.subplots(figsize=(6.8, 1.0), tight_layout=True) post.ColorBar(ax, vmin=limits[pname]["global"]["min"], vmax=limits[pname]["global"]["max"], label=limits[pname]["global"]["unit"], cmap=cmap) fig.savefig("%s/legend_%s.png" % (self.output_path, pname)) plt.close()
def modelPlots(self, m): bname = "%s/%s_Benchmark.nc" % (self.output_path, self.name) fname = "%s/%s_%s.nc" % (self.output_path, self.name, m.name) if not os.path.isfile(bname): return if not os.path.isfile(fname): return # get the HTML page page = [ page for page in self.layout.pages if "MeanState" in page.name ][0] page.priority = ["Beginning", "Ending", "Strength", "Score", "Overall"] for y in self.years: # ---------------------------------------------------------------- # plt.figure(figsize=(5, 5), tight_layout=True) has_data = False for name, color, alpha in zip([bname, fname], ['k', m.color], [0.6, 1.0]): try: v = Variable(filename=name, variable_name="mag%d" % y, groupname="MeanState") has_data = True except: continue plt.polar(v.time / 365. * 2 * np.pi, v.data, '-', color=color, alpha=alpha, lw=2) if has_data: plt.xticks(bnd_months[:-1] / 365. * 2 * np.pi, lbl_months) plt.ylim(0, self.limits["mag"]) plt.savefig("%s/%s_mag%d.png" % (self.output_path, m.name, y)) page.addFigure("%d" % y, "mag%d" % y, "MNAME_mag%d.png" % y, side="DIURNAL MAGNITUDE", legend=False) plt.close() # ---------------------------------------------------------------- # fig, ax = plt.subplots(figsize=(8, 5), tight_layout=True) has_data = False unit = "" for name, color, alpha, lbl in zip([bname, fname], ['k', m.color], [0.6, 1.0], ['Benchmark', m.name]): try: v = Variable(filename=name, variable_name="cycle%d" % y, groupname="MeanState") has_data = True unit = v.unit except: continue v.plot(ax, color=color, alpha=alpha, lw=2, label=lbl) if has_data: ax.set_xticks(np.linspace(0, 1, 9) / 365 + 1850) ax.set_xticklabels( ["%2d:00" % t for t in np.linspace(0, 24, 9)]) ax.set_ylim(self.limits['cycle']['min'], self.limits['cycle']['max']) ax.grid(True) ax.set_ylabel(post.UnitStringToMatplotlib(unit)) ax.set_xlabel("local time") ax.legend(bbox_to_anchor=(0, 1.005, 1, 0.25), loc='lower left', mode='expand', ncol=2, borderaxespad=0, frameon=False) plt.savefig("%s/%s_cycle%d.png" % (self.output_path, m.name, y)) page.addFigure("%d" % y, "cycle%d" % y, "MNAME_cycle%d.png" % y, side="SEASONAL DIURNAL CYCLE", legend=False) plt.close()