def test_WHEN_get_user_model_runs_THEN_list_ordered_by_date_created_newest_first(self):
     with session_scope(Session) as session:
         # First add user
         user1 = User()
         user1.name = 'user1'
         session.add(user1)
         session.commit()
         # Create three published models not in datetime order
         model_run1 = ModelRun()
         model_run1.user_id = user1.id
         model_run1.name = "MR1"
         model_run1.date_created = datetime(2013, 12, 7, 17, 15, 30)
         session.add(model_run1)
         session.commit()
         model_run2 = ModelRun()
         model_run2.user_id = user1.id
         model_run2.name = "MR2"
         model_run2.date_created = datetime(2014, 6, 9, 12, 30, 24)
         session.add(model_run2)
         session.commit()
         model_run3 = ModelRun()
         model_run3.user_id = user1.id
         model_run3.name = "MR3"
         model_run3.date_created = datetime(2014, 6, 9, 11, 39, 30)
         session.add_all([model_run1, model_run2, model_run3])
     model_runs = self.model_run_service.get_models_for_user(user1)
     assert_that(model_runs[0].name, is_("MR2"))
     assert_that(model_runs[1].name, is_("MR3"))
     assert_that(model_runs[2].name, is_("MR1"))
    def test_GIVEN_model_with_parameters_WHEN_get_parameter_value_THEN_parameter_value_returned(self):
        with session_scope(Session) as session:
            user = User()
            user.name = 'user1'
            session.add(user)
            session.commit()

            param = session.query(Parameter).first()
            param_id = param.id
            param_name = param.name
            param_namelist = param.namelist.name

            model_run = ModelRun()
            model_run.name = "MR1"
            model_run.user_id = user.id
            model_run.status = self._status(constants.MODEL_RUN_STATUS_CREATED)
            session.add(model_run)
            session.commit()

            parameter1 = ParameterValue()
            parameter1.parameter_id = param_id
            parameter1.set_value_from_python("param1 value")
            parameter1.model_run_id = model_run.id
            session.add(parameter1)
        model_run_returned = self.model_run_service.get_model_being_created_with_non_default_parameter_values(user)
        param_value_returned = model_run_returned.get_python_parameter_value([param_namelist, param_name])
        assert_that(param_value_returned, is_("param1 value"))
 def test_GIVEN_nonexistent_model_id_WHEN_make_public_model_THEN_ServiceException_raised(self):
     # Add a user
     with session_scope(Session) as session:
         user = User()
         user.name = 'user1'
         session.add(user)
     with self.assertRaises(ServiceException, msg="Should have raised a ServiceException"):
         self.model_run_service.make_public_model(user, -100)
    def test_GIVEN_valid_model_WHEN_convert_to_dictionary_THEN_namelist_with_no_representation_are_present(self):

        job_runner_client = JobRunnerClient(config)

        def _alter_yearly_monthly_output_profiles(params):
            return params

        def _alter_driving_data_start(params):
            return params

        job_runner_client.alter_yearly_monthly_output_profiles = _alter_yearly_monthly_output_profiles
        job_runner_client.alter_driving_data_start = _alter_driving_data_start


        parameter = Parameter(name='param1')
        expected_parameter_value = '12'
        #There is no parameter value:
        parameter.parameter_values = []

        namelist = Namelist(name='NAME_LIST')
        namelist_file = NamelistFile(filename='filename')

        # Need this to run
        parameter2 = Parameter(name=constants.JULES_PARAM_LATLON_REGION[1])
        parameter2.parameter_values = [ParameterValue(value='.true.')]
        namelist2 = Namelist(name=constants.JULES_PARAM_LATLON_REGION[0])
        namelist_file = NamelistFile(filename='filename')
        namelist2.parameters = [parameter2]
        namelist2.namelist_file = namelist_file

        namelist.parameters = [parameter]
        namelist.namelist_file = namelist_file

        model_run = ModelRun()
        model_run.id = 101
        code_version = CodeVersion(name='Jules v3.4.1')

        user = User()
        user.id = 1
        user.email = "email"
        user.username = "******"
        model_run.user = user

        model_run.code_version = code_version
        code_version.parameters = [parameter, parameter2]

        result = job_runner_client.convert_model_to_dictionary(model_run, code_version.parameters, [])

        namelist_file_result = result[constants.JSON_MODEL_NAMELIST_FILES][0]
        assert_that(namelist_file_result[constants.JSON_MODEL_NAMELIST_FILE_FILENAME],
                    is_(namelist_file.filename), "value is correct")
        assert_that(namelist_file_result[constants.JSON_MODEL_NAMELISTS][0][constants.JSON_MODEL_NAMELIST_NAME],
                    is_(namelist.name), "value is correct")

        parameters_result = namelist_file_result[constants.JSON_MODEL_NAMELISTS][0][constants.JSON_MODEL_PARAMETERS]
        assert_that(parameters_result, is_({}), "there are no values")

        assert_that(len(result[constants.JSON_LAND_COVER]), is_(0))
 def test_GIVEN_no_model_runs_WHEN_get_model_runs_for_user_THEN_empty_list(self):
     # Add a user who doesn't have any model runs
     with session_scope(Session) as session:
         user = User()
         user.name = 'Has No Model Runs'
         session.add(user)
     # Get the users model runs
     model_runs = self.model_run_service.get_models_for_user(user)
     assert_that(model_runs, is_([]))
    def test_GIVEN_valid_model_WHEN_convert_to_dictionary_THEN_dictionary_is_correct(self):

        job_runner_client = JobRunnerClient(config)

        def _alter_yearly_monthly_output_profiles(params):
            return params

        def _alter_driving_data_start(params):
            return params

        job_runner_client.alter_yearly_monthly_output_profiles = _alter_yearly_monthly_output_profiles
        job_runner_client.alter_driving_data_start = _alter_driving_data_start

        parameter = Parameter(name=constants.JULES_PARAM_POINTS_FILE[1])
        expected_parameter_value = '51 0.5'
        expected_index = 3
        #There is only one parameter value per run:
        parameter.parameter_values = [ParameterValue(value=expected_parameter_value)]

        namelist = Namelist(name=constants.JULES_PARAM_POINTS_FILE[0])
        namelist_file = NamelistFile(filename='filename')

        namelist.parameters = [parameter]
        namelist.namelist_file = namelist_file
        namelist.index_in_file = expected_index

        model_run = ModelRun()
        model_run.id = 101
        model_run.land_cover_frac = "1 2 3 4 5 6 7 8 9"
        code_version = CodeVersion(name='Jules v3.4.1')

        model_run.code_version = code_version
        code_version.parameters = [parameter]

        user = User()
        user.id = 1
        user.email = "email"
        user.username = "******"
        model_run.user = user

        result = job_runner_client.convert_model_to_dictionary(model_run, code_version.parameters, [])

        assert_that(result[constants.JSON_MODEL_RUN_ID], is_(model_run.id), "value is correct")
        assert_that(result[constants.JSON_MODEL_CODE_VERSION], is_(model_run.code_version.name), "value is correct")
        assert_that(result[constants.JSON_USER_ID], is_(user.id), "user id")
        assert_that(result[constants.JSON_USER_NAME], is_(user.username), "user name")
        assert_that(result[constants.JSON_USER_EMAIL], is_(user.email), "user email")
        namelist_file_result = result[constants.JSON_MODEL_NAMELIST_FILES][0]
        assert_that(namelist_file_result[constants.JSON_MODEL_NAMELIST_FILE_FILENAME],
                    is_(namelist_file.filename), "value is correct")
        assert_that(namelist_file_result[constants.JSON_MODEL_NAMELISTS][0][constants.JSON_MODEL_NAMELIST_NAME],
                    is_(namelist.name), "namelist name")
        assert_that(namelist_file_result[constants.JSON_MODEL_NAMELISTS][0][constants.JSON_MODEL_NAMELIST_INDEX],
                    is_(namelist.index_in_file), "namelist index")
        parameters_result = namelist_file_result[constants.JSON_MODEL_NAMELISTS][0][constants.JSON_MODEL_PARAMETERS]
        assert_that(parameters_result, is_({parameter.name: expected_parameter_value}), "value is correct")
    def test_GIVEN_model_doesnot_exist_WHEN_duplicate_THEN_not_found(self):
        # Add a user who doesn't have any model runs
        with session_scope(Session) as session:
            user = User()
            user.name = 'Has No Model Runs'
            session.add(user)

        # Get the users model runs
        with self.assertRaises(NoResultFound, msg="Should have thrown a NoResultFound exception"):
            model_runs = self.model_run_service.duplicate_run_model(10000, user)
    def test_GIVEN_model_belongs_to_someone_else_WHEN_duplicate_THEN_not_found(self):
        # Add a user who doesn't have any model runs
        other_user = self.login("other_user")
        model = self.create_run_model(10, "test", other_user)

        with session_scope(Session) as session:
            user = User()
            user.name = 'Has No Model Runs'
            session.add(user)

        # Get the users model runs
        with self.assertRaises(NoResultFound, msg="Should have thrown a NoResultFound exception"):
            self.model_run_service.duplicate_run_model(model.id, user)
    def test_create_user(self):

        # Important - don't instantiate the mock class,
        # as the session creation function in the service
        # will do that for us

        sample_user = User()
        sample_user.username = '******'
        sample_user.name = 'Test User'
        sample_user.email = '*****@*****.**'
        sample_user.access_level = 'External'
        sample_user.first_name = "first_name"
        sample_user.last_name = "last_name"
        sample_user.storage_quota_in_gb = 89

        self._mock_session.add = MagicMock()
        self._mock_session.commit = MagicMock()

        user_service = UserService(self._mock_session)
        user_service.create(sample_user.username,
                            sample_user.first_name,
                            sample_user.last_name,
                            sample_user.email,
                            sample_user.access_level)

        self._mock_session.add.assert_called_once_with(ANY)
        self._mock_session.commit.assert_called_once_with()
 def test_GIVEN_complete_model_WHEN_make_public_model_THEN_ServiceException_raised(self):
     # Add a user and give them a model
     with session_scope(Session) as session:
         user = User()
         user.name = 'user1'
         session.add(user)
         session.commit()
         # Give them a model
         model_run = ModelRun()
         model_run.name = "MR1"
         model_run.user_id = user.id
         model_run.status = self._status(constants.MODEL_RUN_STATUS_COMPLETED)
         session.add(model_run)
     # Get the users model runs
     with self.assertRaises(ServiceException, msg="Should have raised a ServiceException"):
         self.model_run_service.make_public_model(user, model_run.id)
    def test_GIVEN_user_has_model_run_WHEN_get_model_run_by_id_THEN_model_run_returned(self):

        # Add a user and give them a model
        with session_scope(Session) as session:
            # First add user
            user = User()
            user.name = 'user1'
            session.add(user)
            session.commit()
            # Give them a model
            model_run = ModelRun()
            model_run.name = "MR1"
            model_run.user_id = user.id
            model_run.status = self._status(constants.MODEL_RUN_STATUS_PUBLIC)
            session.add(model_run)
        # Get the users model runs
        model_run_returned = self.model_run_service.get_model_by_id(user, model_run.id)
        assert_that(model_run_returned.name, is_("MR1"))
 def test_GIVEN_model_run_id_belongs_to_another_user_WHEN_get_model_run_by_id_THEN_NoResultFound_exception(self):
     # Add two users and give one a model
     with session_scope(Session) as session:
         user = User()
         user.name = 'user1'
         user2 = User()
         user2.name = 'user2'
         session.add_all([user, user2])
         session.commit()
         # Give them a model
         model_run = ModelRun()
         model_run.name = "MR1"
         model_run.user_id = user.id
         model_run.status = self._status(constants.MODEL_RUN_STATUS_COMPLETED)
         session.add(model_run)
     # Get the users model runs
     with self.assertRaises(NoResultFound, msg="Should have thrown a NoResultFound exception"):
         self.model_run_service.get_model_by_id(user2, model_run.id)
 def test_GIVEN_model_belongs_to_another_user_WHEN_make_public_model_THEN_ServiceException_raised(self):
     # Add two users and give one a model
     with session_scope(Session) as session:
         user = User()
         user.name = 'user1'
         user2 = User()
         user2.name = 'user2'
         session.add_all([user, user2])
         session.commit()
         # Give them a model
         model_run = ModelRun()
         model_run.name = "MR1"
         model_run.user_id = user.id
         model_run.status = self._status(constants.MODEL_RUN_STATUS_PUBLISHED)
         session.add(model_run)
     # Get the users model runs
     with self.assertRaises(ServiceException, msg="Should have raised a ServiceException"):
         self.model_run_service.make_public_model(user2, model_run.id)
 def test_GIVEN_user_has_completed_model_WHEN_publish_model_THEN_model_published(self):
     # Add a user and give them a model
     with session_scope(Session) as session:
         user = User()
         user.name = 'user1'
         session.add(user)
         session.commit()
         # Give them a model
         model_run = ModelRun()
         model_run.name = "MR1"
         model_run.user_id = user.id
         model_run.status = self._status(constants.MODEL_RUN_STATUS_COMPLETED)
         session.add(model_run)
     # Get the users model runs
     self.model_run_service.publish_model(user, model_run.id)
     with session_scope(Session) as session:
         updated_model_run = session.query(ModelRun).join(ModelRunStatus).filter(ModelRun.id == model_run.id).one()
         assert_that(updated_model_run.status.name, is_(constants.MODEL_RUN_STATUS_PUBLISHED))
 def test_GIVEN_user_has_public_model_run_WHEN_get_published_model_runs_THEN_only_public_model_run_returned(self):
     # Add one user and give them a published and unpublished model run
     with session_scope(Session) as session:
         # First add user
         user1 = User()
         user1.name = 'user1'
         session.add(user1)
         session.commit()
         # Give them each a model
         model_run1 = ModelRun()
         model_run1.name = "MR1"
         model_run1.user_id = user1.id
         model_run2 = ModelRun()
         model_run2.name = "MR2"
         model_run2.status = self._status(constants.MODEL_RUN_STATUS_PUBLIC)
         model_run2.user_id = user1.id
         session.add_all([model_run1, model_run2])
     # Get the published model runs
     model_runs = self.model_run_service.get_published_models()
     assert_that(len(model_runs), is_(1))
     assert_that(model_runs[0].name, is_("MR2"))
 def test_GIVEN_user_has_published_model_run_WHEN_get_model_runs_for_user_THEN_published_model_is_returned(self):
     # Add one user and give them a published and unpublished model run
     with session_scope(Session) as session:
         # First add user
         user1 = User()
         user1.name = 'user1'
         session.add(user1)
         session.commit()
         # Give a model
         model_run1 = ModelRun()
         model_run1.name = "MR1"
         model_run1.user_id = user1.id
         model_run1.status = self._status(constants.MODEL_RUN_STATUS_CREATED)
         model_run2 = ModelRun()
         model_run2.name = "MR2"
         model_run2.status = self._status(constants.MODEL_RUN_STATUS_PUBLISHED)
         model_run2.user_id = user1.id
         model_run2.status = self._status(constants.MODEL_RUN_STATUS_COMPLETED)
         session.add_all([model_run1, model_run2])
     # Get the users model runs
     model_runs = self.model_run_service.get_models_for_user(user1)
     assert_that(len(model_runs), is_(2))
 def test_GIVEN_two_users_with_one_model_each_WHEN_get_model_runs_for_user_THEN_returns_correct_model(self):
     # Add two users and give them each one model run
     with session_scope(Session) as session:
         # First add two users
         user1 = User()
         user1.name = 'user1'
         user2 = User()
         user2.name = 'user2'
         session.add_all([user1, user2])
         session.commit()
         # Give them each a model
         model_run1 = ModelRun()
         model_run1.name = "MR1"
         model_run1.user_id = user1.id
         model_run2 = ModelRun()
         model_run2.name = "MR2"
         model_run2.user_id = user2.id
         session.add_all([model_run1, model_run2])
     # Get the users model runs
     model_runs = self.model_run_service.get_models_for_user(user1)
     assert_that(len(model_runs), is_(1))
     assert_that(model_runs[0].name, is_("MR1"))
    def test_GIVEN_model_with_land_cover_actions_WHEN_convert_to_dictionary_THEN_land_cover_actions_present(self):

        job_runner_client = JobRunnerClient(config)

        def _alter_yearly_monthly_output_profiles(params):
            return params

        def _alter_driving_data_start(params):
            return params

        job_runner_client.alter_yearly_monthly_output_profiles = _alter_yearly_monthly_output_profiles
        job_runner_client.alter_driving_data_start = _alter_driving_data_start

        parameter = Parameter(name='file')
        expected_parameter_value = "'base_frac_file.nc'"
        parameter.parameter_values = [ParameterValue(value=expected_parameter_value)]

        parameter2 = Parameter(name='frac_name')
        expected_parameter_value2 = "'frac'"
        parameter2.parameter_values = [ParameterValue(value=expected_parameter_value2)]

        parameter3 = Parameter(name='latlon_region')
        expected_parameter_value3 = ".true."
        parameter3.parameter_values = [ParameterValue(value=expected_parameter_value3)]

        namelist = Namelist(name='JULES_FRAC')
        namelist_file = NamelistFile(filename='filename')
        expected_index = 3

        namelist.parameters = [parameter, parameter2]
        namelist.namelist_file = namelist_file
        namelist.index_in_file = expected_index

        namelist2 = Namelist(name='JULES_MODEL_GRID')
        namelist_file = NamelistFile(filename='filename')
        expected_index = 3

        namelist2.parameters = [parameter3]
        namelist2.namelist_file = namelist_file
        namelist2.index_in_file = expected_index

        model_run = ModelRun()
        model_run.id = 101
        code_version = CodeVersion(name='Jules v3.4.1')

        model_run.code_version = code_version
        code_version.parameters = [parameter, parameter2, parameter3]

        user = User()
        user.id = 1
        user.email = "email"
        user.username = "******"
        model_run.user = user

        lcr1 = LandCoverRegion()
        lcr1.mask_file = "region1.nc"
        lca1 = LandCoverAction()
        lca1.value_id = 9
        lca1.order = 1
        lca1.region = lcr1

        lcr2 = LandCoverRegion()
        lcr2.mask_file = "region2.nc"
        lca2 = LandCoverAction()
        lca2.value_id = 5
        lca2.order = 2
        lca2.region = lcr2

        land_cover_actions = [lca1, lca2]

        result = job_runner_client.convert_model_to_dictionary(model_run, code_version.parameters, land_cover_actions)
        result_lc = result[constants.JSON_LAND_COVER]

        assert_that(result_lc[constants.JSON_LAND_COVER_BASE_FILE], is_("base_frac_file.nc"))
        assert_that(result_lc[constants.JSON_LAND_COVER_BASE_KEY], is_("frac"))
        assert_that(result_lc[constants.JSON_LAND_COVER_ICE_INDEX], is_(9))
        result_lc_actions = result_lc[constants.JSON_LAND_COVER_ACTIONS]

        assert_that(len(result_lc_actions), is_(2))
        action1 = result_lc_actions[0]
        assert_that(action1[constants.JSON_LAND_COVER_MASK_FILE], is_("region1.nc"))
        assert_that(action1[constants.JSON_LAND_COVER_ORDER], is_(1))
        assert_that(action1[constants.JSON_LAND_COVER_VALUE], is_(9))

        action2 = result_lc_actions[1]
        assert_that(action2[constants.JSON_LAND_COVER_MASK_FILE], is_("region2.nc"))
        assert_that(action2[constants.JSON_LAND_COVER_ORDER], is_(2))
        assert_that(action2[constants.JSON_LAND_COVER_VALUE], is_(5))

        namelist_file_result = result[constants.JSON_MODEL_NAMELIST_FILES][0]
        parameters_result = namelist_file_result[constants.JSON_MODEL_NAMELISTS][0][constants.JSON_MODEL_PARAMETERS]
        assert_that(parameters_result['file'], is_("'" + constants.USER_EDITED_FRACTIONAL_FILENAME + "'"))
        assert_that(parameters_result['frac_name'], is_("'frac'"))
