diff --git a/satpy/tests/reader_tests/_li_test_utils.py b/satpy/tests/reader_tests/_li_test_utils.py index 32107006fc..b656decb89 100644 --- a/satpy/tests/reader_tests/_li_test_utils.py +++ b/satpy/tests/reader_tests/_li_test_utils.py @@ -20,6 +20,7 @@ import xarray as xr from satpy.tests.reader_tests.test_netcdf_utils import FakeNetCDF4FileHandler +from satpy.tests.utils import RANDOM_GEN # mapping of netcdf type code to numpy data type: TYPE_MAP = { @@ -44,7 +45,7 @@ def l2_le_schema(settings=None): nfilters = settings.get("num_filters", 2) def rand_u16(num): - return np.random.randint(low=0, high=np.iinfo(np.uint16).max - 1, size=num, dtype=np.uint16) + return RANDOM_GEN.integers(low=0, high=np.iinfo(np.uint16).max - 1, size=num, dtype=np.uint16) return { "providers": settings.get("providers", {}), @@ -100,7 +101,7 @@ def rand_u16(num): "scale_factor": 0.004, "add_offset": 0.0, "long_name": "L2 filter results", - "default_data": lambda: np.random.randint(low=0, high=255, size=(nobs, nfilters), dtype=np.uint8) + "default_data": lambda: RANDOM_GEN.integers(low=0, high=255, size=(nobs, nfilters), dtype=np.uint8) }, "epoch_time": { "format": "f8", @@ -212,13 +213,13 @@ def l2_lef_schema(settings=None): "long_name": "Radiance of Flash", "standard_name": "radiance", "units": "mW.m-2.sr-1", - "default_data": lambda: np.clip(np.round(np.random.normal(500, 100, nobs)), 1, 2 ** 16 - 1) + "default_data": lambda: np.clip(np.round(RANDOM_GEN.normal(500, 100, nobs)), 1, 2 ** 16 - 1) }, "event_filter_qa": { "format": "u1", "shape": ("events",), "long_name": "L2 event pre-filtering quality assurance value", - "default_data": lambda: np.random.randint(1, 2 ** 8 - 1, nobs) + "default_data": lambda: RANDOM_GEN.integers(1, 2 ** 8 - 1, nobs) }, "epoch_time": { "format": "f8", @@ -232,21 +233,21 @@ def l2_lef_schema(settings=None): "shape": ("events",), "long_name": "Time offset from epoch time", "units": "seconds", - "default_data": lambda: np.random.uniform(1, 2 ** 31 - 1, nobs) + "default_data": lambda: RANDOM_GEN.uniform(1, 2 ** 31 - 1, nobs) }, "detector_row": { "format": "u2", "shape": ("events",), "long_name": "Detector row position of event pixel", "units": "1", - "default_data": lambda: np.random.randint(1, 1000, nobs) + "default_data": lambda: RANDOM_GEN.integers(1, 1000, nobs) }, "detector_column": { "format": "u2", "shape": ("events",), "long_name": "Detector column position of event pixel", "units": "1", - "default_data": lambda: np.random.randint(1, 1000, nobs) + "default_data": lambda: RANDOM_GEN.integers(1, 1000, nobs) }, } } @@ -328,7 +329,7 @@ def l2_lfl_schema(settings=None): "long_name": "Radiance of Flash", "standard_name": "radiance", "units": "mW.m-2.sr-1", - "default_data": lambda: np.round(np.random.normal(500, 100, nobs)) + "default_data": lambda: np.round(RANDOM_GEN.normal(500, 100, nobs)) }, "flash_duration": { "format": "u2", @@ -343,7 +344,7 @@ def l2_lfl_schema(settings=None): "shape": ("flashes",), "long_name": "L2 filtered flash confidence", "standard_name": "flash_filter_confidence", - "default_data": lambda: np.clip(np.round(np.random.normal(20, 10, nobs)), 1, 2 ** 7 - 1) + "default_data": lambda: np.clip(np.round(RANDOM_GEN.normal(20, 10, nobs)), 1, 2 ** 7 - 1) }, "flash_footprint": { "format": "u2", @@ -351,7 +352,7 @@ def l2_lfl_schema(settings=None): "long_name": "Flash footprint size", "standard_name": "flash_footprint", "units": "L1 grid pixels", - "default_data": lambda: np.maximum(1, np.round(np.random.normal(5, 3, nobs))) + "default_data": lambda: np.maximum(1, np.round(RANDOM_GEN.normal(5, 3, nobs))) }, "flash_id": { "format": "u4", @@ -367,7 +368,7 @@ def l2_lfl_schema(settings=None): "units": "seconds since 2000-01-01 00:00:00.0", "standard_name": "time", "precision": "1 millisecond", - "default_data": lambda: np.random.uniform(stime, etime, nobs) + "default_data": lambda: RANDOM_GEN.uniform(stime, etime, nobs) }, "l1b_geolocation_warning": { "format": "i1", @@ -437,7 +438,7 @@ def l2_af_schema(settings=None): "flash_accumulation": { "format": "u2", "shape": ("pixels",), - "default_data": lambda: np.clip(np.round(np.random.normal(1, 2, nobs)), 1, 2 ** 16 - 1) + "default_data": lambda: np.clip(np.round(RANDOM_GEN.normal(1, 2, nobs)), 1, 2 ** 16 - 1) }, "mtg_geos_projection": mtg_geos_projection(), "x": fci_grid_definition("X", nobs), @@ -495,7 +496,7 @@ def l2_afr_schema(settings=None): "long_name": "Area averaged flash radiance accumulation", "grid_mapping": "mtg_geos_projection", "coordinate": "sparse: x y", - "default_data": lambda: np.random.randint(low=1, high=6548, size=(120), dtype=np.int16) + "default_data": lambda: RANDOM_GEN.integers(low=1, high=6548, size=(120), dtype=np.int16) }, "accumulation_start_times": { "format": "f4", @@ -538,7 +539,7 @@ def fci_grid_definition(axis, nobs): "standard_name": standard_name, "units": "radian", "valid_range": np.asarray([1, 5568]), - "default_data": lambda: np.clip(np.round(np.random.normal(2000, 500, nobs)), 1, 2 ** 16 - 1) + "default_data": lambda: np.clip(np.round(RANDOM_GEN.normal(2000, 500, nobs)), 1, 2 ** 16 - 1) } diff --git a/satpy/tests/reader_tests/test_epic_l1b_h5.py b/satpy/tests/reader_tests/test_epic_l1b_h5.py index 472cda7f2d..1ff4c76c07 100644 --- a/satpy/tests/reader_tests/test_epic_l1b_h5.py +++ b/satpy/tests/reader_tests/test_epic_l1b_h5.py @@ -25,14 +25,15 @@ import pytest from satpy.readers.epic_l1b_h5 import CALIB_COEFS - -b317_data = np.random.uniform(low=0, high=5200, size=(100, 100)) -b688_data = np.random.uniform(low=0, high=5200, size=(100, 100)) -sza_data = np.random.uniform(low=0, high=100, size=(100, 100)) -vaa_data = np.random.uniform(low=-180, high=180, size=(100, 100)) -lon_data = np.random.uniform(low=-90, high=90, size=(100, 100)) -lat_data = np.random.uniform(low=-180, high=180, size=(100, 100)) -mas_data = np.random.choice([0, 1], size=(100, 100)) +from satpy.tests.utils import RANDOM_GEN + +b317_data = RANDOM_GEN.uniform(low=0, high=5200, size=(100, 100)) +b688_data = RANDOM_GEN.uniform(low=0, high=5200, size=(100, 100)) +sza_data = RANDOM_GEN.uniform(low=0, high=100, size=(100, 100)) +vaa_data = RANDOM_GEN.uniform(low=-180, high=180, size=(100, 100)) +lon_data = RANDOM_GEN.uniform(low=-90, high=90, size=(100, 100)) +lat_data = RANDOM_GEN.uniform(low=-180, high=180, size=(100, 100)) +mas_data = RANDOM_GEN.choice([0, 1], size=(100, 100)) @pytest.fixture() diff --git a/satpy/tests/reader_tests/test_generic_image.py b/satpy/tests/reader_tests/test_generic_image.py index cd347ce07e..f7a27097a8 100644 --- a/satpy/tests/reader_tests/test_generic_image.py +++ b/satpy/tests/reader_tests/test_generic_image.py @@ -24,7 +24,7 @@ import pytest import xarray as xr -from satpy.tests.utils import make_dataid +from satpy.tests.utils import RANDOM_GEN, make_dataid class TestGenericImage(unittest.TestCase): @@ -62,7 +62,7 @@ def setUp(self): a__[:10, :10] = 0 a__ = da.from_array(a__, chunks=(50, 50)) - r_nan__ = np.random.uniform(0., 1., size=(self.y_size, self.x_size)) + r_nan__ = RANDOM_GEN.uniform(0., 1., size=(self.y_size, self.x_size)) r_nan__[:10, :10] = np.nan r_nan__ = da.from_array(r_nan__, chunks=(50, 50)) diff --git a/satpy/tests/reader_tests/test_goci2_l2_nc.py b/satpy/tests/reader_tests/test_goci2_l2_nc.py index 865ac3184e..da54e11848 100644 --- a/satpy/tests/reader_tests/test_goci2_l2_nc.py +++ b/satpy/tests/reader_tests/test_goci2_l2_nc.py @@ -24,6 +24,7 @@ from pytest_lazyfixture import lazy_fixture from satpy import Scene +from satpy.tests.utils import RANDOM_GEN # NOTE: # The following fixtures are not defined in this file, but are used and injected by Pytest: @@ -78,7 +79,7 @@ def _create_bad_lon_lat(): @pytest.fixture(scope="session") def ac_file(tmp_path_factory): """Create a fake atmospheric correction product.""" - data = np.random.random((10, 10)) + data = RANDOM_GEN.random((10, 10)) RhoC = xr.Dataset( {"RhoC_555": (["number_of_lines", "pixels_per_line"], data)}, coords={"number_of_lines": np.arange(10), "pixels_per_line": np.arange(10)}, @@ -102,7 +103,7 @@ def ac_file(tmp_path_factory): @pytest.fixture(scope="module") def iop_file(tmp_path_factory): """Create a fake IOP product.""" - data = np.random.random((10, 10)) + data = RANDOM_GEN.random((10, 10)) a = xr.Dataset( {"a_total_555": (["number_of_lines", "pixels_per_line"], data)}, coords={"number_of_lines": np.arange(10), "pixels_per_line": np.arange(10)}, @@ -124,7 +125,7 @@ def iop_file(tmp_path_factory): @pytest.fixture(scope="module") def generic_file(tmp_path_factory): """Create a fake ouput product like Chl, Zsd etc.""" - data = np.random.random((10, 10)) + data = RANDOM_GEN.random((10, 10)) geophysical_data = xr.Dataset( {"Chl": (["number_of_lines", "pixels_per_line"], data)}, coords={"number_of_lines": np.arange(10), "pixels_per_line": np.arange(10)}, @@ -141,7 +142,7 @@ def generic_file(tmp_path_factory): @pytest.fixture(scope="module") def generic_bad_file(tmp_path_factory): """Create a PP product with lon/lat base name missing.""" - data = np.random.random((10, 10)) + data = RANDOM_GEN.random((10, 10)) geophysical_data = xr.Dataset( {"PP": (["number_of_lines", "pixels_per_line"], data)}, coords={"number_of_lines": np.arange(10), "pixels_per_line": np.arange(10)}, diff --git a/satpy/tests/reader_tests/test_hrit_base.py b/satpy/tests/reader_tests/test_hrit_base.py index 12317f11f1..eb4a073e02 100644 --- a/satpy/tests/reader_tests/test_hrit_base.py +++ b/satpy/tests/reader_tests/test_hrit_base.py @@ -30,6 +30,7 @@ from satpy.readers import FSFile from satpy.readers.hrit_base import HRITFileHandler, decompress, get_xritdecompress_cmd, get_xritdecompress_outfile +from satpy.tests.utils import RANDOM_GEN # NOTE: # The following fixtures are not defined in this file, but are used and injected by Pytest: @@ -145,9 +146,9 @@ def create_stub_hrit(filename, open_fun=open, meta=mda): lines = meta["number_of_lines"] cols = meta["number_of_columns"] total_bits = lines * cols * nbits - arr = np.random.randint(0, 256, - size=int(total_bits / 8), - dtype=np.uint8) + arr = RANDOM_GEN.integers(0, 256, + size=int(total_bits / 8), + dtype=np.uint8) with open_fun(filename, mode="wb") as fd: fd.write(b" " * meta["total_header_length"]) bytes_data = arr.tobytes() diff --git a/satpy/tests/reader_tests/test_insat3d_img_l1b_h5.py b/satpy/tests/reader_tests/test_insat3d_img_l1b_h5.py index 9fa7af224d..262e832f6a 100644 --- a/satpy/tests/reader_tests/test_insat3d_img_l1b_h5.py +++ b/satpy/tests/reader_tests/test_insat3d_img_l1b_h5.py @@ -16,7 +16,7 @@ open_dataset, open_datatree, ) -from satpy.tests.utils import make_dataid +from satpy.tests.utils import RANDOM_GEN, make_dataid # NOTE: # The following fixtures are not defined in this file, but are used and injected by Pytest: @@ -30,10 +30,10 @@ alb_units = "%" temp_units = "K" chunks_1km = (1, 46, 1126) -values_1km = np.random.randint(0, 1000, shape_1km, dtype=np.uint16) +values_1km = RANDOM_GEN.integers(0, 1000, shape_1km, dtype=np.uint16) values_1km[0, 0, 0] = 0 -values_4km = np.random.randint(0, 1000, shape_4km, dtype=np.uint16) -values_8km = np.random.randint(0, 1000, shape_8km, dtype=np.uint16) +values_4km = RANDOM_GEN.integers(0, 1000, shape_4km, dtype=np.uint16) +values_8km = RANDOM_GEN.integers(0, 1000, shape_8km, dtype=np.uint16) values_by_resolution = {1000: values_1km, 4000: values_4km, diff --git a/satpy/tests/reader_tests/test_mirs.py b/satpy/tests/reader_tests/test_mirs.py index b857147e47..3aeaacb2b0 100644 --- a/satpy/tests/reader_tests/test_mirs.py +++ b/satpy/tests/reader_tests/test_mirs.py @@ -31,6 +31,7 @@ from satpy.dataset import DataID from satpy.readers import load_reader from satpy.readers.yaml_reader import FileYAMLReader +from satpy.tests.utils import RANDOM_GEN METOP_FILE = "IMG_SX.M2.D17037.S1601.E1607.B0000001.WE.HR.ORB.nc" NPP_MIRS_L2_SWATH = "NPR-MIRS-IMG_v11r6_npp_s201702061601000_e201702061607000_c202012201658410.nc" @@ -77,7 +78,7 @@ def fake_coeff_from_fn(fn): """Create Fake Coefficients.""" - ameans = np.random.uniform(261, 267, N_CHANNEL) + ameans = RANDOM_GEN.uniform(261, 267, N_CHANNEL) locations = [ [1, 2], [1, 2], @@ -117,7 +118,7 @@ def fake_coeff_from_fn(fn): str_coeff = " ".join([str(x) for x in random_coeff]) random_means = np.zeros(all_nchx[nx]) str_means = " ".join([str(x) for x in random_means]) - error_val = np.random.uniform(0, 4) + error_val = RANDOM_GEN.uniform(0, 4) coeffs_line = " {:>2} {:>2} {} {} {}\n".format(idx, fov, str_coeff, str_means, @@ -138,7 +139,7 @@ def _get_datasets_with_attributes(**kwargs): "_FillValue": -999, "valid_range": [0, 50000]}, dims=("Scanline", "Field_of_view", "Channel")) - rr = xr.DataArray(np.random.randint(100, 500, size=(N_SCANLINE, N_FOV), dtype=np.int16), + rr = xr.DataArray(RANDOM_GEN.integers(100, 500, size=(N_SCANLINE, N_FOV), dtype=np.int16), attrs={"long_name": "Rain Rate (mm/hr)", "units": "mm/hr", "coordinates": "Longitude Latitude", @@ -146,7 +147,7 @@ def _get_datasets_with_attributes(**kwargs): "_FillValue": -999, "valid_range": [0, 1000]}, dims=("Scanline", "Field_of_view")) - sfc_type = xr.DataArray(np.random.randint(0, 4, size=(N_SCANLINE, N_FOV), dtype=np.int16), + sfc_type = xr.DataArray(RANDOM_GEN.integers(0, 4, size=(N_SCANLINE, N_FOV), dtype=np.int16), attrs={"description": "type of surface:0-ocean," + "1-sea ice,2-land,3-snow", "units": "1", @@ -187,12 +188,12 @@ def _get_datasets_with_less_attributes(): attrs={"long_name": "Channel Temperature (K)", "scale_factor": 0.01}, dims=("Scanline", "Field_of_view", "Channel")) - rr = xr.DataArray(np.random.randint(100, 500, size=(N_SCANLINE, N_FOV), dtype=np.int16), + rr = xr.DataArray(RANDOM_GEN.integers(100, 500, size=(N_SCANLINE, N_FOV), dtype=np.int16), attrs={"long_name": "Rain Rate (mm/hr)", "scale_factor": 0.1}, dims=("Scanline", "Field_of_view")) - sfc_type = xr.DataArray(np.random.randint(0, 4, size=(N_SCANLINE, N_FOV), dtype=np.int16), + sfc_type = xr.DataArray(RANDOM_GEN.integers(0, 4, size=(N_SCANLINE, N_FOV), dtype=np.int16), attrs={"description": "type of surface:0-ocean," + "1-sea ice,2-land,3-snow"}, dims=("Scanline", "Field_of_view")) diff --git a/satpy/tests/reader_tests/test_nwcsaf_msg.py b/satpy/tests/reader_tests/test_nwcsaf_msg.py index 1c8e0fb793..1a9b2ca3bf 100644 --- a/satpy/tests/reader_tests/test_nwcsaf_msg.py +++ b/satpy/tests/reader_tests/test_nwcsaf_msg.py @@ -26,12 +26,13 @@ import pytest from satpy.tests.reader_tests.utils import fill_h5 +from satpy.tests.utils import RANDOM_GEN -CTYPE_TEST_ARRAY = (np.random.rand(1856, 3712) * 255).astype(np.uint8) +CTYPE_TEST_ARRAY = (RANDOM_GEN.random((1856, 3712)) * 255).astype(np.uint8) CTYPE_TEST_FRAME = (np.arange(100).reshape(10, 10) / 100. * 20).astype(np.uint8) CTYPE_TEST_ARRAY[1000:1010, 1000:1010] = CTYPE_TEST_FRAME -CTTH_HEIGHT_TEST_ARRAY = (np.random.rand(1856, 3712) * 255).astype(np.uint8) +CTTH_HEIGHT_TEST_ARRAY = (RANDOM_GEN.random((1856, 3712)) * 255).astype(np.uint8) _CTTH_HEIGHT_TEST_FRAME = (np.arange(100).reshape(10, 10) / 100. * 80).astype(np.uint8) CTTH_HEIGHT_TEST_ARRAY[1000:1010, 1000:1010] = _CTTH_HEIGHT_TEST_FRAME @@ -39,7 +40,7 @@ CTTH_HEIGHT_TEST_FRAME_RES[0, 0:10] = np.nan CTTH_HEIGHT_TEST_FRAME_RES[1, 0:3] = np.nan -CTTH_PRESSURE_TEST_ARRAY = (np.random.rand(1856, 3712) * 255).astype(np.uint8) +CTTH_PRESSURE_TEST_ARRAY = (RANDOM_GEN.random((1856, 3712)) * 255).astype(np.uint8) _CTTH_PRESSURE_TEST_FRAME = (np.arange(100).reshape(10, 10) / 100. * 54).astype(np.uint8) CTTH_PRESSURE_TEST_ARRAY[1000:1010, 1000:1010] = _CTTH_PRESSURE_TEST_FRAME @@ -47,7 +48,7 @@ CTTH_PRESSURE_TEST_FRAME_RES[0, 0:10] = np.nan CTTH_PRESSURE_TEST_FRAME_RES[1, 0:9] = np.nan -CTTH_TEMPERATURE_TEST_ARRAY = (np.random.rand(1856, 3712) * 255).astype(np.uint8) +CTTH_TEMPERATURE_TEST_ARRAY = (RANDOM_GEN.random((1856, 3712)) * 255).astype(np.uint8) _CTTH_TEMPERATURE_TEST_FRAME = (np.arange(100).reshape(10, 10) / 100. * 140).astype(np.uint8) _CTTH_TEMPERATURE_TEST_FRAME[8, 5] = 255 CTTH_TEMPERATURE_TEST_ARRAY[1000:1010, 1000:1010] = _CTTH_TEMPERATURE_TEST_FRAME @@ -130,7 +131,7 @@ "PRODUCT": b"CT__", "SCALING_FACTOR": 1.0, }, - "value": (np.random.rand(1856, 3712) * 255).astype(np.uint8), + "value": (RANDOM_GEN.random((1856, 3712)) * 255).astype(np.uint8), }, "CT_QUALITY": { "attrs": { @@ -145,7 +146,7 @@ "PRODUCT": b"CT__", "SCALING_FACTOR": 1.0, }, - "value": (np.random.rand(1856, 3712) * 65535).astype(np.uint16), + "value": (RANDOM_GEN.random((1856, 3712)) * 65535).astype(np.uint16), }, "attrs": { "CFAC": 13642337, @@ -255,7 +256,7 @@ "PAL_COLORMODEL": b"RGB", "PAL_TYPE": b"DIRECTINDEX", }, - "value": (np.random.rand(128, 3) * 255).astype(np.uint8), + "value": (RANDOM_GEN.random((128, 3)) * 255).astype(np.uint8), }, "03-PALETTE": { "attrs": { @@ -263,7 +264,7 @@ "PAL_COLORMODEL": b"RGB", "PAL_TYPE": b"DIRECTINDEX", }, - "value": (np.random.rand(256, 3) * 255).astype(np.uint8), + "value": (RANDOM_GEN.random((256, 3)) * 255).astype(np.uint8), }, "04-PALETTE": { "attrs": { @@ -323,7 +324,7 @@ "PRODUCT": b"CTTH", "SCALING_FACTOR": 5.0, }, - "value": (np.random.rand(1856, 3712) * 255).astype(np.uint8), + "value": (RANDOM_GEN.random((1856, 3712)) * 255).astype(np.uint8), }, "CTTH_HEIGHT": { "attrs": { @@ -370,7 +371,7 @@ "PRODUCT": b"CTTH", "SCALING_FACTOR": 1.0, }, - "value": (np.random.rand(1856, 3712) * 65535).astype(np.uint16), + "value": (RANDOM_GEN.random((1856, 3712)) * 65535).astype(np.uint16), }, "CTTH_TEMPER": { "attrs": { diff --git a/satpy/tests/reader_tests/test_nwcsaf_nc.py b/satpy/tests/reader_tests/test_nwcsaf_nc.py index 4f6755f390..6a509f023f 100644 --- a/satpy/tests/reader_tests/test_nwcsaf_nc.py +++ b/satpy/tests/reader_tests/test_nwcsaf_nc.py @@ -22,6 +22,7 @@ import xarray as xr from satpy.readers.nwcsaf_nc import NcNWCSAF, read_nwcsaf_time +from satpy.tests.utils import RANDOM_GEN PROJ_KM = {"gdal_projection": "+proj=geos +a=6378.137000 +b=6356.752300 +lon_0=0.000000 +h=35785.863000", "gdal_xgeo_up_left": -5569500.0, @@ -83,9 +84,9 @@ COT_SCALE = 0.01 COT_OFFSET = 0.0 -CRE_ARRAY = np.random.randint(0, 65535, size=(928, 1530), dtype=np.uint16) -COT_ARRAY = np.random.randint(0, 65535, size=(928, 1530), dtype=np.uint16) -PAL_ARRAY = np.random.randint(0, 255, size=(250, 3), dtype=np.uint8) +CRE_ARRAY = RANDOM_GEN.integers(0, 65535, size=(928, 1530), dtype=np.uint16) +COT_ARRAY = RANDOM_GEN.integers(0, 65535, size=(928, 1530), dtype=np.uint16) +PAL_ARRAY = RANDOM_GEN.integers(0, 255, size=(250, 3), dtype=np.uint8) @pytest.fixture(scope="session") @@ -104,7 +105,7 @@ def create_nwcsaf_geo_ct_file(directory, attrs=global_attrs_geo): var = nc_file.create_variable(var_name, ("ny", "nx"), np.uint16, chunks=(256, 256)) - var[:] = np.random.randint(0, 255, size=(928, 1530), dtype=np.uint8) + var[:] = RANDOM_GEN.integers(0, 255, size=(928, 1530), dtype=np.uint8) return filename diff --git a/satpy/tests/reader_tests/test_seviri_l1b_hrit.py b/satpy/tests/reader_tests/test_seviri_l1b_hrit.py index 3fe00edc80..80a14e21f4 100644 --- a/satpy/tests/reader_tests/test_seviri_l1b_hrit.py +++ b/satpy/tests/reader_tests/test_seviri_l1b_hrit.py @@ -31,7 +31,7 @@ from satpy.readers.seviri_l1b_hrit import HRITMSGEpilogueFileHandler, HRITMSGFileHandler, HRITMSGPrologueFileHandler from satpy.tests.reader_tests.test_seviri_base import ORBIT_POLYNOMIALS_INVALID from satpy.tests.reader_tests.test_seviri_l1b_calibration import TestFileHandlerCalibrationBase -from satpy.tests.utils import assert_attrs_equal, make_dataid +from satpy.tests.utils import RANDOM_GEN, assert_attrs_equal, make_dataid class TestHRITMSGBase(unittest.TestCase): @@ -64,9 +64,9 @@ def setUp(self): def test_read_hrv_band(self, memmap): """Test reading the hrv band.""" nbits = self.reader.mda["number_of_bits_per_pixel"] - memmap.return_value = np.random.randint(0, 256, - size=int((464 * 5568 * nbits) / 8), - dtype=np.uint8) + memmap.return_value = RANDOM_GEN.integers(0, 256, + size=int((464 * 5568 * nbits) / 8), + dtype=np.uint8) res = self.reader.read_band("HRV", None) assert res.shape == (464, 5568) @@ -181,9 +181,9 @@ def test_get_area_def(self): def test_read_band(self, memmap): """Test reading a band.""" nbits = self.reader.mda["number_of_bits_per_pixel"] - memmap.return_value = np.random.randint(0, 256, - size=int((464 * 3712 * nbits) / 8), - dtype=np.uint8) + memmap.return_value = RANDOM_GEN.integers(0, 256, + size=int((464 * 3712 * nbits) / 8), + dtype=np.uint8) res = self.reader.read_band("VIS006", None) assert res.shape == (464, 3712) diff --git a/satpy/tests/reader_tests/test_seviri_l2_bufr.py b/satpy/tests/reader_tests/test_seviri_l2_bufr.py index ec3fdf7b56..09e3fa93d5 100644 --- a/satpy/tests/reader_tests/test_seviri_l2_bufr.py +++ b/satpy/tests/reader_tests/test_seviri_l2_bufr.py @@ -27,7 +27,7 @@ import pytest from pyresample import geometry -from satpy.tests.utils import make_dataid +from satpy.tests.utils import RANDOM_GEN, make_dataid FILETYPE_INFO = {"file_type": "seviri_l2_bufr_asr"} @@ -109,9 +109,9 @@ ] # Test data -DATA = np.random.uniform(low=250, high=350, size=(128,)) -LAT = np.random.uniform(low=-80, high=80, size=(128,)) -LON = np.random.uniform(low=-38.5, high=121.5, size=(128,)) +DATA = RANDOM_GEN.uniform(low=250, high=350, size=(128,)) +LAT = RANDOM_GEN.uniform(low=-80, high=80, size=(128,)) +LON = RANDOM_GEN.uniform(low=-38.5, high=121.5, size=(128,)) class SeviriL2BufrData: diff --git a/satpy/tests/reader_tests/test_sgli_l1b.py b/satpy/tests/reader_tests/test_sgli_l1b.py index 7f5fffa70c..7c2551a211 100644 --- a/satpy/tests/reader_tests/test_sgli_l1b.py +++ b/satpy/tests/reader_tests/test_sgli_l1b.py @@ -8,14 +8,15 @@ import pytest from satpy.readers.sgli_l1b import HDF5SGLI +from satpy.tests.utils import RANDOM_GEN START_TIME = datetime.now() END_TIME = START_TIME + timedelta(minutes=5) FULL_KM_ARRAY = np.arange(1955 * 1250, dtype=np.uint16).reshape((1955, 1250)) MASK = 16383 LON_LAT_ARRAY = np.arange(197 * 126, dtype=np.float32).reshape((197, 126)) -AZI_ARRAY = np.random.randint(-180 * 100, 180 * 100, size=(197, 126), dtype=np.int16) -ZEN_ARRAY = np.random.randint(0, 180 * 100, size=(197, 126), dtype=np.int16) +AZI_ARRAY = RANDOM_GEN.integers(-180 * 100, 180 * 100, size=(197, 126), dtype=np.int16) +ZEN_ARRAY = RANDOM_GEN.integers(0, 180 * 100, size=(197, 126), dtype=np.int16) @pytest.fixture(scope="module") diff --git a/satpy/tests/reader_tests/test_viirs_compact.py b/satpy/tests/reader_tests/test_viirs_compact.py index ba8fa6f312..f27d9d6f32 100644 --- a/satpy/tests/reader_tests/test_viirs_compact.py +++ b/satpy/tests/reader_tests/test_viirs_compact.py @@ -24,6 +24,7 @@ import pytest from satpy.tests.reader_tests.utils import fill_h5 +from satpy.tests.utils import RANDOM_GEN # NOTE: # The following fixtures are not defined in this file, but are used and injected by Pytest: @@ -647,13 +648,13 @@ def fake_dnb(): dtype=np.float32, ) }, - "Latitude": {"value": np.random.rand(96, 332).astype(np.float32)}, - "Longitude": {"value": np.random.rand(96, 332).astype(np.float32)}, + "Latitude": {"value": RANDOM_GEN.random((96, 332)).astype(np.float32)}, + "Longitude": {"value": RANDOM_GEN.random((96, 332)).astype(np.float32)}, "LunarAzimuthAngle": { - "value": np.random.rand(96, 332).astype(np.float32) + "value": RANDOM_GEN.random((96, 332)).astype(np.float32) }, "LunarZenithAngle": { - "value": np.random.rand(96, 332).astype(np.float32) + "value": RANDOM_GEN.random((96, 332)).astype(np.float32) }, "MidTime": { "value": np.array( @@ -1170,16 +1171,16 @@ def fake_dnb(): ) }, "SatelliteAzimuthAngle": { - "value": np.random.rand(96, 332).astype(np.float32) + "value": RANDOM_GEN.random((96, 332)).astype(np.float32) }, "SatelliteZenithAngle": { - "value": np.random.rand(96, 332).astype(np.float32) + "value": RANDOM_GEN.random((96, 332)).astype(np.float32) }, "SolarAzimuthAngle": { - "value": np.random.rand(96, 332).astype(np.float32) + "value": RANDOM_GEN.random((96, 332)).astype(np.float32) }, "SolarZenithAngle": { - "value": np.random.rand(96, 332).astype(np.float32) + "value": RANDOM_GEN.random((96, 332)).astype(np.float32) }, "StartTime": { "value": np.array( @@ -1484,7 +1485,7 @@ def fake_dnb(): }, "PadByte1": {"value": np.array([0, 0, 0], dtype=np.uint8)}, "QF1_VIIRSDNBSDR": { - "value": (np.random.rand(768, 4064) * 255).astype(np.uint8) + "value": (RANDOM_GEN.random((768, 4064)) * 255).astype(np.uint8) }, "QF2_SCAN_SDR": { "value": np.array( @@ -1596,7 +1597,7 @@ def fake_dnb(): dtype=np.uint8, ) }, - "Radiance": {"value": np.random.rand(768, 4064).astype(np.float32)}, + "Radiance": {"value": RANDOM_GEN.random((768, 4064)).astype(np.float32)}, "attrs": { "OriginalFilename": np.array( [ diff --git a/satpy/tests/reader_tests/test_viirs_edr.py b/satpy/tests/reader_tests/test_viirs_edr.py index d764891760..6beb3c0cab 100644 --- a/satpy/tests/reader_tests/test_viirs_edr.py +++ b/satpy/tests/reader_tests/test_viirs_edr.py @@ -36,6 +36,8 @@ from pytest import TempPathFactory # noqa: PT013 from pytest_lazyfixture import lazy_fixture +from satpy.tests.utils import RANDOM_GEN + I_COLS = 6400 I_ROWS = 32 # one scan M_COLS = 3200 @@ -135,8 +137,8 @@ def _create_surf_refl_variables() -> dict[str, xr.DataArray]: sr_attrs = {"units": "unitless", "_FillValue": -9999, "scale_factor": np.float32(0.0001), "add_offset": np.float32(0.0)} - i_data = np.random.random_sample((I_ROWS, I_COLS)).astype(np.float32) - m_data = np.random.random_sample((M_ROWS, M_COLS)).astype(np.float32) + i_data = RANDOM_GEN.random((I_ROWS, I_COLS)).astype(np.float32) + m_data = RANDOM_GEN.random((M_ROWS, M_COLS)).astype(np.float32) lon_i_data = (i_data * 360) - 180.0 lon_m_data = (m_data * 360) - 180.0 lat_i_data = (i_data * 180) - 90.0 @@ -261,7 +263,7 @@ def _create_continuous_variables(var_names: Iterable[str]) -> dict[str, xr.DataA cont_attrs = {"units": "Kelvin", "_FillValue": -9999, "scale_factor": np.float32(0.0001), "add_offset": np.float32(0.0)} - m_data = np.random.random_sample((M_ROWS, M_COLS)).astype(np.float32) + m_data = RANDOM_GEN.random((M_ROWS, M_COLS)).astype(np.float32) data_arrs = { "Longitude": xr.DataArray(m_data, dims=dims, attrs=lon_attrs), "Latitude": xr.DataArray(m_data, dims=dims, attrs=lat_attrs), diff --git a/satpy/tests/test_composites.py b/satpy/tests/test_composites.py index c075755d17..780293b1e0 100644 --- a/satpy/tests/test_composites.py +++ b/satpy/tests/test_composites.py @@ -30,7 +30,7 @@ from pyresample import AreaDefinition import satpy -from satpy.tests.utils import CustomScheduler +from satpy.tests.utils import RANDOM_GEN, CustomScheduler # NOTE: # The following fixtures are not defined in this file, but are used and injected by Pytest: @@ -701,10 +701,10 @@ def test_compositor(self, e2d, input_shape, bands): """Test luminance sharpening compositor.""" from satpy.composites import SandwichCompositor - rgb_arr = da.from_array(np.random.random(input_shape), chunks=2) + rgb_arr = da.from_array(RANDOM_GEN.random(input_shape), chunks=2) rgb = xr.DataArray(rgb_arr, dims=["bands", "y", "x"], coords={"bands": bands}) - lum_arr = da.from_array(100 * np.random.random((2, 2)), chunks=2) + lum_arr = da.from_array(100 * RANDOM_GEN.random((2, 2)), chunks=2) lum = xr.DataArray(lum_arr, dims=["y", "x"]) # Make enhance2dataset return unmodified dataset diff --git a/satpy/tests/test_modifiers.py b/satpy/tests/test_modifiers.py index 0c8eb51b3f..148ea6692f 100644 --- a/satpy/tests/test_modifiers.py +++ b/satpy/tests/test_modifiers.py @@ -27,6 +27,8 @@ from pyresample.geometry import AreaDefinition, StackedAreaDefinition from pytest_lazyfixture import lazy_fixture +from satpy.tests.utils import RANDOM_GEN + def _sunz_area_def(): """Get fake area for testing sunz generation.""" @@ -213,23 +215,23 @@ def setUp(self): "area": area, "start_time": self.start_time} - nir_arr = np.random.random((2, 2)) + nir_arr = RANDOM_GEN.random((2, 2)) self.nir = xr.DataArray(da.from_array(nir_arr), dims=["y", "x"]) self.nir.attrs.update(self.metadata) - ir_arr = 100 * np.random.random((2, 2)) + ir_arr = 100 * RANDOM_GEN.random((2, 2)) self.ir_ = xr.DataArray(da.from_array(ir_arr), dims=["y", "x"]) self.ir_.attrs["area"] = area - self.sunz_arr = 100 * np.random.random((2, 2)) + self.sunz_arr = 100 * RANDOM_GEN.random((2, 2)) self.sunz = xr.DataArray(da.from_array(self.sunz_arr), dims=["y", "x"]) self.sunz.attrs["standard_name"] = "solar_zenith_angle" self.sunz.attrs["area"] = area self.da_sunz = da.from_array(self.sunz_arr) - refl_arr = np.random.random((2, 2)) + refl_arr = RANDOM_GEN.random((2, 2)) self.refl = da.from_array(refl_arr) - self.refl_with_co2 = da.from_array(np.random.random((2, 2))) + self.refl_with_co2 = da.from_array(RANDOM_GEN.random((2, 2))) self.refl_from_tbs = mock.MagicMock() self.refl_from_tbs.side_effect = self.fake_refl_from_tbs @@ -292,7 +294,7 @@ def test_no_sunz_with_co2(self, calculator, apply_modifier_info, sza): comp = NIRReflectance(name="test") info = {"modifiers": None} - co2_arr = np.random.random((2, 2)) + co2_arr = RANDOM_GEN.random((2, 2)) co2 = xr.DataArray(da.from_array(co2_arr), dims=["y", "x"]) co2.attrs["wavelength"] = [12.0, 13.0, 14.0] co2.attrs["units"] = "K" @@ -378,14 +380,14 @@ def test_compositor(self, calculator, apply_modifier_info, sza): """Test the NIR emissive part from reflectance compositor.""" from satpy.modifiers.spectral import NIRReflectance - refl_arr = np.random.random((2, 2)) + refl_arr = RANDOM_GEN.random((2, 2)) refl = da.from_array(refl_arr) refl_from_tbs = mock.MagicMock() refl_from_tbs.return_value = refl calculator.return_value = mock.MagicMock(reflectance_from_tbs=refl_from_tbs) - emissive_arr = np.random.random((2, 2)) + emissive_arr = RANDOM_GEN.random((2, 2)) emissive = da.from_array(emissive_arr) emissive_part = mock.MagicMock() emissive_part.return_value = emissive @@ -405,17 +407,17 @@ def test_compositor(self, calculator, apply_modifier_info, sza): get_lonlats.return_value = (lons, lats) area = mock.MagicMock(get_lonlats=get_lonlats) - nir_arr = np.random.random((2, 2)) + nir_arr = RANDOM_GEN.random((2, 2)) nir = xr.DataArray(da.from_array(nir_arr), dims=["y", "x"]) nir.attrs["platform_name"] = platform nir.attrs["sensor"] = sensor nir.attrs["name"] = chan_name nir.attrs["area"] = area - ir_arr = np.random.random((2, 2)) + ir_arr = RANDOM_GEN.random((2, 2)) ir_ = xr.DataArray(da.from_array(ir_arr), dims=["y", "x"]) ir_.attrs["area"] = area - sunz_arr = 100 * np.random.random((2, 2)) + sunz_arr = 100 * RANDOM_GEN.random((2, 2)) sunz = xr.DataArray(da.from_array(sunz_arr), dims=["y", "x"]) sunz.attrs["standard_name"] = "solar_zenith_angle" sunz.attrs["area"] = area diff --git a/satpy/tests/utils.py b/satpy/tests/utils.py index a6ebf8753e..13e771418c 100644 --- a/satpy/tests/utils.py +++ b/satpy/tests/utils.py @@ -37,6 +37,8 @@ FAKE_FILEHANDLER_START = datetime(2020, 1, 1, 0, 0, 0) FAKE_FILEHANDLER_END = datetime(2020, 1, 1, 1, 0, 0) +RANDOM_GEN = np.random.default_rng() + def make_dataid(**items): """Make a DataID with default keys."""