예제 #1
0
def corrected_month_average(corrected: pd.DataFrame, simulated: pd.DataFrame, observed: pd.DataFrame,
                            merged_sim_obs: pd.DataFrame = False, merged_cor_obs: pd.DataFrame = False,
                            titles: dict = None, outformat: str = 'plotly') -> go.Figure or str:
    """
    Calculates and plots the monthly average streamflow. This function uses
    hydrostats.data.merge_data on the 3 inputs. If you have already computed these because you are doing a full
    comparison of bias correction, you can provide them to save time

    Args:
        corrected: the response from the geoglows.bias.correct_historical_simulation function
        simulated: the csv response from historic_simulation
        observed: the dataframe of observed data. Must have a datetime index and a single column of flow values
        merged_sim_obs: (optional) if you have already computed it, hydrostats.data.merge_data(simulated, observed)
        merged_cor_obs: (optional) if you have already computed it, hydrostats.data.merge_data(corrected, observed)
        outformat: either 'plotly' or 'plotly_html' (default plotly)
        titles: (dict) Extra info to show on the title of the plot. For example:
            {'Reach ID': 1234567, 'Drainage Area': '1000km^2'}

    Returns:
         plotly.GraphObject: plotly object, especially for use with python notebooks and the .show() method
    """
    if corrected is False and simulated is False and observed is False:
        if merged_sim_obs is not False and merged_cor_obs is not False:
            pass  # if you provided the merged dataframes already, we use those
    else:
        # merge the datasets together
        merged_sim_obs = hd.merge_data(sim_df=simulated, obs_df=observed)
        merged_cor_obs = hd.merge_data(sim_df=corrected, obs_df=observed)
    monthly_avg = hd.monthly_average(merged_sim_obs)
    monthly_avg2 = hd.monthly_average(merged_cor_obs)

    scatters = [
        go.Scatter(x=monthly_avg.index, y=monthly_avg.iloc[:, 1].values, name='Observed Data'),
        go.Scatter(x=monthly_avg.index, y=monthly_avg.iloc[:, 0].values, name='Simulated Data'),
        go.Scatter(x=monthly_avg2.index, y=monthly_avg2.iloc[:, 0].values, name='Corrected Simulated Data'),
    ]

    layout = go.Layout(
        title=_build_title('Monthly Average Streamflow Comparison', titles),
        xaxis=dict(title='Month'), yaxis=dict(title='Discharge (m<sup>3</sup>/s)', autorange=True),
        showlegend=True)

    if outformat == 'plotly':
        return go.Figure(data=scatters, layout=layout)
    elif outformat == 'plotly_html':
        return offline_plot(
            go.Figure(data=scatters, layout=layout),
            config={'autosizable': True, 'responsive': True},
            output_type='div',
            include_plotlyjs=False
        )
    raise ValueError('Invalid outformat chosen. Choose plotly or plotly_html')
def get_monthlyAverages(request):
    """
    Get observed data from csv files in Hydroshare
    Get historic simulations from ERA Interim
    """
    get_data = request.GET
    global nomRiver
    global nomEstacion
    global simulated_df
    global observed_df
    global corrected_df

    try:

        '''Merge Data'''

        merged_df = hd.merge_data(sim_df=simulated_df, obs_df=observed_df)

        merged_df2 = hd.merge_data(sim_df=corrected_df, obs_df=observed_df)

        '''Plotting Data'''

        monthly_avg = hd.monthly_average(merged_df)

        monthly_avg2 = hd.monthly_average(merged_df2)

        monthly_avg_obs_Q = go.Scatter(x=monthly_avg.index, y=monthly_avg.iloc[:, 1].values, name='Observed', )

        monthly_avg_sim_Q = go.Scatter(x=monthly_avg.index, y=monthly_avg.iloc[:, 0].values, name='Simulated', )

        monthly_avg_corr_sim_Q = go.Scatter(x=monthly_avg2.index, y=monthly_avg2.iloc[:, 0].values,
                                            name='Corrected Simulated', )

        layout = go.Layout(
            title='Monthly Average Streamflow for River {0} at {1}'.format(nomRiver, nomEstacion),
            xaxis=dict(title='Months', ), yaxis=dict(title='Discharge (m<sup>3</sup>/s)', autorange=True),
            showlegend=True)

        chart_obj = PlotlyView(
            go.Figure(data=[monthly_avg_obs_Q, monthly_avg_sim_Q, monthly_avg_corr_sim_Q], layout=layout))

        context = {
            'gizmo_object': chart_obj,
        }

        return render(request, 'historical_validation_tool_somalia/gizmo_ajax.html', context)

    except Exception as e:
        print(str(e))
        return JsonResponse({'error': 'No data found for the selected station.'})
