コード例 #1
0
ファイル: test_mask.py プロジェクト: mathause/ESMValCore
    def test_mask_landsea(self):
        """Test mask_landsea func."""
        iris.save(self.fx_mask, 'sftlf_test.nc')
        new_cube_land = iris.cube.Cube(
            self.new_cube_data, dim_coords_and_dims=self.coords_spec)
        new_cube_sea = iris.cube.Cube(
            self.new_cube_data, dim_coords_and_dims=self.coords_spec)

        # mask with fx files
        result_land = mask_landsea(new_cube_land, ['sftlf_test.nc'], 'land')
        result_sea = mask_landsea(new_cube_sea, ['sftlf_test.nc'], 'sea')
        expected = np.ma.empty((3, 3))
        expected.data[:] = 200.
        expected.mask = np.ones((3, 3), bool)
        # set fillvalues so we are sure they are equal
        np.ma.set_fill_value(result_land.data, 1e+20)
        np.ma.set_fill_value(result_sea.data, 1e+20)
        np.ma.set_fill_value(expected, 1e+20)
        assert_array_equal(result_land.data.mask, expected.mask)
        expected.mask = np.zeros((3, 3), bool)
        assert_array_equal(result_sea.data, expected)
        # remove the fx.nc temporary file
        os.remove('sftlf_test.nc')

        # mask with shp files
        new_cube_land = iris.cube.Cube(
            self.new_cube_data, dim_coords_and_dims=self.coords_spec)
        new_cube_sea = iris.cube.Cube(
            self.new_cube_data, dim_coords_and_dims=self.coords_spec)

        # bear in mind all points are in the ocean
        result_land = mask_landsea(new_cube_land, None, 'land')
        np.ma.set_fill_value(result_land.data, 1e+20)
        expected.mask = np.zeros((3, 3), bool)
        assert_array_equal(result_land.data, expected)
コード例 #2
0
def _extract_variable(cmor_info, attrs, in_dir, out_dir, ctl):
    """Extract variable."""
    filepath = os.path.join(in_dir, ctl['binary_prefix'] + '.dat')
    raw_data = np.fromfile(filepath, ctl['dtype'],
                           ctl['t_size'] * ctl['y_size'] *
                           ctl['x_size']).reshape(ctl['t_size'], ctl['y_size'],
                                                  ctl['x_size'])

    # Get coordinates
    coords = _get_coords(ctl)

    # Build cube
    cube = iris.cube.Cube(raw_data, dim_coords_and_dims=coords)

    # Mask appropriate parts
    if cmor_info.short_name == 'nbp':
        cube = mask_landsea(cube, 'sea')
    elif cmor_info.short_name == 'fgco2':
        cube = mask_landsea(cube, 'land')
    else:
        raise NotImplementedError(
            f"CMORizer for '{cmor_info.short_name}' not implemented yet")

    # Fix metadata
    utils.fix_var_metadata(cube, cmor_info)
    utils.convert_timeunits(cube, 1950)
    utils.fix_coords(cube)
    utils.set_global_atts(cube, attrs)
    utils.save_variable(cube,
                        cmor_info.short_name,
                        out_dir,
                        attrs,
                        unlimited_dimensions=['time'])
コード例 #3
0
ファイル: test_mask.py プロジェクト: xiajz/ESMValCore
    def test_components_fx_dict(self, tmp_path):
        """Test compatibility of input fx dictionary."""
        sftlf_file = str(tmp_path / 'sftlf_mask.nc')
        iris.save(self.fx_mask, sftlf_file)
        new_cube_land = iris.cube.Cube(self.new_cube_data,
                                       dim_coords_and_dims=self.coords_spec)
        result_land = mask_landsea(
            new_cube_land,
            {
                'sftlf': sftlf_file,
                'sftof': [],
            },
            'land',
        )
        assert isinstance(result_land, iris.cube.Cube)

        sftgif_file = str(tmp_path / 'sftgif_mask.nc')
        iris.save(self.fx_mask, sftgif_file)
        new_cube_ice = iris.cube.Cube(self.new_cube_data,
                                      dim_coords_and_dims=self.coords_spec)
        result_ice = mask_landseaice(
            new_cube_ice,
            {
                'sftgif': sftgif_file,
                'sftof': [],
            },
            'ice',
        )
        assert isinstance(result_ice, iris.cube.Cube)
