Skip to content

Commit

Permalink
Use CRS objects as much as possible instead of suppressing the warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
pnuu committed Dec 14, 2023
1 parent 24422ad commit d8af768
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 182 deletions.
7 changes: 6 additions & 1 deletion satpy/readers/nwcsaf_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,13 @@ def get_area_def(self, dsid):
@staticmethod
def _ensure_crs_extents_in_meters(crs, area_extent):
"""Fix units in Earth shape, satellite altitude and 'units' attribute."""
import warnings
if "kilo" in crs.axis_info[0].unit_name:
proj_dict = crs.to_dict()
with warnings.catch_warnings():
# The proj dict route is the only feasible way to modify the area, suppress the warning it causes
warnings.filterwarnings("ignore", category=UserWarning,
message="You will likely lose important projection information")
proj_dict = crs.to_dict()
proj_dict["units"] = "m"
if "a" in proj_dict:
proj_dict["a"] *= 1000.
Expand Down
18 changes: 5 additions & 13 deletions satpy/tests/reader_tests/test_ahi_hsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ def test_region(self, fromfile, np2str):
@mock.patch("satpy.readers.ahi_hsd.np.fromfile")
def test_segment(self, fromfile, np2str):
"""Test segment navigation."""
from pyresample.utils import proj4_radius_parameters
from pyproj import CRS

np2str.side_effect = lambda x: x
m = mock.mock_open()
with mock.patch("satpy.readers.ahi_hsd.open", m, create=True):
Expand Down Expand Up @@ -184,18 +185,9 @@ def test_segment(self, fromfile, np2str):
"spare": ""}

area_def = fh.get_area_def(None)
with warnings.catch_warnings():
warnings.filterwarnings("ignore",
message=r"You will likely lose important projection information",
category=UserWarning)
proj_dict = area_def.proj_dict
a, b = proj4_radius_parameters(proj_dict)
assert a == 6378137.0
assert b == 6356752.3
assert proj_dict["h"] == 35785863.0
assert proj_dict["lon_0"] == 140.7
assert proj_dict["proj"] == "geos"
assert proj_dict["units"] == "m"
expected_crs = CRS.from_dict(dict(a=6378137.0, b=6356752.3, h= 35785863.0,
lon_0=140.7, proj="geos", units="m"))
assert area_def.crs == expected_crs
np.testing.assert_allclose(area_def.area_extent, (-5500000.035542117, -3300000.021325271,
5500000.035542117, -2200000.0142168473))

Expand Down
10 changes: 4 additions & 6 deletions satpy/tests/reader_tests/test_ahi_l2_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_startend(himl2_filename):

def test_ahi_l2_area_def(himl2_filename, caplog):
"""Test reader handles area definition correctly."""
import warnings
from pyproj import CRS

ps = "+a=6378137 +h=35785863 +lon_0=140.7 +no_defs +proj=geos +rf=298.257024882273 +type=crs +units=m +x_0=0 +y_0=0"

Expand All @@ -86,11 +86,9 @@ def test_ahi_l2_area_def(himl2_filename, caplog):
assert area_def.width == dimensions["Columns"]
assert area_def.height == dimensions["Rows"]
assert np.allclose(area_def.area_extent, exp_ext)
with warnings.catch_warnings():
warnings.filterwarnings("ignore",
message=r"You will likely lose important projection information",
category=UserWarning)
assert area_def.proj_str == ps

expected_crs = CRS(ps)
assert area_def.crs == expected_crs

# Check case where input data is incorrect size.
fh = ahil2_filehandler(himl2_filename)
Expand Down
17 changes: 4 additions & 13 deletions satpy/tests/reader_tests/test_geos_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,26 +138,17 @@ def test_get_xy_from_linecol(self):

def test_get_area_definition(self):
"""Test the retrieval of the area definition."""
import warnings

from pyresample.utils import proj4_radius_parameters
from pyproj import CRS

pdict, extent = self.make_pdict_ext(1, "N2S")
good_res = (-3000.4032785810186, -3000.4032785810186)

a_def = get_area_definition(pdict, extent)
assert a_def.area_id == pdict["a_name"]
assert a_def.resolution == good_res
with warnings.catch_warnings():
warnings.filterwarnings("ignore",
message=r"You will likely lose important projection information",
category=UserWarning)
assert a_def.proj_dict["proj"] == "geos"
assert a_def.proj_dict["units"] == "m"
a, b = proj4_radius_parameters(a_def.proj_dict)
assert a == 6378169
assert b == 6356583.8
assert a_def.proj_dict["h"] == 35785831