예제 #3
0
    def test_monthly_average(self):
        original_df = pd.read_csv(r'Comparison_Files/monthly_average.csv', index_col=0)
        original_df.index = np.array(['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'],
                                     dtype=np.object)

        test_df = hd.monthly_average(self.merged_df)

        self.assertIsNone(pd.testing.assert_frame_equal(original_df, test_df))
예제 #4
0
def historic_averages_handler(request, average_type):
    """
    Controller for retrieving averages
    """
    # handle the parameters from the user
    try:
        reach_id, region, units, return_format = handle_parameters(request)
        units_title, units_title_long = get_units_title(units)
        forcing = request.args.get('forcing', 'era_5')
        hist_df = get_historical_dataframe(reach_id, region, units, forcing)
    except Exception as e:
        raise e

    hist_df.index = pd.to_datetime(hist_df.index)
    if average_type == 'daily':
        hist_df = hd.daily_average(hist_df, rolling=True)
    else:
        hist_df = hd.monthly_average(hist_df)
    hist_df.index.name = 'datetime'

    if return_format == 'csv':
        response = make_response(hist_df.to_csv())
        response.headers['content-type'] = 'text/csv'
        response.headers[
            'Content-Disposition'] = f'attachment; filename=seasonal_average_{forcing}_{reach_id}.csv'
        return response

    if return_format == "json":
        return jsonify({
            'region':
            region,
            'simulation_forcing':
            forcing,
            'forcing_fullname':
            forcing.replace('era_', 'ERA ').title(),
            'comid':
            reach_id,
            'gendate':
            datetime.datetime.utcnow().isoformat() + 'Z',
            'time_series': {
                'datetime': hist_df.index.tolist(),
                'flow': hist_df[f'streamflow_{units_title}^3/s'].tolist(),
            },
            'units': {
                'name': 'Streamflow',
                'short': f'{units_title}3/s',
                'long': f'Cubic {units_title_long} per Second'
            }
        })

    else:
        raise ValueError(f'Invalid return_format: {return_format}')
예제 #5
0
                  '{0}_{1}_daily_average.png'.format(str(id), name)))

    hv.plot(merged_data_df=daily_avg,
            legend=('Simulated', 'Observed'),
            grid=True,
            x_season=True,
            title='Daily Average Streamflow for ' + str(id) + ' - ' + name +
            '\n River: ' + rio + '. COMID: ' + str(comid),
            labels=['Datetime', 'Streamflow (m$^3$/s)'],
            linestyles=['b-', 'r-'],
            fig_size=(15, 9))
    plt.savefig(
        path.join(daily_average_out_dir,
                  '{0}_{1}_daily_average_1.png'.format(str(id), name)))

    monthly_avg = hd.monthly_average(merged_df)
    monthly_std_error = hd.monthly_std_error(merged_data=merged_df)
    hv.plot(merged_data_df=monthly_avg,
            legend=('Simulated', 'Observed'),
            grid=True,
            x_season=True,
            title='Monthly Average Streamflow (Standard Error) for ' +
            str(id) + ' - ' + name + '\n River: ' + rio + '. COMID: ' +
            str(comid),
            labels=['Datetime', 'Streamflow (m$^3$/s)'],
            linestyles=['b-', 'r-'],
            fig_size=(15, 9),
            ebars=monthly_std_error,
            ecolor=('b', 'r'),
            tight_xlim=False)
    plt.savefig(