def extend_fine(idir, ifile, extend_aggregation, aggregation, ofile):
    r'''
    '''    
    dic = calculate_change_factor(idir, ifile)
    fmin = scipy.optimize.fmin_l_bfgs_b
    fmin = scipy.optimize.fmin        

    # Changed factors original data
    dsf = dic['dsf']
    mean = dic['mean']
    var = dic['var']
    skew = dic['skew']
    correl_ori = []
    skew_ori = []

    # Find the original 
    # -----------------
    dic = wg_io.load(ifile)
    ts = 1
    lag = 1
    accumulate = True
    group_type = 'm'
    dic_stat = wg_tool.series_statistics(dic, ts, extend_aggregation, lag=lag, 
                                  group_type=group_type, accumulate=accumulate)

    # [Ey, VARy, CVy, SKy, Rly, DSFy]
    dicdic = dic_stat['period']
    for g in range(12):
        skew_index = 3
        skew_ori.append(dicdic[skew_index][g])

        correl_index = 4
        correl_ori.append(dicdic[correl_index][g])        

    # Find indexes to use extension values for bellow 24 hours and calculates 
    # values for over 24 hours    
    n_group = len(dsf)    
    pre_index = []
    post_index = []    

    for i in range(len(extend_aggregation)):
        if not(extend_aggregation[i] in aggregation):
            pre_index.append(i)
    
    for i in range(len(aggregation)):    
        if (aggregation[i] in extend_aggregation):
            post_index.append(i)

    h = numpy.array(aggregation)
    h_ext = numpy.array(extend_aggregation)
        
    # Extend mean: E(h) = A*h. Is linear and cuts in 0. Either one value of future
    # is used, or a regression is made...
    # ---------------------------------------------------------------------- 
    mean_new = []
    for g in range(n_group):
        mean_h = mean[g]        
        Xo = [1]   # A
        Xf = fmin(mean_min, Xo, args=(h, mean_h), disp=0)
        A = Xf[0]
        mean_adj = A * h_ext
        
        mean_res = [mean_adj[i] for i in pre_index] + [mean_h[j] for j in post_index]
        mean_new.append(numpy.array(mean_res))

#        matplotlib.pyplot.plot(h_ext, mean_adj, 'o--')
#        matplotlib.pyplot.plot(h, mean_h)
#        matplotlib.pyplot.show()
        
    # Extend dry spell fraction: dsf(h) = A*(e^h*B)
    # ---------------------------------------------
    # TODO: include the different groupings
    dsf_new = []
    for g in range(n_group):
        dsf_h = dsf[g]        
        Xo = [-1]   # A
        
        if False:
            # Only use 24 72        
            hh = [h[0], h[1]]
            dsf_hh = [dsf_h[0], dsf_h[1]]
        else:
            hh = h
            dsf_hh = dsf_h

        #print fmin(dsf_min, Xo, args=(h, dsf_h), approx_grad=True, bounds=bounds, maxfun=3000)     
        Xf = fmin(dsf_min, Xo, args=(hh, dsf_hh), disp=0)
        A = Xf[0]

        
        dsf_adj = numpy.exp(A * h_ext)
        
        dsf_res = [dsf_adj[i] for i in pre_index] + [dsf_h[j] for j in post_index]
        dsf_new.append(numpy.array(dsf_res))

