Skip to content

Commit

Permalink
fix typo, use context manager when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
zebengberg committed Nov 4, 2024
1 parent 9a8b851 commit 1b3b9b2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
13 changes: 5 additions & 8 deletions pycontrails/datalib/landsat.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class Landsat:
are used. Bands must share a common resolution. The resolutions of each band are:
- B1-B7, B9: 30 m
- B9: 15 m
- B8: 15 m
- B10, B11: 30 m (upsampled from true resolution of 100 m)
cachestore : cache.CacheStore, optional
Expand Down Expand Up @@ -291,9 +291,7 @@ def _check_band_resolution(bands: set[str]) -> None:
there are two valid cases: only band 8, or any bands except band 8.
"""
groups = [
{
"B8",
}, # 15 m
{"B8"}, # 15 m
{f"B{i}" for i in range(1, 12) if i != 8}, # 30 m
]
if not any(bands.issubset(group) for group in groups):
Expand All @@ -313,10 +311,9 @@ def _read(path: str, meta: str, band: str, processing: str) -> xr.DataArray:
pycontrails_optional_package="sat",
)

src = rasterio.open(path)
img = src.read(1)
crs = pyproj.CRS.from_epsg(src.crs.to_epsg())
src.close()
with rasterio.open(path) as src:
img = src.read(1)
crs = pyproj.CRS.from_epsg(src.crs.to_epsg())

if processing == "reflectance":
mult, add = _read_band_reflectance_rescaling(meta, band)
Expand Down
18 changes: 7 additions & 11 deletions pycontrails/datalib/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,8 @@ def _check_band_resolution(bands: set[str]) -> None:
def _read(path: str, granule_meta: str, safe_meta: str, band: str, processing: str) -> xr.DataArray:
"""Read imagery data from Sentinel-2 files."""
Image.MAX_IMAGE_PIXELS = None # avoid decompression bomb warning
src = Image.open(path)
img = np.asarray(src)
src.close()
with Image.open(path) as src:
img = np.asarray(src)

if processing == "reflectance":
gain, offset = _read_band_reflectance_rescaling(safe_meta, band)
Expand Down Expand Up @@ -357,10 +356,9 @@ def _band_id(band: str) -> int:
"""Get band ID used in some metadata files."""
if band in (f"B{i:2d}" for i in range(1, 9)):
return int(band[1:]) - 1
elif band == "B8A":
if band == "B8A":
return 8
else:
return int(band[1:])
return int(band[1:])


def _read_band_reflectance_rescaling(meta: str, band: str) -> tuple[float, float]:
Expand Down Expand Up @@ -389,12 +387,10 @@ def _read_band_reflectance_rescaling(meta: str, band: str) -> tuple[float, float
for elem in elems:
if int(elem.attrib["band_id"]) == band_id and elem.text is not None:
offset = float(elem.text)
break
else:
msg = f"Could not find reflectance offset for band {band} (band ID {band_id})"
raise ValueError(msg)
return gain, offset

return gain, offset
msg = f"Could not find reflectance offset for band {band} (band ID {band_id})"
raise ValueError(msg)


def _read_image_coordinates(meta: str, band: str) -> tuple[np.ndarray, np.ndarray]:
Expand Down

0 comments on commit 1b3b9b2

Please sign in to comment.