Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add limb_correction keyword argument to MiRS reader #1621

Merged
merged 15 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 23 additions & 7 deletions satpy/readers/mirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,28 @@ def limb_correct_atms_bt(bt_data, surf_type_mask, coeff_fns, ds_info):


class MiRSL2ncHandler(BaseFileHandler):
"""MiRS handler for NetCDF4 files using xarray."""
"""MiRS handler for NetCDF4 files using xarray.

def __init__(self, filename, filename_info, filetype_info):
The MiRS retrieval algorithm runs on multiple
sensors. For the ATMS sensors, a limb correction
is applied by default. In order to change that
behavior, use the keyword argument ``limb_correction=False``::


from satpy import Scene, find_files_and_readers

filenames = find_files_and_readers(base_dir, reader="mirs")
scene = Scene(filenames, reader_kwargs={'limb_correction': False})

"""

def __init__(self, filename, filename_info, filetype_info,
limb_correction=True):
"""Init method."""
super(MiRSL2ncHandler, self).__init__(filename,
filename_info,
filetype_info)
filetype_info,
)

self.nc = xr.open_dataset(self.filename,
decode_cf=True,
Expand All @@ -212,6 +227,7 @@ def __init__(self, filename, filename_info, filetype_info):

self.platform_name = self._get_platform_name
self.sensor = self._get_sensor
self.limb_correction = limb_correction

@property
def platform_shortname(self):
Expand Down Expand Up @@ -346,15 +362,15 @@ def get_dataset(self, ds_id, ds_info):
data = self['BT']
data = data.rename(new_name_or_name_dict=ds_info["name"])

if self.sensor.lower() != "atms":
LOG.info("Limb Correction will not be applied to non-ATMS BTs")
data = data[:, :, idx]
else:
if self.sensor.lower() == "atms" and self.limb_correction:
sfc_type_mask = self['Sfc_type']
data = limb_correct_atms_bt(data, sfc_type_mask,
self._get_coeff_filenames,
ds_info)
self.nc = self.nc.merge(data)
else:
LOG.info("No Limb Correction applied.")
data = data[:, :, idx]
else:
data = self[ds_id['name']]

Expand Down
39 changes: 37 additions & 2 deletions satpy/tests/reader_tests/test_mirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

AWIPS_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"
N20_MIRS_L2_SWATH = "NPR-MIRS-IMG_v11r4_n20_s201702061601000_e201702061607000_c202012201658410.nc"
OTHER_MIRS_L2_SWATH = "NPR-MIRS-IMG_v11r4_gpm_s201702061601000_e201702061607000_c202010080001310.nc"

EXAMPLE_FILES = [AWIPS_FILE, NPP_MIRS_L2_SWATH, OTHER_MIRS_L2_SWATH]
Expand Down Expand Up @@ -82,7 +83,7 @@ def fake_coeff_from_fn(fn):
return coeff_str


def _get_datasets_with_attributes():
def _get_datasets_with_attributes(**kwargs):
"""Represent files with two resolution of variables in them (ex. OCEAN)."""
bt = xr.DataArray(np.linspace(1830, 3930, N_SCANLINE * N_FOV * N_CHANNEL).
reshape(N_SCANLINE, N_FOV, N_CHANNEL),
Expand All @@ -91,7 +92,8 @@ def _get_datasets_with_attributes():
'coordinates': "Longitude Latitude Freq",
'scale_factor': 0.01,
'_FillValue': -999,
'valid_range': [0, 50000]})
'valid_range': [0, 50000]},
dims=('Scanline', 'Field_of_view', 'Channel'))
rr = xr.DataArray(np.random.randint(100, 500, size=(N_SCANLINE, N_FOV)),
attrs={'long_name': "Rain Rate (mm/hr)",
'units': "mm/hr",
Expand Down Expand Up @@ -259,6 +261,7 @@ def _check_attrs(data_arr, platform_name):
[
([AWIPS_FILE], TEST_VARS, "metop-a"),
([NPP_MIRS_L2_SWATH], TEST_VARS, "npp"),
([N20_MIRS_L2_SWATH], TEST_VARS, "noaa-20"),
([OTHER_MIRS_L2_SWATH], TEST_VARS, "gpm"),
]
)
Expand All @@ -275,8 +278,40 @@ def test_basic_load(self, filenames, loadable_ids, platform_name):
fd.side_effect = fake_coeff_from_fn
loaded_data_arrs = r.load(loadable_ids)
assert loaded_data_arrs

for data_id, data_arr in loaded_data_arrs.items():
if data_id['name'] not in ['latitude', 'longitude']:
self._check_area(data_arr)
self._check_fill(data_arr)
self._check_attrs(data_arr, platform_name)

if data_arr.attrs['sensor'] == 'atms':
fd.assert_called()
else:
fd.assert_not_called()

@pytest.mark.parametrize(
("filenames", "loadable_ids"),
[
([AWIPS_FILE], TEST_VARS),
([NPP_MIRS_L2_SWATH], TEST_VARS),
([N20_MIRS_L2_SWATH], TEST_VARS),
([OTHER_MIRS_L2_SWATH], TEST_VARS),
]
)
def test_kwarg_load(self, filenames, loadable_ids):
"""Test the limb_correction kwarg when filehandler is loaded."""
from satpy.readers import load_reader
with mock.patch('satpy.readers.mirs.xr.open_dataset') as od:
od.side_effect = fake_open_dataset
r = load_reader(self.reader_configs)
loadables = r.select_files_from_pathnames(filenames)
r.create_filehandlers(loadables, fh_kwargs={"limb_correction": False})

with mock.patch('satpy.readers.mirs.read_atms_coeff_to_string') as \
fd, mock.patch('satpy.readers.mirs.retrieve'):
fd.side_effect = fake_coeff_from_fn
loaded_data_arrs = r.load(loadable_ids)
assert loaded_data_arrs

fd.assert_not_called()