#        matplotlib.pyplot.plot(h_ext, dsf_adj, 'o--')
#        matplotlib.pyplot.plot(h, dsf_h)
#        matplotlib.pyplot.show()

    # Skewness.... hmmmm difficult so far so only observed is downscaled
    # ---------------------------------------------
    # TODO: include the different groupings
    skew_new = []
    for g in range(n_group):
        skew_h = skew[g]
        skew_adj = skew_ori[g]
        skew_res = [skew_adj[i] for i in pre_index] + [skew_h[j] for j in post_index]
        skew_new.append(numpy.array(skew_res))

    # Autocorrelation.... very difficult indeed not taken into account
    # ---------------------------------------------    
    correl_new = correl_ori

    # Variance
    # -------------------------------------------------
    var_new = []
    for g in range(n_group):
        var_h = var[g]

        Xo = [0.5, 1, 0.8]
        Xf = fmin(var_min, Xo, args=(aggregation, var_h), disp=0)

        eps =Xf[0]     ## [h]
        Sig2i = Xf[1]  ## [mm^2]
        alp = Xf[2]    ## []

        var_adj = variance_t_downscaling(extend_aggregation, eps, Sig2i, alp)
        var_res = [var_adj[i] for i in pre_index] + [var_h[j] for j in post_index]
        var_new.append(numpy.array(var_res))

    # Coefficient of variance
    #-----------------------
    cv_new = [(var_new[i])**0.5 / mean_new[i] for i in range(len(mean_new))]
    
    dic = {'aggregation': extend_aggregation,
           'dsf': dsf_new,
           'mean': mean_new,
           'var': var_new,
           'skew': skew_new,
           'correl': correl_new,
           'cv': cv_new}
           
    # Plot test
#    for g in range(n_group):
#        for key in dic:
#            x = dic['aggregation']
#            if not(key == 'aggregation'):
#                y = dic[key][g]
#                
#                fig = matplotlib.pyplot.figure()
##                fig.subplots_adjust(left=0.10, bottom=0.10, wspace=0.40, hspace=0.20) 
#                fig.suptitle(key)
#                ax = fig.add_subplot(111)
#                ax.set_axisbelow(True)
#                ax.plot(x, y, 'b-', label='period statistics')
#                matplotlib.pyplot.show()

    wg_io.save(dic, ofile)

    return dic
Exemplo n.º 2
0
def extend_fine(idir, ifile, extend_aggregation, aggregation, ofile):
    r'''
    '''
    dic = calculate_change_factor(idir, ifile)
    fmin = scipy.optimize.fmin_l_bfgs_b
    fmin = scipy.optimize.fmin

    # Changed factors original data
    dsf = dic['dsf']
    mean = dic['mean']
    var = dic['var']
    skew = dic['skew']
    correl_ori = []
    skew_ori = []

    # Find the original
    # -----------------
    dic = wg_io.load(ifile)
    ts = 1
    lag = 1
    accumulate = True
    group_type = 'm'
    dic_stat = wg_tool.series_statistics(dic,
                                         ts,
                                         extend_aggregation,
                                         lag=lag,
                                         group_type=group_type,
                                         accumulate=accumulate)

    # [Ey, VARy, CVy, SKy, Rly, DSFy]
    dicdic = dic_stat['period']
    for g in range(12):
        skew_index = 3
        skew_ori.append(dicdic[skew_index][g])

        correl_index = 4
        correl_ori.append(dicdic[correl_index][g])

    # Find indexes to use extension values for bellow 24 hours and calculates
    # values for over 24 hours
    n_group = len(dsf)
    pre_index = []
    post_index = []

    for i in range(len(extend_aggregation)):
        if not (extend_aggregation[i] in aggregation):
            pre_index.append(i)

    for i in range(len(aggregation)):
        if (aggregation[i] in extend_aggregation):
            post_index.append(i)

    h = numpy.array(aggregation)
    h_ext = numpy.array(extend_aggregation)

    # Extend mean: E(h) = A*h. Is linear and cuts in 0. Either one value of future
    # is used, or a regression is made...
    # ----------------------------------------------------------------------
    mean_new = []
    for g in range(n_group):
        mean_h = mean[g]
        Xo = [1]  # A
        Xf = fmin(mean_min, Xo, args=(h, mean_h), disp=0)
        A = Xf[0]
        mean_adj = A * h_ext

        mean_res = [mean_adj[i]
                    for i in pre_index] + [mean_h[j] for j in post_index]
        mean_new.append(numpy.array(mean_res))

#        matplotlib.pyplot.plot(h_ext, mean_adj, 'o--')
#        matplotlib.pyplot.plot(h, mean_h)
#        matplotlib.pyplot.show()