expected_crs = CRS(dict(proj="geos", units="m", a=6378169, b=6356583.8, h=35785831))
assert a_def.crs == expected_crs

def test_sampling_to_lfac_cfac(self):
"""Test conversion from angular sampling to line/column offset."""
Expand Down
20 changes: 5 additions & 15 deletions satpy/tests/reader_tests/test_goes_imager_hrit.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
from unittest import mock

import numpy as np
from pyresample.utils import proj4_radius_parameters
from xarray import DataArray

from satpy.readers.goes_imager_hrit import (
ALTITUDE,
EQUATOR_RADIUS,
POLE_RADIUS,
HRITGOESFileHandler,
HRITGOESPrologueFileHandler,
make_gvar_float,
Expand Down Expand Up @@ -172,7 +169,7 @@ def test_get_dataset(self, base_get_dataset):

def test_get_area_def(self):
"""Test getting the area definition."""
import warnings
from pyproj import CRS

self.reader.mda.update({
"cfac": 10216334,
Expand All @@ -186,17 +183,10 @@ def test_get_area_def(self):
resolution=3000)
area = self.reader.get_area_def(dsid)

with warnings.catch_warnings():
warnings.filterwarnings("ignore",
message=r"You will likely lose important projection information",
category=UserWarning)
a, b = proj4_radius_parameters(area.proj_dict)
assert area.proj_dict["h"] == ALTITUDE
assert area.proj_dict["lon_0"] == 100.1640625
assert area.proj_dict["proj"] == "geos"
assert area.proj_dict["units"] == "m"
assert a == EQUATOR_RADIUS
assert b == POLE_RADIUS
expected_crs = CRS(dict(h=ALTITUDE, lon_0=100.1640625, proj="geos", units="m",
rf=295.488065897001, a=6378169))
assert area.crs == expected_crs

assert area.width == 2816
assert area.height == 464
assert area.area_id == "goes-15_goes_imager_fd_3km"
Expand Down
8 changes: 2 additions & 6 deletions satpy/tests/reader_tests/test_gpm_imerg.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def tearDown(self):

def test_load_data(self):
"""Test loading data."""
import warnings
from pyproj import CRS

from satpy.readers import load_reader

Expand All @@ -132,10 +132,6 @@ def test_load_data(self):
assert res["IRprecipitation"].resolution == 0.1
assert res["IRprecipitation"].area.width == 3600
assert res["IRprecipitation"].area.height == 1800
with warnings.catch_warnings():
warnings.filterwarnings("ignore",
message=r"You will likely lose important projection information",
category=UserWarning)
assert res["IRprecipitation"].area.proj_dict == pdict
assert res["IRprecipitation"].area.crs == CRS(pdict)
np.testing.assert_almost_equal(res["IRprecipitation"].area.area_extent,
(-179.95, -89.95, 179.95, 89.95), 5)
20 changes: 5 additions & 15 deletions satpy/tests/reader_tests/test_hrit_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,23 +221,13 @@ def test_get_area_extent(self):

def test_get_area_def(self):
"""Test getting an area definition."""
import warnings

from pyresample.utils import proj4_radius_parameters
from pyproj import CRS

area = self.reader.get_area_def("VIS06")
with warnings.catch_warnings():
warnings.filterwarnings("ignore",
message=r"You will likely lose important projection information",
category=UserWarning)
proj_dict = area.proj_dict
a, b = proj4_radius_parameters(proj_dict)
assert a == 6378169.0
assert b == 6356583.8
assert proj_dict["h"] == 35785831.0
assert proj_dict["lon_0"] == 44.0
assert proj_dict["proj"] == "geos"
assert proj_dict["units"] == "m"

expected_crs = CRS(dict(proj="geos", a=6378169.0, b=6356583.8, h=35785831.0, lon_0=44.0, units="m"))
assert area.crs == expected_crs

assert area.area_extent == (-77771774058.38356, -77771774058.38356,
30310525626438.438, 3720765401003.719)

Expand Down
8 changes: 1 addition & 7 deletions satpy/tests/reader_tests/test_insat3d_img_l1b_h5.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,18 +273,12 @@ def insat_filehandler(insat_filename):

def test_filehandler_returns_area(insat_filehandler):
"""Test that filehandle returns an area."""
import warnings

fh = insat_filehandler

ds_id = make_dataid(name="MIR", resolution=4000, calibration="brightness_temperature")
area_def = fh.get_area_def(ds_id)
_ = area_def.get_lonlats(chunks=1000)
with warnings.catch_warnings():
warnings.filterwarnings("ignore",
message=r"You will likely lose important projection information",
category=UserWarning)
assert "+lon_0=" + str(subsatellite_longitude) in area_def.crs.to_proj4()
assert subsatellite_longitude == area_def.crs.to_cf()["longitude_of_projection_origin"]


def test_filehandler_has_start_and_end_time(insat_filehandler):
Expand Down
17 changes: 2 additions & 15 deletions satpy/tests/reader_tests/test_mviri_l1b_fiduceo_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import xarray as xr
from pyproj import CRS
from pyresample.geometry import AreaDefinition
from pyresample.utils import proj4_radius_parameters

from satpy.readers.mviri_l1b_fiduceo_nc import (
ALTITUDE,
Expand Down Expand Up @@ -497,22 +496,10 @@ def test_angle_cache(self, interp_tiepoints, file_handler):
def test_get_area_definition(self, file_handler, name, resolution,
area_exp):
"""Test getting area definitions."""
import warnings

dataset_id = make_dataid(name=name, resolution=resolution)
area = file_handler.get_area_def(dataset_id)
with warnings.catch_warnings():
warnings.filterwarnings("ignore",
message=r"You will likely lose important projection information",
category=UserWarning)
a, b = proj4_radius_parameters(area.proj_dict)
a_exp, b_exp = proj4_radius_parameters(area_exp.proj_dict)
assert a == a_exp
assert b == b_exp
assert area.width == area_exp.width
assert area.height == area_exp.height
for key in ["h", "lon_0", "proj", "units"]:
assert area.proj_dict[key] == area_exp.proj_dict[key]

assert area.crs == area_exp.crs
np.testing.assert_allclose(area.area_extent, area_exp.area_extent)

def test_calib_exceptions(self, file_handler):
Expand Down
13 changes: 3 additions & 10 deletions satpy/tests/reader_tests/test_nwcsaf_msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ def cut_h5_object_ref(root, attr):

def test_get_area_def(self):
"""Get the area definition."""
import warnings
from pyproj import CRS

from satpy.readers.nwcsaf_msg2013_hdf5 import Hdf5NWCSAF
from satpy.tests.utils import make_dataid
Expand All @@ -488,15 +488,8 @@ def test_get_area_def(self):
for i in range(4):
assert area_def.area_extent[i] == pytest.approx(aext_res[i], abs=1e-4)

proj_dict = AREA_DEF_DICT["proj_dict"]
with warnings.catch_warnings():
warnings.filterwarnings("ignore",
message=r"You will likely lose important projection information",
category=UserWarning)
assert proj_dict["proj"] == area_def.proj_dict["proj"]
# Not all elements passed on Appveyor, so skip testing every single element of the proj-dict:
# for key in proj_dict:
# self.assertEqual(proj_dict[key], area_def.proj_dict[key])
expected_crs = CRS(AREA_DEF_DICT["proj_dict"])
assert expected_crs == area_def.crs

assert AREA_DEF_DICT["x_size"] == area_def.width
assert AREA_DEF_DICT["y_size"] == area_def.height
Expand Down
19 changes: 7 additions & 12 deletions satpy/tests/reader_tests/test_nwcsaf_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,18 +506,13 @@ def test_get_dataset_scales_and_offsets_palette_meanings_using_other_dataset(sel


def _check_filehandler_area_def(file_handler, dsid):
import warnings

correct_h = float(PROJ["gdal_projection"].split("+h=")[-1])
correct_a = float(PROJ["gdal_projection"].split("+a=")[-1].split()[0])
with warnings.catch_warnings():
warnings.filterwarnings("ignore",
message=r"You will likely lose important projection information",
category=UserWarning)
area_definition = file_handler.get_area_def(dsid)
assert area_definition.proj_dict["h"] == correct_h
assert area_definition.proj_dict["a"] == correct_a
assert area_definition.proj_dict["units"] == "m"
from pyproj import CRS

area_definition = file_handler.get_area_def(dsid)

expected_crs = CRS(PROJ["gdal_projection"])
assert area_definition.crs == expected_crs

correct_extent = (PROJ["gdal_xgeo_up_left"],
PROJ["gdal_ygeo_low_right"],
PROJ["gdal_xgeo_low_right"],
Expand Down
1 change: 1 addition & 0 deletions satpy/tests/reader_tests/test_oceancolorcci_l3_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def test_get_area_def(self, area_exp, fake_file_dict):
warnings.filterwarnings("ignore",
message=r"You will likely lose important projection information",
category=UserWarning)
# The corresponding CRS objects do not match even if the proj dicts match, so use the dicts
assert area.proj_dict == area_exp.proj_dict

def test_bad_fname(self, fake_dataset, fake_file_dict):
Expand Down
Loading

0 comments on commit d8af768

Please sign in to comment.