コード例 #4
0
ファイル: test_mask.py プロジェクト: senesis/ESMValCore
    def test_components_fx_var(self, tmp_path):
        """Test compatibility of ancillary variables."""
        self.fx_mask.var_name = 'sftlf'
        self.fx_mask.standard_name = 'land_area_fraction'
        sftlf_file = str(tmp_path / 'sftlf_mask.nc')
        iris.save(self.fx_mask, sftlf_file)
        fx_vars = {
            'sftlf': {
                'short_name': 'sftlf',
                'project': 'CMIP6',
                'dataset': 'EC-Earth3',
                'mip': 'fx',
                'frequency': 'fx',
                'filename': sftlf_file
            }
        }
        new_cube_land = iris.cube.Cube(self.new_cube_data,
                                       dim_coords_and_dims=self.coords_spec)
        new_cube_land = add_fx_variables(new_cube_land, fx_vars,
                                         CheckLevels.IGNORE)
        result_land = mask_landsea(
            new_cube_land,
            'land',
        )
        assert isinstance(result_land, iris.cube.Cube)

        self.fx_mask.var_name = 'sftgif'
        self.fx_mask.standard_name = 'land_ice_area_fraction'
        sftgif_file = str(tmp_path / 'sftgif_mask.nc')
        iris.save(self.fx_mask, sftgif_file)
        fx_vars = {
            'sftgif': {
                'short_name': 'sftgif',
                'project': 'CMIP6',
                'dataset': 'EC-Earth3',
                'mip': 'fx',
                'frequency': 'fx',
                'filename': sftlf_file
            }
        }
        new_cube_ice = iris.cube.Cube(self.new_cube_data,
                                      dim_coords_and_dims=self.coords_spec)
        new_cube_ice = add_fx_variables(new_cube_ice, fx_vars,
                                        CheckLevels.IGNORE)
        result_ice = mask_landseaice(
            new_cube_ice,
            'ice',
        )
        assert isinstance(result_ice, iris.cube.Cube)
コード例 #5
0
def run_mask_landsea(
        cube,
        settings: typing.Dict,
        **kwargs
):
    fx_variables = getattr(
        settings,
        "fx_variables",
        {
            "sftlf": [],
            "sftof": []
        }
    )
    mask_out = settings["mask_out"]
    always_use_ne_mask = settings.get("always_use_ne_mask", False)

    cubes = mask_landsea(
        cube,
        fx_variables=fx_variables,
        mask_out=mask_out,
        always_use_ne_mask=always_use_ne_mask,
    )
    return cubes
コード例 #6
0
ファイル: test_mask.py プロジェクト: xiajz/ESMValCore
    def test_mask_landsea(self, tmp_path):
        """Test mask_landsea func."""
        sftlf_file = str(tmp_path / 'sftlf_mask.nc')
        iris.save(self.fx_mask, sftlf_file)
        new_cube_land = iris.cube.Cube(self.new_cube_data,
                                       dim_coords_and_dims=self.coords_spec)
        new_cube_sea = iris.cube.Cube(self.new_cube_data,
                                      dim_coords_and_dims=self.coords_spec)

        # mask with fx files
        result_land = mask_landsea(
            new_cube_land,
            {'sftlf': sftlf_file},
            'land',
        )
        result_sea = mask_landsea(
            new_cube_sea,
            {'sftlf': sftlf_file},
            'sea',
        )
        expected = np.ma.empty((3, 3))
        expected.data[:] = 200.
        expected.mask = np.ones((3, 3), bool)
        expected.mask[1, 2] = False
        # set fillvalues so we are sure they are equal
        np.ma.set_fill_value(result_land.data, 1e+20)
        np.ma.set_fill_value(result_sea.data, 1e+20)
        np.ma.set_fill_value(expected, 1e+20)
        assert_array_equal(result_land.data, expected)
        expected.mask = np.zeros((3, 3), bool)
        expected.mask[1, 2] = True
        assert_array_equal(result_sea.data, expected)

        # Mask with shp files although sftlf is available
        new_cube_land = iris.cube.Cube(self.new_cube_data,
                                       dim_coords_and_dims=self.coords_spec)
        new_cube_sea = iris.cube.Cube(self.new_cube_data,
                                      dim_coords_and_dims=self.coords_spec)
        result_land = mask_landsea(
            new_cube_land,
            {'sftlf': sftlf_file},
            'land',
            always_use_ne_mask=True,
        )
        result_sea = mask_landsea(
            new_cube_sea,
            {'sftlf': sftlf_file},
            'sea',
            always_use_ne_mask=True,
        )

        # Bear in mind all points are in the ocean
        np.ma.set_fill_value(result_land.data, 1e+20)
        np.ma.set_fill_value(result_sea.data, 1e+20)
        expected.mask = np.zeros((3, 3), bool)
        assert_array_equal(result_land.data, expected)
        expected.mask = np.ones((3, 3), bool)
        assert_array_equal(result_sea.data, expected)

        # mask with shp files
        new_cube_land = iris.cube.Cube(self.new_cube_data,
                                       dim_coords_and_dims=self.coords_spec)
        new_cube_sea = iris.cube.Cube(self.new_cube_data,
                                      dim_coords_and_dims=self.coords_spec)
        result_land = mask_landsea(new_cube_land, {}, 'land')
        result_sea = mask_landsea(new_cube_sea, {}, 'sea')

        # bear in mind all points are in the ocean
        np.ma.set_fill_value(result_land.data, 1e+20)
        np.ma.set_fill_value(result_sea.data, 1e+20)
        expected.mask = np.zeros((3, 3), bool)
        assert_array_equal(result_land.data, expected)
        expected.mask = np.ones((3, 3), bool)
        assert_array_equal(result_sea.data, expected)
