def compare_peak(name_fig, data, validation_elec_data_2015,
                 peak_all_models_all_enduses_fueltype):
    """Compare Peak electricity day with calculated peak energy demand
    """
    print("...compare elec peak results")
    # -------------------------------
    # Find maximumg peak in real data
    # -------------------------------
    max_h_year = 0
    max_day = "None"

    for day in range(365):
        max_h_day = np.max(validation_elec_data_2015[day])

        if max_h_day > max_h_year:
            max_h_year = max_h_day
            max_day = day

    print("Max Peak Day:                    " + str(max_day))
    print("max_h_year (real):               " + str(max_h_year))
    print("max_h_year (modelled):           " +
          str(np.max(peak_all_models_all_enduses_fueltype)))
    print("Fuel max peak day (real):        " +
          str(np.sum(validation_elec_data_2015[max_day])))
    print("Fuel max peak day (modelled):    " +
          str(np.sum(peak_all_models_all_enduses_fueltype)))

    # -------------------------------
    # Compare values
    # -------------------------------
    x = range(24)

    plt.figure(figsize=plotting_program.cm2inch(8, 8))

    plt.plot(x,
             peak_all_models_all_enduses_fueltype,
             color='red',
             label='modelled')
    plt.plot(x,
             validation_elec_data_2015[max_day],
             color='green',
             label='real')

    # Y-axis ticks
    #plt.ylim(0, 80)
    plt.xlim(0, 25)
    plt.yticks(range(0, 90, 10))

    #plt.axis('tight')
    plt.title("Peak day comparison", loc='left')
    plt.xlabel("Hours")
    plt.ylabel("National electrictiy use [GWh / h]")
    plt.legend()
    plt.savefig(
        os.path.join(data['paths']['path_main'], 'model_output', '01-charts',
                     name_fig))
def spatial_validation(reg_coord,
                       subnational_modelled,
                       subnational_real,
                       regions,
                       fueltype_str,
                       fig_name,
                       label_points=False,
                       plotshow=False):
    """Compare gas/elec demand for LADs

    Arguments
    ----------
    lad_infos_shapefile : dict
        Infos of shapefile (dbf / csv)
    ed_fueltype_regs_yh : object
        Regional fuel Given as GWh (?)
    subnational_real : dict
        for electricity: Sub-national electrcity demand given as GWh

    Note
    -----
    SOURCE OF LADS:
        - Data for northern ireland is not included in that, however in BEIS dataset!
    """
    logging.debug("... Validation of spatial disaggregation")
    result_dict = {}
    result_dict['real_demand'] = {}
    result_dict['modelled_demand'] = {}

    # -------------------------------------------
    # Match ECUK sub-regional demand with geocode
    # -------------------------------------------
    for region in regions:
        for reg_geocode in reg_coord:
            if reg_geocode == region:

                try:
                    # Test wheter data is provided for LAD or owtherwise ignore
                    if subnational_real[reg_geocode] == 0:
                        pass
                    else:
                        # --Sub Regional Electricity demand (as GWh)
                        result_dict['real_demand'][
                            reg_geocode] = subnational_real[reg_geocode]
                        result_dict['modelled_demand'][
                            reg_geocode] = subnational_modelled[reg_geocode]

                except KeyError:
                    logging.warning(
                        "Sub-national spatial validation: No fuel for region %s",
                        reg_geocode)

    # --------------------
    # Calculate statistics
    # --------------------
    diff_real_modelled_p = []
    diff_real_modelled_abs = []

    for reg_geocode in regions:
        try:
            real = result_dict['real_demand'][reg_geocode]
            modelled = result_dict['modelled_demand'][reg_geocode]

            diff_real_modelled_p.append((100 / real) * modelled)
            diff_real_modelled_abs.append(real - modelled)
        except KeyError:
            pass

    # Calculate the average deviation between reald and modelled
    av_deviation_real_modelled = np.average(diff_real_modelled_p)

    # Calculate standard deviation
    std_dev_p = np.std(diff_real_modelled_p)  # Given as percent
    std_dev_abs = np.std(diff_real_modelled_abs)  # Given as energy unit

    # -----------------
    # Sort results according to size
    # -----------------
    sorted_dict_real = sorted(result_dict['real_demand'].items(),
                              key=operator.itemgetter(1))

    # -------------------------------------
    # Plot
    # -------------------------------------
    fig = plt.figure(figsize=plotting_program.cm2inch(
        9, 8))  #width, height (9, 8)

    ax = fig.add_subplot(1, 1, 1)

    x_values = np.arange(0, len(sorted_dict_real), 1)

    y_real_demand = []
    y_modelled_demand = []

    labels = []
    for sorted_region in sorted_dict_real:

        geocode_lad = sorted_region[0]

        y_real_demand.append(result_dict['real_demand'][geocode_lad])
        y_modelled_demand.append(result_dict['modelled_demand'][geocode_lad])

        logging.info(
            "validation %s LAD %s: %s %s (%s p diff)", fueltype_str,
            geocode_lad, round(result_dict['real_demand'][geocode_lad], 4),
            round(result_dict['modelled_demand'][geocode_lad], 4),
            round(
                100 - (100 / result_dict['real_demand'][geocode_lad]) *
                result_dict['modelled_demand'][geocode_lad], 4))

        # Labels
        labels.append(geocode_lad)

    # Calculate r_squared
    _slope, _intercept, r_value, _p_value, _std_err = stats.linregress(
        y_real_demand, y_modelled_demand)

    # --------
    # Axis
    # --------
    plt.tick_params(
        axis='x',  # changes apply to the x-axis
        which='both',  # both major and minor ticks are affected
        bottom='off',  # ticks along the bottom edge are off
        top='off',  # ticks along the top edge are off
        labelbottom='off')  # labels along the bottom edge are off

    # ----------------------------------------------
    # Plot
    # ----------------------------------------------
    plt.plot(x_values,
             y_real_demand,
             linestyle='None',
             marker='o',
             markersize=1.6,
             fillstyle='full',
             markerfacecolor='grey',
             markeredgewidth=0.2,
             color='black',
             label='actual')

    plt.plot(x_values,
             y_modelled_demand,
             marker='o',
             linestyle='None',
             markersize=1.6,
             markerfacecolor='white',
             fillstyle='none',
             markeredgewidth=0.5,
             markeredgecolor='blue',
             color='black',
             label='model')

    # Limit
    plt.ylim(ymin=0)

    # -----------
    # Labelling
    # -----------
    if label_points:
        for pos, txt in enumerate(labels):

            ax.text(x_values[pos],
                    y_modelled_demand[pos],
                    txt,
                    horizontalalignment="right",
                    verticalalignment="top",
                    fontsize=3)

    font_additional_info = plotting_styles.font_info()

    font_additional_info['size'] = 6

    title_info = ('R_2: {}, std_%: {} (GWh {}), av_diff_%: {}'.format(
        round(r_value, 2), round(std_dev_p, 2), round(std_dev_abs, 2),
        round(av_deviation_real_modelled, 2)))

    plt.title(title_info, loc='left', fontdict=font_additional_info)

    plt.xlabel("UK regions (excluding northern ireland)")
    plt.ylabel("{} [GWh]".format(fueltype_str))

    # --------
    # Legend
    # --------
    plt.legend(prop={'family': 'arial', 'size': 8}, frameon=False)

    # Tight layout
    plt.margins(x=0)
    plt.tight_layout()
    plt.savefig(fig_name)

    if plotshow:
        plt.show()
    else:
        plt.close()
