https://github.com/sgrieve/ER_Star
Raw File
Tip revision: a31fdcaf52e142f8ab12fe428f5e4e5d1f54f9ce authored by Stuart Grieve on 18 October 2016, 10:48:45 UTC
Update README.md
Tip revision: a31fdca
Plot_ER_Data.py
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 11 14:01:05 2015

@author: SWDG

"""
import matplotlib.pyplot as plt
from matplotlib import rcParams
from matplotlib import container
from matplotlib import collections
import numpy as np
import scipy.optimize as optimize
import bin_data as Bin
from scipy.stats import gaussian_kde
from scipy.stats import sem
from uncertainties import unumpy as unp


def LoadData(Path, Prefix, Order):
    """
    Loads topogrpahic data from the 3 datafiles generated by E_R_STAR.cpp.

    Path is the path where the files are stored, with a trailing slash.
    Prefix is the filename prefix used by E_R_STAR.cpp to denote a landscape.
    Order is the integer basin order used to extract basin average data in
    E_R_STAR.cpp. Returns 2D arrays of topographic data from the raw, patch
    average and basin average datasets.
    """

    # load the data from the raw file
    # not using genfromtext as I want access to individual elements
    # for debugging, may change in future
    with open(Path + Prefix + '_E_R_Star_Raw_Data.csv', 'r') as raw:
        no_of_cols = len(raw.readline().split(','))
        rawdata = raw.readlines()

    # want the data in a 2d array to make moving the values about simpler
    # dimensions will be 6Xlen(rawdata) no_of_cols = 6
    # and the row order will follow the header format in the input file:
    # i j LH CHT Relief Slope

    no_of_lines = len(rawdata)

    RawData = np.zeros((no_of_cols, no_of_lines), dtype='float64')

    for i, r in enumerate(rawdata):
        split = r.split(',')
        for a in range(no_of_cols):
            RawData[a][i] = split[a]
    # now we have a transformed 2d array of our raw data

    # Next, repeat the process for the patch data
    with open(Path + Prefix + '_E_R_Star_Patch_Data.csv', 'r') as patch:
        no_of_cols = len(patch.readline().split(','))
        patchdata = patch.readlines()

    # dimensions will be 18Xlen(patchdata) no_of_cols = 18
    # and the row order will follow the header format in the input file:
    # Final_ID lh_means lh_medians lh_std_devs lh_std_errs cht_means cht_medians
    # cht_std_devs cht_std_errs r_means r_medians r_std_devs r_std_errs s_means
    # s_medians s_std_devs s_std_errs patch_size

    no_of_lines = len(patchdata)

    PatchData = np.zeros((no_of_cols, no_of_lines), dtype='float64')

    for i, p in enumerate(patchdata):
        split = p.split(',')
        for a in range(no_of_cols):
            PatchData[a][i] = split[a]

    # Next, repeat the process for the Basin data
    with open(Path + Prefix + '_E_R_Star_Basin_' + str(Order) +
              '_Data.csv', 'r') as basin:
        no_of_cols = len(basin.readline().split(','))
        basindata = basin.readlines()

    # dimensions will be 11Xlen(basindata) no_of_cols = 19
    # and the row order will follow the header format in the input file:
    # Basin_ID,LH_mean,CHT_mean,Relief_mean,Slope_mean,LH_median,CHT_median,Relief_median,Slope_median,LH_StdDev,CHT_StdDev,Relief_StdDev,Slope_StdDev,LH_StdErr,CHT_StdErr,Relief_StdErr,Slope_StdErr,Area,Count

    no_of_lines = len(basindata)

    BasinData = np.zeros((no_of_cols, no_of_lines), dtype='float64')

    for i, d in enumerate(basindata):
        split = d.split(',')
        for a in range(no_of_cols):
            BasinData[a][i] = split[a]

    return RawData, PatchData, BasinData


def PropagateErrors(PatchData, BasinData):
    """
    Load the hillslope, Relief and hilltop curavture data from the basin and
    patch data files into the uncertainties package, so that we can propagate
    errors through our calculations.
    """

    # median, sem
    patchLH = unp.uarray(PatchData[2], PatchData[4])
    patchR = unp.uarray(PatchData[10], PatchData[12])
    patchCHT = unp.uarray(PatchData[6], PatchData[8])

    basinLH = unp.uarray(BasinData[5], BasinData[13])
    basinR = unp.uarray(BasinData[7], BasinData[15])
    basinCHT = unp.uarray(BasinData[6], BasinData[14])

    return (patchLH, patchR, patchCHT), (basinLH, basinR, basinCHT)


def SetUpPlot():
    """
    Configure the plotting environment.
    """
    rcParams['font.family'] = 'sans-serif'
    rcParams['font.sans-serif'] = ['arial']
    rcParams['font.size'] = 15

    ax = plt.gca()

    ax.set_yscale('log', nonposy='clip')
    ax.set_xscale('log', nonposx='clip')

    plt.xlabel('Dimensionless Erosion Rate, ${E^*}$', size=18)
    plt.ylabel('Dimensionless Relief, ${R^*}$', size=18)

    plt.setp(ax.get_xticklabels(), fontsize=16)
    plt.setp(ax.get_yticklabels(), fontsize=16)

    plt.ylim(0.05, 1.1)
    plt.xlim(0.1, 1000)

    return ax


def PlotRaw(Sc, RawData):
    """
    Plot the raw E*R* data as small grey points.
    """
    plt.plot(E_Star(Sc, RawData[3], RawData[2]), R_Star(Sc, RawData[4],
             RawData[2]), 'k.', alpha=0.2, label='Raw Data')


def PlotRawDensity(Sc, RawData, Thin):
    """
    Plot the raw E*R* data as a density plot, computing the PDF as a gaussian
    and including a colorbar.

    Built around code from: http://stackoverflow.com/a/20107592/1627162
    """
    x = E_Star(Sc, RawData[3], RawData[2])
    y = R_Star(Sc, RawData[4], RawData[2])

    if Thin:
        x = x[::Thin]
        y = y[::Thin]

    xy = np.vstack([x, y])
    density = gaussian_kde(xy)(xy)

    # order the points by density so highest density is on top in the plot
    idx = density.argsort()
    x, y, density = x[idx], y[idx], density[idx]

    plt.scatter(x, y, c=density, edgecolor='', cmap=plt.get_cmap("autumn_r"))
    cbar = plt.colorbar()
    cbar.set_label('Probability Distribution Function')


def PlotRawBins(Sc, RawData, NumBins, MinimumBinSize=100, ErrorBars=True):
    """
    Plot E*R* data binned from the raw data.
    """
    E_s = E_Star(Sc, RawData[3], RawData[2])
    R_s = R_Star(Sc, RawData[4], RawData[2])

    (bin_x, bin_std_x, bin_y, bin_std_y,
     std_err_x, std_err_y, count) = Bin.bin_data_log10(E_s, R_s, NumBins)

    # filter bins based on the number of data points used in their calculation
    bin_x = np.ma.masked_where(count < MinimumBinSize, bin_x)
    bin_y = np.ma.masked_where(count < MinimumBinSize, bin_y)
    # these lines produce a meaningless warning - don't know how to solve it yet

    if ErrorBars:
        # only plot errorbars for y as std dev of x is the bin width
        plt.scatter(bin_x, bin_y, c=count, s=50, edgecolor='',
                    cmap=plt.get_cmap("autumn_r"), label='Binned Raw Data',
                    zorder=100)
        plt.errorbar(bin_x, bin_y, yerr=std_err_y, fmt=None, ecolor='k',
                     zorder=0)
        cbar = plt.colorbar()
        cbar.set_label('Number of values per bin')
    else:
        plt.errorbar(bin_x, bin_y, fmt='bo', label='Binned Raw Data')


def PlotPatchBins(Sc, PatchData, NumBins, MinimumBinSize=10, ErrorBars=True):
    """
    Plot E*R* data binned from the hilltop pacth data.
    """
    E_s = E_Star(Sc, PatchData[6], PatchData[2])
    R_s = R_Star(Sc, PatchData[10], PatchData[2])

    (bin_x, bin_std_x, bin_y, bin_std_y,
     std_err_x, std_err_y, count) = Bin.bin_data_log10(E_s, R_s, NumBins)

    # filter bins based on the number of data points used in their calculation
    bin_x = np.ma.masked_where(count < MinimumBinSize, bin_x)
    bin_y = np.ma.masked_where(count < MinimumBinSize, bin_y)
    # these lines produce a meaningless warning - don't know how to solve it yet

    if ErrorBars:
        # only plot errorbars for y as std dev of x is the bin width
        plt.scatter(bin_x, bin_y, c=count, s=50, edgecolor='',
                    cmap=plt.get_cmap("autumn_r"), label='Binned Patch Data',
                    zorder=100)
        plt.errorbar(bin_x, bin_y, yerr=std_err_y, fmt=None, ecolor='k',
                     zorder=0)
        cbar = plt.colorbar()
        cbar.set_label('Number of values per bin')

    else:
        plt.errorbar(bin_x, bin_y, fmt='bo', label='Binned Patch Data')


def PlotPatches(Sc, PatchData, ErrorBars):
    """
    Plot E*R* data binned from hilltop patches.
    """
    e_star = E_Star(Sc, PatchData[2], PatchData[0])
    r_star = R_Star(Sc, PatchData[1], PatchData[0])
    if ErrorBars:
        plt.errorbar(unp.nominal_values(e_star), unp.nominal_values(r_star),
                     yerr=unp.std_devs(r_star), xerr=unp.std_devs(e_star),
                     fmt='ro', label='Hilltop Patch Data')
    else:
        plt.errorbar(unp.nominal_values(e_star), unp.nominal_values(r_star),
                     fmt='ro', label='Hilltop Patch Data')


def PlotPatchesArea(Sc, PatchData, thresh, alpha):
    """
    Plot patch average E*R* data filtered by a user defined patch area value.
    """
    e_star = E_Star(Sc, PatchData[6], PatchData[2])
    r_star = R_Star(Sc, PatchData[10], PatchData[2])
    area = PatchData[17]

    x = []
    y = []

    for a, b, s in zip(e_star, r_star, area):
        if s > thresh:
            x.append(a)
            y.append(b)

    plt.plot(x, y, color='k', alpha=alpha, marker='o', linestyle='',
             label='Min. Patch Area = ' + str(thresh))


def PlotBasins(Sc, BasinData, ErrorBars):
    """
    Plot basin average E*R* data.
    """
    e_star = E_Star(Sc, BasinData[2], BasinData[0])
    r_star = R_Star(Sc, BasinData[1], BasinData[0])

    if ErrorBars:
        plt.errorbar(unp.nominal_values(e_star), unp.nominal_values(r_star),
                     yerr=unp.std_devs(r_star), xerr=unp.std_devs(e_star),
                     fmt='go', label='Basin Data')
    else:
        plt.errorbar(unp.nominal_values(e_star), unp.nominal_values(r_star),
                     fmt='go', label='Basin Data')


def PlotBasinsArea(Sc, BasinData, thresh, alpha):
    """
    Plot basin average E*R* data filtered by a user defined basin area value.
    """
    e_star = E_Star(Sc, BasinData[6], BasinData[5])
    r_star = R_Star(Sc, BasinData[7], BasinData[5])
    area = BasinData[18]

    x = []
    y = []

    for a, b, s in zip(e_star, r_star, area):
        if s > thresh:
            x.append(a)
            y.append(b)

    plt.plot(x, y, color='k', alpha=alpha, marker='o', linestyle='',
             label='Min. Basin Data Points = ' + str(thresh))


def PlotLandscapeAverage(Sc, RawData, ErrorBars):
    """
    Plot a landscape median data point, calculated using the raw data, and
    generating errorbars using the standard error.
    """
    E_Star_temp = E_Star(Sc, RawData[3], RawData[2])
    R_Star_temp = R_Star(Sc, RawData[4], RawData[2])
    E_Star_avg = np.median(E_Star_temp)
    R_Star_avg = np.median(R_Star_temp)

    if ErrorBars:
        E_Star_std = np.std(E_Star_temp)
        R_Star_std = np.std(R_Star_temp)
        E_Star_serr = sem(E_Star_temp)
        R_Star_serr = sem(R_Star_temp)
        plt.errorbar(E_Star_avg, R_Star_avg, yerr=R_Star_serr, xerr=E_Star_serr,
                     fmt='ko', label='Landscape Average')
    else:
        plt.errorbar(E_Star_avg, R_Star_avg, fmt='ko',
                     label='Landscape Average')


def R_Star_Model(x):
    """
    Return the predicted R* value for a given value of E* using eq 10 in Roering
    et al. (2007) www.sciencedirect.com/science/article/pii/S0012821X07006061
    """
    return ((1. / x) * (np.sqrt(1. + (x * x)) -
            np.log(0.5 * (1. + np.sqrt(1. + (x * x)))) - 1.))


def E_Star(Sc, CHT, LH):
    """
    Calculate the E* value from topographic data after Roering et al. (2007)
    http://www.sciencedirect.com/science/article/pii/S0012821X07006061
    """
    if type(LH[0]) == np.float64:
        return (2. * np.fabs(CHT) * LH) / Sc
    else:
        return (2. * unp.fabs(CHT) * LH) / Sc


def R_Star(Sc, R, LH):
    """
    Calculate the R* value from topographic data after Roering et al. (2007)
    http://www.sciencedirect.com/science/article/pii/S0012821X07006061
    """
    return R / (LH * Sc)


def Residuals(Sc, R, LH, CHT):
    """
    Calculate the residuals between the R* value computed using eq 10 in Roering
    et al. (2007) (www.sciencedirect.com/science/article/pii/S0012821X07006061)
    and the R* calculated from topographic data.
    """
    return R_Star_Model(E_Star(Sc, CHT, LH)) - R_Star(Sc, R, LH)


def reduced_chi_square(Residuals, Sc, DataErrs=None):
    """
    Compute a reduced chi square value for the best fit Sc value.
    """
    # if we are fitting from patches or basins, get the std err and include
    # in the chi squared
    if DataErrs:
        r_star = R_Star(Sc, DataErrs[1], DataErrs[0])

        # get rid of any divide by zero errors
        temp = ((Residuals / unp.std_devs(r_star)) ** 2)
        temp[np.isinf(temp)] = 0
        chi_square = np.sum(temp)

    else:
        chi_square = np.sum(Residuals ** 2)

    # degrees of freedom, as we have 1 free parameter, Sc
    d_o_f = Residuals.size - 2

    return chi_square / d_o_f


def r_squared(Sc, R, LH, CHT, infodict):
    """
    Calculate the R squared value of the best fit Sc value, using the residuals
    from the infodict generated by the optimize package.
    """
    measured = R_Star(Sc, R, LH)
    mean_measured = np.mean(measured)

    sqr_err_w_line = np.square(infodict['fvec'])
    sqr_err_mean = np.square((measured - mean_measured))

    return 1. - (np.sum(sqr_err_w_line) / np.sum(sqr_err_mean))


def DrawCurve():
    """
    Plot the steady state curve in E*R* space.
    """
    # plot the e* r* curve from roering 2007
    x = np.arange(0.01, 1000, 0.1)
    plt.plot(x, R_Star_Model(x), 'k-', linewidth=2, label='Nonlinear Flux Law')


def GetBestFitSc(Method, Data, DataErrs=None):
    """
    Compute the best fit Sc value to the data using the scipy optimize.leassq
    package. Also returns the reduced chi squared as a measure of the goodness
    of fit.
    """
    # Need to have an initial for the optimizer, any valid Sc value can
    # be used - will not impact the final value
    ScInit = 0.8

    # Need to initialize this in case Method is incorrectly defined.
    # Need some error handling!
    Fit_Sc = []

    if Method == 'raw':
        Fit_Sc, _, infodict, _, _ = optimize.leastsq(Residuals, ScInit,
                                                     args=(Data[4], Data[2],
                                                           Data[3]),
                                                     full_output=True)

        chi = reduced_chi_square(infodict['fvec'], Fit_Sc[0])

    elif Method == 'patches':
        Fit_Sc, _, infodict, _, _ = optimize.leastsq(Residuals, ScInit,
                                                     args=(Data[10], Data[2],
                                                           Data[6]),
                                                     full_output=True)

        chi = reduced_chi_square(infodict['fvec'], Fit_Sc[0], DataErrs)

    elif Method == 'basins':
        Fit_Sc, _, infodict, _, _ = optimize.leastsq(Residuals, ScInit,
                                                     args=(Data[7], Data[5],
                                                           Data[6]),
                                                     full_output=True)

        chi = reduced_chi_square(infodict['fvec'], Fit_Sc[0], DataErrs)

    return Fit_Sc[0], chi


def BootstrapSc(Method, Data, n=10000):
    """
    Bootstrap the calculation of the best fit Sc value n times to get the 95%
    confidence interval for the best fit Sc.

    Values of n larger than 10000 will take a long time to run.
    """
    tmp = []
    # convert the LH,R,CHT data into a serial 1D array before bootstrapping
    if Method == 'raw':
        for i in range(len(Data[0])):
            tmp.append(SerializeData(Data[2][i], Data[4][i], Data[3][i]))
    if Method == 'patches':
        for i in range(len(Data[0])):
            tmp.append(SerializeData(Data[2][i], Data[10][i], Data[6][i]))
    if Method == 'basins':
        for i in range(len(Data[0])):
            tmp.append(SerializeData(Data[5][i], Data[7][i], Data[6][i]))

    ToSample = np.array(tmp)

    Scs = []
    i = 0
    while i < n:
        if (i % 1000 == 0):
            print 'Iteration', i + 1, 'of', n

        sample = np.random.choice(ToSample, len(ToSample), replace=True)
        LH, R, CHT = UnserializeList(sample)
        sc, _, _, _, _ = optimize.leastsq(Residuals, 0.8, args=(R, LH, CHT),
                                          full_output=True)
        if sc < 2.0:
            Scs.append(sc[0])
            i += 1

    # mean, upper bound, lower bound
    return (np.mean(Scs), np.percentile(Scs, 97.5) - np.mean(Scs),
            np.mean(Scs) - np.percentile(Scs, 2.5))


def SerializeData(LH, R, CHT):
    """
    Convert the hillslope length, relief, and hilltop curvature data into a
    string, to facilitate the sampling by replacement of the data in the
    bootstrapping method.
    """
    return str([LH, R, CHT])[1:-1]


def UnserializeList(serial_list):
    """
    Convenicence function to unserialize a list of serialized data and return
    arrays of hillslope length, relief, and hilltop curvature.
    """
    LH = []
    R = []
    CHT = []
    for s in serial_list:
        lh, r, cht = UnserializeData(s)
        LH.append(lh)
        R.append(r)
        CHT.append(cht)

    return np.array(LH), np.array(R), np.array(CHT)


def UnserializeData(serial):
    """
    Unpack the data serialized by SerializeData, returning the original values
    in their original format.
    """
    split = [float(s) for s in serial.split(',')]
    return split[0], split[1], split[2]


def Labels(Sc, Method, ax):
    """
    Method to handle the labelling of axes, generation of a legend and creation
    of a plot title.
    """
    # remove errorbars from the legend
    handles, labels = ax.get_legend_handles_labels()
    handles = [h[0] if isinstance(h, container.ErrorbarContainer)
               else h for h in handles]

    # color scatterplot symbols like colormap
    for h in handles:
        if isinstance(h, collections.PathCollection):
            h.set_color('r')
            h.set_edgecolor('')

    ax.legend(handles, labels, loc=4, numpoints=1, scatterpoints=1)

    # in case Method is invalid
    fit_description = ' = '

    if Method == 'raw':
        fit_description = ' from raw data = '

    elif Method == 'patches':
        fit_description = ' from hilltop patches = '

    elif Method == 'basins':
        fit_description = ' from basin average data = '

    if isinstance(Method, int) or isinstance(Method, float):
        plt.title('$\mathregular{S_c}$ = ' + str(round(Sc, 2)), y=1.02)
    else:
        plt.title('Best fit $\mathregular{S_c}$' +
                  fit_description + str(round(Sc, 2)), y=1.02)


def SavePlot(Path, Prefix, Format):
    """
    Wrapper function around the matplotlib savefig method, which save a high
    resolution copy of the final figure into the user supplied path with the
    user suppied file format.
    """
    plt.savefig(Path + Prefix + '_E_R_Star.' + Format, dpi=500)
    plt.clf()


def CRHurst():
    """
    Plots the E*R* data generated for the Cascade Ridge by Hurst et al. (2012)
    (http://onlinelibrary.wiley.com/doi/10.1029/2011JF002057/full) seen in
    figure 14.
    """

    x = [1.15541793184, 2.96599962747, 5.06753455114, 6.87537359947,
         8.86462081724, 10.9425778888, 12.9426702489, 14.9866553641,
         16.9785507349, 19.0034609662, 20.9560856862, 22.8577931724,
         24.6085876779, 27.3044634219, 28.3873092441, 31.1978149101,
         32.8625186998, 35.2335006909, 37.2282499959, 43.8911646306,
         45.5936728215]
    y = [0.379133283693, 0.435531356239, 0.547479389809, 0.588874111323,
         0.652649344881, 0.696659574468, 0.824275084903, 0.733856012658,
         0.783243670886, 0.836195147679, 0.920291139241, 0.862545710267,
         0.953440506329, 0.851824367089, 0.97046835443, 0.909219409283,
         0.964772151899, 1.08295780591, 0.904050632911, 1.13525316456,
         0.934139240506]

    yStdErr = [0.12913016, 0.03901928, 0.04112731, 0.02724568, 0.04694418,
               0.04026138, 0.03122017, 0.02083737, 0.01752255, 0.02029758,
               0.02403573, 0.02065953, 0.02382283, 0.021544, 0.0216427,
               0.02044206, 0.02158805, 0.02227467, 0.03237965, 0.04332041,
               0.09519842]

    plt.errorbar(x, y, yerr=yStdErr, fmt='k^', label='Hurst et al. (2012)',
                 elinewidth=2, capsize=3, markersize=6)


def GMRoering():
    """
    Plots the E*R* data for the Gabilan Mesa generated by Roering et al. (2007)
    http://www.sciencedirect.com/science/article/pii/S0012821X07006061
    """
    x = [1.68] * 2
    y = [0.34, 0.43]

    xerr = [0.7] * 2
    yerr = [0.17, 0.2]

    plt.errorbar(x, y, yerr, xerr, 'k^', label='Roering et al. 2007')


def OCRRoering():
    """
    Plots the E*R* data for the Oregon Coast Range generated by Roering et al.
    (2007) http://www.sciencedirect.com/science/article/pii/S0012821X07006061
    """
    x = [6.3] * 2
    y = [0.57, 0.64]

    xerr = [2.1] * 2
    yerr = [0.23, 0.18]

    plt.errorbar(x, y, yerr, xerr, 'k^', label='Roering et al. 2007')


def MakeThePlot(Path, Prefix, Sc_Method, RawFlag, DensityFlag, BinFlag, NumBins,
                MinBinSize, PatchFlag, BasinFlag, LandscapeFlag, Order,
                ErrorBarFlag=True, Format='png',
                ComparisonData=(False, False, False), NumBootsraps=10000):
    """
    Method which controls the generation of E*R* data. Does not need to be
    interfaced with directly. Is called by the IngestSettings method using
    parameters in the Settings.py file.
    """
    RawData, PatchData, BasinData = LoadData(Path, Prefix, Order)

    PatchDataErrs, BasinDataErrs = PropagateErrors(PatchData, BasinData)

    ax = SetUpPlot()

    DrawCurve()

    if isinstance(Sc_Method, int) or isinstance(Sc_Method, float):
        Sc = Sc_Method
    else:
        if Sc_Method == 'raw':
            Sc, upper, lower = BootstrapSc(Sc_Method, RawData, NumBootsraps)
        if Sc_Method == 'patches':
            Sc, upper, lower = BootstrapSc(Sc_Method, PatchData, NumBootsraps)
        if Sc_Method == 'basins':
            Sc, upper, lower = BootstrapSc(Sc_Method, BasinData, NumBootsraps)

    if RawFlag:
        PlotRaw(Sc, RawData)
    if DensityFlag:
        PlotRawDensity(Sc, RawData, DensityFlag)
    if PatchFlag:
        PlotPatches(Sc, PatchDataErrs, ErrorBarFlag)
    if BinFlag == 'patches':
        PlotPatchBins(Sc, PatchData, NumBins, MinimumBinSize=MinBinSize,
                      ErrorBars=ErrorBarFlag)
    elif BinFlag == 'raw':
        PlotRawBins(Sc, RawData, NumBins, MinimumBinSize=MinBinSize,
                    ErrorBars=ErrorBarFlag)
    if BasinFlag:
        PlotBasins(Sc, BasinDataErrs, ErrorBarFlag)
    if LandscapeFlag:
        PlotLandscapeAverage(Sc, RawData, ErrorBarFlag)

    if ComparisonData[0]:
        GMRoering()
    if ComparisonData[1]:
        OCRRoering()
    if ComparisonData[2]:
        CRHurst()

    Labels(Sc, Sc_Method, ax)
    # plt.show()

    SavePlot(Path, Prefix, Format)


def IngestSettings():
    """
    Load the parameters from the Settings.py file, perform type and sanity
    checking and run the main code to generate E*R* data.
    """
    import Settings
    import sys

    # typecheck inputs
    if not isinstance(Settings.Path, str):
        sys.exit('Path=%s \nThis is not a valid string and so cannot be used as'
                 ' a path.\nExiting...' % Settings.Path)

    if not isinstance(Settings.Prefix, str):
        sys.exit('Prefix=%s \nThis is not a valid string and so cannot be used '
                 'as a filename prefix.\nExiting...' % Settings.Prefix)

    if not isinstance(Settings.Sc_Method,
                      str) and not isinstance(Settings.Sc_Method, float):
        sys.exit('Sc_Method=%s \nThis is not a valid string or floating point '
                 'value.\nExiting...' % Settings.Sc_Method)
    else:
        if isinstance(Settings.Sc_Method,
                      str) and (Settings.Sc_Method != 'raw' and
                                Settings.Sc_Method != 'patches' and
                                Settings.Sc_Method != 'basins'):
            sys.exit('Sc_Method=%s \nThis is not a valid method to fit a '
                     'critical gradient. Valid options are \'raw\',\'patches\','
                     ' or \'basins\'.\nExiting...' % Settings.Sc_Method)
        if isinstance(Settings.Sc_Method,
                      float) and (Settings.Sc_Method <= 0 or
                                  Settings.Sc_Method > 3):
            sys.exit('Sc_Method=%s \nThis critical gradient not within the '
                     'expected range of 0 to 3.\nExiting...'
                     % Settings.Sc_Method)

    if not isinstance(Settings.RawFlag, int):
        sys.exit('RawFlag should be set to 1 to plot the raw data or 0 to '
                 'exclude the raw data. You have entered %s\nExiting...'
                 % Settings.RawFlag)

    if not isinstance(Settings.DensityFlag, int):
        sys.exit('DensityFlag should be set to 1 to produce a density plot or '
                 '0 to not plot a density plot. Integer values greater than 1 '
                 'will thin the data before plotting. You have entered '
                 '%s\nExiting...' % Settings.DensityFlag)

    if not isinstance(Settings.BinFlag, str):
        sys.exit('BinFlag=%s \nThis is not a valid string to select the '
                 'binning method. If not performing binning, enter a blank '
                 'string: \'\'.\nExiting...' % Settings.BinFlag)
    else:
        if Settings.BinFlag:
            if (Settings.BinFlag.lower() != 'raw' and
                    Settings.BinFlag.lower() != 'patches'):
                sys.exit('BinFlag=%s \nSelect either \'raw\' or \'patches\' as'
                         ' the binning method. Enter a blank string: \'\' if no'
                         ' binning is required.\nExiting...' % Settings.BinFlag)

    if not isinstance(Settings.NumBins, int):
        sys.exit('NumBins should be set to the number of bins to be generated '
                 'when binning the data. If no binning is to be performed, set '
                 'the value to 0. You have entered %s\nExiting...'
                 % Settings.NumBins)

    if not isinstance(Settings.PatchFlag, int):
        sys.exit('PatchFlag should be set to 1 to plot the patch data or 0 to '
                 'exclude the patch data. You have entered %s\nExiting...'
                 % Settings.PatchFlag)

    if not isinstance(Settings.BasinFlag, int):
        sys.exit('BasinFlag should be set to 1 to plot the basin data or 0 to '
                 'exclude the basin data. You have entered %s\nExiting...'
                 % Settings.BasinFlag)

    if not isinstance(Settings.LandscapeFlag, int):
        sys.exit('LandscapeFlag should be set to 1 to plot the landscape '
                 'average data or 0 to exclude the landscape average data. You'
                 ' have entered %s\nExiting...' % Settings.LandscapeFlag)

    if not isinstance(Settings.Order, int):
        sys.exit('Order should be set to an integer (eg 1,2,3, etc) to load '
                 'the basin average data generated for that order of basin. You'
                 ' have entered %s, of %s\nExiting...'
                 % (Settings.Order, type(Settings.Order)))

    if not isinstance(Settings.ErrorBarFlag, bool):
        sys.exit('ErrorBarFlag should be set to either True or False. True '
                 'will generate plots with errorbars, False will exclude them. '
                 'You have entered %s\nExiting...' % Settings.ErrorBarFlag)

    ValidFormats = ['png', 'pdf', 'ps', 'eps', 'svg']
    if not isinstance(Settings.Format, str):
        sys.exit('Format=%s \nFile format must be a valid string.\nExiting...'
                 % Settings.Format)
        if Settings.Format.lower() not in ValidFormats:
            sys.exit('Format=%s \nFile format must be one of: png, pdf, ps, '
                     'eps or svg.\nExiting...' % Settings.Format)

    if not isinstance(Settings.GabilanMesa, bool):
        sys.exit('GabilanMesa should be set to either True or False. True will '
                 'plot data from Roering et al. (2007), False will not. You '
                 'have entered %s\nExiting...' % Settings.GabilanMesa)
    if not isinstance(Settings.OregonCoastRange, bool):
        sys.exit('OregonCoastRange should be set to either True or False. True '
                 'will plot data from Roering et al. (2007), False will not. '
                 'You have entered %s\nExiting...' % Settings.OregonCoastRange)
    if not isinstance(Settings.SierraNevada, bool):
        sys.exit('SierraNevada should be set to either True or False. True will'
                 ' plot data from Roering et al. (2007), False will not. You '
                 'have entered %s\nExiting...' % Settings.SierraNevada)

    if not isinstance(Settings.NumBootsraps, int):
        sys.exit('NumBootsraps should be set to an integer (eg 10000) to select'
                 ' the number of iterations in the bootstrapping calculation.'
                 '\n\nThis value is ignored if a value of Sc is supplied. Using'
                 ' a value > 10000 will take a long time on big datasets. You '
                 'have entered %s, of type %s\nExiting...'
                 % (Settings.NumBootsraps, type(Settings.NumBootsraps)))

    if not isinstance(Settings.MinBinSize, int):
        sys.exit('MinBinSize should be set to an integer (eg 100) to select'
                 ' the number data points needed to make a bin valid. You'
                 'have entered %s, of type %s\nExiting...'
                 % (Settings.MinBinSize, type(Settings.MinBinSize)))

    MakeThePlot(Settings.Path, Settings.Prefix, Settings.Sc_Method,
                Settings.RawFlag, Settings.DensityFlag, Settings.BinFlag,
                Settings.NumBins, Settings.MinBinSize, Settings.PatchFlag,
                Settings.BasinFlag, Settings.LandscapeFlag, Settings.Order,
                Settings.ErrorBarFlag, Settings.Format,
                (Settings.GabilanMesa, Settings.OregonCoastRange,
                 Settings.SierraNevada), Settings.NumBootsraps)

IngestSettings()

"""
MakeThePlot('GM','patches',0,0,'',20,0,1,0,2,ErrorBarFlag=False,Format='png')



for l in ['GM','OR','NC','CR']:
    for m in ['raw','patches','basins']:
        print l,m
        MakeThePlot('C:\\Users\\Stuart\\Dropbox\\data\\final\\',l,m,0,0,'',20,0,1,0,2,ErrorBarFlag=False,Format='png')
"""
back to top