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

Save geotiff with keep_palette still add an alpha band #2143

Closed
steph-ben opened this issue Jul 11, 2022 · 3 comments
Closed

Save geotiff with keep_palette still add an alpha band #2143

steph-ben opened this issue Jul 11, 2022 · 3 comments

Comments

@steph-ben
Copy link

Describe the bug

When saving an image with the code below, I expect only one band to be written on my Tiff file.
However, at one point my XRImage has mode = "L", and thus not respecting my "keep_palette" argument ..

Any thoughs on this ?

Might be related to #640

To Reproduce

            crop_channel.save_dataset(channel,
                                      writer='geotiff', filename=str(fp), 
                                      # Add our metadata
                                      tags=tags,
                                      # Ensure data type
                                      dtype=np.uint8, floating_point=False,
                                      # Don't transform our data
                                      enhance=False, keep_palette=True,
                                      # Write compressed
                                      num_threads=8, COMPRESS="LZW")
            crop_channel = None

My output looks like :

$ gdalinfo tmp/plop/himawari_B01_20220708_090000.tiff 
Driver: GTiff/GeoTIFF
Files: tmp/plop/himawari_B01_20220708_090000.tiff
Size is 2800, 1600
...
  name=B01
  platform_name=Himawari-8
  sensor=ahi
...
Band 1 Block=256x256 Type=Byte, ColorInterp=Gray
  Mask Flags: PER_DATASET ALPHA 
Band 2 Block=256x256 Type=Byte, ColorInterp=Alpha   <<<<<<------------ I don't want this

By following step by step, I got to the point that :

keep_palette = True
self.mode = "L"

and therefore still adding an extra band

Environment Info:

  • OS : WSL Debian
  • Python 3.9.13
  • satpy=0.36.0
@djhoese
Copy link
Member

djhoese commented Jul 11, 2022

Side note: I see you have floating_point=False, I think that flag has been deprecated for a while and could be removed.

What composite/product are you saving? I see AHI B01 in the geotiff metadata. For keep_palette to work, you need to enhance your data with the palettize operation. This applies a colormap to your data, but instead of converting it to RGB/A your data becomes the index in the colormap for the assigned color. Unless you are doing this yourself outside of save_dataset then there is no palette for trollimage/satpy to "keep". The if statement you pointed to in xrimage.py is doing this check by saying "Does the user want to save the palette/colormap and we have a P(alette) mode image?".

This is sort of described here:

https://github.com/pytroll/trollimage/blob/00c14a660d1be4d21c5be413c9b7d799a0ba5d0f/trollimage/xrimage.py#L1101-L1107

If you are not worried about colormaps and you just want an L image with no Alpha band then you could specify instead fill_value=0 which would keep your image as L but replace all NaN or invalid pixels with 0 (black).

This is described in multiple places for better or worse, but probably in most detail here:

https://github.com/pytroll/trollimage/blob/00c14a660d1be4d21c5be413c9b7d799a0ba5d0f/trollimage/xrimage.py#L1085-L1097

@steph-ben
Copy link
Author

Thanks a lot, I think fill_value=0 was exactly what I missed !

Actually in my processing I do something like that :

scn = Scene(reader='ahi_hsd', filenames=files)
...
ds = scn[channel]
dataset_scaled = xr.apply_ufunc(scaling_func, ds)
...
crop_channel.save_dataset(channel, writer='geotiff', filename=str(fp), fill_value=0, ...)

So yes I'm already converting my data from albedo values to pixel, following an internal mapping.
My output needed to follow some specific Tiff convention, and the two bands were causing errors.

PS : Many thanks for the quick answer and for this amazing lib ! Automatic generation of pre-defined composite is really a killer

@djhoese
Copy link
Member

djhoese commented Jul 12, 2022

Glad that worked! Let us know if you have any problems and thanks for the kind words.

@djhoese djhoese closed this as completed Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants