Esempio n. 1
0
 def get(self, uuid, observation_id, **kwargs):
     try:
         self.metadata = aggregates.get_metadata(uuid)
     except DataRequestException as e:
         return render_template(self.template, errors=e.errors)
     self.set_template_args(observation_id, **kwargs)
     return render_template(self.template, **self.template_args)
    def set_template_args(self, site_id=None, aggregate_id=None):
        """Builds a dictionary of the appropriate template arguments.
        """
        self.template_args = {}
        # If an id was passed in, set the breadcrumb. The request for location
        # metadata may triggers an error if the object doesnt exist or the user
        # does not have access. So we can handle with a 404 message instead of
        # silently failing and listing all objects.
        if site_id is not None or aggregate_id is not None:
            if site_id is not None:
                location_metadata = sites.get_metadata(site_id)
            else:
                location_metadata = aggregates.get_metadata(aggregate_id)
            self.template_args['breadcrumb'] = self.breadcrumb_html(
                self.get_breadcrumb(location_metadata))

        else:
            self.template_args['page_title'] = 'Forecasts and Observations'

        self.template_args['subnav'] = self.format_subnav(
            **self.get_subnav_kwargs(site_id=site_id,
                                     aggregate_id=aggregate_id))
        table, _ = self.table_function(site_id, aggregate_id)
        self.template_args['data_table'] = table
        self.template_args['current_path'] = request.path
Esempio n. 3
0
 def set_site_or_aggregate_metadata(self):
     """Searches for a site_id or aggregate_id  in self.metadata
     and loads the expected metadata object from the api in either
     the 'site' or 'aggregate' key. If the object could not be retrieved,
     sets a warning and reraises the DataRequestError.
     """
     if self.metadata.get('site_id') is not None:
         try:
             self.metadata['site'] = sites.get_metadata(
                 self.metadata['site_id'])
         except DataRequestException:
             self.template_args.update({
                 'warnings': {
                     'Site Access':
                     ['Site inaccessible. Plots will not be displayed.']
                 },
             })
             raise
     elif self.metadata.get('aggregate_id'):
         try:
             self.metadata['aggregate'] = aggregates.get_metadata(
                 self.metadata['aggregate_id'])
         except DataRequestException:
             self.template_args.update({
                 'warnings': {
                     'Aggregate Access': [
                         'Aggregate inaccessible. Plots will not be '
                         'displayed.'
                     ]
                 },
             })
             raise
     else:
         self.template_args.update({
             'warnings': {
                 'Warning': ['Site or aggregate has been deleted.'],
             }
         })
         raise DataRequestException(404)
Esempio n. 4
0
    def _object_pair_template_attributes(self, pair):
        """Load metadata for objects included in forecast/obs pairs.

        Parameters
        ----------
        pair:
            Dict created from an object in the `object_pairs` field of the
            Solar Forecast Arbiter API report JSON response.

        Returns
        -------
        dict:
            Dict containing the following keys and values:
            * forecast: dict of forecast metadata or None
            * observation: dict of observation metadata or None
            * aggregate: dict of aggregate metadata or None
            * reference_forecast: dict of forecast metadata or None
            * uncertainty: dependent on value
              * float: float
              * 'observation_uncertainty': The value of the observation's
                  uncertainty field if available.
              * None: None
            * cost: cost value of the pair (str or None)
            * forecast_view: The name of the forecast view relative to the
                data_dashboard blueprint e.g. accessible via
                `data_dashboard.<forecast_view>`.
        """
        forecast_type = pair['forecast_type']

        if forecast_type == 'forecast' or forecast_type == 'event_forecast':
            forecast_get = forecasts.get_metadata
            forecast_view = 'forecast_view'

        elif forecast_type == 'probabilistic_forecast':
            forecast_get = cdf_forecast_groups.get_metadata
            forecast_view = 'cdf_forecast_group_view'

        else:
            forecast_get = cdf_forecasts.get_metadata
            forecast_view = 'cdf_forecast_view'

        try:
            forecast_metadata = forecast_get(pair['forecast'])
        except DataRequestException:
            forecast_metadata = None
        else:
            forecast_metadata['name'] = self.rename_forecast(forecast_metadata)

        if pair.get('reference_forecast') is not None:
            try:
                reference_metadata = forecast_get(pair['reference_forecast'])
            except DataRequestException:
                reference_metadata = None
            else:
                reference_metadata['name'] = self.rename_forecast(
                    reference_metadata)
        else:
            reference_metadata = None

        if pair.get('observation') is not None:
            try:
                observation_metadata = observations.get_metadata(
                    pair['observation'])
            except DataRequestException:
                observation_metadata = None
        else:
            observation_metadata = None

        if pair.get('aggregate') is not None:
            try:
                aggregate_metadata = aggregates.get_metadata(
                    pair['aggregate'])
            except DataRequestException:
                aggregate_metadata = None
        else:
            aggregate_metadata = None

        if pair['uncertainty'] == 'observation_uncertainty':
            if observation_metadata is not None:
                uncertainty = observation_metadata['uncertainty']
            else:
                uncertainty = None
        else:
            uncertainty = pair.get('uncertainty')
        return {
            'forecast': forecast_metadata,
            'observation': observation_metadata,
            'aggregate': aggregate_metadata,
            'reference_forecast': reference_metadata,
            'uncertainty': uncertainty,
            'cost': pair.get('cost'),
            'forecast_view': forecast_view,
        }