def add_summary_fields_to_context(self, model_run, context, user):
        """
        Add summary information to a template context, containing basic information
        for each of the pages in the workflow.
        :param model_run: Model run being created
        :param context: Context to summary fields to
        :param user: user
        :return:
        """
        # Create page
        context.model_run = model_run
        context.science_config = self._model_run_service.get_science_configuration_by_id(
            model_run.science_configuration_id)

        # Driving data page
        driving_data = model_run.driving_dataset
        context.driving_data_name = model_run.driving_dataset.name

        # Extents page
        extents_controller_helper = ExtentsControllerHelper()
        context.extents_values = extents_controller_helper.create_values_dict_from_database(model_run, driving_data)

        # Land cover page
        land_cover_service = LandCoverService()
        context.land_cover_actions = land_cover_service.get_land_cover_actions_for_model(model_run)
        land_cover_helper = LandCoverControllerHelper()
        try:
            land_cover_helper.add_fractional_land_cover_to_context(context, {}, model_run, user)
        except ServiceException:
            pass

        # Outputs page
        output_variables = self._model_run_service.get_output_variables()
        output_variable_dict = dict((x.name, x.description) for x in output_variables)
        selected_vars = model_run.get_parameter_values(constants.JULES_PARAM_OUTPUT_VAR)
        selected_output_periods = model_run.get_parameter_values(constants.JULES_PARAM_OUTPUT_PERIOD)
        outputs = {}
        # Each group contains one output variable and one output period
        for selected_var in selected_vars:
            var_name = selected_var.get_value_as_python()
            if var_name not in outputs:
                outputs[var_name] = []
            for output_period in selected_output_periods:
                if output_period.group_id == selected_var.group_id:
                    period = output_period.get_value_as_python()
                    if period == constants.JULES_YEARLY_PERIOD:
                        outputs[var_name].append('Yearly')
                    elif period == constants.JULES_MONTHLY_PERIOD:
                        outputs[var_name].append('Monthly')
                    elif period == constants.JULES_DAILY_PERIOD:
                        outputs[var_name].append('Daily')
                    else:
                        outputs[var_name].append('Hourly')
        context.outputs = []
        for output in outputs:
            context.outputs.append(output_variable_dict[output] + ' - ' + ', '.join(map(str, outputs[output])) + '')
        context.outputs.sort()

        if model_run.status.allow_visualise():
            # Downloads
            context.output_variable_dict = output_variable_dict
            context.output_variable_id_dict = dict((x.name, x.id) for x in output_variables)
            context.downloads = outputs
            context.download_formats = ["NetCDF"]
            if context.extents_values['site'] == 'single':
                context.download_formats.append('ASCII')
Beispiel #2
0
    def extents(self):
        """
        Specify the spatial and temporal extents of the model
        """
        extents_controller_helper = ExtentsControllerHelper()

        # First we need to check that we are allowed to be on this page
        model_run = self.get_model_run_being_created_or_redirect(self._model_run_service)
        c.model_run = model_run
        c.dataset = driving_data = model_run.driving_dataset
        if driving_data is None:
            helpers.error_flash(u"You must select a driving data set before you can set the extents")
            redirect(url(controller='model_run', action='driving_data'))
        errors = {}

        if not request.POST:
            self._user_service.set_current_model_run_creation_action(self.current_user, "extents")
            values = extents_controller_helper.create_values_dict_from_database(model_run, driving_data)
            extents_controller_helper.set_template_context_fields(c, model_run, driving_data)

            # We need to check that saved values for user selected spatial extent are consistent with the chosen
            # driving data (e.g. in case the user has gone back and changed their driving data).
            extents_controller_helper.validate_extents_form_values(values, model_run, driving_data, errors)

            # Finally in our GET we render the page with any errors and values we have
            return htmlfill.render(
                render('model_run/extents.html'),
                defaults=values,
                errors=errors,
                auto_error_formatter=BaseController.error_formatter)

        # This is a POST
        else:
            values = self.form_result
            extents_controller_helper.set_template_context_fields(c, model_run, driving_data)

            extents_controller_helper.validate_extents_form_values(values, model_run, driving_data, errors)

            if len(errors) > 0:
                return htmlfill.render(
                    render('model_run/extents.html'),
                    defaults=values,
                    errors=errors,
                    auto_error_formatter=BaseController.error_formatter)
            try:
                spinup_duration = constants.SPINUP_MAX_TIME_RANGE_YEARS
                science_config = \
                    self._model_run_service.get_science_configuration_by_id(model_run.science_configuration_id)
                if science_config.science_configuration_spinup_in_years is not None:
                    spinup_duration = science_config.science_configuration_spinup_in_years
                extents_controller_helper.save_extents_against_model_run(
                    values,
                    driving_data,
                    model_run,
                    spinup_duration,
                    self._parameter_service, self.current_user)

            except DapClientException as ex:
                helpers.error_flash("Error submitting extents: %s" % ex.message)
                return htmlfill.render(
                    render('model_run/extents.html'),
                    defaults=values,
                    errors=errors,
                    auto_error_formatter=BaseController.error_formatter)

            # Get the action to perform
            self._model_run_controller_helper.check_user_quota(self.current_user)
            try:
                action = values['submit']
            except KeyError:
                action = None
            if action == u'Next':
                redirect(url(controller='model_run', action='land_cover'))
            else:
                redirect(url(controller='model_run', action='driving_data'))