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

How to create a multi-band geotiff? #2698

Open
swdmike opened this issue Dec 18, 2023 · 7 comments
Open

How to create a multi-band geotiff? #2698

swdmike opened this issue Dec 18, 2023 · 7 comments

Comments

@swdmike
Copy link

swdmike commented Dec 18, 2023

Describe the bug
Sorry to ask, does scene.save_datasets support multi band geotiff? The result contains only one band. My data is Himawari HSD.
Well, true color tif is all right.

scene = Scene(reader="ahi_hsd", filenames=filelist)
scene.load(scene.available_dataset_ids())
scene.load(["true_color"], generate=False)
scene = scene.resample(create_area_def)
scene.save_datasets(filename=dest_path1, datasets=["B07", "B14"], writer="geotiff", enhance=False)
scene.save_datasets(filename=dest_path2, datasets=['true_color'], writer="geotiff")
@gerritholl
Copy link
Collaborator

gerritholl commented Dec 18, 2023

The geotiff writer writes one dataset per file. That could be a single band (mode L or LA) or a composite (mode RGB/RGBA) such as true_color. It is not possible to write multiple datasets to a single file with the geotiff writer.

There is also the mitiff writer, which I think can write multiple bands to a single file, but I think those files are for dedicated software called diana and not general geotiffs. https://satpy.readthedocs.io/en/stable/api/satpy.writers.mitiff.html.

@mraspaud
Copy link
Member

@swdmike how many bands would you like in your geotiff? Can geotiff hold an arbitrary number of bands?

@pnuu
Copy link
Member

pnuu commented Dec 18, 2023

Geotiff can hold "many" bands. In a project I've created 7-band files using gdal_translate. Unfortunately I don't have the code at hand, but IIRC it was pretty simple. Don't know how much work it would require the put the functionality to Satpy/Trollimage, though.

@djhoese
Copy link
Member

djhoese commented Dec 19, 2023

If this is a strongly requested feature (I know @simonrp84 has wanted to do this in the past and likely uses GDAL post-satpy to do it) then I think a separate writer based on the existing Geotiff writer could be created. It could/should enhance all the datasets provided to it, join them together along some dimension, then give it to trollimage XRImage. I think that would just work. However:

  1. How do you handle a mixture of single band images and RGB/As? Do you concatenate the single band products along the "bands" dimension? So your result is something like an RGBLLLRGBL combination of bands?
  2. XRImage might get confused or error out if you have a "bands" dimension that isn't an understood image mode because it will try to assign certain geotiff properties based on the mode of the image.
  3. Some metadata might need special handling. Like if the output file name includes {name} in the format pattern then you'll probably want to take all the joined products and do "-".join([x.attrs["name"] for x in datasets]).

This is off the top of my head.

@gerritholl
Copy link
Collaborator

gerritholl commented Dec 19, 2023

  1. How do you handle a mixture of single band images and RGB/As?

You don't. Only mode L can be stored in such files (maybe mode P).

@djhoese
Copy link
Member

djhoese commented Dec 19, 2023

How do you handle a mixture of single band images and RGB/As?

You don't. Only mode L can be stored in such files (maybe mode P).

Are you telling me it isn't possible or are you saying this is your suggestion for the solution? If this is your suggestion, then I'd argue shouldn't we let the users decide how to parse out the bands of the file in whatever application they have? We're already talking about non-standard geotiffs (non-standard in the sense that they aren't normal image modes).

@gerritholl
Copy link
Collaborator

How do you handle a mixture of single band images and RGB/As?

You don't. Only mode L can be stored in such files (maybe mode P).

Are you telling me it isn't possible or are you saying this is your suggestion for the solution?

I thought it wasn't possible, but looking again, I'm not so sure any more.

Note that we already support storing multiple RGBs in a single TIFF file, using the overviews option. But overviews is just a special case for storing multiple images in a file.

Similarly, as I understand it, an image of mode RGB, RGBA, or LA is a special case for storing multiple bands in a single image, and that this interpretation is only given in this case. I think that soon as more than 4 bands are stored in an image, TIFF no longer has this way to tell how images should be interpreted, which arguably means that image modes are not supported at all. The user can interpret and process the data how they want.

Someone storing multiple RGBs in a single file might be better off storing multiple images in a file rather than multiple bands in a single image, but in both cases, Satpy only supports the special case (overviews for multiple images, or LA/RGB/RGBA for multiple bands).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants