Skip to content

Commit

Permalink
Force scale/offse to 1/0 for ninjogeotiff mode p
Browse files Browse the repository at this point in the history
For ninjogeotiff mode p, force scale/offset to be 1 and 0, respectively.

As a workaround for pytroll#2300, do not
check image.mode, but use image.data.attrs["mode"] instead.
  • Loading branch information
gerritholl committed Aug 8, 2024
1 parent 3181a80 commit cb4f646
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions satpy/writers/ninjogeotiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,18 @@ def save_image( # noqa: D417
overviews_minsize=overviews_minsize,
overviews_resampling=overviews_resampling,
tags={**(tags or {}), **ninjo_tags},
scale_offset_tags=(self.scale_offset_tag_names
if self._check_include_scale_offset(image, PhysicUnit)
else None),
scale_offset_tags=self._get_scale_offset_tags(image, PhysicUnit),
**gdal_opts)

def _get_scale_offset_tags(self, image, unit):
"""Get scale offset tags (tuple or dict)."""
if self._check_include_scale_offset(image, unit):
# image.mode cannot be trusted https://github.com/pytroll/satpy/issues/2300
if image.data.attrs["mode"][0] == "P":
return dict(zip(self.scale_offset_tag_names, (1, 0)))
return self.scale_offset_tag_names
return None # explicit is better than implicit

def _fix_units(self, image, quantity, unit):
"""Adapt units between °C and K.
Expand Down Expand Up @@ -238,7 +245,7 @@ def _fix_units(self, image, quantity, unit):

def _check_include_scale_offset(self, image, unit):
"""Check if scale-offset tags should be included."""
if image.mode[0] in "LP" and unit.lower() not in ("n/a", "1", ""):
if image.data.attrs["mode"][0] in "LP" and unit.lower() not in ("n/a", "1", ""):
return True
return False

Expand Down Expand Up @@ -380,16 +387,17 @@ def get_central_meridian(self):

def get_color_depth(self):
"""Return the color depth."""
if self.image.mode in ("L", "P"):
# image.mode cannot be trusted https://github.com/pytroll/satpy/issues/2300
if self.image.data.attrs["mode"] in ("L", "P"):
return 8
if self.image.mode in ("LA", "PA"):
if self.image.data.attrs["mode"] in ("LA", "PA"):
return 16
if self.image.mode == "RGB":
if self.image.data.attrs["mode"] == "RGB":
return 24
if self.image.mode == "RGBA":
if self.image.data.attrs["mode"] == "RGBA":
return 32
raise ValueError(
f"Unsupported image mode: {self.image.mode:s}")
f"Unsupported image mode: {self.image.data.attrs['mode']:s}")

# Set unix epoch here explicitly, because datetime.timestamp() is
# apparently not supported on Windows.
Expand Down

0 comments on commit cb4f646

Please sign in to comment.