コード例 #1
0
ファイル: tistel_demo.py プロジェクト: yisak/shyft
def plot_results(ptgsk, q_obs=None):
    h_obs = None
    if ptgsk is not None:
        plt.subplot(3, 1, 1)
        discharge = ptgsk.region_model.statistics.discharge([0])
        temp = ptgsk.region_model.statistics.temperature([0])
        precip = ptgsk.region_model.statistics.precipitation([0])
        # Results on same time axis, so we only need one
        times = utc_to_greg([discharge.time(i) for i in range(discharge.size())])
        plt.plot(times, np.array(discharge.v))
        plt.gca().set_xlim(times[0], times[-1])
        plt.ylabel(r"Discharge in $\mathbf{m^3s^{-1}}$")
        set_calendar_formatter(Calendar())
    if q_obs is not None:
        obs_times = utc_to_greg([q_obs.time(i) for i in range(q_obs.size())])
        ovs = [q_obs.value(i) for i in range(q_obs.size())]
        h_obs, = plt.plot(obs_times, ovs, linewidth=2, color='k')
        ax = plt.gca()
        ax.set_xlim(obs_times[0], obs_times[-1])
    if ptgsk is not None:
        plt.subplot(3, 1, 2)
        plt.plot(times, np.array(temp.v))
        set_calendar_formatter(Calendar())
        plt.gca().set_xlim(times[0], times[-1])
        plt.ylabel(r"Temperature in C")
        plt.subplot(3, 1, 3)
        plt.plot(times, np.array(precip.v))
        set_calendar_formatter(Calendar())
        plt.gca().set_xlim(times[0], times[-1])
        plt.ylabel(r"Precipitation in mm")
    return h_obs
コード例 #2
0
ファイル: tistel_demo.py プロジェクト: ludoguer/shyft
def plot_results(ptgsk, q_obs=None):
    h_obs = None
    if ptgsk is not None:
        plt.subplot(3, 1, 1)
        discharge = ptgsk.region_model.statistics.discharge([0])
        temp = ptgsk.region_model.statistics.temperature([0])
        precip = ptgsk.region_model.statistics.precipitation([0])
        # Results on same time axis, so we only need one
        times = utc_to_greg(
            [discharge.time(i) for i in range(discharge.size())])
        plt.plot(times, np.array(discharge.v))
        plt.gca().set_xlim(times[0], times[-1])
        plt.ylabel(r"Discharge in $\mathbf{m^3s^{-1}}$")
        set_calendar_formatter(Calendar())
    if q_obs is not None:
        obs_times = utc_to_greg([q_obs.time(i) for i in range(q_obs.size())])
        ovs = [q_obs.value(i) for i in range(q_obs.size())]
        h_obs, = plt.plot(obs_times, ovs, linewidth=2, color='k')
        ax = plt.gca()
        ax.set_xlim(obs_times[0], obs_times[-1])
    if ptgsk is not None:
        plt.subplot(3, 1, 2)
        plt.plot(times, np.array(temp.v))
        set_calendar_formatter(Calendar())
        plt.gca().set_xlim(times[0], times[-1])
        plt.ylabel(r"Temperature in C")
        plt.subplot(3, 1, 3)
        plt.plot(times, np.array(precip.v))
        set_calendar_formatter(Calendar())
        plt.gca().set_xlim(times[0], times[-1])
        plt.ylabel(r"Precipitation in mm")
    return h_obs
