Skip to content

Commit

Permalink
Fix failing case
Browse files Browse the repository at this point in the history
  • Loading branch information
mraspaud committed Mar 26, 2024
1 parent 23e150c commit 3aa2f79
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion satpy/readers/olci_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(self, value, flag_list=None):
try:
meanings = value.attrs["flag_meanings"].split()
masks = value.attrs["flag_masks"]
except AttributeError:
except (AttributeError, KeyError):
meanings = ["INVALID", "WATER", "LAND", "CLOUD", "SNOW_ICE",
"INLAND_WATER", "TIDAL", "COSMETIC", "SUSPECT",
"HISOLZEN", "SATURATED", "MEGLINT", "HIGHGLINT",
Expand Down
34 changes: 33 additions & 1 deletion satpy/tests/reader_tests/test_olci_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def test_bitflags(self):
False])
assert all(mask == expected)

def test_bitflags_with_non_linear_meanings(self):
def test_bitflags_with_flags_from_array(self):
"""Test reading bitflags from DataArray attributes."""
from functools import reduce

Expand Down Expand Up @@ -310,6 +310,38 @@ def test_bitflags_with_non_linear_meanings(self):
assert mask[22].item() is True
assert any(mask[23:]) is False

def test_bitflags_with_dataarray_without_flags(self):
"""Test the BitFlags class."""
from functools import reduce

import numpy as np
import xarray as xr

from satpy.readers.olci_nc import BitFlags
flag_list = ["INVALID", "WATER", "LAND", "CLOUD", "SNOW_ICE",
"INLAND_WATER", "TIDAL", "COSMETIC", "SUSPECT", "HISOLZEN",
"SATURATED", "MEGLINT", "HIGHGLINT", "WHITECAPS",
"ADJAC", "WV_FAIL", "PAR_FAIL", "AC_FAIL", "OC4ME_FAIL",
"OCNN_FAIL", "Extra_1", "KDM_FAIL", "Extra_2",
"CLOUD_AMBIGUOUS", "CLOUD_MARGIN", "BPAC_ON",
"WHITE_SCATT", "LOWRW", "HIGHRW"]

bits = np.array([1 << x for x in range(len(flag_list))])

bflags = BitFlags(xr.DataArray(bits))

items = ["INVALID", "SNOW_ICE", "INLAND_WATER", "SUSPECT",
"AC_FAIL", "CLOUD", "HISOLZEN", "OCNN_FAIL",
"CLOUD_MARGIN", "CLOUD_AMBIGUOUS", "LOWRW", "LAND"]

mask = reduce(np.logical_or, [bflags[item] for item in items])
expected = np.array([True, False, True, True, True, True, False,
False, True, True, False, False, False, False,
False, False, False, True, False, True, False,
False, False, True, True, False, False, True,
False])
assert all(mask == expected)


def test_bitflags_with_custom_flag_list(self):
"""Test the BitFlags class providing a flag list."""
Expand Down

0 comments on commit 3aa2f79

Please sign in to comment.