Open gerritholl opened 2 years ago
This MCVE works as expected (no traceback and tags are written to GDALMetaData):
import logging
from satpy import Scene
#from satpy.utils import debug_on; debug_on()
from sattools.io import plotdir
fn = ["/media/nas/x21308/scratch/AAPP-processed/noaa18_20210122_0952_80791/hrpt_noaa18_20210122_0952_80791.l1b"]
sc = Scene(filenames=fn, reader=["avhrr_l1b_aapp"])
sc.load(["4"])
ls = sc.resample("eurol")
ls.save_datasets(filename=str(plotdir() /
"{start_time:%Y%m%d%H%M}-{platform_name}_{sensor}_{area.area_id}_{name}.tif"),
include_scale_offset=True)
Difference: my synthetic test is passing a dataset with mode LA
to stretch
, whereas in the real data case, the image still has mode L
at that point, and the alpha band only gets added in XRImage.finalize
. Apparently my synthetic data test is not realistic?
This might not occur any current datasets, but as I understand it will affect any case where a reader or compositor returns an image in mode LA to which stretching should subsequently be applied.
I'm having trouble understanding. Is this failing to write the alpha band or is it failing to write the tags?
It's failing to write the tags. It's also applying scaling to the alpha band, which is most likely not what the user wants.
First, XRImage.stretch
is applying the stretch enhancements. It is given a mode LA
input and applies scaling and offset to both layers. XRImage.stretch
dutifully documents the scale factors and offsets per band in a size-2 ndarray. Later, XRImage.rio_save
finds this documentation when collecting the tags. It expects a size-1 ndarray for offset and scale factor and converts this with .item()
, but fails when the offset and scale-factor are size-2.
The original failure here is in XRImage.stretch
, which should identify a mode LA image and apply scaling only to L; then it will also document only a scalar for offset and scale factor.
Related: in https://github.com/pytroll/trollimage/issues/84 I noted that GDAL supports per-band scale factors and offsets, which would be an alternate way to store this information in a GeoTIFF file (but I don't know what client software supports this; at least NinJo doesn't).
So if the problem is multiple factors and offsets then do RGB/A images also fail?
Yes, RGB/A images also fail. However, I think the problem is more pressing for LA images, because, as for L images, it's reasonable to map the pixel values to a physical interpretation. I have a hard time imagining that people would want to map the pixel values of an RGB/A image to a physical interpretation, however; and if they don't, then I don't know why they would need a scale factor and offset interpretation.
ok so the two things (as far as I can tell from this discussion) that need to be fixed are:
min_stretch=[0, 0]
for an LA dataset).
Describe the bug
Saving a mode LA image while storing scale and offset tags fails with
ValueError
.To Reproduce
(NB: the two versions of
save_dataset
are to accommodate for https://github.com/pytroll/trollimage/pull/85)Expected behavior
I expect an LA image to be written with scale and offset written in the metadata. @mraspaud has reported that this works (source: personal communication on pytroll slack).
Actual results
Environment Info:
v0.30.1-3-g47fb3bc3
v1.15.1-9-g7246882
Additional context
I wouldn't actually expect it to work. The scale and offset are only normally meaningful for the L-band, but the tags are global. The writer encounters two scales and offsets, for the L and the A band, tries to write those two values as a scalar, and fails. I'm curious to see why it works in Martins case.