Пример #1
0
def short_range(request):
    """
    Controller for the Short Range page.
    """

    dateraw = []
    date1 = []
    value1 = []
    comid = '18228725'
    config = 'short_range'
    startdate = '2017-07-20'
    enddate = '2017-07-21'
    forecasttime = '12'
    watermlstring = str(get_nwm_forecast(config, comid, startdate, enddate, forecasttime))
    waterml = watermlstring.split('dateTimeUTC="')
    waterml.pop(0)
    for e in waterml:
        parser = e.split('"  methodCode="1"  sourceCode="1"  qualityControlLevelCode="1" >')
        dateraw.append(parser[0])
        value1.append(parser[1].split('<')[0])

    for e in dateraw:
        date1.append(dt.datetime.strptime(e, "%Y-%m-%dT%H:%M:%S"))

    nwm_plot = PlotlyView([go.Scatter(x=date1, y=value1)])

    context = {
        'nwm_plot': nwm_plot
    }

    return render(request, 'nwm_example/short_range.html', context)
def get_hydrographs(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:

        '''Plotting Data'''
        observed_Q = go.Scatter(x=observed_df.index, y=observed_df.iloc[:, 0].values, name='Observed', )
        simulated_Q = go.Scatter(x=simulated_df.index, y=simulated_df.iloc[:, 0].values, name='Simulated', )
        corrected_Q = go.Scatter(x=corrected_df.index, y=corrected_df.iloc[:, 0].values, name='Corrected Simulated', )

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

        chart_obj = PlotlyView(go.Figure(data=[observed_Q, simulated_Q, corrected_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 get_observed_data_temp(request):
    """
    Get observed data from csv files in Hydroshare
    """

    get_data = request.GET

    try:

        codEstacion = get_data['code']
        nomEstacion = get_data['name']

        url = 'https://www.hydroshare.org/resource/713d0a35bd2c48f8ba47c6fa76be2bac/data/contents/TEMP/{}.csv'.format(
            codEstacion)

        s = requests.get(url, verify=False).content

        df = pd.read_csv(io.StringIO(s.decode('utf-8')), index_col=0)
        df.index = pd.to_datetime(df.index)

        datesTEMP = df.index.tolist()
        dataTEMP = df.iloc[:, 0].values
        dataTEMP.tolist()

        if isinstance(dataTEMP[0], str):
            dataTEMP = map(float, dataTEMP)

        observed_TEMP = go.Scatter(
            x=datesTEMP,
            y=dataTEMP,
            name='Temperature',
        )

        layout = go.Layout(title='Temperature at {0}-{1}'.format(
            nomEstacion, codEstacion),
                           xaxis=dict(title='Dates', ),
                           yaxis=dict(title='Temperature (°C)',
                                      autorange=True),
                           showlegend=False)

        chart_obj = PlotlyView(go.Figure(data=[observed_TEMP], layout=layout))

        context = {
            'gizmo_object': chart_obj,
        }

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

    except Exception as e:
        print(str(e))
        return JsonResponse(
            {'error': 'No data found for the selected station.'})
Пример #4
0
def get_observed_data_bs(request):
    """
    Get observed data from csv files in Hydroshare
    """

    get_data = request.GET

    try:

        codEstacion = get_data['code']
        nomEstacion = get_data['name']

        url = 'https://www.hydroshare.org/resource/d7d98390ab884eeda89f4a10b072bbc3/data/contents/BS/{}.csv'.format(
            codEstacion)

        s = requests.get(url, verify=False).content

        df = pd.read_csv(io.StringIO(s.decode('utf-8')), index_col=0)
        df.index = pd.to_datetime(df.index)

        datesBS = df.index.tolist()
        dataBS = df.iloc[:, 0].values
        dataBS.tolist()

        if isinstance(dataBS[0], str):
            dataBS = map(float, dataBS)

        observed_BS = go.Scatter(
            x=datesBS,
            y=dataBS,
            name='Solar Bright',
        )

        layout = go.Layout(title='Solar Bright at {0}-{1}'.format(
            nomEstacion, codEstacion),
                           xaxis=dict(title='Dates', ),
                           yaxis=dict(title='Solar Bright (hours)',
                                      autorange=True),
                           showlegend=False)

        chart_obj = PlotlyView(go.Figure(data=[observed_BS], layout=layout))

        context = {
            'gizmo_object': chart_obj,
        }

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

    except Exception as e:
        print(str(e))
        return JsonResponse(
            {'error': 'No data found for the selected station.'})
Пример #5
0
def get_observed_data_prec(request):
    """
    Get observed data from csv files in Hydroshare
    """

    get_data = request.GET

    try:

        codEstacion = get_data['code']
        nomEstacion = get_data['name']

        url = 'https://www.hydroshare.org/resource/7e1747a802cd42418e7f277ec10f91be/data/contents/PREC/{}.csv'.format(
            codEstacion)
        # Here change id resource for csv
        s = requests.get(url, verify=False).content

        df = pd.read_csv(io.StringIO(s.decode('utf-8')), index_col=0)
        df.index = pd.to_datetime(df.index)

        datesPREC = df.index.tolist()
        dataPREC = df.iloc[:, 0].values
        dataPREC.tolist()

        if isinstance(dataPREC[0], str):
            dataPREC = map(float, dataPREC)

        observed_PREC = go.Scatter(
            x=datesPREC,
            y=dataPREC,
            name='Precipitation',
        )

        layout = go.Layout(title='Precipitation at {0}-{1}'.format(
            nomEstacion, codEstacion),
                           xaxis=dict(title='Dates', ),
                           yaxis=dict(title='Precipitation (mm)',
                                      autorange=True),
                           showlegend=False)

        chart_obj = PlotlyView(go.Figure(data=[observed_PREC], layout=layout))

        context = {
            'gizmo_object': chart_obj,
        }

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

    except Exception as e:
        print(str(e))
        return JsonResponse(
            {'error': 'No data found for the selected station.'})
Пример #6
0
def get_observed_data_hr(request):
    """
    Get observed data from csv files in Hydroshare
    """

    get_data = request.GET

    try:

        codEstacion = get_data['code']
        nomEstacion = get_data['name']

        url = 'https://www.hydroshare.org/resource/5ddb8470be904567af4f6b29434a5d51/data/contents/HR/{}.csv'.format(
            codEstacion)

        s = requests.get(url, verify=False).content

        df = pd.read_csv(io.StringIO(s.decode('utf-8')), index_col=0)
        df.index = pd.to_datetime(df.index)

        datesHR = df.index.tolist()
        dataHR = df.iloc[:, 0].values
        dataHR.tolist()

        if isinstance(dataHR[0], str):
            dataHR = map(float, dataBS)

        observed_HR = go.Scatter(
            x=datesHR,
            y=dataHR,
            name='Relative Humidity',
        )

        layout = go.Layout(title='Relative Humidity at {0}-{1}'.format(
            nomEstacion, codEstacion),
                           xaxis=dict(title='Dates', ),
                           yaxis=dict(title='Relative Humidity (%)',
                                      autorange=True),
                           showlegend=False)

        chart_obj = PlotlyView(go.Figure(data=[observed_HR], layout=layout))

        context = {
            'gizmo_object': chart_obj,
        }

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

    except Exception as e:
        print(str(e))
        return JsonResponse(
            {'error': 'No data found for the selected station.'})
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.'})
Пример #8
0
def details(request):

    siteid = request.GET.get('siteid')
    variables = request.GET.get('variables').split(',')

    vals = getValues(siteid, variables)

    series_collection = []

    for key, variable in vals.items():
        series_collection.append(go.Scatter(
            mode="markers",
            name=key,
            x=[o['date_val'] for o in variable],
            y=[o['value'] for o in variable]
        ))

    layout = go.Layout(
        title="Chemical Data<br><sub>{0} : {1}</sub>".format(
            siteid, request.GET.get('variables')),
        xaxis=dict(
            title='Date',
        ),
        yaxis=dict(
            title='Normalized Concentration to MCL'
        )
    )

    chart_obj = PlotlyView(
        go.Figure(data=series_collection,
                  layout=layout)
    )

    context = {
        'gizmo_object': chart_obj,
    }

    return render(request, 'gw_contaminants/gizmo_ajax.html', context)
Пример #9
0
def metrics(request):
    """
    Controller for the app metrics page.
    """
    regions_list = get_regions()
    num_regions = len(regions_list)
    variables_list = get_variable_list()
    num_variables = len(variables_list)
    aquifers_list = get_aquifers_list()
    num_aquifers = len(aquifers_list)
    num_wells = get_num_wells()
    num_measurements = get_num_measurements()
    metrics_plot = PlotlyView(get_metrics(), show_link=True)

    context = {
        'num_regions': num_regions,
        'num_variables': num_variables,
        'num_aquifers': num_aquifers,
        'num_wells': num_wells,
        'num_measurements': num_measurements,
        'metrics_plot': metrics_plot
    }

    return render(request, 'gwdm/metrics.html', context)
Пример #10
0
def get_time_series_bc(request):
    get_data = request.GET
    global comid
    global codEstacion
    global nomEstacion
    global corrected_df
    global forecast_df
    global fixed_stats
    global forecast_record
    global fixed_records

    try:

        hydroviewer_figure = geoglows.plots.forecast_stats(
            stats=fixed_stats,
            titles={
                'Station': nomEstacion + '-' + str(codEstacion),
                'Reach ID': comid,
                'bias_corrected': True
            })

        x_vals = (fixed_stats.index[0],
                  fixed_stats.index[len(fixed_stats.index) - 1],
                  fixed_stats.index[len(fixed_stats.index) - 1],
                  fixed_stats.index[0])
        max_visible = max(fixed_stats.max())
        '''Getting forecast record'''

        try:

            if len(fixed_records.index) > 0:
                hydroviewer_figure.add_trace(
                    go.Scatter(name='1st days forecasts',
                               x=fixed_records.index,
                               y=fixed_records.iloc[:, 0].values,
                               line=dict(color='#FFA15A', )))

            if 'x_vals' in locals():
                x_vals = (fixed_records.index[0],
                          fixed_stats.index[len(fixed_stats.index) - 1],
                          fixed_stats.index[len(fixed_stats.index) - 1],
                          fixed_records.index[0])
            else:
                x_vals = (fixed_records.index[0],
                          fixed_stats.index[len(fixed_stats.index) - 1],
                          fixed_stats.index[len(fixed_stats.index) - 1],
                          fixed_records.index[0])

            max_visible = max(fixed_records.max(), max_visible)

        except:
            print('There is no forecast record')
        '''Getting Corrected Return Periods'''
        max_annual_flow = corrected_df.groupby(
            corrected_df.index.strftime("%Y")).max()
        mean_value = np.mean(max_annual_flow.iloc[:, 0].values)
        std_value = np.std(max_annual_flow.iloc[:, 0].values)

        return_periods = [100, 50, 25, 10, 5, 2]

        def gumbel_1(std: float, xbar: float, rp: int or float) -> float:
            """
			Solves the Gumbel Type I probability distribution function (pdf) = exp(-exp(-b)) where b is the covariate. Provide
			the standard deviation and mean of the list of annual maximum flows. Compare scipy.stats.gumbel_r
			Args:
				std (float): the standard deviation of the series
				xbar (float): the mean of the series
				rp (int or float): the return period in years
			Returns:
				float, the flow corresponding to the return period specified
			"""
            # xbar = statistics.mean(year_max_flow_list)
            # std = statistics.stdev(year_max_flow_list, xbar=xbar)
            return -math.log(-math.log(1 -
                                       (1 / rp))) * std * .7797 + xbar - (.45 *
                                                                          std)

        return_periods_values = []

        for rp in return_periods:
            return_periods_values.append(gumbel_1(std_value, mean_value, rp))

        d = {
            'rivid': [comid],
            'return_period_100': [return_periods_values[0]],
            'return_period_50': [return_periods_values[1]],
            'return_period_25': [return_periods_values[2]],
            'return_period_10': [return_periods_values[3]],
            'return_period_5': [return_periods_values[4]],
            'return_period_2': [return_periods_values[5]]
        }
        rperiods = pd.DataFrame(data=d)
        rperiods.set_index('rivid', inplace=True)

        r2 = int(rperiods.iloc[0]['return_period_2'])

        colors = {
            '2 Year': 'rgba(254, 240, 1, .4)',
            '5 Year': 'rgba(253, 154, 1, .4)',
            '10 Year': 'rgba(255, 56, 5, .4)',
            '20 Year': 'rgba(128, 0, 246, .4)',
            '25 Year': 'rgba(255, 0, 0, .4)',
            '50 Year': 'rgba(128, 0, 106, .4)',
            '100 Year': 'rgba(128, 0, 246, .4)',
        }

        if max_visible > r2:
            visible = True
            hydroviewer_figure.for_each_trace(
                lambda trace: trace.update(visible=True)
                if trace.name == "Maximum & Minimum Flow" else (), )
        else:
            visible = 'legendonly'
            hydroviewer_figure.for_each_trace(
                lambda trace: trace.update(visible=True)
                if trace.name == "Maximum & Minimum Flow" else (), )

        def template(name, y, color, fill='toself'):
            return go.Scatter(name=name,
                              x=x_vals,
                              y=y,
                              legendgroup='returnperiods',
                              fill=fill,
                              visible=visible,
                              line=dict(color=color, width=0))

        r5 = int(rperiods.iloc[0]['return_period_5'])
        r10 = int(rperiods.iloc[0]['return_period_10'])
        r25 = int(rperiods.iloc[0]['return_period_25'])
        r50 = int(rperiods.iloc[0]['return_period_50'])
        r100 = int(rperiods.iloc[0]['return_period_100'])

        hydroviewer_figure.add_trace(
            template('Return Periods',
                     (r100 * 0.05, r100 * 0.05, r100 * 0.05, r100 * 0.05),
                     'rgba(0,0,0,0)',
                     fill='none'))
        hydroviewer_figure.add_trace(
            template(f'2 Year: {r2}', (r2, r2, r5, r5), colors['2 Year']))
        hydroviewer_figure.add_trace(
            template(f'5 Year: {r5}', (r5, r5, r10, r10), colors['5 Year']))
        hydroviewer_figure.add_trace(
            template(f'10 Year: {r10}', (r10, r10, r25, r25),
                     colors['10 Year']))
        hydroviewer_figure.add_trace(
            template(f'25 Year: {r25}', (r25, r25, r50, r50),
                     colors['25 Year']))
        hydroviewer_figure.add_trace(
            template(f'50 Year: {r50}', (r50, r50, r100, r100),
                     colors['50 Year']))
        hydroviewer_figure.add_trace(
            template(f'100 Year: {r100}',
                     (r100, r100, max(r100 + r100 * 0.05, max_visible),
                      max(r100 + r100 * 0.05, max_visible)),
                     colors['100 Year']))

        chart_obj = PlotlyView(hydroviewer_figure)

        context = {
            'gizmo_object': chart_obj,
        }

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

    except Exception as e:
        print(str(e))
        return JsonResponse({'error': 'No data found for the selected reach.'})
Пример #11
0
def get_time_series_plot(request):
    context = {'success': False}

    if request.method != 'POST':
        return HttpResponseNotAllowed(['POST'])

    try:
        log.debug(f'POST: {request.POST}')

        platform = request.POST.get('platform', None)
        sensor = request.POST.get('sensor', None)
        product = request.POST.get('product', None)
        start_date = request.POST.get('start_date', None)
        end_date = request.POST.get('end_date', None)
        reducer = request.POST.get('reducer', None)
        index_name = request.POST.get('index_name', None)
        scale = float(request.POST.get('scale', 250))
        geometry_str = request.POST.get('geometry', None)

        # Derived parameters
        ee_product = EE_PRODUCTS[platform][sensor][product]
        display_name = ee_product['display']

        if not index_name:
            index_name = ee_product['index']

        try:
            geometry = geojson.loads(geometry_str)
        except JSONDecodeError:
            raise ValueError('Please draw an area of interest.')

        if index_name is None:
            raise ValueError(
                f"We're sorry, but plotting {display_name} is not supported at this time. Please select "
                f"a different product.")

        time_series = get_time_series_from_image_collection(
            platform=platform,
            sensor=sensor,
            product=product,
            index_name=index_name,
            scale=scale,
            geometry=geometry,
            date_from=start_date,
            date_to=end_date,
            reducer=reducer)

        log.debug(f'Time Series: {time_series}')

        figure = generate_figure(figure_title=display_name,
                                 time_series=time_series)

        plot_view = PlotlyView(figure, height='200px', width='100%')

        context.update({'success': True, 'plot_view': plot_view})

    except ValueError as e:
        context['error'] = str(e)

    except Exception:
        context[
            'error'] = f'An unexpected error has occurred. Please try again.'
        log.exception('An unexpected error occurred.')

    return render(request, 'earth_engine/plot.html', context)
Пример #12
0
def get_dailyData(request):
    """
    Get data for stations
    """

    get_data = request.GET

    try:
        codEstacion = get_data['stationcode']
        nomEstacion = get_data['stationname']
        nomCountry = get_data['countryname']
        nomRiver = get_data['stream']

        dir_base = os.path.dirname(__file__)
        url = os.path.join(dir_base, 'public/Data', codEstacion + '-DAILY.csv')
        print(url)
        '''
        with open(url) as csvfile:
            readCSV = csv.reader(csvfile, delimiter=',')
            readCSV.next()
            datesDischarge = []
            dataDischarge = []
            for row in readCSV:
                da = row[0]
                year = int(da[0:4])
                month = int(da[5:7])
                day = int(da[8:10])
                dat = row[1]
                dat = float(dat)
                if dat < 0:
                    dat = np.nan
                dat = str(dat)
                datesDischarge.append(dt.datetime(year, month, day))
                dataDischarge.append(dat)
        '''

        df = pd.read_csv(url, index_col=0)
        df.index = pd.to_datetime(df.index)
        df = df[df.iloc[:, 0] >= 0]

        datesDischarge = df.index.tolist()
        dataDischarge = df.iloc[:, 0].values

        observed_Q = go.Scatter(
            x=datesDischarge,
            y=dataDischarge,
            name='Observed Discharge',
        )

        layout = go.Layout(
            title='Daily Streamflow at {0} - {1} <br> {2} RIVER <br> {3}'.
            format(nomEstacion, codEstacion, nomRiver, nomCountry),
            xaxis=dict(title='Dates', ),
            yaxis=dict(title='Discharge (m<sup>3</sup>/s)', autorange=True),
            showlegend=False)

        chart_obj = PlotlyView(go.Figure(data=[observed_Q], layout=layout))

        context = {
            'gizmo_object': chart_obj,
        }

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

    except Exception as e:
        print(str(e))
        return JsonResponse(
            {'error': 'No observed data found for the selected station.'})
Пример #13
0
def visualize_dataset_workflow(request):
    '''
    This controler is for visualizing
    time series data in a plot
    '''
    dataset = request.GET['dataset']
    # datatype = request.GET['dataset_datatype']

    datatype = quest.api.get_metadata(dataset)[dataset]['datatype']

    # load data
    if not datatype:
        raise ValueError('Cannot visualize a dataset without a datytype')

    if datatype == 'timeseries':
        df = quest.api.open_dataset(dataset, fmt='dataframe')
        parameter = df.metadata['parameter']
        units = df.metadata.get('unit')
        data_col = parameter if parameter in df.columns else df.columns[0]  # TODO fix this in quest

        x = df.index
        if hasattr(x, 'to_timestamp'):
            x = x.to_timestamp()

        # create plotly plot
        scatter_series = go.Scatter(
            x=x,
            y=df[data_col],
            name=dataset,
            fill='tozeroy'
        )
        plotly_layout = go.Layout(
            showlegend=True,
            height=350,
            margin=go.Margin(
                l=50,
                r=0,
                b=30,
                t=0,
                pad=4
            ),
            legend=dict(
                orientation='h',
            ),
            yaxis=dict(
                title="{0}{1}".format(data_col, " ({0})".format(units) if units else ''),
            ),
        )
        # create plotly gizmo
        plot_view_options = PlotlyView(go.Figure(data=[scatter_series],
                                                 layout=plotly_layout),
                                       height='100%',
                                       attributes={'id': 'plot-content',
                                                   'data-dataset_id': dataset},
                                       )

        context = {'plot_view_options': plot_view_options, }

        html = render(request, 'quest/visualize.html', context).content.decode('utf-8')

        result = {'success': True,
                  'html': html,

                  }

    else:
        try:
            metadata = quest.api.get_metadata(dataset)[dataset]
            file_path = metadata['file_path']
            result = {'success': True}
        except Exception as e:

            result['error'] = e

        from django.utils.encoding import smart_str
        import rasterio
        import rasterio.warp

        with rasterio.open(file_path) as src:

            if src.crs.to_string() != '+init=epsg:3857':
                file_extents = rasterio.warp.transform_bounds(src.crs.to_string(), 'EPSG:3857', *src.bounds)
            else:
                file_extents = src.bounds

        result['file_extents'] = file_extents

    result['datatype'] = datatype

    return JsonResponse(result)
Пример #14
0
def get_volumeAnalysis(request):
    """
	Get observed data from csv files in Hydroshare
	Get historic simulations from ERA Interim
	"""
    get_data = request.GET
    global codEstacion
    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'''

        sim_array = merged_df.iloc[:, 0].values
        obs_array = merged_df.iloc[:, 1].values
        corr_array = merged_df2.iloc[:, 0].values

        sim_volume_dt = sim_array * 0.0864
        obs_volume_dt = obs_array * 0.0864
        corr_volume_dt = corr_array * 0.0864

        sim_volume_cum = []
        obs_volume_cum = []
        corr_volume_cum = []
        sum_sim = 0
        sum_obs = 0
        sum_corr = 0

        for i in sim_volume_dt:
            sum_sim = sum_sim + i
            sim_volume_cum.append(sum_sim)

        for j in obs_volume_dt:
            sum_obs = sum_obs + j
            obs_volume_cum.append(sum_obs)

        for k in corr_volume_dt:
            sum_corr = sum_corr + k
            corr_volume_cum.append(sum_corr)

        observed_volume = go.Scatter(
            x=merged_df.index,
            y=obs_volume_cum,
            name='Observed',
        )

        simulated_volume = go.Scatter(
            x=merged_df.index,
            y=sim_volume_cum,
            name='Simulated',
        )

        corrected_volume = go.Scatter(
            x=merged_df2.index,
            y=corr_volume_cum,
            name='Corrected Simulated',
        )

        layout = go.Layout(
            title='Observed & Simulated Volume at<br> {0} - {1}'.format(
                codEstacion, nomEstacion),
            xaxis=dict(title='Dates', ),
            yaxis=dict(title='Volume (Mm<sup>3</sup>)', autorange=True),
            showlegend=True)

        chart_obj = PlotlyView(
            go.Figure(
                data=[observed_volume, simulated_volume, corrected_volume],
                layout=layout))

        context = {
            'gizmo_object': chart_obj,
        }

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

    except Exception as e:
        print(str(e))
        return JsonResponse(
            {'error': 'No data found for the selected station.'})
Пример #15
0
def get_hydrographs(request):
    """
	Get observed data from csv files in Hydroshare
	Get historic simulations from ERA Interim
	"""
    get_data = request.GET
    global codEstacion
    global nomEstacion
    global simulated_df
    global observed_df
    global corrected_df
    global comid
    start_time = time.time()

    try:
        # get Simulated Streamflow
        simulated_df = geoglows.streamflow.historic_simulation(
            comid, forcing='era_5', return_format='csv')
        # Removing Negative Values
        simulated_df[simulated_df < 0] = 0
        simulated_df.index = simulated_df.index.to_series().dt.strftime(
            "%Y-%m-%d")
        simulated_df.index = pd.to_datetime(simulated_df.index)
        simulated_df = pd.DataFrame(data=simulated_df.iloc[:, 0].values,
                                    index=simulated_df.index,
                                    columns=['Simulated Streamflow'])
        '''Get Observed Data'''

        url = 'http://128.187.106.131/app/index.php/dr/services/cuahsi_1_1.asmx/GetValuesObject?location={0}&variable=Q&startDate=1900-01-01&endDate=2019-12-31&version=1.1'.format(
            codEstacion)

        r = requests.get(url, verify=False)
        c = xmltodict.parse(r.content)

        y = []
        x = []

        for i in c['timeSeriesResponse']['timeSeries']['values']['value']:
            y.append(float((i['#text'])))
            x.append(
                dt.datetime.strptime((i['@dateTime']), "%Y-%m-%dT%H:%M:%S"))

        df = pd.DataFrame(data=y, index=x, columns=['Streamflow'])
        df.head()

        datesDischarge = df.index.tolist()
        dataDischarge = df.iloc[:, 0].values

        if isinstance(dataDischarge[0], str):
            dataDischarge = map(float, dataDischarge)

        observed_df = pd.DataFrame(data=dataDischarge,
                                   index=datesDischarge,
                                   columns=['Observed Streamflow'])
        '''Correct the Bias in Sumulation'''

        corrected_df = geoglows.bias.correct_historical(
            simulated_df, observed_df)
        '''Plotting Data'''
        observed_Q = go.Scatter(
            x=observed_df.index,
            y=observed_df.iloc[:, 0].values,
            name='Observed',
        )
        simulated_Q = go.Scatter(
            x=simulated_df.index,
            y=simulated_df.iloc[:, 0].values,
            name='Simulated',
        )
        corrected_Q = go.Scatter(
            x=corrected_df.index,
            y=corrected_df.iloc[:, 0].values,
            name='Corrected Simulated',
        )

        layout = go.Layout(
            title='Observed & Simulated Streamflow at <br> {0} - {1}'.format(
                codEstacion, nomEstacion),
            xaxis=dict(title='Dates', ),
            yaxis=dict(title='Discharge (m<sup>3</sup>/s)', autorange=True),
            showlegend=True)

        chart_obj = PlotlyView(
            go.Figure(data=[observed_Q, simulated_Q, corrected_Q],
                      layout=layout))

        context = {
            'gizmo_object': chart_obj,
        }
        print("--- %s seconds hydrographs ---" % (time.time() - start_time))

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

    except Exception as e:
        print(str(e))
        return JsonResponse(
            {'error': 'No data found for the selected station.'})
Пример #16
0
def get_scatterPlotLogScale(request):
    """
	Get observed data from csv files in Hydroshare
	Get historic simulations from ERA Interim
	"""
    get_data = request.GET
    global codEstacion
    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'''

        scatter_data = go.Scatter(x=merged_df.iloc[:, 0].values,
                                  y=merged_df.iloc[:, 1].values,
                                  mode='markers',
                                  name='original',
                                  marker=dict(color='#ef553b'))

        scatter_data2 = go.Scatter(x=merged_df2.iloc[:, 0].values,
                                   y=merged_df2.iloc[:, 1].values,
                                   mode='markers',
                                   name='corrected',
                                   marker=dict(color='#00cc96'))

        min_value = min(min(merged_df.iloc[:, 1].values),
                        min(merged_df.iloc[:, 0].values))
        max_value = max(max(merged_df.iloc[:, 1].values),
                        max(merged_df.iloc[:, 0].values))

        line_45 = go.Scatter(x=[min_value, max_value],
                             y=[min_value, max_value],
                             mode='lines',
                             name='45deg line',
                             line=dict(color='black'))

        layout = go.Layout(
            title="Scatter Plot for {0} - {1} (Log Scale)".format(
                codEstacion, nomEstacion),
            xaxis=dict(
                title='Simulated',
                type='log',
            ),
            yaxis=dict(title='Observed', type='log', autorange=True),
            showlegend=True)

        chart_obj = PlotlyView(
            go.Figure(data=[scatter_data, scatter_data2, line_45],
                      layout=layout))

        context = {
            'gizmo_object': chart_obj,
        }

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

    except Exception as e:
        print(str(e))
        return JsonResponse(
            {'error': 'No data found for the selected station.'})
Пример #17
0
def get_scatterPlot(request):
    """
	Get observed data from csv files in Hydroshare
	Get historic simulations from ERA Interim
	"""
    get_data = request.GET
    global codEstacion
    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'''

        scatter_data = go.Scatter(x=merged_df.iloc[:, 0].values,
                                  y=merged_df.iloc[:, 1].values,
                                  mode='markers',
                                  name='original',
                                  marker=dict(color='#ef553b'))

        scatter_data2 = go.Scatter(x=merged_df2.iloc[:, 0].values,
                                   y=merged_df2.iloc[:, 1].values,
                                   mode='markers',
                                   name='corrected',
                                   marker=dict(color='#00cc96'))

        min_value = min(min(merged_df.iloc[:, 1].values),
                        min(merged_df.iloc[:, 0].values))
        max_value = max(max(merged_df.iloc[:, 1].values),
                        max(merged_df.iloc[:, 0].values))

        line_45 = go.Scatter(x=[min_value, max_value],
                             y=[min_value, max_value],
                             mode='lines',
                             name='45deg line',
                             line=dict(color='black'))

        slope, intercept, r_value, p_value, std_err = sp.linregress(
            merged_df.iloc[:, 0].values, merged_df.iloc[:, 1].values)

        slope2, intercept2, r_value2, p_value2, std_err2 = sp.linregress(
            merged_df2.iloc[:, 0].values, merged_df2.iloc[:, 1].values)

        line_adjusted = go.Scatter(
            x=[min_value, max_value],
            y=[slope * min_value + intercept, slope * max_value + intercept],
            mode='lines',
            name='{0}x + {1} (Original)'.format(str(round(slope, 2)),
                                                str(round(intercept, 2))),
            line=dict(color='red'))

        line_adjusted2 = go.Scatter(x=[min_value, max_value],
                                    y=[
                                        slope2 * min_value + intercept2,
                                        slope2 * max_value + intercept2
                                    ],
                                    mode='lines',
                                    name='{0}x + {1} (Corrected)'.format(
                                        str(round(slope2, 2)),
                                        str(round(intercept2, 2))),
                                    line=dict(color='green'))

        layout = go.Layout(
            title='Scatter Plot for  {0}-{1} <br> COMID: {2}'.format(
                watershed, subbasin, comid),
            xaxis=dict(title='Simulated', ),
            yaxis=dict(title='Observed', autorange=True),
            showlegend=True)

        chart_obj = PlotlyView(
            go.Figure(data=[
                scatter_data, scatter_data2, line_45, line_adjusted,
                line_adjusted2
            ],
                      layout=layout))

        context = {
            'gizmo_object': chart_obj,
        }

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

    except Exception as e:
        print(str(e))
        return JsonResponse(
            {'error': 'No data found for the selected station.'})
Пример #18
0
def get_time_series(request):
    get_data = request.GET
    global comid
    global codEstacion
    global nomEstacion
    global forecast_df
    global forecast_record

    try:

        hydroviewer_figure = geoglows.plots.forecast_stats(
            stats=forecast_df, titles={'Reach ID': comid})

        x_vals = (forecast_df.index[0],
                  forecast_df.index[len(forecast_df.index) - 1],
                  forecast_df.index[len(forecast_df.index) - 1],
                  forecast_df.index[0])
        max_visible = max(forecast_df.max())
        '''Getting forecast record'''

        try:
            if len(forecast_record.index) > 0:
                hydroviewer_figure.add_trace(
                    go.Scatter(name='1st days forecasts',
                               x=forecast_record.index,
                               y=forecast_record.iloc[:, 0].values,
                               line=dict(color='#FFA15A', )))

            if 'x_vals' in locals():
                x_vals = (forecast_record.index[0],
                          forecast_df.index[len(forecast_df.index) - 1],
                          forecast_df.index[len(forecast_df.index) - 1],
                          forecast_record.index[0])
            else:
                x_vals = (forecast_record.index[0],
                          forecast_df.index[len(forecast_df.index) - 1],
                          forecast_df.index[len(forecast_df.index) - 1],
                          forecast_record.index[0])
                max_visible = max(forecast_record.max(), max_visible)

        except:
            print('Not forecast record for the selected station')
        '''Getting Return Periods'''
        try:
            rperiods = geoglows.streamflow.return_periods(comid)

            r2 = int(rperiods.iloc[0]['return_period_2'])

            colors = {
                '2 Year': 'rgba(254, 240, 1, .4)',
                '5 Year': 'rgba(253, 154, 1, .4)',
                '10 Year': 'rgba(255, 56, 5, .4)',
                '20 Year': 'rgba(128, 0, 246, .4)',
                '25 Year': 'rgba(255, 0, 0, .4)',
                '50 Year': 'rgba(128, 0, 106, .4)',
                '100 Year': 'rgba(128, 0, 246, .4)',
            }

            if max_visible > r2:
                visible = True
                hydroviewer_figure.for_each_trace(
                    lambda trace: trace.update(visible=True)
                    if trace.name == "Maximum & Minimum Flow" else (), )
            else:
                visible = 'legendonly'
                hydroviewer_figure.for_each_trace(
                    lambda trace: trace.update(visible=True)
                    if trace.name == "Maximum & Minimum Flow" else (), )

            def template(name, y, color, fill='toself'):
                return go.Scatter(name=name,
                                  x=x_vals,
                                  y=y,
                                  legendgroup='returnperiods',
                                  fill=fill,
                                  visible=visible,
                                  line=dict(color=color, width=0))

            r5 = int(rperiods.iloc[0]['return_period_5'])
            r10 = int(rperiods.iloc[0]['return_period_10'])
            r25 = int(rperiods.iloc[0]['return_period_25'])
            r50 = int(rperiods.iloc[0]['return_period_50'])
            r100 = int(rperiods.iloc[0]['return_period_100'])

            hydroviewer_figure.add_trace(
                template('Return Periods',
                         (r100 * 0.05, r100 * 0.05, r100 * 0.05, r100 * 0.05),
                         'rgba(0,0,0,0)',
                         fill='none'))
            hydroviewer_figure.add_trace(
                template(f'2 Year: {r2}', (r2, r2, r5, r5), colors['2 Year']))
            hydroviewer_figure.add_trace(
                template(f'5 Year: {r5}', (r5, r5, r10, r10),
                         colors['5 Year']))
            hydroviewer_figure.add_trace(
                template(f'10 Year: {r10}', (r10, r10, r25, r25),
                         colors['10 Year']))
            hydroviewer_figure.add_trace(
                template(f'25 Year: {r25}', (r25, r25, r50, r50),
                         colors['25 Year']))
            hydroviewer_figure.add_trace(
                template(f'50 Year: {r50}', (r50, r50, r100, r100),
                         colors['50 Year']))
            hydroviewer_figure.add_trace(
                template(f'100 Year: {r100}',
                         (r100, r100, max(r100 + r100 * 0.05, max_visible),
                          max(r100 + r100 * 0.05, max_visible)),
                         colors['100 Year']))

        except:
            print('There is no return periods for the desired stream')

        chart_obj = PlotlyView(hydroviewer_figure)

        context = {
            'gizmo_object': chart_obj,
        }

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

    except Exception as e:
        print(str(e))
        return JsonResponse({'error': 'No data found for the selected reach.'})
Пример #19
0
def mapview(request):

    select_input = SelectInput(display_text='Select',
                               name='select1',
                               multiple=False,
                               original=True,
                               options=[('Davis County', '1'),
                                        ('Salt Lake County', '2'),
                                        ('Utah County', '3')],
                               initial=['Utah County'])

    x = [
        datetime(year=2017, month=01, day=01),
        datetime(year=2017, month=02, day=01),
        datetime(year=2017, month=03, day=01),
        datetime(year=2017, month=04, day=01),
        datetime(year=2017, month=05, day=01),
        datetime(year=2017, month=06, day=01),
        datetime(year=2017, month=07, day=01),
    ]

    timeseries_plot = TimeSeries(height='800px',
                                 width='500px',
                                 engine='highcharts',
                                 title='Irregular Timeseries Plot',
                                 y_axis_title='Snow depth',
                                 y_axis_units='m',
                                 series=[{
                                     'name':
                                     'Winter 2007-2008',
                                     'data': [[datetime(2017, 12, 2), 0.8],
                                              [datetime(2017, 12, 9), 0.6],
                                              [datetime(2017, 12, 16), 0.6],
                                              [datetime(2017, 12, 28), 0.67],
                                              [datetime(2017, 1, 1), 0.81],
                                              [datetime(2017, 1, 8), 0.78],
                                              [datetime(2017, 1, 12), 0.98],
                                              [datetime(2017, 1, 27), 1.84],
                                              [datetime(2017, 2, 10), 1.80],
                                              [datetime(2017, 2, 18), 1.80],
                                              [datetime(2017, 2, 24), 1.92],
                                              [datetime(2017, 3, 4), 2.49],
                                              [datetime(2017, 3, 11), 2.79],
                                              [datetime(2017, 3, 15), 2.73],
                                              [datetime(2017, 3, 25), 2.61],
                                              [datetime(2017, 4, 2), 2.76],
                                              [datetime(2017, 4, 6), 2.82],
                                              [datetime(2017, 4, 13), 2.8],
                                              [datetime(2017, 5, 3), 2.1],
                                              [datetime(2017, 5, 26), 1.1],
                                              [datetime(2017, 6, 9), 0.25],
                                              [datetime(2017, 6, 12), 0]]
                                 }])

    my_plotly_view = PlotlyView([go.Scatter(x=x, y=[12, 14, 15, 10, 6, 5, 4])])

    context = {
        'select_input': select_input,
        'plotly_view_input': my_plotly_view,
        'timeseries_plot': timeseries_plot
    }

    return render(request, 'mysnowpackapp/mapview.html', context)