Пример #1
0
                                       smooth_window=cfg['PARAM']['smooth_window'], \
                                       n_runs=cfg['PARAM']['n_runs'], \
                                       skip_steps=cfg['PARAM']['skip_steps'], \
                                       start_date_data=start_date, \
                                       time_step=cfg['PARAM']['time_step'], \
                                       ksteps=cfg['PARAM']['ksteps'], \
                                       latlon_precision=cfg['PARAM']['latlon_precision'])

# Select full water years
start_date_WY, end_date_WY = my_functions.find_full_water_years_within_a_range(\
                                dict_s_total_runoff[dict_s_total_runoff.keys()[0]].index[0],\
                                dict_s_total_runoff[dict_s_total_runoff.keys()[0]].index[-1])

for lat_lon in dict_s_total_runoff.keys():
    s_total_runoff = dict_s_total_runoff[lat_lon]
    dict_s_total_runoff[lat_lon] = my_functions.select_time_range(\
                                        s_total_runoff, start_date_WY, end_date_WY)

##===============================================================#
## 1) Adjust negative runoff to zero;
## 2) Rescale each month so that water is balanced within the month
##===============================================================#
#for lat_lon in dict_s_total_runoff.keys():
#    print 'Adjusting negative runoff and rescaling {}...'.format(lat_lon)
#    s_total_runoff = dict_s_total_runoff[lat_lon]
#    # Original sum of water (with negative runoff)
#    sum_mon_orig = s_total_runoff.resample("M", how='sum')
#    # Set negative runoff to zero
#    s_total_runoff[s_total_runoff<0] = 0  
#    # Calculate new sum of water (higher than original)
#    sum_mon_new = s_total_runoff.resample("M", how='sum')
#    # Rescale the new runoff to keep original water balance
        # Load VIC output file, if this is an active cell
        if fdir[i][j]!=int(NODATA_value):  # if active cell
            vic_output_file = '{}/{}_{:.{}f}_{:.{}f}'\
                                .format(cfg['INPUT']['vic_output_dir'], \
                                        cfg['INPUT']['vic_output_file_prefix'], \
                                        lat, cfg['INPUT']['vic_output_precise'], \
                                        lon, cfg['INPUT']['vic_output_precise'])
            # Load file
            df = my_functions.read_VIC_output(vic_output_file, data_columns=[6,7], \
                                         data_names=['runoff','baseflow'], \
                                         header=True, date_col=3)
            # Calculate total runoff
            s_total_runoff = df['runoff'] + df['baseflow']
            # Select time range interested
            s_total_runoff = my_functions.select_time_range(s_total_runoff, \
                                                            start_date, \
                                                            end_date)
            # Add to final dataframe
            df_vic_output['row{}_col{}'.format(i+1,j+1)] = s_total_runoff
                                        
        else:  # if inactive cell
            df_vic_output['row{}_col{}'.format(i+1,j+1)] = s_inactive

#=====================================================#
# Write runoff field to file
#=====================================================#
df_vic_output.to_csv(cfg['OUTPUT']['basin_runoff_path'], sep=' ', \
                  header=False, index=False)


# Load and process VIC output data - flow
#====================================================#
print 'Loading and processing VIC output flow data...'

#=== Load data ===#
ds_flow = xray.open_dataset(RVIC_output_nc)
da_flow = ds_flow['streamflow'][:-1,:,:]  # Remove the last junk time step

#=== Put data for each grid cell into a df ===#
list_df = []
for i, lat_lon in enumerate(list_flow_lat_lon):
    lat = float(lat_lon.split('_')[0])
    lon = float(lat_lon.split('_')[1])
    df_flow = pd.DataFrame(da_flow.loc[:,lat,lon].values, index=da_flow.coords['time'].values, columns=['flow'])
    # Select time range
    df_flow = my_functions.select_time_range(df_flow, start_datetime, end_datetime)
    # Convert units
    df_flow = df_flow * pow(1000.0/25.4/12, 3)  # convert m3/s to cfs
    # Create df and fill in variables
    df = pd.DataFrame(index=df_flow.index)
    df['day'] = range(1, len(df)+1)  # day number (1,2,3,...)
    df['cell'] = list_flow_cell_number[i]  # cell number
    df['Q_in'] = df_flow['flow'].values  # inflow discharge [cfs]
    df['Q_out'] = df_flow['flow'].values  # outflow discharge [cfs]
    df['Q_diff'] = 0.0  # lateral flow [cfs]
    df['depth'] = a_d * pow(df_flow['flow'].values, b_d)  # flow depth [ft]
    df['width'] = a_w * pow(df_flow['flow'].values, b_w)  # flow width [ft]
    df['u'] = df_flow['flow'].values / df['depth'].values / df['width']  # flow velocoty [ft/s]
    list_df.append(df)

