In [ ]:
import matplotlib.pyplot as plt
import pandas as pd

In [ ]:
%cd /work/charis/ti_model/calibrations
%ls

In [ ]:
file = "IN_Hunza_at_DainyorBridge_3str_DDFnbr=10mm_N050_M050.SA_summary.dat"
drainageid = "IN_Hunza_at_DainyorBridge"
label = "3str DDFnbr=10mm N050 M050"

In [ ]:
import re
# summary file is like this: AM_Vakhsh_at_Komsomolabad_3str_DDFnbr=10mm_N050_M050.SA_summary.dat
# drainageid is string
# label is string
# output will be written to : drainageid_label.SA_summary.png
def plot_sa_summary(file, drainageid, label):
    caldf = pd.read_pickle(file)
    
    best_model = "%.2f_%.2f_%.2f_%.2f" % (
        caldf.iloc[-1].winter_snow_ddf,
        caldf.iloc[-1].summer_snow_ddf,
        caldf.iloc[-1].winter_ice_ddf,
        caldf.iloc[-1].summer_ice_ddf)
    
    # Parse the filename for nCycles and nTrials
    p = re.compile(r"_N(\d*)_M(\d*)")
    m = p.search(file)
    numCycles = int(m.group(1))
    numTrials = int(m.group(2))
    
    fig, ax = plt.subplots(2, 1, figsize=(8,6))

    fig.suptitle(
                "%s Best by SA=%s\n(%s)" % (
                drainageid, 
                best_model,
                label),
            fontsize=12)

    caldf["min_cycle_z"].plot(ax=ax[0], style='k.-')
    ax[0].legend(["Cost"])
    ax[0].set_ylabel('Cost')

    ddfs = caldf.drop(labels='min_cycle_z', axis=1)
    ddfs.plot(ax=ax[1], style=['b.-', 'b--', 'r.-', 'r--'])
    handles, labels = ax[1].get_legend_handles_labels()
    ax[1].legend(handles[::-1], labels[::-1])
    ax[1].set_xlabel('Calibration Cycle (%d trials/cycle)' % numTrials)
    ax[1].set_ylabel('DDF ($mm$)')
    
    fig.tight_layout()
    fig.subplots_adjust(top=0.86)

    outfile = "%s_%s.SA_summary.png" % (drainageid, label.replace(" ", "_"))
    plt.savefig(outfile)
    print("Plot saved to %s" % outfile)

In [ ]:
#caldf = pd.read_pickle(file)
#caldf

In [ ]:
plot_sa_summary(file, drainageid, label)

In [ ]:
caldf.iloc[-1].winter_snow_ddf

In [ ]:
test = "this_is_a_test_N050_M030"
p = re.compile(r"_N(\d*)_")
m = p.search(test)
m.group(1)

In [ ]:
int(m.group(1))

In [ ]: