Skip to content

Commit

Permalink
Remove usage of open_rasterio in sentinel 2 msi reader
Browse files Browse the repository at this point in the history
  • Loading branch information
mraspaud committed Mar 31, 2023
1 parent 0b0ec99 commit cbc498f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
11 changes: 5 additions & 6 deletions satpy/readers/msi_safe.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@
"""

import logging
import xml.etree.ElementTree as ET

import dask.array as da
import defusedxml.ElementTree as ET
import numpy as np
import rioxarray
import xarray as xr
from pyresample import geometry
from xarray import DataArray

from satpy import CHUNK_SIZE
from satpy._compat import cached_property
Expand Down Expand Up @@ -84,7 +83,7 @@ def get_dataset(self, key, info):
return proj

def _read_from_file(self, key):
proj = rioxarray.open_rasterio(self.filename, chunks=CHUNK_SIZE)
proj = xr.open_dataset(self.filename, engine="rasterio", chunks=CHUNK_SIZE)["band_data"]
proj = proj.squeeze("band")
if key["calibration"] == "reflectance":
return self._mda.calibrate_to_reflectances(proj, self._channel)
Expand Down Expand Up @@ -215,7 +214,7 @@ def physical_gains(self):

def _fill_swath_edges(angles):
"""Fill gaps at edges of swath."""
darr = DataArray(angles, dims=['y', 'x'])
darr = xr.DataArray(angles, dims=['y', 'x'])
darr = darr.bfill('x')
darr = darr.ffill('x')
darr = darr.bfill('y')
Expand Down Expand Up @@ -330,7 +329,7 @@ def get_dataset(self, key, info):

res = self.interpolate_angles(angles, key['resolution'])

proj = DataArray(res, dims=['y', 'x'])
proj = xr.DataArray(res, dims=['y', 'x'])
proj.attrs = info.copy()
proj.attrs['units'] = 'degrees'
proj.attrs['platform_name'] = self.platform_name
Expand Down
9 changes: 4 additions & 5 deletions satpy/tests/reader_tests/test_msi_safe.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# You should have received a copy of the GNU General Public License along with
# satpy. If not, see <http://www.gnu.org/licenses/>.
"""Module for testing the satpy.readers.msi_safe module."""
import unittest
import unittest.mock as mock
from io import BytesIO, StringIO

Expand Down Expand Up @@ -863,10 +862,10 @@
""" # noqa


class TestMTDXML(unittest.TestCase):
class TestMTDXML:
"""Test the SAFE MTD XML file handler."""

def setUp(self):
def setup_method(self):
"""Set up the test case."""
from satpy.readers.msi_safe import SAFEMSIMDXML, SAFEMSITileMDXML
filename_info = dict(observation_time=None, dtile_number=None, fmission_id="S2A")
Expand Down Expand Up @@ -973,7 +972,7 @@ def setup_method(self):
"""Set up the test."""
from satpy.readers.msi_safe import SAFEMSITileMDXML
self.filename_info = dict(observation_time=None, fmission_id="S2A", band_name="B01", dtile_number=None)
self.fake_data = xr.DataArray([[[0, 1], [65534, 65535]]], dims=["band", "x", "y"])
self.fake_data = xr.Dataset({"band_data": xr.DataArray([[[0, 1], [65534, 65535]]], dims=["band", "x", "y"])})
self.tile_mda = mock.create_autospec(SAFEMSITileMDXML)(BytesIO(mtd_tile_xml),
self.filename_info, mock.MagicMock())

Expand All @@ -989,6 +988,6 @@ def test_calibration_and_masking(self, mask_saturated, calibration, expected):
mask_saturated=mask_saturated)
self.jp2_fh = SAFEMSIL1C("somefile", self.filename_info, mock.MagicMock(), mda, self.tile_mda)

with mock.patch("satpy.readers.msi_safe.rioxarray.open_rasterio", return_value=self.fake_data):
with mock.patch("xarray.open_dataset", return_value=self.fake_data):
res = self.jp2_fh.get_dataset(make_dataid(name="B01", calibration=calibration), info=dict())
np.testing.assert_allclose(res, expected)

0 comments on commit cbc498f

Please sign in to comment.