コード例 #3
0
ファイル: tistel_demo.py プロジェクト: yisak/shyft
def plot_percentiles(sim, percentiles, obs=None):
    discharges = [s.region_model.statistics.discharge([0]) for s in sim]
    times = utc_to_greg(np.array([discharges[0].time(i) for i in range(discharges[0].size())], dtype='d'))
    all_discharges = np.array([d.v for d in discharges])
    perc_arrs = [a for a in np.percentile(all_discharges, percentiles, 0)]
    h, fill_handles = plot_np_percentiles(times, perc_arrs, base_color=(51/256, 102/256, 193/256))
    percentile_texts = ["{} - {}".format(percentiles[i], percentiles[-(i + 1)]) for i in range(len(percentiles)//2)]
    ax = plt.gca()
    maj_loc = AutoDateLocator(tz=pytz.UTC, interval_multiples=True)
    ax.xaxis.set_major_locator(maj_loc)
    set_calendar_formatter(Calendar())
    if len(percentiles) % 2:
        fill_handles.append(h[0])
        percentile_texts.append("{}".format(percentiles[len(percentiles)//2]))
    if obs is not None:
        h_obs = plot_results(None, obs)
        fill_handles.append(h_obs)
        percentile_texts.append("Observed")

    ax.legend(fill_handles, percentile_texts)
    ax.grid(b=True, color=(51/256, 102/256, 193/256), linewidth=0.1, linestyle='-', axis='y')
    plt.xlabel("Time in UTC")
    plt.ylabel(r"Discharge in $\mathbf{m^3s^{-1}}$", verticalalignment="top", rotation="horizontal")
    ax.yaxis.set_label_coords(0, 1.1)
    return h, ax
コード例 #4
0
ファイル: tistel_demo.py プロジェクト: muguangyuze/shyft
def plot_percentiles(sim, percentiles, obs=None):
    discharges = [s.region_model.statistics.discharge([0]) for s in sim]
    times = utc_to_greg(np.array([discharges[0].time(i) for i in range(discharges[0].size())], dtype='d'))
    all_discharges = np.array([d.v for d in discharges])
    perc_arrs = [a for a in np.percentile(all_discharges, percentiles, 0)]
    h, fill_handles = plot_np_percentiles(times, perc_arrs, base_color=(51/256, 102/256, 193/256))
    percentile_texts = ["{} - {}".format(percentiles[i], percentiles[-(i + 1)]) for i in range(len(percentiles)//2)]
    ax = plt.gca()
    maj_loc = AutoDateLocator(tz=pytz.UTC, interval_multiples=True)
    ax.xaxis.set_major_locator(maj_loc)
    set_calendar_formatter(Calendar())
    if len(percentiles) % 2:
        fill_handles.append(h[0])
        percentile_texts.append("{}".format(percentiles[len(percentiles)//2]))
    if obs is not None:
        h_obs = plot_results(None, obs)
        fill_handles.append(h_obs)
        percentile_texts.append("Observed")

    ax.legend(fill_handles, percentile_texts)
    ax.grid(b=True, color=(51/256, 102/256, 193/256), linewidth=0.1, linestyle='-', axis='y')
    plt.xlabel("Time in UTC")
    plt.ylabel(r"Discharge in $\mathbf{m^3s^{-1}}$", verticalalignment="top", rotation="horizontal")
    ax.yaxis.set_label_coords(0, 1.1)
    return h, ax
コード例 #5
0
ファイル: tistel_demo.py プロジェクト: yisak/shyft
def continuous_calibration():
    utc = Calendar()
    t_start = utc.time(YMDhms(2011, 9, 1))
    t_fc_start = utc.time(YMDhms(2015, 10, 1))
    dt = deltahours(1)
    n_obs = int(round((t_fc_start - t_start)/dt))
    obs_time_axis = Timeaxis(t_start, dt, n_obs + 1)
    q_obs_m3s_ts = observed_tistel_discharge(obs_time_axis.total_period())

    ptgsk = create_tistel_simulator(PTGSKOptModel, tistel.geo_ts_repository(tistel.grid_spec.epsg()))
    initial_state = burn_in_state(ptgsk, t_start, utc.time(YMDhms(2012, 9, 1)), q_obs_m3s_ts)

    num_opt_days = 30
    # Step forward num_opt_days days and store the state for each day:
    recal_start = t_start + deltahours(num_opt_days*24)
    t = t_start
    state = initial_state
    opt_states = {t: state}
    while t < recal_start:
        ptgsk.run(Timeaxis(t, dt, 24), state)
        t += deltahours(24)
        state = ptgsk.reg_model_state
        opt_states[t] = state

    recal_stop = utc.time(YMDhms(2011, 10, 30))
    recal_stop = utc.time(YMDhms(2012, 5, 30))
    curr_time = recal_start
    q_obs_avg = TsTransform().to_average(t_start, dt, n_obs + 1, q_obs_m3s_ts)
    target_spec = TargetSpecificationPts(q_obs_avg, IntVector([0]), 1.0, KLING_GUPTA)
    target_spec_vec = TargetSpecificationVector([target_spec])
    i = 0
    times = []
    values = []
    p, p_min, p_max = construct_calibration_parameters(ptgsk)
    while curr_time < recal_stop:
        print(i)
        i += 1
        opt_start = curr_time - deltahours(24*num_opt_days)
        opt_state = opt_states.pop(opt_start)
        p = ptgsk.region_model.get_region_parameter()
        p_opt = ptgsk.optimize(Timeaxis(opt_start, dt, 24*num_opt_days), opt_state, target_spec_vec,
                               p, p_min, p_max, tr_stop=1.0e-5)
        ptgsk.region_model.set_region_parameter(p_opt)
        corr_state = adjust_simulator_state(ptgsk, curr_time, q_obs_m3s_ts)
        ptgsk.run(Timeaxis(curr_time, dt, 24), corr_state)
        curr_time += deltahours(24)
        opt_states[curr_time] = ptgsk.reg_model_state
        discharge = ptgsk.region_model.statistics.discharge([0])
        times.extend(discharge.time(i) for i in range(discharge.size()))
        values.extend(list(np.array(discharge.v)))
    plt.plot(utc_to_greg(times), values)
    plot_results(None, q_obs=observed_tistel_discharge(UtcPeriod(recal_start, recal_stop)))
    set_calendar_formatter(Calendar())
    #plt.interactive(1)
    plt.title("Continuously recalibrated discharge vs observed")
    plt.xlabel("Time in UTC")
    plt.ylabel(r"Discharge in $\mathbf{m^3s^{-1}}$", verticalalignment="top", rotation="horizontal")
    plt.gca().yaxis.set_label_coords(0, 1.1)
コード例 #6
0
ファイル: tistel_demo.py プロジェクト: muguangyuze/shyft
def continuous_calibration():
    utc = Calendar()
    t_start = utc.time(YMDhms(2011, 9, 1))
    t_fc_start = utc.time(YMDhms(2015, 10, 1))
    dt = deltahours(1)
    n_obs = int(round((t_fc_start - t_start)/dt))
    obs_time_axis = TimeAxisFixedDeltaT(t_start, dt, n_obs + 1)
    q_obs_m3s_ts = observed_tistel_discharge(obs_time_axis.total_period())

    ptgsk = create_tistel_simulator(PTGSKOptModel, tistel.geo_ts_repository(tistel.grid_spec.epsg()))
    initial_state = burn_in_state(ptgsk, t_start, utc.time(YMDhms(2012, 9, 1)), q_obs_m3s_ts)

    num_opt_days = 30
    # Step forward num_opt_days days and store the state for each day:
    recal_start = t_start + deltahours(num_opt_days*24)
    t = t_start
    state = initial_state
    opt_states = {t: state}
    while t < recal_start:
        ptgsk.run(TimeAxisFixedDeltaT(t, dt, 24), state)
        t += deltahours(24)
        state = ptgsk.reg_model_state
        opt_states[t] = state

    recal_stop = utc.time(YMDhms(2011, 10, 30))
    recal_stop = utc.time(YMDhms(2012, 5, 30))
    curr_time = recal_start
    q_obs_avg = TsTransform().to_average(t_start, dt, n_obs + 1, q_obs_m3s_ts)
    target_spec = TargetSpecificationPts(q_obs_avg, IntVector([0]), 1.0, KLING_GUPTA)
    target_spec_vec = TargetSpecificationVector([target_spec])
    i = 0
    times = []
    values = []
    p, p_min, p_max = construct_calibration_parameters(ptgsk)
    while curr_time < recal_stop:
        print(i)
        i += 1
        opt_start = curr_time - deltahours(24*num_opt_days)
        opt_state = opt_states.pop(opt_start)
        p = ptgsk.region_model.get_region_parameter()
        p_opt = ptgsk.optimize(TimeAxisFixedDeltaT(opt_start, dt, 24*num_opt_days), opt_state, target_spec_vec,
                               p, p_min, p_max, tr_stop=1.0e-5)
        ptgsk.region_model.set_region_parameter(p_opt)
        corr_state = adjust_simulator_state(ptgsk, curr_time, q_obs_m3s_ts)
        ptgsk.run(TimeAxisFixedDeltaT(curr_time, dt, 24), corr_state)
        curr_time += deltahours(24)
        opt_states[curr_time] = ptgsk.reg_model_state
        discharge = ptgsk.region_model.statistics.discharge([0])
        times.extend(discharge.time(i) for i in range(discharge.size()))
        values.extend(list(np.array(discharge.v)))
    plt.plot(utc_to_greg(times), values)
    plot_results(None, q_obs=observed_tistel_discharge(UtcPeriod(recal_start, recal_stop)))
    set_calendar_formatter(Calendar())
    #plt.interactive(1)
    plt.title("Continuously recalibrated discharge vs observed")
    plt.xlabel("Time in UTC")
    plt.ylabel(r"Discharge in $\mathbf{m^3s^{-1}}$", verticalalignment="top", rotation="horizontal")
    plt.gca().yaxis.set_label_coords(0, 1.1)
コード例 #7
0
ファイル: kjela_hbv_demo.py プロジェクト: yisak/shyft
def plot_results(ptxsk, q_obs=None):
    h_obs = None
    n_temp = 1
    temp=[]
    precip = None
    discharge = None
    plt.figure() # to start a new plot window
    if ptxsk is not None:
        plt.subplot(8, 1, 1) # dimension 8 rows of plots
        discharge = ptxsk.region_model.statistics.discharge([]) # get the sum discharge all areas
        n_temp=ptxsk.region_model.size()
        temp = [ptxsk.region_model.statistics.temperature([i]) for i in range(n_temp)]
        precip = [ptxsk.region_model.statistics.precipitation([i]) for i in range(n_temp)]
        radiation = [ptxsk.region_model.statistics.radiation([i]) for i in range(n_temp)]
        rel_hum = [ptxsk.region_model.statistics.rel_hum([i]) for i in range(n_temp)]
        wind_speed = [ptxsk.region_model.statistics.wind_speed([i]) for i in range(n_temp)]
        snow_sca = [ptxsk.region_model.hbv_snow_state.sca([i]) for i in range(n_temp)]
        snow_swe = [ptxsk.region_model.hbv_snow_state.swe([i]) for i in range(n_temp)]
        # Results on same time axis, so we only need one
        times = utc_to_greg([discharge.time(i) for i in range(discharge.size())])
        plt.plot(times, np.array(discharge.v),color='blue')
        ax=plt.gca()
        ax.set_xlim(times[0], times[-1])
        plt.ylabel(r"Discharge in $\mathbf{m^3/s}$")
        set_calendar_formatter(Calendar())
    if q_obs is not None:
        obs_times = utc_to_greg([q_obs.time(i) for i in range(q_obs.size())])
        ovs = [q_obs.value(i) for i in range(q_obs.size())]
        h_obs, = plt.plot(obs_times, ovs, linewidth=2,color='green')


    if ptxsk is not None:
        plt.subplot(8, 1, 2,sharex=ax)
        for i in range(n_temp):
            plt.plot(times, np.array(temp[i].v))
        #set_calendar_formatter(Calendar())
        plt.ylabel(r"Temperature in C")

        plt.subplot(8, 1, 3,sharex=ax)
        for i in range(n_temp):
            plt.plot(times, np.array(precip[i].v))
        plt.ylabel(r"Precipitation in mm")

        plt.subplot(8,1,4,sharex=ax)
        for i in range(n_temp):
            plt.plot(times,np.array(radiation[i].v))
        plt.ylabel(r"Radiation w/m2")

        plt.subplot(8, 1, 5,sharex=ax)
        for i in range(n_temp):
            plt.plot(times, np.array(rel_hum[i].v))
        plt.ylabel(r"Rel.hum  %")

        plt.subplot(8, 1, 6,sharex=ax)
        for i in range(n_temp):
            plt.plot(times, np.array(wind_speed[i].v))
        plt.ylabel(r"Wind speed  m/s")

        plt.subplot(8, 1, 7,sharex=ax)
        for i in range(n_temp):
            plt.plot(times, np.asarray(snow_swe[i].v)[:-1])
        plt.ylabel(r"SWE  mm")

        plt.subplot(8, 1, 8,sharex=ax)
        for i in range(n_temp):
            plt.plot(times, np.asarray(snow_sca[i].v)[:-1])
        plt.ylabel(r"SCA  %")

    return h_obs