Esempio n. 1
0
def plotConvergence(ax, locName):
    locId = locations['locId'][locations['locName'] == locName][0]
    locLon = locations['locLon'][locations['locId'] == locId][0]
    locLat = locations['locLat'][locations['locId'] == locId][0]

    records = database.locationRecords(db, str(locId))
    recs = records['wspd'][records['wspd'] > 0]
    data = np.zeros(int(NumSimulations * 365.25))
    data[-len(recs):] = recs
    sortedmax = np.sort(data)
    emprp = empReturnPeriod(data)
    dd, rr = bootstrap(data, n=100)
    ax.plot(emprp[-10000:], data[-10000:], color='k', label="Mean ARI")
    ax.fill_between(rr[0, :],
                    dd[1, :],
                    dd[0, :],
                    alpha=0.5,
                    label="95th percentile")
    ax.set_xscale('log')
    #xlabel = 'Average recurrence interval (years)'
    #ylabel = 'Wind speed (m/s)'
    title = "{0} (n={1:d})".format(locName, len(recs))

    ax.set_title(title)
    addARIGrid(ax)
Esempio n. 2
0
def plotConvergence(ax, locName):
    locId = locations['locId'][locations['locName'] == locName][0]
    locLon = locations['locLon'][locations['locId'] == locId][0]
    locLat = locations['locLat'][locations['locId'] == locId][0]

    records = database.locationRecords(db, locId)
    recs = records['wspd'][records['wspd'] > 0]
    data = np.zeros(int(NumSimulations * 365.25))
    data[-len(recs):] = recs
    sortedmax = np.sort(data)
    emprp = empReturnPeriod(data)
    random.shuffle(data)
    d1 = data[:int(len(data) / 2)]
    d2 = data[int(len(data) / 2 + 1):]
    sortedmax1 = np.sort(d1)
    sortedmax2 = np.sort(d2)
    emprp1 = empReturnPeriod(d1)
    emprp2 = empReturnPeriod(d2)
    ep = 1. / emprp
    ep1 = 1. / emprp1
    ep2 = 1. / emprp2
    ax.semilogx(emprp[emprp > 1],
                sortedmax[emprp > 1],
                color='k',
                label="Mean ARI")
    ax.semilogx(emprp2[emprp2 > 1],
                sortedmax2[emprp2 > 1],
                color="#006983",
                label="Convergence check 1",
                ls='--')
    ax.semilogx(emprp1[emprp1 > 1],
                sortedmax1[emprp1 > 1],
                color="#A33F1F",
                label="Convergence check 2",
                ls='--')
    ax.set_xscale('log')

    xlabel = 'Average recurrence interval (years)'
    ylabel = 'Wind speed (m/s)'
    title = "{0} ({1:5.2f}E, {2:5.2f}S, n={3:d})".format(
        locName, locLon, locLat, len(recs))
    #ax.set_xlabel(xlabel)
    #ax.set_ylabel(ylabel)
    ax.set_title(title)
    addARIGrid(ax)
Esempio n. 3
0
    def plotHazardCurves(self, inputFile, plotPath):
        """
        Plot the hazard values stored in hazardFile, at the stns
        stored in stnFile.
        """

        log.info(("Plotting return period curves for locations within the "
                  "model domain"))
        # Open data file
        try:
            ncobj = nctools.ncLoadFile(inputFile)
            lon = nctools.ncGetDims(ncobj, 'lon')
            lat = nctools.ncGetDims(ncobj, 'lat')
            years = nctools.ncGetDims(ncobj, 'ari')
        except (IOError, RuntimeError, KeyError):
            log.critical("Cannot load input file: %s" % inputFile)
            raise

        placeNames, placeID, placeLats, placeLons, locations = self.getLocations(
        )

        for name, plat, plon, pID in zip(placeNames, placeLats, placeLons,
                                         placeID):

            log.debug("Plotting return period curve for %s" % name)
            i = find_index(lon, plon)
            j = find_index(lat, plat)

            xlabel = 'Average recurrence interval (years)'
            ylabel = 'Wind speed (%s)' % self.plotUnits.label
            title = "Return period wind speeds at " + name + ", \n(%5.1f,%5.1f)" % (
                plon, plat)

            name.replace(' ', '')
            log.debug("Working on {0}".format(name))
            filename = pjoin(plotPath, 'ARI_curve_%s.%s' % (pID, "png"))
            log.debug("Saving hazard curve for %s to %s" % (name, filename))
            wspd = ncobj.variables['wspd'][:, j, i]

            recs = database.locationRecords(self.db, pID)
            data = np.zeros(int(10000 * 365.25))
            if len(recs) > 0:
                data[-len(recs):] = recs['wspd']

            allevents = np.sort(data)
            log.debug("allevents length = {0}".format(len(allevents)))

            placeWspd = metutils.convert(wspd, 'mps', self.plotUnits.units)
            if np.all(placeWspd.mask):
                log.debug("All values for {0} are null".format(name))
                continue

            if self.ciBounds:
                wspdLower = ncobj.variables['wspdlower'][:, j, i]
                wspdUpper = ncobj.variables['wspdupper'][:, j, i]
                placeWspdLower = metutils.convert(wspdLower, 'mps',
                                                  self.plotUnits.units)
                placeWspdUpper = metutils.convert(wspdUpper, 'mps',
                                                  self.plotUnits.units)
            else:
                placeWspdUpper = np.zeros(len(placeWspd))
                placeWspdLower = np.zeros(len(placeWspd))

            saveHazardCurve(years, allevents, placeWspd, placeWspdUpper,
                            placeWspdLower, xlabel, ylabel, title, filename,
                            self.fit)

        ncobj.close()
Esempio n. 4
0
def plotConvergenceTest(locName):
    locId = locations['locId'][locations['locName'] == locName][0]
    locLon = locations['locLon'][locations['locId'] == locId][0]
    locLat = locations['locLat'][locations['locId'] == locId][0]

    records = database.locationRecords(db, locId)
    recs = records['wspd'][records['wspd'] > 0]
    data = np.zeros(int(NumSimulations * 365.25))
    data[-len(recs):] = recs
    sortedmax = np.sort(data)
    emprp = empReturnPeriod(data)
    random.shuffle(data)
    d1 = data[:int(len(data) / 2)]
    d2 = data[int(len(data) / 2):]
    sortedmax1 = np.sort(d1)
    sortedmax2 = np.sort(d2)
    emprp1 = empReturnPeriod(d1)
    emprp2 = empReturnPeriod(d2)
    ep = 1. / emprp
    ep1 = 1. / emprp1
    ep2 = 1. / emprp2
    mn = (sortedmax1[emprp1 > 1] + sortedmax2[emprp2 > 1]) / 2.
    delta = np.abs(sortedmax1[emprp1 > 1] - sortedmax2[emprp2 > 1])
    fdelta = delta / mn

    fig, ax1 = plt.subplots(1, 1)
    ax1.semilogx(emprp[emprp > 1],
                 sortedmax[emprp > 1],
                 color='k',
                 label="Mean ARI")
    ax1.semilogx(emprp2[emprp2 > 1],
                 sortedmax2[emprp2 > 1],
                 color="#006983",
                 label="Convergence check 1")
    ax1.semilogx(emprp1[emprp1 > 1],
                 sortedmax1[emprp1 > 1],
                 color="#A33F1F",
                 label="Convergence check 2")
    ax1.set_xscale('log')

    xlabel = 'Average recurrence interval (years)'
    ylabel = 'Wind speed (m/s)'
    title = "ARI wind speeds at " + locName + \
        " \n(%5.2f,%5.2f, n=%d)"%(locLon, locLat, len(recs))
    ax1.set_xlabel(xlabel)
    ax1.set_ylabel(ylabel)
    ax1.set_title(title)
    ax1.legend(loc=2)
    addARIGrid(ax1)
    fig.tight_layout()
    plt.savefig(os.path.join(plotPath, "{0:05d}_ARI.png".format(locId)),
                bbox_inches='tight')
    plt.close()

    fig2, ax2 = plt.subplots(1, 1)
    ax2.semilogy(sortedmax[emprp > 1],
                 ep[emprp > 1],
                 color="k",
                 label="Mean AEP")

    ax2.semilogy(sortedmax1[emprp1 > 1],
                 ep1[emprp1 > 1],
                 color="#006983",
                 label="Convergence check 1")
    ax2.semilogy(sortedmax2[emprp2 > 1],
                 ep2[emprp2 > 1],
                 color="#A33F1F",
                 label="Convergence check 2")
    ax2.set_xlabel(ylabel)
    title = "AEP wind speeds at " + locName + \
        " \n(%5.2f,%5.2f, n=%d)"%(locLon, locLat, len(recs))
    ax2.set_ylabel("Exceedance probability (events/year)")

    ax2.set_title(title)
    ax2.legend(loc=1)
    addAEPGrid(ax2)
    fig.tight_layout()
    plt.savefig(os.path.join(plotPath, "{0:05d}_AEP.png".format(locId)),
                bbox_inches='tight')
    plt.close()

    fig3, (ax3, ax4) = plt.subplots(2, 1, sharex=True)
    ax3.fill_between(emprp[emprp > 1][0:-1:2],
                     fdelta,
                     color="#006983",
                     alpha=0.5)
    ax3.set_ylabel('Fractional difference')
    ax3.set_title("Difference in convergence test ARI wind speeds at " + \
                  locName + " \n(%5.2f,%5.2f, n=%d)"%(locLon, locLat, len(recs)))
    ax3.set_xscale('log')
    addARIGrid(ax3)

    ax4.fill_between(emprp[emprp > 1][0:-1:2],
                     delta,
                     color="#006983",
                     alpha=0.5)
    ax4.set_ylabel("Difference (m/s)")
    ax4.set_xlabel(xlabel)
    ax4.set_xscale('log')
    addARIGrid(ax4)

    fig.tight_layout()
    plt.savefig(os.path.join(plotPath, "{0:05d}_ARI_delta.png".format(locId)),
                bbox_inches='tight')
    plt.close()
