def __init__(self, **keywords):

        # Calls the regular constructor
        super(ConfIOMB, self).__init__(**keywords)

        # Setup a html layout for generating web views of the results
        pages = []

        # Mean State page
        pages.append(post.HtmlPage("MeanState", "Mean State"))
        pages[-1].setHeader("CNAME / RNAME / MNAME")
        pages[-1].setSections(
            ["Period mean at surface", "Mean regional depth profiles"])
        pages.append(post.HtmlAllModelsPage("AllModels", "All Models"))
        pages[-1].setHeader("CNAME / RNAME")
        pages[-1].setSections([])
        pages[-1].setRegions(self.regions)
        pages.append(post.HtmlPage("DataInformation", "Data Information"))
        pages[-1].setSections([])
        pages[-1].text = "\n"
        with Dataset(self.source) as dset:
            for attr in dset.ncattrs():
                pages[-1].text += "<p><b>&nbsp;&nbsp;%s:&nbsp;</b>%s</p>\n" % (
                    attr, dset.getncattr(attr).encode('ascii', 'ignore'))
        self.layout = post.HtmlLayout(pages, self.longname)
Beispiel #2
0
    def __init__(self, **keywords):

        # Ugly, but this is how we call the Confrontation constructor
        super(ConfCO2, self).__init__(**keywords)
        self.regions = ['global']

        self.lat_bands = np.asarray(self.keywords.get(
            "lat_bands", "-90,-60,-23,0,+23,+60,+90").split(","),
                                    dtype=float)
        sec = []
        for i in range(len(self.lat_bands) - 1):
            sec.append("Latitude Band %d to %d [ppm]" %
                       (self.lat_bands[i], self.lat_bands[i + 1]))
        sec = sec[::-1]

        # Setup a html layout for generating web views of the results
        pages = []

        # Mean State page
        pages.append(post.HtmlPage("MeanState", "Mean State"))
        pages[-1].setHeader("CNAME / RNAME / MNAME")
        pages[-1].setSections([
            "Summary",
        ] + sec)
        pages.append(post.HtmlAllModelsPage("AllModels", "All Models"))
        pages[-1].setHeader("CNAME / RNAME")
        pages[-1].setSections([])
        pages[-1].setRegions(self.regions)
        pages.append(post.HtmlPage("DataInformation", "Data Information"))
        pages[-1].setSections([])
        pages[-1].text = "\n"
        with Dataset(self.source) as dset:
            for attr in dset.ncattrs():
                pages[-1].text += "<p><b>&nbsp;&nbsp;%s:&nbsp;</b>%s</p>\n" % (
                    attr, dset.getncattr(attr).encode('ascii', 'ignore'))
        self.layout = post.HtmlLayout(pages, self.longname)

        # Adding a member variable called basins, add them as regions
        r = Regions()
        self.pulse_dir = "/".join(
            self.source.split("/")[:-2] + ["PulseEmulation"])
        self.pulse_regions = r.addRegionNetCDF4(
            os.path.join(self.pulse_dir, "AtmosphericPulseRegions.nc"))

        # Emulation specific initialization
        self.sites = [
            site.strip()
            for site in self.keywords.get("sites", None).upper().split(",")
        ]
        self.map = None
        if self.sites:
            self.map = [
                self.lbls.index(site) for site in self.sites
                if site in self.lbls
            ]
            self.lbls = [self.lbls[i] for i in self.map]
Beispiel #3
0
 def plot_summary(self):
     for j, site in enumerate(self.site_name):
         print('Process score_' + ''.join(site) + '_No.' + str(j) + '!')
         figname = self.filedir + 'score' + '/' + ''.join(
             site) + 'summary_score.png'
         data = (self.type_score[:, j, :] + self.type_score[:, j, :]
                 )  # how to define this caculations???
         # print(data123.shape,len(self.models_name), len(self.variable_list))
         Post.BenchmarkSummaryFigure(self.models_name, self.variable_list,
                                     data, figname)
         plt.close('all')
Beispiel #4
0
    def __init__(self, **kwargs):

        self.name = kwargs.get("name", None)
        self.output_path = kwargs.get("output_path", "./")
        self.master = True
        self.rate = 0.01
        self.CO2_0 = 284.7  # [ppm]
        self.regions = ['global']
        self.keywords = kwargs

        pages = []
        pages.append(post.HtmlPage("Feedback", "By Model"))
        pages[-1].setHeader("CNAME / RNAME / MNAME")
        pages[-1].setSections(
            ["Global states and fluxes", "Sensitivity parameters"])
        pages[-1].setRegions(self.regions)
        pages.append(post.HtmlAllModelsPage("AllModels", "All Models"))
        pages[-1].setHeader("CNAME / RNAME / MNAME")
        pages[-1].setSections([])
        pages[-1].setRegions(self.regions)

        self.layout = post.HtmlLayout(pages, self.name)
Beispiel #5
0
    def __init__(self, **keywords):

        # Calls the regular constructor
        super(ConfPEcAn, self).__init__(**keywords)

        obs = Variable(filename=self.source,
                       variable_name=self.variable,
                       alternate_vars=self.alternate_vars,
                       convert_calendar=False)
        self.years = np.asarray(
            [t.year for t in cftime.num2date(obs.time, "days since 1850-1-1")],
            dtype=int)
        self.years = np.unique(self.years)

        # Setup a html layout for generating web views of the results
        pages = []

        # Mean State page
        pages.append(post.HtmlPage("MeanState", "Mean State"))
        pages[-1].setHeader("CNAME / RNAME / MNAME")
        pages[-1].setSections([
            "Seasonal Diurnal Cycle",
        ] + ["%d" % y for y in self.years])
        pages.append(post.HtmlAllModelsPage("AllModels", "All Models"))
        pages[-1].setHeader("CNAME / RNAME")
        pages[-1].setSections([])
        pages[-1].setRegions(self.regions)
        pages.append(post.HtmlPage("DataInformation", "Data Information"))
        pages[-1].setSections([])
        pages[-1].text = "\n"
        with Dataset(self.source) as dset:
            for attr in dset.ncattrs():
                a = dset.getncattr(attr)
                if 'astype' in dir(a): a = a.astype('str')
                if 'encode' in dir(a): a = a.encode('ascii', 'ignore')
                pages[-1].text += "<p><b>&nbsp;&nbsp;%s:&nbsp;</b>%s</p>\n" % (
                    attr, a)
        self.layout = post.HtmlLayout(pages, self.longname)
Beispiel #6
0
    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 compositePlots(self):

        if not self.master: return

        # get the HTML page
        page = [
            page for page in self.layout.pages if "MeanState" in page.name
        ][0]

        # composite profile plot
        f1 = {}
        a1 = {}
        u1 = None
        for fname in glob.glob("%s/*.nc" % self.output_path):
            with Dataset(fname) as dset:
                if "MeanState" not in dset.groups: continue
                group = dset.groups["MeanState"]
                variables = getVariableList(group)
                for region in self.regions:

                    vname = "profile_of_%s_over_%s" % (self.variable, region)
                    if vname in variables:
                        if not f1.has_key(region):
                            f1[region], a1[region] = plt.subplots(
                                figsize=(5, 5), tight_layout=True)
                        var = Variable(filename=fname,
                                       variable_name=vname,
                                       groupname="MeanState")
                        u1 = var.unit
                        page.addFigure("Mean regional depth profiles",
                                       "profile",
                                       "RNAME_profile.png",
                                       side="REGIONAL MEAN PROFILE",
                                       legend=False)
                        a1[region].plot(var.data,
                                        var.depth,
                                        '-',
                                        color=dset.getncattr("color"))
        for key in f1.keys():
            a1[key].set_xlabel("%s [%s]" % (self.variable, u1))
            a1[key].set_ylabel("depth [m]")
            a1[key].invert_yaxis()
            f1[key].savefig("%s/%s_profile.png" % (self.output_path, key))
        plt.close()

        # spatial distribution Taylor plot
        models = []
        colors = []
        corr = {}
        std = {}
        has_std = False
        for fname in glob.glob("%s/*.nc" % self.output_path):
            with Dataset(fname) as dset:
                models.append(dset.getncattr("name"))
                colors.append(dset.getncattr("color"))
                if "MeanState" not in dset.groups: continue
                dset = dset.groups["MeanState"]
                for region in self.regions:
                    if not std.has_key(region): std[region] = []
                    if not corr.has_key(region): corr[region] = []
                    key = []
                    if "scalars" in dset.groups:
                        key = [
                            v for v in dset.groups["scalars"].variables.keys()
                            if ("Spatial Distribution Score" in v
                                and region in v)
                        ]
                    if len(key) > 0:
                        has_std = True
                        sds = dset.groups["scalars"].variables[key[0]]
                        corr[region].append(sds.getncattr("R"))
                        std[region].append(sds.getncattr("std"))

        if has_std:

            # Legends
            def _alphabeticalBenchmarkFirst(key):
                key = key[0].upper()
                if key == "BENCHMARK": return 0
                return key

            tmp = sorted(zip(models, colors), key=_alphabeticalBenchmarkFirst)
            fig, ax = plt.subplots()
            for model, color in tmp:
                ax.plot(0, 0, 'o', mew=0, ms=8, color=color, label=model)
            handles, labels = ax.get_legend_handles_labels()
            plt.close()
            ncol = np.ceil(float(len(models)) / 11.).astype(int)
            fig, ax = plt.subplots(figsize=(3. * ncol, 2.8), tight_layout=True)
            ax.legend(handles,
                      labels,
                      loc="upper right",
                      ncol=ncol,
                      fontsize=10,
                      numpoints=1)
            ax.axis('off')
            fig.savefig("%s/legend_spatial_variance.png" % self.output_path)
            plt.close()

            page.addFigure("Period mean at surface",
                           "spatial_variance",
                           "RNAME_spatial_variance.png",
                           side="SPATIAL TAYLOR DIAGRAM",
                           legend=False)
            page.addFigure("Period mean at surface",
                           "legend_spatial_variance",
                           "legend_spatial_variance.png",
                           side="MODEL COLORS",
                           legend=False)
            if "Benchmark" in models: colors.pop(models.index("Benchmark"))
            for region in self.regions:
                if not (std.has_key(region) and corr.has_key(region)): continue
                if len(std[region]) != len(corr[region]): continue
                if len(std[region]) == 0: continue
                fig = plt.figure(figsize=(6.0, 6.0))
                post.TaylorDiagram(np.asarray(std[region]),
                                   np.asarray(corr[region]), 1.0, fig, colors)
                fig.savefig("%s/%s_spatial_variance.png" %
                            (self.output_path, region))
                plt.close()
Beispiel #9
0
    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()