# Extend dry spell fraction: dsf(h) = A*(e^h*B)
# ---------------------------------------------
# TODO: include the different groupings
    dsf_new = []
    for g in range(n_group):
        dsf_h = dsf[g]
        Xo = [-1]  # A

        if False:
            # Only use 24 72
            hh = [h[0], h[1]]
            dsf_hh = [dsf_h[0], dsf_h[1]]
        else:
            hh = h
            dsf_hh = dsf_h

        #print fmin(dsf_min, Xo, args=(h, dsf_h), approx_grad=True, bounds=bounds, maxfun=3000)
        Xf = fmin(dsf_min, Xo, args=(hh, dsf_hh), disp=0)
        A = Xf[0]

        dsf_adj = numpy.exp(A * h_ext)

        dsf_res = [dsf_adj[i]
                   for i in pre_index] + [dsf_h[j] for j in post_index]
        dsf_new.append(numpy.array(dsf_res))


#        matplotlib.pyplot.plot(h_ext, dsf_adj, 'o--')
#        matplotlib.pyplot.plot(h, dsf_h)
#        matplotlib.pyplot.show()

# Skewness.... hmmmm difficult so far so only observed is downscaled
# ---------------------------------------------
# TODO: include the different groupings
    skew_new = []
    for g in range(n_group):
        skew_h = skew[g]
        skew_adj = skew_ori[g]
        skew_res = [skew_adj[i]
                    for i in pre_index] + [skew_h[j] for j in post_index]
        skew_new.append(numpy.array(skew_res))

    # Autocorrelation.... very difficult indeed not taken into account
    # ---------------------------------------------
    correl_new = correl_ori

    # Variance
    # -------------------------------------------------
    var_new = []
    for g in range(n_group):
        var_h = var[g]

        Xo = [0.5, 1, 0.8]
        Xf = fmin(var_min, Xo, args=(aggregation, var_h), disp=0)

        eps = Xf[0]  ## [h]
        Sig2i = Xf[1]  ## [mm^2]
        alp = Xf[2]  ## []

        var_adj = variance_t_downscaling(extend_aggregation, eps, Sig2i, alp)
        var_res = [var_adj[i]
                   for i in pre_index] + [var_h[j] for j in post_index]
        var_new.append(numpy.array(var_res))

    # Coefficient of variance
    #-----------------------
    cv_new = [(var_new[i])**0.5 / mean_new[i] for i in range(len(mean_new))]

    dic = {
        'aggregation': extend_aggregation,
        'dsf': dsf_new,
        'mean': mean_new,
        'var': var_new,
        'skew': skew_new,
        'correl': correl_new,
        'cv': cv_new
    }

    # Plot test
    #    for g in range(n_group):
    #        for key in dic:
    #            x = dic['aggregation']
    #            if not(key == 'aggregation'):
    #                y = dic[key][g]
    #
    #                fig = matplotlib.pyplot.figure()
    ##                fig.subplots_adjust(left=0.10, bottom=0.10, wspace=0.40, hspace=0.20)
    #                fig.suptitle(key)
    #                ax = fig.add_subplot(111)
    #                ax.set_axisbelow(True)
    #                ax.plot(x, y, 'b-', label='period statistics')
    #                matplotlib.pyplot.show()

    wg_io.save(dic, ofile)

    return dic
Exemplo n.º 3
0
Arquivo: nsrp.py Projeto: islenv/HIPy
        else:
            day_normal.append(numpy.int(numpy.round(tail, 0)))
            day_leap.append(numpy.int(numpy.round(tail + 1, 0)))
        
        #10 + [(365-14*10)]
    
    dic = parameter_NSRP(rain_grouped, ts, aggregation, lag,  stats=stats, 
                         weights=weights, change_factors=change_factors, DEBUG=True, 
                         MY_METHOD=MY_METHOD)

    dic['day_normal'] = day_normal
    dic['day_leap'] = day_leap
                                 
    # Here is to save
    print 'Saving: ' + str(ofile)
    wg_io.save(dic, ofile)

    return dic

def run_gen(ifile, ofile, start_year, end_year):
    r'''
    
    Parameters
    ----------
    ifile : String
        TODO
    save_as : String
        TODO
    start_year : Integer
        Inclusive
    end_year : Integer