コード例 #7
0
ファイル: test_mask.py プロジェクト: senesis/ESMValCore
    def test_mask_landsea(self, tmp_path):
        """Test mask_landsea func."""
        self.fx_mask.var_name = 'sftlf'
        self.fx_mask.standard_name = 'land_area_fraction'
        sftlf_file = str(tmp_path / 'sftlf_mask.nc')
        iris.save(self.fx_mask, sftlf_file)
        fx_vars = {
            'sftlf': {
                'short_name': 'sftlf',
                'project': 'CMIP6',
                'dataset': 'EC-Earth3',
                'mip': 'fx',
                'frequency': 'fx',
                'filename': sftlf_file
            }
        }
        new_cube_land = iris.cube.Cube(self.new_cube_data,
                                       dim_coords_and_dims=self.coords_spec)
        new_cube_land = add_fx_variables(new_cube_land, fx_vars,
                                         CheckLevels.IGNORE)
        new_cube_sea = iris.cube.Cube(self.new_cube_data,
                                      dim_coords_and_dims=self.coords_spec)
        new_cube_sea = add_fx_variables(new_cube_sea, fx_vars,
                                        CheckLevels.IGNORE)

        # mask with fx files
        result_land = mask_landsea(
            new_cube_land,
            'land',
        )
        result_sea = mask_landsea(
            new_cube_sea,
            'sea',
        )
        expected = np.ma.empty((3, 3))
        expected.data[:] = 200.
        expected.mask = np.ones((3, 3), bool)
        expected.mask[1, 2] = False
        # set fillvalues so we are sure they are equal
        np.ma.set_fill_value(result_land.data, 1e+20)
        np.ma.set_fill_value(result_sea.data, 1e+20)
        np.ma.set_fill_value(expected, 1e+20)
        assert_array_equal(result_land.data, expected)
        expected.mask = np.zeros((3, 3), bool)
        expected.mask[1, 2] = True
        assert_array_equal(result_sea.data, expected)

        # Mask with shp files although sftlf is available
        new_cube_land = iris.cube.Cube(self.new_cube_data,
                                       dim_coords_and_dims=self.coords_spec)
        new_cube_land = add_fx_variables(new_cube_land, fx_vars,
                                         CheckLevels.IGNORE)
        new_cube_sea = iris.cube.Cube(self.new_cube_data,
                                      dim_coords_and_dims=self.coords_spec)
        new_cube_sea = add_fx_variables(new_cube_sea, fx_vars,
                                        CheckLevels.IGNORE)
        result_land = mask_landsea(
            new_cube_land,
            'land',
            always_use_ne_mask=True,
        )
        result_sea = mask_landsea(
            new_cube_sea,
            'sea',
            always_use_ne_mask=True,
        )

        # Bear in mind all points are in the ocean
        np.ma.set_fill_value(result_land.data, 1e+20)
        np.ma.set_fill_value(result_sea.data, 1e+20)
        expected.mask = np.zeros((3, 3), bool)
        assert_array_equal(result_land.data, expected)
        expected.mask = np.ones((3, 3), bool)
        assert_array_equal(result_sea.data, expected)

        # mask with shp files
        new_cube_land = iris.cube.Cube(self.new_cube_data,
                                       dim_coords_and_dims=self.coords_spec)
        new_cube_sea = iris.cube.Cube(self.new_cube_data,
                                      dim_coords_and_dims=self.coords_spec)
        result_land = mask_landsea(new_cube_land, 'land')
        result_sea = mask_landsea(new_cube_sea, 'sea')

        # bear in mind all points are in the ocean
        np.ma.set_fill_value(result_land.data, 1e+20)
        np.ma.set_fill_value(result_sea.data, 1e+20)
        expected.mask = np.zeros((3, 3), bool)
        assert_array_equal(result_land.data, expected)
        expected.mask = np.ones((3, 3), bool)
        assert_array_equal(result_sea.data, expected)