def main(regions, weather_regions, data):
    """Plot weighted HDD (HDD per Region & region pop)
    with national gas demand
    Note
    ----
    Comparison with national demand is not perfect
    because not all electricity use depends on hdd
    """
    base_yr = 2015
    # ----------------------------------
    # Read temp data and weather station
    # ----------------------------------
    weighted_daily_hdd = np.zeros((365))
    #weighted_daily_hdd_pop = np.zeros((365))

    for region in regions:

        # Get closest weather station to `Region`
        closest_weather_station = get_closest_station(
            data['reg_coord'][region]['longitude'],
            data['reg_coord'][region]['latitude'], data['weather_stations'])

        closest_weather_region = weather_regions[closest_weather_station]

        reg_pop = data['scenario_data']['population'][base_yr][region]

        for day in range(365):
            reg_hdd_day = closest_weather_region.rs_hdd_by[day]

            # ----------------------------
            # Weighted HDD with population
            # ----------------------------
            # WEIGHT WITH POP / TWO OPTIONS
            weighted_daily_hdd[day] += reg_hdd_day * reg_pop
            #weighted_daily_hdd_pop[day] += reg_hdd_day * reg_pop

    # -------------------------------
    # Calculate sum of HDD across all regions for every day [reg][day] --> [day]
    # -------------------------------

    # Convert to list
    weighted_daily_hdd = list(weighted_daily_hdd)
    #weighted_daily_hdd_pop = list(weighted_daily_hdd_pop)
    # -- Non daily metered gas demand in mcm == Residential heating gas
    # demand for year 2015 (Jan - Dez --> Across two excel in orig file)
    # gas demand for 365 days

    # Unit: GWh per day
    gas_demand_NDM_2015_2016_gwh = [
        2059.3346672, 2170.0185108, 2098.5700609, 2129.0042078, 2183.3908583,
        2183.3755211, 2181.77478289999, 2180.2661608, 2171.9539465,
        2093.1630535, 2123.4248103, 2177.2511151, 2177.4395409, 2177.4392085,
        2175.2222323, 2166.6139387, 2087.285658, 2115.1954239, 2167.4317226,
        2166.5545797, 2164.694753, 2163.4837384, 2157.386435, 2080.9887003,
        2111.8947958, 2166.0717924, 2162.6456414, 2159.295252, 2155.8334129,
        2145.8472366, 2061.1717803, 2082.3903686, 2127.0822845, 2118.2712922,
        2113.193853, 2107.8898595, 2095.8412092, 2014.9440596, 2049.2258347,
        2107.0791469, 2112.1583269, 2117.2396604, 2123.0313351, 2122.1358234,
        2051.7066905, 2087.3670451, 2136.4535688, 2132.1460485, 2128.3906968,
        2124.2843977, 2105.6629196, 2019.1113801, 2036.5569675, 2077.2039557,
        2061.8101344, 2046.7869234, 2031.4318873, 2005.2602169, 1914.0892568,
        1922.9069295, 1954.9594171, 1933.6480271, 1912.3061523, 1890.5476499,
        1862.3706414, 1775.6671805, 1783.4502818, 1814.9200643, 1796.8545889,
        1784.4710306, 1771.6500082, 1752.3369114, 1674.0247522, 1687.99816,
        1726.2909774, 1715.8915875, 1705.7032311, 1692.0716697, 1671.1552101,
        1594.1241588, 1603.713891, 1636.247885, 1620.6947572, 1605.2659081,
        1590.0104955, 1569.4656755, 1494.5719877, 1502.5278704, 1535.2362037,
        1526.2747126, 1513.4608687, 1504.8484041, 1490.7666095, 1325.9250159,
        1316.9165572, 1462.4932465, 1458.1802196, 1442.6262542, 1426.8417784,
        1411.3589019, 1335.8538668, 1333.6755582, 1356.6697705, 1334.994619,
        1313.3468669, 1291.2764263, 1261.7044342, 1187.3254679, 1182.7090036,
        1206.201116, 1187.9607269, 1169.0975458, 1150.8622665, 1125.7570188,
        1059.6150794, 1057.5077396, 1081.4643041, 1065.2552632, 1049.0529795,
        1032.9539024, 1007.1793016, 914.58361712, 897.87864486, 880.61178046,
        909.11557166, 890.86945346, 871.96514751, 853.8612021, 791.8538562,
        775.11686001, 832.03363633, 814.21901615, 799.58233329, 784.71165334,
        761.63725303, 707.19260431, 704.66692408, 729.32567359, 716.8394616,
        704.16329367, 692.60720982, 673.62744381, 625.16539826, 616.31467523,
        606.17192685, 636.72436643, 625.93400599, 615.10886486, 605.22026297,
        557.46992056, 551.34168138, 578.47909485, 570.13253752, 561.78823047,
        553.3654021, 538.91778989, 498.94506464, 500.61103512, 529.17638846,
        522.76561207, 516.42800386, 510.56638091, 496.03207692, 456.62523814,
        456.93248186, 484.57825041, 478.35283027, 472.67018165, 467.07413108,
        452.94073995, 415.61047941, 417.54936646, 447.87992936, 444.32552312,
        440.34388174, 436.93497309, 425.39778941, 390.98147195, 393.27803263,
        422.2499116, 418.01587597, 413.61939995, 409.40057065, 397.24314025,
        362.84744615, 363.93696426, 393.56430501, 390.46598983, 387.50245828,
        384.08572436, 373.79849944, 341.87745791, 344.96303388, 375.65480602,
        374.49215286, 372.75648874, 371.74226978, 361.8690835, 331.52439876,
        335.15290392, 366.77742567, 365.12052235, 364.02193295, 362.52261752,
        352.52451205, 322.45011946, 326.07034766, 357.85885375, 357.46873061,
        356.17585959, 356.18529447, 347.76795445, 318.87093053, 323.44991194,
        357.14307241, 358.48343406, 359.41495, 360.13619174, 352.30573134,
        323.75524954, 328.47959503, 361.26301948, 361.91381511, 362.52822042,
        363.04084256, 354.83105903, 327.4003489, 333.7913569, 367.75844026,
        369.11519087, 372.6949059, 375.8462941, 371.01068634, 344.6986732,
        353.4825506, 390.13714534, 393.84951909, 397.83499025, 401.57927692,
        396.97028525, 370.21486247, 379.29129941, 416.16743945, 420.07485221,
        423.97519461, 429.74321627, 427.2986801, 401.46194542, 413.22870233,
        456.07775396, 465.3295712, 474.21723331, 483.12391875, 484.18266475,
        461.009664, 476.92695202, 521.59453157, 530.84505032, 540.18546168,
        549.72258375, 551.25306059, 525.45532919, 542.29079386, 587.07994975,
        596.34233521, 607.50869098, 618.97893781, 622.86393906, 597.19837803,
        621.39030489, 674.41691171, 690.65537739, 706.66602486, 750.44401705,
        761.5020047, 735.3577927, 758.94313283, 820.97761046, 841.64549132,
        862.82785312, 882.73942176, 895.8174329, 867.22285798, 895.86950089,
        962.4264397, 986.21496809, 1010.5025124, 1034.947993, 1049.36376,
        1016.2553526, 1045.7292098, 1113.1746337, 1125.8164178, 1141.3139762,
        1159.7889682, 1167.2284687, 1125.5987857, 1158.1749163, 1228.6271493,
        1250.8619219, 1276.6254017, 1300.3160004, 1317.8170358, 1282.8879339,
        1320.3942354, 1394.2587548, 1416.5190559, 1438.5435458, 1461.7634807,
        1479.7562971, 1438.8539543, 1478.9216764, 1557.1207719, 1573.4090718,
        1587.6655331, 1603.6341589, 1613.333634, 1562.3586478, 1600.277806,
        1679.9344601, 1697.4619665, 1712.8552817, 1724.7516139, 1724.0138982,
        1657.0594241, 1682.3440925, 1748.5809406, 1752.9203251, 1763.9782637,
        1775.1642524, 1782.4227695, 1722.1387718, 1761.2175743, 1843.516748,
        1861.6814774, 1873.721509, 1884.7695907, 1889.1761128, 1820.2893554,
        1849.3759024, 1927.6865797, 1941.1637845, 1949.9179591, 1955.9424808,
        1956.9521671, 1880.0208367, 1906.0644726, 1980.6623416, 1988.0433795,
        1992.2170495, 2003.9919664, 2009.5777063, 1937.9896745, 1964.8414739,
        2036.894857, 2044.9981179, 2053.3450878, 1974.8040044, 1814.6135915,
        1904.8874509, 1909.229843, 1911.2513971, 1995.545462, 1995.3479943,
        1997.4328038
    ]

    # Total SND (includes IUK exports and storage injection)
    gas_demand_TOTALSND_2015_2016 = [
        3017, 3218, 3093, 3105, 3281, 3281, 3280, 3279, 3246, 3089, 3101, 3277,
        3278, 3278, 3276, 3242, 3085, 3095, 3270, 3269, 3267, 3267, 3235, 3081,
        3093, 3270, 3267, 3264, 3261, 3226, 3063, 3066, 3234, 3226, 3221, 3216,
        3179, 3020, 3035, 3217, 3222, 3227, 3233, 3207, 3056, 3073, 3246, 3241,
        3237, 3233, 3188, 3023, 3022, 3185, 3170, 3155, 3139, 3088, 2919, 2904,
        3058, 3037, 3016, 2994, 2941, 2773, 2764, 2915, 2897, 2885, 2872, 2828,
        2673, 2669, 2826, 2816, 2805, 2792, 2746, 2594, 2586, 2736, 2720, 2705,
        2690, 2645, 2496, 2486, 2635, 2626, 2597, 2588, 2537, 2311, 2300, 2527,
        2541, 2526, 2510, 2476, 2340, 2321, 2458, 2436, 2413, 2391, 2339, 2193,
        2175, 2315, 2302, 2287, 2274, 2230, 2098, 2084, 2223, 2211, 2200, 2190,
        2142, 1975, 1961, 2009, 2064, 2050, 2036, 2006, 1885, 1871, 2035, 2022,
        2012, 2002, 1967, 1848, 1834, 1969, 1961, 1952, 1945, 1908, 1795, 1778,
        1848, 1888, 1881, 1874, 1850, 1746, 1738, 1872, 1867, 1862, 1857, 1825,
        1721, 1711, 1845, 1842, 1839, 1836, 1803, 1700, 1689, 1821, 1817, 1814,
        1811, 1778, 1677, 1667, 1801, 1799, 1797, 1796, 1766, 1667, 1657, 1788,
        1786, 1708, 1705, 1674, 1584, 1573, 1693, 1691, 1690, 1688, 1659, 1571,
        1562, 1682, 1681, 1679, 1678, 1650, 1563, 1553, 1673, 1672, 1670, 1669,
        1637, 1550, 1540, 1660, 1659, 1657, 1656, 1628, 1542, 1533, 1653, 1653,
        1652, 1654, 1626, 1541, 1531, 1649, 1648, 1647, 1646, 1619, 1534, 1526,
        1646, 1646, 1648, 1650, 1625, 1540, 1534, 1657, 1659, 1661, 1663, 1638,
        1551, 1545, 1669, 1670, 1672, 1675, 1651, 1564, 1560, 1691, 1697, 1703,
        1709, 1688, 1602, 1601, 1735, 1742, 1748, 1754, 1733, 1643, 1643, 1779,
        1784, 1792, 1803, 1783, 1692, 1698, 1843, 1855, 1867, 2041, 2009, 1868,
        1873, 2089, 2103, 2119, 2132, 2102, 1958, 1968, 2188, 2206, 2224, 2242,
        2212, 2063, 2074, 2296, 2303, 2312, 2324, 2288, 2130, 2143, 2369, 2385,
        2404, 2422, 2395, 2243, 2261, 2491, 2508, 2524, 2541, 2514, 2357, 2376,
        2612, 2623, 2633, 2649, 2619, 2457, 2481, 2725, 2743, 2758, 2771, 2731,
        2552, 2563, 2796, 2801, 2812, 2824, 2791, 2618, 2642, 2893, 2911, 2923,
        2935, 2899, 2716, 2730, 2979, 2993, 3002, 3008, 2969, 2777, 2788, 3035,
        3042, 3047, 3059, 3024, 2835, 2847, 3033, 3041, 3050, 2907, 2758, 2710,
        2715, 2816, 2929, 2930, 2932
    ]

    # ----------------
    # Linear regression
    # ----------------
    def lin_func(x, slope, intercept):
        y = slope * x + intercept
        return y

    slope, intercept, r_value, p_value, std_err = stats.linregress(
        gas_demand_NDM_2015_2016_gwh, weighted_daily_hdd)

    logging.warning("Slope:         %s", str(slope))
    logging.warning("intercept:     %s", str(intercept))
    logging.warning("r_value:       %s", str(r_value))
    logging.warning("p_value:       %s", str(p_value))
    logging.warning("std_err:       %s", str(std_err))
    logging.warning("sum:           %s", str(sum(weighted_daily_hdd)))
    logging.warning("av:            %s",
                    str(sum(weighted_daily_hdd) / len(weighted_daily_hdd)))
    logging.warning("Nr of reg:     %s", str(len(regions)))
    logging.warning("nr of days (gray points): " +
                    str(len(gas_demand_NDM_2015_2016_gwh)))
    logging.warning("Nr of days:    " + str(len(weighted_daily_hdd)))

    # Set figure size in cm
    plt.figure(figsize=plotting_program.cm2inch(8, 8))

    # ----------------
    # PLoty daily GWh (Points are days)
    # ----------------
    plt.plot(gas_demand_NDM_2015_2016_gwh,
             weighted_daily_hdd,
             linestyle='None',
             marker='o',
             markersize=2.7,
             fillstyle='full',
             markerfacecolor='grey',
             markeredgewidth=0.2,
             color='grey')

    # ---------------------
    # Plot regression line
    # ---------------------
    x_plot = np.linspace(270, 2200, 500)
    y_plot = []
    for x in x_plot:
        y_plot.append(lin_func(x, slope, intercept))
    plt.plot(x_plot, y_plot, color='black', linestyle='--')

    plt.xlim(0, 2300)

    # ---------------------
    # Labelling
    # ---------------------
    font_additional_info = plotting_styles.font_info()
    #plt.xlabel("UK non daily metered gas demand [GWh per day]")
    #plt.ylabel("HDD * POP [mio]")
    plt.title("slope: {}, intercept: {}, r2: {})".format(
        round(slope, 3), round(intercept, 3), round(r_value, 3)),
              fontdict=font_additional_info)

    plt.tight_layout()
    plt.margins(x=0)
    plt.show()
def compare_lad_regions(fig_name, data, lad_infos_shapefile, model_run_object,
                        nr_of_fueltypes, lu_fueltypes, lu_reg):
    """Compare gas/elec demand for LADs

    Parameters
    ----------
    lad_infos_shapefile : dict
        Infos of shapefile (dbf / csv)
    model_run_object : object
        Model run results

    Note
    -----
    SOURCE OF LADS:
    """
    print("..Validation of spatial disaggregation")
    result_dict = {}
    result_dict['REAL_electricity_demand'] = {}
    result_dict['modelled_electricity_demand'] = {}

    # Match ECUK sub-regional demand with geocode
    for region_name in lu_reg:

        # Iterate loaded data
        for reg_csv_geocode in lad_infos_shapefile:
            if reg_csv_geocode == region_name:

                # --Sub Regional Electricity
                #value_gwh = unit_conversions.convert_ktoe_gwh(lad_infos_shapefile[reg_csv_geocode]['elec_tot15']) # Add data (CHECK UNIT: TODO)TODO
                result_dict['REAL_electricity_demand'][
                    region_name] = lad_infos_shapefile[reg_csv_geocode][
                        'elec_tot15']  #TODO: CHECK UNIT

                all_fueltypes_reg_demand = model_run_object.get_regional_yh(
                    nr_of_fueltypes, reg_csv_geocode)
                result_dict['modelled_electricity_demand'][
                    region_name] = np.sum(
                        all_fueltypes_reg_demand[lu_fueltypes['electricity']])

    # -----------------
    # Sort results according to size
    # -----------------
    result_dict['modelled_electricity_demand_sorted'] = {}

    # --Sorted sub regional electricity demand
    sorted_dict_REAL_elec_demand = sorted(
        result_dict['REAL_electricity_demand'].items(),
        key=operator.itemgetter(1))

    # -------------------------------------
    # Plot
    # -------------------------------------
    nr_of_labels = len(sorted_dict_REAL_elec_demand)

    x_values = []
    for i in range(nr_of_labels):
        x_values.append(0 + i * 0.2)

    y_values_REAL_electricity_demand = []
    y_values_modelled_electricity_demand = []

    labels = []
    for sorted_region in sorted_dict_REAL_elec_demand:
        y_values_REAL_electricity_demand.append(
            result_dict['REAL_electricity_demand'][sorted_region[0]])
        y_values_modelled_electricity_demand.append(
            result_dict['modelled_electricity_demand'][sorted_region[0]])
        labels.append(sorted_region)

    # RMSE calculations
    rmse_value = basic_functions.rmse(
        np.array(y_values_modelled_electricity_demand),
        np.array(y_values_REAL_electricity_demand))

    # ----------------------------------------------
    # Plot
    # ----------------------------------------------
    plt.figure(figsize=plotting_program.cm2inch(17, 10))
    plt.margins(x=0)  #remove white space

    plt.plot(x_values,
             y_values_REAL_electricity_demand,
             'ro',
             markersize=1,
             color='green',
             label='Sub-regional demand (real)')
    plt.plot(x_values,
             y_values_modelled_electricity_demand,
             'ro',
             markersize=1,
             color='red',
             label='Disaggregated demand (modelled)')

    #plt.xticks(x_values, labels, rotation=90)
    plt.ylim(0, 6000)

    title_left = (
        'Comparison of sub-regional electricity demand (RMSE: {}, number of areas= {})'
        .format(rmse_value, len(y_values_REAL_electricity_demand)))
    plt.title(title_left, loc='left')
    plt.xlabel("Regions")
    plt.ylabel("Sub-regional yearly electricity demand [GW]")
    plt.legend()

    plt.savefig(
        os.path.join(data['paths']['path_main'], 'model_output', '01-charts',
                     fig_name))
def plot_LAD_comparison_scenarios(
        scenario_data,
        year_to_plot,
        fig_name,
        plotshow=True
    ):
    """Plot chart comparing total annual demand for all LADs

    Arguments
    ---------
    scenario_data : dict
        Scenario name, scenario data
    year_to_plot : int
        Year to plot different LAD values
    fig_name : str
        Path to out pdf figure
    plotshow : bool
        Plot figure or not

    Info
    -----
    if scenario name starts with _ the legend does not work
    """

    # Get first scenario in dict
    all_scenarios = list(scenario_data.keys())
    first_scenario = str(all_scenarios[:1][0])

    # ----------------
    # Sort regions according to size
    # -----------------
    regions = {}
    for fueltype, fuels_regs in enumerate(scenario_data[first_scenario]['results_every_year'][2015]):

        for region_array_nr, fuel_reg in enumerate(fuels_regs):
            try:
                regions[region_array_nr] += np.sum(fuel_reg)
            except KeyError:
                regions[region_array_nr] = np.sum(fuel_reg)

    sorted_regions = sorted(
        regions.items(),
        key=operator.itemgetter(1))

    sorted_regions_nrs = []
    for sort_info in sorted_regions:
        sorted_regions_nrs.append(sort_info[0])

    # Labels
    labels = []
    for sorted_region in sorted_regions_nrs:
        geocode_lad = sorted_region # If actual LAD name, change this
        labels.append(geocode_lad)

    # -------------------------------------
    # Plot
    # -------------------------------------
    fig = plt.figure(
        figsize=plotting_program.cm2inch(9, 8))

    ax = fig.add_subplot(1, 1, 1)

    x_values = np.arange(0, len(sorted_regions_nrs), 1)

    # ----------------------------------------------
    # Plot base year values
    # ----------------------------------------------
    base_year_data = []
    for reg_array_nr in sorted_regions_nrs:
        base_year_data.append(regions[reg_array_nr])
    total_base_year_sum = sum(base_year_data)
    print("SUM: " + str(total_base_year_sum))

    plt.plot(
        x_values,
        base_year_data,
        linestyle='None',
        marker='o',
        markersize=1.6,
        fillstyle='full',
        markerfacecolor='grey',
        markeredgewidth=0.4,
        color='black',
        label='actual_by ({})'.format(total_base_year_sum))

    # ----------------------------------------------
    # Plot all future scenario values
    # ----------------------------------------------
    color_list = plotting_styles.color_list()

    for scenario_nr, (scenario_name, fuel_data) in enumerate(scenario_data.items()):

        sorted_year_data = []
        for reg_array_nr in sorted_regions_nrs:
            tot_fuel_across_fueltypes = 0
            for fueltype, fuel_regs in enumerate(fuel_data['results_every_year'][year_to_plot]):
                tot_fuel_across_fueltypes += np.sum(fuel_regs[reg_array_nr])

            sorted_year_data.append(tot_fuel_across_fueltypes)
        
        print("TT: " + str(sum(sorted_year_data)))
        tot_fuel_all_reg = np.sum(fuel_data['results_every_year'][year_to_plot])
        print("TOTAL FUEL in GWH " + str(tot_fuel_all_reg))

        # Calculate total annual demand
        tot_demand = sum(sorted_year_data)
        scenario_name = "{} (tot: {} [GWh])".format(
            scenario_name, round(tot_demand, 2))

        plt.plot(
            x_values,
            sorted_year_data,
            linestyle='None',
            marker='o',
            markersize=1.6,
            fillstyle='full',
            markerfacecolor=color_list[scenario_nr],
            markeredgewidth=0.4,
            color=color_list[scenario_nr],
            label=scenario_name)

    # --------
    # Axis
    # --------
    plt.tick_params(
        axis='x',          # changes apply to the x-axis
        which='both',      # both major and minor ticks are affected
        bottom='off',      # ticks along the bottom edge are off
        top='off',         # ticks along the top edge are off
        labelbottom='off') # labels along the bottom edge are off

    # Limit
    plt.ylim(ymin=0)

    # -----------
    # Labelling
    # -----------
    label_points = False
    if label_points:
        for pos, txt in enumerate(labels):
            ax.text(
                x_values[pos],
                sorted_year_data[pos],
                txt,
                horizontalalignment="right",
                verticalalignment="top",
                fontsize=3)

    plt.title(
        "TEST",
        loc='left',
        fontdict=plotting_styles.font_info())

    plt.xlabel("UK regions (excluding northern ireland)")
    plt.ylabel("[GWh]")

    # --------
    # Legend
    # --------
    plt.legend(
        prop={
            'family': 'arial',
            'size': 8},
        frameon=False)

    # Tight layout
    plt.margins(x=0)
    plt.tight_layout()
    plt.savefig(fig_name)

    if plotshow:
        plt.show()
    else:
        plt.close()
def plot_reg_y_over_time(
        scenario_data,
        fig_name,
        plotshow=False
    ):
    """Plot total demand over simulation period for every
    scenario for all regions
    """
    # Set figure size
    plt.figure(figsize=plotting_program.cm2inch(14, 8))

    y_scenario = {}

    for scenario_name, scen_data in scenario_data.items():

        data_years_regs = {}
        for year, fueltype_reg_time in scen_data['results_every_year'].items():
            data_years_regs[year] = {}

            for _fueltype, regions_fuel in enumerate(fueltype_reg_time):
                
                for region_nr, region_fuel in enumerate(regions_fuel):

                    # Sum all regions and fueltypes
                    reg_gwh_fueltype_y = np.sum(region_fuel)

                    try:
                        data_years_regs[year][region_nr] += reg_gwh_fueltype_y
                    except:
                        data_years_regs[year][region_nr] = reg_gwh_fueltype_y

        y_scenario[scenario_name] = data_years_regs

    # -----------------
    # Axis
    # -----------------
    base_yr, year_interval = 2015, 5
    first_scen = list(y_scenario.keys())[0]
    end_yr = list(y_scenario[first_scen].keys())

    major_ticks = np.arange(
        base_yr,
        end_yr[-1] + year_interval,
        year_interval)

    plt.xticks(major_ticks, major_ticks)

    # ----------
    # Plot lines
    # ----------
    color_list_selection = plotting_styles.color_list_selection()

    for scenario_name, fuel_fueltype_yrs in y_scenario.items():

        color_scenario = color_list_selection.pop()

        for year, regs in fuel_fueltype_yrs.items():
            nr_of_reg = len(regs.keys())
            break

        for reg_nr in range(nr_of_reg):
            reg_data = []
            for year, regions_fuel in fuel_fueltype_yrs.items():
                reg_data.append(regions_fuel[reg_nr])

            plt.plot(
                list(fuel_fueltype_yrs.keys()),
                list(reg_data),
                color=str(color_scenario))
    # ----
    # Axis
    # ----
    plt.ylim(ymin=0)

    # ------------
    # Plot legend
    # ------------
    plt.legend(
        ncol=2,
        loc=2,
        prop={
            'family': 'arial',
            'size': 10},
        frameon=False)

    # ---------
    # Labels
    # ---------
    plt.ylabel("GWh")
    plt.xlabel("year")
    plt.title("tot y ED all fueltypes")

    # Tight layout
    plt.tight_layout()
    plt.margins(x=0)

    plt.savefig(fig_name)

    if plotshow:
        plt.show()
        plt.close()
    else:
        plt.close()
def plot_tot_y_over_time(
        scenario_data,
        fig_name,
        plotshow=False
    ):
    """Plot total demand over simulation period for every
    scenario for all regions
    """
    # Set figure size
    plt.figure(figsize=plotting_program.cm2inch(14, 8))

    y_scenario = {}

    for scenario_name, scen_data in scenario_data.items():

        # Read out fueltype specific max h load
        data_years = {}
        for year, fueltype_reg_time in scen_data['results_every_year'].items():

            # Sum all regions and fueltypes
            tot_gwh_fueltype_y = np.sum(fueltype_reg_time)

            # Convert to TWh
            tot_twh_fueltype_y = conversions.gwh_to_twh(tot_gwh_fueltype_y)

            data_years[year] = tot_twh_fueltype_y

        y_scenario[scenario_name] = data_years

    # -----------------
    # Axis
    # -----------------
    base_yr, year_interval = 2015, 5
    first_scen = list(y_scenario.keys())[0]
    end_yr = list(y_scenario[first_scen].keys())

    major_ticks = np.arange(
        base_yr,
        end_yr[-1] + year_interval,
        year_interval)

    plt.xticks(major_ticks, major_ticks)

    # ----------
    # Plot lines
    # ----------
    color_list_selection = plotting_styles.color_list_selection()

    for scenario_name, fuel_fueltype_yrs in y_scenario.items():

        #scenario_name = "{} (tot: {} [TWh])".format(
        #scenario, round(tot_demand, 2))

        plt.plot(
            list(fuel_fueltype_yrs.keys()),     # years
            list(fuel_fueltype_yrs.values()),   # yearly data per fueltype
            color=str(color_list_selection.pop()),
            label=scenario_name)

    # ----
    # Axis
    # ----
    plt.ylim(ymin=0)

    # ------------
    # Plot legend
    # ------------
    plt.legend(
        ncol=2,
        loc=2,
        prop={
            'family': 'arial',
            'size': 10},
        frameon=False)

    # ---------
    # Labels
    # ---------
    plt.ylabel("TWh")
    plt.xlabel("year")
    plt.title("tot y ED all fueltypes")

    # Tight layout
    plt.tight_layout()
    plt.margins(x=0)

    plt.savefig(fig_name)

    if plotshow:
        plt.show()
        plt.close()
    else:
        plt.close()
def compare_results(
        name_fig,
        path_result,
        y_real_array_indo,
        y_real_array_itsdo,
        y_factored_indo,
        y_calculated_array,
        title_left,
        days_to_plot,
        plot_crit=False
    ):
    """Compare national electrictiy demand data with model results

    Note
    ----
    RMSE fit criteria : Lower values of RMSE indicate better fit
    https://stackoverflow.com/questions/17197492/root-mean-square-error-in-python

    https://matplotlib.org/examples/lines_bars_and_markers/marker_fillstyle_reference.html
    """
    logging.debug("...compare elec results")
    nr_of_h_to_plot = len(days_to_plot) * 24

    x_data = range(nr_of_h_to_plot)

    y_real_indo_factored = []
    y_calculated_list = []
    y_diff_p = []
    y_diff_abs = []

    for day in days_to_plot:
        for hour in range(24):
            y_calculated_list.append(y_calculated_array[day][hour])
            y_real_indo_factored.append(y_factored_indo[day][hour])

            # Calculate absolute differences
            y_diff_abs.append(abs(y_factored_indo[day][hour] - y_calculated_array[day][hour]))

            # Calculate difference in percent
            y_diff_p.append((100 / y_factored_indo[day][hour]) * y_calculated_array[day][hour] - 100)

    # -------------
    # RMSE
    # -------------
    rmse_val_corrected = basic_functions.rmse(np.array(y_real_indo_factored), np.array(y_calculated_list))

    # ----------
    # Standard deviation
    # ----------
    standard_dev_real_modelled = np.std(y_diff_p)       # Differences in %
    standard_dev_real_modelled_abs = np.std(y_diff_abs) # Absolute differences 

    logging.info(
        "Standard deviation given as percentage: " + str(standard_dev_real_modelled))
    logging.info(
        "Standard deviation given as GW:         " + str(standard_dev_real_modelled_abs))

    # ---------
    # R squared
    # ---------
    slope, intercept, r_value, p_value, std_err = stats.linregress(
        y_real_indo_factored,
        y_calculated_list)

    # ----------
    # Plot residuals
    # ----------
    plot_residual_histogram(
        y_diff_p, path_result, "residuals_{}".format(name_fig))

    # ----------
    # Plot figure
    # ----------
    fig = plt.figure(
        figsize=plotting_program.cm2inch(22, 8)) #width, height

    # plot points
    plt.plot(
        x_data,
        y_real_indo_factored,
        label='indo_factored',
        linestyle='-',
        linewidth=0.5,
        fillstyle='full',
        color='black')

    plt.plot(
        x_data,
        y_calculated_list,
        label='model',
        linestyle='--',
        linewidth=0.5,
        fillstyle='full',
        color='blue')

    #Grid
    #plt.grid(True)

    plt.xlim([0, 8760])
    plt.margins(x=0)
    plt.axis('tight')

    # -------------------
    # Label x axis in dates
    # -------------------
    major_ticks_days, major_ticks_labels = get_date_strings(
        days_to_plot,
        daystep=1)

    plt.xticks(major_ticks_days, major_ticks_labels)

    # ----------
    # Labelling
    # ----------
    font_additional_info = plotting_styles.font_info()

    plt.title(
        'RMSE: {} Std_dev_% {} (+-{} GW) R_2: {}'.format(
            round(rmse_val_corrected, 3),
            round(standard_dev_real_modelled, 3),
            round(standard_dev_real_modelled_abs, 3),
            round(r_value, 3)),
        fontsize=10,
        fontdict=font_additional_info,
        loc='right')

    plt.title(title_left, loc='left')

    plt.xlabel("hour", fontsize=10)
    plt.ylabel("uk elec use [GW] for {}".format(title_left), fontsize=10)

    plt.legend(frameon=False)

    plt.savefig(os.path.join(path_result, name_fig))

    if plot_crit:
        plt.show()
        plt.close()
    else:
        plt.close()
def compare_peak(
        name_fig,
        path_result,
        validation_elec_2015_peak,
        modelled_peak_dh
    ):
    """Compare peak electricity day with calculated peak energy demand

    Arguments
    ---------
    name_fig : str
        Name of figure
    local_paths : dict
        Paths
    validation_elec_2015_peak : array
        Real data of peak day
    modelled_peak_dh : array
        Modelled peak day
    """
    logging.debug("...compare elec peak results")

    # -------------------------------
    # Compare values
    # -------------------------------
    fig = plt.figure(figsize=plotting_program.cm2inch(8, 8))

    plt.plot(
        range(24),
        modelled_peak_dh,
        color='blue',
        linestyle='-',
        linewidth=0.5,
        label='model')
    #plt.plot(range(24), validation_elec_data_2015[max_day], color='green', label='real')
    plt.plot(
        range(24),
        validation_elec_2015_peak,
        color='black',
        linestyle='--',
        linewidth=0.5,
        label='actual')

    # Calculate hourly differences in %
    diff_p_h = np.round((100 / validation_elec_2015_peak) * modelled_peak_dh, 1)

    # Y-axis ticks
    plt.xlim(0, 25)
    plt.yticks(range(0, 90, 10))

    # Legend
    plt.legend(frameon=False)

    font_additional_info = plotting_styles.font_info()

    # Labelling
    plt.title("peak comparison") # d_%:{}".format(diff_p_h), loc='left', fontdict=font_additional_info)
    plt.xlabel("h")
    plt.ylabel("uk electrictiy use [GW]")

    plt.text(
        -6, 0, diff_p_h, 
        horizontalalignment='center',
        fontdict={
        'family': 'arial',
        'color': 'black',
        'weight': 'normal',
        'size': 6})

    # Tight layout
    plt.tight_layout()
    plt.margins(x=0)

    # Save fig
    plt.savefig(os.path.join(path_result, name_fig))
    plt.close()
    y = slope * x + intercept
    return y


print("...regression")
slope, intercept, r_value, p_value, std_err = stats.linregress(
    gas_demand_NDM_2015_2016, hdd_reg)

print("Slope:         " + str(slope))
print("intercept:     " + str(intercept))
print("r_value:       " + str(r_value))
print("p_value:       " + str(p_value))
print("std_err:       " + str(std_err))

# Set figure size in cm
plt.figure(figsize=plotting_program.cm2inch(8, 8))

# plot points
plt.plot(gas_demand_NDM_2015_2016, hdd_reg, 'ro', markersize=5, color='gray')

# plot line
#plt.plot(gas_demand_NDM_2015_2016, hdd_reg, 'ro')

# Plot regression line
X_plot = np.linspace(300, 2250, 500)

Y_plot = []
for x in X_plot:
    Y_plot.append(lin_func(x, slope, intercept))

plt.plot(X_plot, Y_plot, color='k')