Exemple #19
0
def setup_app(command, conf, vars):
    """
    Place any commands to setup joj here - currently creating db tables
    :param command:
    :param conf:
    :param vars:
    :return:
    """

    # Don't reload the app if it was loaded under the testing environment
    if not pylons.test.pylonsapp:
        load_environment(conf.global_conf, conf.local_conf)

    # Create the tables
    Base.metadata.drop_all(bind=Session.bind)
    Base.metadata.create_all(bind=Session.bind)

    jules_config_parser = JulesNamelistParser()

    with session_scope(Session) as session:
        user = User()
        user.name = 'John Holt'
        user.first_name = 'John'
        user.last_name = 'Holt'
        user.username = '******'
        user.email = '*****@*****.**'
        user.access_level = "Admin"
        user.storage_quota_in_gb = conf['storage_quota_admin_GB']

        session.add(user)

        user2 = User()
        user2.name = 'Matt Kendall'
        user2.first_name = 'Matt'
        user2.last_name = 'Kendall'
        user2.username = '******'
        user2.email = '*****@*****.**'
        user2.access_level = "Admin"
        user2.storage_quota_in_gb = conf['storage_quota_admin_GB']

        session.add(user2)

        user3 = User()
        user3.name = 'Matt Fry'
        user3.first_name = 'Matt'
        user3.last_name = 'Fry'
        user3.username = '******'
        user3.email = '*****@*****.**'
        user3.access_level = "Admin"
        user3.storage_quota_in_gb = conf['storage_quota_admin_GB']

        session.add(user3)

        user4 = User()
        user4.name = 'Eleanor Blyth'
        user4.first_name = 'Eleanor'
        user4.last_name = 'Blyth'
        user4.username = '******'
        user4.email = '*****@*****.**'
        user4.access_level = "Admin"
        user4.storage_quota_in_gb = conf['storage_quota_admin_GB']

        session.add(user4)

        user5 = User()
        user5.name = 'Sandeep Kaur'
        user5.first_name = 'Sandeep'
        user5.last_name = 'Kaur'
        user5.username = '******'
        user5.email = '*****@*****.**'
        user5.access_level = "Admin"
        user5.storage_quota_in_gb = conf['storage_quota_admin_GB']

        session.add(user5)

        core_user = User()
        core_user.name = 'System'
        core_user.first_name = 'The'
        core_user.last_name = 'System'
        core_user.username = constants.CORE_USERNAME
        core_user.email = ''
        core_user.storage_quota_in_gb = conf['storage_quota_total_GB']  # Total storage for the group workspace

        session.add(core_user)

        cover_dst = DatasetType(type=constants.DATASET_TYPE_COVERAGE)
        session.add(cover_dst)
        land_cover_frac_dst = DatasetType(type=constants.DATASET_TYPE_LAND_COVER_FRAC)
        session.add(land_cover_frac_dst)
        soil_prop_dst = DatasetType(type=constants.DATASET_TYPE_SOIL_PROP)
        session.add(soil_prop_dst)
        single_cell_dst = DatasetType(type=constants.DATASET_TYPE_SINGLE_CELL)
        session.add(single_cell_dst)
        transects_dst = DatasetType(type=constants.DATASET_TYPE_TRANSECT)
        session.add(transects_dst)

        level = UserLevel()
        level.name = 'Beginner'
        session.add(level)

        stat_created = ModelRunStatus(constants.MODEL_RUN_STATUS_CREATED)
        stat_submitted = ModelRunStatus(constants.MODEL_RUN_STATUS_SUBMITTED)
        stat_pending = ModelRunStatus(constants.MODEL_RUN_STATUS_PENDING)
        stat_running = ModelRunStatus(constants.MODEL_RUN_STATUS_RUNNING)
        stat_completed = ModelRunStatus(constants.MODEL_RUN_STATUS_COMPLETED)
        stat_published = ModelRunStatus(constants.MODEL_RUN_STATUS_PUBLISHED)
        stat_public = ModelRunStatus(constants.MODEL_RUN_STATUS_PUBLIC)
        stat_failed = ModelRunStatus(constants.MODEL_RUN_STATUS_FAILED)
        stat_submit_failed = ModelRunStatus(constants.MODEL_RUN_STATUS_SUBMIT_FAILED)
        stat_unknown = ModelRunStatus(constants.MODEL_RUN_STATUS_UNKNOWN)

        map(session.add, [stat_created, stat_submitted, stat_pending, stat_running, stat_completed, stat_published,
                          stat_public, stat_failed, stat_submit_failed, stat_unknown])

        session.add(SystemAlertEmail(
            code=SystemAlertEmail.GROUP_SPACE_FULL_ALERT,
            sent_frequency_in_s=conf['alert_email_frequency_in_s']))

        default_code_version = CodeVersion()
        default_code_version.name = conf.local_conf['default_code_version']
        default_code_version.url_base = 'http://www.jchmr.org/jules/documentation/user_guide/vn3.4/'
        default_code_version.is_default = True
        session.add(default_code_version)

        # Namelists from docs
        jules_parameter_parser = JulesParameterParser()
        namelist_files = jules_parameter_parser.parse_all("docs/Jules/user_guide/html/namelists/", default_code_version)
        for namelist_file in namelist_files:
            session.add(namelist_file)

        # Output variable from docs
        jules_output_variables_parser = JulesOutputVariableParser()
        output_variables = jules_output_variables_parser.parse("docs/Jules/user_guide/html/output-variables.html")
        session.add_all(output_variables)

        # Add WATCH 100 Years run
        watch_model_run = jules_config_parser.parse_namelist_files_to_create_a_model_run(
            default_code_version,
            "Watch data run over the length of the data",
            "configuration/Jules/watch",
            "Watch 100 Years Data",
            namelist_files,
            stat_published,
            core_user,
            None)
        watch_model_run.date_created = datetime.datetime(2014, 8, 26, 16, 00, 00)
        watch_model_run.date_submitted = datetime.datetime(2014, 8, 26, 16, 00, 00)
        watch_model_run.date_started = datetime.datetime(2014, 8, 26, 16, 00, 00)
        watch_model_run.last_status_change = datetime.datetime(2014, 8, 26, 16, 00, 00)
        watch_model_run.storage_in_mb = 1000

        land_cover_file = insert_before_file_extension(WATCH_FRAC_FILE, constants.MODIFIED_FOR_VISUALISATION_EXTENSION)
        ancils = [
            [WATCH_SOIL_PROPS_FILE, 'Soil Properties', 0, 10, soil_prop_dst],
            [land_cover_file, 'Land Cover Fractions', 0, 10, land_cover_frac_dst]
        ]

        for path, var, name, min, max in WATCH_DRIVING_DATA:
            ds = Dataset()
            ds.name = name
            ds.wms_url = conf.local_conf['thredds.server_url'] \
                + "wms/model_runs/run1/data/WATCH_2D/driving/" + path + ".ncml"\
                "?service=WMS&version=1.3.0&request=GetCapabilities"
            ds.netcdf_url = conf.local_conf['thredds.server_url'] + "dodsC/model_runs/run1/data/WATCH_2D/driving/" + \
                path + ".ncml"
            ds.data_range_from = min
            ds.data_range_to = max
            ds.is_categorical = 0
            ds.deleted = 0
            ds.dataset_type = cover_dst
            ds.is_input = True
            ds.model_run = watch_model_run

        for path, name, min, max, dataset_type in ancils:
            ds = Dataset()
            ds.name = name
            ds.wms_url = conf.local_conf['thredds.server_url'] \
                + "wms/model_runs/run1/" + path + \
                "?service=WMS&version=1.3.0&request=GetCapabilities"
            ds.netcdf_url = conf.local_conf['thredds.server_url'] + "dodsC/model_runs/run1/" + path
            ds.data_range_from = min
            ds.data_range_to = max
            ds.is_categorical = 0
            ds.deleted = 0
            ds.dataset_type = dataset_type
            ds.is_input = True
            ds.model_run = watch_model_run

        profiles = ["Monthly"]
        outputs = [
            ['gpp_gb_{}', 'Gridbox gross primary productivity ({})', 0, 1e-7],
            ['rad_net_{}', 'Surface net radiation of land points ({})', -50, 150],
            ['resp_p_gb_{}', 'Gridbox plant respiration ({})', 0, 4e-8],
            ['smc_tot_{}', 'Gridbox total soil moisture in column ({})', 0, 1500],
            ['sub_surf_roff_{}', 'Gridbox sub-surface runoff ({})', 0, 1e-6],
            ['surf_roff_{}', 'Gridbox surface runoff ({})', 0, 5e-5],
            ['swet_liq_tot_{}', 'Gridbox unfrozen soil moisture as fraction of saturation ({})', 0, 1]]

        for profile in profiles:
            for path_template, name_template, min, max in outputs:
                path = path_template.format(profile.lower())
                name = name_template.format(profile)
                ds = Dataset()
                ds.name = name
                ds.wms_url = conf.local_conf['thredds.server_url'] \
                    + "wms/model_runs/run1/output/majic." + path + ".ncml" + \
                    "?service=WMS&version=1.3.0&request=GetCapabilities"
                ds.netcdf_url = conf.local_conf['thredds.server_url'] + "dodsC/model_runs/run1/output/majic." \
                    + path + ".ncml"
                ds.data_range_from = min
                ds.data_range_to = max
                ds.is_categorical = 0
                ds.deleted = 0
                ds.dataset_type = cover_dst
                ds.is_input = False
                ds.model_run = watch_model_run

        session.add(watch_model_run)

        jules_config_parser.parse_all(
            "configuration/Jules/scientific_configurations/",
            namelist_files,
            core_user,
            default_code_version,
            stat_created)

    with session_scope(Session) as session:
        watch_driving_data_name = create_watch_driving_data(conf, cover_dst, land_cover_frac_dst, soil_prop_dst,
                                                            "configuration/Jules/country.csv")
        chess_driving_data_name = create_chess_driving_data(conf, cover_dst, land_cover_frac_dst, soil_prop_dst)

    with session_scope(Session) as session:

        driving_ds_upload = DrivingDataset()
        driving_ds_upload.name = constants.USER_UPLOAD_DRIVING_DATASET_NAME
        driving_ds_upload.description = "Choose this option if you wish to use your own uploaded driving data for a " \
                                        "single cell site"
        driving_ds_upload.view_order_index = 1000
        driving_ds_upload.is_restricted_to_admins = False

        parameters_upload = [
            [constants.JULES_PARAM_POST_PROCESSING_ID, "3"],
            [constants.JULES_PARAM_INPUT_GRID_NX, "1"],
            [constants.JULES_PARAM_INPUT_GRID_NY, "1"],

            # Default soil properties, hopefully overridden in the background when the user sets the land cover.
            [constants.JULES_PARAM_SOIL_PROPS_NVARS, "9"],
            [constants.JULES_PARAM_SOIL_PROPS_VAR,
             "'b'       'sathh'  'satcon'  'sm_sat'  'sm_crit'  'sm_wilt'  'hcap'      'hcon'   'albsoil'"],
            [constants.JULES_PARAM_SOIL_PROPS_USE_FILE, ".false. .false. .false. .false. .false. .false. .false. "
                ".false. .false."],
            [constants.JULES_PARAM_SOIL_PROPS_CONST_VAL,
             "0.9     0.0      0.0         50.0     275.0        278.0    10.0 0.0"],

            [constants.JULES_PARAM_INITIAL_NVARS, "10"],
            [constants.JULES_PARAM_INITIAL_VAR,
                "'sthuf' 'canopy' 'snow_tile' 'rgrain' 'tstar_tile' 't_soil' 'cs' 'gs'  'lai' 'canht'"],
            [constants.JULES_PARAM_INITIAL_USE_FILE,
                ".false.  .false.  .false.  .false.  .false.  .false.  .false.  .false. .false.  .false."],
            [constants.JULES_PARAM_INITIAL_CONST_VAL,
                "0.9     0.0      0.0         50.0     275.0        278.0    10.0   0.0   1.0   2.0"]
        ]

        model_run_service = ModelRunService()
        for constant, value in parameters_upload:
            ddpv = DrivingDatasetParameterValue(model_run_service, driving_ds_upload, constant, value)
            driving_ds_upload.parameter_values.append(ddpv)

        session.add(driving_ds_upload)

        land_cover_types = {1: 'Broad-leaved Tree', 2: 'Needle-leaved Tree', 3: 'C3 Grass', 4: 'C4 Grass', 5: 'Shrub',
                            6: 'Urban', 7: 'Lake', 8: 'Soil', 9: constants.FRACTIONAL_ICE_NAME}
        for index in land_cover_types:
            land_cover_type = LandCoverValue()
            land_cover_type.index = index
            land_cover_type.name = land_cover_types[index]
            session.add(land_cover_type)

    with session_scope() as session:
        watch_model = session.query(ModelRun).filter(ModelRun.name == watch_model_run.name).one()
        science_config = session.query(ModelRun).filter(ModelRun.name == "Full carbon cycle").one()
        driving_dataset = session.query(DrivingDataset).filter(DrivingDataset.name == watch_driving_data_name).one()

        watch_model.science_configuration_id = science_config.id
        watch_model.driving_dataset_id = driving_dataset.id
    def test_GIVEN_model_contains_repeated_namelists_WHEN_convert_to_dictionary_THEN_namelists_grouped_correctly(self):
        job_runner_client = JobRunnerClient(config)

        def _alter_yearly_monthly_output_profiles(params):
            return params

        def _alter_driving_data_start(params):
            return params

        job_runner_client.alter_yearly_monthly_output_profiles = _alter_yearly_monthly_output_profiles
        job_runner_client.alter_driving_data_start = _alter_driving_data_start

        # Set the namelists here:
        nml_output = Namelist(name=constants.JULES_NML_OUTPUT)
        nml_output_profile = Namelist(name=constants.JULES_NML_OUTPUT_PROFILE)
        nml_output.namelist_file = nml_output_profile.namelist_file = NamelistFile(filename="output.nml")

        nml_model_grid = Namelist(name=constants.JULES_PARAM_LATLON_REGION[0])
        nml_model_grid.namelist_file = NamelistFile(filename="model_grid.nml")

        # Set the parameters here:
        param_nprofiles = Parameter(name=constants.JULES_PARAM_OUTPUT_NPROFILES[1])
        param_profile_name = Parameter(name=constants.JULES_PARAM_OUTPUT_PROFILE_NAME[1])
        param_var = Parameter(name=constants.JULES_PARAM_OUTPUT_VAR[1])
        param_latlon_region = Parameter(name=constants.JULES_PARAM_LATLON_REGION[1])

        # Set the parameter values
        param_nprofiles.parameter_values = [ParameterValue(value=2)]
        param_profile_name.parameter_values = [ParameterValue(value="monthly", group_id=0),
                                               ParameterValue(value="yearly", group_id=1)]

        param_var.parameter_values = [ParameterValue(value='"emis_gb", "ftl_gb"', group_id=0),
                                      ParameterValue(value='"snow_mass_gb", "tstar_gb"', group_id=1)]
        param_latlon_region.parameter_values = [ParameterValue(value=".true.")]

        # Assign the parameters to namelists
        nml_output.parameters = [param_nprofiles]
        nml_output_profile.parameters = [param_profile_name, param_var]
        nml_model_grid.parameters = [param_latlon_region]

        model_run = ModelRun()
        model_run.id = 101
        code_version = CodeVersion(name='Jules v3.4.1')

        user = User()
        user.id = 1
        user.email = "email"
        user.username = "******"
        model_run.user = user

        model_run.code_version = code_version
        code_version.parameters = [param_profile_name, param_var, param_nprofiles, param_latlon_region]

        result = job_runner_client.convert_model_to_dictionary(model_run, code_version.parameters, [])

        # Check the result dictionary has the correct run ID and code version
        assert_that(result[constants.JSON_MODEL_RUN_ID], is_(model_run.id), "value is correct")
        assert_that(result[constants.JSON_MODEL_CODE_VERSION], is_(model_run.code_version.name), "value is correct")

        # Check that the namelist_files has the correct namelist file
        namelist_file_result = result[constants.JSON_MODEL_NAMELIST_FILES][0]
        assert_that(namelist_file_result[constants.JSON_MODEL_NAMELIST_FILE_FILENAME],
                    is_("output.nml"), "value is correct")

        # Check that the namelist file contains the three namelists
        namelists = namelist_file_result[constants.JSON_MODEL_NAMELISTS]
        assert_that(len(namelists), is_(3))

        # Check the dictionaries are as expected
        profiles_dict1 = {constants.JSON_MODEL_NAMELIST_GROUP_ID: 0,
                          constants.JSON_MODEL_NAMELIST_INDEX: None,
                          constants.JSON_MODEL_NAMELIST_NAME: constants.JULES_NML_OUTPUT_PROFILE,
                          constants.JSON_MODEL_PARAMETERS: {
                              constants.JULES_PARAM_OUTPUT_VAR[1]: '"emis_gb", "ftl_gb"',
                              constants.JULES_PARAM_OUTPUT_PROFILE_NAME[1]: "monthly"
                          }}
        profiles_dict2 = {constants.JSON_MODEL_NAMELIST_GROUP_ID: 1,
                          constants.JSON_MODEL_NAMELIST_INDEX: None,
                          constants.JSON_MODEL_NAMELIST_NAME: constants.JULES_NML_OUTPUT_PROFILE,
                          constants.JSON_MODEL_PARAMETERS: {
                              constants.JULES_PARAM_OUTPUT_VAR[1]: '"snow_mass_gb", "tstar_gb"',
                              constants.JULES_PARAM_OUTPUT_PROFILE_NAME[1]: "yearly"
                          }}
        output_dict = {constants.JSON_MODEL_NAMELIST_GROUP_ID: None,
                       constants.JSON_MODEL_NAMELIST_INDEX: None,
                       constants.JSON_MODEL_NAMELIST_NAME: constants.JULES_NML_OUTPUT,
                       constants.JSON_MODEL_PARAMETERS: {
                           constants.JULES_PARAM_OUTPUT_NPROFILES[1]: 2
                       }}
        assert_that(namelists, contains_inanyorder(profiles_dict1, profiles_dict2, output_dict))
 def test_GIVEN_non_existent_user_WHEN_get_model_runs_for_user_THEN_returns_empty_list(self):
     user = User()
     user.id = -100
     user.name = "Doesn't Exist"
     model_runs = self.model_run_service.get_models_for_user(user)
     assert_that(model_runs, is_([]))
Exemple #22
0
    def create_in_session(self,
                          session,
                          username,
                          first_name,
                          last_name,
                          email,
                          access_level,
                          institution="",
                          workbench_username=None):
        """
        Creates a user within a session

        :param session: session to use
        :param username: The login name of the user
        :param first_name: User's first name
        :param last_name: User's last name
        :param email: User's email address
        :param access_level: Set to 'Admin' for administrative functions
        :param institution: users institution may be blank
        :param workbench_username: username within the workbench may be none
        :return: nothing
        """

        user = User()
        user.username = username.strip()
        user.name = " ".join([first_name, last_name])
        user.email = email
        user.access_level = access_level
        user.first_name = first_name
        user.last_name = last_name
        user.institution = institution
        user.workbench_username = workbench_username
        if user.access_level == constants.USER_ACCESS_LEVEL_ADMIN:
            user.storage_quota_in_gb = config['storage_quota_admin_GB']
        else:
            user.storage_quota_in_gb = config['storage_quota_user_GB']
        session.add(user)
        return user