#=== Rearrange data ===#
Пример #4
0
        # determine the common range of available data of both data sets
        data_avai_start_date, data_avai_end_date = my_functions.\
                        find_data_common_range([s_usgs, s_TVA])
        if (data_avai_start_date-data_avai_end_date).days>=0: # if no common time range
            print "No common range data available!"
            exit()
        # find the full water years with available data for both data sets
        plot_start_date, plot_end_date = my_functions.\
                find_full_water_years_within_a_range(data_avai_start_date, data_avai_end_date)
        # determine time locator #
        if plot_end_date.year-plot_start_date.year < 5:  # if less than 5 years
            time_locator = ('year', 1)  # time locator on the plot; 'year' for year; 'month' for month. e.g., ('month', 3) for plot one tick every 3 months
        else:  # if at least 5 years
            time_locator = ('year', (plot_end_date.year-plot_start_date.year)/5)  # time locator on the plot; 'year' for year; 'month' for month. e.g., ('month', 3) for plot one tick every 3 months
        # Select data to be plotted
        s_usgs_to_plot = my_functions.select_time_range(s_usgs, \
                                                        plot_start_date, plot_end_date)
        s_TVA_to_plot = my_functions.select_time_range(s_TVA, \
                                                        plot_start_date, plot_end_date)

        #-------------------------------------------
        df_to_plot = pd.DataFrame(s_TVA_to_plot.values, index=s_TVA_to_plot.index, columns=['tva'])
        df_to_plot['usgs'] = s_usgs_to_plot
        df_to_plot = df_to_plot.dropna()
        s_usgs_to_plot = df_to_plot['usgs']
        s_TVA_to_plot = df_to_plot['tva']
        #-------------------------------------------

        #=== Plot flow duration curves (daily data) ===#
        fig = my_functions.plot_duration_curve(\
            list_s_data=[s_TVA_to_plot, s_usgs_to_plot], \
            list_style=['k-', 'b-'], \
Пример #5
0
plot_start_date, plot_end_date = my_functions.find_full_water_years_within_a_range(
    data_avai_start_date, data_avai_end_date)
# determine time locator
if plot_end_date.year - plot_start_date.year < 5:  # if less than 5 years
    time_locator = (
        'year', 1
    )  # time locator on the plot; 'year' for year; 'month' for month. e.g., ('month', 3) for plot one tick every 3 months
else:  # if at least 5 years
    time_locator = (
        'year', (plot_end_date.year - plot_start_date.year) / 5
    )  # time locator on the plot; 'year' for year; 'month' for month. e.g., ('month', 3) for plot one tick every 3 months

#========================================================
# Select data to be plotted
#========================================================
s_rbm_to_plot = my_functions.select_time_range(s_rbm, plot_start_date,
                                               plot_end_date)
s_usgs_to_plot = my_functions.select_time_range(s_usgs, plot_start_date,
                                                plot_end_date)

#========================================================
# plot
#========================================================
#============== plot daily data ===============#
fig = plt.figure()
ax = plt.axes()
plt.plot_date(s_usgs_to_plot.index, s_usgs_to_plot, 'b-', label='USGS gauge')
plt.plot_date(s_rbm_to_plot.index, s_rbm_to_plot, 'r--', label='RBM')
plt.ylabel('Stream T (degC)', fontsize=16)
plt.title('%s, %s' % (usgs_site_name, usgs_site_code), fontsize=16)
plt.legend()
my_functions.plot_date_format(ax,
Пример #6
0
data_avai_start_date, data_avai_end_date = my_functions.find_data_common_range(s_rbm, s_usgs)
if (data_avai_start_date-data_avai_end_date).days>=0: # if no common time range
	print "No common range data available!"
	exit()
# find the full water years with available data for both data sets
plot_start_date, plot_end_date = my_functions.find_full_water_years_within_a_range(data_avai_start_date, data_avai_end_date)
# determine time locator
if plot_end_date.year-plot_start_date.year < 5:  # if less than 5 years
	time_locator = ('year', 1)  # time locator on the plot; 'year' for year; 'month' for month. e.g., ('month', 3) for plot one tick every 3 months
else:  # if at least 5 years
	time_locator = ('year', (plot_end_date.year-plot_start_date.year)/5)  # time locator on the plot; 'year' for year; 'month' for month. e.g., ('month', 3) for plot one tick every 3 months

#========================================================
# Select data to be plotted
#========================================================
s_rbm_to_plot = my_functions.select_time_range(s_rbm, plot_start_date, plot_end_date)
s_usgs_to_plot = my_functions.select_time_range(s_usgs, plot_start_date, plot_end_date)

#========================================================
# plot
#========================================================
#============== plot daily data ===============#
fig = plt.figure()
ax = plt.axes()
plt.plot_date(s_usgs_to_plot.index, s_usgs_to_plot, 'b-', label='USGS gauge')
plt.plot_date(s_rbm_to_plot.index, s_rbm_to_plot, 'r--', label='RBM')
plt.ylabel('Stream T (degC)', fontsize=16)
plt.title('%s, %s' %(usgs_site_name, usgs_site_code), fontsize=16)
plt.legend()
my_functions.plot_date_format(ax, time_range=(plot_start_date, plot_end_date), locator=time_locator, time_format='%Y/%m')
Пример #7
0
        exit()
f.close()

# Read in routed streamflow from inverted runoff
dict_Lohmann_routed = {}  # {station_name: pd.Series of daily data} [unit: cfs]
for stn in dict_path:
    # Load data
    s_Lohmann_routed = my_functions.read_Lohmann_route_daily_output(
        dict_path[stn][1])
    dict_Lohmann_routed[stn] = s_Lohmann_routed
    # Select full water years
    start_date_WY, end_date_WY = my_functions.find_full_water_years_within_a_range(\
                                                dict_Lohmann_routed[stn].index[0], \
                                                dict_Lohmann_routed[stn].index[-1])
    dict_Lohmann_routed[stn] = my_functions.select_time_range(dict_Lohmann_routed[stn], \
                                                              start_date_WY, \
                                                              end_date_WY)

# Read in original station obs rmat
dict_obs = {}  # {station_name: pd.Series of daily data} [unit: cfs]
for stn in dict_path:
    # Load data
    filename = dict_path[stn][0]
    if cfg['INPUT']['obs_format'] == 'USGS':
        column = dict_path[stn][2]
        dict_obs[stn] = my_functions.read_USGS_data(filename, [column],
                                                    ['Discharge'])
    elif cfg['INPUT']['obs_format'] == 'Lohmann':
        dict_obs[stn] = my_functions.read_Lohmann_route_daily_output(filename)

    # Select the same range as Lohmann routed flow
Пример #8
0
# Load data
#======================================================#
# Load data and select time range needed
dict_df_stn = {}  # a dictionary of station data
                  # {station_code: df}
for stn in list_stn:  # for each gauge station, load data
    # Load data
    filename = '{}/{}'.format(cfg['INPUT']['stn_data_dir'], stn)
    if cfg['INPUT']['data_formst']=='USGS':
        column = dict_stn_info[stn][2]
        dict_df_stn[stn] = my_functions.read_USGS_data(filename, [column], ['Discharge'])
    elif cfg['INPUT']['data_formst']=='Lohmann':
        dict_df_stn[stn] = my_functions.read_Lohmann_route_daily_output(filename)

    # Select time range needed
    dict_df_stn[stn] = my_functions.select_time_range(dict_df_stn[stn], \
                                                      start_date, end_date)
    # Convert data to cfs
    if cfg['PARAM']['input_flow_unit']=='cfs':
        pass

#======================================================#
# Write basin.stn.list and basin.stn.obs
#======================================================#
# Write basin.stn.list
f = open(cfg['OUTPUT']['basin_stn_list_path'], 'w')
for stn in list_stn:
    f.write('1 {} {} {} 1000 1000 1000\n'.format(stn, dict_stn_info[stn][0], \
                                                 dict_stn_info[stn][1]+360))
f.close()

# Write basin.stn.obs
Пример #9
0
# Load data and select time range needed
dict_df_stn = {}  # a dictionary of station data
# {station_code: df}
for stn in list_stn:  # for each gauge station, load data
    # Load data
    filename = '{}/{}'.format(cfg['INPUT']['stn_data_dir'], stn)
    if cfg['INPUT']['data_formst'] == 'USGS':
        column = dict_stn_info[stn][2]
        dict_df_stn[stn] = my_functions.read_USGS_data(filename, [column],
                                                       ['Discharge'])
    elif cfg['INPUT']['data_formst'] == 'Lohmann':
        dict_df_stn[stn] = my_functions.read_Lohmann_route_daily_output(
            filename)

    # Select time range needed
    dict_df_stn[stn] = my_functions.select_time_range(dict_df_stn[stn], \
                                                      start_date, end_date)
    # Convert data to cfs
    if cfg['PARAM']['input_flow_unit'] == 'cfs':
        pass

#======================================================#
# Write basin.stn.list and basin.stn.obs
#======================================================#
# Write basin.stn.list
f = open(cfg['OUTPUT']['basin_stn_list_path'], 'w')
for stn in list_stn:
    f.write('1 {} {} {} 1000 1000 1000\n'.format(stn, dict_stn_info[stn][0], \
                                                 dict_stn_info[stn][1]+360))
f.close()

# Write basin.stn.obs