예제 #1
0
    def duplicate_run_model(self, model_id, user):
        """
        Duplicate the run model and all its parameters to a new run model
        :param model_id: model run id to duplicate
        :param user: the user duplicating the model
        :return: nothing
        """

        id_for_user_upload_driving_dataset = self._dataset_service.get_id_for_user_upload_driving_dataset()
        self.delete_model_run_being_created(user)

        with self.transaction_scope() as session:
            model_run_to_duplicate = session\
                .query(ModelRun)\
                .join(ModelRunStatus)\
                .outerjoin(ParameterValue)\
                .outerjoin(LandCoverAction) \
                .filter(ModelRun.id == model_id) \
                .filter(or_(
                    ModelRun.user_id == user.id,
                    ModelRunStatus.name == constants.MODEL_RUN_STATUS_PUBLISHED,
                    ModelRunStatus.name == constants.MODEL_RUN_STATUS_PUBLIC,)) \
                .options(contains_eager(ModelRun.parameter_values)) \
                .options(contains_eager(ModelRun.land_cover_actions)) \
                .one()

            new_model_run_name = model_run_to_duplicate.name
            if self._is_duplicate_name(model_run_to_duplicate.name, session, user):
                new_model_run_name = "{} (Copy)".format(model_run_to_duplicate.name)
                copy_id = 1
                while self._is_duplicate_name(new_model_run_name, session, user):
                    copy_id += 1
                    new_model_run_name = "{} (Copy {})".format(model_run_to_duplicate.name, copy_id)

            new_model_run = ModelRun()

            new_model_run.duplicate_from(model_run_to_duplicate)
            new_model_run.name = new_model_run_name
            new_model_run.user = user
            new_model_run.change_status(session, constants.MODEL_RUN_STATUS_CREATED)
            for parameter_value in model_run_to_duplicate.parameter_values:
                new_parameter = ParameterValue()
                new_parameter.duplicate_from(parameter_value)
                new_parameter.model_run = new_model_run

            for land_cover_action in model_run_to_duplicate.land_cover_actions:
                new_land_cover_action = LandCoverAction()
                new_land_cover_action.duplicate_from(land_cover_action)
                new_land_cover_action.model_run = new_model_run

            session.add(new_model_run)

        if model_run_to_duplicate.driving_dataset_id == id_for_user_upload_driving_dataset:
            try:
                self._job_runner_client.duplicate_uploaded_driving_data(model_run_to_duplicate.id, new_model_run.id)
            except ServiceException:
                self.delete_model_run_being_created(user)
                raise ServiceException("Could not duplicate the model run because "
                                       "the user uploaded data can not be duplicated")
예제 #2
0
    def _create_new_model_run(self, session, user):
        """
        Create a brand new model run for user
        :param session: session to use
        :param user: Currently logged in user
        :return: New model run
        """
        model_run = ModelRun()
        model_run.change_status(session, constants.MODEL_RUN_STATUS_CREATED)
        model_run.user = user

        parameters = [
            [constants.JULES_PARAM_TIMESTEP_LEN, constants.TIMESTEP_LEN, None],
            [constants.JULES_PARAM_OUTPUT_RUN_ID, constants.RUN_ID, None],
            [constants.JULES_PARAM_OUTPUT_OUTPUT_DIR, "./" + constants.OUTPUT_DIR, None],
        ]

        # Add CHESS defaults:
        chess_defaults = ['surf_roff', 'sub_surf_roff', 'fqw_gb', 'rad_net', 'ftl_gb', 'gpp_gb', 'resp_p_gb',
                          'tstar_gb', 'snow_mass_gb', 't_soil', 'smc_tot', 'smcl', 'swet_liq_tot']
        chess_periods = [(constants.JULES_MONTHLY_PERIOD, "_monthly")]

        group_id = 0
        for output_variable in chess_defaults:
            for period, period_profile_name in chess_periods:
                parameters.append([constants.JULES_PARAM_OUTPUT_VAR, output_variable, group_id])
                parameters.append([constants.JULES_PARAM_OUTPUT_PERIOD, period, group_id])
                parameters.append([constants.JULES_PARAM_OUTPUT_PROFILE_NAME,
                                   output_variable + period_profile_name, group_id])
                parameters.append([constants.JULES_PARAM_OUTPUT_NVARS, 1, group_id])
                parameters.append([constants.JULES_PARAM_OUTPUT_MAIN_RUN, True, group_id])
                parameters.append([constants.JULES_PARAM_OUTPUT_TYPE, 'M', group_id])
                group_id += 1
        parameters.append([constants.JULES_PARAM_OUTPUT_NPROFILES, group_id, None])

        for constant, value, group_id in parameters:
            param = self.parameter_service.get_parameter_by_constant(constant, session)
            param_value = ParameterValue()
            param_value.parameter = param
            param_value.model_run = model_run
            param_value.group_id = group_id
            param_value.set_value_from_python(value)

        return model_run
    def setUp(self):
        self.clean_database()
        self.user = self.login()
        self.create_two_driving_datasets()
        dds = DatasetService().get_driving_datasets(self.user)[0]
        with session_scope() as session:
            model_run = ModelRun()
            model_run.name = "model run"
            model_run.change_status(session, MODEL_RUN_STATUS_CREATED)
            model_run.user = self.user
            model_run.driving_dataset_id = dds.id
            session.add(model_run)
            session.commit()

            self.model_run_service = ModelRunService()
            model_run = self.model_run_service._get_model_run_being_created(session, self.user)

            parameter_val = ParameterValue()
            parameter_val.parameter = self.model_run_service.get_parameter_by_constant(JULES_PARAM_LATLON_REGION)
            parameter_val.set_value_from_python(True)
            parameter_val.model_run_id = model_run.id
            session.add(parameter_val)

        self.add_land_cover_region(model_run)