satpy.tests.utils module

Utilities for various satpy tests.

class satpy.tests.utils.CustomScheduler(max_computes=1)[source]

Bases: object

Scheduler raising an exception if data are computed too many times.

Set starting and maximum compute counts.

class satpy.tests.utils.FakeCompositor(name, common_channel_mask=True, **kwargs)[source]

Bases: GenericCompositor

Act as a compositor that produces fake RGB data.

Collect custom configuration values.

Parameters:

common_channel_mask (bool) – If True, mask all the channels with a mask that combines all the invalid areas of the given data.

class satpy.tests.utils.FakeFileHandler(filename, filename_info, filetype_info, **kwargs)[source]

Bases: BaseFileHandler

Fake file handler to be used by test readers.

Initialize file handler and accept all keyword arguments.

available_datasets(configured_datasets=None)[source]

Report YAML datasets available unless ‘not_available’ is specified during creation.

property end_time

Get static end time datetime object.

get_dataset(data_id: DataID, ds_info: dict)[source]

Get fake DataArray for testing.

property sensor_names

Get sensor name from filetype configuration.

property start_time

Get static start time datetime object.

class satpy.tests.utils.FakeModifier(name, prerequisites=None, optional_prerequisites=None, **kwargs)[source]

Bases: ModifierBase

Act as a modifier that performs different modifications.

Initialise the compositor.

_handle_res_change(datasets, info)[source]
satpy.tests.utils._compare_nonarray(val1: Any, val2: Any) None[source]
satpy.tests.utils._compare_numpy_array(val1: ndarray, val2: ndarray) None[source]
satpy.tests.utils._filter_datasets(all_ds, names_or_ids)[source]

Help filtering DataIDs by name or DataQuery.

satpy.tests.utils._get_did_for_fake_scene(area, arr, extra_attrs, daskify)[source]

Add instance to fake scene. Helper for make_fake_scene.

satpy.tests.utils._get_fake_scene_area(arr, area)[source]

Get area for fake scene. Helper for make_fake_scene.

satpy.tests.utils._swath_def_of_data_arrays(rows, cols)[source]
satpy.tests.utils.assert_attrs_equal(attrs, attrs_exp, tolerance=0)[source]

Test that attributes are equal.

Walks dictionary recursively. Numerical attributes are compared with the given relative tolerance.

satpy.tests.utils.assert_dict_array_equality(d1, d2)[source]

Check that dicts containing arrays are equal.

satpy.tests.utils.assert_maximum_dask_computes(max_computes=1)[source]

Context manager to make sure dask computations are not executed more than max_computes times.

satpy.tests.utils.convert_file_content_to_data_array(file_content, attrs=(), dims=('z', 'y', 'x'))[source]

Help old reader tests that still use numpy arrays.

A lot of old reader tests still use numpy arrays and depend on the “var_name/attr/attr_name” convention established before Satpy used xarray and dask. While these conventions are still used and should be supported, readers need to use xarray DataArrays instead.

If possible, new tests should be based on pure DataArray objects instead of the “var_name/attr/attr_name” style syntax provided by the utility file handlers.

Parameters:
  • file_content (dict) – Dictionary of string file keys to fake file data.

  • attrs (iterable) – Series of attributes to copy to DataArray object from file content dictionary. Defaults to no attributes.

  • dims (iterable) – Dimension names to use for resulting DataArrays. The second to last dimension is used for 1D arrays, so for dims of ('z', 'y', 'x') this would use 'y'. Otherwise, the dimensions are used starting with the last, so 2D arrays are ('y', 'x') Dimensions are used in reverse order so the last dimension specified is used as the only dimension for 1D arrays and the last dimension for other arrays.

satpy.tests.utils.make_cid(**items)[source]

Make a DataID with a minimal set of keys to id composites.

satpy.tests.utils.make_dataid(**items)[source]

Make a DataID with default keys.

satpy.tests.utils.make_dsq(**items)[source]

Make a dataset query.

satpy.tests.utils.make_fake_scene(content_dict, daskify=False, area=True, common_attrs=None)[source]

Create a fake Scene.

Create a fake Scene object from fake data. Data are provided in the content_dict argument. In content_dict, keys should be strings or DataID, and values may be either numpy.ndarray or xarray.DataArray, in either case with exactly two dimensions. The function will convert each of the numpy.ndarray objects into an xarray.DataArray and assign those as datasets to a Scene object. A fake AreaDefinition will be assigned for each array, unless disabled by passing area=False. When areas are automatically generated, arrays with the same shape will get the same area.

This function is exclusively intended for testing purposes.

If regular ndarrays are passed and the keyword argument daskify is True, DataArrays will be created as dask arrays. If False (default), regular DataArrays will be created. When the user passes xarray.DataArray objects then this flag has no effect.

Parameters:
  • content_dict (Mapping) – Mapping where keys correspond to objects accepted by Scene.__setitem__, i.e. strings or DataID, and values may be either numpy.ndarray or xarray.DataArray.

  • daskify (bool) – optional, to use dask when converting numpy.ndarray to xarray.DataArray. No effect when the values in content_dict are already xarray.DataArray.

  • area (bool or BaseDefinition) – Can be True, False, or an instance of pyresample.geometry.BaseDefinition such as AreaDefinition or SwathDefinition. If True, which is the default, automatically generate areas with the name “test-area”. If False, values will not have assigned areas. If an instance of pyresample.geometry.BaseDefinition, those instances will be used for all generated fake datasets. Warning: Passing an area as a string (area="germ") is not supported.

  • common_attrs (Mapping) – optional, additional attributes that will be added to every dataset in the scene.

Returns:

Scene object with datasets corresponding to content_dict.

satpy.tests.utils.spy_decorator(method_to_decorate)[source]

Fancy decorator to wrap an object while still calling it.

See https://stackoverflow.com/a/41599695/433202

satpy.tests.utils.xfail_h5py_unstable_numpy2()[source]

Determine if h5py-based tests should be xfail in the unstable numpy 2.x environment.

satpy.tests.utils.xfail_skyfield_unstable_numpy2()[source]

Determine if skyfield-based tests should be xfail in the unstable numpy 2.x environment.