Esempio n. 5
0
def plotConvergenceTest(locName):
    locId = locations['locId'][locations['locName'] == locName][0]
    locLon = locations['locLon'][locations['locId'] == locId][0]
    locLat = locations['locLat'][locations['locId'] == locId][0]

    records = database.locationRecords(db, str(locId))
    recs = records['wspd'][records['wspd'] > 0]
    data = np.zeros(int(NumSimulations * 365.25))
    data[-len(recs):] = recs
    sortedmax = np.sort(data)
    emprp = empReturnPeriod(data)
    dd, rr = bootstrap(data, n=100)

    ep = 1. / emprp
    ep1 = 1. / rr[0, :]
    ep2 = 1. / rr[1, :]
    mn = dd.mean(axis=0)
    delta = np.abs(np.diff(dd, axis=0))
    fdelta = delta / mn

    fig, ax1 = plt.subplots(1, 1, figsize=figsize)

    ax1.fill_between(rr[0, :],
                     dd[1, :],
                     dd[0, :],
                     alpha=0.5,
                     label="95th percentile")
    ax1.plot(emprp[-10000:], data[-10000:], color='k', label="Mean ARI")
    ax1.set_xscale('log')

    xlabel = 'Average recurrence interval (years)'
    ylabel = 'Wind speed (m/s)'
    title = "ARI wind speeds at " + locName + \
        " \n(%5.2f,%5.2f, n=%d)"%(locLon, locLat, len(recs))
    ax1.set_xlabel(xlabel)
    ax1.set_ylabel(ylabel)
    ax1.set_title(title)
    ax1.legend(loc=2)
    addARIGrid(ax1)
    fig.tight_layout()
    plt.savefig(os.path.join(plotPath, "{0:05d}_ARI.png".format(locId)),
                bbox_inches='tight')
    plt.close()

    fig2, ax2 = plt.subplots(1, 1, figsize=figsize)
    ax2.semilogy(sortedmax[-10000:], ep[-10000:], color="k", label="Mean AEP")
    ax2.fill_betweenx(1. / rr[0, :],
                      dd[0, :],
                      dd[1, :],
                      alpha=0.5,
                      label="95th percentile")
    ax2.set_xlabel(ylabel)
    title = "AEP wind speeds at " + locName + \
        " \n(%5.2f,%5.2f, n=%d)"%(locLon, locLat, len(recs))
    ax2.set_ylabel("Exceedance probability (events/year)")

    ax2.set_title(title)
    ax2.legend(loc=1)
    addAEPGrid(ax2)
    fig.tight_layout()
    plt.savefig(os.path.join(plotPath, "{0:05d}_AEP.png".format(locId)),
                bbox_inches='tight')
    plt.close()