Esempio n. 1
0
 def test_cmor_checker_called(self):
     """Check that the cmor check is done."""
     checker = Mock()
     checker.return_value = Mock()
     with patch('esmvalcore.cmor._fixes.fix.Fix.get_fixes',
                return_value=[]):
         with patch('esmvalcore.cmor.fix._get_cmor_checker',
                    return_value=checker) as get_mock:
             fix_metadata(
                 cubes=[self.cube],
                 short_name='short_name',
                 project='CMIP6',
                 dataset='dataset',
                 mip='mip',
                 frequency='frequency',
             )
             get_mock.assert_called_once_with(
                 automatic_fixes=True,
                 fail_on_error=False,
                 frequency='frequency',
                 mip='mip',
                 short_name='short_name',
                 table='CMIP6',
                 check_level=CheckLevels.DEFAULT,
             )
             checker.assert_called_once_with(self.cube)
             checker.return_value.check_metadata.assert_called_once_with()
Esempio n. 2
0
 def test_select_var_failed_if_bad_var_name(self):
     """Check that the same cube is returned if no fix is available."""
     with mock.patch(
             'esmvalcore.cmor._fixes.fix.Fix.get_fixes', return_value=[]):
         with self.assertRaises(ValueError):
             fix_metadata(
                 [
                     self._create_mock_cube('not_me'),
                     self._create_mock_cube('me_neither')
                 ],
                 'short_name',
                 'project',
                 'model'
             )
Esempio n. 3
0
 def test_nofix(self):
     """Check that the same cube is returned if no fix is available."""
     with mock.patch(
             'esmvalcore.cmor._fixes.fix.Fix.get_fixes', return_value=[]):
         cube_returned = fix_metadata([self.cube], 'short_name', 'project',
                                      'model')[0]
         self.assertTrue(cube_returned is self.cube)
         self.assertTrue(cube_returned is not self.fixed_cube)
Esempio n. 4
0
 def test_cmor_checker_called(self):
     """Check that the cmor check is done."""
     checker = mock.Mock()
     checker.return_value = mock.Mock()
     with mock.patch(
             'esmvalcore.cmor._fixes.fix.Fix.get_fixes', return_value=[]):
         with mock.patch(
                 'esmvalcore.cmor.fix._get_cmor_checker',
                 return_value=checker) as get_mock:
             fix_metadata([self.cube], 'short_name', 'project', 'model',
                          'cmor_table', 'mip', 'frequency')
             get_mock.assert_called_once_with(
                 automatic_fixes=True,
                 fail_on_error=False,
                 frequency='frequency',
                 mip='mip',
                 short_name='short_name',
                 table='cmor_table')
             checker.assert_called_once_with(self.cube)
             checker.return_value.check_metadata.assert_called_once_with()
Esempio n. 5
0
def test_cmorization(era5_cubes, cmor_cubes, var, mip):
    """Verify that cmorization results in the expected target cube."""
    fixed_cubes = fix_metadata(era5_cubes, var, 'native6', 'era5', mip)
    assert len(fixed_cubes) == 1
    fixed_cube = fixed_cubes[0]
    cmor_cube = cmor_cubes[0]
    if fixed_cube.coords('time'):
        for cube in [fixed_cube, cmor_cube]:
            coord = cube.coord('time')
            coord.points = np.round(coord.points, decimals=7)
            if coord.bounds is not None:
                coord.bounds = np.round(coord.bounds, decimals=7)
    print('cmor_cube:', cmor_cube.xml())
    print('fixed_cube:', fixed_cube.xml())
    assert fixed_cube.xml() == cmor_cube.xml()
    assert fixed_cube == cmor_cube
Esempio n. 6
0
def _load_fx(fx_info, check_level):
    """Load and CMOR-check fx variables."""
    fx_cubes = iris.cube.CubeList()

    for fx_file in fx_info['filename']:
        loaded_cube = load(fx_file, callback=concatenate_callback)
        short_name = fx_info['short_name']
        project = fx_info['project']
        dataset = fx_info['dataset']
        mip = fx_info['mip']
        freq = fx_info['frequency']
        loaded_cube = fix_metadata(loaded_cube,
                                   short_name=short_name,
                                   project=project,
                                   dataset=dataset,
                                   mip=mip,
                                   frequency=freq,
                                   check_level=check_level)
        fx_cubes.append(loaded_cube[0])

    fx_cube = concatenate(fx_cubes)

    fx_cube = cmor_check_metadata(fx_cube,
                                  cmor_table=project,
                                  mip=mip,
                                  short_name=short_name,
                                  frequency=freq,
                                  check_level=check_level)

    fx_cube = fix_data(fx_cube,
                       short_name=short_name,
                       project=project,
                       dataset=dataset,
                       mip=mip,
                       frequency=freq,
                       check_level=check_level)

    fx_cube = cmor_check_data(fx_cube,
                              cmor_table=project,
                              mip=mip,
                              short_name=fx_cube.var_name,
                              frequency=freq,
                              check_level=check_level)

    return fx_cube
Esempio n. 7
0
 def test_select_var(self):
     """Check that the same cube is returned if no fix is available."""
     self.check_metadata.side_effect = lambda: self.cube
     with patch('esmvalcore.cmor._fixes.fix.Fix.get_fixes',
                return_value=[]):
         with patch('esmvalcore.cmor.fix._get_cmor_checker',
                    return_value=self.checker):
             cube_returned = fix_metadata(
                 cubes=[self.cube,
                        self._create_mock_cube('extra')],
                 short_name='short_name',
                 project='CMIP6',
                 dataset='model',
                 mip='mip',
             )[0]
             self.checker.assert_called_once_with(self.cube)
             self.check_metadata.assert_called_once_with()
             assert cube_returned is self.cube
Esempio n. 8
0
def run_fix_metadata(cubes: iris.cube.CubeList, variable: typing.Dict,
                     **kwargs) -> iris.cube.CubeList:
    short_name = variable["short_name"]
    project = variable["project"]
    dataset = variable["dataset"]
    mip = variable["mip"]
    frequency = variable["frequency"]

    fixed_cubes = fix_metadata(
        cubes,
        short_name=short_name,
        project=project,
        dataset=dataset,
        mip=mip,
        frequency=frequency,
    )

    return fixed_cubes
Esempio n. 9
0
 def test_fix(self):
     """Check that the returned fix is applied."""
     self.check_metadata.side_effect = lambda: self.fixed_cube
     with patch('esmvalcore.cmor._fixes.fix.Fix.get_fixes',
                return_value=[self.mock_fix]):
         with patch('esmvalcore.cmor.fix._get_cmor_checker',
                    return_value=self.checker):
             cube_returned = fix_metadata(
                 cubes=[self.cube],
                 short_name='short_name',
                 project='project',
                 dataset='model',
                 mip='mip',
             )[0]
             self.checker.assert_called_once_with(self.intermediate_cube)
             self.check_metadata.assert_called_once_with()
             assert cube_returned is not self.cube
             assert cube_returned is not self.intermediate_cube
             assert cube_returned is self.fixed_cube